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