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 .SetGroupName (
"FdNetDevice")
87 .AddAttribute (
"Address",
88 "The MAC address of this device.",
92 .AddAttribute (
"Start",
93 "The simulation time at which to spin up " 98 .AddAttribute (
"Stop",
99 "The simulation time at which to tear down " 100 "the device thread.",
104 .AddAttribute (
"EncapsulationMode",
105 "The link-layer encapsulation type to use.",
111 .AddAttribute (
"RxQueueSize",
"Maximum size of the read queue. " 112 "This value limits number of packets that have been read " 113 "from the network into a memory buffer but have not yet " 114 "been processed by the simulator.",
117 MakeUintegerChecker<uint32_t> ())
126 .AddTraceSource (
"MacTx",
127 "Trace source indicating a packet has " 128 "arrived for transmission by this device",
130 "ns3::Packet::TracedCallback")
131 .AddTraceSource (
"MacTxDrop",
132 "Trace source indicating a packet has " 133 "been dropped by the device before transmission",
135 "ns3::Packet::TracedCallback")
136 .AddTraceSource (
"MacPromiscRx",
137 "A packet has been received by this device, " 138 "has been passed up from the physical layer " 139 "and is being forwarded up the local protocol stack. " 140 "This is a promiscuous trace,",
142 "ns3::Packet::TracedCallback")
143 .AddTraceSource (
"MacRx",
144 "A packet has been received by this device, " 145 "has been passed up from the physical layer " 146 "and is being forwarded up the local protocol stack. " 147 "This is a non-promiscuous trace,",
149 "ns3::Packet::TracedCallback")
154 .AddTraceSource (
"Sniffer",
155 "Trace source simulating a non-promiscuous " 156 "packet sniffer attached to the device",
158 "ns3::Packet::TracedCallback")
159 .AddTraceSource (
"PromiscSniffer",
160 "Trace source simulating a promiscuous " 161 "packet sniffer attached to the device",
163 "ns3::Packet::TracedCallback")
175 m_isBroadcast (true),
176 m_isMulticast (false),
251 NS_LOG_DEBUG (
"FdNetDevice::Start(): Failure, invalid file descriptor.");
309 struct timespec time = {
312 nanosleep (&time, NULL);
334 uint8_t *buf2 = (uint8_t*)malloc (len + 4);
335 memcpy (buf2 + 4, buf, len);
342 uint16_t proto = 0x0008;
345 if (buf[12] == 0x81 && buf[13] == 0x00 && len > 18)
348 proto = buf[16] | (buf[17] << 8);
353 proto = buf[12] | (buf[13] << 8);
356 buf2[0] = (uint8_t)flags;
357 buf2[1] = (uint8_t)(flags >> 8);
358 buf2[2] = (uint8_t)proto;
359 buf2[3] = (uint8_t)(proto >> 8);
379 memmove (buf, buf + 4, len);
380 buf = (uint8_t*)realloc (buf, len);
411 Ptr<Packet> packet = Create<Packet> (
reinterpret_cast<const uint8_t *
> (buf), len);
424 bool isBroadcast =
false;
425 bool isMulticast =
false;
480 else if (isMulticast)
580 size_t len = (size_t) packet->
GetSize ();
581 uint8_t *buffer = (uint8_t*)malloc (len);
590 ssize_t written = write (
m_fd, buffer, len);
593 if (written == -1 || (
size_t) written != len)
605 if (
m_fd == -1 and fd > 0)
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
virtual uint32_t GetIfIndex(void) const
virtual Ptr< Node > GetNode(void) const
bool IsBroadcast(void) const
uint64_t GetUid(void) const
Returns the packet's Uid.
Simulation virtual time values and global simulation resolution.
void StopDevice(void)
Tear down the device.
A structure representing data read.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
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 bool IsLinkUp(void) const
uint16_t m_mtu
The MTU associated to the file descriptor technology.
uint32_t GetId(void) const
DIX II / Ethernet II packet.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
void SetFileDescriptor(int fd)
Set the associated file descriptor.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
FdNetDevice::EncapsulationMode GetEncapsulationMode(void) const
Get the link layer encapsulation mode of this device.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
virtual void AddLinkChangeCallback(Callback< void > callback)
EncapsulationMode m_encapMode
The type of encapsulation of the received/transmitted frames.
When using TAP devices, if flag IFF_NO_PI is not set on the device, IP packets will have an extra hea...
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
TracedCallback< Ptr< const Packet > > m_macRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
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_macTxDropTrace
The trace source fired when packets coming into the "top" of the device at the L3/L2 transition are d...
Ptr< const AttributeChecker > MakeMac48AddressChecker(void)
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...
Time m_tStart
Time to start spinning up the device.
virtual void DoDispose(void)
Destructor implementation.
virtual bool SupportsSendFrom() const
Ptr< FdNetDeviceFdReader > m_fdReader
Reader for the file descriptor.
void Stop(Time tStop)
Set a stop time for the device.
virtual void SetIsBroadcast(bool broadcast)
Set if the NetDevice is able to send Broadcast messages.
Ptr< const AttributeAccessor > MakeMac48AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
virtual void SetIfIndex(const uint32_t index)
a polymophic address class
void NotifyLinkUp(void)
Notify that the link is up and ready.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
static void AddPIHeader(uint8_t *&buf, size_t &len)
Synthesize PI header for the kernel.
virtual Ptr< Channel > GetChannel(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.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
static Mac48Address GetMulticast(Ipv4Address address)
AttributeValue implementation for Time.
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual bool IsBroadcast(void) const
int m_fd
The file descriptor to read from.
virtual void SetIsMulticast(bool multicast)
Set if the NetDevice is able to send Multicast messages.
A class which provides a simple way to implement a Critical Section.
virtual Address GetAddress(void) const
Hold an unsigned integer type.
void ForwardUp(void)
Forward the frame to the appropriate callback for processing.
uint32_t m_maxPendingReads
Maximum number of packets that can be received and scheduled for read but not yet read...
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void StartDevice(void)
Spin up the device.
void ReceiveCallback(uint8_t *buf, ssize_t len)
Callback to invoke when a new frame is received.
Ptr< Node > m_node
The ns-3 node associated to the net device.
static Mac48Address ConvertFrom(const Address &address)
void SetEncapsulationMode(FdNetDevice::EncapsulationMode mode)
Set the link layer encapsulation mode of this device.
EventId m_stopEvent
NetDevice stop event.
virtual void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb)
int m_fd
The file descriptor used for receive/send network traffic.
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
A trace source that emulates a promiscuous mode protocol sniffer connected to the device...
virtual uint16_t GetMtu(void) const
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
virtual void SetNode(Ptr< Node > node)
virtual Address GetBroadcast(void) const
TracedCallback< Ptr< const Packet > > m_snifferTrace
A trace source that emulates a non-promiscuous protocol sniffer connected to the device.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
virtual bool NeedsArp(void) const
FdReader::Data DoRead(void)
The read implementation.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
TracedCallback< Ptr< const Packet > > m_phyRxDropTrace
The trace source fired when the phy layer drops a packet it has received.
virtual bool SetMtu(const uint16_t mtu)
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)
Make an EnumChecker pre-configured with a set of allowed values by name.
Describes an IPv6 address.
uint32_t m_ifIndex
The ns-3 interface index (in the sense of net device index) that has been assigned to this network de...
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Ipv4 addresses are stored in host order in this class.
static TypeId GetTypeId(void)
Get the type ID.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Network layer to device interface.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
AttributeValue implementation for Mac48Address.
virtual void SetAddress(Address address)
Set the address of this interface.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
void Start(Time tStart)
Set a start time for the device.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Time Seconds(double value)
Construct a Time in the indicated unit.
EventId m_startEvent
NetDevice start event.
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.
uint32_t m_bufferSize
size of the read buffer
void SetBufferSize(uint32_t bufferSize)
Set size of the read buffer.
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
a NetDevice to read/write network traffic from/into a file descriptor.
Mac48Address m_address
The net device mac address.
PacketType
Packet types are used as they are in Linux.
bool m_isBroadcast
Flag indicating whether or not the underlying net device supports broadcast.
bool IsNull(void) const
Check for null implementation.
virtual void DoDispose(void)
Destructor implementation.
bool m_linkUp
Flag indicating whether or not the link is up.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
a unique identifier for an interface.
FdNetDevice()
Constructor for the FdNetDevice.
virtual ~FdNetDevice()
Destructor for the FdNetDevice.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
bool m_isMulticast
Flag indicating whether or not the underlying net device supports multicast.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Time m_tStop
Time to start tearing down the device.
void AddHeader(const Header &header)
Add header to this packet.
virtual bool IsMulticast(void) const
SystemMutex m_pendingReadMutex
Mutex to increase pending read counter.
EventImpl * MakeEvent(void(*f)(void))
Make an EventImpl from a function pointer taking varying numbers of arguments.
static void RemovePIHeader(uint8_t *&buf, ssize_t &len)
Removes PI header.