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 {
101  m_phy->Initialize ();
102  m_mac->Initialize ();
105 }
106 
107 void
109 {
110  if (m_mac == 0
111  || m_phy == 0
112  || m_stationManager == 0
113  || m_node == 0
114  || m_configComplete)
115  {
116  return;
117  }
118  m_mac->SetWifiRemoteStationManager (m_stationManager);
119  m_mac->SetWifiPhy (m_phy);
120  m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
121  m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
122  m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
125  m_configComplete = true;
126 }
127 
128 void
130 {
131  NS_LOG_FUNCTION (this);
132  if (m_queueInterface == 0)
133  {
134  Ptr<NetDeviceQueueInterface> ndqi = this->GetObject<NetDeviceQueueInterface> ();
135  //verify that it's a valid netdevice queue interface and that
136  //the netdevice queue interface was not set before
137  if (ndqi != 0)
138  {
139  m_queueInterface = ndqi;
140  // register the select queue callback
141  m_queueInterface->SetSelectQueueCallback (MakeCallback (&WifiNetDevice::SelectQueue, this));
142  m_queueInterface->SetLateTxQueuesCreation (true);
144  }
145  }
147 }
148 
149 void
151 {
152  if (m_mac == 0 || m_queueInterface == 0)
153  {
154  return;
155  }
156 
157  Ptr<RegularWifiMac> mac = DynamicCast<RegularWifiMac> (m_mac);
158  if (mac == 0)
159  {
160  NS_LOG_WARN ("Flow control is only supported by RegularWifiMac");
161  return;
162  }
163 
164  BooleanValue qosSupported;
165  mac->GetAttributeFailSafe ("QosSupported", qosSupported);
166  PointerValue ptr;
167  Ptr<WifiMacQueue> wmq;
168  if (qosSupported.Get ())
169  {
170  m_queueInterface->SetTxQueuesN (4);
171  m_queueInterface->CreateTxQueues ();
172 
173  mac->GetAttributeFailSafe ("BE_EdcaTxopN", ptr);
174  wmq = ptr.Get<EdcaTxopN> ()->GetQueue ();
175  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 0);
176 
177  mac->GetAttributeFailSafe ("BK_EdcaTxopN", ptr);
178  wmq = ptr.Get<EdcaTxopN> ()->GetQueue ();
179  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 1);
180 
181  mac->GetAttributeFailSafe ("VI_EdcaTxopN", ptr);
182  wmq = ptr.Get<EdcaTxopN> ()->GetQueue ();
183  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 2);
184 
185  mac->GetAttributeFailSafe ("VO_EdcaTxopN", ptr);
186  wmq = ptr.Get<EdcaTxopN> ()->GetQueue ();
187  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 3);
188  }
189  else
190  {
191  m_queueInterface->CreateTxQueues ();
192 
193  mac->GetAttributeFailSafe ("DcaTxop", ptr);
194  wmq = ptr.Get<DcaTxop> ()->GetQueue ();
195  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 0);
196  }
197 }
198 
199 void
201 {
202  m_mac = mac;
203  CompleteConfig ();
205 }
206 
207 void
209 {
210  m_phy = phy;
211  CompleteConfig ();
212 }
213 
214 void
216 {
217  m_stationManager = manager;
218  CompleteConfig ();
219 }
220 
223 {
224  return m_mac;
225 }
226 
229 {
230  return m_phy;
231 }
232 
235 {
236  return m_stationManager;
237 }
238 
239 void
240 WifiNetDevice::SetIfIndex (const uint32_t index)
241 {
242  m_ifIndex = index;
243 }
244 
245 uint32_t
247 {
248  return m_ifIndex;
249 }
250 
253 {
254  return m_phy->GetChannel ();
255 }
256 
259 {
260  return m_phy->GetChannel ();
261 }
262 
263 void
265 {
266  m_mac->SetAddress (Mac48Address::ConvertFrom (address));
267 }
268 
269 Address
271 {
272  return m_mac->GetAddress ();
273 }
274 
275 bool
276 WifiNetDevice::SetMtu (const uint16_t mtu)
277 {
279  {
280  return false;
281  }
282  m_mtu = mtu;
283  return true;
284 }
285 
286 uint16_t
288 {
289  return m_mtu;
290 }
291 
292 bool
294 {
295  return m_phy != 0 && m_linkUp;
296 }
297 
298 void
300 {
302 }
303 
304 bool
306 {
307  return true;
308 }
309 
310 Address
312 {
313  return Mac48Address::GetBroadcast ();
314 }
315 
316 bool
318 {
319  return true;
320 }
321 
322 Address
324 {
325  return Mac48Address::GetMulticast (multicastGroup);
326 }
327 
329 {
330  return Mac48Address::GetMulticast (addr);
331 }
332 
333 bool
335 {
336  return false;
337 }
338 
339 bool
341 {
342  return false;
343 }
344 
345 bool
346 WifiNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
347 {
348  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
350 
351  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
352 
353  LlcSnapHeader llc;
354  llc.SetType (protocolNumber);
355  packet->AddHeader (llc);
356 
357  m_mac->NotifyTx (packet);
358  m_mac->Enqueue (packet, realTo);
359  return true;
360 }
361 
362 Ptr<Node>
364 {
365  return m_node;
366 }
367 
368 void
370 {
371  m_node = node;
372  CompleteConfig ();
373 }
374 
375 bool
377 {
378  return true;
379 }
380 
381 void
383 {
384  m_forwardUp = cb;
385 }
386 
387 void
389 {
390  NS_LOG_FUNCTION (this << packet << from << to);
391  LlcSnapHeader llc;
393  if (to.IsBroadcast ())
394  {
396  }
397  else if (to.IsGroup ())
398  {
400  }
401  else if (to == m_mac->GetAddress ())
402  {
403  type = NetDevice::PACKET_HOST;
404  }
405  else
406  {
408  }
409 
410  if (type != NetDevice::PACKET_OTHERHOST)
411  {
412  m_mac->NotifyRx (packet);
413  packet->RemoveHeader (llc);
414  m_forwardUp (this, packet, llc.GetType (), from);
415  }
416  else
417  {
418  packet->RemoveHeader (llc);
419  }
420 
421  if (!m_promiscRx.IsNull ())
422  {
423  m_mac->NotifyPromiscRx (packet);
424  m_promiscRx (this, packet, llc.GetType (), from, to, type);
425  }
426 }
427 
428 void
430 {
431  m_linkUp = true;
432  m_linkChanges ();
433 }
434 
435 void
437 {
438  m_linkUp = false;
439  m_linkChanges ();
440 }
441 
442 bool
443 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
444 {
445  NS_LOG_FUNCTION (this << packet << source << dest << protocolNumber);
448 
449  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
450  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
451 
452  LlcSnapHeader llc;
453  llc.SetType (protocolNumber);
454  packet->AddHeader (llc);
455 
456  m_mac->NotifyTx (packet);
457  m_mac->Enqueue (packet, realTo, realFrom);
458 
459  return true;
460 }
461 
462 void
464 {
465  m_promiscRx = cb;
466  m_mac->SetPromisc ();
467 }
468 
469 bool
471 {
472  return m_mac->SupportsSendFrom ();
473 }
474 
475 uint8_t
477 {
478  NS_LOG_FUNCTION (this << item);
479 
481 
482  if (m_queueInterface->GetNTxQueues () == 1)
483  {
484  return 0;
485  }
486 
487  uint8_t dscp, priority = 0;
488  if (item->GetUint8Value (QueueItem::IP_DSFIELD, dscp))
489  {
490  // if the QoS map element is implemented, it should be used here
491  // to set the priority.
492  // User priority is set to the three most significant bits of the DS field
493  priority = dscp >> 5;
494  }
495 
496  // replace the priority tag
497  SocketPriorityTag priorityTag;
498  priorityTag.SetPriority (priority);
499  item->GetPacket ()->ReplacePacketTag (priorityTag);
500 
501  // if the admission control were implemented, here we should check whether
502  // the access category assigned to the packet should be downgraded
503 
504  return QosUtilsMapTidToAc (priority);
505 }
506 
507 } //namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:267
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:48
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).
Ptr< Channel > DoGetChannel(void) const
Return the Channel this device is connected to.
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)