A Discrete-Event Network Simulator
API
wifi-wired-bridging.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 // Default network topology includes some number of AP nodes specified by
18 // the variable nWifis (defaults to two). Off of each AP node, there are some
19 // number of STA nodes specified by the variable nStas (defaults to two).
20 // Each AP talks to its associated STA nodes. There are bridge net devices
21 // on each AP node that bridge the whole thing into one network.
22 //
23 // +-----+ +-----+ +-----+ +-----+
24 // | STA | | STA | | STA | | STA |
25 // +-----+ +-----+ +-----+ +-----+
26 // 192.168.0.2 192.168.0.3 192.168.0.5 192.168.0.6
27 // -------- -------- -------- --------
28 // WIFI STA WIFI STA WIFI STA WIFI STA
29 // -------- -------- -------- --------
30 // ((*)) ((*)) | ((*)) ((*))
31 // |
32 // ((*)) | ((*))
33 // ------- -------
34 // WIFI AP CSMA ========= CSMA WIFI AP
35 // ------- ---- ---- -------
36 // ############## ##############
37 // BRIDGE BRIDGE
38 // ############## ##############
39 // 192.168.0.1 192.168.0.4
40 // +---------+ +---------+
41 // | AP Node | | AP Node |
42 // +---------+ +---------+
43 
44 #include "ns3/command-line.h"
45 #include "ns3/double.h"
46 #include "ns3/uinteger.h"
47 #include "ns3/rectangle.h"
48 #include "ns3/string.h"
49 #include "ns3/yans-wifi-helper.h"
50 #include "ns3/ssid.h"
51 #include "ns3/mobility-helper.h"
52 #include "ns3/internet-stack-helper.h"
53 #include "ns3/ipv4-address-helper.h"
54 #include "ns3/on-off-helper.h"
55 #include "ns3/yans-wifi-channel.h"
56 #include "ns3/csma-helper.h"
57 #include "ns3/bridge-helper.h"
58 #include "ns3/packet-socket-address.h"
59 
60 using namespace ns3;
61 
62 int 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", DoubleValue (wifiX),
122  "MinY", DoubleValue (0.0),
123  "DeltaX", DoubleValue (5.0),
124  "DeltaY", DoubleValue (5.0),
125  "GridWidth", UintegerValue (1),
126  "LayoutType", StringValue ("RowFirst"));
127 
128  // setup the AP.
129  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
130  mobility.Install (backboneNodes.Get (i));
131  wifiMac.SetType ("ns3::ApWifiMac",
132  "Ssid", SsidValue (ssid));
133  apDev = wifi.Install (wifiPhy, wifiMac, backboneNodes.Get (i));
134 
135  NetDeviceContainer bridgeDev;
136  bridgeDev = bridge.Install (backboneNodes.Get (i), NetDeviceContainer (apDev, backboneDevices.Get (i)));
137 
138  // assign AP IP address to bridge, not wifi
139  apInterface = ip.Assign (bridgeDev);
140 
141  // setup the STAs
142  stack.Install (sta);
143  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
144  "Mode", StringValue ("Time"),
145  "Time", StringValue ("2s"),
146  "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"),
147  "Bounds", RectangleValue (Rectangle (wifiX, wifiX + 5.0,0.0, (nStas + 1) * 5.0)));
148  mobility.Install (sta);
149  wifiMac.SetType ("ns3::StaWifiMac",
150  "Ssid", SsidValue (ssid));
151  staDev = wifi.Install (wifiPhy, wifiMac, sta);
152  staInterface = ip.Assign (staDev);
153 
154  // save everything in containers.
155  staNodes.push_back (sta);
156  apDevices.push_back (apDev);
157  apInterfaces.push_back (apInterface);
158  staDevices.push_back (staDev);
159  staInterfaces.push_back (staInterface);
160 
161  wifiX += 20.0;
162  }
163 
164  Address dest;
165  std::string protocol;
166  if (sendIp)
167  {
168  dest = InetSocketAddress (staInterfaces[1].GetAddress (1), 1025);
169  protocol = "ns3::UdpSocketFactory";
170  }
171  else
172  {
174  tmp.SetSingleDevice (staDevices[0].Get (0)->GetIfIndex ());
175  tmp.SetPhysicalAddress (staDevices[1].Get (0)->GetAddress ());
176  tmp.SetProtocol (0x807);
177  dest = tmp;
178  protocol = "ns3::PacketSocketFactory";
179  }
180 
181  OnOffHelper onoff = OnOffHelper (protocol, dest);
182  onoff.SetConstantRate (DataRate ("500kb/s"));
183  ApplicationContainer apps = onoff.Install (staNodes[0].Get (0));
184  apps.Start (Seconds (0.5));
185  apps.Stop (Seconds (3.0));
186 
187  wifiPhy.EnablePcap ("wifi-wired-bridging", apDevices[0]);
188  wifiPhy.EnablePcap ("wifi-wired-bridging", apDevices[1]);
189 
190  if (writeMobility)
191  {
192  AsciiTraceHelper ascii;
193  MobilityHelper::EnableAsciiAll (ascii.CreateFileStream ("wifi-wired-bridging.mob"));
194  }
195 
196  Simulator::Stop (Seconds (5.0));
197  Simulator::Run ();
199 }
holds a vector of ns3::Application pointers.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Manage ASCII trace files for device models.
Definition: trace-helper.h:162
an Inet address class
void SetType(std::string type, Args &&... args)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the YANS model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
an address for a packet socket
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
aggregate IP/TCP/UDP functionality to existing Nodes.
staDevices
Definition: third.py:103
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
helps to create WifiNetDevice objects
Definition: wifi-helper.h:326
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
stack
Definition: first.py:41
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
a polymophic address class
Definition: address.h:90
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:108
AttributeValue implementation for Rectangle.
Definition: rectangle.h:97
Class for representing data rates.
Definition: data-rate.h:88
void SetChannel(Ptr< YansWifiChannel > channel)
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
Hold an unsigned integer type.
Definition: uinteger.h:44
ssid
Definition: third.py:100
holds a vector of ns3::NetDevice pointers
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
Definition: bridge-helper.h:43
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:227
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
manage and create wifi channel objects for the YANS model.
create MAC layers for a ns3::WifiNetDevice.
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:573
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:35
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
wifi
Definition: third.py:96
Helper class used to assign positions and mobility models to nodes.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
AttributeValue implementation for Ssid.
Definition: ssid.h:105
void SetProtocol(uint16_t protocol)
Set the protocol.
static void EnableAsciiAll(Ptr< OutputStreamWrapper > stream)
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
apDevices
Definition: third.py:106
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
Include Radiotap link layer information.
Definition: wifi-helper.h:180
a 2d rectangle
Definition: rectangle.h:34
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.