A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
fd-tap-ping6.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 University of Washington, 2012 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 */
6
7// Allow ns-3 to ping a TAP device in the host machine.
8//
9// -------------------------------------------------
10// | ns-3 simulation |
11// | |
12// | ------- -------- |
13// | | node | | node | |
14// | | (r) | | (n) | |
15// | | | | | |
16// | ------- -------- -------- |
17// | | fd- | csma- | | csma- | |
18// | | net- | net- | | net- | |
19// | | device| device | | device | |
20// | ------- -------- -------- |
21// | | |____csma channel_____| |
22// | | |
23// ----|------------------------------------------
24// | --- |
25// | | | |
26// | |TAP| |
27// | | | |
28// | --- |
29// | |
30// | host |
31// ------------------
32//
33//
34
35#include "ns3/core-module.h"
36#include "ns3/csma-module.h"
37#include "ns3/fd-net-device-module.h"
38#include "ns3/internet-apps-module.h"
39#include "ns3/internet-module.h"
40
41#include <sstream>
42#include <string>
43
44using namespace ns3;
45
46NS_LOG_COMPONENT_DEFINE("TAPPing6Example");
47
48int
49main(int argc, char* argv[])
50{
52 cmd.Parse(argc, argv);
53
54 NS_LOG_INFO("Ping6 Emulation Example with TAP");
55
56 //
57 // Since we are using a real piece of hardware we need to use the realtime
58 // simulator.
59 //
60 GlobalValue::Bind("SimulatorImplementationType", StringValue("ns3::RealtimeSimulatorImpl"));
61
62 //
63 // Since we are going to be talking to real-world machines, we need to enable
64 // calculation of checksums in our protocols.
65 //
66 GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
67
68 //
69 // Create the two nodes.
70 //
73 NodeContainer net(n, r);
74
75 //
76 // Install IPv6 stack.
77 //
80
81 //
82 // Create CSMA channel.
83 //
85 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
86 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
88
89 //
90 // Assign IPv6 addresses.
91 //
93
94 ipv6.SetBase(Ipv6Address("4001:beef:1::"), Ipv6Prefix(64));
96 i1.SetForwarding(1, true);
97 i1.SetDefaultRouteInAllNodes(1);
98
99 ipv6.SetBase(Ipv6Address("4001:beef:2::"), Ipv6Prefix(64));
101 std::stringstream ss;
102 std::string tapIp;
104 ss >> tapIp;
105
106 //
107 // Create FdNetDevice.
108 //
110 helper.SetDeviceName("tap0");
111 helper.SetTapIpv6Address(tapIp.c_str());
112 helper.SetTapIpv6Prefix(64);
113
114 NetDeviceContainer fdevs = helper.Install(r);
115 Ptr<NetDevice> device = fdevs.Get(0);
116 Ptr<FdNetDevice> fdevice = device->GetObject<FdNetDevice>();
117 fdevice->SetIsMulticast(true);
119 i2.SetForwarding(0, true);
120 i2.SetDefaultRouteInAllNodes(0);
121
122 //
123 // Create the Ping6 application.
124 //
125 uint32_t packetSize = 1024;
127 Time interPacketInterval = Seconds(1);
128
129 PingHelper ping(Ipv6Address(tapIp.c_str()));
130 ping.SetAttribute("Count", UintegerValue(maxPacketCount));
131 ping.SetAttribute("Interval", TimeValue(interPacketInterval));
132 ping.SetAttribute("Size", UintegerValue(packetSize));
133 ApplicationContainer apps = ping.Install(n);
134
135 // Ping6Helper ping6;
136
137 // ping6.SetRemote(tapIp.c_str());
138
139 // ping6.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
140 // ping6.SetAttribute("Interval", TimeValue(interPacketInterval));
141 // ping6.SetAttribute("PacketSize", UintegerValue(packetSize));
142 // ApplicationContainer apps = ping6.Install(n);
143 apps.Start(Seconds(2));
144 apps.Stop(Seconds(20));
145
147 csma.EnableAsciiAll(ascii.CreateFileStream("csma-ping6.tr"));
148 csma.EnablePcapAll("csma-ping6", true);
149
150 //
151 // Enable a promiscuous pcap trace to see what is coming and going on in the fd-net-device.
152 //
153 helper.EnablePcap("fd-ping6", fdevice, true);
154
155 //
156 // Run the experiment.
157 //
158 NS_LOG_INFO("Run Emulation.");
162 NS_LOG_INFO("Done.");
163
164 return 0;
165}
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.
Manage ASCII trace files for device models.
AttributeValue implementation for Boolean.
Definition boolean.h:26
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
AttributeValue implementation for DataRate.
Definition data-rate.h:285
void SetDeviceName(std::string deviceName)
Set the device name of this device.
a NetDevice to read/write network traffic from/into a file descriptor.
virtual void SetIsMulticast(bool multicast)
Set if the NetDevice is able to send Multicast messages.
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.
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.
Describes an IPv6 prefix.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:66
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
build a set of FdNetDevice objects attached to a virtual TAP network interface
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
AttributeValue implementation for Time.
Definition nstime.h:1431
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1356
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t packetSize
Packet size generated at the AP.