A Discrete-Event Network Simulator
API
aloha-noack-net-device.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 CTTC
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Nicola Baldo <nbaldo@cttc.es>
18  */
19 
20 #include "aloha-noack-net-device.h"
21 
22 #include "aloha-noack-mac-header.h"
23 
24 #include "ns3/boolean.h"
25 #include "ns3/channel.h"
26 #include "ns3/enum.h"
27 #include "ns3/llc-snap-header.h"
28 #include "ns3/log.h"
29 #include "ns3/pointer.h"
30 #include "ns3/queue.h"
31 #include "ns3/simulator.h"
32 #include "ns3/trace-source-accessor.h"
33 #include "ns3/uinteger.h"
34 
35 namespace ns3
36 {
37 
38 NS_LOG_COMPONENT_DEFINE("AlohaNoackNetDevice");
39 
46 std::ostream&
47 operator<<(std::ostream& os, AlohaNoackNetDevice::State state)
48 {
49  switch (state)
50  {
52  os << "IDLE";
53  break;
55  os << "TX";
56  break;
58  os << "RX";
59  break;
60  }
61  return os;
62 }
63 
64 NS_OBJECT_ENSURE_REGISTERED(AlohaNoackNetDevice);
65 
66 TypeId
68 {
69  static TypeId tid =
70  TypeId("ns3::AlohaNoackNetDevice")
72  .SetGroupName("Spectrum")
73  .AddConstructor<AlohaNoackNetDevice>()
74  .AddAttribute("Address",
75  "The MAC address of this device.",
76  Mac48AddressValue(Mac48Address("12:34:56:78:90:12")),
77  MakeMac48AddressAccessor(&AlohaNoackNetDevice::m_address),
78  MakeMac48AddressChecker())
79  .AddAttribute("Queue",
80  "packets being transmitted get queued here",
81  PointerValue(),
83  MakePointerChecker<Queue<Packet>>())
84  .AddAttribute(
85  "Mtu",
86  "The Maximum Transmission Unit",
87  UintegerValue(1500),
89  MakeUintegerChecker<uint16_t>(1, 65535))
90  .AddAttribute(
91  "Phy",
92  "The PHY layer attached to this device.",
93  PointerValue(),
95  MakePointerChecker<Object>())
96  .AddTraceSource("MacTx",
97  "Trace source indicating a packet has arrived "
98  "for transmission by this device",
100  "ns3::Packet::TracedCallback")
101  .AddTraceSource("MacTxDrop",
102  "Trace source indicating a packet has been dropped "
103  "by the device before transmission",
105  "ns3::Packet::TracedCallback")
106  .AddTraceSource("MacPromiscRx",
107  "A packet has been received by this device, has been "
108  "passed up from the physical layer "
109  "and is being forwarded up the local protocol stack. "
110  "This is a promiscuous trace,",
112  "ns3::Packet::TracedCallback")
113  .AddTraceSource("MacRx",
114  "A packet has been received by this device, "
115  "has been passed up from the physical layer "
116  "and is being forwarded up the local protocol stack. "
117  "This is a non-promiscuous trace,",
119  "ns3::Packet::TracedCallback");
120  return tid;
121 }
122 
124  : m_state(IDLE)
125 {
126  NS_LOG_FUNCTION(this);
127 }
128 
130 {
131  NS_LOG_FUNCTION(this);
132  m_queue = nullptr;
133 }
134 
135 void
137 {
138  NS_LOG_FUNCTION(this);
139  m_queue = nullptr;
140  m_node = nullptr;
141  m_channel = nullptr;
142  m_currentPkt = nullptr;
143  m_phy = nullptr;
144  m_phyMacTxStartCallback = MakeNullCallback<bool, Ptr<Packet>>();
146 }
147 
148 void
149 AlohaNoackNetDevice::SetIfIndex(const uint32_t index)
150 {
151  NS_LOG_FUNCTION(index);
152  m_ifIndex = index;
153 }
154 
155 uint32_t
157 {
158  NS_LOG_FUNCTION(this);
159  return m_ifIndex;
160 }
161 
162 bool
164 {
165  NS_LOG_FUNCTION(mtu);
166  m_mtu = mtu;
167  return true;
168 }
169 
170 uint16_t
172 {
173  NS_LOG_FUNCTION(this);
174  return m_mtu;
175 }
176 
177 void
179 {
180  NS_LOG_FUNCTION(q);
181  m_queue = q;
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION(this);
189 }
190 
191 Address
193 {
194  NS_LOG_FUNCTION(this);
195  return m_address;
196 }
197 
198 bool
200 {
201  NS_LOG_FUNCTION(this);
202  return true;
203 }
204 
205 Address
207 {
208  NS_LOG_FUNCTION(this);
209  return Mac48Address("ff:ff:ff:ff:ff:ff");
210 }
211 
212 bool
214 {
215  NS_LOG_FUNCTION(this);
216  return true;
217 }
218 
219 Address
221 {
222  NS_LOG_FUNCTION(addr);
224  return ad;
225 }
226 
227 Address
229 {
230  NS_LOG_FUNCTION(addr);
232  return ad;
233 }
234 
235 bool
237 {
238  NS_LOG_FUNCTION(this);
239  return false;
240 }
241 
242 bool
244 {
245  NS_LOG_FUNCTION(this);
246  return false;
247 }
248 
249 Ptr<Node>
251 {
252  NS_LOG_FUNCTION(this);
253  return m_node;
254 }
255 
256 void
258 {
259  NS_LOG_FUNCTION(node);
260 
261  m_node = node;
262 }
263 
264 void
266 {
267  NS_LOG_FUNCTION(this << phy);
268  m_phy = phy;
269 }
270 
273 {
274  NS_LOG_FUNCTION(this);
275  return m_phy;
276 }
277 
278 void
280 {
281  NS_LOG_FUNCTION(this << c);
282  m_channel = c;
283 }
284 
287 {
288  NS_LOG_FUNCTION(this);
289  return m_channel;
290 }
291 
292 bool
294 {
295  NS_LOG_FUNCTION(this);
296  return true;
297 }
298 
299 bool
301 {
302  NS_LOG_FUNCTION(this);
303  return m_linkUp;
304 }
305 
306 void
308 {
309  NS_LOG_FUNCTION(&callback);
311 }
312 
313 void
315 {
316  NS_LOG_FUNCTION(&cb);
317  m_rxCallback = cb;
318 }
319 
320 void
322 {
323  NS_LOG_FUNCTION(&cb);
324  m_promiscRxCallback = cb;
325 }
326 
327 bool
329 {
330  NS_LOG_FUNCTION(this);
331  return true;
332 }
333 
334 bool
335 AlohaNoackNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
336 {
337  NS_LOG_FUNCTION(packet << dest << protocolNumber);
338  return SendFrom(packet, m_address, dest, protocolNumber);
339 }
340 
341 bool
343  const Address& src,
344  const Address& dest,
345  uint16_t protocolNumber)
346 {
347  NS_LOG_FUNCTION(packet << src << dest << protocolNumber);
348 
349  LlcSnapHeader llc;
350  llc.SetType(protocolNumber);
351  packet->AddHeader(llc);
352 
353  AlohaNoackMacHeader header;
356  packet->AddHeader(header);
357 
358  m_macTxTrace(packet);
359 
360  bool sendOk = true;
361  //
362  // If the device is idle, transmission starts immediately. Otherwise,
363  // the transmission will be started by NotifyTransmissionEnd
364  //
365  NS_LOG_LOGIC(this << " state=" << m_state);
366  if (m_state == IDLE)
367  {
368  if (m_queue->IsEmpty())
369  {
370  NS_LOG_LOGIC("new packet is head of queue, starting TX immediately");
371  m_currentPkt = packet;
373  }
374  else
375  {
376  NS_LOG_LOGIC("enqueueing new packet");
377  if (!m_queue->Enqueue(packet))
378  {
379  m_macTxDropTrace(packet);
380  sendOk = false;
381  }
382  }
383  }
384  else
385  {
386  NS_LOG_LOGIC("deferring TX, enqueueing new packet");
388  if (!m_queue->Enqueue(packet))
389  {
390  m_macTxDropTrace(packet);
391  sendOk = false;
392  }
393  }
394  return sendOk;
395 }
396 
397 void
399 {
400  NS_LOG_FUNCTION(this);
402 }
403 
404 void
406 {
407  NS_LOG_FUNCTION(this);
408 
410  NS_ASSERT(m_state == IDLE);
411 
413  {
414  NS_LOG_WARN("PHY refused to start TX");
415  }
416  else
417  {
418  m_state = TX;
419  }
420 }
421 
422 void
424 {
425  NS_LOG_FUNCTION(this);
426  NS_ASSERT_MSG(m_state == TX, "TX end notified while state != TX");
427  m_state = IDLE;
429  if (!m_queue->IsEmpty())
430  {
431  Ptr<Packet> p = m_queue->Dequeue();
432  NS_ASSERT(p);
433  m_currentPkt = p;
434  NS_LOG_LOGIC("scheduling transmission now");
436  }
437 }
438 
439 void
441 {
442  NS_LOG_FUNCTION(this);
443 }
444 
445 void
447 {
448  NS_LOG_FUNCTION(this);
449 }
450 
451 void
453 {
454  NS_LOG_FUNCTION(this << packet);
455  AlohaNoackMacHeader header;
456  packet->RemoveHeader(header);
457  NS_LOG_LOGIC("packet " << header.GetSource() << " --> " << header.GetDestination()
458  << " (here: " << m_address << ")");
459 
460  LlcSnapHeader llc;
461  packet->RemoveHeader(llc);
462 
463  PacketType packetType;
464  if (header.GetDestination().IsBroadcast())
465  {
466  packetType = PACKET_BROADCAST;
467  }
468  else if (header.GetDestination().IsGroup())
469  {
470  packetType = PACKET_MULTICAST;
471  }
472  else if (header.GetDestination() == m_address)
473  {
474  packetType = PACKET_HOST;
475  }
476  else
477  {
478  packetType = PACKET_OTHERHOST;
479  }
480 
481  NS_LOG_LOGIC("packet type = " << packetType);
482 
484  {
485  m_promiscRxCallback(this,
486  packet->Copy(),
487  llc.GetType(),
488  header.GetSource(),
489  header.GetDestination(),
490  packetType);
491  }
492 
493  if (packetType != PACKET_OTHERHOST)
494  {
495  m_rxCallback(this, packet, llc.GetType(), header.GetSource());
496  }
497 }
498 
499 } // namespace ns3
a polymophic address class
Definition: address.h:101
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
bool IsLinkUp() const override
void AddLinkChangeCallback(Callback< void > callback) override
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
bool NeedsArp() const override
uint32_t m_ifIndex
Interface index.
void DoDispose() override
Destructor implementation.
bool IsBroadcast() const override
uint32_t m_mtu
NetDevice MTU.
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
virtual void SetQueue(Ptr< Queue< Packet >> queue)
set the queue which is going to be used by this device
Ptr< Node > GetNode() const override
bool IsBridge() const override
Return true if the net device is acting as a bridge.
bool IsNull() const
Check for null implementation.
Definition: callback.h:569
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
Header for the LLC/SNAP encapsulation.
uint16_t GetType()
Return the Ethertype.
void SetType(uint16_t type)
Set the Ethertype.
an EUI-48 address
Definition: mac48-address.h:46
static Mac48Address GetMulticast(Ipv4Address address)
bool IsGroup() const
static Mac48Address ConvertFrom(const Address &address)
bool IsBroadcast() const
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
@ PACKET_HOST
Packet addressed to us.
Definition: net-device.h:301
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:307
@ PACKET_BROADCAST
Packet addressed to all.
Definition: net-device.h:303
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:305
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Template class for packet Queues.
Definition: queue.h:268
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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:261
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
@ IDLE
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:76
phy
Definition: third.py:89