A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
codel-vs-pfifo-basic-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 ResiliNets, ITTC, University of Kansas
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Truc Anh N Nguyen <trucanh524@gmail.com>
7 * Modified by: Pasquale Imputato <p.imputato@gmail.com>
8 *
9 */
10
11/*
12 * This is a basic example that compares CoDel and PfifoFast queues using a simple, single-flow
13 * topology:
14 *
15 * source -------------------------- router ------------------------ sink
16 * 100 Mb/s, 0.1 ms pfifofast 5 Mb/s, 5ms
17 * or codel bottleneck
18 *
19 * The source generates traffic across the network using BulkSendApplication.
20 * The default TCP version in ns-3, TcpNewReno, is used as the transport-layer protocol.
21 * Packets transmitted during a simulation run are captured into a .pcap file, and
22 * congestion window values are also traced.
23 */
24
25#include "ns3/applications-module.h"
26#include "ns3/core-module.h"
27#include "ns3/enum.h"
28#include "ns3/error-model.h"
29#include "ns3/event-id.h"
30#include "ns3/internet-module.h"
31#include "ns3/ipv4-global-routing-helper.h"
32#include "ns3/network-module.h"
33#include "ns3/point-to-point-module.h"
34#include "ns3/tcp-header.h"
35#include "ns3/traffic-control-module.h"
36#include "ns3/udp-header.h"
37
38#include <fstream>
39#include <iostream>
40#include <string>
41
42using namespace ns3;
43
44NS_LOG_COMPONENT_DEFINE("CoDelPfifoFastBasicTest");
45
46/**
47 * Function called when Congestion Window is changed.
48 *
49 * \param stream Output stream.
50 * \param oldval Old value.
51 * \param newval New value.
52 */
53static void
55{
56 *stream->GetStream() << oldval << " " << newval << std::endl;
57}
58
59/**
60 * Function to enable the Congestion window tracing.
61 *
62 * Note that you can not hook to the trace before the socket is created.
63 *
64 * \param cwndTrFileName Name of the output file.
65 */
66static void
67TraceCwnd(std::string cwndTrFileName)
68{
69 AsciiTraceHelper ascii;
70 if (cwndTrFileName.empty())
71 {
72 NS_LOG_DEBUG("No trace file for cwnd provided");
73 return;
74 }
75 else
76 {
77 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream(cwndTrFileName);
79 "/NodeList/1/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",
81 }
82}
83
84int
85main(int argc, char* argv[])
86{
87 std::string bottleneckBandwidth = "5Mbps";
88 std::string bottleneckDelay = "5ms";
89 std::string accessBandwidth = "100Mbps";
90 std::string accessDelay = "0.1ms";
91
92 std::string queueDiscType = "PfifoFast"; // PfifoFast or CoDel
93 uint32_t queueDiscSize = 1000; // in packets
94 uint32_t queueSize = 10; // in packets
95 uint32_t pktSize = 1458; // in bytes. 1458 to prevent fragments
96 float startTime = 0.1F;
97 float simDuration = 60; // in seconds
98
99 bool isPcapEnabled = true;
100 std::string pcapFileName = "pcapFilePfifoFast.pcap";
101 std::string cwndTrFileName = "cwndPfifoFast.tr";
102 bool logging = false;
103
104 CommandLine cmd(__FILE__);
105 cmd.AddValue("bottleneckBandwidth", "Bottleneck bandwidth", bottleneckBandwidth);
106 cmd.AddValue("bottleneckDelay", "Bottleneck delay", bottleneckDelay);
107 cmd.AddValue("accessBandwidth", "Access link bandwidth", accessBandwidth);
108 cmd.AddValue("accessDelay", "Access link delay", accessDelay);
109 cmd.AddValue("queueDiscType", "Bottleneck queue disc type: PfifoFast, CoDel", queueDiscType);
110 cmd.AddValue("queueDiscSize", "Bottleneck queue disc size in packets", queueDiscSize);
111 cmd.AddValue("queueSize", "Devices queue size in packets", queueSize);
112 cmd.AddValue("pktSize", "Packet size in bytes", pktSize);
113 cmd.AddValue("startTime", "Simulation start time", startTime);
114 cmd.AddValue("simDuration", "Simulation duration in seconds", simDuration);
115 cmd.AddValue("isPcapEnabled", "Flag to enable/disable pcap", isPcapEnabled);
116 cmd.AddValue("pcapFileName", "Name of pcap file", pcapFileName);
117 cmd.AddValue("cwndTrFileName", "Name of cwnd trace file", cwndTrFileName);
118 cmd.AddValue("logging", "Flag to enable/disable logging", logging);
119 cmd.Parse(argc, argv);
120
121 float stopTime = startTime + simDuration;
122
123 if (logging)
124 {
125 LogComponentEnable("CoDelPfifoFastBasicTest", LOG_LEVEL_ALL);
126 LogComponentEnable("BulkSendApplication", LOG_LEVEL_INFO);
127 LogComponentEnable("PfifoFastQueueDisc", LOG_LEVEL_ALL);
128 LogComponentEnable("CoDelQueueDisc", LOG_LEVEL_ALL);
129 }
130
131 // Enable checksum
132 if (isPcapEnabled)
133 {
134 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
135 }
136
137 // Devices queue configuration
138 Config::SetDefault("ns3::DropTailQueue<Packet>::MaxSize",
139 QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, queueSize)));
140
141 // Create gateway, source, and sink
142 NodeContainer gateway;
143 gateway.Create(1);
144 NodeContainer source;
145 source.Create(1);
147 sink.Create(1);
148
149 // Create and configure access link and bottleneck link
150 PointToPointHelper accessLink;
151 accessLink.SetDeviceAttribute("DataRate", StringValue(accessBandwidth));
152 accessLink.SetChannelAttribute("Delay", StringValue(accessDelay));
153
154 PointToPointHelper bottleneckLink;
155 bottleneckLink.SetDeviceAttribute("DataRate", StringValue(bottleneckBandwidth));
156 bottleneckLink.SetChannelAttribute("Delay", StringValue(bottleneckDelay));
157
159 stack.InstallAll();
160
161 // Access link traffic control configuration
162 TrafficControlHelper tchPfifoFastAccess;
163 tchPfifoFastAccess.SetRootQueueDisc("ns3::PfifoFastQueueDisc", "MaxSize", StringValue("1000p"));
164
165 // Bottleneck link traffic control configuration
166 TrafficControlHelper tchPfifo;
167 tchPfifo.SetRootQueueDisc("ns3::PfifoFastQueueDisc",
168 "MaxSize",
169 StringValue(std::to_string(queueDiscSize) + "p"));
170
171 TrafficControlHelper tchCoDel;
172 tchCoDel.SetRootQueueDisc("ns3::CoDelQueueDisc");
173 Config::SetDefault("ns3::CoDelQueueDisc::MaxSize",
174 StringValue(std::to_string(queueDiscSize) + "p"));
175
177 address.SetBase("10.0.0.0", "255.255.255.0");
178
179 // Configure the source and sink net devices
180 // and the channels between the source/sink and the gateway
181 Ipv4InterfaceContainer sinkInterface;
182
183 NetDeviceContainer devicesAccessLink;
184 NetDeviceContainer devicesBottleneckLink;
185
186 devicesAccessLink = accessLink.Install(source.Get(0), gateway.Get(0));
187 tchPfifoFastAccess.Install(devicesAccessLink);
188 address.NewNetwork();
189 Ipv4InterfaceContainer interfaces = address.Assign(devicesAccessLink);
190
191 devicesBottleneckLink = bottleneckLink.Install(gateway.Get(0), sink.Get(0));
192 address.NewNetwork();
193
194 if (queueDiscType == "PfifoFast")
195 {
196 tchPfifo.Install(devicesBottleneckLink);
197 }
198 else if (queueDiscType == "CoDel")
199 {
200 tchCoDel.Install(devicesBottleneckLink);
201 }
202 else
203 {
205 "Invalid queue disc type: Use --queueDiscType=PfifoFast or --queueDiscType=CoDel");
206 }
207 interfaces = address.Assign(devicesBottleneckLink);
208
209 sinkInterface.Add(interfaces.Get(1));
210
211 NS_LOG_INFO("Initialize Global Routing.");
213
214 uint16_t port = 50000;
216 PacketSinkHelper sinkHelper("ns3::TcpSocketFactory", sinkLocalAddress);
217
218 // Configure application
220 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(pktSize));
221 BulkSendHelper ftp("ns3::TcpSocketFactory", Address());
222 ftp.SetAttribute("Remote", remoteAddress);
223 ftp.SetAttribute("SendSize", UintegerValue(pktSize));
224 ftp.SetAttribute("MaxBytes", UintegerValue(0));
225
226 ApplicationContainer sourceApp = ftp.Install(source.Get(0));
227 sourceApp.Start(Seconds(0));
228 sourceApp.Stop(Seconds(stopTime - 3));
229
230 sinkHelper.SetAttribute("Protocol", TypeIdValue(TcpSocketFactory::GetTypeId()));
231 ApplicationContainer sinkApp = sinkHelper.Install(sink);
232 sinkApp.Start(Seconds(0));
233 sinkApp.Stop(Seconds(stopTime));
234
235 Simulator::Schedule(Seconds(0.00001), &TraceCwnd, cwndTrFileName);
236
237 if (isPcapEnabled)
238 {
239 accessLink.EnablePcap(pcapFileName, source, true);
240 }
241
244
246 return 0;
247}
a polymophic address class
Definition address.h:90
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.
Manage ASCII trace files for device models.
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.
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes.
Parse command-line arguments.
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...
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
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.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
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::PacketSinkApplication on a set of nodes.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
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)
Smart pointer class similar to boost::intrusive_ptr.
Class for representing queue sizes.
Definition queue-size.h:85
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
static TypeId GetTypeId()
Get the type ID.
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.
Hold an unsigned integer type.
Definition uinteger.h:34
static void TraceCwnd(std::string cwndTrFileName)
Function to enable the Congestion window tracing.
static void CwndTracer(Ptr< OutputStreamWrapper > stream, uint32_t oldval, uint32_t newval)
Function called when Congestion Window is changed.
uint16_t port
Definition dsdv-manet.cc:33
Time stopTime
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:943
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
address
Definition first.py:36
stack
Definition first.py:33
interfaces
Definition first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
std::ofstream queueSize
uint32_t pktSize
packet size used for the simulation (in bytes)
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44