A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fd-emu-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 // | client host | | server host |
24 // +----------------------+ +-----------------------+
25 // | ns-3 Node 0 | | ns-3 Node 1 |
26 // | +----------------+ | | +----------------+ |
27 // | | ns-3 TCP | | | | ns-3 TCP | |
28 // | +----------------+ | | +----------------+ |
29 // | | ns-3 IPv4 | | | | ns-3 IPv4 | |
30 // | +----------------+ | | +----------------+ |
31 // | | FdNetDevice | | | | FdNetDevice | |
32 // | | 10.1.1.1 | | | | 10.1.1.2 | |
33 // | +----------------+ | | +----------------+ |
34 // | | raw socket | | | | raw socket | |
35 // | +----------------+ | | +----------------+ |
36 // | | eth0 | | | | eth0 | |
37 // +-------+------+-------+ +--------+------+-------+
38 //
39 // 10.1.1.11 10.1.1.12
40 //
41 // | |
42 // +----------------------------+
43 //
44 // This example is aimed at measuring the throughput of the FdNetDevice
45 // when using the EmuFdNetDeviceHelper. This is achieved by saturating
46 // the channel with TCP traffic. Then the throughput can be obtained from
47 // the generated .pcap files.
48 //
49 // To run this example you will need two hosts (client & server).
50 // Steps to run the experiment:
51 //
52 // 1 - Connect the 2 computers with an Ethernet cable.
53 // 2 - Set the IP addresses on both Ethernet devices.
54 //
55 // client machine: $ sudo ip addr add dev eth0 10.1.1.11/24
56 // server machine: $ sudo ip addr add dev eth0 10.1.1.12/24
57 //
58 // 3 - Set both Ethernet devices to promiscuous mode.
59 //
60 // both machines: $ sudo ip link set eth0 promisc on
61 //
62 // 4 - Give root suid to the raw socket creator binary.
63 // If the --enable-sudo option was used to configure ns-3 with waf, then the following
64 // step will not be necessary.
65 //
66 // both hosts: $ sudo chown root.root build/src/fd-net-device/ns3-dev-raw-sock-creator
67 // both hosts: $ sudo chmod 4755 build/src/fd-net-device/ns3-dev-raw-sock-creator
68 //
69 // 5 - Run the server side:
70 //
71 // server host: $ ./waf --run="fd-emu-onoff --serverMode=1"
72 //
73 // 6 - Run the client side:
74 //
75 // client host: $ ./waf --run="fd-emu-onoff"
76 //
77 
78 #include <iostream>
79 #include <fstream>
80 #include <vector>
81 #include <string>
82 
83 #include "ns3/core-module.h"
84 #include "ns3/network-module.h"
85 #include "ns3/internet-module.h"
86 #include "ns3/applications-module.h"
87 #include "ns3/config-store-module.h"
88 #include "ns3/fd-net-device-module.h"
89 
90 using namespace ns3;
91 
92 NS_LOG_COMPONENT_DEFINE ("EmuFdNetDeviceSaturationExample");
93 
94 int
95 main (int argc, char *argv[])
96 {
97  uint16_t sinkPort = 8000;
98  uint32_t packetSize = 10000; // bytes
99  std::string dataRate("1000Mb/s");
100  bool serverMode = false;
101 
102  std::string deviceName ("eth0");
103  std::string client ("10.1.1.1");
104  std::string server ("10.1.1.2");
105  std::string netmask ("255.255.255.0");
106  std::string macClient ("00:00:00:00:00:01");
107  std::string macServer ("00:00:00:00:00:02");
108 
109  CommandLine cmd;
110  cmd.AddValue ("deviceName", "Device name", deviceName);
111  cmd.AddValue ("client", "Local IP address (dotted decimal only please)", client);
112  cmd.AddValue ("server", "Remote IP address (dotted decimal only please)", server);
113  cmd.AddValue ("localmask", "Local mask address (dotted decimal only please)", netmask);
114  cmd.AddValue ("serverMode", "1:true, 0:false, default client", serverMode);
115  cmd.AddValue ("mac-client", "Mac Address for Server Client : 00:00:00:00:00:01", macClient);
116  cmd.AddValue ("mac-server", "Mac Address for Server Default : 00:00:00:00:00:02", macServer);
117  cmd.AddValue ("data-rate", "Data rate defaults to 1000Mb/s", dataRate);
118  cmd.Parse (argc, argv);
119 
120  Ipv4Address remoteIp;
121  Ipv4Address localIp;
122  Mac48AddressValue localMac;
123 
124  if (serverMode)
125  {
126  remoteIp = Ipv4Address (client.c_str ());
127  localIp = Ipv4Address (server.c_str ());
128  localMac = Mac48AddressValue (macServer.c_str ());
129  }
130  else
131  {
132  remoteIp = Ipv4Address (server.c_str ());
133  localIp = Ipv4Address (client.c_str ());
134  localMac = Mac48AddressValue (macClient.c_str ());
135  }
136 
137  Ipv4Mask localMask (netmask.c_str ());
138 
139  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
140 
141  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
142 
143  NS_LOG_INFO ("Create Node");
144  Ptr<Node> node = CreateObject<Node> ();
145 
146  NS_LOG_INFO ("Create Device");
148  emu.SetDeviceName (deviceName);
149  NetDeviceContainer devices = emu.Install (node);
150  Ptr<NetDevice> device = devices.Get (0);
151  device->SetAttribute ("Address", localMac);
152 
153  NS_LOG_INFO ("Add Internet Stack");
154  InternetStackHelper internetStackHelper;
155  internetStackHelper.SetIpv4StackInstall(true);
156  internetStackHelper.Install (node);
157 
158  NS_LOG_INFO ("Create IPv4 Interface");
159  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
160  uint32_t interface = ipv4->AddInterface (device);
161  Ipv4InterfaceAddress address = Ipv4InterfaceAddress (localIp, localMask);
162  ipv4->AddAddress (interface, address);
163  ipv4->SetMetric (interface, 1);
164  ipv4->SetUp (interface);
165 
166  if(serverMode)
167  {
168  Address sinkLocalAddress (InetSocketAddress (localIp, sinkPort));
169  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
170  ApplicationContainer sinkApp = sinkHelper.Install (node);
171  sinkApp.Start (Seconds (1.0));
172  sinkApp.Stop (Seconds (60.0));
173 
174  emu.EnablePcap ("fd-server", device);
175  }
176  else
177  {
178  AddressValue remoteAddress (InetSocketAddress (remoteIp, sinkPort));
179  OnOffHelper onoff ("ns3::TcpSocketFactory", Address ());
180  onoff.SetAttribute ("Remote", remoteAddress);
181  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
182  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
183  onoff.SetAttribute ("DataRate", DataRateValue (dataRate));
184  onoff.SetAttribute ("PacketSize", UintegerValue (packetSize));
185 
186  ApplicationContainer clientApps = onoff.Install (node);
187  clientApps.Start (Seconds (4.0));
188  clientApps.Stop (Seconds (58.0));
189 
190  emu.EnablePcap ("fd-client", device);
191  }
192 
193  Simulator::Stop (Seconds (61.0));
194  Simulator::Run ();
196 
197  return 0;
198 }
199 
holds a vector of ns3::Application pointers.
virtual NetDeviceContainer Install(Ptr< Node > node) const
an Inet address class
Hold a bool native type.
Definition: boolean.h:38
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.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
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.
#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
a polymophic address class
Definition: address.h:86
virtual void SetUp(uint32_t interface)=0
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...
parse command-line argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
static void Destroy(void)
Definition: simulator.cc:121
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
void SetDeviceName(std::string deviceName)
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
a class to store IPv4 address information on an interface
hold objects of type ns3::DataRate
hold objects of type ns3::Mac48Address
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:134
static void Stop(void)
Definition: simulator.cc:164
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:160
Ptr< T > GetObject(void) const
Definition: object.h:332
build a set of FdNetDevice objects attached to a physical network interface