A Discrete-Event Network Simulator
API
ipv4-global-routing-test-suite.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16#include "ns3/boolean.h"
17#include "ns3/bridge-helper.h"
18#include "ns3/config.h"
19#include "ns3/inet-socket-address.h"
20#include "ns3/internet-stack-helper.h"
21#include "ns3/ipv4-address-helper.h"
22#include "ns3/ipv4-global-routing-helper.h"
23#include "ns3/ipv4-global-routing.h"
24#include "ns3/ipv4-l3-protocol.h"
25#include "ns3/ipv4-packet-info-tag.h"
26#include "ns3/ipv4-routing-protocol.h"
27#include "ns3/ipv4-routing-table-entry.h"
28#include "ns3/ipv4-static-routing-helper.h"
29#include "ns3/log.h"
30#include "ns3/node-container.h"
31#include "ns3/node.h"
32#include "ns3/packet.h"
33#include "ns3/pointer.h"
34#include "ns3/simple-channel.h"
35#include "ns3/simple-net-device-helper.h"
36#include "ns3/simple-net-device.h"
37#include "ns3/simulator.h"
38#include "ns3/socket-factory.h"
39#include "ns3/string.h"
40#include "ns3/test.h"
41#include "ns3/udp-socket-factory.h"
42#include "ns3/uinteger.h"
43
44#include <vector>
45
46using namespace ns3;
47
48NS_LOG_COMPONENT_DEFINE("Ipv4GlobalRoutingTestSuite");
49
50// This test suite tests the operation of global routing on a few sample
51// networks to ensure that routes are built correctly
52//
53// Link test:
54// n0 <--------> n1 (point-to-point link)
55// 10.1.1.1 10.1.1.2
56// Expected routes:
57// n0: route to 0.0.0.0 gw 10.1.1.2
58// n1: route to 0.0.0.0 gw 10.1.1.1
59// Note: These default routes to 0.0.0.0 are generated by the extension
60// in the global route manager to install default routes via the
61// peer node on a point-to-point link, when the node is on a
62// stub link
63//
64// LAN test:
65// n0 <--------> n1 (broadcast link on subnet 10.1.1.0/24)
66// Expected routes:
67// n0: route to 10.1.1.0 gw 0.0.0.0
68// n1: route to 10.1.1.0 gw 0.0.0.0
69// Two link test:
70// n0 <--------> n1 <--------> n2 (point-to-point links)
71// 10.1.1.1 10.1.1.2/ 10.1.2.2
72// 10.1.2.1
73// Expected routes:
74// n0: route to 0.0.0.0 gw 10.1.1.2
75// n1: route to 10.1.1.1 gw 10.1.1.1
76// route to 10.1.2.2 gw 10.1.2.2
77// route to 10.1.1.0 gw 10.1.1.1
78// route to 10.1.2.0 gw 10.1.2.2
79// n2: route to 0.0.0.0 gw 10.1.2.1
80// Note: These default routes to 0.0.0.0 are generated by the extension
81// in the global route manager to install default routes via the
82// peer node on a point-to-point link, when the node is on a
83// stub link
84// Two LANs test:
85// n0 <--------> n1 <--------> n2 (broadcast links)
86// Expected routes:
87// n0: route to 10.1.1.0 gw 0.0.0.0
88// route to 0.0.0.0 gw 10.1.1.2
89// n1: route to 10.1.1.1 gw 10.1.1.1
90// route to 10.1.2.2 gw 10.1.2.2
91// route to 10.1.1.0 gw 10.1.1.1
92// route to 10.1.2.0 gw 10.1.2.2
93// n2: route to 0.0.0.0 gw 10.1.2.1
94// Bridge test:
95// n0 <--------> n1 <---> Bridge-n2 <---> n3 <-------> n4 (broadcast links)
96// 10.1.1.0/24 10.1.2.0/24 10.1.3.0/24
97// Expected routes:
98// n0: route to 10.1.1.0 gw 0.0.0.0
99// route to 10.1.2.0 gw 10.1.1.2
100// route to 10.1.3.0 gw 10.1.1.2
101// n1: route to 10.1.1.0 gw 0.0.0.0
102// route to 10.1.2.0 gw 0.0.0.0
103// route to 10.1.3.0 gw 10.1.2.2
104// n3: route to 10.1.1.0 gw 10.1.2.1
105// route to 10.1.2.0 gw 0.0.0.0
106// route to 10.1.3.0 gw 0.0.0.0
107// n4: route to 10.1.3.0 gw 0.0.0.0
108// route to 10.1.2.0 gw 10.1.3.1
109// route to 10.1.1.0 gw 10.1.3.1
110// Two Bridge test:
111// n0 <------> n1 <---> Bridge-n2 <---> Bridge-n3 <---> n4 (broadcast links)
112// 10.1.1.0/24 10.1.2.0/24
113// Expected routes:
114// n0: route to 10.1.1.0 gw 0.0.0.0
115// route to 10.1.2.0 gw 10.1.1.2
116// n4: route to 10.1.2.0 gw 0.0.0.0
117// route to 10.1.1.0 gw 10.1.2.1
118
125class LinkTest : public TestCase
126{
127 public:
128 void DoSetup() override;
129 void DoRun() override;
130 LinkTest();
131
132 private:
134};
135
137 : TestCase("Global routing on point-to-point link")
138{
139}
140
141void
143{
144 m_nodes.Create(2);
145
146 Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
147 SimpleNetDeviceHelper simpleHelper;
148 simpleHelper.SetNetDevicePointToPointMode(true);
149 NetDeviceContainer net = simpleHelper.Install(m_nodes, channel);
150
151 InternetStackHelper internet;
152 // By default, InternetStackHelper adds a static and global routing
153 // implementation. We just want the global for this test.
154 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
155 internet.SetRoutingHelper(ipv4RoutingHelper);
156 internet.Install(m_nodes);
157
159 ipv4.SetBase("10.1.1.0", "255.255.255.252");
160 Ipv4InterfaceContainer i = ipv4.Assign(net);
161}
162
163void
165{
166 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
167
169 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
171 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
172 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
173 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
174 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
175 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
176 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
177 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
178
179 // Test that the right number of routes found
180 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
181 NS_LOG_DEBUG("LinkTest nRoutes0 " << nRoutes0);
182 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- not one route");
183 Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
184 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
185 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
186 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
187
188 // Test that the right number of routes found
189 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
190 NS_TEST_ASSERT_MSG_EQ(nRoutes1, 1, "Error-- not one route");
191 NS_LOG_DEBUG("LinkTest nRoutes1 " << nRoutes1);
192 route = globalRouting1->GetRoute(0);
193 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
194 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
195 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
196
197 bool result = true;
198
199 NS_TEST_ASSERT_MSG_EQ(result, true, "Message");
200 Simulator::Run();
201 Simulator::Destroy();
202}
203
210class LanTest : public TestCase
211{
212 public:
213 void DoSetup() override;
214 void DoRun() override;
215 LanTest();
216
217 private:
219};
220
222 : TestCase("Global routing on broadcast link")
223{
224}
225
226void
228{
229 m_nodes.Create(2);
230
231 Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
232 SimpleNetDeviceHelper simpleHelper;
233 NetDeviceContainer net = simpleHelper.Install(m_nodes, channel);
234
235 InternetStackHelper internet;
236 // By default, InternetStackHelper adds a static and global routing
237 // implementation. We just want the global for this test.
238 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
239 internet.SetRoutingHelper(ipv4RoutingHelper);
240 internet.Install(m_nodes);
241
243 ipv4.SetBase("10.1.1.0", "255.255.255.0");
244 Ipv4InterfaceContainer i = ipv4.Assign(net);
245}
246
247void
249{
250 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
251
253 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
255 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
256 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
257 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
258 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
259 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
260 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
261 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
262
263 // Test that the right number of routes found
264 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
265 NS_LOG_DEBUG("LanTest nRoutes0 " << nRoutes0);
266 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- more than one entry");
267 for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
268 {
269 Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(i);
270 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
271 }
272
273 // Test that the right number of routes found
274 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
275 NS_LOG_DEBUG("LanTest nRoutes1 " << nRoutes1);
276 NS_TEST_ASSERT_MSG_EQ(nRoutes1, 1, "Error-- more than one entry");
277 for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
278 {
279 Ipv4RoutingTableEntry* route = globalRouting1->GetRoute(i);
280 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
281 }
282
283 Simulator::Destroy();
284}
285
292class TwoLinkTest : public TestCase
293{
294 public:
295 void DoSetup() override;
296 void DoRun() override;
297 TwoLinkTest();
298
299 private:
301};
302
304 : TestCase("Global routing across two hops (point-to-point links)")
305{
306}
307
308void
310{
311 m_nodes.Create(3);
312
313 Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
314 SimpleNetDeviceHelper simpleHelper;
315 simpleHelper.SetNetDevicePointToPointMode(true);
316 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
317 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
318
319 Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
320 SimpleNetDeviceHelper simpleHelper2;
321 simpleHelper2.SetNetDevicePointToPointMode(true);
322 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
323 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
324
325 InternetStackHelper internet;
326 // By default, InternetStackHelper adds a static and global routing
327 // implementation. We just want the global for this test.
328 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
329 internet.SetRoutingHelper(ipv4RoutingHelper);
330 internet.Install(m_nodes);
331
333 ipv4.SetBase("10.1.1.0", "255.255.255.252");
334 Ipv4InterfaceContainer i = ipv4.Assign(net);
335 ipv4.SetBase("10.1.2.0", "255.255.255.252");
336 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
337}
338
339void
341{
342 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
343
345 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
347 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
349 NS_TEST_ASSERT_MSG_NE(ip2, nullptr, "Error-- no Ipv4 object");
350 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
351 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
352 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
353 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
354 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
355 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
356 Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol();
357 Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject<Ipv4GlobalRouting>();
358 NS_TEST_ASSERT_MSG_NE(globalRouting2, nullptr, "Error-- no Ipv4GlobalRouting object");
359
360 // node n0
361 // Test that the right number of routes found
362 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
363 NS_LOG_DEBUG("TwoLinkTest nRoutes0 " << nRoutes0);
364 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- wrong number of links");
365
366 Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
367 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
368 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
369 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
370
371 // node n1
372 // Test that the right number of routes found
373 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
374 NS_LOG_DEBUG("TwoLinkTest nRoutes1 " << nRoutes1);
375 route = globalRouting1->GetRoute(0);
376 NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
377 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.1"), "Error-- wrong destination");
378 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
379 route = globalRouting1->GetRoute(1);
380 NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
381 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.2"), "Error-- wrong destination");
382 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.2"), "Error-- wrong gateway");
383 route = globalRouting1->GetRoute(2);
384 NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
385 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
386 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
387 route = globalRouting1->GetRoute(3);
388 NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
389 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
390 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.2"), "Error-- wrong gateway");
391
392 // node n2
393 // Test that the right number of routes found
394 uint32_t nRoutes2 = globalRouting2->GetNRoutes();
395 NS_LOG_DEBUG("TwoLinkTest nRoutes2 " << nRoutes2);
396 NS_TEST_ASSERT_MSG_EQ(nRoutes2, 1, "Error-- wrong number of links");
397
398 route = globalRouting2->GetRoute(0);
399 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
400 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
401 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.1"), "Error-- wrong gateway");
402
403 Simulator::Destroy();
404}
405
412class TwoLanTest : public TestCase
413{
414 public:
415 void DoSetup() override;
416 void DoRun() override;
417 TwoLanTest();
418
419 private:
421};
422
424 : TestCase("Global routing across two hops (broadcast links)")
425{
426}
427
428void
430{
431 m_nodes.Create(3);
432
433 Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
434 SimpleNetDeviceHelper simpleHelper;
435 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
436 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
437
438 Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
439 SimpleNetDeviceHelper simpleHelper2;
440 NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
441 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
442
443 InternetStackHelper internet;
444 // By default, InternetStackHelper adds a static and global routing
445 // implementation. We just want the global for this test.
446 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
447 internet.SetRoutingHelper(ipv4RoutingHelper);
448 internet.Install(m_nodes);
449
451 ipv4.SetBase("10.1.1.0", "255.255.255.0");
452 Ipv4InterfaceContainer i = ipv4.Assign(net);
453 ipv4.SetBase("10.1.2.0", "255.255.255.0");
454 Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
455}
456
457void
459{
460 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
461
463 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
465 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
467 NS_TEST_ASSERT_MSG_NE(ip2, nullptr, "Error-- no Ipv4 object");
468 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
469 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
470 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
471 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
472 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
473 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
474 Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol();
475 Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject<Ipv4GlobalRouting>();
476 NS_TEST_ASSERT_MSG_NE(globalRouting2, nullptr, "Error-- no Ipv4GlobalRouting object");
477
478 // Test that the right number of routes found
479 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
480 NS_LOG_DEBUG("TwoLanTest nRoutes0 " << nRoutes0);
481 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 2, "Error-- not two entries");
482 Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
483 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
484 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
485 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
486 route = globalRouting0->GetRoute(1);
487 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
488 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
489 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
490
491 // Test that the right number of routes found
492 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
493 NS_LOG_DEBUG("TwoLanTest nRoutes1 " << nRoutes1);
494 NS_TEST_ASSERT_MSG_EQ(nRoutes1, 2, "Error-- not two entries");
495 route = globalRouting1->GetRoute(0);
496 NS_LOG_DEBUG("TwoLanTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
497 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
498 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
499 route = globalRouting1->GetRoute(1);
500 NS_LOG_DEBUG("TwoLanTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
501 NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
502 NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
503
504 Simulator::Destroy();
505}
506
513class BridgeTest : public TestCase
514{
515 public:
516 void DoSetup() override;
517 void DoRun() override;
518 BridgeTest();
519
520 private:
522};
523
525 : TestCase("Global routing across bridging topology (bug 2102)")
526{
527}
528
529void
531{
532 m_nodes.Create(5);
533
534 // connect node0 to node1
535 Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
536 SimpleNetDeviceHelper simpleHelper;
537 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
538 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
539
540 NetDeviceContainer bridgeFacingDevices;
541 NetDeviceContainer switchDevices;
542
543 // connect node1 to node2 (switch)
544 Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
545 SimpleNetDeviceHelper simpleHelper2;
546 NetDeviceContainer net2 = simpleHelper2.Install(m_nodes.Get(1), channel2);
547 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
548 bridgeFacingDevices.Add(net2.Get(0));
549 switchDevices.Add(net2.Get(1));
550
551 // connect node2 (switch) to node3
552 Ptr<SimpleChannel> channel3 = CreateObject<SimpleChannel>();
553 SimpleNetDeviceHelper simpleHelper3;
554 NetDeviceContainer net3 = simpleHelper3.Install(m_nodes.Get(2), channel3);
555 net3.Add(simpleHelper3.Install(m_nodes.Get(3), channel3));
556 bridgeFacingDevices.Add(net3.Get(1));
557 switchDevices.Add(net3.Get(0));
558
559 // connect node3 to node4
560 Ptr<SimpleChannel> channel4 = CreateObject<SimpleChannel>();
561 SimpleNetDeviceHelper simpleHelper4;
562 NetDeviceContainer net4 = simpleHelper4.Install(m_nodes.Get(3), channel4);
563 net4.Add(simpleHelper4.Install(m_nodes.Get(4), channel4));
564
565 Ptr<Node> switchNode = m_nodes.Get(2);
566 BridgeHelper bridge;
567 bridge.Install(switchNode, switchDevices);
568
569 InternetStackHelper internet;
570 // By default, InternetStackHelper adds a static and global routing
571 // implementation. We just want the global for this test.
572 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
573 internet.SetRoutingHelper(ipv4RoutingHelper);
574
575 internet.Install(m_nodes.Get(0));
576 internet.Install(m_nodes.Get(1));
577 // m_nodes.Get (2) is bridge node
578 internet.Install(m_nodes.Get(3));
579 internet.Install(m_nodes.Get(4));
580
582 address.SetBase("10.1.1.0", "255.255.255.0");
583 address.Assign(net);
584
585 address.SetBase("10.1.2.0", "255.255.255.0");
586 address.Assign(bridgeFacingDevices);
587
588 address.SetBase("10.1.3.0", "255.255.255.0");
589 address.Assign(net4);
590}
591
592void
594{
595 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
596
598 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
599 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
600 NS_TEST_ASSERT_MSG_NE(routing0, nullptr, "Error-- no Ipv4 routing protocol object");
601 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
602 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
603
605 NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
606 Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
607 NS_TEST_ASSERT_MSG_NE(routing1, nullptr, "Error-- no Ipv4 routing protocol object");
608 Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
609 NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
610
611 // Skip to n4
613 NS_TEST_ASSERT_MSG_NE(ip4, nullptr, "Error-- no Ipv4 object");
614 Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol();
615 NS_TEST_ASSERT_MSG_NE(routing4, nullptr, "Error-- no Ipv4 routing protocol object");
616 Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject<Ipv4GlobalRouting>();
617 NS_TEST_ASSERT_MSG_NE(globalRouting4, nullptr, "Error-- no Ipv4GlobalRouting object");
618
619 Ipv4RoutingTableEntry* route = nullptr;
620 // n0
621 // Test that the right number of routes found
622 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
623 NS_LOG_DEBUG("BridgeTest nRoutes0 " << nRoutes0);
624 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 3, "Error-- not three entries");
625 for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
626 {
627 route = globalRouting0->GetRoute(i);
628 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
629 }
630 // Spot check the last route
631 if (route)
632 {
634 Ipv4Address("10.1.3.0"),
635 "Error-- wrong destination");
637 Ipv4Address("10.1.1.2"),
638 "Error-- wrong gateway");
639 }
640
641 // n1
642 // Test that the right number of routes found
643 route = nullptr;
644 uint32_t nRoutes1 = globalRouting1->GetNRoutes();
645 NS_LOG_DEBUG("BridgeTest nRoutes1 " << nRoutes1);
646 NS_TEST_ASSERT_MSG_EQ(nRoutes1, 3, "Error-- not three entries");
647 for (uint32_t i = 0; i < globalRouting1->GetNRoutes(); i++)
648 {
649 route = globalRouting1->GetRoute(i);
650 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
651 }
652 // Spot check the last route
653 if (route)
654 {
656 Ipv4Address("10.1.3.0"),
657 "Error-- wrong destination");
659 Ipv4Address("10.1.2.2"),
660 "Error-- wrong gateway");
661 }
662
663 // skip n2 and n3, just verify n4
664 NS_LOG_DEBUG("BridgeTest skip print out of n2 and n3, go next to node n4");
665
666 // n4
667 route = nullptr;
668 // Test that the right number of routes found
669 uint32_t nRoutes4 = globalRouting4->GetNRoutes();
670 NS_LOG_DEBUG("BridgeTest nRoutes4 " << nRoutes4);
671 NS_TEST_ASSERT_MSG_EQ(nRoutes4, 3, "Error-- not three entries");
672 for (uint32_t i = 0; i < globalRouting4->GetNRoutes(); i++)
673 {
674 route = globalRouting4->GetRoute(i);
675 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
676 }
677 // Spot check the last route
678 if (route)
679 {
681 Ipv4Address("10.1.1.0"),
682 "Error-- wrong destination");
684 Ipv4Address("10.1.3.1"),
685 "Error-- wrong gateway");
686 }
687
688 Simulator::Destroy();
689}
690
698{
699 public:
700 void DoSetup() override;
701 void DoRun() override;
703
704 private:
706};
707
709 : TestCase("Global routing across two bridges")
710{
711}
712
713void
715{
716 m_nodes.Create(5);
717
718 // connect node0 to node1
719 Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
720 SimpleNetDeviceHelper simpleHelper;
721 NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
722 net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
723
724 NetDeviceContainer bridgeFacingDevices;
725 NetDeviceContainer switchn2Devices;
726 NetDeviceContainer switchn3Devices;
727
728 // connect node1 to node2 (switch)
729 Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
730 SimpleNetDeviceHelper simpleHelper2;
731 NetDeviceContainer net2 = simpleHelper2.Install(m_nodes.Get(1), channel2);
732 net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
733 bridgeFacingDevices.Add(net2.Get(0));
734 switchn2Devices.Add(net2.Get(1));
735
736 // connect node2 (switch) to node3
737 Ptr<SimpleChannel> channel3 = CreateObject<SimpleChannel>();
738 SimpleNetDeviceHelper simpleHelper3;
739 NetDeviceContainer net3 = simpleHelper3.Install(m_nodes.Get(2), channel3);
740 net3.Add(simpleHelper3.Install(m_nodes.Get(3), channel3));
741 switchn2Devices.Add(net3.Get(0));
742 switchn3Devices.Add(net3.Get(1));
743
744 // connect node3 to node4
745 Ptr<SimpleChannel> channel4 = CreateObject<SimpleChannel>();
746 SimpleNetDeviceHelper simpleHelper4;
747 NetDeviceContainer net4 = simpleHelper4.Install(m_nodes.Get(3), channel4);
748 net4.Add(simpleHelper4.Install(m_nodes.Get(4), channel4));
749 switchn3Devices.Add(net4.Get(0));
750 bridgeFacingDevices.Add(net4.Get(1));
751
752 Ptr<Node> switchn2Node = m_nodes.Get(2);
753 BridgeHelper bridgen2Helper;
754 bridgen2Helper.Install(switchn2Node, switchn2Devices);
755
756 Ptr<Node> switchn3Node = m_nodes.Get(3);
757 BridgeHelper bridgen3Helper;
758 bridgen3Helper.Install(switchn3Node, switchn3Devices);
759
760 InternetStackHelper internet;
761 // By default, InternetStackHelper adds a static and global routing
762 // implementation. We just want the global for this test.
763 Ipv4GlobalRoutingHelper ipv4RoutingHelper;
764 internet.SetRoutingHelper(ipv4RoutingHelper);
765
766 internet.Install(m_nodes.Get(0));
767 internet.Install(m_nodes.Get(1));
768 // m_nodes.Get (2) is bridge node
769 // m_nodes.Get (3) is bridge node
770 internet.Install(m_nodes.Get(4));
771
773 address.SetBase("10.1.1.0", "255.255.255.0");
774 address.Assign(net);
775
776 address.SetBase("10.1.2.0", "255.255.255.0");
777 address.Assign(bridgeFacingDevices);
778}
779
780void
782{
783 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
784
786 NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
787 Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
788 NS_TEST_ASSERT_MSG_NE(routing0, nullptr, "Error-- no Ipv4 routing protocol object");
789 Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
790 NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
791
792 // Skip to n4
794 NS_TEST_ASSERT_MSG_NE(ip4, nullptr, "Error-- no Ipv4 object");
795 Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol();
796 NS_TEST_ASSERT_MSG_NE(routing4, nullptr, "Error-- no Ipv4 routing protocol object");
797 Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject<Ipv4GlobalRouting>();
798 NS_TEST_ASSERT_MSG_NE(globalRouting4, nullptr, "Error-- no Ipv4GlobalRouting object");
799
800 Ipv4RoutingTableEntry* route = nullptr;
801 // n0
802 // Test that the right number of routes found
803 uint32_t nRoutes0 = globalRouting0->GetNRoutes();
804 NS_LOG_DEBUG("BridgeTest nRoutes0 " << nRoutes0);
805 NS_TEST_ASSERT_MSG_EQ(nRoutes0, 2, "Error-- not two entries");
806 for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
807 {
808 route = globalRouting0->GetRoute(i);
809 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
810 }
811 // Spot check the last route
812 if (route)
813 {
815 Ipv4Address("10.1.2.0"),
816 "Error-- wrong destination");
818 Ipv4Address("10.1.1.2"),
819 "Error-- wrong gateway");
820 }
821 // skip n2 and n3, just verify n4
822 NS_LOG_DEBUG("BridgeTest skip print out of n1-n3, go next to node n4");
823
824 // n4
825 // Test that the right number of routes found
826 route = nullptr;
827 uint32_t nRoutes4 = globalRouting4->GetNRoutes();
828 NS_LOG_DEBUG("BridgeTest nRoutes4 " << nRoutes4);
829 NS_TEST_ASSERT_MSG_EQ(nRoutes4, 2, "Error-- not two entries");
830 for (uint32_t i = 0; i < globalRouting4->GetNRoutes(); i++)
831 {
832 route = globalRouting4->GetRoute(i);
833 NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
834 }
835 // Spot check the last route
836 if (route)
837 {
839 Ipv4Address("10.1.1.0"),
840 "Error-- wrong destination");
842 Ipv4Address("10.1.2.1"),
843 "Error-- wrong gateway");
844 }
845
846 Simulator::Destroy();
847}
848
856{
857 public:
860
861 private:
866 void SendData(uint8_t index);
867
872 void ShutDownSock(uint8_t index);
873
878 void HandleRead(Ptr<Socket> socket);
879 void DoRun() override;
880
881 uint16_t m_count;
882 std::vector<std::pair<Ptr<Socket>, bool>> m_sendSocks;
884 uint16_t m_packetSize;
885 std::vector<uint8_t>
887 std::vector<uint8_t>
889};
890
891// Add some help text to this case to describe what it is intended to test
893 : TestCase("Dynamic global routing example"),
894 m_count(0)
895{
896 m_firstInterface.resize(16);
897 m_secondInterface.resize(16);
898 m_dataRate = DataRate("2kbps");
899 m_packetSize = 50;
900}
901
903{
904 std::vector<std::pair<Ptr<Socket>, bool>>::iterator iter;
905
906 for (iter = m_sendSocks.begin(); iter != m_sendSocks.end(); iter++)
907 {
908 if (iter->second)
909 {
910 iter->second = false;
911 iter->first->Close();
912 iter->first = nullptr;
913 }
914 }
915}
916
917void
919{
920 Ptr<Packet> packet;
921 Address from;
922 while ((packet = socket->RecvFrom(from)))
923 {
924 if (packet->GetSize() == 0)
925 { // EOF
926 break;
927 }
929 bool found;
930 found = packet->PeekPacketTag(tag);
931 uint8_t now = static_cast<uint8_t>(Simulator::Now().GetSeconds());
932 if (found)
933 {
934 if (tag.GetRecvIf() == 1)
935 {
936 m_firstInterface[now]++;
937 }
938 if (tag.GetRecvIf() == 2)
939 {
940 m_secondInterface[now]++;
941 }
942 m_count++;
943 }
944 }
945}
946
947void
949{
950 if (m_sendSocks[index].second == false)
951 {
952 return;
953 }
954 Ptr<Packet> packet = Create<Packet>(m_packetSize);
955 m_sendSocks[index].first->Send(packet);
956
957 Time tNext(MicroSeconds(m_packetSize * 8 * 1e6 / m_dataRate.GetBitRate()));
958 Simulator::Schedule(tNext, &Ipv4DynamicGlobalRoutingTestCase::SendData, this, index);
959}
960
961void
963{
964 m_sendSocks[index].second = false;
965 m_sendSocks[index].first->Close();
966 m_sendSocks[index].first = nullptr;
967}
968
969// Test derived from examples/routing/dynamic-global-routing.cc
970//
971// Network topology
972//
973// n0
974// \ p-p
975// \ (shared csma/cd)
976// n2 -------------------------n3
977// / | |
978// / p-p n4 n5 ---------- n6
979// n1 p-p
980// | |
981// ----------------------------------------
982// p-p
983//
984// Test that for node n6, the interface facing n5 receives packets at
985// times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
986// facing n1 receives packets at times (2-4), (6-8), (12-13)
987//
988void
990{
991 // The below value configures the default behavior of global routing.
992 // By default, it is disabled. To respond to interface events, set to true
993 Config::SetDefault("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue(true));
994
996 c.Create(7);
997 NodeContainer n0n2 = NodeContainer(c.Get(0), c.Get(2));
998 NodeContainer n1n2 = NodeContainer(c.Get(1), c.Get(2));
999 NodeContainer n5n6 = NodeContainer(c.Get(5), c.Get(6));
1000 NodeContainer n1n6 = NodeContainer(c.Get(1), c.Get(6));
1001 NodeContainer n2345 = NodeContainer(c.Get(2), c.Get(3), c.Get(4), c.Get(5));
1002
1003 InternetStackHelper internet;
1004 internet.Install(c);
1005
1006 // We create the channels first without any IP addressing information
1007 SimpleNetDeviceHelper devHelper;
1008
1009 devHelper.SetNetDevicePointToPointMode(true);
1010 NetDeviceContainer d0d2 = devHelper.Install(n0n2);
1011 devHelper.SetNetDevicePointToPointMode(false);
1012
1013 NetDeviceContainer d1d6 = devHelper.Install(n1n6);
1014 NetDeviceContainer d1d2 = devHelper.Install(n1n2);
1015 NetDeviceContainer d5d6 = devHelper.Install(n5n6);
1016 NetDeviceContainer d2345 = devHelper.Install(n2345);
1017
1018 // Later, we add IP addresses.
1019 Ipv4AddressHelper ipv4;
1020 ipv4.SetBase("10.1.1.0", "255.255.255.0");
1021 ipv4.Assign(d0d2);
1022
1023 ipv4.SetBase("10.1.2.0", "255.255.255.0");
1024 ipv4.Assign(d1d2);
1025
1026 ipv4.SetBase("10.1.3.0", "255.255.255.0");
1027 Ipv4InterfaceContainer i5i6 = ipv4.Assign(d5d6);
1028
1029 ipv4.SetBase("10.250.1.0", "255.255.255.0");
1030 ipv4.Assign(d2345);
1031
1032 ipv4.SetBase("172.16.1.0", "255.255.255.0");
1033 Ipv4InterfaceContainer i1i6 = ipv4.Assign(d1d6);
1034
1035 // Create router nodes, initialize routing database and set up the routing
1036 // tables in the nodes.
1037 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
1038
1039 // Create the applications to send UDP datagrams of size
1040 // 50 bytes at a rate of 2 Kb/s
1041 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
1042 uint16_t port = 9; // Discard port (RFC 863)
1043
1044 std::pair<Ptr<Socket>, bool> sendSockA;
1045 sendSockA.first = Socket::CreateSocket(c.Get(1), tid);
1046 sendSockA.first->Bind();
1047 sendSockA.first->Connect(InetSocketAddress(i5i6.GetAddress(1), port));
1048 sendSockA.second = true;
1049 m_sendSocks.push_back(sendSockA);
1050 Simulator::Schedule(Seconds(1.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 0);
1051 Simulator::Schedule(Seconds(10.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 0);
1052
1053 std::pair<Ptr<Socket>, bool> sendSockB;
1054 sendSockB.first = Socket::CreateSocket(c.Get(1), tid);
1055 sendSockB.first->Bind();
1056 sendSockB.first->Connect(InetSocketAddress(i1i6.GetAddress(1), port));
1057 sendSockB.second = true;
1058 m_sendSocks.push_back(sendSockB);
1059 Simulator::Schedule(Seconds(11.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 1);
1060 Simulator::Schedule(Seconds(16.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 1);
1061
1062 // Create an optional packet sink to receive these packets
1063 Ptr<Socket> sink2 = Socket::CreateSocket(c.Get(6), tid);
1064 sink2->Bind(Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
1065 sink2->Listen();
1066 sink2->ShutdownSend();
1067
1068 sink2->SetRecvPktInfo(true);
1070
1071 Ptr<Node> n1 = c.Get(1);
1072 Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4>();
1073 // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1074 // then the next p2p is numbered 2
1075 uint32_t ipv4ifIndex1 = 2;
1076
1077 Simulator::Schedule(Seconds(2), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
1078 Simulator::Schedule(Seconds(4), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
1079
1080 Ptr<Node> n6 = c.Get(6);
1081 Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4>();
1082 // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1083 // then the next p2p is numbered 2
1084 uint32_t ipv4ifIndex6 = 2;
1085 Simulator::Schedule(Seconds(6), &Ipv4::SetDown, ipv46, ipv4ifIndex6);
1086 Simulator::Schedule(Seconds(8), &Ipv4::SetUp, ipv46, ipv4ifIndex6);
1087
1088 Simulator::Schedule(Seconds(12), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
1089 Simulator::Schedule(Seconds(14), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
1090
1091 Simulator::Run();
1092
1093 NS_TEST_ASSERT_MSG_EQ(m_count, 70, "Dynamic global routing did not deliver all packets");
1094 // Test that for node n6, the interface facing n5 receives packets at
1095 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
1096 // facing n1 receives packets at times (2-4), (6-8), (12-13)
1098 5,
1099 "Dynamic global routing did not deliver all packets "
1100 << int(m_firstInterface[1]));
1102 5,
1103 "Dynamic global routing did not deliver all packets "
1104 << int(m_secondInterface[2]));
1106 5,
1107 "Dynamic global routing did not deliver all packets "
1108 << int(m_secondInterface[3]));
1110 5,
1111 "Dynamic global routing did not deliver all packets "
1112 << int(m_firstInterface[4]));
1114 5,
1115 "Dynamic global routing did not deliver all packets "
1116 << int(m_firstInterface[5]));
1118 5,
1119 "Dynamic global routing did not deliver all packets "
1120 << int(m_secondInterface[6]));
1122 5,
1123 "Dynamic global routing did not deliver all packets "
1124 << int(m_secondInterface[7]));
1126 5,
1127 "Dynamic global routing did not deliver all packets "
1128 << int(m_firstInterface[8]));
1130 5,
1131 "Dynamic global routing did not deliver all packets "
1132 << int(m_firstInterface[9]));
1134 0,
1135 "Dynamic global routing did not deliver all packets "
1136 << int(m_firstInterface[10]));
1138 5,
1139 "Dynamic global routing did not deliver all packets "
1140 << int(m_firstInterface[11]));
1142 5,
1143 "Dynamic global routing did not deliver all packets "
1144 << int(m_secondInterface[12]));
1146 5,
1147 "Dynamic global routing did not deliver all packets "
1148 << int(m_secondInterface[13]));
1150 5,
1151 "Dynamic global routing did not deliver all packets "
1152 << int(m_firstInterface[14]));
1154 5,
1155 "Dynamic global routing did not deliver all packets "
1156 << int(m_firstInterface[15]));
1157 Simulator::Destroy();
1158}
1159
1167{
1168 public:
1171
1173
1178 void ReceivePkt(Ptr<Socket> socket);
1184 void DoSendData(Ptr<Socket> socket, std::string to);
1190 void SendData(Ptr<Socket> socket, std::string to);
1191
1192 private:
1193 void DoRun() override;
1194};
1195
1196// Add some help text to this case to describe what it is intended to test
1198 : TestCase("Slash 32 global routing example")
1199{
1200}
1201
1203{
1204}
1205
1206void
1208{
1209 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
1211 NS_TEST_ASSERT_MSG_EQ(availableData,
1213 "Received packet size is not equal to Rx buffer size");
1214}
1215
1216void
1218{
1219 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
1220 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
1221}
1222
1223void
1225{
1226 m_receivedPacket = Create<Packet>();
1227 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
1228 Seconds(60),
1230 this,
1231 socket,
1232 to);
1233 Simulator::Stop(Seconds(66));
1234 Simulator::Run();
1235}
1236
1237// Test program for this 3-router scenario, using global routing
1238//
1239// (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
1240//
1241void
1243{
1244 Ptr<Node> nA = CreateObject<Node>();
1245 Ptr<Node> nB = CreateObject<Node>();
1246 Ptr<Node> nC = CreateObject<Node>();
1247
1248 NodeContainer c = NodeContainer(nA, nB, nC);
1249
1250 InternetStackHelper internet;
1251 internet.Install(c);
1252
1253 // simple links
1254 NodeContainer nAnB = NodeContainer(nA, nB);
1255 NodeContainer nBnC = NodeContainer(nB, nC);
1256
1257 SimpleNetDeviceHelper devHelper;
1258
1259 Ptr<SimpleNetDevice> deviceA = CreateObject<SimpleNetDevice>();
1260 deviceA->SetAddress(Mac48Address::Allocate());
1261 nA->AddDevice(deviceA);
1262
1263 NetDeviceContainer dAdB = devHelper.Install(nAnB);
1264 NetDeviceContainer dBdC = devHelper.Install(nBnC);
1265
1266 Ptr<SimpleNetDevice> deviceC = CreateObject<SimpleNetDevice>();
1267 deviceC->SetAddress(Mac48Address::Allocate());
1268 nC->AddDevice(deviceC);
1269
1270 // Later, we add IP addresses.
1271 Ipv4AddressHelper ipv4;
1272 ipv4.SetBase("10.1.1.0", "255.255.255.252");
1273 Ipv4InterfaceContainer iAiB = ipv4.Assign(dAdB);
1274
1275 ipv4.SetBase("10.1.1.4", "255.255.255.252");
1276 Ipv4InterfaceContainer iBiC = ipv4.Assign(dBdC);
1277
1278 Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4>();
1279 Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4>();
1280 Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4>();
1281
1282 int32_t ifIndexA = ipv4A->AddInterface(deviceA);
1283 int32_t ifIndexC = ipv4C->AddInterface(deviceC);
1284
1285 Ipv4InterfaceAddress ifInAddrA =
1286 Ipv4InterfaceAddress(Ipv4Address("172.16.1.1"), Ipv4Mask("/32"));
1287 ipv4A->AddAddress(ifIndexA, ifInAddrA);
1288 ipv4A->SetMetric(ifIndexA, 1);
1289 ipv4A->SetUp(ifIndexA);
1290
1291 Ipv4InterfaceAddress ifInAddrC =
1292 Ipv4InterfaceAddress(Ipv4Address("192.168.1.1"), Ipv4Mask("/32"));
1293 ipv4C->AddAddress(ifIndexC, ifInAddrC);
1294 ipv4C->SetMetric(ifIndexC, 1);
1295 ipv4C->SetUp(ifIndexC);
1296
1297 // Create router nodes, initialize routing database and set up the routing
1298 // tables in the nodes.
1299 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
1300
1301 // Create the UDP sockets
1302 Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory>();
1303 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
1304 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("192.168.1.1"), 1234)),
1305 0,
1306 "trivial");
1307 rxSocket->SetRecvCallback(MakeCallback(&Ipv4GlobalRoutingSlash32TestCase::ReceivePkt, this));
1308
1309 Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory>();
1310 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
1311 txSocket->SetAllowBroadcast(true);
1312
1313 // ------ Now the tests ------------
1314
1315 // Unicast test
1316 SendData(txSocket, "192.168.1.1");
1318 123,
1319 "Static routing with /32 did not deliver all packets.");
1320
1321 Simulator::Destroy();
1322}
1323
1331{
1332 public:
1334};
1335
1337 : TestSuite("ipv4-global-routing", UNIT)
1338{
1339 AddTestCase(new LinkTest, TestCase::QUICK);
1340 AddTestCase(new LanTest, TestCase::QUICK);
1341 AddTestCase(new TwoLinkTest, TestCase::QUICK);
1342 AddTestCase(new TwoLanTest, TestCase::QUICK);
1343 AddTestCase(new BridgeTest, TestCase::QUICK);
1344 AddTestCase(new TwoBridgeTest, TestCase::QUICK);
1345 AddTestCase(new Ipv4DynamicGlobalRoutingTestCase, TestCase::QUICK);
1346 AddTestCase(new Ipv4GlobalRoutingSlash32TestCase, TestCase::QUICK);
1347}
1348
#define max(a, b)
Definition: 80211b.c:43
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:92
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
Definition: bridge-helper.h:45
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:90
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition: data-rate.cc:305
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
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:43
Helper class that adds ns3::Ipv4GlobalRouting objects.
Global routing protocol for IPv4 stacks.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
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
Definition: ipv4-address.h:258
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
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.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t GetId() const
Definition: node.cc:117
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:1002
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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...
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:352
virtual uint32_t GetRxAvailable() const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
virtual int ShutdownSend()=0
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
virtual int Listen()=0
Listen for incoming connections.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
a unique identifier for an interface.
Definition: type-id.h:60
API to create UDP socket instances.
uint16_t port
Definition: dsdv-manet.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
#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:144
#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:251
#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:564
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1362
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
static Ipv4GlobalRoutingTestSuite g_globalRoutingTestSuite
Static variable for test initialization.
address
Definition: first.py:40
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:691
Definition: second.py:1
channel
Definition: third.py:81
void SetUp(char *deviceName)