A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dynamic-global-routing.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Contributed by: Luis Cortes (cortes@gatech.edu)
5 */
6
7// This script exercises global routing code in a mixed point-to-point
8// and csma/cd environment. We bring up and down interfaces and observe
9// the effect on global routing. We explicitly enable the attribute
10// to respond to interface events, so that routes are recomputed
11// automatically.
12//
13// Network topology
14//
15// n0
16// \ p-p
17// \ (shared csma/cd)
18// n2 -------------------------n3
19// / | |
20// / p-p n4 n5 ---------- n6
21// n1 p-p
22// | |
23// ----------------------------------------
24// p-p
25//
26// - at time 1 CBR/UDP flow from n1 to n6's IP address on the n5/n6 link
27// - at time 10, start similar flow from n1 to n6's address on the n1/n6 link
28//
29// Order of events
30// At pre-simulation time, configure global routes. Shortest path from
31// n1 to n6 is via the direct point-to-point link
32// At time 1s, start CBR traffic flow from n1 to n6
33// At time 2s, set the n1 point-to-point interface to down. Packets
34// will be diverted to the n1-n2-n5-n6 path
35// At time 4s, re-enable the n1/n6 interface to up. n1-n6 route restored.
36// At time 6s, set the n6-n1 point-to-point Ipv4 interface to down (note, this
37// keeps the point-to-point link "up" from n1's perspective). Traffic will
38// flow through the path n1-n2-n5-n6
39// At time 8s, bring the interface back up. Path n1-n6 is restored
40// At time 10s, stop the first flow.
41// At time 11s, start a new flow, but to n6's other IP address (the one
42// on the n1/n6 p2p link)
43// At time 12s, bring the n1 interface down between n1 and n6. Packets
44// will be diverted to the alternate path
45// At time 14s, re-enable the n1/n6 interface to up. This will change
46// routing back to n1-n6 since the interface up notification will cause
47// a new local interface route, at higher priority than global routing
48// At time 16s, stop the second flow.
49
50// - Tracing of queues and packet receptions to file "dynamic-global-routing.tr"
51
52#include "ns3/applications-module.h"
53#include "ns3/core-module.h"
54#include "ns3/csma-module.h"
55#include "ns3/internet-module.h"
56#include "ns3/ipv4-global-routing-helper.h"
57#include "ns3/network-module.h"
58#include "ns3/point-to-point-module.h"
59
60#include <cassert>
61#include <fstream>
62#include <iostream>
63#include <string>
64
65using namespace ns3;
66
67NS_LOG_COMPONENT_DEFINE("DynamicGlobalRoutingExample");
68
69int
70main(int argc, char* argv[])
71{
72 // The below value configures the default behavior of global routing.
73 // By default, it is disabled. To respond to interface events, set to true
74 Config::SetDefault("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue(true));
75
76 // Allow the user to override any of the defaults and the above
77 // Bind ()s at run-time, via command-line arguments
78 CommandLine cmd(__FILE__);
79 cmd.Parse(argc, argv);
80
81 NS_LOG_INFO("Create nodes.");
83 c.Create(7);
86 NodeContainer n5n6 = NodeContainer(c.Get(5), c.Get(6));
87 NodeContainer n1n6 = NodeContainer(c.Get(1), c.Get(6));
88 NodeContainer n2345 = NodeContainer(c.Get(2), c.Get(3), c.Get(4), c.Get(5));
89
91 internet.Install(c);
92
93 // We create the channels first without any IP addressing information
94 NS_LOG_INFO("Create channels.");
96 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
97 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
98 NetDeviceContainer d0d2 = p2p.Install(n0n2);
99 NetDeviceContainer d1d6 = p2p.Install(n1n6);
100
101 NetDeviceContainer d1d2 = p2p.Install(n1n2);
102
103 p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
104 p2p.SetChannelAttribute("Delay", StringValue("10ms"));
105 NetDeviceContainer d5d6 = p2p.Install(n5n6);
106
107 // We create the channels first without any IP addressing information
109 csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
110 csma.SetChannelAttribute("Delay", StringValue("2ms"));
111 NetDeviceContainer d2345 = csma.Install(n2345);
112
113 // Later, we add IP addresses.
114 NS_LOG_INFO("Assign IP Addresses.");
116 ipv4.SetBase("10.1.1.0", "255.255.255.0");
117 ipv4.Assign(d0d2);
118
119 ipv4.SetBase("10.1.2.0", "255.255.255.0");
120 ipv4.Assign(d1d2);
121
122 ipv4.SetBase("10.1.3.0", "255.255.255.0");
123 Ipv4InterfaceContainer i5i6 = ipv4.Assign(d5d6);
124
125 ipv4.SetBase("10.250.1.0", "255.255.255.0");
126 ipv4.Assign(d2345);
127
128 ipv4.SetBase("172.16.1.0", "255.255.255.0");
129 Ipv4InterfaceContainer i1i6 = ipv4.Assign(d1d6);
130
131 // Create router nodes, initialize routing database and set up the routing
132 // tables in the nodes.
134
135 // Create the OnOff application to send UDP datagrams of size
136 // 210 bytes at a rate of 448 Kb/s
137 NS_LOG_INFO("Create Applications.");
138 uint16_t port = 9; // Discard port (RFC 863)
139 OnOffHelper onoff("ns3::UdpSocketFactory", InetSocketAddress(i5i6.GetAddress(1), port));
140 onoff.SetConstantRate(DataRate("2kbps"));
141 onoff.SetAttribute("PacketSize", UintegerValue(50));
142
143 ApplicationContainer apps = onoff.Install(c.Get(1));
144 apps.Start(Seconds(1.0));
145 apps.Stop(Seconds(10.0));
146
147 // Create a second OnOff application to send UDP datagrams of size
148 // 210 bytes at a rate of 448 Kb/s
149 OnOffHelper onoff2("ns3::UdpSocketFactory", InetSocketAddress(i1i6.GetAddress(1), port));
150 onoff2.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
151 onoff2.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
152 onoff2.SetAttribute("DataRate", StringValue("2kbps"));
153 onoff2.SetAttribute("PacketSize", UintegerValue(50));
154
155 ApplicationContainer apps2 = onoff2.Install(c.Get(1));
156 apps2.Start(Seconds(11.0));
157 apps2.Stop(Seconds(16.0));
158
159 // Create an optional packet sink to receive these packets
160 PacketSinkHelper sink("ns3::UdpSocketFactory",
162 apps = sink.Install(c.Get(6));
163 apps.Start(Seconds(1.0));
164 apps.Stop(Seconds(10.0));
165
166 PacketSinkHelper sink2("ns3::UdpSocketFactory",
168 apps2 = sink2.Install(c.Get(6));
169 apps2.Start(Seconds(11.0));
170 apps2.Stop(Seconds(16.0));
171
172 AsciiTraceHelper ascii;
173 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("dynamic-global-routing.tr");
174 p2p.EnableAsciiAll(stream);
175 csma.EnableAsciiAll(stream);
176 internet.EnableAsciiIpv4All(stream);
177
178 p2p.EnablePcapAll("dynamic-global-routing");
179 csma.EnablePcapAll("dynamic-global-routing", false);
180
181 Ptr<Node> n1 = c.Get(1);
182 Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4>();
183 // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
184 // then the next p2p is numbered 2
185 uint32_t ipv4ifIndex1 = 2;
186
187 Simulator::Schedule(Seconds(2), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
188 Simulator::Schedule(Seconds(4), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
189
190 Ptr<Node> n6 = c.Get(6);
191 Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4>();
192 // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
193 // then the next p2p is numbered 2
194 uint32_t ipv4ifIndex6 = 2;
195 Simulator::Schedule(Seconds(6), &Ipv4::SetDown, ipv46, ipv4ifIndex6);
196 Simulator::Schedule(Seconds(8), &Ipv4::SetUp, ipv46, ipv4ifIndex6);
197
198 Simulator::Schedule(Seconds(12), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
199 Simulator::Schedule(Seconds(14), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
200
201 // Trace routing tables
202 Ptr<OutputStreamWrapper> routingStream =
203 Create<OutputStreamWrapper>("dynamic-global-routing.routes", std::ios::out);
205
206 NS_LOG_INFO("Run Simulation.");
209 NS_LOG_INFO("Done.");
210
211 return 0;
212}
NodeContainer n1n2
Nodecontainer n1 + n2.
NodeContainer n0n2
Nodecontainer n0 + n2.
a polymophic address class
Definition address.h:90
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 Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
virtual void SetUp(uint32_t interface)=0
virtual void SetDown(uint32_t interface)=0
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
static void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of all nodes at a particular time.
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.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
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
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44