A Discrete-Event Network Simulator
API
fd2fd-onoff.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 University of Washington, 2012 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19  *
20  */
21 
22 //
23 // node 0 node 1
24 // +----------------+ +----------------+
25 // | ns-3 TCP | | ns-3 TCP |
26 // +----------------+ +----------------+
27 // | 10.1.1.1 | | 10.1.1.2 |
28 // +----------------+ socketpair +----------------+
29 // | fd-net-device |--------------| fd-net-device |
30 // +----------------+ +----------------+
31 //
32 // This example is aimed at measuring the throughput of the FdNetDevice
33 // in a pure simulation. For this purpose two FdNetDevices, attached to
34 // different nodes but in a same simulation, are connected using a socket pair.
35 // TCP traffic is sent at a saturating data rate. Then the throughput can
36 // be obtained from the generated .pcap files.
37 //
38 // Steps to run the experiment:
39 //
40 // $ ./waf --run="fd2fd-onoff"
41 // $ ./waf --run="fd2fd-onoff --tcpMode=1"
42 //
43 
44 #include <sys/socket.h>
45 #include <errno.h>
46 
47 #include "ns3/core-module.h"
48 #include "ns3/network-module.h"
49 #include "ns3/internet-module.h"
50 #include "ns3/fd-net-device-module.h"
51 #include "ns3/applications-module.h"
52 
53 using namespace ns3;
54 
55 NS_LOG_COMPONENT_DEFINE ("FdNetDeviceSaturationExample");
56 
57 int
58 main (int argc, char *argv[])
59 {
60 
61  // Command-line arguments
62  //
63  bool tcpMode = false;
64  CommandLine cmd (__FILE__);
65  cmd.AddValue ("tcpMode", "1:true, 0:false, default mode UDP",tcpMode);
66  cmd.Parse (argc, argv);
67 
68  std::string factory;
69  if (tcpMode==1)
70  {
71  factory = "ns3::TcpSocketFactory";
72  }
73  else
74  {
75  factory = "ns3::UdpSocketFactory";
76  }
77 
78  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
79 
80  uint16_t sinkPort = 8000;
81  uint32_t packetSize = 10000; // bytes
82  std::string dataRate("10Mb/s");
83 
84  NS_LOG_INFO ("Create Node");
86  nodes.Create (2);
87 
88  NS_LOG_INFO ("Create Device");
91 
92  int sv[2];
93  if (socketpair (AF_UNIX, SOCK_DGRAM, 0, sv) < 0)
94  {
95  NS_FATAL_ERROR ("Error creating pipe=" << strerror (errno));
96  }
97 
98  Ptr<NetDevice> d1 = devices.Get (0);
99  Ptr<FdNetDevice> clientDevice = d1->GetObject<FdNetDevice> ();
100  clientDevice->SetFileDescriptor (sv[0]);
101 
102  Ptr<NetDevice> d2 = devices.Get (1);
103  Ptr<FdNetDevice> serverDevice = d2->GetObject<FdNetDevice> ();
104  serverDevice->SetFileDescriptor (sv[1]);
105 
106  NS_LOG_INFO ("Add Internet Stack");
107  InternetStackHelper internetStackHelper;
108  internetStackHelper.SetIpv4StackInstall(true);
109  internetStackHelper.Install (nodes);
110 
111  NS_LOG_INFO ("Create IPv4 Interface");
112  Ipv4AddressHelper addresses;
113  addresses.SetBase ("10.0.0.0", "255.255.255.0");
115 
116  Ptr<Node> clientNode = nodes.Get (0);
117  Ipv4Address serverIp = interfaces.GetAddress (1);
118  Ptr<Node> serverNode = nodes.Get (1);
119 
120  // server
121  Address sinkLocalAddress (InetSocketAddress (serverIp, sinkPort));
122 
123  PacketSinkHelper sinkHelper (factory, sinkLocalAddress);
124  ApplicationContainer sinkApp = sinkHelper.Install (serverNode);
125  sinkApp.Start (Seconds (0.0));
126  sinkApp.Stop (Seconds (30.0));
127  fd.EnablePcap ("fd2fd-onoff-server", serverDevice);
128 
129  // client
130  AddressValue serverAddress (InetSocketAddress (serverIp, sinkPort));
131  OnOffHelper onoff (factory, Address ());
132  onoff.SetAttribute ("Remote", serverAddress);
133  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
134  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
135  onoff.SetAttribute ("DataRate", DataRateValue (dataRate));
136  onoff.SetAttribute ("PacketSize", UintegerValue (packetSize));
137  ApplicationContainer clientApps = onoff.Install (clientNode);
138  clientApps.Start (Seconds (2.0));
139  clientApps.Stop (Seconds (29.0));
140  fd.EnablePcap ("fd2fd-onoff-client", clientDevice);
141 
142  Simulator::Stop (Seconds (30.0));
143  Simulator::Run ();
145 }
holds a vector of ns3::Application pointers.
an Inet address class
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:36
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetFileDescriptor(int fd)
Set the associated file descriptor.
Hold variables of type string.
Definition: string.h:41
static const uint32_t packetSize
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
aggregate IP/TCP/UDP functionality to existing Nodes.
build a set of FdNetDevice objects Normally we eschew multiple inheritance, however, the classes PcapUserHelperForDevice and AsciiTraceUserHelperForDevice are treated as "mixins".
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
a polymophic address class
Definition: address.h:90
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
nodes
Definition: first.py:32
Hold an unsigned integer type.
Definition: uinteger.h:44
void SetIpv4StackInstall(bool enable)
Enable/disable IPv4 stack install.
holds a vector of ns3::NetDevice pointers
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...
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:226
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
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:41
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
AttributeValue implementation for DataRate.
Definition: data-rate.h:229
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
interfaces
Definition: first.py:48
clientApps
Definition: first.py:61
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:84
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
devices
Definition: first.py:39
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.