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