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