A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tap-wifi-dumbbell.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// Network topology
6//
7// +----------+
8// | external |
9// | Linux |
10// | Host |
11// | |
12// | "mytap" |
13// +----------+
14// | n0 n3 n4
15// | +--------+ +------------+ +------------+
16// +-------| tap | | | | |
17// | bridge | ... | | | |
18// +--------+ +------------+ +------------+
19// | Wifi | | Wifi | P2P |-----| P2P | CSMA |
20// +--------+ +------+-----+ +-----+------+
21// | | ^ |
22// ((*)) ((*)) | |
23// P2P 10.1.2 |
24// ((*)) ((*)) | n5 n6 n7
25// | | | | | |
26// n1 n2 ================
27// Wifi 10.1.1 CSMA LAN 10.1.3
28//
29// The Wifi device on node zero is: 10.1.1.1
30// The Wifi device on node one is: 10.1.1.2
31// The Wifi device on node two is: 10.1.1.3
32// The Wifi device on node three is: 10.1.1.4
33// The P2P device on node three is: 10.1.2.1
34// The P2P device on node four is: 10.1.2.2
35// The CSMA device on node four is: 10.1.3.1
36// The CSMA device on node five is: 10.1.3.2
37// The CSMA device on node six is: 10.1.3.3
38// The CSMA device on node seven is: 10.1.3.4
39//
40// Some simple things to do:
41//
42// 1) Ping one of the simulated nodes on the left side of the topology.
43//
44// ./ns3 run --enable-sudo tap-wifi-dumbbell&
45// ping 10.1.1.3
46//
47// 2) Configure a route in the linux host and ping once of the nodes on the
48// right, across the point-to-point link. You will see relatively large
49// delays due to CBR background traffic on the point-to-point (see next
50// item).
51//
52// ./ns3 run --enable-sudo tap-wifi-dumbbell&
53// sudo ip route add 10.1.3.0/24 dev thetap via 10.1.1.2
54// ping 10.1.3.4
55//
56// Take a look at the pcap traces and note that the timing reflects the
57// addition of the significant delay and low bandwidth configured on the
58// point-to-point link along with the high traffic.
59//
60// 3) Fiddle with the background CBR traffic across the point-to-point
61// link and watch the ping timing change. The OnOffApplication "DataRate"
62// attribute defaults to 500kb/s and the "PacketSize" Attribute defaults
63// to 512. The point-to-point "DataRate" is set to 512kb/s in the script,
64// so in the default case, the link is pretty full. This should be
65// reflected in large delays seen by ping. You can crank down the CBR
66// traffic data rate and watch the ping timing change dramatically.
67//
68// ./ns3 run --enable-sudo "tap-wifi-dumbbell --ns3::OnOffApplication::DataRate=100kb/s"&
69// sudo ip route add 10.1.3.0/24 dev thetap via 10.1.1.2
70// ping 10.1.3.4
71//
72// 4) Try to run this in UseBridge mode. This allows you to bridge an ns-3
73// simulation to an existing pre-configured bridge. This uses tap devices
74// just for illustration, you can create your own bridge if you want.
75// The "--enable-sudo" option to "./ns3 run" is not needed in this case
76// because devices are being created outside of ns-3 execution.
77//
78// sudo ip tuntap add mode tap mytap1
79// sudo ip link set mytap1 promisc on up
80// sudo ip tuntap add mode tap mytap2
81// sudo ip link set mytap2 promisc on up
82// sudo ip link add mybridge type bridge
83// sudo ip link set dev mytap1 master mybridge
84// sudo ip link set dev mytap2 master mybridge
85// sudo ip address add 10.1.1.5/24 dev mybridge
86// sudo ip link set dev mybridge up
87// ./ns3 run "tap-wifi-dumbbell --mode=UseBridge --tapName=mytap2"&
88// ping 10.1.1.3
89
90#include "ns3/applications-module.h"
91#include "ns3/core-module.h"
92#include "ns3/csma-module.h"
93#include "ns3/internet-module.h"
94#include "ns3/ipv4-global-routing-helper.h"
95#include "ns3/mobility-module.h"
96#include "ns3/network-module.h"
97#include "ns3/point-to-point-module.h"
98#include "ns3/tap-bridge-module.h"
99#include "ns3/wifi-module.h"
100
101#include <fstream>
102#include <iostream>
103
104using namespace ns3;
105
106NS_LOG_COMPONENT_DEFINE("TapDumbbellExample");
107
108int
109main(int argc, char* argv[])
110{
111 std::string mode = "ConfigureLocal";
112 std::string tapName = "thetap";
113
114 CommandLine cmd(__FILE__);
115 cmd.AddValue("mode", "Mode setting of TapBridge", mode);
116 cmd.AddValue("tapName", "Name of the OS tap device", tapName);
117 cmd.Parse(argc, argv);
118
119 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
120 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
121
122 //
123 // The topology has a Wifi network of four nodes on the left side. We'll make
124 // node zero the AP and have the other three will be the STAs.
125 //
126 NodeContainer nodesLeft;
127 nodesLeft.Create(4);
128
129 YansWifiPhyHelper wifiPhy;
131 wifiPhy.SetChannel(wifiChannel.Create());
132
133 Ssid ssid = Ssid("left");
135 WifiMacHelper wifiMac;
136 wifi.SetStandard(WIFI_STANDARD_80211a);
137
138 wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
139 NetDeviceContainer devicesLeft = wifi.Install(wifiPhy, wifiMac, nodesLeft.Get(0));
140
141 wifiMac.SetType("ns3::StaWifiMac",
142 "Ssid",
143 SsidValue(ssid),
144 "ActiveProbing",
145 BooleanValue(false));
146 devicesLeft.Add(
147 wifi.Install(wifiPhy,
148 wifiMac,
149 NodeContainer(nodesLeft.Get(1), nodesLeft.Get(2), nodesLeft.Get(3))));
150
152 mobility.Install(nodesLeft);
153
154 InternetStackHelper internetLeft;
155 internetLeft.Install(nodesLeft);
156
157 Ipv4AddressHelper ipv4Left;
158 ipv4Left.SetBase("10.1.1.0", "255.255.255.0");
159 Ipv4InterfaceContainer interfacesLeft = ipv4Left.Assign(devicesLeft);
160
161 TapBridgeHelper tapBridge(interfacesLeft.GetAddress(1));
162 tapBridge.SetAttribute("Mode", StringValue(mode));
163 tapBridge.SetAttribute("DeviceName", StringValue(tapName));
164 tapBridge.Install(nodesLeft.Get(0), devicesLeft.Get(0));
165
166 //
167 // Now, create the right side.
168 //
169 NodeContainer nodesRight;
170 nodesRight.Create(4);
171
172 CsmaHelper csmaRight;
173 csmaRight.SetChannelAttribute("DataRate", DataRateValue(5000000));
174 csmaRight.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
175
176 NetDeviceContainer devicesRight = csmaRight.Install(nodesRight);
177
178 InternetStackHelper internetRight;
179 internetRight.Install(nodesRight);
180
181 Ipv4AddressHelper ipv4Right;
182 ipv4Right.SetBase("10.1.3.0", "255.255.255.0");
183 Ipv4InterfaceContainer interfacesRight = ipv4Right.Assign(devicesRight);
184
185 //
186 // Stick in the point-to-point line between the sides.
187 //
189 p2p.SetDeviceAttribute("DataRate", StringValue("512kbps"));
190 p2p.SetChannelAttribute("Delay", StringValue("10ms"));
191
192 NodeContainer nodes = NodeContainer(nodesLeft.Get(3), nodesRight.Get(0));
194
196 ipv4.SetBase("10.1.2.0", "255.255.255.192");
197 Ipv4InterfaceContainer interfaces = ipv4.Assign(devices);
198
199 //
200 // Simulate some CBR traffic over the point-to-point link
201 //
202 uint16_t port = 9; // Discard port (RFC 863)
203 OnOffHelper onoff("ns3::UdpSocketFactory", InetSocketAddress(interfaces.GetAddress(1), port));
204 onoff.SetConstantRate(DataRate("500kb/s"));
205
206 ApplicationContainer apps = onoff.Install(nodesLeft.Get(3));
207 apps.Start(Seconds(1.0));
208
209 // Create a packet sink to receive these packets
210 PacketSinkHelper sink("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
211
212 apps = sink.Install(nodesRight.Get(0));
213 apps.Start(Seconds(1.0));
214
215 wifiPhy.EnablePcapAll("tap-wifi-dumbbell");
216
217 csmaRight.EnablePcapAll("tap-wifi-dumbbell", false);
219
223
224 return 0;
225}
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.
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Class for representing data rates.
Definition data-rate.h:78
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.
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.
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
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
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...
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)
uint16_t port
Definition dsdv-manet.cc:33
#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
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
@ WIFI_STANDARD_80211a
NodeContainer nodes
devices
Definition first.py:31
interfaces
Definition first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ssid
Definition third.py:82
wifi
Definition third.py:84
mobility
Definition third.py:92
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44