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 
150  InternetStackHelper internet;
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 
157  Ipv4AddressHelper ipv4;
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 
233  InternetStackHelper internet;
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 
240  Ipv4AddressHelper ipv4;
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 
322  InternetStackHelper internet;
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 
329  Ipv4AddressHelper ipv4;
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 
439  InternetStackHelper internet;
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 
446  Ipv4AddressHelper ipv4;
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;
536  NetDeviceContainer switchDevices;
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 
560  Ptr<Node> switchNode = m_nodes.Get(2);
561  BridgeHelper bridge;
562  bridge.Install(switchNode, switchDevices);
563 
564  InternetStackHelper internet;
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 
754  InternetStackHelper internet;
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  std::vector<std::pair<Ptr<Socket>, bool>>::iterator iter;
898 
899  for (iter = m_sendSocks.begin(); iter != m_sendSocks.end(); iter++)
900  {
901  if (iter->second)
902  {
903  iter->second = false;
904  iter->first->Close();
905  iter->first = nullptr;
906  }
907  }
908 }
909 
910 void
912 {
913  Ptr<Packet> packet;
914  Address from;
915  while ((packet = socket->RecvFrom(from)))
916  {
917  if (packet->GetSize() == 0)
918  { // EOF
919  break;
920  }
921  Ipv4PacketInfoTag tag;
922  bool found;
923  found = packet->PeekPacketTag(tag);
924  uint8_t now = static_cast<uint8_t>(Simulator::Now().GetSeconds());
925  if (found)
926  {
927  if (tag.GetRecvIf() == 1)
928  {
929  m_firstInterface[now]++;
930  }
931  if (tag.GetRecvIf() == 2)
932  {
933  m_secondInterface[now]++;
934  }
935  m_count++;
936  }
937  }
938 }
939 
940 void
942 {
943  if (m_sendSocks[index].second == false)
944  {
945  return;
946  }
947  Ptr<Packet> packet = Create<Packet>(m_packetSize);
948  m_sendSocks[index].first->Send(packet);
949 
950  Time tNext(MicroSeconds(m_packetSize * 8 * 1e6 / m_dataRate.GetBitRate()));
951  Simulator::Schedule(tNext, &Ipv4DynamicGlobalRoutingTestCase::SendData, this, index);
952 }
953 
954 void
956 {
957  m_sendSocks[index].second = false;
958  m_sendSocks[index].first->Close();
959  m_sendSocks[index].first = nullptr;
960 }
961 
962 // Test derived from examples/routing/dynamic-global-routing.cc
963 //
964 // Network topology
965 //
966 // n0
967 // \ p-p
968 // \ (shared csma/cd)
969 // n2 -------------------------n3
970 // / | |
971 // / p-p n4 n5 ---------- n6
972 // n1 p-p
973 // | |
974 // ----------------------------------------
975 // p-p
976 //
977 // Test that for node n6, the interface facing n5 receives packets at
978 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
979 // facing n1 receives packets at times (2-4), (6-8), (12-13)
980 //
981 void
983 {
984  // The below value configures the default behavior of global routing.
985  // By default, it is disabled. To respond to interface events, set to true
986  Config::SetDefault("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue(true));
987 
988  NodeContainer c;
989  c.Create(7);
990  NodeContainer n0n2 = NodeContainer(c.Get(0), c.Get(2));
991  NodeContainer n1n2 = NodeContainer(c.Get(1), c.Get(2));
992  NodeContainer n5n6 = NodeContainer(c.Get(5), c.Get(6));
993  NodeContainer n1n6 = NodeContainer(c.Get(1), c.Get(6));
994  NodeContainer n2345 = NodeContainer(c.Get(2), c.Get(3), c.Get(4), c.Get(5));
995 
996  InternetStackHelper internet;
997  internet.Install(c);
998 
999  // We create the channels first without any IP addressing information
1000  SimpleNetDeviceHelper devHelper;
1001 
1002  devHelper.SetNetDevicePointToPointMode(true);
1003  NetDeviceContainer d0d2 = devHelper.Install(n0n2);
1004  devHelper.SetNetDevicePointToPointMode(false);
1005 
1006  NetDeviceContainer d1d6 = devHelper.Install(n1n6);
1007  NetDeviceContainer d1d2 = devHelper.Install(n1n2);
1008  NetDeviceContainer d5d6 = devHelper.Install(n5n6);
1009  NetDeviceContainer d2345 = devHelper.Install(n2345);
1010 
1011  // Later, we add IP addresses.
1012  Ipv4AddressHelper ipv4;
1013  ipv4.SetBase("10.1.1.0", "255.255.255.0");
1014  ipv4.Assign(d0d2);
1015 
1016  ipv4.SetBase("10.1.2.0", "255.255.255.0");
1017  ipv4.Assign(d1d2);
1018 
1019  ipv4.SetBase("10.1.3.0", "255.255.255.0");
1020  Ipv4InterfaceContainer i5i6 = ipv4.Assign(d5d6);
1021 
1022  ipv4.SetBase("10.250.1.0", "255.255.255.0");
1023  ipv4.Assign(d2345);
1024 
1025  ipv4.SetBase("172.16.1.0", "255.255.255.0");
1026  Ipv4InterfaceContainer i1i6 = ipv4.Assign(d1d6);
1027 
1028  // Create router nodes, initialize routing database and set up the routing
1029  // tables in the nodes.
1030  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
1031 
1032  // Create the applications to send UDP datagrams of size
1033  // 50 bytes at a rate of 2 Kb/s
1034  TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
1035  uint16_t port = 9; // Discard port (RFC 863)
1036 
1037  std::pair<Ptr<Socket>, bool> sendSockA;
1038  sendSockA.first = Socket::CreateSocket(c.Get(1), tid);
1039  sendSockA.first->Bind();
1040  sendSockA.first->Connect(InetSocketAddress(i5i6.GetAddress(1), port));
1041  sendSockA.second = true;
1042  m_sendSocks.push_back(sendSockA);
1043  Simulator::Schedule(Seconds(1.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 0);
1044  Simulator::Schedule(Seconds(10.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 0);
1045 
1046  std::pair<Ptr<Socket>, bool> sendSockB;
1047  sendSockB.first = Socket::CreateSocket(c.Get(1), tid);
1048  sendSockB.first->Bind();
1049  sendSockB.first->Connect(InetSocketAddress(i1i6.GetAddress(1), port));
1050  sendSockB.second = true;
1051  m_sendSocks.push_back(sendSockB);
1052  Simulator::Schedule(Seconds(11.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 1);
1053  Simulator::Schedule(Seconds(16.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 1);
1054 
1055  // Create an optional packet sink to receive these packets
1056  Ptr<Socket> sink2 = Socket::CreateSocket(c.Get(6), tid);
1057  sink2->Bind(Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
1058  sink2->Listen();
1059  sink2->ShutdownSend();
1060 
1061  sink2->SetRecvPktInfo(true);
1063 
1064  Ptr<Node> n1 = c.Get(1);
1065  Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4>();
1066  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1067  // then the next p2p is numbered 2
1068  uint32_t ipv4ifIndex1 = 2;
1069 
1070  Simulator::Schedule(Seconds(2), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
1071  Simulator::Schedule(Seconds(4), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
1072 
1073  Ptr<Node> n6 = c.Get(6);
1074  Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4>();
1075  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1076  // then the next p2p is numbered 2
1077  uint32_t ipv4ifIndex6 = 2;
1078  Simulator::Schedule(Seconds(6), &Ipv4::SetDown, ipv46, ipv4ifIndex6);
1079  Simulator::Schedule(Seconds(8), &Ipv4::SetUp, ipv46, ipv4ifIndex6);
1080 
1081  Simulator::Schedule(Seconds(12), &Ipv4::SetDown, ipv41, ipv4ifIndex1);
1082  Simulator::Schedule(Seconds(14), &Ipv4::SetUp, ipv41, ipv4ifIndex1);
1083 
1084  Simulator::Run();
1085 
1086  NS_TEST_ASSERT_MSG_EQ(m_count, 70, "Dynamic global routing did not deliver all packets");
1087  // Test that for node n6, the interface facing n5 receives packets at
1088  // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
1089  // facing n1 receives packets at times (2-4), (6-8), (12-13)
1091  5,
1092  "Dynamic global routing did not deliver all packets "
1093  << int(m_firstInterface[1]));
1095  5,
1096  "Dynamic global routing did not deliver all packets "
1097  << int(m_secondInterface[2]));
1099  5,
1100  "Dynamic global routing did not deliver all packets "
1101  << int(m_secondInterface[3]));
1103  5,
1104  "Dynamic global routing did not deliver all packets "
1105  << int(m_firstInterface[4]));
1107  5,
1108  "Dynamic global routing did not deliver all packets "
1109  << int(m_firstInterface[5]));
1111  5,
1112  "Dynamic global routing did not deliver all packets "
1113  << int(m_secondInterface[6]));
1115  5,
1116  "Dynamic global routing did not deliver all packets "
1117  << int(m_secondInterface[7]));
1119  5,
1120  "Dynamic global routing did not deliver all packets "
1121  << int(m_firstInterface[8]));
1123  5,
1124  "Dynamic global routing did not deliver all packets "
1125  << int(m_firstInterface[9]));
1127  0,
1128  "Dynamic global routing did not deliver all packets "
1129  << int(m_firstInterface[10]));
1131  5,
1132  "Dynamic global routing did not deliver all packets "
1133  << int(m_firstInterface[11]));
1135  5,
1136  "Dynamic global routing did not deliver all packets "
1137  << int(m_secondInterface[12]));
1139  5,
1140  "Dynamic global routing did not deliver all packets "
1141  << int(m_secondInterface[13]));
1143  5,
1144  "Dynamic global routing did not deliver all packets "
1145  << int(m_firstInterface[14]));
1147  5,
1148  "Dynamic global routing did not deliver all packets "
1149  << int(m_firstInterface[15]));
1150  Simulator::Destroy();
1151 }
1152 
1159 {
1160  public:
1163 
1165 
1170  void ReceivePkt(Ptr<Socket> socket);
1176  void DoSendData(Ptr<Socket> socket, std::string to);
1182  void SendData(Ptr<Socket> socket, std::string to);
1183 
1184  private:
1185  void DoRun() override;
1186 };
1187 
1188 // Add some help text to this case to describe what it is intended to test
1190  : TestCase("Slash 32 global routing example")
1191 {
1192 }
1193 
1195 {
1196 }
1197 
1198 void
1200 {
1201  uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
1203  NS_TEST_ASSERT_MSG_EQ(availableData,
1205  "Received packet size is not equal to Rx buffer size");
1206 }
1207 
1208 void
1210 {
1211  Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
1212  NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
1213 }
1214 
1215 void
1217 {
1218  m_receivedPacket = Create<Packet>();
1219  Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
1220  Seconds(60),
1222  this,
1223  socket,
1224  to);
1225  Simulator::Stop(Seconds(66));
1226  Simulator::Run();
1227 }
1228 
1229 // Test program for this 3-router scenario, using global routing
1230 //
1231 // (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
1232 //
1233 void
1235 {
1236  Ptr<Node> nA = CreateObject<Node>();
1237  Ptr<Node> nB = CreateObject<Node>();
1238  Ptr<Node> nC = CreateObject<Node>();
1239 
1240  NodeContainer c = NodeContainer(nA, nB, nC);
1241 
1242  InternetStackHelper internet;
1243  internet.Install(c);
1244 
1245  // simple links
1246  NodeContainer nAnB = NodeContainer(nA, nB);
1247  NodeContainer nBnC = NodeContainer(nB, nC);
1248 
1249  SimpleNetDeviceHelper devHelper;
1250 
1251  Ptr<SimpleNetDevice> deviceA = CreateObject<SimpleNetDevice>();
1252  deviceA->SetAddress(Mac48Address::Allocate());
1253  nA->AddDevice(deviceA);
1254 
1255  NetDeviceContainer dAdB = devHelper.Install(nAnB);
1256  NetDeviceContainer dBdC = devHelper.Install(nBnC);
1257 
1258  Ptr<SimpleNetDevice> deviceC = CreateObject<SimpleNetDevice>();
1259  deviceC->SetAddress(Mac48Address::Allocate());
1260  nC->AddDevice(deviceC);
1261 
1262  // Later, we add IP addresses.
1263  Ipv4AddressHelper ipv4;
1264  ipv4.SetBase("10.1.1.0", "255.255.255.252");
1265  Ipv4InterfaceContainer iAiB = ipv4.Assign(dAdB);
1266 
1267  ipv4.SetBase("10.1.1.4", "255.255.255.252");
1268  Ipv4InterfaceContainer iBiC = ipv4.Assign(dBdC);
1269 
1270  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4>();
1271  Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4>();
1272  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4>();
1273 
1274  int32_t ifIndexA = ipv4A->AddInterface(deviceA);
1275  int32_t ifIndexC = ipv4C->AddInterface(deviceC);
1276 
1277  Ipv4InterfaceAddress ifInAddrA =
1278  Ipv4InterfaceAddress(Ipv4Address("172.16.1.1"), Ipv4Mask("/32"));
1279  ipv4A->AddAddress(ifIndexA, ifInAddrA);
1280  ipv4A->SetMetric(ifIndexA, 1);
1281  ipv4A->SetUp(ifIndexA);
1282 
1283  Ipv4InterfaceAddress ifInAddrC =
1284  Ipv4InterfaceAddress(Ipv4Address("192.168.1.1"), Ipv4Mask("/32"));
1285  ipv4C->AddAddress(ifIndexC, ifInAddrC);
1286  ipv4C->SetMetric(ifIndexC, 1);
1287  ipv4C->SetUp(ifIndexC);
1288 
1289  // Create router nodes, initialize routing database and set up the routing
1290  // tables in the nodes.
1291  Ipv4GlobalRoutingHelper::PopulateRoutingTables();
1292 
1293  // Create the UDP sockets
1294  Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory>();
1295  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
1296  NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("192.168.1.1"), 1234)),
1297  0,
1298  "trivial");
1299  rxSocket->SetRecvCallback(MakeCallback(&Ipv4GlobalRoutingSlash32TestCase::ReceivePkt, this));
1300 
1301  Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory>();
1302  Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
1303  txSocket->SetAllowBroadcast(true);
1304 
1305  // ------ Now the tests ------------
1306 
1307  // Unicast test
1308  SendData(txSocket, "192.168.1.1");
1310  123,
1311  "Static routing with /32 did not deliver all packets.");
1312 
1313  Simulator::Destroy();
1314 }
1315 
1322 {
1323  public:
1325 };
1326 
1328  : TestSuite("ipv4-global-routing", UNIT)
1329 {
1330  AddTestCase(new LinkTest, TestCase::QUICK);
1331  AddTestCase(new LanTest, TestCase::QUICK);
1332  AddTestCase(new TwoLinkTest, TestCase::QUICK);
1333  AddTestCase(new TwoLanTest, TestCase::QUICK);
1334  AddTestCase(new BridgeTest, TestCase::QUICK);
1335  AddTestCase(new TwoBridgeTest, TestCase::QUICK);
1336  AddTestCase(new Ipv4DynamicGlobalRoutingTestCase, TestCase::QUICK);
1337  AddTestCase(new Ipv4GlobalRoutingSlash32TestCase, TestCase::QUICK);
1338 }
1339 
#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:100
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...
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.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
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: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:1360
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
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:707
Definition: second.py:1
channel
Definition: third.py:81
void SetUp(char *deviceName)