A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
nix-simple.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 NITK Surathkal
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Modified By: Ameya Deshpande <ameyanrd@outlook.com>
7 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
8 */
9
10#include "ns3/applications-module.h"
11#include "ns3/core-module.h"
12#include "ns3/internet-module.h"
13#include "ns3/ipv4-list-routing-helper.h"
14#include "ns3/ipv4-static-routing-helper.h"
15#include "ns3/ipv6-list-routing-helper.h"
16#include "ns3/ipv6-static-routing-helper.h"
17#include "ns3/network-module.h"
18#include "ns3/nix-vector-helper.h"
19#include "ns3/point-to-point-module.h"
20
21/**
22 * This program demonstrates two types of
23 * trace output that are available:
24 * 1) Print Routing Table for all the nodes.
25 * 2) Print Routing Path, given source and destination.
26 *
27 * Simple point to point links:
28 * \verbatim
29 ________
30 / \
31 n0 -- n1 -- n2 -- n3
32
33 Using IPv4:
34 n0 IP: 10.1.1.1, 10.1.4.1
35 n1 IP: 10.1.1.2, 10.1.2.1
36 n2 IP: 10.1.2.2, 10.1.3.1, 10.1.4.2
37 n3 IP: 10.1.3.2
38
39 Using IPv6: (parenthesis mentions the link for node
40 interface associated)
41 n0 IP: 2001:1::200:ff:fe00:1 (Global Unicast on n0 -- n1)
42 2001:4::200:ff:fe00:7 (Global Unicast on n0 -- n2)
43 fe80::200:ff:fe00:1 (Link-local on n0 -- n1)
44 fe80::200:ff:fe00:7 (Link-local on n0 -- n2)
45 n1 IP: 2001:1::200:ff:fe00:2 (Global Unicast on n0 -- n1)
46 2001:2::200:ff:fe00:3 (Global Unicast on n1 -- n2)
47 fe80::200:ff:fe00:2 (Link-local on n0 -- n1)
48 fe80::200:ff:fe00:3 (Link-local on n0 -- n2)
49 n2 IP: 2001:2::200:ff:fe00:4 (Global Unicast on n1 -- n2)
50 2001:3::200:ff:fe00:5 (Global Unicast on n2 -- n3)
51 2001:4::200:ff:fe00:8 (Global Unicast on n0 -- n2)
52 fe80::200:ff:fe00:4 (Link-local on n1 -- n2)
53 fe80::200:ff:fe00:5 (Link-local on n2 -- n3)
54 fe80::200:ff:fe00:8 (Link-local on n0 -- n2)
55 n3 IP: 2001:3::200:ff:fe00:6 (Global Unicast on n2 -- n3)
56 fe80::200:ff:fe00:6 (Link-local on n2 -- n3)
57 \endverbatim
58 *
59 * Route Path for considered cases:
60 * - Source (n0) and Destination (n3)
61 * It goes from n0 -> n2 -> n3
62 * - Source (n1) and Destination (n3)
63 * It goes from n1 -> n2 -> n3
64 * - Source (n2) and Destination (n0)
65 * It goes from n2 -> n0
66 * - Source (n1) and Destination (n1)
67 * It goes from n1 -> n1
68 * .
69 *
70 * Expected IPv4 Routing Path output for above
71 * cases (in the output stream):
72 * \verbatim
73 Time: +3s, Nix Routing
74 Route path from Node 0 to Node 3, Nix Vector: 101 (3 bits left)
75 10.1.4.1 (Node 0) ----> 10.1.4.2 (Node 2)
76 10.1.3.1 (Node 2) ----> 10.1.3.2 (Node 3)
77
78 Time: +5s, Nix Routing
79 Route path from Node 1 to Node 3, Nix Vector: 101 (3 bits left)
80 10.1.2.1 (Node 1) ----> 10.1.2.2 (Node 2)
81 10.1.3.1 (Node 2) ----> 10.1.3.2 (Node 3)
82
83 Time: +6s, Nix Routing
84 Route path from Node 2 to Node 0, Nix Vector: 10 (2 bits left)
85 10.1.4.2 (Node 2) ----> 10.1.1.1 (Node 0)
86
87 Time: +7s, Nix Routing
88 Route path from Node 1 to Node 1, Nix Vector:
89 10.1.1.2 (Node 1) ----> 10.1.1.2 (Node 1)
90
91 Node: 0, Time: +8s, Local time: +8s, Nix Routing
92 NixCache:
93 Destination NixVector
94 10.1.3.2 101 (3 bits left)
95 IpRouteCache:
96 Destination Gateway Source OutputDevice 10.1.3.2 10.1.4.2
97 10.1.4.1 1
98
99 Node: 1, Time: +8s, Local time: +8s, Nix Routing
100 NixCache:
101 IpRouteCache:
102
103 Node: 2, Time: +8s, Local time: +8s, Nix Routing
104 NixCache:
105 IpRouteCache:
106 Destination Gateway Source OutputDevice 10.1.3.2 10.1.3.2
107 10.1.3.1 1
108 10.1.4.1 10.1.4.1 10.1.4.2 2
109
110 Node: 3, Time: +8s, Local time: +8s, Nix Routing
111 NixCache:
112 Destination NixVector
113 10.1.4.1 010 (3 bits left)
114 IpRouteCache:
115 Destination Gateway Source OutputDevice 10.1.4.1 10.1.3.1
116 10.1.3.2 0 \endverbatim
117 *
118 * Expected IPv6 Routing Path output for above
119 * cases (in the output stream):
120 * \verbatim
121 Time: +3s, Nix Routing
122 Route path from Node 0 to Node 3, Nix Vector: 101 (3 bits left)
123 2001:4::200:ff:fe00:7 (Node 0) ----> fe80::200:ff:fe00:8 (Node 2)
124 fe80::200:ff:fe00:5 (Node 2) ----> 2001:3::200:ff:fe00:6 (Node 3)
125
126 Time: +5s, Nix Routing
127 Route path from Node 1 to Node 3, Nix Vector: 101 (3 bits left)
128 2001:2::200:ff:fe00:3 (Node 1) ----> fe80::200:ff:fe00:4 (Node 2)
129 fe80::200:ff:fe00:5 (Node 2) ----> 2001:3::200:ff:fe00:6 (Node 3)
130
131 Time: +6s, Nix Routing
132 Route path from Node 2 to Node 0, Nix Vector: 10 (2 bits left)
133 2001:4::200:ff:fe00:8 (Node 2) ----> 2001:1::200:ff:fe00:1 (Node 0)
134
135 Time: +7s, Nix Routing
136 Route path from Node 1 to Node 1, Nix Vector:
137 2001:1::200:ff:fe00:2 (Node 1) ----> 2001:1::200:ff:fe00:2 (Node 1)
138
139 Node: 0, Time: +8s, Local time: +8s, Nix Routing
140 NixCache:
141 Destination NixVector
142 2001:3::200:ff:fe00:6 101 (3 bits left)
143 IpRouteCache:
144 Destination Gateway Source OutputDevice
145 2001:3::200:ff:fe00:6 fe80::200:ff:fe00:8 2001:4::200:ff:fe00:7 1
146
147 Node: 1, Time: +8s, Local time: +8s, Nix Routing
148 NixCache:
149 IpRouteCache:
150
151 Node: 2, Time: +8s, Local time: +8s, Nix Routing
152 NixCache:
153 IpRouteCache:
154 Destination Gateway Source OutputDevice
155 2001:3::200:ff:fe00:6 fe80::200:ff:fe00:6 fe80::200:ff:fe00:5 1
156 2001:4::200:ff:fe00:7 fe80::200:ff:fe00:7 fe80::200:ff:fe00:8 2
157
158 Node: 3, Time: +8s, Local time: +8s, Nix Routing
159 NixCache:
160 Destination NixVector
161 2001:4::200:ff:fe00:7 010 (3 bits left)
162 IpRouteCache:
163 Destination Gateway Source OutputDevice
164 2001:4::200:ff:fe00:7 fe80::200:ff:fe00:5 2001:3::200:ff:fe00:6 0
165 \endverbatim
166 */
167
168using namespace ns3;
169
170NS_LOG_COMPONENT_DEFINE("NixSimpleExample");
171
172int
173main(int argc, char* argv[])
174{
175 bool useIpv6 = false;
176
177 CommandLine cmd(__FILE__);
178 cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", useIpv6);
179 cmd.Parse(argc, argv);
180
181 LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
182 LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
183
185 nodes.Create(4);
186
188 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
189 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
190
191 NetDeviceContainer devices01;
192 NetDeviceContainer devices12;
193 NetDeviceContainer devices23;
194 NetDeviceContainer devices02;
195 devices01 = pointToPoint.Install(NodeContainer(nodes.Get(0), nodes.Get(1)));
196 devices12 = pointToPoint.Install(NodeContainer(nodes.Get(1), nodes.Get(2)));
197 devices23 = pointToPoint.Install(NodeContainer(nodes.Get(2), nodes.Get(3)));
198 devices02 = pointToPoint.Install(NodeContainer(nodes.Get(0), nodes.Get(2)));
199
200 Address udpServerAddress;
201
202 if (!useIpv6)
203 {
204 // NixHelper to install nix-vector routing
205 // on all nodes
208 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
209 stack.Install(nodes);
210
211 Ipv4AddressHelper address1;
212 address1.SetBase("10.1.1.0", "255.255.255.0");
213 Ipv4AddressHelper address2;
214 address2.SetBase("10.1.2.0", "255.255.255.0");
215 Ipv4AddressHelper address3;
216 address3.SetBase("10.1.3.0", "255.255.255.0");
217 Ipv4AddressHelper address4;
218 address4.SetBase("10.1.4.0", "255.255.255.0");
219
220 Ipv4InterfaceContainer interfaces01 = address1.Assign(devices01);
221 Ipv4InterfaceContainer interfaces12 = address2.Assign(devices12);
222 Ipv4InterfaceContainer interfaces23 = address3.Assign(devices23);
223 Ipv4InterfaceContainer interfaces02 = address4.Assign(devices02);
224
225 udpServerAddress = interfaces23.GetAddress(1);
226
227 // Trace routing paths for different source and destinations.
228 Ptr<OutputStreamWrapper> routingStream =
229 Create<OutputStreamWrapper>("nix-simple-ipv4.routes", std::ios::out);
230 nixRouting.PrintRoutingPathAt(Seconds(3),
231 nodes.Get(0),
232 interfaces23.GetAddress(1),
233 routingStream);
234 nixRouting.PrintRoutingPathAt(Seconds(5),
235 nodes.Get(1),
236 interfaces23.GetAddress(1),
237 routingStream);
238 nixRouting.PrintRoutingPathAt(Seconds(6),
239 nodes.Get(2),
240 interfaces01.GetAddress(0),
241 routingStream);
242 nixRouting.PrintRoutingPathAt(Seconds(7),
243 nodes.Get(1),
244 interfaces01.GetAddress(1),
245 routingStream);
246 // Trace routing tables
247 Ipv4NixVectorHelper::PrintRoutingTableAllAt(Seconds(8), routingStream);
248 }
249 else
250 {
251 // NixHelper to install nix-vector routing
252 // on all nodes
255 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
256 stack.Install(nodes);
257
258 Ipv6AddressHelper address1;
259 address1.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
260 Ipv6AddressHelper address2;
261 address2.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
262 Ipv6AddressHelper address3;
263 address3.SetBase(Ipv6Address("2001:3::"), Ipv6Prefix(64));
264 Ipv6AddressHelper address4;
265 address4.SetBase(Ipv6Address("2001:4::"), Ipv6Prefix(64));
266
267 Ipv6InterfaceContainer interfaces01 = address1.Assign(devices01);
268 Ipv6InterfaceContainer interfaces12 = address2.Assign(devices12);
269 Ipv6InterfaceContainer interfaces23 = address3.Assign(devices23);
270 Ipv6InterfaceContainer interfaces02 = address4.Assign(devices02);
271
272 udpServerAddress = interfaces23.GetAddress(1, 1);
273
274 // Trace routing paths for different source and destinations.
275 Ptr<OutputStreamWrapper> routingStream =
276 Create<OutputStreamWrapper>("nix-simple-ipv6.routes", std::ios::out);
277 nixRouting.PrintRoutingPathAt(Seconds(3),
278 nodes.Get(0),
279 interfaces23.GetAddress(1, 1),
280 routingStream);
281 nixRouting.PrintRoutingPathAt(Seconds(5),
282 nodes.Get(1),
283 interfaces23.GetAddress(1, 1),
284 routingStream);
285 nixRouting.PrintRoutingPathAt(Seconds(6),
286 nodes.Get(2),
287 interfaces01.GetAddress(0, 1),
288 routingStream);
289 nixRouting.PrintRoutingPathAt(Seconds(7),
290 nodes.Get(1),
291 interfaces01.GetAddress(1, 1),
292 routingStream);
293 // Trace routing tables
294 Ipv6NixVectorHelper::PrintRoutingTableAllAt(Seconds(8), routingStream);
295 }
296
298
300 serverApps.Start(Seconds(1.0));
301 serverApps.Stop(Seconds(10.0));
302
303 UdpEchoClientHelper echoClient(udpServerAddress, 9);
304 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
305 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
306 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
307
309 clientApps.Start(Seconds(2.0));
310 clientApps.Stop(Seconds(10.0));
311
314 return 0;
315}
a polymophic address class
Definition address.h:90
holds a vector of ns3::Application pointers.
Parse command-line arguments.
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class to auto-assign global IPv6 unicast addresses.
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.
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Describes an IPv6 prefix.
holds a vector of ns3::NetDevice pointers
Helper class that adds Nix-vector routing to nodes.
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.
Build a set of PointToPointNetDevice objects.
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
Hold variables of type string.
Definition string.h:45
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
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
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:1308
NodeContainer nodes
echoClient
Definition first.py:48
serverApps
Definition first.py:43
pointToPoint
Definition first.py:27
echoServer
Definition first.py:41
clientApps
Definition first.py:53
stack
Definition first.py:33
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:291
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93