A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 //
18 // Default network topology includes some number of AP nodes specified by
19 // the variable nWifis (defaults to two). Off of each AP node, there are some
20 // number of STA nodes specified by the variable nStas (defaults to two).
21 // Each AP talks to its associated STA nodes. There are bridge net devices
22 // on each AP node that bridge the whole thing into one network.
23 //
24 // +-----+ +-----+ +-----+ +-----+
25 // | STA | | STA | | STA | | STA |
26 // +-----+ +-----+ +-----+ +-----+
27 // 192.168.0.3 192.168.0.4 192.168.0.5 192.168.0.6
28 // -------- -------- -------- --------
29 // WIFI STA WIFI STA WIFI STA WIFI STA
30 // -------- -------- -------- --------
31 // ((*)) ((*)) | ((*)) ((*))
32 // |
33 // ((*)) | ((*))
34 // ------- -------
35 // WIFI AP CSMA ========= CSMA WIFI AP
36 // ------- ---- ---- -------
37 // ############## ##############
38 // BRIDGE BRIDGE
39 // ############## ##############
40 // 192.168.0.1 192.168.0.2
41 // +---------+ +---------+
42 // | AP Node | | AP Node |
43 // +---------+ +---------+
44 //
45 
46 #include "ns3/core-module.h"
47 #include "ns3/mobility-module.h"
48 #include "ns3/applications-module.h"
49 #include "ns3/wifi-module.h"
50 #include "ns3/network-module.h"
51 #include "ns3/csma-module.h"
52 #include "ns3/internet-module.h"
53 #include "ns3/bridge-helper.h"
54 #include <vector>
55 #include <stdint.h>
56 #include <sstream>
57 #include <fstream>
58 
59 using namespace ns3;
60 
61 int main (int argc, char *argv[])
62 {
63  uint32_t nWifis = 2;
64  uint32_t nStas = 2;
65  bool sendIp = true;
66  bool writeMobility = false;
67 
68  CommandLine cmd;
69  cmd.AddValue ("nWifis", "Number of wifi networks", nWifis);
70  cmd.AddValue ("nStas", "Number of stations per wifi network", nStas);
71  cmd.AddValue ("SendIp", "Send Ipv4 or raw packets", sendIp);
72  cmd.AddValue ("writeMobility", "Write mobility trace", writeMobility);
73  cmd.Parse (argc, argv);
74 
75  NodeContainer backboneNodes;
76  NetDeviceContainer backboneDevices;
77  Ipv4InterfaceContainer backboneInterfaces;
78  std::vector<NodeContainer> staNodes;
79  std::vector<NetDeviceContainer> staDevices;
80  std::vector<NetDeviceContainer> apDevices;
81  std::vector<Ipv4InterfaceContainer> staInterfaces;
82  std::vector<Ipv4InterfaceContainer> apInterfaces;
83 
85  CsmaHelper csma;
87  ip.SetBase ("192.168.0.0", "255.255.255.0");
88 
89  backboneNodes.Create (nWifis);
90  stack.Install (backboneNodes);
91 
92  backboneDevices = csma.Install (backboneNodes);
93 
94  double wifiX = 0.0;
95 
98 
99  for (uint32_t i = 0; i < nWifis; ++i)
100  {
101  // calculate ssid for wifi subnetwork
102  std::ostringstream oss;
103  oss << "wifi-default-" << i;
104  Ssid ssid = Ssid (oss.str ());
105 
106  NodeContainer sta;
107  NetDeviceContainer staDev;
108  NetDeviceContainer apDev;
109  Ipv4InterfaceContainer staInterface;
110  Ipv4InterfaceContainer apInterface;
111  MobilityHelper mobility;
112  BridgeHelper bridge;
116  wifiPhy.SetChannel (wifiChannel.Create ());
117 
118  sta.Create (nStas);
119  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
120  "MinX", DoubleValue (wifiX),
121  "MinY", DoubleValue (0.0),
122  "DeltaX", DoubleValue (5.0),
123  "DeltaY", DoubleValue (5.0),
124  "GridWidth", UintegerValue (1),
125  "LayoutType", StringValue ("RowFirst"));
126 
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  "ActiveProbing", BooleanValue (false));
152  staDev = wifi.Install (wifiPhy, wifiMac, sta);
153  staInterface = ip.Assign (staDev);
154 
155  // save everything in containers.
156  staNodes.push_back (sta);
157  apDevices.push_back (apDev);
158  apInterfaces.push_back (apInterface);
159  staDevices.push_back (staDev);
160  staInterfaces.push_back (staInterface);
161 
162  wifiX += 20.0;
163  }
164 
165  Address dest;
166  std::string protocol;
167  if (sendIp)
168  {
169  dest = InetSocketAddress (staInterfaces[1].GetAddress (1), 1025);
170  protocol = "ns3::UdpSocketFactory";
171  }
172  else
173  {
175  tmp.SetSingleDevice (staDevices[0].Get (0)->GetIfIndex ());
176  tmp.SetPhysicalAddress (staDevices[1].Get (0)->GetAddress ());
177  tmp.SetProtocol (0x807);
178  dest = tmp;
179  protocol = "ns3::PacketSocketFactory";
180  }
181 
182  OnOffHelper onoff = OnOffHelper (protocol, dest);
183  onoff.SetConstantRate (DataRate ("500kb/s"));
184  ApplicationContainer apps = onoff.Install (staNodes[0].Get (0));
185  apps.Start (Seconds (0.5));
186  apps.Stop (Seconds (3.0));
187 
188  wifiPhy.EnablePcap ("wifi-wired-bridging", apDevices[0]);
189  wifiPhy.EnablePcap ("wifi-wired-bridging", apDevices[1]);
190 
191  if (writeMobility)
192  {
193  AsciiTraceHelper ascii;
194  MobilityHelper::EnableAsciiAll (ascii.CreateFileStream ("wifi-wired-bridging.mob"));
195  }
196 
197  Simulator::Stop (Seconds (5.0));
198  Simulator::Run ();
200 }