A Discrete-Event Network Simulator
API
codel-vs-droptail-basic-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 ResiliNets, ITTC, University of Kansas
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: Truc Anh N Nguyen <trucanh524@gmail.com>
19  *
20  */
21 
22 /*
23  * This is a basic example that compares CoDel and DropTail queues using a simple, single-flow topology:
24  *
25  * source -------------------------- router ------------------------ sink
26  * 100 Mb/s, 0.1 ms droptail 5 Mb/s, 5ms
27  * or codel bottleneck
28  *
29  * The source generates traffic across the network using BulkSendApplication.
30  * The default TCP version in ns-3, TcpNewReno, is used as the transport-layer protocol.
31  * Packets transmitted during a simulation run are captured into a .pcap file, and
32  * congestion window values are also traced.
33  */
34 
35 #include <iostream>
36 #include <fstream>
37 #include <string>
38 
39 #include "ns3/core-module.h"
40 #include "ns3/network-module.h"
41 #include "ns3/internet-module.h"
42 #include "ns3/point-to-point-module.h"
43 #include "ns3/applications-module.h"
44 #include "ns3/error-model.h"
45 #include "ns3/tcp-header.h"
46 #include "ns3/udp-header.h"
47 #include "ns3/enum.h"
48 #include "ns3/event-id.h"
49 #include "ns3/ipv4-global-routing-helper.h"
50 
51 using namespace ns3;
52 
53 NS_LOG_COMPONENT_DEFINE ("CoDelDropTailBasicTest");
54 
55 static void
56 CwndTracer (Ptr<OutputStreamWrapper>stream, uint32_t oldval, uint32_t newval)
57 {
58  *stream->GetStream () << oldval << " " << newval << std::endl;
59 }
60 
61 static void
62 TraceCwnd (std::string cwndTrFileName)
63 {
64  AsciiTraceHelper ascii;
65  if (cwndTrFileName.compare ("") == 0)
66  {
67  NS_LOG_DEBUG ("No trace file for cwnd provided");
68  return;
69  }
70  else
71  {
72  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream (cwndTrFileName.c_str ());
73  Config::ConnectWithoutContext ("/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",MakeBoundCallback (&CwndTracer, stream));
74  }
75 }
76 
77 int main (int argc, char *argv[])
78 {
79  std::string bottleneckBandwidth = "5Mbps";
80  std::string bottleneckDelay = "5ms";
81  std::string accessBandwidth = "100Mbps";
82  std::string accessDelay = "0.1ms";
83 
84  std::string queueType = "DropTail"; //DropTail or CoDel
85  uint32_t queueSize = 1000; //in packets
86  uint32_t pktSize = 1458; //in bytes. 1458 to prevent fragments
87  float startTime = 0.1;
88  float simDuration = 60; //in seconds
89 
90  bool isPcapEnabled = true;
91  std::string pcapFileName = "pcapFileDropTail.pcap";
92  std::string cwndTrFileName = "cwndDropTail.tr";
93  bool logging = false;
94 
96  cmd.AddValue ("bottleneckBandwidth", "Bottleneck bandwidth", bottleneckBandwidth);
97  cmd.AddValue ("bottleneckDelay", "Bottleneck delay", bottleneckDelay);
98  cmd.AddValue ("accessBandwidth", "Access link bandwidth", accessBandwidth);
99  cmd.AddValue ("accessDelay", "Access link delay", accessDelay);
100  cmd.AddValue ("queueType", "Queue type: DropTail, CoDel", queueType);
101  cmd.AddValue ("queueSize", "Queue size in packets", queueSize);
102  cmd.AddValue ("pktSize", "Packet size in bytes", pktSize);
103  cmd.AddValue ("startTime", "Simulation start time", startTime);
104  cmd.AddValue ("simDuration", "Simulation duration in seconds", simDuration);
105  cmd.AddValue ("isPcapEnabled", "Flag to enable/disable pcap", isPcapEnabled);
106  cmd.AddValue ("pcapFileName", "Name of pcap file", pcapFileName);
107  cmd.AddValue ("cwndTrFileName", "Name of cwnd trace file", cwndTrFileName);
108  cmd.AddValue ("logging", "Flag to enable/disable logging", logging);
109  cmd.Parse (argc, argv);
110 
111  float stopTime = startTime + simDuration;
112 
113  if (logging)
114  {
115  LogComponentEnable ("CoDelDropTailBasicTest", LOG_LEVEL_ALL);
116  LogComponentEnable ("BulkSendApplication", LOG_LEVEL_INFO);
117  LogComponentEnable ("DropTailQueue", LOG_LEVEL_ALL);
118  LogComponentEnable ("CoDelQueue", LOG_LEVEL_ALL);
119  }
120 
121  // Enable checksum
122  if (isPcapEnabled)
123  {
124  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
125  }
126 
127  // Create gateway, source, and sink
128  NodeContainer gateway;
129  gateway.Create (1);
130  NodeContainer source;
131  source.Create (1);
132  NodeContainer sink;
133  sink.Create (1);
134 
135  // Create and configure access link and bottleneck link
136  PointToPointHelper accessLink;
137  accessLink.SetDeviceAttribute ("DataRate", StringValue (accessBandwidth));
138  accessLink.SetChannelAttribute ("Delay", StringValue (accessDelay));
139 
140  PointToPointHelper bottleneckLink;
141  bottleneckLink.SetDeviceAttribute ("DataRate", StringValue (bottleneckBandwidth));
142  bottleneckLink.SetChannelAttribute ("Delay", StringValue (bottleneckDelay));
143 
144  // Configure the queue
145  if (queueType.compare ("DropTail") == 0)
146  {
147  bottleneckLink.SetQueue ("ns3::DropTailQueue",
148  "Mode", StringValue ("QUEUE_MODE_PACKETS"),
149  "MaxPackets", UintegerValue (queueSize));
150  }
151  else if (queueType.compare ("CoDel") == 0)
152  {
153  bottleneckLink.SetQueue ("ns3::CoDelQueue",
154  "Mode", StringValue ("QUEUE_MODE_PACKETS"),
155  "MaxPackets", UintegerValue (queueSize));
156  }
157  else
158  {
159  NS_LOG_DEBUG ("Invalid queue type");
160  exit (1);
161  }
162 
164  stack.InstallAll ();
165 
167  address.SetBase ("10.0.0.0", "255.255.255.0");
168 
169  // Configure the source and sink net devices
170  // and the channels between the source/sink and the gateway
171  Ipv4InterfaceContainer sinkInterface;
172 
174  devices = accessLink.Install (source.Get (0), gateway.Get (0));
175  address.NewNetwork ();
176  Ipv4InterfaceContainer interfaces = address.Assign (devices);
177  devices = bottleneckLink.Install (gateway.Get (0), sink.Get (0));
178  address.NewNetwork ();
179  interfaces = address.Assign (devices);
180 
181  sinkInterface.Add (interfaces.Get (1));
182 
183  NS_LOG_INFO ("Initialize Global Routing.");
185 
186  uint16_t port = 50000;
187  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
188  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
189 
190  // Configure application
191  AddressValue remoteAddress (InetSocketAddress (sinkInterface.GetAddress (0, 0), port));
192  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (pktSize));
193  BulkSendHelper ftp ("ns3::TcpSocketFactory", Address ());
194  ftp.SetAttribute ("Remote", remoteAddress);
195  ftp.SetAttribute ("SendSize", UintegerValue (pktSize));
196  ftp.SetAttribute ("MaxBytes", UintegerValue (0));
197 
198  ApplicationContainer sourceApp = ftp.Install (source.Get (0));
199  sourceApp.Start (Seconds (0));
200  sourceApp.Stop (Seconds (stopTime - 3));
201 
202  sinkHelper.SetAttribute ("Protocol", TypeIdValue (TcpSocketFactory::GetTypeId ()));
203  ApplicationContainer sinkApp = sinkHelper.Install (sink);
204  sinkApp.Start (Seconds (0));
205  sinkApp.Stop (Seconds (stopTime));
206 
207  Simulator::Schedule (Seconds (0.00001), &TraceCwnd, cwndTrFileName);
208 
209  if (isPcapEnabled)
210  {
211  accessLink.EnablePcap (pcapFileName,source,true);
212  }
213 
214  Simulator::Stop (Seconds (stopTime));
215  Simulator::Run ();
216 
218  return 0;
219 }
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:155
an Inet address class
static Ipv4Address GetAny(void)
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr and interface stored at the location specified by the index...
AttributeValue implementation for Boolean.
Definition: boolean.h:34
tuple devices
Definition: first.py:32
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes...
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)
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
Definition: callback.h:1677
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 void Run(void)
Run the simulation.
Definition: simulator.cc:200
#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
#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.
tuple cmd
Definition: second.py:35
double stopTime
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
static void TraceCwnd(std::string cwndTrFileName)
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:351
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1216
void InstallAll(void) const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
Hold an unsigned integer type.
Definition: uinteger.h:44
double startTime
tuple interfaces
Definition: first.py:41
holds a vector of ns3::NetDevice pointers
AttributeValue implementation for TypeId.
Definition: type-id.h:548
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:824
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...
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
static TypeId GetTypeId(void)
Get the type ID.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
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...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:491
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
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
static void CwndTracer(Ptr< OutputStreamWrapper >stream, uint32_t oldval, uint32_t newval)
Print everything.
Definition: log.h:112
Ipv4Address NewNetwork(void)
Increment the network number and reset the IP address counter to the base value provided in the SetBa...
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.
void Add(Ipv4InterfaceContainer other)
Concatenate the entries in the other container with ours.
tuple address
Definition: first.py:37
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
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