A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
wifi-wired-bridging.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 // Default network topology includes some number of AP nodes specified by
17 // the variable nWifis (defaults to two). Off of each AP node, there are some
18 // number of STA nodes specified by the variable nStas (defaults to two).
19 // Each AP talks to its associated STA nodes. There are bridge net devices
20 // on each AP node that bridge the whole thing into one network.
21 //
22 // +-----+ +-----+ +-----+ +-----+
23 // | STA | | STA | | STA | | STA |
24 // +-----+ +-----+ +-----+ +-----+
25 // 192.168.0.2 192.168.0.3 192.168.0.5 192.168.0.6
26 // -------- -------- -------- --------
27 // WIFI STA WIFI STA WIFI STA WIFI STA
28 // -------- -------- -------- --------
29 // ((*)) ((*)) | ((*)) ((*))
30 // |
31 // ((*)) | ((*))
32 // ------- -------
33 // WIFI AP CSMA ========= CSMA WIFI AP
34 // ------- ---- ---- -------
35 // ############## ##############
36 // BRIDGE BRIDGE
37 // ############## ##############
38 // 192.168.0.1 192.168.0.4
39 // +---------+ +---------+
40 // | AP Node | | AP Node |
41 // +---------+ +---------+
42 
43 #include "ns3/bridge-helper.h"
44 #include "ns3/command-line.h"
45 #include "ns3/csma-helper.h"
46 #include "ns3/double.h"
47 #include "ns3/internet-stack-helper.h"
48 #include "ns3/ipv4-address-helper.h"
49 #include "ns3/mobility-helper.h"
50 #include "ns3/on-off-helper.h"
51 #include "ns3/packet-socket-address.h"
52 #include "ns3/rectangle.h"
53 #include "ns3/ssid.h"
54 #include "ns3/string.h"
55 #include "ns3/uinteger.h"
56 #include "ns3/yans-wifi-channel.h"
57 #include "ns3/yans-wifi-helper.h"
58 
59 using namespace ns3;
60 
61 int
62 main(int argc, char* argv[])
63 {
64  uint32_t nWifis = 2;
65  uint32_t nStas = 2;
66  bool sendIp = true;
67  bool writeMobility = false;
68 
69  CommandLine cmd(__FILE__);
70  cmd.AddValue("nWifis", "Number of wifi networks", nWifis);
71  cmd.AddValue("nStas", "Number of stations per wifi network", nStas);
72  cmd.AddValue("SendIp", "Send Ipv4 or raw packets", sendIp);
73  cmd.AddValue("writeMobility", "Write mobility trace", writeMobility);
74  cmd.Parse(argc, argv);
75 
76  NodeContainer backboneNodes;
77  NetDeviceContainer backboneDevices;
78  Ipv4InterfaceContainer backboneInterfaces;
79  std::vector<NodeContainer> staNodes;
80  std::vector<NetDeviceContainer> staDevices;
81  std::vector<NetDeviceContainer> apDevices;
82  std::vector<Ipv4InterfaceContainer> staInterfaces;
83  std::vector<Ipv4InterfaceContainer> apInterfaces;
84 
88  ip.SetBase("192.168.0.0", "255.255.255.0");
89 
90  backboneNodes.Create(nWifis);
91  stack.Install(backboneNodes);
92 
93  backboneDevices = csma.Install(backboneNodes);
94 
95  double wifiX = 0.0;
96 
97  YansWifiPhyHelper wifiPhy;
99 
100  for (uint32_t i = 0; i < nWifis; ++i)
101  {
102  // calculate ssid for wifi subnetwork
103  std::ostringstream oss;
104  oss << "wifi-default-" << i;
105  Ssid ssid = Ssid(oss.str());
106 
107  NodeContainer sta;
108  NetDeviceContainer staDev;
109  NetDeviceContainer apDev;
110  Ipv4InterfaceContainer staInterface;
111  Ipv4InterfaceContainer apInterface;
113  BridgeHelper bridge;
115  WifiMacHelper wifiMac;
117  wifiPhy.SetChannel(wifiChannel.Create());
118 
119  sta.Create(nStas);
120  mobility.SetPositionAllocator("ns3::GridPositionAllocator",
121  "MinX",
122  DoubleValue(wifiX),
123  "MinY",
124  DoubleValue(0.0),
125  "DeltaX",
126  DoubleValue(5.0),
127  "DeltaY",
128  DoubleValue(5.0),
129  "GridWidth",
130  UintegerValue(1),
131  "LayoutType",
132  StringValue("RowFirst"));
133 
134  // setup the AP.
135  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
136  mobility.Install(backboneNodes.Get(i));
137  wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
138  apDev = wifi.Install(wifiPhy, wifiMac, backboneNodes.Get(i));
139 
140  NetDeviceContainer bridgeDev;
141  bridgeDev =
142  bridge.Install(backboneNodes.Get(i), NetDeviceContainer(apDev, backboneDevices.Get(i)));
143 
144  // assign AP IP address to bridge, not wifi
145  apInterface = ip.Assign(bridgeDev);
146 
147  // setup the STAs
148  stack.Install(sta);
149  mobility.SetMobilityModel(
150  "ns3::RandomWalk2dMobilityModel",
151  "Mode",
152  StringValue("Time"),
153  "Time",
154  StringValue("2s"),
155  "Speed",
156  StringValue("ns3::ConstantRandomVariable[Constant=1.0]"),
157  "Bounds",
158  RectangleValue(Rectangle(wifiX, wifiX + 5.0, 0.0, (nStas + 1) * 5.0)));
159  mobility.Install(sta);
160  wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
161  staDev = wifi.Install(wifiPhy, wifiMac, sta);
162  staInterface = ip.Assign(staDev);
163 
164  // save everything in containers.
165  staNodes.push_back(sta);
166  apDevices.push_back(apDev);
167  apInterfaces.push_back(apInterface);
168  staDevices.push_back(staDev);
169  staInterfaces.push_back(staInterface);
170 
171  wifiX += 20.0;
172  }
173 
174  Address dest;
175  std::string protocol;
176  if (sendIp)
177  {
178  dest = InetSocketAddress(staInterfaces[1].GetAddress(1), 1025);
179  protocol = "ns3::UdpSocketFactory";
180  }
181  else
182  {
184  tmp.SetSingleDevice(staDevices[0].Get(0)->GetIfIndex());
185  tmp.SetPhysicalAddress(staDevices[1].Get(0)->GetAddress());
186  tmp.SetProtocol(0x807);
187  dest = tmp;
188  protocol = "ns3::PacketSocketFactory";
189  }
190 
191  OnOffHelper onoff = OnOffHelper(protocol, dest);
192  onoff.SetConstantRate(DataRate("500kb/s"));
193  ApplicationContainer apps = onoff.Install(staNodes[0].Get(0));
194  apps.Start(Seconds(0.5));
195  apps.Stop(Seconds(3.0));
196 
197  wifiPhy.EnablePcap("wifi-wired-bridging", apDevices[0]);
198  wifiPhy.EnablePcap("wifi-wired-bridging", apDevices[1]);
199 
200  if (writeMobility)
201  {
202  AsciiTraceHelper ascii;
203  MobilityHelper::EnableAsciiAll(ascii.CreateFileStream("wifi-wired-bridging.mob"));
204  }
205 
206  Simulator::Stop(Seconds(5.0));
207  Simulator::Run();
209 
210  return 0;
211 }
a polymophic address class
Definition: address.h:101
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.
Definition: trace-helper.h:174
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)
Definition: bridge-helper.h:45
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
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.
Definition: on-off-helper.h:44
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:35
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
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.
Definition: wifi-helper.cc:543
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
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)
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
csma
Definition: second.py:63
cmd
Definition: second.py:40
staDevices
Definition: third.py:100
ssid
Definition: third.py:93
wifi
Definition: third.py:95
apDevices
Definition: third.py:103
mobility
Definition: third.py:105