File: queue-discs-benchmark.cc

package info (click to toggle)
ns3 3.46-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 105,864 kB
  • sloc: cpp: 624,863; python: 14,863; ansic: 6,772; makefile: 1,950; sh: 987; javascript: 167; perl: 102
file content (361 lines) | stat: -rw-r--r-- 14,515 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright (c) 2015 Universita' degli Studi di Napoli Federico II
 *
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Authors: Pasquale Imputato <p.imputato@gmail.com>
 *          Stefano Avallone <stefano.avallone@unina.it>
 */

// This example serves as a benchmark for all the queue discs (with BQL enabled or not)
//
// Network topology
//
//                192.168.1.0                             192.168.2.0
// n1 ------------------------------------ n2 ----------------------------------- n3
//   point-to-point (access link)                point-to-point (bottleneck link)
//   100 Mbps, 0.1 ms                            bandwidth [10 Mbps], delay [5 ms]
//   qdiscs PfifoFast with capacity              qdiscs queueDiscType in {PfifoFast, ARED, CoDel,
//   FqCoDel, PIE} [PfifoFast] of 1000 packets                             with capacity of
//   queueDiscSize packets [1000] netdevices queues with size of 100 packets  netdevices queues with
//   size of netdevicesQueueSize packets [100] without BQL                                 bql BQL
//   [false]
//   *** fixed configuration ***
//
// Two TCP flows are generated: one from n1 to n3 and the other from n3 to n1.
// Additionally, n1 pings n3, so that the RTT can be measured.
//
// The output will consist of a number of ping Rtt such as:
//
//    /NodeList/0/ApplicationList/2/$ns3::Ping/Rtt=111 ms
//    /NodeList/0/ApplicationList/2/$ns3::Ping/Rtt=111 ms
//    /NodeList/0/ApplicationList/2/$ns3::Ping/Rtt=110 ms
//    /NodeList/0/ApplicationList/2/$ns3::Ping/Rtt=111 ms
//    /NodeList/0/ApplicationList/2/$ns3::Ping/Rtt=111 ms
//    /NodeList/0/ApplicationList/2/$ns3::Ping/Rtt=112 ms
//    /NodeList/0/ApplicationList/2/$ns3::Ping/Rtt=111 ms
//
// The files output will consist of a trace file with bytes in queue and of a trace file for limits
// (when BQL is enabled) both for bottleneck NetDevice on n2, two files with upload and download
// goodput for flows configuration and a file with flow monitor stats.
//
// If you use an AQM as queue disc on the bottleneck netdevices, you can observe that the ping Rtt
// decrease. A further decrease can be observed when you enable BQL.

#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/internet-apps-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/traffic-control-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("BenchmarkQueueDiscs");

/**
 * Print the queue limits.
 *
 * @param stream The output stream.
 * @param oldVal Old value.
 * @param newVal New value.
 */
void
LimitsTrace(Ptr<OutputStreamWrapper> stream, uint32_t oldVal, uint32_t newVal)
{
    *stream->GetStream() << Simulator::Now().GetSeconds() << " " << newVal << std::endl;
}

/**
 * Print the bytes in the queue.
 *
 * @param stream The output stream.
 * @param oldVal Old value.
 * @param newVal New value.
 */
void
BytesInQueueTrace(Ptr<OutputStreamWrapper> stream, uint32_t oldVal, uint32_t newVal)
{
    *stream->GetStream() << Simulator::Now().GetSeconds() << " " << newVal << std::endl;
}

/**
 * Sample and print the queue goodput.
 *
 * @param app The Tx app.
 * @param stream The output stream.
 * @param period The sampling period.
 */
static void
GoodputSampling(ApplicationContainer app, Ptr<OutputStreamWrapper> stream, float period)
{
    Simulator::Schedule(Seconds(period), &GoodputSampling, app, stream, period);
    double goodput;
    uint64_t totalPackets = DynamicCast<PacketSink>(app.Get(0))->GetTotalRx();
    goodput = totalPackets * 8 / (Simulator::Now().GetSeconds() * 1024); // Kbit/s
    *stream->GetStream() << Simulator::Now().GetSeconds() << " " << goodput << std::endl;
}

/**
 * Print the ping RTT.
 *
 * @param context The context.
 * @param rtt The RTT.
 */
static void
PingRtt(std::string context, uint16_t, Time rtt)
{
    std::cout << context << "=" << rtt.GetMilliSeconds() << " ms" << std::endl;
}

int
main(int argc, char* argv[])
{
    std::string bandwidth = "10Mbps";
    std::string delay = "5ms";
    std::string queueDiscType = "PfifoFast";
    uint32_t queueDiscSize = 1000;
    uint32_t netdevicesQueueSize = 50;
    bool bql = false;

    std::string flowsDatarate = "20Mbps";
    uint32_t flowsPacketsSize = 1000;

    float startTime = 0.1F; // in s
    float simDuration = 60;
    float samplingPeriod = 1;

    CommandLine cmd(__FILE__);
    cmd.AddValue("bandwidth", "Bottleneck bandwidth", bandwidth);
    cmd.AddValue("delay", "Bottleneck delay", delay);
    cmd.AddValue("queueDiscType",
                 "Bottleneck queue disc type in {PfifoFast, ARED, CoDel, FqCoDel, PIE, prio}",
                 queueDiscType);
    cmd.AddValue("queueDiscSize", "Bottleneck queue disc size in packets", queueDiscSize);
    cmd.AddValue("netdevicesQueueSize",
                 "Bottleneck netdevices queue size in packets",
                 netdevicesQueueSize);
    cmd.AddValue("bql", "Enable byte queue limits on bottleneck netdevices", bql);
    cmd.AddValue("flowsDatarate", "Upload and download flows datarate", flowsDatarate);
    cmd.AddValue("flowsPacketsSize", "Upload and download flows packets sizes", flowsPacketsSize);
    cmd.AddValue("startTime", "Simulation start time", startTime);
    cmd.AddValue("simDuration", "Simulation duration in seconds", simDuration);
    cmd.AddValue("samplingPeriod", "Goodput sampling period in seconds", samplingPeriod);
    cmd.Parse(argc, argv);

    float stopTime = startTime + simDuration;

    // Create nodes
    NodeContainer n1;
    NodeContainer n2;
    NodeContainer n3;
    n1.Create(1);
    n2.Create(1);
    n3.Create(1);

    // Create and configure access link and bottleneck link
    PointToPointHelper accessLink;
    accessLink.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
    accessLink.SetChannelAttribute("Delay", StringValue("0.1ms"));
    accessLink.SetQueue("ns3::DropTailQueue", "MaxSize", StringValue("100p"));

    PointToPointHelper bottleneckLink;
    bottleneckLink.SetDeviceAttribute("DataRate", StringValue(bandwidth));
    bottleneckLink.SetChannelAttribute("Delay", StringValue(delay));
    bottleneckLink.SetQueue("ns3::DropTailQueue",
                            "MaxSize",
                            StringValue(std::to_string(netdevicesQueueSize) + "p"));

    InternetStackHelper stack;
    stack.InstallAll();

    // Access link traffic control configuration
    TrafficControlHelper tchPfifoFastAccess;
    tchPfifoFastAccess.SetRootQueueDisc("ns3::PfifoFastQueueDisc", "MaxSize", StringValue("1000p"));

    // Bottleneck link traffic control configuration
    TrafficControlHelper tchBottleneck;

    if (queueDiscType == "PfifoFast")
    {
        tchBottleneck.SetRootQueueDisc(
            "ns3::PfifoFastQueueDisc",
            "MaxSize",
            QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, queueDiscSize)));
    }
    else if (queueDiscType == "ARED")
    {
        tchBottleneck.SetRootQueueDisc("ns3::RedQueueDisc");
        Config::SetDefault("ns3::RedQueueDisc::ARED", BooleanValue(true));
        Config::SetDefault("ns3::RedQueueDisc::MaxSize",
                           QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, queueDiscSize)));
    }
    else if (queueDiscType == "CoDel")
    {
        tchBottleneck.SetRootQueueDisc("ns3::CoDelQueueDisc");
        Config::SetDefault("ns3::CoDelQueueDisc::MaxSize",
                           QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, queueDiscSize)));
    }
    else if (queueDiscType == "FqCoDel")
    {
        tchBottleneck.SetRootQueueDisc("ns3::FqCoDelQueueDisc");
        Config::SetDefault("ns3::FqCoDelQueueDisc::MaxSize",
                           QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, queueDiscSize)));
    }
    else if (queueDiscType == "PIE")
    {
        tchBottleneck.SetRootQueueDisc("ns3::PieQueueDisc");
        Config::SetDefault("ns3::PieQueueDisc::MaxSize",
                           QueueSizeValue(QueueSize(QueueSizeUnit::PACKETS, queueDiscSize)));
    }
    else if (queueDiscType == "prio")
    {
        uint16_t handle =
            tchBottleneck.SetRootQueueDisc("ns3::PrioQueueDisc",
                                           "Priomap",
                                           StringValue("0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1"));
        TrafficControlHelper::ClassIdList cid =
            tchBottleneck.AddQueueDiscClasses(handle, 2, "ns3::QueueDiscClass");
        tchBottleneck.AddChildQueueDisc(handle, cid[0], "ns3::FifoQueueDisc");
        tchBottleneck.AddChildQueueDisc(handle, cid[1], "ns3::RedQueueDisc");
    }
    else
    {
        NS_ABORT_MSG("--queueDiscType not valid");
    }

    if (bql)
    {
        tchBottleneck.SetQueueLimits("ns3::DynamicQueueLimits");
    }

    NetDeviceContainer devicesAccessLink = accessLink.Install(n1.Get(0), n2.Get(0));
    tchPfifoFastAccess.Install(devicesAccessLink);
    Ipv4AddressHelper address;
    address.SetBase("192.168.0.0", "255.255.255.0");
    address.NewNetwork();
    Ipv4InterfaceContainer interfacesAccess = address.Assign(devicesAccessLink);

    NetDeviceContainer devicesBottleneckLink = bottleneckLink.Install(n2.Get(0), n3.Get(0));
    QueueDiscContainer qdiscs;
    qdiscs = tchBottleneck.Install(devicesBottleneckLink);

    address.NewNetwork();
    Ipv4InterfaceContainer interfacesBottleneck = address.Assign(devicesBottleneckLink);

    Ptr<NetDeviceQueueInterface> interface =
        devicesBottleneckLink.Get(0)->GetObject<NetDeviceQueueInterface>();
    Ptr<NetDeviceQueue> queueInterface = interface->GetTxQueue(0);
    Ptr<DynamicQueueLimits> queueLimits =
        StaticCast<DynamicQueueLimits>(queueInterface->GetQueueLimits());

    AsciiTraceHelper ascii;
    if (bql)
    {
        queueDiscType = queueDiscType + "-bql";
        Ptr<OutputStreamWrapper> streamLimits =
            ascii.CreateFileStream(queueDiscType + "-limits.txt");
        queueLimits->TraceConnectWithoutContext("Limit",
                                                MakeBoundCallback(&LimitsTrace, streamLimits));
    }
    Ptr<Queue<Packet>> queue =
        StaticCast<PointToPointNetDevice>(devicesBottleneckLink.Get(0))->GetQueue();
    Ptr<OutputStreamWrapper> streamBytesInQueue =
        ascii.CreateFileStream(queueDiscType + "-bytesInQueue.txt");
    queue->TraceConnectWithoutContext("BytesInQueue",
                                      MakeBoundCallback(&BytesInQueueTrace, streamBytesInQueue));

    Ipv4InterfaceContainer n1Interface;
    n1Interface.Add(interfacesAccess.Get(0));

    Ipv4InterfaceContainer n3Interface;
    n3Interface.Add(interfacesBottleneck.Get(1));

    Ipv4GlobalRoutingHelper::PopulateRoutingTables();

    Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(flowsPacketsSize));

    // Flows configuration
    // Bidirectional TCP streams with ping like flent tcp_bidirectional test.
    uint16_t port = 7;
    ApplicationContainer uploadApp;
    ApplicationContainer downloadApp;
    ApplicationContainer sourceApps;
    // Configure and install upload flow
    Address addUp(InetSocketAddress(Ipv4Address::GetAny(), port));
    PacketSinkHelper sinkHelperUp("ns3::TcpSocketFactory", addUp);
    sinkHelperUp.SetAttribute("Protocol", TypeIdValue(TcpSocketFactory::GetTypeId()));
    uploadApp.Add(sinkHelperUp.Install(n3));

    InetSocketAddress socketAddressUp = InetSocketAddress(n3Interface.GetAddress(0), port);
    OnOffHelper onOffHelperUp("ns3::TcpSocketFactory", Address());
    onOffHelperUp.SetAttribute("Remote", AddressValue(socketAddressUp));
    onOffHelperUp.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
    onOffHelperUp.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
    onOffHelperUp.SetAttribute("PacketSize", UintegerValue(flowsPacketsSize));
    onOffHelperUp.SetAttribute("DataRate", StringValue(flowsDatarate));
    sourceApps.Add(onOffHelperUp.Install(n1));

    port = 8;
    // Configure and install download flow
    Address addDown(InetSocketAddress(Ipv4Address::GetAny(), port));
    PacketSinkHelper sinkHelperDown("ns3::TcpSocketFactory", addDown);
    sinkHelperDown.SetAttribute("Protocol", TypeIdValue(TcpSocketFactory::GetTypeId()));
    downloadApp.Add(sinkHelperDown.Install(n1));

    InetSocketAddress socketAddressDown = InetSocketAddress(n1Interface.GetAddress(0), port);
    OnOffHelper onOffHelperDown("ns3::TcpSocketFactory", Address());
    onOffHelperDown.SetAttribute("Remote", AddressValue(socketAddressDown));
    onOffHelperDown.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
    onOffHelperDown.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
    onOffHelperDown.SetAttribute("PacketSize", UintegerValue(flowsPacketsSize));
    onOffHelperDown.SetAttribute("DataRate", StringValue(flowsDatarate));
    sourceApps.Add(onOffHelperDown.Install(n3));

    // Configure and install ping
    PingHelper ping(n3Interface.GetAddress(0));
    ping.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::QUIET));
    ping.Install(n1);

    Config::Connect("/NodeList/*/ApplicationList/*/$ns3::Ping/Rtt", MakeCallback(&PingRtt));

    uploadApp.Start(Seconds(0));
    uploadApp.Stop(Seconds(stopTime));
    downloadApp.Start(Seconds(0));
    downloadApp.Stop(Seconds(stopTime));

    sourceApps.Start(Seconds(0 + 0.1));
    sourceApps.Stop(Seconds(stopTime - 0.1));

    Ptr<OutputStreamWrapper> uploadGoodputStream =
        ascii.CreateFileStream(queueDiscType + "-upGoodput.txt");
    Simulator::Schedule(Seconds(samplingPeriod),
                        &GoodputSampling,
                        uploadApp,
                        uploadGoodputStream,
                        samplingPeriod);
    Ptr<OutputStreamWrapper> downloadGoodputStream =
        ascii.CreateFileStream(queueDiscType + "-downGoodput.txt");
    Simulator::Schedule(Seconds(samplingPeriod),
                        &GoodputSampling,
                        downloadApp,
                        downloadGoodputStream,
                        samplingPeriod);

    // Flow monitor
    Ptr<FlowMonitor> flowMonitor;
    FlowMonitorHelper flowHelper;
    flowMonitor = flowHelper.InstallAll();

    accessLink.EnablePcapAll("queue");

    Simulator::Stop(Seconds(stopTime));
    Simulator::Run();

    flowMonitor->SerializeToXmlFile(queueDiscType + "-flowMonitor.xml", true, true);

    Simulator::Destroy();
    return 0;
}