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/applications-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  //
93  CsmaHelper csma;
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.SetRouter (1, true);
106 
107  ipv6.SetBase (Ipv6Address ("2001:0DB8:2::"), Ipv6Prefix (64));
108  Ipv6Address tapAddr = ipv6.NewAddress ();
109  std::stringstream ss;
110  std::string tapIp;
111  tapAddr.Print (ss);
112  ss >> tapIp;
113 
114  //
115  // Create FdNetDevice.
116  //
117  TapFdNetDeviceHelper helper;
118  helper.SetDeviceName ("tap0");
119  helper.SetTapIpv6Address (tapIp.c_str ());
120  helper.SetTapIpv6Prefix (64);
121 
122  NetDeviceContainer fdevs = helper.Install (r);
123  Ptr<NetDevice> device = fdevs.Get (0);
124  Ptr<FdNetDevice> fdevice = device->GetObject<FdNetDevice> ();
125  fdevice-> SetIsMulticast (true);
126  Ipv6InterfaceContainer i2 = ipv6.Assign (fdevs);
127  i2.SetRouter (0, true);
128 
129  //
130  // Create the Ping6 application.
131  //
132  uint32_t packetSize = 1024;
133  uint32_t maxPacketCount = 1;
134  Time interPacketInterval = Seconds (1.0);
135 
136  Ping6Helper ping6;
137 
138  ping6.SetRemote (tapIp.c_str ());
139 
140  ping6.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
141  ping6.SetAttribute ("Interval", TimeValue (interPacketInterval));
142  ping6.SetAttribute ("PacketSize", UintegerValue (packetSize));
143  ApplicationContainer apps = ping6.Install (n);
144  apps.Start (Seconds (2.0));
145  apps.Stop (Seconds (20.0));
146 
147  AsciiTraceHelper ascii;
148  csma.EnableAsciiAll (ascii.CreateFileStream ("csma-ping6.tr"));
149  csma.EnablePcapAll ("csma-ping6", true);
150 
151  //
152  // Enable a promiscuous pcap trace to see what is coming and going on in the fd-net-device.
153  //
154  helper.EnablePcap ("fd-ping6", fdevice, true);
155 
156  //
157  // Run the experiment.
158  //
159  NS_LOG_INFO ("Run Emulation.");
160  Simulator::Stop (Seconds (200.0));
161  Simulator::Run ();
163  NS_LOG_INFO ("Done.");
164 }