A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 //
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 ("FdNetDeviceSaturationExample");
55 
56 int
57 main (int argc, char *argv[])
58 {
59  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
60 
61  uint16_t sinkPort = 8000;
62  uint32_t packetSize = 10000; // bytes
63  std::string dataRate("10Mb/s");
64 
65  NS_LOG_INFO ("Create Node");
66  NodeContainer nodes;
67  nodes.Create (2);
68 
69  NS_LOG_INFO ("Create Device");
71  NetDeviceContainer devices = fd.Install (nodes);
72 
73  int sv[2];
74  if (socketpair (AF_UNIX, SOCK_DGRAM, 0, sv) < 0)
75  {
76  NS_FATAL_ERROR ("Error creating pipe=" << strerror (errno));
77  }
78 
79  Ptr<NetDevice> d1 = devices.Get (0);
80  Ptr<FdNetDevice> clientDevice = d1->GetObject<FdNetDevice> ();
81  clientDevice->SetFileDescriptor (sv[0]);
82 
83  Ptr<NetDevice> d2 = devices.Get (1);
84  Ptr<FdNetDevice> serverDevice = d2->GetObject<FdNetDevice> ();
85  serverDevice->SetFileDescriptor (sv[1]);
86 
87  NS_LOG_INFO ("Add Internet Stack");
88  InternetStackHelper internetStackHelper;
89  internetStackHelper.SetIpv4StackInstall(true);
90  internetStackHelper.Install (nodes);
91 
92  NS_LOG_INFO ("Create IPv4 Interface");
93  Ipv4AddressHelper addresses;
94  addresses.SetBase ("10.0.0.0", "255.255.255.0");
95  Ipv4InterfaceContainer interfaces = addresses.Assign (devices);
96 
97  Ptr<Node> clientNode = nodes.Get (0);
98  Ipv4Address serverIp = interfaces.GetAddress (1);
99  Ptr<Node> serverNode = nodes.Get (1);
100 
101  // server
102  Address sinkLocalAddress (InetSocketAddress (serverIp, sinkPort));
103  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
104  ApplicationContainer sinkApp = sinkHelper.Install (serverNode);
105  sinkApp.Start (Seconds (0.0));
106  sinkApp.Stop (Seconds (30.0));
107  fd.EnablePcap ("fd2fd-onoff-server", serverDevice);
108 
109  // client
110  AddressValue serverAddress (InetSocketAddress (serverIp, sinkPort));
111  OnOffHelper onoff ("ns3::TcpSocketFactory", Address ());
112  onoff.SetAttribute ("Remote", serverAddress);
113  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
114  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
115  onoff.SetAttribute ("DataRate", DataRateValue (dataRate));
116  onoff.SetAttribute ("PacketSize", UintegerValue (packetSize));
117  ApplicationContainer clientApps = onoff.Install (clientNode);
118  clientApps.Start (Seconds (2.0));
119  clientApps.Stop (Seconds (29.0));
120  fd.EnablePcap ("fd2fd-onoff-client", clientDevice);
121 
122  Simulator::Stop (Seconds (30.0));
123  Simulator::Run ();
125 }