13#include "ns3/boolean.h"
14#include "ns3/channel.h"
16#include "ns3/ethernet-header.h"
17#include "ns3/ethernet-trailer.h"
18#include "ns3/llc-snap-header.h"
20#include "ns3/mac48-address.h"
21#include "ns3/pointer.h"
22#include "ns3/simulator.h"
23#include "ns3/string.h"
24#include "ns3/trace-source-accessor.h"
25#include "ns3/uinteger.h"
28#include <net/ethernet.h>
76 .SetGroupName(
"FdNetDevice")
78 .AddAttribute(
"Address",
79 "The MAC address of this device.",
83 .AddAttribute(
"Start",
84 "The simulation time at which to spin up "
90 "The simulation time at which to tear down "
95 .AddAttribute(
"EncapsulationMode",
96 "The link-layer encapsulation type to use.",
100 .AddAttribute(
"RxQueueSize",
101 "Maximum size of the read queue. "
102 "This value limits number of packets that have been read "
103 "from the network into a memory buffer but have not yet "
104 "been processed by the simulator.",
116 .AddTraceSource(
"MacTx",
117 "Trace source indicating a packet has "
118 "arrived for transmission by this device",
120 "ns3::Packet::TracedCallback")
121 .AddTraceSource(
"MacTxDrop",
122 "Trace source indicating a packet has "
123 "been dropped by the device before transmission",
125 "ns3::Packet::TracedCallback")
126 .AddTraceSource(
"MacPromiscRx",
127 "A packet has been received by this device, "
128 "has been passed up from the physical layer "
129 "and is being forwarded up the local protocol stack. "
130 "This is a promiscuous trace,",
132 "ns3::Packet::TracedCallback")
133 .AddTraceSource(
"MacRx",
134 "A packet has been received by this device, "
135 "has been passed up from the physical layer "
136 "and is being forwarded up the local protocol stack. "
137 "This is a non-promiscuous trace,",
139 "ns3::Packet::TracedCallback")
144 .AddTraceSource(
"Sniffer",
145 "Trace source simulating a non-promiscuous "
146 "packet sniffer attached to the device",
148 "ns3::Packet::TracedCallback")
149 .AddTraceSource(
"PromiscSniffer",
150 "Trace source simulating a promiscuous "
151 "packet sniffer attached to the device",
153 "ns3::Packet::TracedCallback");
165 m_isMulticast(false),
236 NS_LOG_DEBUG(
"FdNetDevice::Start(): Failure, invalid file descriptor.");
255 fdReader->SetBufferSize(
m_mtu + 22);
319 struct timespec time = {0, 100000000L};
320 nanosleep(&time,
nullptr);
342 auto buf2 = (uint8_t*)malloc(len + 4);
343 memcpy(buf2 + 4, buf, len);
350 uint16_t proto = 0x0008;
353 if (buf[12] == 0x81 && buf[13] == 0x00 && len > 18)
356 proto = buf[16] | (buf[17] << 8);
361 proto = buf[12] | (buf[13] << 8);
364 buf2[0] = (uint8_t)flags;
365 buf2[1] = (uint8_t)(flags >> 8);
366 buf2[2] = (uint8_t)proto;
367 buf2[3] = (uint8_t)(proto >> 8);
387 memmove(buf, buf + 4, len);
388 buf = (uint8_t*)realloc(buf, len);
395 return (uint8_t*)malloc(len);
409 uint8_t* buf =
nullptr;
414 NS_LOG_LOGIC(
"buffer is empty, probably the device is stopped.");
427 NS_LOG_LOGIC(
"buffer: " <<
static_cast<void*
>(buf) <<
" length: " << len);
451 bool isBroadcast =
false;
452 bool isMulticast =
false;
468 packet->RemoveHeader(header);
494 packet->RemoveHeader(llc);
507 else if (isMulticast)
557 uint16_t protocolNumber)
560 NS_LOG_LOGIC(
"packet: " << packet <<
" UID: " << packet->GetUid());
571 NS_LOG_LOGIC(
"Transmit packet with UID " << packet->GetUid());
580 "FdNetDevice::SendFrom(): Packet too big " << packet->GetSize());
586 packet->AddHeader(llc);
595 packet->AddHeader(header);
609 auto len = (size_t)packet->GetSize();
617 packet->CopyData(buffer, len);
625 ssize_t written =
Write(buffer, len);
628 if (written == -1 || (
size_t)written != len)
649 if (
m_fd == -1 and fd > 0)
a polymophic address class
bool IsNull() const
Check for null implementation.
Hold variables of type enum.
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
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.
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.
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.
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
int m_fd
The file descriptor to read from.
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 address.
static Mac48Address GetMulticast(Ipv4Address address)
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address GetBroadcast()
Network layer to device interface.
PacketType
Packet types are used as they are in Linux.
Callback< bool, Ptr< NetDevice >, Ptr< const Packet >, uint16_t, const Address & > ReceiveCallback
virtual void DoInitialize()
Initialize() implementation.
virtual void DoDispose()
Destructor implementation.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
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...
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Simulation virtual time values and global simulation resolution.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
std::enable_if_t< std::is_member_pointer_v< MEM >, EventImpl * > MakeEvent(MEM mem_ptr, OBJ obj, Ts... args)
Make an EventImpl from class method members which take varying numbers of arguments.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
static void RemovePIHeader(uint8_t *&buf, ssize_t &len)
Removes PI header.
static void AddPIHeader(uint8_t *&buf, size_t &len)
Synthesize PI header for the kernel.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Time Seconds(double value)
Construct a Time in the indicated unit.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeMac48AddressAccessor(T1 a1)
Ptr< const AttributeChecker > MakeMac48AddressChecker()
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
A structure representing data read.