A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
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
78
using namespace
ns3
;
79
80
NS_LOG_COMPONENT_DEFINE
(
"TapWifiVirtualMachineExample"
);
81
82
int
83
main (
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
//
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
;
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
//
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
}
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition:
net-device-container.h:42
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
ns3::YansWifiChannelHelper::Default
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
Definition:
yans-wifi-helper.cc:41
ns3::YansWifiPhyHelper
Make it easy to create and manage PHY objects for the YANS model.
Definition:
yans-wifi-helper.h:161
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:228
ns3::ListPositionAllocator::Add
void Add(Vector v)
Add a position to the list of positions.
Definition:
position-allocator.cc:70
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition:
boolean.h:37
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::WifiHelper
helps to create WifiNetDevice objects
Definition:
wifi-helper.h:327
ns3::TapBridgeHelper::SetAttribute
void SetAttribute(std::string n1, const AttributeValue &v1)
Set an attribute in the underlying TapBridge net device when these devices are automatically created.
Definition:
tap-bridge-helper.cc:45
ns3::YansWifiPhyHelper::SetChannel
void SetChannel(Ptr< YansWifiChannel > channel)
Definition:
yans-wifi-helper.cc:134
first.devices
devices
Definition:
first.py:39
first.nodes
nodes
Definition:
first.py:32
third.wifi
wifi
Definition:
third.py:96
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:74
ns3::YansWifiChannelHelper::Create
Ptr< YansWifiChannel > Create(void) const
Definition:
yans-wifi-helper.cc:98
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition:
simulator.cc:180
ns3::WIFI_STANDARD_80211a
@ WIFI_STANDARD_80211a
Definition:
wifi-standards.h:127
ns3::TapBridgeHelper
build TapBridge to allow ns-3 simulations to interact with Linux tap devices and processes on the Lin...
Definition:
tap-bridge-helper.h:37
second.cmd
cmd
Definition:
second.py:35
ns3::TapBridgeHelper::Install
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...
Definition:
tap-bridge-helper.cc:61
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition:
simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition:
string.h:41
ns3::GlobalValue::Bind
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...
Definition:
global-value.cc:155
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1289
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:136
ns3::NodeContainer
keep track of a set of node pointers.
Definition:
node-container.h:39
ns3::WifiMacHelper::SetType
void SetType(std::string type, Args &&... args)
Definition:
wifi-mac-helper.h:130
ns3::YansWifiChannelHelper
manage and create wifi channel objects for the YANS model.
Definition:
yans-wifi-helper.h:37
ns3::WifiMacHelper
create MAC layers for a ns3::WifiNetDevice.
Definition:
wifi-mac-helper.h:48
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition:
mobility-helper.h:43
third.mobility
mobility
Definition:
third.py:108
src
tap-bridge
examples
tap-wifi-virtual-machine.cc
Generated on Fri Oct 1 2021 17:03:36 for ns-3 by
1.8.20