A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tap-csma-virtual-machine.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16//
17// This is an illustration of how one could use virtualization techniques to
18// allow running applications on virtual machines talking over simulated
19// networks.
20//
21// The actual steps required to configure the virtual machines can be rather
22// involved, so we don't go into that here. Please have a look at one of
23// our HOWTOs on the nsnam wiki for more details about how to get the
24// system configured. For an example, have a look at "HOWTO Use Linux
25// Containers to set up virtual networks" which uses this code as an
26// example.
27//
28// The configuration you are after is explained in great detail in the
29// HOWTO, but looks like the following:
30//
31// +----------+ +----------+
32// | virtual | | virtual |
33// | Linux | | Linux |
34// | Host | | Host |
35// | | | |
36// | eth0 | | eth0 |
37// +----------+ +----------+
38// | |
39// +----------+ +----------+
40// | Linux | | Linux |
41// | Bridge | | Bridge |
42// +----------+ +----------+
43// | |
44// +------------+ +-------------+
45// | "tap-left" | | "tap-right" |
46// +------------+ +-------------+
47// | n0 n1 |
48// | +--------+ +--------+ |
49// +-------| tap | | tap |-------+
50// | bridge | | bridge |
51// +--------+ +--------+
52// | CSMA | | CSMA |
53// +--------+ +--------+
54// | |
55// | |
56// | |
57// ===============
58// CSMA LAN
59//
60#include "ns3/core-module.h"
61#include "ns3/csma-module.h"
62#include "ns3/network-module.h"
63#include "ns3/tap-bridge-module.h"
64
65#include <fstream>
66#include <iostream>
67
68using namespace ns3;
69
70NS_LOG_COMPONENT_DEFINE("TapCsmaVirtualMachineExample");
71
72int
73main(int argc, char* argv[])
74{
75 CommandLine cmd(__FILE__);
76 cmd.Parse(argc, argv);
77
78 //
79 // We are interacting with the outside, real, world. This means we have to
80 // interact in real-time and therefore means we have to use the real-time
81 // simulator and take the time to calculate checksums.
82 //
83 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
84 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
85
86 //
87 // Create two ghost nodes. The first will represent the virtual machine host
88 // on the left side of the network; and the second will represent the VM on
89 // the right side.
90 //
92 nodes.Create(2);
93
94 //
95 // Use a CsmaHelper to get a CSMA channel created, and the needed net
96 // devices installed on both of the nodes. The data rate and delay for the
97 // channel can be set through the command-line parser. For example,
98 //
99 // ./ns3 run "tap-csma-virtual-machine --ns3::CsmaChannel::DataRate=10000000"
100 //
103
104 //
105 // Use the TapBridgeHelper to connect to the pre-configured tap devices for
106 // the left side. We go with "UseBridge" mode since the CSMA devices support
107 // promiscuous mode and can therefore make it appear that the bridge is
108 // extended into ns-3. The install method essentially bridges the specified
109 // tap to the specified CSMA device.
110 //
111 TapBridgeHelper tapBridge;
112 tapBridge.SetAttribute("Mode", StringValue("UseBridge"));
113 tapBridge.SetAttribute("DeviceName", StringValue("tap-left"));
114 tapBridge.Install(nodes.Get(0), devices.Get(0));
115
116 //
117 // Connect the right side tap to the right side CSMA device on the right-side
118 // ghost node.
119 //
120 tapBridge.SetAttribute("DeviceName", StringValue("tap-right"));
121 tapBridge.Install(nodes.Get(1), devices.Get(1));
122
123 //
124 // Run the simulation for ten minutes to give the user time to play around
125 //
129
130 return 0;
131}
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
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...
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
NodeContainer nodes
ns devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40
ns csma
Definition: second.py:63