A Discrete-Event Network Simulator
API
fd-net-device.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 INRIA, 2012 University of Washington
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: Alina Quereilhac <alina.quereilhac@inria.fr>
19  * Claudio Freire <klaussfreire@sourceforge.net>
20  */
21 
22 #ifndef FD_NET_DEVICE_H
23 #define FD_NET_DEVICE_H
24 
25 #include "ns3/address.h"
26 #include "ns3/callback.h"
27 #include "ns3/data-rate.h"
28 #include "ns3/event-id.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 #include "ns3/unix-fd-reader.h"
36 
37 #include <mutex>
38 #include <utility>
39 #include <queue>
40 
41 namespace ns3 {
42 
43 
55 {
56 public:
58 
63  void SetBufferSize (uint32_t bufferSize);
64 
65 private:
66  FdReader::Data DoRead (void);
67 
68  uint32_t m_bufferSize;
69 };
70 
71 class Node;
72 
84 class FdNetDevice : public NetDevice
85 {
86 public:
91  static TypeId GetTypeId (void);
92 
97  {
98  DIX,
99  LLC,
100  DIXPI,
106  };
107 
111  FdNetDevice ();
112 
116  virtual ~FdNetDevice ();
117 
118  // Delete assignment operator to avoid misuse
119  FdNetDevice (FdNetDevice const &) = delete;
120 
128 
135 
140  void SetFileDescriptor (int fd);
141 
147  void Start (Time tStart);
148 
154  void Stop (Time tStop);
155 
156  // inherited from NetDevice base class.
157  virtual void SetIfIndex (const uint32_t index);
158  virtual uint32_t GetIfIndex (void) const;
159  virtual Ptr<Channel> GetChannel (void) const;
160  virtual void SetAddress (Address address);
161  virtual Address GetAddress (void) const;
162  virtual bool SetMtu (const uint16_t mtu);
163  virtual uint16_t GetMtu (void) const;
164  virtual bool IsLinkUp (void) const;
165  virtual void AddLinkChangeCallback (Callback<void> callback);
166  virtual bool IsBroadcast (void) const;
167  virtual Address GetBroadcast (void) const;
168  virtual bool IsMulticast (void) const;
169  virtual Address GetMulticast (Ipv4Address multicastGroup) const;
170  virtual bool IsPointToPoint (void) const;
171  virtual bool IsBridge (void) const;
172  virtual bool Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
173  virtual bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
174  virtual Ptr<Node> GetNode (void) const;
175  virtual void SetNode (Ptr<Node> node);
176  virtual bool NeedsArp (void) const;
179  virtual bool SupportsSendFrom () const;
180  virtual Address GetMulticast (Ipv6Address addr) const;
181 
186  virtual void SetIsBroadcast (bool broadcast);
191  virtual void SetIsMulticast (bool multicast);
192 
199  virtual ssize_t Write (uint8_t *buffer, size_t length);
200 
201 protected:
205  virtual void DoInitialize (void);
206 
207  virtual void DoDispose (void);
208 
213  int GetFileDescriptor (void) const;
214 
220  virtual uint8_t* AllocateBuffer (size_t len);
221 
226  virtual void FreeBuffer (uint8_t* buf);
227 
233  void ReceiveCallback (uint8_t *buf, ssize_t len);
234 
238  std::mutex m_pendingReadMutex;
239 
243  std::queue< std::pair<uint8_t *, ssize_t> > m_pendingQueue;
244 
245 private:
249  void StartDevice (void);
250 
254  void StopDevice (void);
255 
260  virtual Ptr<FdReader> DoCreateFdReader (void);
261 
265  virtual void DoFinishStartingDevice (void);
266 
270  virtual void DoFinishStoppingDevice (void);
271 
275  void ForwardUp (void);
276 
283 
287  void NotifyLinkUp (void);
288 
293 
299  uint32_t m_nodeId;
300 
304  uint32_t m_ifIndex;
305 
309  uint16_t m_mtu;
310 
314  int m_fd;
315 
320 
325 
330 
335  bool m_linkUp;
336 
341 
347 
353 
358 
363 
368 
377 
382 
387 
395 
403 
412 
421 
430 
440 
447 
467 
487 
488 };
489 
490 } // namespace ns3
491 
492 #endif /* FD_NET_DEVICE_H */
493 
a polymophic address class
Definition: address.h:91
An identifier for simulation events.
Definition: event-id.h:54
This class performs the actual data reading from the sockets.
Definition: fd-net-device.h:55
uint32_t m_bufferSize
size of the read buffer
Definition: fd-net-device.h:68
void SetBufferSize(uint32_t bufferSize)
Set size of the read buffer.
FdReader::Data DoRead(void)
The read implementation.
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:85
EventId m_stopEvent
NetDevice stop event.
virtual uint32_t GetIfIndex(void) const
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.
virtual void DoInitialize(void)
Method Initialization for start and stop attributes.
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.
virtual void SetIsBroadcast(bool broadcast)
Set if the NetDevice is able to send Broadcast messages.
virtual void DoDispose(void)
Destructor implementation.
virtual void SetNode(Ptr< Node > node)
virtual ~FdNetDevice()
Destructor for the FdNetDevice.
FdNetDevice::EncapsulationMode GetEncapsulationMode(void) const
Get the link layer encapsulation mode of this device.
void Start(Time tStart)
Set a start time for the device.
Ptr< Node > m_node
The ns-3 node associated to the net device.
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
virtual uint16_t GetMtu(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
uint32_t m_maxPendingReads
Maximum number of packets that can be received and scheduled for read but not yet read.
virtual bool IsMulticast(void) const
std::mutex m_pendingReadMutex
Mutex to increase pending read counter.
std::queue< std::pair< uint8_t *, ssize_t > > m_pendingQueue
Number of packets that were received and scheduled for read but not yet read.
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...
void NotifyLinkUp(void)
Notify that the link is up and ready.
virtual bool NeedsArp(void) const
Time m_tStart
Time to start spinning up the device.
void Stop(Time tStop)
Set a stop time for the device.
virtual void DoFinishStoppingDevice(void)
Complete additional actions, if any, to tear down the device.
EventId m_startEvent
NetDevice start event.
bool m_isMulticast
Flag indicating whether or not the underlying net device supports multicast.
virtual Address GetAddress(void) const
TracedCallback< Ptr< const Packet > > m_macTxTrace
The trace source fired when packets come into the "top" of the device at the L3/L2 transition,...
virtual void SetIfIndex(const uint32_t index)
virtual bool SetMtu(const uint16_t mtu)
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
A trace source that emulates a promiscuous mode protocol sniffer connected to the device.
virtual void SetAddress(Address address)
Set the address of this interface.
void ForwardUp(void)
Forward the frame to the appropriate callback for processing.
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.
virtual bool IsLinkUp(void) const
static TypeId GetTypeId(void)
Get the type ID.
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
void SetEncapsulationMode(FdNetDevice::EncapsulationMode mode)
Set the link layer encapsulation mode of this device.
virtual Ptr< FdReader > DoCreateFdReader(void)
Create the FdReader object.
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual Ptr< Node > GetNode(void) const
EncapsulationMode m_encapMode
The type of encapsulation of the received/transmitted frames.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
NetDevice::PromiscReceiveCallback m_promiscRxCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode.
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.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
EncapsulationMode
Enumeration of the types of frames supported in the class.
Definition: fd-net-device.h:97
@ DIX
DIX II / Ethernet II packet.
Definition: fd-net-device.h:98
@ 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
Definition: fd-net-device.h:99
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
bool m_linkUp
Flag indicating whether or not the link is up.
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
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.
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
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 Ptr< Channel > GetChannel(void) const
virtual void DoFinishStartingDevice(void)
Complete additional actions, if any, to spin up down the device.
Mac48Address m_address
The net device mac address.
FdNetDevice(FdNetDevice const &)=delete
virtual void SetIsMulticast(bool multicast)
Set if the NetDevice is able to send Multicast messages.
virtual bool SupportsSendFrom() const
virtual bool IsBroadcast(void) const
virtual Address GetBroadcast(void) const
int GetFileDescriptor(void) const
Get the associated file descriptor.
void StopDevice(void)
Tear down the device.
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.
void StartDevice(void)
Spin up the device.
A class that asynchronously reads from a file descriptor.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Describes an IPv6 address.
Definition: ipv6-address.h:50
an EUI-48 address
Definition: mac48-address.h:44
Network layer to device interface.
Definition: net-device.h:96
Callback< bool, Ptr< NetDevice >, Ptr< const Packet >, uint16_t, const Address & > ReceiveCallback
Definition: net-device.h:318
A network Node.
Definition: node.h:57
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
A structure representing data read.