A Discrete-Event Network Simulator
API
qkd_etsi_014.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 // ^ ^
28 // |-----n3(QKDControl)--|
29 //
30 // - udp flows from n0 to n2
31 
32 #include <fstream>
33 #include "ns3/core-module.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 
41 #include "ns3/qkd-link-helper.h"
42 #include "ns3/qkd-app-helper.h"
43 #include "ns3/qkd-app.h"
44 
45 #include "ns3/olsr-module.h"
46 #include "ns3/dsdv-module.h"
47 
48 #include "ns3/network-module.h"
49 #include "ns3/fd-net-device-module.h"
50 #include "ns3/internet-apps-module.h"
51 
52 #include "ns3/netanim-module.h"
53 
54 using namespace ns3;
55 
56 NS_LOG_COMPONENT_DEFINE ("QKD_CHANNEL_TEST");
57 
58 uint32_t m_bytes_total = 0;
59 uint32_t m_bytes_received = 0;
60 uint32_t m_bytes_sent = 0;
61 uint32_t m_packets_received = 0;
62 double m_time = 0;
63 
64 void
65 SentPacket(std::string context, Ptr<const Packet> p){
66 
67  m_bytes_sent += p->GetSize();
68 }
69 
70 void
71 ReceivedPacket(std::string context, Ptr<const Packet> p, const Address& addr){
72 
73  m_bytes_received += p->GetSize();
74  m_bytes_total += p->GetSize();
76 
77 }
78 
79 void
80 Ratio(uint32_t m_bytes_sent, uint32_t m_packets_sent ){
81  std::cout << "Sent (bytes):\t" << m_bytes_sent
82  << "\tReceived (bytes):\t" << m_bytes_received
83  << "\nSent (Packets):\t" << m_packets_sent
84  << "\tReceived (Packets):\t" << m_packets_received
85 
86  << "\nRatio (bytes):\t" << (float)m_bytes_received/(float)m_bytes_sent
87  << "\tRatio (packets):\t" << (float)m_packets_received/(float)m_packets_sent << "\n";
88 }
89 
90 
91 int main (int argc, char *argv[])
92 {
95  //
96  // Explicitly create the nodes required by the topology (shown above).
97  //
98  NS_LOG_INFO ("Create nodes.");
99  NodeContainer n;
100  n.Create (4);
101 
102  NodeContainer n0n1 = NodeContainer (n.Get(0), n.Get (1));
103  NodeContainer n1n2 = NodeContainer (n.Get(1), n.Get (2));
104 
105  //DsdvHelper routingProtocol;
106  //OlsrHelper routingProtocol;
107 
108  InternetStackHelper internet;
109  //internet.SetRoutingHelper (routingProtocol);
110  internet.Install (n);
111 
112  // Set Mobility for all nodes
114  Ptr<ListPositionAllocator> positionAlloc = CreateObject <ListPositionAllocator>();
115  positionAlloc ->Add(Vector(0, 200, 0)); // node0
116  positionAlloc ->Add(Vector(200, 200, 0)); // node1
117  positionAlloc ->Add(Vector(400, 200, 0)); // node2
118  mobility.SetPositionAllocator(positionAlloc);
119  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
120  mobility.Install(n);
121 
122  // We create the channels first without any IP addressing information
123  NS_LOG_INFO ("Create channels.");
124  PointToPointHelper p2p;
125  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
126  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
127 
128  NetDeviceContainer d0d1 = p2p.Install (n0n1);
129  NetDeviceContainer d1d2 = p2p.Install (n1n2);
130 
131  //
132  // We've got the "hardware" in place. Now we need to add IP addresses.
133  //
134  NS_LOG_INFO ("Assign IP Addresses.");
135  Ipv4AddressHelper ipv4;
136 
137  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
138  Ipv4InterfaceContainer i0i1 = ipv4.Assign (d0d1);
139 
140  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
141  Ipv4InterfaceContainer i1i2 = ipv4.Assign (d1d2);
142 
143  // install LKMS on nodes 0 and 2
144  QKDAppHelper QAHelper;
145  QKDLinkHelper QLinkHelper;
146 
147  ApplicationContainer lkmsApplications;
148  lkmsApplications.Add( QAHelper.InstallKMS(n.Get(0)) );
149  lkmsApplications.Add( QAHelper.InstallKMS(n.Get(2)) );
150 
151  // install QKD Control the node 3
152  Ptr<QKDControl> control = QLinkHelper.InstallQKDControl ( n.Get(3) );
153  // install QKD Buffers on the node 0 and 2
154  QLinkHelper.CreateQKDLink (
155  control,
156  n.Get(0),
157  n.Get(2),
158  1000000, //min
159  1500000, //thr
160  5000000, //ma
161  0 //current
162  );
163  //Create graph to monitor buffer changes
164  QLinkHelper.AddGraph(control, n.Get(0), n.Get(2)); //srcNode, destinationAddress, BufferTitle
165  QLinkHelper.AddGraph(control, n.Get(2), n.Get(0)); //srcNode, destinationAddress, BufferTitle
166 
167  NS_LOG_INFO ("Create Applications.");
168 
169  //Create APP to generate keys
170  ApplicationContainer postprocessingApplications;
171  postprocessingApplications.Add(
172  QAHelper.InstallPostProcessing(
173  n.Get(0),
174  n.Get(2),
175  InetSocketAddress (i0i1.GetAddress(0), 102),
177  8092, //size of key to be added to QKD buffer
178  DataRate ("100kbps"), //average QKD key rate
179  100, //average data packet size
180  DataRate ("1Mbps") //average data traffic rate
181  )
182  );
183 
184  postprocessingApplications.Start (Seconds (5.)); //500
185  postprocessingApplications.Stop (Seconds (19.));
186 
187  //Create APP to consume keys
188  //ALICE sends user's data
189  uint16_t communicationPort = 8081;
190  Ptr<QKDApp> appAlice = CreateObject<QKDApp> ();
191  appAlice->Setup(
192  "tcp", //connection type
193  InetSocketAddress (i0i1.GetAddress(0), communicationPort), //from address
194  InetSocketAddress (i1i2.GetAddress(1), communicationPort), //to address
195  n.Get(2),//bob's location
196  800, //1000 //payload size //NOTE: 1000*8 = 8000, key for OTP 8000, and VMAC +128 > 8092
197  50, //number of packets (to limit transfer - if needed)
198  DataRate ("100Mbps"), //packetRate,
199  "alice" //connection role
200  );
201  n.Get (0)->AddApplication (appAlice);
202  appAlice->SetStartTime (Seconds (20.));
203  appAlice->SetStopTime (Seconds (300.));
204 
205  appAlice->SetAttribute("NumberOfKeyToFetchFromKMS", UintegerValue (3)); //Number of keys to obtain per request!
206 
207  //schedule some actions in advance (GetStatusFromKMS, GetKeysFromKMS or ExchangeInfoMessages)
208  //uint32_t eventId1 = appAlice->ScheduleAction(Seconds(25.), "GetStatusFromKMS");
209  //NS_LOG_INFO(eventId1); //just log the eventId
210  //appAlice->CancelScheduledAction(eventId1);
211 
212  //uint32_t eventId2 = appAlice->ScheduleAction(Seconds(27.), "ExchangeInfoMessages");
213  //NS_LOG_INFO(eventId2); //just log the eventId
214  //appAlice->CancelScheduledAction(eventId2);
215 
216 
217  //uint32_t eventId3 = appAlice->ScheduleAction(Seconds(30.), "GetKeysFromKMS");
218  //NS_LOG_INFO(eventId3); //just log the eventId
219  //appAlice->CancelScheduledAction(eventId3);
220 
221 
222  //BOB receives user's data
223  Ptr<QKDApp> appBob = CreateObject<QKDApp> ();
224  appBob->Setup(
225  "tcp", //connection type
226  InetSocketAddress (i1i2.GetAddress(1), communicationPort), //from address
227  InetSocketAddress (i0i1.GetAddress(0), communicationPort), //to address
228  n.Get(0),//alice's location
229  "bob" //connection role
230  );
231  n.Get (2)->AddApplication (appBob);
232  appBob->SetStartTime (Seconds (18.)); //150
233  appBob->SetStopTime (Seconds (300.));
234 
235  // DEFINE SIMULATION TIME
236  lkmsApplications.Start (Seconds (20.));
237  lkmsApplications.Stop (Seconds (300.));
238 
239  std::cout << "\t SrcNode: " << n.Get(0)->GetId() << " Source IP address: " << i0i1.GetAddress(0) << std::endl;
240  std::cout << "\t DstNode: " << n.Get(2)->GetId() << " Destination IP address: " << i1i2.GetAddress(1) << std::endl;
241 
243 
247 
248  //if we need we can create pcap files
249  AsciiTraceHelper ascii;
250  p2p.EnableAsciiAll (ascii.CreateFileStream ("qkd_channel_test.tr"));
251  p2p.EnablePcapAll ("qkd_channel_test");
252  AnimationInterface anim ("qkd_channel_test.xml"); // where "animation.xml" is any arbitrary filename
253 
254  //Config::Connect("/NodeList/*/ApplicationList/*/$ns3::QKDApp/Tx", MakeCallback(&SentPacket));
255  //Config::Connect("/NodeList/*/ApplicationList/*/$ns3::QKDApp/Rx", MakeCallback(&ReceivedPacket));
256 
257  Simulator::Stop (Seconds (31));
258  Simulator::Run ();
259 
260  //Ratio(app->sendDataStats(), app->sendPacketStats());
261 
262  //Finally print the graphs
263  QLinkHelper.PrintGraphs();
265 }
NodeContainer n1n2
Ipv4InterfaceContainer i1i2
a polymophic address class
Definition: address.h:91
Interface to network animator.
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Class for representing data rates.
Definition: data-rate.h:89
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t GetId(void) const
Definition: node.cc:109
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:159
static void EnablePrinting(void)
Enable printing packets metadata.
Definition: packet.cc:572
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
static void Enable(void)
Enable the packet metadata.
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 ...
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.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
A helper to make it easier to instantiate an ns3::QKDAppApplication on a set of nodes.
ApplicationContainer InstallPostProcessing(Ptr< Node > node1, Ptr< Node > node2, Address masterAddress, Address slaveAddress, Address masterKMSAddress, Address slaveKMSAddress, uint32_t keySizeInBits, DataRate keyRate, uint32_t packetSize, DataRate dataRate)
Install an ns3::QKDPostprocessingApplication on the node configured with all the attributes set with ...
Ptr< QKDKeyManagerSystemApplication > InstallKMS(Ptr< Node > node, Ipv4Address kmsAddress) const
Install an ns3::QKDKeyManagmentSystem on the node configured with all the attributes set with SetAttr...
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
mobility
Definition: third.py:108
uint32_t m_bytes_received
Definition: qkd_etsi_014.cc:59
double m_time
Definition: qkd_etsi_014.cc:62
uint32_t m_bytes_total
Definition: qkd_etsi_014.cc:58
void SentPacket(std::string context, Ptr< const Packet > p)
Definition: qkd_etsi_014.cc:65
void Ratio(uint32_t m_bytes_sent, uint32_t m_packets_sent)
Definition: qkd_etsi_014.cc:80
uint32_t m_packets_received
Definition: qkd_etsi_014.cc:61
uint32_t m_bytes_sent
Definition: qkd_etsi_014.cc:60
void ReceivedPacket(std::string context, Ptr< const Packet > p, const Address &addr)
Definition: qkd_etsi_014.cc:71