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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Modified By: Ameya Deshpande <ameyanrd@outlook.com>
18 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
19 */
20
21#include "ns3/applications-module.h"
22#include "ns3/core-module.h"
23#include "ns3/internet-module.h"
24#include "ns3/ipv4-list-routing-helper.h"
25#include "ns3/ipv4-static-routing-helper.h"
26#include "ns3/ipv6-list-routing-helper.h"
27#include "ns3/ipv6-static-routing-helper.h"
28#include "ns3/network-module.h"
29#include "ns3/nix-vector-helper.h"
30#include "ns3/point-to-point-module.h"
31
32/**
33 * This program demonstrates two types of
34 * trace output that are available:
35 * 1) Print Routing Table for all the nodes.
36 * 2) Print Routing Path, given source and destination.
37 *
38 * Simple point to point links:
39 * \verbatim
40 ________
41 / \
42 n0 -- n1 -- n2 -- n3
43
44 Using IPv4:
45 n0 IP: 10.1.1.1, 10.1.4.1
46 n1 IP: 10.1.1.2, 10.1.2.1
47 n2 IP: 10.1.2.2, 10.1.3.1, 10.1.4.2
48 n3 IP: 10.1.3.2
49
50 Using IPv6: (parenthesis mentions the link for node
51 interface associated)
52 n0 IP: 2001:1::200:ff:fe00:1 (Global Unicast on n0 -- n1)
53 2001:4::200:ff:fe00:7 (Global Unicast on n0 -- n2)
54 fe80::200:ff:fe00:1 (Link-local on n0 -- n1)
55 fe80::200:ff:fe00:7 (Link-local on n0 -- n2)
56 n1 IP: 2001:1::200:ff:fe00:2 (Global Unicast on n0 -- n1)
57 2001:2::200:ff:fe00:3 (Global Unicast on n1 -- n2)
58 fe80::200:ff:fe00:2 (Link-local on n0 -- n1)
59 fe80::200:ff:fe00:3 (Link-local on n0 -- n2)
60 n2 IP: 2001:2::200:ff:fe00:4 (Global Unicast on n1 -- n2)
61 2001:3::200:ff:fe00:5 (Global Unicast on n2 -- n3)
62 2001:4::200:ff:fe00:8 (Global Unicast on n0 -- n2)
63 fe80::200:ff:fe00:4 (Link-local on n1 -- n2)
64 fe80::200:ff:fe00:5 (Link-local on n2 -- n3)
65 fe80::200:ff:fe00:8 (Link-local on n0 -- n2)
66 n3 IP: 2001:3::200:ff:fe00:6 (Global Unicast on n2 -- n3)
67 fe80::200:ff:fe00:6 (Link-local on n2 -- n3)
68 \endverbatim
69 *
70 * Route Path for considered cases:
71 * - Source (n0) and Destination (n3)
72 * It goes from n0 -> n2 -> n3
73 * - Source (n1) and Destination (n3)
74 * It goes from n1 -> n2 -> n3
75 * - Source (n2) and Destination (n0)
76 * It goes from n2 -> n0
77 * - Source (n1) and Destination (n1)
78 * It goes from n1 -> n1
79 * .
80 *
81 * Expected IPv4 Routing Path output for above
82 * cases (in the output stream):
83 * \verbatim
84 Time: +3s, Nix Routing
85 Route path from Node 0 to Node 3, Nix Vector: 101 (3 bits left)
86 10.1.4.1 (Node 0) ----> 10.1.4.2 (Node 2)
87 10.1.3.1 (Node 2) ----> 10.1.3.2 (Node 3)
88
89 Time: +5s, Nix Routing
90 Route path from Node 1 to Node 3, Nix Vector: 101 (3 bits left)
91 10.1.2.1 (Node 1) ----> 10.1.2.2 (Node 2)
92 10.1.3.1 (Node 2) ----> 10.1.3.2 (Node 3)
93
94 Time: +6s, Nix Routing
95 Route path from Node 2 to Node 0, Nix Vector: 10 (2 bits left)
96 10.1.4.2 (Node 2) ----> 10.1.1.1 (Node 0)
97
98 Time: +7s, Nix Routing
99 Route path from Node 1 to Node 1, Nix Vector:
100 10.1.1.2 (Node 1) ----> 10.1.1.2 (Node 1)
101
102 Node: 0, Time: +8s, Local time: +8s, Nix Routing
103 NixCache:
104 Destination NixVector
105 10.1.3.2 101 (3 bits left)
106 IpRouteCache:
107 Destination Gateway Source OutputDevice 10.1.3.2 10.1.4.2
108 10.1.4.1 1
109
110 Node: 1, Time: +8s, Local time: +8s, Nix Routing
111 NixCache:
112 IpRouteCache:
113
114 Node: 2, Time: +8s, Local time: +8s, Nix Routing
115 NixCache:
116 IpRouteCache:
117 Destination Gateway Source OutputDevice 10.1.3.2 10.1.3.2
118 10.1.3.1 1
119 10.1.4.1 10.1.4.1 10.1.4.2 2
120
121 Node: 3, Time: +8s, Local time: +8s, Nix Routing
122 NixCache:
123 Destination NixVector
124 10.1.4.1 010 (3 bits left)
125 IpRouteCache:
126 Destination Gateway Source OutputDevice 10.1.4.1 10.1.3.1
127 10.1.3.2 0 \endverbatim
128 *
129 * Expected IPv6 Routing Path output for above
130 * cases (in the output stream):
131 * \verbatim
132 Time: +3s, Nix Routing
133 Route path from Node 0 to Node 3, Nix Vector: 101 (3 bits left)
134 2001:4::200:ff:fe00:7 (Node 0) ----> fe80::200:ff:fe00:8 (Node 2)
135 fe80::200:ff:fe00:5 (Node 2) ----> 2001:3::200:ff:fe00:6 (Node 3)
136
137 Time: +5s, Nix Routing
138 Route path from Node 1 to Node 3, Nix Vector: 101 (3 bits left)
139 2001:2::200:ff:fe00:3 (Node 1) ----> fe80::200:ff:fe00:4 (Node 2)
140 fe80::200:ff:fe00:5 (Node 2) ----> 2001:3::200:ff:fe00:6 (Node 3)
141
142 Time: +6s, Nix Routing
143 Route path from Node 2 to Node 0, Nix Vector: 10 (2 bits left)
144 2001:4::200:ff:fe00:8 (Node 2) ----> 2001:1::200:ff:fe00:1 (Node 0)
145
146 Time: +7s, Nix Routing
147 Route path from Node 1 to Node 1, Nix Vector:
148 2001:1::200:ff:fe00:2 (Node 1) ----> 2001:1::200:ff:fe00:2 (Node 1)
149
150 Node: 0, Time: +8s, Local time: +8s, Nix Routing
151 NixCache:
152 Destination NixVector
153 2001:3::200:ff:fe00:6 101 (3 bits left)
154 IpRouteCache:
155 Destination Gateway Source OutputDevice
156 2001:3::200:ff:fe00:6 fe80::200:ff:fe00:8 2001:4::200:ff:fe00:7 1
157
158 Node: 1, Time: +8s, Local time: +8s, Nix Routing
159 NixCache:
160 IpRouteCache:
161
162 Node: 2, Time: +8s, Local time: +8s, Nix Routing
163 NixCache:
164 IpRouteCache:
165 Destination Gateway Source OutputDevice
166 2001:3::200:ff:fe00:6 fe80::200:ff:fe00:6 fe80::200:ff:fe00:5 1
167 2001:4::200:ff:fe00:7 fe80::200:ff:fe00:7 fe80::200:ff:fe00:8 2
168
169 Node: 3, Time: +8s, Local time: +8s, Nix Routing
170 NixCache:
171 Destination NixVector
172 2001:4::200:ff:fe00:7 010 (3 bits left)
173 IpRouteCache:
174 Destination Gateway Source OutputDevice
175 2001:4::200:ff:fe00:7 fe80::200:ff:fe00:5 2001:3::200:ff:fe00:6 0
176 \endverbatim
177 */
178
179using namespace ns3;
180
181NS_LOG_COMPONENT_DEFINE("NixSimpleExample");
182
183int
184main(int argc, char* argv[])
185{
186 bool useIpv6 = false;
187
188 CommandLine cmd(__FILE__);
189 cmd.AddValue("useIPv6", "Use IPv6 instead of IPv4", useIpv6);
190 cmd.Parse(argc, argv);
191
192 LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
193 LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
194
196 nodes.Create(4);
197
199 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
200 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
201
202 NetDeviceContainer devices01;
203 NetDeviceContainer devices12;
204 NetDeviceContainer devices23;
205 NetDeviceContainer devices02;
206 devices01 = pointToPoint.Install(NodeContainer(nodes.Get(0), nodes.Get(1)));
207 devices12 = pointToPoint.Install(NodeContainer(nodes.Get(1), nodes.Get(2)));
208 devices23 = pointToPoint.Install(NodeContainer(nodes.Get(2), nodes.Get(3)));
209 devices02 = pointToPoint.Install(NodeContainer(nodes.Get(0), nodes.Get(2)));
210
211 Address udpServerAddress;
212
213 if (!useIpv6)
214 {
215 // NixHelper to install nix-vector routing
216 // on all nodes
219 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
220 stack.Install(nodes);
221
222 Ipv4AddressHelper address1;
223 address1.SetBase("10.1.1.0", "255.255.255.0");
224 Ipv4AddressHelper address2;
225 address2.SetBase("10.1.2.0", "255.255.255.0");
226 Ipv4AddressHelper address3;
227 address3.SetBase("10.1.3.0", "255.255.255.0");
228 Ipv4AddressHelper address4;
229 address4.SetBase("10.1.4.0", "255.255.255.0");
230
231 Ipv4InterfaceContainer interfaces01 = address1.Assign(devices01);
232 Ipv4InterfaceContainer interfaces12 = address2.Assign(devices12);
233 Ipv4InterfaceContainer interfaces23 = address3.Assign(devices23);
234 Ipv4InterfaceContainer interfaces02 = address4.Assign(devices02);
235
236 udpServerAddress = interfaces23.GetAddress(1);
237
238 // Trace routing paths for different source and destinations.
239 Ptr<OutputStreamWrapper> routingStream =
240 Create<OutputStreamWrapper>("nix-simple-ipv4.routes", std::ios::out);
241 nixRouting.PrintRoutingPathAt(Seconds(3),
242 nodes.Get(0),
243 interfaces23.GetAddress(1),
244 routingStream);
245 nixRouting.PrintRoutingPathAt(Seconds(5),
246 nodes.Get(1),
247 interfaces23.GetAddress(1),
248 routingStream);
249 nixRouting.PrintRoutingPathAt(Seconds(6),
250 nodes.Get(2),
251 interfaces01.GetAddress(0),
252 routingStream);
253 nixRouting.PrintRoutingPathAt(Seconds(7),
254 nodes.Get(1),
255 interfaces01.GetAddress(1),
256 routingStream);
257 // Trace routing tables
258 Ipv4NixVectorHelper::PrintRoutingTableAllAt(Seconds(8), routingStream);
259 }
260 else
261 {
262 // NixHelper to install nix-vector routing
263 // on all nodes
266 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
267 stack.Install(nodes);
268
269 Ipv6AddressHelper address1;
270 address1.SetBase(Ipv6Address("2001:1::"), Ipv6Prefix(64));
271 Ipv6AddressHelper address2;
272 address2.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
273 Ipv6AddressHelper address3;
274 address3.SetBase(Ipv6Address("2001:3::"), Ipv6Prefix(64));
275 Ipv6AddressHelper address4;
276 address4.SetBase(Ipv6Address("2001:4::"), Ipv6Prefix(64));
277
278 Ipv6InterfaceContainer interfaces01 = address1.Assign(devices01);
279 Ipv6InterfaceContainer interfaces12 = address2.Assign(devices12);
280 Ipv6InterfaceContainer interfaces23 = address3.Assign(devices23);
281 Ipv6InterfaceContainer interfaces02 = address4.Assign(devices02);
282
283 udpServerAddress = interfaces23.GetAddress(1, 1);
284
285 // Trace routing paths for different source and destinations.
286 Ptr<OutputStreamWrapper> routingStream =
287 Create<OutputStreamWrapper>("nix-simple-ipv6.routes", std::ios::out);
288 nixRouting.PrintRoutingPathAt(Seconds(3),
289 nodes.Get(0),
290 interfaces23.GetAddress(1, 1),
291 routingStream);
292 nixRouting.PrintRoutingPathAt(Seconds(5),
293 nodes.Get(1),
294 interfaces23.GetAddress(1, 1),
295 routingStream);
296 nixRouting.PrintRoutingPathAt(Seconds(6),
297 nodes.Get(2),
298 interfaces01.GetAddress(0, 1),
299 routingStream);
300 nixRouting.PrintRoutingPathAt(Seconds(7),
301 nodes.Get(1),
302 interfaces01.GetAddress(1, 1),
303 routingStream);
304 // Trace routing tables
305 Ipv6NixVectorHelper::PrintRoutingTableAllAt(Seconds(8), routingStream);
306 }
307
309
311 serverApps.Start(Seconds(1.0));
312 serverApps.Stop(Seconds(10.0));
313
314 UdpEchoClientHelper echoClient(udpServerAddress, 9);
315 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
316 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
317 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
318
320 clientApps.Start(Seconds(2.0));
321 clientApps.Stop(Seconds(10.0));
322
325 return 0;
326}
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
Parse command-line arguments.
Definition: command-line.h:232
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.
Definition: ipv6-address.h:49
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.
Definition: ipv6-address.h:455
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.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1406
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:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
NodeContainer nodes
ns echoServer
Definition: first.py:52
ns serverApps
Definition: first.py:54
ns echoClient
Definition: first.py:59
ns clientApps
Definition: first.py:64
ns stack
Definition: first.py:44
ns pointToPoint
Definition: first.py:38
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:302
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
ns cmd
Definition: second.py:40