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  .AddConstructor<AlohaNoackNetDevice> ()
64  .AddAttribute ("Address",
65  "The MAC address of this device.",
66  Mac48AddressValue (Mac48Address ("12:34:56:78:90:12")),
69  .AddAttribute ("Queue",
70  "packets being transmitted get queued here",
71  PointerValue (),
73  MakePointerChecker<Queue> ())
74  .AddAttribute ("Mtu", "The Maximum Transmission Unit",
75  UintegerValue (1500),
78  MakeUintegerChecker<uint16_t> (1,65535))
79  .AddAttribute ("Phy", "The PHY layer attached to this device.",
80  PointerValue (),
83  MakePointerChecker<Object> ())
84  .AddTraceSource ("MacTx",
85  "Trace source indicating a packet has arrived "
86  "for transmission by this device",
88  "ns3::Packet::TracedCallback")
89  .AddTraceSource ("MacTxDrop",
90  "Trace source indicating a packet has been dropped "
91  "by the device before transmission",
93  "ns3::Packet::TracedCallback")
94  .AddTraceSource ("MacPromiscRx",
95  "A packet has been received by this device, has been "
96  "passed up from the physical layer "
97  "and is being forwarded up the local protocol stack. "
98  "This is a promiscuous trace,",
100  "ns3::Packet::TracedCallback")
101  .AddTraceSource ("MacRx",
102  "A packet has been received by this device, "
103  "has been passed up from the physical layer "
104  "and is being forwarded up the local protocol stack. "
105  "This is a non-promiscuous trace,",
107  "ns3::Packet::TracedCallback")
108  ;
109  return tid;
110 }
111 
113  : m_state (IDLE)
114 {
115  NS_LOG_FUNCTION (this);
116 }
117 
119 {
120  NS_LOG_FUNCTION (this);
121  m_queue = 0;
122 }
123 
124 void
126 {
127  NS_LOG_FUNCTION (this);
128  m_queue = 0;
129  m_node = 0;
130  m_channel = 0;
131  m_currentPkt = 0;
132  m_phy = 0;
133  m_phyMacTxStartCallback = MakeNullCallback< bool, Ptr<Packet> > ();
135 }
136 
137 
138 void
139 AlohaNoackNetDevice::SetIfIndex (const uint32_t index)
140 {
141  NS_LOG_FUNCTION (index);
142  m_ifIndex = index;
143 }
144 
145 uint32_t
147 {
148  NS_LOG_FUNCTION (this);
149  return m_ifIndex;
150 }
151 
152 bool
154 {
155  NS_LOG_FUNCTION (mtu);
156  m_mtu = mtu;
157  return true;
158 }
159 
160 uint16_t
162 {
163  NS_LOG_FUNCTION (this);
164  return m_mtu;
165 }
166 
167 
168 void
170 {
171  NS_LOG_FUNCTION (q);
172  m_queue = q;
173 }
174 
175 
176 void
178 {
179  NS_LOG_FUNCTION (this);
181 }
182 
183 Address
185 {
186  NS_LOG_FUNCTION (this);
187  return m_address;
188 }
189 
190 bool
192 {
193  NS_LOG_FUNCTION (this);
194  return true;
195 }
196 
197 Address
199 {
200  NS_LOG_FUNCTION (this);
201  return Mac48Address ("ff:ff:ff:ff:ff:ff");
202 }
203 
204 bool
206 {
207  NS_LOG_FUNCTION (this);
208  return true;
209 }
210 
211 Address
213 {
214  NS_LOG_FUNCTION (addr);
216  return ad;
217 }
218 
219 
221 {
222  NS_LOG_FUNCTION (addr);
224  return ad;
225 }
226 
227 
228 bool
230 {
231  NS_LOG_FUNCTION (this);
232  return false;
233 }
234 
235 bool
237 {
238  NS_LOG_FUNCTION (this);
239  return false;
240 }
241 
242 
243 Ptr<Node>
245 {
246  NS_LOG_FUNCTION (this);
247  return m_node;
248 }
249 
250 void
252 {
253  NS_LOG_FUNCTION (node);
254 
255  m_node = node;
256 }
257 
258 void
260 {
261  NS_LOG_FUNCTION (this << phy);
262  m_phy = phy;
263 }
264 
265 
268 {
269  NS_LOG_FUNCTION (this);
270  return m_phy;
271 }
272 
273 
274 void
276 {
277  NS_LOG_FUNCTION (this << c);
278  m_channel = c;
279 }
280 
281 
284 {
285  NS_LOG_FUNCTION (this);
286  return m_channel;
287 }
288 
289 
290 bool
292 {
293  NS_LOG_FUNCTION (this);
294  return true;
295 }
296 
297 bool
299 {
300  NS_LOG_FUNCTION (this);
301  return m_linkUp;
302 }
303 
304 void
306 {
307  NS_LOG_FUNCTION (&callback);
309 }
310 
311 void
313 {
314  NS_LOG_FUNCTION (&cb);
315  m_rxCallback = cb;
316 }
317 
318 void
320 {
321  NS_LOG_FUNCTION (&cb);
322  m_promiscRxCallback = cb;
323 }
324 
325 bool
327 {
328  NS_LOG_FUNCTION (this);
329  return true;
330 }
331 
332 
333 bool
334 AlohaNoackNetDevice::Send (Ptr<Packet> packet,const Address& dest, uint16_t protocolNumber)
335 {
336  NS_LOG_FUNCTION (packet << dest << protocolNumber);
337  return SendFrom (packet, m_address, dest, protocolNumber);
338 }
339 
340 bool
341 AlohaNoackNetDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber)
342 {
343  NS_LOG_FUNCTION (packet << src << dest << protocolNumber);
344 
345  LlcSnapHeader llc;
346  llc.SetType (protocolNumber);
347  packet->AddHeader (llc);
348 
349  AlohaNoackMacHeader header;
350  header.SetSource (Mac48Address::ConvertFrom (src));
352  packet->AddHeader (header);
353 
354  m_macTxTrace (packet);
355 
356 
357  bool sendOk = true;
358  //
359  // If the device is idle, transmission starts immediately. Otherwise,
360  // the transmission will be started by NotifyTransmissionEnd
361  //
362  NS_LOG_LOGIC (this << " state=" << m_state);
363  if (m_state == IDLE)
364  {
365  if (m_queue->IsEmpty ())
366  {
367  NS_LOG_LOGIC ("new packet is head of queue, starting TX immediately");
368  m_currentPkt = packet;
370  }
371  else
372  {
373  NS_LOG_LOGIC ("enqueueing new packet");
374  if (m_queue->Enqueue (packet) == false)
375  {
376  m_macTxDropTrace (packet);
377  sendOk = false;
378  }
379  }
380  }
381  else
382  {
383  NS_LOG_LOGIC ("deferring TX, enqueueing new packet");
384  NS_ASSERT (m_queue);
385  if (m_queue->Enqueue (packet) == false)
386  {
387  m_macTxDropTrace (packet);
388  sendOk = false;
389  }
390  }
391  return sendOk;
392 }
393 
394 void
396 {
397  NS_LOG_FUNCTION (this);
399 }
400 
401 void
403 {
404  NS_LOG_FUNCTION (this);
405 
406  NS_ASSERT (m_currentPkt != 0);
407  NS_ASSERT (m_state == IDLE);
408 
410  {
411  NS_LOG_WARN ("PHY refused to start TX");
412  }
413  else
414  {
415  m_state = TX;
416  }
417 }
418 
419 
420 
421 void
423 {
424  NS_LOG_FUNCTION (this);
425  NS_ASSERT_MSG (m_state == TX, "TX end notified while state != TX");
426  m_state = IDLE;
427  NS_ASSERT (m_queue);
428  if (m_queue->IsEmpty () == false)
429  {
430  m_currentPkt = m_queue->Dequeue ();
432  NS_LOG_LOGIC ("scheduling transmission now");
434  }
435 }
436 
437 
438 void
440 {
441  NS_LOG_FUNCTION (this);
442 }
443 
444 
445 
446 void
448 {
449  NS_LOG_FUNCTION (this);
450 }
451 
452 
453 
454 
455 
456 void
458 {
459  NS_LOG_FUNCTION (this << packet);
460  AlohaNoackMacHeader header;
461  packet->RemoveHeader (header);
462  NS_LOG_LOGIC ("packet " << header.GetSource () << " --> " << header.GetDestination () << " (here: " << m_address << ")");
463 
464  LlcSnapHeader llc;
465  packet->RemoveHeader (llc);
466 
467  PacketType packetType;
468  if (header.GetDestination ().IsBroadcast ())
469  {
470  packetType = PACKET_BROADCAST;
471  }
472  else if (header.GetDestination ().IsGroup ())
473  {
474  packetType = PACKET_MULTICAST;
475  }
476  else if (header.GetDestination () == m_address)
477  {
478  packetType = PACKET_HOST;
479  }
480  else
481  {
482  packetType = PACKET_OTHERHOST;
483  }
484 
485  NS_LOG_LOGIC ("packet type = " << packetType);
486 
487  if (!m_promiscRxCallback.IsNull ())
488  {
489  m_promiscRxCallback (this, packet->Copy (), llc.GetType (), header.GetSource (), header.GetDestination (), packetType);
490  }
491 
492  if (packetType != PACKET_OTHERHOST)
493  {
494  m_rxCallback (this, packet, llc.GetType (), header.GetSource () );
495  }
496 }
497 
498 
499 
500 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
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:1072
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:272
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:62
#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)
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:338
Ptr< const AttributeChecker > MakeMac48AddressChecker(void)
Packet addressed to multicast group.
Definition: net-device.h:278
void SetType(uint16_t type)
Set the Ethertype.
Packet addressed oo us.
Definition: net-device.h:274
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
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:210
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:280
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:980
virtual void SetQueue(Ptr< Queue > queue)
set the queue which is going to be used by this device
virtual bool IsMulticast(void) const
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:84
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:276
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:51
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual uint32_t GetIfIndex(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
virtual void SetNode(Ptr< Node > node)
NetDevice::ReceiveCallback m_rxCallback
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
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.