A Discrete-Event Network Simulator
API
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
52{
54
60 void DoSendData(Ptr<Socket> socket, std::string to);
66 void SendData(Ptr<Socket> socket, std::string to);
67
68 public:
69 void DoRun() override;
71
76 void ReceivePkt(Ptr<Socket> socket);
77};
78
80 : TestCase("UDP socket implementation")
81{
82}
83
84void
86{
87 uint32_t availableData;
88 availableData = socket->GetRxAvailable();
90 NS_TEST_ASSERT_MSG_EQ(availableData,
92 "Received packet size is not equal to Rx buffer size");
93}
94
95void
97{
98 Address realTo = InetSocketAddress(Ipv4Address(to.c_str()), 1234);
99 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
100}
101
102void
104{
105 m_receivedPacket = Create<Packet>();
106 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
107 Seconds(0),
109 this,
110 socket,
111 to);
112 Simulator::Run();
113}
114
115void
117{
118 // Create topology
119
120 // Receiver Node
121 Ptr<Node> rxNode = CreateObject<Node>();
122
123 InternetStackHelper internet;
124 internet.SetIpv6StackInstall(false);
125
126 internet.Install(rxNode);
128 { // first interface
129 rxDev = CreateObject<SimpleNetDevice>();
130 rxDev->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
131 rxNode->AddDevice(rxDev);
132 Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4>();
133 uint32_t netdev_idx = ipv4->AddInterface(rxDev);
134 Ipv4InterfaceAddress ipv4Addr =
135 Ipv4InterfaceAddress(Ipv4Address("10.0.0.2"), Ipv4Mask(0xffff0000U));
136 ipv4->AddAddress(netdev_idx, ipv4Addr);
137 ipv4->SetUp(netdev_idx);
138 }
139
140 // Forwarding Node
141 Ptr<Node> fwNode = CreateObject<Node>();
142
143 internet.Install(fwNode);
146 { // first interface
147 fwDev1 = CreateObject<SimpleNetDevice>();
148 fwDev1->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
149 fwNode->AddDevice(fwDev1);
150 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
151 uint32_t netdev_idx = ipv4->AddInterface(fwDev1);
152 Ipv4InterfaceAddress ipv4Addr =
153 Ipv4InterfaceAddress(Ipv4Address("10.0.0.1"), Ipv4Mask(0xffff0000U));
154 ipv4->AddAddress(netdev_idx, ipv4Addr);
155 ipv4->SetUp(netdev_idx);
156 }
157
158 { // second interface
159 fwDev2 = CreateObject<SimpleNetDevice>();
160 fwDev2->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
161 fwNode->AddDevice(fwDev2);
162 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
163 uint32_t netdev_idx = ipv4->AddInterface(fwDev2);
164 Ipv4InterfaceAddress ipv4Addr =
165 Ipv4InterfaceAddress(Ipv4Address("10.1.0.1"), Ipv4Mask(0xffff0000U));
166 ipv4->AddAddress(netdev_idx, ipv4Addr);
167 ipv4->SetUp(netdev_idx);
168 }
169
170 // Sender Node
171 Ptr<Node> txNode = CreateObject<Node>();
172
173 internet.Install(txNode);
175 {
176 txDev = CreateObject<SimpleNetDevice>();
177 txDev->SetAddress(Mac48Address::ConvertFrom(Mac48Address::Allocate()));
178 txNode->AddDevice(txDev);
179 Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4>();
180 uint32_t netdev_idx = ipv4->AddInterface(txDev);
181 Ipv4InterfaceAddress ipv4Addr =
182 Ipv4InterfaceAddress(Ipv4Address("10.1.0.2"), Ipv4Mask(0xffff0000U));
183 ipv4->AddAddress(netdev_idx, ipv4Addr);
184 ipv4->SetUp(netdev_idx);
185 Ptr<Ipv4StaticRouting> ipv4StaticRouting = Ipv4RoutingHelper::GetRouting<Ipv4StaticRouting>(
186 txNode->GetObject<Ipv4>()->GetRoutingProtocol());
187 ipv4StaticRouting->SetDefaultRoute(Ipv4Address("10.1.0.1"), netdev_idx);
188 }
189
190 // link the two nodes
191 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
192 rxDev->SetChannel(channel1);
193 fwDev1->SetChannel(channel1);
194
195 Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel>();
196 fwDev2->SetChannel(channel2);
197 txDev->SetChannel(channel2);
198
199 // Create the UDP sockets
200 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
201 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
202 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(InetSocketAddress(Ipv4Address("10.0.0.2"), 1234)),
203 0,
204 "trivial");
205 rxSocket->SetRecvCallback(MakeCallback(&Ipv4ForwardingTest::ReceivePkt, this));
206
207 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
208 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
209 txSocket->SetAllowBroadcast(true);
210
211 // ------ Now the tests ------------
212
213 // Unicast test
214 SendData(txSocket, "10.0.0.2");
215 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 123, "IPv4 Forwarding on");
216
218 m_receivedPacket = nullptr;
219
220 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4>();
221 ipv4->SetAttribute("IpForward", BooleanValue(false));
222 SendData(txSocket, "10.0.0.2");
223 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 0, "IPv4 Forwarding off");
224
225 Simulator::Destroy();
226}
227
235{
236 public:
238
239 private:
240};
241
243 : TestSuite("ipv4-forwarding", UNIT)
244{
245 AddTestCase(new Ipv4ForwardingTest, TestCase::QUICK);
246}
247
#define max(a, b)
Definition: 80211b.c:43
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:92
AttributeValue implementation for Boolean.
Definition: boolean.h:37
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetIpv6StackInstall(bool enable)
Enable/disable IPv6 stack install.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
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:258
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:863
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition: packet.cc:393
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:305
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:1338
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:691