A Discrete-Event Network Simulator
API
simple-global-routing.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  */
16 
17 //
18 // Network topology
19 //
20 // n0
21 // \ 5 Mb/s, 2ms
22 // \ 1.5Mb/s, 10ms
23 // n2 -------------------------n3
24 // /
25 // / 5 Mb/s, 2ms
26 // n1
27 //
28 // - all links are point-to-point links with indicated one-way BW/delay
29 // - CBR/UDP flows from n0 to n3, and from n3 to n1
30 // - FTP/TCP flow from n0 to n3, starting at time 1.2 to time 1.35 sec.
31 // - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
32 // (i.e., DataRate of 448,000 bps)
33 // - DropTail queues
34 // - Tracing of queues and packet receptions to file "simple-global-routing.tr"
35 
36 #include "ns3/applications-module.h"
37 #include "ns3/core-module.h"
38 #include "ns3/flow-monitor-helper.h"
39 #include "ns3/internet-module.h"
40 #include "ns3/ipv4-global-routing-helper.h"
41 #include "ns3/network-module.h"
42 #include "ns3/point-to-point-module.h"
43 
44 #include <cassert>
45 #include <fstream>
46 #include <iostream>
47 #include <string>
48 
49 using namespace ns3;
50 
51 NS_LOG_COMPONENT_DEFINE("SimpleGlobalRoutingExample");
52 
53 int
54 main(int argc, char* argv[])
55 {
56  // Users may find it convenient to turn on explicit debugging
57  // for selected modules; the below lines suggest how to do this
58 #if 0
59  LogComponentEnable ("SimpleGlobalRoutingExample", LOG_LEVEL_INFO);
60 #endif
61 
62  // Set up some default values for the simulation. Use the
63  Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(210));
64  Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("448kb/s"));
65 
66  // DefaultValue::Bind ("DropTailQueue::m_maxPackets", 30);
67 
68  // Allow the user to override any of the defaults and the above
69  // DefaultValue::Bind ()s at run-time, via command-line arguments
70  CommandLine cmd(__FILE__);
71  bool enableFlowMonitor = false;
72  cmd.AddValue("EnableMonitor", "Enable Flow Monitor", enableFlowMonitor);
73  cmd.Parse(argc, argv);
74 
75  // Here, we will explicitly create four nodes. In more sophisticated
76  // topologies, we could configure a node factory.
77  NS_LOG_INFO("Create nodes.");
78  NodeContainer c;
79  c.Create(4);
80  NodeContainer n0n2 = NodeContainer(c.Get(0), c.Get(2));
81  NodeContainer n1n2 = NodeContainer(c.Get(1), c.Get(2));
82  NodeContainer n3n2 = NodeContainer(c.Get(3), c.Get(2));
83 
85  internet.Install(c);
86 
87  // We create the channels first without any IP addressing information
88  NS_LOG_INFO("Create channels.");
90  p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
91  p2p.SetChannelAttribute("Delay", StringValue("2ms"));
92  NetDeviceContainer d0d2 = p2p.Install(n0n2);
93 
94  NetDeviceContainer d1d2 = p2p.Install(n1n2);
95 
96  p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
97  p2p.SetChannelAttribute("Delay", StringValue("10ms"));
98  NetDeviceContainer d3d2 = p2p.Install(n3n2);
99 
100  // Later, we add IP addresses.
101  NS_LOG_INFO("Assign IP Addresses.");
103  ipv4.SetBase("10.1.1.0", "255.255.255.0");
104  Ipv4InterfaceContainer i0i2 = ipv4.Assign(d0d2);
105 
106  ipv4.SetBase("10.1.2.0", "255.255.255.0");
107  Ipv4InterfaceContainer i1i2 = ipv4.Assign(d1d2);
108 
109  ipv4.SetBase("10.1.3.0", "255.255.255.0");
110  Ipv4InterfaceContainer i3i2 = ipv4.Assign(d3d2);
111 
112  // Create router nodes, initialize routing database and set up the routing
113  // tables in the nodes.
115 
116  // Create the OnOff application to send UDP datagrams of size
117  // 210 bytes at a rate of 448 Kb/s
118  NS_LOG_INFO("Create Applications.");
119  uint16_t port = 9; // Discard port (RFC 863)
120  OnOffHelper onoff("ns3::UdpSocketFactory",
122  onoff.SetConstantRate(DataRate("448kb/s"));
123  ApplicationContainer apps = onoff.Install(c.Get(0));
124  apps.Start(Seconds(1.0));
125  apps.Stop(Seconds(10.0));
126 
127  // Create a packet sink to receive these packets
128  PacketSinkHelper sink("ns3::UdpSocketFactory",
130  apps = sink.Install(c.Get(3));
131  apps.Start(Seconds(1.0));
132  apps.Stop(Seconds(10.0));
133 
134  // Create a similar flow from n3 to n1, starting at time 1.1 seconds
135  onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(i1i2.GetAddress(0), port)));
136  apps = onoff.Install(c.Get(3));
137  apps.Start(Seconds(1.1));
138  apps.Stop(Seconds(10.0));
139 
140  // Create a packet sink to receive these packets
141  apps = sink.Install(c.Get(1));
142  apps.Start(Seconds(1.1));
143  apps.Stop(Seconds(10.0));
144 
145  AsciiTraceHelper ascii;
146  p2p.EnableAsciiAll(ascii.CreateFileStream("simple-global-routing.tr"));
147  p2p.EnablePcapAll("simple-global-routing");
148 
149  // Flow Monitor
150  FlowMonitorHelper flowmonHelper;
151  if (enableFlowMonitor)
152  {
153  flowmonHelper.InstallAll();
154  }
155 
156  NS_LOG_INFO("Run Simulation.");
158  Simulator::Run();
159  NS_LOG_INFO("Done.");
160 
161  if (enableFlowMonitor)
162  {
163  flowmonHelper.SerializeToXmlFile("simple-global-routing.flowmon", false, false);
164  }
165 
167  return 0;
168 }
Ipv4InterfaceContainer i0i2
IPv4 interface container i0 + i2.
NodeContainer n1n2
Nodecontainer n1 + n2.
Ipv4InterfaceContainer i1i2
IPv4 interface container i1 + i2.
NodeContainer n0n2
Nodecontainer n0 + n2.
a polymophic address class
Definition: address.h:101
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.
Definition: trace-helper.h:174
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.
Definition: command-line.h:232
Helper to enable IP flow monitoring on a set of Nodes.
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
void SerializeToXmlFile(std::string fileName, bool enableHistograms, bool enableProbes)
Same as SerializeToXmlStream, but writes to a file instead.
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 Ipv4Address GetAny()
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.
Definition: on-off-helper.h:44
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
cmd
Definition: second.py:40
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55