A Discrete-Event Network Simulator
API
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
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("IPv6 forwarding")
81{
82}
83
84void
86{
87 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
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 = Inet6SocketAddress(Ipv6Address(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);
111 Simulator::Run();
112}
113
114void
116{
117 // Create topology
118
119 // Receiver Node
120 Ptr<Node> rxNode = CreateObject<Node>();
121 // Forwarding Node
122 Ptr<Node> fwNode = CreateObject<Node>();
123 // Sender Node
124 Ptr<Node> txNode = CreateObject<Node>();
125
126 NodeContainer net1nodes(rxNode, fwNode);
127 NodeContainer net2nodes(fwNode, txNode);
128 NodeContainer nodes(rxNode, fwNode, txNode);
129
130 SimpleNetDeviceHelper helperChannel1;
131 helperChannel1.SetNetDevicePointToPointMode(true);
132 NetDeviceContainer net1 = helperChannel1.Install(net1nodes);
133
134 SimpleNetDeviceHelper helperChannel2;
135 helperChannel2.SetNetDevicePointToPointMode(true);
136 NetDeviceContainer net2 = helperChannel2.Install(net2nodes);
137
138 InternetStackHelper internetv6;
139 internetv6.Install(nodes);
140
141 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
142 fwNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
143 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
144
145 Ipv6AddressHelper ipv6helper;
146 Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress(net1);
147 Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress(net2);
148
149 Ptr<NetDevice> device;
150 Ptr<Ipv6> ipv6;
151 int32_t ifIndex;
152 Ipv6InterfaceAddress ipv6Addr;
153
154 ipv6 = rxNode->GetObject<Ipv6>();
155 device = net1.Get(0);
156 ifIndex = ipv6->GetInterfaceForDevice(device);
157 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:1::2"), Ipv6Prefix(64));
158 ipv6->AddAddress(ifIndex, ipv6Addr);
159
160 ipv6 = fwNode->GetObject<Ipv6>();
161 device = net1.Get(1);
162 ifIndex = ipv6->GetInterfaceForDevice(device);
163 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:1::1"), Ipv6Prefix(64));
164 ipv6->AddAddress(ifIndex, ipv6Addr);
165
166 device = net2.Get(0);
167 ifIndex = ipv6->GetInterfaceForDevice(device);
168 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:2::1"), Ipv6Prefix(64));
169 ipv6->AddAddress(ifIndex, ipv6Addr);
170
171 ipv6 = txNode->GetObject<Ipv6>();
172 device = net2.Get(1);
173 ifIndex = ipv6->GetInterfaceForDevice(device);
174 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:2::2"), Ipv6Prefix(64));
175 ipv6->AddAddress(ifIndex, ipv6Addr);
176
177 // Setup at least a route from the sender.
178 Ptr<Ipv6StaticRouting> ipv6StaticRouting = Ipv6RoutingHelper::GetRouting<Ipv6StaticRouting>(
179 txNode->GetObject<Ipv6>()->GetRoutingProtocol());
180 ipv6StaticRouting->SetDefaultRoute(Ipv6Address("2001:2::1"), ifIndex);
181
182 // Create the UDP sockets
183 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
184 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
185 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:1::2"), 1234)),
186 0,
187 "trivial");
188 rxSocket->SetRecvCallback(MakeCallback(&Ipv6ForwardingTest::ReceivePkt, this));
189
190 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
191 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
192 txSocket->SetAllowBroadcast(true);
193
194 // ------ Now the tests ------------
195
196 // Unicast test
197 SendData(txSocket, "2001:1::2");
198 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 0, "IPv6 Forwarding off");
199
201 m_receivedPacket = nullptr;
202
203 ipv6 = fwNode->GetObject<Ipv6>();
204 ipv6->SetAttribute("IpForward", BooleanValue(true));
205 SendData(txSocket, "2001:1::2");
206 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 123, "IPv6 Forwarding on");
207
209
210 Simulator::Destroy();
211}
212
220{
221 public:
223 : TestSuite("ipv6-forwarding", UNIT)
224 {
225 AddTestCase(new Ipv6ForwardingTest, TestCase::QUICK);
226 }
227};
228
#define max(a, b)
Definition: 80211b.c:43
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:92
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:50
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:456
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 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
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...
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
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
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 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:691