-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.cc
262 lines (216 loc) · 10.6 KB
/
2.cc
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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2015 Universita' degli Studi di Napoli "Federico II"
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Pasquale Imputato <p.imputato@gmail.com>
* Author: Stefano Avallone <stefano.avallone@unina.it>
*/
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/traffic-control-module.h"
#include "ns3/flow-monitor-module.h"
// This simple example shows how to use TrafficControlHelper to install a
// QueueDisc on a device.
//
// The default QueueDisc is a pfifo_fast with a capacity of 1000 packets (as in
// Linux). However, in this example, we install a RedQueueDisc with a capacity
// of 10000 packets.
//
// Network topology
//
// 10.1.1.0
// n0 -------------- n1
// point-to-point
//
// The output will consist of all the traced changes in the length of the RED
// internal queue and in the length of the netdevice queue:
//
// DevicePacketsInQueue 0 to 1
// TcPacketsInQueue 7 to 8
// TcPacketsInQueue 8 to 9
// DevicePacketsInQueue 1 to 0
// TcPacketsInQueue 9 to 8
//
// plus some statistics collected at the network layer (by the flow monitor)
// and the application layer. Finally, the number of packets dropped by the
// queuing discipline, the number of packets dropped by the netdevice and
// the number of packets requeued by the queuing discipline are reported.
//
// If the size of the DropTail queue of the netdevice were increased from 1
// to a large number (e.g. 1000), one would observe that the number of dropped
// packets goes to zero, but the latency grows in an uncontrolled manner. This
// is the so-called bufferbloat problem, and illustrates the importance of
// having a small device queue, so that the standing queues build in the traffic
// control layer where they can be managed by advanced queue discs rather than
// in the device layer.
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("TrafficControlExample");
/*
void
TcPacketsInQueueTrace (uint32_t oldValue, uint32_t newValue)
{
std::cout << "TcPacketsInQueue " << oldValue << " to " << newValue << std::endl;
}
void
DevicePacketsInQueueTrace (uint32_t oldValue, uint32_t newValue)
{
std::cout << "DevicePacketsInQueue " << oldValue << " to " << newValue << std::endl;
}
*/
int
main (int argc, char *argv[])
{
double simulationTime = 10; //seconds
std::string transportProt = "Udp";
std::string socketType;
CommandLine cmd;
// cmd.AddValue ("transportProt", "Transport protocol to use: Tcp, Udp", transportProt);
cmd.Parse (argc, argv);
if (transportProt.compare ("Tcp") == 0)
{
socketType = "ns3::TcpSocketFactory";
}
else
{
socketType = "ns3::UdpSocketFactory";
}
NodeContainer nodes;
nodes.Create (4);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("20Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
pointToPoint.SetQueue ("ns3::DropTailQueue", "MaxSize", StringValue ("1p"));
NetDeviceContainer device1;
device1 = pointToPoint.Install (nodes.Get(0), nodes.Get(2));
NetDeviceContainer device2;
device2 = pointToPoint.Install (nodes.Get(1), nodes.Get(2));
NetDeviceContainer device3;
device3 = pointToPoint.Install (nodes.Get(2), nodes.Get(3));
InternetStackHelper stack;
stack.Install (nodes);
/*
TrafficControlHelper tch;
uint16_t handle = tch.SetRootQueueDisc ("ns3::RedQueueDisc");
// Add the internal queue used by Red
tch.AddInternalQueues (handle, 1, "ns3::DropTailQueue", "MaxPackets", UintegerValue (10000));
QueueDiscContainer qdiscs = tch.Install (devices);
Ptr<QueueDisc> q = qdiscs.Get (1);
q->TraceConnectWithoutContext ("PacketsInQueue", MakeCallback (&TcPacketsInQueueTrace));
// Alternatively:
// Config::ConnectWithoutContext ("/NodeList/1/$ns3::TrafficControlLayer/RootQueueDiscList/0/PacketsInQueue",
// MakeCallback (&TcPacketsInQueueTrace));
Ptr<NetDevice> nd = devices.Get (1);
Ptr<PointToPointNetDevice> ptpnd = DynamicCast<PointToPointNetDevice> (nd);
Ptr<Queue> queue = ptpnd->GetQueue ();
queue->TraceConnectWithoutContext ("PacketsInQueue", MakeCallback (&DevicePacketsInQueueTrace));
*/
Ipv4AddressHelper address1;
address1.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interface1 = address1.Assign (device1);
Ipv4AddressHelper address2;
address2.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer interface2 = address2.Assign (device2);
Ipv4AddressHelper address3;
address3.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer interface3 = address3.Assign (device3);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
//UDP Flow
uint16_t port = 7;
Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
PacketSinkHelper packetSinkHelper (socketType, localAddress);
ApplicationContainer sinkApp = packetSinkHelper.Install (nodes.Get (3));
sinkApp.Start (Seconds (0.0));
sinkApp.Stop (Seconds (simulationTime + 0.1));
uint32_t payloadSize = 1448;
// Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
OnOffHelper onoff (socketType, Ipv4Address::GetAny ());
onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
onoff.SetAttribute ("DataRate", StringValue ("50Mbps")); //bit/s
ApplicationContainer apps;
AddressValue remoteAddress (InetSocketAddress (interface3.GetAddress (1), port));
onoff.SetAttribute ("Remote", remoteAddress);
apps.Add (onoff.Install (nodes.Get (1)));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (simulationTime + 0.1));
//TCP Flow
uint16_t port_tcp = 9;
socketType = "ns3::TcpSocketFactory";
Address localAddress_tcp (InetSocketAddress (Ipv4Address::GetAny (), port_tcp));
PacketSinkHelper packetSinkHelper_tcp (socketType, localAddress_tcp);
ApplicationContainer sinkApp_tcp = packetSinkHelper_tcp.Install (nodes.Get (3));
sinkApp_tcp.Start (Seconds (0.5));
sinkApp_tcp.Stop (Seconds (simulationTime + 0.1));
// uint32_t payloadSize = 1448;
// Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
OnOffHelper onoff_tcp (socketType, Ipv4Address::GetAny ());
onoff_tcp.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
onoff_tcp.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
onoff_tcp.SetAttribute ("PacketSize", UintegerValue (payloadSize));
onoff_tcp.SetAttribute ("DataRate", StringValue ("50Mbps")); //bit/s
ApplicationContainer apps_tcp;
AddressValue remoteAddress_tcp (InetSocketAddress (interface3.GetAddress (1), port_tcp));
onoff_tcp.SetAttribute ("Remote", remoteAddress_tcp);
apps_tcp.Add (onoff_tcp.Install (nodes.Get (0)));
apps_tcp.Start (Seconds (1.5));
apps_tcp.Stop (Seconds (simulationTime + 0.1));
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
Simulator::Stop (Seconds (simulationTime + 5));
Simulator::Run ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
std::cout << std::endl << "*** Flow monitor statistics ***" << std::endl;
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
NS_LOG_UNCOND ("Flow ID:" << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n");
NS_LOG_UNCOND ("Number of packets transmitted: " << i->second.txPackets << "\n");
}
// std::cout << " Dropped Packets: " << stats[1].lostPackets << std::endl;
/*
std::cout << " Tx Bytes: " << stats[1].txBytes << std::endl;
std::cout << " Offered Load: " << stats[1].txBytes * 8.0 / (stats[1].timeLastTxPacket.GetSeconds () - stats[1].timeFirstTxPacket.GetSeconds ()) / 1000000 << " Mbps" << std::endl;
std::cout << " Rx Packets: " << stats[1].rxPackets << std::endl;
std::cout << " Rx Bytes: " << stats[1].rxBytes << std::endl;
std::cout << " Packets Dropped by Queue Disc: " << stats[1].packetsDropped[Ipv4FlowProbe::DROP_QUEUE_DISC] << std::endl;
std::cout << " Bytes Dropped by Queue Disc: " << stats[1].bytesDropped[Ipv4FlowProbe::DROP_QUEUE_DISC] << std::endl;
std::cout << " Packets Dropped by NetDevice: " << stats[1].packetsDropped[Ipv4FlowProbe::DROP_QUEUE] << std::endl;
std::cout << " Bytes Dropped by NetDevice: " << stats[1].bytesDropped[Ipv4FlowProbe::DROP_QUEUE] << std::endl;
std::cout << " Throughput: " << stats[1].rxBytes * 8.0 / (stats[1].timeLastRxPacket.GetSeconds () - stats[1].timeFirstRxPacket.GetSeconds ()) / 1000000 << " Mbps" << std::endl;
std::cout << " Mean delay: " << stats[1].delaySum.GetSeconds () / stats[1].rxPackets << std::endl;
std::cout << " Mean jitter: " << stats[1].jitterSum.GetSeconds () / (stats[1].rxPackets - 1) << std::endl;
*/
Simulator::Destroy ();
/*
std::cout << std::endl << "*** Application statistics ***" << std::endl;
double thr = 0;
uint32_t totalPacketsThr = DynamicCast<PacketSink> (sinkApp.Get (0))->GetTotalRx ();
thr = totalPacketsThr * 8 / (simulationTime * 1000000.0); //Mbit/s
std::cout << " Rx Bytes: " << totalPacketsThr << std::endl;
std::cout << " Average Goodput: " << thr << " Mbit/s" << std::endl;
std::cout << std::endl << "*** TC Layer statistics ***" << std::endl;
std::cout << " Packets dropped by the TC layer: " << q->GetTotalDroppedPackets () << std::endl;
std::cout << " Bytes dropped by the TC layer: " << q->GetTotalDroppedBytes () << std::endl;
std::cout << " Packets dropped by the netdevice: " << queue->GetTotalDroppedPackets () << std::endl;
std::cout << " Packets requeued by the TC layer: " << q->GetTotalRequeuedPackets () << std::endl;
*/
return 0;
}