A Discrete-Event Network Simulator
API
qkd_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/applications-module.h"
32 #include "ns3/internet-module.h"
33 #include "ns3/flow-monitor-module.h"
34 #include "ns3/mobility-module.h"
35 #include "ns3/point-to-point-module.h"
36 #include "ns3/gnuplot.h"
37 
38 #include "ns3/qkd-helper.h"
39 #include "ns3/qkd-app-charging-helper.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 
46 #include "ns3/network-module.h"
47 #include "ns3/fd-net-device-module.h"
48 #include "ns3/internet-apps-module.h"
49 
50 using namespace ns3;
51 
52 NS_LOG_COMPONENT_DEFINE ("QKD_CHANNEL_TEST");
53 
54 uint32_t m_bytes_total = 0;
55 uint32_t m_bytes_received = 0;
56 uint32_t m_bytes_sent = 0;
57 uint32_t m_packets_received = 0;
58 double m_time = 0;
59 
60 void
61 SentPacket(std::string context, Ptr<const Packet> p){
62 
63  //std::cout << "Received: \t" << (double)Simulator::Now().GetSeconds() << "\n";
64 
65 
66  //std::cout << "\n ..................SentPacket....." << p->GetUid() << "..." << p->GetSize() << "....... \n";
67  /*
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  /*
82  p->Print(std::cout);
83  std::cout << "\n ............................................ \n";
84 
85 
86  //double temp = (double)Simulator::Now().GetSeconds();
87  std::map<uint32_t, double >::iterator i = m_delayTable.find ( p->GetUid() );
88  //std::cout << i->first << "\t" << temp - i->second << "\n";
89 
90  if(i != m_delayTable.end()){
91  m_delayTable.erase(i);
92  }
93  */
94  //std::cout << temp << "\t" << temp - m_time << "\n";
95  //m_time = temp;
96  //std::cout << "Received: \t" << (double)Simulator::Now().GetSeconds() << "\t" << p->GetSize() << "\n";
97 
98  //std::cout << "Received: \t" << (double)Simulator::Now().GetSeconds() << "\n";
99  m_bytes_received += p->GetSize();
100  m_bytes_total += p->GetSize();
102 
103 }
104 
105 void
106 Ratio(uint32_t m_bytes_sent, uint32_t m_packets_sent ){
107  std::cout << "Sent (bytes):\t" << m_bytes_sent
108  << "\tReceived (bytes):\t" << m_bytes_received
109  << "\nSent (Packets):\t" << m_packets_sent
110  << "\tReceived (Packets):\t" << m_packets_received
111 
112  << "\nRatio (bytes):\t" << (float)m_bytes_received/(float)m_bytes_sent
113  << "\tRatio (packets):\t" << (float)m_packets_received/(float)m_packets_sent << "\n";
114 }
115 
116 
117 int main (int argc, char *argv[])
118 {
121  //
122  // Explicitly create the nodes required by the topology (shown above).
123  //
124  NS_LOG_INFO ("Create nodes.");
126  n.Create (3);
127 
128  NodeContainer n0n1 = NodeContainer (n.Get(0), n.Get (1));
129  NodeContainer n1n2 = NodeContainer (n.Get(1), n.Get (2));
130 
131  //Enable OLSR
132  //AodvHelper routingProtocol;
133  OlsrHelper routingProtocol;
134  //DsdvHelper routingProtocol;
135 
136  InternetStackHelper internet;
137  internet.SetRoutingHelper (routingProtocol);
138  internet.Install (n);
139 
140  // Set Mobility for all nodes
142  Ptr<ListPositionAllocator> positionAlloc = CreateObject <ListPositionAllocator>();
143  positionAlloc ->Add(Vector(0, 200, 0)); // node0
144  positionAlloc ->Add(Vector(200, 200, 0)); // node1
145  positionAlloc ->Add(Vector(400, 200, 0)); // node2
146  mobility.SetPositionAllocator(positionAlloc);
147  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
148  mobility.Install(n);
149 
150  // We create the channels first without any IP addressing information
151  NS_LOG_INFO ("Create channels.");
152  PointToPointHelper p2p;
153  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
154  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
155 
156  NetDeviceContainer d0d1 = p2p.Install (n0n1);
157  NetDeviceContainer d1d2 = p2p.Install (n1n2);
158 
159  //
160  // We've got the "hardware" in place. Now we need to add IP addresses.
161  //
162  NS_LOG_INFO ("Assign IP Addresses.");
163  Ipv4AddressHelper ipv4;
164 
165  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
166  Ipv4InterfaceContainer i0i1 = ipv4.Assign (d0d1);
167 
168  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
169  Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
170 
171  // Create router nodes, initialize routing database and set up the routing
172  // tables in the nodes.
173  //Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
174  //routingProtocol.Install ();
175 
176  //
177  // Explicitly create the channels required by the topology (shown above).
178  //
179  // install QKD Managers on the nodes
180  QKDHelper QHelper;
181  QHelper.InstallQKDManager (n);
182 
183 
184  //create QKD connection between nodes 0 and 1
185  //using random values fr
186  NetDeviceContainer qkdNetDevices01 = QHelper.InstallQKD (
187  d0d1.Get(0), d0d1.Get(1),
188  1048576, //min
189  11324620, //thr
190  52428800, //max
191  52428800 //current //20485770
192  );
193 
194  //Create graph to monitor buffer changes
195  QHelper.AddGraph(n.Get(0), d0d1.Get(0)); //srcNode, destinationAddress, BufferTitle
196 
197 
198  //create QKD connection between nodes 1 and 2
199  //using random values fr
200 
201  NetDeviceContainer qkdNetDevices12 = QHelper.InstallQKD (
202  d1d2.Get(0), d1d2.Get(1),
203  1048576, //min
204  11324620, //thr
205  52428800, //max
206  52428800 //current //20485770
207  );
208 
209  //Create graph to monitor buffer changes
210  QHelper.AddGraph(n.Get(1), d0d1.Get(0)); //srcNode, destinationAddress, BufferTitle
211 
212  NS_LOG_INFO ("Create Applications.");
213 
214  std::cout << "Source IP address: " << i0i1.GetAddress(0) << std::endl;
215  //std::cout << "Destination IP address: " << i1i2.GetAddress(1) << std::endl;
216 
217  /* QKD APPs for charing
218  QKDAppChargingHelper qkdChargingApp("ns3::TcpSocketFactory", i0i1.GetAddress(0), i0i1.GetAddress(1), 3072000);
219  ApplicationContainer qkdChrgApps = qkdChargingApp.Install ( d0d1.Get(0), d0d1.Get(1) );
220  qkdChrgApps.Start (Seconds (5.));
221  qkdChrgApps.Stop (Seconds (1500.));
222 
223  QKDAppChargingHelper qkdChargingApp12("ns3::TcpSocketFactory", i1i2.GetAddress(0), i1i2.GetAddress(1), 3072000);
224  ApplicationContainer qkdChrgApps12 = qkdChargingApp12.Install ( d1d2.Get(0), d1d2.Get(1) );
225  qkdChrgApps12.Start (Seconds (5.));
226  qkdChrgApps12.Stop (Seconds (1500.));
227  */
228 
229  /* Create user's traffic between v0 and v1 */
230  /* Create sink app */
231  uint16_t sinkPort = 8080;
232  QKDSinkAppHelper packetSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
233  ApplicationContainer sinkApps = packetSinkHelper.Install (n.Get (2));
234  sinkApps.Start (Seconds (25.));
235  sinkApps.Stop (Seconds (300.));
236 
237  /* Create source app */
238  Address sinkAddress (InetSocketAddress (i1i2.GetAddress(1), sinkPort));
239  Address sourceAddress (InetSocketAddress (i0i1.GetAddress(0), sinkPort));
241 
242  Ptr<QKDSend> app = CreateObject<QKDSend> ();
243  app->Setup (socket, sourceAddress, sinkAddress, 640, 5, DataRate ("160kbps"));
244  n.Get (0)->AddApplication (app);
245  app->SetStartTime (Seconds (25.));
246  app->SetStopTime (Seconds (300.));
247 
248 
249  //
250  // Create the ping application.
251  // This application knows how to send ICMP echo requests.
252  //
253  /*
254  NS_LOG_INFO ("Create V4Ping Appliation");
255  Ptr<V4Ping> app = CreateObject<V4Ping> ();
256  app->SetAttribute ("Remote", Ipv4AddressValue ( i0i1.GetAddress (1) ) );
257  app->SetAttribute ("Verbose", BooleanValue (true) );
258  n.Get (0)->AddApplication (app);
259  app->SetStartTime (Seconds (10.0));
260  app->SetStopTime (Seconds (21.0));
261  */
262 
266 
267  //if we need we can create pcap files
268  p2p.EnablePcapAll ("QKD_channel_test");
269  //QHelper.EnablePcapAll ("QKD_channel_test_QHELPER");
270 
271  Config::Connect("/NodeList/*/ApplicationList/*/$ns3::QKDSend/Tx", MakeCallback(&SentPacket));
272  Config::Connect("/NodeList/*/ApplicationList/*/$ns3::QKDSink/Rx", MakeCallback(&ReceivedPacket));
273 
274  Simulator::Stop (Seconds (50));
275  Simulator::Run ();
276 
277  Ratio(app->sendDataStats(), app->sendPacketStats());
278 
279  //Finally print the graphs
280  QHelper.PrintGraphs();
282 }
uint32_t m_bytes_total
static TypeId GetTypeId(void)
Get the type ID.
void ReceivedPacket(std::string context, Ptr< const Packet > p, const Address &addr)
holds a vector of ns3::Application pointers.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:157
an Inet address class
static Ipv4Address GetAny(void)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
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)
Ipv4InterfaceContainer i1i2
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
void Ratio(uint32_t m_bytes_sent, uint32_t m_packets_sent)
aggregate IP/TCP/UDP functionality to existing Nodes.
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:40
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.
uint32_t m_bytes_sent
Build a set of QKDNetDevice objects.
Definition: qkd-helper.h:52
double m_time
a polymophic address class
Definition: address.h:90
mobility
Definition: third.py:101
Class for representing data rates.
Definition: data-rate.h:88
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_packets_received
holds a vector of ns3::NetDevice pointers
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
NetDeviceContainer InstallQKD(Ptr< NetDevice > IPa, Ptr< NetDevice > IPb, uint32_t Mmin, uint32_t Mthr, uint32_t Mmax, uint32_t Mcurrent)
Install and setup SINGLE TCP/IP QKD link between the nodes.
Definition: qkd-helper.cc:794
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
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
static void Enable(void)
Enable the packet metadata.
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...
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
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.
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)
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...
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
NodeContainer n1n2
uint32_t m_bytes_received
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
void PrintGraphs()
Print QKDGraphs.
Definition: qkd-helper.cc:270