A Discrete-Event Network Simulator
API
mixed-wireless.py
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 
17 #
18 # This ns-3 example demonstrates the use of helper functions to ease
19 # the construction of simulation scenarios.
20 #
21 # The simulation topology consists of a mixed wired and wireless
22 # scenario in which a hierarchical mobility model is used.
23 #
24 # The simulation layout consists of N backbone routers interconnected
25 # by an ad hoc wifi network.
26 # Each backbone router also has a local 802.11 network and is connected
27 # to a local LAN. An additional set of(K-1) nodes are connected to
28 # this backbone. Finally, a local LAN is connected to each router
29 # on the backbone, with L-1 additional hosts.
30 #
31 # The nodes are populated with TCP/IP stacks, and OLSR unicast routing
32 # on the backbone. An example UDP transfer is shown. The simulator
33 # be configured to output tcpdumps or traces from different nodes.
34 #
35 #
36 # +--------------------------------------------------------+
37 # | |
38 # | 802.11 ad hoc, ns-2 mobility |
39 # | |
40 # +--------------------------------------------------------+
41 # | o o o(N backbone routers) |
42 # +--------+ +--------+
43 # wired LAN | mobile | wired LAN | mobile |
44 # -----------| router | -----------| router |
45 # --------- ---------
46 # | |
47 # +----------------+ +----------------+
48 # | 802.11 | | 802.11 |
49 # | net | | net |
50 # | K-1 hosts | | K-1 hosts |
51 # +----------------+ +----------------+
52 #
53 
54 import ns.applications
55 import ns.core
56 import ns.csma
57 import ns.internet
58 import ns.mobility
59 import ns.network
60 import ns.olsr
61 import ns.wifi
62 
63 # #
64 # # This function will be used below as a trace sink
65 # #
66 # static void
67 # CourseChangeCallback(std.string path, Ptr<const MobilityModel> model)
68 # {
69 # Vector position = model.GetPosition();
70 # std.cout << "CourseChange " << path << " x=" << position.x << ", y=" << position.y << ", z=" << position.z << std.endl;
71 # }
72 
73 def main(argv):
74  #
75  # First, we initialize a few local variables that control some
76  # simulation parameters.
77  #
78 
79  cmd = ns.core.CommandLine()
80  cmd.backboneNodes = 10
81  cmd.infraNodes = 2
82  cmd.lanNodes = 2
83  cmd.stopTime = 20
84 
85  #
86  # Simulation defaults are typically set next, before command line
87  # arguments are parsed.
88  #
89  ns.core.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.StringValue("1472"))
90  ns.core.Config.SetDefault("ns3::OnOffApplication::DataRate", ns.core.StringValue("100kb/s"))
91 
92  #
93  # For convenience, we add the local variables to the command line argument
94  # system so that they can be overridden with flags such as
95  # "--backboneNodes=20"
96  #
97 
98  cmd.AddValue("backboneNodes", "number of backbone nodes")
99  cmd.AddValue("infraNodes", "number of leaf nodes")
100  cmd.AddValue("lanNodes", "number of LAN nodes")
101  cmd.AddValue("stopTime", "simulation stop time(seconds)")
102 
103  #
104  # The system global variables and the local values added to the argument
105  # system can be overridden by command line arguments by using this call.
106  #
107  cmd.Parse(argv)
108 
109  backboneNodes = int(cmd.backboneNodes)
110  infraNodes = int(cmd.infraNodes)
111  lanNodes = int(cmd.lanNodes)
112  stopTime = int(cmd.stopTime)
113 
114  if (stopTime < 10):
115  print "Use a simulation stop time >= 10 seconds"
116  exit(1)
117  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
118  # #
119  # Construct the backbone #
120  # #
121  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
122 
123  #
124  # Create a container to manage the nodes of the adhoc(backbone) network.
125  # Later we'll create the rest of the nodes we'll need.
126  #
127  backbone = ns.network.NodeContainer()
128  backbone.Create(backboneNodes)
129  #
130  # Create the backbone wifi net devices and install them into the nodes in
131  # our container
132  #
133  wifi = ns.wifi.WifiHelper()
134  mac = ns.wifi.WifiMacHelper()
135  mac.SetType("ns3::AdhocWifiMac")
136  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
137  "DataMode", ns.core.StringValue("OfdmRate54Mbps"))
138  wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
139  wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
140  wifiPhy.SetChannel(wifiChannel.Create())
141  backboneDevices = wifi.Install(wifiPhy, mac, backbone)
142  #
143  # Add the IPv4 protocol stack to the nodes in our container
144  #
145  print "Enabling OLSR routing on all backbone nodes"
146  internet = ns.internet.InternetStackHelper()
147  olsr = ns.olsr.OlsrHelper()
148  internet.SetRoutingHelper(olsr); # has effect on the next Install ()
149  internet.Install(backbone);
150  # re-initialize for non-olsr routing.
151  # internet.Reset()
152  #
153  # Assign IPv4 addresses to the device drivers(actually to the associated
154  # IPv4 interfaces) we just created.
155  #
156  ipAddrs = ns.internet.Ipv4AddressHelper()
157  ipAddrs.SetBase(ns.network.Ipv4Address("192.168.0.0"), ns.network.Ipv4Mask("255.255.255.0"))
158  ipAddrs.Assign(backboneDevices)
159 
160  #
161  # The ad-hoc network nodes need a mobility model so we aggregate one to
162  # each of the nodes we just finished building.
163  #
164  mobility = ns.mobility.MobilityHelper()
165  mobility.SetPositionAllocator("ns3::GridPositionAllocator",
166  "MinX", ns.core.DoubleValue(20.0),
167  "MinY", ns.core.DoubleValue(20.0),
168  "DeltaX", ns.core.DoubleValue(20.0),
169  "DeltaY", ns.core.DoubleValue(20.0),
170  "GridWidth", ns.core.UintegerValue(5),
171  "LayoutType", ns.core.StringValue("RowFirst"))
172  mobility.SetMobilityModel("ns3::RandomDirection2dMobilityModel",
173  "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-500, 500, -500, 500)),
174  "Speed", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=2]"),
175  "Pause", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=0.2]"))
176  mobility.Install(backbone)
177 
178  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
179  # #
180  # Construct the LANs #
181  # #
182  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
183 
184  # Reset the address base-- all of the CSMA networks will be in
185  # the "172.16 address space
186  ipAddrs.SetBase(ns.network.Ipv4Address("172.16.0.0"), ns.network.Ipv4Mask("255.255.255.0"))
187 
188  for i in range(backboneNodes):
189  print "Configuring local area network for backbone node ", i
190  #
191  # Create a container to manage the nodes of the LAN. We need
192  # two containers here; one with all of the new nodes, and one
193  # with all of the nodes including new and existing nodes
194  #
195  newLanNodes = ns.network.NodeContainer()
196  newLanNodes.Create(lanNodes - 1)
197  # Now, create the container with all nodes on this link
198  lan = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), newLanNodes)
199  #
200  # Create the CSMA net devices and install them into the nodes in our
201  # collection.
202  #
203  csma = ns.csma.CsmaHelper()
204  csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
205  csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
206  lanDevices = csma.Install(lan)
207  #
208  # Add the IPv4 protocol stack to the new LAN nodes
209  #
210  internet.Install(newLanNodes)
211  #
212  # Assign IPv4 addresses to the device drivers(actually to the
213  # associated IPv4 interfaces) we just created.
214  #
215  ipAddrs.Assign(lanDevices)
216  #
217  # Assign a new network prefix for the next LAN, according to the
218  # network mask initialized above
219  #
220  ipAddrs.NewNetwork()
221  #
222  # The new LAN nodes need a mobility model so we aggregate one
223  # to each of the nodes we just finished building.
224  #
225  mobilityLan = ns.mobility.MobilityHelper()
226  positionAlloc = ns.mobility.ListPositionAllocator()
227  for j in range(newLanNodes.GetN()):
228  positionAlloc.Add(ns.core.Vector(0.0, (j*10 + 10), 0.0))
229 
230  mobilityLan.SetPositionAllocator(positionAlloc)
231  mobilityLan.PushReferenceMobilityModel(backbone.Get(i))
232  mobilityLan.SetMobilityModel("ns3::ConstantPositionMobilityModel")
233  mobilityLan.Install(newLanNodes);
234 
235  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
236  # #
237  # Construct the mobile networks #
238  # #
239  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
240 
241  # Reset the address base-- all of the 802.11 networks will be in
242  # the "10.0" address space
243  ipAddrs.SetBase(ns.network.Ipv4Address("10.0.0.0"), ns.network.Ipv4Mask("255.255.255.0"))
244 
245  for i in range(backboneNodes):
246  print "Configuring wireless network for backbone node ", i
247  #
248  # Create a container to manage the nodes of the LAN. We need
249  # two containers here; one with all of the new nodes, and one
250  # with all of the nodes including new and existing nodes
251  #
252  stas = ns.network.NodeContainer()
253  stas.Create(infraNodes - 1)
254  # Now, create the container with all nodes on this link
255  infra = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), stas)
256  #
257  # Create another ad hoc network and devices
258  #
259  ssid = ns.wifi.Ssid('wifi-infra' + str(i))
260  wifiInfra = ns.wifi.WifiHelper.Default()
261  wifiPhy.SetChannel(wifiChannel.Create())
262  wifiInfra.SetRemoteStationManager('ns3::ArfWifiManager')
263  macInfra = ns.wifi.WifiMacHelper();
264  macInfra.SetType("ns3::StaWifiMac",
265  "Ssid", ns.wifi.SsidValue(ssid),
266  "ActiveProbing", ns.core.BooleanValue(False))
267 
268  # setup stas
269  staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
270  # setup ap.
271  macInfra.SetType("ns3::ApWifiMac",
272  "Ssid", ns.wifi.SsidValue(ssid),
273  "BeaconGeneration", ns.core.BooleanValue(True),
274  "BeaconInterval", ns.core.TimeValue(ns.core.Seconds(2.5)))
275  apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i))
276  # Collect all of these new devices
277  infraDevices = ns.network.NetDeviceContainer(apDevices, staDevices)
278 
279  # Add the IPv4 protocol stack to the nodes in our container
280  #
281  internet.Install(stas)
282  #
283  # Assign IPv4 addresses to the device drivers(actually to the associated
284  # IPv4 interfaces) we just created.
285  #
286  ipAddrs.Assign(infraDevices)
287  #
288  # Assign a new network prefix for each mobile network, according to
289  # the network mask initialized above
290  #
291  ipAddrs.NewNetwork()
292  #
293  # The new wireless nodes need a mobility model so we aggregate one
294  # to each of the nodes we just finished building.
295  #
296  subnetAlloc = ns.mobility.ListPositionAllocator()
297  for j in range(infra.GetN()):
298  subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0))
299 
300  mobility.PushReferenceMobilityModel(backbone.Get(i))
301  mobility.SetPositionAllocator(subnetAlloc)
302  mobility.SetMobilityModel("ns3::RandomDirection2dMobilityModel",
303  "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-10, 10, -10, 10)),
304  "Speed", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=3]"),
305  "Pause", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=0.4]"))
306  mobility.Install(stas)
307 
308  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
309  # #
310  # Application configuration #
311  # #
312  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
313 
314  # Create the OnOff application to send UDP datagrams of size
315  # 210 bytes at a rate of 448 Kb/s, between two nodes
316  print "Create Applications."
317  port = 9 # Discard port(RFC 863)
318 
319  appSource = ns.network.NodeList.GetNode(backboneNodes)
320  lastNodeIndex = backboneNodes + backboneNodes*(lanNodes - 1) + backboneNodes*(infraNodes - 1) - 1
321  appSink = ns.network.NodeList.GetNode(lastNodeIndex)
322  # Let's fetch the IP address of the last node, which is on Ipv4Interface 1
323  remoteAddr = appSink.GetObject(ns.internet.Ipv4.GetTypeId()).GetAddress(1,0).GetLocal()
324 
325  onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
326  ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
327  apps = onoff.Install(ns.network.NodeContainer(appSource))
328  apps.Start(ns.core.Seconds(3))
329  apps.Stop(ns.core.Seconds(stopTime - 1))
330 
331  # Create a packet sink to receive these packets
332  sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory",
333  ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port))
334  apps = sink.Install(ns.network.NodeContainer(appSink))
335  apps.Start(ns.core.Seconds(3))
336 
337  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
338  # #
339  # Tracing configuration #
340  # #
341  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
342 
343  print "Configure Tracing."
344  csma = ns.csma.CsmaHelper()
345  #
346  # Let's set up some ns-2-like ascii traces, using another helper class
347  #
348  ascii = ns.network.AsciiTraceHelper();
349  stream = ascii.CreateFileStream("mixed-wireless.tr");
350  wifiPhy.EnableAsciiAll(stream);
351  csma.EnableAsciiAll(stream);
352  internet.EnableAsciiIpv4All(stream);
353 
354  # Csma captures in non-promiscuous mode
355  csma.EnablePcapAll("mixed-wireless", False)
356  # Let's do a pcap trace on the backbone devices
357  wifiPhy.EnablePcap("mixed-wireless", backboneDevices)
358  wifiPhy.EnablePcap("mixed-wireless", appSink.GetId(), 0)
359 
360 # #ifdef ENABLE_FOR_TRACING_EXAMPLE
361 # Config.Connect("/NodeList/*/$MobilityModel/CourseChange",
362 # MakeCallback(&CourseChangeCallback))
363 # #endif
364 
365 
366  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
367  # #
368  # Run simulation #
369  # #
370  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
371 
372  print "Run Simulation."
373  ns.core.Simulator.Stop(ns.core.Seconds(stopTime))
374  ns.core.Simulator.Run()
375  ns.core.Simulator.Destroy()
376 
377 
378 if __name__ == '__main__':
379  import sys
380  main(sys.argv)
381 
382