A Discrete-Event Network Simulator
API
tap-wifi-dumbbell.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 // Network topology
17 //
18 // +----------+
19 // | external |
20 // | Linux |
21 // | Host |
22 // | |
23 // | "mytap" |
24 // +----------+
25 // | n0 n3 n4
26 // | +--------+ +------------+ +------------+
27 // +-------| tap | | | | |
28 // | bridge | ... | | | |
29 // +--------+ +------------+ +------------+
30 // | Wifi | | Wifi | P2P |-----| P2P | CSMA |
31 // +--------+ +------+-----+ +-----+------+
32 // | | ^ |
33 // ((*)) ((*)) | |
34 // P2P 10.1.2 |
35 // ((*)) ((*)) | n5 n6 n7
36 // | | | | | |
37 // n1 n2 ================
38 // Wifi 10.1.1 CSMA LAN 10.1.3
39 //
40 // The Wifi device on node zero is: 10.1.1.1
41 // The Wifi device on node one is: 10.1.1.2
42 // The Wifi device on node two is: 10.1.1.3
43 // The Wifi device on node three is: 10.1.1.4
44 // The P2P device on node three is: 10.1.2.1
45 // The P2P device on node four is: 10.1.2.2
46 // The CSMA device on node four is: 10.1.3.1
47 // The CSMA device on node five is: 10.1.3.2
48 // The CSMA device on node six is: 10.1.3.3
49 // The CSMA device on node seven is: 10.1.3.4
50 //
51 // Some simple things to do:
52 //
53 // 1) Ping one of the simulated nodes on the left side of the topology.
54 //
55 // ./ns3 run tap-wifi-dumbbell&
56 // ping 10.1.1.3
57 //
58 // 2) Configure a route in the linux host and ping once of the nodes on the
59 // right, across the point-to-point link. You will see relatively large
60 // delays due to CBR background traffic on the point-to-point (see next
61 // item).
62 //
63 // ./ns3 run tap-wifi-dumbbell&
64 // sudo route add -net 10.1.3.0 netmask 255.255.255.0 dev thetap gw 10.1.1.2
65 // ping 10.1.3.4
66 //
67 // Take a look at the pcap traces and note that the timing reflects the
68 // addition of the significant delay and low bandwidth configured on the
69 // point-to-point link along with the high traffic.
70 //
71 // 3) Fiddle with the background CBR traffic across the point-to-point
72 // link and watch the ping timing change. The OnOffApplication "DataRate"
73 // attribute defaults to 500kb/s and the "PacketSize" Attribute defaults
74 // to 512. The point-to-point "DataRate" is set to 512kb/s in the script,
75 // so in the default case, the link is pretty full. This should be
76 // reflected in large delays seen by ping. You can crank down the CBR
77 // traffic data rate and watch the ping timing change dramatically.
78 //
79 // ./ns3 run "tap-wifi-dumbbell --ns3::OnOffApplication::DataRate=100kb/s"&
80 // sudo route add -net 10.1.3.0 netmask 255.255.255.0 dev thetap gw 10.1.1.2
81 // ping 10.1.3.4
82 //
83 // 4) Try to run this in UseBridge mode. This allows you to bridge an ns-3
84 // simulation to an existing pre-configured bridge. This uses tap devices
85 // just for illustration, you can create your own bridge if you want.
86 //
87 // sudo tunctl -t mytap1
88 // sudo ifconfig mytap1 0.0.0.0 promisc up
89 // sudo tunctl -t mytap2
90 // sudo ifconfig mytap2 0.0.0.0 promisc up
91 // sudo brctl addbr mybridge
92 // sudo brctl addif mybridge mytap1
93 // sudo brctl addif mybridge mytap2
94 // sudo ifconfig mybridge 10.1.1.5 netmask 255.255.255.0 up
95 // ./ns3 run "tap-wifi-dumbbell --mode=UseBridge --tapName=mytap2"&
96 // ping 10.1.1.3
97 
98 #include "ns3/applications-module.h"
99 #include "ns3/core-module.h"
100 #include "ns3/csma-module.h"
101 #include "ns3/internet-module.h"
102 #include "ns3/ipv4-global-routing-helper.h"
103 #include "ns3/mobility-module.h"
104 #include "ns3/network-module.h"
105 #include "ns3/point-to-point-module.h"
106 #include "ns3/tap-bridge-module.h"
107 #include "ns3/wifi-module.h"
108 
109 #include <fstream>
110 #include <iostream>
111 
112 using namespace ns3;
113 
114 NS_LOG_COMPONENT_DEFINE("TapDumbbellExample");
115 
116 int
117 main(int argc, char* argv[])
118 {
119  std::string mode = "ConfigureLocal";
120  std::string tapName = "thetap";
121 
122  CommandLine cmd(__FILE__);
123  cmd.AddValue("mode", "Mode setting of TapBridge", mode);
124  cmd.AddValue("tapName", "Name of the OS tap device", tapName);
125  cmd.Parse(argc, argv);
126 
127  GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
128  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
129 
130  //
131  // The topology has a Wifi network of four nodes on the left side. We'll make
132  // node zero the AP and have the other three will be the STAs.
133  //
134  NodeContainer nodesLeft;
135  nodesLeft.Create(4);
136 
137  YansWifiPhyHelper wifiPhy;
139  wifiPhy.SetChannel(wifiChannel.Create());
140 
141  Ssid ssid = Ssid("left");
143  WifiMacHelper wifiMac;
144  wifi.SetRemoteStationManager("ns3::ArfWifiManager");
145 
146  wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
147  NetDeviceContainer devicesLeft = wifi.Install(wifiPhy, wifiMac, nodesLeft.Get(0));
148 
149  wifiMac.SetType("ns3::StaWifiMac",
150  "Ssid",
151  SsidValue(ssid),
152  "ActiveProbing",
153  BooleanValue(false));
154  devicesLeft.Add(
155  wifi.Install(wifiPhy,
156  wifiMac,
157  NodeContainer(nodesLeft.Get(1), nodesLeft.Get(2), nodesLeft.Get(3))));
158 
160  mobility.Install(nodesLeft);
161 
162  InternetStackHelper internetLeft;
163  internetLeft.Install(nodesLeft);
164 
165  Ipv4AddressHelper ipv4Left;
166  ipv4Left.SetBase("10.1.1.0", "255.255.255.0");
167  Ipv4InterfaceContainer interfacesLeft = ipv4Left.Assign(devicesLeft);
168 
169  TapBridgeHelper tapBridge(interfacesLeft.GetAddress(1));
170  tapBridge.SetAttribute("Mode", StringValue(mode));
171  tapBridge.SetAttribute("DeviceName", StringValue(tapName));
172  tapBridge.Install(nodesLeft.Get(0), devicesLeft.Get(0));
173 
174  //
175  // Now, create the right side.
176  //
177  NodeContainer nodesRight;
178  nodesRight.Create(4);
179 
180  CsmaHelper csmaRight;
181  csmaRight.SetChannelAttribute("DataRate", DataRateValue(5000000));
182  csmaRight.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
183 
184  NetDeviceContainer devicesRight = csmaRight.Install(nodesRight);
185 
186  InternetStackHelper internetRight;
187  internetRight.Install(nodesRight);
188 
189  Ipv4AddressHelper ipv4Right;
190  ipv4Right.SetBase("10.1.3.0", "255.255.255.0");
191  Ipv4InterfaceContainer interfacesRight = ipv4Right.Assign(devicesRight);
192 
193  //
194  // Stick in the point-to-point line between the sides.
195  //
196  PointToPointHelper p2p;
197  p2p.SetDeviceAttribute("DataRate", StringValue("512kbps"));
198  p2p.SetChannelAttribute("Delay", StringValue("10ms"));
199 
200  NodeContainer nodes = NodeContainer(nodesLeft.Get(3), nodesRight.Get(0));
202 
203  Ipv4AddressHelper ipv4;
204  ipv4.SetBase("10.1.2.0", "255.255.255.192");
206 
207  //
208  // Simulate some CBR traffic over the point-to-point link
209  //
210  uint16_t port = 9; // Discard port (RFC 863)
211  OnOffHelper onoff("ns3::UdpSocketFactory", InetSocketAddress(interfaces.GetAddress(1), port));
212  onoff.SetConstantRate(DataRate("500kb/s"));
213 
214  ApplicationContainer apps = onoff.Install(nodesLeft.Get(3));
215  apps.Start(Seconds(1.0));
216 
217  // Create a packet sink to receive these packets
218  PacketSinkHelper sink("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
219 
220  apps = sink.Install(nodesRight.Get(0));
221  apps.Start(Seconds(1.0));
222 
223  wifiPhy.EnablePcapAll("tap-wifi-dumbbell");
224 
225  csmaRight.EnablePcapAll("tap-wifi-dumbbell", false);
227 
228  Simulator::Stop(Seconds(60.));
229  Simulator::Run();
231 
232  return 0;
233 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:56
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:226
AttributeValue implementation for DataRate.
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...
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Hold variables of type string.
Definition: string.h:56
build TapBridge to allow ns-3 simulations to interact with Linux tap devices and processes on the Lin...
AttributeValue implementation for Time.
Definition: nstime.h:1423
helps to create WifiNetDevice objects
Definition: wifi-helper.h:325
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)
uint16_t port
Definition: dsdv-manet.cc:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1348
NodeContainer nodes
devices
Definition: first.py:35
interfaces
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:33
ssid
Definition: third.py:86
wifi
Definition: third.py:88
mobility
Definition: third.py:96
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55