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 "ns3/llc-snap-header.h"
22 #include "ns3/channel.h"
23 #include "ns3/socket.h"
24 #include "ns3/pointer.h"
25 #include "ns3/log.h"
26 #include "ns3/node.h"
27 #include "ns3/net-device-queue-interface.h"
28 #include "wifi-net-device.h"
29 #include "wifi-phy.h"
30 #include "regular-wifi-mac.h"
31 #include "wifi-mac-queue.h"
32 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("WifiNetDevice");
36 
37 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::WifiNetDevice")
43  .SetParent<NetDevice> ()
44  .AddConstructor<WifiNetDevice> ()
45  .SetGroupName ("Wifi")
46  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
50  MakeUintegerChecker<uint16_t> (1,MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH))
51  .AddAttribute ("Channel", "The channel attached to this device",
52  PointerValue (),
54  MakePointerChecker<Channel> ())
55  .AddAttribute ("Phy", "The PHY layer attached to this device.",
56  PointerValue (),
59  MakePointerChecker<WifiPhy> ())
60  .AddAttribute ("Mac", "The MAC layer attached to this device.",
61  PointerValue (),
64  MakePointerChecker<WifiMac> ())
65  .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
66  PointerValue (),
69  MakePointerChecker<WifiRemoteStationManager> ())
70  ;
71  return tid;
72 }
73 
75  : m_configComplete (false)
76 {
78 }
79 
81 {
83 }
84 
85 void
87 {
89  m_node = 0;
90  m_mac->Dispose ();
91  m_phy->Dispose ();
93  m_mac = 0;
94  m_phy = 0;
95  m_stationManager = 0;
96  m_queueInterface = 0;
98 }
99 
100 void
102 {
104  m_phy->Initialize ();
105  m_mac->Initialize ();
108 }
109 
110 void
112 {
113  if (m_mac == 0
114  || m_phy == 0
115  || m_stationManager == 0
116  || m_node == 0
117  || m_configComplete)
118  {
119  return;
120  }
121  m_mac->SetWifiRemoteStationManager (m_stationManager);
122  m_mac->SetWifiPhy (m_phy);
123  m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
124  m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
125  m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
128  m_configComplete = true;
129 }
130 
131 void
133 {
134  NS_LOG_FUNCTION (this);
135  if (m_queueInterface == 0)
136  {
137  Ptr<NetDeviceQueueInterface> ndqi = this->GetObject<NetDeviceQueueInterface> ();
138  //verify that it's a valid netdevice queue interface and that
139  //the netdevice queue interface was not set before
140  if (ndqi != 0)
141  {
142  m_queueInterface = ndqi;
143  // register the select queue callback
144  m_queueInterface->SetSelectQueueCallback (MakeCallback (&WifiNetDevice::SelectQueue, this));
145  m_queueInterface->SetLateTxQueuesCreation (true);
147  }
148  }
150 }
151 
152 void
154 {
155  if (m_mac == 0 || m_queueInterface == 0)
156  {
157  return;
158  }
159 
160  Ptr<RegularWifiMac> mac = DynamicCast<RegularWifiMac> (m_mac);
161  if (mac == 0)
162  {
163  NS_LOG_WARN ("Flow control is only supported by RegularWifiMac");
164  return;
165  }
166 
167  BooleanValue qosSupported;
168  mac->GetAttributeFailSafe ("QosSupported", qosSupported);
169  PointerValue ptr;
170  Ptr<WifiMacQueue> wmq;
171  if (qosSupported.Get ())
172  {
173  m_queueInterface->SetTxQueuesN (4);
174  m_queueInterface->CreateTxQueues ();
175 
176  mac->GetAttributeFailSafe ("BE_Txop", ptr);
177  wmq = ptr.Get<QosTxop> ()->GetWifiMacQueue ();
178  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 0);
179 
180  mac->GetAttributeFailSafe ("BK_Txop", ptr);
181  wmq = ptr.Get<QosTxop> ()->GetWifiMacQueue ();
182  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 1);
183 
184  mac->GetAttributeFailSafe ("VI_Txop", ptr);
185  wmq = ptr.Get<QosTxop> ()->GetWifiMacQueue ();
186  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 2);
187 
188  mac->GetAttributeFailSafe ("VO_Txop", ptr);
189  wmq = ptr.Get<QosTxop> ()->GetWifiMacQueue ();
190  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 3);
191  }
192  else
193  {
194  m_queueInterface->CreateTxQueues ();
195 
196  mac->GetAttributeFailSafe ("Txop", ptr);
197  wmq = ptr.Get<Txop> ()->GetWifiMacQueue ();
198  m_queueInterface->ConnectQueueTraces<WifiMacQueueItem> (wmq, 0);
199  }
200 }
201 
202 void
204 {
205  m_mac = mac;
206  CompleteConfig ();
208 }
209 
210 void
212 {
213  m_phy = phy;
214  CompleteConfig ();
215 }
216 
217 void
219 {
220  m_stationManager = manager;
221  CompleteConfig ();
222 }
223 
226 {
227  return m_mac;
228 }
229 
232 {
233  return m_phy;
234 }
235 
238 {
239  return m_stationManager;
240 }
241 
242 void
243 WifiNetDevice::SetIfIndex (const uint32_t index)
244 {
245  m_ifIndex = index;
246 }
247 
248 uint32_t
250 {
251  return m_ifIndex;
252 }
253 
256 {
257  return m_phy->GetChannel ();
258 }
259 
260 void
262 {
263  m_mac->SetAddress (Mac48Address::ConvertFrom (address));
264 }
265 
266 Address
268 {
269  return m_mac->GetAddress ();
270 }
271 
272 bool
273 WifiNetDevice::SetMtu (const uint16_t mtu)
274 {
276  {
277  return false;
278  }
279  m_mtu = mtu;
280  return true;
281 }
282 
283 uint16_t
285 {
286  return m_mtu;
287 }
288 
289 bool
291 {
292  return m_phy != 0 && m_linkUp;
293 }
294 
295 void
297 {
299 }
300 
301 bool
303 {
304  return true;
305 }
306 
307 Address
309 {
310  return Mac48Address::GetBroadcast ();
311 }
312 
313 bool
315 {
316  return true;
317 }
318 
319 Address
321 {
322  return Mac48Address::GetMulticast (multicastGroup);
323 }
324 
326 {
327  return Mac48Address::GetMulticast (addr);
328 }
329 
330 bool
332 {
333  return false;
334 }
335 
336 bool
338 {
339  return false;
340 }
341 
342 bool
343 WifiNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
344 {
345  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
347 
348  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
349 
350  LlcSnapHeader llc;
351  llc.SetType (protocolNumber);
352  packet->AddHeader (llc);
353 
354  m_mac->NotifyTx (packet);
355  m_mac->Enqueue (packet, realTo);
356  return true;
357 }
358 
359 Ptr<Node>
361 {
362  return m_node;
363 }
364 
365 void
367 {
368  m_node = node;
369  CompleteConfig ();
370 }
371 
372 bool
374 {
375  return true;
376 }
377 
378 void
380 {
381  m_forwardUp = cb;
382 }
383 
384 void
386 {
387  NS_LOG_FUNCTION (this << packet << from << to);
388  LlcSnapHeader llc;
390  if (to.IsBroadcast ())
391  {
393  }
394  else if (to.IsGroup ())
395  {
397  }
398  else if (to == m_mac->GetAddress ())
399  {
400  type = NetDevice::PACKET_HOST;
401  }
402  else
403  {
405  }
406 
407  if (type != NetDevice::PACKET_OTHERHOST)
408  {
409  m_mac->NotifyRx (packet);
410  packet->RemoveHeader (llc);
411  m_forwardUp (this, packet, llc.GetType (), from);
412  }
413  else
414  {
415  packet->RemoveHeader (llc);
416  }
417 
418  if (!m_promiscRx.IsNull ())
419  {
420  m_mac->NotifyPromiscRx (packet);
421  m_promiscRx (this, packet, llc.GetType (), from, to, type);
422  }
423 }
424 
425 void
427 {
428  m_linkUp = true;
429  m_linkChanges ();
430 }
431 
432 void
434 {
435  m_linkUp = false;
436  m_linkChanges ();
437 }
438 
439 bool
440 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
441 {
442  NS_LOG_FUNCTION (this << packet << source << dest << protocolNumber);
445 
446  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
447  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
448 
449  LlcSnapHeader llc;
450  llc.SetType (protocolNumber);
451  packet->AddHeader (llc);
452 
453  m_mac->NotifyTx (packet);
454  m_mac->Enqueue (packet, realTo, realFrom);
455 
456  return true;
457 }
458 
459 void
461 {
462  m_promiscRx = cb;
463  m_mac->SetPromisc ();
464 }
465 
466 bool
468 {
469  return m_mac->SupportsSendFrom ();
470 }
471 
472 uint8_t
474 {
475  NS_LOG_FUNCTION (this << item);
476 
478 
479  if (m_queueInterface->GetNTxQueues () == 1)
480  {
481  return 0;
482  }
483 
484  uint8_t dscp, priority = 0;
485  if (item->GetUint8Value (QueueItem::IP_DSFIELD, dscp))
486  {
487  // if the QoS map element is implemented, it should be used here
488  // to set the priority.
489  // User priority is set to the three most significant bits of the DS field
490  priority = dscp >> 5;
491  }
492 
493  // replace the priority tag
494  SocketPriorityTag priorityTag;
495  priorityTag.SetPriority (priority);
496  item->GetPacket ()->ReplacePacketTag (priorityTag);
497 
498  // if the admission control were implemented, here we should check whether
499  // the access category assigned to the packet should be downgraded
500 
501  return static_cast<uint8_t> (QosUtilsMapTidToAc (priority));
502 }
503 
504 } //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.
Packet addressed to someone else.
Definition: net-device.h:310
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint16_t GetMtu(void) const
Handle packet fragmentation and retransmissions for QoS data frames as well as MSDU aggregation (A-MS...
Definition: qos-txop.h:90
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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
bool IsBroadcast(void) const
Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
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.
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
phy
Definition: third.py:86
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
WifiMacQueueItem stores (const) packets along with their Wifi MAC headers and the time when they were...
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.
static const uint16_t MAX_MSDU_SIZE
This value conforms to the 802.11 specification.
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< WifiMac > m_mac
the MAC
bool SupportsSendFrom(void) const
mac
Definition: third.py:92
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:31
Ptr< WifiPhy > m_phy
the phy
static Mac48Address GetBroadcast(void)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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<T>.
Definition: pointer.h:36
address
Definition: first.py:37
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
bool IsGroup(void) const
Ptr< Node > m_node
the node
uint32_t GetIfIndex(void) const
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).
Packet addressed oo us.
Definition: net-device.h:304
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:262
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)
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
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:302
void LinkDown(void)
Set that the link is down (i.e.
void SetPriority(uint8_t priority)
Set the tag&#39;s priority.
Definition: socket.cc:848
Ptr< NetDeviceQueueInterface > m_queueInterface
NetDevice queue interface.
Packet addressed to multicast group.
Definition: net-device.h:308
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:915
Packet addressed to all.
Definition: net-device.h:306
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)
Handle packet fragmentation and retransmissions for data and management frames.
Definition: txop.h:64