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