A Discrete-Event Network Simulator
API
fd-planetlab-ping.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 
19 // Allow ns-3 to ping a real host somewhere, using emulation mode and ping
20 // the simulated node from the host.
21 //
22 //
23 // +-------------------------------------+
24 // | PlanetLab host |
25 // +-------------------------------------+
26 // | ns-3 simulation | |
27 // +----------------------+ |
28 // | ns-3 Node | |
29 // | +----------------+ | |
30 // | | ns-3 TCP | | |
31 // | +----------------+ | |
32 // | | ns-3 IPv4 | | |
33 // | +----------------+ | |
34 // | | FdNetDevice | | |
35 // |--+----------------+--+ +------+ |
36 // | | TAP | | eth0 | |
37 // | +------+ +------+ |
38 // | 1.2.3.4 | |
39 // +-------------------------------|-----+
40 // |
41 // |
42 // ------------ (Internet) -----
43 //
44 // To use this example:
45 // 1) A ns-3 creator binary will create the TAP device for you in the host machine.
46 // For this you need to provide the network address to allocate IP addresses
47 // for the TAP/TU device and the ns-3 FdNetDevice.
48 // 2) Once the experiment is running you can ping the FdNetDevice IP address from
49 // the host machine.
50 // 3) For more information on the required configuration to create TAP devices
51 // on PlanetLab refer to:
52 // http://minerva.netgroup.uniroma2.it/fairvpn/wiki/TapDevice
53 //
54 
55 #include "ns3/abort.h"
56 #include "ns3/core-module.h"
57 #include "ns3/internet-module.h"
58 #include "ns3/network-module.h"
59 #include "ns3/fd-net-device-module.h"
60 #include "ns3/internet-apps-module.h"
61 #include "ns3/ipv4-static-routing-helper.h"
62 #include "ns3/ipv4-list-routing-helper.h"
63 
64 using namespace ns3;
65 
66 NS_LOG_COMPONENT_DEFINE ("PlanetLabTAPPingExample");
67 
68 static void
69 PingRtt (std::string context, Time rtt)
70 {
71  NS_LOG_UNCOND ("Received Response with RTT = " << rtt);
72 }
73 
74 int
75 main (int argc, char *argv[])
76 {
77  NS_LOG_INFO ("Ping Emulation Example with TAP on PlanetLab host");
78 
79  std::string remote ("173.194.34.51"); // example.com
80  //
81  // Make sure network and mask correspond to those assigned to your PlanetLab
82  // slice, through the tag vsys_vnet x.x.x.x/yy .
83  // In this case The network should be x.x.x.x and the mask should correspond to
84  // the prefix yy.
85  //
86  std::string network ("1.2.3.4");
87  std::string mask ("255.255.255.0");
88 
89  //
90  // Allow the user to override any of the defaults at run-time, via
91  // command-line arguments
92  //
94  cmd.AddValue ("remote", "Remote IP address (dotted decimal only please)", remote);
95  cmd.AddValue ("tapNetwork", "Network address to assign the TAP device IP address (dotted decimal only please). Note that the network address must be that from the vsys_vnet tag which must exist in your PlanetLab slice.", network);
96  cmd.AddValue ("tapMask", "Network mask for configure the TAP device (dotted decimal only please)", mask);
97  cmd.Parse (argc, argv);
98 
99  NS_ABORT_MSG_IF (network == "1.2.3.4", "You must change the local IP address before running this example");
100 
101  Ipv4Address remoteIp (remote.c_str ());
102  Ipv4Address tapNetwork (network.c_str ());
103  Ipv4Mask tapMask (mask.c_str ());
104 
105  //
106  // Since we are using a real piece of hardware we need to use the realtime
107  // simulator.
108  //
109  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
110 
111  //
112  // Since we are going to be talking to real-world machines, we need to enable
113  // calculation of checksums in our protocols.
114  //
115  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
116 
117  //
118  // In such a simple topology, the use of the helper API can be a hindrance
119  // so we drop down into the low level API and do it manually.
120  //
121  // First we need a single node.
122  //
123  NS_LOG_INFO ("Create Node");
124  Ptr<Node> node = CreateObject<Node> ();
125 
126  // Create an fd device, set a MAC address and point the device to the
127  // Linux device name. The device needs a transmit queueing discipline so
128  // create a droptail queue and give it to the device. Finally, "install"
129  // the device into the node.
130  //
131  Ipv4AddressHelper addresses;
132  addresses.SetBase (tapNetwork, tapMask);
133  Ipv4Address tapIp = addresses.NewAddress ();
134 
135  NS_LOG_INFO ("Create Device");
137  helper.SetTapIpAddress (tapIp);
138  helper.SetTapMask (tapMask);
139 
140  NetDeviceContainer devices = helper.Install (node);
141  Ptr<NetDevice> device = devices.Get (0);
142 
143  //
144  // Add a default internet stack to the node (ARP, IPv4, ICMP, UDP and TCP).
145  //
146  NS_LOG_INFO ("Add Internet Stack");
147  InternetStackHelper internetStackHelper;
148  internetStackHelper.Install (node);
149 
150  //
151  // Add an address to the ns-3 device in the same network than one
152  // assigned to the TAP.
153  //
154  NS_LOG_INFO ("Create IPv4 Interface");
155  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
156  uint32_t interface = ipv4->AddInterface (device);
157  Ipv4Address devIp = addresses.NewAddress ();
159  ipv4->AddAddress (interface, address);
160  ipv4->SetMetric (interface, 1);
161  ipv4->SetUp (interface);
162 
163  //
164  // Add a route to the ns-3 device so it can reach the outside world though the
165  // TAP.
166  //
167  Ipv4StaticRoutingHelper ipv4RoutingHelper;
168  Ptr<Ipv4StaticRouting> staticRouting = ipv4RoutingHelper.GetStaticRouting (ipv4);
169  staticRouting->SetDefaultRoute (tapIp, interface);
170 
171  //
172  // Create the ping application. This application knows how to send
173  // ICMP echo requests. Setting up the packet sink manually is a bit
174  // of a hassle and since there is no law that says we cannot mix the
175  // helper API with the low level API, let's just use the helper.
176  //
177  NS_LOG_INFO ("Create V4Ping Appliation");
178  Ptr<V4Ping> app = CreateObject<V4Ping> ();
179  app->SetAttribute ("Remote", Ipv4AddressValue (remoteIp));
180  app->SetAttribute ("Verbose", BooleanValue (true) );
181  node->AddApplication (app);
182  app->SetStartTime (Seconds (1.0));
183  app->SetStopTime (Seconds (21.0));
184 
185  //
186  // Give the application a name. This makes life much easier when constructing
187  // config paths.
188  //
189  Names::Add ("app", app);
190 
191  //
192  // Hook a trace to print something when the response comes back.
193  //
194  Config::Connect ("/Names/app/Rtt", MakeCallback (&PingRtt));
195 
196  //
197  // Enable a promiscuous pcap trace to see what is coming and going on our device.
198  //
199  helper.EnablePcap ("fd-planete-ping", device, true);
200 
201  //
202  // Now, do the actual emulation.
203  //
204  NS_LOG_INFO ("Run Emulation.");
205  Simulator::Stop (Seconds (22.0));
206  Simulator::Run ();
208  NS_LOG_INFO ("Done.");
209 }
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:157
void SetStopTime(Time stop)
Specify application stop time.
Definition: application.cc:75
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
AttributeValue implementation for Boolean.
Definition: boolean.h:36
void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a default route to the static routing table.
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
cmd
Definition: second.py:35
void SetTapMask(Ipv4Mask mask)
Set the network mask for the TAP device.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:770
static void PingRtt(std::string context, Time rtt)
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
build a set of FdNetDevice objects attached to a virtual TAP network interface
virtual void SetUp(uint32_t interface)=0
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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...
Parse command-line arguments.
Definition: command-line.h:213
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
AttributeValue implementation for Ipv4Address.
Definition: ipv4-address.h:329
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
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
a class to store IPv4 address information on an interface
Helper class that adds ns3::Ipv4StaticRouting objects.
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
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetTapIpAddress(Ipv4Address address)
Set the device IPv4 address.
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
void SetStartTime(Time start)
Specify application start time.
Definition: application.cc:69
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.