A Discrete-Event Network Simulator
API
fd-emu-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 // | 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 
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.
an Inet address class
AttributeValue implementation for Boolean.
Definition: boolean.h:36
Hold variables of type string.
Definition: string.h:41
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
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.
virtual void SetUp(uint32_t interface)=0
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:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
void SetDeviceName(std::string deviceName)
Set the device name of this device.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
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:40
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...
a class to store IPv4 address information on an interface
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
AttributeValue implementation for Mac48Address.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
clientApps
Definition: first.py:54
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
static const uint32_t packetSize
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:32
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
build a set of FdNetDevice objects attached to a physical network interface