A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
virtual-net-device-example.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Based on simple-global-routing.cc
5 * The Tunnel class adds two tunnels, n0<=>n3 and n1<=>n3
6 */
7
8// Network topology
9//
10// n0
11// \ 5 Mb/s, 2ms
12// \ 1.5Mb/s, 10ms
13// n2 -------------------------n3
14// /
15// / 5 Mb/s, 2ms
16// n1
17//
18// - all links are point-to-point links with indicated one-way BW/delay
19// - Tracing of queues and packet receptions to file "virtual-net-device.tr"
20
21// Tunneling changes (relative to the simple-global-routing example):
22// - n0 will receive an extra virtual interface with hardcoded inner-tunnel address 11.0.0.1
23// - n1 will also receive an extra virtual interface with the same inner-tunnel address 11.0.0.1
24// - n3 will receive an extra virtual interface with inner-tunnel address 11.0.0.254
25// - The flows will be between 11.0.0.x (inner-tunnel) addresses instead of 10.1.x.y ones
26// - n3 will decide, on a per-packet basis, via random number, whether to
27// send the packet to n0 or to n1.
28//
29// Note: here we create a tunnel where IP packets are tunneled over
30// UDP/IP, but tunneling directly IP-over-IP would also be possible;
31// see src/node/ipv4-raw-socket-factory.h.
32
33#include "ns3/applications-module.h"
34#include "ns3/core-module.h"
35#include "ns3/iana-ieee802-numbers.h"
36#include "ns3/internet-module.h"
37#include "ns3/network-module.h"
38#include "ns3/point-to-point-module.h"
39#include "ns3/virtual-net-device.h"
40
41#include <cassert>
42#include <fstream>
43#include <iostream>
44#include <string>
45
46using namespace ns3;
47
48NS_LOG_COMPONENT_DEFINE("VirtualNetDeviceExample");
49
50/**
51 * @ingroup virtual-net-device
52 *
53 * Tunnel class - its goal is to create and manage the tunnels between endpoints.
54 */
55class Tunnel
56{
57 Ptr<Socket> m_n3Socket; //!< Socket on the N3 node
58 Ptr<Socket> m_n0Socket; //!< Socket on the N0 node
59 Ptr<Socket> m_n1Socket; //!< Socket on the N1 node
60 Ipv4Address m_n3Address; //!< Address of the N3 node
61 Ipv4Address m_n0Address; //!< Address of the N0 node
62 Ipv4Address m_n1Address; //!< Address of the N1 node
63 Ptr<UniformRandomVariable> m_rng; //!< Random number generator
64 Ptr<VirtualNetDevice> m_n0Tap; //!< VirtualNetDevice on the N0 node
65 Ptr<VirtualNetDevice> m_n1Tap; //!< VirtualNetDevice on the N1 node
66 Ptr<VirtualNetDevice> m_n3Tap; //!< VirtualNetDevice on the N3 node
67
68 /**
69 * Send a packet through the N0 VirtualNetDevice
70 * @param packet Packet to send
71 * @param source Source IPv4 address
72 * @param dest Destination IPv4 address
73 * @param protocolNumber Protocol number
74 * @return true (always)
75 */
77 const Address& source,
78 const Address& dest,
79 uint16_t protocolNumber)
80 {
81 NS_LOG_DEBUG("Send to " << m_n3Address << ": " << *packet);
82 m_n0Socket->SendTo(packet, 0, InetSocketAddress(m_n3Address, 667));
83 return true;
84 }
85
86 /**
87 * Send a packet through the N1 VirtualNetDevice
88 * @param packet Packet to send
89 * @param source Source IPv4 address
90 * @param dest Destination IPv4 address
91 * @param protocolNumber Protocol number
92 * @return true (always)
93 */
95 const Address& source,
96 const Address& dest,
97 uint16_t protocolNumber)
98 {
99 NS_LOG_DEBUG("Send to " << m_n3Address << ": " << *packet);
100 m_n1Socket->SendTo(packet, 0, InetSocketAddress(m_n3Address, 667));
101 return true;
102 }
103
104 /**
105 * Send a packet through the N3 VirtualNetDevice
106 * @param packet Packet to send
107 * @param source Source IPv4 address
108 * @param dest Destination IPv4 address
109 * @param protocolNumber Protocol number
110 * @return true (always)
111 */
113 const Address& source,
114 const Address& dest,
115 uint16_t protocolNumber)
116 {
117 if (m_rng->GetValue() < 0.25)
118 {
119 NS_LOG_DEBUG("Send to " << m_n0Address << ": " << *packet);
120 m_n3Socket->SendTo(packet, 0, InetSocketAddress(m_n0Address, 667));
121 }
122 else
123 {
124 NS_LOG_DEBUG("Send to " << m_n1Address << ": " << *packet);
125 m_n3Socket->SendTo(packet, 0, InetSocketAddress(m_n1Address, 667));
126 }
127 return true;
128 }
129
130 /**
131 * Receive a packet on the N3 VirtualNetDevice
132 * @param socket Receiving socket
133 */
135 {
136 Ptr<Packet> packet = socket->Recv(65535, 0);
137 NS_LOG_DEBUG("N3SocketRecv: " << *packet);
138 m_n3Tap->Receive(packet,
140 m_n3Tap->GetAddress(),
141 m_n3Tap->GetAddress(),
143 }
144
145 /**
146 * Receive a packet on the N0 VirtualNetDevice
147 * @param socket Receiving socket
148 */
150 {
151 Ptr<Packet> packet = socket->Recv(65535, 0);
152 NS_LOG_DEBUG("N0SocketRecv: " << *packet);
153 m_n0Tap->Receive(packet,
155 m_n0Tap->GetAddress(),
156 m_n0Tap->GetAddress(),
158 }
159
160 /**
161 * Receive a packet on the N1 VirtualNetDevice
162 * @param socket Receiving socket
163 */
165 {
166 Ptr<Packet> packet = socket->Recv(65535, 0);
167 NS_LOG_DEBUG("N1SocketRecv: " << *packet);
168 m_n1Tap->Receive(packet,
170 m_n1Tap->GetAddress(),
171 m_n1Tap->GetAddress(),
173 }
174
175 public:
176 /**
177 * Constructor
178 * @param n3 Pointer of Node 3
179 * @param n0 Pointer of Node 0
180 * @param n1 Pointer of Node 1
181 * @param n3Addr IPv4 address of Node 3
182 * @param n0Addr IPv4 address of Node 0
183 * @param n1Addr IPv4 address of Node 1
184 */
186 Ptr<Node> n0,
187 Ptr<Node> n1,
188 Ipv4Address n3Addr,
189 Ipv4Address n0Addr,
190 Ipv4Address n1Addr)
191 : m_n3Address(n3Addr),
192 m_n0Address(n0Addr),
193 m_n1Address(n1Addr)
194 {
196 m_n3Socket = Socket::CreateSocket(n3, TypeId::LookupByName("ns3::UdpSocketFactory"));
198 m_n3Socket->SetRecvCallback(MakeCallback(&Tunnel::N3SocketRecv, this));
199
200 m_n0Socket = Socket::CreateSocket(n0, TypeId::LookupByName("ns3::UdpSocketFactory"));
202 m_n0Socket->SetRecvCallback(MakeCallback(&Tunnel::N0SocketRecv, this));
203
204 m_n1Socket = Socket::CreateSocket(n1, TypeId::LookupByName("ns3::UdpSocketFactory"));
206 m_n1Socket->SetRecvCallback(MakeCallback(&Tunnel::N1SocketRecv, this));
207
208 // n0 tap device
210 m_n0Tap->SetAddress(Mac48Address("11:00:01:02:03:01"));
211 m_n0Tap->SetSendCallback(MakeCallback(&Tunnel::N0VirtualSend, this));
212 n0->AddDevice(m_n0Tap);
213 Ptr<Ipv4> ipv4 = n0->GetObject<Ipv4>();
214 uint32_t i = ipv4->AddInterface(m_n0Tap);
215 ipv4->AddAddress(i,
216 Ipv4InterfaceAddress(Ipv4Address("11.0.0.1"), Ipv4Mask("255.255.255.0")));
217 ipv4->SetUp(i);
218
219 // n1 tap device
221 m_n1Tap->SetAddress(Mac48Address("11:00:01:02:03:02"));
222 m_n1Tap->SetSendCallback(MakeCallback(&Tunnel::N1VirtualSend, this));
223 n1->AddDevice(m_n1Tap);
224 ipv4 = n1->GetObject<Ipv4>();
225 i = ipv4->AddInterface(m_n1Tap);
226 ipv4->AddAddress(i,
227 Ipv4InterfaceAddress(Ipv4Address("11.0.0.1"), Ipv4Mask("255.255.255.0")));
228 ipv4->SetUp(i);
229
230 // n3 tap device
232 m_n3Tap->SetAddress(Mac48Address("11:00:01:02:03:04"));
233 m_n3Tap->SetSendCallback(MakeCallback(&Tunnel::N3VirtualSend, this));
234 n3->AddDevice(m_n3Tap);
235 ipv4 = n3->GetObject<Ipv4>();
236 i = ipv4->AddInterface(m_n3Tap);
237 ipv4->AddAddress(
238 i,
239 Ipv4InterfaceAddress(Ipv4Address("11.0.0.254"), Ipv4Mask("255.255.255.0")));
240 ipv4->SetUp(i);
241 }
242};
243
244int
245main(int argc, char* argv[])
246{
247 // Users may find it convenient to turn on explicit logging
248 // for selected modules; the below lines suggest how to do this
249#if 0
250 LogComponentEnable ("VirtualNetDeviceExample", LOG_LEVEL_INFO);
251#endif
253
254 // Set up some default values for the simulation. Use the
255 Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(210));
256 Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("448kb/s"));
257
258 // Allow the user to override any of the defaults and the above
259 // Config::SetDefault ()s at run-time, via command-line arguments
260 CommandLine cmd(__FILE__);
261 cmd.Parse(argc, argv);
262
263 NS_LOG_INFO("Create nodes.");
265 c.Create(4);
266 NodeContainer n0n2 = NodeContainer(c.Get(0), c.Get(2));
267 NodeContainer n1n2 = NodeContainer(c.Get(1), c.Get(2));
268 NodeContainer n3n2 = NodeContainer(c.Get(3), c.Get(2));
269
271 internet.Install(c);
272
273 // We create the channels first without any IP addressing information
274 NS_LOG_INFO("Create channels.");
276 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
277 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
278 NetDeviceContainer d0d2 = p2p.Install(n0n2);
279
280 NetDeviceContainer d1d2 = p2p.Install(n1n2);
281
282 p2p.SetDeviceAttribute("DataRate", StringValue("1500kbps"));
283 p2p.SetChannelAttribute("Delay", StringValue("10ms"));
284 NetDeviceContainer d3d2 = p2p.Install(n3n2);
285
286 // Later, we add IP addresses.
287 NS_LOG_INFO("Assign IP Addresses.");
289 ipv4.SetBase("10.1.1.0", "255.255.255.0");
290 Ipv4InterfaceContainer i0i2 = ipv4.Assign(d0d2);
291
292 ipv4.SetBase("10.1.2.0", "255.255.255.0");
293 Ipv4InterfaceContainer i1i2 = ipv4.Assign(d1d2);
294
295 ipv4.SetBase("10.1.3.0", "255.255.255.0");
296 Ipv4InterfaceContainer i3i2 = ipv4.Assign(d3d2);
297
298 // Create router nodes, initialize routing database and set up the routing
299 // tables in the nodes.
301
302 // Add the tunnels n0<=>n3 and n1<=>n3
303 Tunnel tunnel(c.Get(3),
304 c.Get(0),
305 c.Get(1),
306 i3i2.GetAddress(0),
307 i0i2.GetAddress(0),
308 i1i2.GetAddress(0));
309
310 // Create the OnOff application to send UDP datagrams of size
311 // 210 bytes at a rate of 448 Kb/s
312 NS_LOG_INFO("Create Applications.");
313 uint16_t port = 9; // Discard port (RFC 863)
314 OnOffHelper onoff("ns3::UdpSocketFactory",
315 Address(InetSocketAddress(Ipv4Address("11.0.0.254"), port)));
316 onoff.SetConstantRate(DataRate("448kb/s"));
317 ApplicationContainer apps = onoff.Install(c.Get(0));
318 apps.Start(Seconds(1));
319 apps.Stop(Seconds(10));
320
321 // Create a packet sink to receive these packets
322 PacketSinkHelper sink("ns3::UdpSocketFactory",
324 apps = sink.Install(c.Get(3));
325 apps.Start(Seconds(1));
326 // apps.Stop (Seconds (10.0));
327
328 // Create a similar flow from n3 to n1, starting at time 1.1 seconds
329 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(Ipv4Address("11.0.0.1"), port)));
330 apps = onoff.Install(c.Get(3));
331 apps.Start(Seconds(1.1));
332 apps.Stop(Seconds(10));
333
334 // Create a packet sink to receive these packets
335 apps = sink.Install(c.Get(1));
336 apps.Start(Seconds(1.1));
337 // apps.Stop (Seconds (10.0));
338
339 AsciiTraceHelper ascii;
340 p2p.EnableAsciiAll(ascii.CreateFileStream("virtual-net-device.tr"));
341 p2p.EnablePcapAll("virtual-net-device");
342
343 NS_LOG_INFO("Run Simulation.");
346 NS_LOG_INFO("Done.");
347
348 return 0;
349}
Ipv4InterfaceContainer i0i2
IPv4 interface container i0 + i2.
NodeContainer n1n2
Nodecontainer n1 + n2.
Ipv4InterfaceContainer i1i2
IPv4 interface container i1 + i2.
NodeContainer n0n2
Nodecontainer n0 + n2.
Tunnel class - its goal is to create and manage the tunnels between endpoints.
void N0SocketRecv(Ptr< Socket > socket)
Receive a packet on the N0 VirtualNetDevice.
bool N0VirtualSend(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Send a packet through the N0 VirtualNetDevice.
Ptr< Socket > m_n3Socket
Socket on the N3 node.
void N3SocketRecv(Ptr< Socket > socket)
Receive a packet on the N3 VirtualNetDevice.
Ptr< Socket > m_n1Socket
Socket on the N1 node.
Ipv4Address m_n1Address
Address of the N1 node.
Ipv4Address m_n3Address
Address of the N3 node.
Ipv4Address m_n0Address
Address of the N0 node.
bool N3VirtualSend(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Send a packet through the N3 VirtualNetDevice.
Ptr< UniformRandomVariable > m_rng
Random number generator.
void N1SocketRecv(Ptr< Socket > socket)
Receive a packet on the N1 VirtualNetDevice.
Ptr< VirtualNetDevice > m_n0Tap
VirtualNetDevice on the N0 node.
Ptr< VirtualNetDevice > m_n3Tap
VirtualNetDevice on the N3 node.
bool N1VirtualSend(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Send a packet through the N1 VirtualNetDevice.
Ptr< Socket > m_n0Socket
Socket on the N0 node.
Ptr< VirtualNetDevice > m_n1Tap
VirtualNetDevice on the N1 node.
Tunnel(Ptr< Node > n3, Ptr< Node > n0, Ptr< Node > n1, Ipv4Address n3Addr, Ipv4Address n0Addr, Ipv4Address n1Addr)
Constructor.
a polymophic address class
Definition address.h:114
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.
Class for representing data rates.
Definition data-rate.h:78
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
a class to store IPv4 address information on an interface
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
a class to represent an Ipv4 address mask
an EUI-48 address
holds a vector of ns3::NetDevice pointers
@ PACKET_HOST
Packet addressed to us.
Definition net-device.h:315
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:586
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
Hold variables of type string.
Definition string.h:45
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:874
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:690
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:886
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:274
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
constexpr uint16_t IPV4
IPv4 EtherType [RFC894].
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:491
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:103
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44