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 
38 std::ostream& operator<< (std::ostream& os, AlohaNoackNetDevice::State state)
39 {
40  switch (state)
41  {
43  os << "IDLE";
44  break;
46  os << "TX";
47  break;
49  os << "RX";
50  break;
51  }
52  return os;
53 }
54 
55 
56 NS_OBJECT_ENSURE_REGISTERED (AlohaNoackNetDevice);
57 
58 TypeId
60 {
61  static TypeId tid = TypeId ("ns3::AlohaNoackNetDevice")
62  .SetParent<NetDevice> ()
63  .SetGroupName ("Spectrum")
64  .AddConstructor<AlohaNoackNetDevice> ()
65  .AddAttribute ("Address",
66  "The MAC address of this device.",
67  Mac48AddressValue (Mac48Address ("12:34:56:78:90:12")),
70  .AddAttribute ("Queue",
71  "packets being transmitted get queued here",
72  PointerValue (),
74  MakePointerChecker<Queue> ())
75  .AddAttribute ("Mtu", "The Maximum Transmission Unit",
76  UintegerValue (1500),
79  MakeUintegerChecker<uint16_t> (1,65535))
80  .AddAttribute ("Phy", "The PHY layer attached to this device.",
81  PointerValue (),
84  MakePointerChecker<Object> ())
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  ;
110  return tid;
111 }
112 
114  : m_state (IDLE)
115 {
116  NS_LOG_FUNCTION (this);
117 }
118 
120 {
121  NS_LOG_FUNCTION (this);
122  m_queue = 0;
123 }
124 
125 void
127 {
128  NS_LOG_FUNCTION (this);
129  m_queue = 0;
130  m_node = 0;
131  m_channel = 0;
132  m_currentPkt = 0;
133  m_phy = 0;
134  m_phyMacTxStartCallback = MakeNullCallback< bool, Ptr<Packet> > ();
136 }
137 
138 
139 void
140 AlohaNoackNetDevice::SetIfIndex (const uint32_t index)
141 {
142  NS_LOG_FUNCTION (index);
143  m_ifIndex = index;
144 }
145 
146 uint32_t
148 {
149  NS_LOG_FUNCTION (this);
150  return m_ifIndex;
151 }
152 
153 bool
155 {
156  NS_LOG_FUNCTION (mtu);
157  m_mtu = mtu;
158  return true;
159 }
160 
161 uint16_t
163 {
164  NS_LOG_FUNCTION (this);
165  return m_mtu;
166 }
167 
168 
169 void
171 {
172  NS_LOG_FUNCTION (q);
173  m_queue = q;
174 }
175 
176 
177 void
179 {
180  NS_LOG_FUNCTION (this);
182 }
183 
184 Address
186 {
187  NS_LOG_FUNCTION (this);
188  return m_address;
189 }
190 
191 bool
193 {
194  NS_LOG_FUNCTION (this);
195  return true;
196 }
197 
198 Address
200 {
201  NS_LOG_FUNCTION (this);
202  return Mac48Address ("ff:ff:ff:ff:ff:ff");
203 }
204 
205 bool
207 {
208  NS_LOG_FUNCTION (this);
209  return true;
210 }
211 
212 Address
214 {
215  NS_LOG_FUNCTION (addr);
217  return ad;
218 }
219 
220 
222 {
223  NS_LOG_FUNCTION (addr);
225  return ad;
226 }
227 
228 
229 bool
231 {
232  NS_LOG_FUNCTION (this);
233  return false;
234 }
235 
236 bool
238 {
239  NS_LOG_FUNCTION (this);
240  return false;
241 }
242 
243 
244 Ptr<Node>
246 {
247  NS_LOG_FUNCTION (this);
248  return m_node;
249 }
250 
251 void
253 {
254  NS_LOG_FUNCTION (node);
255 
256  m_node = node;
257 }
258 
259 void
261 {
262  NS_LOG_FUNCTION (this << phy);
263  m_phy = phy;
264 }
265 
266 
269 {
270  NS_LOG_FUNCTION (this);
271  return m_phy;
272 }
273 
274 
275 void
277 {
278  NS_LOG_FUNCTION (this << c);
279  m_channel = c;
280 }
281 
282 
285 {
286  NS_LOG_FUNCTION (this);
287  return m_channel;
288 }
289 
290 
291 bool
293 {
294  NS_LOG_FUNCTION (this);
295  return true;
296 }
297 
298 bool
300 {
301  NS_LOG_FUNCTION (this);
302  return m_linkUp;
303 }
304 
305 void
307 {
308  NS_LOG_FUNCTION (&callback);
310 }
311 
312 void
314 {
315  NS_LOG_FUNCTION (&cb);
316  m_rxCallback = cb;
317 }
318 
319 void
321 {
322  NS_LOG_FUNCTION (&cb);
323  m_promiscRxCallback = cb;
324 }
325 
326 bool
328 {
329  NS_LOG_FUNCTION (this);
330  return true;
331 }
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
342 AlohaNoackNetDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber)
343 {
344  NS_LOG_FUNCTION (packet << src << dest << protocolNumber);
345 
346  LlcSnapHeader llc;
347  llc.SetType (protocolNumber);
348  packet->AddHeader (llc);
349 
350  AlohaNoackMacHeader header;
351  header.SetSource (Mac48Address::ConvertFrom (src));
353  packet->AddHeader (header);
354 
355  m_macTxTrace (packet);
356 
357 
358  bool sendOk = true;
359  //
360  // If the device is idle, transmission starts immediately. Otherwise,
361  // the transmission will be started by NotifyTransmissionEnd
362  //
363  NS_LOG_LOGIC (this << " state=" << m_state);
364  if (m_state == IDLE)
365  {
366  if (m_queue->IsEmpty ())
367  {
368  NS_LOG_LOGIC ("new packet is head of queue, starting TX immediately");
369  m_currentPkt = packet;
371  }
372  else
373  {
374  NS_LOG_LOGIC ("enqueueing new packet");
375  if (m_queue->Enqueue (packet) == false)
376  {
377  m_macTxDropTrace (packet);
378  sendOk = false;
379  }
380  }
381  }
382  else
383  {
384  NS_LOG_LOGIC ("deferring TX, enqueueing new packet");
385  NS_ASSERT (m_queue);
386  if (m_queue->Enqueue (packet) == false)
387  {
388  m_macTxDropTrace (packet);
389  sendOk = false;
390  }
391  }
392  return sendOk;
393 }
394 
395 void
397 {
398  NS_LOG_FUNCTION (this);
400 }
401 
402 void
404 {
405  NS_LOG_FUNCTION (this);
406 
407  NS_ASSERT (m_currentPkt != 0);
408  NS_ASSERT (m_state == IDLE);
409 
411  {
412  NS_LOG_WARN ("PHY refused to start TX");
413  }
414  else
415  {
416  m_state = TX;
417  }
418 }
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;
428  NS_ASSERT (m_queue);
429  if (m_queue->IsEmpty () == false)
430  {
431  m_currentPkt = m_queue->Dequeue ();
433  NS_LOG_LOGIC ("scheduling transmission now");
435  }
436 }
437 
438 
439 void
441 {
442  NS_LOG_FUNCTION (this);
443 }
444 
445 
446 
447 void
449 {
450  NS_LOG_FUNCTION (this);
451 }
452 
453 
454 
455 
456 
457 void
459 {
460  NS_LOG_FUNCTION (this << packet);
461  AlohaNoackMacHeader header;
462  packet->RemoveHeader (header);
463  NS_LOG_LOGIC ("packet " << header.GetSource () << " --> " << header.GetDestination () << " (here: " << m_address << ")");
464 
465  LlcSnapHeader llc;
466  packet->RemoveHeader (llc);
467 
468  PacketType packetType;
469  if (header.GetDestination ().IsBroadcast ())
470  {
471  packetType = PACKET_BROADCAST;
472  }
473  else if (header.GetDestination ().IsGroup ())
474  {
475  packetType = PACKET_MULTICAST;
476  }
477  else if (header.GetDestination () == m_address)
478  {
479  packetType = PACKET_HOST;
480  }
481  else
482  {
483  packetType = PACKET_OTHERHOST;
484  }
485 
486  NS_LOG_LOGIC ("packet type = " << packetType);
487 
488  if (!m_promiscRxCallback.IsNull ())
489  {
490  m_promiscRxCallback (this, packet->Copy (), llc.GetType (), header.GetSource (), header.GetDestination (), packetType);
491  }
492 
493  if (packetType != PACKET_OTHERHOST)
494  {
495  m_rxCallback (this, packet, llc.GetType (), header.GetSource () );
496  }
497 }
498 
499 
500 
501 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:266
virtual void SetIfIndex(const uint32_t index)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void NotifyReceptionStart()
Notify the MAC that the PHY has started a reception.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual Address GetMulticast(Ipv4Address addr) const
Make and return a MAC multicast address using the provided multicast group.
void SetGenericPhyTxStartCallback(GenericPhyTxStartCallback c)
set the callback used to instruct the lower layer to start a TX
virtual Address GetBroadcast(void) const
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1258
#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
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:276
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
TracedCallback< Ptr< const Packet > > m_macPromiscRxTrace
bool IsBroadcast(void) const
virtual void AddLinkChangeCallback(Callback< void > callback)
Ptr< const AttributeChecker > MakeMac48AddressChecker(void)
Packet addressed to multicast group.
Definition: net-device.h:282
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
void SetType(uint16_t type)
Set the Ethertype.
Packet addressed oo us.
Definition: net-device.h:278
Ptr< const AttributeAccessor > MakeMac48AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual Ptr< Node > GetNode(void) const
void NotifyReceptionEndOk(Ptr< Packet > p)
Notify the MAC that the PHY finished a reception successfully.
GenericPhyTxStartCallback m_phyMacTxStartCallback
tuple phy
Definition: third.py:86
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:220
static Mac48Address GetMulticast(Ipv4Address address)
void SetDestination(Mac48Address destination)
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Hold an unsigned integer type.
Definition: uinteger.h:44
virtual uint16_t GetMtu(void) const
virtual bool IsBroadcast(void) const
Introspection did not find any typical Config paths.
void NotifyTransmissionEnd(Ptr< const Packet >)
Notify the MAC that the PHY has finished a previously started transmission.
virtual bool SetMtu(const uint16_t mtu)
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
static Mac48Address ConvertFrom(const Address &address)
virtual void DoDispose(void)
Destructor implementation.
TracedCallback< Ptr< const Packet > > m_macRxTrace
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
virtual bool IsLinkUp(void) const
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
virtual bool SupportsSendFrom(void) const
TracedCallback< Ptr< const Packet > > m_macTxTrace
void SetChannel(Ptr< Channel > c)
This class doesn't talk directly with the underlying channel (a dedicated PHY class is expected to do...
bool IsGroup(void) const
void StartTransmission()
start the transmission of a packet by contacting the PHY layer
Packet addressed to someone else.
Definition: net-device.h:284
virtual bool NeedsArp(void) const
an EUI-48 address
Definition: mac48-address.h:43
Ptr< Object > GetPhy() const
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1379
virtual void SetQueue(Ptr< Queue > queue)
set the queue which is going to be used by this device
virtual bool IsMulticast(void) const
This devices implements the following features:
NetDevice::PromiscReceiveCallback m_promiscRxCallback
#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:90
void NotifyReceptionEndError()
Notify the MAC that the PHY finished a reception with an error.
Describes an IPv6 address.
Definition: ipv6-address.h:47
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Network layer to device interface.
Definition: net-device.h:75
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
AttributeValue implementation for Mac48Address.
void SetPhy(Ptr< Object > phy)
Set the Phy object which is attached to this device.
Mac48Address GetSource() const
Packet addressed to all.
Definition: net-device.h:280
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
void SetSource(Mac48Address source)
Mac48Address GetDestination() const
tuple address
Definition: first.py:37
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
a unique identifier for an interface.
Definition: type-id.h:58
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual uint32_t GetIfIndex(void) const
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
virtual void SetNode(Ptr< Node > node)
NetDevice::ReceiveCallback m_rxCallback
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:255
Header for the LLC/SNAP encapsulation.
virtual Ptr< Channel > GetChannel(void) const
virtual Address GetAddress(void) const
virtual void SetAddress(Address address)
Set the address of this interface.