24 #include "ns3/abort.h"
25 #include "ns3/boolean.h"
26 #include "ns3/channel.h"
28 #include "ns3/ethernet-header.h"
29 #include "ns3/ethernet-trailer.h"
31 #include "ns3/llc-snap-header.h"
32 #include "ns3/mac48-address.h"
33 #include "ns3/pointer.h"
34 #include "ns3/simulator.h"
35 #include "ns3/string.h"
36 #include "ns3/trace-source-accessor.h"
37 #include "ns3/uinteger.h"
40 #include <arpa/inet.h>
41 #include <net/ethernet.h>
48 : m_bufferSize (65536)
85 .AddConstructor<FdNetDevice> ()
86 .AddAttribute (
"Address",
87 "The MAC address of this device.",
90 MakeMac48AddressChecker ())
91 .AddAttribute (
"Start",
92 "The simulation time at which to spin up the device thread.",
96 .AddAttribute (
"Stop",
97 "The simulation time at which to tear down the device thread.",
101 .AddAttribute (
"EncapsulationMode",
102 "The link-layer encapsulation type to use.",
108 .AddAttribute (
"RxQueueSize",
"Maximum size of the read queue. "
109 "This value limits number of packets that have been read "
110 "from the network into a memory buffer but have not yet "
111 "been processed by the simulator.",
114 MakeUintegerChecker<uint32_t> ())
123 .AddTraceSource (
"MacTx",
124 "Trace source indicating a packet has arrived for transmission by this device",
126 .AddTraceSource (
"MacTxDrop",
127 "Trace source indicating a packet has been dropped by the device before transmission",
129 .AddTraceSource (
"MacPromiscRx",
130 "A packet has been received by this device, has been passed up from the physical layer "
131 "and is being forwarded up the local protocol stack. This is a promiscuous trace,",
133 .AddTraceSource (
"MacRx",
134 "A packet has been received by this device, has been passed up from the physical layer "
135 "and is being forwarded up the local protocol stack. This is a non-promiscuous trace,",
141 .AddTraceSource (
"Sniffer",
142 "Trace source simulating a non-promiscuous packet sniffer attached to the device",
144 .AddTraceSource (
"PromiscSniffer",
145 "Trace source simulating a promiscuous packet sniffer attached to the device",
157 m_isBroadcast (true),
158 m_isMulticast (false),
159 m_pendingReadCount (0),
222 NS_LOG_DEBUG (
"FdNetDevice::Start(): Failure, invalid file descriptor.");
279 struct timespec time = { 0, 100000000L };
280 nanosleep (&time, NULL);
295 uint8_t *buf2 = (uint8_t*)malloc (len + 4);
296 memcpy (buf2 + 4, buf, len);
303 uint16_t proto = 0x0008;
306 if (buf[12] == 0x81 && buf[13] == 0x00 && len > 18)
309 proto = buf[16] | (buf[17] << 8);
314 proto = buf[12] | (buf[13] << 8);
317 buf2[0] = (uint8_t)flags;
318 buf2[1] = (uint8_t)(flags >> 8);
319 buf2[2] = (uint8_t)proto;
320 buf2[3] = (uint8_t)(proto >> 8);
334 memmove (buf, buf + 4, len);
335 buf = (uint8_t*)realloc (buf, len);
361 Ptr<Packet> packet = Create<Packet> (
reinterpret_cast<const uint8_t *
> (buf), len);
374 bool isBroadcast =
false;
375 bool isMulticast =
false;
430 else if (isMulticast)
530 ssize_t len = (ssize_t) packet->
GetSize ();
531 uint8_t *buffer = (uint8_t*)malloc (len);
540 ssize_t written = write (
m_fd, buffer, len);
543 if (written == -1 || written != len)
555 if (
m_fd == -1 and fd > 0)
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
keep track of time values and allow control of global simulation resolution
A structure representing data read.
#define NS_LOG_FUNCTION(parameters)
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
DIX II / Ethernet II packet.
void ForwardUp(uint8_t *buf, ssize_t len)
void SetFileDescriptor(int fd)
Set the associated file descriptor.
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual bool SupportsSendFrom() const
EncapsulationMode m_encapMode
When using TAP devices, if flag IFF_NO_PI is not set on the device, IP packets will have an extra hea...
virtual bool NeedsArp(void) const
TracedCallback< Ptr< const Packet > > m_macRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
uint64_t GetUid(void) const
A packet is allocated a new uid when it is created empty or with zero-filled payload.
bool IsNull(void) const
Check for null implementation.
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
uint32_t GetSize(void) const
bool IsBroadcast(void) const
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
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...
static void RemovePIHeader(uint8_t *&buf, ssize_t &len)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
virtual bool IsMulticast(void) const
Ptr< FdNetDeviceFdReader > m_fdReader
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
void Stop(Time tStop)
Set a stop time for the device.
virtual void SetIsBroadcast(bool broadcast)
virtual uint32_t GetIfIndex(void) const
virtual void SetIfIndex(const uint32_t index)
a polymophic address class
virtual Ptr< Node > GetNode(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...
hold variables of type 'enum'
virtual Ptr< Channel > GetChannel(void) const
static Mac48Address GetMulticast(Ipv4Address address)
hold objects of type ns3::Time
int m_fd
The file descriptor to read from.
virtual void SetIsMulticast(bool multicast)
A class which provides a simple way to implement a Critical Section.
Hold an unsigned integer type.
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
uint32_t m_maxPendingReads
virtual Address GetBroadcast(void) const
FdNetDeviceFdReader()
Constructor for the FdNetDevice.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void ReceiveCallback(uint8_t *buf, ssize_t len)
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
#define NS_LOG_LOGIC(msg)
virtual bool IsLinkUp(void) const
NS_LOG_COMPONENT_DEFINE("FdNetDevice")
static Mac48Address ConvertFrom(const Address &address)
void SetEncapsulationMode(FdNetDevice::EncapsulationMode mode)
Set the link layer encapsulation mode of this device.
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Ptr< Packet > Copy(void) const
TracedCallback m_linkChangeCallbacks
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
A trace source that emulates a promiscuous mode protocol sniffer connected to the device...
virtual bool IsBroadcast(void) const
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual void SetNode(Ptr< Node > node)
TracedCallback< Ptr< const Packet > > m_snifferTrace
A trace source that emulates a non-promiscuous protocol sniffer connected to the device.
FdReader::Data DoRead(void)
The read implementation.
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
FdNetDevice::EncapsulationMode GetEncapsulationMode(void) const
Get the link layer encapsulation mode of this device.
#define NS_ASSERT_MSG(condition, message)
TracedCallback< Ptr< const Packet > > m_phyRxDropTrace
The trace source fired when the phy layer drops a packet it has received.
uint32_t m_pendingReadCount
virtual bool SetMtu(const uint16_t mtu)
Describes an IPv6 address.
Ipv4 addresses are stored in host order in this class.
static TypeId GetTypeId(void)
void ConnectWithoutContext(const CallbackBase &callback)
uint32_t GetId(void) const
virtual Address GetAddress(void) const
Network layer to device interface.
hold objects of type ns3::Mac48Address
virtual void SetAddress(Address address)
Set the address of this interface.
void Start(Time tStart)
Set a start time for the device.
#define NS_LOG_DEBUG(msg)
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
NetDevice::PromiscReceiveCallback m_promiscRxCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode...
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
EncapsulationMode
Enumeration of the types of frames supported in the class.
void SetBufferSize(uint32_t bufferSize)
Set size of the read buffer.
EventImpl * MakeEvent(void(*f)(void))
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
a NetDevice to read/write network traffic from/into a file descriptor.
PacketType
Packet types are used as they are in Linux.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
static void AddPIHeader(uint8_t *&buf, ssize_t &len)
virtual uint16_t GetMtu(void) const
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
a unique identifier for an interface.
FdNetDevice()
Constructor for the FdNetDevice.
virtual ~FdNetDevice()
Destructor for the FdNetDevice.
TypeId SetParent(TypeId tid)
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
void AddHeader(const Header &header)
Add header to this packet.
SystemMutex m_pendingReadMutex