A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
aloha-noack-net-device.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 CTTC
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Nicola Baldo <nbaldo@cttc.es>
7 */
8
10
12
13#include "ns3/boolean.h"
14#include "ns3/channel.h"
15#include "ns3/enum.h"
16#include "ns3/llc-snap-header.h"
17#include "ns3/log.h"
18#include "ns3/pointer.h"
19#include "ns3/queue.h"
20#include "ns3/simulator.h"
21#include "ns3/trace-source-accessor.h"
22#include "ns3/uinteger.h"
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("AlohaNoackNetDevice");
28
29/**
30 * \brief Output stream operator
31 * \param os output stream
32 * \param state the state to print
33 * \return an output stream
34 */
35std::ostream&
36operator<<(std::ostream& os, AlohaNoackNetDevice::State state)
37{
38 switch (state)
39 {
41 os << "IDLE";
42 break;
44 os << "TX";
45 break;
47 os << "RX";
48 break;
49 }
50 return os;
51}
52
53NS_OBJECT_ENSURE_REGISTERED(AlohaNoackNetDevice);
54
55TypeId
57{
58 static TypeId tid =
59 TypeId("ns3::AlohaNoackNetDevice")
61 .SetGroupName("Spectrum")
62 .AddConstructor<AlohaNoackNetDevice>()
63 .AddAttribute("Address",
64 "The MAC address of this device.",
65 Mac48AddressValue(Mac48Address("12:34:56:78:90:12")),
68 .AddAttribute("Queue",
69 "packets being transmitted get queued here",
73 .AddAttribute(
74 "Mtu",
75 "The Maximum Transmission Unit",
76 UintegerValue(1500),
79 .AddAttribute(
80 "Phy",
81 "The PHY layer attached to this device.",
85 .AddTraceSource("MacTx",
86 "Trace source indicating a packet has arrived "
87 "for transmission by this device",
89 "ns3::Packet::TracedCallback")
90 .AddTraceSource("MacTxDrop",
91 "Trace source indicating a packet has been dropped "
92 "by the device before transmission",
94 "ns3::Packet::TracedCallback")
95 .AddTraceSource("MacPromiscRx",
96 "A packet has been received by this device, has been "
97 "passed up from the physical layer "
98 "and is being forwarded up the local protocol stack. "
99 "This is a promiscuous trace,",
101 "ns3::Packet::TracedCallback")
102 .AddTraceSource("MacRx",
103 "A packet has been received by this device, "
104 "has been passed up from the physical layer "
105 "and is being forwarded up the local protocol stack. "
106 "This is a non-promiscuous trace,",
108 "ns3::Packet::TracedCallback");
109 return tid;
110}
111
117
123
124void
126{
127 NS_LOG_FUNCTION(this);
128 m_queue = nullptr;
129 m_node = nullptr;
130 m_channel = nullptr;
131 m_currentPkt = nullptr;
132 m_phy = nullptr;
135}
136
137void
139{
140 NS_LOG_FUNCTION(index);
141 m_ifIndex = index;
142}
143
146{
147 NS_LOG_FUNCTION(this);
148 return m_ifIndex;
149}
150
151bool
153{
154 NS_LOG_FUNCTION(mtu);
155 m_mtu = mtu;
156 return true;
157}
158
159uint16_t
161{
162 NS_LOG_FUNCTION(this);
163 return m_mtu;
164}
165
166void
172
173void
179
182{
183 NS_LOG_FUNCTION(this);
184 return m_address;
185}
186
187bool
189{
190 NS_LOG_FUNCTION(this);
191 return true;
192}
193
200
201bool
203{
204 NS_LOG_FUNCTION(this);
205 return true;
206}
207
210{
211 NS_LOG_FUNCTION(addr);
213 return ad;
214}
215
218{
219 NS_LOG_FUNCTION(addr);
221 return ad;
222}
223
224bool
226{
227 NS_LOG_FUNCTION(this);
228 return false;
229}
230
231bool
233{
234 NS_LOG_FUNCTION(this);
235 return false;
236}
237
240{
241 NS_LOG_FUNCTION(this);
242 return m_node;
243}
244
245void
247{
248 NS_LOG_FUNCTION(node);
249
250 m_node = node;
251}
252
253void
255{
256 NS_LOG_FUNCTION(this << phy);
257 m_phy = phy;
258}
259
262{
263 NS_LOG_FUNCTION(this);
264 return m_phy;
265}
266
267void
273
276{
277 NS_LOG_FUNCTION(this);
278 return m_channel;
279}
280
281bool
283{
284 NS_LOG_FUNCTION(this);
285 return true;
286}
287
288bool
290{
291 NS_LOG_FUNCTION(this);
292 return m_linkUp;
293}
294
295void
301
302void
308
309void
315
316bool
318{
319 NS_LOG_FUNCTION(this);
320 return true;
321}
322
323bool
324AlohaNoackNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
325{
326 NS_LOG_FUNCTION(packet << dest << protocolNumber);
327 return SendFrom(packet, m_address, dest, protocolNumber);
328}
329
330bool
332 const Address& src,
333 const Address& dest,
334 uint16_t protocolNumber)
335{
336 NS_LOG_FUNCTION(packet << src << dest << protocolNumber);
337
338 LlcSnapHeader llc;
339 llc.SetType(protocolNumber);
340 packet->AddHeader(llc);
341
342 AlohaNoackMacHeader header;
345 packet->AddHeader(header);
346
347 m_macTxTrace(packet);
348
349 bool sendOk = true;
350 //
351 // If the device is idle, transmission starts immediately. Otherwise,
352 // the transmission will be started by NotifyTransmissionEnd
353 //
354 NS_LOG_LOGIC(this << " state=" << m_state);
355 if (m_state == IDLE)
356 {
357 if (m_queue->IsEmpty())
358 {
359 NS_LOG_LOGIC("new packet is head of queue, starting TX immediately");
360 m_currentPkt = packet;
362 }
363 else
364 {
365 NS_LOG_LOGIC("enqueueing new packet");
366 if (!m_queue->Enqueue(packet))
367 {
368 m_macTxDropTrace(packet);
369 sendOk = false;
370 }
371 }
372 }
373 else
374 {
375 NS_LOG_LOGIC("deferring TX, enqueueing new packet");
377 if (!m_queue->Enqueue(packet))
378 {
379 m_macTxDropTrace(packet);
380 sendOk = false;
381 }
382 }
383 return sendOk;
384}
385
386void
392
393void
395{
396 NS_LOG_FUNCTION(this);
397
400
402 {
403 NS_LOG_WARN("PHY refused to start TX");
404 }
405 else
406 {
407 m_state = TX;
408 }
409}
410
411void
413{
414 NS_LOG_FUNCTION(this);
415 NS_ASSERT_MSG(m_state == TX, "TX end notified while state != TX");
416 m_state = IDLE;
418 if (!m_queue->IsEmpty())
419 {
420 Ptr<Packet> p = m_queue->Dequeue();
421 NS_ASSERT(p);
422 m_currentPkt = p;
423 NS_LOG_LOGIC("scheduling transmission now");
425 }
426}
427
428void
433
434void
439
440void
442{
443 NS_LOG_FUNCTION(this << packet);
444 AlohaNoackMacHeader header;
445 packet->RemoveHeader(header);
446 NS_LOG_LOGIC("packet " << header.GetSource() << " --> " << header.GetDestination()
447 << " (here: " << m_address << ")");
448
449 LlcSnapHeader llc;
450 packet->RemoveHeader(llc);
451
452 PacketType packetType;
453 if (header.GetDestination().IsBroadcast())
454 {
455 packetType = PACKET_BROADCAST;
456 }
457 else if (header.GetDestination().IsGroup())
458 {
459 packetType = PACKET_MULTICAST;
460 }
461 else if (header.GetDestination() == m_address)
462 {
463 packetType = PACKET_HOST;
464 }
465 else
466 {
467 packetType = PACKET_OTHERHOST;
468 }
469
470 NS_LOG_LOGIC("packet type = " << packetType);
471
473 {
475 packet->Copy(),
476 llc.GetType(),
477 header.GetSource(),
478 header.GetDestination(),
479 packetType);
480 }
481
482 if (packetType != PACKET_OTHERHOST)
483 {
484 m_rxCallback(this, packet, llc.GetType(), header.GetSource());
485 }
486}
487
488} // namespace ns3
a polymophic address class
Definition address.h:90
Header for the AlohaNoack NetDevice.
Mac48Address GetSource() const
Get the source address.
void SetDestination(Mac48Address destination)
Set the destination address.
Mac48Address GetDestination() const
Get the destination address.
void SetSource(Mac48Address source)
Set the source address.
This devices implements the following features:
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
void NotifyReceptionEndError()
Notify the MAC that the PHY finished a reception with an error.
TracedCallback< Ptr< const Packet > > m_macRxTrace
Rx trace.
Ptr< Queue< Packet > > m_queue
packet queue
void AddLinkChangeCallback(Callback< void > callback) override
virtual void SetQueue(Ptr< Queue< Packet > > queue)
set the queue which is going to be used by this device
Mac48Address m_address
MAC address.
Address GetAddress() const override
bool m_linkUp
true if the link is up
NetDevice::ReceiveCallback m_rxCallback
Rx callback.
void SetIfIndex(const uint32_t index) override
void StartTransmission()
start the transmission of a packet by contacting the PHY layer
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Promiscuous Rx callback.
void NotifyReceptionStart()
Notify the MAC that the PHY has started a reception.
Ptr< Object > m_phy
PHY object.
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
void SetAddress(Address address) override
Set the address of this interface.
Ptr< Packet > m_currentPkt
Current packet.
Ptr< Channel > m_channel
Channel.
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
Promiscuous Rx trace.
Ptr< Node > m_node
Node owning this NetDevice.
Ptr< Channel > GetChannel() const override
void SetGenericPhyTxStartCallback(GenericPhyTxStartCallback c)
set the callback used to instruct the lower layer to start a TX
Address GetBroadcast() const override
uint16_t GetMtu() const override
uint32_t GetIfIndex() const override
GenericPhyTxStartCallback m_phyMacTxStartCallback
Tx Start callback.
bool SupportsSendFrom() const override
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
static TypeId GetTypeId()
Get the type ID.
void NotifyTransmissionEnd(Ptr< const Packet >)
Notify the MAC that the PHY has finished a previously started transmission.
void SetChannel(Ptr< Channel > c)
This class doesn't talk directly with the underlying channel (a dedicated PHY class is expected to do...
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Address GetMulticast(Ipv4Address addr) const override
Make and return a MAC multicast address using the provided multicast group.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
State m_state
State of the NetDevice.
void SetNode(Ptr< Node > node) override
bool IsMulticast() const override
uint32_t m_ifIndex
Interface index.
void DoDispose() override
Destructor implementation.
bool IsBroadcast() const override
void SetPhy(Ptr< Object > phy)
Set the Phy object which is attached to this device.
TracedCallback< Ptr< const Packet > > m_macTxTrace
Tx trace.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
void NotifyReceptionEndOk(Ptr< Packet > p)
Notify the MAC that the PHY finished a reception successfully.
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
Tx Drop trace.
State
State of the NetDevice.
bool SetMtu(const uint16_t mtu) override
Ptr< Node > GetNode() const override
bool IsBridge() const override
Return true if the net device is acting as a bridge.
Callback template class.
Definition callback.h:422
bool IsNull() const
Check for null implementation.
Definition callback.h:555
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 address.
Header for the LLC/SNAP encapsulation.
uint16_t GetType()
Return the Ethertype.
void SetType(uint16_t type)
Set the Ethertype.
an EUI-48 address
static Mac48Address GetMulticast(Ipv4Address address)
bool IsGroup() const
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address GetBroadcast()
bool IsBroadcast() const
Network layer to device interface.
Definition net-device.h:87
PacketType
Packet types are used as they are in Linux.
Definition net-device.h:289
@ PACKET_HOST
Packet addressed to us.
Definition net-device.h:290
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition net-device.h:296
@ PACKET_BROADCAST
Packet addressed to all.
Definition net-device.h:292
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition net-device.h:294
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
Template class for packet Queues.
Definition queue.h:257
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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.
Definition log.h:250
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
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()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
Ptr< const AttributeAccessor > MakeMac48AddressAccessor(T1 a1)
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
Ptr< const AttributeChecker > MakeMac48AddressChecker()
@ IDLE
Channel is IDLE, no packet is being transmitted.