A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
traffic-control.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Universita' degli Studi di Napoli "Federico II"
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: Pasquale Imputato <p.imputato@gmail.com>
18 * Author: Stefano Avallone <stefano.avallone@unina.it>
19 */
20
21#include "ns3/applications-module.h"
22#include "ns3/core-module.h"
23#include "ns3/flow-monitor-module.h"
24#include "ns3/internet-module.h"
25#include "ns3/network-module.h"
26#include "ns3/point-to-point-module.h"
27#include "ns3/traffic-control-module.h"
28
29// This simple example shows how to use TrafficControlHelper to install a
30// QueueDisc on a device.
31//
32// The default QueueDisc is a pfifo_fast with a capacity of 1000 packets (as in
33// Linux). However, in this example, we install a RedQueueDisc with a capacity
34// of 10000 packets.
35//
36// Network topology
37//
38// 10.1.1.0
39// n0 -------------- n1
40// point-to-point
41//
42// The output will consist of all the traced changes in the length of the RED
43// internal queue and in the length of the netdevice queue:
44//
45// DevicePacketsInQueue 0 to 1
46// TcPacketsInQueue 7 to 8
47// TcPacketsInQueue 8 to 9
48// DevicePacketsInQueue 1 to 0
49// TcPacketsInQueue 9 to 8
50//
51// plus some statistics collected at the network layer (by the flow monitor)
52// and the application layer. Finally, the number of packets dropped by the
53// queuing discipline, the number of packets dropped by the netdevice and
54// the number of packets requeued by the queuing discipline are reported.
55//
56// If the size of the DropTail queue of the netdevice were increased from 1
57// to a large number (e.g. 1000), one would observe that the number of dropped
58// packets goes to zero, but the latency grows in an uncontrolled manner. This
59// is the so-called bufferbloat problem, and illustrates the importance of
60// having a small device queue, so that the standing queues build in the traffic
61// control layer where they can be managed by advanced queue discs rather than
62// in the device layer.
63
64using namespace ns3;
65
66NS_LOG_COMPONENT_DEFINE("TrafficControlExample");
67
74void
76{
77 std::cout << "TcPacketsInQueue " << oldValue << " to " << newValue << std::endl;
78}
79
86void
88{
89 std::cout << "DevicePacketsInQueue " << oldValue << " to " << newValue << std::endl;
90}
91
97void
99{
100 std::cout << "Sojourn time " << sojournTime.ToDouble(Time::MS) << "ms" << std::endl;
101}
102
103int
104main(int argc, char* argv[])
105{
106 double simulationTime = 10; // seconds
107 std::string transportProt = "Tcp";
108 std::string socketType;
109
110 CommandLine cmd(__FILE__);
111 cmd.AddValue("transportProt", "Transport protocol to use: Tcp, Udp", transportProt);
112 cmd.Parse(argc, argv);
113
114 if (transportProt == "Tcp")
115 {
116 socketType = "ns3::TcpSocketFactory";
117 }
118 else
119 {
120 socketType = "ns3::UdpSocketFactory";
121 }
122
124 nodes.Create(2);
125
127 pointToPoint.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
128 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
129 pointToPoint.SetQueue("ns3::DropTailQueue", "MaxSize", StringValue("1p"));
130
132 devices = pointToPoint.Install(nodes);
133
135 stack.Install(nodes);
136
138 tch.SetRootQueueDisc("ns3::RedQueueDisc");
139 QueueDiscContainer qdiscs = tch.Install(devices);
140
141 Ptr<QueueDisc> q = qdiscs.Get(1);
142 q->TraceConnectWithoutContext("PacketsInQueue", MakeCallback(&TcPacketsInQueueTrace));
144 "/NodeList/1/$ns3::TrafficControlLayer/RootQueueDiscList/0/SojournTime",
146
147 Ptr<NetDevice> nd = devices.Get(1);
148 Ptr<PointToPointNetDevice> ptpnd = DynamicCast<PointToPointNetDevice>(nd);
149 Ptr<Queue<Packet>> queue = ptpnd->GetQueue();
150 queue->TraceConnectWithoutContext("PacketsInQueue", MakeCallback(&DevicePacketsInQueueTrace));
151
153 address.SetBase("10.1.1.0", "255.255.255.0");
154
155 Ipv4InterfaceContainer interfaces = address.Assign(devices);
156
157 // Flow
158 uint16_t port = 7;
160 PacketSinkHelper packetSinkHelper(socketType, localAddress);
161 ApplicationContainer sinkApp = packetSinkHelper.Install(nodes.Get(0));
162
163 sinkApp.Start(Seconds(0.0));
164 sinkApp.Stop(Seconds(simulationTime + 0.1));
165
166 uint32_t payloadSize = 1448;
167 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
168
170 onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
171 onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
172 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
173 onoff.SetAttribute("DataRate", StringValue("50Mbps")); // bit/s
175
176 InetSocketAddress rmt(interfaces.GetAddress(0), port);
177 onoff.SetAttribute("Remote", AddressValue(rmt));
178 onoff.SetAttribute("Tos", UintegerValue(0xb8));
179 apps.Add(onoff.Install(nodes.Get(1)));
180 apps.Start(Seconds(1.0));
181 apps.Stop(Seconds(simulationTime + 0.1));
182
183 FlowMonitorHelper flowmon;
184 Ptr<FlowMonitor> monitor = flowmon.InstallAll();
185
186 Simulator::Stop(Seconds(simulationTime + 5));
188
189 Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>(flowmon.GetClassifier());
190 std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats();
191 std::cout << std::endl << "*** Flow monitor statistics ***" << std::endl;
192 std::cout << " Tx Packets/Bytes: " << stats[1].txPackets << " / " << stats[1].txBytes
193 << std::endl;
194 std::cout << " Offered Load: "
195 << stats[1].txBytes * 8.0 /
196 (stats[1].timeLastTxPacket.GetSeconds() -
197 stats[1].timeFirstTxPacket.GetSeconds()) /
198 1000000
199 << " Mbps" << std::endl;
200 std::cout << " Rx Packets/Bytes: " << stats[1].rxPackets << " / " << stats[1].rxBytes
201 << std::endl;
202 uint32_t packetsDroppedByQueueDisc = 0;
203 uint64_t bytesDroppedByQueueDisc = 0;
204 if (stats[1].packetsDropped.size() > Ipv4FlowProbe::DROP_QUEUE_DISC)
205 {
206 packetsDroppedByQueueDisc = stats[1].packetsDropped[Ipv4FlowProbe::DROP_QUEUE_DISC];
207 bytesDroppedByQueueDisc = stats[1].bytesDropped[Ipv4FlowProbe::DROP_QUEUE_DISC];
208 }
209 std::cout << " Packets/Bytes Dropped by Queue Disc: " << packetsDroppedByQueueDisc << " / "
210 << bytesDroppedByQueueDisc << std::endl;
211 uint32_t packetsDroppedByNetDevice = 0;
212 uint64_t bytesDroppedByNetDevice = 0;
213 if (stats[1].packetsDropped.size() > Ipv4FlowProbe::DROP_QUEUE)
214 {
215 packetsDroppedByNetDevice = stats[1].packetsDropped[Ipv4FlowProbe::DROP_QUEUE];
216 bytesDroppedByNetDevice = stats[1].bytesDropped[Ipv4FlowProbe::DROP_QUEUE];
217 }
218 std::cout << " Packets/Bytes Dropped by NetDevice: " << packetsDroppedByNetDevice << " / "
219 << bytesDroppedByNetDevice << std::endl;
220 std::cout << " Throughput: "
221 << stats[1].rxBytes * 8.0 /
222 (stats[1].timeLastRxPacket.GetSeconds() -
223 stats[1].timeFirstRxPacket.GetSeconds()) /
224 1000000
225 << " Mbps" << std::endl;
226 std::cout << " Mean delay: " << stats[1].delaySum.GetSeconds() / stats[1].rxPackets
227 << std::endl;
228 std::cout << " Mean jitter: " << stats[1].jitterSum.GetSeconds() / (stats[1].rxPackets - 1)
229 << std::endl;
230 auto dscpVec = classifier->GetDscpCounts(1);
231 for (auto p : dscpVec)
232 {
233 std::cout << " DSCP value: 0x" << std::hex << static_cast<uint32_t>(p.first) << std::dec
234 << " count: " << p.second << std::endl;
235 }
236
238
239 std::cout << std::endl << "*** Application statistics ***" << std::endl;
240 double thr = 0;
241 uint64_t totalPacketsThr = DynamicCast<PacketSink>(sinkApp.Get(0))->GetTotalRx();
242 thr = totalPacketsThr * 8 / (simulationTime * 1000000.0); // Mbit/s
243 std::cout << " Rx Bytes: " << totalPacketsThr << std::endl;
244 std::cout << " Average Goodput: " << thr << " Mbit/s" << std::endl;
245 std::cout << std::endl << "*** TC Layer statistics ***" << std::endl;
246 std::cout << q->GetStats() << std::endl;
247 return 0;
248}
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Address.
Definition: address.h:286
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.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
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.
Parse command-line arguments.
Definition: command-line.h:232
Helper to enable IP flow monitoring on a set of Nodes.
Ptr< FlowClassifier > GetClassifier()
Retrieve the FlowClassifier object for IPv4 created by the Install* methods.
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
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()
@ DROP_QUEUE_DISC
Packet dropped by the queue disc.
@ DROP_QUEUE
Packet dropped due to queue overflow.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
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:37
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
@ MS
millisecond
Definition: nstime.h:117
double ToDouble(Unit unit) const
Get the Time value expressed in a particular unit.
Definition: nstime.h:573
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:45
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:954
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
ns devices
Definition: first.py:42
ns interfaces
Definition: first.py:50
ns address
Definition: first.py:47
ns stack
Definition: first.py:44
ns pointToPoint
Definition: first.py:38
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
ns cmd
Definition: second.py:40
void TcPacketsInQueueTrace(uint32_t oldValue, uint32_t newValue)
Number of packets in TX queue trace.
void SojournTimeTrace(Time sojournTime)
TC Soujoun time trace.
void DevicePacketsInQueueTrace(uint32_t oldValue, uint32_t newValue)
Packets in the device queue trace.