A Discrete-Event Network Simulator
API
qkd-net-device.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 VSB-TU Ostrava
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 #include "ns3/log.h"
22 #include "ns3/queue.h"
23 #include "ns3/simulator.h"
24 #include "ns3/mac48-address.h"
25 #include "ns3/llc-snap-header.h"
26 #include "ns3/error-model.h"
27 #include "qkd-net-device.h"
28 #include "ns3/pointer.h"
29 #include "ns3/channel.h"
30 #include "ns3/trace-source-accessor.h"
31 #include "ns3/uinteger.h"
32 
33 #include <vector>
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("QKDNetDevice");
38 NS_OBJECT_ENSURE_REGISTERED (QKDNetDevice);
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::QKDNetDevice")
44  .SetParent<NetDevice> ()
45  .AddConstructor<QKDNetDevice> ()
46  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
47  UintegerValue (1500),
50  MakeUintegerChecker<uint16_t> ())
51  .AddTraceSource ("MacTx",
52  "Trace source indicating a packet has arrived for transmission by this device",
54  "ns3::QKDNetDevice::MacTx")
55  .AddTraceSource ("MacTxDrop",
56  "Trace source indicating a packet has been dropped "
57  "by the device before transmission",
59  "ns3::Packet::MacTxDrop")
60  .AddTraceSource ("MacPromiscRx",
61  "A packet has been received by this device, has been passed up from the physical layer "
62  "and is being forwarded up the local protocol stack. This is a promiscuous trace,",
64  "ns3::QKDNetDevice::MacPromiscRx")
65  .AddTraceSource ("MacRx",
66  "A packet has been received by this device, has been passed up from the physical layer "
67  "and is being forwarded up the local protocol stack. This is a non-promiscuous trace,",
69  "ns3::QKDNetDevice::MacRx")
70  //
71  // Transmit queueing discipline for the device which includes its own set
72  // of trace hooks.
73  //
74  .AddAttribute ("TxQueue",
75  "A queue to use as the transmit queue in the device.",
76  PointerValue (),
79  //
80  // Trace sources designed to simulate a packet sniffer facility (tcpdump).
81  //
82  .AddTraceSource ("Sniffer",
83  "Trace source simulating a non-promiscuous packet sniffer attached to the device",
85  "ns3::QKDNetDevice::Sniffer")
86  .AddTraceSource ("PromiscSniffer",
87  "Trace source simulating a promiscuous packet sniffer attached to the device",
89  "ns3::QKDNetDevice::PromiscSniffer")
90  ;
91  return tid;
92 }
93 
95 {
96  m_needsArp = false;
97  m_supportsSendFrom = true;
98  m_isPointToPoint = true;
99 }
100 
101 
102 void
104 {
105  m_sendCb = sendCb;
106 }
107 
108 void
110 {
111  m_needsArp = needsArp;
112 }
113 
114 void
115 QKDNetDevice::SetSupportsSendFrom (bool supportsSendFrom)
116 {
117  m_supportsSendFrom = supportsSendFrom;
118 }
119 
120 void
121 QKDNetDevice::SetIsPointToPoint (bool isPointToPoint)
122 {
123  m_isPointToPoint = isPointToPoint;
124 }
125 
126 bool
127 QKDNetDevice::SetMtu (const uint16_t mtu)
128 {
129  m_mtu = mtu;
130  return true;
131 }
132 
133 
135 {
137 }
138 
139 
141 {
143  m_node = 0;
145 }
146 
147 void
149 {
150  NS_LOG_FUNCTION (this << q);
151  m_queue = q;
152 }
153 
156 {
157  NS_LOG_FUNCTION (this);
158  return m_queue;
159 }
160 
161 bool
163 {
164  NS_LOG_FUNCTION (this);
165  return true;
166 }
167 
168 
169 void
170 QKDNetDevice::SetIfIndex (const uint32_t index)
171 {
172  m_index = index;
173 }
174 
175 uint32_t
177 {
178  return m_index;
179 }
180 
183 {
184  return Ptr<Channel> ();
185 }
186 
187 Address
189 {
190  return m_myAddress;
191 }
192 
193 void
195 {
196  m_myAddress = addr;
197 }
198 
199 uint16_t
201 {
202  return m_mtu;
203 }
204 
205 bool
207 {
208  return true;
209 }
210 
211 void
213 {
214 }
215 
216 bool
218 {
219  return true;
220 }
221 
222 Address
224 {
225  return Mac48Address ("ff:ff:ff:ff:ff:ff");
226 }
227 
228 bool
230 {
231  return false;
232 }
233 
235 {
236  return Mac48Address ("ff:ff:ff:ff:ff:ff");
237 }
238 
240 {
241  return Mac48Address ("ff:ff:ff:ff:ff:ff");
242 }
243 
244 
245 bool
247 {
248  return m_isPointToPoint;
249 }
250 
251 bool
252 QKDNetDevice::Send (Ptr<Packet> p, const Address& dest, uint16_t protocolNumber)
253 {
254  NS_LOG_FUNCTION (this << dest << p->GetSize () );
255  m_macTxTrace (p);
256 
257  NS_LOG_FUNCTION( this << " sends packet " << p->GetUid() << " of size " << p->GetSize() << "\n" << p);
258 
259 
260  /*
261  std::cout << "\n###################################################\n";
262  p->Print(std::cout);
263  std::cout << "\n###################################################\n";
264  */
265 
266  if (m_sendCb (p, GetAddress (), dest, protocolNumber)){
267  m_snifferTrace (p);
269  }else{
270  m_macTxDropTrace (p);
271  return false;
272  }
273  return true;
274 }
275 
276 
277 bool
278 QKDNetDevice::Receive (Ptr<Packet> p, uint16_t protocol,
279  const Address &source, const Address &destination,
280  PacketType packetType)
281 {
282  NS_LOG_FUNCTION (this << p->GetSize () << p );
283 
284  //
285  // For all kinds of packetType we receive, we hit the promiscuous sniffer
286  // hook and pass a copy up to the promiscuous callback. Pass a copy to
287  // make sure that nobody messes with our packet.
288  //
290  if (!m_promiscRxCallback.IsNull ())
291  {
293  m_promiscRxCallback (this, p, protocol, source, destination, packetType);
294  }
295 
296  //
297  // If this packet is not destined for some other host, it must be for us
298  // as either a broadcast, multicast or unicast. We need to hit the mac
299  // packet received trace hook and forward the packet up the stack.
300  //
301  if (packetType != PACKET_OTHERHOST)
302  {
303  m_snifferTrace (p);
304  m_macRxTrace (p);
305  m_rxCallback (this, p, protocol, source);
306  }
307 
308  return true;
309 
310 }
311 
312 
313 bool
314 QKDNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
315 {
316  NS_LOG_FUNCTION (this << packet << source << dest << protocolNumber);
317  return false;
318 }
319 
320 Ptr<Node>
322 {
323  return m_node;
324 }
325 
326 void
328 {
329  m_node = node;
330 }
331 
332 bool
334 {
335  return m_needsArp;
336 }
337 
338 void
340 {
341  m_rxCallback = cb;
342 }
343 
344 void
346 {
347  m_promiscRxCallback = cb;
348 }
349 
350 bool
352 {
353  return m_supportsSendFrom;
354 }
355 
356 bool QKDNetDevice::IsBridge (void) const
357 {
358  return false;
359 }
360 
361 } // namespace ns3
SendCallback m_sendCb
Ptr< Queue< Packet > > m_queue
The Queue which this PointToPointNetDevice uses as a packet source.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
Packet addressed to someone else.
Definition: net-device.h:310
TracedCallback< Ptr< const Packet > > m_macRxTrace
ReceiveCallback m_rxCallback
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
virtual Address GetBroadcast(void) const
Ptr< AttributeChecker > MakePointerChecker(void)
Create a PointerChecker for a type.
Definition: pointer.h:224
TracedCallback< Ptr< const Packet > > m_snifferTrace
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
Ptr< Queue< Packet > > GetQueue(void) const
Get a copy of the attached Queue.
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual uint16_t GetMtu(void) const
virtual bool IsMulticast(void) const
static TypeId GetTypeId(void)
bool SetMtu(const uint16_t mtu)
Configure the reported MTU for the virtual device.
virtual bool IsBroadcast(void) const
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
Ptr< Node > m_node
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
The trace source fired when packets coming into the "top" of the device at the L3/L2 transition are d...
virtual bool IsReady(void) const
virtual Ptr< Channel > GetChannel(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
virtual void SetIfIndex(const uint32_t index)
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
Hold an unsigned integer type.
Definition: uinteger.h:44
virtual bool NeedsArp(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual void DoDispose(void)
Destructor implementation.
virtual bool SupportsSendFrom() const
virtual Ptr< Node > GetNode(void) const
virtual uint32_t GetIfIndex(void) const
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
void SetSendCallback(SendCallback transmitCb)
Set the user callback to be called when a L2 packet is to be transmitted.
TracedCallback< Ptr< const Packet > > m_macTxTrace
void SetIsPointToPoint(bool isPointToPoint)
Configure whether the virtual device is point-to-point.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
an EUI-48 address
Definition: mac48-address.h:43
virtual void SetAddress(Address address)
Set the address of this interface.
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
PromiscReceiveCallback m_promiscRxCallback
Describes an IPv6 address.
Definition: ipv6-address.h:49
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
virtual bool IsLinkUp(void) const
Network layer to device interface.
Definition: net-device.h:95
void SetQueue(Ptr< Queue< Packet > > queue)
Attach a queue to the PointToPointNetDevice.
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:302
virtual void SetNode(Ptr< Node > node)
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
void SetNeedsArp(bool needsArp)
Configure whether the virtual device needs ARP.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
virtual void AddLinkChangeCallback(Callback< void > callback)
void SetSupportsSendFrom(bool supportsSendFrom)
Configure whether the virtual device supports SendFrom.
virtual Address GetAddress(void) const