A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-raw-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Hajime Tazaki
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Hajime Tazaki <tazaki@sfc.wide.ad.jp>
7 */
8/*
9 * This is the test code for ipv6-raw-socket-impl.cc.
10 */
11
12#include "ns3/boolean.h"
13#include "ns3/error-model.h"
14#include "ns3/icmpv6-l4-protocol.h"
15#include "ns3/inet6-socket-address.h"
16#include "ns3/internet-stack-helper.h"
17#include "ns3/ipv6-address-helper.h"
18#include "ns3/ipv6-l3-protocol.h"
19#include "ns3/ipv6-list-routing.h"
20#include "ns3/ipv6-raw-socket-factory.h"
21#include "ns3/ipv6-static-routing.h"
22#include "ns3/log.h"
23#include "ns3/node-container.h"
24#include "ns3/node.h"
25#include "ns3/pointer.h"
26#include "ns3/simple-channel.h"
27#include "ns3/simple-net-device-helper.h"
28#include "ns3/simple-net-device.h"
29#include "ns3/simulator.h"
30#include "ns3/socket-factory.h"
31#include "ns3/socket.h"
32#include "ns3/test.h"
33#include "ns3/uinteger.h"
34
35#ifdef __WIN32__
36#include "ns3/win32-internet.h"
37#else
38#include <netinet/in.h>
39#include <sys/socket.h>
40#endif
41
42#include <limits>
43#include <string>
44#include <sys/types.h>
45
46using namespace ns3;
47
48NS_LOG_COMPONENT_DEFINE("ipv6-raw-fragmentation-test");
49
50/**
51 * @ingroup internet-test
52 *
53 * @brief IPv6 Raw Socket Fragmentation Test
54 * Configuration : Two sockets. 1 sender, 1 receiver
55 * Sender socket sends packets in two epochs.
56 * In the first epoch, it sends payload of sizes {500, 1000, 5000, 10000,
57 * 20000, 40000, 60000}.
58 * In the second epoch, it sends payload of sizes {5000, 10000, 20000,
59 * 40000, 60000}. These are payloads of size greater than the MTU of
60 * the link.
61 * In the first epoch, there is no loss model.
62 * In the second epoch, the second fragment of the packet sent is dropped.
63 *
64 * Expected behaviour: In the first epoch: Receiver should receive only 1 packet and the size of
65 * the payload received should be equal to the size of the payload sent.
66 * No packets should have been dropped.
67 * In the second epoch: Receiver should not receive any packet. Only 1 packet
68 * should have been dropped at the physical layer.
69 */
71{
72 public:
74 void DoRun() override;
75
76 private:
77 /**
78 * @brief Send a packet
79 * @param socket The sending socket
80 * @param dst Destination IPv6 address.
81 * @param size Size (bytes) of the payload to send.
82 */
83 void SendPacket(Ptr<Socket> socket, Ipv6Address dst, uint32_t size);
84
85 /**
86 * @brief Receive a packet
87 * @param socket Socket that received a packet
88 */
89 void RxCallback(Ptr<Socket> socket);
90
91 /**
92 * @brief Packet dropped callback at Phy Rx.
93 * @param p Dropped packet.
94 */
96
97 uint32_t m_rxCount{0}; //!< Total count of packets received.
98 uint32_t m_rxPayloadSize{0}; //!< Size of the last packet received.
99 uint32_t m_droppedFragments{0}; //!< Number of dropped packets
100};
101
103 : TestCase("ipv6-raw-fragmentation-test")
104{
105}
106
107void
109{
110 Ptr<Packet> p = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
111
112 Ipv6Header hdr;
113 if (p->PeekHeader(hdr))
114 {
115 p->RemoveHeader(hdr);
116 }
117
118 m_rxPayloadSize = p->GetSize();
119 m_rxCount++;
120 NS_LOG_DEBUG("Receiver got " << m_rxPayloadSize << " bytes");
121}
122
123void
125{
126 Ptr<Packet> p = Create<Packet>(size);
127 sock->SendTo(p, 0, Inet6SocketAddress(dst, 0));
128 NS_LOG_DEBUG("Sender sent " << p->GetSize() << " bytes to " << dst);
129}
130
131void
136
137void
139{
140 Ptr<Node> rxNode = CreateObject<Node>();
141 Ptr<Node> txNode = CreateObject<Node>();
142
144 helper.SetNetDevicePointToPointMode(true);
145
148
149 txDev->SetMtu(1280); // Minimum MTU allowed for IPv6
150
151 rxDev->TraceConnectWithoutContext(
152 "PhyRxDrop",
154
156 rxDev->SetChannel(ch);
157 txDev->SetChannel(ch);
158
160 stack.SetIpv4StackInstall(false);
161 stack.Install(NodeContainer(rxNode, txNode));
162
163 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
164 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
165
166 Ptr<Ipv6> ipv6Rx = rxNode->GetObject<Ipv6>();
167 Ptr<Ipv6> ipv6Tx = txNode->GetObject<Ipv6>();
168
169 uint32_t ifRx = ipv6Rx->AddInterface(rxDev);
170 ipv6Rx->AddAddress(ifRx, Ipv6InterfaceAddress("2001:db8::1", Ipv6Prefix(64)));
171 ipv6Rx->SetUp(ifRx);
172
173 uint32_t ifTx = ipv6Tx->AddInterface(txDev);
174 ipv6Tx->AddAddress(ifTx, Ipv6InterfaceAddress("2001:db8::2", Ipv6Prefix(64)));
175 ipv6Tx->SetUp(ifTx);
176
177 const uint8_t proto = Ipv6Header::IPV6_ICMPV6;
178
179 Ptr<Socket> rxSock = rxNode->GetObject<Ipv6RawSocketFactory>()->CreateSocket();
180 rxSock->SetAttribute("Protocol", UintegerValue(proto));
181 rxSock->Bind(Inet6SocketAddress(Ipv6Address::GetAny(), 0));
182 rxSock->SetRecvCallback(MakeCallback(&Ipv6RawFragmentationTest::RxCallback, this));
183
184 Ptr<Socket> txSock = txNode->GetObject<Ipv6RawSocketFactory>()->CreateSocket();
185 txSock->SetAttribute("Protocol", UintegerValue(proto));
186
187 std::vector<uint32_t> payloadSizes = {500, 1000, 5000, 10000, 20000, 40000, 60000};
188
189 for (auto sentPayloadSize : payloadSizes)
190 {
191 m_rxCount = 0;
192 m_rxPayloadSize = 0;
193
196 this,
197 txSock,
198 Ipv6Address("2001:db8::1"),
199 sentPayloadSize);
202
204 sentPayloadSize,
205 "Reassembled packet size should be the same as the packet size sent");
206
207 NS_TEST_EXPECT_MSG_EQ(m_rxCount, 1, "Only 1 packet should have been received");
208
209 NS_TEST_EXPECT_MSG_EQ(m_droppedFragments, 0, "No packets should have been dropped");
210 }
211
212 std::vector<uint32_t> fragmentablePayloadSizes = {5000, 10000, 20000, 40000, 60000};
213
214 for (auto sentPayloadSize : fragmentablePayloadSizes)
215 {
216 m_rxCount = 0;
218
220 errModel->SetList({1});
221
222 rxDev->SetAttribute("ReceiveErrorModel", PointerValue(errModel));
223
226 this,
227 txSock,
228 Ipv6Address("2001:db8::1"),
229 sentPayloadSize);
232
233 NS_TEST_EXPECT_MSG_EQ(m_rxCount, 0, "No packet should have been received");
234
235 NS_TEST_EXPECT_MSG_EQ(m_droppedFragments, 1, "Exactly 1 packet should have been dropped.");
236 }
238}
239
240/**
241 * @ingroup internet-test
242 *
243 * @brief IPv6 RAW Socket Test
244 */
246{
247 Ptr<Packet> m_receivedPacket; //!< Received packet (1).
248 Ptr<Packet> m_receivedPacket2; //!< Received packet (2).
249
250 /**
251 * @brief Send data.
252 * @param socket The sending socket.
253 * @param to Destination address.
254 */
255 void DoSendData(Ptr<Socket> socket, std::string to);
256 /**
257 * @brief Send data.
258 * @param socket The sending socket.
259 * @param to Destination address.
260 */
261 void SendData(Ptr<Socket> socket, std::string to);
262
263 public:
264 void DoRun() override;
266
267 /**
268 * @brief Receive data.
269 * @param socket The receiving socket.
270 * @param packet The received packet.
271 * @param from The sender.
272 */
273 void ReceivePacket(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
274 /**
275 * @brief Receive data.
276 * @param socket The receiving socket.
277 * @param packet The received packet.
278 * @param from The sender.
279 */
280 void ReceivePacket2(Ptr<Socket> socket, Ptr<Packet> packet, const Address& from);
281 /**
282 * @brief Receive data.
283 * @param socket The receiving socket.
284 */
285 void ReceivePkt(Ptr<Socket> socket);
286 /**
287 * @brief Receive data.
288 * @param socket The receiving socket.
289 */
290 void ReceivePkt2(Ptr<Socket> socket);
291};
292
294 : TestCase("Ipv6 Raw socket implementation")
295{
296}
297
298void
300{
301 m_receivedPacket = packet;
302}
303
304void
306{
307 m_receivedPacket2 = packet;
308}
309
310void
312{
313 uint32_t availableData;
314 availableData = socket->GetRxAvailable();
315 m_receivedPacket = socket->Recv(2, MSG_PEEK);
316 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket->GetSize(), 2, "ReceivedPacket size is not equal to 2");
317 m_receivedPacket = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
318 NS_TEST_ASSERT_MSG_EQ(availableData,
320 "Received packet size is not equal to Rx buffer size");
321}
322
323void
325{
326 uint32_t availableData;
327 Address addr;
328 availableData = socket->GetRxAvailable();
329 m_receivedPacket2 = socket->Recv(2, MSG_PEEK);
330 NS_TEST_ASSERT_MSG_EQ(m_receivedPacket2->GetSize(), 2, "ReceivedPacket size is not equal to 2");
331 m_receivedPacket2 = socket->RecvFrom(std::numeric_limits<uint32_t>::max(), 0, addr);
332 NS_TEST_ASSERT_MSG_EQ(availableData,
334 "Received packet size is not equal to Rx buffer size");
336 NS_TEST_EXPECT_MSG_EQ(v6addr.GetIpv6(), Ipv6Address("2001:db8::2"), "recvfrom");
337}
338
339void
341{
342 Address realTo = Inet6SocketAddress(Ipv6Address(to.c_str()), 0);
343 NS_TEST_EXPECT_MSG_EQ(socket->SendTo(Create<Packet>(123), 0, realTo), 123, to);
344}
345
346void
348{
351 Simulator::ScheduleWithContext(socket->GetNode()->GetId(),
352 Seconds(0),
354 this,
355 socket,
356 to);
358}
359
360void
362{
363 // Create topology
364
365 // Receiver Node
366 Ptr<Node> rxNode = CreateObject<Node>();
367 // Sender Node
368 Ptr<Node> txNode = CreateObject<Node>();
369
370 NodeContainer nodes(rxNode, txNode);
371
372 SimpleNetDeviceHelper helperChannel1;
373 helperChannel1.SetNetDevicePointToPointMode(true);
374 NetDeviceContainer net1 = helperChannel1.Install(nodes);
375
376 SimpleNetDeviceHelper helperChannel2;
377 helperChannel2.SetNetDevicePointToPointMode(true);
378 NetDeviceContainer net2 = helperChannel2.Install(nodes);
379
380 InternetStackHelper internetv6;
381 internetv6.Install(nodes);
382
383 txNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
384 rxNode->GetObject<Icmpv6L4Protocol>()->SetAttribute("DAD", BooleanValue(false));
385
386 Ipv6AddressHelper ipv6helper;
387 Ipv6InterfaceContainer iic1 = ipv6helper.AssignWithoutAddress(net1);
388 Ipv6InterfaceContainer iic2 = ipv6helper.AssignWithoutAddress(net2);
389
390 Ptr<NetDevice> device;
391 Ptr<Ipv6> ipv6;
392 int32_t ifIndex;
393 Ipv6InterfaceAddress ipv6Addr;
394
395 ipv6 = rxNode->GetObject<Ipv6>();
396 device = net1.Get(0);
397 ifIndex = ipv6->GetInterfaceForDevice(device);
398 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8::1"), Ipv6Prefix(64));
399 ipv6->AddAddress(ifIndex, ipv6Addr);
400
401 device = net2.Get(0);
402 ifIndex = ipv6->GetInterfaceForDevice(device);
403 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8:1::3"), Ipv6Prefix(64));
404 ipv6->AddAddress(ifIndex, ipv6Addr);
405
406 ipv6 = txNode->GetObject<Ipv6>();
407 device = net1.Get(1);
408 ifIndex = ipv6->GetInterfaceForDevice(device);
409 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8::2"), Ipv6Prefix(64));
410 ipv6->AddAddress(ifIndex, ipv6Addr);
411 ipv6->SetForwarding(ifIndex, true);
412
413 device = net2.Get(1);
414 ifIndex = ipv6->GetInterfaceForDevice(device);
415 ipv6Addr = Ipv6InterfaceAddress(Ipv6Address("2001:db8:1::4"), Ipv6Prefix(64));
416 ipv6->AddAddress(ifIndex, ipv6Addr);
417 ipv6->SetForwarding(ifIndex, true);
418
419 // Create the Ipv6 Raw sockets
420 Ptr<SocketFactory> rxSocketFactory = rxNode->GetObject<Ipv6RawSocketFactory>();
421 Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket();
423 0,
424 "trivial");
425 rxSocket->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
426 rxSocket->SetRecvCallback(MakeCallback(&Ipv6RawSocketImplTest::ReceivePkt, this));
427
428 Ptr<Socket> rxSocket2 = rxSocketFactory->CreateSocket();
429 rxSocket2->SetRecvCallback(MakeCallback(&Ipv6RawSocketImplTest::ReceivePkt2, this));
430 rxSocket2->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
431 NS_TEST_EXPECT_MSG_EQ(rxSocket2->Bind(Inet6SocketAddress(Ipv6Address("2001:db8:1::3"), 0)),
432 0,
433 "trivial");
434
435 Ptr<SocketFactory> txSocketFactory = txNode->GetObject<Ipv6RawSocketFactory>();
436 Ptr<Socket> txSocket = txSocketFactory->CreateSocket();
437 txSocket->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
438
439 // ------ Now the tests ------------
440
441 // Unicast test
442 SendData(txSocket, "2001:db8::1");
443
444 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: 2001:db8::1");
446 0,
447 "second interface should not receive it");
448
451
452 // Simple Link-local multicast test
453 txSocket->Bind(Inet6SocketAddress(Ipv6Address("2001:db8::2"), 0));
454 SendData(txSocket, "ff02::1");
455 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: ff02::1");
457 0,
458 "second socket should not receive it (it is bound specifically to the "
459 "second interface's address");
460
463
464 // Broadcast test with multiple receiving sockets
465
466 // When receiving broadcast packets, all sockets sockets bound to
467 // the address/port should receive a copy of the same packet -- if
468 // the socket address matches.
469 rxSocket2->Dispose();
470 rxSocket2 = rxSocketFactory->CreateSocket();
471 rxSocket2->SetRecvCallback(MakeCallback(&Ipv6RawSocketImplTest::ReceivePkt2, this));
472 rxSocket2->SetAttribute("Protocol", UintegerValue(Ipv6Header::IPV6_ICMPV6));
474 0,
475 "trivial");
476
477 SendData(txSocket, "ff02::1");
478 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket->GetSize(), 163, "recv: ff02::1");
479 NS_TEST_EXPECT_MSG_EQ(m_receivedPacket2->GetSize(), 163, "recv: ff02::1");
480
481 m_receivedPacket = nullptr;
482 m_receivedPacket2 = nullptr;
483
484 // Simple getpeername tests
485
486 Address peerAddress;
487 int err = txSocket->GetPeerName(peerAddress);
488 NS_TEST_EXPECT_MSG_EQ(err, -1, "socket GetPeerName() should fail when socket is not connected");
489 NS_TEST_EXPECT_MSG_EQ(txSocket->GetErrno(),
491 "socket error code should be ERROR_NOTCONN");
492
493 Inet6SocketAddress peer("2001:db8::1", 1234);
494 err = txSocket->Connect(peer);
495 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket Connect() should succeed");
496
497 err = txSocket->GetPeerName(peerAddress);
498 NS_TEST_EXPECT_MSG_EQ(err, 0, "socket GetPeerName() should succeed when socket is connected");
499 peer.SetPort(0);
500 NS_TEST_EXPECT_MSG_EQ(peerAddress,
501 peer,
502 "address from socket GetPeerName() should equal the connected address");
503
505}
506
507/**
508 * @ingroup internet-test
509 *
510 * @brief IPv6 RAW Socket TestSuite
511 */
513{
514 public:
516 : TestSuite("ipv6-raw", Type::UNIT)
517 {
518 AddTestCase(new Ipv6RawSocketImplTest, TestCase::Duration::QUICK);
519 AddTestCase(new Ipv6RawFragmentationTest, TestCase::Duration::QUICK);
520 }
521};
522
523static Ipv6RawTestSuite g_ipv6rawTestSuite; //!< Static variable for test initialization
IPv6 Raw Socket Fragmentation Test Configuration : Two sockets.
void RxCallback(Ptr< Socket > socket)
Receive a packet.
void SendPacket(Ptr< Socket > socket, Ipv6Address dst, uint32_t size)
Send a packet.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_rxCount
Total count of packets received.
void PhyRxDropCallback(Ptr< const Packet > p)
Packet dropped callback at Phy Rx.
uint32_t m_droppedFragments
Number of dropped packets.
uint32_t m_rxPayloadSize
Size of the last packet received.
IPv6 RAW Socket Test.
Ptr< Packet > m_receivedPacket2
Received packet (2).
Ptr< Packet > m_receivedPacket
Received packet (1).
void ReceivePacket2(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
void ReceivePkt2(Ptr< Socket > socket)
Receive data.
void DoRun() override
Implementation to actually run this TestCase.
void DoSendData(Ptr< Socket > socket, std::string to)
Send data.
void ReceivePkt(Ptr< Socket > socket)
Receive data.
void ReceivePacket(Ptr< Socket > socket, Ptr< Packet > packet, const Address &from)
Receive data.
void SendData(Ptr< Socket > socket, std::string to)
Send data.
IPv6 RAW Socket TestSuite.
a polymophic address class
Definition address.h:90
AttributeValue implementation for Boolean.
Definition boolean.h:26
An implementation of the ICMPv6 protocol.
An Inet6 address class.
void SetPort(uint16_t port)
Set the port.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Ipv6Address GetIpv6() const
Get the IPv6 address.
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.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Packet header for IPv6.
Definition ipv6-header.h:24
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
IPv6 address associated with an interface.
Keep track of a set of IPv6 interfaces.
Describes an IPv6 prefix.
API to create IPv6 RAW socket instances.
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
AttributeValue implementation for Pointer.
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 EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
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:578
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
@ ERROR_NOTCONN
Definition socket.h:76
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
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
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:439
#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:1369
static Ipv6RawTestSuite g_ipv6rawTestSuite
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
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:585