A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tid-to-link-mapping-element.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sharan Naribole <sharan.naribole@gmail.com>
7 */
8
10
11#include "ns3/assert.h"
12#include "ns3/simulator.h"
13
14namespace ns3
15{
16
17/// Bitmask with all bits from 63 to 26 set to 1, all the others set to 0
18static constexpr uint64_t BIT_63_TO_26_MASK = 0xfffffffffc000000;
19
20uint16_t
22{
23 // IEEE 802.11be D3.1 Figure 9-1002ap
25 "Presence bitmap not expected if default mapping is set");
27 if (!presenceBitmap.has_value())
28 {
29 return size;
30 }
32}
33
34void
36{
37 auto val = static_cast<uint8_t>(direction) | ((defaultMapping ? 1 : 0) << 2) |
38 ((mappingSwitchTimePresent ? 1 : 0) << 3) |
39 ((expectedDurationPresent ? 1 : 0) << 4) | ((linkMappingSize == 1 ? 1 : 0) << 5);
40
41 start.WriteU8(val);
42 NS_ASSERT_MSG(!defaultMapping || !presenceBitmap.has_value(),
43 "Presence bitmap not expected if default mapping is set");
44 if (presenceBitmap.has_value())
45 {
46 start.WriteU8(presenceBitmap.value());
47 }
48}
49
50uint16_t
52{
53 auto i = start;
54 uint8_t count = 0;
55 auto val = i.ReadU8();
56 count++;
57
58 direction = static_cast<WifiDirection>(val & 0x03);
59 defaultMapping = (((val >> 2) & 0x01) == 1);
60 mappingSwitchTimePresent = (((val >> 3) & 0x01) == 1);
61 expectedDurationPresent = (((val >> 4) & 0x01) == 1);
62 linkMappingSize = (((val >> 5) & 0x01) == 1 ? 1 : 2);
63 if (defaultMapping)
64 {
65 presenceBitmap.reset();
66 return count;
67 }
68 presenceBitmap = i.ReadU8();
69 return ++count;
70}
71
74{
75 return IE_EXTENSION;
76}
77
83
84void
86{
87 // The 2 octet Mapping Switch Time field has units of TUs and is set to the time at which
88 // the new mapping is established using as a time-base the value of the TSF corresponding
89 // to the BSS identified by the BSSID of the frame containing the TID-To-Link Mapping
90 // element: i.e., bits 10 to 25 of the TSF. (Sec. 9.4.2.314 of 802.11be D3.1)
91 NS_ABORT_IF(mappingSwitchTime < Simulator::Now());
92 auto switchTimeUsec = static_cast<uint64_t>(mappingSwitchTime.GetMicroSeconds());
93 // set the Mapping Switch Time field to bits 10 to 25 of the given time
94 m_mappingSwitchTime = (switchTimeUsec & ~BIT_63_TO_26_MASK) >> 10;
96}
97
98std::optional<Time>
100{
102 {
103 return std::nullopt;
104 }
105
106 auto nowUsec = static_cast<uint64_t>(Simulator::Now().GetMicroSeconds());
107 uint64_t switchTimeUsec = (*m_mappingSwitchTime << 10) + (nowUsec & BIT_63_TO_26_MASK);
108 if (switchTimeUsec < nowUsec)
109 {
110 // The switch time derived from the value in the corresponding field may be less than the
111 // current time in case the bits 10 to 25 of TSF have been reset since the transmission
112 // of the frame carrying this field. In such a case we have to increase bits 63 to 26 by 1
113 switchTimeUsec += (1 << 26);
114 }
115 return MicroSeconds(switchTimeUsec);
116}
117
118void
120{
121 auto durationTu = static_cast<uint64_t>(expectedDuration.GetMicroSeconds()) >> 10;
122 m_expectedDuration = (durationTu & 0x0000000000ffffff); // Expected Duration size is 3 bytes
124}
125
126std::optional<Time>
128{
130 {
131 return std::nullopt;
132 }
133
134 return MicroSeconds(*m_expectedDuration << 10);
135}
136
137void
138TidToLinkMapping::SetLinkMappingOfTid(uint8_t tid, std::set<uint8_t> linkIds)
139{
140 NS_ABORT_MSG_IF(tid > 7, "Invalid tid: " << +tid);
142 "Per-TID link mapping not expected if default mapping is set");
143
144 // derive link mapping for the given TID
145 uint16_t linkMapping = 0;
146
147 for (const auto& linkId : linkIds)
148 {
149 linkMapping |= (1 << linkId);
150 if (linkId > 7)
151 {
153 }
154 }
155
156 m_linkMapping[tid] = linkMapping;
157 m_control.presenceBitmap = m_control.presenceBitmap.value_or(0) | (1 << tid);
158}
159
160std::set<uint8_t>
162{
163 auto it = m_linkMapping.find(tid);
164
165 if (it == m_linkMapping.cend())
166 {
167 return {};
168 }
169
170 std::set<uint8_t> linkIds;
171 for (uint8_t linkId = 0; linkId < 15; linkId++)
172 {
173 if (((it->second >> linkId) & 0x0001) == 1)
174 {
175 linkIds.insert(linkId);
176 }
177 }
178 NS_ABORT_MSG_IF(linkIds.empty(), "TID " << +tid << " cannot be mapped to an empty link set");
179
180 return linkIds;
181}
182
183uint16_t
185{
186 // IEEE 802.11be D3.1 9.4.2.314 TID-To-Link Mapping element
187 uint16_t ret = WIFI_IE_ELEMENT_ID_EXT_SIZE; // Element ID Extension
188 ret += m_control.GetSubfieldSize();
190 {
191 ret += 2; // Mapping Switch Time
192 }
194 {
195 ret += 3; // Expected Duration
196 }
197
199 "Per-TID link mapping not expected if default mapping is set");
200 ret += m_control.linkMappingSize * (m_linkMapping.size());
201 return ret;
202}
203
204void
206{
207 // IEEE 802.11be D3.1 9.4.2.314 TID-To-Link Mapping element
208 m_control.Serialize(start);
210 {
211 start.WriteHtolsbU16(*m_mappingSwitchTime);
212 }
214 {
215 start.WriteU8((*m_expectedDuration >> 0) & 0xff);
216 start.WriteU8((*m_expectedDuration >> 8) & 0xff);
217 start.WriteU8((*m_expectedDuration >> 16) & 0xff);
218 }
219
221 "Per-TID link mapping not expected if default mapping is set");
222
223 for (const auto& [tid, linkMapping] : m_linkMapping)
224 {
225 if (m_control.linkMappingSize == 1)
226 {
227 start.WriteU8(linkMapping);
228 }
229 else
230 {
231 start.WriteHtolsbU16(linkMapping);
232 }
233 }
234}
235
236uint16_t
238{
239 auto i = start;
240 uint16_t count = 0;
241 auto nCtrlOctets = m_control.Deserialize(i);
242 NS_ASSERT_MSG(nCtrlOctets <= length, "Tid-to-Link Mapping deserialize error");
243 i.Next(nCtrlOctets);
244 count += nCtrlOctets;
246 {
247 m_mappingSwitchTime = i.ReadLsbtohU16();
248 count += 2;
249 }
251 {
252 uint8_t byte0 = i.ReadU8();
253 uint8_t byte1 = i.ReadU8();
254 uint8_t byte2 = i.ReadU8();
255 m_expectedDuration = byte2;
256 m_expectedDuration.value() <<= 8;
257 m_expectedDuration.value() |= byte1;
258 m_expectedDuration.value() <<= 8;
259 m_expectedDuration.value() |= byte0;
260 count += 3;
261 }
262 m_linkMapping.clear();
263 if (m_control.presenceBitmap.has_value())
264 {
266 "Default mapping should not be set when presence bitmap is present");
267 const auto presenceBitmap = m_control.presenceBitmap.value();
268 for (uint8_t tid = 0; tid < 8; tid++)
269 {
270 if (((presenceBitmap >> tid) & 0x01) == 1)
271 {
272 if (m_control.linkMappingSize == 1)
273 {
274 m_linkMapping[tid] = i.ReadU8();
275 count++;
276 }
277 else
278 {
279 m_linkMapping[tid] = i.ReadLsbtohU16();
280 count += 2;
281 }
282 }
283 }
284 }
285
286 NS_ABORT_MSG_IF(count != length,
287 "TID-to-Link Mapping Length (" << +length
288 << ") differs "
289 "from actual number of bytes read ("
290 << +count << ")");
291 return count;
292}
293
294} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetMicroSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:402
#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:75
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition abort.h:65
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1332
Every class exported by the ns3 library is enclosed in the ns3 namespace.
constexpr uint16_t WIFI_TID_TO_LINK_MAPPING_CONTROL_BASIC_SIZE_B
size in bytes of the TID-To-Link Control field with default link mapping
static constexpr uint64_t BIT_63_TO_26_MASK
Bitmask with all bits from 63 to 26 set to 1, all the others set to 0.
constexpr uint8_t WIFI_IE_ELEMENT_ID_EXT_SIZE
Size in bytes of the Element ID Extension field (IEEE 802.11-2020 9.4.2.1 General)
WifiDirection
Wifi direction.
Definition wifi-utils.h:34
constexpr uint16_t WIFI_LINK_MAPPING_PRESENCE_IND_SIZE_B
size in bytes of the Link Mapping Presence Indicator field (IEEE 802.11be D2.0 9.4....
uint8_t WifiInformationElementId
This type is used to represent an Information Element ID.
#define IE_EXTENSION
#define IE_EXT_TID_TO_LINK_MAPPING_ELEMENT