A Discrete-Event Network Simulator
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
69using namespace ns3;
70
71NS_LOG_COMPONENT_DEFINE ("TapCsmaVirtualMachineExample");
72
73int
74main (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 //
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 //
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}
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Hold variables of type string.
Definition: string.h:41
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:205
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
devices
Definition: first.py:39
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
csma
Definition: second.py:63
cmd
Definition: second.py:35