A Discrete-Event Network Simulator
API
pie-example.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: Shravya Ks <shravya.ks0@gmail.com>
19  * Smriti Murali <m.smriti.95@gmail.com>
20  * Mohit P. Tahiliani <tahiliani@nitk.edu.in>
21  *
22  */
23 
35 #include "ns3/core-module.h"
36 #include "ns3/network-module.h"
37 #include "ns3/internet-module.h"
38 #include "ns3/flow-monitor-helper.h"
39 #include "ns3/point-to-point-module.h"
40 #include "ns3/applications-module.h"
41 #include "ns3/traffic-control-module.h"
42 
43 using namespace ns3;
44 
45 NS_LOG_COMPONENT_DEFINE ("PieExample");
46 
47 uint32_t checkTimes;
49 
50 // The times
57 
63 
69 
70 std::stringstream filePlotQueueDisc;
71 std::stringstream filePlotQueueDiscAvg;
72 
73 void
75 {
76  uint32_t qSize = StaticCast<PieQueueDisc> (queue)->GetQueueSize ();
77 
78  avgQueueDiscSize += qSize;
79  checkTimes++;
80 
81  // check queue disc size every 1/100 of a second
83 
84  std::ofstream fPlotQueueDisc (filePlotQueueDisc.str ().c_str (), std::ios::out | std::ios::app);
85  fPlotQueueDisc << Simulator::Now ().GetSeconds () << " " << qSize << std::endl;
86  fPlotQueueDisc.close ();
87 
88  std::ofstream fPlotQueueDiscAvg (filePlotQueueDiscAvg.str ().c_str (), std::ios::out | std::ios::app);
89  fPlotQueueDiscAvg << Simulator::Now ().GetSeconds () << " " << avgQueueDiscSize / checkTimes << std::endl;
90  fPlotQueueDiscAvg.close ();
91 }
92 
93 void
95 {
96  // SINK is in the right side
97  uint16_t port = 50000;
98  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
99  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
100  ApplicationContainer sinkApp = sinkHelper.Install (n3n4.Get (1));
101  sinkApp.Start (Seconds (sink_start_time));
102  sinkApp.Stop (Seconds (sink_stop_time));
103 
104  // Connection one
105  // Clients are in left side
106  /*
107  * Create the OnOff applications to send TCP to the server
108  * onoffhelper is a client that send data to TCP destination
109  */
110  OnOffHelper clientHelper1 ("ns3::TcpSocketFactory", Address ());
111  clientHelper1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
112  clientHelper1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
113  clientHelper1.SetAttribute ("PacketSize", UintegerValue (1000));
114  clientHelper1.SetAttribute ("DataRate", DataRateValue (DataRate ("10Mb/s")));
115 
116  // Connection two
117  OnOffHelper clientHelper2 ("ns3::TcpSocketFactory", Address ());
118  clientHelper2.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
119  clientHelper2.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
120  clientHelper2.SetAttribute ("PacketSize", UintegerValue (1000));
121  clientHelper2.SetAttribute ("DataRate", DataRateValue (DataRate ("10Mb/s")));
122 
123  ApplicationContainer clientApps1;
124  AddressValue remoteAddress (InetSocketAddress (i3i4.GetAddress (1), port));
125  clientHelper1.SetAttribute ("Remote", remoteAddress);
126  clientApps1.Add (clientHelper1.Install (n0n2.Get (0)));
127  clientApps1.Start (Seconds (client_start_time));
128  clientApps1.Stop (Seconds (client_stop_time));
129 
130  ApplicationContainer clientApps2;
131  clientHelper2.SetAttribute ("Remote", remoteAddress);
132  clientApps2.Add (clientHelper2.Install (n1n2.Get (0)));
133  clientApps2.Start (Seconds (client_start_time));
134  clientApps2.Stop (Seconds (client_stop_time));
135 }
136 
137 int
138 main (int argc, char *argv[])
139 {
140  LogComponentEnable ("PieQueueDisc", LOG_LEVEL_INFO);
141 
142  std::string pieLinkDataRate = "1.5Mbps";
143  std::string pieLinkDelay = "20ms";
144 
145  std::string pathOut;
146  bool writeForPlot = false;
147  bool writePcap = false;
148  bool flowMonitor = false;
149 
150  bool printPieStats = true;
151 
152  global_start_time = 0.0;
155  global_stop_time = 7.0;
158 
159  // Configuration and command line parameter parsing
160  // Will only save in the directory if enable opts below
161  pathOut = "."; // Current directory
163  cmd.AddValue ("pathOut", "Path to save results from --writeForPlot/--writePcap/--writeFlowMonitor", pathOut);
164  cmd.AddValue ("writeForPlot", "<0/1> to write results for plot (gnuplot)", writeForPlot);
165  cmd.AddValue ("writePcap", "<0/1> to write results in pcapfile", writePcap);
166  cmd.AddValue ("writeFlowMonitor", "<0/1> to enable Flow Monitor and write their results", flowMonitor);
167 
168  cmd.Parse (argc, argv);
169 
170  NS_LOG_INFO ("Create nodes");
171  NodeContainer c;
172  c.Create (6);
173  Names::Add ( "N0", c.Get (0));
174  Names::Add ( "N1", c.Get (1));
175  Names::Add ( "N2", c.Get (2));
176  Names::Add ( "N3", c.Get (3));
177  Names::Add ( "N4", c.Get (4));
178  Names::Add ( "N5", c.Get (5));
179  n0n2 = NodeContainer (c.Get (0), c.Get (2));
180  n1n2 = NodeContainer (c.Get (1), c.Get (2));
181  n2n3 = NodeContainer (c.Get (2), c.Get (3));
182  n3n4 = NodeContainer (c.Get (3), c.Get (4));
183  n3n5 = NodeContainer (c.Get (3), c.Get (5));
184 
185  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", StringValue ("ns3::TcpNewReno"));
186  // 42 = headers size
187  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (1000 - 42));
188  Config::SetDefault ("ns3::TcpSocket::DelAckCount", UintegerValue (1));
189  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (false));
190 
191  uint32_t meanPktSize = 1000;
192 
193  // PIE params
194  NS_LOG_INFO ("Set PIE params");
195  Config::SetDefault ("ns3::PieQueueDisc::Mode", StringValue ("QUEUE_DISC_MODE_PACKETS"));
196  Config::SetDefault ("ns3::PieQueueDisc::MeanPktSize", UintegerValue (meanPktSize));
197  Config::SetDefault ("ns3::PieQueueDisc::DequeueThreshold", UintegerValue (10000));
198  Config::SetDefault ("ns3::PieQueueDisc::QueueDelayReference", TimeValue (Seconds (0.02)));
199  Config::SetDefault ("ns3::PieQueueDisc::MaxBurstAllowance", TimeValue (Seconds (0.1)));
200  Config::SetDefault ("ns3::PieQueueDisc::QueueLimit", UintegerValue (100));
201 
202  NS_LOG_INFO ("Install internet stack on all nodes.");
203  InternetStackHelper internet;
204  internet.Install (c);
205 
206  TrafficControlHelper tchPfifo;
207  uint16_t handle = tchPfifo.SetRootQueueDisc ("ns3::PfifoFastQueueDisc");
208  tchPfifo.AddInternalQueues (handle, 3, "ns3::DropTailQueue", "MaxPackets", UintegerValue (1000));
209 
210  TrafficControlHelper tchPie;
211  tchPie.SetRootQueueDisc ("ns3::PieQueueDisc");
212 
213  NS_LOG_INFO ("Create channels");
214  PointToPointHelper p2p;
215 
216  NetDeviceContainer devn0n2;
217  NetDeviceContainer devn1n2;
218  NetDeviceContainer devn2n3;
219  NetDeviceContainer devn3n4;
220  NetDeviceContainer devn3n5;
221 
222  QueueDiscContainer queueDiscs;
223 
224  p2p.SetQueue ("ns3::DropTailQueue");
225  p2p.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
226  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
227  devn0n2 = p2p.Install (n0n2);
228  tchPfifo.Install (devn0n2);
229 
230  p2p.SetQueue ("ns3::DropTailQueue");
231  p2p.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
232  p2p.SetChannelAttribute ("Delay", StringValue ("3ms"));
233  devn1n2 = p2p.Install (n1n2);
234  tchPfifo.Install (devn1n2);
235 
236  p2p.SetQueue ("ns3::DropTailQueue");
237  p2p.SetDeviceAttribute ("DataRate", StringValue (pieLinkDataRate));
238  p2p.SetChannelAttribute ("Delay", StringValue (pieLinkDelay));
239  devn2n3 = p2p.Install (n2n3);
240  // only backbone link has PIE queue disc
241  queueDiscs = tchPie.Install (devn2n3);
242 
243  p2p.SetQueue ("ns3::DropTailQueue");
244  p2p.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
245  p2p.SetChannelAttribute ("Delay", StringValue ("4ms"));
246  devn3n4 = p2p.Install (n3n4);
247  tchPfifo.Install (devn3n4);
248 
249  p2p.SetQueue ("ns3::DropTailQueue");
250  p2p.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
251  p2p.SetChannelAttribute ("Delay", StringValue ("5ms"));
252  devn3n5 = p2p.Install (n3n5);
253  tchPfifo.Install (devn3n5);
254 
255  NS_LOG_INFO ("Assign IP Addresses");
256  Ipv4AddressHelper ipv4;
257 
258  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
259  i0i2 = ipv4.Assign (devn0n2);
260 
261  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
262  i1i2 = ipv4.Assign (devn1n2);
263 
264  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
265  i2i3 = ipv4.Assign (devn2n3);
266 
267  ipv4.SetBase ("10.1.4.0", "255.255.255.0");
268  i3i4 = ipv4.Assign (devn3n4);
269 
270  ipv4.SetBase ("10.1.5.0", "255.255.255.0");
271  i3i5 = ipv4.Assign (devn3n5);
272 
273  // Set up the routing
275 
276  BuildAppsTest ();
277 
278  if (writePcap)
279  {
280  PointToPointHelper ptp;
281  std::stringstream stmp;
282  stmp << pathOut << "/pie";
283  ptp.EnablePcapAll (stmp.str ().c_str ());
284  }
285 
286  Ptr<FlowMonitor> flowmon;
287  if (flowMonitor)
288  {
289  FlowMonitorHelper flowmonHelper;
290  flowmon = flowmonHelper.InstallAll ();
291  }
292 
293  if (writeForPlot)
294  {
295  filePlotQueueDisc << pathOut << "/" << "pie-queue-disc.plotme";
296  filePlotQueueDiscAvg << pathOut << "/" << "pie-queue-disc_avg.plotme";
297 
298  remove (filePlotQueueDisc.str ().c_str ());
299  remove (filePlotQueueDiscAvg.str ().c_str ());
300  Ptr<QueueDisc> queue = queueDiscs.Get (0);
302  }
303 
305  Simulator::Run ();
306 
307  QueueDisc::Stats st = queueDiscs.Get (0)->GetStats ();
308 
310  {
311  std::cout << "There should be no drops due to queue full." << std::endl;
312  exit (1);
313  }
314 
315  if (flowMonitor)
316  {
317  std::stringstream stmp;
318  stmp << pathOut << "/pie.flowmon";
319 
320  flowmon->SerializeToXmlFile (stmp.str ().c_str (), false, false);
321  }
322 
323  if (printPieStats)
324  {
325  std::cout << "*** PIE stats from Node 2 queue ***" << std::endl;
326  std::cout << "\t " << st.GetNDroppedPackets (PieQueueDisc::UNFORCED_DROP)
327  << " drops due to prob mark" << std::endl;
328  std::cout << "\t " << st.GetNDroppedPackets (PieQueueDisc::FORCED_DROP)
329  << " drops due to queue limits" << std::endl;
330  }
331 
333 
334  return 0;
335 }
Structure that keeps the queue disc statistics.
Definition: queue-disc.h:155
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)
holds a vector of std::pair of Ptr and interface index.
double sink_start_time
Definition: pie-example.cc:53
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)
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
NodeContainer n0n2
Definition: pie-example.cc:58
void SetQueue(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue())
Each point to point net device must have a queue to pass packets through.
static constexpr const char * FORCED_DROP
Drops due to queue limit: reactive.
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#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.
LOG_INFO and above.
Definition: log.h:103
static constexpr const char * UNFORCED_DROP
Early probability drops: proactive.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
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.
double global_start_time
Definition: pie-example.cc:51
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
Holds a vector of ns3::QueueDisc pointers.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr obj.
Definition: names.cc:770
std::stringstream filePlotQueueDisc
Definition: pie-example.cc:70
Class for representing data rates.
Definition: data-rate.h:88
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
double sink_stop_time
Definition: pie-example.cc:54
Ptr< QueueDisc > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
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 ...
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
AttributeValue implementation for Time.
Definition: nstime.h:1055
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
Ipv4InterfaceContainer i3i4
Definition: pie-example.cc:67
Ipv4InterfaceContainer i0i2
Definition: pie-example.cc:64
Build a set of QueueDisc objects.
const Stats & GetStats(void)
Retrieve all the collected statistics.
Definition: queue-disc.cc:411
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
uint32_t GetNDroppedPackets(std::string reason) const
Get the number of packets dropped for the given reason.
Definition: queue-disc.cc:113
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
NodeContainer n1n2
Definition: pie-example.cc:59
Ipv4InterfaceContainer i1i2
Definition: pie-example.cc:65
NodeContainer n3n4
Definition: pie-example.cc:61
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
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...
Helper to enable IP flow monitoring on a set of Nodes.
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1564
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
Ipv4InterfaceContainer i2i3
Definition: pie-example.cc:66
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
double client_start_time
Definition: pie-example.cc:55
NodeContainer n2n3
Definition: pie-example.cc:60
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...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void AddInternalQueues(uint16_t handle, uint16_t count, 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())
Helper function used to add the given number of internal queues (of the given type and with the given...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
void SerializeToXmlFile(std::string fileName, bool enableHistograms, bool enableProbes)
Same as SerializeToXmlStream, but writes to a file instead.
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:993
double global_stop_time
Definition: pie-example.cc:52
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
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 Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
double client_stop_time
Definition: pie-example.cc:56
Ipv4InterfaceContainer i3i5
Definition: pie-example.cc:68
void BuildAppsTest()
Definition: pie-example.cc:94
std::stringstream filePlotQueueDiscAvg
Definition: pie-example.cc:71
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void CheckQueueDiscSize(Ptr< QueueDisc > queue)
Definition: pie-example.cc:74
uint32_t checkTimes
Definition: pie-example.cc:45
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
NodeContainer n3n5
Definition: pie-example.cc:62
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
double avgQueueDiscSize
Definition: pie-example.cc:48