A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
78
using namespace
ns3;
79
80
NS_LOG_COMPONENT_DEFINE
(
"TapWifiVirtualMachineExample"
);
81
82
int
83
main
(
int
argc,
char
*argv[])
84
{
85
CommandLine
cmd;
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
//
101
NodeContainer
nodes;
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
//
107
WifiHelper
wifi =
WifiHelper::Default
();
108
wifi.
SetStandard
(
WIFI_PHY_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
NqosWifiMacHelper
wifiMac =
NqosWifiMacHelper::Default
();
115
wifiMac.
SetType
(
"ns3::AdhocWifiMac"
);
116
117
//
118
// Configure the physcial layer.
119
//
120
YansWifiChannelHelper
wifiChannel =
YansWifiChannelHelper::Default
();
121
YansWifiPhyHelper
wifiPhy =
YansWifiPhyHelper::Default
();
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
//
133
MobilityHelper
mobility;
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
}
src
tap-bridge
examples
tap-wifi-virtual-machine.cc
Generated on Tue May 14 2013 11:08:33 for ns-3 by
1.8.1.2