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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18 */
19
20#include "ns3/arp-l3-protocol.h"
21#include "ns3/boolean.h"
22#include "ns3/icmpv4-l4-protocol.h"
23#include "ns3/inet-socket-address.h"
24#include "ns3/internet-stack-helper.h"
25#include "ns3/ipv4-l3-protocol.h"
26#include "ns3/ipv4-routing-helper.h"
27#include "ns3/ipv4-static-routing.h"
28#include "ns3/log.h"
29#include "ns3/node.h"
30#include "ns3/simple-channel.h"
31#include "ns3/simple-net-device.h"
32#include "ns3/simulator.h"
33#include "ns3/socket-factory.h"
34#include "ns3/socket.h"
35#include "ns3/test.h"
36#include "ns3/traffic-control-layer.h"
37#include "ns3/udp-l4-protocol.h"
38#include "ns3/udp-socket-factory.h"
39
40#include <limits>
41#include <string>
42
43using namespace ns3;
44
51{
53
59 void DoSendData(Ptr<Socket> socket, std::string to);
65 void SendData(Ptr<Socket> socket, std::string to);
66
67 public:
68 void DoRun() override;
70
75 void ReceivePkt(Ptr<Socket> socket);
76};
77
79 : TestCase("UDP socket implementation")
80{
81}
82
83void
85{
86 uint32_t availableData;
87 availableData = socket->GetRxAvailable();
88 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
89 NS_TEST_ASSERT_MSG_EQ(availableData,
91 "Received packet size is not equal to Rx buffer size");
92}
93
94void
96{
97 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
98 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
99}
100
101void
103{
104 m_receivedPacket = Create<Packet>();
105 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
106 Seconds(0),
108 this,
109 socket,
110 to);
112}
113
114void
116{
117 // Create topology
118
119 // Receiver Node
120 Ptr<Node> rxNode = CreateObject<Node>();
121
122 InternetStackHelper internet;
123 internet.SetIpv6StackInstall(false);
124
125 internet.Install(rxNode);
127 { // first interface
128 rxDev = CreateObject<SimpleNetDevice>();
130 rxNode->AddDevice(rxDev);
131 Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4>();
132 uint32_t netdev_idx = ipv4->AddInterface(rxDev);
133 Ipv4InterfaceAddress ipv4Addr =
134 Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
135 ipv4->AddAddress(netdev_idx, ipv4Addr);
136 ipv4->SetUp(netdev_idx);
137 }
138
139 // Forwarding Node
140 Ptr<Node> fwNode = CreateObject<Node>();
141
142 internet.Install(fwNode);
145 { // first interface
146 fwDev1 = CreateObject<SimpleNetDevice>();
148 fwNode->AddDevice(fwDev1);
149 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
150 uint32_t netdev_idx = ipv4->AddInterface(fwDev1);
151 Ipv4InterfaceAddress ipv4Addr =
152 Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
153 ipv4->AddAddress(netdev_idx, ipv4Addr);
154 ipv4->SetUp(netdev_idx);
155 }
156
157 { // second interface
158 fwDev2 = CreateObject<SimpleNetDevice>();
160 fwNode->AddDevice(fwDev2);
161 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
162 uint32_t netdev_idx = ipv4->AddInterface(fwDev2);
163 Ipv4InterfaceAddress ipv4Addr =
164 Ipv4InterfaceAddress(Ipv4Address("10.1.0.1"), Ipv4Mask(0xffff0000U));
165 ipv4->AddAddress(netdev_idx, ipv4Addr);
166 ipv4->SetUp(netdev_idx);
167 }
168
169 // Sender Node
170 Ptr<Node> txNode = CreateObject<Node>();
171
172 internet.Install(txNode);
174 {
175 txDev = CreateObject<SimpleNetDevice>();
177 txNode->AddDevice(txDev);
178 Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4>();
179 uint32_t netdev_idx = ipv4->AddInterface(txDev);
180 Ipv4InterfaceAddress ipv4Addr =
181 Ipv4InterfaceAddress(Ipv4Address("10.1.0.2"), Ipv4Mask(0xffff0000U));
182 ipv4->AddAddress(netdev_idx, ipv4Addr);
183 ipv4->SetUp(netdev_idx);
184 Ptr<Ipv4StaticRouting> ipv4StaticRouting = Ipv4RoutingHelper::GetRouting<Ipv4StaticRouting>(
185 txNode->GetObject<Ipv4>()->GetRoutingProtocol());
186 ipv4StaticRouting->SetDefaultRoute(Ipv4Address("10.1.0.1"), netdev_idx);
187 }
188
189 // link the two nodes
190 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
191 rxDev->SetChannel(channel1);
192 fwDev1->SetChannel(channel1);
193
194 Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
195 fwDev2->SetChannel(channel2);
196 txDev->SetChannel(channel2);
197
198 // Create the UDP sockets
199 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
200 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
201 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.2"), 1234)),
202 0,
203 "trivial");
204 rxSocket->SetRecvCallback(MakeCallback(&Ipv4ForwardingTest::ReceivePkt, this));
205
206 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
207 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
208 txSocket->SetAllowBroadcast(true);
209
210 // ------ Now the tests ------------
211
212 // Unicast test
213 SendData(txSocket, "10.0.0.2");
214 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 123, "IPv4 Forwarding on");
215
217 m_receivedPacket = nullptr;
218
219 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
220 ipv4->SetAttribute("IpForward", BooleanValue(false));
221 SendData(txSocket, "10.0.0.2");
222 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 0, "IPv4 Forwarding off");
223
225}
226
233{
234 public:
236
237 private:
238};
239
241 : TestSuite("ipv4-forwarding", Type::UNIT)
242{
243 AddTestCase(new Ipv4ForwardingTest, TestCase::Duration::QUICK);
244}
245
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:101
AttributeValue implementation for Boolean.
Definition: boolean.h:37
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
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
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
Definition: ipv4-address.h:257
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:861
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
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:145
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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:704