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