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/pointer.h"
24 #include "ns3/log.h"
25 #include "ns3/node.h"
26 #include "ns3/uinteger.h"
27 #include "wifi-net-device.h"
28 #include "wifi-phy.h"
29 #include "wifi-mac.h"
30 #include "ns3/ht-configuration.h"
31 #include "ns3/vht-configuration.h"
32 #include "ns3/he-configuration.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("WifiNetDevice");
37 
38 NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::WifiNetDevice")
44  .SetParent<NetDevice> ()
45  .AddConstructor<WifiNetDevice> ()
46  .SetGroupName ("Wifi")
47  .AddAttribute ("Mtu", "The MAC-level Maximum Transmission Unit",
51  MakeUintegerChecker<uint16_t> (1,MAX_MSDU_SIZE - LLC_SNAP_HEADER_LENGTH))
52  .AddAttribute ("Channel", "The channel attached to this device",
53  PointerValue (),
55  MakePointerChecker<Channel> ())
56  .AddAttribute ("Phy", "The PHY layer attached to this device.",
57  PointerValue (),
60  MakePointerChecker<WifiPhy> ())
61  .AddAttribute ("Mac", "The MAC layer attached to this device.",
62  PointerValue (),
65  MakePointerChecker<WifiMac> ())
66  .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
67  PointerValue (),
70  MakePointerChecker<WifiRemoteStationManager> ())
71  .AddAttribute ("HtConfiguration",
72  "The HtConfiguration object.",
73  PointerValue (),
75  MakePointerChecker<HtConfiguration> ())
76  .AddAttribute ("VhtConfiguration",
77  "The VhtConfiguration object.",
78  PointerValue (),
80  MakePointerChecker<VhtConfiguration> ())
81  .AddAttribute ("HeConfiguration",
82  "The HeConfiguration object.",
83  PointerValue (),
85  MakePointerChecker<HeConfiguration> ())
86  ;
87  return tid;
88 }
89 
91  : m_standard (WIFI_STANDARD_UNSPECIFIED),
92  m_configComplete (false)
93 {
95 }
96 
98 {
100 }
101 
102 void
104 {
106  m_node = 0;
107  if (m_mac)
108  {
109  m_mac->Dispose ();
110  m_mac = 0;
111  }
112  if (m_phy)
113  {
114  m_phy->Dispose ();
115  m_phy = 0;
116  }
117  if (m_stationManager)
118  {
120  m_stationManager = 0;
121  }
122  if (m_htConfiguration)
123  {
124  m_htConfiguration->Dispose ();
125  m_htConfiguration = 0;
126  }
127  if (m_vhtConfiguration)
128  {
129  m_vhtConfiguration->Dispose ();
130  m_vhtConfiguration = 0;
131  }
132  if (m_heConfiguration)
133  {
134  m_heConfiguration->Dispose ();
135  m_heConfiguration = 0;
136  }
138 }
139 
140 void
142 {
144  if (m_phy)
145  {
146  m_phy->Initialize ();
147  }
148  if (m_mac)
149  {
150  m_mac->Initialize ();
151  }
152  if (m_stationManager)
153  {
155  }
157 }
158 
159 void
161 {
162  if (m_mac == 0
163  || m_phy == 0
164  || m_stationManager == 0
165  || m_node == 0
166  || m_configComplete)
167  {
168  return;
169  }
170  m_mac->SetWifiRemoteStationManager (m_stationManager);
171  m_mac->SetWifiPhy (m_phy);
172  m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
173  m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
174  m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
177  m_configComplete = true;
178 }
179 
180 void
182 {
183  NS_ABORT_MSG_IF (m_standard != WIFI_STANDARD_UNSPECIFIED, "Wifi standard already set");
184  m_standard = standard;
185 }
186 
189 {
190  return m_standard;
191 }
192 
193 void
195 {
196  m_mac = mac;
197  CompleteConfig ();
198 }
199 
200 void
202 {
203  m_phy = phy;
204  CompleteConfig ();
205 }
206 
207 void
209 {
210  m_stationManager = manager;
211  CompleteConfig ();
212 }
213 
216 {
217  return m_mac;
218 }
219 
222 {
223  return m_phy;
224 }
225 
228 {
229  return m_stationManager;
230 }
231 
232 void
233 WifiNetDevice::SetIfIndex (const uint32_t index)
234 {
235  m_ifIndex = index;
236 }
237 
238 uint32_t
240 {
241  return m_ifIndex;
242 }
243 
246 {
247  return m_phy->GetChannel ();
248 }
249 
250 void
252 {
253  m_mac->SetAddress (Mac48Address::ConvertFrom (address));
254 }
255 
256 Address
258 {
259  return m_mac->GetAddress ();
260 }
261 
262 bool
263 WifiNetDevice::SetMtu (const uint16_t mtu)
264 {
266  {
267  return false;
268  }
269  m_mtu = mtu;
270  return true;
271 }
272 
273 uint16_t
275 {
276  return m_mtu;
277 }
278 
279 bool
281 {
282  return m_phy != 0 && m_linkUp;
283 }
284 
285 void
287 {
289 }
290 
291 bool
293 {
294  return true;
295 }
296 
297 Address
299 {
300  return Mac48Address::GetBroadcast ();
301 }
302 
303 bool
305 {
306  return true;
307 }
308 
309 Address
311 {
312  return Mac48Address::GetMulticast (multicastGroup);
313 }
314 
316 {
317  return Mac48Address::GetMulticast (addr);
318 }
319 
320 bool
322 {
323  return false;
324 }
325 
326 bool
328 {
329  return false;
330 }
331 
332 bool
333 WifiNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
334 {
335  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
337 
338  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
339 
340  LlcSnapHeader llc;
341  llc.SetType (protocolNumber);
342  packet->AddHeader (llc);
343 
344  m_mac->NotifyTx (packet);
345  m_mac->Enqueue (packet, realTo);
346  return true;
347 }
348 
349 Ptr<Node>
351 {
352  return m_node;
353 }
354 
355 void
357 {
358  m_node = node;
359  CompleteConfig ();
360 }
361 
362 bool
364 {
365  return true;
366 }
367 
368 void
370 {
371  m_forwardUp = cb;
372 }
373 
374 void
376 {
377  NS_LOG_FUNCTION (this << packet << from << to);
378  LlcSnapHeader llc;
380  if (to.IsBroadcast ())
381  {
383  }
384  else if (to.IsGroup ())
385  {
387  }
388  else if (to == m_mac->GetAddress ())
389  {
390  type = NetDevice::PACKET_HOST;
391  }
392  else
393  {
395  }
396 
397  Ptr<Packet> copy = packet->Copy ();
398  if (type != NetDevice::PACKET_OTHERHOST)
399  {
400  m_mac->NotifyRx (packet);
401  copy->RemoveHeader (llc);
402  m_forwardUp (this, copy, llc.GetType (), from);
403  }
404  else
405  {
406  copy->RemoveHeader (llc);
407  }
408 
409  if (!m_promiscRx.IsNull ())
410  {
411  m_mac->NotifyPromiscRx (copy);
412  m_promiscRx (this, copy, llc.GetType (), from, to, type);
413  }
414 }
415 
416 void
418 {
419  m_linkUp = true;
420  m_linkChanges ();
421 }
422 
423 void
425 {
426  m_linkUp = false;
427  m_linkChanges ();
428 }
429 
430 bool
431 WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
432 {
433  NS_LOG_FUNCTION (this << packet << source << dest << protocolNumber);
436 
437  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
438  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
439 
440  LlcSnapHeader llc;
441  llc.SetType (protocolNumber);
442  packet->AddHeader (llc);
443 
444  m_mac->NotifyTx (packet);
445  m_mac->Enqueue (packet, realTo, realFrom);
446 
447  return true;
448 }
449 
450 void
452 {
453  m_promiscRx = cb;
454  m_mac->SetPromisc ();
455 }
456 
457 bool
459 {
460  return m_mac->SupportsSendFrom ();
461 }
462 
463 void
465 {
466  m_htConfiguration = htConfiguration;
467 }
468 
471 {
472  return (m_standard >= WIFI_STANDARD_80211n ? m_htConfiguration : nullptr);
473 }
474 
475 void
477 {
478  m_vhtConfiguration = vhtConfiguration;
479 }
480 
483 {
485  ? m_vhtConfiguration : nullptr);
486 }
487 
488 void
490 {
491  m_heConfiguration = heConfiguration;
492 }
493 
496 {
497  return (m_standard >= WIFI_STANDARD_80211ax ? m_heConfiguration : nullptr);
498 }
499 
500 } //namespace ns3
a polymophic address class
Definition: address.h:91
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1386
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Describes an IPv6 address.
Definition: ipv6-address.h:50
Header for the LLC/SNAP encapsulation.
uint16_t GetType(void)
Return the Ethertype.
void SetType(uint16_t type)
Set the Ethertype.
an EUI-48 address
Definition: mac48-address.h:44
static Mac48Address GetMulticast(Ipv4Address address)
static Mac48Address GetBroadcast(void)
static bool IsMatchingType(const Address &address)
bool IsGroup(void) const
bool IsBroadcast(void) const
static Mac48Address ConvertFrom(const Address &address)
Network layer to device interface.
Definition: net-device.h:96
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
@ PACKET_HOST
Packet addressed oo us.
Definition: net-device.h:298
@ PACKET_OTHERHOST
Packet addressed to someone else.
Definition: net-device.h:304
@ PACKET_BROADCAST
Packet addressed to all.
Definition: net-device.h:300
@ PACKET_MULTICAST
Packet addressed to multicast group.
Definition: net-device.h:302
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
void ForwardUp(Ptr< const Packet > packet, Mac48Address from, Mac48Address to)
Receive a packet from the lower layer and pass the packet up the stack.
bool NeedsArp(void) const override
void LinkUp(void)
Set that the link is up.
void SetMac(const Ptr< WifiMac > mac)
Ptr< HtConfiguration > m_htConfiguration
the HtConfiguration
uint16_t GetMtu(void) const override
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
Ptr< HtConfiguration > GetHtConfiguration(void) const
bool IsMulticast(void) const override
void DoInitialize(void) override
Initialize() implementation.
void SetHeConfiguration(Ptr< HeConfiguration > heConfiguration)
bool IsBroadcast(void) const override
void LinkDown(void)
Set that the link is down (i.e.
void SetHtConfiguration(Ptr< HtConfiguration > htConfiguration)
Ptr< WifiMac > GetMac(void) const
bool SetMtu(const uint16_t mtu) override
bool IsPointToPoint(void) const override
Return true if the net device is on a point-to-point link.
uint32_t m_ifIndex
IF index.
Ptr< VhtConfiguration > m_vhtConfiguration
the VhtConfiguration
bool m_configComplete
configuration complete
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
void SetIfIndex(const uint32_t index) override
Ptr< Channel > GetChannel(void) const override
NetDevice::ReceiveCallback m_forwardUp
forward up callback
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Address GetAddress(void) const override
bool SupportsSendFrom(void) const override
bool IsLinkUp(void) const override
Ptr< HeConfiguration > m_heConfiguration
the HeConfiguration
WifiStandard GetStandard(void) const
Get the Wifi standard.
Address GetBroadcast(void) const override
void SetVhtConfiguration(Ptr< VhtConfiguration > vhtConfiguration)
NetDevice::PromiscReceiveCallback m_promiscRx
promiscuous receive callback
Ptr< WifiPhy > GetPhy(void) const
TracedCallback m_linkChanges
link change callback
void CompleteConfig(void)
Complete the configuration of this Wi-Fi device by connecting all lower components (e....
void SetRemoteStationManager(const Ptr< WifiRemoteStationManager > manager)
static TypeId GetTypeId(void)
Get the type ID.
void SetNode(const Ptr< Node > node) override
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
Ptr< WifiRemoteStationManager > m_stationManager
the station manager
uint32_t GetIfIndex(void) const override
Ptr< VhtConfiguration > GetVhtConfiguration(void) const
void SetAddress(Address address) override
Set the address of this interface.
Ptr< WifiPhy > m_phy
the phy
void SetStandard(WifiStandard standard)
Set the Wifi standard.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
bool IsBridge(void) const override
Return true if the net device is acting as a bridge.
Ptr< WifiRemoteStationManager > GetRemoteStationManager(void) const
Ptr< Node > m_node
the node
WifiStandard m_standard
Wifi standard.
void SetPhy(const Ptr< WifiPhy > phy)
Ptr< WifiMac > m_mac
the MAC
void AddLinkChangeCallback(Callback< void > callback) override
void DoDispose(void) override
Destructor implementation.
Ptr< HeConfiguration > GetHeConfiguration(void) const
Ptr< Node > GetNode(void) const override
WifiPhyBand GetPhyBand(void) const
Get the configured Wi-Fi band.
Definition: wifi-phy.cc:887
virtual Ptr< Channel > GetChannel(void) const =0
Return the Channel this WifiPhy is connected to.
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...
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...
#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
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:227
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
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
WifiStandard
Identifies the IEEE 802.11 specifications that a Wifi device can be configured to use.
@ WIFI_STANDARD_80211n
@ WIFI_STANDARD_80211ax
@ WIFI_STANDARD_UNSPECIFIED
@ WIFI_STANDARD_80211ac
@ WIFI_PHY_BAND_5GHZ
The 5 GHz band.
Definition: wifi-phy-band.h:37
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint16_t MAX_MSDU_SIZE
This value conforms to the 802.11 specification.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
static const uint16_t LLC_SNAP_HEADER_LENGTH
The length in octects of the LLC/SNAP header.
mac
Definition: third.py:99
phy
Definition: third.py:93