A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
fd-net-device.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 INRIA, 2012 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 * Claudio Freire <klaussfreire@sourceforge.net>
19 */
20
21#ifndef FD_NET_DEVICE_H
22#define FD_NET_DEVICE_H
23
24#include "ns3/address.h"
25#include "ns3/callback.h"
26#include "ns3/data-rate.h"
27#include "ns3/event-id.h"
28#include "ns3/fd-reader.h"
29#include "ns3/mac48-address.h"
30#include "ns3/net-device.h"
31#include "ns3/node.h"
32#include "ns3/packet.h"
33#include "ns3/ptr.h"
34#include "ns3/traced-callback.h"
35
36#include <mutex>
37#include <queue>
38#include <utility>
39
40namespace ns3
41{
42
43/**
44 * \defgroup fd-net-device File Descriptor Network Device
45 * This section documents the API of the ns-3 fd-net-device module.
46 * For a generic functional description, please refer to the ns-3 manual.
47 */
48
49/**
50 * \ingroup fd-net-device
51 * \brief This class performs the actual data reading from the sockets.
52 */
54{
55 public:
57
58 /**
59 * Set size of the read buffer.
60 * \param bufferSize the buffer size
61 */
62 void SetBufferSize(uint32_t bufferSize);
63
64 private:
65 FdReader::Data DoRead() override;
66
67 uint32_t m_bufferSize; //!< size of the read buffer
68};
69
70class Node;
71
72/**
73 * \ingroup fd-net-device
74 *
75 * \brief a NetDevice to read/write network traffic from/into a file descriptor.
76 *
77 * A FdNetDevice object will read and write frames/packets from/to a file descriptor.
78 * This file descriptor might be associated to a Linux TAP/TUN device, to a socket
79 * or to a user space process, allowing the simulation to exchange traffic with the
80 * "outside-world"
81 *
82 */
83class FdNetDevice : public NetDevice
84{
85 public:
86 /**
87 * \brief Get the type ID.
88 * \return the object TypeId
89 */
90 static TypeId GetTypeId();
91
92 /**
93 * Enumeration of the types of frames supported in the class.
94 */
96 {
97 DIX, /**< DIX II / Ethernet II packet */
98 LLC, /**< 802.2 LLC/SNAP Packet*/
99 DIXPI, /**< When using TAP devices, if flag
100 IFF_NO_PI is not set on the device,
101 IP packets will have an extra header:
102 Flags [2 bytes]
103 Proto [2 bytes]
104 Raw protocol(IP, IPv6, etc) frame. */
105 };
106
107 /**
108 * Constructor for the FdNetDevice.
109 */
110 FdNetDevice();
111
112 /**
113 * Destructor for the FdNetDevice.
114 */
115 ~FdNetDevice() override;
116
117 // Delete assignment operator to avoid misuse
118 FdNetDevice(const FdNetDevice&) = delete;
119
120 /**
121 * Set the link layer encapsulation mode of this device.
122 *
123 * \param mode The link layer encapsulation mode of this device.
124 *
125 */
127
128 /**
129 * Get the link layer encapsulation mode of this device.
130 *
131 * \returns The link layer encapsulation mode of this device.
132 */
134
135 /**
136 * Set the associated file descriptor.
137 * \param fd the file descriptor
138 */
139 void SetFileDescriptor(int fd);
140
141 /**
142 * Set a start time for the device.
143 *
144 * @param tStart the start time
145 */
146 void Start(Time tStart);
147
148 /**
149 * Set a stop time for the device.
150 *
151 * @param tStop the stop time
152 */
153 void Stop(Time tStop);
154
155 // inherited from NetDevice base class.
156 void SetIfIndex(const uint32_t index) override;
157 uint32_t GetIfIndex() const override;
158 Ptr<Channel> GetChannel() const override;
159 void SetAddress(Address address) override;
160 Address GetAddress() const override;
161 bool SetMtu(const uint16_t mtu) override;
162 uint16_t GetMtu() const override;
163 bool IsLinkUp() const override;
164 void AddLinkChangeCallback(Callback<void> callback) override;
165 bool IsBroadcast() const override;
166 Address GetBroadcast() const override;
167 bool IsMulticast() const override;
168 Address GetMulticast(Ipv4Address multicastGroup) const override;
169 bool IsPointToPoint() const override;
170 bool IsBridge() const override;
171 bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) override;
172 bool SendFrom(Ptr<Packet> packet,
173 const Address& source,
174 const Address& dest,
175 uint16_t protocolNumber) override;
176 Ptr<Node> GetNode() const override;
177 void SetNode(Ptr<Node> node) override;
178 bool NeedsArp() const override;
181 bool SupportsSendFrom() const override;
182 Address GetMulticast(Ipv6Address addr) const override;
183
184 /**
185 * Set if the NetDevice is able to send Broadcast messages
186 * \param broadcast true if the NetDevice can send Broadcast
187 */
188 virtual void SetIsBroadcast(bool broadcast);
189 /**
190 * Set if the NetDevice is able to send Multicast messages
191 * \param multicast true if the NetDevice can send Multicast
192 */
193 virtual void SetIsMulticast(bool multicast);
194
195 /**
196 * Write packet data to device.
197 * \param buffer The data.
198 * \param length The data length.
199 * \return The size of data written.
200 */
201 virtual ssize_t Write(uint8_t* buffer, size_t length);
202
203 protected:
204 /**
205 * Method Initialization for start and stop attributes.
206 */
207 void DoInitialize() override;
208
209 void DoDispose() override;
210
211 /**
212 * Get the associated file descriptor.
213 * \return the associated file descriptor
214 */
215 int GetFileDescriptor() const;
216
217 /**
218 * Allocate packet buffer.
219 * \param len the length of the buffer
220 * \return A pointer to the newly allocated buffer.
221 */
222 virtual uint8_t* AllocateBuffer(size_t len);
223
224 /**
225 * Free the given packet buffer.
226 * \param buf the buffer to free
227 */
228 virtual void FreeBuffer(uint8_t* buf);
229
230 /**
231 * Callback to invoke when a new frame is received
232 * \param buf a buffer containing the received frame
233 * \param len the length of the frame
234 */
235 void ReceiveCallback(uint8_t* buf, ssize_t len);
236
237 /**
238 * Mutex to increase pending read counter.
239 */
241
242 /**
243 * Number of packets that were received and scheduled for read but not yet read.
244 */
245 std::queue<std::pair<uint8_t*, ssize_t>> m_pendingQueue;
246
247 private:
248 /**
249 * Spin up the device
250 */
251 void StartDevice();
252
253 /**
254 * Tear down the device
255 */
256 void StopDevice();
257
258 /**
259 * Create the FdReader object
260 * \return the created FdReader object
261 */
263
264 /**
265 * Complete additional actions, if any, to spin up down the device
266 */
267 virtual void DoFinishStartingDevice();
268
269 /**
270 * Complete additional actions, if any, to tear down the device
271 */
272 virtual void DoFinishStoppingDevice();
273
274 /**
275 * Forward the frame to the appropriate callback for processing
276 */
277 void ForwardUp();
278
279 /**
280 * Start Sending a Packet Down the Wire.
281 * @param p packet to send
282 * @returns true if success, false on failure
283 */
285
286 /**
287 * Notify that the link is up and ready
288 */
289 void NotifyLinkUp();
290
291 /**
292 * The ns-3 node associated to the net device.
293 */
295
296 /**
297 * a copy of the node id so the read thread doesn't have to GetNode() in
298 * in order to find the node ID. Thread unsafe reference counting in
299 * multithreaded apps is not a good thing.
300 */
302
303 /**
304 * The ns-3 interface index (in the sense of net device index) that has been assigned to this
305 * network device.
306 */
308
309 /**
310 * The MTU associated to the file descriptor technology
311 */
312 uint16_t m_mtu;
313
314 /**
315 * The file descriptor used for receive/send network traffic.
316 */
317 int m_fd;
318
319 /**
320 * Reader for the file descriptor.
321 */
323
324 /**
325 * The net device mac address.
326 */
328
329 /**
330 * The type of encapsulation of the received/transmitted frames.
331 */
333
334 /**
335 * Flag indicating whether or not the link is up. In this case,
336 * whether or not the device is connected to a channel.
337 */
339
340 /**
341 * Callbacks to fire if the link changes state (up or down).
342 */
344
345 /**
346 * Flag indicating whether or not the underlying net device supports
347 * broadcast.
348 */
350
351 /**
352 * Flag indicating whether or not the underlying net device supports
353 * multicast.
354 */
356
357 /**
358 * Maximum number of packets that can be received and scheduled for read but not yet read.
359 */
361
362 /**
363 * Time to start spinning up the device
364 */
366
367 /**
368 * Time to start tearing down the device
369 */
371
372 /**
373 * NetDevice start event
374 */
376 /**
377 * NetDevice stop event
378 */
380
381 /**
382 * The callback used to notify higher layers that a packet has been received.
383 */
385
386 /**
387 * The callback used to notify higher layers that a packet has been received in promiscuous
388 * mode.
389 */
391
392 /**
393 * The trace source fired when packets come into the "top" of the device
394 * at the L3/L2 transition, before being queued for transmission.
395 *
396 * \see class CallBackTraceSource
397 */
399
400 /**
401 * The trace source fired when packets coming into the "top" of the device
402 * at the L3/L2 transition are dropped before being queued for transmission.
403 *
404 * \see class CallBackTraceSource
405 */
407
408 /**
409 * The trace source fired for packets successfully received by the device
410 * immediately before being forwarded up to higher layers (at the L2/L3
411 * transition). This is a promiscuous trace.
412 *
413 * \see class CallBackTraceSource
414 */
416
417 /**
418 * The trace source fired for packets successfully received by the device
419 * immediately before being forwarded up to higher layers (at the L2/L3
420 * transition). This is a non-promiscuous trace.
421 *
422 * \see class CallBackTraceSource
423 */
425
426 /**
427 * The trace source fired for packets successfully received by the device
428 * but which are dropped before being forwarded up to higher layers (at the
429 * L2/L3 transition).
430 *
431 * \see class CallBackTraceSource
432 */
434
435 /**
436 * The trace source fired when the phy layer drops a packet as it tries
437 * to transmit it.
438 *
439 * \todo Remove: this TracedCallback is never invoked.
440 *
441 * \see class CallBackTraceSource
442 */
444
445 /**
446 * The trace source fired when the phy layer drops a packet it has received.
447 *
448 * \see class CallBackTraceSource
449 */
451
452 /**
453 * A trace source that emulates a non-promiscuous protocol sniffer connected
454 * to the device. Unlike your average everyday sniffer, this trace source
455 * will not fire on PACKET_OTHERHOST events.
456 *
457 * On the transmit size, this trace hook will fire after a packet is dequeued
458 * from the device queue for transmission. In Linux, for example, this would
459 * correspond to the point just before a device hard_start_xmit where
460 * dev_queue_xmit_nit is called to dispatch the packet to the PF_PACKET
461 * ETH_P_ALL handlers.
462 *
463 * On the receive side, this trace hook will fire when a packet is received,
464 * just before the receive callback is executed. In Linux, for example,
465 * this would correspond to the point at which the packet is dispatched to
466 * packet sniffers in netif_receive_skb.
467 *
468 * \see class CallBackTraceSource
469 */
471
472 /**
473 * A trace source that emulates a promiscuous mode protocol sniffer connected
474 * to the device. This trace source fire on packets destined for any host
475 * just like your average everyday packet sniffer.
476 *
477 * On the transmit size, this trace hook will fire after a packet is dequeued
478 * from the device queue for transmission. In Linux, for example, this would
479 * correspond to the point just before a device hard_start_xmit where
480 * dev_queue_xmit_nit is called to dispatch the packet to the PF_PACKET
481 * ETH_P_ALL handlers.
482 *
483 * On the receive side, this trace hook will fire when a packet is received,
484 * just before the receive callback is executed. In Linux, for example,
485 * this would correspond to the point at which the packet is dispatched to
486 * packet sniffers in netif_receive_skb.
487 *
488 * \see class CallBackTraceSource
489 */
491};
492
493} // namespace ns3
494
495#endif /* FD_NET_DEVICE_H */
a polymophic address class
Definition: address.h:101
Callback template class.
Definition: callback.h:438
An identifier for simulation events.
Definition: event-id.h:55
This class performs the actual data reading from the sockets.
Definition: fd-net-device.h:54
uint32_t m_bufferSize
size of the read buffer
Definition: fd-net-device.h:67
void SetBufferSize(uint32_t bufferSize)
Set size of the read buffer.
FdReader::Data DoRead() override
The read implementation.
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:84
EventId m_stopEvent
NetDevice stop event.
void StopDevice()
Tear down the device.
virtual void FreeBuffer(uint8_t *buf)
Free the given packet buffer.
bool m_isBroadcast
Flag indicating whether or not the underlying net device supports broadcast.
Ptr< FdReader > m_fdReader
Reader for the file descriptor.
bool IsMulticast() const override
TracedCallback< Ptr< const Packet > > m_macRxDropTrace
The trace source fired for packets successfully received by the device but which are dropped before b...
uint32_t m_ifIndex
The ns-3 interface index (in the sense of net device index) that has been assigned to this network de...
int m_fd
The file descriptor used for receive/send network traffic.
virtual uint8_t * AllocateBuffer(size_t len)
Allocate packet buffer.
uint16_t m_mtu
The MTU associated to the file descriptor technology.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
virtual void SetIsBroadcast(bool broadcast)
Set if the NetDevice is able to send Broadcast messages.
Ptr< Node > GetNode() const override
void Start(Time tStart)
Set a start time for the device.
Ptr< Node > m_node
The ns-3 node associated to the net device.
bool SetMtu(const uint16_t mtu) override
int GetFileDescriptor() const
Get the associated file descriptor.
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
virtual Ptr< FdReader > DoCreateFdReader()
Create the FdReader object.
void SetAddress(Address address) override
Set the address of this interface.
uint32_t m_maxPendingReads
Maximum number of packets that can be received and scheduled for read but not yet read.
std::mutex m_pendingReadMutex
Mutex to increase pending read counter.
Address GetBroadcast() const override
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
TracedCallback< Ptr< const Packet > > m_macRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
Time m_tStart
Time to start spinning up the device.
void SetNode(Ptr< Node > node) override
std::queue< std::pair< uint8_t *, ssize_t > > m_pendingQueue
Number of packets that were received and scheduled for read but not yet read.
void Stop(Time tStop)
Set a stop time for the device.
void SetIfIndex(const uint32_t index) override
EventId m_startEvent
NetDevice start event.
void StartDevice()
Spin up the device.
bool m_isMulticast
Flag indicating whether or not the underlying net device supports multicast.
void DoDispose() override
Destructor implementation.
TracedCallback< Ptr< const Packet > > m_macTxTrace
The trace source fired when packets come into the "top" of the device at the L3/L2 transition,...
bool IsBroadcast() const override
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
A trace source that emulates a promiscuous mode protocol sniffer connected to the device.
Address GetAddress() const override
FdNetDevice()
Constructor for the FdNetDevice.
TracedCallback< Ptr< const Packet > > m_snifferTrace
A trace source that emulates a non-promiscuous protocol sniffer connected to the device.
bool NeedsArp() const override
~FdNetDevice() override
Destructor for the FdNetDevice.
uint16_t GetMtu() const override
void SetEncapsulationMode(FdNetDevice::EncapsulationMode mode)
Set the link layer encapsulation mode of this device.
static TypeId GetTypeId()
Get the type ID.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb) override
EncapsulationMode m_encapMode
The type of encapsulation of the received/transmitted frames.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
Ptr< Channel > GetChannel() const override
NetDevice::PromiscReceiveCallback m_promiscRxCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode.
void NotifyLinkUp()
Notify that the link is up and ready.
bool TransmitStart(Ptr< Packet > p)
Start Sending a Packet Down the Wire.
TracedCallback< Ptr< const Packet > > m_phyRxDropTrace
The trace source fired when the phy layer drops a packet it has received.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
uint32_t GetIfIndex() const override
void AddLinkChangeCallback(Callback< void > callback) override
EncapsulationMode
Enumeration of the types of frames supported in the class.
Definition: fd-net-device.h:96
@ DIX
DIX II / Ethernet II packet.
Definition: fd-net-device.h:97
@ DIXPI
When using TAP devices, if flag IFF_NO_PI is not set on the device, IP packets will have an extra hea...
Definition: fd-net-device.h:99
@ LLC
802.2 LLC/SNAP Packet
Definition: fd-net-device.h:98
bool m_linkUp
Flag indicating whether or not the link is up.
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
virtual void DoFinishStoppingDevice()
Complete additional actions, if any, to tear down the device.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
void SetFileDescriptor(int fd)
Set the associated file descriptor.
Time m_tStop
Time to start tearing down the device.
TracedCallback< Ptr< const Packet > > m_phyTxDropTrace
The trace source fired when the phy layer drops a packet as it tries to transmit it.
virtual ssize_t Write(uint8_t *buffer, size_t length)
Write packet data to device.
bool IsLinkUp() const override
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...
Mac48Address m_address
The net device mac address.
virtual void SetIsMulticast(bool multicast)
Set if the NetDevice is able to send Multicast messages.
void ForwardUp()
Forward the frame to the appropriate callback for processing.
void DoInitialize() override
Method Initialization for start and stop attributes.
FdNetDevice::EncapsulationMode GetEncapsulationMode() const
Get the link layer encapsulation mode of this device.
bool SupportsSendFrom() const override
uint32_t m_nodeId
a copy of the node id so the read thread doesn't have to GetNode() in in order to find the node ID.
virtual void DoFinishStartingDevice()
Complete additional actions, if any, to spin up down the device.
FdNetDevice(const FdNetDevice &)=delete
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
A class that asynchronously reads from a file descriptor.
Definition: fd-reader.h:57
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
an EUI-48 address
Definition: mac48-address.h:46
Network layer to device interface.
Definition: net-device.h:98
Callback< bool, Ptr< NetDevice >, Ptr< const Packet >, uint16_t, const Address & > ReceiveCallback
Definition: net-device.h:322
A network Node.
Definition: node.h:57
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.
A structure representing data read.
Definition: fd-reader.h:91