A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
static-routing-test-suite.cc
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
55 StaticRoutingSlash32TestCase::StaticRoutingSlash32TestCase ()
56  : TestCase ("Slash 32 static routing example")
57 {
58 }
59 
60 StaticRoutingSlash32TestCase::~StaticRoutingSlash32TestCase ()
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 
163 StaticRoutingTestSuite::StaticRoutingTestSuite ()
164  : TestSuite ("static-routing", UNIT)
165 {
166  AddTestCase (new StaticRoutingSlash32TestCase, TestCase::QUICK);
167 }
168 
169 // Do not forget to allocate an instance of this TestSuite
170 static StaticRoutingTestSuite staticRoutingTestSuite;
holds a vector of ns3::Application pointers.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
holds a vector of std::pair of Ptr<Ipv4> and interface index.
hold variables of type string
Definition: string.h:19
NetDeviceContainer Install(NodeContainer c)
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
A suite of tests to run.
Definition: test.h:962
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Build a set of PointToPointNetDevice objects.
encapsulates test code
Definition: test.h:834
void SetDeviceAttribute(std::string name, const AttributeValue &value)
void AddHostRouteTo(Ipv4Address dest, Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a host route to the static routing table.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:41
a polymophic address class
Definition: address.h:86
Class for representing data rates.
Definition: data-rate.h:71
virtual void SetUp(uint32_t interface)=0
holds a vector of ns3::NetDevice pointers
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
keep track of a set of node pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Install(std::string nodeName) const
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
uint32_t GetTotalRx() const
Definition: packet-sink.cc:72
virtual void DoRun(void)
Implementation to actually run this test case.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:119
a class to store IPv4 address information on an interface
Helper class that adds ns3::Ipv4StaticRouting objects.
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
virtual void SetAddress(Address address)
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
ApplicationContainer Install(NodeContainer c) const
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ptr< T > GetObject(void) const
Definition: object.h:332
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.