A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
realtime-fd2fd-onoff.cc
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 meassuring the thoughput 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 thoughput can
36 // be obtained from the generated .pcap files.
37 //
38 // Steps to run the experiment:
39 //
40 // $ ./waf --run="fd2fd-onoff"
41 //
42 
43 #include <sys/socket.h>
44 #include <errno.h>
45 
46 #include "ns3/core-module.h"
47 #include "ns3/network-module.h"
48 #include "ns3/internet-module.h"
49 #include "ns3/fd-net-device-module.h"
50 #include "ns3/applications-module.h"
51 
52 using namespace ns3;
53 
54 NS_LOG_COMPONENT_DEFINE ("RealtimeFdNetDeviceSaturationExample");
55 
56 int
57 main (int argc, char *argv[])
58 {
59 
60  uint16_t sinkPort = 8000;
61  uint32_t packetSize = 10000; // bytes
62  std::string dataRate("1000Mb/s");
63 
64  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
65  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
66 
67  NS_LOG_INFO ("Create Node");
68  NodeContainer nodes;
69  nodes.Create (2);
70 
71  NS_LOG_INFO ("Create Device");
73  NetDeviceContainer devices = fd.Install (nodes);
74 
75  int sv[2];
76  if (socketpair (AF_UNIX, SOCK_DGRAM, 0, sv) < 0)
77  {
78  NS_FATAL_ERROR ("Error creating pipe=" << strerror (errno));
79  }
80 
81  Ptr<NetDevice> d1 = devices.Get (0);
82  Ptr<FdNetDevice> clientDevice = d1->GetObject<FdNetDevice> ();
83  clientDevice->SetFileDescriptor (sv[0]);
84 
85  Ptr<NetDevice> d2 = devices.Get (1);
86  Ptr<FdNetDevice> serverDevice = d2->GetObject<FdNetDevice> ();
87  serverDevice->SetFileDescriptor (sv[1]);
88 
89  NS_LOG_INFO ("Add Internet Stack");
90  InternetStackHelper internetStackHelper;
91  internetStackHelper.SetIpv4StackInstall(true);
92  internetStackHelper.Install (nodes);
93 
94  NS_LOG_INFO ("Create IPv4 Interface");
95  Ipv4AddressHelper addresses;
96  addresses.SetBase ("10.0.0.0", "255.255.255.0");
97  Ipv4InterfaceContainer interfaces = addresses.Assign (devices);
98 
99  Ptr<Node> clientNode = nodes.Get (0);
100  Ipv4Address serverIp = interfaces.GetAddress (1);
101  Ptr<Node> serverNode = nodes.Get (1);
102 
103  // server
104  Address sinkLocalAddress (InetSocketAddress (serverIp, sinkPort));
105  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
106  ApplicationContainer sinkApp = sinkHelper.Install (serverNode);
107  sinkApp.Start (Seconds (0.0));
108  sinkApp.Stop (Seconds (40.0));
109  fd.EnablePcap ("rt-fd2fd-onoff-server", serverDevice);
110 
111  // client
112  AddressValue serverAddress (InetSocketAddress (serverIp, sinkPort));
113  OnOffHelper onoff ("ns3::TcpSocketFactory", Address ());
114  onoff.SetAttribute ("Remote", serverAddress);
115  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
116  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
117  onoff.SetAttribute ("DataRate", DataRateValue (dataRate));
118  onoff.SetAttribute ("PacketSize", UintegerValue (packetSize));
119  ApplicationContainer clientApps = onoff.Install (clientNode);
120  clientApps.Start (Seconds (1.0));
121  clientApps.Stop (Seconds (39.0));
122  fd.EnablePcap ("rt-fd2fd-onoff-client", clientDevice);
123 
124  Simulator::Stop (Seconds (40.0));
125  Simulator::Run ();
127 }
holds a vector of ns3::Application pointers.
virtual NetDeviceContainer Install(Ptr< Node > node) const
an Inet address class
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Hold a bool native type.
Definition: boolean.h:38
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetFileDescriptor(int fd)
hold variables of type string
Definition: string.h:19
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
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". A mixin is a self-contained class that encapsulates a general attribute or a set of functionality that may be of interest to many other classes.
#define NS_LOG_INFO(msg)
Definition: log.h:264
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:41
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
a polymophic address class
Definition: address.h:86
Hold an unsigned integer type.
Definition: uinteger.h:46
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)
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
static void Destroy(void)
Definition: simulator.cc:121
keep track of a set of node pointers.
void Install(std::string nodeName) const
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
hold objects of type ns3::Address
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
hold objects of type ns3::DataRate
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:82
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Ptr< T > GetObject(void) const
Definition: object.h:332
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const