A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
63  NS_LOG_INFO ("Ping6 Emulation Example with TAP");
64 
65  //
66  // Since we are using a real piece of hardware we need to use the realtime
67  // simulator.
68  //
69  GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
70 
71  //
72  // Since we are going to be talking to real-world machines, we need to enable
73  // calculation of checksums in our protocols.
74  //
75  GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
76 
77  //
78  // Create the two nodes.
79  //
80  Ptr<Node> n = CreateObject<Node> ();
81  Ptr<Node> r = CreateObject<Node> ();
82  NodeContainer net (n, r);
83 
84  //
85  // Install IPv6 stack.
86  //
87  InternetStackHelper internetv6;
88  internetv6.Install (net);
89 
90  //
91  // Create CSMA channel.
92  //
94  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
95  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
96  NetDeviceContainer devs = csma.Install (net);
97 
98  //
99  // Assign IPv6 addresses.
100  //
101  Ipv6AddressHelper ipv6;
102 
103  ipv6.SetBase (Ipv6Address ("2001:0DB8:1::"), Ipv6Prefix (64));
104  Ipv6InterfaceContainer i1 = ipv6.Assign (devs);
105  i1.SetForwarding (1, true);
107 
108  ipv6.SetBase (Ipv6Address ("2001:0DB8:2::"), Ipv6Prefix (64));
109  Ipv6Address tapAddr = ipv6.NewAddress ();
110  std::stringstream ss;
111  std::string tapIp;
112  tapAddr.Print (ss);
113  ss >> tapIp;
114 
115  //
116  // Create FdNetDevice.
117  //
118  TapFdNetDeviceHelper helper;
119  helper.SetDeviceName ("tap0");
120  helper.SetTapIpv6Address (tapIp.c_str ());
121  helper.SetTapIpv6Prefix (64);
122 
123  NetDeviceContainer fdevs = helper.Install (r);
124  Ptr<NetDevice> device = fdevs.Get (0);
125  Ptr<FdNetDevice> fdevice = device->GetObject<FdNetDevice> ();
126  fdevice-> SetIsMulticast (true);
127  Ipv6InterfaceContainer i2 = ipv6.Assign (fdevs);
128  i2.SetForwarding (0, true);
130 
131  //
132  // Create the Ping6 application.
133  //
134  uint32_t packetSize = 1024;
135  uint32_t maxPacketCount = 1;
136  Time interPacketInterval = Seconds (1.0);
137 
138  Ping6Helper ping6;
139 
140  ping6.SetRemote (tapIp.c_str ());
141 
142  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
143  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
144  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
145  ApplicationContainer apps = ping6.Install (n);
146  apps.Start (Seconds (2.0));
147  apps.Stop (Seconds (20.0));
148 
149  AsciiTraceHelper ascii;
150  csma.EnableAsciiAll (ascii.CreateFileStream ("csma-ping6.tr"));
151  csma.EnablePcapAll ("csma-ping6", true);
152 
153  //
154  // Enable a promiscuous pcap trace to see what is coming and going on in the fd-net-device.
155  //
156  helper.EnablePcap ("fd-ping6", fdevice, true);
157 
158  //
159  // Run the experiment.
160  //
161  NS_LOG_INFO ("Run Emulation.");
162  Simulator::Stop (Seconds (200.0));
163  Simulator::Run ();
165  NS_LOG_INFO ("Done.");
166 }
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
Manage ASCII trace files for device models.
Definition: trace-helper.h:156
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
AttributeValue implementation for Boolean.
Definition: boolean.h:34
Keep track of a set of IPv6 interfaces.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
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).
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
ApplicationContainer Install(NodeContainer c)
Install the application in Nodes.
Definition: ping6-helper.cc:50
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
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.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
void SetAttribute(std::string name, const AttributeValue &value)
Set some attributes.
Definition: ping6-helper.cc:45
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'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.
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
AttributeValue implementation for Time.
Definition: nstime.h:957
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
void Print(std::ostream &os) const
Print this address to the given output stream.
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...
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
void SetDeviceName(std::string deviceName)
Set the device name of this device.
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
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.
Describes an IPv6 address.
Definition: ipv6-address.h:48
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.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
Ipv6Address NewAddress(Address addr)
Allocate a new Ipv6Address.
Describes an IPv6 prefix.
Definition: ipv6-address.h:393
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
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:39
tuple csma
Definition: second.py:63