A Discrete-Event Network Simulator
API
simple-alternate-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 // 1.5 Mb/s, 100ms
29 //
30 // this is a modification of simple-global-routing to allow for
31 // a single hop but higher-cost path between n1 and n3
32 //
33 // - Tracing of queues and packet receptions to file "simple-rerouting.tr"
34 
35 #include <iostream>
36 #include <fstream>
37 #include <string>
38 #include <cassert>
39 
40 #include "ns3/core-module.h"
41 #include "ns3/internet-module.h"
42 #include "ns3/point-to-point-module.h"
43 #include "ns3/network-module.h"
44 #include "ns3/applications-module.h"
45 #include "ns3/ipv4-global-routing-helper.h"
46 
47 using namespace ns3;
48 
49 NS_LOG_COMPONENT_DEFINE ("SimpleAlternateRoutingExample");
50 
51 int
52 main (int argc, char *argv[])
53 {
54  // Users may find it convenient to turn on explicit debugging
55  // for selected modules; the below lines suggest how to do this
56 #if 0
57  LogComponentEnable ("GlobalRoutingHelper", LOG_LOGIC);
58  LogComponentEnable ("GlobalRouter", LOG_LOGIC);
59 #endif
60 
61 
62  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (210));
63  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("300b/s"));
64 
65  // The below metric, if set to 3 or higher, will cause packets between
66  // n1 and n3 to take the 2-hop route through n2
67 
69  //
70  // Additionally, we plumb this metric into the default value / command
71  // line argument system as well, for exemplary purposes. This means
72  // that it can be resettable at the command-line to the program,
73  // rather than recompiling
74  // e.g. waf --run "simple-alternate-routing --AlternateCost=5"
75  uint16_t sampleMetric = 1;
76  cmd.AddValue ("AlternateCost",
77  "This metric is used in the example script between n3 and n1 ",
78  sampleMetric);
79 
80  // Allow the user to override any of the defaults and the above
81  // DefaultValue::Bind ()s at run-time, via command-line arguments
82  cmd.Parse (argc, argv);
83 
84  // Here, we will explicitly create four nodes. In more sophisticated
85  // topologies, we could configure a node factory.
86  NS_LOG_INFO ("Create nodes.");
87  NodeContainer c;
88  c.Create (4);
89  NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
90  NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
91  NodeContainer n3n2 = NodeContainer (c.Get (3), c.Get (2));
92  NodeContainer n1n3 = NodeContainer (c.Get (1), c.Get (3));
93 
94  // We create the channels first without any IP addressing information
95  NS_LOG_INFO ("Create channels.");
97  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
98  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
99  NetDeviceContainer d0d2 = p2p.Install (n0n2);
100 
101  NetDeviceContainer d1d2 = p2p.Install (n1n2);
102 
103  p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
104  p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
105  NetDeviceContainer d3d2 = p2p.Install (n3n2);
106 
107  p2p.SetChannelAttribute ("Delay", StringValue ("100ms"));
108  NetDeviceContainer d1d3 = p2p.Install (n1n3);
109 
110  InternetStackHelper internet;
111  internet.Install (c);
112 
113  // Later, we add IP addresses. The middle two octets correspond to
114  // the channel number.
115  NS_LOG_INFO ("Assign IP Addresses.");
116  Ipv4AddressHelper ipv4;
117  ipv4.SetBase ("10.0.0.0", "255.255.255.0");
118  ipv4.Assign (d0d2);
119 
120  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
121  Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
122 
123  ipv4.SetBase ("10.2.2.0", "255.255.255.0");
124  ipv4.Assign (d3d2);
125 
126  ipv4.SetBase ("10.3.3.0", "255.255.255.0");
127  Ipv4InterfaceContainer i1i3 = ipv4.Assign (d1d3);
128 
129  i1i3.SetMetric (0, sampleMetric);
130  i1i3.SetMetric (1, sampleMetric);
131 
132  // Create router nodes, initialize routing database and set up the routing
133  // tables in the nodes.
135 
136  // Create the OnOff application to send UDP datagrams
137  NS_LOG_INFO ("Create Application.");
138  uint16_t port = 9; // Discard port (RFC 863)
139 
140  // Create a flow from n3 to n1, starting at time 1.1 seconds
141  OnOffHelper onoff ("ns3::UdpSocketFactory",
142  Address (InetSocketAddress (i1i2.GetAddress (0), port)));
143  onoff.SetConstantRate (DataRate ("300b/s"));
144 
145  ApplicationContainer apps = onoff.Install (c.Get (3));
146  apps.Start (Seconds (1.1));
147  apps.Stop (Seconds (10.0));
148 
149  // Create a packet sink to receive these packets
150  PacketSinkHelper sink ("ns3::UdpSocketFactory",
152  apps = sink.Install (c.Get (1));
153  apps.Start (Seconds (1.1));
154  apps.Stop (Seconds (10.0));
155 
156  AsciiTraceHelper ascii;
157  p2p.EnableAsciiAll (ascii.CreateFileStream ("simple-alternate-routing.tr"));
158  p2p.EnablePcapAll ("simple-alternate-routing");
159 
160  NS_LOG_INFO ("Run Simulation.");
161  Simulator::Run ();
163  NS_LOG_INFO ("Done.");
164 
165  return 0;
166 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:47
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
an Inet address class
static Ipv4Address GetAny(void)
holds a vector of std::pair of Ptr 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)
Ipv4InterfaceContainer i1i2
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
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:244
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
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. ...
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
Control flow tracing within functions.
Definition: log.h:108
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:351
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 ...
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...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
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...
void SetMetric(uint32_t i, uint16_t metric)
Set a metric for the given interface.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void Parse(int argc, char *argv[])
Parse the program arguments.
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.
NodeContainer n0n2
NodeContainer n1n2
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const