A Discrete-Event Network Simulator
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
57using namespace ns3;
58
59NS_LOG_COMPONENT_DEFINE("TapCsmaVirtualMachineExample");
60
61int
62main(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 //
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 //
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 //
118
119 return 0;
120}
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
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:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
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:191
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
devices
Definition first.py:31
Every class exported by the ns3 library is enclosed in the ns3 namespace.