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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "ns3/boolean.h"
10#include "ns3/icmpv6-l4-protocol.h"
11#include "ns3/inet6-socket-address.h"
12#include "ns3/internet-stack-helper.h"
13#include "ns3/ipv6-address-helper.h"
14#include "ns3/ipv6-l3-protocol.h"
15#include "ns3/ipv6-routing-helper.h"
16#include "ns3/ipv6-static-routing.h"
17#include "ns3/log.h"
18#include "ns3/node.h"
19#include "ns3/simple-channel.h"
20#include "ns3/simple-net-device-helper.h"
21#include "ns3/simple-net-device.h"
22#include "ns3/simulator.h"
23#include "ns3/socket-factory.h"
24#include "ns3/socket.h"
25#include "ns3/test.h"
26#include "ns3/udp-l4-protocol.h"
27#include "ns3/udp-socket-factory.h"
28
29#include <limits>
30#include <string>
31
32using namespace ns3;
33
34/**
35 * \ingroup internet-test
36 *
37 * \brief IPv6 Forwarding Test
38 */
40{
41 Ptr<Packet> m_receivedPacket; //!< Received packet.
42
43 /**
44 * \brief Send data.
45 * \param socket The sending socket.
46 * \param to Destination address.
47 */
48 void DoSendData(Ptr<Socket> socket, std::string to);
49 /**
50 * \brief Send data.
51 * \param socket The sending socket.
52 * \param to Destination address.
53 */
54 void SendData(Ptr<Socket> socket, std::string to);
55
56 public:
57 void DoRun() override;
59
60 /**
61 * \brief Receive data.
62 * \param socket The receiving socket.
63 */
64 void ReceivePkt(Ptr<Socket> socket);
65};
66
68 : TestCase("IPv6 forwarding")
69{
70}
71
72void
74{
75 uint32_t availableData [[maybe_unused]] = socket->GetRxAvailable();
76 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
77 NS_TEST_ASSERT_MSG_EQ(availableData,
79 "Received packet size is not equal to Rx buffer size");
80}
81
82void
84{
85 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 1234);
86 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, "100");
87}
88
89void
91{
93 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
94 Seconds(0),
96 this,
97 socket,
98 to);
100}
101
102void
104{
105 // Create topology
106
107 // Receiver Node
108 Ptr<Node> rxNode = CreateObject<Node>();
109 // Forwarding Node
110 Ptr<Node> fwNode = CreateObject<Node>();
111 // Sender Node
112 Ptr<Node> txNode = CreateObject<Node>();
113
114 NodeContainer net1nodes(rxNode, fwNode);
115 NodeContainer net2nodes(fwNode, txNode);
116 NodeContainer nodes(rxNode, fwNode, txNode);
117
118 SimpleNetDeviceHelper helperChannel1;
119 helperChannel1.SetNetDevicePointToPointMode(true);
120 NetDeviceContainer net1 = helperChannel1.Install(net1nodes);
121
122 SimpleNetDeviceHelper helperChannel2;
123 helperChannel2.SetNetDevicePointToPointMode(true);
124 NetDeviceContainer net2 = helperChannel2.Install(net2nodes);
125
126 InternetStackHelper internetv6;
127 internetv6.Install(nodes);
128
129 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
130 fwNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
131 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
132
133 Ipv6AddressHelper ipv6helper;
134 Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress(net1);
135 Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress(net2);
136
137 Ptr<NetDevice> device;
138 Ptr<Ipv6> ipv6;
139 int32_t ifIndex;
140 Ipv6InterfaceAddress ipv6Addr;
141
142 ipv6 = rxNode->GetObject<Ipv6>();
143 device = net1.Get(0);
144 ifIndex = ipv6->GetInterfaceForDevice(device);
145 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:1::2"), Ipv6Prefix(64));
146 ipv6->AddAddress(ifIndex, ipv6Addr);
147
148 ipv6 = fwNode->GetObject<Ipv6>();
149 device = net1.Get(1);
150 ifIndex = ipv6->GetInterfaceForDevice(device);
151 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:1::1"), Ipv6Prefix(64));
152 ipv6->AddAddress(ifIndex, ipv6Addr);
153
154 device = net2.Get(0);
155 ifIndex = ipv6->GetInterfaceForDevice(device);
156 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:2::1"), Ipv6Prefix(64));
157 ipv6->AddAddress(ifIndex, ipv6Addr);
158
159 ipv6 = txNode->GetObject<Ipv6>();
160 device = net2.Get(1);
161 ifIndex = ipv6->GetInterfaceForDevice(device);
162 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:2::2"), Ipv6Prefix(64));
163 ipv6->AddAddress(ifIndex, ipv6Addr);
164
165 // Setup at least a route from the sender.
167 txNode->GetObject<Ipv6>()->GetRoutingProtocol());
168 ipv6StaticRouting->SetDefaultRoute(Ipv6Address("2001:2::1"), ifIndex);
169
170 // Create the UDP sockets
171 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<UdpSocketFactory>();
172 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
173 NS_TEST_EXPECT_MSG_EQ(rxSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:1::2"), 1234)),
174 0,
175 "trivial");
176 rxSocket->SetRecvCallback(MakeCallback(&Ipv6ForwardingTest::ReceivePkt, this));
177
178 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<UdpSocketFactory>();
179 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
180 txSocket->SetAllowBroadcast(true);
181
182 // ------ Now the tests ------------
183
184 // Unicast test
185 SendData(txSocket, "2001:1::2");
186 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 0, "IPv6 Forwarding off");
187
189 m_receivedPacket = nullptr;
190
191 ipv6 = fwNode->GetObject<Ipv6>();
192 ipv6->SetAttribute("IpForward", BooleanValue(true));
193 SendData(txSocket, "2001:1::2");
194 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 123, "IPv6 Forwarding on");
195
197
199}
200
201/**
202 * \ingroup internet-test
203 *
204 * \brief IPv6 Forwarding TestSuite
205 */
207{
208 public:
210 : TestSuite("ipv6-forwarding", Type::UNIT)
211 {
212 AddTestCase(new Ipv6ForwardingTest, TestCase::Duration::QUICK);
213 }
214};
215
217 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:90
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.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
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.
static Ptr< T > GetRouting(Ptr< Ipv6RoutingProtocol > protocol)
Request a specified routing protocol <T> from Ipv6RoutingProtocol protocol.
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:850
void RemoveAllByteTags()
Remove all byte tags stored in this packet.
Definition packet.cc:382
Smart pointer class similar to boost::intrusive_ptr.
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:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static constexpr auto UNIT
Definition test.h:1291
API to create UDP socket instances.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:134
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684