A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-global-routing-test-suite.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5#include "ns3/boolean.h"
6#include "ns3/bridge-helper.h"
7#include "ns3/config.h"
8#include "ns3/inet-socket-address.h"
9#include "ns3/internet-stack-helper.h"
10#include "ns3/ipv4-address-helper.h"
11#include "ns3/ipv4-global-routing-helper.h"
12#include "ns3/ipv4-global-routing.h"
13#include "ns3/ipv4-l3-protocol.h"
14#include "ns3/ipv4-packet-info-tag.h"
15#include "ns3/ipv4-routing-protocol.h"
16#include "ns3/ipv4-routing-table-entry.h"
17#include "ns3/ipv4-static-routing-helper.h"
18#include "ns3/log.h"
19#include "ns3/node-container.h"
20#include "ns3/node.h"
21#include "ns3/packet.h"
22#include "ns3/pointer.h"
23#include "ns3/simple-channel.h"
24#include "ns3/simple-net-device-helper.h"
25#include "ns3/simple-net-device.h"
26#include "ns3/simulator.h"
27#include "ns3/socket-factory.h"
28#include "ns3/string.h"
29#include "ns3/test.h"
30#include "ns3/udp-socket-factory.h"
31#include "ns3/uinteger.h"
32
33#include <vector>
34
35using namespace ns3;
36
37NS_LOG_COMPONENT_DEFINE("Ipv4GlobalRoutingTestSuite");
38
39// This test suite tests the operation of global routing on a few sample
40// networks to ensure that routes are built correctly
41//
42// Link test:
43// n0 <--------> n1 (point-to-point link)
44// 10.1.1.1 10.1.1.2
45// Expected routes:
46// n0: route to 0.0.0.0 gw 10.1.1.2
47// n1: route to 0.0.0.0 gw 10.1.1.1
48// Note: These default routes to 0.0.0.0 are generated by the extension
49// in the global route manager to install default routes via the
50// peer node on a point-to-point link, when the node is on a
51// stub link
52//
53// LAN test:
54// n0 <--------> n1 (broadcast link on subnet 10.1.1.0/24)
55// Expected routes:
56// n0: route to 10.1.1.0 gw 0.0.0.0
57// n1: route to 10.1.1.0 gw 0.0.0.0
58// Two link test:
59// n0 <--------> n1 <--------> n2 (point-to-point links)
60// 10.1.1.1 10.1.1.2/ 10.1.2.2
61// 10.1.2.1
62// Expected routes:
63// n0: route to 0.0.0.0 gw 10.1.1.2
64// n1: route to 10.1.1.1 gw 10.1.1.1
65// route to 10.1.2.2 gw 10.1.2.2
66// route to 10.1.1.0 gw 10.1.1.1
67// route to 10.1.2.0 gw 10.1.2.2
68// n2: route to 0.0.0.0 gw 10.1.2.1
69// Note: These default routes to 0.0.0.0 are generated by the extension
70// in the global route manager to install default routes via the
71// peer node on a point-to-point link, when the node is on a
72// stub link
73// Two LANs test:
74// n0 <--------> n1 <--------> n2 (broadcast links)
75// Expected routes:
76// n0: route to 10.1.1.0 gw 0.0.0.0
77// route to 0.0.0.0 gw 10.1.1.2
78// n1: route to 10.1.1.1 gw 10.1.1.1
79// route to 10.1.2.2 gw 10.1.2.2
80// route to 10.1.1.0 gw 10.1.1.1
81// route to 10.1.2.0 gw 10.1.2.2
82// n2: route to 0.0.0.0 gw 10.1.2.1
83// Bridge test:
84// n0 <--------> n1 <---> Bridge-n2 <---> n3 <-------> n4 (broadcast links)
85// 10.1.1.0/24 10.1.2.0/24 10.1.3.0/24
86// Expected routes:
87// n0: route to 10.1.1.0 gw 0.0.0.0
88// route to 10.1.2.0 gw 10.1.1.2
89// route to 10.1.3.0 gw 10.1.1.2
90// n1: route to 10.1.1.0 gw 0.0.0.0
91// route to 10.1.2.0 gw 0.0.0.0
92// route to 10.1.3.0 gw 10.1.2.2
93// n3: route to 10.1.1.0 gw 10.1.2.1
94// route to 10.1.2.0 gw 0.0.0.0
95// route to 10.1.3.0 gw 0.0.0.0
96// n4: route to 10.1.3.0 gw 0.0.0.0
97// route to 10.1.2.0 gw 10.1.3.1
98// route to 10.1.1.0 gw 10.1.3.1
99// Two Bridge test:
100// n0 <------> n1 <---> Bridge-n2 <---> Bridge-n3 <---> n4 (broadcast links)
101// 10.1.1.0/24 10.1.2.0/24
102// Expected routes:
103// n0: route to 10.1.1.0 gw 0.0.0.0
104// route to 10.1.2.0 gw 10.1.1.2
105// n4: route to 10.1.2.0 gw 0.0.0.0
106// route to 10.1.1.0 gw 10.1.2.1
107
108/**
109 * \ingroup internet-test
110 *
111 * \brief IPv4 GlobalRouting Link test
112 */
113class LinkTest : public TestCase
114{
115 public:
116 void DoSetup() override;
117 void DoRun() override;
118 LinkTest();
119
120 private:
121 NodeContainer m_nodes; //!< Nodes used in the test.
122};
123
125 : TestCase("Global routing on point-to-point link")
126{
127}
128
129void
131{
132 m_nodes.Create(2);
133
135 SimpleNetDeviceHelper simpleHelper;
136 simpleHelper.SetNetDevicePointToPointMode(true);
137 NetDeviceContainer net = simpleHelper.Install(m_nodes, channel);
138
139 InternetStackHelper internet;
140 // By default, InternetStackHelper adds a static and global routing
141 // implementation. We just want the global for this test.
142 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
143 internet.SetRoutingHelper(ipv4RoutingHelper);
144 internet.Install(m_nodes);
145
147 ipv4.SetBase("10.1.1.0", "255.255.255.252");
148 Ipv4InterfaceContainer i = ipv4.Assign(net);
149}
150
151void
153{
155
157 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
159 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
160 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
161 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
162 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
163 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
164 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
165 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
166
167 // Test that the right number of routes found
168 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
169 NS_LOG_DEBUG("LinkTest nRoutes0 " << nRoutes0);
170 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- not one route");
171 Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
172 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
173 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
174 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
175
176 // Test that the right number of routes found
177 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
178 NS_TEST_ASSERT_MSG_EQ(nRoutes1, 1, "Error-- not one route");
179 NS_LOG_DEBUG("LinkTest nRoutes1 " << nRoutes1);
180 route = globalRouting1->GetRoute(0);
181 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
182 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
183 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
184
185 bool result = true;
186
187 NS_TEST_ASSERT_MSG_EQ(result, true, "Message");
190}
191
192/**
193 * \ingroup internet-test
194 *
195 * \brief IPv4 GlobalRouting LAN test
196 */
197class LanTest : public TestCase
198{
199 public:
200 void DoSetup() override;
201 void DoRun() override;
202 LanTest();
203
204 private:
205 NodeContainer m_nodes; //!< Nodes used in the test.
206};
207
209 : TestCase("Global routing on broadcast link")
210{
211}
212
213void
215{
216 m_nodes.Create(2);
217
219 SimpleNetDeviceHelper simpleHelper;
220 NetDeviceContainer net = simpleHelper.Install(m_nodes, channel);
221
222 InternetStackHelper internet;
223 // By default, InternetStackHelper adds a static and global routing
224 // implementation. We just want the global for this test.
225 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
226 internet.SetRoutingHelper(ipv4RoutingHelper);
227 internet.Install(m_nodes);
228
230 ipv4.SetBase("10.1.1.0", "255.255.255.0");
231 Ipv4InterfaceContainer i = ipv4.Assign(net);
232}
233
234void
236{
238
240 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
242 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
243 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
244 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
245 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
246 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
247 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
248 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
249
250 // Test that the right number of routes found
251 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
252 NS_LOG_DEBUG("LanTest nRoutes0 " << nRoutes0);
253 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- more than one entry");
254 for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
255 {
256 Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(i);
257 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
258 }
259
260 // Test that the right number of routes found
261 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
262 NS_LOG_DEBUG("LanTest nRoutes1 " << nRoutes1);
263 NS_TEST_ASSERT_MSG_EQ(nRoutes1, 1, "Error-- more than one entry");
264 for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
265 {
266 Ipv4RoutingTableEntry* route = globalRouting1->GetRoute(i);
267 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
268 }
269
271}
272
273/**
274 * \ingroup internet-test
275 *
276 * \brief IPv4 GlobalRouting Two Link test
277 */
278class TwoLinkTest : public TestCase
279{
280 public:
281 void DoSetup() override;
282 void DoRun() override;
283 TwoLinkTest();
284
285 private:
286 NodeContainer m_nodes; //!< Nodes used in the test.
287};
288
290 : TestCase("Global routing across two hops (point-to-point links)")
291{
292}
293
294void
296{
297 m_nodes.Create(3);
298
300 SimpleNetDeviceHelper simpleHelper;
301 simpleHelper.SetNetDevicePointToPointMode(true);
302 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
303 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
304
306 SimpleNetDeviceHelper simpleHelper2;
307 simpleHelper2.SetNetDevicePointToPointMode(true);
308 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
309 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
310
311 InternetStackHelper internet;
312 // By default, InternetStackHelper adds a static and global routing
313 // implementation. We just want the global for this test.
314 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
315 internet.SetRoutingHelper(ipv4RoutingHelper);
316 internet.Install(m_nodes);
317
319 ipv4.SetBase("10.1.1.0", "255.255.255.252");
320 Ipv4InterfaceContainer i = ipv4.Assign(net);
321 ipv4.SetBase("10.1.2.0", "255.255.255.252");
322 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
323}
324
325void
327{
329
331 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
333 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
335 NS_TEST_ASSERT_MSG_NE(ip2, nullptr, "Error-- no Ipv4 object");
336 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
337 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
338 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
339 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
340 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
341 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
342 Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol();
343 Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject<Ipv4GlobalRouting>();
344 NS_TEST_ASSERT_MSG_NE(globalRouting2, nullptr, "Error-- no Ipv4GlobalRouting object");
345
346 // node n0
347 // Test that the right number of routes found
348 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
349 NS_LOG_DEBUG("TwoLinkTest nRoutes0 " << nRoutes0);
350 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- wrong number of links");
351
352 Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
353 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
354 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
355 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
356
357 // node n1
358 // Test that the right number of routes found
359 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
360 NS_LOG_DEBUG("TwoLinkTest nRoutes1 " << nRoutes1);
361 route = globalRouting1->GetRoute(0);
362 NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
363 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.1"), "Error-- wrong destination");
364 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
365 route = globalRouting1->GetRoute(1);
366 NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
367 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.2"), "Error-- wrong destination");
368 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.2"), "Error-- wrong gateway");
369 route = globalRouting1->GetRoute(2);
370 NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
371 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
372 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
373 route = globalRouting1->GetRoute(3);
374 NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
375 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
376 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.2"), "Error-- wrong gateway");
377
378 // node n2
379 // Test that the right number of routes found
380 uint32_t nRoutes2 = globalRouting2->GetNRoutes();
381 NS_LOG_DEBUG("TwoLinkTest nRoutes2 " << nRoutes2);
382 NS_TEST_ASSERT_MSG_EQ(nRoutes2, 1, "Error-- wrong number of links");
383
384 route = globalRouting2->GetRoute(0);
385 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
386 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
387 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.1"), "Error-- wrong gateway");
388
390}
391
392/**
393 * \ingroup internet-test
394 *
395 * \brief IPv4 GlobalRouting Two LAN test
396 */
397class TwoLanTest : public TestCase
398{
399 public:
400 void DoSetup() override;
401 void DoRun() override;
402 TwoLanTest();
403
404 private:
405 NodeContainer m_nodes; //!< Nodes used in the test.
406};
407
409 : TestCase("Global routing across two hops (broadcast links)")
410{
411}
412
413void
415{
416 m_nodes.Create(3);
417
419 SimpleNetDeviceHelper simpleHelper;
420 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
421 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
422
424 SimpleNetDeviceHelper simpleHelper2;
425 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
426 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
427
428 InternetStackHelper internet;
429 // By default, InternetStackHelper adds a static and global routing
430 // implementation. We just want the global for this test.
431 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
432 internet.SetRoutingHelper(ipv4RoutingHelper);
433 internet.Install(m_nodes);
434
436 ipv4.SetBase("10.1.1.0", "255.255.255.0");
437 Ipv4InterfaceContainer i = ipv4.Assign(net);
438 ipv4.SetBase("10.1.2.0", "255.255.255.0");
439 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
440}
441
442void
444{
446
448 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
450 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
452 NS_TEST_ASSERT_MSG_NE(ip2, nullptr, "Error-- no Ipv4 object");
453 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
454 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
455 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
456 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
457 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
458 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
459 Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol();
460 Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject<Ipv4GlobalRouting>();
461 NS_TEST_ASSERT_MSG_NE(globalRouting2, nullptr, "Error-- no Ipv4GlobalRouting object");
462
463 // Test that the right number of routes found
464 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
465 NS_LOG_DEBUG("TwoLanTest nRoutes0 " << nRoutes0);
466 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 2, "Error-- not two entries");
467 Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
468 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
469 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
470 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
471 route = globalRouting0->GetRoute(1);
472 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
473 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
474 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
475
476 // Test that the right number of routes found
477 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
478 NS_LOG_DEBUG("TwoLanTest nRoutes1 " << nRoutes1);
479 NS_TEST_ASSERT_MSG_EQ(nRoutes1, 2, "Error-- not two entries");
480 route = globalRouting1->GetRoute(0);
481 NS_LOG_DEBUG("TwoLanTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
482 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
483 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
484 route = globalRouting1->GetRoute(1);
485 NS_LOG_DEBUG("TwoLanTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
486 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
487 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
488
490}
491
492/**
493 * \ingroup internet-test
494 *
495 * \brief IPv4 GlobalRouting Bridge test
496 */
497class BridgeTest : public TestCase
498{
499 public:
500 void DoSetup() override;
501 void DoRun() override;
502 BridgeTest();
503
504 private:
505 NodeContainer m_nodes; //!< Nodes used in the test.
506};
507
509 : TestCase("Global routing across bridging topology (bug 2102)")
510{
511}
512
513void
515{
516 m_nodes.Create(5);
517
518 // connect node0 to node1
520 SimpleNetDeviceHelper simpleHelper;
521 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
522 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
523
524 NetDeviceContainer bridgeFacingDevices;
525 NetDeviceContainer switchDevices;
526
527 // connect node1 to node2 (switch)
529 SimpleNetDeviceHelper simpleHelper2;
530 NetDeviceContainer net2 = simpleHelper2.Install(m_nodes.Get(1), channel2);
531 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
532 bridgeFacingDevices.Add(net2.Get(0));
533 switchDevices.Add(net2.Get(1));
534
535 // connect node2 (switch) to node3
537 SimpleNetDeviceHelper simpleHelper3;
538 NetDeviceContainer net3 = simpleHelper3.Install(m_nodes.Get(2), channel3);
539 net3.Add(simpleHelper3.Install(m_nodes.Get(3), channel3));
540 bridgeFacingDevices.Add(net3.Get(1));
541 switchDevices.Add(net3.Get(0));
542
543 // connect node3 to node4
545 SimpleNetDeviceHelper simpleHelper4;
546 NetDeviceContainer net4 = simpleHelper4.Install(m_nodes.Get(3), channel4);
547 net4.Add(simpleHelper4.Install(m_nodes.Get(4), channel4));
548
549 Ptr<Node> switchNode = m_nodes.Get(2);
550 BridgeHelper bridge;
551 bridge.Install(switchNode, switchDevices);
552
553 InternetStackHelper internet;
554 // By default, InternetStackHelper adds a static and global routing
555 // implementation. We just want the global for this test.
556 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
557 internet.SetRoutingHelper(ipv4RoutingHelper);
558
559 internet.Install(m_nodes.Get(0));
560 internet.Install(m_nodes.Get(1));
561 // m_nodes.Get (2) is bridge node
562 internet.Install(m_nodes.Get(3));
563 internet.Install(m_nodes.Get(4));
564
565 Ipv4AddressHelper address;
566 address.SetBase("10.1.1.0", "255.255.255.0");
567 address.Assign(net);
568
569 address.SetBase("10.1.2.0", "255.255.255.0");
570 address.Assign(bridgeFacingDevices);
571
572 address.SetBase("10.1.3.0", "255.255.255.0");
573 address.Assign(net4);
574}
575
576void
578{
580
582 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
583 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
584 NS_TEST_ASSERT_MSG_NE(routing0, nullptr, "Error-- no Ipv4 routing protocol object");
585 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
586 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
587
589 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
590 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
591 NS_TEST_ASSERT_MSG_NE(routing1, nullptr, "Error-- no Ipv4 routing protocol object");
592 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
593 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
594
595 // Skip to n4
597 NS_TEST_ASSERT_MSG_NE(ip4, nullptr, "Error-- no Ipv4 object");
598 Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol();
599 NS_TEST_ASSERT_MSG_NE(routing4, nullptr, "Error-- no Ipv4 routing protocol object");
600 Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject<Ipv4GlobalRouting>();
601 NS_TEST_ASSERT_MSG_NE(globalRouting4, nullptr, "Error-- no Ipv4GlobalRouting object");
602
603 Ipv4RoutingTableEntry* route = nullptr;
604 // n0
605 // Test that the right number of routes found
606 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
607 NS_LOG_DEBUG("BridgeTest nRoutes0 " << nRoutes0);
608 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 3, "Error-- not three entries");
609 for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
610 {
611 route = globalRouting0->GetRoute(i);
612 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
613 }
614 // Spot check the last route
615 if (route)
616 {
618 Ipv4Address("10.1.3.0"),
619 "Error-- wrong destination");
621 Ipv4Address("10.1.1.2"),
622 "Error-- wrong gateway");
623 }
624
625 // n1
626 // Test that the right number of routes found
627 route = nullptr;
628 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
629 NS_LOG_DEBUG("BridgeTest nRoutes1 " << nRoutes1);
630 NS_TEST_ASSERT_MSG_EQ(nRoutes1, 3, "Error-- not three entries");
631 for (uint32_t i = 0; i < globalRouting1->GetNRoutes(); i++)
632 {
633 route = globalRouting1->GetRoute(i);
634 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
635 }
636 // Spot check the last route
637 if (route)
638 {
640 Ipv4Address("10.1.3.0"),
641 "Error-- wrong destination");
643 Ipv4Address("10.1.2.2"),
644 "Error-- wrong gateway");
645 }
646
647 // skip n2 and n3, just verify n4
648 NS_LOG_DEBUG("BridgeTest skip print out of n2 and n3, go next to node n4");
649
650 // n4
651 route = nullptr;
652 // Test that the right number of routes found
653 uint32_t nRoutes4 = globalRouting4->GetNRoutes();
654 NS_LOG_DEBUG("BridgeTest nRoutes4 " << nRoutes4);
655 NS_TEST_ASSERT_MSG_EQ(nRoutes4, 3, "Error-- not three entries");
656 for (uint32_t i = 0; i < globalRouting4->GetNRoutes(); i++)
657 {
658 route = globalRouting4->GetRoute(i);
659 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
660 }
661 // Spot check the last route
662 if (route)
663 {
665 Ipv4Address("10.1.1.0"),
666 "Error-- wrong destination");
668 Ipv4Address("10.1.3.1"),
669 "Error-- wrong gateway");
670 }
671
673}
674
675/**
676 * \ingroup internet-test
677 *
678 * \brief IPv4 GlobalRouting Two bridges test
679 */
681{
682 public:
683 void DoSetup() override;
684 void DoRun() override;
686
687 private:
688 NodeContainer m_nodes; //!< Nodes used in the test.
689};
690
692 : TestCase("Global routing across two bridges")
693{
694}
695
696void
698{
699 m_nodes.Create(5);
700
701 // connect node0 to node1
703 SimpleNetDeviceHelper simpleHelper;
704 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
705 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
706
707 NetDeviceContainer bridgeFacingDevices;
708 NetDeviceContainer switchn2Devices;
709 NetDeviceContainer switchn3Devices;
710
711 // connect node1 to node2 (switch)
713 SimpleNetDeviceHelper simpleHelper2;
714 NetDeviceContainer net2 = simpleHelper2.Install(m_nodes.Get(1), channel2);
715 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
716 bridgeFacingDevices.Add(net2.Get(0));
717 switchn2Devices.Add(net2.Get(1));
718
719 // connect node2 (switch) to node3
721 SimpleNetDeviceHelper simpleHelper3;
722 NetDeviceContainer net3 = simpleHelper3.Install(m_nodes.Get(2), channel3);
723 net3.Add(simpleHelper3.Install(m_nodes.Get(3), channel3));
724 switchn2Devices.Add(net3.Get(0));
725 switchn3Devices.Add(net3.Get(1));
726
727 // connect node3 to node4
729 SimpleNetDeviceHelper simpleHelper4;
730 NetDeviceContainer net4 = simpleHelper4.Install(m_nodes.Get(3), channel4);
731 net4.Add(simpleHelper4.Install(m_nodes.Get(4), channel4));
732 switchn3Devices.Add(net4.Get(0));
733 bridgeFacingDevices.Add(net4.Get(1));
734
735 Ptr<Node> switchn2Node = m_nodes.Get(2);
736 BridgeHelper bridgen2Helper;
737 bridgen2Helper.Install(switchn2Node, switchn2Devices);
738
739 Ptr<Node> switchn3Node = m_nodes.Get(3);
740 BridgeHelper bridgen3Helper;
741 bridgen3Helper.Install(switchn3Node, switchn3Devices);
742
743 InternetStackHelper internet;
744 // By default, InternetStackHelper adds a static and global routing
745 // implementation. We just want the global for this test.
746 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
747 internet.SetRoutingHelper(ipv4RoutingHelper);
748
749 internet.Install(m_nodes.Get(0));
750 internet.Install(m_nodes.Get(1));
751 // m_nodes.Get (2) is bridge node
752 // m_nodes.Get (3) is bridge node
753 internet.Install(m_nodes.Get(4));
754
755 Ipv4AddressHelper address;
756 address.SetBase("10.1.1.0", "255.255.255.0");
757 address.Assign(net);
758
759 address.SetBase("10.1.2.0", "255.255.255.0");
760 address.Assign(bridgeFacingDevices);
761}
762
763void
765{
767
769 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
770 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
771 NS_TEST_ASSERT_MSG_NE(routing0, nullptr, "Error-- no Ipv4 routing protocol object");
772 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
773 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
774
775 // Skip to n4
777 NS_TEST_ASSERT_MSG_NE(ip4, nullptr, "Error-- no Ipv4 object");
778 Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol();
779 NS_TEST_ASSERT_MSG_NE(routing4, nullptr, "Error-- no Ipv4 routing protocol object");
780 Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject<Ipv4GlobalRouting>();
781 NS_TEST_ASSERT_MSG_NE(globalRouting4, nullptr, "Error-- no Ipv4GlobalRouting object");
782
783 Ipv4RoutingTableEntry* route = nullptr;
784 // n0
785 // Test that the right number of routes found
786 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
787 NS_LOG_DEBUG("BridgeTest nRoutes0 " << nRoutes0);
788 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 2, "Error-- not two entries");
789 for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
790 {
791 route = globalRouting0->GetRoute(i);
792 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
793 }
794 // Spot check the last route
795 if (route)
796 {
798 Ipv4Address("10.1.2.0"),
799 "Error-- wrong destination");
801 Ipv4Address("10.1.1.2"),
802 "Error-- wrong gateway");
803 }
804 // skip n2 and n3, just verify n4
805 NS_LOG_DEBUG("BridgeTest skip print out of n1-n3, go next to node n4");
806
807 // n4
808 // Test that the right number of routes found
809 route = nullptr;
810 uint32_t nRoutes4 = globalRouting4->GetNRoutes();
811 NS_LOG_DEBUG("BridgeTest nRoutes4 " << nRoutes4);
812 NS_TEST_ASSERT_MSG_EQ(nRoutes4, 2, "Error-- not two entries");
813 for (uint32_t i = 0; i < globalRouting4->GetNRoutes(); i++)
814 {
815 route = globalRouting4->GetRoute(i);
816 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
817 }
818 // Spot check the last route
819 if (route)
820 {
822 Ipv4Address("10.1.1.0"),
823 "Error-- wrong destination");
825 Ipv4Address("10.1.2.1"),
826 "Error-- wrong gateway");
827 }
828
830}
831
832/**
833 * \ingroup internet-test
834 *
835 * \brief IPv4 Dynamic GlobalRouting test
836 */
838{
839 public:
842
843 private:
844 /**
845 * \brief Send some data
846 * \param index Index of the socket to use.
847 */
848 void SendData(uint8_t index);
849
850 /**
851 * \brief Shutdown a socket
852 * \param index Index of the socket to close.
853 */
854 void ShutDownSock(uint8_t index);
855
856 /**
857 * Handle an incoming packet
858 * \param socket The input socket.
859 */
860 void HandleRead(Ptr<Socket> socket);
861 void DoRun() override;
862
863 uint16_t m_count; //!< Number of packets received.
864 std::vector<std::pair<Ptr<Socket>, bool>> m_sendSocks; //!< Sending sockets.
865 DataRate m_dataRate; //!< Data rate.
866 uint16_t m_packetSize; //!< Packet size.
867 std::vector<uint8_t>
868 m_firstInterface; //!< Packets received on the 1st interface at a given time.
869 std::vector<uint8_t>
870 m_secondInterface; //!< Packets received on the 2nd interface at a given time.
871};
872
873// Add some help text to this case to describe what it is intended to test
875 : TestCase("Dynamic global routing example"),
876 m_count(0)
877{
878 m_firstInterface.resize(16);
879 m_secondInterface.resize(16);
880 m_dataRate = DataRate("2kbps");
881 m_packetSize = 50;
882}
883
885{
886 for (auto iter = m_sendSocks.begin(); iter != m_sendSocks.end(); iter++)
887 {
888 if (iter->second)
889 {
890 iter->second = false;
891 iter->first->Close();
892 iter->first = nullptr;
893 }
894 }
895}
896
897void
899{
900 Ptr<Packet> packet;
901 Address from;
902 while ((packet = socket->RecvFrom(from)))
903 {
904 if (packet->GetSize() == 0)
905 { // EOF
906 break;
907 }
909 bool found;
910 found = packet->PeekPacketTag(tag);
911 uint8_t now = static_cast<uint8_t>(Simulator::Now().GetSeconds());
912 if (found)
913 {
914 if (tag.GetRecvIf() == 1)
915 {
916 m_firstInterface[now]++;
917 }
918 if (tag.GetRecvIf() == 2)
919 {
920 m_secondInterface[now]++;
921 }
922 m_count++;
923 }
924 }
925}
926
927void
929{
930 if (!m_sendSocks[index].second)
931 {
932 return;
933 }
935 m_sendSocks[index].first->Send(packet);
936
937 Time tNext(MicroSeconds(m_packetSize * 8 * 1e6 / m_dataRate.GetBitRate()));
939}
940
941void
943{
944 m_sendSocks[index].second = false;
945 m_sendSocks[index].first->Close();
946 m_sendSocks[index].first = nullptr;
947}
948
949// Test derived from examples/routing/dynamic-global-routing.cc
950//
951// Network topology
952//
953// n0
954// \ p-p
955// \ (shared csma/cd)
956// n2 -------------------------n3
957// / | |
958// / p-p n4 n5 ---------- n6
959// n1 p-p
960// | |
961// ----------------------------------------
962// p-p
963//
964// Test that for node n6, the interface facing n5 receives packets at
965// times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
966// facing n1 receives packets at times (2-4), (6-8), (12-13)
967//
968void
970{
971 // The below value configures the default behavior of global routing.
972 // By default, it is disabled. To respond to interface events, set to true
973 Config::SetDefault("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue(true));
974
976 c.Create(7);
977 NodeContainer n0n2 = NodeContainer(c.Get(0), c.Get(2));
978 NodeContainer n1n2 = NodeContainer(c.Get(1), c.Get(2));
979 NodeContainer n5n6 = NodeContainer(c.Get(5), c.Get(6));
980 NodeContainer n1n6 = NodeContainer(c.Get(1), c.Get(6));
981 NodeContainer n2345 = NodeContainer(c.Get(2), c.Get(3), c.Get(4), c.Get(5));
982
983 InternetStackHelper internet;
984 internet.Install(c);
985
986 // We create the channels first without any IP addressing information
987 SimpleNetDeviceHelper devHelper;
988
989 devHelper.SetNetDevicePointToPointMode(true);
990 NetDeviceContainer d0d2 = devHelper.Install(n0n2);
991 devHelper.SetNetDevicePointToPointMode(false);
992
993 NetDeviceContainer d1d6 = devHelper.Install(n1n6);
994 NetDeviceContainer d1d2 = devHelper.Install(n1n2);
995 NetDeviceContainer d5d6 = devHelper.Install(n5n6);
996 NetDeviceContainer d2345 = devHelper.Install(n2345);
997
998 // Later, we add IP addresses.
1000 ipv4.SetBase("10.1.1.0", "255.255.255.0");
1001 ipv4.Assign(d0d2);
1002
1003 ipv4.SetBase("10.1.2.0", "255.255.255.0");
1004 ipv4.Assign(d1d2);
1005
1006 ipv4.SetBase("10.1.3.0", "255.255.255.0");
1007 Ipv4InterfaceContainer i5i6 = ipv4.Assign(d5d6);
1008
1009 ipv4.SetBase("10.250.1.0", "255.255.255.0");
1010 ipv4.Assign(d2345);
1011
1012 ipv4.SetBase("172.16.1.0", "255.255.255.0");
1013 Ipv4InterfaceContainer i1i6 = ipv4.Assign(d1d6);
1014
1015 // Create router nodes, initialize routing database and set up the routing
1016 // tables in the nodes.
1018
1019 // Create the applications to send UDP datagrams of size
1020 // 50 bytes at a rate of 2 Kb/s
1021 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
1022 uint16_t port = 9; // Discard port (RFC 863)
1023
1024 std::pair<Ptr<Socket>, bool> sendSockA;
1025 sendSockA.first = Socket::CreateSocket(c.Get(1), tid);
1026 sendSockA.first->Bind();
1027 sendSockA.first->Connect(InetSocketAddress(i5i6.GetAddress(1), port));
1028 sendSockA.second = true;
1029 m_sendSocks.push_back(sendSockA);
1032
1033 std::pair<Ptr<Socket>, bool> sendSockB;
1034 sendSockB.first = Socket::CreateSocket(c.Get(1), tid);
1035 sendSockB.first->Bind();
1036 sendSockB.first->Connect(InetSocketAddress(i1i6.GetAddress(1), port));
1037 sendSockB.second = true;
1038 m_sendSocks.push_back(sendSockB);
1041
1042 // Create an optional packet sink to receive these packets
1043 Ptr<Socket> sink2 = Socket::CreateSocket(c.Get(6), tid);
1045 sink2->Listen();
1046 sink2->ShutdownSend();
1047
1048 sink2->SetRecvPktInfo(true);
1049 sink2->SetRecvCallback(MakeCallback(&Ipv4DynamicGlobalRoutingTestCase::HandleRead, this));
1050
1051 Ptr<Node> n1 = c.Get(1);
1052 Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4>();
1053 // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1054 // then the next p2p is numbered 2
1055 uint32_t ipv4ifIndex1 = 2;
1056
1057 Simulator::Schedule(Seconds(2), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
1058 Simulator::Schedule(Seconds(4), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
1059
1060 Ptr<Node> n6 = c.Get(6);
1061 Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4>();
1062 // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1063 // then the next p2p is numbered 2
1064 uint32_t ipv4ifIndex6 = 2;
1065 Simulator::Schedule(Seconds(6), &Ipv4::SetDown, ipv46, ipv4ifIndex6);
1066 Simulator::Schedule(Seconds(8), &Ipv4::SetUp, ipv46, ipv4ifIndex6);
1067
1068 Simulator::Schedule(Seconds(12), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
1069 Simulator::Schedule(Seconds(14), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
1070
1072
1073 NS_TEST_ASSERT_MSG_EQ(m_count, 70, "Dynamic global routing did not deliver all packets");
1074 // Test that for node n6, the interface facing n5 receives packets at
1075 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
1076 // facing n1 receives packets at times (2-4), (6-8), (12-13)
1078 5,
1079 "Dynamic global routing did not deliver all packets "
1080 << int(m_firstInterface[1]));
1082 5,
1083 "Dynamic global routing did not deliver all packets "
1084 << int(m_secondInterface[2]));
1086 5,
1087 "Dynamic global routing did not deliver all packets "
1088 << int(m_secondInterface[3]));
1090 5,
1091 "Dynamic global routing did not deliver all packets "
1092 << int(m_firstInterface[4]));
1094 5,
1095 "Dynamic global routing did not deliver all packets "
1096 << int(m_firstInterface[5]));
1098 5,
1099 "Dynamic global routing did not deliver all packets "
1100 << int(m_secondInterface[6]));
1102 5,
1103 "Dynamic global routing did not deliver all packets "
1104 << int(m_secondInterface[7]));
1106 5,
1107 "Dynamic global routing did not deliver all packets "
1108 << int(m_firstInterface[8]));
1110 5,
1111 "Dynamic global routing did not deliver all packets "
1112 << int(m_firstInterface[9]));
1114 0,
1115 "Dynamic global routing did not deliver all packets "
1116 << int(m_firstInterface[10]));
1118 5,
1119 "Dynamic global routing did not deliver all packets "
1120 << int(m_firstInterface[11]));
1122 5,
1123 "Dynamic global routing did not deliver all packets "
1124 << int(m_secondInterface[12]));
1126 5,
1127 "Dynamic global routing did not deliver all packets "
1128 << int(m_secondInterface[13]));
1130 5,
1131 "Dynamic global routing did not deliver all packets "
1132 << int(m_firstInterface[14]));
1134 5,
1135 "Dynamic global routing did not deliver all packets "
1136 << int(m_firstInterface[15]));
1138}
1139
1140/**
1141 * \ingroup internet-test
1142 *
1143 * \brief IPv4 Dynamic GlobalRouting /32 test
1144 */
1146{
1147 public:
1150
1151 Ptr<Packet> m_receivedPacket; //!< number of received packets
1152
1153 /**
1154 * \brief Receive a packet.
1155 * \param socket The receiving socket.
1156 */
1157 void ReceivePkt(Ptr<Socket> socket);
1158 /**
1159 * \brief Send a packet.
1160 * \param socket The sending socket.
1161 * \param to The address of the receiver.
1162 */
1163 void DoSendData(Ptr<Socket> socket, std::string to);
1164 /**
1165 * \brief Send a packet.
1166 * \param socket The sending socket.
1167 * \param to The address of the receiver.
1168 */
1169 void SendData(Ptr<Socket> socket, std::string to);
1170
1171 private:
1172 void DoRun() override;
1173};
1174
1175// Add some help text to this case to describe what it is intended to test
1177 : TestCase("Slash 32 global routing example")
1178{
1179}
1180
1184
1185void
1187{
1188 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
1189 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
1190 NS_TEST_ASSERT_MSG_EQ(availableData,
1192 "Received packet size is not equal to Rx buffer size");
1193}
1194
1195void
1197{
1198 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
1199 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
1200}
1201
1202void
1204{
1206 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
1207 Seconds(60),
1209 this,
1210 socket,
1211 to);
1214}
1215
1216// Test program for this 3-router scenario, using global routing
1217//
1218// (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
1219//
1220void
1222{
1226
1227 NodeContainer c = NodeContainer(nA, nB, nC);
1228
1229 InternetStackHelper internet;
1230 internet.Install(c);
1231
1232 // simple links
1233 NodeContainer nAnB = NodeContainer(nA, nB);
1234 NodeContainer nBnC = NodeContainer(nB, nC);
1235
1236 SimpleNetDeviceHelper devHelper;
1237
1239 deviceA->SetAddress(Mac48Address::Allocate());
1240 nA->AddDevice(deviceA);
1241
1242 NetDeviceContainer dAdB = devHelper.Install(nAnB);
1243 NetDeviceContainer dBdC = devHelper.Install(nBnC);
1244
1246 deviceC->SetAddress(Mac48Address::Allocate());
1247 nC->AddDevice(deviceC);
1248
1249 // Later, we add IP addresses.
1250 Ipv4AddressHelper ipv4;
1251 ipv4.SetBase("10.1.1.0", "255.255.255.252");
1252 Ipv4InterfaceContainer iAiB = ipv4.Assign(dAdB);
1253
1254 ipv4.SetBase("10.1.1.4", "255.255.255.252");
1255 Ipv4InterfaceContainer iBiC = ipv4.Assign(dBdC);
1256
1257 Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4>();
1258 Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4>();
1259 Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4>();
1260
1261 int32_t ifIndexA = ipv4A->AddInterface(deviceA);
1262 int32_t ifIndexC = ipv4C->AddInterface(deviceC);
1263
1264 Ipv4InterfaceAddress ifInAddrA =
1265 Ipv4InterfaceAddress(Ipv4Address("172.16.1.1"), Ipv4Mask("/32"));
1266 ipv4A->AddAddress(ifIndexA, ifInAddrA);
1267 ipv4A->SetMetric(ifIndexA, 1);
1268 ipv4A->SetUp(ifIndexA);
1269
1270 Ipv4InterfaceAddress ifInAddrC =
1271 Ipv4InterfaceAddress(Ipv4Address("192.168.1.1"), Ipv4Mask("/32"));
1272 ipv4C->AddAddress(ifIndexC, ifInAddrC);
1273 ipv4C->SetMetric(ifIndexC, 1);
1274 ipv4C->SetUp(ifIndexC);
1275
1276 // Create router nodes, initialize routing database and set up the routing
1277 // tables in the nodes.
1279
1280 // Create the UDP sockets
1281 Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory>();
1282 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
1283 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("192.168.1.1"), 1234)),
1284 0,
1285 "trivial");
1286 rxSocket->SetRecvCallback(MakeCallback(&Ipv4GlobalRoutingSlash32TestCase::ReceivePkt, this));
1287
1288 Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory>();
1289 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
1290 txSocket->SetAllowBroadcast(true);
1291
1292 // ------ Now the tests ------------
1293
1294 // Unicast test
1295 SendData(txSocket, "192.168.1.1");
1297 123,
1298 "Static routing with /32 did not deliver all packets.");
1299
1301}
1302
1303/**
1304 * \ingroup internet-test
1305 *
1306 * \brief IPv4 GlobalRouting TestSuite
1307 */
1309{
1310 public:
1312};
1313
1315 : TestSuite("ipv4-global-routing", Type::UNIT)
1316{
1317 AddTestCase(new LinkTest, TestCase::Duration::QUICK);
1318 AddTestCase(new LanTest, TestCase::Duration::QUICK);
1319 AddTestCase(new TwoLinkTest, TestCase::Duration::QUICK);
1320 AddTestCase(new TwoLanTest, TestCase::Duration::QUICK);
1321 AddTestCase(new BridgeTest, TestCase::Duration::QUICK);
1322 AddTestCase(new TwoBridgeTest, TestCase::Duration::QUICK);
1323 AddTestCase(new Ipv4DynamicGlobalRoutingTestCase, TestCase::Duration::QUICK);
1324 AddTestCase(new Ipv4GlobalRoutingSlash32TestCase, TestCase::Duration::QUICK);
1325}
1326
1328 g_globalRoutingTestSuite; //!< Static variable for test initialization
NodeContainer n1n2
Nodecontainer n1 + n2.
NodeContainer n0n2
Nodecontainer n0 + n2.
IPv4 GlobalRouting Bridge test.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
NodeContainer m_nodes
Nodes used in the test.
void DoRun() override
Implementation to actually run this TestCase.
std::vector< uint8_t > m_secondInterface
Packets received on the 2nd interface at a given time.
void HandleRead(Ptr< Socket > socket)
Handle an incoming packet.
uint16_t m_count
Number of packets received.
std::vector< std::pair< Ptr< Socket >, bool > > m_sendSocks
Sending sockets.
std::vector< uint8_t > m_firstInterface
Packets received on the 1st interface at a given time.
void ShutDownSock(uint8_t index)
Shutdown a socket.
void SendData(uint8_t index)
Send some data.
IPv4 Dynamic GlobalRouting /32 test.
void ReceivePkt(Ptr< Socket > socket)
Receive a packet.
void SendData(Ptr< Socket > socket, std::string to)
Send a packet.
void DoRun() override
Implementation to actually run this TestCase.
void DoSendData(Ptr< Socket > socket, std::string to)
Send a packet.
Ptr< Packet > m_receivedPacket
number of received packets
IPv4 GlobalRouting LAN test.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
NodeContainer m_nodes
Nodes used in the test.
IPv4 GlobalRouting Two bridges test.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
NodeContainer m_nodes
Nodes used in the test.
IPv4 GlobalRouting Two LAN test.
NodeContainer m_nodes
Nodes used in the test.
void DoRun() override
Implementation to actually run this TestCase.
void DoSetup() override
Implementation to do any local setup required for this TestCase.
a polymophic address class
Definition address.h:90
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Class for representing data rates.
Definition data-rate.h:78
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition data-rate.cc:234
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()
Helper class that adds ns3::Ipv4GlobalRouting objects.
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
Global routing protocol for IPv4 stacks.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
virtual void SetUp(uint32_t interface)=0
virtual void SetDown(uint32_t interface)=0
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
Implement the IPv4 layer.
a class to represent an Ipv4 address mask
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
uint32_t GetRecvIf() const
Get the tag's receiving interface.
A record of an IPv4 routing table entry for Ipv4GlobalRouting and Ipv4StaticRouting.
Ipv4Address GetDest() const
Ipv4Address GetGateway() const
static Mac48Address Allocate()
Allocate a new Mac48Address.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
Smart pointer class similar to boost::intrusive_ptr.
build a set of SimpleNetDevice objects
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
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 ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
API to create UDP socket instances.
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:134
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:241
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition test.h:554
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1332
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
static Ipv4GlobalRoutingTestSuite g_globalRoutingTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:684