1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3 * Copyright (c) 2015 Universita' degli Studi di Napoli "Federico II"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Pasquale Imputato <p.imputato@gmail.com>
19 * Author: Stefano Avallone <stefano.avallone@unina.it>
20 */
21
22 #include "ns3/core-module.h"
23 #include "ns3/network-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/point-to-point-module.h"
26 #include "ns3/applications-module.h"
27 #include "ns3/traffic-control-module.h"
28 #include "ns3/flow-monitor-module.h"
29
30 // This simple example shows how to use TrafficControlHelper to install a
31 // QueueDisc on a device.
32 //
33 // The default QueueDisc is a pfifo_fast with a capacity of 1000 packets (as in
34 // Linux). However, in this example, we install a RedQueueDisc with a capacity
35 // of 10000 packets.
36 //
37 // Network topology
38 //
39 // 10.1.1.0
40 // n0 -------------- n1
41 // point-to-point
42 //
43 // The output will consist of all the traced changes in the length of the RED
44 // internal queue and in the length of the netdevice queue:
45 //
46 // DevicePacketsInQueue 0 to 1
47 // TcPacketsInQueue 7 to 8
48 // TcPacketsInQueue 8 to 9
49 // DevicePacketsInQueue 1 to 0
50 // TcPacketsInQueue 9 to 8
51 //
52 // plus some statistics collected at the network layer (by the flow monitor)
53 // and the application layer. Finally, the number of packets dropped by the
54 // queuing discipline, the number of packets dropped by the netdevice and
55 // the number of packets requeued by the queuing discipline are reported.
56 //
57 // If the size of the DropTail queue of the netdevice were increased from 1
58 // to a large number (e.g. 1000), one would observe that the number of dropped
59 // packets goes to zero, but the latency grows in an uncontrolled manner. This
60 // is the so-called bufferbloat problem, and illustrates the importance of
61 // having a small device queue, so that the standing queues build in the traffic
62 // control layer where they can be managed by advanced queue discs rather than
63 // in the device layer.
64
65 using namespace ns3;
66
67 NS_LOG_COMPONENT_DEFINE ("TrafficControlExample");
68
69 void
TcPacketsInQueueTrace(uint32_t oldValue,uint32_t newValue)70 TcPacketsInQueueTrace (uint32_t oldValue, uint32_t newValue)
71 {
72 std::cout << "TcPacketsInQueue " << oldValue << " to " << newValue << std::endl;
73 }
74
75 void
DevicePacketsInQueueTrace(uint32_t oldValue,uint32_t newValue)76 DevicePacketsInQueueTrace (uint32_t oldValue, uint32_t newValue)
77 {
78 std::cout << "DevicePacketsInQueue " << oldValue << " to " << newValue << std::endl;
79 }
80
81 void
SojournTimeTrace(Time sojournTime)82 SojournTimeTrace (Time sojournTime)
83 {
84 std::cout << "Sojourn time " << sojournTime.ToDouble (Time::MS) << "ms" << std::endl;
85 }
86
87 int
main(int argc,char * argv[])88 main (int argc, char *argv[])
89 {
90 double simulationTime = 10; //seconds
91 std::string transportProt = "Tcp";
92 std::string socketType;
93
94 CommandLine cmd (__FILE__);
95 cmd.AddValue ("transportProt", "Transport protocol to use: Tcp, Udp", transportProt);
96 cmd.Parse (argc, argv);
97
98 if (transportProt.compare ("Tcp") == 0)
99 {
100 socketType = "ns3::TcpSocketFactory";
101 }
102 else
103 {
104 socketType = "ns3::UdpSocketFactory";
105 }
106
107 NodeContainer nodes;
108 nodes.Create (2);
109
110 PointToPointHelper pointToPoint;
111 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
112 pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
113 pointToPoint.SetQueue ("ns3::DropTailQueue", "MaxSize", StringValue ("1p"));
114
115 NetDeviceContainer devices;
116 devices = pointToPoint.Install (nodes);
117
118 InternetStackHelper stack;
119 stack.Install (nodes);
120
121 TrafficControlHelper tch;
122 tch.SetRootQueueDisc ("ns3::RedQueueDisc");
123 QueueDiscContainer qdiscs = tch.Install (devices);
124
125 Ptr<QueueDisc> q = qdiscs.Get (1);
126 q->TraceConnectWithoutContext ("PacketsInQueue", MakeCallback (&TcPacketsInQueueTrace));
127 Config::ConnectWithoutContext ("/NodeList/1/$ns3::TrafficControlLayer/RootQueueDiscList/0/SojournTime",
128 MakeCallback (&SojournTimeTrace));
129
130 Ptr<NetDevice> nd = devices.Get (1);
131 Ptr<PointToPointNetDevice> ptpnd = DynamicCast<PointToPointNetDevice> (nd);
132 Ptr<Queue<Packet> > queue = ptpnd->GetQueue ();
133 queue->TraceConnectWithoutContext ("PacketsInQueue", MakeCallback (&DevicePacketsInQueueTrace));
134
135 Ipv4AddressHelper address;
136 address.SetBase ("10.1.1.0", "255.255.255.0");
137
138 Ipv4InterfaceContainer interfaces = address.Assign (devices);
139
140 //Flow
141 uint16_t port = 7;
142 Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
143 PacketSinkHelper packetSinkHelper (socketType, localAddress);
144 ApplicationContainer sinkApp = packetSinkHelper.Install (nodes.Get (0));
145
146 sinkApp.Start (Seconds (0.0));
147 sinkApp.Stop (Seconds (simulationTime + 0.1));
148
149 uint32_t payloadSize = 1448;
150 Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
151
152 OnOffHelper onoff (socketType, Ipv4Address::GetAny ());
153 onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
154 onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
155 onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
156 onoff.SetAttribute ("DataRate", StringValue ("50Mbps")); //bit/s
157 ApplicationContainer apps;
158
159 InetSocketAddress rmt (interfaces.GetAddress (0), port);
160 rmt.SetTos (0xb8);
161 AddressValue remoteAddress (rmt);
162 onoff.SetAttribute ("Remote", remoteAddress);
163 apps.Add (onoff.Install (nodes.Get (1)));
164 apps.Start (Seconds (1.0));
165 apps.Stop (Seconds (simulationTime + 0.1));
166
167 FlowMonitorHelper flowmon;
168 Ptr<FlowMonitor> monitor = flowmon.InstallAll();
169
170 Simulator::Stop (Seconds (simulationTime + 5));
171 Simulator::Run ();
172
173 Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
174 std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
175 std::cout << std::endl << "*** Flow monitor statistics ***" << std::endl;
176 std::cout << " Tx Packets/Bytes: " << stats[1].txPackets
177 << " / " << stats[1].txBytes << std::endl;
178 std::cout << " Offered Load: " << stats[1].txBytes * 8.0 / (stats[1].timeLastTxPacket.GetSeconds () - stats[1].timeFirstTxPacket.GetSeconds ()) / 1000000 << " Mbps" << std::endl;
179 std::cout << " Rx Packets/Bytes: " << stats[1].rxPackets
180 << " / " << stats[1].rxBytes << std::endl;
181 uint32_t packetsDroppedByQueueDisc = 0;
182 uint64_t bytesDroppedByQueueDisc = 0;
183 if (stats[1].packetsDropped.size () > Ipv4FlowProbe::DROP_QUEUE_DISC)
184 {
185 packetsDroppedByQueueDisc = stats[1].packetsDropped[Ipv4FlowProbe::DROP_QUEUE_DISC];
186 bytesDroppedByQueueDisc = stats[1].bytesDropped[Ipv4FlowProbe::DROP_QUEUE_DISC];
187 }
188 std::cout << " Packets/Bytes Dropped by Queue Disc: " << packetsDroppedByQueueDisc
189 << " / " << bytesDroppedByQueueDisc << std::endl;
190 uint32_t packetsDroppedByNetDevice = 0;
191 uint64_t bytesDroppedByNetDevice = 0;
192 if (stats[1].packetsDropped.size () > Ipv4FlowProbe::DROP_QUEUE)
193 {
194 packetsDroppedByNetDevice = stats[1].packetsDropped[Ipv4FlowProbe::DROP_QUEUE];
195 bytesDroppedByNetDevice = stats[1].bytesDropped[Ipv4FlowProbe::DROP_QUEUE];
196 }
197 std::cout << " Packets/Bytes Dropped by NetDevice: " << packetsDroppedByNetDevice
198 << " / " << bytesDroppedByNetDevice << std::endl;
199 std::cout << " Throughput: " << stats[1].rxBytes * 8.0 / (stats[1].timeLastRxPacket.GetSeconds () - stats[1].timeFirstRxPacket.GetSeconds ()) / 1000000 << " Mbps" << std::endl;
200 std::cout << " Mean delay: " << stats[1].delaySum.GetSeconds () / stats[1].rxPackets << std::endl;
201 std::cout << " Mean jitter: " << stats[1].jitterSum.GetSeconds () / (stats[1].rxPackets - 1) << std::endl;
202 auto dscpVec = classifier->GetDscpCounts (1);
203 for (auto p : dscpVec)
204 {
205 std::cout << " DSCP value: 0x" << std::hex << static_cast<uint32_t> (p.first) << std::dec
206 << " count: "<< p.second << std::endl;
207 }
208
209 Simulator::Destroy ();
210
211 std::cout << std::endl << "*** Application statistics ***" << std::endl;
212 double thr = 0;
213 uint64_t totalPacketsThr = DynamicCast<PacketSink> (sinkApp.Get (0))->GetTotalRx ();
214 thr = totalPacketsThr * 8 / (simulationTime * 1000000.0); //Mbit/s
215 std::cout << " Rx Bytes: " << totalPacketsThr << std::endl;
216 std::cout << " Average Goodput: " << thr << " Mbit/s" << std::endl;
217 std::cout << std::endl << "*** TC Layer statistics ***" << std::endl;
218 std::cout << q->GetStats () << std::endl;
219 return 0;
220 }
221