A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
aloha-noack-net-device.cc
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  {
44  case AlohaNoackNetDevice::IDLE:
45  os << "IDLE";
46  break;
47  case AlohaNoackNetDevice::TX:
48  os << "TX";
49  break;
50  case AlohaNoackNetDevice::RX:
51  os << "RX";
52  break;
53  }
54  return os;
55 }
56 
57 
58 NS_OBJECT_ENSURE_REGISTERED (AlohaNoackNetDevice);
59 
60 TypeId
61 AlohaNoackNetDevice::GetTypeId (void)
62 {
63  static TypeId tid = TypeId ("ns3::AlohaNoackNetDevice")
64  .SetParent<NetDevice> ()
65  .AddConstructor<AlohaNoackNetDevice> ()
66  .AddAttribute ("Address",
67  "The MAC address of this device.",
68  Mac48AddressValue (Mac48Address ("12:34:56:78:90:12")),
69  MakeMac48AddressAccessor (&AlohaNoackNetDevice::m_address),
70  MakeMac48AddressChecker ())
71  .AddAttribute ("Queue",
72  "packets being transmitted get queued here",
73  PointerValue (),
74  MakePointerAccessor (&AlohaNoackNetDevice::m_queue),
75  MakePointerChecker<Queue> ())
76  .AddAttribute ("Mtu", "The Maximum Transmission Unit",
77  UintegerValue (1500),
78  MakeUintegerAccessor (&AlohaNoackNetDevice::SetMtu,
80  MakeUintegerChecker<uint16_t> (1,65535))
81  .AddAttribute ("Phy", "The PHY layer attached to this device.",
82  PointerValue (),
83  MakePointerAccessor (&AlohaNoackNetDevice::GetPhy,
85  MakePointerChecker<Object> ())
86  .AddTraceSource ("MacTx",
87  "Trace source indicating a packet has arrived for transmission by this device",
88  MakeTraceSourceAccessor (&AlohaNoackNetDevice::m_macTxTrace))
89  .AddTraceSource ("MacTxDrop",
90  "Trace source indicating a packet has been dropped by the device before transmission",
91  MakeTraceSourceAccessor (&AlohaNoackNetDevice::m_macTxDropTrace))
92  .AddTraceSource ("MacPromiscRx",
93  "A packet has been received by this device, has been passed up from the physical layer "
94  "and is being forwarded up the local protocol stack. This is a promiscuous trace,",
95  MakeTraceSourceAccessor (&AlohaNoackNetDevice::m_macPromiscRxTrace))
96  .AddTraceSource ("MacRx",
97  "A packet has been received by this device, has been passed up from the physical layer "
98  "and is being forwarded up the local protocol stack. This is a non-promiscuous trace,",
99  MakeTraceSourceAccessor (&AlohaNoackNetDevice::m_macRxTrace))
100  ;
101  return tid;
102 }
103 
104 AlohaNoackNetDevice::AlohaNoackNetDevice ()
105  : m_state (IDLE)
106 {
107  NS_LOG_FUNCTION (this);
108 }
109 
110 AlohaNoackNetDevice::~AlohaNoackNetDevice ()
111 {
112  NS_LOG_FUNCTION (this);
113  m_queue = 0;
114 }
115 
116 void
118 {
119  NS_LOG_FUNCTION (this);
120  m_queue = 0;
121  m_node = 0;
122  m_channel = 0;
123  m_currentPkt = 0;
124  m_phy = 0;
125  m_phyMacTxStartCallback = MakeNullCallback< bool, Ptr<Packet> > ();
127 }
128 
129 
130 void
131 AlohaNoackNetDevice::SetIfIndex (const uint32_t index)
132 {
133  NS_LOG_FUNCTION (index);
134  m_ifIndex = index;
135 }
136 
137 uint32_t
139 {
140  NS_LOG_FUNCTION (this);
141  return m_ifIndex;
142 }
143 
144 bool
146 {
147  NS_LOG_FUNCTION (mtu);
148  m_mtu = mtu;
149  return true;
150 }
151 
152 uint16_t
154 {
155  NS_LOG_FUNCTION (this);
156  return m_mtu;
157 }
158 
159 
160 void
162 {
163  NS_LOG_FUNCTION (q);
164  m_queue = q;
165 }
166 
167 
168 void
170 {
171  NS_LOG_FUNCTION (this);
172  m_address = Mac48Address::ConvertFrom (address);
173 }
174 
175 Address
177 {
178  NS_LOG_FUNCTION (this);
179  return m_address;
180 }
181 
182 bool
184 {
185  NS_LOG_FUNCTION (this);
186  return true;
187 }
188 
189 Address
191 {
192  NS_LOG_FUNCTION (this);
193  return Mac48Address ("ff:ff:ff:ff:ff:ff");
194 }
195 
196 bool
198 {
199  NS_LOG_FUNCTION (this);
200  return true;
201 }
202 
203 Address
205 {
206  NS_LOG_FUNCTION (addr);
208  return ad;
209 }
210 
211 
213 {
214  NS_LOG_FUNCTION (addr);
216  return ad;
217 }
218 
219 
220 bool
222 {
223  NS_LOG_FUNCTION (this);
224  return false;
225 }
226 
227 bool
229 {
230  NS_LOG_FUNCTION (this);
231  return false;
232 }
233 
234 
235 Ptr<Node>
237 {
238  NS_LOG_FUNCTION (this);
239  return m_node;
240 }
241 
242 void
244 {
245  NS_LOG_FUNCTION (node);
246 
247  m_node = node;
248 }
249 
250 void
252 {
253  NS_LOG_FUNCTION (this << phy);
254  m_phy = phy;
255 }
256 
257 
260 {
261  NS_LOG_FUNCTION (this);
262  return m_phy;
263 }
264 
265 
266 void
268 {
269  NS_LOG_FUNCTION (this << c);
270  m_channel = c;
271 }
272 
273 
276 {
277  NS_LOG_FUNCTION (this);
278  return m_channel;
279 }
280 
281 
282 bool
284 {
285  NS_LOG_FUNCTION (this);
286  return true;
287 }
288 
289 bool
291 {
292  NS_LOG_FUNCTION (this);
293  return m_linkUp;
294 }
295 
296 void
298 {
299  NS_LOG_FUNCTION (&callback);
301 }
302 
303 void
305 {
306  NS_LOG_FUNCTION (&cb);
307  m_rxCallback = cb;
308 }
309 
310 void
312 {
313  NS_LOG_FUNCTION (&cb);
314  m_promiscRxCallback = cb;
315 }
316 
317 bool
319 {
320  NS_LOG_FUNCTION (this);
321  return true;
322 }
323 
324 
325 bool
326 AlohaNoackNetDevice::Send (Ptr<Packet> packet,const Address& dest, uint16_t protocolNumber)
327 {
328  NS_LOG_FUNCTION (packet << dest << protocolNumber);
329  return SendFrom (packet, m_address, dest, protocolNumber);
330 }
331 
332 bool
333 AlohaNoackNetDevice::SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber)
334 {
335  NS_LOG_FUNCTION (packet << src << dest << protocolNumber);
336 
337  LlcSnapHeader llc;
338  llc.SetType (protocolNumber);
339  packet->AddHeader (llc);
340 
341  AlohaNoackMacHeader header;
342  header.SetSource (Mac48Address::ConvertFrom (src));
343  header.SetDestination (Mac48Address::ConvertFrom (dest));
344  packet->AddHeader (header);
345 
346  m_macTxTrace (packet);
347 
348 
349  bool sendOk = true;
350  //
351  // If the device is idle, transmission starts immediately. Otherwise,
352  // the transmission will be started by NotifyTransmissionEnd
353  //
354  NS_LOG_LOGIC (this << " state=" << m_state);
355  if (m_state == IDLE)
356  {
357  if (m_queue->IsEmpty ())
358  {
359  NS_LOG_LOGIC ("new packet is head of queue, starting TX immediately");
360  m_currentPkt = packet;
362  }
363  else
364  {
365  NS_LOG_LOGIC ("enqueueing new packet");
366  if (m_queue->Enqueue (packet) == false)
367  {
368  m_macTxDropTrace (packet);
369  sendOk = false;
370  }
371  }
372  }
373  else
374  {
375  NS_LOG_LOGIC ("deferring TX, enqueueing new packet");
376  NS_ASSERT (m_queue);
377  if (m_queue->Enqueue (packet) == false)
378  {
379  m_macTxDropTrace (packet);
380  sendOk = false;
381  }
382  }
383  return sendOk;
384 }
385 
386 void
388 {
389  NS_LOG_FUNCTION (this);
390  m_phyMacTxStartCallback = c;
391 }
392 
393 void
395 {
396  NS_LOG_FUNCTION (this);
397 
398  NS_ASSERT (m_currentPkt != 0);
399  NS_ASSERT (m_state == IDLE);
400 
401  if (m_phyMacTxStartCallback (m_currentPkt))
402  {
403  NS_LOG_WARN ("PHY refused to start TX");
404  }
405  else
406  {
407  m_state = TX;
408  }
409 }
410 
411 
412 
413 void
415 {
416  NS_LOG_FUNCTION (this);
417  NS_ASSERT_MSG (m_state == TX, "TX end notified while state != TX");
418  m_state = IDLE;
419  NS_ASSERT (m_queue);
420  if (m_queue->IsEmpty () == false)
421  {
422  m_currentPkt = m_queue->Dequeue ();
423  NS_ASSERT (m_currentPkt);
424  NS_LOG_LOGIC ("scheduling transmission now");
426  }
427 }
428 
429 
430 void
432 {
433  NS_LOG_FUNCTION (this);
434 }
435 
436 
437 
438 void
440 {
441  NS_LOG_FUNCTION (this);
442 }
443 
444 
445 
446 
447 
448 void
450 {
451  NS_LOG_FUNCTION (this << packet);
452  AlohaNoackMacHeader header;
453  packet->RemoveHeader (header);
454  NS_LOG_LOGIC ("packet " << header.GetSource () << " --> " << header.GetDestination () << " (here: " << m_address << ")");
455 
456  LlcSnapHeader llc;
457  packet->RemoveHeader (llc);
458 
459  PacketType packetType;
460  if (header.GetDestination ().IsBroadcast ())
461  {
462  packetType = PACKET_BROADCAST;
463  }
464  else if (header.GetDestination ().IsGroup ())
465  {
466  packetType = PACKET_MULTICAST;
467  }
468  else if (header.GetDestination () == m_address)
469  {
470  packetType = PACKET_HOST;
471  }
472  else
473  {
474  packetType = PACKET_OTHERHOST;
475  }
476 
477  NS_LOG_LOGIC ("packet type = " << packetType);
478 
479  if (!m_promiscRxCallback.IsNull ())
480  {
481  m_promiscRxCallback (this, packet->Copy (), llc.GetType (), header.GetSource (), header.GetDestination (), packetType);
482  }
483 
484  if (packetType != PACKET_OTHERHOST)
485  {
486  m_rxCallback (this, packet, llc.GetType (), header.GetSource () );
487  }
488 }
489 
490 
491 
492 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Definition: packet.cc:285
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:311
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)
virtual Address GetBroadcast(void) const
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
bool IsBroadcast(void) const
virtual void AddLinkChangeCallback(Callback< void > callback)
virtual void DoDispose(void)
Definition: object.cc:335
a polymophic address class
Definition: address.h:86
virtual Ptr< Node > GetNode(void) const
void NotifyReceptionEndOk(Ptr< Packet > p)
static Mac48Address GetMulticast(Ipv4Address address)
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual uint16_t GetMtu(void) const
virtual bool IsBroadcast(void) const
void NotifyTransmissionEnd(Ptr< const Packet >)
virtual bool SetMtu(const uint16_t mtu)
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
static Mac48Address ConvertFrom(const Address &address)
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
Ptr< Packet > Copy(void) const
Definition: packet.cc:131
virtual bool IsLinkUp(void) const
virtual bool SupportsSendFrom(void) const
void SetChannel(Ptr< Channel > c)
bool IsGroup(void) const
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)
Definition: simulator.h:981
virtual void SetQueue(Ptr< Queue > queue)
virtual bool IsMulticast(void) const
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Describes an IPv6 address.
Definition: ipv6-address.h:44
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ConnectWithoutContext(const CallbackBase &callback)
#define NS_LOG_WARN(msg)
Definition: log.h:246
void SetPhy(Ptr< Object > phy)
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual uint32_t GetIfIndex(void) const
virtual void SetNode(Ptr< Node > node)
void AddHeader(const Header &header)
Definition: packet.cc:270
Header for the LLC/SNAP encapsulation.
virtual Ptr< Channel > GetChannel(void) const
virtual Address GetAddress(void) const
virtual void SetAddress(Address address)