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 
46 using namespace ns3;
47 
48 NS_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 
124 class LinkTest : public TestCase
125 {
126  public:
127  void DoSetup() override;
128  void DoRun() override;
129  LinkTest();
130 
131  private:
133 };
134 
136  : TestCase("Global routing on point-to-point link")
137 {
138 }
139 
140 void
142 {
143  m_nodes.Create(2);
144 
145  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
146  SimpleNetDeviceHelper simpleHelper;
147  simpleHelper.SetNetDevicePointToPointMode(true);
148  NetDeviceContainer net = simpleHelper.Install(m_nodes, channel);
149 
151  // By default, InternetStackHelper adds a static and global routing
152  // implementation. We just want the global for this test.
153  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
154  internet.SetRoutingHelper(ipv4RoutingHelper);
155  internet.Install(m_nodes);
156 
158  ipv4.SetBase("10.1.1.0", "255.255.255.252");
159  Ipv4InterfaceContainer i = ipv4.Assign(net);
160 }
161 
162 void
164 {
165  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
166 
168  NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
170  NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
171  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
172  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
173  NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
174  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
175  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
176  NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
177 
178  // Test that the right number of routes found
179  uint32_t nRoutes0 = globalRouting0->GetNRoutes();
180  NS_LOG_DEBUG("LinkTest nRoutes0 " << nRoutes0);
181  NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- not one route");
182  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
183  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
184  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
185  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
186 
187  // Test that the right number of routes found
188  uint32_t nRoutes1 = globalRouting1->GetNRoutes();
189  NS_TEST_ASSERT_MSG_EQ(nRoutes1, 1, "Error-- not one route");
190  NS_LOG_DEBUG("LinkTest nRoutes1 " << nRoutes1);
191  route = globalRouting1->GetRoute(0);
192  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
193  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
194  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
195 
196  bool result = true;
197 
198  NS_TEST_ASSERT_MSG_EQ(result, true, "Message");
199  Simulator::Run();
200  Simulator::Destroy();
201 }
202 
208 class LanTest : public TestCase
209 {
210  public:
211  void DoSetup() override;
212  void DoRun() override;
213  LanTest();
214 
215  private:
217 };
218 
220  : TestCase("Global routing on broadcast link")
221 {
222 }
223 
224 void
226 {
227  m_nodes.Create(2);
228 
229  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
230  SimpleNetDeviceHelper simpleHelper;
231  NetDeviceContainer net = simpleHelper.Install(m_nodes, channel);
232 
234  // By default, InternetStackHelper adds a static and global routing
235  // implementation. We just want the global for this test.
236  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
237  internet.SetRoutingHelper(ipv4RoutingHelper);
238  internet.Install(m_nodes);
239 
241  ipv4.SetBase("10.1.1.0", "255.255.255.0");
242  Ipv4InterfaceContainer i = ipv4.Assign(net);
243 }
244 
245 void
247 {
248  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
249 
251  NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
253  NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
254  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
255  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
256  NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
257  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
258  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
259  NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
260 
261  // Test that the right number of routes found
262  uint32_t nRoutes0 = globalRouting0->GetNRoutes();
263  NS_LOG_DEBUG("LanTest nRoutes0 " << nRoutes0);
264  NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- more than one entry");
265  for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
266  {
267  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(i);
268  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
269  }
270 
271  // Test that the right number of routes found
272  uint32_t nRoutes1 = globalRouting1->GetNRoutes();
273  NS_LOG_DEBUG("LanTest nRoutes1 " << nRoutes1);
274  NS_TEST_ASSERT_MSG_EQ(nRoutes1, 1, "Error-- more than one entry");
275  for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
276  {
277  Ipv4RoutingTableEntry* route = globalRouting1->GetRoute(i);
278  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
279  }
280 
281  Simulator::Destroy();
282 }
283 
289 class TwoLinkTest : public TestCase
290 {
291  public:
292  void DoSetup() override;
293  void DoRun() override;
294  TwoLinkTest();
295 
296  private:
298 };
299 
301  : TestCase("Global routing across two hops (point-to-point links)")
302 {
303 }
304 
305 void
307 {
308  m_nodes.Create(3);
309 
310  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
311  SimpleNetDeviceHelper simpleHelper;
312  simpleHelper.SetNetDevicePointToPointMode(true);
313  NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
314  net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
315 
316  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
317  SimpleNetDeviceHelper simpleHelper2;
318  simpleHelper2.SetNetDevicePointToPointMode(true);
319  NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
320  net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
321 
323  // By default, InternetStackHelper adds a static and global routing
324  // implementation. We just want the global for this test.
325  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
326  internet.SetRoutingHelper(ipv4RoutingHelper);
327  internet.Install(m_nodes);
328 
330  ipv4.SetBase("10.1.1.0", "255.255.255.252");
331  Ipv4InterfaceContainer i = ipv4.Assign(net);
332  ipv4.SetBase("10.1.2.0", "255.255.255.252");
333  Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
334 }
335 
336 void
338 {
339  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
340 
342  NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
344  NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
346  NS_TEST_ASSERT_MSG_NE(ip2, nullptr, "Error-- no Ipv4 object");
347  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
348  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
349  NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
350  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
351  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
352  NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
353  Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol();
354  Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject<Ipv4GlobalRouting>();
355  NS_TEST_ASSERT_MSG_NE(globalRouting2, nullptr, "Error-- no Ipv4GlobalRouting object");
356 
357  // node n0
358  // Test that the right number of routes found
359  uint32_t nRoutes0 = globalRouting0->GetNRoutes();
360  NS_LOG_DEBUG("TwoLinkTest nRoutes0 " << nRoutes0);
361  NS_TEST_ASSERT_MSG_EQ(nRoutes0, 1, "Error-- wrong number of links");
362 
363  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
364  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
365  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
366  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
367 
368  // node n1
369  // Test that the right number of routes found
370  uint32_t nRoutes1 = globalRouting1->GetNRoutes();
371  NS_LOG_DEBUG("TwoLinkTest nRoutes1 " << nRoutes1);
372  route = globalRouting1->GetRoute(0);
373  NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
374  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.1"), "Error-- wrong destination");
375  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
376  route = globalRouting1->GetRoute(1);
377  NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
378  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.2"), "Error-- wrong destination");
379  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.2"), "Error-- wrong gateway");
380  route = globalRouting1->GetRoute(2);
381  NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
382  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
383  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.1"), "Error-- wrong gateway");
384  route = globalRouting1->GetRoute(3);
385  NS_LOG_DEBUG("TwoLinkTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
386  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
387  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.2"), "Error-- wrong gateway");
388 
389  // node n2
390  // Test that the right number of routes found
391  uint32_t nRoutes2 = globalRouting2->GetNRoutes();
392  NS_LOG_DEBUG("TwoLinkTest nRoutes2 " << nRoutes2);
393  NS_TEST_ASSERT_MSG_EQ(nRoutes2, 1, "Error-- wrong number of links");
394 
395  route = globalRouting2->GetRoute(0);
396  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
397  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("0.0.0.0"), "Error-- wrong destination");
398  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.2.1"), "Error-- wrong gateway");
399 
400  Simulator::Destroy();
401 }
402 
408 class TwoLanTest : public TestCase
409 {
410  public:
411  void DoSetup() override;
412  void DoRun() override;
413  TwoLanTest();
414 
415  private:
417 };
418 
420  : TestCase("Global routing across two hops (broadcast links)")
421 {
422 }
423 
424 void
426 {
427  m_nodes.Create(3);
428 
429  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
430  SimpleNetDeviceHelper simpleHelper;
431  NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
432  net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
433 
434  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
435  SimpleNetDeviceHelper simpleHelper2;
436  NetDeviceContainer net2 = simpleHelper.Install(m_nodes.Get(1), channel2);
437  net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
438 
440  // By default, InternetStackHelper adds a static and global routing
441  // implementation. We just want the global for this test.
442  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
443  internet.SetRoutingHelper(ipv4RoutingHelper);
444  internet.Install(m_nodes);
445 
447  ipv4.SetBase("10.1.1.0", "255.255.255.0");
448  Ipv4InterfaceContainer i = ipv4.Assign(net);
449  ipv4.SetBase("10.1.2.0", "255.255.255.0");
450  Ipv4InterfaceContainer i2 = ipv4.Assign(net2);
451 }
452 
453 void
455 {
456  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
457 
459  NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
461  NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
463  NS_TEST_ASSERT_MSG_NE(ip2, nullptr, "Error-- no Ipv4 object");
464  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
465  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
466  NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
467  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
468  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
469  NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
470  Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol();
471  Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject<Ipv4GlobalRouting>();
472  NS_TEST_ASSERT_MSG_NE(globalRouting2, nullptr, "Error-- no Ipv4GlobalRouting object");
473 
474  // Test that the right number of routes found
475  uint32_t nRoutes0 = globalRouting0->GetNRoutes();
476  NS_LOG_DEBUG("TwoLanTest nRoutes0 " << nRoutes0);
477  NS_TEST_ASSERT_MSG_EQ(nRoutes0, 2, "Error-- not two entries");
478  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute(0);
479  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
480  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
481  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
482  route = globalRouting0->GetRoute(1);
483  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
484  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
485  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("10.1.1.2"), "Error-- wrong gateway");
486 
487  // Test that the right number of routes found
488  uint32_t nRoutes1 = globalRouting1->GetNRoutes();
489  NS_LOG_DEBUG("TwoLanTest nRoutes1 " << nRoutes1);
490  NS_TEST_ASSERT_MSG_EQ(nRoutes1, 2, "Error-- not two entries");
491  route = globalRouting1->GetRoute(0);
492  NS_LOG_DEBUG("TwoLanTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
493  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.1.0"), "Error-- wrong destination");
494  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
495  route = globalRouting1->GetRoute(1);
496  NS_LOG_DEBUG("TwoLanTest entry dest " << route->GetDest() << " gw " << route->GetGateway());
497  NS_TEST_ASSERT_MSG_EQ(route->GetDest(), Ipv4Address("10.1.2.0"), "Error-- wrong destination");
498  NS_TEST_ASSERT_MSG_EQ(route->GetGateway(), Ipv4Address("0.0.0.0"), "Error-- wrong gateway");
499 
500  Simulator::Destroy();
501 }
502 
508 class BridgeTest : public TestCase
509 {
510  public:
511  void DoSetup() override;
512  void DoRun() override;
513  BridgeTest();
514 
515  private:
517 };
518 
520  : TestCase("Global routing across bridging topology (bug 2102)")
521 {
522 }
523 
524 void
526 {
527  m_nodes.Create(5);
528 
529  // connect node0 to node1
530  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
531  SimpleNetDeviceHelper simpleHelper;
532  NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
533  net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
534 
535  NetDeviceContainer bridgeFacingDevices;
537 
538  // connect node1 to node2 (switch)
539  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
540  SimpleNetDeviceHelper simpleHelper2;
541  NetDeviceContainer net2 = simpleHelper2.Install(m_nodes.Get(1), channel2);
542  net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
543  bridgeFacingDevices.Add(net2.Get(0));
544  switchDevices.Add(net2.Get(1));
545 
546  // connect node2 (switch) to node3
547  Ptr<SimpleChannel> channel3 = CreateObject<SimpleChannel>();
548  SimpleNetDeviceHelper simpleHelper3;
549  NetDeviceContainer net3 = simpleHelper3.Install(m_nodes.Get(2), channel3);
550  net3.Add(simpleHelper3.Install(m_nodes.Get(3), channel3));
551  bridgeFacingDevices.Add(net3.Get(1));
552  switchDevices.Add(net3.Get(0));
553 
554  // connect node3 to node4
555  Ptr<SimpleChannel> channel4 = CreateObject<SimpleChannel>();
556  SimpleNetDeviceHelper simpleHelper4;
557  NetDeviceContainer net4 = simpleHelper4.Install(m_nodes.Get(3), channel4);
558  net4.Add(simpleHelper4.Install(m_nodes.Get(4), channel4));
559 
561  BridgeHelper bridge;
563 
565  // By default, InternetStackHelper adds a static and global routing
566  // implementation. We just want the global for this test.
567  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
568  internet.SetRoutingHelper(ipv4RoutingHelper);
569 
570  internet.Install(m_nodes.Get(0));
571  internet.Install(m_nodes.Get(1));
572  // m_nodes.Get (2) is bridge node
573  internet.Install(m_nodes.Get(3));
574  internet.Install(m_nodes.Get(4));
575 
577  address.SetBase("10.1.1.0", "255.255.255.0");
578  address.Assign(net);
579 
580  address.SetBase("10.1.2.0", "255.255.255.0");
581  address.Assign(bridgeFacingDevices);
582 
583  address.SetBase("10.1.3.0", "255.255.255.0");
584  address.Assign(net4);
585 }
586 
587 void
589 {
590  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
591 
593  NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
594  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
595  NS_TEST_ASSERT_MSG_NE(routing0, nullptr, "Error-- no Ipv4 routing protocol object");
596  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
597  NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
598 
600  NS_TEST_ASSERT_MSG_NE(ip1, nullptr, "Error-- no Ipv4 object");
601  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol();
602  NS_TEST_ASSERT_MSG_NE(routing1, nullptr, "Error-- no Ipv4 routing protocol object");
603  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject<Ipv4GlobalRouting>();
604  NS_TEST_ASSERT_MSG_NE(globalRouting1, nullptr, "Error-- no Ipv4GlobalRouting object");
605 
606  // Skip to n4
608  NS_TEST_ASSERT_MSG_NE(ip4, nullptr, "Error-- no Ipv4 object");
609  Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol();
610  NS_TEST_ASSERT_MSG_NE(routing4, nullptr, "Error-- no Ipv4 routing protocol object");
611  Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject<Ipv4GlobalRouting>();
612  NS_TEST_ASSERT_MSG_NE(globalRouting4, nullptr, "Error-- no Ipv4GlobalRouting object");
613 
614  Ipv4RoutingTableEntry* route = nullptr;
615  // n0
616  // Test that the right number of routes found
617  uint32_t nRoutes0 = globalRouting0->GetNRoutes();
618  NS_LOG_DEBUG("BridgeTest nRoutes0 " << nRoutes0);
619  NS_TEST_ASSERT_MSG_EQ(nRoutes0, 3, "Error-- not three entries");
620  for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
621  {
622  route = globalRouting0->GetRoute(i);
623  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
624  }
625  // Spot check the last route
626  if (route)
627  {
629  Ipv4Address("10.1.3.0"),
630  "Error-- wrong destination");
632  Ipv4Address("10.1.1.2"),
633  "Error-- wrong gateway");
634  }
635 
636  // n1
637  // Test that the right number of routes found
638  route = nullptr;
639  uint32_t nRoutes1 = globalRouting1->GetNRoutes();
640  NS_LOG_DEBUG("BridgeTest nRoutes1 " << nRoutes1);
641  NS_TEST_ASSERT_MSG_EQ(nRoutes1, 3, "Error-- not three entries");
642  for (uint32_t i = 0; i < globalRouting1->GetNRoutes(); i++)
643  {
644  route = globalRouting1->GetRoute(i);
645  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
646  }
647  // Spot check the last route
648  if (route)
649  {
651  Ipv4Address("10.1.3.0"),
652  "Error-- wrong destination");
654  Ipv4Address("10.1.2.2"),
655  "Error-- wrong gateway");
656  }
657 
658  // skip n2 and n3, just verify n4
659  NS_LOG_DEBUG("BridgeTest skip print out of n2 and n3, go next to node n4");
660 
661  // n4
662  route = nullptr;
663  // Test that the right number of routes found
664  uint32_t nRoutes4 = globalRouting4->GetNRoutes();
665  NS_LOG_DEBUG("BridgeTest nRoutes4 " << nRoutes4);
666  NS_TEST_ASSERT_MSG_EQ(nRoutes4, 3, "Error-- not three entries");
667  for (uint32_t i = 0; i < globalRouting4->GetNRoutes(); i++)
668  {
669  route = globalRouting4->GetRoute(i);
670  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
671  }
672  // Spot check the last route
673  if (route)
674  {
676  Ipv4Address("10.1.1.0"),
677  "Error-- wrong destination");
679  Ipv4Address("10.1.3.1"),
680  "Error-- wrong gateway");
681  }
682 
683  Simulator::Destroy();
684 }
685 
691 class TwoBridgeTest : public TestCase
692 {
693  public:
694  void DoSetup() override;
695  void DoRun() override;
696  TwoBridgeTest();
697 
698  private:
700 };
701 
703  : TestCase("Global routing across two bridges")
704 {
705 }
706 
707 void
709 {
710  m_nodes.Create(5);
711 
712  // connect node0 to node1
713  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
714  SimpleNetDeviceHelper simpleHelper;
715  NetDeviceContainer net = simpleHelper.Install(m_nodes.Get(0), channel);
716  net.Add(simpleHelper.Install(m_nodes.Get(1), channel));
717 
718  NetDeviceContainer bridgeFacingDevices;
719  NetDeviceContainer switchn2Devices;
720  NetDeviceContainer switchn3Devices;
721 
722  // connect node1 to node2 (switch)
723  Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
724  SimpleNetDeviceHelper simpleHelper2;
725  NetDeviceContainer net2 = simpleHelper2.Install(m_nodes.Get(1), channel2);
726  net2.Add(simpleHelper2.Install(m_nodes.Get(2), channel2));
727  bridgeFacingDevices.Add(net2.Get(0));
728  switchn2Devices.Add(net2.Get(1));
729 
730  // connect node2 (switch) to node3
731  Ptr<SimpleChannel> channel3 = CreateObject<SimpleChannel>();
732  SimpleNetDeviceHelper simpleHelper3;
733  NetDeviceContainer net3 = simpleHelper3.Install(m_nodes.Get(2), channel3);
734  net3.Add(simpleHelper3.Install(m_nodes.Get(3), channel3));
735  switchn2Devices.Add(net3.Get(0));
736  switchn3Devices.Add(net3.Get(1));
737 
738  // connect node3 to node4
739  Ptr<SimpleChannel> channel4 = CreateObject<SimpleChannel>();
740  SimpleNetDeviceHelper simpleHelper4;
741  NetDeviceContainer net4 = simpleHelper4.Install(m_nodes.Get(3), channel4);
742  net4.Add(simpleHelper4.Install(m_nodes.Get(4), channel4));
743  switchn3Devices.Add(net4.Get(0));
744  bridgeFacingDevices.Add(net4.Get(1));
745 
746  Ptr<Node> switchn2Node = m_nodes.Get(2);
747  BridgeHelper bridgen2Helper;
748  bridgen2Helper.Install(switchn2Node, switchn2Devices);
749 
750  Ptr<Node> switchn3Node = m_nodes.Get(3);
751  BridgeHelper bridgen3Helper;
752  bridgen3Helper.Install(switchn3Node, switchn3Devices);
753 
755  // By default, InternetStackHelper adds a static and global routing
756  // implementation. We just want the global for this test.
757  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
758  internet.SetRoutingHelper(ipv4RoutingHelper);
759 
760  internet.Install(m_nodes.Get(0));
761  internet.Install(m_nodes.Get(1));
762  // m_nodes.Get (2) is bridge node
763  // m_nodes.Get (3) is bridge node
764  internet.Install(m_nodes.Get(4));
765 
767  address.SetBase("10.1.1.0", "255.255.255.0");
768  address.Assign(net);
769 
770  address.SetBase("10.1.2.0", "255.255.255.0");
771  address.Assign(bridgeFacingDevices);
772 }
773 
774 void
776 {
777  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
778 
780  NS_TEST_ASSERT_MSG_NE(ip0, nullptr, "Error-- no Ipv4 object");
781  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol();
782  NS_TEST_ASSERT_MSG_NE(routing0, nullptr, "Error-- no Ipv4 routing protocol object");
783  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject<Ipv4GlobalRouting>();
784  NS_TEST_ASSERT_MSG_NE(globalRouting0, nullptr, "Error-- no Ipv4GlobalRouting object");
785 
786  // Skip to n4
788  NS_TEST_ASSERT_MSG_NE(ip4, nullptr, "Error-- no Ipv4 object");
789  Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol();
790  NS_TEST_ASSERT_MSG_NE(routing4, nullptr, "Error-- no Ipv4 routing protocol object");
791  Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject<Ipv4GlobalRouting>();
792  NS_TEST_ASSERT_MSG_NE(globalRouting4, nullptr, "Error-- no Ipv4GlobalRouting object");
793 
794  Ipv4RoutingTableEntry* route = nullptr;
795  // n0
796  // Test that the right number of routes found
797  uint32_t nRoutes0 = globalRouting0->GetNRoutes();
798  NS_LOG_DEBUG("BridgeTest nRoutes0 " << nRoutes0);
799  NS_TEST_ASSERT_MSG_EQ(nRoutes0, 2, "Error-- not two entries");
800  for (uint32_t i = 0; i < globalRouting0->GetNRoutes(); i++)
801  {
802  route = globalRouting0->GetRoute(i);
803  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
804  }
805  // Spot check the last route
806  if (route)
807  {
809  Ipv4Address("10.1.2.0"),
810  "Error-- wrong destination");
812  Ipv4Address("10.1.1.2"),
813  "Error-- wrong gateway");
814  }
815  // skip n2 and n3, just verify n4
816  NS_LOG_DEBUG("BridgeTest skip print out of n1-n3, go next to node n4");
817 
818  // n4
819  // Test that the right number of routes found
820  route = nullptr;
821  uint32_t nRoutes4 = globalRouting4->GetNRoutes();
822  NS_LOG_DEBUG("BridgeTest nRoutes4 " << nRoutes4);
823  NS_TEST_ASSERT_MSG_EQ(nRoutes4, 2, "Error-- not two entries");
824  for (uint32_t i = 0; i < globalRouting4->GetNRoutes(); i++)
825  {
826  route = globalRouting4->GetRoute(i);
827  NS_LOG_DEBUG("entry dest " << route->GetDest() << " gw " << route->GetGateway());
828  }
829  // Spot check the last route
830  if (route)
831  {
833  Ipv4Address("10.1.1.0"),
834  "Error-- wrong destination");
836  Ipv4Address("10.1.2.1"),
837  "Error-- wrong gateway");
838  }
839 
840  Simulator::Destroy();
841 }
842 
849 {
850  public:
853 
854  private:
859  void SendData(uint8_t index);
860 
865  void ShutDownSock(uint8_t index);
866 
871  void HandleRead(Ptr<Socket> socket);
872  void DoRun() override;
873 
874  uint16_t m_count;
875  std::vector<std::pair<Ptr<Socket>, bool>> m_sendSocks;
877  uint16_t m_packetSize;
878  std::vector<uint8_t>
880  std::vector<uint8_t>
882 };
883 
884 // Add some help text to this case to describe what it is intended to test
886  : TestCase("Dynamic global routing example"),
887  m_count(0)
888 {
889  m_firstInterface.resize(16);
890  m_secondInterface.resize(16);
891  m_dataRate = DataRate("2kbps");
892  m_packetSize = 50;
893 }
894 
896 {
897  for (auto iter = m_sendSocks.begin(); iter != m_sendSocks.end(); iter++)
898  {
899  if (iter->second)
900  {
901  iter->second = false;
902  iter->first->Close();
903  iter->first = nullptr;
904  }
905  }
906 }
907 
908 void
910 {
911  Ptr<Packet> packet;
912  Address from;
913  while ((packet = socket->RecvFrom(from)))
914  {
915  if (packet->GetSize() == 0)
916  { // EOF
917  break;
918  }
919  Ipv4PacketInfoTag tag;
920  bool found;
921  found = packet->PeekPacketTag(tag);
922  uint8_t now = static_cast<uint8_t>(Simulator::Now().GetSeconds());
923  if (found)
924  {
925  if (tag.GetRecvIf() == 1)
926  {
927  m_firstInterface[now]++;
928  }
929  if (tag.GetRecvIf() == 2)
930  {
931  m_secondInterface[now]++;
932  }
933  m_count++;
934  }
935  }
936 }
937 
938 void
940 {
941  if (!m_sendSocks[index].second)
942  {
943  return;
944  }
945  Ptr<Packet> packet = Create<Packet>(m_packetSize);
946  m_sendSocks[index].first->Send(packet);
947 
948  Time tNext(MicroSeconds(m_packetSize * 8 * 1e6 / m_dataRate.GetBitRate()));
949  Simulator::Schedule(tNext, &Ipv4DynamicGlobalRoutingTestCase::SendData, this, index);
950 }
951 
952 void
954 {
955  m_sendSocks[index].second = false;
956  m_sendSocks[index].first->Close();
957  m_sendSocks[index].first = nullptr;
958 }
959 
960 // Test derived from examples/routing/dynamic-global-routing.cc
961 //
962 // Network topology
963 //
964 // n0
965 // \ p-p
966 // \ (shared csma/cd)
967 // n2 -------------------------n3
968 // / | |
969 // / p-p n4 n5 ---------- n6
970 // n1 p-p
971 // | |
972 // ----------------------------------------
973 // p-p
974 //
975 // Test that for node n6, the interface facing n5 receives packets at
976 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
977 // facing n1 receives packets at times (2-4), (6-8), (12-13)
978 //
979 void
981 {
982  // The below value configures the default behavior of global routing.
983  // By default, it is disabled. To respond to interface events, set to true
984  Config::SetDefault("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue(true));
985 
986  NodeContainer c;
987  c.Create(7);
988  NodeContainer n0n2 = NodeContainer(c.Get(0), c.Get(2));
989  NodeContainer n1n2 = NodeContainer(c.Get(1), c.Get(2));
990  NodeContainer n5n6 = NodeContainer(c.Get(5), c.Get(6));
991  NodeContainer n1n6 = NodeContainer(c.Get(1), c.Get(6));
992  NodeContainer n2345 = NodeContainer(c.Get(2), c.Get(3), c.Get(4), c.Get(5));
993 
995  internet.Install(c);
996 
997  // We create the channels first without any IP addressing information
998  SimpleNetDeviceHelper devHelper;
999 
1000  devHelper.SetNetDevicePointToPointMode(true);
1001  NetDeviceContainer d0d2 = devHelper.Install(n0n2);
1002  devHelper.SetNetDevicePointToPointMode(false);
1003 
1004  NetDeviceContainer d1d6 = devHelper.Install(n1n6);
1005  NetDeviceContainer d1d2 = devHelper.Install(n1n2);
1006  NetDeviceContainer d5d6 = devHelper.Install(n5n6);
1007  NetDeviceContainer d2345 = devHelper.Install(n2345);
1008 
1009  // Later, we add IP addresses.
1011  ipv4.SetBase("10.1.1.0", "255.255.255.0");
1012  ipv4.Assign(d0d2);
1013 
1014  ipv4.SetBase("10.1.2.0", "255.255.255.0");
1015  ipv4.Assign(d1d2);
1016 
1017  ipv4.SetBase("10.1.3.0", "255.255.255.0");
1018  Ipv4InterfaceContainer i5i6 = ipv4.Assign(d5d6);
1019 
1020  ipv4.SetBase("10.250.1.0", "255.255.255.0");
1021  ipv4.Assign(d2345);
1022 
1023  ipv4.SetBase("172.16.1.0", "255.255.255.0");
1024  Ipv4InterfaceContainer i1i6 = ipv4.Assign(d1d6);
1025 
1026  // Create router nodes, initialize routing database and set up the routing
1027  // tables in the nodes.
1028  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
1029 
1030  // Create the applications to send UDP datagrams of size
1031  // 50 bytes at a rate of 2 Kb/s
1032  TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
1033  uint16_t port = 9; // Discard port (RFC 863)
1034 
1035  std::pair<Ptr<Socket>, bool> sendSockA;
1036  sendSockA.first = Socket::CreateSocket(c.Get(1), tid);
1037  sendSockA.first->Bind();
1038  sendSockA.first->Connect(InetSocketAddress(i5i6.GetAddress(1), port));
1039  sendSockA.second = true;
1040  m_sendSocks.push_back(sendSockA);
1041  Simulator::Schedule(Seconds(1.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 0);
1042  Simulator::Schedule(Seconds(10.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 0);
1043 
1044  std::pair<Ptr<Socket>, bool> sendSockB;
1045  sendSockB.first = Socket::CreateSocket(c.Get(1), tid);
1046  sendSockB.first->Bind();
1047  sendSockB.first->Connect(InetSocketAddress(i1i6.GetAddress(1), port));
1048  sendSockB.second = true;
1049  m_sendSocks.push_back(sendSockB);
1050  Simulator::Schedule(Seconds(11.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 1);
1051  Simulator::Schedule(Seconds(16.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 1);
1052 
1053  // Create an optional packet sink to receive these packets
1054  Ptr<Socket> sink2 = Socket::CreateSocket(c.Get(6), tid);
1055  sink2->Bind(Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
1056  sink2->Listen();
1057  sink2->ShutdownSend();
1058 
1059  sink2->SetRecvPktInfo(true);
1061 
1062  Ptr<Node> n1 = c.Get(1);
1063  Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4>();
1064  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1065  // then the next p2p is numbered 2
1066  uint32_t ipv4ifIndex1 = 2;
1067 
1068  Simulator::Schedule(Seconds(2), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
1069  Simulator::Schedule(Seconds(4), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
1070 
1071  Ptr<Node> n6 = c.Get(6);
1072  Ptr<Ipv4> ipv46 = n6->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 ipv4ifIndex6 = 2;
1076  Simulator::Schedule(Seconds(6), &Ipv4::SetDown, ipv46, ipv4ifIndex6);
1077  Simulator::Schedule(Seconds(8), &Ipv4::SetUp, ipv46, ipv4ifIndex6);
1078 
1079  Simulator::Schedule(Seconds(12), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
1080  Simulator::Schedule(Seconds(14), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
1081 
1082  Simulator::Run();
1083 
1084  NS_TEST_ASSERT_MSG_EQ(m_count, 70, "Dynamic global routing did not deliver all packets");
1085  // Test that for node n6, the interface facing n5 receives packets at
1086  // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
1087  // facing n1 receives packets at times (2-4), (6-8), (12-13)
1089  5,
1090  "Dynamic global routing did not deliver all packets "
1091  << int(m_firstInterface[1]));
1093  5,
1094  "Dynamic global routing did not deliver all packets "
1095  << int(m_secondInterface[2]));
1097  5,
1098  "Dynamic global routing did not deliver all packets "
1099  << int(m_secondInterface[3]));
1101  5,
1102  "Dynamic global routing did not deliver all packets "
1103  << int(m_firstInterface[4]));
1105  5,
1106  "Dynamic global routing did not deliver all packets "
1107  << int(m_firstInterface[5]));
1109  5,
1110  "Dynamic global routing did not deliver all packets "
1111  << int(m_secondInterface[6]));
1113  5,
1114  "Dynamic global routing did not deliver all packets "
1115  << int(m_secondInterface[7]));
1117  5,
1118  "Dynamic global routing did not deliver all packets "
1119  << int(m_firstInterface[8]));
1121  5,
1122  "Dynamic global routing did not deliver all packets "
1123  << int(m_firstInterface[9]));
1125  0,
1126  "Dynamic global routing did not deliver all packets "
1127  << int(m_firstInterface[10]));
1129  5,
1130  "Dynamic global routing did not deliver all packets "
1131  << int(m_firstInterface[11]));
1133  5,
1134  "Dynamic global routing did not deliver all packets "
1135  << int(m_secondInterface[12]));
1137  5,
1138  "Dynamic global routing did not deliver all packets "
1139  << int(m_secondInterface[13]));
1141  5,
1142  "Dynamic global routing did not deliver all packets "
1143  << int(m_firstInterface[14]));
1145  5,
1146  "Dynamic global routing did not deliver all packets "
1147  << int(m_firstInterface[15]));
1148  Simulator::Destroy();
1149 }
1150 
1157 {
1158  public:
1161 
1163 
1168  void ReceivePkt(Ptr<Socket> socket);
1174  void DoSendData(Ptr<Socket> socket, std::string to);
1180  void SendData(Ptr<Socket> socket, std::string to);
1181 
1182  private:
1183  void DoRun() override;
1184 };
1185 
1186 // Add some help text to this case to describe what it is intended to test
1188  : TestCase("Slash 32 global routing example")
1189 {
1190 }
1191 
1193 {
1194 }
1195 
1196 void
1198 {
1199  uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
1201  NS_TEST_ASSERT_MSG_EQ(availableData,
1203  "Received packet size is not equal to Rx buffer size");
1204 }
1205 
1206 void
1208 {
1209  Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
1210  NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
1211 }
1212 
1213 void
1215 {
1216  m_receivedPacket = Create<Packet>();
1217  Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
1218  Seconds(60),
1220  this,
1221  socket,
1222  to);
1223  Simulator::Stop(Seconds(66));
1224  Simulator::Run();
1225 }
1226 
1227 // Test program for this 3-router scenario, using global routing
1228 //
1229 // (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
1230 //
1231 void
1233 {
1234  Ptr<Node> nA = CreateObject<Node>();
1235  Ptr<Node> nB = CreateObject<Node>();
1236  Ptr<Node> nC = CreateObject<Node>();
1237 
1238  NodeContainer c = NodeContainer(nA, nB, nC);
1239 
1241  internet.Install(c);
1242 
1243  // simple links
1244  NodeContainer nAnB = NodeContainer(nA, nB);
1245  NodeContainer nBnC = NodeContainer(nB, nC);
1246 
1247  SimpleNetDeviceHelper devHelper;
1248 
1249  Ptr<SimpleNetDevice> deviceA = CreateObject<SimpleNetDevice>();
1250  deviceA->SetAddress(Mac48Address::Allocate());
1251  nA->AddDevice(deviceA);
1252 
1253  NetDeviceContainer dAdB = devHelper.Install(nAnB);
1254  NetDeviceContainer dBdC = devHelper.Install(nBnC);
1255 
1256  Ptr<SimpleNetDevice> deviceC = CreateObject<SimpleNetDevice>();
1257  deviceC->SetAddress(Mac48Address::Allocate());
1258  nC->AddDevice(deviceC);
1259 
1260  // Later, we add IP addresses.
1262  ipv4.SetBase("10.1.1.0", "255.255.255.252");
1263  Ipv4InterfaceContainer iAiB = ipv4.Assign(dAdB);
1264 
1265  ipv4.SetBase("10.1.1.4", "255.255.255.252");
1266  Ipv4InterfaceContainer iBiC = ipv4.Assign(dBdC);
1267 
1268  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4>();
1269  Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4>();
1270  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4>();
1271 
1272  int32_t ifIndexA = ipv4A->AddInterface(deviceA);
1273  int32_t ifIndexC = ipv4C->AddInterface(deviceC);
1274 
1275  Ipv4InterfaceAddress ifInAddrA =
1276  Ipv4InterfaceAddress(Ipv4Address("172.16.1.1"), Ipv4Mask("/32"));
1277  ipv4A->AddAddress(ifIndexA, ifInAddrA);
1278  ipv4A->SetMetric(ifIndexA, 1);
1279  ipv4A->SetUp(ifIndexA);
1280 
1281  Ipv4InterfaceAddress ifInAddrC =
1282  Ipv4InterfaceAddress(Ipv4Address("192.168.1.1"), Ipv4Mask("/32"));
1283  ipv4C->AddAddress(ifIndexC, ifInAddrC);
1284  ipv4C->SetMetric(ifIndexC, 1);
1285  ipv4C->SetUp(ifIndexC);
1286 
1287  // Create router nodes, initialize routing database and set up the routing
1288  // tables in the nodes.
1289  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
1290 
1291  // Create the UDP sockets
1292  Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory>();
1293  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
1294  NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("192.168.1.1"), 1234)),
1295  0,
1296  "trivial");
1297  rxSocket->SetRecvCallback(MakeCallback(&Ipv4GlobalRoutingSlash32TestCase::ReceivePkt, this));
1298 
1299  Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory>();
1300  Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
1301  txSocket->SetAllowBroadcast(true);
1302 
1303  // ------ Now the tests ------------
1304 
1305  // Unicast test
1306  SendData(txSocket, "192.168.1.1");
1308  123,
1309  "Static routing with /32 did not deliver all packets.");
1310 
1311  Simulator::Destroy();
1312 }
1313 
1320 {
1321  public:
1323 };
1324 
1326  : TestSuite("ipv4-global-routing", UNIT)
1327 {
1328  AddTestCase(new LinkTest, TestCase::QUICK);
1329  AddTestCase(new LanTest, TestCase::QUICK);
1330  AddTestCase(new TwoLinkTest, TestCase::QUICK);
1331  AddTestCase(new TwoLanTest, TestCase::QUICK);
1332  AddTestCase(new BridgeTest, TestCase::QUICK);
1333  AddTestCase(new TwoBridgeTest, TestCase::QUICK);
1334  AddTestCase(new Ipv4DynamicGlobalRoutingTestCase, TestCase::QUICK);
1335  AddTestCase(new Ipv4GlobalRoutingSlash32TestCase, TestCase::QUICK);
1336 }
1337 
#define max(a, b)
Definition: 80211b.c:42
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:101
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:89
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.
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.
Definition: ipv4-address.h:42
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:80
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:257
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:861
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:983
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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...
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:354
virtual uint32_t GetRxAvailable() const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual int ShutdownSend()=0
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual int Listen()=0
Listen for incoming connections.
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.
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:301
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:403
a unique identifier for an interface.
Definition: type-id.h:59
API to create UDP socket instances.
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#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:327
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
#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:1350
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static Ipv4GlobalRoutingTestSuite g_globalRoutingTestSuite
Static variable for test initialization.
address
Definition: first.py:47
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:704
Definition: second.py:1
channel
Definition: third.py:88
void SetUp(char *deviceName)