A Discrete-Event Network Simulator
API
ipv4-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/inet-socket-address.h"
22 #include "ns3/internet-stack-helper.h"
23 #include "ns3/ipv4-address-helper.h"
24 #include "ns3/ipv4-static-routing-helper.h"
25 #include "ns3/node.h"
26 #include "ns3/node-container.h"
27 #include "ns3/packet.h"
28 #include "ns3/pointer.h"
29 #include "ns3/simulator.h"
30 #include "ns3/string.h"
31 #include "ns3/test.h"
32 #include "ns3/uinteger.h"
33 #include "ns3/simple-net-device.h"
34 #include "ns3/simple-channel.h"
35 #include "ns3/simple-net-device-helper.h"
36 #include "ns3/socket-factory.h"
37 #include "ns3/udp-socket-factory.h"
38 
39 using namespace ns3;
40 
42 {
43 public:
46 
48  void ReceivePkt (Ptr<Socket> socket);
49  void DoSendData (Ptr<Socket> socket, std::string to);
50  void SendData (Ptr<Socket> socket, std::string to);
51 
52 private:
53  virtual void DoRun (void);
54 };
55 
56 // Add some help text to this case to describe what it is intended to test
58  : TestCase ("Slash 32 static routing example")
59 {
60 }
61 
63 {
64 }
65 
66 void
68 {
69  uint32_t availableData;
70  availableData = socket->GetRxAvailable ();
72  NS_ASSERT (availableData == m_receivedPacket->GetSize ());
73  //cast availableData to void, to suppress 'availableData' set but not used
74  //compiler warning
75  (void) availableData;
76 }
77 
78 void
80 {
81  Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 1234);
82  NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
83  123, "100");
84 }
85 
86 void
88 {
89  m_receivedPacket = Create<Packet> ();
90  Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (60),
92  Simulator::Stop (Seconds (66));
93  Simulator::Run ();
94 }
95 
96 // Test program for this 3-router scenario, using static routing
97 //
98 // (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
99 //
100 void
102 {
103  Ptr<Node> nA = CreateObject<Node> ();
104  Ptr<Node> nB = CreateObject<Node> ();
105  Ptr<Node> nC = CreateObject<Node> ();
106 
107  NodeContainer c = NodeContainer (nA, nB, nC);
108 
109  InternetStackHelper internet;
110  internet.Install (c);
111 
112  // simple links
113  NodeContainer nAnB = NodeContainer (nA, nB);
114  NodeContainer nBnC = NodeContainer (nB, nC);
115 
116  SimpleNetDeviceHelper devHelper;
117 
118  Ptr<SimpleNetDevice> deviceA = CreateObject<SimpleNetDevice> ();
119  deviceA->SetAddress (Mac48Address::Allocate ());
120  nA->AddDevice (deviceA);
121 
122  NetDeviceContainer dAdB = devHelper.Install (nAnB);
123  NetDeviceContainer dBdC = devHelper.Install (nBnC);
124 
125  Ptr<SimpleNetDevice> deviceC = CreateObject<SimpleNetDevice> ();
126  deviceC->SetAddress (Mac48Address::Allocate ());
127  nC->AddDevice (deviceC);
128 
129  // Later, we add IP addresses.
130  Ipv4AddressHelper ipv4;
131  ipv4.SetBase ("10.1.1.0", "255.255.255.252");
132  Ipv4InterfaceContainer iAiB = ipv4.Assign (dAdB);
133 
134  ipv4.SetBase ("10.1.1.4", "255.255.255.252");
135  Ipv4InterfaceContainer iBiC = ipv4.Assign (dBdC);
136 
137  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4> ();
138  Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4> ();
139  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4> ();
140 
141  int32_t ifIndexA = ipv4A->AddInterface (deviceA);
142  int32_t ifIndexC = ipv4C->AddInterface (deviceC);
143 
144  Ipv4InterfaceAddress ifInAddrA = Ipv4InterfaceAddress (Ipv4Address ("172.16.1.1"), Ipv4Mask ("/32"));
145  ipv4A->AddAddress (ifIndexA, ifInAddrA);
146  ipv4A->SetMetric (ifIndexA, 1);
147  ipv4A->SetUp (ifIndexA);
148 
149  Ipv4InterfaceAddress ifInAddrC = Ipv4InterfaceAddress (Ipv4Address ("192.168.1.1"), Ipv4Mask ("/32"));
150  ipv4C->AddAddress (ifIndexC, ifInAddrC);
151  ipv4C->SetMetric (ifIndexC, 1);
152  ipv4C->SetUp (ifIndexC);
153 
154  Ipv4StaticRoutingHelper ipv4RoutingHelper;
155  // Create static routes from A to C
156  Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting (ipv4A);
157  // The ifIndex for this outbound route is 1; the first p2p link added
158  staticRoutingA->AddHostRouteTo (Ipv4Address ("192.168.1.1"), Ipv4Address ("10.1.1.2"), 1);
159  Ptr<Ipv4StaticRouting> staticRoutingB = ipv4RoutingHelper.GetStaticRouting (ipv4B);
160  // The ifIndex we want on node B is 2; 0 corresponds to loopback, and 1 to the first point to point link
161  staticRoutingB->AddHostRouteTo (Ipv4Address ("192.168.1.1"), Ipv4Address ("10.1.1.6"), 2);
162 
163  // Create the UDP sockets
164  Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory> ();
165  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
166  NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("192.168.1.1"), 1234)), 0, "trivial");
167  rxSocket->SetRecvCallback (MakeCallback (&Ipv4StaticRoutingSlash32TestCase::ReceivePkt, this));
168 
169  Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory> ();
170  Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
171  txSocket->SetAllowBroadcast (true);
172 
173  // ------ Now the tests ------------
174 
175  // Unicast test
176  SendData (txSocket, "192.168.1.1");
177  NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "Static routing with /32 did not deliver all packets.");
178 
179  Simulator::Destroy ();
180 }
181 
183 {
184 public:
186 };
187 
189  : TestSuite ("ipv4-static-routing", UNIT)
190 {
191  AddTestCase (new Ipv4StaticRoutingSlash32TestCase, TestCase::QUICK);
192 }
193 
194 // Do not forget to allocate an instance of this TestSuite
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
an Inet address class
static Ipv4StaticRoutingTestSuite ipv4StaticRoutingTestSuite
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.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
A suite of tests to run.
Definition: test.h:1333
void DoSendData(Ptr< Socket > socket, std::string to)
#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
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
encapsulates test code
Definition: test.h:1147
void AddHostRouteTo(Ipv4Address dest, Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a host route to the static routing table.
a polymophic address class
Definition: address.h:90
#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 SetUp(uint32_t interface)=0
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
virtual void DoRun(void)
Implementation to actually run this TestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SendData(Ptr< Socket > socket, std::string to)
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 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...
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
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
a class to store IPv4 address information on an interface
Helper class that adds ns3::Ipv4StaticRouting objects.
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.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
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.
API to create UDP socket instances.
build a set of SimpleNetDevice objects
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.