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 "regular-wifi-mac.h"
23 #include "wifi-phy.h"
25 #include "wifi-channel.h"
26 #include "qos-utils.h"
27 #include "ns3/llc-snap-header.h"
28 #include "ns3/packet.h"
29 #include "ns3/socket.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/pointer.h"
32 #include "ns3/node.h"
33 #include "ns3/trace-source-accessor.h"
34 #include "ns3/log.h"
35 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("WifiNetDevice");
39 
40 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::WifiNetDevice")
46  .SetParent<NetDevice> ()
47  .AddConstructor<WifiNetDevice> ()
48  .SetGroupName ("Wifi")
49  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
53  MakeUintegerChecker<uint16_t> (1,MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH))
54  .AddAttribute ("Channel", "The channel attached to this device",
55  PointerValue (),
57  MakePointerChecker<WifiChannel> ())
58  .AddAttribute ("Phy", "The PHY layer attached to this device.",
59  PointerValue (),
62  MakePointerChecker<WifiPhy> ())
63  .AddAttribute ("Mac", "The MAC layer attached to this device.",
64  PointerValue (),
67  MakePointerChecker<WifiMac> ())
68  .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
69  PointerValue (),
72  MakePointerChecker<WifiRemoteStationManager> ())
73  ;
74  return tid;
75 }
76 
78  : m_configComplete (false)
79 {
81 }
82 
84 {
86 }
87 
88 void
90 {
92  m_node = 0;
93  m_mac->Dispose ();
94  m_phy->Dispose ();
96  m_mac = 0;
97  m_phy = 0;
98  m_stationManager = 0;
99  m_queueInterface = 0;
101 }
102 
103 void
105 {
106  m_phy->Initialize ();
107  m_mac->Initialize ();
110 }
111 
112 void
114 {
115  if (m_mac == 0
116  || m_phy == 0
117  || m_stationManager == 0
118  || m_node == 0
119  || m_configComplete)
120  {
121  return;
122  }
123  m_mac->SetWifiRemoteStationManager (m_stationManager);
124  m_mac->SetWifiPhy (m_phy);
125  m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
126  m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
127  m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
130  m_configComplete = true;
131 }
132 
133 void
135 {
136  NS_LOG_FUNCTION (this);
137  if (m_queueInterface == 0)
138  {
139  Ptr<NetDeviceQueueInterface> ndqi = this->GetObject<NetDeviceQueueInterface> ();
140  //verify that it's a valid netdevice queue interface and that
141  //the netdevice queue interface was not set before
142  if (ndqi != 0)
143  {
144  m_queueInterface = ndqi;
145  if (m_mac == 0)
146  {
147  NS_LOG_WARN ("A mac has not been installed yet, using a single tx queue");
148  }
149  else
150  {
151  Ptr<RegularWifiMac> mac = DynamicCast<RegularWifiMac> (m_mac);
152  if (mac != 0)
153  {
154  BooleanValue qosSupported;
155  mac->GetAttributeFailSafe ("QosSupported", qosSupported);
156  if (qosSupported.Get ())
157  {
158  m_queueInterface->SetTxQueuesN (4);
159  // register the select queue callback
160  m_queueInterface->SetSelectQueueCallback (MakeCallback (&WifiNetDevice::SelectQueue, this));
161  }
162  }
163  }
164  }
165  }
167 }
168 
169 void
171 {
172  m_mac = mac;
173  CompleteConfig ();
174 }
175 
176 void
178 {
179  m_phy = phy;
180  CompleteConfig ();
181 }
182 
183 void
185 {
186  m_stationManager = manager;
187  CompleteConfig ();
188 }
189 
192 {
193  return m_mac;
194 }
195 
198 {
199  return m_phy;
200 }
201 
204 {
205  return m_stationManager;
206 }
207 
208 void
209 WifiNetDevice::SetIfIndex (const uint32_t index)
210 {
211  m_ifIndex = index;
212 }
213 
214 uint32_t
216 {
217  return m_ifIndex;
218 }
219 
222 {
223  return m_phy->GetChannel ();
224 }
225 
228 {
229  return m_phy->GetChannel ();
230 }
231 
232 void
234 {
235  m_mac->SetAddress (Mac48Address::ConvertFrom (address));
236 }
237 
238 Address
240 {
241  return m_mac->GetAddress ();
242 }
243 
244 bool
245 WifiNetDevice::SetMtu (const uint16_t mtu)
246 {
248  {
249  return false;
250  }
251  m_mtu = mtu;
252  return true;
253 }
254 
255 uint16_t
257 {
258  return m_mtu;
259 }
260 
261 bool
263 {
264  return m_phy != 0 && m_linkUp;
265 }
266 
267 void
269 {
271 }
272 
273 bool
275 {
276  return true;
277 }
278 
279 Address
281 {
282  return Mac48Address::GetBroadcast ();
283 }
284 
285 bool
287 {
288  return true;
289 }
290 
291 Address
293 {
294  return Mac48Address::GetMulticast (multicastGroup);
295 }
296 
298 {
299  return Mac48Address::GetMulticast (addr);
300 }
301 
302 bool
304 {
305  return false;
306 }
307 
308 bool
310 {
311  return false;
312 }
313 
314 bool
315 WifiNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
316 {
317  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
319 
320  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
321 
322  LlcSnapHeader llc;
323  llc.SetType (protocolNumber);
324  packet->AddHeader (llc);
325 
326  m_mac->NotifyTx (packet);
327  m_mac->Enqueue (packet, realTo);
328  return true;
329 }
330 
331 Ptr<Node>
333 {
334  return m_node;
335 }
336 
337 void
339 {
340  m_node = node;
341  CompleteConfig ();
342 }
343 
344 bool
346 {
347  return true;
348 }
349 
350 void
352 {
353  m_forwardUp = cb;
354 }
355 
356 void
358 {
359  NS_LOG_FUNCTION (this << packet << from << to);
360  LlcSnapHeader llc;
361  enum NetDevice::PacketType type;
362  if (to.IsBroadcast ())
363  {
365  }
366  else if (to.IsGroup ())
367  {
369  }
370  else if (to == m_mac->GetAddress ())
371  {
372  type = NetDevice::PACKET_HOST;
373  }
374  else
375  {
377  }
378 
379  if (type != NetDevice::PACKET_OTHERHOST)
380  {
381  m_mac->NotifyRx (packet);
382  packet->RemoveHeader (llc);
383  m_forwardUp (this, packet, llc.GetType (), from);
384  }
385  else
386  {
387  packet->RemoveHeader (llc);
388  }
389 
390  if (!m_promiscRx.IsNull ())
391  {
392  m_mac->NotifyPromiscRx (packet);
393  m_promiscRx (this, packet, llc.GetType (), from, to, type);
394  }
395 }
396 
397 void
399 {
400  m_linkUp = true;
401  m_linkChanges ();
402 }
403 
404 void
406 {
407  m_linkUp = false;
408  m_linkChanges ();
409 }
410 
411 bool
412 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
413 {
414  NS_LOG_FUNCTION (this << packet << source << dest << protocolNumber);
417 
418  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
419  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
420 
421  LlcSnapHeader llc;
422  llc.SetType (protocolNumber);
423  packet->AddHeader (llc);
424 
425  m_mac->NotifyTx (packet);
426  m_mac->Enqueue (packet, realTo, realFrom);
427 
428  return true;
429 }
430 
431 void
433 {
434  m_promiscRx = cb;
435  m_mac->SetPromisc ();
436 }
437 
438 bool
440 {
441  return m_mac->SupportsSendFrom ();
442 }
443 
444 uint8_t
446 {
447  NS_LOG_FUNCTION (this << item);
448 
450 
451  if (m_queueInterface->GetNTxQueues () == 1)
452  {
453  return 0;
454  }
455 
456  uint8_t dscp, priority = 0;
457  if (item->GetUint8Value (QueueItem::IP_DSFIELD, dscp))
458  {
459  // if the QoS map element is implemented, it should be used here
460  // to set the priority.
461  // User priority is set to the three most significant bits of the DS field
462  priority = dscp >> 5;
463  }
464 
465  // replace the priority tag
466  SocketPriorityTag priorityTag;
467  priorityTag.SetPriority (priority);
468  item->GetPacket ()->ReplacePacketTag (priorityTag);
469 
470  // if the admission control were implemented, here we should check whether
471  // the access category assigned to the packet should be downgraded
472 
473  return QosUtilsMapTidToAc (priority);
474 }
475 
476 } //namespace ns3
virtual void SetNode(Ptr< Node > node)
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
virtual 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 Ptr< WifiChannel > GetChannel(void) const =0
Return the WifiChannel this WifiPhy is connected to.
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.
virtual bool SetMtu(const uint16_t mtu)
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:34
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:44
virtual uint16_t GetMtu(void) const
virtual Address GetAddress(void) const
Ptr< WifiRemoteStationManager > m_stationManager
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:606
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
bool IsBroadcast(void) const
virtual 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:612
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:608
void ForwardUp(Ptr< Packet > packet, Mac48Address from, Mac48Address to)
Receive a packet from the lower layer and pass the packet up the stack.
virtual void SetupPhy(Ptr< WifiPhy > phy)
Set up PHY associated with this device since it is the object that knows the full set of transmit rat...
virtual Ptr< Channel > GetChannel(void) const
virtual bool IsBroadcast(void) const
a polymophic address class
Definition: address.h:90
void SetMac(Ptr< WifiMac > mac)
virtual bool IsMulticast(void) const
Ptr< WifiPhy > GetPhy(void) const
tuple phy
Definition: third.py:86
virtual 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)
uint16_t GetType(void)
Return the Ethertype.
void SetPhy(Ptr< WifiPhy > phy)
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< WifiChannel > DoGetChannel(void) const
Return the WifiChannel this device is connected to.
Ptr< WifiMac > m_mac
virtual bool SupportsSendFrom(void) const
indicates whether the socket has a priority set.
Definition: socket.h:1304
AcIndex QosUtilsMapTidToAc(uint8_t tid)
Maps TID (Traffic ID) to Access classes.
Definition: qos-utils.cc:28
Ptr< WifiPhy > m_phy
static Mac48Address GetBroadcast(void)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
tuple mac
Definition: third.py:92
void SetRemoteStationManager(Ptr< WifiRemoteStationManager > manager)
static Mac48Address ConvertFrom(const Address &address)
static const uint16_t LLC_SNAP_HEADER_LENGTH
The length in octects of the LLC/SNAP header.
virtual Ptr< Node > GetNode(void) const
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TracedCallback m_linkChanges
Hold objects of type Ptr.
Definition: pointer.h:36
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
bool IsGroup(void) const
Packet addressed to someone else.
Definition: net-device.h:614
virtual uint32_t GetIfIndex(void) const
static const uint16_t MAX_MSDU_SIZE
an EUI-48 address
Definition: mac48-address.h:43
virtual void DoInitialize(void)
Initialize() implementation.
virtual bool NeedsArp(void) const
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual void SetupMac(Ptr< WifiMac > mac)
Set up MAC associated with this device since it is the object that knows the full set of timing param...
virtual Address GetBroadcast(void) const
Describes an IPv6 address.
Definition: ipv6-address.h:48
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
virtual void SetIfIndex(const uint32_t index)
NetDevice::ReceiveCallback m_forwardUp
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Network layer to device interface.
Definition: net-device.h:405
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
Packet addressed to all.
Definition: net-device.h:610
virtual bool IsLinkUp(void) const
virtual 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
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
tuple address
Definition: first.py:37
virtual 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.
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:904
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
static TypeId GetTypeId(void)
NetDevice::PromiscReceiveCallback m_promiscRx
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
virtual void SetAddress(Address address)
Set the address of this interface.
Header for the LLC/SNAP encapsulation.