A Discrete-Event Network Simulator
API
tap-wifi-virtual-machine.cc
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 // This is an illustration of how one could use virtualization techniques to
18 // allow running applications on virtual machines talking over simulated
19 // networks.
20 //
21 // The actual steps required to configure the virtual machines can be rather
22 // involved, so we don't go into that here. Please have a look at one of
23 // our HOWTOs on the nsnam wiki for more details about how to get the
24 // system configured. For an example, have a look at "HOWTO Use Linux
25 // Containers to set up virtual networks" which uses this code as an
26 // example.
27 //
28 // The configuration you are after is explained in great detail in the
29 // HOWTO, but looks like the following:
30 //
31 // +----------+ +----------+
32 // | virtual | | virtual |
33 // | Linux | | Linux |
34 // | Host | | Host |
35 // | | | |
36 // | eth0 | | eth0 |
37 // +----------+ +----------+
38 // | |
39 // +----------+ +----------+
40 // | Linux | | Linux |
41 // | Bridge | | Bridge |
42 // +----------+ +----------+
43 // | |
44 // +------------+ +-------------+
45 // | "tap-left" | | "tap-right" |
46 // +------------+ +-------------+
47 // | n0 n1 |
48 // | +--------+ +--------+ |
49 // +-------| tap | | tap |-------+
50 // | bridge | | bridge |
51 // +--------+ +--------+
52 // | wifi | | wifi |
53 // +--------+ +--------+
54 // | |
55 // ((*)) ((*))
56 //
57 // Wifi LAN
58 //
59 // ((*))
60 // |
61 // +--------+
62 // | wifi |
63 // +--------+
64 // | access |
65 // | point |
66 // +--------+
67 //
68 #include "ns3/core-module.h"
69 #include "ns3/mobility-module.h"
70 #include "ns3/network-module.h"
71 #include "ns3/tap-bridge-module.h"
72 #include "ns3/wifi-module.h"
73 
74 #include <fstream>
75 #include <iostream>
76 
77 using namespace ns3;
78 
79 NS_LOG_COMPONENT_DEFINE("TapWifiVirtualMachineExample");
80 
81 int
82 main(int argc, char* argv[])
83 {
84  CommandLine cmd(__FILE__);
85  cmd.Parse(argc, argv);
86 
87  //
88  // We are interacting with the outside, real, world. This means we have to
89  // interact in real-time and therefore means we have to use the real-time
90  // simulator and take the time to calculate checksums.
91  //
92  GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
93  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
94 
95  //
96  // Create two ghost nodes. The first will represent the virtual machine host
97  // on the left side of the network; and the second will represent the VM on
98  // the right side.
99  //
101  nodes.Create(2);
102 
103  //
104  // We're going to use 802.11 A so set up a wifi helper to reflect that.
105  //
107  wifi.SetStandard(WIFI_STANDARD_80211a);
108  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
109  "DataMode",
110  StringValue("OfdmRate54Mbps"));
111 
112  //
113  // No reason for pesky access points, so we'll use an ad-hoc network.
114  //
115  WifiMacHelper wifiMac;
116  wifiMac.SetType("ns3::AdhocWifiMac");
117 
118  //
119  // Configure the physical layer.
120  //
122  YansWifiPhyHelper wifiPhy;
123  wifiPhy.SetChannel(wifiChannel.Create());
124 
125  //
126  // Install the wireless devices onto our ghost nodes.
127  //
128  NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);
129 
130  //
131  // We need location information since we are talking about wifi, so add a
132  // constant position to the ghost nodes.
133  //
135  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
136  positionAlloc->Add(Vector(0.0, 0.0, 0.0));
137  positionAlloc->Add(Vector(5.0, 0.0, 0.0));
138  mobility.SetPositionAllocator(positionAlloc);
139  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
140  mobility.Install(nodes);
141 
142  //
143  // Use the TapBridgeHelper to connect to the pre-configured tap devices for
144  // the left side. We go with "UseLocal" mode since the wifi devices do not
145  // support promiscuous mode (because of their natures0. This is a special
146  // case mode that allows us to extend a linux bridge into ns-3 IFF we will
147  // only see traffic from one other device on that bridge. That is the case
148  // for this configuration.
149  //
150  TapBridgeHelper tapBridge;
151  tapBridge.SetAttribute("Mode", StringValue("UseLocal"));
152  tapBridge.SetAttribute("DeviceName", StringValue("tap-left"));
153  tapBridge.Install(nodes.Get(0), devices.Get(0));
154 
155  //
156  // Connect the right side tap to the right side wifi device on the right-side
157  // ghost node.
158  //
159  tapBridge.SetAttribute("DeviceName", StringValue("tap-right"));
160  tapBridge.Install(nodes.Get(1), devices.Get(1));
161 
162  //
163  // Run the simulation for ten minutes to give the user time to play around
164  //
165  Simulator::Stop(Seconds(600.));
166  Simulator::Run();
168 
169  return 0;
170 }
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
Hold variables of type string.
Definition: string.h:56
build TapBridge to allow ns-3 simulations to interact with Linux tap devices and processes on the Lin...
Ptr< NetDevice > Install(Ptr< Node > node, Ptr< NetDevice > nd)
This method installs a TapBridge on the specified Node and forms the bridge with the NetDevice specif...
void SetAttribute(std::string n1, const AttributeValue &v1)
Set an attribute in the underlying TapBridge net device when these devices are automatically created.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:325
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
@ WIFI_STANDARD_80211a
NodeContainer nodes
devices
Definition: first.py:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:33
wifi
Definition: third.py:88
mobility
Definition: third.py:96