A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-roaming.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Stefano Avallone <stavallo@unina.it>
5 */
6
7#include "ns3/applications-module.h"
8#include "ns3/bridge-module.h"
9#include "ns3/core-module.h"
10#include "ns3/csma-module.h"
11#include "ns3/internet-module.h"
12#include "ns3/mobility-module.h"
13#include "ns3/network-module.h"
14#include "ns3/propagation-module.h"
15#include "ns3/spectrum-module.h"
16#include "ns3/wifi-module.h"
17
18#include <algorithm>
19#include <fstream>
20#include <iostream>
21#include <map>
22#include <vector>
23
24/**
25 * @file
26 *
27 * This example considers two APs and a server connected to a CSMA bridge. A STA is initially
28 * associated with AP 1 and can be configured to move closer to and associate with AP 2 at a given
29 * point in time. It is possible to configure whether or not the STA notifies the disassociation to
30 * AP 1 when roaming. DL and/or UL UDP (or TCP) traffic can be exchanged between STA and the server.
31 *
32 * Network (logical) topology
33 *
34 * ┌─────────────────────┐
35 * │ CSMA bridge │
36 * └┬─────────┬─────────┬┘
37 * │ │ │ <-- CSMA links
38 * AP 1 AP 2 Server
39 *
40 *
41 * Implementation
42 *
43 * @verbatim
44 CSMA bridge
45 ┌───────────────────────────────────┐
46 │ Bridge │
47 ├───────────┬───────────┬───────────┤
48 │CSMA NetDev│CSMA NetDev│CSMA NetDev│
49 └──┬────────┴──────┬────┴──────┬────┘
50 │ │ │
51 ┌──────┬──┴───┐ ┌──────┬──┴───┐ ┌──┴───┐
52 │ WiFi │ CSMA │ │ WiFi │ CSMA │ │ CSMA │
53 │NetDev│NetDev│ │NetDev│NetDev│ │NetDev│
54 ├──────┴──────┤ ├──────┴──────┤ └──────┘
55 │ Bridge │ │ Bridge │ Server
56 └─────────────┘ └─────────────┘
57 AP 1 AP 2
58 @endverbatim
59 *
60 * The association messages and the throughput reports printed before and after the roam are the
61 * most interesting output. The time between the disassociation from AP 1 and the association with
62 * AP 2 is the roaming interruption. The subsequent block acknowledgment (BA) agreement messages
63 * indicate when the link is ready for aggregated QoS data. Compare the throughput on the two sides
64 * of the roam, and inspect the final per-device drop statistics and the queue sizes printed at the
65 * roam for packets that could not follow the STA.
66 *
67 * For example, the command used by the regression tests is:
68 *
69 * ./ns3 run "wifi-roaming --simulationTime=5s --disassocTime=2.5s --dlLoad=10Mbps --udp=0"
70 *
71 * It produces output similar to the following excerpt (exact times can vary):
72 *
73 * At time +2.5s, STA disassociated from BSSID 00:00:00:00:00:03
74 * From +1s to +2.5s:
75 * Wi-Fi STA received 1858000 bytes, DL throughput = 9909.33 kbps
76 * At time +2.50063s, STA completed association with BSSID 00:00:00:00:00:04
77 * At time +2.50135s, AP 2 established BA agreement with 00:00:00:00:00:05 for TID 0
78 * From +2.5s to +6s:
79 * Wi-Fi STA received 4374000 bytes, DL throughput = 9997.71 kbps
80 *
81 * Thus, in this run the STA moves from AP 1 to AP 2 in less than a millisecond, and TCP downlink
82 * throughput remains close to the offered 10 Mbps load after the roam.
83 *
84 * The following command-line arguments have particularly visible effects on this output:
85 *
86 * - `simulationTime` is the traffic duration after applications start at +1s; the simulation ends
87 * at +1s plus this value. It must leave enough time after `disassocTime` to observe recovery.
88 * - `disassocTime` sets the absolute simulation time of the roam and hence moves the association
89 * messages and the boundary between the two throughput reports. A value of zero disables the
90 * roam and the move toward AP 2.
91 * - `dlLoad` and `ulLoad` select the traffic direction and offered rate; a zero rate disables that
92 * direction and its throughput report. Higher loads can make packets queued or dropped during
93 * the interruption visible.
94 * - `notifyAp=1` makes the STA send a Disassociation frame. AP 1 then knows immediately that it can
95 * no longer forward frames to the STA, which can increase AP 1's final "Dropped because cannot
96 * be forwarded" count for downlink packets that still reach it.
97 * - `forwardFrameOnAssoc=1` (the default for this example) lets AP 2 update the CSMA bridge's
98 * learned port for the STA as soon as it associates. With downlink-only UDP traffic, setting
99 * `forwardFrameOnAssoc=0` leaves no reverse traffic to update the bridge: the association with
100 * AP 2 is still reported, but the example terminates because the post-roam downlink throughput
101 * is zero. TCP ACKs or traffic enabled by `ulLoad` can eventually update the bridge, so TCP
102 * typically shows a delayed BA agreement and reduced post-roam throughput instead of a complete
103 * outage.
104 * - `ap2Channels` and `staChannels` control whether the STA can discover AP 2. If they select no
105 * common channel, the STA may reassociate with AP 1 instead, and post-roam throughput drops.
106 */
107
108using namespace ns3;
109
110NS_LOG_COMPONENT_DEFINE("WifiRoamingExample");
111
112namespace
113{
114
115/**
116 * Wi-Fi setup parameters
117 */
119{
120 NodeContainer apNodes; ///< AP node container
121 NodeContainer staNodes; ///< STA node container
122 std::string ap1Channels{"{36, 0, BAND_5GHZ, 0}"}; ///< Semicolon separated (no spaces) list of
123 ///< channel settings for the links of AP 1
124 std::string ap2Channels; ///< Semicolon separated (no spaces) list of
125 ///< channel settings for the links of AP 2
126 std::string staChannels; ///< Semicolon separated (no spaces) list of
127 ///< channel settings for the links of STA
128};
129
130/**
131 * Packet drop statistics.
132 */
134{
135 std::size_t noFwdCount{0}; ///< count of packets dropped because cannot be forwarded
136 std::map<WifiMacDropReason, std::size_t> droppedMpdus; ///< per-reason count of dropped MPDUs
137};
138
139/**
140 * Get a SpectrumWifiPhyHelper object configured with the channel settings listed in the given
141 * input string and with the given Spectrum channel
142 *
143 * @param channelsStr the input string containing semicolon separated channel settings
144 * @param channel the given Spectrum channel
145 * @return a SpectrumWifiPhyHelper object configured with the given channel settings
146 */
148GetPhyHelper(const std::string& channelsStr, Ptr<SpectrumChannel> channel)
149{
150 auto strings = SplitString(channelsStr, ";");
151 SpectrumWifiPhyHelper phy{static_cast<uint8_t>(strings.size())};
152 for (std::size_t id = 0; id < strings.size(); ++id)
153 {
154 NS_LOG_INFO("PHY ID " << id << ": " << strings[id]);
155 phy.Set(id, "ChannelSettings", StringValue(strings[id]));
156 }
157 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
158 phy.SetChannel(channel);
159
160 return phy;
161}
162
163/**
164 * Create Wi-Fi devices.
165 *
166 * @param wifiParams the parameters to setup Wi-Fi devices
167 * @return a pair of containers for the AP and non-AP STA devices
168 */
169std::pair<NetDeviceContainer, NetDeviceContainer>
171{
172 NS_ABORT_MSG_IF(wifiParams.apNodes.GetN() != 2,
173 "Expected 2 AP nodes instead of " << wifiParams.apNodes.GetN());
174 NS_ABORT_MSG_IF(wifiParams.staNodes.GetN() != 1,
175 "Expected 1 STA node instead of " << wifiParams.staNodes.GetN());
176
177 WifiHelper wifi;
178 wifi.SetStandard(WIFI_STANDARD_80211be);
179 wifi.SetRemoteStationManager("ns3::ThompsonSamplingWifiManager");
180
181 WifiMacHelper mac;
182 Ssid ssid("wifi-roaming");
183
184 auto spectrumChannel = CreateObject<MultiModelSpectrumChannel>();
186 spectrumChannel->AddPropagationLossModel(lossModel);
187
188 NS_LOG_INFO("Configuring channel settings for AP 1");
189 auto ap1Phy = GetPhyHelper(wifiParams.ap1Channels, spectrumChannel);
190 mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
191 auto apDevices = wifi.Install(ap1Phy, mac, wifiParams.apNodes.Get(0));
192
193 NS_LOG_INFO("Configuring channel settings for AP 2");
194 auto ap2Phy = GetPhyHelper(wifiParams.ap2Channels.empty() ? wifiParams.ap1Channels
195 : wifiParams.ap2Channels,
196 spectrumChannel);
197 apDevices.Add(wifi.Install(ap2Phy, mac, wifiParams.apNodes.Get(1)));
198
199 NS_LOG_INFO("Configuring channel settings for STA");
200 auto staPhy = GetPhyHelper(wifiParams.staChannels.empty() ? wifiParams.ap1Channels
201 : wifiParams.staChannels,
202 spectrumChannel);
203 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
204 auto staDevices = wifi.Install(staPhy, mac, wifiParams.staNodes);
205
206 NS_LOG_INFO("AP 1 device address: " << apDevices.Get(0)->GetAddress());
207 NS_LOG_INFO("AP 2 device address: " << apDevices.Get(1)->GetAddress());
208 NS_LOG_INFO("STA device address: " << staDevices.Get(0)->GetAddress());
209
210 int64_t streamNumber = 100;
211 streamNumber += WifiHelper::AssignStreams(apDevices, streamNumber);
212 streamNumber += WifiHelper::AssignStreams(staDevices, streamNumber);
213
214 // mobility.
215 MobilityHelper mobility;
216 auto positionAlloc = CreateObject<ListPositionAllocator>();
217
218 positionAlloc->Add(Vector(0.0, 0.0, 0.0)); // AP 1
219 positionAlloc->Add(Vector(50.0, 0.0, 0.0)); // AP 2
220 positionAlloc->Add(Vector(0.0, 10.0, 0.0)); // non-AP STA
221 mobility.SetPositionAllocator(positionAlloc);
222
223 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
224
225 mobility.Install(wifiParams.apNodes);
226 mobility.Install(wifiParams.staNodes);
227
228 ap1Phy.EnablePcap("wifi-AP", apDevices);
229 ap1Phy.EnablePcap("wifi-STA", staDevices);
230
231 return {apDevices, staDevices};
232}
233
234/**
235 * Print the average UL and DL throughput over the given time interval.
236 *
237 * @param start start of the time interval
238 * @param end end of the time interval
239 * @param serverNode the server node
240 * @param staNode the STA node
241 * @param ulTxBytes cumulative number of bytes transmitted in the UL
242 * @param dlTxBytes cumulative number of bytes transmitted in the DL
243 * @param ulRxBytes cumulative number of bytes received in the UL before the interval; updated with
244 * the current cumulative value
245 * @param dlRxBytes cumulative number of bytes received in the DL before the interval; updated with
246 * the current cumulative value
247 */
248void
250 const Time& end,
251 Ptr<Node> serverNode,
252 Ptr<Node> staNode,
253 uint64_t ulTxBytes,
254 uint64_t dlTxBytes,
255 uint64_t& ulRxBytes,
256 uint64_t& dlRxBytes)
257{
258 // Compute UL throughput
259 for (uint32_t i = 0; i < serverNode->GetNApplications(); ++i)
260 {
261 if (auto sink = DynamicCast<PacketSink>(serverNode->GetApplication(i)))
262 {
263 std::cout << "---------------- UL ----------------"
264 << "From " << start.As(Time::S) << " to " << end.As(Time::S) << ":\n"
265 << "Wi-Fi STA transmitted " << ulTxBytes << " bytes\n";
266 auto rxBytes = sink->GetTotalRx() - ulRxBytes;
267 if (rxBytes == 0)
268 {
269 NS_LOG_ERROR("UL throughput is zero");
270 exit(1);
271 }
272 std::cout << "SERVER received " << rxBytes << " bytes, UL throughput = "
273 << (rxBytes * 8.0) / (end - start).GetMilliSeconds() << " kbps" << std::endl
274 << "------------------------------------" << std::endl;
275 ulRxBytes = sink->GetTotalRx();
276 }
277 }
278
279 // Compute DL throughput
280 for (uint32_t i = 0; i < staNode->GetNApplications(); ++i)
281 {
282 if (auto sink = DynamicCast<PacketSink>(staNode->GetApplication(i)))
283 {
284 std::cout << "---------------- DL ----------------\n"
285 << "From " << start.As(Time::S) << " to " << end.As(Time::S) << ":\n"
286 << "SERVER transmitted " << dlTxBytes << " bytes\n";
287 auto rxBytes = sink->GetTotalRx() - dlRxBytes;
288 if (rxBytes == 0)
289 {
290 NS_LOG_ERROR("DL throughput is zero");
291 exit(1);
292 }
293 std::cout << "Wi-Fi STA received " << rxBytes << " bytes, DL throughput = "
294 << (rxBytes * 8.0) / (end - start).GetMilliSeconds() << " kbps" << std::endl
295 << "------------------------------------" << std::endl;
296 dlRxBytes = sink->GetTotalRx();
297 }
298 }
299}
300
301} // namespace
302
303int
304main(int argc, char* argv[])
305{
306 Time simulationTime{"10s"};
307 DataRate ulLoad;
308 DataRate dlLoad;
309 bool udp{true};
310 Time disassocTime{"5s"};
311 bool notifyAp{false};
312 WifiSetupParams wifiParams;
313 bool forwardFrameOnAssoc{true};
314 uint32_t pktSize{2000};
315
316 CommandLine cmd(__FILE__);
317 cmd.Usage("This example considers two APs and a server connected to a CSMA bridge. A STA is "
318 "initially associated with AP 1 and can be configured to move closer to and "
319 "associate with AP 2 at a given point in time. It is possible to configure whether "
320 "or not the STA notifies the disassociation to AP 1 when roaming. DL and/or UL UDP "
321 "(or TCP) traffic can be exchanged between STA and the server.");
322 cmd.AddValue("simulationTime", "Simulation time", simulationTime);
323 cmd.AddValue("dlLoad", "Rate of the downlink load to generate", dlLoad);
324 cmd.AddValue("ulLoad", "Rate of the uplink load to generate", ulLoad);
325 cmd.AddValue("udp", "True to use UDP, false to use TCP", udp);
326 cmd.AddValue("disassocTime",
327 "If non-null, the time at which the STA sends a Disassociation frame and moves "
328 "closer to AP 2",
329 disassocTime);
330 cmd.AddValue("notifyAp",
331 "Whether to send a Disassociation frame to notify disassociation to AP 1",
332 notifyAp);
333 cmd.AddValue("ap1Channels",
334 "Semicolon separated (no spaces) list of channel settings for the links of AP 1",
335 wifiParams.ap1Channels);
336 cmd.AddValue("ap2Channels",
337 "Semicolon separated (no spaces) list of channel settings for the links of AP 2; "
338 "leave empty to use the same channel set as AP 1",
339 wifiParams.ap2Channels);
340 cmd.AddValue(
341 "staChannels",
342 "Semicolon separated (no spaces) list of channel settings for the links of the STA; "
343 "leave empty to use the same channel set as AP 1",
344 wifiParams.staChannels);
345 cmd.AddValue("forwardFrameOnAssoc",
346 "Whether APs forward a synthetic frame to make the CSMA bridge learn the port "
347 "of a newly associated STA",
348 forwardFrameOnAssoc);
349 cmd.AddValue("pktSize", "The application packet size in bytes", pktSize);
350 cmd.Parse(argc, argv);
351
352 const Time startTime{"1s"}; // the simulation time when traffic generation starts
353 std::map<Ptr<WifiMac>, DropStats> stats;
354 uint64_t dlTxBytes{0};
355 uint64_t ulTxBytes{0};
356 uint64_t dlRxBytes{0};
357 uint64_t ulRxBytes{0};
358
359 //
360 // Explicitly create the nodes required by the topology (shown above).
361 //
362 NodeContainer ethSwitch(1);
363 NodeContainer serverNodes(1);
364 wifiParams.apNodes.Create(2);
365 wifiParams.staNodes.Create(1);
366
367 BridgeHelper bridge;
369 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate("10Gbps")));
370 csma.SetChannelAttribute("Delay", TimeValue(Time{0}));
371
372 NetDeviceContainer ethSwitchPorts;
373 NetDeviceContainer serverCsmaDevices;
374 NetDeviceContainer apCsmaDevices;
375
376 // Create a CSMA link between the server and the switch
377 for (std::size_t i = 0; i < serverNodes.GetN(); ++i)
378 {
379 auto link = csma.Install(NodeContainer(serverNodes.Get(i), ethSwitch));
380 serverCsmaDevices.Add(link.Get(0));
381 ethSwitchPorts.Add(link.Get(1));
382 NS_LOG_INFO("Server-Switch CSMA link addresses: " << link.Get(0)->GetAddress() << " - "
383 << link.Get(1)->GetAddress());
384 }
385
386 // Create the wifi devices
387 Config::SetDefault("ns3::ApWifiMac::ForwardFrameOnAssoc", BooleanValue(forwardFrameOnAssoc));
388
389 auto [apWifiDevices, staWifiDevices] = CreateWifiDevices(wifiParams);
390
391 for (std::size_t i = 0; i < wifiParams.apNodes.GetN(); ++i)
392 {
393 // create a CSMA link between AP i and the CSMA bridge (which adds a CSMA netdevice to
394 // each node)
395 auto csmaDevices = csma.Install(NodeContainer(wifiParams.apNodes.Get(i), ethSwitch));
396 // bridge the AP CSMA netdevice with the AP Wi-Fi netdevice
397 bridge.Install(wifiParams.apNodes.Get(i),
398 NetDeviceContainer(csmaDevices.Get(0), apWifiDevices.Get(i)));
399 // add the other CSMA netdevice as another switch port
400 ethSwitchPorts.Add(csmaDevices.Get(1));
401 NS_LOG_INFO("AP " << i
402 << "-Switch CSMA link addresses: " << csmaDevices.Get(0)->GetAddress()
403 << " - " << csmaDevices.Get(1)->GetAddress());
404 }
405
406 bridge.Install(ethSwitch.Get(0), ethSwitchPorts);
407
408 // Add internet stack to client and server
410 internet.Install(serverNodes);
411 internet.Install(wifiParams.staNodes);
412
413 // We've got the "hardware" in place. Now we need to add IP addresses.
414 //
415 NS_LOG_INFO("Assign IP Addresses.");
417 ipv4.SetBase("10.1.1.0", "255.255.255.0");
418 auto serverInterfaces = ipv4.Assign(serverCsmaDevices);
419 auto staWifiInterfaces = ipv4.Assign(staWifiDevices);
420
421 NS_LOG_INFO("Create Applications.");
422 uint16_t port = 9; // Discard port (RFC 863)
423 auto socketFactory = udp ? "ns3::UdpSocketFactory" : "ns3::TcpSocketFactory";
424
425 // Install client and server apps for UL flow (if data rate > 0)
426 if (ulLoad.GetBitRate() > 0)
427 {
428 OnOffHelper onoff(socketFactory,
430 onoff.SetConstantRate(ulLoad, pktSize);
431
432 auto clientApp = onoff.Install(wifiParams.staNodes.Get(0));
433 // Start the application
434 clientApp.Start(startTime);
435 clientApp.Stop(startTime + simulationTime);
436
437 // Create a packet sink to receive these packets
438 PacketSinkHelper sink(socketFactory,
440 auto serverApp = sink.Install(serverNodes.Get(0));
441 serverApp.Start(Time{0});
442 }
443
444 // Install client and server apps for DL flow (if data rate > 0)
445 if (dlLoad.GetBitRate() > 0)
446 {
447 OnOffHelper onoff(socketFactory,
448 Address(InetSocketAddress(staWifiInterfaces.GetAddress(0), port)));
449 onoff.SetConstantRate(dlLoad, pktSize);
450
451 auto clientApp = onoff.Install(serverNodes.Get(0));
452 // Start the application
453 clientApp.Start(startTime);
454 clientApp.Stop(startTime + simulationTime);
455
456 // Create a packet sink to receive these packets
457 PacketSinkHelper sink(socketFactory,
459 auto serverApp = sink.Install(wifiParams.staNodes.Get(0));
460 serverApp.Start(Time{0});
461 }
462
463 auto staDev = DynamicCast<WifiNetDevice>(staWifiDevices.Get(0));
464 NS_ASSERT(staDev);
465 auto staMac = DynamicCast<StaWifiMac>(staDev->GetMac());
466 NS_ASSERT(staMac);
467
468 // print messages when (dis)association is completed
469 staMac->TraceConnectWithoutContext("Assoc",
471 std::cout << "At time " << Simulator::Now().As(Time::S)
472 << ", STA completed association with BSSID "
473 << apAddr << std::endl;
474 }));
475
476 staMac->TraceConnectWithoutContext("DeAssoc",
478 std::cout << "At time " << Simulator::Now().As(Time::S)
479 << ", STA disassociated from BSSID " << apAddr
480 << std::endl;
481 }));
482
483 auto ap1Dev = DynamicCast<WifiNetDevice>(apWifiDevices.Get(0));
484 NS_ASSERT(ap1Dev);
485 auto ap1Mac = DynamicCast<ApWifiMac>(ap1Dev->GetMac());
486 NS_ASSERT(ap1Mac);
487
488 auto ap2Dev = DynamicCast<WifiNetDevice>(apWifiDevices.Get(1));
489 NS_ASSERT(ap2Dev);
490 auto ap2Mac = DynamicCast<ApWifiMac>(ap2Dev->GetMac());
491 NS_ASSERT(ap2Mac);
492
493 // print messages when BA agreements (TID 0) are established
494 staMac->GetQosTxop(AC_BE)->TraceConnectWithoutContext(
495 "BaEstablished",
496 Callback<void, Mac48Address, uint8_t, std::optional<Mac48Address>>(
497 [](Mac48Address recipient, uint8_t tid, std::optional<Mac48Address> /* gcrGroup */) {
498 std::cout << "At time " << Simulator::Now().As(Time::S)
499 << ", STA established BA agreement with " << recipient << " for TID "
500 << +tid << std::endl;
501 }));
502
503 ap1Mac->GetQosTxop(AC_BE)->TraceConnectWithoutContext(
504 "BaEstablished",
505 Callback<void, Mac48Address, uint8_t, std::optional<Mac48Address>>(
506 [](Mac48Address recipient, uint8_t tid, std::optional<Mac48Address> /* gcrGroup */) {
507 std::cout << "At time " << Simulator::Now().As(Time::S)
508 << ", AP 1 established BA agreement with " << recipient << " for TID "
509 << +tid << std::endl;
510 }));
511
512 ap2Mac->GetQosTxop(AC_BE)->TraceConnectWithoutContext(
513 "BaEstablished",
514 Callback<void, Mac48Address, uint8_t, std::optional<Mac48Address>>(
515 [](Mac48Address recipient, uint8_t tid, std::optional<Mac48Address> /* gcrGroup */) {
516 std::cout << "At time " << Simulator::Now().As(Time::S)
517 << ", AP 2 established BA agreement with " << recipient << " for TID "
518 << +tid << std::endl;
519 }));
520
521 // connect callbacks to Tx trace source to track transmitted UL and DL bytes
522 staDev->GetNode()->GetApplication(0)->TraceConnectWithoutContext(
523 "Tx",
524 Callback<void, uint64_t&, Ptr<const Packet>>([](uint64_t& txBytes,
525 Ptr<const Packet> packet) {
526 txBytes += packet->GetSize();
527 }).Bind(std::ref(ulTxBytes)));
528
529 serverNodes.Get(0)->GetApplication(0)->TraceConnectWithoutContext(
530 "Tx",
531 Callback<void, uint64_t&, Ptr<const Packet>>([](uint64_t& txBytes,
532 Ptr<const Packet> packet) {
533 txBytes += packet->GetSize();
534 }).Bind(std::ref(dlTxBytes)));
535
536 stats.try_emplace(staMac);
537 stats.try_emplace(ap1Mac);
538 stats.try_emplace(ap2Mac);
539
540 for (auto& [mac, drops] : stats)
541 {
542 mac->TraceConnectWithoutContext(
543 "MacTxDrop",
544 Callback<void, Ptr<const Packet>>([&](Ptr<const Packet> p) { ++drops.noFwdCount; }));
545 // clang-format off
546 mac->TraceConnectWithoutContext("DroppedMpdu",
549 auto [it, inserted] =
550 drops.droppedMpdus.insert({reason, 1});
551 if (!inserted)
552 {
553 ++it->second;
554 }
555 }));
556 // clang-format on
557 }
558
559 // schedule disassociation, if requested
560 if (disassocTime.IsStrictlyPositive())
561 {
562 Simulator::Schedule(disassocTime, [=, &ulTxBytes, &dlTxBytes, &ulRxBytes, &dlRxBytes]() {
563 if (!staMac->IsAssociated())
564 {
565 std::cout << "STA not associated at time disassociation is requested ("
566 << Simulator::Now().As(Time::S) << "), do nothing" << std::endl;
567 return;
568 }
569 staMac->ForceDisassociation(notifyAp);
570 auto mobility = wifiParams.staNodes.Get(0)->GetObject<MobilityModel>();
571 NS_ASSERT(mobility);
572 mobility->SetPosition(Vector(50.0, 10.0, 0.0));
573 PrintThroughput(startTime,
574 disassocTime,
575 serverNodes.Get(0),
576 wifiParams.staNodes.Get(0),
577 ulTxBytes,
578 dlTxBytes,
579 ulRxBytes,
580 dlRxBytes);
581 std::cout << "Packets left in the STA BE queue: "
582 << staMac->GetTxopQueue(AC_BE)->GetNPackets() << std::endl;
583 std::cout << "Packets left in the AP 1 BE queue: "
584 << ap1Mac->GetTxopQueue(AC_BE)->GetNPackets() << std::endl;
585 });
586 }
587
588 csma.EnablePcapAll("csma-bridge", false);
589
590 //
591 // Now, do the actual simulation.
592 //
593 NS_LOG_INFO("Run Simulation.");
594 Simulator::Stop(startTime + simulationTime);
596
597 PrintThroughput(disassocTime,
598 startTime + simulationTime,
599 serverNodes.Get(0),
600 wifiParams.staNodes.Get(0),
601 ulTxBytes,
602 dlTxBytes,
603 ulRxBytes,
604 dlRxBytes);
605
606 // print statistics
607 for (auto& [mac, drops] : stats)
608 {
609 const auto str = (mac->GetTypeOfStation() == STA
610 ? "STA"
611 : "AP " + std::to_string(mac->GetDevice()->GetNode()->GetId() - 1));
612
613 std::cout << "\nStatistics for " << str << "\n--------------\n"
614 << "Dropped because cannot be forwarded: " << drops.noFwdCount << std::endl;
615 for (const auto& [reason, count] : drops.droppedMpdus)
616 {
617 std::cout << reason << ": " << count << std::endl;
618 }
619 }
620
622 NS_LOG_INFO("Done.");
623
624 return 0;
625}
a polymophic address class
Definition address.h:114
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging).
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Callback template class.
Definition callback.h:428
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition data-rate.cc:201
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
an EUI-48 address
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
keep track of a set of node pointers.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
Make it easy to create and manage PHY objects for the spectrum model.
The IEEE 802.11 SSID Information Element.
Definition ssid.h:27
AttributeValue implementation for Ssid.
Definition ssid.h:98
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
@ S
second
Definition nstime.h:106
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:422
bool IsStrictlyPositive() const
Exactly equivalent to t > 0.
Definition nstime.h:341
helps to create WifiNetDevice objects
static int64_t AssignStreams(NetDeviceContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the PHY and MAC aspects ...
create MAC layers for a ns3::WifiNetDevice.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
uint16_t port
Definition dsdv-manet.cc:33
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:886
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:99
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:253
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:274
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
WifiMacDropReason
The reason why an MPDU was dropped.
Definition wifi-mac.h:72
@ STA
Definition wifi-mac.h:60
@ WIFI_STANDARD_80211be
@ AC_BE
Best Effort.
Definition qos-utils.h:66
uint32_t pktSize
packet size used for the simulation (in bytes)
std::pair< NetDeviceContainer, NetDeviceContainer > CreateWifiDevices(const WifiSetupParams &wifiParams)
Create Wi-Fi devices.
SpectrumWifiPhyHelper GetPhyHelper(const std::string &channelsStr, Ptr< SpectrumChannel > channel)
Get a SpectrumWifiPhyHelper object configured with the channel settings listed in the given input str...
void PrintThroughput(const Time &start, const Time &end, Ptr< Node > serverNode, Ptr< Node > staNode, uint64_t ulTxBytes, uint64_t dlTxBytes, uint64_t &ulRxBytes, uint64_t &dlRxBytes)
Print the average UL and DL throughput over the given time interval.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:643
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
Definition string.cc:23
csmaDevices
Definition second.py:56
mac
Definition third.py:81
mobility
Definition third.py:92
std::size_t noFwdCount
count of packets dropped because cannot be forwarded
std::map< WifiMacDropReason, std::size_t > droppedMpdus
per-reason count of dropped MPDUs
std::string ap2Channels
Semicolon separated (no spaces) list of channel settings for the links of AP 2.
std::string staChannels
Semicolon separated (no spaces) list of channel settings for the links of STA.
std::string ap1Channels
Semicolon separated (no spaces) list of channel settings for the links of AP 1.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44