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