A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tap-csma-virtual-machine.cc
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;
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  // ./waf --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 ();
130 }
Hold a bool native type.
Definition: boolean.h:38
hold variables of type string
Definition: string.h:19
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
NetDeviceContainer Install(Ptr< Node > node) const
Definition: csma-helper.cc:215
holds a vector of ns3::NetDevice pointers
static void Bind(std::string name, const AttributeValue &value)
parse command-line argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
static void Destroy(void)
Definition: simulator.cc:121
keep track of a set of node pointers.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Ptr< NetDevice > Install(Ptr< Node > node, Ptr< NetDevice > nd)
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84
build TapBridge to allow ns-3 simulations to interact with Linux tap devices and processes on the Lin...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.