22 #include "fd-net-device.h"
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)
55 m_bufferSize = bufferSize;
62 uint8_t *buf = (uint8_t *)malloc (m_bufferSize);
66 ssize_t len = read (
m_fd, buf, m_bufferSize);
80 FdNetDevice::GetTypeId (
void)
84 .AddConstructor<FdNetDevice> ()
85 .AddAttribute (
"Address",
86 "The MAC address of this device.",
89 MakeMac48AddressChecker ())
90 .AddAttribute (
"Start",
91 "The simulation time at which to spin up the device thread.",
95 .AddAttribute (
"Stop",
96 "The simulation time at which to tear down the device thread.",
100 .AddAttribute (
"EncapsulationMode",
101 "The link-layer encapsulation type to use.",
104 MakeEnumChecker (
DIX,
"Dix",
107 .AddAttribute (
"RxQueueSize",
"Maximum size of the read queue. "
108 "This value limits number of packets that have been read "
109 "from the network into a memory buffer but have not yet "
110 "been processed by the simulator.",
113 MakeUintegerChecker<uint32_t> ())
122 .AddTraceSource (
"MacTx",
123 "Trace source indicating a packet has arrived for transmission by this device",
125 .AddTraceSource (
"MacTxDrop",
126 "Trace source indicating a packet has been dropped by the device before transmission",
128 .AddTraceSource (
"MacPromiscRx",
129 "A packet has been received by this device, has been passed up from the physical layer "
130 "and is being forwarded up the local protocol stack. This is a promiscuous trace,",
132 .AddTraceSource (
"MacRx",
133 "A packet has been received by this device, has been passed up from the physical layer "
134 "and is being forwarded up the local protocol stack. This is a non-promiscuous trace,",
140 .AddTraceSource (
"Sniffer",
141 "Trace source simulating a non-promiscuous packet sniffer attached to the device",
143 .AddTraceSource (
"PromiscSniffer",
144 "Trace source simulating a promiscuous packet sniffer attached to the device",
156 m_isBroadcast (true),
157 m_isMulticast (false),
158 m_pendingReadCount (0),
221 NS_LOG_DEBUG (
"FdNetDevice::Start(): Failure, invalid file descriptor.");
278 struct timespec time = { 0, 100000000L };
279 nanosleep (&time, NULL);
291 AddPIHeader (uint8_t *&buf, ssize_t &len)
294 uint8_t *buf2 = (uint8_t*)malloc (len + 4);
295 memcpy (buf2 + 4, buf, len);
302 uint16_t proto = 0x0008;
305 if (buf[12] == 0x81 && buf[13] == 0x00 && len > 18)
308 proto = buf[16] | (buf[17] << 8);
313 proto = buf[12] | (buf[13] << 8);
316 buf2[0] = (uint8_t)flags;
317 buf2[1] = (uint8_t)(flags >> 8);
318 buf2[2] = (uint8_t)proto;
319 buf2[3] = (uint8_t)(proto >> 8);
327 RemovePIHeader (uint8_t *&buf, ssize_t &len)
333 memmove (buf, buf + 4, len);
334 buf = (uint8_t*)realloc (buf, len);
354 RemovePIHeader (buf, len);
360 Ptr<Packet> packet = Create<Packet> (
reinterpret_cast<const uint8_t *
> (buf), len);
373 bool isBroadcast =
false;
374 bool isMulticast =
false;
417 protocol = llc.GetType ();
427 packetType = NS3_PACKET_BROADCAST;
429 else if (isMulticast)
431 packetType = NS3_PACKET_MULTICAST;
435 packetType = NS3_PACKET_HOST;
439 packetType = NS3_PACKET_OTHERHOST;
461 if (packetType != NS3_PACKET_OTHERHOST)
503 llc.SetType (protocolNumber);
529 ssize_t len = (ssize_t) packet->
GetSize ();
530 uint8_t *buffer = (uint8_t*)malloc (len);
536 AddPIHeader (buffer, len);
539 ssize_t written = write (
m_fd, buffer, len);
542 if (written == -1 || written != len)
554 if (
m_fd == -1 and fd > 0)
573 FdNetDevice::NotifyLinkUp (
void)
635 FdNetDevice::SetIsBroadcast (
bool broadcast)
653 FdNetDevice::SetIsMulticast (
bool multicast)
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
uint32_t RemoveHeader(Header &header)
A structure representing data read.
#define NS_LOG_FUNCTION(parameters)
void ForwardUp(uint8_t *buf, ssize_t len)
void SetFileDescriptor(int fd)
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual bool SupportsSendFrom() const
EncapsulationMode m_encapMode
virtual bool NeedsArp(void) const
TracedCallback< Ptr< const Packet > > m_macRxTrace
uint64_t GetUid(void) const
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
#define NS_LOG_COMPONENT_DEFINE(name)
uint32_t GetSize(void) const
bool IsBroadcast(void) const
NetDevice::ReceiveCallback m_rxCallback
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
virtual void DoDispose(void)
static void Cancel(const EventId &id)
virtual bool IsMulticast(void) const
Ptr< FdNetDeviceFdReader > m_fdReader
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
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
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.
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
void ReceiveCallback(uint8_t *buf, ssize_t len)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
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)
#define NS_LOG_LOGIC(msg)
virtual bool IsLinkUp(void) const
static Mac48Address ConvertFrom(const Address &address)
void SetEncapsulationMode(FdNetDevice::EncapsulationMode mode)
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
Ptr< Packet > Copy(void) const
TracedCallback m_linkChangeCallbacks
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
virtual bool IsBroadcast(void) const
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual void SetNode(Ptr< Node > node)
TracedCallback< Ptr< const Packet > > m_snifferTrace
FdReader::Data DoRead(void)
The read implementation.
FdNetDevice::EncapsulationMode GetEncapsulationMode(void) const
#define NS_ASSERT_MSG(condition, message)
TracedCallback< Ptr< const Packet > > m_phyRxDropTrace
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.
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
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)
#define NS_LOG_DEBUG(msg)
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
NetDevice::PromiscReceiveCallback m_promiscRxCallback
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
void SetBufferSize(uint32_t bufferSize)
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
a NetDevice to read/write network traffic from/into a file descriptor.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
virtual uint16_t GetMtu(void) const
virtual void DoDispose(void)
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
void AddHeader(const Header &header)
SystemMutex m_pendingReadMutex