A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-static-routing-test-suite.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5// End-to-end tests for Ipv4 static routing
6
7#include "ns3/boolean.h"
8#include "ns3/config.h"
9#include "ns3/inet-socket-address.h"
10#include "ns3/internet-stack-helper.h"
11#include "ns3/ipv4-address-helper.h"
12#include "ns3/ipv4-static-routing-helper.h"
13#include "ns3/node-container.h"
14#include "ns3/node.h"
15#include "ns3/packet.h"
16#include "ns3/pointer.h"
17#include "ns3/simple-channel.h"
18#include "ns3/simple-net-device-helper.h"
19#include "ns3/simple-net-device.h"
20#include "ns3/simulator.h"
21#include "ns3/socket-factory.h"
22#include "ns3/string.h"
23#include "ns3/test.h"
24#include "ns3/udp-socket-factory.h"
25#include "ns3/uinteger.h"
26
27using namespace ns3;
28
29/**
30 * \ingroup internet-test
31 *
32 * \brief IPv4 StaticRouting /32 Test
33 */
35{
36 public:
39
40 Ptr<Packet> m_receivedPacket; //!< Received packet
41
42 /**
43 * \brief Send data.
44 * \param socket The sending socket.
45 * \param to Destination address.
46 */
47 void DoSendData(Ptr<Socket> socket, std::string to);
48 /**
49 * \brief Send data.
50 * \param socket The sending socket.
51 * \param to Destination address.
52 */
53 void SendData(Ptr<Socket> socket, std::string to);
54
55 /**
56 * \brief Receive data.
57 * \param socket The receiving socket.
58 */
59 void ReceivePkt(Ptr<Socket> socket);
60
61 private:
62 void DoRun() override;
63};
64
65// Add some help text to this case to describe what it is intended to test
67 : TestCase("Slash 32 static routing example")
68{
69}
70
74
75void
77{
78 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
79 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
80 NS_TEST_ASSERT_MSG_EQ(availableData,
82 "Received packet size is not equal to Rx buffer size");
83}
84
85void
87{
88 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
89 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
90}
91
92void
94{
96 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
97 Seconds(60),
99 this,
100 socket,
101 to);
104}
105
106// Test program for this 3-router scenario, using static routing
107//
108// (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
109//
110void
112{
116
117 NodeContainer c = NodeContainer(nA, nB, nC);
118
119 InternetStackHelper internet;
120 internet.Install(c);
121
122 // simple links
123 NodeContainer nAnB = NodeContainer(nA, nB);
124 NodeContainer nBnC = NodeContainer(nB, nC);
125
126 SimpleNetDeviceHelper devHelper;
127
129 deviceA->SetAddress(Mac48Address::Allocate());
130 nA->AddDevice(deviceA);
131
132 NetDeviceContainer dAdB = devHelper.Install(nAnB);
133 NetDeviceContainer dBdC = devHelper.Install(nBnC);
134
136 deviceC->SetAddress(Mac48Address::Allocate());
137 nC->AddDevice(deviceC);
138
139 // Later, we add IP addresses.
141 ipv4.SetBase("10.1.1.0", "255.255.255.252");
142 Ipv4InterfaceContainer iAiB = ipv4.Assign(dAdB);
143
144 ipv4.SetBase("10.1.1.4", "255.255.255.252");
145 Ipv4InterfaceContainer iBiC = ipv4.Assign(dBdC);
146
147 Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4>();
148 Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4>();
149 Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4>();
150
151 int32_t ifIndexA = ipv4A->AddInterface(deviceA);
152 int32_t ifIndexC = ipv4C->AddInterface(deviceC);
153
154 Ipv4InterfaceAddress ifInAddrA =
155 Ipv4InterfaceAddress(Ipv4Address("172.16.1.1"), Ipv4Mask("/32"));
156 ipv4A->AddAddress(ifIndexA, ifInAddrA);
157 ipv4A->SetMetric(ifIndexA, 1);
158 ipv4A->SetUp(ifIndexA);
159
160 Ipv4InterfaceAddress ifInAddrC =
161 Ipv4InterfaceAddress(Ipv4Address("192.168.1.1"), Ipv4Mask("/32"));
162 ipv4C->AddAddress(ifIndexC, ifInAddrC);
163 ipv4C->SetMetric(ifIndexC, 1);
164 ipv4C->SetUp(ifIndexC);
165
166 Ipv4StaticRoutingHelper ipv4RoutingHelper;
167 // Create static routes from A to C
168 Ptr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting(ipv4A);
169 // The ifIndex for this outbound route is 1; the first p2p link added
170 staticRoutingA->AddHostRouteTo(Ipv4Address("192.168.1.1"), Ipv4Address("10.1.1.2"), 1);
171 Ptr<Ipv4StaticRouting> staticRoutingB = ipv4RoutingHelper.GetStaticRouting(ipv4B);
172 // The ifIndex we want on node B is 2; 0 corresponds to loopback, and 1 to the first point to
173 // point link
174 staticRoutingB->AddHostRouteTo(Ipv4Address("192.168.1.1"), Ipv4Address("10.1.1.6"), 2);
175
176 // Create the UDP sockets
177 Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory>();
178 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
179 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("192.168.1.1"), 1234)),
180 0,
181 "trivial");
182 rxSocket->SetRecvCallback(MakeCallback(&Ipv4StaticRoutingSlash32TestCase::ReceivePkt, this));
183
184 Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory>();
185 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
186 txSocket->SetAllowBroadcast(true);
187
188 // ------ Now the tests ------------
189
190 // Unicast test
191 SendData(txSocket, "192.168.1.1");
193 123,
194 "Static routing with /32 did not deliver all packets.");
195
197}
198
199/**
200 * \ingroup internet-test
201 *
202 * \brief IPv4 StaticRouting /32 TestSuite
203 */
205{
206 public:
208};
209
211 : TestSuite("ipv4-static-routing", Type::UNIT)
212{
213 AddTestCase(new Ipv4StaticRoutingSlash32TestCase, TestCase::Duration::QUICK);
214}
215
217 ipv4StaticRoutingTestSuite; //!< Static variable for test initialization
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.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
IPv4 StaticRouting /32 TestSuite.
a polymophic address class
Definition address.h:90
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.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
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
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...
static Mac48Address Allocate()
Allocate a new Mac48Address.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
Smart pointer class similar to boost::intrusive_ptr.
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...
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
API to create UDP socket instances.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:134
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684