A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tap-wifi-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 // | wifi | | wifi |
54 // +--------+ +--------+
55 // | |
56 // ((*)) ((*))
57 //
58 // Wifi LAN
59 //
60 // ((*))
61 // |
62 // +--------+
63 // | wifi |
64 // +--------+
65 // | access |
66 // | point |
67 // +--------+
68 //
69 #include <iostream>
70 #include <fstream>
71 
72 #include "ns3/core-module.h"
73 #include "ns3/network-module.h"
74 #include "ns3/mobility-module.h"
75 #include "ns3/wifi-module.h"
76 #include "ns3/tap-bridge-module.h"
77 
78 using namespace ns3;
79 
80 NS_LOG_COMPONENT_DEFINE ("TapWifiVirtualMachineExample");
81 
82 int
83 main (int argc, char *argv[])
84 {
85  CommandLine cmd;
86  cmd.Parse (argc, argv);
87 
88  //
89  // We are interacting with the outside, real, world. This means we have to
90  // interact in real-time and therefore means we have to use the real-time
91  // simulator and take the time to calculate checksums.
92  //
93  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
94  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
95 
96  //
97  // Create two ghost nodes. The first will represent the virtual machine host
98  // on the left side of the network; and the second will represent the VM on
99  // the right side.
100  //
101  NodeContainer nodes;
102  nodes.Create (2);
103 
104  //
105  // We're going to use 802.11 A so set up a wifi helper to reflect that.
106  //
109  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate54Mbps"));
110 
111  //
112  // No reason for pesky access points, so we'll use an ad-hoc network.
113  //
115  wifiMac.SetType ("ns3::AdhocWifiMac");
116 
117  //
118  // Configure the physcial layer.
119  //
122  wifiPhy.SetChannel (wifiChannel.Create ());
123 
124  //
125  // Install the wireless devices onto our ghost nodes.
126  //
127  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
128 
129  //
130  // We need location information since we are talking about wifi, so add a
131  // constant position to the ghost nodes.
132  //
133  MobilityHelper mobility;
134  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
135  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
136  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
137  mobility.SetPositionAllocator (positionAlloc);
138  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
139  mobility.Install (nodes);
140 
141  //
142  // Use the TapBridgeHelper to connect to the pre-configured tap devices for
143  // the left side. We go with "UseLocal" mode since the wifi devices do not
144  // support promiscuous mode (because of their natures0. This is a special
145  // case mode that allows us to extend a linux bridge into ns-3 IFF we will
146  // only see traffic from one other device on that bridge. That is the case
147  // for this configuration.
148  //
149  TapBridgeHelper tapBridge;
150  tapBridge.SetAttribute ("Mode", StringValue ("UseLocal"));
151  tapBridge.SetAttribute ("DeviceName", StringValue ("tap-left"));
152  tapBridge.Install (nodes.Get (0), devices.Get (0));
153 
154  //
155  // Connect the right side tap to the right side wifi device on the right-side
156  // ghost node.
157  //
158  tapBridge.SetAttribute ("DeviceName", StringValue ("tap-right"));
159  tapBridge.Install (nodes.Get (1), devices.Get (1));
160 
161  //
162  // Run the simulation for ten minutes to give the user time to play around
163  //
164  Simulator::Stop (Seconds (600.));
165  Simulator::Run ();
167 }
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Hold a bool native type.
Definition: boolean.h:38
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:68
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.
Make it easy to create and manage PHY objects for the yans model.
static YansWifiChannelHelper Default(void)
void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
static YansWifiPhyHelper Default(void)
helps to create WifiNetDevice objects
Definition: wifi-helper.h:92
a 3d vector
Definition: vector.h:31
NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:97
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
holds a vector of ns3::NetDevice pointers
void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:91
static NqosWifiMacHelper Default(void)
static void Bind(std::string name, const AttributeValue &value)
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
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.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
manage and create wifi channel objects for the yans model.
Helper class used to assign positions and mobility models to nodes.
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.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
static WifiHelper Default(void)
Definition: wifi-helper.cc:60