A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
channel-access-manager.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
21
22#include "txop.h"
23#include "wifi-mac-queue.h"
24#include "wifi-phy-listener.h"
25#include "wifi-phy.h"
26
27#include "ns3/eht-frame-exchange-manager.h"
28#include "ns3/log.h"
29#include "ns3/simulator.h"
30
31#include <sstream>
32
33#undef NS_LOG_APPEND_CONTEXT
34#define NS_LOG_APPEND_CONTEXT std::clog << "[link=" << +m_linkId << "] "
35
36namespace ns3
37{
38
39NS_LOG_COMPONENT_DEFINE("ChannelAccessManager");
40
41NS_OBJECT_ENSURE_REGISTERED(ChannelAccessManager);
42
51{
52 public:
59 : m_cam(cam),
60 m_active(true)
61 {
62 }
63
64 ~PhyListener() override
65 {
66 }
67
73 void SetActive(bool active)
74 {
75 m_active = active;
76 }
77
81 bool IsActive() const
82 {
83 return m_active;
84 }
85
86 void NotifyRxStart(Time duration) override
87 {
88 if (m_active)
89 {
90 m_cam->NotifyRxStartNow(duration);
91 }
92 }
93
94 void NotifyRxEndOk() override
95 {
96 if (m_active)
97 {
99 }
100 }
101
102 void NotifyRxEndError() override
103 {
104 if (m_active)
105 {
107 }
108 }
109
110 void NotifyTxStart(Time duration, double txPowerDbm) override
111 {
112 if (m_active)
113 {
114 m_cam->NotifyTxStartNow(duration);
115 }
116 }
117
119 WifiChannelListType channelType,
120 const std::vector<Time>& per20MhzDurations) override
121 {
122 if (m_active)
123 {
124 m_cam->NotifyCcaBusyStartNow(duration, channelType, per20MhzDurations);
125 }
126 }
127
128 void NotifySwitchingStart(Time duration) override
129 {
130 m_cam->NotifySwitchingStartNow(this, duration);
131 }
132
133 void NotifySleep() override
134 {
135 if (m_active)
136 {
138 }
139 }
140
141 void NotifyOff() override
142 {
143 if (m_active)
144 {
146 }
147 }
148
149 void NotifyWakeup() override
150 {
151 if (m_active)
152 {
154 }
155 }
156
157 void NotifyOn() override
158 {
159 if (m_active)
160 {
162 }
163 }
164
165 private:
167 bool m_active;
168};
169
170/****************************************************************
171 * Implement the channel access manager of all Txop holders
172 ****************************************************************/
173
174TypeId
176{
177 static TypeId tid =
178 TypeId("ns3::ChannelAccessManager")
180 .SetGroupName("Wifi")
181 .AddConstructor<ChannelAccessManager>()
182 .AddAttribute("GenerateBackoffIfTxopWithoutTx",
183 "Specify whether the backoff should be invoked when the AC gains the "
184 "right to start a TXOP but it does not transmit any frame "
185 "(e.g., due to constraints associated with EMLSR operations), "
186 "provided that the queue is not actually empty.",
187 BooleanValue(false),
191 return tid;
192}
193
195 : m_lastAckTimeoutEnd(0),
196 m_lastCtsTimeoutEnd(0),
197 m_lastNavEnd(0),
198 m_lastRx({MicroSeconds(0), MicroSeconds(0)}),
199 m_lastRxReceivedOk(true),
200 m_lastTxEnd(0),
201 m_lastSwitchingEnd(0),
202 m_usingOtherEmlsrLink(false),
203 m_sleeping(false),
204 m_off(false),
205 m_linkId(0)
206{
207 NS_LOG_FUNCTION(this);
208 InitLastBusyStructs();
209}
210
212{
213 NS_LOG_FUNCTION(this);
214}
215
216void
218{
219 NS_LOG_FUNCTION(this);
221}
222
223void
225{
226 NS_LOG_FUNCTION(this);
227 for (Ptr<Txop> i : m_txops)
228 {
229 i->Dispose();
230 i = nullptr;
231 }
232 m_phy = nullptr;
233 m_feManager = nullptr;
234 m_phyListeners.clear();
235}
236
237std::shared_ptr<PhyListener>
239{
240 if (auto listenerIt = m_phyListeners.find(phy); listenerIt != m_phyListeners.end())
241 {
242 return listenerIt->second;
243 }
244 return nullptr;
245}
246
247void
249{
250 NS_LOG_FUNCTION(this << phy);
251
252 if (auto phyListener = GetPhyListener(phy))
253 {
254 // a PHY listener for the given PHY already exists, it must be inactive
255 NS_ASSERT_MSG(!phyListener->IsActive(),
256 "There is already an active listener registered for given PHY");
257 NS_ASSERT_MSG(!m_phy, "Cannot reactivate a listener if another PHY is active");
258 phyListener->SetActive(true);
259 }
260 else
261 {
262 phyListener = std::make_shared<PhyListener>(this);
263 m_phyListeners.emplace(phy, phyListener);
264 phy->RegisterListener(phyListener);
265 }
266 if (m_phy)
267 {
269 }
270 m_phy = phy; // this is the new active PHY
272 if (phy->IsStateSwitching())
273 {
274 auto duration = phy->GetDelayUntilIdle();
275 NS_LOG_DEBUG("switching start for " << duration);
276 m_lastSwitchingEnd = Simulator::Now() + duration;
277 }
278}
279
280void
282{
283 NS_LOG_FUNCTION(this << phy);
284 if (auto phyListener = GetPhyListener(phy))
285 {
286 phy->UnregisterListener(phyListener);
287 m_phyListeners.erase(phy);
288 // reset m_phy if we are removing listener registered for the active PHY
289 if (m_phy == phy)
290 {
291 m_phy = nullptr;
292 }
293 }
294}
295
296void
298{
299 NS_LOG_FUNCTION(this << phy);
300 if (auto listener = GetPhyListener(phy))
301 {
302 listener->SetActive(false);
303 }
304 if (m_phy == phy)
305 {
306 m_phy = nullptr;
307 }
308}
309
310void
312 const WifiPhyOperatingChannel& channel,
313 uint8_t linkId)
314{
315 NS_LOG_FUNCTION(this << phy << channel << linkId);
316 NS_ASSERT_MSG(m_switchingEmlsrLinks.count(phy) == 0,
317 "The given PHY is already expected to switch channel");
318 m_switchingEmlsrLinks.emplace(phy, EmlsrLinkSwitchInfo{channel, linkId});
319}
320
321void
323{
324 NS_LOG_FUNCTION(this << +linkId);
325 m_linkId = linkId;
326}
327
328void
330{
331 NS_LOG_FUNCTION(this << feManager);
332 m_feManager = feManager;
333 m_feManager->SetChannelAccessManager(this);
334}
335
336Time
338{
339 return m_phy->GetSlot();
340}
341
342Time
344{
345 return m_phy->GetSifs();
346}
347
348Time
350{
351 return m_phy->GetSifs() + m_phy->GetAckTxTime();
352}
353
354void
356{
357 NS_LOG_FUNCTION(this << txop);
358 m_txops.push_back(txop);
359}
360
361void
363{
364 NS_LOG_FUNCTION(this);
365 Time now = Simulator::Now();
366 m_lastBusyEnd.clear();
367 m_lastPer20MHzBusyEnd.clear();
368 m_lastIdle.clear();
370 m_lastIdle[WIFI_CHANLIST_PRIMARY] = {now, now};
371
373 {
374 return;
375 }
376
377 uint16_t width = m_phy->GetChannelWidth();
378
379 if (width >= 40)
380 {
383 }
384 if (width >= 80)
385 {
388 }
389 if (width >= 160)
390 {
393 }
394 // TODO Add conditions for new channel widths as they get supported
395
396 if (m_phy->GetStandard() >= WIFI_STANDARD_80211ax && width > 20)
397 {
398 m_lastPer20MHzBusyEnd.assign(width / 20, now);
399 }
400}
401
402bool
404{
405 NS_LOG_FUNCTION(this);
406 Time now = Simulator::Now();
407 return (m_lastRx.end > now) // RX
408 || (m_lastTxEnd > now) // TX
409 || (m_lastNavEnd > now) // NAV busy
410 // an EDCA TXOP is obtained based solely on activity of the primary channel
411 // (Sec. 10.23.2.5 of IEEE 802.11-2020)
412 || (m_lastBusyEnd.at(WIFI_CHANLIST_PRIMARY) > now); // CCA busy
413}
414
415bool
417 bool hadFramesToTransmit,
418 bool checkMediumBusy)
419{
420 NS_LOG_FUNCTION(this << txop << hadFramesToTransmit << checkMediumBusy);
421
422 // No backoff needed if in sleep mode or off
423 if (m_sleeping || m_off)
424 {
425 return false;
426 }
427
428 // the Txop might have a stale value of remaining backoff slots
430
431 /*
432 * From section 10.3.4.2 "Basic access" of IEEE 802.11-2016:
433 *
434 * A STA may transmit an MPDU when it is operating under the DCF access
435 * method, either in the absence of a PC, or in the CP of the PCF access
436 * method, when the STA determines that the medium is idle when a frame is
437 * queued for transmission, and remains idle for a period of a DIFS, or an
438 * EIFS (10.3.2.3.7) from the end of the immediately preceding medium-busy
439 * event, whichever is the greater, and the backoff timer is zero. Otherwise
440 * the random backoff procedure described in 10.3.4.3 shall be followed.
441 *
442 * From section 10.22.2.2 "EDCA backoff procedure" of IEEE 802.11-2016:
443 *
444 * The backoff procedure shall be invoked by an EDCAF when any of the following
445 * events occurs:
446 * a) An MA-UNITDATA.request primitive is received that causes a frame with that AC
447 * to be queued for transmission such that one of the transmit queues associated
448 * with that AC has now become non-empty and any other transmit queues
449 * associated with that AC are empty; the medium is busy on the primary channel
450 */
451 if (!hadFramesToTransmit && txop->HasFramesToTransmit(m_linkId) &&
452 txop->GetAccessStatus(m_linkId) != Txop::GRANTED && txop->GetBackoffSlots(m_linkId) == 0)
453 {
454 if (checkMediumBusy && !IsBusy())
455 {
456 // medium idle. If this is a DCF, use immediate access (we can transmit
457 // in a DIFS if the medium remains idle). If this is an EDCAF, update
458 // the backoff start time kept by the EDCAF to the current time in order
459 // to correctly align the backoff start time at the next slot boundary
460 // (performed by the next call to ChannelAccessManager::RequestAccess())
461 Time delay =
462 (txop->IsQosTxop() ? Seconds(0) : GetSifs() + txop->GetAifsn(m_linkId) * GetSlot());
463 txop->UpdateBackoffSlotsNow(0, Simulator::Now() + delay, m_linkId);
464 }
465 else
466 {
467 // medium busy, backoff is needed
468 return true;
469 }
470 }
471 return false;
472}
473
474void
476{
477 NS_LOG_FUNCTION(this << txop);
478 if (m_phy && txop->HasFramesToTransmit(m_linkId))
479 {
481 }
482 // Deny access if in sleep mode or off
483 if (m_sleeping || m_off)
484 {
485 return;
486 }
487 /*
488 * EDCAF operations shall be performed at slot boundaries (Sec. 10.22.2.4 of 802.11-2016)
489 */
490 Time accessGrantStart = GetAccessGrantStart() + (txop->GetAifsn(m_linkId) * GetSlot());
491
492 if (txop->IsQosTxop() && txop->GetBackoffStart(m_linkId) > accessGrantStart)
493 {
494 // The backoff start time reported by the EDCAF is more recent than the last
495 // time the medium was busy plus an AIFS, hence we need to align it to the
496 // next slot boundary.
497 Time diff = txop->GetBackoffStart(m_linkId) - accessGrantStart;
498 uint32_t nIntSlots = (diff / GetSlot()).GetHigh() + 1;
499 txop->UpdateBackoffSlotsNow(0, accessGrantStart + (nIntSlots * GetSlot()), m_linkId);
500 }
501
503 NS_ASSERT(txop->GetAccessStatus(m_linkId) != Txop::REQUESTED);
504 txop->NotifyAccessRequested(m_linkId);
507}
508
509void
511{
512 NS_LOG_FUNCTION(this);
513 uint32_t k = 0;
514 Time now = Simulator::Now();
515 for (auto i = m_txops.begin(); i != m_txops.end(); k++)
516 {
517 Ptr<Txop> txop = *i;
518 if (txop->GetAccessStatus(m_linkId) == Txop::REQUESTED &&
519 (!txop->IsQosTxop() || !StaticCast<QosTxop>(txop)->EdcaDisabled(m_linkId)) &&
520 GetBackoffEndFor(txop) <= now)
521 {
526 NS_LOG_DEBUG("dcf " << k << " needs access. backoff expired. access granted. slots="
527 << txop->GetBackoffSlots(m_linkId));
528 i++; // go to the next item in the list.
529 k++;
530 std::vector<Ptr<Txop>> internalCollisionTxops;
531 for (auto j = i; j != m_txops.end(); j++, k++)
532 {
533 Ptr<Txop> otherTxop = *j;
534 if (otherTxop->GetAccessStatus(m_linkId) == Txop::REQUESTED &&
535 GetBackoffEndFor(otherTxop) <= now)
536 {
538 "dcf " << k << " needs access. backoff expired. internal collision. slots="
539 << otherTxop->GetBackoffSlots(m_linkId));
545 internalCollisionTxops.push_back(otherTxop);
546 }
547 }
548
558 // If we are operating on an OFDM channel wider than 20 MHz, find the largest
559 // idle primary channel and pass its width to the FrameExchangeManager, so that
560 // the latter can transmit PPDUs of the appropriate width (see Section 10.23.2.5
561 // of IEEE 802.11-2020).
562 auto interval = (m_phy->GetPhyBand() == WIFI_PHY_BAND_2_4GHZ)
563 ? GetSifs() + 2 * GetSlot()
564 : m_phy->GetPifs();
565 auto width = (m_phy->GetOperatingChannel().IsOfdm() && m_phy->GetChannelWidth() > 20)
566 ? GetLargestIdlePrimaryChannel(interval, now)
568 if (m_feManager->StartTransmission(txop, width))
569 {
570 for (auto& collidingTxop : internalCollisionTxops)
571 {
572 m_feManager->NotifyInternalCollision(collidingTxop);
573 }
574 break;
575 }
576 else
577 {
578 // this TXOP did not transmit anything, make sure that backoff counter starts
579 // decreasing in a slot again
580 txop->UpdateBackoffSlotsNow(0, now, m_linkId);
581 // reset the current state to the EDCAF that won the contention
582 // but did not transmit anything
583 i--;
584 k = std::distance(m_txops.begin(), i);
585 }
586 }
587 i++;
588 }
589}
590
591void
593{
594 NS_LOG_FUNCTION(this);
598}
599
600Time
602{
603 NS_LOG_FUNCTION(this);
604 const Time& sifs = GetSifs();
605 Time rxAccessStart = m_lastRx.end + sifs;
607 {
608 rxAccessStart += GetEifsNoDifs();
609 }
610 // an EDCA TXOP is obtained based solely on activity of the primary channel
611 // (Sec. 10.23.2.5 of IEEE 802.11-2020)
612 Time busyAccessStart = m_lastBusyEnd.at(WIFI_CHANLIST_PRIMARY) + sifs;
613 Time txAccessStart = m_lastTxEnd + sifs;
614 Time navAccessStart = m_lastNavEnd + sifs;
615 Time ackTimeoutAccessStart = m_lastAckTimeoutEnd + sifs;
616 Time ctsTimeoutAccessStart = m_lastCtsTimeoutEnd + sifs;
617 Time switchingAccessStart = m_lastSwitchingEnd + sifs;
618 Time accessGrantedStart;
619 if (ignoreNav)
620 {
621 accessGrantedStart = std::max({rxAccessStart,
622 busyAccessStart,
623 txAccessStart,
624 ackTimeoutAccessStart,
625 ctsTimeoutAccessStart,
626 switchingAccessStart});
627 }
628 else
629 {
630 accessGrantedStart = std::max({rxAccessStart,
631 busyAccessStart,
632 txAccessStart,
633 navAccessStart,
634 ackTimeoutAccessStart,
635 ctsTimeoutAccessStart,
636 switchingAccessStart});
637 }
638 NS_LOG_INFO("access grant start=" << accessGrantedStart.As(Time::US)
639 << ", rx access start=" << rxAccessStart.As(Time::US)
640 << ", busy access start=" << busyAccessStart.As(Time::US)
641 << ", tx access start=" << txAccessStart.As(Time::US)
642 << ", nav access start=" << navAccessStart.As(Time::US)
643 << ", switching access start="
644 << switchingAccessStart.As(Time::US));
645 return accessGrantedStart;
646}
647
648Time
650{
651 NS_LOG_FUNCTION(this << txop);
652 Time mostRecentEvent =
653 std::max({txop->GetBackoffStart(m_linkId),
654 GetAccessGrantStart() + (txop->GetAifsn(m_linkId) * GetSlot())});
655 NS_LOG_DEBUG("Backoff start for " << txop->GetWifiMacQueue()->GetAc() << ": "
656 << mostRecentEvent.As(Time::US));
657
658 return mostRecentEvent;
659}
660
661Time
663{
664 NS_LOG_FUNCTION(this << txop);
665 Time backoffEnd = GetBackoffStartFor(txop) + (txop->GetBackoffSlots(m_linkId) * GetSlot());
666 NS_LOG_DEBUG("Backoff end for " << txop->GetWifiMacQueue()->GetAc() << ": "
667 << backoffEnd.As(Time::US));
668
669 return backoffEnd;
670}
671
672void
674{
675 NS_LOG_FUNCTION(this);
676 uint32_t k = 0;
677 for (auto txop : m_txops)
678 {
679 Time backoffStart = GetBackoffStartFor(txop);
680 if (backoffStart <= Simulator::Now())
681 {
682 uint32_t nIntSlots = ((Simulator::Now() - backoffStart) / GetSlot()).GetHigh();
683 /*
684 * EDCA behaves slightly different to DCA. For EDCA we
685 * decrement once at the slot boundary at the end of AIFS as
686 * well as once at the end of each clear slot
687 * thereafter. For DCA we only decrement at the end of each
688 * clear slot after DIFS. We account for the extra backoff
689 * by incrementing the slot count here in the case of
690 * EDCA. The if statement whose body we are in has confirmed
691 * that a minimum of AIFS has elapsed since last busy
692 * medium.
693 */
694 if (txop->IsQosTxop())
695 {
696 nIntSlots++;
697 }
698 uint32_t n = std::min(nIntSlots, txop->GetBackoffSlots(m_linkId));
699 NS_LOG_DEBUG("dcf " << k << " dec backoff slots=" << n);
700 Time backoffUpdateBound = backoffStart + (n * GetSlot());
701 txop->UpdateBackoffSlotsNow(n, backoffUpdateBound, m_linkId);
702 }
703 ++k;
704 }
705}
706
707void
709{
710 NS_LOG_FUNCTION(this);
715 bool accessTimeoutNeeded = false;
716 Time expectedBackoffEnd = Simulator::GetMaximumSimulationTime();
717 for (auto txop : m_txops)
718 {
719 if (txop->GetAccessStatus(m_linkId) == Txop::REQUESTED)
720 {
721 Time tmp = GetBackoffEndFor(txop);
722 if (tmp > Simulator::Now())
723 {
724 accessTimeoutNeeded = true;
725 expectedBackoffEnd = std::min(expectedBackoffEnd, tmp);
726 }
727 }
728 }
729 NS_LOG_DEBUG("Access timeout needed: " << accessTimeoutNeeded);
730 if (accessTimeoutNeeded)
731 {
732 NS_LOG_DEBUG("expected backoff end=" << expectedBackoffEnd);
733 Time expectedBackoffDelay = expectedBackoffEnd - Simulator::Now();
735 Simulator::GetDelayLeft(m_accessTimeout) > expectedBackoffDelay)
736 {
738 }
740 {
741 m_accessTimeout = Simulator::Schedule(expectedBackoffDelay,
743 this);
744 }
745 }
746}
747
748uint16_t
750{
751 NS_LOG_FUNCTION(this << interval.As(Time::US) << end.As(Time::S));
752
753 // If the medium is busy or it just became idle, UpdateLastIdlePeriod does
754 // nothing. This allows us to call this method, e.g., at the end of a frame
755 // reception and check the busy/idle status of the channel before the start
756 // of the frame reception (last idle period was last updated at the start of
757 // the frame reception).
758 // If the medium has been idle for some time, UpdateLastIdlePeriod updates
759 // the last idle period. This is normally what we want because this method may
760 // also be called before starting a TXOP gained through EDCA.
762
763 uint16_t width = 0;
764
765 // we iterate over the different types of channels in the same order as they
766 // are listed in WifiChannelListType
767 for (const auto& lastIdle : m_lastIdle)
768 {
769 if (lastIdle.second.start <= end - interval && lastIdle.second.end >= end)
770 {
771 // channel idle, update width
772 width = (width == 0) ? 20 : (2 * width);
773 }
774 else
775 {
776 break;
777 }
778 }
779 return width;
780}
781
782bool
783ChannelAccessManager::GetPer20MHzBusy(const std::set<uint8_t>& indices) const
784{
785 const auto now = Simulator::Now();
786
787 if (m_phy->GetChannelWidth() < 40)
788 {
789 NS_ASSERT_MSG(indices.size() == 1 && *indices.cbegin() == 0,
790 "Index 0 only can be specified if the channel width is less than 40 MHz");
791 return m_lastBusyEnd.at(WIFI_CHANLIST_PRIMARY) > now;
792 }
793
794 for (const auto index : indices)
795 {
796 NS_ASSERT(index < m_lastPer20MHzBusyEnd.size());
797 if (m_lastPer20MHzBusyEnd.at(index) > now)
798 {
799 NS_LOG_DEBUG("20 MHz channel with index " << +index << " is busy");
800 return true;
801 }
802 }
803 return false;
804}
805
806void
808{
809 NS_LOG_FUNCTION(this << qosTxop << duration);
810 NS_ASSERT(qosTxop->IsQosTxop());
812 Time resume = Simulator::Now() + duration;
813 NS_LOG_DEBUG("Backoff will resume at time " << resume << " with "
814 << qosTxop->GetBackoffSlots(m_linkId)
815 << " remaining slot(s)");
816 qosTxop->UpdateBackoffSlotsNow(0, resume, m_linkId);
818}
819
820void
822{
823 NS_LOG_FUNCTION(this << enable);
825}
826
827bool
829{
831}
832
833void
835{
836 NS_LOG_FUNCTION(this << duration);
837 NS_LOG_DEBUG("rx start for=" << duration);
841 m_lastRx.end = m_lastRx.start + duration;
842 m_lastRxReceivedOk = true;
843}
844
845void
847{
848 NS_LOG_FUNCTION(this);
849 NS_LOG_DEBUG("rx end ok");
851 m_lastRxReceivedOk = true;
852}
853
854void
856{
857 NS_LOG_FUNCTION(this);
858 NS_LOG_DEBUG("rx end error");
859 // we expect the PHY to notify us of the start of a CCA busy period, if needed
861 m_lastRxReceivedOk = false;
862}
863
864void
866{
867 NS_LOG_FUNCTION(this << duration);
868 m_lastRxReceivedOk = true;
869 Time now = Simulator::Now();
870 if (m_lastRx.end > now)
871 {
872 // this may be caused only if PHY has started to receive a packet
873 // inside SIFS, so, we check that lastRxStart was maximum a SIFS ago
874 NS_ASSERT(now - m_lastRx.start <= GetSifs());
875 m_lastRx.end = now;
876 }
877 else
878 {
880 }
881 NS_LOG_DEBUG("tx start for " << duration);
883 m_lastTxEnd = now + duration;
884}
885
886void
888 WifiChannelListType channelType,
889 const std::vector<Time>& per20MhzDurations)
890{
891 NS_LOG_FUNCTION(this << duration << channelType);
894 auto lastBusyEndIt = m_lastBusyEnd.find(channelType);
895 NS_ASSERT(lastBusyEndIt != m_lastBusyEnd.end());
896 Time now = Simulator::Now();
897 lastBusyEndIt->second = now + duration;
898 NS_ASSERT_MSG(per20MhzDurations.size() == m_lastPer20MHzBusyEnd.size(),
899 "Size of received vector (" << per20MhzDurations.size()
900 << ") differs from the expected size ("
901 << m_lastPer20MHzBusyEnd.size() << ")");
902 for (std::size_t chIdx = 0; chIdx < per20MhzDurations.size(); ++chIdx)
903 {
904 if (per20MhzDurations[chIdx].IsStrictlyPositive())
905 {
906 m_lastPer20MHzBusyEnd[chIdx] = now + per20MhzDurations[chIdx];
907 }
908 }
909}
910
911void
913{
914 NS_LOG_FUNCTION(this << phyListener << duration);
915
916 Time now = Simulator::Now();
917 NS_ASSERT(m_lastTxEnd <= now);
919
920 if (phyListener) // to make tests happy
921 {
922 // check if the PHY switched channel to operate on another EMLSR link
923
924 for (const auto& [phyRef, listener] : m_phyListeners)
925 {
926 Ptr<WifiPhy> phy = phyRef;
927 auto emlsrInfoIt = m_switchingEmlsrLinks.find(phy);
928
929 if (listener.get() == phyListener && emlsrInfoIt != m_switchingEmlsrLinks.cend() &&
930 phy->GetOperatingChannel() == emlsrInfoIt->second.channel)
931 {
932 // the PHY associated with the given PHY listener switched channel to
933 // operate on another EMLSR link as expected. We don't need this listener
934 // anymore. The MAC will connect a new listener to the ChannelAccessManager
935 // instance associated with the link the PHY is now operating on
937 auto ehtFem = DynamicCast<EhtFrameExchangeManager>(m_feManager);
938 NS_ASSERT(ehtFem);
939 ehtFem->NotifySwitchingEmlsrLink(phy, emlsrInfoIt->second.linkId, duration);
940 m_switchingEmlsrLinks.erase(emlsrInfoIt);
941 return;
942 }
943 }
944 }
945
946 ResetState();
947
948 // Reset backoffs
949 for (const auto& txop : m_txops)
950 {
951 ResetBackoff(txop);
952 }
953
954 // Notify the FEM, which will in turn notify the MAC
955 m_feManager->NotifySwitchingStartNow(duration);
956
957 NS_LOG_DEBUG("switching start for " << duration);
958 m_lastSwitchingEnd = now + duration;
959}
960
961void
963{
964 NS_LOG_FUNCTION(this);
965
966 Time now = Simulator::Now();
967 m_lastRxReceivedOk = true;
969 m_lastRx.end = std::min(m_lastRx.end, now);
970 m_lastNavEnd = std::min(m_lastNavEnd, now);
973
975
976 // Cancel timeout
978 {
980 }
981}
982
983void
985{
986 NS_LOG_FUNCTION(this << txop);
987
988 uint32_t remainingSlots = txop->GetBackoffSlots(m_linkId);
989 if (remainingSlots > 0)
990 {
991 txop->UpdateBackoffSlotsNow(remainingSlots, Simulator::Now(), m_linkId);
992 NS_ASSERT(txop->GetBackoffSlots(m_linkId) == 0);
993 }
994 txop->ResetCw(m_linkId);
995 txop->GetLink(m_linkId).access = Txop::NOT_REQUESTED;
996}
997
998void
1000{
1001 NS_LOG_FUNCTION(this);
1002
1003 for (const auto& txop : m_txops)
1004 {
1005 ResetBackoff(txop);
1006 }
1008}
1009
1010void
1012{
1013 NS_LOG_FUNCTION(this);
1014 m_sleeping = true;
1015 // Cancel timeout
1017 {
1019 }
1020
1021 // Reset backoffs
1022 for (auto txop : m_txops)
1023 {
1024 txop->NotifySleep(m_linkId);
1025 }
1026}
1027
1028void
1030{
1031 NS_LOG_FUNCTION(this);
1032 m_off = true;
1033 // Cancel timeout
1035 {
1037 }
1038
1039 // Reset backoffs
1040 for (auto txop : m_txops)
1041 {
1042 txop->NotifyOff();
1043 }
1044}
1045
1046void
1048{
1049 NS_LOG_FUNCTION(this);
1050 m_sleeping = false;
1051 for (auto txop : m_txops)
1052 {
1053 ResetBackoff(txop);
1054 txop->NotifyWakeUp(m_linkId);
1055 }
1056}
1057
1058void
1060{
1061 NS_LOG_FUNCTION(this);
1062 m_off = false;
1063 for (auto txop : m_txops)
1064 {
1065 ResetBackoff(txop);
1066 txop->NotifyOn();
1067 }
1068}
1069
1070void
1072{
1073 NS_LOG_FUNCTION(this << duration);
1074
1075 if (!m_phy)
1076 {
1077 NS_LOG_DEBUG("Do not reset NAV, CTS may have been missed due to the main PHY switching "
1078 "to another link to take over a TXOP while receiving the CTS");
1079 return;
1080 }
1081
1082 NS_LOG_DEBUG("nav reset for=" << duration);
1083 UpdateBackoff();
1084 m_lastNavEnd = Simulator::Now() + duration;
1092}
1093
1094void
1096{
1097 NS_LOG_FUNCTION(this << duration);
1098 NS_LOG_DEBUG("nav start for=" << duration);
1099 UpdateBackoff();
1100 m_lastNavEnd = std::max(m_lastNavEnd, Simulator::Now() + duration);
1101}
1102
1103void
1105{
1106 NS_LOG_FUNCTION(this << duration);
1108 m_lastAckTimeoutEnd = Simulator::Now() + duration;
1109}
1110
1111void
1113{
1114 NS_LOG_FUNCTION(this);
1117}
1118
1119void
1121{
1122 NS_LOG_FUNCTION(this << duration);
1123 m_lastCtsTimeoutEnd = Simulator::Now() + duration;
1124}
1125
1126void
1128{
1129 NS_LOG_FUNCTION(this);
1132}
1133
1134void
1136{
1137 NS_LOG_FUNCTION(this);
1138 m_usingOtherEmlsrLink = true;
1139}
1140
1141void
1143{
1144 NS_LOG_FUNCTION(this);
1145 m_usingOtherEmlsrLink = false;
1146}
1147
1148void
1150{
1151 NS_LOG_FUNCTION(this);
1152 Time idleStart = std::max({m_lastTxEnd, m_lastRx.end, m_lastSwitchingEnd});
1153 Time now = Simulator::Now();
1154
1155 if (idleStart >= now)
1156 {
1157 // No new idle period
1158 return;
1159 }
1160
1161 for (const auto& busyEnd : m_lastBusyEnd)
1162 {
1163 if (busyEnd.second < now)
1164 {
1165 auto lastIdleIt = m_lastIdle.find(busyEnd.first);
1166 NS_ASSERT(lastIdleIt != m_lastIdle.end());
1167 lastIdleIt->second = {std::max(idleStart, busyEnd.second), now};
1168 NS_LOG_DEBUG("New idle period (" << lastIdleIt->second.start.As(Time::S) << ", "
1169 << lastIdleIt->second.end.As(Time::S)
1170 << ") on channel " << lastIdleIt->first);
1171 }
1172 }
1173}
1174
1175} // namespace ns3
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Manage a set of ns3::Txop.
uint16_t GetLargestIdlePrimaryChannel(Time interval, Time end)
Return the width of the largest primary channel that has been idle for the given time interval before...
std::vector< Time > m_lastPer20MHzBusyEnd
the last busy end time per 20 MHz channel (HE stations and channel width > 20 MHz only)
bool IsBusy() const
Check if the device is busy sending or receiving, or NAV or CCA busy.
void ResetBackoff(Ptr< Txop > txop)
Reset the backoff for the given DCF/EDCAF.
void NotifyRxEndErrorNow()
Notify the Txop that a packet reception was just completed unsuccessfuly.
bool m_off
flag whether it is in off state
void NotifyRxStartNow(Time duration)
void NotifySwitchingStartNow(PhyListener *phyListener, Time duration)
Time GetBackoffEndFor(Ptr< Txop > txop)
Return the time when the backoff procedure ended (or will ended) for the given Txop.
void ResetState()
Reset the state variables of this channel access manager.
void NotifySwitchingEmlsrLink(Ptr< WifiPhy > phy, const WifiPhyOperatingChannel &channel, uint8_t linkId)
Notify that the given PHY is about to switch to the given operating channel, which is used by the giv...
void NotifyStopUsingOtherEmlsrLink()
Notify that another EMLSR link is no longer being used, hence medium access can be resumed.
void ResetAllBackoffs()
Reset the backoff for all the DCF/EDCAF.
void NotifyWakeupNow()
Notify the Txop that the device has been resumed from sleep mode.
bool m_lastRxReceivedOk
the last receive OK
std::unordered_map< Ptr< WifiPhy >, EmlsrLinkSwitchInfo > m_switchingEmlsrLinks
Store information about the PHY objects that are going to operate on another EMLSR link.
std::map< WifiChannelListType, Timespan > m_lastIdle
the last idle start and end time for each channel type
Ptr< WifiPhy > m_phy
pointer to the unique active PHY
void NotifyAckTimeoutResetNow()
Notify that ack timer has reset.
void SetGenerateBackoffOnNoTx(bool enable)
Set the member variable indicating whether the backoff should be invoked when an AC gains the right t...
void NotifyTxStartNow(Time duration)
void NotifyRxEndOkNow()
Notify the Txop that a packet reception was just completed successfully.
virtual Time GetEifsNoDifs() const
Return the EIFS duration minus a DIFS.
uint8_t m_linkId
the ID of the link this object is associated with
void NotifyCcaBusyStartNow(Time duration, WifiChannelListType channelType, const std::vector< Time > &per20MhzDurations)
Time m_lastAckTimeoutEnd
the last Ack timeout end time
virtual Time GetSlot() const
Return the slot duration for this PHY.
void NotifyAckTimeoutStartNow(Time duration)
Notify that ack timer has started for the given duration.
void AccessTimeout()
Called when access timeout should occur (e.g.
void UpdateBackoff()
Update backoff slots for all Txops.
void DeactivatePhyListener(Ptr< WifiPhy > phy)
Deactivate current registered listener for PHY events on the given PHY.
bool m_sleeping
flag whether it is in sleeping state
void SetLinkId(uint8_t linkId)
Set the ID of the link this Channel Access Manager is associated with.
void SetupFrameExchangeManager(Ptr< FrameExchangeManager > feManager)
Set up the Frame Exchange Manager.
bool NeedBackoffUponAccess(Ptr< Txop > txop, bool hadFramesToTransmit, bool checkMediumBusy)
Determine if a new backoff needs to be generated as per letter a) of Section 10.23....
void NotifyCtsTimeoutStartNow(Time duration)
Notify that CTS timer has started for the given duration.
void RequestAccess(Ptr< Txop > txop)
Time m_lastSwitchingEnd
the last switching end time
Timespan m_lastRx
the last receive start and end time
std::map< WifiChannelListType, Time > m_lastBusyEnd
the last busy end time for each channel type
void RemovePhyListener(Ptr< WifiPhy > phy)
Remove current registered listener for PHY events on the given PHY.
bool m_generateBackoffOnNoTx
whether the backoff should be invoked when the AC gains the right to start a TXOP but it does not tra...
Time m_lastTxEnd
the last transmit end time
void SetupPhyListener(Ptr< WifiPhy > phy)
Set up (or reactivate) listener for PHY events on the given PHY.
void NotifyStartUsingOtherEmlsrLink()
Notify that another EMLSR link is being used, hence medium access should be disabled.
Time m_lastCtsTimeoutEnd
the last CTS timeout end time
void DoDispose() override
Destructor implementation.
void NotifySleepNow()
Notify the Txop that the device has been put in sleep mode.
Ptr< FrameExchangeManager > m_feManager
pointer to the Frame Exchange Manager
bool m_usingOtherEmlsrLink
whether another EMLSR link is being used
void UpdateLastIdlePeriod()
This method determines whether the medium has been idle during a period (of non-null duration) immedi...
void DisableEdcaFor(Ptr< Txop > qosTxop, Time duration)
void DoInitialize() override
Initialize() implementation.
Txops m_txops
the vector of managed Txops
bool GetPer20MHzBusy(const std::set< uint8_t > &indices) const
static TypeId GetTypeId()
Get the type ID.
void DoGrantDcfAccess()
Grant access to Txop using DCF/EDCF contention rules.
std::shared_ptr< PhyListener > GetPhyListener(Ptr< WifiPhy > phy) const
Get current registered listener for PHY events on the given PHY.
Time m_lastNavEnd
the last NAV end time
void NotifyCtsTimeoutResetNow()
Notify that CTS timer has reset.
void NotifyOffNow()
Notify the Txop that the device has been put in off mode.
Time GetAccessGrantStart(bool ignoreNav=false) const
Access will never be granted to the medium before the time returned by this method.
void NotifyOnNow()
Notify the Txop that the device has been resumed from off mode.
Time GetBackoffStartFor(Ptr< Txop > txop)
Return the time when the backoff procedure started for the given Txop.
PhyListenerMap m_phyListeners
the PHY listeners
virtual Time GetSifs() const
Return the Short Interframe Space (SIFS) for this PHY.
EventId m_accessTimeout
the access timeout ID
void InitLastBusyStructs()
Initialize the structures holding busy end times per channel type (primary, secondary,...
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
A base class which provides memory management and object aggregation.
Definition: object.h:89
Listener for PHY events.
bool m_active
whether this PHY listener is active
PhyListener(ns3::ChannelAccessManager *cam)
Create a PhyListener for the given ChannelAccessManager.
void NotifyOff() override
Notify listeners that we went to switch off.
void NotifySleep() override
Notify listeners that we went to sleep.
ns3::ChannelAccessManager * m_cam
ChannelAccessManager to forward events to.
void NotifyTxStart(Time duration, double txPowerDbm) override
void NotifyRxStart(Time duration) override
void NotifyRxEndError() override
We have received the last bit of a packet for which NotifyRxStart was invoked first and,...
void NotifyOn() override
Notify listeners that we went to switch on.
void NotifySwitchingStart(Time duration) override
void SetActive(bool active)
Set this listener to be active or not.
void NotifyRxEndOk() override
We have received the last bit of a packet for which NotifyRxStart was invoked first and,...
void NotifyWakeup() override
Notify listeners that we woke up.
void NotifyCcaBusyStart(Time duration, WifiChannelListType channelType, const std::vector< Time > &per20MhzDurations) override
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static Time GetMaximumSimulationTime()
Get the maximum representable simulation time.
Definition: simulator.cc:311
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:217
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:415
@ US
microsecond
Definition: nstime.h:118
@ S
second
Definition: nstime.h:116
@ GRANTED
Definition: txop.h:105
@ NOT_REQUESTED
Definition: txop.h:103
@ REQUESTED
Definition: txop.h:104
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Time GetSlot() const
Return the slot duration for this PHY.
Definition: wifi-phy.cc:793
uint16_t GetChannelWidth() const
Definition: wifi-phy.cc:1051
Time GetSifs() const
Return the Short Interframe Space (SIFS) for this PHY.
Definition: wifi-phy.cc:781
WifiPhyBand GetPhyBand() const
Get the configured Wi-Fi band.
Definition: wifi-phy.cc:1021
Time GetPifs() const
Return the PCF Interframe Space (PIFS) for this PHY.
Definition: wifi-phy.cc:805
WifiStandard GetStandard() const
Get the configured Wi-Fi standard.
Definition: wifi-phy.cc:1027
void NotifyChannelAccessRequested()
Notify the PHY that an access to the channel was requested.
Definition: wifi-phy.cc:1907
Time GetAckTxTime() const
Return the estimated Ack TX time for this PHY.
Definition: wifi-phy.cc:811
const WifiPhyOperatingChannel & GetOperatingChannel() const
Get a const reference to the operating channel.
Definition: wifi-phy.cc:1033
receive notifications about PHY events.
Class that keeps track of all information about the current PHY operating channel.
bool IsOfdm() const
Return whether the operating channel is an OFDM channel.
#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
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
#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_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
WifiChannelListType
Enumeration of the possible channel-list parameter elements defined in Table 8-5 of IEEE 802....
@ WIFI_STANDARD_80211ax
@ WIFI_PHY_BAND_2_4GHZ
The 2.4 GHz band.
Definition: wifi-phy-band.h:35
@ WIFI_CHANLIST_PRIMARY
@ WIFI_CHANLIST_SECONDARY40
@ WIFI_CHANLIST_SECONDARY
@ WIFI_CHANLIST_SECONDARY80
Every class exported by the ns3 library is enclosed in the ns3 namespace.