forked from ajahjefend1/TK4ProjekatTCPProxyMehanizam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqkd_overlay_channel_test.cc
329 lines (254 loc) · 11 KB
/
qkd_overlay_channel_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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2015 LIPTEL.ieee.org
*
* 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: Miralem Mehic <[email protected]>
*/
// Network topology
//
// n0 ---p2p-- n1 --p2p-- n2
// |---------qkd---------|
//
// - udp flows from n0 to n2
#include <fstream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/qkd-helper.h"
#include "ns3/qkd-app-charging-helper.h"
#include "ns3/qkd-graph-manager.h"
#include "ns3/applications-module.h"
#include "ns3/internet-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/mobility-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/gnuplot.h"
#include "ns3/qkd-send.h"
#include "ns3/error-model.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/aodv-module.h"
#include "ns3/olsr-module.h"
#include "ns3/dsdv-module.h"
#include "ns3/dsr-module.h"
#include "ns3/aodvq-module.h"
#include "ns3/dsdvq-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("QKD_CHANNEL_TEST");
//Create variables to test the results
uint32_t m_bytes_sent = 0;
uint32_t m_bytes_received = 0;
uint32_t m_packets_sent = 0;
uint32_t m_packets_received = 0;
//Create help variable m_time
double m_time = 0;
//Create c++ map for measuring delay time
std::map<uint32_t, double> m_delayTable;
/**
* \brief Each time the packet is sent, this function is called using TracedCallback and information about packet is stored in m_delayTable
* \param context - used to display full path of the information provided (useful for taking information about the nodes or applications)
* \param packet - Ptr reference to the packet itself
*/
void
SentPacket(std::string context, Ptr<const Packet> p){
/*
//HELP LINES USED FOR TESTING
std::cout << "\n ..................SentPacket....." << p->GetUid() << "..." << p->GetSize() << "....... \n";
p->Print(std::cout);
std::cout << "\n ............................................ \n";
*/
//Sum bytes of the packet that was sent
m_bytes_sent += p->GetSize();
m_packets_sent++;
//Insert in the delay table details about the packet that was sent
m_delayTable.insert (std::make_pair (p->GetUid(), (double)Simulator::Now().GetSeconds()));
}
void
ReceivedPacket(std::string context, Ptr<const Packet> p, const Address& addr){
/*
//HELP LINES USED FOR TESTING
std::cout << "\n ..................ReceivedPacket....." << p->GetUid() << "..." << p->GetSize() << "....... \n";
p->Print(std::cout);
std::cout << "\n ............................................ \n";
*/
//Find the record in m_delayTable based on packetID
std::map<uint32_t, double >::iterator i = m_delayTable.find ( p->GetUid() );
//Get the current time in the temp variable
double temp = (double)Simulator::Now().GetSeconds();
//Display the delay for the packet in the form of "packetID delay" where delay is calculated as the current time - time when the packet was sent
std::cout << p->GetUid() << "\t" << temp - i->second << "\n";
//Remove the entry from the delayTable to clear the RAM memroy and obey memory leakage
if(i != m_delayTable.end()){
m_delayTable.erase(i);
}
//Sum bytes and number of packets that were sent
m_bytes_received += p->GetSize();
m_packets_received++;
}
void
Ratio(){
std::cout << "Sent (bytes):\t" << m_bytes_sent
<< "\tReceived (bytes):\t" << m_bytes_received
<< "\nSent (Packets):\t" << m_packets_sent
<< "\tReceived (Packets):\t" << m_packets_received
<< "\nRatio (bytes):\t" << (float)m_bytes_received/(float)m_bytes_sent
<< "\tRatio (packets):\t" << (float)m_packets_received/(float)m_packets_sent << "\n";
}
static void
CwndChange (uint32_t oldCwnd, uint32_t newCwnd)
{
// NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
}
int main (int argc, char *argv[])
{
Packet::EnablePrinting();
PacketMetadata::Enable ();
//
// Explicitly create the nodes required by the topology (shown above).
//
NS_LOG_INFO ("Create nodes.");
NodeContainer n;
n.Create (5);
NodeContainer n0n1 = NodeContainer (n.Get(0), n.Get (1));
NodeContainer n1n2 = NodeContainer (n.Get(1), n.Get (2));
NodeContainer n2n3 = NodeContainer (n.Get(2), n.Get (3));
NodeContainer n3n4 = NodeContainer (n.Get(3), n.Get (4));
NodeContainer qkdNodes = NodeContainer (
n.Get (0),
n.Get (2),
n.Get (4)
);
//Underlay network - set routing protocol (if any)
//Enable OLSR
//OlsrHelper routingProtocol;
//DsdvHelper routingProtocol;
InternetStackHelper internet;
//internet.SetRoutingHelper (routingProtocol);
internet.Install (n);
// Set Mobility for all nodes
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject <ListPositionAllocator>();
positionAlloc ->Add(Vector(0, 200, 0)); // node0
positionAlloc ->Add(Vector(200, 200, 0)); // node1
positionAlloc ->Add(Vector(400, 200, 0)); // node2
positionAlloc ->Add(Vector(600, 200, 0)); // node3
positionAlloc ->Add(Vector(800, 200, 0)); // node4
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(n);
// We create the channels first without any IP addressing information
NS_LOG_INFO ("Create channels.");
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", StringValue ("1Gbps"));
p2p.SetChannelAttribute ("Delay", StringValue ("100ps"));
NetDeviceContainer d0d1 = p2p.Install (n0n1);
NetDeviceContainer d1d2 = p2p.Install (n1n2);
NetDeviceContainer d2d3 = p2p.Install (n2n3);
NetDeviceContainer d3d4 = p2p.Install (n3n4);
//////// ERROR MODEL///////////
Ptr<RateErrorModel> em = CreateObject<RateErrorModel> ();
em->SetAttribute ("ErrorRate", DoubleValue (0.0001));
d1d2.Get(1) ->SetAttribute ("ReceiveErrorModel", PointerValue (em));
///////////////////////////////
//
// We've got the "hardware" in place. Now we need to add IP addresses.
//
NS_LOG_INFO ("Assign IP Addresses.");
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer i0i1 = ipv4.Assign (d0d1);
ipv4.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
ipv4.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer i2i3 = ipv4.Assign (d2d3);
ipv4.SetBase ("10.1.4.0", "255.255.255.0");
Ipv4InterfaceContainer i3i4 = ipv4.Assign (d3d4);
// Create router nodes, initialize routing database and set up the routing
// tables in the nodes.
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
//Overlay network - set routing protocol
//Enable Overlay Routing
AodvqHelper routingOverlayProtocol;
// DsdvqHelper routingOverlayProtocol;
//
// Explicitly create the channels required by the topology (shown above).
//
QKDHelper QHelper;
//install QKD Managers on the nodes
QHelper.SetRoutingHelper (routingOverlayProtocol);
QHelper.InstallQKDManager (qkdNodes);
//Create QKDNetDevices and create QKDbuffers
Ipv4InterfaceAddress va02_0 (Ipv4Address ("11.0.0.1"), Ipv4Mask ("255.255.255.0"));
Ipv4InterfaceAddress va02_2 (Ipv4Address ("11.0.0.2"), Ipv4Mask ("255.255.255.0"));
Ipv4InterfaceAddress va24_2 (Ipv4Address ("11.0.0.3"), Ipv4Mask ("255.255.255.0"));
Ipv4InterfaceAddress va24_4 (Ipv4Address ("11.0.0.4"), Ipv4Mask ("255.255.255.0"));
//create QKD connection between nodes 0 and 2
NetDeviceContainer qkdNetDevices02 = QHelper.InstallOverlayQKD (
d0d1.Get(0), d1d2.Get(1),
va02_0, va02_2,
108576, //min
0, //thr - will be set automatically
1085760, //max
1085760 //current //20485770
);
//Create graph to monitor buffer changes
QHelper.AddGraph(qkdNodes.Get(0), d1d2.Get (0), "myGraph02"); //srcNode, destinationAddress, BufferTitle
//create QKD connection between nodes 0 and 2
NetDeviceContainer qkdNetDevices24 = QHelper.InstallOverlayQKD (
d2d3.Get(0), d3d4.Get(1),
va24_2, va24_4,
108576, //min
0, //thr - will be set automatically
1085760, //max
1085760 //current //88576
);
//Create graph to monitor buffer changes
QHelper.AddGraph(qkdNodes.Get(1), d3d4.Get (0), "myGraph24"); //srcNode, destinationAddress, BufferTitle
NS_LOG_INFO ("Create Applications.");
/* Create user's traffic between v0 and v1 */
/* Create sink app */
uint16_t sinkPort = 8080;
QKDSinkAppHelper packetSinkHelper ("ns3::VirtualUdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
ApplicationContainer sinkApps = packetSinkHelper.Install (qkdNodes.Get (2));
sinkApps.Start (Seconds (25.));
sinkApps.Stop (Seconds (170.));
/* Create source app */
Address sinkAddress (InetSocketAddress (va24_4.GetLocal (), sinkPort));
Address sourceAddress (InetSocketAddress (va02_0.GetLocal (), sinkPort));
Ptr<Socket> overlaySocket = Socket::CreateSocket (qkdNodes.Get (0), VirtualUdpSocketFactory::GetTypeId ());
Ptr<QKDSend> app = CreateObject<QKDSend> ();
app->Setup (overlaySocket, sourceAddress, sinkAddress, 200, 0, DataRate ("5kbps"));
qkdNodes.Get (0)->AddApplication (app);
app->SetStartTime (Seconds (25.));
app->SetStopTime (Seconds (170.));
////////// PROZOR ZAGUSENJA////////
Config::ConnectWithoutContext("/NodeList/0/$ns3::TcpL4Protocol/SocketList/*/CongestionWindow",MakeCallback(&CwndChange));
//////////////////////////////////////////////////
//////////////////////////////////////
//// STATISTICS
//////////////////////////////////////
//if we need we can create pcap files
p2p.EnablePcapAll ("QKD_vnet_test");
QHelper.EnablePcapAll ("QKD_overlay_vnet_test");
///ispisivanje primljenih i poslanih paketa
Config::Connect("/NodeList/*/ApplicationList/*/$ns3::QKDSend/Tx", MakeCallback(&SentPacket));
Config::Connect("/NodeList/*/ApplicationList/*/$ns3::QKDSink/Rx", MakeCallback(&ReceivedPacket));
Simulator::Stop ( Seconds (100) );
Simulator::Run ();
Ratio( );
//Finally print the graphs
QHelper.PrintGraphs();
Simulator::Destroy ();
}