A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("AlohaNoackNetDevice");
35 
36 
37 namespace ns3 {
38 
39 
40 std::ostream& operator<< (std::ostream& os, AlohaNoackNetDevice::State state)
41 {
42  switch (state)
43  {
45  os << "IDLE";
46  break;
48  os << "TX";
49  break;
51  os << "RX";
52  break;
53  }
54  return os;
55 }
56 
57 
58 NS_OBJECT_ENSURE_REGISTERED (AlohaNoackNetDevice)
59  ;
60 
61 TypeId
63 {
64  static TypeId tid = TypeId ("ns3::AlohaNoackNetDevice")
65  .SetParent<NetDevice> ()
66  .AddConstructor<AlohaNoackNetDevice> ()
67  .AddAttribute ("Address",
68  "The MAC address of this device.",
69  Mac48AddressValue (Mac48Address ("12:34:56:78:90:12")),
70  MakeMac48AddressAccessor (&AlohaNoackNetDevice::m_address),
71  MakeMac48AddressChecker ())
72  .AddAttribute ("Queue",
73  "packets being transmitted get queued here",
74  PointerValue (),
75  MakePointerAccessor (&AlohaNoackNetDevice::m_queue),
76  MakePointerChecker<Queue> ())
77  .AddAttribute ("Mtu", "The Maximum Transmission Unit",
78  UintegerValue (1500),
79  MakeUintegerAccessor (&AlohaNoackNetDevice::SetMtu,
81  MakeUintegerChecker<uint16_t> (1,65535))
82  .AddAttribute ("Phy", "The PHY layer attached to this device.",
83  PointerValue (),
84  MakePointerAccessor (&AlohaNoackNetDevice::GetPhy,
86  MakePointerChecker<Object> ())
87  .AddTraceSource ("MacTx",
88  "Trace source indicating a packet has arrived for transmission by this device",
90  .AddTraceSource ("MacTxDrop",
91  "Trace source indicating a packet has been dropped by the device before transmission",
93  .AddTraceSource ("MacPromiscRx",
94  "A packet has been received by this device, has been passed up from the physical layer "
95  "and is being forwarded up the local protocol stack. This is a promiscuous trace,",
97  .AddTraceSource ("MacRx",
98  "A packet has been received by this device, has been passed up from the physical layer "
99  "and is being forwarded up the local protocol stack. This is a non-promiscuous trace,",
101  ;
102  return tid;
103 }
104 
106  : m_state (IDLE)
107 {
108  NS_LOG_FUNCTION (this);
109 }
110 
112 {
113  NS_LOG_FUNCTION (this);
114  m_queue = 0;
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION (this);
121  m_queue = 0;
122  m_node = 0;
123  m_channel = 0;
124  m_currentPkt = 0;
125  m_phy = 0;
126  m_phyMacTxStartCallback = MakeNullCallback< bool, Ptr<Packet> > ();
128 }
129 
130 
131 void
132 AlohaNoackNetDevice::SetIfIndex (const uint32_t index)
133 {
134  NS_LOG_FUNCTION (index);
135  m_ifIndex = index;
136 }
137 
138 uint32_t
140 {
141  NS_LOG_FUNCTION (this);
142  return m_ifIndex;
143 }
144 
145 bool
147 {
148  NS_LOG_FUNCTION (mtu);
149  m_mtu = mtu;
150  return true;
151 }
152 
153 uint16_t
155 {
156  NS_LOG_FUNCTION (this);
157  return m_mtu;
158 }
159 
160 
161 void
163 {
164  NS_LOG_FUNCTION (q);
165  m_queue = q;
166 }
167 
168 
169 void
171 {
172  NS_LOG_FUNCTION (this);
174 }
175 
176 Address
178 {
179  NS_LOG_FUNCTION (this);
180  return m_address;
181 }
182 
183 bool
185 {
186  NS_LOG_FUNCTION (this);
187  return true;
188 }
189 
190 Address
192 {
193  NS_LOG_FUNCTION (this);
194  return Mac48Address ("ff:ff:ff:ff:ff:ff");
195 }
196 
197 bool
199 {
200  NS_LOG_FUNCTION (this);
201  return true;
202 }
203 
204 Address
206 {
207  NS_LOG_FUNCTION (addr);
209  return ad;
210 }
211 
212 
214 {
215  NS_LOG_FUNCTION (addr);
217  return ad;
218 }
219 
220 
221 bool
223 {
224  NS_LOG_FUNCTION (this);
225  return false;
226 }
227 
228 bool
230 {
231  NS_LOG_FUNCTION (this);
232  return false;
233 }
234 
235 
236 Ptr<Node>
238 {
239  NS_LOG_FUNCTION (this);
240  return m_node;
241 }
242 
243 void
245 {
246  NS_LOG_FUNCTION (node);
247 
248  m_node = node;
249 }
250 
251 void
253 {
254  NS_LOG_FUNCTION (this << phy);
255  m_phy = phy;
256 }
257 
258 
261 {
262  NS_LOG_FUNCTION (this);
263  return m_phy;
264 }
265 
266 
267 void
269 {
270  NS_LOG_FUNCTION (this << c);
271  m_channel = c;
272 }
273 
274 
277 {
278  NS_LOG_FUNCTION (this);
279  return m_channel;
280 }
281 
282 
283 bool
285 {
286  NS_LOG_FUNCTION (this);
287  return true;
288 }
289 
290 bool
292 {
293  NS_LOG_FUNCTION (this);
294  return m_linkUp;
295 }
296 
297 void
299 {
300  NS_LOG_FUNCTION (&callback);
302 }
303 
304 void
306 {
307  NS_LOG_FUNCTION (&cb);
308  m_rxCallback = cb;
309 }
310 
311 void
313 {
314  NS_LOG_FUNCTION (&cb);
315  m_promiscRxCallback = cb;
316 }
317 
318 bool
320 {
321  NS_LOG_FUNCTION (this);
322  return true;
323 }
324 
325 
326 bool
327 AlohaNoackNetDevice::Send (Ptr<Packet> packet,const Address& dest, uint16_t protocolNumber)
328 {
329  NS_LOG_FUNCTION (packet << dest << protocolNumber);
330  return SendFrom (packet, m_address, dest, protocolNumber);
331 }
332 
333 bool
334 AlohaNoackNetDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, 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;
343  header.SetSource (Mac48Address::ConvertFrom (src));
345  packet->AddHeader (header);
346 
347  m_macTxTrace (packet);
348 
349 
350  bool sendOk = true;
351  //
352  // If the device is idle, transmission starts immediately. Otherwise,
353  // the transmission will be started by NotifyTransmissionEnd
354  //
355  NS_LOG_LOGIC (this << " state=" << m_state);
356  if (m_state == IDLE)
357  {
358  if (m_queue->IsEmpty ())
359  {
360  NS_LOG_LOGIC ("new packet is head of queue, starting TX immediately");
361  m_currentPkt = packet;
363  }
364  else
365  {
366  NS_LOG_LOGIC ("enqueueing new packet");
367  if (m_queue->Enqueue (packet) == false)
368  {
369  m_macTxDropTrace (packet);
370  sendOk = false;
371  }
372  }
373  }
374  else
375  {
376  NS_LOG_LOGIC ("deferring TX, enqueueing new packet");
377  NS_ASSERT (m_queue);
378  if (m_queue->Enqueue (packet) == false)
379  {
380  m_macTxDropTrace (packet);
381  sendOk = false;
382  }
383  }
384  return sendOk;
385 }
386 
387 void
389 {
390  NS_LOG_FUNCTION (this);
392 }
393 
394 void
396 {
397  NS_LOG_FUNCTION (this);
398 
399  NS_ASSERT (m_currentPkt != 0);
400  NS_ASSERT (m_state == IDLE);
401 
403  {
404  NS_LOG_WARN ("PHY refused to start TX");
405  }
406  else
407  {
408  m_state = TX;
409  }
410 }
411 
412 
413 
414 void
416 {
417  NS_LOG_FUNCTION (this);
418  NS_ASSERT_MSG (m_state == TX, "TX end notified while state != TX");
419  m_state = IDLE;
420  NS_ASSERT (m_queue);
421  if (m_queue->IsEmpty () == false)
422  {
423  m_currentPkt = m_queue->Dequeue ();
425  NS_LOG_LOGIC ("scheduling transmission now");
427  }
428 }
429 
430 
431 void
433 {
434  NS_LOG_FUNCTION (this);
435 }
436 
437 
438 
439 void
441 {
442  NS_LOG_FUNCTION (this);
443 }
444 
445 
446 
447 
448 
449 void
451 {
452  NS_LOG_FUNCTION (this << packet);
453  AlohaNoackMacHeader header;
454  packet->RemoveHeader (header);
455  NS_LOG_LOGIC ("packet " << header.GetSource () << " --> " << header.GetDestination () << " (here: " << m_address << ")");
456 
457  LlcSnapHeader llc;
458  packet->RemoveHeader (llc);
459 
460  PacketType packetType;
461  if (header.GetDestination ().IsBroadcast ())
462  {
463  packetType = PACKET_BROADCAST;
464  }
465  else if (header.GetDestination ().IsGroup ())
466  {
467  packetType = PACKET_MULTICAST;
468  }
469  else if (header.GetDestination () == m_address)
470  {
471  packetType = PACKET_HOST;
472  }
473  else
474  {
475  packetType = PACKET_OTHERHOST;
476  }
477 
478  NS_LOG_LOGIC ("packet type = " << packetType);
479 
480  if (!m_promiscRxCallback.IsNull ())
481  {
482  m_promiscRxCallback (this, packet->Copy (), llc.GetType (), header.GetSource (), header.GetDestination (), packetType);
483  }
484 
485  if (packetType != PACKET_OTHERHOST)
486  {
487  m_rxCallback (this, packet, llc.GetType (), header.GetSource () );
488  }
489 }
490 
491 
492 
493 } // 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:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Packet addressed to someone else.
Definition: net-device.h:278
void NotifyReceptionStart()
Notify the MAC that the PHY has started a reception.
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:1014
#define NS_ASSERT(condition)
Definition: assert.h:64
TracedCallback< Ptr< const Packet > > m_macTxDropTrace
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
Channel is IDLE, no packet is being transmitted.
Definition: csma-channel.h:62
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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
void SetType(uint16_t type)
TracedCallback m_linkChangeCallbacks
List of callbacks to fire if the link changes state (up or down).
a polymophic address class
Definition: address.h:86
virtual Ptr< Node > GetNode(void) const
void NotifyReceptionEndOk(Ptr< Packet > p)
Notify the MAC that the PHY finished a reception successfully.
GenericPhyTxStartCallback m_phyMacTxStartCallback
static Mac48Address GetMulticast(Ipv4Address address)
void SetDestination(Mac48Address destination)
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
Hold an unsigned integer type.
Definition: uinteger.h:46
virtual uint16_t GetMtu(void) const
virtual bool IsBroadcast(void) const
Doxygen 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)
Definition: log.h:368
static Mac48Address ConvertFrom(const Address &address)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
TracedCallback< Ptr< const Packet > > m_macRxTrace
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
Ptr< Packet > Copy(void) const
Definition: packet.cc:122
virtual bool IsLinkUp(void) const
hold objects of type Ptr
Definition: pointer.h:33
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
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual bool NeedsArp(void) const
an EUI-48 address
Definition: mac48-address.h:41
Ptr< Object > GetPhy() const
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
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)
Definition: assert.h:86
void NotifyReceptionEndError()
Notify the MAC that the PHY finished a reception with an error.
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ConnectWithoutContext(const CallbackBase &callback)
Packet addressed oo us.
Definition: net-device.h:272
Network layer to device interface.
Definition: net-device.h:75
#define NS_LOG_WARN(msg)
Definition: log.h:280
hold objects of type ns3::Mac48Address
void SetPhy(Ptr< Object > phy)
Set the Phy object which is attached to this device.
Mac48Address GetSource() const
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
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:270
NS_LOG_COMPONENT_DEFINE("AlohaNoackNetDevice")
Packet addressed to multicast group.
Definition: net-device.h:276
a unique identifier for an interface.
Definition: type-id.h:49
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual uint32_t GetIfIndex(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
Packet addressed to all.
Definition: net-device.h:274
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.