A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-wired-bridging.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// Default network topology includes some number of AP nodes specified by
6// the variable nWifis (defaults to two). Off of each AP node, there are some
7// number of STA nodes specified by the variable nStas (defaults to two).
8// Each AP talks to its associated STA nodes. There are bridge net devices
9// on each AP node that bridge the whole thing into one network.
10//
11// +-----+ +-----+ +-----+ +-----+
12// | STA | | STA | | STA | | STA |
13// +-----+ +-----+ +-----+ +-----+
14// 192.168.0.2 192.168.0.3 192.168.0.5 192.168.0.6
15// -------- -------- -------- --------
16// WIFI STA WIFI STA WIFI STA WIFI STA
17// -------- -------- -------- --------
18// ((*)) ((*)) | ((*)) ((*))
19// |
20// ((*)) | ((*))
21// ------- -------
22// WIFI AP CSMA ========= CSMA WIFI AP
23// ------- ---- ---- -------
24// ############## ##############
25// BRIDGE BRIDGE
26// ############## ##############
27// 192.168.0.1 192.168.0.4
28// +---------+ +---------+
29// | AP Node | | AP Node |
30// +---------+ +---------+
31
32#include "ns3/bridge-helper.h"
33#include "ns3/command-line.h"
34#include "ns3/csma-helper.h"
35#include "ns3/double.h"
36#include "ns3/internet-stack-helper.h"
37#include "ns3/ipv4-address-helper.h"
38#include "ns3/mobility-helper.h"
39#include "ns3/on-off-helper.h"
40#include "ns3/packet-socket-address.h"
41#include "ns3/rectangle.h"
42#include "ns3/ssid.h"
43#include "ns3/string.h"
44#include "ns3/uinteger.h"
45#include "ns3/yans-wifi-channel.h"
46#include "ns3/yans-wifi-helper.h"
47
48using namespace ns3;
49
50int
51main(int argc, char* argv[])
52{
53 uint32_t nWifis = 2;
54 uint32_t nStas = 2;
55 bool sendIp = true;
56 bool writeMobility = false;
57
58 CommandLine cmd(__FILE__);
59 cmd.AddValue("nWifis", "Number of wifi networks", nWifis);
60 cmd.AddValue("nStas", "Number of stations per wifi network", nStas);
61 cmd.AddValue("SendIp", "Send Ipv4 or raw packets", sendIp);
62 cmd.AddValue("writeMobility", "Write mobility trace", writeMobility);
63 cmd.Parse(argc, argv);
64
65 NodeContainer backboneNodes;
66 NetDeviceContainer backboneDevices;
67 Ipv4InterfaceContainer backboneInterfaces;
68 std::vector<NodeContainer> staNodes;
69 std::vector<NetDeviceContainer> staDevices;
70 std::vector<NetDeviceContainer> apDevices;
71 std::vector<Ipv4InterfaceContainer> staInterfaces;
72 std::vector<Ipv4InterfaceContainer> apInterfaces;
73
77 ip.SetBase("192.168.0.0", "255.255.255.0");
78
79 backboneNodes.Create(nWifis);
80 stack.Install(backboneNodes);
81
82 backboneDevices = csma.Install(backboneNodes);
83
84 meter_u wifiX = 0.0;
85
86 YansWifiPhyHelper wifiPhy;
88
89 for (uint32_t i = 0; i < nWifis; ++i)
90 {
91 // calculate ssid for wifi subnetwork
92 std::ostringstream oss;
93 oss << "wifi-default-" << i;
94 Ssid ssid = Ssid(oss.str());
95
96 NodeContainer sta;
97 NetDeviceContainer staDev;
99 Ipv4InterfaceContainer staInterface;
100 Ipv4InterfaceContainer apInterface;
102 BridgeHelper bridge;
104 WifiMacHelper wifiMac;
106 wifiPhy.SetChannel(wifiChannel.Create());
107
108 sta.Create(nStas);
109 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
110 "MinX",
111 DoubleValue(wifiX),
112 "MinY",
113 DoubleValue(0.0),
114 "DeltaX",
115 DoubleValue(5.0),
116 "DeltaY",
117 DoubleValue(5.0),
118 "GridWidth",
119 UintegerValue(1),
120 "LayoutType",
121 StringValue("RowFirst"));
122
123 // setup the AP.
124 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
125 mobility.Install(backboneNodes.Get(i));
126 wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
127 apDev = wifi.Install(wifiPhy, wifiMac, backboneNodes.Get(i));
128
129 NetDeviceContainer bridgeDev;
130 bridgeDev =
131 bridge.Install(backboneNodes.Get(i), NetDeviceContainer(apDev, backboneDevices.Get(i)));
132
133 // assign AP IP address to bridge, not wifi
134 apInterface = ip.Assign(bridgeDev);
135
136 // setup the STAs
137 stack.Install(sta);
138 mobility.SetMobilityModel(
139 "ns3::RandomWalk2dMobilityModel",
140 "Mode",
141 StringValue("Time"),
142 "Time",
143 StringValue("2s"),
144 "Speed",
145 StringValue("ns3::ConstantRandomVariable[Constant=1.0]"),
146 "Bounds",
147 RectangleValue(Rectangle(wifiX, wifiX + 5.0, 0.0, (nStas + 1) * 5.0)));
148 mobility.Install(sta);
149 wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
150 staDev = wifi.Install(wifiPhy, wifiMac, sta);
151 staInterface = ip.Assign(staDev);
152
153 // save everything in containers.
154 staNodes.push_back(sta);
155 apDevices.push_back(apDev);
156 apInterfaces.push_back(apInterface);
157 staDevices.push_back(staDev);
158 staInterfaces.push_back(staInterface);
159
160 wifiX += 20.0;
161 }
162
163 Address dest;
164 std::string protocol;
165 if (sendIp)
166 {
167 dest = InetSocketAddress(staInterfaces[1].GetAddress(1), 1025);
168 protocol = "ns3::UdpSocketFactory";
169 }
170 else
171 {
173 tmp.SetSingleDevice(staDevices[0].Get(0)->GetIfIndex());
174 tmp.SetPhysicalAddress(staDevices[1].Get(0)->GetAddress());
175 tmp.SetProtocol(0x807);
176 dest = tmp;
177 protocol = "ns3::PacketSocketFactory";
178 }
179
180 OnOffHelper onoff = OnOffHelper(protocol, dest);
181 onoff.SetConstantRate(DataRate("500kb/s"));
182 ApplicationContainer apps = onoff.Install(staNodes[0].Get(0));
183 apps.Start(Seconds(0.5));
184 apps.Stop(Seconds(3.0));
185
186 wifiPhy.EnablePcap("wifi-wired-bridging", apDevices[0]);
187 wifiPhy.EnablePcap("wifi-wired-bridging", apDevices[1]);
188
189 if (writeMobility)
190 {
191 AsciiTraceHelper ascii;
192 MobilityHelper::EnableAsciiAll(ascii.CreateFileStream("wifi-wired-bridging.mob"));
193 }
194
198
199 return 0;
200}
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
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
Definition csma-helper.h:37
Class for representing data rates.
Definition data-rate.h:78
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
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.
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
a 2d rectangle
Definition rectangle.h:24
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
staDevices
Definition third.py:87
ssid
Definition third.py:82
wifi
Definition third.py:84
apDevices
Definition third.py:90
mobility
Definition third.py:92