A Discrete-Event Network Simulator
API
dynamic-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  * Contributed by: Luis Cortes (cortes@gatech.edu)
17  */
18 
19 
20 // This script exercises global routing code in a mixed point-to-point
21 // and csma/cd environment. We bring up and down interfaces and observe
22 // the effect on global routing. We explicitly enable the attribute
23 // to respond to interface events, so that routes are recomputed
24 // automatically.
25 //
26 // Network topology
27 //
28 // n0
29 // \ p-p
30 // \ (shared csma/cd)
31 // n2 -------------------------n3
32 // / | |
33 // / p-p n4 n5 ---------- n6
34 // n1 p-p
35 // | |
36 // ----------------------------------------
37 // p-p
38 //
39 // - at time 1 CBR/UDP flow from n1 to n6's IP address on the n5/n6 link
40 // - at time 10, start similar flow from n1 to n6's address on the n1/n6 link
41 //
42 // Order of events
43 // At pre-simulation time, configure global routes. Shortest path from
44 // n1 to n6 is via the direct point-to-point link
45 // At time 1s, start CBR traffic flow from n1 to n6
46 // At time 2s, set the n1 point-to-point interface to down. Packets
47 // will be diverted to the n1-n2-n5-n6 path
48 // At time 4s, re-enable the n1/n6 interface to up. n1-n6 route restored.
49 // At time 6s, set the n6-n1 point-to-point Ipv4 interface to down (note, this
50 // keeps the point-to-point link "up" from n1's perspective). Traffic will
51 // flow through the path n1-n2-n5-n6
52 // At time 8s, bring the interface back up. Path n1-n6 is restored
53 // At time 10s, stop the first flow.
54 // At time 11s, start a new flow, but to n6's other IP address (the one
55 // on the n1/n6 p2p link)
56 // At time 12s, bring the n1 interface down between n1 and n6. Packets
57 // will be diverted to the alternate path
58 // At time 14s, re-enable the n1/n6 interface to up. This will change
59 // routing back to n1-n6 since the interface up notification will cause
60 // a new local interface route, at higher priority than global routing
61 // At time 16s, stop the second flow.
62 
63 // - Tracing of queues and packet receptions to file "dynamic-global-routing.tr"
64 
65 #include <iostream>
66 #include <fstream>
67 #include <string>
68 #include <cassert>
69 
70 #include "ns3/core-module.h"
71 #include "ns3/network-module.h"
72 #include "ns3/csma-module.h"
73 #include "ns3/internet-module.h"
74 #include "ns3/point-to-point-module.h"
75 #include "ns3/applications-module.h"
76 #include "ns3/ipv4-global-routing-helper.h"
77 
78 using namespace ns3;
79 
80 NS_LOG_COMPONENT_DEFINE ("DynamicGlobalRoutingExample");
81 
82 int
83 main (int argc, char *argv[])
84 {
85  // The below value configures the default behavior of global routing.
86  // By default, it is disabled. To respond to interface events, set to true
87  Config::SetDefault ("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue (true));
88 
89  // Allow the user to override any of the defaults and the above
90  // Bind ()s at run-time, via command-line arguments
91  CommandLine cmd (__FILE__);
92  cmd.Parse (argc, argv);
93 
94  NS_LOG_INFO ("Create nodes.");
95  NodeContainer c;
96  c.Create (7);
97  NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
98  NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
99  NodeContainer n5n6 = NodeContainer (c.Get (5), c.Get (6));
100  NodeContainer n1n6 = NodeContainer (c.Get (1), c.Get (6));
101  NodeContainer n2345 = NodeContainer (c.Get (2), c.Get (3), c.Get (4), c.Get (5));
102 
103  InternetStackHelper internet;
104  internet.Install (c);
105 
106  // We create the channels first without any IP addressing information
107  NS_LOG_INFO ("Create channels.");
108  PointToPointHelper p2p;
109  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
110  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
111  NetDeviceContainer d0d2 = p2p.Install (n0n2);
112  NetDeviceContainer d1d6 = p2p.Install (n1n6);
113 
114  NetDeviceContainer d1d2 = p2p.Install (n1n2);
115 
116  p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
117  p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
118  NetDeviceContainer d5d6 = p2p.Install (n5n6);
119 
120  // We create the channels first without any IP addressing information
122  csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
123  csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
124  NetDeviceContainer d2345 = csma.Install (n2345);
125 
126  // Later, we add IP addresses.
127  NS_LOG_INFO ("Assign IP Addresses.");
128  Ipv4AddressHelper ipv4;
129  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
130  ipv4.Assign (d0d2);
131 
132  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
133  ipv4.Assign (d1d2);
134 
135  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
136  Ipv4InterfaceContainer i5i6 = ipv4.Assign (d5d6);
137 
138  ipv4.SetBase ("10.250.1.0", "255.255.255.0");
139  ipv4.Assign (d2345);
140 
141  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
142  Ipv4InterfaceContainer i1i6 = ipv4.Assign (d1d6);
143 
144  // Create router nodes, initialize routing database and set up the routing
145  // tables in the nodes.
147 
148  // Create the OnOff application to send UDP datagrams of size
149  // 210 bytes at a rate of 448 Kb/s
150  NS_LOG_INFO ("Create Applications.");
151  uint16_t port = 9; // Discard port (RFC 863)
152  OnOffHelper onoff ("ns3::UdpSocketFactory",
153  InetSocketAddress (i5i6.GetAddress (1), port));
154  onoff.SetConstantRate (DataRate ("2kbps"));
155  onoff.SetAttribute ("PacketSize", UintegerValue (50));
156 
157  ApplicationContainer apps = onoff.Install (c.Get (1));
158  apps.Start (Seconds (1.0));
159  apps.Stop (Seconds (10.0));
160 
161  // Create a second OnOff application to send UDP datagrams of size
162  // 210 bytes at a rate of 448 Kb/s
163  OnOffHelper onoff2 ("ns3::UdpSocketFactory",
164  InetSocketAddress (i1i6.GetAddress (1), port));
165  onoff2.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
166  onoff2.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
167  onoff2.SetAttribute ("DataRate", StringValue ("2kbps"));
168  onoff2.SetAttribute ("PacketSize", UintegerValue (50));
169 
170  ApplicationContainer apps2 = onoff2.Install (c.Get (1));
171  apps2.Start (Seconds (11.0));
172  apps2.Stop (Seconds (16.0));
173 
174  // Create an optional packet sink to receive these packets
175  PacketSinkHelper sink ("ns3::UdpSocketFactory",
177  apps = sink.Install (c.Get (6));
178  apps.Start (Seconds (1.0));
179  apps.Stop (Seconds (10.0));
180 
181  PacketSinkHelper sink2 ("ns3::UdpSocketFactory",
183  apps2 = sink2.Install (c.Get (6));
184  apps2.Start (Seconds (11.0));
185  apps2.Stop (Seconds (16.0));
186 
187 
188  AsciiTraceHelper ascii;
189  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("dynamic-global-routing.tr");
190  p2p.EnableAsciiAll (stream);
191  csma.EnableAsciiAll (stream);
192  internet.EnableAsciiIpv4All (stream);
193 
194  p2p.EnablePcapAll ("dynamic-global-routing");
195  csma.EnablePcapAll ("dynamic-global-routing", false);
196 
197  Ptr<Node> n1 = c.Get (1);
198  Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
199  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
200  // then the next p2p is numbered 2
201  uint32_t ipv4ifIndex1 = 2;
202 
203  Simulator::Schedule (Seconds (2),&Ipv4::SetDown,ipv41, ipv4ifIndex1);
204  Simulator::Schedule (Seconds (4),&Ipv4::SetUp,ipv41, ipv4ifIndex1);
205 
206  Ptr<Node> n6 = c.Get (6);
207  Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4> ();
208  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
209  // then the next p2p is numbered 2
210  uint32_t ipv4ifIndex6 = 2;
211  Simulator::Schedule (Seconds (6),&Ipv4::SetDown,ipv46, ipv4ifIndex6);
212  Simulator::Schedule (Seconds (8),&Ipv4::SetUp,ipv46, ipv4ifIndex6);
213 
214  Simulator::Schedule (Seconds (12),&Ipv4::SetDown,ipv41, ipv4ifIndex1);
215  Simulator::Schedule (Seconds (14),&Ipv4::SetUp,ipv41, ipv4ifIndex1);
216 
217  // Trace routing tables
219  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("dynamic-global-routing.routes", std::ios::out);
220  g.PrintRoutingTableAllAt (Seconds (12), routingStream);
221 
222  NS_LOG_INFO ("Run Simulation.");
223  Simulator::Run ();
225  NS_LOG_INFO ("Done.");
226 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
AttributeValue implementation for Boolean.
Definition: boolean.h:36
holds a vector of std::pair of Ptr<Ipv4> and interface index.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
NetDeviceContainer Install(NodeContainer c)
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
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 ...
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
virtual void SetUp(uint32_t interface)=0
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:226
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Helper class that adds ns3::Ipv4GlobalRouting objects.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
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...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
virtual void SetDown(uint32_t interface)=0
NodeContainer n0n2
NodeContainer n1n2
static void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of all nodes at a particular time.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.