A Discrete-Event Network Simulator
API
red-vs-ared.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 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  * Author: Mohit P. Tahiliani <tahiliani@nitk.edu.in>
19  *
20  */
21 
22 #include "ns3/core-module.h"
23 #include "ns3/network-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/point-to-point-module.h"
26 #include "ns3/applications-module.h"
27 #include "ns3/point-to-point-layout-module.h"
28 #include "ns3/traffic-control-module.h"
29 
30 #include <iostream>
31 #include <iomanip>
32 #include <map>
33 
34 using namespace ns3;
35 
36 int main (int argc, char *argv[])
37 {
38  uint32_t nLeaf = 10;
39  uint32_t maxPackets = 100;
40  bool modeBytes = false;
41  uint32_t queueDiscLimitPackets = 1000;
42  double minTh = 5;
43  double maxTh = 15;
44  uint32_t pktSize = 512;
45  std::string appDataRate = "10Mbps";
46  std::string queueDiscType = "RED";
47  uint16_t port = 5001;
48  std::string bottleNeckLinkBw = "1Mbps";
49  std::string bottleNeckLinkDelay = "50ms";
50 
52  cmd.AddValue ("nLeaf", "Number of left and right side leaf nodes", nLeaf);
53  cmd.AddValue ("maxPackets","Max Packets allowed in the device queue", maxPackets);
54  cmd.AddValue ("queueDiscLimitPackets","Max Packets allowed in the queue disc", queueDiscLimitPackets);
55  cmd.AddValue ("queueDiscType", "Set Queue disc type to RED or ARED", queueDiscType);
56  cmd.AddValue ("appPktSize", "Set OnOff App Packet Size", pktSize);
57  cmd.AddValue ("appDataRate", "Set OnOff App DataRate", appDataRate);
58  cmd.AddValue ("modeBytes", "Set Queue disc mode to Packets <false> or bytes <true>", modeBytes);
59 
60  cmd.AddValue ("redMinTh", "RED queue minimum threshold", minTh);
61  cmd.AddValue ("redMaxTh", "RED queue maximum threshold", maxTh);
62  cmd.Parse (argc,argv);
63 
64  if ((queueDiscType != "RED") && (queueDiscType != "ARED"))
65  {
66  std::cout << "Invalid queue disc type: Use --queueDiscType=RED or --queueDiscType=ARED" << std::endl;
67  exit (1);
68  }
69 
70  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (pktSize));
71  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (appDataRate));
72 
73  Config::SetDefault ("ns3::Queue::Mode", StringValue ("QUEUE_MODE_PACKETS"));
74  Config::SetDefault ("ns3::Queue::MaxPackets", UintegerValue (maxPackets));
75 
76  if (!modeBytes)
77  {
78  Config::SetDefault ("ns3::RedQueueDisc::Mode", StringValue ("QUEUE_MODE_PACKETS"));
79  Config::SetDefault ("ns3::RedQueueDisc::QueueLimit", UintegerValue (queueDiscLimitPackets));
80  }
81  else
82  {
83  Config::SetDefault ("ns3::RedQueueDisc::Mode", StringValue ("QUEUE_MODE_BYTES"));
84  Config::SetDefault ("ns3::RedQueueDisc::QueueLimit", UintegerValue (queueDiscLimitPackets * pktSize));
85  minTh *= pktSize;
86  maxTh *= pktSize;
87  }
88 
89  Config::SetDefault ("ns3::RedQueueDisc::MinTh", DoubleValue (minTh));
90  Config::SetDefault ("ns3::RedQueueDisc::MaxTh", DoubleValue (maxTh));
91  Config::SetDefault ("ns3::RedQueueDisc::LinkBandwidth", StringValue (bottleNeckLinkBw));
92  Config::SetDefault ("ns3::RedQueueDisc::LinkDelay", StringValue (bottleNeckLinkDelay));
93  Config::SetDefault ("ns3::RedQueueDisc::MeanPktSize", UintegerValue (pktSize));
94 
95  if (queueDiscType == "ARED")
96  {
97  // Turn on ARED
98  Config::SetDefault ("ns3::RedQueueDisc::ARED", BooleanValue (true));
99  Config::SetDefault ("ns3::RedQueueDisc::LInterm", DoubleValue (10.0));
100  }
101 
102  // Create the point-to-point link helpers
103  PointToPointHelper bottleNeckLink;
104  bottleNeckLink.SetDeviceAttribute ("DataRate", StringValue (bottleNeckLinkBw));
105  bottleNeckLink.SetChannelAttribute ("Delay", StringValue (bottleNeckLinkDelay));
106 
107  PointToPointHelper pointToPointLeaf;
108  pointToPointLeaf.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
109  pointToPointLeaf.SetChannelAttribute ("Delay", StringValue ("1ms"));
110 
111  PointToPointDumbbellHelper d (nLeaf, pointToPointLeaf,
112  nLeaf, pointToPointLeaf,
113  bottleNeckLink);
114 
115  // Install Stack
117  for (uint32_t i = 0; i < d.LeftCount (); ++i)
118  {
119  stack.Install (d.GetLeft (i));
120  }
121  for (uint32_t i = 0; i < d.RightCount (); ++i)
122  {
123  stack.Install (d.GetRight (i));
124  }
125 
126  stack.Install (d.GetLeft ());
127  stack.Install (d.GetRight ());
128  TrafficControlHelper tchBottleneck;
129  tchBottleneck.SetRootQueueDisc ("ns3::RedQueueDisc");
130  tchBottleneck.Install (d.GetLeft ()->GetDevice (0));
131  tchBottleneck.Install (d.GetRight ()->GetDevice (0));
132 
133  // Assign IP Addresses
134  d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
135  Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
136  Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));
137 
138  // Install on/off app on all right side nodes
139  OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ());
140  clientHelper.SetAttribute ("OnTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
141  clientHelper.SetAttribute ("OffTime", StringValue ("ns3::UniformRandomVariable[Min=0.,Max=1.]"));
142  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
143  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
144  ApplicationContainer sinkApps;
145  for (uint32_t i = 0; i < d.LeftCount (); ++i)
146  {
147  sinkApps.Add (packetSinkHelper.Install (d.GetLeft (i)));
148  }
149  sinkApps.Start (Seconds (0.0));
150  sinkApps.Stop (Seconds (30.0));
151 
153  for (uint32_t i = 0; i < d.RightCount (); ++i)
154  {
155  // Create an on/off app sending packets to the left side
156  AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), port));
157  clientHelper.SetAttribute ("Remote", remoteAddress);
158  clientApps.Add (clientHelper.Install (d.GetRight (i)));
159  }
160  clientApps.Start (Seconds (1.0)); // Start 1 second after sink
161  clientApps.Stop (Seconds (15.0)); // Stop before the sink
162 
164 
165  std::cout << "Running the simulation" << std::endl;
166  Simulator::Run ();
167 
168  uint32_t totalRxBytesCounter = 0;
169  for (uint32_t i = 0; i < sinkApps.GetN (); i++)
170  {
171  Ptr <Application> app = sinkApps.Get (i);
172  Ptr <PacketSink> pktSink = DynamicCast <PacketSink> (app);
173  totalRxBytesCounter += pktSink->GetTotalRx ();
174  }
175 
176  if (queueDiscType == "RED")
177  {
178  if (modeBytes)
179  {
180  if (totalRxBytesCounter > 2772992)
181  {
182  std::cout << "RED Goodput is too high, should be about 10403.2 Bytes/sec" << std::endl;
183  exit (-1);
184  }
185  else if (totalRxBytesCounter < 2661888)
186  {
187  std::cout << "RED Goodput is too low, should be about 10403.2 Bytes/sec" << std::endl;
188  exit (-1);
189  }
190  }
191  else
192  {
193  if (totalRxBytesCounter > 2759680)
194  {
195  std::cout << "RED Goodput is too high, should be about 10355.1 Bytes/sec" << std::endl;
196  exit (-1);
197  }
198  else if (totalRxBytesCounter < 2666496)
199  {
200  std::cout << "RED Goodput is too low, should be about 10355.1 Bytes/sec" << std::endl;
201  exit (-1);
202  }
203  }
204  }
205  else if (queueDiscType == "ARED")
206  {
207  if (modeBytes)
208  {
209  if (totalRxBytesCounter > 2771968)
210  {
211  std::cout << "ARED Goodput is too high, should be about 10366.7 Bytes/sec" << std::endl;
212  exit (-1);
213  }
214  else if (totalRxBytesCounter < 2654208)
215  {
216  std::cout << "ARED Goodput is too low, should be about 10366.7 Bytes/sec" << std::endl;
217  exit (-1);
218  }
219  }
220  else
221  {
222  if (totalRxBytesCounter > 2765824)
223  {
224  std::cout << "ARED Goodput is too high, should be about 10317.4 Bytes/sec" << std::endl;
225  exit (-1);
226  }
227  else if (totalRxBytesCounter < 2636800)
228  {
229  std::cout << "ARED Goodput is too low, should be about 10317.4 Bytes/sec" << std::endl;
230  exit (-1);
231  }
232  }
233  }
234 
235  std::cout << "----------------------------\nQueue disc Type:"
236  << queueDiscType
237  << "\nGoodput Bytes/sec:"
238  << totalRxBytesCounter / Simulator::Now ().GetSeconds () << std::endl;
239  std::cout << "----------------------------" << std::endl;
240  std::cout << "Destroying the simulation" << std::endl;
241 
243  return 0;
244 }
holds a vector of ns3::Application pointers.
an Inet address class
static Ipv4Address GetAny(void)
AttributeValue implementation for Boolean.
Definition: boolean.h:34
QueueDiscContainer Install(NetDeviceContainer c)
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:200
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
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
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
tuple clientApps
Definition: first.py:54
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
Hold an unsigned integer type.
Definition: uinteger.h:44
Build a set of QueueDisc objects.
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:201
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< Application > Get(uint32_t i) const
Get the Ptr 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 Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
uint32_t GetTotalRx() const
Definition: packet-sink.cc:78
tuple stack
Definition: first.py:34
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...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:491
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
A helper to make it easier to create a dumbbell topology with p2p links.
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.
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41