A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
nix-simple-multi-address.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 * Author: Ameya Deshpande <ameyanrd@outlook.com>
7 *
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/network-module.h"
16#include "ns3/nix-vector-helper.h"
17#include "ns3/point-to-point-module.h"
18
19/**
20 * This program demonstrates a routing and
21 * PrintRoutingPath example for multiple interface
22 * addresses.
23 *
24 * Simple point-to-point links:
25 * \verbatim
26 n0 -- n1 -- n2 -- n3
27
28 From t = 0s onwards,
29 n0 (right) IP: 10.1.1.1, 10.2.1.1
30 n1 (left) IP: 10.1.1.2, 10.2.1.2
31 n1 (right) IP: 10.1.2.1
32 n2 (left) IP: 10.1.2.2
33 n2 (right) IP: 10.1.3.1
34 n3 (left) IP: 10.1.3.2
35
36 From t = +5s onwards (new address subnet
37 added for n2 and n3),
38 n0 (right) IP: 10.1.1.1, 10.2.1.1
39 n1 (left) IP: 10.1.1.2, 10.2.1.2
40 n1 (right) IP: 10.1.2.1
41 n2 (left) IP: 10.1.2.2
42 n2 (right) IP: 10.1.3.1, 10.2.3.1
43 n3 (left) IP: 10.1.3.2, 10.2.3.2
44 \endverbatim
45 *
46 * Cases considered:
47 * 1. For UDP Echo Application:
48 * - At t = +2s, Path from n0 to n3 (10.1.3.2).
49 * 2. For PrintRoutingPath, 10.1.1.3
50 * - At t = +3s, Path from n0 to n1 (10.2.1.2).
51 * - At t = +7s, Path from n0 to n3 (10.2.3.2).
52 *
53 * Logging output:
54 * \verbatim
55 At time +2s client sent 1024 bytes to 10.1.3.2 port 9
56 At time +2.01106s server received 1024 bytes from 10.1.1.1 port 49153
57 At time +2.01106s server sent 1024 bytes to 10.1.1.1 port 49153
58 At time +2.02212s client received 1024 bytes from 10.1.3.2 port 9
59 \endverbatim
60 *
61 * Output in nix-simple-multi-address.routes:
62 * \verbatim
63 Time: +3s, Nix Routing
64 Route path from Node 0 to Node 1, Nix Vector: 0 (1 bits left)
65 10.2.1.1 (Node 0) ----> 10.2.1.2 (Node 1)
66
67 Node: 0, Time: +4s, Local time: +4s, Nix Routing
68 NixCache:
69 Destination NixVector
70 10.1.3.2 011 (3 bits left)
71 IpRouteCache:
72 Destination Gateway Source OutputDevice 10.1.3.2 10.1.1.2
73 10.1.1.1 1
74
75 Node: 1, Time: +4s, Local time: +4s, Nix Routing
76 NixCache:
77 IpRouteCache:
78 Destination Gateway Source OutputDevice 10.1.1.1 10.1.1.1
79 10.1.1.2 1
80 10.1.3.2 10.1.2.2 10.1.2.1 2
81
82 Node: 2, Time: +4s, Local time: +4s, Nix Routing
83 NixCache:
84 IpRouteCache:
85 Destination Gateway Source OutputDevice 10.1.1.1 10.1.2.1
86 10.1.2.2 1
87 10.1.3.2 10.1.3.2 10.1.3.1 2
88
89 Node: 3, Time: +4s, Local time: +4s, Nix Routing
90 NixCache:
91 Destination NixVector
92 10.1.1.1 000 (3 bits left)
93 IpRouteCache:
94 Destination Gateway Source OutputDevice 10.1.1.1 10.1.3.1
95 10.1.3.2 1
96
97 Node: 0, Time: +6s, Local time: +6s, Nix Routing
98 NixCache:
99 IpRouteCache:
100
101 Node: 1, Time: +6s, Local time: +6s, Nix Routing
102 NixCache:
103 IpRouteCache:
104
105 Node: 2, Time: +6s, Local time: +6s, Nix Routing
106 NixCache:
107 IpRouteCache:
108
109 Node: 3, Time: +6s, Local time: +6s, Nix Routing
110 NixCache:
111 IpRouteCache:
112
113 Time: +7s, Nix Routing
114 Route path from Node 0 to Node 3, Nix Vector: 011 (3 bits left)
115 10.1.1.1 (Node 0) ----> 10.1.1.2 (Node 1)
116 10.1.2.1 (Node 1) ----> 10.1.2.2 (Node 2)
117 10.1.3.1 (Node 2) ----> 10.2.3.2 (Node 3)
118 \endverbatim
119 */
120
121using namespace ns3;
122
123NS_LOG_COMPONENT_DEFINE("NixSimpleMultiAddressExample");
124
125int
126main(int argc, char* argv[])
127{
128 CommandLine cmd(__FILE__);
129 cmd.Parse(argc, argv);
130
131 LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
132 LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
133
134 NodeContainer nodes12;
135 nodes12.Create(2);
136
137 NodeContainer nodes23;
138 nodes23.Add(nodes12.Get(1));
139 nodes23.Create(1);
140
141 NodeContainer nodes34;
142 nodes34.Add(nodes23.Get(1));
143 nodes34.Create(1);
144
146 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
147 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
148
149 NodeContainer allNodes = NodeContainer(nodes12, nodes23.Get(1), nodes34.Get(1));
150
151 // NixHelper to install nix-vector routing
152 // on all nodes
155 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
156 stack.Install(allNodes);
157
158 NetDeviceContainer devices12;
159 NetDeviceContainer devices23;
160 NetDeviceContainer devices34;
161 devices12 = pointToPoint.Install(nodes12);
162 devices23 = pointToPoint.Install(nodes23);
163 devices34 = pointToPoint.Install(nodes34);
164
165 Ipv4AddressHelper address1;
166 address1.SetBase("10.1.1.0", "255.255.255.0");
167 Ipv4AddressHelper address2;
168 address2.SetBase("10.1.2.0", "255.255.255.0");
169 Ipv4AddressHelper address3;
170 address3.SetBase("10.1.3.0", "255.255.255.0");
171 Ipv4AddressHelper address4;
172 address4.SetBase("10.2.1.0", "255.255.255.0");
173 Ipv4AddressHelper address5;
174 address5.SetBase("10.2.3.0", "255.255.255.0");
175
176 address1.Assign(devices12);
177 address2.Assign(devices23);
178 Ipv4InterfaceContainer interfaces34 = address3.Assign(devices34);
179 Ipv4InterfaceContainer interfaces12 = address4.Assign(devices12);
180
182
183 ApplicationContainer serverApps = echoServer.Install(nodes34.Get(1));
184 serverApps.Start(Seconds(1.0));
185 serverApps.Stop(Seconds(10.0));
186
187 // Set the destination address as 10.1.3.2
188 UdpEchoClientHelper echoClient(interfaces34.GetAddress(1), 9);
189 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
190 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
191 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
192
193 ApplicationContainer clientApps = echoClient.Install(nodes12.Get(0));
194 clientApps.Start(Seconds(2.0));
195 clientApps.Stop(Seconds(10.0));
196
197 // Trace routing paths for different source and destinations.
198 Ptr<OutputStreamWrapper> routingStream =
199 Create<OutputStreamWrapper>("nix-simple-multi-address.routes", std::ios::out);
200 // Check the path from n0 to n1 (10.1.3.2).
201 nixRouting.PrintRoutingPathAt(Seconds(3),
202 nodes12.Get(0),
203 interfaces12.GetAddress(1, 1),
204 routingStream);
205 // Trace routing tables.
206 Ipv4NixVectorHelper::PrintRoutingTableAllAt(Seconds(4), routingStream);
207 // Assign address5 addresses to n2 and n3.
208 // n2 gets 10.2.3.1 and n3 gets 10.2.3.2 on the same interface.
209 Simulator::Schedule(Seconds(5), &Ipv4AddressHelper::Assign, &address5, devices34);
210
211 // Trace routing tables.
212 // Notice that NixCache and Ipv4RouteCache becomes empty for each node.
213 // This happens because new addresses are added at t = +5s, due to which
214 // existing caches get flushed.
215 Ipv4NixVectorHelper::PrintRoutingTableAllAt(Seconds(6), routingStream);
216
217 // Check the path from n0 to n3 (10.2.3.2).
218 nixRouting.PrintRoutingPathAt(Seconds(7),
219 nodes12.Get(0),
220 Ipv4Address("10.2.3.2"),
221 routingStream);
222
225 return 0;
226}
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...
Ipv4 addresses are stored in host order in this class.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
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 EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
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
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