-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2p_test.cc
481 lines (414 loc) · 13.3 KB
/
p2p_test.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* 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
*/
#include "ns3/apps.h" // including basic header, MyTag and MyApp
#include "ns3/tools.h"
#include "ns3/point-to-point-layout-module.h"
#include "ns3/traffic-control-module.h"
#include "ns3/tcp-socket-factory.h"
#include "ns3/udp-socket-factory.h"
#include "ns3/ipv4-flow-classifier.h"
#include "ns3/tag.h"
#include "ns3/packet.h"
#include "ns3/uinteger.h"
#include <cstring>
#include <fstream>
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE("p2p_test");
uint32_t cnt[2] = {0};
uint32_t cnt1[2] = {0};
uint32_t cnt2[2] = {0};
uint32_t cnt3[2] = {0};
uint32_t cntt[2] = {0};
fstream fout;
Ptr<PointToPointNetDevice> p2pDev;
TypeId
MyTag::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::MyTag")
.SetParent<Tag> ()
.AddConstructor<MyTag> ()
.AddAttribute ("SimpleValue",
"A simple value",
EmptyAttributeValue (),
MakeUintegerAccessor (&MyTag::GetSimpleValue),
MakeUintegerChecker<uint32_t> ())
;
return tid;
}
TypeId
MyTag::GetInstanceTypeId (void) const
{
return GetTypeId ();
}
uint32_t
MyTag::GetSerializedSize (void) const
{
return 4;
}
void
MyTag::Serialize (TagBuffer i) const
{
i.WriteU32 (m_simpleValue);
}
void
MyTag::Deserialize (TagBuffer i)
{
m_simpleValue = i.ReadU32 ();
}
void
MyTag::Print (std::ostream &os) const
{
os << "v=" << (uint32_t)m_simpleValue;
}
void
MyTag::SetSimpleValue (uint32_t value)
{
m_simpleValue = value;
}
uint32_t
MyTag::GetSimpleValue (void) const
{
return m_simpleValue;
}
//=========================================================================//
//===============Beigining of Application definition=======================//
//=========================================================================//
MyApp::MyApp ()
: m_socket (0),
m_peer (),
m_packetSize (0),
//m_nPackets (0),
m_dataRate (0),
m_sendEvent (),
m_running (false),
//m_packetsSent (0)
m_tagValue (0),
m_cnt (0)
{
isTrackPkt = false;
}
MyApp::~MyApp()
{
m_socket = 0;
}
void
//MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, DataRate dataRate)
{
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
//m_nPackets = nPackets;
m_dataRate = dataRate;
}
void
MyApp::SetDataRate(DataRate rate)
{
m_dataRate = rate;
}
void
MyApp::SetTagValue(uint32_t value)
{
m_tagValue = value;
}
void
MyApp::StartApplication (void)
{
m_running = true;
//m_packetsSent = 0;
m_socket->Bind ();
m_socket->Connect (m_peer);
SendPacket ();
}
void
MyApp::StopApplication (void)
{
m_running = false;
if (m_sendEvent.IsRunning ())
{
Simulator::Cancel (m_sendEvent);
}
if (m_socket)
{
m_socket->Close ();
}
}
void
MyApp::SendPacket (void)
{
//create the tags
MyTag tag;
if(!isTrackPkt) tag.SetSimpleValue (m_tagValue);
else tag.SetSimpleValue (m_tagValue * tagScale + ++m_cnt);
stringstream ss;
ss << "- Tx: " << m_tagValue - 1 << ". " << m_cnt;
// NS_LOG_INFO(ss.str());
Ptr<Packet> packet = Create<Packet> (m_packetSize);
packet -> AddPacketTag (tag); //add tags
m_socket->Send (packet);
// for debug only
// NS_LOG_INFO(" socket for " + to_string(m_tagValue - 1) + ": sending pkt, cnt = " + to_string(m_cnt));
ScheduleTx ();
}
void
MyApp::ScheduleTx (void)
{
if (m_running)
{
Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
}
}
Ptr<Socket> MyApp::GetSocket()
{
return m_socket;
}
//=========================================================================//
//===================End of Application definition=========================//
//=========================================================================//
void handler(QueueDiscContainer qDisc)
{
double curt = Simulator::Now().GetSeconds();
cout << curt << "s: # pkt in Q: " << qDisc.Get(0)->GetCurrentSize() << endl;
Simulator::Schedule(Seconds(5.0), &handler, qDisc);
}
void
onCwndChange(string context, uint32_t oldValue, uint32_t newValue)
{
size_t pos = context.find("/Congestion");
uint32_t i = context.substr(pos - 1, 1)[0] - '0'; // get the flow index, need testing
// uint32_t i = context.c_str()[0] - '0';
NS_LOG_INFO(" - CongWnd = " + to_string(newValue) + "\n");
}
void
DevPktInQ(uint32_t vOld, uint32_t vNew)
{
cout << "# device packet in queue: " << vNew << endl;
}
void
TcPktInQ(uint32_t vOld, uint32_t vNew)
{
cout << "Tc packet in queue: " << vNew << endl;
fout << Simulator::Now().GetSeconds() << " " << vNew << endl;
}
void
SojournTimeTrace (Time sojournTime)
{
std::cout << "Sojourn time " << sojournTime.ToDouble (Time::MS) << "ms" << std::endl;
}
static
void PktMacTx(Ptr<const Packet> p)
{
Ptr<Packet> pcp = p->Copy();
MyTag tag;
if(pcp->PeekPacketTag(tag))
{
int idx = tag.GetSimpleValue();
stringstream ss;
ss << Simulator::Now().GetSeconds() << "s: Mac Tx: " << idx << ". " << cnt[idx - 1] ++ << endl;
NS_LOG_INFO(ss.str());
}
}
static
void PktMacTxDrop(Ptr<const Packet> p)
{
Ptr<Packet> pcp = p->Copy();
MyTag tag;
if(pcp->PeekPacketTag(tag))
{
int idx = tag.GetSimpleValue();
stringstream ss;
ss << Simulator::Now().GetSeconds() << "s: Mac Tx Drop: " << idx << ". " << cntt[idx - 1] ++ << endl;
NS_LOG_INFO(ss.str());
}
}
static
void PktMacRx(Ptr<const Packet> p)
{
Ptr<Packet> pcp = p->Copy();
MyTag tag;
if(pcp->PeekPacketTag(tag))
{
int idx = tag.GetSimpleValue();
stringstream ss;
ss << Simulator::Now().GetSeconds() << "s: Mac Rx: " << idx << ". " << cnt3[idx - 1] ++;
NS_LOG_INFO(ss.str());
// for test
p2pDev->SetEarlyDrop(true);
}
}
static
void PktDrop(Ptr<const Packet> p)
{
Ptr<Packet> pcp = p->Copy();
MyTag tag;
if(pcp->PeekPacketTag(tag))
{
int idx = tag.GetSimpleValue();
stringstream ss;
ss << "! " << Simulator::Now().GetSeconds() << "s: Drop: " << idx << ". " << ++ cnt1[idx - 1] << endl;
NS_LOG_INFO(ss.str());
}
}
static
void QueueOp(Ptr<const QueueDiscItem> qi)
{
// q->GetNBytes();
stringstream ss;
ss << " " << Simulator::Now().GetSeconds() << "s: Queue op. ";
ss << qi->GetPacket()->GetUid();
NS_LOG_INFO(ss.str());
}
static
void QueueDrop(Ptr<const QueueDiscItem> qi)
{
// q->GetNBytes();
Ptr<Packet> p = qi->GetPacket();
MyTag tag;
if(p->PeekPacketTag(tag))
{
int idx = tag.GetSimpleValue();
stringstream ss;
ss << " " << Simulator::Now().GetSeconds() << "s: Queue drop! " << idx << ". " << ++ cnt2[idx - 1];
NS_LOG_INFO(ss.str());
}
}
int
main(int argc, char *argv[])
{
string cRate = "20Mbps";
string bRate = "5Mbps";
string bDelay = "2ms";
double tStop = 50.0;
string qType = "RED";
uint16_t port = 8080;
string fname = "REDout.dat";
remove(fname.c_str());
fout.open(fname, ios::app | ios::out);
CommandLine cmd;
cmd.AddValue("cRate", "Data rate of client", cRate);
cmd.AddValue("bRate", "Data rate of bottleneck link", bRate);
cmd.AddValue("tStop", "Stop time", tStop);
cmd.AddValue("queue", "Queue Type", qType);
cmd.Parse (argc, argv);
LogComponentEnable ("p2p_test", LOG_LEVEL_INFO); /// test here
Config::SetDefault("ns3::TcpL4Protocol::SocketType", StringValue("ns3::TcpNewReno"));
Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue (1000));
// p2p topology
PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate", StringValue(bRate));
p2p.SetChannelAttribute("Delay", StringValue(bDelay));
// p2p.SetQueue("ns3::DropTailQueue", "MaxSize", StringValue("1p"));
PointToPointHelper leaf;
leaf.SetDeviceAttribute("DataRate", StringValue("1Gbps"));
leaf.SetChannelAttribute("Delay", StringValue("1ms"));
PointToPointDumbbellHelper d (2, leaf, 2, leaf, p2p);
// stack
InternetStackHelper stack;
d.InstallStack (stack);
// queue disc: different from old version
TrafficControlHelper tch;
tch.SetRootQueueDisc("ns3::RedQueueDisc");
tch.Install(d.GetLeft()->GetDevice(0));
QueueDiscContainer qDiscs = tch.Install(d.GetRight()->GetDevice(0)); // QueueDiscContainer qdisc;
cout << "MAC: " << d.GetRight()->GetDevice(0)->GetAddress() << endl;
Simulator::Schedule(Seconds(10), &handler, qDiscs);
// Assign IP Addresses
d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"),
Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"),
Ipv4AddressHelper ("10.3.1.0", "255.255.255.0"));
// IP address test
stringstream ss1;
ss1 << "TX: " << endl;
for(uint32_t i = 0; i < d.RightCount(); i ++)
{
ss1 << " ";
d.GetRightIpv4Address(i).Print(ss1);
ss1 << endl;
}
ss1 << "RX: " << endl;
for(uint32_t i = 0; i < d.LeftCount(); i ++)
{
ss1 << " ";
d.GetLeftIpv4Address(i).Print(ss1);
ss1 << endl;
}
NS_LOG_INFO(ss1.str());
// sink App
Address sinkLocalAddress(InetSocketAddress(Ipv4Address::GetAny(), port));
// PacketSinkHelper psh("ns3::TcpSocketFactory", sinkLocalAddress);
PacketSinkHelper psh("ns3::UdpSocketFactory", sinkLocalAddress);
ApplicationContainer sinkApps;
for(uint32_t i = 0; i < d.LeftCount(); i ++)
sinkApps.Add(psh.Install(d.GetLeft(i)));
sinkApps.Start(Seconds(0.0));
sinkApps.Stop(Seconds(tStop));
// client App
for(uint32_t i = 0; i < d.RightCount(); i ++)
{
// Ptr<Socket> skt = Socket::CreateSocket(d.GetRight(i), TcpSocketFactory::GetTypeId());
Ptr<Socket> skt = Socket::CreateSocket(d.GetRight(i), UdpSocketFactory::GetTypeId());
Address addr(InetSocketAddress(d.GetLeftIpv4Address(i), port));
Ptr<MyApp> app = CreateObject<MyApp> ();
app->SetTagValue(i + 1);
app->Setup(skt, addr, 1000, DataRate(cRate));
d.GetRight(i)->AddApplication(app); // not a helper -> no install()
app->SetStartTime(Seconds(0.1));
app->SetStopTime(Seconds(tStop));
// tracing cwnd chagne: need test
// string context1 = "/NodeList/0/$ns3::TcpL4Protocol/SocketList/" + to_string(i) + "/CongestionWindow";
// app->GetSocket()->TraceConnect("CongestionWindow", context1, MakeCallback(&onCwndChange));
// skt->TraceConnectWithoutContext("Congestionwindow", MakeCallback(&onCwndChange));
}
// set the p2p device
p2pDev = DynamicCast<PointToPointNetDevice> (d.GetRight()->GetDevice(0));
// processing and tracing here
for (uint32_t i = 0; i < d.RightCount() + 1; i ++)
{
d.GetRight()->GetDevice(i)->TraceConnectWithoutContext("MacRx", MakeCallback(&PktMacRx));
d.GetRight()->GetDevice(i)->TraceConnectWithoutContext("PhyRxDrop", MakeCallback(&PktDrop));
}
d.GetRight()->GetDevice(0)->TraceConnectWithoutContext("MacTx", MakeCallback(&PktMacTx));
d.GetRight()->GetDevice(0)->TraceConnectWithoutContext("MacTxDrop", MakeCallback(&PktMacTx));
// tracing queue by visiting it from dumbbell d
Ptr<Queue<Packet>> qp = DynamicCast<PointToPointNetDevice>(d.GetRight()->GetDevice(0))->GetQueue();
qp->TraceConnectWithoutContext("Drop", MakeCallback(&PktDrop));
// tracing from traffic-control.cc
// qp->TraceConnectWithoutContext("PacketsInQueue", MakeCallback(&DevPktInQ));
Ptr<QueueDisc> q = qDiscs.Get(0);
q->TraceConnectWithoutContext("Drop", MakeCallback(&QueueDrop));
q->TraceConnectWithoutContext("EnQueue", MakeCallback(&QueueOp)); // not fired
// q->TraceConnectWithoutContext("DeQueue", MakeCallback(&QueueOp)); // not fired
q->TraceConnectWithoutContext("PacketsInQueue", MakeCallback(&TcPktInQ));
// set simulation
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
cout << "Running the simulation..." << endl;
Simulator::Stop(Seconds(tStop));
Simulator::Run();
// stats
QueueDisc::Stats st = qDiscs.Get(0)->GetStats();
// st.Print(cout);
if (st.GetNDroppedPackets (RedQueueDisc::UNFORCED_DROP) == 0)
cout << "There should be some unforced drops" << endl;
if (st.GetNDroppedPackets (QueueDisc::INTERNAL_QUEUE_DROP) != 0)
cout << "There should be zero drops due to queue full" << endl;
// end
fout.close();
cout << "\nDestroying the simulation..." << endl;
Simulator::Destroy();
return 0;
}