A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-default-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
22
23#include "sta-wifi-mac.h"
24#include "wifi-phy.h"
25
26#include "ns3/log.h"
27#include "ns3/simulator.h"
28
29#include <algorithm>
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("WifiDefaultAssocManager");
35
36NS_OBJECT_ENSURE_REGISTERED(WifiDefaultAssocManager);
37
38TypeId
40{
41 static TypeId tid =
42 TypeId("ns3::WifiDefaultAssocManager")
44 .AddConstructor<WifiDefaultAssocManager>()
45 .SetGroupName("Wifi")
46 .AddAttribute("ChannelSwitchTimeout",
47 "After requesting a channel switch on a link to setup that link, "
48 "wait at most this amount of time. If a channel switch is not "
49 "notified within this amount of time, we give up setting up that link.",
53 return tid;
54}
55
57{
58 NS_LOG_FUNCTION(this);
59}
60
62{
63 NS_LOG_FUNCTION(this);
64}
65
66void
68{
69 NS_LOG_FUNCTION(this);
73}
74
75bool
77{
78 return lhs.m_snr > rhs.m_snr;
79}
80
81void
83{
84 NS_LOG_FUNCTION(this);
85
86 // if there are entries in the sorted list of AP information, reuse them and
87 // do not perform scanning
88 if (!GetSortedList().empty())
89 {
91 return;
92 }
93
96
98 {
99 for (uint8_t linkId = 0; linkId < m_mac->GetNLinks(); linkId++)
100 {
103 m_mac,
104 linkId);
105 }
107 Simulator::Schedule(GetScanParams().probeDelay + GetScanParams().maxChannelTime,
109 this);
110 }
111 else
112 {
115 this);
116 }
117}
118
119void
121{
122 NS_LOG_FUNCTION(this);
123
124 OptMleConstRef mle;
125 OptRnrConstRef rnr;
126 std::list<WifiAssocManager::RnrLinkInfo> apList;
127
128 // If multi-link setup is not possible, just call ScanningTimeout() and return
129 if (!CanSetupMultiLink(mle, rnr) || (apList = GetAllAffiliatedAps(*rnr)).empty())
130 {
132 return;
133 }
134
135 auto& bestAp = *GetSortedList().begin();
136
137 // store AP MLD MAC address in the WifiRemoteStationManager associated with
138 // the link on which the Beacon/Probe Response was received
139 m_mac->GetWifiRemoteStationManager(bestAp.m_linkId)
140 ->SetMldAddress(bestAp.m_apAddr, mle->get().GetMldMacAddress());
141 auto& setupLinks = GetSetupLinks(bestAp);
142
143 setupLinks.clear();
144 setupLinks.emplace_back(bestAp.m_linkId, mle->get().GetLinkIdInfo());
145
146 // sort local PHY objects so that radios with constrained PHY band comes first,
147 // then radios with no constraint
148 std::list<uint8_t> localLinkIds;
149
150 for (uint8_t linkId = 0; linkId < m_mac->GetNLinks(); linkId++)
151 {
152 if (linkId == bestAp.m_linkId)
153 {
154 // this link has been already added (it is the link on which the Beacon/Probe
155 // Response was received)
156 continue;
157 }
158
159 if (m_mac->GetWifiPhy(linkId)->HasFixedPhyBand())
160 {
161 localLinkIds.push_front(linkId);
162 }
163 else
164 {
165 localLinkIds.push_back(linkId);
166 }
167 }
168
169 // iterate over all the local links and find if we can setup a link for each of them
170 for (const auto& linkId : localLinkIds)
171 {
172 auto phy = m_mac->GetWifiPhy(linkId);
173 auto apIt = apList.begin();
174
175 while (apIt != apList.end())
176 {
177 auto apChannel = rnr->get().GetOperatingChannel(apIt->m_nbrApInfoId);
178
179 // we cannot setup a link with this affiliated AP if this PHY object is
180 // constrained to operate in the current PHY band and this affiliated AP
181 // is operating in a different PHY band than this PHY object
182 if (phy->HasFixedPhyBand() && phy->GetPhyBand() != apChannel.GetPhyBand())
183 {
184 apIt++;
185 continue;
186 }
187
188 bool needChannelSwitch = false;
189 if (phy->GetOperatingChannel() != apChannel)
190 {
191 needChannelSwitch = true;
192 }
193
194 if (needChannelSwitch && phy->IsStateSwitching())
195 {
196 // skip this affiliated AP, which is operating on a different channel
197 // than ours, because we are already switching channel and cannot
198 // schedule another channel switch to match the affiliated AP channel
199 apIt++;
200 continue;
201 }
202
203 // if we get here, it means we can setup a link with this affiliated AP
204 // set the BSSID for this link
205 Mac48Address bssid = rnr->get().GetBssid(apIt->m_nbrApInfoId, apIt->m_tbttInfoFieldId);
206 // store AP MLD MAC address in the WifiRemoteStationManager associated with
207 // the link requested to setup
209 bssid,
210 mle->get().GetMldMacAddress());
211 setupLinks.emplace_back(
212 linkId,
213 rnr->get().GetLinkId(apIt->m_nbrApInfoId, apIt->m_tbttInfoFieldId));
214
215 if (needChannelSwitch)
216 {
217 if (phy->IsStateSleep())
218 {
219 // switching channel while a PHY is in sleep state fails
220 phy->ResumeFromSleep();
221 }
222 // switch this link to using the channel used by a reported AP
223 // TODO check if the STA only supports a narrower channel width
224 NS_LOG_DEBUG("Switch link " << +linkId << " to using " << apChannel);
225 WifiPhy::ChannelTuple chTuple{apChannel.GetNumber(),
226 apChannel.GetWidth(),
227 apChannel.GetPhyBand(),
228 apChannel.GetPrimaryChannelIndex(20)};
229 phy->SetOperatingChannel(chTuple);
230 // actual channel switching may be delayed, thus setup a channel switch timer
232 m_channelSwitchInfo[linkId].timer.Cancel();
233 m_channelSwitchInfo[linkId].timer =
236 this,
237 linkId);
238 m_channelSwitchInfo[linkId].apLinkAddress = bssid;
239 m_channelSwitchInfo[linkId].apMldAddress = mle->get().GetMldMacAddress();
240 }
241
242 // remove the affiliated AP with which we are going to setup a link and move
243 // to the next local linkId
244 apList.erase(apIt);
245 break;
246 }
247 }
248
249 if (std::none_of(m_channelSwitchInfo.begin(), m_channelSwitchInfo.end(), [](auto&& info) {
250 return info.timer.IsRunning();
251 }))
252 {
253 // we are done
255 }
256}
257
258void
260{
261 NS_LOG_FUNCTION(this << +linkId);
262 if (m_channelSwitchInfo.size() > linkId && m_channelSwitchInfo[linkId].timer.IsRunning())
263 {
264 // we were waiting for this notification
265 m_channelSwitchInfo[linkId].timer.Cancel();
266
267 // the remote station manager has been reset after switching, hence store
268 // information about AP link address and AP MLD address
270 m_channelSwitchInfo[linkId].apLinkAddress,
271 m_channelSwitchInfo[linkId].apMldAddress);
272
273 if (std::none_of(m_channelSwitchInfo.begin(), m_channelSwitchInfo.end(), [](auto&& info) {
274 return info.timer.IsRunning();
275 }))
276 {
277 // we are done
279 }
280 }
281}
282
283void
285{
286 NS_LOG_FUNCTION(this << +linkId);
287
288 // we give up setting up this link
289 auto& bestAp = *GetSortedList().begin();
290 auto& setupLinks = GetSetupLinks(bestAp);
291 auto it = std::find_if(setupLinks.begin(), setupLinks.end(), [&linkId](auto&& linkIds) {
292 return linkIds.first == linkId;
293 });
294 NS_ASSERT(it != setupLinks.end());
295 setupLinks.erase(it);
296
297 if (std::none_of(m_channelSwitchInfo.begin(), m_channelSwitchInfo.end(), [](auto&& info) {
298 return info.timer.IsRunning();
299 }))
300 {
301 // we are done
303 }
304}
305
306bool
308{
310}
311
312bool
314{
315 return true;
316}
317
318} // namespace ns3
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
an EUI-48 address
Definition: mac48-address.h:46
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:606
void SendProbeRequest(uint8_t linkId)
Enqueue a probe request packet for transmission on the given link.
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
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
Ptr< StaWifiMac > m_mac
pointer to the STA wifi MAC
std::list< std::pair< uint8_t, uint8_t > > & GetSetupLinks(const StaWifiMac::ApInfo &apInfo)
Get a reference to the list of the links to setup with the given AP.
void ScanningTimeout()
Extract the best AP to associate with from the sorted list and return it, if any, to the STA wifi MAC...
std::optional< std::reference_wrapper< const ReducedNeighborReport > > OptRnrConstRef
typedef for an optional const reference to a ReducedNeighborReport object
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.
const WifiScanParams & GetScanParams() const
void DoDispose() override
Destructor implementation.
bool CanSetupMultiLink(OptMleConstRef &mle, OptRnrConstRef &rnr)
Check whether 11be Multi-Link setup can be established with the current best AP.
void EndScanning()
Perform operations to do at the end of a scanning procedure, such as identifying the links to setup i...
void DoDispose() override
Destructor implementation.
EventId m_probeRequestEvent
probe request event
void DoStartScanning() override
Start a scanning procedure.
bool CanBeInserted(const StaWifiMac::ApInfo &apInfo) const override
Allow subclasses to choose whether the given ApInfo shall be considered and hence inserted in the sor...
Time m_channelSwitchTimeout
maximum delay for channel switching
void NotifyChannelSwitched(uint8_t linkId) override
Notify that the given link has completed channel switching.
bool Compare(const StaWifiMac::ApInfo &lhs, const StaWifiMac::ApInfo &rhs) const override
Compare two ApInfo objects for the purpose of keeping a sorted list of ApInfo objects.
EventId m_waitBeaconEvent
wait beacon event
static TypeId GetTypeId()
Get the type ID.
void ChannelSwitchTimeout(uint8_t linkId)
Take action upon the expiration of the timer set when requesting channel switch on the given link.
std::vector< ChannelSwitchInfo > m_channelSwitchInfo
per-link channel switch info
bool CanBeReturned(const StaWifiMac::ApInfo &apInfo) const override
Allow subclasses to choose whether the given ApInfo shall be returned or discarded when the STA wifi ...
uint8_t GetNLinks() const
Get the number of links (can be greater than 1 for 11be devices only).
Definition: wifi-mac.cc:930
Ptr< WifiPhy > GetWifiPhy(uint8_t linkId=SINGLE_LINK_OP_ID) const
Definition: wifi-mac.cc:978
Ptr< WifiRemoteStationManager > GetWifiRemoteStationManager(uint8_t linkId=0) const
Definition: wifi-mac.cc:910
bool HasFixedPhyBand() const
Definition: wifi-phy.cc:1053
std::tuple< uint8_t, uint16_t, int, uint8_t > ChannelTuple
Tuple identifying an operating channel.
Definition: wifi-phy.h:872
const WifiPhyOperatingChannel & GetOperatingChannel() const
Get a const reference to the operating channel.
Definition: wifi-phy.cc:1017
void SetMldAddress(const Mac48Address &address, const Mac48Address &mldAddress)
Set the address of the MLD the given station is affiliated with.
#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
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1444
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1424
#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:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1348
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Struct to hold information regarding observed AP through active/passive scanning.
Definition: sta-wifi-mac.h:164
double m_snr
SNR in linear scale.
Definition: sta-wifi-mac.h:167