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"
152 for (std::size_t
id = 0;
id < strings.size(); ++id)
154 NS_LOG_INFO(
"PHY ID " <<
id <<
": " << strings[
id]);
155 phy.Set(
id,
"ChannelSettings",
StringValue(strings[
id]));
158 phy.SetChannel(channel);
169std::pair<NetDeviceContainer, NetDeviceContainer>
173 "Expected 2 AP nodes instead of " << wifiParams.
apNodes.
GetN());
175 "Expected 1 STA node instead of " << wifiParams.
staNodes.
GetN());
179 wifi.SetRemoteStationManager(
"ns3::ThompsonSamplingWifiManager");
182 Ssid ssid(
"wifi-roaming");
186 spectrumChannel->AddPropagationLossModel(lossModel);
188 NS_LOG_INFO(
"Configuring channel settings for AP 1");
190 mac.SetType(
"ns3::ApWifiMac",
"Ssid",
SsidValue(ssid));
191 auto apDevices = wifi.Install(ap1Phy, mac, wifiParams.
apNodes.
Get(0));
193 NS_LOG_INFO(
"Configuring channel settings for AP 2");
197 apDevices.Add(wifi.Install(ap2Phy, mac, wifiParams.
apNodes.
Get(1)));
199 NS_LOG_INFO(
"Configuring channel settings for STA");
203 mac.SetType(
"ns3::StaWifiMac",
"Ssid",
SsidValue(ssid));
204 auto staDevices = wifi.Install(staPhy, mac, wifiParams.
staNodes);
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());
210 int64_t streamNumber = 100;
218 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
219 positionAlloc->Add(Vector(50.0, 0.0, 0.0));
220 positionAlloc->Add(Vector(0.0, 10.0, 0.0));
221 mobility.SetPositionAllocator(positionAlloc);
223 mobility.SetMobilityModel(
"ns3::ConstantPositionMobilityModel");
225 mobility.Install(wifiParams.
apNodes);
226 mobility.Install(wifiParams.
staNodes);
228 ap1Phy.EnablePcap(
"wifi-AP", apDevices);
229 ap1Phy.EnablePcap(
"wifi-STA", staDevices);
231 return {apDevices, staDevices};
259 for (
uint32_t i = 0; i < serverNode->GetNApplications(); ++i)
263 std::cout <<
"---------------- UL ----------------"
265 <<
"Wi-Fi STA transmitted " << ulTxBytes <<
" bytes\n";
266 auto rxBytes =
sink->GetTotalRx() - ulRxBytes;
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();
280 for (
uint32_t i = 0; i < staNode->GetNApplications(); ++i)
284 std::cout <<
"---------------- DL ----------------\n"
286 <<
"SERVER transmitted " << dlTxBytes <<
" bytes\n";
287 auto rxBytes =
sink->GetTotalRx() - dlRxBytes;
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();
304main(
int argc,
char* argv[])
310 Time disassocTime{
"5s"};
311 bool notifyAp{
false};
312 WifiSetupParams wifiParams;
313 bool forwardFrameOnAssoc{
true};
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 "
330 cmd.AddValue(
"notifyAp",
331 "Whether to send a Disassociation frame to notify disassociation to AP 1",
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);
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);
352 const Time startTime{
"1s"};
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};
364 wifiParams.apNodes.Create(2);
365 wifiParams.staNodes.Create(1);
377 for (std::size_t i = 0; i < serverNodes.GetN(); ++i)
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());
391 for (std::size_t i = 0; i < wifiParams.apNodes.GetN(); ++i)
397 bridge.
Install(wifiParams.apNodes.Get(i),
402 <<
"-Switch CSMA link addresses: " <<
csmaDevices.Get(0)->GetAddress()
406 bridge.
Install(ethSwitch.Get(0), ethSwitchPorts);
411 internet.Install(wifiParams.staNodes);
417 ipv4.SetBase(
"10.1.1.0",
"255.255.255.0");
419 auto staWifiInterfaces =
ipv4.Assign(staWifiDevices);
423 auto socketFactory = udp ?
"ns3::UdpSocketFactory" :
"ns3::TcpSocketFactory";
430 onoff.SetConstantRate(ulLoad, pktSize);
432 auto clientApp =
onoff.Install(wifiParams.staNodes.Get(0));
434 clientApp.Start(startTime);
435 clientApp.Stop(startTime + simulationTime);
440 auto serverApp =
sink.Install(serverNodes.Get(0));
441 serverApp.Start(
Time{0});
449 onoff.SetConstantRate(dlLoad, pktSize);
451 auto clientApp =
onoff.Install(serverNodes.Get(0));
453 clientApp.Start(startTime);
454 clientApp.Stop(startTime + simulationTime);
459 auto serverApp =
sink.Install(wifiParams.staNodes.Get(0));
460 serverApp.Start(
Time{0});
469 staMac->TraceConnectWithoutContext(
"Assoc",
472 <<
", STA completed association with BSSID "
473 << apAddr << std::endl;
476 staMac->TraceConnectWithoutContext(
"DeAssoc",
479 <<
", STA disassociated from BSSID " << apAddr
494 staMac->GetQosTxop(
AC_BE)->TraceConnectWithoutContext(
497 [](
Mac48Address recipient, uint8_t tid, std::optional<Mac48Address> ) {
499 <<
", STA established BA agreement with " << recipient <<
" for TID "
500 << +tid << std::endl;
503 ap1Mac->GetQosTxop(
AC_BE)->TraceConnectWithoutContext(
506 [](
Mac48Address recipient, uint8_t tid, std::optional<Mac48Address> ) {
508 <<
", AP 1 established BA agreement with " << recipient <<
" for TID "
509 << +tid << std::endl;
512 ap2Mac->GetQosTxop(
AC_BE)->TraceConnectWithoutContext(
515 [](
Mac48Address recipient, uint8_t tid, std::optional<Mac48Address> ) {
517 <<
", AP 2 established BA agreement with " << recipient <<
" for TID "
518 << +tid << std::endl;
522 staDev->GetNode()->GetApplication(0)->TraceConnectWithoutContext(
526 txBytes += packet->GetSize();
527 }).Bind(std::ref(ulTxBytes)));
529 serverNodes.Get(0)->GetApplication(0)->TraceConnectWithoutContext(
533 txBytes += packet->GetSize();
534 }).Bind(std::ref(dlTxBytes)));
536 stats.try_emplace(staMac);
537 stats.try_emplace(ap1Mac);
538 stats.try_emplace(ap2Mac);
540 for (
auto& [mac, drops] : stats)
542 mac->TraceConnectWithoutContext(
546 mac->TraceConnectWithoutContext(
"DroppedMpdu",
549 auto [it, inserted] =
550 drops.droppedMpdus.insert({reason, 1});
563 if (!staMac->IsAssociated())
565 std::cout <<
"STA not associated at time disassociation is requested ("
569 staMac->ForceDisassociation(notifyAp);
572 mobility->SetPosition(Vector(50.0, 10.0, 0.0));
576 wifiParams.staNodes.Get(0),
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;
588 csma.EnablePcapAll(
"csma-bridge",
false);
598 startTime + simulationTime,
600 wifiParams.staNodes.Get(0),
607 for (
auto& [mac, drops] : stats)
609 const auto str = (
mac->GetTypeOfStation() ==
STA
611 :
"AP " + std::to_string(
mac->GetDevice()->GetNode()->GetId() - 1));
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)
617 std::cout << reason <<
": " << count << std::endl;
a polymophic address class
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...
Parse command-line arguments.
build a set of CsmaNetDevice objects
Class for representing data rates.
uint64_t GetBitRate() const
Get the underlying bitrate.
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()
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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
static Time Now()
Return the current simulation virtual time.
static void Run()
Run the simulation.
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Make it easy to create and manage PHY objects for the spectrum model.
The IEEE 802.11 SSID Information Element.
AttributeValue implementation for Ssid.
Hold variables of type string.
Simulation virtual time values and global simulation resolution.
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
bool IsStrictlyPositive() const
Exactly equivalent to t > 0.
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.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
void SetDefault(std::string name, const AttributeValue &value)
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
WifiMacDropReason
The reason why an MPDU was dropped.
const auto simulationTime
The simulation time.
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.
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
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.
NodeContainer staNodes
STA node container.
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.
NodeContainer apNodes
AP node container.
Ptr< PacketSink > sink
Pointer to the packet sink application.