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 
120 class LinkTest : public TestCase
121 {
122 public:
123  virtual void DoSetup (void);
124  virtual void DoRun (void);
125  LinkTest ();
126 private:
128 };
129 
131  : TestCase ("Global routing on point-to-point link")
132 {
133 }
134 
135 void
137 {
138  m_nodes.Create (2);
139 
140  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
141  SimpleNetDeviceHelper simpleHelper;
142  simpleHelper.SetNetDevicePointToPointMode (true);
143  NetDeviceContainer net = simpleHelper.Install (m_nodes, channel);
144 
145  InternetStackHelper internet;
146  // By default, InternetStackHelper adds a static and global routing
147  // implementation. We just want the global for this test.
148  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
149  internet.SetRoutingHelper (ipv4RoutingHelper);
150  internet.Install (m_nodes);
151 
152  Ipv4AddressHelper ipv4;
153  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
154  Ipv4InterfaceContainer i = ipv4.Assign (net);
155 }
156 
157 void
159 {
160  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
161 
163  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
165  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
166  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
167  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
168  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
169  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
170  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
171  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
172 
173  // Test that the right number of routes found
174  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
175  NS_LOG_DEBUG ("LinkTest nRoutes0 " << nRoutes0);
176  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- not one route");
177  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0);
178  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
179  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination");
180  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
181 
182  // Test that the right number of routes found
183  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
184  NS_TEST_ASSERT_MSG_EQ (nRoutes1, 1, "Error-- not one route");
185  NS_LOG_DEBUG ("LinkTest nRoutes1 " << nRoutes1);
186  route = globalRouting1->GetRoute (0);
187  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
188  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination");
189  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway");
190 
191  bool result = true;
192 
193  NS_TEST_ASSERT_MSG_EQ (result, true, "Message");
194  Simulator::Run ();
195  Simulator::Destroy ();
196 }
197 
198 class LanTest : public TestCase
199 {
200 public:
201  virtual void DoSetup (void);
202  virtual void DoRun (void);
203  LanTest ();
204 private:
206 };
207 
209  : TestCase ("Global routing on broadcast link")
210 {
211 }
212 
213 void
215 {
216  m_nodes.Create (2);
217 
218  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
219  SimpleNetDeviceHelper simpleHelper;
220  NetDeviceContainer net = simpleHelper.Install (m_nodes, channel);
221 
222  InternetStackHelper internet;
223  // By default, InternetStackHelper adds a static and global routing
224  // implementation. We just want the global for this test.
225  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
226  internet.SetRoutingHelper (ipv4RoutingHelper);
227  internet.Install (m_nodes);
228 
229  Ipv4AddressHelper ipv4;
230  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
231  Ipv4InterfaceContainer i = ipv4.Assign (net);
232 }
233 
234 void
236 {
237  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
238 
240  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
242  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
243  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
244  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
245  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
246  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
247  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
248  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
249 
250  // Test that the right number of routes found
251  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
252  NS_LOG_DEBUG ("LanTest nRoutes0 " << nRoutes0);
253  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- more than one entry");
254  for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++)
255  {
256  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (i);
257  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
258  }
259 
260  // Test that the right number of routes found
261  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
262  NS_LOG_DEBUG ("LanTest nRoutes1 " << nRoutes1);
263  NS_TEST_ASSERT_MSG_EQ (nRoutes1, 1, "Error-- more than one entry");
264  for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++)
265  {
266  Ipv4RoutingTableEntry* route = globalRouting1->GetRoute (i);
267  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
268  }
269 
270  Simulator::Destroy ();
271 }
272 
273 class TwoLinkTest : public TestCase
274 {
275 public:
276  virtual void DoSetup (void);
277  virtual void DoRun (void);
278  TwoLinkTest ();
279 private:
281 };
282 
284  : TestCase ("Global routing across two hops (point-to-point links)")
285 {
286 }
287 
288 void
290 {
291  m_nodes.Create (3);
292 
293  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
294  SimpleNetDeviceHelper simpleHelper;
295  simpleHelper.SetNetDevicePointToPointMode (true);
296  NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel);
297  net.Add (simpleHelper.Install (m_nodes.Get (1), channel));
298 
299  Ptr<SimpleChannel> channel2 = CreateObject <SimpleChannel> ();
300  SimpleNetDeviceHelper simpleHelper2;
301  simpleHelper2.SetNetDevicePointToPointMode (true);
302  NetDeviceContainer net2 = simpleHelper.Install (m_nodes.Get (1), channel2);
303  net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2));
304 
305  InternetStackHelper internet;
306  // By default, InternetStackHelper adds a static and global routing
307  // implementation. We just want the global for this test.
308  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
309  internet.SetRoutingHelper (ipv4RoutingHelper);
310  internet.Install (m_nodes);
311 
312  Ipv4AddressHelper ipv4;
313  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
314  Ipv4InterfaceContainer i = ipv4.Assign (net);
315  ipv4.SetBase ("10.1.2.0", "255.255.255.252");
316  Ipv4InterfaceContainer i2 = ipv4.Assign (net2);
317 }
318 
319 void
321 {
322  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
323 
325  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
327  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
329  NS_TEST_ASSERT_MSG_NE (ip2, 0, "Error-- no Ipv4 object");
330  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
331  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
332  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
333  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
334  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
335  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
336  Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol ();
337  Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject <Ipv4GlobalRouting> ();
338  NS_TEST_ASSERT_MSG_NE (globalRouting2, 0, "Error-- no Ipv4GlobalRouting object");
339 
340  // node n0
341  // Test that the right number of routes found
342  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
343  NS_LOG_DEBUG ("TwoLinkTest nRoutes0 " << nRoutes0);
344  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 1, "Error-- wrong number of links");
345 
346  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0);
347  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
348  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination");
349  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
350 
351  // node n1
352  // Test that the right number of routes found
353  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
354  NS_LOG_DEBUG ("TwoLinkTest nRoutes1 " << nRoutes1);
355  route = globalRouting1->GetRoute (0);
356  NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
357  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.1"), "Error-- wrong destination");
358  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway");
359  route = globalRouting1->GetRoute (1);
360  NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
361  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.2"), "Error-- wrong destination");
362  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.2"), "Error-- wrong gateway");
363  route = globalRouting1->GetRoute (2);
364  NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
365  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
366  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.1"), "Error-- wrong gateway");
367  route = globalRouting1->GetRoute (3);
368  NS_LOG_DEBUG ("TwoLinkTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
369  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination");
370  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.2"), "Error-- wrong gateway");
371 
372  // node n2
373  // Test that the right number of routes found
374  uint32_t nRoutes2 = globalRouting2->GetNRoutes ();
375  NS_LOG_DEBUG ("TwoLinkTest nRoutes2 " << nRoutes2);
376  NS_TEST_ASSERT_MSG_EQ (nRoutes2, 1, "Error-- wrong number of links");
377 
378  route = globalRouting2->GetRoute (0);
379  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
380  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("0.0.0.0"), "Error-- wrong destination");
381  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.1"), "Error-- wrong gateway");
382 
383  Simulator::Destroy ();
384 }
385 
386 class TwoLanTest : public TestCase
387 {
388 public:
389  virtual void DoSetup (void);
390  virtual void DoRun (void);
391  TwoLanTest ();
392 private:
394 };
395 
397  : TestCase ("Global routing across two hops (broadcast links)")
398 {
399 }
400 
401 void
403 {
404  m_nodes.Create (3);
405 
406  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
407  SimpleNetDeviceHelper simpleHelper;
408  NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel);
409  net.Add (simpleHelper.Install (m_nodes.Get (1), channel));
410 
411  Ptr<SimpleChannel> channel2 = CreateObject <SimpleChannel> ();
412  SimpleNetDeviceHelper simpleHelper2;
413  NetDeviceContainer net2 = simpleHelper.Install (m_nodes.Get (1), channel2);
414  net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2));
415 
416  InternetStackHelper internet;
417  // By default, InternetStackHelper adds a static and global routing
418  // implementation. We just want the global for this test.
419  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
420  internet.SetRoutingHelper (ipv4RoutingHelper);
421  internet.Install (m_nodes);
422 
423  Ipv4AddressHelper ipv4;
424  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
425  Ipv4InterfaceContainer i = ipv4.Assign (net);
426  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
427  Ipv4InterfaceContainer i2 = ipv4.Assign (net2);
428 }
429 
430 void
432 {
433  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
434 
436  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
438  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
440  NS_TEST_ASSERT_MSG_NE (ip2, 0, "Error-- no Ipv4 object");
441  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
442  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
443  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
444  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
445  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
446  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
447  Ptr<Ipv4RoutingProtocol> routing2 = ip2->GetRoutingProtocol ();
448  Ptr<Ipv4GlobalRouting> globalRouting2 = routing2->GetObject <Ipv4GlobalRouting> ();
449  NS_TEST_ASSERT_MSG_NE (globalRouting2, 0, "Error-- no Ipv4GlobalRouting object");
450 
451  // Test that the right number of routes found
452  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
453  NS_LOG_DEBUG ("TwoLanTest nRoutes0 " << nRoutes0);
454  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 2, "Error-- not two entries");
455  Ipv4RoutingTableEntry* route = globalRouting0->GetRoute (0);
456  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
457  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
458  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway");
459  route = globalRouting0->GetRoute (1);
460  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
461  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination");
462  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
463 
464  // Test that the right number of routes found
465  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
466  NS_LOG_DEBUG ("TwoLanTest nRoutes1 " << nRoutes1);
467  NS_TEST_ASSERT_MSG_EQ (nRoutes1, 2, "Error-- not two entries");
468  route = globalRouting1->GetRoute (0);
469  NS_LOG_DEBUG ("TwoLanTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
470  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
471  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway");
472  route = globalRouting1->GetRoute (1);
473  NS_LOG_DEBUG ("TwoLanTest entry dest " << route->GetDest () << " gw " << route->GetGateway ());
474  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination");
475  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("0.0.0.0"), "Error-- wrong gateway");
476 
477  Simulator::Destroy ();
478 }
479 
480 class BridgeTest : public TestCase
481 {
482 public:
483  virtual void DoSetup (void);
484  virtual void DoRun (void);
485  BridgeTest ();
486 private:
488 };
489 
491  : TestCase ("Global routing across bridging topology (bug 2102)")
492 {
493 }
494 
495 void
497 {
498  m_nodes.Create (5);
499 
500  //connect node0 to node1
501  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
502  SimpleNetDeviceHelper simpleHelper;
503  NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel);
504  net.Add (simpleHelper.Install (m_nodes.Get (1), channel));
505 
506  NetDeviceContainer bridgeFacingDevices;
507  NetDeviceContainer switchDevices;
508 
509  //connect node1 to node2 (switch)
510  Ptr<SimpleChannel> channel2 = CreateObject <SimpleChannel> ();
511  SimpleNetDeviceHelper simpleHelper2;
512  NetDeviceContainer net2 = simpleHelper2.Install (m_nodes.Get (1), channel2);
513  net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2));
514  bridgeFacingDevices.Add (net2.Get (0));
515  switchDevices.Add (net2.Get (1));
516 
517  //connect node2 (switch) to node3
518  Ptr<SimpleChannel> channel3 = CreateObject <SimpleChannel> ();
519  SimpleNetDeviceHelper simpleHelper3;
520  NetDeviceContainer net3 = simpleHelper3.Install (m_nodes.Get (2), channel3);
521  net3.Add (simpleHelper3.Install (m_nodes.Get (3), channel3));
522  bridgeFacingDevices.Add (net3.Get (1));
523  switchDevices.Add (net3.Get (0));
524 
525  //connect node3 to node4
526  Ptr<SimpleChannel> channel4 = CreateObject <SimpleChannel> ();
527  SimpleNetDeviceHelper simpleHelper4;
528  NetDeviceContainer net4 = simpleHelper4.Install (m_nodes.Get (3), channel4);
529  net4.Add (simpleHelper4.Install (m_nodes.Get (4), channel4));
530 
531  Ptr<Node> switchNode = m_nodes.Get (2);
532  BridgeHelper bridge;
533  bridge.Install (switchNode, switchDevices);
534 
535  InternetStackHelper internet;
536  // By default, InternetStackHelper adds a static and global routing
537  // implementation. We just want the global for this test.
538  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
539  internet.SetRoutingHelper (ipv4RoutingHelper);
540 
541  internet.Install (m_nodes.Get (0));
542  internet.Install (m_nodes.Get (1));
543  // m_nodes.Get (2) is bridge node
544  internet.Install (m_nodes.Get (3));
545  internet.Install (m_nodes.Get (4));
546 
548  address.SetBase ("10.1.1.0", "255.255.255.0");
549  address.Assign (net);
550 
551  address.SetBase ("10.1.2.0", "255.255.255.0");
552  address.Assign (bridgeFacingDevices);
553 
554  address.SetBase ("10.1.3.0", "255.255.255.0");
555  address.Assign (net4);
556 
557 }
558 
559 void
561 {
562  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
563 
565  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
566  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
567  NS_TEST_ASSERT_MSG_NE (routing0, 0, "Error-- no Ipv4 routing protocol object");
568  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
569  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
570 
572  NS_TEST_ASSERT_MSG_NE (ip1, 0, "Error-- no Ipv4 object");
573  Ptr<Ipv4RoutingProtocol> routing1 = ip1->GetRoutingProtocol ();
574  NS_TEST_ASSERT_MSG_NE (routing1, 0, "Error-- no Ipv4 routing protocol object");
575  Ptr<Ipv4GlobalRouting> globalRouting1 = routing1->GetObject <Ipv4GlobalRouting> ();
576  NS_TEST_ASSERT_MSG_NE (globalRouting1, 0, "Error-- no Ipv4GlobalRouting object");
577 
578  // Skip to n4
580  NS_TEST_ASSERT_MSG_NE (ip4, 0, "Error-- no Ipv4 object");
581  Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol ();
582  NS_TEST_ASSERT_MSG_NE (routing4, 0, "Error-- no Ipv4 routing protocol object");
583  Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject <Ipv4GlobalRouting> ();
584  NS_TEST_ASSERT_MSG_NE (globalRouting4, 0, "Error-- no Ipv4GlobalRouting object");
585 
586  Ipv4RoutingTableEntry* route = 0;
587  // n0
588  // Test that the right number of routes found
589  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
590  NS_LOG_DEBUG ("BridgeTest nRoutes0 " << nRoutes0);
591  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 3, "Error-- not three entries");
592  for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++)
593  {
594  route = globalRouting0->GetRoute (i);
595  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
596  }
597  // Spot check the last route
598  if (route)
599  {
600  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.3.0"), "Error-- wrong destination");
601  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
602  }
603 
604  // n1
605  // Test that the right number of routes found
606  route = 0;
607  uint32_t nRoutes1 = globalRouting1->GetNRoutes ();
608  NS_LOG_DEBUG ("BridgeTest nRoutes1 " << nRoutes1);
609  NS_TEST_ASSERT_MSG_EQ (nRoutes1, 3, "Error-- not three entries");
610  for (uint32_t i = 0; i < globalRouting1->GetNRoutes (); i++)
611  {
612  route = globalRouting1->GetRoute (i);
613  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
614  }
615  // Spot check the last route
616  if (route)
617  {
618  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.3.0"), "Error-- wrong destination");
619  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.2"), "Error-- wrong gateway");
620  }
621 
622  // skip n2 and n3, just verify n4
623  NS_LOG_DEBUG ("BridgeTest skip print out of n2 and n3, go next to node n4");
624 
625  // n4
626  route = 0;
627  // Test that the right number of routes found
628  uint32_t nRoutes4 = globalRouting4->GetNRoutes ();
629  NS_LOG_DEBUG ("BridgeTest nRoutes4 " << nRoutes4);
630  NS_TEST_ASSERT_MSG_EQ (nRoutes4, 3, "Error-- not three entries");
631  for (uint32_t i = 0; i < globalRouting4->GetNRoutes (); i++)
632  {
633  route = globalRouting4->GetRoute (i);
634  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
635  }
636  // Spot check the last route
637  if (route)
638  {
639  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
640  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.3.1"), "Error-- wrong gateway");
641  }
642 
643  Simulator::Destroy ();
644 }
645 
646 class TwoBridgeTest : public TestCase
647 {
648 public:
649  virtual void DoSetup (void);
650  virtual void DoRun (void);
651  TwoBridgeTest ();
652 private:
654 };
655 
657  : TestCase ("Global routing across two bridges")
658 {
659 }
660 
661 void
663 {
664  m_nodes.Create (5);
665 
666  //connect node0 to node1
667  Ptr<SimpleChannel> channel = CreateObject <SimpleChannel> ();
668  SimpleNetDeviceHelper simpleHelper;
669  NetDeviceContainer net = simpleHelper.Install (m_nodes.Get (0), channel);
670  net.Add (simpleHelper.Install (m_nodes.Get (1), channel));
671 
672  NetDeviceContainer bridgeFacingDevices;
673  NetDeviceContainer switchn2Devices;
674  NetDeviceContainer switchn3Devices;
675 
676  //connect node1 to node2 (switch)
677  Ptr<SimpleChannel> channel2 = CreateObject <SimpleChannel> ();
678  SimpleNetDeviceHelper simpleHelper2;
679  NetDeviceContainer net2 = simpleHelper2.Install (m_nodes.Get (1), channel2);
680  net2.Add (simpleHelper2.Install (m_nodes.Get (2), channel2));
681  bridgeFacingDevices.Add (net2.Get (0));
682  switchn2Devices.Add (net2.Get (1));
683 
684  //connect node2 (switch) to node3
685  Ptr<SimpleChannel> channel3 = CreateObject <SimpleChannel> ();
686  SimpleNetDeviceHelper simpleHelper3;
687  NetDeviceContainer net3 = simpleHelper3.Install (m_nodes.Get (2), channel3);
688  net3.Add (simpleHelper3.Install (m_nodes.Get (3), channel3));
689  switchn2Devices.Add (net3.Get (0));
690  switchn3Devices.Add (net3.Get (1));
691 
692  //connect node3 to node4
693  Ptr<SimpleChannel> channel4 = CreateObject <SimpleChannel> ();
694  SimpleNetDeviceHelper simpleHelper4;
695  NetDeviceContainer net4 = simpleHelper4.Install (m_nodes.Get (3), channel4);
696  net4.Add (simpleHelper4.Install (m_nodes.Get (4), channel4));
697  switchn3Devices.Add (net4.Get (0));
698  bridgeFacingDevices.Add (net4.Get (1));
699 
700  Ptr<Node> switchn2Node = m_nodes.Get (2);
701  BridgeHelper bridgen2Helper;
702  bridgen2Helper.Install (switchn2Node, switchn2Devices);
703 
704  Ptr<Node> switchn3Node = m_nodes.Get (3);
705  BridgeHelper bridgen3Helper;
706  bridgen3Helper.Install (switchn3Node, switchn3Devices);
707 
708  InternetStackHelper internet;
709  // By default, InternetStackHelper adds a static and global routing
710  // implementation. We just want the global for this test.
711  Ipv4GlobalRoutingHelper ipv4RoutingHelper;
712  internet.SetRoutingHelper (ipv4RoutingHelper);
713 
714  internet.Install (m_nodes.Get (0));
715  internet.Install (m_nodes.Get (1));
716  // m_nodes.Get (2) is bridge node
717  // m_nodes.Get (3) is bridge node
718  internet.Install (m_nodes.Get (4));
719 
721  address.SetBase ("10.1.1.0", "255.255.255.0");
722  address.Assign (net);
723 
724  address.SetBase ("10.1.2.0", "255.255.255.0");
725  address.Assign (bridgeFacingDevices);
726 
727 }
728 
729 void
731 {
732  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
733 
735  NS_TEST_ASSERT_MSG_NE (ip0, 0, "Error-- no Ipv4 object");
736  Ptr<Ipv4RoutingProtocol> routing0 = ip0->GetRoutingProtocol ();
737  NS_TEST_ASSERT_MSG_NE (routing0, 0, "Error-- no Ipv4 routing protocol object");
738  Ptr<Ipv4GlobalRouting> globalRouting0 = routing0->GetObject <Ipv4GlobalRouting> ();
739  NS_TEST_ASSERT_MSG_NE (globalRouting0, 0, "Error-- no Ipv4GlobalRouting object");
740 
741  // Skip to n4
743  NS_TEST_ASSERT_MSG_NE (ip4, 0, "Error-- no Ipv4 object");
744  Ptr<Ipv4RoutingProtocol> routing4 = ip4->GetRoutingProtocol ();
745  NS_TEST_ASSERT_MSG_NE (routing4, 0, "Error-- no Ipv4 routing protocol object");
746  Ptr<Ipv4GlobalRouting> globalRouting4 = routing4->GetObject <Ipv4GlobalRouting> ();
747  NS_TEST_ASSERT_MSG_NE (globalRouting4, 0, "Error-- no Ipv4GlobalRouting object");
748 
749  Ipv4RoutingTableEntry* route = 0;
750  // n0
751  // Test that the right number of routes found
752  uint32_t nRoutes0 = globalRouting0->GetNRoutes ();
753  NS_LOG_DEBUG ("BridgeTest nRoutes0 " << nRoutes0);
754  NS_TEST_ASSERT_MSG_EQ (nRoutes0, 2, "Error-- not two entries");
755  for (uint32_t i = 0; i < globalRouting0->GetNRoutes (); i++)
756  {
757  route = globalRouting0->GetRoute (i);
758  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
759  }
760  // Spot check the last route
761  if (route)
762  {
763  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.2.0"), "Error-- wrong destination");
764  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.1.2"), "Error-- wrong gateway");
765  }
766  // skip n2 and n3, just verify n4
767  NS_LOG_DEBUG ("BridgeTest skip print out of n1-n3, go next to node n4");
768 
769  // n4
770  // Test that the right number of routes found
771  route = 0;
772  uint32_t nRoutes4 = globalRouting4->GetNRoutes ();
773  NS_LOG_DEBUG ("BridgeTest nRoutes4 " << nRoutes4);
774  NS_TEST_ASSERT_MSG_EQ (nRoutes4, 2, "Error-- not two entries");
775  for (uint32_t i = 0; i < globalRouting4->GetNRoutes (); i++)
776  {
777  route = globalRouting4->GetRoute (i);
778  NS_LOG_DEBUG ("entry dest " << route->GetDest () << " gw " << route->GetGateway ());
779  }
780  // Spot check the last route
781  if (route)
782  {
783  NS_TEST_ASSERT_MSG_EQ (route->GetDest (), Ipv4Address ("10.1.1.0"), "Error-- wrong destination");
784  NS_TEST_ASSERT_MSG_EQ (route->GetGateway (), Ipv4Address ("10.1.2.1"), "Error-- wrong gateway");
785  }
786 
787  Simulator::Destroy ();
788 }
789 
791 {
792 public:
795 
796 private:
797  void SendData (uint8_t index);
798  void ShutDownSock (uint8_t index);
799  void HandleRead (Ptr<Socket>);
800  virtual void DoRun (void);
801 
802  int m_count;
803  std::vector<std::pair<Ptr<Socket>, bool> > m_sendSocks;
805  uint16_t m_packetSize;
806  std::vector<uint8_t> m_firstInterface;
807  std::vector<uint8_t> m_secondInterface;
808 };
809 
810 // Add some help text to this case to describe what it is intended to test
812  : TestCase ("Dynamic global routing example"), m_count (0)
813 {
814  m_firstInterface.resize (16);
815  m_secondInterface.resize (16);
816  m_dataRate = DataRate ("2kbps");
817  m_packetSize = 50;
818 }
819 
821 {
822  std::vector<std::pair<Ptr<Socket>, bool> >::iterator iter;
823 
824  for (iter = m_sendSocks.begin (); iter != m_sendSocks.end (); iter++)
825  {
826  if (iter->second)
827  {
828  iter->second = false;
829  iter->first->Close ();
830  iter->first = 0;
831  }
832  }
833 }
834 
835 void
837 {
838  Ptr<Packet> packet;
839  Address from;
840  while ((packet = socket->RecvFrom (from)))
841  {
842  if (packet->GetSize () == 0)
843  { //EOF
844  break;
845  }
846  Ipv4PacketInfoTag tag;
847  bool found;
848  found = packet->PeekPacketTag (tag);
849  uint8_t now = static_cast<uint8_t> (Simulator::Now ().GetSeconds ());
850  if (found)
851  {
852  if (tag.GetRecvIf () == 1)
853  {
854  m_firstInterface[now]++;
855  }
856  if (tag.GetRecvIf () == 2)
857  {
858  m_secondInterface[now]++;
859  }
860  m_count++;
861  }
862  }
863 }
864 
865 void
867 {
868  if (m_sendSocks[index].second == false)
869  {
870  return;
871  }
872  Ptr<Packet> packet = Create<Packet> (m_packetSize);
873  m_sendSocks[index].first->Send (packet);
874 
875  Time tNext (MicroSeconds (m_packetSize * 8 * 1e6 / m_dataRate.GetBitRate ()));
876  Simulator::Schedule (tNext, &Ipv4DynamicGlobalRoutingTestCase::SendData, this, index);
877 }
878 
879 void
881 {
882  m_sendSocks[index].second = false;
883  m_sendSocks[index].first->Close ();
884  m_sendSocks[index].first = 0;
885 }
886 
887 // Test derived from examples/routing/dynamic-global-routing.cc
888 //
889 // Network topology
890 //
891 // n0
892 // \ p-p
893 // \ (shared csma/cd)
894 // n2 -------------------------n3
895 // / | |
896 // / p-p n4 n5 ---------- n6
897 // n1 p-p
898 // | |
899 // ----------------------------------------
900 // p-p
901 //
902 // Test that for node n6, the interface facing n5 receives packets at
903 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
904 // facing n1 receives packets at times (2-4), (6-8), (12-13)
905 //
906 void
908 {
909  // The below value configures the default behavior of global routing.
910  // By default, it is disabled. To respond to interface events, set to true
911  Config::SetDefault ("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue (true));
912 
913  NodeContainer c;
914  c.Create (7);
915  NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
916  NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
917  NodeContainer n5n6 = NodeContainer (c.Get (5), c.Get (6));
918  NodeContainer n1n6 = NodeContainer (c.Get (1), c.Get (6));
919  NodeContainer n2345 = NodeContainer (c.Get (2), c.Get (3), c.Get (4), c.Get (5));
920 
921  InternetStackHelper internet;
922  internet.Install (c);
923 
924  // We create the channels first without any IP addressing information
925  SimpleNetDeviceHelper devHelper;
926 
927  devHelper.SetNetDevicePointToPointMode (true);
928  NetDeviceContainer d0d2 = devHelper.Install (n0n2);
929  devHelper.SetNetDevicePointToPointMode (false);
930 
931  NetDeviceContainer d1d6 = devHelper.Install (n1n6);
932  NetDeviceContainer d1d2 = devHelper.Install (n1n2);
933  NetDeviceContainer d5d6 = devHelper.Install (n5n6);
934  NetDeviceContainer d2345 = devHelper.Install (n2345);
935 
936  // Later, we add IP addresses.
937  Ipv4AddressHelper ipv4;
938  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
939  ipv4.Assign (d0d2);
940 
941  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
942  ipv4.Assign (d1d2);
943 
944  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
945  Ipv4InterfaceContainer i5i6 = ipv4.Assign (d5d6);
946 
947  ipv4.SetBase ("10.250.1.0", "255.255.255.0");
948  ipv4.Assign (d2345);
949 
950  ipv4.SetBase ("172.16.1.0", "255.255.255.0");
951  Ipv4InterfaceContainer i1i6 = ipv4.Assign (d1d6);
952 
953  // Create router nodes, initialize routing database and set up the routing
954  // tables in the nodes.
955  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
956 
957  // Create the applications to send UDP datagrams of size
958  // 50 bytes at a rate of 2 Kb/s
959  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
960  uint16_t port = 9; // Discard port (RFC 863)
961 
962  std::pair<Ptr<Socket>, bool> sendSockA;
963  sendSockA.first = Socket::CreateSocket (c.Get (1), tid);
964  sendSockA.first->Bind ();
965  sendSockA.first->Connect (InetSocketAddress (i5i6.GetAddress (1), port));
966  sendSockA.second = true;
967  m_sendSocks.push_back (sendSockA);
968  Simulator::Schedule (Seconds (1.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 0);
969  Simulator::Schedule (Seconds (10.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 0);
970 
971  std::pair<Ptr<Socket>, bool> sendSockB;
972  sendSockB.first = Socket::CreateSocket (c.Get (1), tid);
973  sendSockB.first->Bind ();
974  sendSockB.first->Connect (InetSocketAddress (i1i6.GetAddress (1), port));
975  sendSockB.second = true;
976  m_sendSocks.push_back (sendSockB);
977  Simulator::Schedule (Seconds (11.0), &Ipv4DynamicGlobalRoutingTestCase::SendData, this, 1);
978  Simulator::Schedule (Seconds (16.0), &Ipv4DynamicGlobalRoutingTestCase::ShutDownSock, this, 1);
979 
980 
981  // Create an optional packet sink to receive these packets
982  Ptr<Socket> sink2 = Socket::CreateSocket (c.Get (6), tid);
983  sink2->Bind (Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
984  sink2->Listen ();
985  sink2->ShutdownSend ();
986 
987  sink2->SetRecvPktInfo (true);
989 
990  Ptr<Node> n1 = c.Get (1);
991  Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
992  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
993  // then the next p2p is numbered 2
994  uint32_t ipv4ifIndex1 = 2;
995 
996  Simulator::Schedule (Seconds (2), &Ipv4::SetDown,ipv41, ipv4ifIndex1);
997  Simulator::Schedule (Seconds (4), &Ipv4::SetUp,ipv41, ipv4ifIndex1);
998 
999  Ptr<Node> n6 = c.Get (6);
1000  Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4> ();
1001  // The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
1002  // then the next p2p is numbered 2
1003  uint32_t ipv4ifIndex6 = 2;
1004  Simulator::Schedule (Seconds (6), &Ipv4::SetDown,ipv46, ipv4ifIndex6);
1005  Simulator::Schedule (Seconds (8), &Ipv4::SetUp,ipv46, ipv4ifIndex6);
1006 
1007  Simulator::Schedule (Seconds (12), &Ipv4::SetDown,ipv41, ipv4ifIndex1);
1008  Simulator::Schedule (Seconds (14), &Ipv4::SetUp,ipv41, ipv4ifIndex1);
1009 
1010  Simulator::Run ();
1011 
1012  NS_TEST_ASSERT_MSG_EQ (m_count, 70, "Dynamic global routing did not deliver all packets");
1013 // Test that for node n6, the interface facing n5 receives packets at
1014 // times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
1015 // facing n1 receives packets at times (2-4), (6-8), (12-13)
1016  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[1], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[1]));
1017  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[2], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[2]));
1018  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[3], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[3]));
1019  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[4], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[4]));
1020  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[5], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[5]));
1021  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[6], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[6]));
1022  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[7], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[7]));
1023  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[8], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[8]));
1024  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[9], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[9]));
1025  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[10], 0, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[10]));
1026  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[11], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[11]));
1027  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[12], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[12]));
1028  NS_TEST_ASSERT_MSG_EQ (m_secondInterface[13], 5, "Dynamic global routing did not deliver all packets " << int(m_secondInterface[13]));
1029  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[14], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[14]));
1030  NS_TEST_ASSERT_MSG_EQ (m_firstInterface[15], 5, "Dynamic global routing did not deliver all packets " << int(m_firstInterface[15]));
1031  Simulator::Destroy ();
1032 }
1033 
1035 {
1036 public:
1039 
1041  void ReceivePkt (Ptr<Socket> socket);
1042  void DoSendData (Ptr<Socket> socket, std::string to);
1043  void SendData (Ptr<Socket> socket, std::string to);
1044 
1045 private:
1046  virtual void DoRun (void);
1047 };
1048 
1049 // Add some help text to this case to describe what it is intended to test
1051  : TestCase ("Slash 32 global routing example")
1052 {
1053 }
1054 
1056 {
1057 }
1058 
1059 void
1061 {
1062  uint32_t availableData;
1063  availableData = socket->GetRxAvailable ();
1065  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
1066  //cast availableData to void, to suppress 'availableData' set but not used
1067  //compiler warning
1068  (void) availableData;
1069 }
1070 
1071 void
1073 {
1074  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 1234);
1075  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
1076  123, "100");
1077 }
1078 
1079 void
1081 {
1082  m_receivedPacket = Create<Packet> ();
1083  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (60),
1085  Simulator::Stop (Seconds (66));
1086  Simulator::Run ();
1087 }
1088 
1089 // Test program for this 3-router scenario, using global routing
1090 //
1091 // (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
1092 //
1093 void
1095 {
1096  Ptr<Node> nA = CreateObject<Node> ();
1097  Ptr<Node> nB = CreateObject<Node> ();
1098  Ptr<Node> nC = CreateObject<Node> ();
1099 
1100  NodeContainer c = NodeContainer (nA, nB, nC);
1101 
1102  InternetStackHelper internet;
1103  internet.Install (c);
1104 
1105  // simple links
1106  NodeContainer nAnB = NodeContainer (nA, nB);
1107  NodeContainer nBnC = NodeContainer (nB, nC);
1108 
1109  SimpleNetDeviceHelper devHelper;
1110 
1111  Ptr<SimpleNetDevice> deviceA = CreateObject<SimpleNetDevice> ();
1112  deviceA->SetAddress (Mac48Address::Allocate ());
1113  nA->AddDevice (deviceA);
1114 
1115  NetDeviceContainer dAdB = devHelper.Install (nAnB);
1116  NetDeviceContainer dBdC = devHelper.Install (nBnC);
1117 
1118  Ptr<SimpleNetDevice> deviceC = CreateObject<SimpleNetDevice> ();
1119  deviceC->SetAddress (Mac48Address::Allocate ());
1120  nC->AddDevice (deviceC);
1121 
1122  // Later, we add IP addresses.
1123  Ipv4AddressHelper ipv4;
1124  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
1125  Ipv4InterfaceContainer iAiB = ipv4.Assign (dAdB);
1126 
1127  ipv4.SetBase ("10.1.1.4", "255.255.255.252");
1128  Ipv4InterfaceContainer iBiC = ipv4.Assign (dBdC);
1129 
1130  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4> ();
1131  Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4> ();
1132  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4> ();
1133 
1134  int32_t ifIndexA = ipv4A->AddInterface (deviceA);
1135  int32_t ifIndexC = ipv4C->AddInterface (deviceC);
1136 
1137  Ipv4InterfaceAddress ifInAddrA = Ipv4InterfaceAddress (Ipv4Address ("172.16.1.1"), Ipv4Mask ("/32"));
1138  ipv4A->AddAddress (ifIndexA, ifInAddrA);
1139  ipv4A->SetMetric (ifIndexA, 1);
1140  ipv4A->SetUp (ifIndexA);
1141 
1142  Ipv4InterfaceAddress ifInAddrC = Ipv4InterfaceAddress (Ipv4Address ("192.168.1.1"), Ipv4Mask ("/32"));
1143  ipv4C->AddAddress (ifIndexC, ifInAddrC);
1144  ipv4C->SetMetric (ifIndexC, 1);
1145  ipv4C->SetUp (ifIndexC);
1146 
1147  // Create router nodes, initialize routing database and set up the routing
1148  // tables in the nodes.
1149  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
1150 
1151  // Create the UDP sockets
1152  Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory> ();
1153  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
1154  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("192.168.1.1"), 1234)), 0, "trivial");
1155  rxSocket->SetRecvCallback (MakeCallback (&Ipv4GlobalRoutingSlash32TestCase::ReceivePkt, this));
1156 
1157  Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory> ();
1158  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
1159  txSocket->SetAllowBroadcast (true);
1160 
1161  // ------ Now the tests ------------
1162 
1163  // Unicast test
1164  SendData (txSocket, "192.168.1.1");
1165  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "Static routing with /32 did not deliver all packets.");
1166 
1167  Simulator::Destroy ();
1168 }
1169 
1171 {
1172 public:
1174 };
1175 
1177  : TestSuite ("ipv4-global-routing", UNIT)
1178  {
1179  AddTestCase (new LinkTest, TestCase::QUICK);
1180  AddTestCase (new LanTest, TestCase::QUICK);
1181  AddTestCase (new TwoLinkTest, TestCase::QUICK);
1182  AddTestCase (new TwoLanTest, TestCase::QUICK);
1183  AddTestCase (new BridgeTest, TestCase::QUICK);
1184  AddTestCase (new TwoBridgeTest, TestCase::QUICK);
1185  AddTestCase (new Ipv4DynamicGlobalRoutingTestCase, TestCase::QUICK);
1186  AddTestCase (new Ipv4GlobalRoutingSlash32TestCase, TestCase::QUICK);
1187  }
1188 
1189 // Do not forget to allocate an instance of this TestSuite
virtual void DoSetup(void)
Implementation to do any local setup required for this TestCase.
tuple channel
Definition: third.py:85
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
AttributeValue implementation for Boolean.
Definition: boolean.h:34
Definition: second.py:1
virtual void DoRun(void)
Implementation to actually run this TestCase.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
holds a vector of std::pair of Ptr and interface index.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
virtual int ShutdownSend(void)=0
A suite of tests to run.
Definition: test.h:1333
#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:201
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:278
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
virtual Ptr< Socket > CreateSocket(void)=0
Ipv4RoutingTableEntry * GetRoute(uint32_t i) const
Get a route from the global unicast routing table.
encapsulates test code
Definition: test.h:1147
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
void SetUp(char *deviceName)
virtual int Listen(void)=0
Listen for incoming connections.
std::vector< std::pair< Ptr< Socket >, bool > > m_sendSocks
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
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:846
A record of an IPv4 routing table entry for Ipv4GlobalRouting and Ipv4StaticRouting.
#define max(a, b)
Definition: 80211b.c:45
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
virtual void DoRun(void)
Implementation to actually run this TestCase.
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.
uint32_t GetRecvIf(void) const
Get the tag's receiving interface.
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:161
void DoSendData(Ptr< Socket > socket, std::string to)
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
virtual void DoRun(void)
Implementation to actually run this TestCase.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
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.
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.
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition: data-rate.cc:249
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SendData(Ptr< Socket > socket, std::string to)
Ipv4Address GetDest(void) const
static Ipv4GlobalRoutingTestSuite globalRoutingTestSuite
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...
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
uint32_t GetId(void) const
Definition: node.cc:107
#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:617
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.
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Ipv4Address GetGateway(void) const
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:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
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:911
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:340
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
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.
build a set of SimpleNetDevice objects
NodeContainer n0n2
Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const
Get the routing protocol to be used by this Ipv4 stack.
a unique identifier for an interface.
Definition: type-id.h:58
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
NodeContainer n1n2
uint32_t GetNRoutes(void) const
Get the number of individual unicast routes that have been added to the routing table.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.