A Discrete-Event Network Simulator
API
cobalt-vs-codel.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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  * Author: Shefali Gupta <shefaligups11@ogmail.com>
18  * Jendaipou Palmei <jendaipoupalmei@gmail.com>
19  * Mohit P. Tahiliani <tahiliani@nitk.edu.in>
20  */
21 
22 #include "ns3/applications-module.h"
23 #include "ns3/core-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/ipv6-routing-table-entry.h"
26 #include "ns3/ipv6-static-routing-helper.h"
27 #include "ns3/network-module.h"
28 #include "ns3/point-to-point-module.h"
29 #include "ns3/tcp-header.h"
30 #include "ns3/traffic-control-module.h"
31 
32 #include <fstream>
33 #include <string>
34 
35 // Dumbbell topology with 7 senders and 1 receiver
36 // is used for this example. On successful completion,
37 // the Congestion window and Queue size traces get stored
38 // in MixTraffic/ directory, inside cwndTraces and
39 // queueTraces sub-directories, respectively.
40 
41 using namespace ns3;
42 
43 std::string dir = "MixTraffic/";
44 
45 void
46 CheckQueueSize(Ptr<QueueDisc> queue, std::string queue_disc_type)
47 {
48  double qSize = queue->GetCurrentSize().GetValue();
49  // check queue size every 1/10 of a second
50  Simulator::Schedule(Seconds(0.1), &CheckQueueSize, queue, queue_disc_type);
51 
52  std::ofstream fPlotQueue(dir + queue_disc_type + "/queueTraces/queue.plotme",
53  std::ios::out | std::ios::app);
54  fPlotQueue << Simulator::Now().GetSeconds() << " " << qSize << std::endl;
55  fPlotQueue.close();
56 }
57 
58 static void
59 CwndTrace(Ptr<OutputStreamWrapper> stream, uint32_t oldCwnd, uint32_t newCwnd)
60 {
61  *stream->GetStream() << Simulator::Now().GetSeconds() << " " << newCwnd / 1446.0 << std::endl;
62 }
63 
64 static void
65 TraceCwnd(std::string queue_disc_type)
66 {
67  for (uint8_t i = 0; i < 5; i++)
68  {
69  AsciiTraceHelper asciiTraceHelper;
70  Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream(
71  dir + queue_disc_type + "/cwndTraces/S1-" + std::to_string(i + 1) + ".plotme");
72  Config::ConnectWithoutContext("/NodeList/" + std::to_string(i) +
73  "/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",
74  MakeBoundCallback(&CwndTrace, stream));
75  }
76 }
77 
78 void
79 experiment(std::string queue_disc_type)
80 {
81  // Set the simulation stop time in seconds
82  double stopTime = 101;
83  std::string queue_disc = std::string("ns3::") + queue_disc_type;
84 
85  std::string bottleneckBandwidth = "10Mbps";
86  std::string bottleneckDelay = "50ms";
87 
88  std::string accessBandwidth = "10Mbps";
89  std::string accessDelay = "5ms";
90 
91  // Create sender
92  NodeContainer tcpSender;
93  tcpSender.Create(5);
94 
95  NodeContainer udpSender;
96  udpSender.Create(2);
97 
98  // Create gateway
99  NodeContainer gateway;
100  gateway.Create(2);
101 
102  // Create sink
104  sink.Create(1);
105 
106  Config::SetDefault("ns3::TcpSocket::SndBufSize", UintegerValue(1 << 20));
107  Config::SetDefault("ns3::TcpSocket::RcvBufSize", UintegerValue(1 << 20));
108  Config::SetDefault("ns3::TcpSocket::DelAckTimeout", TimeValue(Seconds(0)));
109  Config::SetDefault("ns3::TcpSocket::InitialCwnd", UintegerValue(1));
110  Config::SetDefault("ns3::TcpSocketBase::LimitedTransmit", BooleanValue(false));
111  Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(1446));
112  Config::SetDefault("ns3::TcpSocketBase::WindowScaling", BooleanValue(true));
113  Config::SetDefault(queue_disc + "::MaxSize", QueueSizeValue(QueueSize("200p")));
114 
115  InternetStackHelper internet;
116  internet.InstallAll();
117 
118  TrafficControlHelper tchPfifo;
119  uint16_t handle = tchPfifo.SetRootQueueDisc("ns3::PfifoFastQueueDisc");
120  tchPfifo.AddInternalQueues(handle, 3, "ns3::DropTailQueue", "MaxSize", StringValue("1000p"));
121 
123  tch.SetRootQueueDisc(queue_disc);
124 
125  PointToPointHelper accessLink;
126  accessLink.SetDeviceAttribute("DataRate", StringValue(accessBandwidth));
127  accessLink.SetChannelAttribute("Delay", StringValue(accessDelay));
128 
129  // Configure the senders and sinks net devices
130  // and the channels between the senders/sinks and the gateways
132  for (uint8_t i = 0; i < 5; i++)
133  {
134  devices[i] = accessLink.Install(tcpSender.Get(i), gateway.Get(0));
135  tchPfifo.Install(devices[i]);
136  }
137 
138  NetDeviceContainer devices_sink;
139  devices_sink = accessLink.Install(gateway.Get(1), sink.Get(0));
140  tchPfifo.Install(devices_sink);
141 
142  PointToPointHelper bottleneckLink;
143  bottleneckLink.SetDeviceAttribute("DataRate", StringValue(bottleneckBandwidth));
144  bottleneckLink.SetChannelAttribute("Delay", StringValue(bottleneckDelay));
145 
146  NetDeviceContainer devices_gateway;
147  devices_gateway = bottleneckLink.Install(gateway.Get(0), gateway.Get(1));
148  // Install QueueDisc at gateway
149  QueueDiscContainer queueDiscs = tch.Install(devices_gateway);
150 
152  address.SetBase("10.0.0.0", "255.255.255.0");
153 
155  Ipv4InterfaceContainer interfaces_sink;
156  Ipv4InterfaceContainer interfaces_gateway;
157  Ipv4InterfaceContainer udpinterfaces[2];
158 
159  NetDeviceContainer udpdevices[2];
160 
161  for (uint8_t i = 0; i < 5; i++)
162  {
163  address.NewNetwork();
164  interfaces[i] = address.Assign(devices[i]);
165  }
166 
167  for (uint8_t i = 0; i < 2; i++)
168  {
169  udpdevices[i] = accessLink.Install(udpSender.Get(i), gateway.Get(0));
170  address.NewNetwork();
171  udpinterfaces[i] = address.Assign(udpdevices[i]);
172  }
173 
174  address.NewNetwork();
175  interfaces_gateway = address.Assign(devices_gateway);
176 
177  address.NewNetwork();
178  interfaces_sink = address.Assign(devices_sink);
179 
181 
182  uint16_t port = 50000;
183  uint16_t port1 = 50001;
184  Address sinkLocalAddress(InetSocketAddress(Ipv4Address::GetAny(), port));
185  Address sinkLocalAddress1(InetSocketAddress(Ipv4Address::GetAny(), port1));
186  PacketSinkHelper sinkHelper("ns3::TcpSocketFactory", sinkLocalAddress);
187  PacketSinkHelper sinkHelper1("ns3::UdpSocketFactory", sinkLocalAddress1);
188 
189  AddressValue remoteAddress(InetSocketAddress(interfaces_sink.GetAddress(1), port));
190  AddressValue remoteAddress1(InetSocketAddress(interfaces_sink.GetAddress(1), port1));
191 
192  BulkSendHelper ftp("ns3::TcpSocketFactory", Address());
193  ftp.SetAttribute("Remote", remoteAddress);
194  ftp.SetAttribute("SendSize", UintegerValue(1000));
195 
196  ApplicationContainer sourceApp = ftp.Install(tcpSender);
197  sourceApp.Start(Seconds(0));
198  sourceApp.Stop(Seconds(stopTime - 1));
199 
200  sinkHelper.SetAttribute("Protocol", TypeIdValue(TcpSocketFactory::GetTypeId()));
201  ApplicationContainer sinkApp = sinkHelper.Install(sink);
202  sinkApp.Start(Seconds(0));
203  sinkApp.Stop(Seconds(stopTime));
204 
205  OnOffHelper clientHelper6("ns3::UdpSocketFactory", Address());
206  clientHelper6.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
207  clientHelper6.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
208  clientHelper6.SetAttribute("DataRate", DataRateValue(DataRate("10Mb/s")));
209  clientHelper6.SetAttribute("PacketSize", UintegerValue(1000));
210 
211  ApplicationContainer clientApps6;
212  clientHelper6.SetAttribute("Remote", remoteAddress1);
213  clientApps6.Add(clientHelper6.Install(udpSender.Get(0)));
214  clientApps6.Start(Seconds(0));
215  clientApps6.Stop(Seconds(stopTime - 1));
216 
217  OnOffHelper clientHelper7("ns3::UdpSocketFactory", Address());
218  clientHelper7.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
219  clientHelper7.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
220  clientHelper7.SetAttribute("DataRate", DataRateValue(DataRate("10Mb/s")));
221  clientHelper7.SetAttribute("PacketSize", UintegerValue(1000));
222 
223  ApplicationContainer clientApps7;
224  clientHelper7.SetAttribute("Remote", remoteAddress1);
225  clientApps7.Add(clientHelper7.Install(udpSender.Get(1)));
226  clientApps7.Start(Seconds(0));
227  clientApps7.Stop(Seconds(stopTime - 1));
228 
229  sinkHelper1.SetAttribute("Protocol", TypeIdValue(UdpSocketFactory::GetTypeId()));
230  ApplicationContainer sinkApp1 = sinkHelper1.Install(sink);
231  sinkApp1.Start(Seconds(0));
232  sinkApp1.Stop(Seconds(stopTime));
233 
234  Ptr<QueueDisc> queue = queueDiscs.Get(0);
235  Simulator::ScheduleNow(&CheckQueueSize, queue, queue_disc_type);
236 
237  std::string dirToSave = "mkdir -p " + dir + queue_disc_type;
238  if (system((dirToSave + "/cwndTraces/").c_str()) == -1 ||
239  system((dirToSave + "/queueTraces/").c_str()) == -1)
240  {
241  exit(1);
242  }
243 
244  Simulator::Schedule(Seconds(0.1), &TraceCwnd, queue_disc_type);
245 
247  Simulator::Run();
249 }
250 
251 int
252 main(int argc, char** argv)
253 {
254  std::cout << "Simulation with COBALT QueueDisc: Start\n" << std::flush;
255  experiment("CobaltQueueDisc");
256  std::cout << "Simulation with COBALT QueueDisc: End\n" << std::flush;
257  std::cout << "------------------------------------------------\n";
258  std::cout << "Simulation with CoDel QueueDisc: Start\n";
259  experiment("CoDelQueueDisc");
260  std::cout << "Simulation with CoDel QueueDisc: End\n";
261 
262  return 0;
263 }
a polymophic address class
Definition: address.h:100
AttributeValue implementation for Address.
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.
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes, not the socket attributes.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::BulkSendApplication on each node of the input container configured with all the attri...
AttributeValue implementation for DataRate.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void InstallAll() const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
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.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
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.
std::ostream * GetStream()
Return a pointer to an ostream previously set in the wrapper.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
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.
NetDeviceContainer Install(NodeContainer c)
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.
QueueSize GetCurrentSize()
Get the current size of the queue disc in bytes, if operating in bytes mode, or packets,...
Definition: queue-disc.cc:519
Class for representing queue sizes.
Definition: queue-size.h:96
uint32_t GetValue() const
Get the underlying value.
Definition: queue-size.cc:183
AttributeValue implementation for QueueSize.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
static void Run()
Run the simulation.
Definition: simulator.cc:176
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:606
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
Hold variables of type string.
Definition: string.h:56
static TypeId GetTypeId()
Get the type ID.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
AttributeValue implementation for Time.
Definition: nstime.h:1423
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.
void AddInternalQueues(uint16_t handle, uint16_t count, std::string type, Args &&... args)
Helper function used to add the given number of internal queues (of the given type and with the given...
AttributeValue implementation for TypeId.
Definition: type-id.h:600
static TypeId GetTypeId()
Get the type ID.
Hold an unsigned integer type.
Definition: uinteger.h:45
void CheckQueueSize(Ptr< QueueDisc > queue, std::string queue_disc_type)
void experiment(std::string queue_disc_type)
static void TraceCwnd(std::string queue_disc_type)
static void CwndTrace(Ptr< OutputStreamWrapper > stream, uint32_t oldCwnd, uint32_t newCwnd)
std::string dir
uint16_t port
Definition: dsdv-manet.cc:45
Time stopTime
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:951
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition: callback.h:768
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
address
Definition: first.py:40
devices
Definition: first.py:35
interfaces
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55