A Discrete-Event Network Simulator
API
ipv4-static-routing-test-suite.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 // End-to-end tests for Ipv4 static routing
17 
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-static-routing-helper.h"
24 #include "ns3/node-container.h"
25 #include "ns3/node.h"
26 #include "ns3/packet.h"
27 #include "ns3/pointer.h"
28 #include "ns3/simple-channel.h"
29 #include "ns3/simple-net-device-helper.h"
30 #include "ns3/simple-net-device.h"
31 #include "ns3/simulator.h"
32 #include "ns3/socket-factory.h"
33 #include "ns3/string.h"
34 #include "ns3/test.h"
35 #include "ns3/udp-socket-factory.h"
36 #include "ns3/uinteger.h"
37 
38 using namespace ns3;
39 
46 {
47  public:
50 
52 
58  void DoSendData(Ptr<Socket> socket, std::string to);
64  void SendData(Ptr<Socket> socket, std::string to);
65 
70  void ReceivePkt(Ptr<Socket> socket);
71 
72  private:
73  void DoRun() override;
74 };
75 
76 // Add some help text to this case to describe what it is intended to test
78  : TestCase("Slash 32 static routing example")
79 {
80 }
81 
83 {
84 }
85 
86 void
88 {
89  uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
91  NS_TEST_ASSERT_MSG_EQ(availableData,
93  "Received packet size is not equal to Rx buffer size");
94 }
95 
96 void
98 {
99  Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
100  NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
101 }
102 
103 void
105 {
106  m_receivedPacket = Create<Packet>();
107  Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
108  Seconds(60),
110  this,
111  socket,
112  to);
113  Simulator::Stop(Seconds(66));
114  Simulator::Run();
115 }
116 
117 // Test program for this 3-router scenario, using static routing
118 //
119 // (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
120 //
121 void
123 {
124  Ptr<Node> nA = CreateObject<Node>();
125  Ptr<Node> nB = CreateObject<Node>();
126  Ptr<Node> nC = CreateObject<Node>();
127 
128  NodeContainer c = NodeContainer(nA, nB, nC);
129 
131  internet.Install(c);
132 
133  // simple links
134  NodeContainer nAnB = NodeContainer(nA, nB);
135  NodeContainer nBnC = NodeContainer(nB, nC);
136 
137  SimpleNetDeviceHelper devHelper;
138 
139  Ptr<SimpleNetDevice> deviceA = CreateObject<SimpleNetDevice>();
140  deviceA->SetAddress(Mac48Address::Allocate());
141  nA->AddDevice(deviceA);
142 
143  NetDeviceContainer dAdB = devHelper.Install(nAnB);
144  NetDeviceContainer dBdC = devHelper.Install(nBnC);
145 
146  Ptr<SimpleNetDevice> deviceC = CreateObject<SimpleNetDevice>();
147  deviceC->SetAddress(Mac48Address::Allocate());
148  nC->AddDevice(deviceC);
149 
150  // Later, we add IP addresses.
152  ipv4.SetBase("10.1.1.0", "255.255.255.252");
153  Ipv4InterfaceContainer iAiB = ipv4.Assign(dAdB);
154 
155  ipv4.SetBase("10.1.1.4", "255.255.255.252");
156  Ipv4InterfaceContainer iBiC = ipv4.Assign(dBdC);
157 
158  Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4>();
159  Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4>();
160  Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4>();
161 
162  int32_t ifIndexA = ipv4A->AddInterface(deviceA);
163  int32_t ifIndexC = ipv4C->AddInterface(deviceC);
164 
165  Ipv4InterfaceAddress ifInAddrA =
166  Ipv4InterfaceAddress(Ipv4Address("172.16.1.1"), Ipv4Mask("/32"));
167  ipv4A->AddAddress(ifIndexA, ifInAddrA);
168  ipv4A->SetMetric(ifIndexA, 1);
169  ipv4A->SetUp(ifIndexA);
170 
171  Ipv4InterfaceAddress ifInAddrC =
172  Ipv4InterfaceAddress(Ipv4Address("192.168.1.1"), Ipv4Mask("/32"));
173  ipv4C->AddAddress(ifIndexC, ifInAddrC);
174  ipv4C->SetMetric(ifIndexC, 1);
175  ipv4C->SetUp(ifIndexC);
176 
177  Ipv4StaticRoutingHelper ipv4RoutingHelper;
178  // Create static routes from A to C
179  Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting(ipv4A);
180  // The ifIndex for this outbound route is 1; the first p2p link added
181  staticRoutingA->AddHostRouteTo(Ipv4Address("192.168.1.1"), Ipv4Address("10.1.1.2"), 1);
182  Ptr<Ipv4StaticRouting> staticRoutingB = ipv4RoutingHelper.GetStaticRouting(ipv4B);
183  // The ifIndex we want on node B is 2; 0 corresponds to loopback, and 1 to the first point to
184  // point link
185  staticRoutingB->AddHostRouteTo(Ipv4Address("192.168.1.1"), Ipv4Address("10.1.1.6"), 2);
186 
187  // Create the UDP sockets
188  Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory>();
189  Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
190  NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("192.168.1.1"), 1234)),
191  0,
192  "trivial");
193  rxSocket->SetRecvCallback(MakeCallback(&Ipv4StaticRoutingSlash32TestCase::ReceivePkt, this));
194 
195  Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory>();
196  Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
197  txSocket->SetAllowBroadcast(true);
198 
199  // ------ Now the tests ------------
200 
201  // Unicast test
202  SendData(txSocket, "192.168.1.1");
204  123,
205  "Static routing with /32 did not deliver all packets.");
206 
207  Simulator::Destroy();
208 }
209 
216 {
217  public:
219 };
220 
222  : TestSuite("ipv4-static-routing", UNIT)
223 {
224  AddTestCase(new Ipv4StaticRoutingSlash32TestCase, TestCase::QUICK);
225 }
226 
#define max(a, b)
Definition: 80211b.c:42
void SendData(Ptr< Socket > socket, std::string to)
Send data.
void DoRun() override
Implementation to actually run this TestCase.
void ReceivePkt(Ptr< Socket > socket)
Receive data.
Ptr< Packet > m_receivedPacket
Received packet.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
IPv4 StaticRouting /32 TestSuite.
a polymophic address class
Definition: address.h:101
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
a class to store IPv4 address information on an interface
holds a vector of std::pair of Ptr<Ipv4> and interface index.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
Helper class that adds ns3::Ipv4StaticRouting objects.
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...
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t GetId() const
Definition: node.cc:117
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
build a set of SimpleNetDevice objects
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
virtual uint32_t GetRxAvailable() const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual Ptr< Node > GetNode() const =0
Return the node this socket is associated with.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
API to create UDP socket instances.
#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:144
#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:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static Ipv4StaticRoutingTestSuite ipv4StaticRoutingTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704