A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
static-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 // End-to-end tests for Ipv4 static routing
18 
19 #include "ns3/boolean.h"
20 #include "ns3/config.h"
21 #include "ns3/csma-helper.h"
22 #include "ns3/csma-net-device.h"
23 #include "ns3/inet-socket-address.h"
24 #include "ns3/internet-stack-helper.h"
25 #include "ns3/ipv4-address-helper.h"
26 #include "ns3/ipv4-static-routing-helper.h"
27 #include "ns3/node.h"
28 #include "ns3/node-container.h"
29 #include "ns3/on-off-helper.h"
30 #include "ns3/packet.h"
31 #include "ns3/packet-sink-helper.h"
32 #include "ns3/packet-sink.h"
33 #include "ns3/packet-socket-helper.h"
34 #include "ns3/packet-socket-address.h"
35 #include "ns3/point-to-point-helper.h"
36 #include "ns3/pointer.h"
37 #include "ns3/simulator.h"
38 #include "ns3/string.h"
39 #include "ns3/test.h"
40 #include "ns3/uinteger.h"
41 
42 using namespace ns3;
43 
45 {
46 public:
48  virtual ~StaticRoutingSlash32TestCase ();
49 
50 private:
51  virtual void DoRun (void);
52 };
53 
54 // Add some help text to this case to describe what it is intended to test
56  : TestCase ("Slash 32 static routing example")
57 {
58 }
59 
61 {
62 }
63 
64 // Test program for this 3-router scenario, using static routing
65 //
66 // (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
67 //
68 void
70 {
71  Ptr<Node> nA = CreateObject<Node> ();
72  Ptr<Node> nB = CreateObject<Node> ();
73  Ptr<Node> nC = CreateObject<Node> ();
74 
75  NodeContainer c = NodeContainer (nA, nB, nC);
76 
77  InternetStackHelper internet;
78  internet.Install (c);
79 
80  // Point-to-point links
81  NodeContainer nAnB = NodeContainer (nA, nB);
82  NodeContainer nBnC = NodeContainer (nB, nC);
83 
84  // We create the channels first without any IP addressing information
86  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
87  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
88  NetDeviceContainer dAdB = p2p.Install (nAnB);
89 
90  NetDeviceContainer dBdC = p2p.Install (nBnC);;
91 
92  Ptr<CsmaNetDevice> deviceA = CreateObject<CsmaNetDevice> ();
93  deviceA->SetAddress (Mac48Address::Allocate ());
94  nA->AddDevice (deviceA);
95 
96  Ptr<CsmaNetDevice> deviceC = CreateObject<CsmaNetDevice> ();
97  deviceC->SetAddress (Mac48Address::Allocate ());
98  nC->AddDevice (deviceC);
99 
100  // Later, we add IP addresses.
101  Ipv4AddressHelper ipv4;
102  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
103  Ipv4InterfaceContainer iAiB = ipv4.Assign (dAdB);
104 
105  ipv4.SetBase ("10.1.1.4", "255.255.255.252");
106  Ipv4InterfaceContainer iBiC = ipv4.Assign (dBdC);
107 
108  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4> ();
109  Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4> ();
110  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4> ();
111 
112  int32_t ifIndexA = ipv4A->AddInterface (deviceA);
113  int32_t ifIndexC = ipv4C->AddInterface (deviceC);
114 
115  Ipv4InterfaceAddress ifInAddrA = Ipv4InterfaceAddress (Ipv4Address ("172.16.1.1"), Ipv4Mask ("/32"));
116  ipv4A->AddAddress (ifIndexA, ifInAddrA);
117  ipv4A->SetMetric (ifIndexA, 1);
118  ipv4A->SetUp (ifIndexA);
119 
120  Ipv4InterfaceAddress ifInAddrC = Ipv4InterfaceAddress (Ipv4Address ("192.168.1.1"), Ipv4Mask ("/32"));
121  ipv4C->AddAddress (ifIndexC, ifInAddrC);
122  ipv4C->SetMetric (ifIndexC, 1);
123  ipv4C->SetUp (ifIndexC);
124 
125  Ipv4StaticRoutingHelper ipv4RoutingHelper;
126  // Create static routes from A to C
127  Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting (ipv4A);
128  // The ifIndex for this outbound route is 1; the first p2p link added
129  staticRoutingA->AddHostRouteTo (Ipv4Address ("192.168.1.1"), Ipv4Address ("10.1.1.2"), 1);
130  Ptr<Ipv4StaticRouting> staticRoutingB = ipv4RoutingHelper.GetStaticRouting (ipv4B);
131  // The ifIndex we want on node B is 2; 0 corresponds to loopback, and 1 to the first point to point link
132  staticRoutingB->AddHostRouteTo (Ipv4Address ("192.168.1.1"), Ipv4Address ("10.1.1.6"), 2);
133  // Create the OnOff application to send UDP datagrams of size
134  // 210 bytes at a rate of 448 Kb/s
135  uint16_t port = 9; // Discard port (RFC 863)
136  OnOffHelper onoff ("ns3::UdpSocketFactory",
137  Address (InetSocketAddress (ifInAddrC.GetLocal (), port)));
138  onoff.SetConstantRate (DataRate (6000));
139  ApplicationContainer apps = onoff.Install (nA);
140  apps.Start (Seconds (1.0));
141  apps.Stop (Seconds (10.0));
142 
143  // Create a packet sink to receive these packets
144  PacketSinkHelper sink ("ns3::UdpSocketFactory",
145  Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
146  apps = sink.Install (nC);
147  apps.Start (Seconds (1.0));
148  apps.Stop (Seconds (10.0));
149 
150  Simulator::Run ();
151  // Check that we received 13 * 512 = 6656 bytes
152  Ptr<PacketSink> sinkPtr = DynamicCast <PacketSink> (apps.Get (0));
153  NS_TEST_ASSERT_MSG_EQ (sinkPtr->GetTotalRx (), 6656, "Static routing with /32 did not deliver all packets");
154  Simulator::Destroy ();
155 }
156 
158 {
159 public:
161 };
162 
164  : TestSuite ("static-routing", UNIT)
165 {
167 }
168 
169 // Do not forget to allocate an instance of this TestSuite