A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
tap-csma-virtual-machine.cc
Go to the documentation of this file.
1
/*
2
* SPDX-License-Identifier: GPL-2.0-only
3
*/
4
5
//
6
// This is an illustration of how one could use virtualization techniques to
7
// allow running applications on virtual machines talking over simulated
8
// networks.
9
//
10
// The actual steps required to configure the virtual machines can be rather
11
// involved, so we don't go into that here. Please have a look at one of
12
// our HOWTOs on the nsnam wiki for more details about how to get the
13
// system configured. For an example, have a look at "HOWTO Use Linux
14
// Containers to set up virtual networks" which uses this code as an
15
// example.
16
//
17
// The configuration you are after is explained in great detail in the
18
// HOWTO, but looks like the following:
19
//
20
// +----------+ +----------+
21
// | virtual | | virtual |
22
// | Linux | | Linux |
23
// | Host | | Host |
24
// | | | |
25
// | eth0 | | eth0 |
26
// +----------+ +----------+
27
// | |
28
// +----------+ +----------+
29
// | Linux | | Linux |
30
// | Bridge | | Bridge |
31
// +----------+ +----------+
32
// | |
33
// +------------+ +-------------+
34
// | "tap-left" | | "tap-right" |
35
// +------------+ +-------------+
36
// | n0 n1 |
37
// | +--------+ +--------+ |
38
// +-------| tap | | tap |-------+
39
// | bridge | | bridge |
40
// +--------+ +--------+
41
// | CSMA | | CSMA |
42
// +--------+ +--------+
43
// | |
44
// | |
45
// | |
46
// ===============
47
// CSMA LAN
48
//
49
#include "ns3/core-module.h"
50
#include "ns3/csma-module.h"
51
#include "ns3/network-module.h"
52
#include "ns3/tap-bridge-module.h"
53
54
#include <fstream>
55
#include <iostream>
56
57
using namespace
ns3
;
58
59
NS_LOG_COMPONENT_DEFINE
(
"TapCsmaVirtualMachineExample"
);
60
61
int
62
main(
int
argc,
char
* argv[])
63
{
64
CommandLine
cmd
(__FILE__);
65
cmd
.Parse(argc, argv);
66
67
//
68
// We are interacting with the outside, real, world. This means we have to
69
// interact in real-time and therefore means we have to use the real-time
70
// simulator and take the time to calculate checksums.
71
//
72
GlobalValue::Bind
(
"SimulatorImplementationType"
,
StringValue
(
"ns3::RealtimeSimulatorImpl"
));
73
GlobalValue::Bind
(
"ChecksumEnabled"
,
BooleanValue
(
true
));
74
75
//
76
// Create two ghost nodes. The first will represent the virtual machine host
77
// on the left side of the network; and the second will represent the VM on
78
// the right side.
79
//
80
NodeContainer
nodes
;
81
nodes
.
Create
(2);
82
83
//
84
// Use a CsmaHelper to get a CSMA channel created, and the needed net
85
// devices installed on both of the nodes. The data rate and delay for the
86
// channel can be set through the command-line parser. For example,
87
//
88
// ./ns3 run "tap-csma-virtual-machine --ns3::CsmaChannel::DataRate=10000000"
89
//
90
CsmaHelper
csma
;
91
NetDeviceContainer
devices
=
csma
.Install(
nodes
);
92
93
//
94
// Use the TapBridgeHelper to connect to the pre-configured tap devices for
95
// the left side. We go with "UseBridge" mode since the CSMA devices support
96
// promiscuous mode and can therefore make it appear that the bridge is
97
// extended into ns-3. The install method essentially bridges the specified
98
// tap to the specified CSMA device.
99
//
100
TapBridgeHelper
tapBridge;
101
tapBridge.
SetAttribute
(
"Mode"
,
StringValue
(
"UseBridge"
));
102
tapBridge.
SetAttribute
(
"DeviceName"
,
StringValue
(
"tap-left"
));
103
tapBridge.
Install
(
nodes
.
Get
(0),
devices
.Get(0));
104
105
//
106
// Connect the right side tap to the right side CSMA device on the right-side
107
// ghost node.
108
//
109
tapBridge.
SetAttribute
(
"DeviceName"
,
StringValue
(
"tap-right"
));
110
tapBridge.
Install
(
nodes
.
Get
(1),
devices
.Get(1));
111
112
//
113
// Run the simulation for ten minutes to give the user time to play around
114
//
115
Simulator::Stop
(
Seconds
(600.));
116
Simulator::Run
();
117
Simulator::Destroy
();
118
119
return
0;
120
}
ns3::BooleanValue
Definition
boolean.h:26
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::CsmaHelper
build a set of CsmaNetDevice objects
Definition
csma-helper.h:37
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:126
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition
net-device-container.h:32
ns3::NodeContainer
keep track of a set of node pointers.
Definition
node-container.h:29
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition
node-container.cc:73
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition
node-container.cc:67
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::Simulator::Stop
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition
simulator.cc:175
ns3::StringValue
Hold variables of type string.
Definition
string.h:45
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:27
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:50
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:35
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1308
nodes
NodeContainer nodes
Definition
lr-wpan-bootstrap.cc:43
first.devices
devices
Definition
first.py:31
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
second.csma
csma
Definition
second.py:52
second.cmd
cmd
Definition
second.py:29
src
tap-bridge
examples
tap-csma-virtual-machine.cc
Generated on Fri Nov 8 2024 13:59:06 for ns-3 by
1.11.0