A Discrete-Event Network Simulator
API
ipv4-forwarding-test.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013 Universita' di Firenze
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19 */
20
21#include "ns3/test.h"
22#include "ns3/socket-factory.h"
23#include "ns3/udp-socket-factory.h"
24#include "ns3/simulator.h"
25#include "ns3/simple-channel.h"
26#include "ns3/simple-net-device.h"
27#include "ns3/socket.h"
28#include "ns3/boolean.h"
29
30#include "ns3/log.h"
31#include "ns3/node.h"
32#include "ns3/inet-socket-address.h"
33
34#include "ns3/arp-l3-protocol.h"
35#include "ns3/ipv4-l3-protocol.h"
36#include "ns3/icmpv4-l4-protocol.h"
37#include "ns3/udp-l4-protocol.h"
38#include "ns3/ipv4-static-routing.h"
39#include "ns3/internet-stack-helper.h"
40#include "ns3/ipv4-routing-helper.h"
41
42#include "ns3/traffic-control-layer.h"
43
44#include <string>
45#include <limits>
46
47using namespace ns3;
48
49
57{
59
65 void DoSendData (Ptr<Socket> socket, std::string to);
71 void SendData (Ptr<Socket> socket, std::string to);
72
73public:
74 virtual void DoRun (void);
76
81 void ReceivePkt (Ptr<Socket> socket);
82};
83
85 : TestCase ("UDP socket implementation")
86{
87}
88
90{
91 uint32_t availableData;
92 availableData = socket->GetRxAvailable ();
94 NS_TEST_ASSERT_MSG_EQ (availableData, m_receivedPacket->GetSize (), "Received packet size is not equal to Rx buffer size");
95}
96
97void
99{
100 Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 1234);
101 NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),
102 123, "100");
103}
104
105void
107{
108 m_receivedPacket = Create<Packet> ();
109 Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (0),
110 &Ipv4ForwardingTest::DoSendData, this, socket, to);
111 Simulator::Run ();
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> ();
129 rxDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
130 rxNode->AddDevice (rxDev);
131 Ptr<Ipv4> ipv4 = rxNode->GetObject<Ipv4> ();
132 uint32_t netdev_idx = ipv4->AddInterface (rxDev);
133 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.2"), Ipv4Mask (0xffff0000U));
134 ipv4->AddAddress (netdev_idx, ipv4Addr);
135 ipv4->SetUp (netdev_idx);
136 }
137
138 // Forwarding Node
139 Ptr<Node> fwNode = CreateObject<Node> ();
140
141 internet.Install (fwNode);
142 Ptr<SimpleNetDevice> fwDev1, fwDev2;
143 { // first interface
144 fwDev1 = CreateObject<SimpleNetDevice> ();
145 fwDev1->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
146 fwNode->AddDevice (fwDev1);
147 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
148 uint32_t netdev_idx = ipv4->AddInterface (fwDev1);
149 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.0.0.1"), Ipv4Mask (0xffff0000U));
150 ipv4->AddAddress (netdev_idx, ipv4Addr);
151 ipv4->SetUp (netdev_idx);
152 }
153
154 { // second interface
155 fwDev2 = CreateObject<SimpleNetDevice> ();
156 fwDev2->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
157 fwNode->AddDevice (fwDev2);
158 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
159 uint32_t netdev_idx = ipv4->AddInterface (fwDev2);
160 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.1.0.1"), Ipv4Mask (0xffff0000U));
161 ipv4->AddAddress (netdev_idx, ipv4Addr);
162 ipv4->SetUp (netdev_idx);
163 }
164
165 // Sender Node
166 Ptr<Node> txNode = CreateObject<Node> ();
167
168 internet.Install (txNode);
170 {
171 txDev = CreateObject<SimpleNetDevice> ();
172 txDev->SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
173 txNode->AddDevice (txDev);
174 Ptr<Ipv4> ipv4 = txNode->GetObject<Ipv4> ();
175 uint32_t netdev_idx = ipv4->AddInterface (txDev);
176 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address ("10.1.0.2"), Ipv4Mask (0xffff0000U));
177 ipv4->AddAddress (netdev_idx, ipv4Addr);
178 ipv4->SetUp (netdev_idx);
179 Ptr<Ipv4StaticRouting> ipv4StaticRouting = Ipv4RoutingHelper::GetRouting <Ipv4StaticRouting> (txNode->GetObject<Ipv4> ()->GetRoutingProtocol ());
180 ipv4StaticRouting->SetDefaultRoute(Ipv4Address("10.1.0.1"), netdev_idx);
181 }
182
183 // link the two nodes
184 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
185 rxDev->SetChannel (channel1);
186 fwDev1->SetChannel (channel1);
187
188 Ptr<SimpleChannel> channel2 = CreateObject<SimpleChannel> ();
189 fwDev2->SetChannel (channel2);
190 txDev->SetChannel (channel2);
191
192 // Create the UDP sockets
193 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory> ();
194 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();
195 NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("10.0.0.2"), 1234)), 0, "trivial");
196 rxSocket->SetRecvCallback (MakeCallback (&Ipv4ForwardingTest::ReceivePkt, this));
197
198 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory> ();
199 Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();
200 txSocket->SetAllowBroadcast (true);
201
202 // ------ Now the tests ------------
203
204 // Unicast test
205 SendData (txSocket, "10.0.0.2");
206 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "IPv4 Forwarding on");
207
210
211 Ptr<Ipv4> ipv4 = fwNode->GetObject<Ipv4> ();
212 ipv4->SetAttribute("IpForward", BooleanValue (false));
213 SendData (txSocket, "10.0.0.2");
214 NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 0, "IPv4 Forwarding off");
215
216 Simulator::Destroy ();
217
218}
219
220
228{
229public:
231private:
232};
233
235 : TestSuite ("ipv4-forwarding", UNIT)
236{
237 AddTestCase (new Ipv4ForwardingTest, TestCase::QUICK);
238}
239
241
#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.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SendData(Ptr< Socket > socket, std::string to)
Send data.
IPv4 Forwarding TestSuite.
a polymophic address class
Definition: address.h:91
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:41
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
virtual Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) 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:256
uint32_t GetId(void) const
Definition: node.cc:109
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:130
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void RemoveAllByteTags(void)
Remove all byte tags stored in this packet.
Definition: packet.cc:371
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
virtual Ptr< Node > GetNode(void) const =0
Return the node this socket is associated with.
virtual uint32_t GetRxAvailable(void) 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 int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
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:141
#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:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static Ipv4ForwardingTestSuite g_ipv4forwardingTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648