A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
fd2fd-onoff.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 University of Washington, 2012 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Alina Quereilhac <alina.quereilhac@inria.fr>
7 *
8 */
9
10//
11// node 0 node 1
12// +----------------+ +----------------+
13// | ns-3 TCP | | ns-3 TCP |
14// +----------------+ +----------------+
15// | 10.1.1.1 | | 10.1.1.2 |
16// +----------------+ socketpair +----------------+
17// | fd-net-device |--------------| fd-net-device |
18// +----------------+ +----------------+
19//
20// This example is aimed at measuring the throughput of the FdNetDevice
21// in a pure simulation. For this purpose two FdNetDevices, attached to
22// different nodes but in a same simulation, are connected using a socket pair.
23// TCP traffic is sent at a saturating data rate. Then the throughput can
24// be obtained from the generated .pcap files.
25//
26// Steps to run the experiment:
27//
28// $ ./ns3 run "fd2fd-onoff"
29// $ ./ns3 run "fd2fd-onoff --tcpMode=1"
30//
31
32#include "ns3/applications-module.h"
33#include "ns3/core-module.h"
34#include "ns3/fd-net-device-module.h"
35#include "ns3/internet-module.h"
36#include "ns3/network-module.h"
37
38#include <errno.h>
39#include <sys/socket.h>
40
41using namespace ns3;
42
43NS_LOG_COMPONENT_DEFINE("FdNetDeviceSaturationExample");
44
45int
46main(int argc, char* argv[])
47{
48 // Command-line arguments
49 //
50 bool tcpMode = false;
51 CommandLine cmd(__FILE__);
52 cmd.AddValue("tcpMode", "1:true, 0:false, default mode UDP", tcpMode);
53 cmd.Parse(argc, argv);
54
55 std::string factory;
56 if (tcpMode)
57 {
58 factory = "ns3::TcpSocketFactory";
59 }
60 else
61 {
62 factory = "ns3::UdpSocketFactory";
63 }
64
65 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
66
67 uint16_t sinkPort = 8000;
68 uint32_t packetSize = 10000; // bytes
69 std::string dataRate("10Mb/s");
70
71 NS_LOG_INFO("Create Node");
73 nodes.Create(2);
74
75 NS_LOG_INFO("Create Device");
78
79 int sv[2];
80 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) < 0)
81 {
82 NS_FATAL_ERROR("Error creating pipe=" << strerror(errno));
83 }
84
85 Ptr<NetDevice> d1 = devices.Get(0);
86 Ptr<FdNetDevice> clientDevice = d1->GetObject<FdNetDevice>();
87 clientDevice->SetFileDescriptor(sv[0]);
88
89 Ptr<NetDevice> d2 = devices.Get(1);
90 Ptr<FdNetDevice> serverDevice = d2->GetObject<FdNetDevice>();
91 serverDevice->SetFileDescriptor(sv[1]);
92
93 NS_LOG_INFO("Add Internet Stack");
94 InternetStackHelper internetStackHelper;
95 internetStackHelper.SetIpv4StackInstall(true);
96 internetStackHelper.Install(nodes);
97
98 NS_LOG_INFO("Create IPv4 Interface");
99 Ipv4AddressHelper addresses;
100 addresses.SetBase("10.0.0.0", "255.255.255.0");
101 Ipv4InterfaceContainer interfaces = addresses.Assign(devices);
102
103 Ptr<Node> clientNode = nodes.Get(0);
104 Ipv4Address serverIp = interfaces.GetAddress(1);
105 Ptr<Node> serverNode = nodes.Get(1);
106
107 // server
108 Address sinkLocalAddress(InetSocketAddress(serverIp, sinkPort));
109
110 PacketSinkHelper sinkHelper(factory, sinkLocalAddress);
111 ApplicationContainer sinkApp = sinkHelper.Install(serverNode);
112 sinkApp.Start(Seconds(0.0));
113 sinkApp.Stop(Seconds(30.0));
114 fd.EnablePcap("fd2fd-onoff-server", serverDevice);
115
116 // client
117 AddressValue serverAddress(InetSocketAddress(serverIp, sinkPort));
118 OnOffHelper onoff(factory, Address());
119 onoff.SetAttribute("Remote", serverAddress);
120 onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
121 onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
122 onoff.SetAttribute("DataRate", DataRateValue(dataRate));
123 onoff.SetAttribute("PacketSize", UintegerValue(packetSize));
124 ApplicationContainer clientApps = onoff.Install(clientNode);
125 clientApps.Start(Seconds(2.0));
126 clientApps.Stop(Seconds(29.0));
127 fd.EnablePcap("fd2fd-onoff-client", clientDevice);
128
132
133 return 0;
134}
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 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.
a NetDevice to read/write network traffic from/into a file descriptor.
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...
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...
Ipv4 addresses are stored in host order in this class.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
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 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
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
clientApps
Definition first.py:53
devices
Definition first.py:31
interfaces
Definition first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t packetSize
Packet size generated at the AP.