A Discrete-Event Network Simulator
API
tap-wifi-virtual-machine.py
Go to the documentation of this file.
1 # -*- Mode: Python; -*-
2 #
3 # Copyright 2010 University of Washington
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #
18 
19 import sys
20 import ns.core
21 import ns.internet
22 import ns.mobility
23 import ns.network
24 import ns.tap_bridge
25 import ns.wifi
26 
27 def main(argv):
28 
29  ns.core.CommandLine().Parse(argv)
30 
31  #
32  # We are interacting with the outside, real, world. This means we have to
33  # interact in real-time and therefore we have to use the real-time simulator
34  # and take the time to calculate checksums.
35  #
36  ns.core.GlobalValue.Bind("SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl"))
37  ns.core.GlobalValue.Bind("ChecksumEnabled", ns.core.BooleanValue("true"))
38 
39  #
40  # Create two ghost nodes. The first will represent the virtual machine host
41  # on the left side of the network; and the second will represent the VM on
42  # the right side.
43  #
44  nodes = ns.network.NodeContainer()
45  nodes.Create (2);
46 
47  #
48  # We're going to use 802.11 A so set up a wifi helper to reflect that.
49  #
50  wifi = ns.wifi.WifiHelper()
51  wifi.SetStandard (ns.wifi.WIFI_STANDARD_80211a);
52  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", ns.core.StringValue ("OfdmRate54Mbps"));
53 
54  #
55  # No reason for pesky access points, so we'll use an ad-hoc network.
56  #
57  wifiMac = ns.wifi.WifiMacHelper()
58  wifiMac.SetType ("ns3::AdhocWifiMac");
59 
60  #
61  # Configure the physical layer.
62  #
63  wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
64  wifiPhy = ns.wifi.YansWifiPhyHelper()
65  wifiPhy.SetChannel(wifiChannel.Create())
66 
67  #
68  # Install the wireless devices onto our ghost nodes.
69  #
70  devices = wifi.Install(wifiPhy, wifiMac, nodes)
71 
72  #
73  # We need location information since we are talking about wifi, so add a
74  # constant position to the ghost nodes.
75  #
76  mobility = ns.mobility.MobilityHelper()
77  positionAlloc = ns.mobility.ListPositionAllocator()
78  positionAlloc.Add(ns.core.Vector(0.0, 0.0, 0.0))
79  positionAlloc.Add(ns.core.Vector(5.0, 0.0, 0.0))
80  mobility.SetPositionAllocator(positionAlloc)
81  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel")
82  mobility.Install(nodes)
83 
84  #
85  # Use the TapBridgeHelper to connect to the pre-configured tap devices for
86  # the left side. We go with "UseLocal" mode since the wifi devices do not
87  # support promiscuous mode (because of their natures0. This is a special
88  # case mode that allows us to extend a linux bridge into ns-3 IFF we will
89  # only see traffic from one other device on that bridge. That is the case
90  # for this configuration.
91  #
92  tapBridge = ns.tap_bridge.TapBridgeHelper()
93  tapBridge.SetAttribute ("Mode", ns.core.StringValue ("UseLocal"));
94  tapBridge.SetAttribute ("DeviceName", ns.core.StringValue ("tap-left"));
95  tapBridge.Install (nodes.Get (0), devices.Get (0));
96 
97  #
98  # Connect the right side tap to the right side wifi device on the right-side
99  # ghost node.
100  #
101  tapBridge.SetAttribute ("DeviceName", ns.core.StringValue ("tap-right"));
102  tapBridge.Install (nodes.Get (1), devices.Get (1));
103 
104  #
105  # Run the simulation for ten minutes to give the user time to play around
106  #
107  ns.core.Simulator.Stop (ns.core.Seconds (600));
108  ns.core.Simulator.Run(signal_check_frequency = -1)
109  ns.core.Simulator.Destroy()
110  return 0
111 
112 if __name__ == '__main__':
113  sys.exit(main(sys.argv))
114