A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tap-wifi-virtual-machine.py
Go to the documentation of this file.
1# -*- Mode: Python; -*-
2#
3# Copyright 2010 University of Washington
4#
5# SPDX-License-Identifier: GPL-2.0-only
6#
7
8import sys
9
10try:
11 from ns import ns
12except ModuleNotFoundError:
13 raise SystemExit(
14 "Error: ns3 Python module not found;"
15 " Python bindings may not be enabled"
16 " or your PYTHONPATH might not be properly configured"
17 )
18
19
20def main(argv):
21 ns.CommandLine().Parse(argv)
22
23 #
24 # We are interacting with the outside, real, world. This means we have to
25 # interact in real-time and therefore we have to use the real-time simulator
26 # and take the time to calculate checksums.
27 #
28 ns.GlobalValue.Bind("SimulatorImplementationType", ns.StringValue("ns3::RealtimeSimulatorImpl"))
29 ns.GlobalValue.Bind("ChecksumEnabled", ns.BooleanValue(True))
30
31 #
32 # Create two ghost nodes. The first will represent the virtual machine host
33 # on the left side of the network; and the second will represent the VM on
34 # the right side.
35 #
36 nodes = ns.NodeContainer()
37 nodes.Create(2)
38
39 #
40 # We're going to use 802.11 A so set up a wifi helper to reflect that.
41 #
42 wifi = ns.WifiHelper()
43 wifi.SetStandard(ns.WIFI_STANDARD_80211a)
44 wifi.SetRemoteStationManager(
45 "ns3::ConstantRateWifiManager", "DataMode", ns.StringValue("OfdmRate54Mbps")
46 )
47
48 #
49 # No reason for pesky access points, so we'll use an ad-hoc network.
50 #
51 wifiMac = ns.WifiMacHelper()
52 wifiMac.SetType("ns3::AdhocWifiMac")
53
54 #
55 # Configure the physical layer.
56 #
57 wifiChannel = ns.YansWifiChannelHelper.Default()
58 wifiPhy = ns.YansWifiPhyHelper()
59 wifiPhy.SetChannel(wifiChannel.Create())
60
61 #
62 # Install the wireless devices onto our ghost nodes.
63 #
64 devices = wifi.Install(wifiPhy, wifiMac, nodes)
65
66 #
67 # We need location information since we are talking about wifi, so add a
68 # constant position to the ghost nodes.
69 #
70 mobility = ns.MobilityHelper()
71 positionAlloc = ns.ListPositionAllocator()
72 positionAlloc.Add(ns.Vector(0.0, 0.0, 0.0))
73 positionAlloc.Add(ns.Vector(5.0, 0.0, 0.0))
74 mobility.SetPositionAllocator(positionAlloc)
75 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel")
76 mobility.Install(nodes)
77
78 #
79 # Use the TapBridgeHelper to connect to the pre-configured tap devices for
80 # the left side. We go with "UseLocal" mode since the wifi devices do not
81 # support promiscuous mode (because of their natures0. This is a special
82 # case mode that allows us to extend a linux bridge into ns-3 IFF we will
83 # only see traffic from one other device on that bridge. That is the case
84 # for this configuration.
85 #
86 tapBridge = ns.TapBridgeHelper()
87 tapBridge.SetAttribute("Mode", ns.StringValue("UseLocal"))
88 tapBridge.SetAttribute("DeviceName", ns.StringValue("tap-left"))
89 tapBridge.Install(nodes.Get(0), devices.Get(0))
90
91 #
92 # Connect the right side tap to the right side wifi device on the right-side
93 # ghost node.
94 #
95 tapBridge.SetAttribute("DeviceName", ns.StringValue("tap-right"))
96 tapBridge.Install(nodes.Get(1), devices.Get(1))
97
98 #
99 # Run the simulation for ten minutes to give the user time to play around
100 #
101 ns.Simulator.Stop(ns.Seconds(600))
102 ns.Simulator.Run() # signal_check_frequency = -1
103 ns.Simulator.Destroy()
104 return 0
105
106
107if __name__ == "__main__":
108 sys.exit(main(sys.argv))