A Discrete-Event Network Simulator
Go to the documentation of this file.
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
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 * Contributed by: Luis Cortes (cortes@gatech.edu)
16 */
18// This script exercises global routing code in a mixed point-to-point
19// and csma/cd environment. We bring up and down interfaces and observe
20// the effect on global routing. We explicitly enable the attribute
21// to respond to interface events, so that routes are recomputed
22// automatically.
24// Network topology
26// n0
27// \ p-p
28// \ (shared csma/cd)
29// n2 -------------------------n3
30// / | |
31// / p-p n4 n5 ---------- n6
32// n1 p-p
33// | |
34// ----------------------------------------
35// p-p
37// - at time 1 CBR/UDP flow from n1 to n6's IP address on the n5/n6 link
38// - at time 10, start similar flow from n1 to n6's address on the n1/n6 link
40// Order of events
41// At pre-simulation time, configure global routes. Shortest path from
42// n1 to n6 is via the direct point-to-point link
43// At time 1s, start CBR traffic flow from n1 to n6
44// At time 2s, set the n1 point-to-point interface to down. Packets
45// will be diverted to the n1-n2-n5-n6 path
46// At time 4s, re-enable the n1/n6 interface to up. n1-n6 route restored.
47// At time 6s, set the n6-n1 point-to-point Ipv4 interface to down (note, this
48// keeps the point-to-point link "up" from n1's perspective). Traffic will
49// flow through the path n1-n2-n5-n6
50// At time 8s, bring the interface back up. Path n1-n6 is restored
51// At time 10s, stop the first flow.
52// At time 11s, start a new flow, but to n6's other IP address (the one
53// on the n1/n6 p2p link)
54// At time 12s, bring the n1 interface down between n1 and n6. Packets
55// will be diverted to the alternate path
56// At time 14s, re-enable the n1/n6 interface to up. This will change
57// routing back to n1-n6 since the interface up notification will cause
58// a new local interface route, at higher priority than global routing
59// At time 16s, stop the second flow.
61// - Tracing of queues and packet receptions to file "dynamic-global-routing.tr"
63#include "ns3/applications-module.h"
64#include "ns3/core-module.h"
65#include "ns3/csma-module.h"
66#include "ns3/internet-module.h"
67#include "ns3/ipv4-global-routing-helper.h"
68#include "ns3/network-module.h"
69#include "ns3/point-to-point-module.h"
71#include <cassert>
72#include <fstream>
73#include <iostream>
74#include <string>
76using namespace ns3;
81main(int argc, char* argv[])
83 // The below value configures the default behavior of global routing.
84 // By default, it is disabled. To respond to interface events, set to true
85 Config::SetDefault("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue(true));
87 // Allow the user to override any of the defaults and the above
88 // Bind ()s at run-time, via command-line arguments
89 CommandLine cmd(__FILE__);
90 cmd.Parse(argc, argv);
92 NS_LOG_INFO("Create nodes.");
94 c.Create(7);
97 NodeContainer n5n6 = NodeContainer(c.Get(5), c.Get(6));
98 NodeContainer n1n6 = NodeContainer(c.Get(1), c.Get(6));
99 NodeContainer n2345 = NodeContainer(c.Get(2), c.Get(3), c.Get(4), c.Get(5));
101 InternetStackHelper internet;
102 internet.Install(c);
104 // We create the channels first without any IP addressing information
105 NS_LOG_INFO("Create channels.");
107 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
108 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
109 NetDeviceContainer d0d2 = p2p.Install(n0n2);
110 NetDeviceContainer d1d6 = p2p.Install(n1n6);
112 NetDeviceContainer d1d2 = p2p.Install(n1n2);
114 p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
115 p2p.SetChannelAttribute("Delay", StringValue("10ms"));
116 NetDeviceContainer d5d6 = p2p.Install(n5n6);
118 // We create the channels first without any IP addressing information
120 csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
121 csma.SetChannelAttribute("Delay", StringValue("2ms"));
122 NetDeviceContainer d2345 = csma.Install(n2345);
124 // Later, we add IP addresses.
125 NS_LOG_INFO("Assign IP Addresses.");
127 ipv4.SetBase("", "");
128 ipv4.Assign(d0d2);
130 ipv4.SetBase("", "");
131 ipv4.Assign(d1d2);
133 ipv4.SetBase("", "");
134 Ipv4InterfaceContainer i5i6 = ipv4.Assign(d5d6);
136 ipv4.SetBase("", "");
137 ipv4.Assign(d2345);
139 ipv4.SetBase("", "");
140 Ipv4InterfaceContainer i1i6 = ipv4.Assign(d1d6);
142 // Create router nodes, initialize routing database and set up the routing
143 // tables in the nodes.
144 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
146 // Create the OnOff application to send UDP datagrams of size
147 // 210 bytes at a rate of 448 Kb/s
148 NS_LOG_INFO("Create Applications.");
149 uint16_t port = 9; // Discard port (RFC 863)
150 OnOffHelper onoff("ns3::UdpSocketFactory", InetSocketAddress(i5i6.GetAddress(1), port));
151 onoff.SetConstantRate(DataRate("2kbps"));
152 onoff.SetAttribute("PacketSize", UintegerValue(50));
154 ApplicationContainer apps = onoff.Install(c.Get(1));
155 apps.Start(Seconds(1.0));
156 apps.Stop(Seconds(10.0));
158 // Create a second OnOff application to send UDP datagrams of size
159 // 210 bytes at a rate of 448 Kb/s
160 OnOffHelper onoff2("ns3::UdpSocketFactory", InetSocketAddress(i1i6.GetAddress(1), port));
161 onoff2.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
162 onoff2.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
163 onoff2.SetAttribute("DataRate", StringValue("2kbps"));
164 onoff2.SetAttribute("PacketSize", UintegerValue(50));
166 ApplicationContainer apps2 = onoff2.Install(c.Get(1));
167 apps2.Start(Seconds(11.0));
168 apps2.Stop(Seconds(16.0));
170 // Create an optional packet sink to receive these packets
171 PacketSinkHelper sink("ns3::UdpSocketFactory",
172 Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
173 apps = sink.Install(c.Get(6));
174 apps.Start(Seconds(1.0));
175 apps.Stop(Seconds(10.0));
177 PacketSinkHelper sink2("ns3::UdpSocketFactory",
178 Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
179 apps2 = sink2.Install(c.Get(6));
180 apps2.Start(Seconds(11.0));
181 apps2.Stop(Seconds(16.0));
183 AsciiTraceHelper ascii;
184 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream("dynamic-global-routing.tr");
185 p2p.EnableAsciiAll(stream);
186 csma.EnableAsciiAll(stream);
187 internet.EnableAsciiIpv4All(stream);
189 p2p.EnablePcapAll("dynamic-global-routing");
190 csma.EnablePcapAll("dynamic-global-routing", false);
192 Ptr<Node> n1 = c.Get(1);
193 Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4>();
194 // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
195 // then the next p2p is numbered 2
196 uint32_t ipv4ifIndex1 = 2;
198 Simulator::Schedule(Seconds(2), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
199 Simulator::Schedule(Seconds(4), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
201 Ptr<Node> n6 = c.Get(6);
202 Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4>();
203 // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
204 // then the next p2p is numbered 2
205 uint32_t ipv4ifIndex6 = 2;
206 Simulator::Schedule(Seconds(6), &Ipv4::SetDown, ipv46, ipv4ifIndex6);
207 Simulator::Schedule(Seconds(8), &Ipv4::SetUp, ipv46, ipv4ifIndex6);
209 Simulator::Schedule(Seconds(12), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
210 Simulator::Schedule(Seconds(14), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
212 // Trace routing tables
213 Ptr<OutputStreamWrapper> routingStream =
214 Create<OutputStreamWrapper>("dynamic-global-routing.routes", std::ios::out);
215 Ipv4RoutingHelper::PrintRoutingTableAllAt(Seconds(12), routingStream);
217 NS_LOG_INFO("Run Simulation.");
218 Simulator::Run();
219 Simulator::Destroy();
220 NS_LOG_INFO("Done.");
222 return 0;
NodeContainer n1n2
Nodecontainer n1 + n2.
NodeContainer n0n2
Nodecontainer n0 + n2.
a polymophic address class
Definition: address.h:100
holds a vector of ns3::Application pointers.
void Start(Time start)
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
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 EnableAsciiIpv4All(std::string prefix)
Enable ascii trace output on all Ipv4 and interface pairs existing in the set of all nodes created in...
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
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.
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 ...
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.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
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:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
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:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Definition: second.py:56
Definition: second.py:33
void SetUp(char *deviceName)
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55