A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
pie-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 NITK Surathkal
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Shravya Ks <shravya.ks0@gmail.com>
7 * Smriti Murali <m.smriti.95@gmail.com>
8 * Mohit P. Tahiliani <tahiliani@nitk.edu.in>
9 *
10 */
11
12/** Network topology
13 *
14 * 10Mb/s, 2ms 10Mb/s, 4ms
15 * n0--------------| |---------------n4
16 * | 1.5Mbps, 20ms |
17 * n2------------------n3
18 * 10Mb/s, 3ms | QueueLimit = 100 | 10Mb/s, 5ms
19 * n1--------------| |---------------n5
20 *
21 */
22
23#include "ns3/applications-module.h"
24#include "ns3/core-module.h"
25#include "ns3/flow-monitor-helper.h"
26#include "ns3/internet-module.h"
27#include "ns3/network-module.h"
28#include "ns3/point-to-point-module.h"
29#include "ns3/traffic-control-module.h"
30
31using namespace ns3;
32
33NS_LOG_COMPONENT_DEFINE("PieExample");
34
37
38// The times
45
51
57
58std::stringstream filePlotQueueDisc;
59std::stringstream filePlotQueueDiscAvg;
60
61void
63{
64 uint32_t qSize = queue->GetCurrentSize().GetValue();
65
67 checkTimes++;
68
69 // check queue disc size every 1/100 of a second
71
72 std::ofstream fPlotQueueDisc(filePlotQueueDisc.str(), std::ios::out | std::ios::app);
73 fPlotQueueDisc << Simulator::Now().GetSeconds() << " " << qSize << std::endl;
74 fPlotQueueDisc.close();
75
76 std::ofstream fPlotQueueDiscAvg(filePlotQueueDiscAvg.str(), std::ios::out | std::ios::app);
78 << std::endl;
79 fPlotQueueDiscAvg.close();
80}
81
82void
84{
85 // SINK is in the right side
86 uint16_t port = 50000;
88 PacketSinkHelper sinkHelper("ns3::TcpSocketFactory", sinkLocalAddress);
92
93 // Connection one
94 // Clients are in left side
95 /*
96 * Create the OnOff applications to send TCP to the server
97 * onoffhelper is a client that send data to TCP destination
98 */
99 OnOffHelper clientHelper1("ns3::TcpSocketFactory", Address());
100 clientHelper1.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
101 clientHelper1.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
102 clientHelper1.SetAttribute("PacketSize", UintegerValue(1000));
103 clientHelper1.SetAttribute("DataRate", DataRateValue(DataRate("10Mb/s")));
104
105 // Connection two
106 OnOffHelper clientHelper2("ns3::TcpSocketFactory", Address());
107 clientHelper2.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
108 clientHelper2.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
109 clientHelper2.SetAttribute("PacketSize", UintegerValue(1000));
110 clientHelper2.SetAttribute("DataRate", DataRateValue(DataRate("10Mb/s")));
111
114 clientHelper1.SetAttribute("Remote", remoteAddress);
115 clientApps1.Add(clientHelper1.Install(n0n2.Get(0)));
118
120 clientHelper2.SetAttribute("Remote", remoteAddress);
121 clientApps2.Add(clientHelper2.Install(n1n2.Get(0)));
124}
125
126int
127main(int argc, char* argv[])
128{
129 LogComponentEnable("PieQueueDisc", LOG_LEVEL_INFO);
130
131 std::string pieLinkDataRate = "1.5Mbps";
132 std::string pieLinkDelay = "20ms";
133
134 std::string pathOut;
135 bool writeForPlot = false;
136 bool writePcap = false;
137 bool flowMonitor = false;
138
139 bool printPieStats = true;
140
141 global_start_time = 0.0;
144 global_stop_time = 7.0;
147
148 // Configuration and command line parameter parsing
149 // Will only save in the directory if enable opts below
150 pathOut = "."; // Current directory
152 cmd.AddValue("pathOut",
153 "Path to save results from --writeForPlot/--writePcap/--writeFlowMonitor",
154 pathOut);
155 cmd.AddValue("writeForPlot", "<0/1> to write results for plot (gnuplot)", writeForPlot);
156 cmd.AddValue("writePcap", "<0/1> to write results in pcapfile", writePcap);
157 cmd.AddValue("writeFlowMonitor",
158 "<0/1> to enable Flow Monitor and write their results",
160
161 cmd.Parse(argc, argv);
162
163 NS_LOG_INFO("Create nodes");
165 c.Create(6);
166 Names::Add("N0", c.Get(0));
167 Names::Add("N1", c.Get(1));
168 Names::Add("N2", c.Get(2));
169 Names::Add("N3", c.Get(3));
170 Names::Add("N4", c.Get(4));
171 Names::Add("N5", c.Get(5));
172 n0n2 = NodeContainer(c.Get(0), c.Get(2));
173 n1n2 = NodeContainer(c.Get(1), c.Get(2));
174 n2n3 = NodeContainer(c.Get(2), c.Get(3));
175 n3n4 = NodeContainer(c.Get(3), c.Get(4));
176 n3n5 = NodeContainer(c.Get(3), c.Get(5));
177
178 Config::SetDefault("ns3::TcpL4Protocol::SocketType", StringValue("ns3::TcpNewReno"));
179 // 42 = headers size
180 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(1000 - 42));
181 Config::SetDefault("ns3::TcpSocket::DelAckCount", UintegerValue(1));
182 GlobalValue::Bind("ChecksumEnabled", BooleanValue(false));
183
184 uint32_t meanPktSize = 1000;
185
186 // PIE params
187 NS_LOG_INFO("Set PIE params");
188 Config::SetDefault("ns3::PieQueueDisc::MaxSize", StringValue("100p"));
189 Config::SetDefault("ns3::PieQueueDisc::MeanPktSize", UintegerValue(meanPktSize));
190 Config::SetDefault("ns3::PieQueueDisc::DequeueThreshold", UintegerValue(10000));
191 Config::SetDefault("ns3::PieQueueDisc::QueueDelayReference", TimeValue(Seconds(0.02)));
192 Config::SetDefault("ns3::PieQueueDisc::MaxBurstAllowance", TimeValue(Seconds(0.1)));
193
194 NS_LOG_INFO("Install internet stack on all nodes.");
196 internet.Install(c);
197
199 uint16_t handle = tchPfifo.SetRootQueueDisc("ns3::PfifoFastQueueDisc");
200 tchPfifo.AddInternalQueues(handle, 3, "ns3::DropTailQueue", "MaxSize", StringValue("1000p"));
201
203 tchPie.SetRootQueueDisc("ns3::PieQueueDisc");
204
205 NS_LOG_INFO("Create channels");
207
213
215
216 p2p.SetQueue("ns3::DropTailQueue");
217 p2p.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
218 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
219 devn0n2 = p2p.Install(n0n2);
220 tchPfifo.Install(devn0n2);
221
222 p2p.SetQueue("ns3::DropTailQueue");
223 p2p.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
224 p2p.SetChannelAttribute("Delay", StringValue("3ms"));
225 devn1n2 = p2p.Install(n1n2);
226 tchPfifo.Install(devn1n2);
227
228 p2p.SetQueue("ns3::DropTailQueue");
229 p2p.SetDeviceAttribute("DataRate", StringValue(pieLinkDataRate));
230 p2p.SetChannelAttribute("Delay", StringValue(pieLinkDelay));
231 devn2n3 = p2p.Install(n2n3);
232 // only backbone link has PIE queue disc
233 queueDiscs = tchPie.Install(devn2n3);
234
235 p2p.SetQueue("ns3::DropTailQueue");
236 p2p.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
237 p2p.SetChannelAttribute("Delay", StringValue("4ms"));
238 devn3n4 = p2p.Install(n3n4);
239 tchPfifo.Install(devn3n4);
240
241 p2p.SetQueue("ns3::DropTailQueue");
242 p2p.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
243 p2p.SetChannelAttribute("Delay", StringValue("5ms"));
244 devn3n5 = p2p.Install(n3n5);
245 tchPfifo.Install(devn3n5);
246
247 NS_LOG_INFO("Assign IP Addresses");
249
250 ipv4.SetBase("10.1.1.0", "255.255.255.0");
251 i0i2 = ipv4.Assign(devn0n2);
252
253 ipv4.SetBase("10.1.2.0", "255.255.255.0");
254 i1i2 = ipv4.Assign(devn1n2);
255
256 ipv4.SetBase("10.1.3.0", "255.255.255.0");
257 i2i3 = ipv4.Assign(devn2n3);
258
259 ipv4.SetBase("10.1.4.0", "255.255.255.0");
260 i3i4 = ipv4.Assign(devn3n4);
261
262 ipv4.SetBase("10.1.5.0", "255.255.255.0");
263 i3i5 = ipv4.Assign(devn3n5);
264
265 // Set up the routing
267
269
270 if (writePcap)
271 {
273 std::stringstream stmp;
274 stmp << pathOut << "/pie";
275 ptp.EnablePcapAll(stmp.str());
276 }
277
279 if (flowMonitor)
280 {
283 }
284
285 if (writeForPlot)
286 {
287 filePlotQueueDisc << pathOut << "/"
288 << "pie-queue-disc.plotme";
290 << "pie-queue-disc_avg.plotme";
291
292 remove(filePlotQueueDisc.str().c_str());
293 remove(filePlotQueueDiscAvg.str().c_str());
294 Ptr<QueueDisc> queue = queueDiscs.Get(0);
296 }
297
300
301 QueueDisc::Stats st = queueDiscs.Get(0)->GetStats();
302
303 if (st.GetNDroppedPackets(PieQueueDisc::FORCED_DROP) != 0)
304 {
305 std::cout << "There should be no drops due to queue full." << std::endl;
306 exit(1);
307 }
308
309 if (flowMonitor)
310 {
311 std::stringstream stmp;
312 stmp << pathOut << "/pie.flowmon";
313
314 flowmon->SerializeToXmlFile(stmp.str(), false, false);
315 }
316
317 if (printPieStats)
318 {
319 std::cout << "*** PIE stats from Node 2 queue ***" << std::endl;
320 std::cout << "\t " << st.GetNDroppedPackets(PieQueueDisc::UNFORCED_DROP)
321 << " drops due to prob mark" << std::endl;
322 std::cout << "\t " << st.GetNDroppedPackets(PieQueueDisc::FORCED_DROP)
323 << " drops due to queue limits" << std::endl;
324 }
325
327
328 return 0;
329}
a polymophic address class
Definition address.h:90
AttributeValue implementation for Address.
Definition address.h:275
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 Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
AttributeValue implementation for Boolean.
Definition boolean.h:26
Parse command-line arguments.
Class for representing data rates.
Definition data-rate.h:78
AttributeValue implementation for DataRate.
Definition data-rate.h:285
Helper to enable IP flow monitoring on a set of Nodes.
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
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.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
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.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
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 constexpr const char * UNFORCED_DROP
Early probability drops: proactive.
static constexpr const char * FORCED_DROP
Drops due to queue limit: reactive.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:66
Holds a vector of ns3::QueueDisc pointers.
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 Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
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
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
AttributeValue implementation for Time.
Definition nstime.h:1431
Build a set of QueueDisc objects.
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
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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_INFO
LOG_INFO and above.
Definition log.h:93
Ipv4InterfaceContainer i0i2
std::stringstream filePlotQueueDisc
void BuildAppsTest()
double client_start_time
double sink_stop_time
double sink_start_time
double global_stop_time
std::stringstream filePlotQueueDiscAvg
NodeContainer n2n3
void CheckQueueDiscSize(Ptr< QueueDisc > queue)
NodeContainer n1n2
double avgQueueDiscSize
NodeContainer n3n4
double global_start_time
Ipv4InterfaceContainer i1i2
Ipv4InterfaceContainer i3i4
NodeContainer n0n2
double client_stop_time
uint32_t checkTimes
NodeContainer n3n5
Ipv4InterfaceContainer i3i5
Ipv4InterfaceContainer i2i3
Structure that keeps the queue disc statistics.
Definition queue-disc.h:177