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