A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
tap-csma-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
// | CSMA | | CSMA |
54
// +--------+ +--------+
55
// | |
56
// | |
57
// | |
58
// ===============
59
// CSMA LAN
60
//
61
#include <iostream>
62
#include <fstream>
63
64
#include "ns3/core-module.h"
65
#include "ns3/network-module.h"
66
#include "ns3/csma-module.h"
67
#include "ns3/tap-bridge-module.h"
68
69
using namespace
ns3
;
70
71
NS_LOG_COMPONENT_DEFINE
(
"TapCsmaVirtualMachineExample"
);
72
73
int
74
main (
int
argc,
char
*argv[])
75
{
76
CommandLine
cmd
(__FILE__);
77
cmd
.Parse (argc, argv);
78
79
//
80
// We are interacting with the outside, real, world. This means we have to
81
// interact in real-time and therefore means we have to use the real-time
82
// simulator and take the time to calculate checksums.
83
//
84
GlobalValue::Bind
(
"SimulatorImplementationType"
,
StringValue
(
"ns3::RealtimeSimulatorImpl"
));
85
GlobalValue::Bind
(
"ChecksumEnabled"
,
BooleanValue
(
true
));
86
87
//
88
// Create two ghost nodes. The first will represent the virtual machine host
89
// on the left side of the network; and the second will represent the VM on
90
// the right side.
91
//
92
NodeContainer
nodes
;
93
nodes
.Create (2);
94
95
//
96
// Use a CsmaHelper to get a CSMA channel created, and the needed net
97
// devices installed on both of the nodes. The data rate and delay for the
98
// channel can be set through the command-line parser. For example,
99
//
100
// ./ns3 run "tap=csma-virtual-machine --ns3::CsmaChannel::DataRate=10000000"
101
//
102
CsmaHelper
csma
;
103
NetDeviceContainer
devices
=
csma
.Install (
nodes
);
104
105
//
106
// Use the TapBridgeHelper to connect to the pre-configured tap devices for
107
// the left side. We go with "UseBridge" mode since the CSMA devices support
108
// promiscuous mode and can therefore make it appear that the bridge is
109
// extended into ns-3. The install method essentially bridges the specified
110
// tap to the specified CSMA device.
111
//
112
TapBridgeHelper
tapBridge;
113
tapBridge.
SetAttribute
(
"Mode"
,
StringValue
(
"UseBridge"
));
114
tapBridge.
SetAttribute
(
"DeviceName"
,
StringValue
(
"tap-left"
));
115
tapBridge.
Install
(
nodes
.Get (0),
devices
.Get (0));
116
117
//
118
// Connect the right side tap to the right side CSMA device on the right-side
119
// ghost node.
120
//
121
tapBridge.
SetAttribute
(
"DeviceName"
,
StringValue
(
"tap-right"
));
122
tapBridge.
Install
(
nodes
.Get (1),
devices
.Get (1));
123
124
//
125
// Run the simulation for ten minutes to give the user time to play around
126
//
127
Simulator::Stop
(
Seconds
(600.));
128
Simulator::Run
();
129
Simulator::Destroy
();
130
}
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition:
boolean.h:37
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:229
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition:
csma-helper.h:47
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::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition:
net-device-container.h:42
ns3::NodeContainer
keep track of a set of node pointers.
Definition:
node-container.h:39
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition:
simulator.cc:180
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:136
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::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
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::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
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1244
first.devices
devices
Definition:
first.py:39
first.nodes
nodes
Definition:
first.py:32
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
second.csma
csma
Definition:
second.py:63
second.cmd
cmd
Definition:
second.py:35
src
tap-bridge
examples
tap-csma-virtual-machine.cc
Generated on Tue Feb 6 2024 19:21:27 for ns-3 by
1.9.1