A Discrete-Event Network Simulator
API
fd-tap-ping6.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 TAP device in the host machine.
19 //
20 // -------------------------------------------------
21 // | ns-3 simulation |
22 // | |
23 // | ------- -------- |
24 // | | node | | node | |
25 // | | (r) | | (n) | |
26 // | | | | | |
27 // | ------- -------- -------- |
28 // | | fd- | csma- | | csma- | |
29 // | | net- | net- | | net- | |
30 // | | device| device | | device | |
31 // | ------- -------- -------- |
32 // | | |____csma channel_____| |
33 // | | |
34 // ----|------------------------------------------
35 // | --- |
36 // | | | |
37 // | |TAP| |
38 // | | | |
39 // | --- |
40 // | |
41 // | host |
42 // ------------------
43 //
44 //
45 
46 #include "ns3/core-module.h"
47 #include "ns3/csma-module.h"
48 #include "ns3/fd-net-device-module.h"
49 #include "ns3/internet-apps-module.h"
50 #include "ns3/internet-module.h"
51 
52 #include <sstream>
53 #include <string>
54 
55 using namespace ns3;
56 
57 NS_LOG_COMPONENT_DEFINE("TAPPing6Example");
58 
59 int
60 main(int argc, char* argv[])
61 {
62  CommandLine cmd(__FILE__);
63  cmd.Parse(argc, argv);
64 
65  NS_LOG_INFO("Ping6 Emulation Example with TAP");
66 
67  //
68  // Since we are using a real piece of hardware we need to use the realtime
69  // simulator.
70  //
71  GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
72 
73  //
74  // Since we are going to be talking to real-world machines, we need to enable
75  // calculation of checksums in our protocols.
76  //
77  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
78 
79  //
80  // Create the two nodes.
81  //
82  Ptr<Node> n = CreateObject<Node>();
83  Ptr<Node> r = CreateObject<Node>();
84  NodeContainer net(n, r);
85 
86  //
87  // Install IPv6 stack.
88  //
89  InternetStackHelper internetv6;
90  internetv6.Install(net);
91 
92  //
93  // Create CSMA channel.
94  //
96  csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
97  csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
98  NetDeviceContainer devs = csma.Install(net);
99 
100  //
101  // Assign IPv6 addresses.
102  //
103  Ipv6AddressHelper ipv6;
104 
105  ipv6.SetBase(Ipv6Address("4001:beef:1::"), Ipv6Prefix(64));
106  Ipv6InterfaceContainer i1 = ipv6.Assign(devs);
107  i1.SetForwarding(1, true);
109 
110  ipv6.SetBase(Ipv6Address("4001:beef:2::"), Ipv6Prefix(64));
111  Ipv6Address tapAddr = ipv6.NewAddress();
112  std::stringstream ss;
113  std::string tapIp;
114  tapAddr.Print(ss);
115  ss >> tapIp;
116 
117  //
118  // Create FdNetDevice.
119  //
120  TapFdNetDeviceHelper helper;
121  helper.SetDeviceName("tap0");
122  helper.SetTapIpv6Address(tapIp.c_str());
123  helper.SetTapIpv6Prefix(64);
124 
125  NetDeviceContainer fdevs = helper.Install(r);
126  Ptr<NetDevice> device = fdevs.Get(0);
127  Ptr<FdNetDevice> fdevice = device->GetObject<FdNetDevice>();
128  fdevice->SetIsMulticast(true);
129  Ipv6InterfaceContainer i2 = ipv6.Assign(fdevs);
130  i2.SetForwarding(0, true);
132 
133  //
134  // Create the Ping6 application.
135  //
136  uint32_t packetSize = 1024;
137  uint32_t maxPacketCount = 1;
138  Time interPacketInterval = Seconds(1.0);
139 
140  PingHelper ping(Ipv6Address(tapIp.c_str()));
141  ping.SetAttribute("Count", UintegerValue(maxPacketCount));
142  ping.SetAttribute("Interval", TimeValue(interPacketInterval));
143  ping.SetAttribute("Size", UintegerValue(packetSize));
144  ApplicationContainer apps = ping.Install(n);
145 
146  // Ping6Helper ping6;
147 
148  // ping6.SetRemote(tapIp.c_str());
149 
150  // ping6.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
151  // ping6.SetAttribute("Interval", TimeValue(interPacketInterval));
152  // ping6.SetAttribute("PacketSize", UintegerValue(packetSize));
153  // ApplicationContainer apps = ping6.Install(n);
154  apps.Start(Seconds(2.0));
155  apps.Stop(Seconds(20.0));
156 
157  AsciiTraceHelper ascii;
158  csma.EnableAsciiAll(ascii.CreateFileStream("csma-ping6.tr"));
159  csma.EnablePcapAll("csma-ping6", true);
160 
161  //
162  // Enable a promiscuous pcap trace to see what is coming and going on in the fd-net-device.
163  //
164  helper.EnablePcap("fd-ping6", fdevice, true);
165 
166  //
167  // Run the experiment.
168  //
169  NS_LOG_INFO("Run Emulation.");
170  Simulator::Stop(Seconds(200.0));
171  Simulator::Run();
173  NS_LOG_INFO("Done.");
174 
175  return 0;
176 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
void SetDeviceName(std::string deviceName)
Set the device name of this device.
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:84
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...
Helper class to auto-assign global IPv6 unicast addresses.
Ipv6Address NewAddress(Address addr)
Allocate a new Ipv6Address.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
void Print(std::ostream &os) const
Print this address to the given output stream.
Keep track of a set of IPv6 interfaces.
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Create a ping application and associate it to a node.
Definition: ping-helper.h:48
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 SetTapIpv6Prefix(int prefix)
Set the IPv6 network mask for the TAP device.
void SetTapIpv6Address(Ipv6Address address)
Set the device IPv6 address.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Hold an unsigned integer type.
Definition: uinteger.h:45
#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
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.
csma
Definition: second.py:63
cmd
Definition: second.py:40
static const uint32_t packetSize
Packet size generated at the AP.