A Discrete-Event Network Simulator
API
fd-tap-ping.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 University of Washington, 2012 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 // Allow ns-3 to ping a real host somewhere, using emulation mode and ping
19 // the simulated node from the host.
20 //
21 // +-------------------------------------+
22 // | host |
23 // +-------------------------------------+
24 // | ns-3 simulation | |
25 // +----------------------+ |
26 // | ns-3 Node | |
27 // | +----------------+ | |
28 // | | ns-3 TCP | | |
29 // | +----------------+ | |
30 // | | ns-3 IPv4 | | |
31 // | +----------------+ | |
32 // | | FdNetDevice | | |
33 // |--+----------------+--+ +------+ |
34 // | | TAP | | eth0 | |
35 // | +------+ +------+ |
36 // | 1.2.3.4 | |
37 // +-------------------------------|-----+
38 // |
39 // | +-------------+
40 // ------------ (Internet) ----- | Remote host |
41 // +-------------+
42 //
43 // To use this example:
44 // 1) ns-3 will create the TAP device for you in the host machine.
45 // For this you need to provide the network address to allocate IP addresses
46 // for the TAP device and the ns-3 FdNetDevice.
47 //
48 // 2) Take into consideration that this experiment requires the host to be able
49 // to forward traffic generated by the simulation to the Internet.
50 // So for Linux systems, make sure to configure:
51 // # echo 1 > /proc/sys/net/ipv4/ip_forward
52 //
53 // Also enable natting so the ICMP replies from the remote host can reach
54 // back the TAP.
55 // - TAP-network-address is the same as 'tapNetwork'
56 // - TAP-network-mask is the same as 'tapMask'
57 // # iptables -t nat -A POSTROUTING -s <TAP-network-address>/<TAP-network-mask> -j MASQUERADE
58 //
59 // 3) Before running the example make sure that the tap creator binary has root suid.
60 // If the --enable-sudo option was used to configure ns-3 with ns3, then the following
61 // step will not be necessary.
62 //
63 // # chown root.root build/src/fd-net-device/ns3-dev-tap-device-creator
64 // # sudo chmod 4755 build/src/fd-net-device/ns3-dev-tap-device-creator
65 //
66 // 4) The example can be executed as follows using ns3:
67 //
68 // ./ns3 run fd-tap-ping --command-template="%s --tapNetwork=<TAP-network-address>
69 // --tapMask=<TAP-network-mask>"
70 //
71 
72 #include "ns3/abort.h"
73 #include "ns3/core-module.h"
74 #include "ns3/fd-net-device-module.h"
75 #include "ns3/internet-apps-module.h"
76 #include "ns3/internet-module.h"
77 #include "ns3/ipv4-list-routing-helper.h"
78 #include "ns3/ipv4-static-routing-helper.h"
79 #include "ns3/network-module.h"
80 
81 using namespace ns3;
82 
83 NS_LOG_COMPONENT_DEFINE("TAPPingExample");
84 
85 static void
86 PingRtt(std::string context, uint16_t seqNo, Time rtt)
87 {
88  NS_LOG_UNCOND("Received " << seqNo << " Response with RTT = " << rtt);
89 }
90 
91 int
92 main(int argc, char* argv[])
93 {
94  NS_LOG_INFO("Ping Emulation Example with TAP");
95 
96  std::string deviceName("tap0");
97  std::string remote("192.0.43.10"); // example.com
98  std::string network("1.2.3.4");
99  std::string mask("255.255.255.0");
100  std::string pi("no");
101 
102  //
103  // Allow the user to override any of the defaults at run-time, via
104  // command-line arguments
105  //
106  CommandLine cmd(__FILE__);
107  cmd.AddValue("deviceName", "Device name", deviceName);
108  cmd.AddValue("remote", "Remote IP address (dotted decimal only please)", remote);
109  cmd.AddValue("tapNetwork",
110  "Network address to assign the TAP device IP address (dotted decimal only please)",
111  network);
112  cmd.AddValue("tapMask",
113  "Network mask for configure the TAP device (dotted decimal only please)",
114  mask);
115  cmd.AddValue("modePi",
116  "If 'yes' a PI header will be added to the traffic traversing the device(flag "
117  "IFF_NOPI will be unset).",
118  pi);
119  cmd.Parse(argc, argv);
120 
121  NS_ABORT_MSG_IF(network == "1.2.3.4",
122  "You must change the local IP address before running this example");
123 
124  Ipv4Address remoteIp(remote.c_str());
125  Ipv4Address tapNetwork(network.c_str());
126  Ipv4Mask tapMask(mask.c_str());
127 
128  bool modePi = (pi == "yes");
129 
130  //
131  // Since we are using a real piece of hardware we need to use the realtime
132  // simulator.
133  //
134  GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
135 
136  //
137  // Since we are going to be talking to real-world machines, we need to enable
138  // calculation of checksums in our protocols.
139  //
140  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
141 
142  //
143  // In such a simple topology, the use of the helper API can be a hindrance
144  // so we drop down into the low level API and do it manually.
145  //
146  // First we need a single node.
147  //
148  NS_LOG_INFO("Create Node");
149  Ptr<Node> node = CreateObject<Node>();
150 
151  // Create an fd device, set a MAC address and point the device to the
152  // Linux device name. The device needs a transmit queueing discipline so
153  // create a droptail queue and give it to the device. Finally, "install"
154  // the device into the node.
155  //
156  Ipv4AddressHelper addresses;
157  addresses.SetBase(tapNetwork, tapMask);
158  Ipv4Address tapIp = addresses.NewAddress();
159 
160  NS_LOG_INFO("Create Device");
161  TapFdNetDeviceHelper helper;
162  helper.SetDeviceName(deviceName);
163  helper.SetModePi(modePi);
164  helper.SetTapIpv4Address(tapIp);
165  helper.SetTapIpv4Mask(tapMask);
166 
167  NetDeviceContainer devices = helper.Install(node);
168  Ptr<NetDevice> device = devices.Get(0);
169 
170  //
171  // Add a default internet stack to the node (ARP, IPv4, ICMP, UDP and TCP).
172  //
173  NS_LOG_INFO("Add Internet Stack");
174  InternetStackHelper internetStackHelper;
175  internetStackHelper.Install(node);
176 
177  //
178  // Add an address to the ns-3 device in the same network than one
179  // assigned to the TAP.
180  //
181  NS_LOG_INFO("Create IPv4 Interface");
182  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
183  uint32_t interface = ipv4->AddInterface(device);
184  Ipv4Address devIp = addresses.NewAddress();
186  ipv4->AddAddress(interface, address);
187  ipv4->SetMetric(interface, 1);
188  ipv4->SetUp(interface);
189 
190  //
191  // Add a route to the ns-3 device so it can reach the outside world though the
192  // TAP.
193  //
194  Ipv4StaticRoutingHelper ipv4RoutingHelper;
195  Ptr<Ipv4StaticRouting> staticRouting = ipv4RoutingHelper.GetStaticRouting(ipv4);
196  staticRouting->SetDefaultRoute(tapIp, interface);
197 
198  //
199  // Create the ping application. This application knows how to send
200  // ICMP echo requests. Setting up the packet sink manually is a bit
201  // of a hassle and since there is no law that says we cannot mix the
202  // helper API with the low level API, let's just use the helper.
203  //
204  NS_LOG_INFO("Create Ping Application");
205  Ptr<Ping> app = CreateObject<Ping>();
206  app->SetAttribute("Destination", AddressValue(remoteIp));
207  app->SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
208  node->AddApplication(app);
209  app->SetStartTime(Seconds(1.0));
210  app->SetStopTime(Seconds(21.0));
211 
212  //
213  // Give the application a name. This makes life much easier when constructing
214  // config paths.
215  //
216  Names::Add("app", app);
217 
218  //
219  // Hook a trace to print something when the response comes back.
220  //
221  Config::Connect("/Names/app/Rtt", MakeCallback(&PingRtt));
222 
223  //
224  // Enable a promiscuous pcap trace to see what is coming and going on our device.
225  //
226  helper.EnablePcap("fd-tap-ping", device, true);
227 
228  //
229  // Now, do the actual emulation.
230  //
231  NS_LOG_INFO("Run Emulation.");
232  Simulator::Stop(Seconds(25.0));
233  Simulator::Run();
235  NS_LOG_INFO("Done.");
236 
237  return 0;
238 }
Parse command-line arguments.
Definition: command-line.h:232
void SetDeviceName(std::string deviceName)
Set the device name of this device.
Hold variables of type enum.
Definition: enum.h:62
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
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...
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4Address NewAddress()
Increment the IP address counter used to allocate IP addresses.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
Helper class that adds ns3::Ipv4StaticRouting objects.
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...
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:775
holds a vector of ns3::NetDevice pointers
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
Hold variables of type string.
Definition: string.h:56
build a set of FdNetDevice objects attached to a virtual TAP network interface
void SetTapIpv4Mask(Ipv4Mask mask)
Set the IPv4 network mask for the TAP device.
void SetTapIpv4Address(Ipv4Address address)
Set the device IPv4 address.
void SetModePi(bool pi)
Set flag IFF_NO_PI on the device.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
static void PingRtt(std::string context, uint16_t seqNo, Time rtt)
Definition: fd-tap-ping.cc:86
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:974
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
address
Definition: first.py:47
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
cmd
Definition: second.py:40