A Discrete-Event Network Simulator
API
wifi-assoc-manager.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Universita' degli Studi di Napoli Federico II
3
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: Stefano Avallone <stavallo@unina.it>
19 */
20
21#include "wifi-assoc-manager.h"
22
23#include "sta-wifi-mac.h"
24
25#include "ns3/log.h"
26
27#include <algorithm>
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("WifiAssocManager");
33
34NS_OBJECT_ENSURE_REGISTERED(WifiAssocManager);
35
37 : m_manager(manager)
38{
39}
40
41bool
43 const StaWifiMac::ApInfo& rhs) const
44{
45 NS_ASSERT_MSG(lhs.m_bssid != rhs.m_bssid,
46 "Comparing two ApInfo objects with the same BSSID: " << lhs.m_bssid);
47
48 bool lhsBefore = m_manager.Compare(lhs, rhs);
49 if (lhsBefore)
50 {
51 return true;
52 }
53
54 // the Compare method implemented by subclass may be such that the two ApInfo objects
55 // compare equal; in such a case, use the BSSID as tie breaker
56 bool rhsBefore = m_manager.Compare(rhs, lhs);
57 if (rhsBefore)
58 {
59 return false;
60 }
61
62 WifiAddressHash hash;
63 return hash(lhs.m_bssid) > hash(rhs.m_bssid);
64}
65
68{
69 static TypeId tid = TypeId("ns3::WifiAssocManager").SetParent<Object>().SetGroupName("Wifi");
70 return tid;
71}
72
74 : m_scanParams(), // zero-initialization
76{
77}
78
80{
81 NS_LOG_FUNCTION(this);
82}
83
84void
86{
87 NS_LOG_FUNCTION(this);
88 m_mac = nullptr;
89}
90
91void
93{
94 NS_LOG_FUNCTION(this << mac);
95 m_mac = mac;
96}
97
100{
101 return m_apList;
102}
103
104const WifiScanParams&
106{
107 return m_scanParams;
108}
109
110bool
112{
113 NS_LOG_FUNCTION(this << apInfo);
114
116 {
117 // we need to check if AP's advertised SSID matches the requested SSID
118 Ssid apSsid;
119 if (auto beacon = std::get_if<MgtBeaconHeader>(&apInfo.m_frame); beacon)
120 {
121 apSsid = beacon->GetSsid();
122 }
123 else
124 {
125 auto probeResp = std::get_if<MgtProbeResponseHeader>(&apInfo.m_frame);
126 NS_ASSERT(probeResp);
127 apSsid = probeResp->GetSsid();
128 }
129 if (!apSsid.IsEqual(m_scanParams.ssid))
130 {
131 NS_LOG_DEBUG("AP " << apInfo.m_bssid << " does not advertise our SSID " << apSsid
132 << " " << m_scanParams.ssid);
133 return false;
134 }
135 }
136
137 // we need to check if the AP is operating on a requested channel
138 auto channelMatch = [&apInfo](auto&& channel) {
139 if (channel.number != 0 && channel.number != apInfo.m_channel.number)
140 {
141 return false;
142 }
143 if (channel.band != WIFI_PHY_BAND_UNSPECIFIED && channel.band != apInfo.m_channel.band)
144 {
145 return false;
146 }
147 return true;
148 };
149
151 if (std::find_if(m_scanParams.channelList[apInfo.m_linkId].cbegin(),
152 m_scanParams.channelList[apInfo.m_linkId].cend(),
153 channelMatch) == m_scanParams.channelList[apInfo.m_linkId].cend())
154 {
155 NS_LOG_DEBUG("AP " << apInfo.m_bssid << " is not operating on a requested channel");
156 return false;
157 }
158
159 return true;
160}
161
162void
164{
165 NS_LOG_FUNCTION(this);
166 m_scanParams = std::move(scanParams);
167
168 // remove stored AP information not matching the scanning parameters
169 for (auto ap = m_apList.begin(); ap != m_apList.end();)
170 {
171 if (!MatchScanParams(*ap))
172 {
173 // remove AP info from list
174 m_apListIt.erase(ap->m_bssid);
175 ap = m_apList.erase(ap);
176 }
177 else
178 {
179 ++ap;
180 }
181 }
182
184}
185
186void
188{
189 NS_LOG_FUNCTION(this << apInfo);
190
191 if (!CanBeInserted(apInfo) || !MatchScanParams(apInfo))
192 {
193 return;
194 }
195
196 // check if an ApInfo object with the same BSSID is already present in the
197 // sorted list of ApInfo objects. This is done by trying to insert the BSSID
198 // in the hash table (insertion fails if the BSSID is already present)
199 auto [hashIt, hashInserted] = m_apListIt.insert({apInfo.m_bssid, {}});
200 if (!hashInserted)
201 {
202 // an element with the searched BSSID is already present in the hash table.
203 // Remove the corresponding ApInfo object from the sorted list.
204 m_apList.erase(hashIt->second);
205 }
206 // insert the ApInfo object
207 auto [listIt, listInserted] = m_apList.insert(std::move(apInfo));
208 // update the hash table entry
209 NS_ASSERT_MSG(listInserted,
210 "An entry (" << listIt->m_apAddr << ", " << listIt->m_bssid << ", "
211 << +listIt->m_linkId
212 << ") prevented insertion of given ApInfo object");
213 hashIt->second = listIt;
214}
215
216void
218{
219 NS_LOG_FUNCTION(this);
220
221 StaWifiMac::ApInfo bestAp;
222
223 do
224 {
225 if (m_apList.empty())
226 {
227 m_mac->ScanningTimeout(std::nullopt);
228 return;
229 }
230
231 bestAp = std::move(m_apList.extract(m_apList.begin()).value());
232 m_apListIt.erase(bestAp.m_bssid);
233 } while (!CanBeReturned(bestAp));
234
235 m_mac->ScanningTimeout(std::move(bestAp));
236}
237
238std::list<std::pair<std::uint8_t, uint8_t>>&
240{
241 return const_cast<std::list<std::pair<std::uint8_t, uint8_t>>&>(apInfo.m_setupLinks);
242}
243
244bool
246{
247 NS_LOG_FUNCTION(this);
248
249 if (m_mac->GetNLinks() == 1 || GetSortedList().empty())
250 {
251 return false;
252 }
253
254 // Get the Multi-Link Element and the RNR element, if present,
255 // from Beacon or Probe Response
256 if (auto beacon = std::get_if<MgtBeaconHeader>(&m_apList.begin()->m_frame); beacon)
257 {
258 mle = beacon->GetMultiLinkElement();
259 rnr = beacon->GetReducedNeighborReport();
260 }
261 else
262 {
263 auto probeResp = std::get_if<MgtProbeResponseHeader>(&m_apList.begin()->m_frame);
264 NS_ASSERT(probeResp);
265 mle = probeResp->GetMultiLinkElement();
266 rnr = probeResp->GetReducedNeighborReport();
267 }
268
269 if (!mle.has_value())
270 {
271 NS_LOG_DEBUG("No Multi-Link Element in Beacon/Probe Response");
272 return false;
273 }
274
275 if (!rnr.has_value() || rnr->get().GetNNbrApInfoFields() == 0)
276 {
277 NS_LOG_DEBUG("No Reduced Neighbor Report Element in Beacon/Probe Response");
278 return false;
279 }
280
281 // The Multi-Link Element must contain the MLD MAC Address subfield and the
282 // Link ID Info subfield
283 if (!mle->get().HasLinkIdInfo())
284 {
285 NS_LOG_DEBUG("No Link ID Info subfield in the Multi-Link Element");
286 return false;
287 }
288
289 return true;
290}
291
292std::optional<WifiAssocManager::RnrLinkInfo>
294{
295 NS_LOG_FUNCTION(nbrApInfoId);
296
297 while (nbrApInfoId < rnr.GetNNbrApInfoFields())
298 {
299 if (!rnr.HasMldParameters(nbrApInfoId))
300 {
301 // this Neighbor AP Info field is not suitable to setup a link
302 nbrApInfoId++;
303 continue;
304 }
305
306 std::size_t tbttInfoFieldIndex = 0;
307 while (tbttInfoFieldIndex < rnr.GetNTbttInformationFields(nbrApInfoId) &&
308 rnr.GetMldId(nbrApInfoId, tbttInfoFieldIndex) != 0)
309 {
310 tbttInfoFieldIndex++;
311 }
312
313 if (tbttInfoFieldIndex < rnr.GetNTbttInformationFields(nbrApInfoId))
314 {
315 // this Neighbor AP Info field contains an AP affiliated to the
316 // same AP MLD as the reporting AP
317 return RnrLinkInfo{nbrApInfoId, tbttInfoFieldIndex};
318 }
319 nbrApInfoId++;
320 }
321
322 return std::nullopt;
323}
324
325std::list<WifiAssocManager::RnrLinkInfo>
327{
328 std::list<WifiAssocManager::RnrLinkInfo> apList;
329 std::size_t nbrApInfoId = 0;
330 std::optional<WifiAssocManager::RnrLinkInfo> next;
331
332 while ((next = GetNextAffiliatedAp(rnr, nbrApInfoId)).has_value())
333 {
334 apList.push_back({*next});
335 nbrApInfoId = next->m_nbrApInfoId + 1;
336 }
337
338 return apList;
339}
340
341} // namespace ns3
A base class which provides memory management and object aggregation.
Definition: object.h:89
The Reduced Neighbor Report element.
std::size_t GetNNbrApInfoFields() const
Get the number of Neighbor AP Information fields.
std::size_t GetNTbttInformationFields(std::size_t nbrApInfoId) const
Get the number of TBTT Information fields included in the TBTT Information Set field of the given Nei...
uint8_t GetMldId(std::size_t nbrApInfoId, std::size_t index) const
Get the MLD ID value in the MLD Parameters subfield (must be present) in the i-th TBTT Information fi...
bool HasMldParameters(std::size_t nbrApInfoId) const
Return true if the MLD Parameters subfield is present in all the TBTT Information fields of the given...
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
bool IsEqual(const Ssid &o) const
Check if the two SSIDs are equal.
Definition: ssid.cc:55
bool IsBroadcast() const
Check if the SSID is broadcast.
Definition: ssid.cc:70
void ScanningTimeout(const std::optional< ApInfo > &bestAp)
This method is called after wait beacon timeout or wait probe request timeout has occurred.
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Abstract base class for the Association Manager, which manages scanning and association for single li...
const SortedList & GetSortedList() const
std::optional< std::reference_wrapper< const MultiLinkElement > > OptMleConstRef
typedef for an optional const reference to a MultiLinkElement object
virtual void NotifyApInfo(const StaWifiMac::ApInfo &&apInfo)
STA wifi MAC received a Beacon frame or Probe Response frame while scanning and notifies us the AP in...
Ptr< StaWifiMac > m_mac
pointer to the STA wifi MAC
virtual void DoStartScanning()=0
Start a scanning procedure.
void SetStaWifiMac(Ptr< StaWifiMac > mac)
Set the pointer to the STA wifi MAC.
void ScanningTimeout()
Extract the best AP to associate with from the sorted list and return it, if any, to the STA wifi MAC...
virtual bool CanBeInserted(const StaWifiMac::ApInfo &apInfo) const =0
Allow subclasses to choose whether the given ApInfo shall be considered and hence inserted in the sor...
std::optional< std::reference_wrapper< const ReducedNeighborReport > > OptRnrConstRef
typedef for an optional const reference to a ReducedNeighborReport object
SortedList m_apList
sorted list of candidate APs
static std::optional< WifiAssocManager::RnrLinkInfo > GetNextAffiliatedAp(const ReducedNeighborReport &rnr, std::size_t nbrApInfoId)
Search the given RNR element for APs affiliated to the same AP MLD as the reporting AP.
WifiScanParams m_scanParams
scanning parameters
virtual bool CanBeReturned(const StaWifiMac::ApInfo &apInfo) const =0
Allow subclasses to choose whether the given ApInfo shall be returned or discarded when the STA wifi ...
void StartScanning(WifiScanParams &&scanParams)
Request the Association Manager to start a scanning procedure according to the given scanning paramet...
std::set< StaWifiMac::ApInfo, ApInfoCompare > SortedList
typedef for the sorted list of ApInfo objects
static std::list< WifiAssocManager::RnrLinkInfo > GetAllAffiliatedAps(const ReducedNeighborReport &rnr)
Find all the APs affiliated to the same AP MLD as the reporting AP that sent the given RNR element.
static TypeId GetTypeId()
Get the type ID.
const WifiScanParams & GetScanParams() const
void DoDispose() override
Destructor implementation.
std::unordered_map< Mac48Address, SortedList::const_iterator, WifiAddressHash > m_apListIt
hash table to help locate ApInfo objects in the sorted list based on the BSSID
bool CanSetupMultiLink(OptMleConstRef &mle, OptRnrConstRef &rnr)
Check whether 11be Multi-Link setup can be established with the current best AP.
WifiAssocManager()
Constructor (protected as this is an abstract base class)
std::list< std::pair< std::uint8_t, uint8_t > > & GetSetupLinks(const StaWifiMac::ApInfo &apInfo)
Get a reference to the list of the links to setup with the given AP.
bool MatchScanParams(const StaWifiMac::ApInfo &apInfo) const
Check whether the given AP information match the current scanning parameters.
uint8_t GetNLinks() const
Get the number of links (can be greater than 1 for 11be devices only).
Definition: wifi-mac.cc:901
Empty class, used as a default parent class for SimpleRefCount.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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
@ WIFI_PHY_BAND_UNSPECIFIED
Unspecified.
Definition: wifi-phy-band.h:43
Every class exported by the ns3 library is enclosed in the ns3 namespace.
channel
Definition: third.py:81
mac
Definition: third.py:85
Struct to hold information regarding observed AP through active/passive scanning.
Definition: sta-wifi-mac.h:140
std::list< std::pair< std::uint8_t, uint8_t > > m_setupLinks
list of (local link ID, AP link ID) pairs identifying the links to setup between MLDs
Definition: sta-wifi-mac.h:148
MgtFrameType m_frame
The body of the management frame used to update AP info.
Definition: sta-wifi-mac.h:144
WifiScanParams::Channel m_channel
The channel the management frame was received on.
Definition: sta-wifi-mac.h:145
uint8_t m_linkId
ID of the link used to communicate with the AP.
Definition: sta-wifi-mac.h:146
Mac48Address m_bssid
BSSID.
Definition: sta-wifi-mac.h:141
Function object to compute the hash of a MAC address.
Definition: qos-utils.h:57
Struct providing a function call operator to compare two ApInfo objects.
ApInfoCompare(const WifiAssocManager &manager)
Constructor.
bool operator()(const StaWifiMac::ApInfo &lhs, const StaWifiMac::ApInfo &rhs) const
Function call operator.
WifiPhyBand band
PHY band.
Definition: sta-wifi-mac.h:58
uint16_t number
channel number
Definition: sta-wifi-mac.h:57
Structure holding scan parameters.
Definition: sta-wifi-mac.h:48
std::vector< ChannelList > channelList
list of channels to scan, for each link
Definition: sta-wifi-mac.h:71
Ssid ssid
desired SSID or wildcard SSID
Definition: sta-wifi-mac.h:70