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{
51 CommandLine cmd(__FILE__);
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 //
78 InternetStackHelper internetv6;
79 internetv6.Install(net);
80
81 //
82 // Create CSMA channel.
83 //
85 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
86 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
87 NetDeviceContainer devs = csma.Install(net);
88
89 //
90 // Assign IPv6 addresses.
91 //
93
94 ipv6.SetBase(Ipv6Address("4001:beef:1::"), Ipv6Prefix(64));
95 Ipv6InterfaceContainer i1 = ipv6.Assign(devs);
96 i1.SetForwarding(1, true);
98
99 ipv6.SetBase(Ipv6Address("4001:beef:2::"), Ipv6Prefix(64));
100 Ipv6Address tapAddr = ipv6.NewAddress();
101 std::stringstream ss;
102 std::string tapIp;
103 tapAddr.Print(ss);
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);
118 Ipv6InterfaceContainer i2 = ipv6.Assign(fdevs);
119 i2.SetForwarding(0, true);
121
122 //
123 // Create the Ping6 application.
124 //
125 uint32_t packetSize = 1024;
126 uint32_t maxPacketCount = 1;
127 Time interPacketInterval = Seconds(1.0);
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.0));
144 apps.Stop(Seconds(20.0));
145
146 AsciiTraceHelper ascii;
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.");
159 Simulator::Stop(Seconds(200.0));
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.
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.
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.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
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.
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.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
Describes an IPv6 prefix.
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:31
Smart pointer class similar to boost::intrusive_ptr.
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
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:94
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 > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint32_t packetSize
Packet size generated at the AP.