A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-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/boolean.h"
21#include "ns3/icmpv6-l4-protocol.h"
22#include "ns3/inet6-socket-address.h"
23#include "ns3/internet-stack-helper.h"
24#include "ns3/ipv6-address-helper.h"
25#include "ns3/ipv6-l3-protocol.h"
26#include "ns3/ipv6-routing-helper.h"
27#include "ns3/ipv6-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-helper.h"
32#include "ns3/simple-net-device.h"
33#include "ns3/simulator.h"
34#include "ns3/socket-factory.h"
35#include "ns3/socket.h"
36#include "ns3/test.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
45/**
46 * \ingroup internet-test
47 *
48 * \brief IPv6 Forwarding Test
49 */
51{
52 Ptr<Packet> m_receivedPacket; //!< Received packet.
53
54 /**
55 * \brief Send data.
56 * \param socket The sending socket.
57 * \param to Destination address.
58 */
59 void DoSendData(Ptr<Socket> socket, std::string to);
60 /**
61 * \brief Send data.
62 * \param socket The sending socket.
63 * \param to Destination address.
64 */
65 void SendData(Ptr<Socket> socket, std::string to);
66
67 public:
68 void DoRun() override;
70
71 /**
72 * \brief Receive data.
73 * \param socket The receiving socket.
74 */
75 void ReceivePkt(Ptr<Socket> socket);
76};
77
79 : TestCase("IPv6 forwarding")
80{
81}
82
83void
85{
86 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
87 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
88 NS_TEST_ASSERT_MSG_EQ(availableData,
90 "Received packet size is not equal to Rx buffer size");
91}
92
93void
95{
96 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 1234);
97 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
98}
99
100void
102{
103 m_receivedPacket = Create<Packet>();
104 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
105 Seconds(0),
107 this,
108 socket,
109 to);
111}
112
113void
115{
116 // Create topology
117
118 // Receiver Node
119 Ptr<Node> rxNode = CreateObject<Node>();
120 // Forwarding Node
121 Ptr<Node> fwNode = CreateObject<Node>();
122 // Sender Node
123 Ptr<Node> txNode = CreateObject<Node>();
124
125 NodeContainer net1nodes(rxNode, fwNode);
126 NodeContainer net2nodes(fwNode, txNode);
127 NodeContainer nodes(rxNode, fwNode, txNode);
128
129 SimpleNetDeviceHelper helperChannel1;
130 helperChannel1.SetNetDevicePointToPointMode(true);
131 NetDeviceContainer net1 = helperChannel1.Install(net1nodes);
132
133 SimpleNetDeviceHelper helperChannel2;
134 helperChannel2.SetNetDevicePointToPointMode(true);
135 NetDeviceContainer net2 = helperChannel2.Install(net2nodes);
136
137 InternetStackHelper internetv6;
138 internetv6.Install(nodes);
139
140 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
141 fwNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
142 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
143
144 Ipv6AddressHelper ipv6helper;
145 Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress(net1);
146 Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress(net2);
147
148 Ptr<NetDevice> device;
149 Ptr<Ipv6> ipv6;
150 int32_t ifIndex;
151 Ipv6InterfaceAddress ipv6Addr;
152
153 ipv6 = rxNode->GetObject<Ipv6>();
154 device = net1.Get(0);
155 ifIndex = ipv6->GetInterfaceForDevice(device);
156 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:1::2"), Ipv6Prefix(64));
157 ipv6->AddAddress(ifIndex, ipv6Addr);
158
159 ipv6 = fwNode->GetObject<Ipv6>();
160 device = net1.Get(1);
161 ifIndex = ipv6->GetInterfaceForDevice(device);
162 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:1::1"), Ipv6Prefix(64));
163 ipv6->AddAddress(ifIndex, ipv6Addr);
164
165 device = net2.Get(0);
166 ifIndex = ipv6->GetInterfaceForDevice(device);
167 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:2::1"), Ipv6Prefix(64));
168 ipv6->AddAddress(ifIndex, ipv6Addr);
169
170 ipv6 = txNode->GetObject<Ipv6>();
171 device = net2.Get(1);
172 ifIndex = ipv6->GetInterfaceForDevice(device);
173 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:2::2"), Ipv6Prefix(64));
174 ipv6->AddAddress(ifIndex, ipv6Addr);
175
176 // Setup at least a route from the sender.
177 Ptr<Ipv6StaticRouting> ipv6StaticRouting = Ipv6RoutingHelper::GetRouting<Ipv6StaticRouting>(
178 txNode->GetObject<Ipv6>()->GetRoutingProtocol());
179 ipv6StaticRouting->SetDefaultRoute(Ipv6Address("2001:2::1"), ifIndex);
180
181 // Create the UDP sockets
182 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
183 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
184 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:1::2"), 1234)),
185 0,
186 "trivial");
187 rxSocket->SetRecvCallback(MakeCallback(&Ipv6ForwardingTest::ReceivePkt, this));
188
189 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
190 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
191 txSocket->SetAllowBroadcast(true);
192
193 // ------ Now the tests ------------
194
195 // Unicast test
196 SendData(txSocket, "2001:1::2");
197 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 0, "IPv6 Forwarding off");
198
200 m_receivedPacket = nullptr;
201
202 ipv6 = fwNode->GetObject<Ipv6>();
203 ipv6->SetAttribute("IpForward", BooleanValue(true));
204 SendData(txSocket, "2001:1::2");
205 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 123, "IPv6 Forwarding on");
206
208
210}
211
212/**
213 * \ingroup internet-test
214 *
215 * \brief IPv6 Forwarding TestSuite
216 */
218{
219 public:
221 : TestSuite("ipv6-forwarding", Type::UNIT)
222 {
223 AddTestCase(new Ipv6ForwardingTest, TestCase::Duration::QUICK);
224 }
225};
226
228 g_ipv6forwardingTestSuite; //!< Static variable for test initialization
IPv6 Forwarding Test.
Ptr< Packet > m_receivedPacket
Received packet.
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.
void ReceivePkt(Ptr< Socket > socket)
Receive data.
IPv6 Forwarding TestSuite.
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An implementation of the ICMPv6 protocol.
An Inet6 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...
Helper class to auto-assign global IPv6 unicast addresses.
Describes an IPv6 address.
Definition: ipv6-address.h:49
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
virtual Ptr< Ipv6RoutingProtocol > GetRoutingProtocol() const =0
Get the routing protocol to be used by this IPv6 stack.
IPv6 address associated with an interface.
Keep track of a set of IPv6 interfaces.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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: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
build a set of SimpleNetDevice objects
void SetNetDevicePointToPointMode(bool pointToPointMode)
SimpleNetDevice is Broadcast capable and ARP needing.
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: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
static constexpr auto UNIT
Definition: test.h:1286
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:1319
static Ipv6ForwardingTestSuite g_ipv6forwardingTestSuite
Static variable for test initialization.
NodeContainer nodes
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:706