A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tap-wifi-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// | wifi | | wifi |
42// +--------+ +--------+
43// | |
44// ((*)) ((*))
45//
46// Wifi LAN
47//
48// ((*))
49// |
50// +--------+
51// | wifi |
52// +--------+
53// | access |
54// | point |
55// +--------+
56//
57#include "ns3/core-module.h"
58#include "ns3/mobility-module.h"
59#include "ns3/network-module.h"
60#include "ns3/tap-bridge-module.h"
61#include "ns3/wifi-module.h"
62
63#include <fstream>
64#include <iostream>
65
66using namespace ns3;
67
68NS_LOG_COMPONENT_DEFINE("TapWifiVirtualMachineExample");
69
70int
71main(int argc, char* argv[])
72{
73 CommandLine cmd(__FILE__);
74 cmd.Parse(argc, argv);
75
76 //
77 // We are interacting with the outside, real, world. This means we have to
78 // interact in real-time and therefore means we have to use the real-time
79 // simulator and take the time to calculate checksums.
80 //
81 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
82 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
83
84 //
85 // Create two ghost nodes. The first will represent the virtual machine host
86 // on the left side of the network; and the second will represent the VM on
87 // the right side.
88 //
90 nodes.Create(2);
91
92 //
93 // We're going to use 802.11 A so set up a wifi helper to reflect that.
94 //
96 wifi.SetStandard(WIFI_STANDARD_80211a);
97 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
98 "DataMode",
99 StringValue("OfdmRate54Mbps"));
100
101 //
102 // No reason for pesky access points, so we'll use an ad-hoc network.
103 //
104 WifiMacHelper wifiMac;
105 wifiMac.SetType("ns3::AdhocWifiMac");
106
107 //
108 // Configure the physical layer.
109 //
111 YansWifiPhyHelper wifiPhy;
112 wifiPhy.SetChannel(wifiChannel.Create());
113
114 //
115 // Install the wireless devices onto our ghost nodes.
116 //
117 NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);
118
119 //
120 // We need location information since we are talking about wifi, so add a
121 // constant position to the ghost nodes.
122 //
125 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
126 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
127 mobility.SetPositionAllocator(positionAlloc);
128 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
129 mobility.Install(nodes);
130
131 //
132 // Use the TapBridgeHelper to connect to the pre-configured tap devices for
133 // the left side. We go with "UseLocal" mode since the wifi devices do not
134 // support promiscuous mode (because of their natures0. This is a special
135 // case mode that allows us to extend a linux bridge into ns-3 IFF we will
136 // only see traffic from one other device on that bridge. That is the case
137 // for this configuration.
138 //
139 TapBridgeHelper tapBridge;
140 tapBridge.SetAttribute("Mode", StringValue("UseLocal"));
141 tapBridge.SetAttribute("DeviceName", StringValue("tap-left"));
142 tapBridge.Install(nodes.Get(0), devices.Get(0));
143
144 //
145 // Connect the right side tap to the right side wifi device on the right-side
146 // ghost node.
147 //
148 tapBridge.SetAttribute("DeviceName", StringValue("tap-right"));
149 tapBridge.Install(nodes.Get(1), devices.Get(1));
150
151 //
152 // Run the simulation for ten minutes to give the user time to play around
153 //
157
158 return 0;
159}
Parse command-line arguments.
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...
Helper class used to assign positions and mobility models to nodes.
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.
Smart pointer class similar to boost::intrusive_ptr.
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.
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
@ WIFI_STANDARD_80211a
NodeContainer nodes
devices
Definition first.py:31
Every class exported by the ns3 library is enclosed in the ns3 namespace.
wifi
Definition third.py:84
mobility
Definition third.py:92