13#include "ns3/boolean.h"
15#include "ns3/error-model.h"
16#include "ns3/ethernet-header.h"
17#include "ns3/ethernet-trailer.h"
18#include "ns3/llc-snap-header.h"
20#include "ns3/pointer.h"
22#include "ns3/simulator.h"
23#include "ns3/trace-source-accessor.h"
24#include "ns3/uinteger.h"
37 TypeId(
"ns3::CsmaNetDevice")
41 .AddAttribute(
"Address",
42 "The MAC address of this device.",
47 "The MAC-level Maximum Transmission Unit",
51 .AddAttribute(
"EncapsulationMode",
52 "The link-layer encapsulation type to use.",
56 .AddAttribute(
"SendEnable",
57 "Enable or disable the transmitter section of the device.",
61 .AddAttribute(
"ReceiveEnable",
62 "Enable or disable the receiver section of the device.",
66 .AddAttribute(
"ReceiveErrorModel",
67 "The receiver error model used to simulate packet loss",
76 .AddAttribute(
"TxQueue",
77 "A queue to use as the transmit queue in the device.",
86 .AddTraceSource(
"MacTx",
87 "Trace source indicating a packet has "
88 "arrived for transmission by this device",
90 "ns3::Packet::TracedCallback")
91 .AddTraceSource(
"MacTxDrop",
92 "Trace source indicating a packet has been "
93 "dropped by the device before transmission",
95 "ns3::Packet::TracedCallback")
96 .AddTraceSource(
"MacPromiscRx",
97 "A packet has been received by this device, "
98 "has been passed up from the physical layer "
99 "and is being forwarded up the local protocol stack. "
100 "This is a promiscuous trace,",
102 "ns3::Packet::TracedCallback")
103 .AddTraceSource(
"MacRx",
104 "A packet has been received by this device, "
105 "has been passed up from the physical layer "
106 "and is being forwarded up the local protocol stack. "
107 "This is a non-promiscuous trace,",
109 "ns3::Packet::TracedCallback")
112 .AddTraceSource (
"MacRxDrop",
113 "Trace source indicating a packet was received, "
114 "but dropped before being forwarded up the stack",
116 "ns3::Packet::TracedCallback")
118 .AddTraceSource(
"MacTxBackoff",
119 "Trace source indicating a packet has been "
120 "delayed by the CSMA backoff process",
122 "ns3::Packet::TracedCallback")
127 .AddTraceSource(
"PhyTxBegin",
128 "Trace source indicating a packet has "
129 "begun transmitting over the channel",
131 "ns3::Packet::TracedCallback")
132 .AddTraceSource(
"PhyTxEnd",
133 "Trace source indicating a packet has been "
134 "completely transmitted over the channel",
136 "ns3::Packet::TracedCallback")
137 .AddTraceSource(
"PhyTxDrop",
138 "Trace source indicating a packet has been "
139 "dropped by the device during transmission",
141 "ns3::Packet::TracedCallback")
144 .AddTraceSource (
"PhyRxBegin",
145 "Trace source indicating a packet has "
146 "begun being received by the device",
148 "ns3::Packet::TracedCallback")
150 .AddTraceSource(
"PhyRxEnd",
151 "Trace source indicating a packet has been "
152 "completely received by the device",
154 "ns3::Packet::TracedCallback")
155 .AddTraceSource(
"PhyRxDrop",
156 "Trace source indicating a packet has been "
157 "dropped by the device during reception",
159 "ns3::Packet::TracedCallback")
163 .AddTraceSource(
"Sniffer",
164 "Trace source simulating a non-promiscuous "
165 "packet sniffer attached to the device",
167 "ns3::Packet::TracedCallback")
168 .AddTraceSource(
"PromiscSniffer",
169 "Trace source simulating a promiscuous "
170 "packet sniffer attached to the device",
172 "ns3::Packet::TracedCallback");
293 NS_LOG_FUNCTION(slotTime << minSlots << maxSlots << ceiling << maxRetries);
305 uint16_t protocolNumber)
319 uint16_t lengthType = 0;
323 NS_LOG_LOGIC(
"Encapsulating packet as DIX (type interpretation)");
328 lengthType = protocolNumber;
336 if (p->GetSize() < 46)
339 memset(buffer, 0, 46);
345 NS_LOG_LOGIC(
"Encapsulating packet as LLC (length interpretation)");
356 lengthType = p->GetSize();
364 if (p->GetSize() < 46)
367 memset(buffer, 0, 46);
373 "CsmaNetDevice::AddHeader(): 802.3 Length/Type field with LLC/SNAP: "
374 "length interpretation must not exceed device frame size minus overhead");
379 NS_FATAL_ERROR(
"CsmaNetDevice::AddHeader(): Unknown packet encapsulation mode");
383 NS_LOG_LOGIC(
"header.SetLengthType (" << lengthType <<
")");
385 p->AddHeader(header);
392 p->AddTrailer(trailer);
397CsmaNetDevice::ProcessHeader (
Ptr<Packet> p, uint16_t & param)
402 p->RemoveTrailer (trailer);
405 p->RemoveHeader (header);
416 param = header.GetLengthType ();
421 p->RemoveHeader (llc);
422 param = llc.GetType ();
427 NS_FATAL_ERROR (
"CsmaNetDevice::ProcessHeader(): Unknown packet encapsulation mode");
506 NS_LOG_WARN(
"Channel TransmitStart returns an error");
566 "CsmaNetDevice::TransmitAbort(): IsEmpty false but no Packet on queue?");
586 "CsmaNetDevice::transmitCompleteEvent(): Must be BUSY if transmitting");
618 "CsmaNetDevice::TransmitReadyEvent(): Must be in interframe gap");
638 "CsmaNetDevice::TransmitReadyEvent(): IsEmpty false but no Packet on queue?");
696 if (senderDevice ==
this)
731 pktCopy->RemoveTrailer(trailer);
737 bool crcGood = trailer.
CheckFcs(pktCopy);
746 pktCopy->RemoveHeader(header);
765 pktCopy->RemoveAtEnd(padlen);
769 pktCopy->RemoveHeader(llc);
956 uint16_t protocolNumber)
975 AddHeader(packet, source, destination, protocolNumber);
1000 "CsmaNetDevice::SendFrom(): IsEmpty false but no Packet on queue?");
a polymophic address class
void ResetBackoffTime()
Indicates to the backoff object that the last packet was successfully transmitted and that the number...
uint32_t m_maxRetries
Maximum number of transmission retries before the packet is dropped.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
uint32_t m_maxSlots
Maximum number of backoff slots (when multiplied by m_slotTime, determines maximum backoff time)
bool MaxRetriesReached() const
void IncrNumRetries()
Increments the number of retries by 1.
uint32_t m_minSlots
Minimum number of backoff slots (when multiplied by m_slotTime, determines minimum backoff time)
uint32_t m_ceiling
Caps the exponential function when the number of retries reaches m_ceiling.
Time m_slotTime
Length of one slot.
AttributeValue implementation for Boolean.
bool IsNull() const
Check for null implementation.
A Device for a Csma Network Link.
void SetInterframeGap(Time t)
Set the interframe gap used to separate packets.
Ptr< Queue< Packet > > GetQueue() const
Get a copy of the attached Queue.
TracedCallback< Ptr< const Packet > > m_macRxDropTrace
The trace source fired for packets successfully received by the device but dropped before being forwa...
Ptr< Node > GetNode() const override
Get the node to which this device is attached.
EncapsulationMode
Enumeration of the types of packets supported in the class.
@ ILLEGAL
Encapsulation mode not set.
@ DIX
DIX II / Ethernet II packet.
@ LLC
802.2 LLC/SNAP Packet
NetDevice::ReceiveCallback m_rxCallback
The callback used to notify higher layers that a packet has been received.
TracedCallback< Ptr< const Packet > > m_macTxBackoffTrace
The trace source fired when the mac layer is forced to begin the backoff process for a packet.
void SetReceiveEnable(bool enable)
Enable or disable the receive side of the network device.
Backoff m_backoff
Holds the backoff parameters and is used to calculate the next backoff time to use when the channel i...
DataRate m_bps
The data rate that the Net Device uses to simulate packet transmission timing.
void DoDispose() override
Perform any object release functionality required to break reference cycles in reference counted obje...
~CsmaNetDevice() override
Destroy a CsmaNetDevice.
bool SetMtu(const uint16_t mtu) override
void TransmitAbort()
Aborts the transmission of the current packet.
EncapsulationMode m_encapMode
The type of packet that should be created by the AddHeader function and that should be processed by t...
bool NeedsArp() const override
Does this device need to use the address resolution protocol?
TracedCallback< Ptr< const Packet > > m_macRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
void TransmitStart()
Start Sending a Packet Down the Wire.
void SetIfIndex(const uint32_t index) override
@ BACKOFF
The transmitter is waiting for the channel to be free.
@ READY
The transmitter is ready to begin transmission of a packet.
@ BUSY
The transmitter is busy transmitting a packet.
@ GAP
The transmitter is in the interframe gap time.
Ptr< Queue< Packet > > m_queue
The Queue which this CsmaNetDevice uses as a packet source.
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
The trace source fired for packets successfully received by the device immediately before being forwa...
uint32_t m_mtu
The Maximum Transmission Unit.
bool SupportsSendFrom() const override
NetDevice::PromiscReceiveCallback m_promiscRxCallback
The callback used to notify higher layers that a packet has been received in promiscuous mode.
CsmaNetDevice()
Construct a CsmaNetDevice.
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
Address GetBroadcast() const override
static TypeId GetTypeId()
Get the type ID.
bool IsPointToPoint() const override
Is this a point to point link?
Ptr< CsmaChannel > m_channel
The CsmaChannel to which this CsmaNetDevice has been attached.
bool Attach(Ptr< CsmaChannel > ch)
Attach the device to a channel.
bool IsBridge() const override
Is this a bridge?
void TransmitCompleteEvent()
Stop Sending a Packet Down the Wire and Begin the Interframe Gap.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
Start sending a packet down the channel, with MAC spoofing.
void Receive(Ptr< const Packet > p, Ptr< CsmaNetDevice > sender)
Receive a packet from a connected CsmaChannel.
bool IsMulticast() const override
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
TracedCallback< Ptr< const Packet > > m_phyTxBeginTrace
The trace source fired when a packet begins the transmission process on the medium.
CsmaNetDevice::EncapsulationMode GetEncapsulationMode()
Get the encapsulation mode of this device.
void AddHeader(Ptr< Packet > p, Mac48Address source, Mac48Address dest, uint16_t protocolNumber)
Adds the necessary headers and trailers to a packet of data in order to respect the packet type.
uint16_t GetMtu() const override
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Ptr< Channel > GetChannel() const override
void NotifyLinkUp()
Notify any interested parties that the link has come up.
void SetBackoffParams(Time slotTime, uint32_t minSlots, uint32_t maxSlots, uint32_t maxRetries, uint32_t ceiling)
Set the backoff parameters used to determine the wait to retry transmitting a packet when the channel...
Ptr< Packet > m_currentPkt
Next packet that will be transmitted (if transmitter is not currently transmitting) or packet that is...
TracedCallback< Ptr< const Packet > > m_snifferTrace
A trace source that emulates a non-promiscuous protocol sniffer connected to the device.
TracedCallback< Ptr< const Packet > > m_phyTxEndTrace
The trace source fired when a packet ends the transmission process on the medium.
uint32_t m_deviceId
Device ID returned by the attached functions.
bool m_receiveEnable
Enable net device to receive packets.
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.
Ptr< Node > m_node
The Node to which this device is attached.
void SetReceiveErrorModel(Ptr< ErrorModel > em)
Attach a receive ErrorModel to the CsmaNetDevice.
void SetSendEnable(bool enable)
Enable or disable the send side of the network device.
bool IsLinkUp() const override
void TransmitReadyEvent()
Cause the Transmitter to Become Ready to Send Another Packet.
TracedCallback< Ptr< const Packet > > m_promiscSnifferTrace
A trace source that emulates a promiscuous mode protocol sniffer connected to the device.
uint32_t GetIfIndex() const override
bool IsBroadcast() const override
static const uint16_t DEFAULT_MTU
Default Maximum Transmission Unit (MTU) for the CsmaNetDevice.
void SetEncapsulationMode(CsmaNetDevice::EncapsulationMode mode)
Set the encapsulation mode of this device.
TracedCallback< Ptr< const Packet > > m_phyRxBeginTrace
The trace source fired when a packet begins the reception process from the medium.
bool m_sendEnable
Enable net device to send packets.
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...
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Start sending a packet down the channel.
bool IsSendEnabled() const
Is the send side of the network device enabled?
TracedCallback< Ptr< const Packet > > m_macTxTrace
The trace source fired when packets come into the "top" of the device at the L3/L2 transition,...
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
Set the callback to be used to notify higher layers when a packet has been received.
TracedCallback< Ptr< const Packet > > m_phyTxDropTrace
The trace source fired when the phy layer drops a packet as it tries to transmit it.
Address GetAddress() const override
void AddLinkChangeCallback(Callback< void > callback) override
void SetNode(Ptr< Node > node) override
Set the node to which this device is being attached.
void SetAddress(Address address) override
Set the address of this interface.
Mac48Address m_address
The MAC address which has been assigned to this device.
Time m_tInterframeGap
The interframe gap that the Net Device uses insert time between packet transmission.
void SetQueue(Ptr< Queue< Packet > > queue)
Attach a queue to the CsmaNetDevice.
Ptr< ErrorModel > m_receiveErrorModel
Error model for receive packet events.
TxMachineState m_txMachineState
The state of the Net Device transmit state machine.
uint32_t m_ifIndex
The interface index (really net evice index) that has been assigned to this network device.
bool IsReceiveEnabled() const
Is the receive side of the network device enabled?
bool m_linkUp
Flag indicating whether or not the link is up.
TracedCallback< Ptr< const Packet > > m_phyRxEndTrace
The trace source fired when a packet ends the reception process from the medium.
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Hold variables of type enum.
Packet trailer for Ethernet.
bool CheckFcs(Ptr< const Packet > p) const
Calculate an FCS on the provided packet and check this value against the FCS found when the trailer w...
void EnableFcs(bool enable)
Enable or disable FCS checking and calculations.
void CalcFcs(Ptr< const Packet > p)
Updates the Fcs Field to the correct FCS.
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()
AttributeValue implementation for Mac48Address.
Network layer to device interface.
PacketType
Packet types are used as they are in Linux.
@ PACKET_HOST
Packet addressed to us.
@ PACKET_OTHERHOST
Packet addressed to someone else.
@ PACKET_BROADCAST
Packet addressed to all.
@ PACKET_MULTICAST
Packet addressed to multicast group.
static bool ChecksumEnabled()
virtual void DoDispose()
Destructor implementation.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
uint64_t GetUid() const
Returns the packet's Uid.
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
Template class for packet Queues.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
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(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Ptr< const AttributeChecker > MakeBooleanChecker()
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakeMac48AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeChecker > MakeMac48AddressChecker()
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Ptr< const AttributeChecker > MakeUintegerChecker()
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#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_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#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 > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
@ TRANSMITTING
Channel is BUSY, a packet is being written by a net device.
@ IDLE
Channel is IDLE, no packet is being transmitted.