A Discrete-Event Network Simulator
API
qkd_overlay_channel_test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 LIPTEL.ieee.org
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: Miralem Mehic <miralem.mehic@ieee.org>
19  */
20 
21 
22 // Network topology
23 //
24 // n0 ---p2p-- n1 --p2p-- n2
25 // |---------qkd---------|
26 //
27 // - udp flows from n0 to n2
28 
29 #include <fstream>
30 #include "ns3/core-module.h"
31 #include "ns3/qkd-helper.h"
32 #include "ns3/qkd-app-charging-helper.h"
33 #include "ns3/qkd-graph-manager.h"
34 #include "ns3/applications-module.h"
35 #include "ns3/internet-module.h"
36 #include "ns3/flow-monitor-module.h"
37 #include "ns3/mobility-module.h"
38 #include "ns3/point-to-point-module.h"
39 #include "ns3/gnuplot.h"
40 #include "ns3/qkd-send.h"
41 
42 #include "ns3/aodv-module.h"
43 #include "ns3/olsr-module.h"
44 #include "ns3/dsdv-module.h"
45 #include "ns3/dsr-module.h"
46 
47 #include "ns3/aodvq-module.h"
48 #include "ns3/dsdvq-module.h"
49 
50 using namespace ns3;
51 
52 NS_LOG_COMPONENT_DEFINE ("QKD_CHANNEL_TEST");
53 
54 
55 uint32_t m_bytes_total = 0;
56 uint32_t m_bytes_received = 0;
57 uint32_t m_bytes_sent = 0;
58 uint32_t m_packets_received = 0;
59 double m_time = 0;
60 
61 void
62 SentPacket(std::string context, Ptr<const Packet> p){
63 
64  //std::cout << "Received: \t" << (double)Simulator::Now().GetSeconds() << "\n";
65 
66  /*
67  std::cout << "\n ..................SentPacket....." << p->GetUid() << "..." << p->GetSize() << "....... \n";
68  p->Print(std::cout);
69  std::cout << "\n ............................................ \n";
70  */
71 
72  m_bytes_sent += p->GetSize();
73  //m_delayTable.insert (std::make_pair (p->GetUid(), (double)Simulator::Now().GetSeconds()));
74 }
75 
76 void
77 ReceivedPacket(std::string context, Ptr<const Packet> p, const Address& addr){
78 
79  /*
80  std::cout << "\n ..................ReceivedPacket....." << p->GetUid() << "..." << p->GetSize() << "....... \n";
81  p->Print(std::cout);
82  std::cout << "\n ............................................ \n";
83 
84 
85  //double temp = (double)Simulator::Now().GetSeconds();
86  std::map<uint32_t, double >::iterator i = m_delayTable.find ( p->GetUid() );
87  //std::cout << i->first << "\t" << temp - i->second << "\n";
88 
89  if(i != m_delayTable.end()){
90  m_delayTable.erase(i);
91  }
92  */
93  //std::cout << temp << "\t" << temp - m_time << "\n";
94  //m_time = temp;
95  //std::cout << "Received: \t" << (double)Simulator::Now().GetSeconds() << "\t" << p->GetSize() << "\n";
96 
97  //std::cout << "Received: \t" << (double)Simulator::Now().GetSeconds() << "\n";
98  m_bytes_received += p->GetSize();
99  m_bytes_total += p->GetSize();
101 
102 }
103 
104 void
105 Ratio(uint32_t m_bytes_sent, uint32_t m_packets_sent ){
106  std::cout << "Sent (bytes):\t" << m_bytes_sent
107  << "\tReceived (bytes):\t" << m_bytes_received
108  << "\nSent (Packets):\t" << m_packets_sent
109  << "\tReceived (Packets):\t" << m_packets_received
110 
111  << "\nRatio (bytes):\t" << (float)m_bytes_received/(float)m_bytes_sent
112  << "\tRatio (packets):\t" << (float)m_packets_received/(float)m_packets_sent << "\n";
113 }
114 int main (int argc, char *argv[])
115 {
118  //
119  // Explicitly create the nodes required by the topology (shown above).
120  //
121  NS_LOG_INFO ("Create nodes.");
123  n.Create (5);
124 
125  NodeContainer n0n1 = NodeContainer (n.Get(0), n.Get (1));
126  NodeContainer n1n2 = NodeContainer (n.Get(1), n.Get (2));
127  NodeContainer n2n3 = NodeContainer (n.Get(2), n.Get (3));
128  NodeContainer n3n4 = NodeContainer (n.Get(3), n.Get (4));
129  NodeContainer qkdNodes = NodeContainer (
130  n.Get (0),
131  n.Get (2),
132  n.Get (4)
133  );
134 
135  //Underlay network - set routing protocol
136 
137  //Enable OLSR
138  //OlsrHelper routingProtocol;
139  //DsdvHelper routingProtocol;
140 
141  InternetStackHelper internet;
142  //internet.SetRoutingHelper (routingProtocol);
143  internet.Install (n);
144 
145  // Set Mobility for all nodes
147  Ptr<ListPositionAllocator> positionAlloc = CreateObject <ListPositionAllocator>();
148  positionAlloc ->Add(Vector(0, 200, 0)); // node0
149  positionAlloc ->Add(Vector(200, 200, 0)); // node1
150  positionAlloc ->Add(Vector(400, 200, 0)); // node2
151  positionAlloc ->Add(Vector(600, 200, 0)); // node3
152  positionAlloc ->Add(Vector(800, 200, 0)); // node4
153 
154  mobility.SetPositionAllocator(positionAlloc);
155  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
156  mobility.Install(n);
157 
158  // We create the channels first without any IP addressing information
159  NS_LOG_INFO ("Create channels.");
160  PointToPointHelper p2p;
161  p2p.SetDeviceAttribute ("DataRate", StringValue ("1Gbps"));
162  //p2p.SetChannelAttribute ("Delay", StringValue ("100ps"));
163 
164  NetDeviceContainer d0d1 = p2p.Install (n0n1);
165  NetDeviceContainer d1d2 = p2p.Install (n1n2);
166  NetDeviceContainer d2d3 = p2p.Install (n2n3);
167  NetDeviceContainer d3d4 = p2p.Install (n3n4);
168 
169  //
170  // We've got the "hardware" in place. Now we need to add IP addresses.
171  //
172  NS_LOG_INFO ("Assign IP Addresses.");
173  Ipv4AddressHelper ipv4;
174 
175  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
176  Ipv4InterfaceContainer i0i1 = ipv4.Assign (d0d1);
177 
178  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
179  Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
180 
181  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
182  Ipv4InterfaceContainer i2i3 = ipv4.Assign (d2d3);
183 
184  ipv4.SetBase ("10.1.4.0", "255.255.255.0");
185  Ipv4InterfaceContainer i3i4 = ipv4.Assign (d3d4);
186 
187  // Create router nodes, initialize routing database and set up the routing
188  // tables in the nodes.
190 
191  //Overlay network - set routing protocol
192 
193  //Enable Overlay Routing
194  //AodvqHelper routingOverlayProtocol;
195  DsdvqHelper routingOverlayProtocol;
196 
197  //
198  // Explicitly create the channels required by the topology (shown above).
199  //
200  QKDHelper QHelper;
201 
202  //install QKD Managers on the nodes
203  QHelper.SetRoutingHelper (routingOverlayProtocol);
204  QHelper.InstallQKDManager (qkdNodes);
205 
206  //Create QKDNetDevices and create QKDbuffers
207  Ipv4InterfaceAddress va02_0 (Ipv4Address ("11.0.0.1"), Ipv4Mask ("255.255.255.0"));
208  Ipv4InterfaceAddress va02_2 (Ipv4Address ("11.0.0.2"), Ipv4Mask ("255.255.255.0"));
209 
210  Ipv4InterfaceAddress va24_2 (Ipv4Address ("11.0.0.3"), Ipv4Mask ("255.255.255.0"));
211  Ipv4InterfaceAddress va24_4 (Ipv4Address ("11.0.0.4"), Ipv4Mask ("255.255.255.0"));
212 
213  //create QKD connection between nodes 0 and 2
214  NetDeviceContainer qkdNetDevices02 = QHelper.InstallOverlayQKD (
215  d0d1.Get(0), d1d2.Get(1),
216  va02_0, va02_2,
217  108576, //min
218  0, //thr - will be set automatically
219  1085760, //max
220  1085760 //current //20485770
221  );
222  //Create graph to monitor buffer changes
223  QHelper.AddGraph(qkdNodes.Get(0), d1d2.Get (0), "myGraph02"); //srcNode, destinationAddress, BufferTitle
224 
225  //create QKD connection between nodes 0 and 2
226  NetDeviceContainer qkdNetDevices24 = QHelper.InstallOverlayQKD (
227  d2d3.Get(0), d3d4.Get(1),
228  va24_2, va24_4,
229  108576, //min
230  0, //thr - will be set automatically
231  1085760, //max
232  1085760 //current //88576
233  );
234  //Create graph to monitor buffer changes
235  QHelper.AddGraph(qkdNodes.Get(1), d3d4.Get (0), "myGraph24"); //srcNode, destinationAddress, BufferTitle
236 
237  NS_LOG_INFO ("Create Applications.");
238 
239  /* QKD APPs for charing
240  //Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (2536));
241 
242  QKDAppChargingHelper qkdChargingApp02("ns3::VirtualTcpSocketFactory", va02_0.GetLocal (), va02_2.GetLocal (), 200000);
243  ApplicationContainer qkdChrgApps02 = qkdChargingApp02.Install ( qkdNetDevices02.Get (0), qkdNetDevices02.Get (1) );
244  qkdChrgApps02.Start (Seconds (10.));
245  qkdChrgApps02.Stop (Seconds (120.));
246 
247  QKDAppChargingHelper qkdChargingApp24("ns3::VirtualTcpSocketFactory", va24_2.GetLocal (), va24_4.GetLocal (), 200000);
248  ApplicationContainer qkdChrgApps24 = qkdChargingApp24.Install ( qkdNetDevices24.Get (0), qkdNetDevices24.Get (1) );
249  qkdChrgApps24.Start (Seconds (10.));
250  qkdChrgApps24.Stop (Seconds (120.));
251  */
252  /* Create user's traffic between v0 and v1 */
253  /* Create sink app */
254  uint16_t sinkPort = 8080;
255  QKDSinkAppHelper packetSinkHelper ("ns3::VirtualUdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
256  ApplicationContainer sinkApps = packetSinkHelper.Install (qkdNodes.Get (2));
257  sinkApps.Start (Seconds (25.));
258  sinkApps.Stop (Seconds (170.));
259 
260  /* Create source app */
261  Address sinkAddress (InetSocketAddress (va24_4.GetLocal (), sinkPort));
262  Address sourceAddress (InetSocketAddress (va02_0.GetLocal (), sinkPort));
263  Ptr<Socket> overlaySocket = Socket::CreateSocket (qkdNodes.Get (0), VirtualUdpSocketFactory::GetTypeId ());
264 
265  Ptr<QKDSend> app = CreateObject<QKDSend> ();
266  app->Setup (overlaySocket, sourceAddress, sinkAddress, 1040, 0, DataRate ("5kbps"));
267  qkdNodes.Get (0)->AddApplication (app);
268  app->SetStartTime (Seconds (25.));
269  app->SetStopTime (Seconds (170.));
270 
274 
275  //if we need we can create pcap files
276  p2p.EnablePcapAll ("QKD_vnet_test");
277  QHelper.EnablePcapAll ("QKD_overlay_vnet_test");
278 
279  Config::Connect("/NodeList/*/ApplicationList/*/$ns3::QKDSend/Tx", MakeCallback(&SentPacket));
280  Config::Connect("/NodeList/*/ApplicationList/*/$ns3::QKDSink/Rx", MakeCallback(&ReceivedPacket));
281 
282  Simulator::Stop ( Seconds (30) );
283  Simulator::Run ();
284 
285  Ratio(app->sendDataStats(), app->sendPacketStats());
286 
287  //Finally print the graphs
288  QHelper.PrintGraphs();
290 }
holds a vector of ns3::Application pointers.
an Inet address class
static Ipv4Address GetAny(void)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void ReceivedPacket(std::string context, Ptr< const Packet > p, const Address &addr)
Ipv4Address GetLocal(void) const
Get the local address.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
NetDeviceContainer Install(NodeContainer c)
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
Ipv4InterfaceContainer i1i2
uint32_t m_bytes_sent
Ipv4InterfaceContainer i3i4
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
Build a set of QKDNetDevice objects.
Definition: qkd-helper.h:52
a polymophic address class
Definition: address.h:90
NodeContainer n3n4
mobility
Definition: third.py:101
uint32_t m_packets_received
Class for representing data rates.
Definition: data-rate.h:88
double m_time
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
uint32_t m_bytes_total
static TypeId GetTypeId(void)
Get the type ID.
holds a vector of ns3::NetDevice pointers
Helper class that adds DSDV routing to nodes.
Definition: dsdvq-helper.h:45
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
NetDeviceContainer InstallOverlayQKD(Ptr< NetDevice > IPa, Ptr< NetDevice > IPb, Ipv4InterfaceAddress da, Ipv4InterfaceAddress db, uint32_t Mmin, uint32_t Mthr, uint32_t Mmax, uint32_t Mcurrent, const std::string typeId)
Install and setup OVERLAY QKD link between the nodes.
Definition: qkd-helper.cc:437
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
Set routing protocol to be used in overlay QKDNetwork.
Definition: qkd-helper.cc:301
uint32_t m_bytes_received
static void Enable(void)
Enable the packet metadata.
void Ratio(uint32_t m_bytes_sent, uint32_t m_packets_sent)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
NodeContainer n2n3
void InstallQKDManager(NodeContainer &n)
Install QKDManager on the node.
Definition: qkd-helper.cc:281
Helper class used to assign positions and mobility models to nodes.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void SentPacket(std::string context, Ptr< const Packet > p)
a class to store IPv4 address information on an interface
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1007
void Add(Vector v)
Add a position to the list of positions.
void AddGraph(Ptr< Node > node, Ptr< NetDevice > device)
ADD QKDGraph.
Definition: qkd-helper.cc:224
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
NodeContainer n1n2
Ipv4InterfaceContainer i2i3
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
void PrintGraphs()
Print QKDGraphs.
Definition: qkd-helper.cc:270