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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 *
19 */
20
21//
22// node 0 node 1
23// +----------------+ +----------------+
24// | ns-3 TCP | | ns-3 TCP |
25// +----------------+ +----------------+
26// | 10.1.1.1 | | 10.1.1.2 |
27// +----------------+ socketpair +----------------+
28// | fd-net-device |--------------| fd-net-device |
29// +----------------+ +----------------+
30//
31// This example is aimed at measuring the throughput of the FdNetDevice
32// in a pure simulation. For this purpose two FdNetDevices, attached to
33// different nodes but in a same simulation, are connected using a socket pair.
34// TCP traffic is sent at a saturating data rate. Then the throughput can
35// be obtained from the generated .pcap files.
36//
37// Steps to run the experiment:
38//
39// $ ./ns3 run "fd2fd-onoff"
40// $ ./ns3 run "fd2fd-onoff --tcpMode=1"
41//
42
43#include "ns3/applications-module.h"
44#include "ns3/core-module.h"
45#include "ns3/fd-net-device-module.h"
46#include "ns3/internet-module.h"
47#include "ns3/network-module.h"
48
49#include <errno.h>
50#include <sys/socket.h>
51
52using namespace ns3;
53
54NS_LOG_COMPONENT_DEFINE("FdNetDeviceSaturationExample");
55
56int
57main(int argc, char* argv[])
58{
59 // Command-line arguments
60 //
61 bool tcpMode = false;
62 CommandLine cmd(__FILE__);
63 cmd.AddValue("tcpMode", "1:true, 0:false, default mode UDP", tcpMode);
64 cmd.Parse(argc, argv);
65
66 std::string factory;
67 if (tcpMode)
68 {
69 factory = "ns3::TcpSocketFactory";
70 }
71 else
72 {
73 factory = "ns3::UdpSocketFactory";
74 }
75
76 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
77
78 uint16_t sinkPort = 8000;
79 uint32_t packetSize = 10000; // bytes
80 std::string dataRate("10Mb/s");
81
82 NS_LOG_INFO("Create Node");
84 nodes.Create(2);
85
86 NS_LOG_INFO("Create Device");
89
90 int sv[2];
91 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) < 0)
92 {
93 NS_FATAL_ERROR("Error creating pipe=" << strerror(errno));
94 }
95
96 Ptr<NetDevice> d1 = devices.Get(0);
97 Ptr<FdNetDevice> clientDevice = d1->GetObject<FdNetDevice>();
98 clientDevice->SetFileDescriptor(sv[0]);
99
100 Ptr<NetDevice> d2 = devices.Get(1);
101 Ptr<FdNetDevice> serverDevice = d2->GetObject<FdNetDevice>();
102 serverDevice->SetFileDescriptor(sv[1]);
103
104 NS_LOG_INFO("Add Internet Stack");
105 InternetStackHelper internetStackHelper;
106 internetStackHelper.SetIpv4StackInstall(true);
107 internetStackHelper.Install(nodes);
108
109 NS_LOG_INFO("Create IPv4 Interface");
110 Ipv4AddressHelper addresses;
111 addresses.SetBase("10.0.0.0", "255.255.255.0");
112 Ipv4InterfaceContainer interfaces = addresses.Assign(devices);
113
114 Ptr<Node> clientNode = nodes.Get(0);
115 Ipv4Address serverIp = interfaces.GetAddress(1);
116 Ptr<Node> serverNode = nodes.Get(1);
117
118 // server
119 Address sinkLocalAddress(InetSocketAddress(serverIp, sinkPort));
120
121 PacketSinkHelper sinkHelper(factory, sinkLocalAddress);
122 ApplicationContainer sinkApp = sinkHelper.Install(serverNode);
123 sinkApp.Start(Seconds(0.0));
124 sinkApp.Stop(Seconds(30.0));
125 fd.EnablePcap("fd2fd-onoff-server", serverDevice);
126
127 // client
128 AddressValue serverAddress(InetSocketAddress(serverIp, sinkPort));
129 OnOffHelper onoff(factory, Address());
130 onoff.SetAttribute("Remote", serverAddress);
131 onoff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
132 onoff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
133 onoff.SetAttribute("DataRate", DataRateValue(dataRate));
134 onoff.SetAttribute("PacketSize", UintegerValue(packetSize));
135 ApplicationContainer clientApps = onoff.Install(clientNode);
136 clientApps.Start(Seconds(2.0));
137 clientApps.Stop(Seconds(29.0));
138 fd.EnablePcap("fd2fd-onoff-client", clientDevice);
139
143
144 return 0;
145}
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Address.
Definition: address.h:286
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
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.
Definition: fd-net-device.h:84
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.
Definition: ipv4-address.h:42
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.
Definition: on-off-helper.h:37
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.
Definition: ptr.h:77
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
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#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:1319
NodeContainer nodes
ns devices
Definition: first.py:42
ns clientApps
Definition: first.py:64
ns interfaces
Definition: first.py:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40
static const uint32_t packetSize
Packet size generated at the AP.