A Discrete-Event Network Simulator
API
aloha-noack-net-device.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/queue.h"
23 #include "ns3/simulator.h"
24 #include "ns3/enum.h"
25 #include "ns3/boolean.h"
26 #include "ns3/uinteger.h"
27 #include "ns3/pointer.h"
28 #include "ns3/channel.h"
29 #include "ns3/trace-source-accessor.h"
30 #include "aloha-noack-mac-header.h"
31 #include "aloha-noack-net-device.h"
32 #include "ns3/llc-snap-header.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("AlohaNoackNetDevice");
37 
44 std::ostream& operator<< (std::ostream& os, AlohaNoackNetDevice::State state)
45 {
46  switch (state)
47  {
49  os << "IDLE";
50  break;
52  os << "TX";
53  break;
55  os << "RX";
56  break;
57  }
58  return os;
59 }
60 
61 
62 NS_OBJECT_ENSURE_REGISTERED (AlohaNoackNetDevice);
63 
64 TypeId
66 {
67  static TypeId tid = TypeId ("ns3::AlohaNoackNetDevice")
68  .SetParent<NetDevice> ()
69  .SetGroupName ("Spectrum")
70  .AddConstructor<AlohaNoackNetDevice> ()
71  .AddAttribute ("Address",
72  "The MAC address of this device.",
73  Mac48AddressValue (Mac48Address ("12:34:56:78:90:12")),
76  .AddAttribute ("Queue",
77  "packets being transmitted get queued here",
78  PointerValue (),
80  MakePointerChecker<Queue<Packet> > ())
81  .AddAttribute ("Mtu", "The Maximum Transmission Unit",
82  UintegerValue (1500),
85  MakeUintegerChecker<uint16_t> (1,65535))
86  .AddAttribute ("Phy", "The PHY layer attached to this device.",
87  PointerValue (),
90  MakePointerChecker<Object> ())
91  .AddTraceSource ("MacTx",
92  "Trace source indicating a packet has arrived "
93  "for transmission by this device",
95  "ns3::Packet::TracedCallback")
96  .AddTraceSource ("MacTxDrop",
97  "Trace source indicating a packet has been dropped "
98  "by the device before transmission",
100  "ns3::Packet::TracedCallback")
101  .AddTraceSource ("MacPromiscRx",
102  "A packet has been received by this device, has been "
103  "passed up from the physical layer "
104  "and is being forwarded up the local protocol stack. "
105  "This is a promiscuous trace,",
107  "ns3::Packet::TracedCallback")
108  .AddTraceSource ("MacRx",
109  "A packet has been received by this device, "
110  "has been passed up from the physical layer "
111  "and is being forwarded up the local protocol stack. "
112  "This is a non-promiscuous trace,",
114  "ns3::Packet::TracedCallback")
115  ;
116  return tid;
117 }
118 
120  : m_state (IDLE)
121 {
122  NS_LOG_FUNCTION (this);
123 }
124 
126 {
127  NS_LOG_FUNCTION (this);
128  m_queue = 0;
129 }
130 
131 void
133 {
134  NS_LOG_FUNCTION (this);
135  m_queue = 0;
136  m_node = 0;
137  m_channel = 0;
138  m_currentPkt = 0;
139  m_phy = 0;
140  m_phyMacTxStartCallback = MakeNullCallback< bool, Ptr<Packet> > ();
142 }
143 
144 
145 void
146 AlohaNoackNetDevice::SetIfIndex (const uint32_t index)
147 {
148  NS_LOG_FUNCTION (index);
149  m_ifIndex = index;
150 }
151 
152 uint32_t
154 {
155  NS_LOG_FUNCTION (this);
156  return m_ifIndex;
157 }
158 
159 bool
161 {
162  NS_LOG_FUNCTION (mtu);
163  m_mtu = mtu;
164  return true;
165 }
166 
167 uint16_t
169 {
170  NS_LOG_FUNCTION (this);
171  return m_mtu;
172 }
173 
174 
175 void
177 {
178  NS_LOG_FUNCTION (q);
179  m_queue = q;
180 }
181 
182 
183 void
185 {
186  NS_LOG_FUNCTION (this);
188 }
189 
190 Address
192 {
193  NS_LOG_FUNCTION (this);
194  return m_address;
195 }
196 
197 bool
199 {
200  NS_LOG_FUNCTION (this);
201  return true;
202 }
203 
204 Address
206 {
207  NS_LOG_FUNCTION (this);
208  return Mac48Address ("ff:ff:ff:ff:ff:ff");
209 }
210 
211 bool
213 {
214  NS_LOG_FUNCTION (this);
215  return true;
216 }
217 
218 Address
220 {
221  NS_LOG_FUNCTION (addr);
223  return ad;
224 }
225 
226 
228 {
229  NS_LOG_FUNCTION (addr);
231  return ad;
232 }
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 
250 Ptr<Node>
252 {
253  NS_LOG_FUNCTION (this);
254  return m_node;
255 }
256 
257 void
259 {
260  NS_LOG_FUNCTION (node);
261 
262  m_node = node;
263 }
264 
265 void
267 {
268  NS_LOG_FUNCTION (this << phy);
269  m_phy = phy;
270 }
271 
272 
275 {
276  NS_LOG_FUNCTION (this);
277  return m_phy;
278 }
279 
280 
281 void
283 {
284  NS_LOG_FUNCTION (this << c);
285  m_channel = c;
286 }
287 
288 
291 {
292  NS_LOG_FUNCTION (this);
293  return m_channel;
294 }
295 
296 
297 bool
299 {
300  NS_LOG_FUNCTION (this);
301  return true;
302 }
303 
304 bool
306 {
307  NS_LOG_FUNCTION (this);
308  return m_linkUp;
309 }
310 
311 void
313 {
314  NS_LOG_FUNCTION (&callback);
316 }
317 
318 void
320 {
321  NS_LOG_FUNCTION (&cb);
322  m_rxCallback = cb;
323 }
324 
325 void
327 {
328  NS_LOG_FUNCTION (&cb);
329  m_promiscRxCallback = cb;
330 }
331 
332 bool
334 {
335  NS_LOG_FUNCTION (this);
336  return true;
337 }
338 
339 
340 bool
341 AlohaNoackNetDevice::Send (Ptr<Packet> packet,const Address& dest, uint16_t protocolNumber)
342 {
343  NS_LOG_FUNCTION (packet << dest << protocolNumber);
344  return SendFrom (packet, m_address, dest, protocolNumber);
345 }
346 
347 bool
348 AlohaNoackNetDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber)
349 {
350  NS_LOG_FUNCTION (packet << src << dest << protocolNumber);
351 
352  LlcSnapHeader llc;
353  llc.SetType (protocolNumber);
354  packet->AddHeader (llc);
355 
356  AlohaNoackMacHeader header;
357  header.SetSource (Mac48Address::ConvertFrom (src));
359  packet->AddHeader (header);
360 
361  m_macTxTrace (packet);
362 
363 
364  bool sendOk = true;
365  //
366  // If the device is idle, transmission starts immediately. Otherwise,
367  // the transmission will be started by NotifyTransmissionEnd
368  //
369  NS_LOG_LOGIC (this << " state=" << m_state);
370  if (m_state == IDLE)
371  {
372  if (m_queue->IsEmpty ())
373  {
374  NS_LOG_LOGIC ("new packet is head of queue, starting TX immediately");
375  m_currentPkt = packet;
377  }
378  else
379  {
380  NS_LOG_LOGIC ("enqueueing new packet");
381  if (m_queue->Enqueue (packet) == false)
382  {
383  m_macTxDropTrace (packet);
384  sendOk = false;
385  }
386  }
387  }
388  else
389  {
390  NS_LOG_LOGIC ("deferring TX, enqueueing new packet");
391  NS_ASSERT (m_queue);
392  if (m_queue->Enqueue (packet) == false)
393  {
394  m_macTxDropTrace (packet);
395  sendOk = false;
396  }
397  }
398  return sendOk;
399 }
400 
401 void
403 {
404  NS_LOG_FUNCTION (this);
406 }
407 
408 void
410 {
411  NS_LOG_FUNCTION (this);
412 
413  NS_ASSERT (m_currentPkt != 0);
414  NS_ASSERT (m_state == IDLE);
415 
417  {
418  NS_LOG_WARN ("PHY refused to start TX");
419  }
420  else
421  {
422  m_state = TX;
423  }
424 }
425 
426 
427 
428 void
430 {
431  NS_LOG_FUNCTION (this);
432  NS_ASSERT_MSG (m_state == TX, "TX end notified while state != TX");
433  m_state = IDLE;
434  NS_ASSERT (m_queue);
435  if (m_queue->IsEmpty () == false)
436  {
437  Ptr<Packet> p = m_queue->Dequeue ();
438  NS_ASSERT (p);
439  m_currentPkt = p;
440  NS_LOG_LOGIC ("scheduling transmission now");
442  }
443 }
444 
445 
446 void
448 {
449  NS_LOG_FUNCTION (this);
450 }
451 
452 
453 
454 void
456 {
457  NS_LOG_FUNCTION (this);
458 }
459 
460 
461 
462 
463 
464 void
466 {
467  NS_LOG_FUNCTION (this << packet);
468  AlohaNoackMacHeader header;
469  packet->RemoveHeader (header);
470  NS_LOG_LOGIC ("packet " << header.GetSource () << " --> " << header.GetDestination () << " (here: " << m_address << ")");
471 
472  LlcSnapHeader llc;
473  packet->RemoveHeader (llc);
474 
475  PacketType packetType;
476  if (header.GetDestination ().IsBroadcast ())
477  {
478  packetType = PACKET_BROADCAST;
479  }
480  else if (header.GetDestination ().IsGroup ())
481  {
482  packetType = PACKET_MULTICAST;
483  }
484  else if (header.GetDestination () == m_address)
485  {
486  packetType = PACKET_HOST;
487  }
488  else
489  {
490  packetType = PACKET_OTHERHOST;
491  }
492 
493  NS_LOG_LOGIC ("packet type = " << packetType);
494 
495  if (!m_promiscRxCallback.IsNull ())
496  {
497  m_promiscRxCallback (this, packet->Copy (), llc.GetType (), header.GetSource (), header.GetDestination (), packetType);
498  }
499 
500  if (packetType != PACKET_OTHERHOST)
501  {
502  m_rxCallback (this, packet, llc.GetType (), header.GetSource () );
503  }
504 }
505 
506 
507 
508 } // namespace ns3
a polymophic address class
Definition: address.h:91
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
virtual void SetQueue(Ptr< Queue< Packet > > queue)
set the queue which is going to be used by this device
Mac48Address m_address
MAC address.
bool m_linkUp
true if the link is up
NetDevice::ReceiveCallback m_rxCallback
Rx callback.
virtual uint32_t GetIfIndex(void) const
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.
virtual void SetAddress(Address address)
Set the address of this interface.
Ptr< Object > m_phy
PHY object.
virtual void DoDispose(void)
Destructor implementation.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
virtual void SetNode(Ptr< Node > node)
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.
void SetGenericPhyTxStartCallback(GenericPhyTxStartCallback c)
set the callback used to instruct the lower layer to start a TX
virtual uint16_t GetMtu(void) const
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
GenericPhyTxStartCallback m_phyMacTxStartCallback
Tx Start callback.
static TypeId GetTypeId(void)
Get the type ID.
virtual bool IsBroadcast(void) const
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
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...
virtual Address GetAddress(void) const
State m_state
State of the NetDevice.
virtual Address GetMulticast(Ipv4Address addr) const
Make and return a MAC multicast address using the provided multicast group.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual void SetIfIndex(const uint32_t index)
virtual Ptr< Channel > GetChannel(void) const
virtual Address GetBroadcast(void) const
virtual bool SupportsSendFrom(void) const
virtual bool NeedsArp(void) const
uint32_t m_ifIndex
Interface index.
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.
virtual Ptr< Node > GetNode(void) const
void NotifyReceptionEndOk(Ptr< Packet > p)
Notify the MAC that the PHY finished a reception successfully.
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
Tx Drop trace.
State
State of the NetDevice.
virtual bool IsLinkUp(void) const
virtual bool SetMtu(const uint16_t mtu)
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool IsMulticast(void) const
virtual void AddLinkChangeCallback(Callback< void > callback)
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Describes an IPv6 address.
Definition: ipv6-address.h:50
Header for the LLC/SNAP encapsulation.
uint16_t GetType(void)
Return the Ethertype.
void SetType(uint16_t type)
Set the Ethertype.
an EUI-48 address
Definition: mac48-address.h:44
static Mac48Address GetMulticast(Ipv4Address address)
bool IsGroup(void) const
bool IsBroadcast(void) const
static Mac48Address ConvertFrom(const Address &address)
AttributeValue implementation for Mac48Address.
Network layer to device interface.
Definition: net-device.h:96
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
@ PACKET_HOST
Packet addressed oo us.
Definition: net-device.h:298
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:304
@ PACKET_BROADCAST
Packet addressed to all.
Definition: net-device.h:300
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:302
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:587
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:922
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#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:88
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(void)
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:227
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:265
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
@ IDLE
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:75
phy
Definition: third.py:93