A Discrete-Event Network Simulator
API
red-vs-nlred.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 NITK Surathkal
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Phani Kiran S V S <phanikiran.harithas@gmail.com>
18  * Nichit Bodhak Goel <nichit93@gmail.com>
19  * Mohit P. Tahiliani <tahiliani@nitk.edu.in>
20  *
21  */
22 
23 #include "ns3/applications-module.h"
24 #include "ns3/core-module.h"
25 #include "ns3/internet-module.h"
26 #include "ns3/network-module.h"
27 #include "ns3/point-to-point-layout-module.h"
28 #include "ns3/point-to-point-module.h"
29 #include "ns3/traffic-control-module.h"
30 
31 #include <iomanip>
32 #include <iostream>
33 #include <map>
34 
35 using namespace ns3;
36 
37 int
38 main(int argc, char* argv[])
39 {
40  uint32_t nLeaf = 10;
41  uint32_t maxPackets = 100;
42  bool modeBytes = false;
43  uint32_t queueDiscLimitPackets = 1000;
44  double minTh = 5;
45  double maxTh = 15;
46  uint32_t pktSize = 512;
47  std::string appDataRate = "10Mbps";
48  std::string queueDiscType = "RED";
49  uint16_t port = 5001;
50  std::string bottleNeckLinkBw = "1Mbps";
51  std::string bottleNeckLinkDelay = "50ms";
52 
53  CommandLine cmd(__FILE__);
54  cmd.AddValue("nLeaf", "Number of left and right side leaf nodes", nLeaf);
55  cmd.AddValue("maxPackets", "Max Packets allowed in the device queue", maxPackets);
56  cmd.AddValue("queueDiscLimitPackets",
57  "Max Packets allowed in the queue disc",
58  queueDiscLimitPackets);
59  cmd.AddValue("queueDiscType", "Set Queue disc type to RED or NLRED", queueDiscType);
60  cmd.AddValue("appPktSize", "Set OnOff App Packet Size", pktSize);
61  cmd.AddValue("appDataRate", "Set OnOff App DataRate", appDataRate);
62  cmd.AddValue("modeBytes", "Set Queue disc mode to Packets (false) or bytes (true)", modeBytes);
63 
64  cmd.AddValue("redMinTh", "RED queue minimum threshold", minTh);
65  cmd.AddValue("redMaxTh", "RED queue maximum threshold", maxTh);
66  cmd.Parse(argc, argv);
67 
68  if ((queueDiscType != "RED") && (queueDiscType != "NLRED"))
69  {
70  std::cout << "Invalid queue disc type: Use --queueDiscType=RED or --queueDiscType=NLRED"
71  << std::endl;
72  exit(1);
73  }
74 
75  Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(pktSize));
76  Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue(appDataRate));
77 
78  Config::SetDefault("ns3::DropTailQueue<Packet>::MaxSize",
79  QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, maxPackets)));
80 
81  if (!modeBytes)
82  {
84  "ns3::RedQueueDisc::MaxSize",
85  QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, queueDiscLimitPackets)));
86  }
87  else
88  {
90  "ns3::RedQueueDisc::MaxSize",
91  QueueSizeValue(QueueSize(QueueSizeUnit::BYTES, queueDiscLimitPackets * pktSize)));
92  minTh *= pktSize;
93  maxTh *= pktSize;
94  }
95 
96  Config::SetDefault("ns3::RedQueueDisc::MinTh", DoubleValue(minTh));
97  Config::SetDefault("ns3::RedQueueDisc::MaxTh", DoubleValue(maxTh));
98  Config::SetDefault("ns3::RedQueueDisc::LinkBandwidth", StringValue(bottleNeckLinkBw));
99  Config::SetDefault("ns3::RedQueueDisc::LinkDelay", StringValue(bottleNeckLinkDelay));
100  Config::SetDefault("ns3::RedQueueDisc::MeanPktSize", UintegerValue(pktSize));
101 
102  if (queueDiscType == "NLRED")
103  {
104  // Turn on NLRED
105  Config::SetDefault("ns3::RedQueueDisc::NLRED", BooleanValue(true));
106  }
107 
108  // Create the point-to-point link helpers
109  PointToPointHelper bottleNeckLink;
110  bottleNeckLink.SetDeviceAttribute("DataRate", StringValue(bottleNeckLinkBw));
111  bottleNeckLink.SetChannelAttribute("Delay", StringValue(bottleNeckLinkDelay));
112 
113  PointToPointHelper pointToPointLeaf;
114  pointToPointLeaf.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
115  pointToPointLeaf.SetChannelAttribute("Delay", StringValue("1ms"));
116 
117  PointToPointDumbbellHelper d(nLeaf, pointToPointLeaf, nLeaf, pointToPointLeaf, bottleNeckLink);
118 
119  // Install Stack
121  for (uint32_t i = 0; i < d.LeftCount(); ++i)
122  {
123  stack.Install(d.GetLeft(i));
124  }
125  for (uint32_t i = 0; i < d.RightCount(); ++i)
126  {
127  stack.Install(d.GetRight(i));
128  }
129 
130  stack.Install(d.GetLeft());
131  stack.Install(d.GetRight());
132  TrafficControlHelper tchBottleneck;
133  QueueDiscContainer queueDiscs;
134  tchBottleneck.SetRootQueueDisc("ns3::RedQueueDisc");
135  tchBottleneck.Install(d.GetLeft()->GetDevice(0));
136  queueDiscs = tchBottleneck.Install(d.GetRight()->GetDevice(0));
137 
138  // Assign IP Addresses
139  d.AssignIpv4Addresses(Ipv4AddressHelper("10.1.1.0", "255.255.255.0"),
140  Ipv4AddressHelper("10.2.1.0", "255.255.255.0"),
141  Ipv4AddressHelper("10.3.1.0", "255.255.255.0"));
142 
143  // Install on/off app on all right side nodes
144  OnOffHelper clientHelper("ns3::TcpSocketFactory", Address());
145  clientHelper.SetAttribute("OnTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
146  clientHelper.SetAttribute("OffTime", StringValue("ns3::UniformRandomVariable[Min=0.|Max=1.]"));
147  Address sinkLocalAddress(InetSocketAddress(Ipv4Address::GetAny(), port));
148  PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", sinkLocalAddress);
149  ApplicationContainer sinkApps;
150  for (uint32_t i = 0; i < d.LeftCount(); ++i)
151  {
152  sinkApps.Add(packetSinkHelper.Install(d.GetLeft(i)));
153  }
154  sinkApps.Start(Seconds(0.0));
155  sinkApps.Stop(Seconds(30.0));
156 
158  for (uint32_t i = 0; i < d.RightCount(); ++i)
159  {
160  // Create an on/off app sending packets to the left side
161  AddressValue remoteAddress(InetSocketAddress(d.GetLeftIpv4Address(i), port));
162  clientHelper.SetAttribute("Remote", remoteAddress);
163  clientApps.Add(clientHelper.Install(d.GetRight(i)));
164  }
165  clientApps.Start(Seconds(1.0)); // Start 1 second after sink
166  clientApps.Stop(Seconds(15.0)); // Stop before the sink
167 
169 
170  std::cout << "Running the simulation" << std::endl;
171  Simulator::Run();
172 
173  QueueDisc::Stats st = queueDiscs.Get(0)->GetStats();
174 
176  {
177  std::cout << "There should be some unforced drops" << std::endl;
178  exit(1);
179  }
180 
182  {
183  std::cout << "There should be zero drops due to queue full" << std::endl;
184  exit(1);
185  }
186 
187  std::cout << "*** Stats from the bottleneck queue disc ***" << std::endl;
188  std::cout << st << std::endl;
189  std::cout << "Destroying the simulation" << std::endl;
190 
192  return 0;
193 }
a polymophic address class
Definition: address.h:101
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.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
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.
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.
A helper to make it easier to create a dumbbell topology with p2p links.
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.
Holds a vector of ns3::QueueDisc pointers.
Ptr< QueueDisc > Get(std::size_t i) const
Get the Ptr<QueueDisc> stored in this container at a given index.
static constexpr const char * INTERNAL_QUEUE_DROP
Packet dropped by an internal queue.
Definition: queue-disc.h:522
const Stats & GetStats()
Retrieve all the collected statistics.
Definition: queue-disc.cc:412
Class for representing queue sizes.
Definition: queue-size.h:96
static constexpr const char * UNFORCED_DROP
Early probability drops.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Hold variables of type string.
Definition: string.h:56
Build a set of QueueDisc objects.
QueueDiscContainer Install(NetDeviceContainer c)
uint16_t SetRootQueueDisc(const std::string &type, Args &&... args)
Helper function used to set a root queue disc of the given type and with the given attributes.
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:46
@ PACKETS
Use number of packets for queue size.
Definition: queue-size.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
clientApps
Definition: first.py:64
stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
Structure that keeps the queue disc statistics.
Definition: queue-disc.h:188
uint32_t GetNDroppedPackets(std::string reason) const
Get the number of packets dropped for the given reason.
Definition: queue-disc.cc:111
uint32_t pktSize
packet size used for the simulation (in bytes)