A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-forwarding-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 Universita' di Firenze
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "ns3/arp-l3-protocol.h"
10#include "ns3/boolean.h"
11#include "ns3/icmpv4-l4-protocol.h"
12#include "ns3/inet-socket-address.h"
13#include "ns3/internet-stack-helper.h"
14#include "ns3/ipv4-l3-protocol.h"
15#include "ns3/ipv4-routing-helper.h"
16#include "ns3/ipv4-static-routing.h"
17#include "ns3/log.h"
18#include "ns3/node.h"
19#include "ns3/simple-channel.h"
20#include "ns3/simple-net-device.h"
21#include "ns3/simulator.h"
22#include "ns3/socket-factory.h"
23#include "ns3/socket.h"
24#include "ns3/test.h"
25#include "ns3/traffic-control-layer.h"
26#include "ns3/udp-l4-protocol.h"
27#include "ns3/udp-socket-factory.h"
28
29#include <limits>
30#include <string>
31
32using namespace ns3;
33
34/**
35 * @ingroup internet-test
36 *
37 * @brief IPv4 Forwarding Test
38 */
40{
41 Ptr<Packet> m_receivedPacket; //!< Received packet
42
43 /**
44 * @brief Send data.
45 * @param socket The sending socket.
46 * @param to Destination address.
47 */
48 void DoSendData(Ptr<Socket> socket, std::string to);
49 /**
50 * @brief Send data.
51 * @param socket The sending socket.
52 * @param to Destination address.
53 */
54 void SendData(Ptr<Socket> socket, std::string to);
55
56 public:
57 void DoRun() override;
59
60 /**
61 * @brief Receive data.
62 * @param socket The receiving socket.
63 */
64 void ReceivePkt(Ptr<Socket> socket);
65};
66
68 : TestCase("UDP socket implementation")
69{
70}
71
72void
74{
75 uint32_t availableData;
76 availableData = socket->GetRxAvailable();
77 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
78 NS_TEST_ASSERT_MSG_EQ(availableData,
80 "Received packet size is not equal to Rx buffer size");
81}
82
83void
85{
86 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
87 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
88}
89
90void
92{
94 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
95 Seconds(0),
97 this,
98 socket,
99 to);
101}
102
103void
105{
106 // Create topology
107
108 // Receiver Node
109 Ptr<Node> rxNode = CreateObject<Node>();
110
111 InternetStackHelper internet;
112 internet.SetIpv6StackInstall(false);
113
114 internet.Install(rxNode);
116 { // first interface
119 rxNode->AddDevice(rxDev);
120 Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4>();
121 uint32_t netdev_idx = ipv4->AddInterface(rxDev);
122 Ipv4InterfaceAddress ipv4Addr =
123 Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
124 ipv4->AddAddress(netdev_idx, ipv4Addr);
125 ipv4->SetUp(netdev_idx);
126 }
127
128 // Forwarding Node
129 Ptr<Node> fwNode = CreateObject<Node>();
130
131 internet.Install(fwNode);
134 { // first interface
137 fwNode->AddDevice(fwDev1);
138 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
139 uint32_t netdev_idx = ipv4->AddInterface(fwDev1);
140 Ipv4InterfaceAddress ipv4Addr =
141 Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
142 ipv4->AddAddress(netdev_idx, ipv4Addr);
143 ipv4->SetUp(netdev_idx);
144 }
145
146 { // second interface
149 fwNode->AddDevice(fwDev2);
150 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
151 uint32_t netdev_idx = ipv4->AddInterface(fwDev2);
152 Ipv4InterfaceAddress ipv4Addr =
153 Ipv4InterfaceAddress(Ipv4Address("10.1.0.1"), Ipv4Mask(0xffff0000U));
154 ipv4->AddAddress(netdev_idx, ipv4Addr);
155 ipv4->SetUp(netdev_idx);
156 }
157
158 // Sender Node
159 Ptr<Node> txNode = CreateObject<Node>();
160
161 internet.Install(txNode);
163 {
166 txNode->AddDevice(txDev);
167 Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4>();
168 uint32_t netdev_idx = ipv4->AddInterface(txDev);
169 Ipv4InterfaceAddress ipv4Addr =
170 Ipv4InterfaceAddress(Ipv4Address("10.1.0.2"), Ipv4Mask(0xffff0000U));
171 ipv4->AddAddress(netdev_idx, ipv4Addr);
172 ipv4->SetUp(netdev_idx);
174 txNode->GetObject<Ipv4>()->GetRoutingProtocol());
175 ipv4StaticRouting->SetDefaultRoute(Ipv4Address("10.1.0.1"), netdev_idx);
176 }
177
178 // link the two nodes
180 rxDev->SetChannel(channel1);
181 fwDev1->SetChannel(channel1);
182
184 fwDev2->SetChannel(channel2);
185 txDev->SetChannel(channel2);
186
187 // Create the UDP sockets
188 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
189 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
190 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.2"), 1234)),
191 0,
192 "trivial");
193 rxSocket->SetRecvCallback(MakeCallback(&Ipv4ForwardingTest::ReceivePkt, this));
194
195 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
196 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
197 txSocket->SetAllowBroadcast(true);
198
199 // ------ Now the tests ------------
200
201 // Unicast test
202 SendData(txSocket, "10.0.0.2");
203 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 123, "IPv4 Forwarding on");
204
206 m_receivedPacket = nullptr;
207
208 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
209 ipv4->SetAttribute("IpForward", BooleanValue(false));
210 SendData(txSocket, "10.0.0.2");
211 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 0, "IPv4 Forwarding off");
212
214}
215
216/**
217 * @ingroup internet-test
218 *
219 * @brief IPv4 Forwarding TestSuite
220 */
222{
223 public:
225
226 private:
227};
228
230 : TestSuite("ipv4-forwarding", Type::UNIT)
231{
232 AddTestCase(new Ipv4ForwardingTest, TestCase::Duration::QUICK);
233}
234
236 g_ipv4forwardingTestSuite; //!< Static variable for test initialization
IPv4 Forwarding Test.
Ptr< Packet > m_receivedPacket
Received packet.
void ReceivePkt(Ptr< Socket > socket)
Receive data.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
void SendData(Ptr< Socket > socket, std::string to)
Send data.
void DoRun() override
Implementation to actually run this TestCase.
IPv4 Forwarding TestSuite.
a polymophic address class
Definition address.h:90
AttributeValue implementation for Boolean.
Definition boolean.h:26
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
Ipv4 addresses are stored in host order in this class.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
virtual Ptr< Ipv4RoutingProtocol > GetRoutingProtocol() const =0
Get the routing protocol to be used by this Ipv4 stack.
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
static Ptr< T > GetRouting(Ptr< Ipv4RoutingProtocol > protocol)
Request a specified routing protocol <T> from Ipv4RoutingProtocol protocol.
static Mac48Address ConvertFrom(const Address &address)
static Mac48Address Allocate()
Allocate a new Mac48Address.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition packet.cc:382
Smart pointer class similar to boost::intrusive_ptr.
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
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:1344
static Ipv4ForwardingTestSuite g_ipv4forwardingTestSuite
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