A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
traffic-control-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Universita' degli Studi di Napoli "Federico II"
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Pasquale Imputato <p.imputato@gmail.com>
7 * Author: Stefano Avallone <stefano.avallone@unina.it>
8 */
9
10#include "ns3/applications-module.h"
11#include "ns3/core-module.h"
12#include "ns3/flow-monitor-module.h"
13#include "ns3/internet-module.h"
14#include "ns3/network-module.h"
15#include "ns3/point-to-point-module.h"
16#include "ns3/traffic-control-module.h"
17
18// This simple example shows how to use TrafficControlHelper to install a
19// QueueDisc on a device.
20//
21// The default QueueDisc is a pfifo_fast with a capacity of 1000 packets (as in
22// Linux). However, in this example, we install a RedQueueDisc with a capacity
23// of 10000 packets.
24//
25// Network topology
26//
27// 10.1.1.0
28// n0 -------------- n1
29// point-to-point
30//
31// The output will consist of all the traced changes in the length of the RED
32// internal queue and in the length of the netdevice queue:
33//
34// DevicePacketsInQueue 0 to 1
35// TcPacketsInQueue 7 to 8
36// TcPacketsInQueue 8 to 9
37// DevicePacketsInQueue 1 to 0
38// TcPacketsInQueue 9 to 8
39//
40// plus some statistics collected at the network layer (by the flow monitor)
41// and the application layer. Finally, the number of packets dropped by the
42// queuing discipline, the number of packets dropped by the netdevice and
43// the number of packets requeued by the queuing discipline are reported.
44//
45// If the size of the DropTail queue of the netdevice were increased from 1
46// to a large number (e.g. 1000), one would observe that the number of dropped
47// packets goes to zero, but the latency grows in an uncontrolled manner. This
48// is the so-called bufferbloat problem, and illustrates the importance of
49// having a small device queue, so that the standing queues build in the traffic
50// control layer where they can be managed by advanced queue discs rather than
51// in the device layer.
52
53using namespace ns3;
54
55NS_LOG_COMPONENT_DEFINE("TrafficControlExample");
56
57/**
58 * Number of packets in TX queue trace.
59 *
60 * \param oldValue Old velue.
61 * \param newValue New value.
62 */
63void
65{
66 std::cout << "TcPacketsInQueue " << oldValue << " to " << newValue << std::endl;
67}
68
69/**
70 * Packets in the device queue trace.
71 *
72 * \param oldValue Old velue.
73 * \param newValue New value.
74 */
75void
77{
78 std::cout << "DevicePacketsInQueue " << oldValue << " to " << newValue << std::endl;
79}
80
81/**
82 * TC Soujoun time trace.
83 *
84 * \param sojournTime The soujourn time.
85 */
86void
88{
89 std::cout << "Sojourn time " << sojournTime.ToDouble(Time::MS) << "ms" << std::endl;
90}
91
92int
93main(int argc, char* argv[])
94{
95 double simulationTime = 10; // seconds
96 std::string transportProt = "Tcp";
97 std::string socketType;
98
99 CommandLine cmd(__FILE__);
100 cmd.AddValue("transportProt", "Transport protocol to use: Tcp, Udp", transportProt);
101 cmd.Parse(argc, argv);
102
103 if (transportProt == "Tcp")
104 {
105 socketType = "ns3::TcpSocketFactory";
106 }
107 else
108 {
109 socketType = "ns3::UdpSocketFactory";
110 }
111
113 nodes.Create(2);
114
116 pointToPoint.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
117 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
118 pointToPoint.SetQueue("ns3::DropTailQueue", "MaxSize", StringValue("1p"));
119
121 devices = pointToPoint.Install(nodes);
122
124 stack.Install(nodes);
125
127 tch.SetRootQueueDisc("ns3::RedQueueDisc");
128 QueueDiscContainer qdiscs = tch.Install(devices);
129
130 Ptr<QueueDisc> q = qdiscs.Get(1);
131 q->TraceConnectWithoutContext("PacketsInQueue", MakeCallback(&TcPacketsInQueueTrace));
133 "/NodeList/1/$ns3::TrafficControlLayer/RootQueueDiscList/0/SojournTime",
135
136 Ptr<NetDevice> nd = devices.Get(1);
138 Ptr<Queue<Packet>> queue = ptpnd->GetQueue();
139 queue->TraceConnectWithoutContext("PacketsInQueue", MakeCallback(&DevicePacketsInQueueTrace));
140
142 address.SetBase("10.1.1.0", "255.255.255.0");
143
144 Ipv4InterfaceContainer interfaces = address.Assign(devices);
145
146 // Flow
147 uint16_t port = 7;
149 PacketSinkHelper packetSinkHelper(socketType, localAddress);
150 ApplicationContainer sinkApp = packetSinkHelper.Install(nodes.Get(0));
151
152 sinkApp.Start(Seconds(0.0));
153 sinkApp.Stop(Seconds(simulationTime + 0.1));
154
155 uint32_t payloadSize = 1448;
156 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
157
159 onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
160 onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
161 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
162 onoff.SetAttribute("DataRate", StringValue("50Mbps")); // bit/s
164
165 InetSocketAddress rmt(interfaces.GetAddress(0), port);
166 onoff.SetAttribute("Remote", AddressValue(rmt));
167 onoff.SetAttribute("Tos", UintegerValue(0xb8));
168 apps.Add(onoff.Install(nodes.Get(1)));
169 apps.Start(Seconds(1.0));
170 apps.Stop(Seconds(simulationTime + 0.1));
171
172 FlowMonitorHelper flowmon;
173 Ptr<FlowMonitor> monitor = flowmon.InstallAll();
174
175 Simulator::Stop(Seconds(simulationTime + 5));
177
179 std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats();
180 std::cout << std::endl << "*** Flow monitor statistics ***" << std::endl;
181 std::cout << " Tx Packets/Bytes: " << stats[1].txPackets << " / " << stats[1].txBytes
182 << std::endl;
183 std::cout << " Offered Load: "
184 << stats[1].txBytes * 8.0 /
185 (stats[1].timeLastTxPacket.GetSeconds() -
186 stats[1].timeFirstTxPacket.GetSeconds()) /
187 1000000
188 << " Mbps" << std::endl;
189 std::cout << " Rx Packets/Bytes: " << stats[1].rxPackets << " / " << stats[1].rxBytes
190 << std::endl;
191 uint32_t packetsDroppedByQueueDisc = 0;
192 uint64_t bytesDroppedByQueueDisc = 0;
193 if (stats[1].packetsDropped.size() > Ipv4FlowProbe::DROP_QUEUE_DISC)
194 {
195 packetsDroppedByQueueDisc = stats[1].packetsDropped[Ipv4FlowProbe::DROP_QUEUE_DISC];
196 bytesDroppedByQueueDisc = stats[1].bytesDropped[Ipv4FlowProbe::DROP_QUEUE_DISC];
197 }
198 std::cout << " Packets/Bytes Dropped by Queue Disc: " << packetsDroppedByQueueDisc << " / "
199 << bytesDroppedByQueueDisc << std::endl;
200 uint32_t packetsDroppedByNetDevice = 0;
201 uint64_t bytesDroppedByNetDevice = 0;
202 if (stats[1].packetsDropped.size() > Ipv4FlowProbe::DROP_QUEUE)
203 {
204 packetsDroppedByNetDevice = stats[1].packetsDropped[Ipv4FlowProbe::DROP_QUEUE];
205 bytesDroppedByNetDevice = stats[1].bytesDropped[Ipv4FlowProbe::DROP_QUEUE];
206 }
207 std::cout << " Packets/Bytes Dropped by NetDevice: " << packetsDroppedByNetDevice << " / "
208 << bytesDroppedByNetDevice << std::endl;
209 std::cout << " Throughput: "
210 << stats[1].rxBytes * 8.0 /
211 (stats[1].timeLastRxPacket.GetSeconds() -
212 stats[1].timeFirstRxPacket.GetSeconds()) /
213 1000000
214 << " Mbps" << std::endl;
215 std::cout << " Mean delay: " << stats[1].delaySum.GetSeconds() / stats[1].rxPackets
216 << std::endl;
217 std::cout << " Mean jitter: " << stats[1].jitterSum.GetSeconds() / (stats[1].rxPackets - 1)
218 << std::endl;
219 auto dscpVec = classifier->GetDscpCounts(1);
220 for (auto p : dscpVec)
221 {
222 std::cout << " DSCP value: 0x" << std::hex << static_cast<uint32_t>(p.first) << std::dec
223 << " count: " << p.second << std::endl;
224 }
225
227
228 std::cout << std::endl << "*** Application statistics ***" << std::endl;
229 double thr = 0;
230 uint64_t totalPacketsThr = DynamicCast<PacketSink>(sinkApp.Get(0))->GetTotalRx();
231 thr = totalPacketsThr * 8 / (simulationTime * 1000000.0); // Mbit/s
232 std::cout << " Rx Bytes: " << totalPacketsThr << std::endl;
233 std::cout << " Average Goodput: " << thr << " Mbit/s" << std::endl;
234 std::cout << std::endl << "*** TC Layer statistics ***" << std::endl;
235 std::cout << q->GetStats() << std::endl;
236 return 0;
237}
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.
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.
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.
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.
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: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
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
@ MS
millisecond
Definition nstime.h:106
double ToDouble(Unit unit) const
Get the Time value expressed in a particular unit.
Definition nstime.h:562
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
uint16_t port
Definition dsdv-manet.cc:33
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_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
address
Definition first.py:36
pointToPoint
Definition first.py:27
devices
Definition first.py:31
stack
Definition first.py:33
interfaces
Definition first.py:39
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:684
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
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.