A Discrete-Event Network Simulator
API
ipv4-global-routing-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 #include <vector>
18 #include "ns3/boolean.h"
19 #include "ns3/config.h"
20 #include "ns3/inet-socket-address.h"
21 #include "ns3/internet-stack-helper.h"
22 #include "ns3/ipv4-address-helper.h"
23 #include "ns3/ipv4-global-routing-helper.h"
24 #include "ns3/ipv4-static-routing-helper.h"
25 #include "ns3/node.h"
26 #include "ns3/log.h"
27 #include "ns3/node-container.h"
28 #include "ns3/packet.h"
29 #include "ns3/simple-net-device-helper.h"
30 #include "ns3/pointer.h"
31 #include "ns3/simulator.h"
32 #include "ns3/string.h"
33 #include "ns3/test.h"
34 #include "ns3/uinteger.h"
35 #include "ns3/ipv4-packet-info-tag.h"
36 #include "ns3/simple-net-device.h"
37 #include "ns3/simple-channel.h"
38 #include "ns3/socket-factory.h"
39 #include "ns3/udp-socket-factory.h"
40 #include "ns3/ipv4-l3-protocol.h"
41 #include "ns3/ipv4-routing-protocol.h"
42 #include "ns3/ipv4-routing-table-entry.h"
43 #include "ns3/ipv4-global-routing.h"
44 #include "ns3/bridge-helper.h"
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 
119 
126 class LinkTest : public TestCase
127 {
128 public:
129  virtual void DoSetup (void);
130  virtual void DoRun (void);
131  LinkTest ();
132 private:
134 };
135 
137  : TestCase ("Global routing on point-to-point link")
138 {
139 }
140 
141 void
143 {
144  m_nodes.Create (2);
145 
146  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
147  SimpleNetDeviceHelper simpleHelper;
148  simpleHelper.SetNetDevicePointToPointMode (true);
149  NetDeviceContainer net = simpleHelper.Install (m_nodes, channel);
150 
151  InternetStackHelper internet;
152  // By default, InternetStackHelper adds a static and global routing
153  // implementation. We just want the global for this test.
154  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
155  internet.SetRoutingHelper (ipv4RoutingHelper);
156  internet.Install (m_nodes);
157 
158  Ipv4AddressHelper ipv4;
159  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
160  Ipv4InterfaceContainer i = ipv4.Assign (net);
161 }
162 
163 void
165 {
166  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
167 
169  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
171  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
172  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
173  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
174  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
175  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
176  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
177  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
178 
179  // Test that the right number of routes found
180  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
181  NS_LOG_DEBUG ("LinkTest nRoutes0 " << nRoutes0);
182  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- not one route");
183  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0);
184  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
185  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination");
186  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
187 
188  // Test that the right number of routes found
189  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
190  NS_TEST_ASSERT_MSG_EQ (nRoutes1, 1, "Error-- not one route");
191  NS_LOG_DEBUG ("LinkTest nRoutes1 " << nRoutes1);
192  route = globalRouting1->GetRoute (0);
193  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
194  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination");
195  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway");
196 
197  bool result = true;
198 
199  NS_TEST_ASSERT_MSG_EQ (result, true, "Message");
200  Simulator::Run ();
201  Simulator::Destroy ();
202 }
203 
210 class LanTest : public TestCase
211 {
212 public:
213  virtual void DoSetup (void);
214  virtual void DoRun (void);
215  LanTest ();
216 private:
218 };
219 
221  : TestCase ("Global routing on broadcast link")
222 {
223 }
224 
225 void
227 {
228  m_nodes.Create (2);
229 
230  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
231  SimpleNetDeviceHelper simpleHelper;
232  NetDeviceContainer net = simpleHelper.Install (m_nodes, channel);
233 
234  InternetStackHelper internet;
235  // By default, InternetStackHelper adds a static and global routing
236  // implementation. We just want the global for this test.
237  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
238  internet.SetRoutingHelper (ipv4RoutingHelper);
239  internet.Install (m_nodes);
240 
241  Ipv4AddressHelper ipv4;
242  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
243  Ipv4InterfaceContainer i = ipv4.Assign (net);
244 }
245 
246 void
248 {
249  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
250 
252  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
254  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
255  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
256  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
257  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
258  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
259  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
260  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
261 
262  // Test that the right number of routes found
263  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
264  NS_LOG_DEBUG ("LanTest nRoutes0 " << nRoutes0);
265  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- more than one entry");
266  for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++)
267  {
268  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (i);
269  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
270  }
271 
272  // Test that the right number of routes found
273  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
274  NS_LOG_DEBUG ("LanTest nRoutes1 " << nRoutes1);
275  NS_TEST_ASSERT_MSG_EQ (nRoutes1, 1, "Error-- more than one entry");
276  for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++)
277  {
278  Ipv4RoutingTableEntry* route = globalRouting1->GetRoute (i);
279  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
280  }
281 
282  Simulator::Destroy ();
283 }
284 
291 class TwoLinkTest : public TestCase
292 {
293 public:
294  virtual void DoSetup (void);
295  virtual void DoRun (void);
296  TwoLinkTest ();
297 private:
299 };
300 
302  : TestCase ("Global routing across two hops (point-to-point links)")
303 {
304 }
305 
306 void
308 {
309  m_nodes.Create (3);
310 
311  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
312  SimpleNetDeviceHelper simpleHelper;
313  simpleHelper.SetNetDevicePointToPointMode (true);
314  NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel);
315  net.Add (simpleHelper.Install (m_nodes.Get (1), channel));
316 
317  Ptr<SimpleChannel> channel2 = CreateObject <SimpleChannel> ();
318  SimpleNetDeviceHelper simpleHelper2;
319  simpleHelper2.SetNetDevicePointToPointMode (true);
320  NetDeviceContainer net2 = simpleHelper.Install (m_nodes.Get (1), channel2);
321  net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2));
322 
323  InternetStackHelper internet;
324  // By default, InternetStackHelper adds a static and global routing
325  // implementation. We just want the global for this test.
326  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
327  internet.SetRoutingHelper (ipv4RoutingHelper);
328  internet.Install (m_nodes);
329 
330  Ipv4AddressHelper ipv4;
331  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
332  Ipv4InterfaceContainer i = ipv4.Assign (net);
333  ipv4.SetBase ("10.1.2.0", "255.255.255.252");
334  Ipv4InterfaceContainer i2 = ipv4.Assign (net2);
335 }
336 
337 void
339 {
340  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
341 
343  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
345  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
347  NS_TEST_ASSERT_MSG_NE (ip2, 0, "Error-- no Ipv4 object");
348  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
349  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
350  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
351  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
352  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
353  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
354  Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol ();
355  Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject <Ipv4GlobalRouting> ();
356  NS_TEST_ASSERT_MSG_NE (globalRouting2, 0, "Error-- no Ipv4GlobalRouting object");
357 
358  // node n0
359  // Test that the right number of routes found
360  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
361  NS_LOG_DEBUG ("TwoLinkTest nRoutes0 " << nRoutes0);
362  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- wrong number of links");
363 
364  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0);
365  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
366  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination");
367  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
368 
369  // node n1
370  // Test that the right number of routes found
371  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
372  NS_LOG_DEBUG ("TwoLinkTest nRoutes1 " << nRoutes1);
373  route = globalRouting1->GetRoute (0);
374  NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
375  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.1"), "Error-- wrong destination");
376  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway");
377  route = globalRouting1->GetRoute (1);
378  NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
379  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.2"), "Error-- wrong destination");
380  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.2"), "Error-- wrong gateway");
381  route = globalRouting1->GetRoute (2);
382  NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
383  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
384  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway");
385  route = globalRouting1->GetRoute (3);
386  NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
387  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination");
388  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.2"), "Error-- wrong gateway");
389 
390  // node n2
391  // Test that the right number of routes found
392  uint32_t nRoutes2 = globalRouting2->GetNRoutes ();
393  NS_LOG_DEBUG ("TwoLinkTest nRoutes2 " << nRoutes2);
394  NS_TEST_ASSERT_MSG_EQ (nRoutes2, 1, "Error-- wrong number of links");
395 
396  route = globalRouting2->GetRoute (0);
397  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
398  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination");
399  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.1"), "Error-- wrong gateway");
400 
401  Simulator::Destroy ();
402 }
403 
410 class TwoLanTest : public TestCase
411 {
412 public:
413  virtual void DoSetup (void);
414  virtual void DoRun (void);
415  TwoLanTest ();
416 private:
418 };
419 
421  : TestCase ("Global routing across two hops (broadcast links)")
422 {
423 }
424 
425 void
427 {
428  m_nodes.Create (3);
429 
430  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
431  SimpleNetDeviceHelper simpleHelper;
432  NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel);
433  net.Add (simpleHelper.Install (m_nodes.Get (1), channel));
434 
435  Ptr<SimpleChannel> channel2 = CreateObject <SimpleChannel> ();
436  SimpleNetDeviceHelper simpleHelper2;
437  NetDeviceContainer net2 = simpleHelper.Install (m_nodes.Get (1), channel2);
438  net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2));
439 
440  InternetStackHelper internet;
441  // By default, InternetStackHelper adds a static and global routing
442  // implementation. We just want the global for this test.
443  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
444  internet.SetRoutingHelper (ipv4RoutingHelper);
445  internet.Install (m_nodes);
446 
447  Ipv4AddressHelper ipv4;
448  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
449  Ipv4InterfaceContainer i = ipv4.Assign (net);
450  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
451  Ipv4InterfaceContainer i2 = ipv4.Assign (net2);
452 }
453 
454 void
456 {
457  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
458 
460  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
462  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
464  NS_TEST_ASSERT_MSG_NE (ip2, 0, "Error-- no Ipv4 object");
465  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
466  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
467  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
468  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
469  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
470  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
471  Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol ();
472  Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject <Ipv4GlobalRouting> ();
473  NS_TEST_ASSERT_MSG_NE (globalRouting2, 0, "Error-- no Ipv4GlobalRouting object");
474 
475  // Test that the right number of routes found
476  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
477  NS_LOG_DEBUG ("TwoLanTest nRoutes0 " << nRoutes0);
478  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 2, "Error-- not two entries");
479  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0);
480  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
481  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
482  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway");
483  route = globalRouting0->GetRoute (1);
484  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
485  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination");
486  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
487 
488  // Test that the right number of routes found
489  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
490  NS_LOG_DEBUG ("TwoLanTest nRoutes1 " << nRoutes1);
491  NS_TEST_ASSERT_MSG_EQ (nRoutes1, 2, "Error-- not two entries");
492  route = globalRouting1->GetRoute (0);
493  NS_LOG_DEBUG ("TwoLanTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
494  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
495  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway");
496  route = globalRouting1->GetRoute (1);
497  NS_LOG_DEBUG ("TwoLanTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
498  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination");
499  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway");
500 
501  Simulator::Destroy ();
502 }
503 
510 class BridgeTest : public TestCase
511 {
512 public:
513  virtual void DoSetup (void);
514  virtual void DoRun (void);
515  BridgeTest ();
516 private:
518 };
519 
521  : TestCase ("Global routing across bridging topology (bug 2102)")
522 {
523 }
524 
525 void
527 {
528  m_nodes.Create (5);
529 
530  //connect node0 to node1
531  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
532  SimpleNetDeviceHelper simpleHelper;
533  NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel);
534  net.Add (simpleHelper.Install (m_nodes.Get (1), channel));
535 
536  NetDeviceContainer bridgeFacingDevices;
537  NetDeviceContainer switchDevices;
538 
539  //connect node1 to node2 (switch)
540  Ptr<SimpleChannel> channel2 = CreateObject <SimpleChannel> ();
541  SimpleNetDeviceHelper simpleHelper2;
542  NetDeviceContainer net2 = simpleHelper2.Install (m_nodes.Get (1), channel2);
543  net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2));
544  bridgeFacingDevices.Add (net2.Get (0));
545  switchDevices.Add (net2.Get (1));
546 
547  //connect node2 (switch) to node3
548  Ptr<SimpleChannel> channel3 = CreateObject <SimpleChannel> ();
549  SimpleNetDeviceHelper simpleHelper3;
550  NetDeviceContainer net3 = simpleHelper3.Install (m_nodes.Get (2), channel3);
551  net3.Add (simpleHelper3.Install (m_nodes.Get (3), channel3));
552  bridgeFacingDevices.Add (net3.Get (1));
553  switchDevices.Add (net3.Get (0));
554 
555  //connect node3 to node4
556  Ptr<SimpleChannel> channel4 = CreateObject <SimpleChannel> ();
557  SimpleNetDeviceHelper simpleHelper4;
558  NetDeviceContainer net4 = simpleHelper4.Install (m_nodes.Get (3), channel4);
559  net4.Add (simpleHelper4.Install (m_nodes.Get (4), channel4));
560 
561  Ptr<Node> switchNode = m_nodes.Get (2);
562  BridgeHelper bridge;
563  bridge.Install (switchNode, switchDevices);
564 
565  InternetStackHelper internet;
566  // By default, InternetStackHelper adds a static and global routing
567  // implementation. We just want the global for this test.
568  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
569  internet.SetRoutingHelper (ipv4RoutingHelper);
570 
571  internet.Install (m_nodes.Get (0));
572  internet.Install (m_nodes.Get (1));
573  // m_nodes.Get (2) is bridge node
574  internet.Install (m_nodes.Get (3));
575  internet.Install (m_nodes.Get (4));
576 
578  address.SetBase ("10.1.1.0", "255.255.255.0");
579  address.Assign (net);
580 
581  address.SetBase ("10.1.2.0", "255.255.255.0");
582  address.Assign (bridgeFacingDevices);
583 
584  address.SetBase ("10.1.3.0", "255.255.255.0");
585  address.Assign (net4);
586 
587 }
588 
589 void
591 {
592  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
593 
595  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
596  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
597  NS_TEST_ASSERT_MSG_NE (routing0, 0, "Error-- no Ipv4 routing protocol object");
598  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
599  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
600 
602  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
603  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
604  NS_TEST_ASSERT_MSG_NE (routing1, 0, "Error-- no Ipv4 routing protocol object");
605  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
606  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
607 
608  // Skip to n4
610  NS_TEST_ASSERT_MSG_NE (ip4, 0, "Error-- no Ipv4 object");
611  Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol ();
612  NS_TEST_ASSERT_MSG_NE (routing4, 0, "Error-- no Ipv4 routing protocol object");
613  Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject <Ipv4GlobalRouting> ();
614  NS_TEST_ASSERT_MSG_NE (globalRouting4, 0, "Error-- no Ipv4GlobalRouting object");
615 
616  Ipv4RoutingTableEntry* route = 0;
617  // n0
618  // Test that the right number of routes found
619  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
620  NS_LOG_DEBUG ("BridgeTest nRoutes0 " << nRoutes0);
621  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 3, "Error-- not three entries");
622  for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++)
623  {
624  route = globalRouting0->GetRoute (i);
625  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
626  }
627  // Spot check the last route
628  if (route)
629  {
630  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.3.0"), "Error-- wrong destination");
631  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
632  }
633 
634  // n1
635  // Test that the right number of routes found
636  route = 0;
637  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
638  NS_LOG_DEBUG ("BridgeTest nRoutes1 " << nRoutes1);
639  NS_TEST_ASSERT_MSG_EQ (nRoutes1, 3, "Error-- not three entries");
640  for (uint32_t i = 0; i < globalRouting1->GetNRoutes (); i++)
641  {
642  route = globalRouting1->GetRoute (i);
643  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
644  }
645  // Spot check the last route
646  if (route)
647  {
648  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.3.0"), "Error-- wrong destination");
649  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.2"), "Error-- wrong gateway");
650  }
651 
652  // skip n2 and n3, just verify n4
653  NS_LOG_DEBUG ("BridgeTest skip print out of n2 and n3, go next to node n4");
654 
655  // n4
656  route = 0;
657  // Test that the right number of routes found
658  uint32_t nRoutes4 = globalRouting4->GetNRoutes ();
659  NS_LOG_DEBUG ("BridgeTest nRoutes4 " << nRoutes4);
660  NS_TEST_ASSERT_MSG_EQ (nRoutes4, 3, "Error-- not three entries");
661  for (uint32_t i = 0; i < globalRouting4->GetNRoutes (); i++)
662  {
663  route = globalRouting4->GetRoute (i);
664  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
665  }
666  // Spot check the last route
667  if (route)
668  {
669  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
670  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.3.1"), "Error-- wrong gateway");
671  }
672 
673  Simulator::Destroy ();
674 }
675 
682 class TwoBridgeTest : public TestCase
683 {
684 public:
685  virtual void DoSetup (void);
686  virtual void DoRun (void);
687  TwoBridgeTest ();
688 private:
690 };
691 
693  : TestCase ("Global routing across two bridges")
694 {
695 }
696 
697 void
699 {
700  m_nodes.Create (5);
701 
702  //connect node0 to node1
703  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
704  SimpleNetDeviceHelper simpleHelper;
705  NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel);
706  net.Add (simpleHelper.Install (m_nodes.Get (1), channel));
707 
708  NetDeviceContainer bridgeFacingDevices;
709  NetDeviceContainer switchn2Devices;
710  NetDeviceContainer switchn3Devices;
711 
712  //connect node1 to node2 (switch)
713  Ptr<SimpleChannel> channel2 = CreateObject <SimpleChannel> ();
714  SimpleNetDeviceHelper simpleHelper2;
715  NetDeviceContainer net2 = simpleHelper2.Install (m_nodes.Get (1), channel2);
716  net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2));
717  bridgeFacingDevices.Add (net2.Get (0));
718  switchn2Devices.Add (net2.Get (1));
719 
720  //connect node2 (switch) to node3
721  Ptr<SimpleChannel> channel3 = CreateObject <SimpleChannel> ();
722  SimpleNetDeviceHelper simpleHelper3;
723  NetDeviceContainer net3 = simpleHelper3.Install (m_nodes.Get (2), channel3);
724  net3.Add (simpleHelper3.Install (m_nodes.Get (3), channel3));
725  switchn2Devices.Add (net3.Get (0));
726  switchn3Devices.Add (net3.Get (1));
727 
728  //connect node3 to node4
729  Ptr<SimpleChannel> channel4 = CreateObject <SimpleChannel> ();
730  SimpleNetDeviceHelper simpleHelper4;
731  NetDeviceContainer net4 = simpleHelper4.Install (m_nodes.Get (3), channel4);
732  net4.Add (simpleHelper4.Install (m_nodes.Get (4), channel4));
733  switchn3Devices.Add (net4.Get (0));
734  bridgeFacingDevices.Add (net4.Get (1));
735 
736  Ptr<Node> switchn2Node = m_nodes.Get (2);
737  BridgeHelper bridgen2Helper;
738  bridgen2Helper.Install (switchn2Node, switchn2Devices);
739 
740  Ptr<Node> switchn3Node = m_nodes.Get (3);
741  BridgeHelper bridgen3Helper;
742  bridgen3Helper.Install (switchn3Node, switchn3Devices);
743 
744  InternetStackHelper internet;
745  // By default, InternetStackHelper adds a static and global routing
746  // implementation. We just want the global for this test.
747  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
748  internet.SetRoutingHelper (ipv4RoutingHelper);
749 
750  internet.Install (m_nodes.Get (0));
751  internet.Install (m_nodes.Get (1));
752  // m_nodes.Get (2) is bridge node
753  // m_nodes.Get (3) is bridge node
754  internet.Install (m_nodes.Get (4));
755 
757  address.SetBase ("10.1.1.0", "255.255.255.0");
758  address.Assign (net);
759 
760  address.SetBase ("10.1.2.0", "255.255.255.0");
761  address.Assign (bridgeFacingDevices);
762 
763 }
764 
765 void
767 {
768  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
769 
771  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
772  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
773  NS_TEST_ASSERT_MSG_NE (routing0, 0, "Error-- no Ipv4 routing protocol object");
774  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
775  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
776 
777  // Skip to n4
779  NS_TEST_ASSERT_MSG_NE (ip4, 0, "Error-- no Ipv4 object");
780  Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol ();
781  NS_TEST_ASSERT_MSG_NE (routing4, 0, "Error-- no Ipv4 routing protocol object");
782  Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject <Ipv4GlobalRouting> ();
783  NS_TEST_ASSERT_MSG_NE (globalRouting4, 0, "Error-- no Ipv4GlobalRouting object");
784 
785  Ipv4RoutingTableEntry* route = 0;
786  // n0
787  // Test that the right number of routes found
788  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
789  NS_LOG_DEBUG ("BridgeTest nRoutes0 " << nRoutes0);
790  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 2, "Error-- not two entries");
791  for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++)
792  {
793  route = globalRouting0->GetRoute (i);
794  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
795  }
796  // Spot check the last route
797  if (route)
798  {
799  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination");
800  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
801  }
802  // skip n2 and n3, just verify n4
803  NS_LOG_DEBUG ("BridgeTest skip print out of n1-n3, go next to node n4");
804 
805  // n4
806  // Test that the right number of routes found
807  route = 0;
808  uint32_t nRoutes4 = globalRouting4->GetNRoutes ();
809  NS_LOG_DEBUG ("BridgeTest nRoutes4 " << nRoutes4);
810  NS_TEST_ASSERT_MSG_EQ (nRoutes4, 2, "Error-- not two entries");
811  for (uint32_t i = 0; i < globalRouting4->GetNRoutes (); i++)
812  {
813  route = globalRouting4->GetRoute (i);
814  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
815  }
816  // Spot check the last route
817  if (route)
818  {
819  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
820  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.1"), "Error-- wrong gateway");
821  }
822 
823  Simulator::Destroy ();
824 }
825 
833 {
834 public:
837 
838 private:
843  void SendData (uint8_t index);
844 
849  void ShutDownSock (uint8_t index);
850 
855  void HandleRead (Ptr<Socket> socket);
856  virtual void DoRun (void);
857 
858  uint16_t m_count;
859  std::vector<std::pair<Ptr<Socket>, bool> > m_sendSocks;
861  uint16_t m_packetSize;
862  std::vector<uint8_t> m_firstInterface;
863  std::vector<uint8_t> m_secondInterface;
864 };
865 
866 // Add some help text to this case to describe what it is intended to test
868  : TestCase ("Dynamic global routing example"), m_count (0)
869 {
870  m_firstInterface.resize (16);
871  m_secondInterface.resize (16);
872  m_dataRate = DataRate ("2kbps");
873  m_packetSize = 50;
874 }
875 
877 {
878  std::vector<std::pair<Ptr<Socket>, bool> >::iterator iter;
879 
880  for (iter = m_sendSocks.begin (); iter != m_sendSocks.end (); iter++)
881  {
882  if (iter->second)
883  {
884  iter->second = false;
885  iter->first->Close ();
886  iter->first = 0;
887  }
888  }
889 }
890 
891 void
893 {
894  Ptr<Packet> packet;
895  Address from;
896  while ((packet = socket->RecvFrom (from)))
897  {
898  if (packet->GetSize () == 0)
899  { //EOF
900  break;
901  }
902  Ipv4PacketInfoTag tag;
903  bool found;
904  found = packet->PeekPacketTag (tag);
905  uint8_t now = static_cast<uint8_t> (Simulator::Now ().GetSeconds ());
906  if (found)
907  {
908  if (tag.GetRecvIf () == 1)
909  {
910  m_firstInterface[now]++;
911  }
912  if (tag.GetRecvIf () == 2)
913  {
914  m_secondInterface[now]++;
915  }
916  m_count++;
917  }
918  }
919 }
920 
921 void
923 {
924  if (m_sendSocks[index].second == false)
925  {
926  return;
927  }
928  Ptr<Packet> packet = Create<Packet> (m_packetSize);
929  m_sendSocks[index].first->Send (packet);
930 
931  Time tNext (MicroSeconds (m_packetSize * 8 * 1e6 / m_dataRate.GetBitRate ()));
932  Simulator::Schedule (tNext, &Ipv4DynamicGlobalRoutingTestCase::SendData, this, index);
933 }
934 
935 void
937 {
938  m_sendSocks[index].second = false;
939  m_sendSocks[index].first->Close ();
940  m_sendSocks[index].first = 0;
941 }
942 
943 // Test derived from examples/routing/dynamic-global-routing.cc
944 //
945 // Network topology
946 //
947 // n0
948 // \ p-p
949 // \ (shared csma/cd)
950 // n2 -------------------------n3
951 // / | |
952 // / p-p n4 n5 ---------- n6
953 // n1 p-p
954 // | |
955 // ----------------------------------------
956 // p-p
957 //
958 // Test that for node n6, the interface facing n5 receives packets at
959 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
960 // facing n1 receives packets at times (2-4), (6-8), (12-13)
961 //
962 void
964 {
965  // The below value configures the default behavior of global routing.
966  // By default, it is disabled. To respond to interface events, set to true
967  Config::SetDefault ("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue (true));
968 
969  NodeContainer c;
970  c.Create (7);
971  NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
972  NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
973  NodeContainer n5n6 = NodeContainer (c.Get (5), c.Get (6));
974  NodeContainer n1n6 = NodeContainer (c.Get (1), c.Get (6));
975  NodeContainer n2345 = NodeContainer (c.Get (2), c.Get (3), c.Get (4), c.Get (5));
976 
977  InternetStackHelper internet;
978  internet.Install (c);
979 
980  // We create the channels first without any IP addressing information
981  SimpleNetDeviceHelper devHelper;
982 
983  devHelper.SetNetDevicePointToPointMode (true);
984  NetDeviceContainer d0d2 = devHelper.Install (n0n2);
985  devHelper.SetNetDevicePointToPointMode (false);
986 
987  NetDeviceContainer d1d6 = devHelper.Install (n1n6);
988  NetDeviceContainer d1d2 = devHelper.Install (n1n2);
989  NetDeviceContainer d5d6 = devHelper.Install (n5n6);
990  NetDeviceContainer d2345 = devHelper.Install (n2345);
991 
992  // Later, we add IP addresses.
993  Ipv4AddressHelper ipv4;
994  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
995  ipv4.Assign (d0d2);
996 
997  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
998  ipv4.Assign (d1d2);
999 
1000  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
1001  Ipv4InterfaceContainer i5i6 = ipv4.Assign (d5d6);
1002 
1003  ipv4.SetBase ("10.250.1.0", "255.255.255.0");
1004  ipv4.Assign (d2345);
1005 
1006  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
1007  Ipv4InterfaceContainer i1i6 = ipv4.Assign (d1d6);
1008 
1009  // Create router nodes, initialize routing database and set up the routing
1010  // tables in the nodes.
1011  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
1012 
1013  // Create the applications to send UDP datagrams of size
1014  // 50 bytes at a rate of 2 Kb/s
1015  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
1016  uint16_t port = 9; // Discard port (RFC 863)
1017 
1018  std::pair<Ptr<Socket>, bool> sendSockA;
1019  sendSockA.first = Socket::CreateSocket (c.Get (1), tid);
1020  sendSockA.first->Bind ();
1021  sendSockA.first->Connect (InetSocketAddress (i5i6.GetAddress (1), port));
1022  sendSockA.second = true;
1023  m_sendSocks.push_back (sendSockA);
1024  Simulator::Schedule (Seconds (1.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 0);
1025  Simulator::Schedule (Seconds (10.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 0);
1026 
1027  std::pair<Ptr<Socket>, bool> sendSockB;
1028  sendSockB.first = Socket::CreateSocket (c.Get (1), tid);
1029  sendSockB.first->Bind ();
1030  sendSockB.first->Connect (InetSocketAddress (i1i6.GetAddress (1), port));
1031  sendSockB.second = true;
1032  m_sendSocks.push_back (sendSockB);
1033  Simulator::Schedule (Seconds (11.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 1);
1034  Simulator::Schedule (Seconds (16.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 1);
1035 
1036 
1037  // Create an optional packet sink to receive these packets
1038  Ptr<Socket> sink2 = Socket::CreateSocket (c.Get (6), tid);
1039  sink2->Bind (Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
1040  sink2->Listen ();
1041  sink2->ShutdownSend ();
1042 
1043  sink2->SetRecvPktInfo (true);
1045 
1046  Ptr<Node> n1 = c.Get (1);
1047  Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
1048  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1049  // then the next p2p is numbered 2
1050  uint32_t ipv4ifIndex1 = 2;
1051 
1052  Simulator::Schedule (Seconds (2), &Ipv4::SetDown,ipv41, ipv4ifIndex1);
1053  Simulator::Schedule (Seconds (4), &Ipv4::SetUp,ipv41, ipv4ifIndex1);
1054 
1055  Ptr<Node> n6 = c.Get (6);
1056  Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4> ();
1057  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1058  // then the next p2p is numbered 2
1059  uint32_t ipv4ifIndex6 = 2;
1060  Simulator::Schedule (Seconds (6), &Ipv4::SetDown,ipv46, ipv4ifIndex6);
1061  Simulator::Schedule (Seconds (8), &Ipv4::SetUp,ipv46, ipv4ifIndex6);
1062 
1063  Simulator::Schedule (Seconds (12), &Ipv4::SetDown,ipv41, ipv4ifIndex1);
1064  Simulator::Schedule (Seconds (14), &Ipv4::SetUp,ipv41, ipv4ifIndex1);
1065 
1066  Simulator::Run ();
1067 
1068  NS_TEST_ASSERT_MSG_EQ (m_count, 70, "Dynamic global routing did not deliver all packets");
1069 // Test that for node n6, the interface facing n5 receives packets at
1070 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
1071 // facing n1 receives packets at times (2-4), (6-8), (12-13)
1072  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[1], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[1]));
1073  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[2], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[2]));
1074  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[3], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[3]));
1075  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[4], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[4]));
1076  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[5], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[5]));
1077  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[6], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[6]));
1078  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[7], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[7]));
1079  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[8], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[8]));
1080  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[9], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[9]));
1081  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[10], 0, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[10]));
1082  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[11], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[11]));
1083  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[12], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[12]));
1084  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[13], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[13]));
1085  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[14], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[14]));
1086  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[15], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[15]));
1087  Simulator::Destroy ();
1088 }
1089 
1097 {
1098 public:
1101 
1103 
1108  void ReceivePkt (Ptr<Socket> socket);
1114  void DoSendData (Ptr<Socket> socket, std::string to);
1120  void SendData (Ptr<Socket> socket, std::string to);
1121 
1122 private:
1123  virtual void DoRun (void);
1124 };
1125 
1126 // Add some help text to this case to describe what it is intended to test
1128  : TestCase ("Slash 32 global routing example")
1129 {
1130 }
1131 
1133 {
1134 }
1135 
1136 void
1138 {
1139  uint32_t availableData;
1140  availableData = socket->GetRxAvailable ();
1142  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
1143  //cast availableData to void, to suppress 'availableData' set but not used
1144  //compiler warning
1145  (void) availableData;
1146 }
1147 
1148 void
1150 {
1151  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 1234);
1152  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
1153  123, "100");
1154 }
1155 
1156 void
1158 {
1159  m_receivedPacket = Create<Packet> ();
1160  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (60),
1162  Simulator::Stop (Seconds (66));
1163  Simulator::Run ();
1164 }
1165 
1166 // Test program for this 3-router scenario, using global routing
1167 //
1168 // (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
1169 //
1170 void
1172 {
1173  Ptr<Node> nA = CreateObject<Node> ();
1174  Ptr<Node> nB = CreateObject<Node> ();
1175  Ptr<Node> nC = CreateObject<Node> ();
1176 
1177  NodeContainer c = NodeContainer (nA, nB, nC);
1178 
1179  InternetStackHelper internet;
1180  internet.Install (c);
1181 
1182  // simple links
1183  NodeContainer nAnB = NodeContainer (nA, nB);
1184  NodeContainer nBnC = NodeContainer (nB, nC);
1185 
1186  SimpleNetDeviceHelper devHelper;
1187 
1188  Ptr<SimpleNetDevice> deviceA = CreateObject<SimpleNetDevice> ();
1189  deviceA->SetAddress (Mac48Address::Allocate ());
1190  nA->AddDevice (deviceA);
1191 
1192  NetDeviceContainer dAdB = devHelper.Install (nAnB);
1193  NetDeviceContainer dBdC = devHelper.Install (nBnC);
1194 
1195  Ptr<SimpleNetDevice> deviceC = CreateObject<SimpleNetDevice> ();
1196  deviceC->SetAddress (Mac48Address::Allocate ());
1197  nC->AddDevice (deviceC);
1198 
1199  // Later, we add IP addresses.
1200  Ipv4AddressHelper ipv4;
1201  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
1202  Ipv4InterfaceContainer iAiB = ipv4.Assign (dAdB);
1203 
1204  ipv4.SetBase ("10.1.1.4", "255.255.255.252");
1205  Ipv4InterfaceContainer iBiC = ipv4.Assign (dBdC);
1206 
1207  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4> ();
1208  Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4> ();
1209  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4> ();
1210 
1211  int32_t ifIndexA = ipv4A->AddInterface (deviceA);
1212  int32_t ifIndexC = ipv4C->AddInterface (deviceC);
1213 
1214  Ipv4InterfaceAddress ifInAddrA = Ipv4InterfaceAddress (Ipv4Address ("172.16.1.1"), Ipv4Mask ("/32"));
1215  ipv4A->AddAddress (ifIndexA, ifInAddrA);
1216  ipv4A->SetMetric (ifIndexA, 1);
1217  ipv4A->SetUp (ifIndexA);
1218 
1219  Ipv4InterfaceAddress ifInAddrC = Ipv4InterfaceAddress (Ipv4Address ("192.168.1.1"), Ipv4Mask ("/32"));
1220  ipv4C->AddAddress (ifIndexC, ifInAddrC);
1221  ipv4C->SetMetric (ifIndexC, 1);
1222  ipv4C->SetUp (ifIndexC);
1223 
1224  // Create router nodes, initialize routing database and set up the routing
1225  // tables in the nodes.
1226  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
1227 
1228  // Create the UDP sockets
1229  Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory> ();
1230  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
1231  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("192.168.1.1"), 1234)), 0, "trivial");
1232  rxSocket->SetRecvCallback (MakeCallback (&Ipv4GlobalRoutingSlash32TestCase::ReceivePkt, this));
1233 
1234  Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory> ();
1235  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
1236  txSocket->SetAllowBroadcast (true);
1237 
1238  // ------ Now the tests ------------
1239 
1240  // Unicast test
1241  SendData (txSocket, "192.168.1.1");
1242  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "Static routing with /32 did not deliver all packets.");
1243 
1244  Simulator::Destroy ();
1245 }
1246 
1254 {
1255 public:
1257 };
1258 
1260  : TestSuite ("ipv4-global-routing", UNIT)
1261  {
1262  AddTestCase (new LinkTest, TestCase::QUICK);
1263  AddTestCase (new LanTest, TestCase::QUICK);
1264  AddTestCase (new TwoLinkTest, TestCase::QUICK);
1265  AddTestCase (new TwoLanTest, TestCase::QUICK);
1266  AddTestCase (new BridgeTest, TestCase::QUICK);
1267  AddTestCase (new TwoBridgeTest, TestCase::QUICK);
1268  AddTestCase (new Ipv4DynamicGlobalRoutingTestCase, TestCase::QUICK);
1269  AddTestCase (new Ipv4GlobalRoutingSlash32TestCase, TestCase::QUICK);
1270  }
1271 
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
an Inet address class
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
IPv4 GlobalRouting Two LAN test.
Ptr< Packet > m_receivedPacket
number of received packets
AttributeValue implementation for Boolean.
Definition: boolean.h:36
Definition: second.py:1
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t GetId(void) const
Definition: node.cc:107
holds a vector of std::pair of Ptr<Ipv4> and interface index.
IPv4 GlobalRouting Bridge test.
IPv4 GlobalRouting LAN test.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
NodeContainer m_nodes
Nodes used in the test.
virtual int ShutdownSend(void)=0
A suite of tests to run.
Definition: test.h:1342
uint32_t GetRecvIf(void) const
Get the tag&#39;s receiving interface.
NodeContainer m_nodes
Nodes used in the test.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
uint16_t m_count
Number of packets received.
aggregate IP/TCP/UDP functionality to existing Nodes.
#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:285
Ipv4Address GetDest(void) const
virtual Ptr< Socket > CreateSocket(void)=0
encapsulates test code
Definition: test.h:1155
Ipv4Address GetGateway(void) const
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition: data-rate.cc:249
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:85
NodeContainer m_nodes
Nodes used in the test.
void SetUp(char *deviceName)
std::vector< uint8_t > m_secondInterface
Packets received on the 2nd interface at a given time.
virtual int Listen(void)=0
Listen for incoming connections.
std::vector< std::pair< Ptr< Socket >, bool > > m_sendSocks
Sending sockets.
Class for representing data rates.
Definition: data-rate.h:88
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:358
IPv4 Dynamic GlobalRouting /32 test.
A record of an IPv4 routing table entry for Ipv4GlobalRouting and Ipv4StaticRouting.
#define max(a, b)
Definition: 80211b.c:43
virtual void DoRun(void)
Implementation to actually run this TestCase.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
virtual void SetUp(uint32_t interface)=0
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
IPv4 GlobalRouting Two bridges test.
Global routing protocol for IPv4 stacks.
#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:168
void DoSendData(Ptr< Socket > socket, std::string to)
Send a packet.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
Definition: bridge-helper.h:37
void SendData(uint8_t index)
Send some data.
void ReceivePkt(Ptr< Socket > socket)
Receive a packet.
uint32_t GetNRoutes(void) const
Get the number of individual unicast routes that have been added to the routing table.
NodeContainer m_nodes
Nodes used in the test.
Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const
Get the routing protocol to be used by this Ipv4 stack.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Implement the IPv4 layer.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
address
Definition: first.py:37
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
Ipv4RoutingTableEntry * GetRoute(uint32_t i) const
Get a route from the global unicast routing table.
void SendData(Ptr< Socket > socket, std::string to)
Send a packet.
void HandleRead(Ptr< Socket > socket)
Handle an incoming packet.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
#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:624
a class to store IPv4 address information on an interface
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual void SetAddress(Address address)
Set the address of this interface.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Helper class that adds ns3::Ipv4GlobalRouting objects.
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1030
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:365
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
API to create UDP socket instances.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
void ShutDownSock(uint8_t index)
Shutdown a socket.
build a set of SimpleNetDevice objects
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:874
NodeContainer n0n2
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
a unique identifier for an interface.
Definition: type-id.h:58
static Ipv4GlobalRoutingTestSuite g_globalRoutingTestSuite
Static variable for test initialization.
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
std::vector< uint8_t > m_firstInterface
Packets received on the 1st interface at a given time.
NodeContainer n1n2
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.