A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
mixed-global-routing.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 */
5
6// This script exercises global routing code in a mixed point-to-point
7// and csma/cd environment
8//
9// Network topology
10//
11// n0
12// \ p-p
13// \ (shared csma/cd)
14// n2 -------------------------n3
15// / | |
16// / p-p n4 n5 ---------- n6
17// n1 p-p
18//
19// - CBR/UDP flows from n0 to n6
20// - Tracing of queues and packet receptions to file "mixed-global-routing.tr"
21
22#include "ns3/applications-module.h"
23#include "ns3/core-module.h"
24#include "ns3/csma-module.h"
25#include "ns3/internet-module.h"
26#include "ns3/network-module.h"
27#include "ns3/point-to-point-module.h"
28
29#include <cassert>
30#include <fstream>
31#include <iostream>
32#include <string>
33
34using namespace ns3;
35
36NS_LOG_COMPONENT_DEFINE("MixedGlobalRoutingExample");
37
38int
39main(int argc, char* argv[])
40{
41 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(210));
42 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("448kb/s"));
43
44 // Allow the user to override any of the defaults and the above
45 // Bind ()s at run-time, via command-line arguments
46 CommandLine cmd(__FILE__);
47 cmd.Parse(argc, argv);
48
49 NS_LOG_INFO("Create nodes.");
51 c.Create(7);
54 NodeContainer n5n6 = NodeContainer(c.Get(5), c.Get(6));
55 NodeContainer n2345 = NodeContainer(c.Get(2), c.Get(3), c.Get(4), c.Get(5));
56
58 internet.Install(c);
59
60 // We create the channels first without any IP addressing information
61 NS_LOG_INFO("Create channels.");
63 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
64 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
65 NetDeviceContainer d0d2 = p2p.Install(n0n2);
66
67 NetDeviceContainer d1d2 = p2p.Install(n1n2);
68
69 p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
70 p2p.SetChannelAttribute("Delay", StringValue("10ms"));
71 NetDeviceContainer d5d6 = p2p.Install(n5n6);
72
73 // We create the channels first without any IP addressing information
75 csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
76 csma.SetChannelAttribute("Delay", StringValue("2ms"));
77 NetDeviceContainer d2345 = csma.Install(n2345);
78
79 // Later, we add IP addresses.
80 NS_LOG_INFO("Assign IP Addresses.");
82 ipv4.SetBase("10.1.1.0", "255.255.255.0");
83 ipv4.Assign(d0d2);
84
85 ipv4.SetBase("10.1.2.0", "255.255.255.0");
86 ipv4.Assign(d1d2);
87
88 ipv4.SetBase("10.1.3.0", "255.255.255.0");
89 Ipv4InterfaceContainer i5i6 = ipv4.Assign(d5d6);
90
91 ipv4.SetBase("10.250.1.0", "255.255.255.0");
92 ipv4.Assign(d2345);
93
94 // Create router nodes, initialize routing database and set up the routing
95 // tables in the nodes.
97
98 // Create the OnOff application to send UDP datagrams of size
99 // 210 bytes at a rate of 448 Kb/s
100 NS_LOG_INFO("Create Applications.");
101 uint16_t port = 9; // Discard port (RFC 863)
102 OnOffHelper onoff("ns3::UdpSocketFactory", InetSocketAddress(i5i6.GetAddress(1), port));
103 onoff.SetConstantRate(DataRate("300bps"));
104 onoff.SetAttribute("PacketSize", UintegerValue(50));
105
106 ApplicationContainer apps = onoff.Install(c.Get(0));
107 apps.Start(Seconds(1.0));
108 apps.Stop(Seconds(10.0));
109
110 AsciiTraceHelper ascii;
111 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("mixed-global-routing.tr");
112 p2p.EnableAsciiAll(stream);
113 csma.EnableAsciiAll(stream);
114
115 p2p.EnablePcapAll("mixed-global-routing");
116 csma.EnablePcapAll("mixed-global-routing", false);
117
118 NS_LOG_INFO("Run Simulation.");
121 NS_LOG_INFO("Done.");
122
123 return 0;
124}
NodeContainer n1n2
Nodecontainer n1 + n2.
NodeContainer n0n2
Nodecontainer n0 + n2.
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.