A Discrete-Event Network Simulator
API
fd-emu-onoff.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 University of Washington, 2012 INRIA
3  * 2017 Università' degli Studi di Napoli Federico II
4  * 2019 NITK Surathkal
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20  * Extended by: Pasquale Imputato <p.imputato@gmail.com>
21  * Harsh Patel <thadodaharsh10@gmail.com>
22  * Hrishikesh Hiraskar <hrishihiraskar@gmail.com>
23  * Mohit P. Tahiliani <tahiliani@nitk.edu.in>
24  *
25  */
26 
27 // +----------------------+ +-----------------------+
28 // | client host | | server host |
29 // +----------------------+ +-----------------------+
30 // | ns-3 Node 0 | | ns-3 Node 1 |
31 // | +----------------+ | | +----------------+ |
32 // | | ns-3 TCP | | | | ns-3 TCP | |
33 // | +----------------+ | | +----------------+ |
34 // | | ns-3 IPv4 | | | | ns-3 IPv4 | |
35 // | +----------------+ | | +----------------+ |
36 // | | FdNetDevice | | | | FdNetDevice | |
37 // | | or | | | | or | |
38 // | | NetmapNetDevice| | | | NetmapNetDevice| |
39 // | | or | | | | or | |
40 // | | DpdkNetDevice | | | | DpdkNetDevice | |
41 // | | 10.1.1.1 | | | | 10.1.1.2 | |
42 // | +----------------+ | | +----------------+ |
43 // | | fd | | | | fd | |
44 // | | or | | | | or | |
45 // | | EAL | | | | EAL | |
46 // | +----------------+ | | +----------------+ |
47 // | | eth0 | | | | eth0 | |
48 // | | or | | | | or | |
49 // | | 0000:00.1f | | | | 0000:00.1f | |
50 // +----+------------+----+ +-----+------------+----+
51 //
52 // 10.1.1.11 10.1.1.12
53 //
54 // | |
55 // +----------------------------+
56 //
57 // This example is aimed at measuring the throughput of the FdNetDevice
58 // when using the EmuFdNetDeviceHelper. This is achieved by saturating
59 // the channel with TCP traffic. Then the throughput can be obtained from
60 // the generated .pcap files.
61 //
62 // To run this example you will need two hosts (client & server).
63 // Steps to run the experiment:
64 //
65 // 1 - Connect the 2 computers with an Ethernet cable.
66 // 2 - Set the IP addresses on both Ethernet devices.
67 //
68 // client machine: $ sudo ip addr add dev eth0 10.1.1.11/24
69 // server machine: $ sudo ip addr add dev eth0 10.1.1.12/24
70 //
71 // 3 - Set both Ethernet devices to promiscuous mode.
72 //
73 // both machines: $ sudo ip link set eth0 promisc on
74 //
75 // 3' - If you run emulation in netmap or dpdk mode, you need before to load
76 // the netmap.ko or dpdk modules. The user is in charge to configure and
77 // build netmap/dpdk separately.
78 //
79 // 4 - Give root suid to the raw or netmap socket creator binary.
80 // If the --enable-sudo option was used to configure ns-3 with ns3, then the following
81 // step will not be necessary.
82 //
83 // both hosts: $ sudo chown root.root build/src/fd-net-device/ns3-dev-raw-sock-creator
84 // both hosts: $ sudo chmod 4755 build/src/fd-net-device/ns3-dev-raw-sock-creator
85 //
86 // or (if you run emulation in netmap mode):
87 // both hosts: $ sudo chown root.root build/src/fd-net-device/ns3-dev-netmap-device-creator
88 // both hosts: $ sudo chmod 4755 build/src/fd-net-device/ns3-dev-netmap-device-creator
89 //
90 // 4' - If you run emulation in dpdk mode, you will need to run example as root.
91 //
92 // 5 - In case of DpdkNetDevice, use device address instead of device name
93 //
94 // For example: 0000:00:1f.6 (can be obtained by lspci)
95 //
96 // 6 - Run the server side:
97 //
98 // server host: $ ./ns3 run "fd-emu-onoff --serverMode=1"
99 //
100 // 7 - Run the client side:
101 //
102 // client host: $ ./ns3 run "fd-emu-onoff"
103 //
104 
105 #include "ns3/applications-module.h"
106 #include "ns3/core-module.h"
107 #include "ns3/fd-net-device-module.h"
108 #include "ns3/internet-module.h"
109 #include "ns3/network-module.h"
110 
111 #include <fstream>
112 #include <iostream>
113 #include <string>
114 #include <vector>
115 
116 using namespace ns3;
117 
118 NS_LOG_COMPONENT_DEFINE("EmuFdNetDeviceSaturationExample");
119 
120 int
121 main(int argc, char* argv[])
122 {
123  uint16_t sinkPort = 8000;
124  uint32_t packetSize = 1400; // bytes
125  std::string dataRate("1000Mb/s");
126  bool serverMode = false;
127 
128  std::string deviceName("eth0");
129  std::string client("10.1.1.1");
130  std::string server("10.1.1.2");
131  std::string netmask("255.255.255.0");
132  std::string macClient("00:00:00:00:00:01");
133  std::string macServer("00:00:00:00:00:02");
134  std::string transportProt = "Tcp";
135  std::string socketType;
136 #ifdef HAVE_PACKET_H
137  std::string emuMode("raw");
138 #elif HAVE_NETMAP_USER_H
139  std::string emuMode("netmap");
140 #else // HAVE_DPDK_USER_H is true (otherwise this example is not compiled)
141  std::string emuMode("dpdk");
142 #endif
143 
144  CommandLine cmd(__FILE__);
145  cmd.AddValue("deviceName",
146  "Device name (in raw, netmap mode) or Device address (in dpdk mode, eg: "
147  "0000:00:1f.6). Use `lspci` to find device address.",
148  deviceName);
149  cmd.AddValue("client", "Local IP address (dotted decimal only please)", client);
150  cmd.AddValue("server", "Remote IP address (dotted decimal only please)", server);
151  cmd.AddValue("localmask", "Local mask address (dotted decimal only please)", netmask);
152  cmd.AddValue("serverMode", "1:true, 0:false, default client", serverMode);
153  cmd.AddValue("mac-client", "Mac Address for Server Client : 00:00:00:00:00:01", macClient);
154  cmd.AddValue("mac-server", "Mac Address for Server Default : 00:00:00:00:00:02", macServer);
155  cmd.AddValue("data-rate", "Data rate defaults to 1000Mb/s", dataRate);
156  cmd.AddValue("transportProt", "Transport protocol to use: Tcp, Udp", transportProt);
157  cmd.AddValue("emuMode", "Emulation mode in {raw, netmap}", emuMode);
158  cmd.Parse(argc, argv);
159 
160  if (transportProt == "Tcp")
161  {
162  socketType = "ns3::TcpSocketFactory";
163  }
164  else
165  {
166  socketType = "ns3::UdpSocketFactory";
167  }
168 
169  Ipv4Address remoteIp;
170  Ipv4Address localIp;
171  Mac48AddressValue localMac;
172 
173  Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(packetSize));
174 
175  if (serverMode)
176  {
177  remoteIp = Ipv4Address(client.c_str());
178  localIp = Ipv4Address(server.c_str());
179  localMac = Mac48AddressValue(macServer.c_str());
180  }
181  else
182  {
183  remoteIp = Ipv4Address(server.c_str());
184  localIp = Ipv4Address(client.c_str());
185  localMac = Mac48AddressValue(macClient.c_str());
186  }
187 
188  Ipv4Mask localMask(netmask.c_str());
189 
190  GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
191 
192  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
193 
194  NS_LOG_INFO("Create Node");
195  Ptr<Node> node = CreateObject<Node>();
196 
197  NS_LOG_INFO("Create Device");
198  FdNetDeviceHelper* helper = nullptr;
199 
200 #ifdef HAVE_PACKET_H
201  if (emuMode == "raw")
202  {
203  auto raw = new EmuFdNetDeviceHelper;
204  raw->SetDeviceName(deviceName);
205  helper = raw;
206  }
207 #endif
208 #ifdef HAVE_NETMAP_USER_H
209  if (emuMode == "netmap")
210  {
212  netmap->SetDeviceName(deviceName);
213  helper = netmap;
214  }
215 #endif
216 #ifdef HAVE_DPDK_USER_H
217  if (emuMode == "dpdk")
218  {
220  // Use e1000 driver library (this is for IGb PMD supporting Intel 1GbE NIC)
221  // NOTE: DPDK supports multiple Poll Mode Drivers (PMDs) and you can use it
222  // based on your NIC. You just need to set pmd library as follows:
223  dpdk->SetPmdLibrary("librte_pmd_e1000.so");
224  // Set dpdk driver to use for the NIC. `uio_pci_generic` supports most NICs.
225  dpdk->SetDpdkDriver("uio_pci_generic");
226  // Set device name
227  dpdk->SetDeviceName(deviceName);
228  helper = dpdk;
229  }
230 #endif
231 
232  if (helper == nullptr)
233  {
234  NS_ABORT_MSG(emuMode << " not supported.");
235  }
236 
237  NetDeviceContainer devices = helper->Install(node);
238  Ptr<NetDevice> device = devices.Get(0);
239  device->SetAttribute("Address", localMac);
240 
241  NS_LOG_INFO("Add Internet Stack");
242  InternetStackHelper internetStackHelper;
243  internetStackHelper.SetIpv4StackInstall(true);
244  internetStackHelper.Install(node);
245 
246  NS_LOG_INFO("Create IPv4 Interface");
247  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
248  uint32_t interface = ipv4->AddInterface(device);
249  Ipv4InterfaceAddress address = Ipv4InterfaceAddress(localIp, localMask);
250  ipv4->AddAddress(interface, address);
251  ipv4->SetMetric(interface, 1);
252  ipv4->SetUp(interface);
253 
254  if (serverMode)
255  {
256  Address sinkLocalAddress(InetSocketAddress(localIp, sinkPort));
257  PacketSinkHelper sinkHelper(socketType, sinkLocalAddress);
258  ApplicationContainer sinkApp = sinkHelper.Install(node);
259  sinkApp.Start(Seconds(1.0));
260  sinkApp.Stop(Seconds(60.0));
261 
262  helper->EnablePcap("fd-server", device);
263  }
264  else
265  {
266  AddressValue remoteAddress(InetSocketAddress(remoteIp, sinkPort));
267  OnOffHelper onoff(socketType, Address());
268  onoff.SetAttribute("Remote", remoteAddress);
269  onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
270  onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
271  onoff.SetAttribute("DataRate", DataRateValue(dataRate));
272  onoff.SetAttribute("PacketSize", UintegerValue(packetSize));
273 
274  ApplicationContainer clientApps = onoff.Install(node);
275  clientApps.Start(Seconds(4.0));
276  clientApps.Stop(Seconds(58.0));
277 
278  helper->EnablePcap("fd-client", device);
279  }
280 
281  Simulator::Stop(Seconds(61.0));
282  Simulator::Run();
284  delete helper;
285 
286  return 0;
287 }
a polymophic address class
Definition: address.h:101
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.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
build a DpdkNetDevice object attached to a physical network interface
void SetPmdLibrary(std::string pmdLibrary)
Sets PMD Library to be used for the NIC.
void SetDpdkDriver(std::string dpdkDriver)
Sets DPDK Driver to bind NIC to.
build a set of FdNetDevice objects attached to a physical network interface
void SetDeviceName(std::string deviceName)
Set the device name of this device.
build a set of FdNetDevice objects Normally we eschew multiple inheritance, however,...
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
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 SetIpv4StackInstall(bool enable)
Enable/disable IPv4 stack install.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
holds a vector of ns3::NetDevice pointers
build a set of FdNetDevice objects attached to a physical network interface
void SetDeviceName(std::string deviceName)
Set the device name of this device.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
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 EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
address
Definition: first.py:47
clientApps
Definition: first.py:64
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
static const uint32_t packetSize
Packet size generated at the AP.