A Discrete-Event Network Simulator
API
wifi-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) 2005,2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "wifi-net-device.h"
22 #include "wifi-phy.h"
23 #include "regular-wifi-mac.h"
24 #include "wifi-mac-queue.h"
25 #include "ns3/llc-snap-header.h"
26 #include "ns3/socket.h"
27 #include "ns3/pointer.h"
28 #include "ns3/log.h"
29 #include "ns3/net-device-queue-interface.h"
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("WifiNetDevice");
34 
35 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::WifiNetDevice")
41  .SetParent<NetDevice> ()
42  .AddConstructor<WifiNetDevice> ()
43  .SetGroupName ("Wifi")
44  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
48  MakeUintegerChecker<uint16_t> (1,MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH))
49  .AddAttribute ("Channel", "The channel attached to this device",
50  PointerValue (),
52  MakePointerChecker<Channel> ())
53  .AddAttribute ("Phy", "The PHY layer attached to this device.",
54  PointerValue (),
57  MakePointerChecker<WifiPhy> ())
58  .AddAttribute ("Mac", "The MAC layer attached to this device.",
59  PointerValue (),
62  MakePointerChecker<WifiMac> ())
63  .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
64  PointerValue (),
67  MakePointerChecker<WifiRemoteStationManager> ())
68  ;
69  return tid;
70 }
71 
73  : m_configComplete (false)
74 {
76 }
77 
79 {
81 }
82 
83 void
85 {
87  m_node = 0;
88  m_mac->Dispose ();
89  m_phy->Dispose ();
91  m_mac = 0;
92  m_phy = 0;
93  m_stationManager = 0;
94  m_queueInterface = 0;
96 }
97 
98 void
100 {
102  m_phy->Initialize ();
103  m_mac->Initialize ();
106 }
107 
108 void
110 {
111  if (m_mac == 0
112  || m_phy == 0
113  || m_stationManager == 0
114  || m_node == 0
115  || m_configComplete)
116  {
117  return;
118  }
119  m_mac->SetWifiRemoteStationManager (m_stationManager);
120  m_mac->SetWifiPhy (m_phy);
121  m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
122  m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
123  m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
126  m_configComplete = true;
127 }
128 
129 void
131 {
132  NS_LOG_FUNCTION (this);
133  if (m_queueInterface == 0)
134  {
135  Ptr<NetDeviceQueueInterface> ndqi = this->GetObject<NetDeviceQueueInterface> ();
136  //verify that it's a valid netdevice queue interface and that
137  //the netdevice queue interface was not set before
138  if (ndqi != 0)
139  {
140  m_queueInterface = ndqi;
141  // register the select queue callback
142  m_queueInterface->SetSelectQueueCallback (MakeCallback (&WifiNetDevice::SelectQueue, this));
143  m_queueInterface->SetLateTxQueuesCreation (true);
145  }
146  }
148 }
149 
150 void
152 {
153  if (m_mac == 0 || m_queueInterface == 0)
154  {
155  return;
156  }
157 
158  Ptr<RegularWifiMac> mac = DynamicCast<RegularWifiMac> (m_mac);
159  if (mac == 0)
160  {
161  NS_LOG_WARN ("Flow control is only supported by RegularWifiMac");
162  return;
163  }
164 
165  BooleanValue qosSupported;
166  mac->GetAttributeFailSafe ("QosSupported", qosSupported);
167  PointerValue ptr;
168  Ptr<WifiMacQueue> wmq;
169  if (qosSupported.Get ())
170  {
171  m_queueInterface->SetTxQueuesN (4);
172  m_queueInterface->CreateTxQueues ();
173 
174  mac->GetAttributeFailSafe ("BE_EdcaTxopN", ptr);
175  wmq = ptr.Get<EdcaTxopN> ()->GetQueue ();
176  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 0);
177 
178  mac->GetAttributeFailSafe ("BK_EdcaTxopN", ptr);
179  wmq = ptr.Get<EdcaTxopN> ()->GetQueue ();
180  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 1);
181 
182  mac->GetAttributeFailSafe ("VI_EdcaTxopN", ptr);
183  wmq = ptr.Get<EdcaTxopN> ()->GetQueue ();
184  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 2);
185 
186  mac->GetAttributeFailSafe ("VO_EdcaTxopN", ptr);
187  wmq = ptr.Get<EdcaTxopN> ()->GetQueue ();
188  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 3);
189  }
190  else
191  {
192  m_queueInterface->CreateTxQueues ();
193 
194  mac->GetAttributeFailSafe ("DcaTxop", ptr);
195  wmq = ptr.Get<DcaTxop> ()->GetQueue ();
196  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 0);
197  }
198 }
199 
200 void
202 {
203  m_mac = mac;
204  CompleteConfig ();
206 }
207 
208 void
210 {
211  m_phy = phy;
212  CompleteConfig ();
213 }
214 
215 void
217 {
218  m_stationManager = manager;
219  CompleteConfig ();
220 }
221 
224 {
225  return m_mac;
226 }
227 
230 {
231  return m_phy;
232 }
233 
236 {
237  return m_stationManager;
238 }
239 
240 void
241 WifiNetDevice::SetIfIndex (const uint32_t index)
242 {
243  m_ifIndex = index;
244 }
245 
246 uint32_t
248 {
249  return m_ifIndex;
250 }
251 
254 {
255  return m_phy->GetChannel ();
256 }
257 
258 void
260 {
261  m_mac->SetAddress (Mac48Address::ConvertFrom (address));
262 }
263 
264 Address
266 {
267  return m_mac->GetAddress ();
268 }
269 
270 bool
271 WifiNetDevice::SetMtu (const uint16_t mtu)
272 {
274  {
275  return false;
276  }
277  m_mtu = mtu;
278  return true;
279 }
280 
281 uint16_t
283 {
284  return m_mtu;
285 }
286 
287 bool
289 {
290  return m_phy != 0 && m_linkUp;
291 }
292 
293 void
295 {
297 }
298 
299 bool
301 {
302  return true;
303 }
304 
305 Address
307 {
308  return Mac48Address::GetBroadcast ();
309 }
310 
311 bool
313 {
314  return true;
315 }
316 
317 Address
319 {
320  return Mac48Address::GetMulticast (multicastGroup);
321 }
322 
324 {
325  return Mac48Address::GetMulticast (addr);
326 }
327 
328 bool
330 {
331  return false;
332 }
333 
334 bool
336 {
337  return false;
338 }
339 
340 bool
341 WifiNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
342 {
343  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
345 
346  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
347 
348  LlcSnapHeader llc;
349  llc.SetType (protocolNumber);
350  packet->AddHeader (llc);
351 
352  m_mac->NotifyTx (packet);
353  m_mac->Enqueue (packet, realTo);
354  return true;
355 }
356 
357 Ptr<Node>
359 {
360  return m_node;
361 }
362 
363 void
365 {
366  m_node = node;
367  CompleteConfig ();
368 }
369 
370 bool
372 {
373  return true;
374 }
375 
376 void
378 {
379  m_forwardUp = cb;
380 }
381 
382 void
384 {
385  NS_LOG_FUNCTION (this << packet << from << to);
386  LlcSnapHeader llc;
388  if (to.IsBroadcast ())
389  {
391  }
392  else if (to.IsGroup ())
393  {
395  }
396  else if (to == m_mac->GetAddress ())
397  {
398  type = NetDevice::PACKET_HOST;
399  }
400  else
401  {
403  }
404 
405  if (type != NetDevice::PACKET_OTHERHOST)
406  {
407  m_mac->NotifyRx (packet);
408  packet->RemoveHeader (llc);
409  m_forwardUp (this, packet, llc.GetType (), from);
410  }
411  else
412  {
413  packet->RemoveHeader (llc);
414  }
415 
416  if (!m_promiscRx.IsNull ())
417  {
418  m_mac->NotifyPromiscRx (packet);
419  m_promiscRx (this, packet, llc.GetType (), from, to, type);
420  }
421 }
422 
423 void
425 {
426  m_linkUp = true;
427  m_linkChanges ();
428 }
429 
430 void
432 {
433  m_linkUp = false;
434  m_linkChanges ();
435 }
436 
437 bool
438 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
439 {
440  NS_LOG_FUNCTION (this << packet << source << dest << protocolNumber);
443 
444  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
445  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
446 
447  LlcSnapHeader llc;
448  llc.SetType (protocolNumber);
449  packet->AddHeader (llc);
450 
451  m_mac->NotifyTx (packet);
452  m_mac->Enqueue (packet, realTo, realFrom);
453 
454  return true;
455 }
456 
457 void
459 {
460  m_promiscRx = cb;
461  m_mac->SetPromisc ();
462 }
463 
464 bool
466 {
467  return m_mac->SupportsSendFrom ();
468 }
469 
470 uint8_t
472 {
473  NS_LOG_FUNCTION (this << item);
474 
476 
477  if (m_queueInterface->GetNTxQueues () == 1)
478  {
479  return 0;
480  }
481 
482  uint8_t dscp, priority = 0;
483  if (item->GetUint8Value (QueueItem::IP_DSFIELD, dscp))
484  {
485  // if the QoS map element is implemented, it should be used here
486  // to set the priority.
487  // User priority is set to the three most significant bits of the DS field
488  priority = dscp >> 5;
489  }
490 
491  // replace the priority tag
492  SocketPriorityTag priorityTag;
493  priorityTag.SetPriority (priority);
494  item->GetPacket ()->ReplacePacketTag (priorityTag);
495 
496  // if the admission control were implemented, here we should check whether
497  // the access category assigned to the packet should be downgraded
498 
499  return QosUtilsMapTidToAc (priority);
500 }
501 
502 } //namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
bool IsBridge(void) const
Return true if the net device is acting as a bridge.
static bool IsMatchingType(const Address &address)
bool Get(void) const
Definition: boolean.cc:51
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
uint8_t SelectQueue(Ptr< QueueItem > item) const
Determine the tx queue for a given packet.
bool m_linkUp
link up
bool SetMtu(const uint16_t mtu)
Ptr< T > Get(void) const
Definition: pointer.h:194
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 "...
AttributeValue implementation for Boolean.
Definition: boolean.h:36
void LinkUp(void)
Set that the link is up.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint16_t GetMtu(void) const
Address GetAddress(void) const
Ptr< WifiRemoteStationManager > m_stationManager
the station manager
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
#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:296
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
bool IsBroadcast(void) const
Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Packet addressed to multicast group.
Definition: net-device.h:302
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
void SetType(uint16_t type)
Set the Ethertype.
Packet addressed oo us.
Definition: net-device.h:298
void ForwardUp(Ptr< Packet > packet, Mac48Address from, Mac48Address to)
Receive a packet from the lower layer and pass the packet up the stack.
Ptr< Channel > GetChannel(void) const
bool IsBroadcast(void) const
a polymophic address class
Definition: address.h:90
virtual Ptr< Channel > GetChannel(void) const =0
Return the Channel this WifiPhy is connected to.
uint16_t m_mtu
MTU.
bool IsMulticast(void) const
Ptr< WifiPhy > GetPhy(void) const
This queue contains packets for a particular access class.
Definition: edca-txop-n.h:68
WifiMacQueueItem stores (const) packets along with their Wifi MAC headers and the time when they were...
tuple phy
Definition: third.py:86
void DoDispose(void)
Destructor implementation.
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
Ptr< WifiRemoteStationManager > GetRemoteStationManager(void) const
static Mac48Address GetMulticast(Ipv4Address address)
void SetMac(const Ptr< WifiMac > mac)
uint16_t GetType(void)
Return the Ethertype.
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< WifiMac > m_mac
the MAC
bool SupportsSendFrom(void) const
indicates whether the socket has a priority set.
Definition: socket.h:1303
AcIndex QosUtilsMapTidToAc(uint8_t tid)
Maps TID (Traffic ID) to Access classes.
Definition: qos-utils.cc:28
Ptr< WifiPhy > m_phy
the phy
static Mac48Address GetBroadcast(void)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
tuple mac
Definition: third.py:92
virtual void SetupPhy(const Ptr< WifiPhy > phy)
Set up PHY associated with this device since it is the object that knows the full set of transmit rat...
static Mac48Address ConvertFrom(const Address &address)
static const uint16_t LLC_SNAP_HEADER_LENGTH
The length in octects of the LLC/SNAP header.
Ptr< Node > GetNode(void) const
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TracedCallback m_linkChanges
link change callback
Hold objects of type Ptr.
Definition: pointer.h:36
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
bool IsGroup(void) const
Packet addressed to someone else.
Definition: net-device.h:304
Ptr< Node > m_node
the node
uint32_t GetIfIndex(void) const
static const uint16_t MAX_MSDU_SIZE
This value conforms to the 802.11 specification.
void FlowControlConfig(void)
Perform the actions needed to support flow control and dynamic queue limits.
an EUI-48 address
Definition: mac48-address.h:43
void DoInitialize(void)
Initialize() implementation.
bool NeedsArp(void) const
uint32_t m_ifIndex
IF index.
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Address GetBroadcast(void) const
Describes an IPv6 address.
Definition: ipv6-address.h:49
void SetPhy(const Ptr< WifiPhy > phy)
virtual void SetupMac(const Ptr< WifiMac > mac)
Set up MAC associated with this device since it is the object that knows the full set of timing param...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void SetIfIndex(const uint32_t index)
NetDevice::ReceiveCallback m_forwardUp
forward up callback
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Network layer to device interface.
Definition: net-device.h:95
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
Packet addressed to all.
Definition: net-device.h:300
bool IsLinkUp(void) const
void AddLinkChangeCallback(Callback< void > callback)
void CompleteConfig(void)
Complete the configuration of this Wi-Fi device by connecting all lower components (e...
Ptr< WifiMac > GetMac(void) const
void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
tuple address
Definition: first.py:37
bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:325
void LinkDown(void)
Set that the link is down (i.e.
handle packet fragmentation and retransmissions.
Definition: dca-txop.h:58
void SetPriority(uint8_t priority)
Set the tag's priority.
Definition: socket.cc:848
Ptr< NetDeviceQueueInterface > m_queueInterface
NetDevice queue interface.
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
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
void SetReceiveCallback(NetDevice::ReceiveCallback cb)
static TypeId GetTypeId(void)
Get the type ID.
NetDevice::PromiscReceiveCallback m_promiscRx
promiscious receive callback
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
bool m_configComplete
configuration complete
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
void SetNode(const Ptr< Node > node)
void SetAddress(Address address)
Set the address of this interface.
Header for the LLC/SNAP encapsulation.
void SetRemoteStationManager(const Ptr< WifiRemoteStationManager > manager)