A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sixlowpan-iphc-stateful-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Universita' di Firenze, Italy
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "mock-net-device.h"
10
11#include "ns3/boolean.h"
12#include "ns3/icmpv6-l4-protocol.h"
13#include "ns3/inet6-socket-address.h"
14#include "ns3/internet-stack-helper.h"
15#include "ns3/ipv6-address-helper.h"
16#include "ns3/log.h"
17#include "ns3/node.h"
18#include "ns3/simulator.h"
19#include "ns3/sixlowpan-header.h"
20#include "ns3/sixlowpan-helper.h"
21#include "ns3/sixlowpan-net-device.h"
22#include "ns3/socket-factory.h"
23#include "ns3/socket.h"
24#include "ns3/test.h"
25
26#include <limits>
27#include <string>
28#include <vector>
29
30using namespace ns3;
31
32/**
33 * \ingroup sixlowpan-tests
34 *
35 * \brief 6LoWPAN IPHC stateful compression Test
36 */
38{
39 /**
40 * \brief Structure to hold the Rx/Tx packets.
41 */
42 struct Data
43 {
44 Ptr<Packet> packet; /**< Packet data */
45 Address src; /**< Source address */
46 Address dst; /**< Destination address */
47 };
48
49 std::vector<Data> m_txPackets; //!< Transmitted packets
50 std::vector<Data> m_rxPackets; //!< Received packets
51
52 /**
53 * Receive from a MockDevice.
54 * \param device a pointer to the net device which is calling this function
55 * \param packet the packet received
56 * \param protocol the 16 bit protocol number associated with this packet.
57 * \param source the address of the sender
58 * \param destination the address of the receiver
59 * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
60 * \returns true.
61 */
63 Ptr<const Packet> packet,
64 uint16_t protocol,
65 const Address& source,
66 const Address& destination,
67 NetDevice::PacketType packetType);
68
69 /**
70 * Promiscuous receive from a SixLowPanNetDevice.
71 * \param device a pointer to the net device which is calling this function
72 * \param packet the packet received
73 * \param protocol the 16 bit protocol number associated with this packet.
74 * \param source the address of the sender
75 * \param destination the address of the receiver
76 * \param packetType type of packet received (broadcast/multicast/unicast/otherhost)
77 * \returns true.
78 */
80 Ptr<const Packet> packet,
81 uint16_t protocol,
82 const Address& source,
83 const Address& destination,
84 NetDevice::PacketType packetType);
85
86 /**
87 * Send one packet.
88 * \param device the device to send from
89 * \param from sender address
90 * \param to destination address
91 */
93
94 NetDeviceContainer m_mockDevices; //!< MockNetDevice container
95 NetDeviceContainer m_sixDevices; //!< SixLowPanNetDevice container
96
97 public:
98 void DoRun() override;
100};
101
103 : TestCase("Sixlowpan IPHC stateful implementation")
104{
105}
106
107bool
109 Ptr<const Packet> packet,
110 uint16_t protocol,
111 const Address& source,
112 const Address& destination,
113 NetDevice::PacketType packetType)
114{
115 Data incomingPkt;
116 incomingPkt.packet = packet->Copy();
117 incomingPkt.src = source;
118 incomingPkt.dst = destination;
119 m_txPackets.push_back(incomingPkt);
120
122 if (mockDev)
123 {
124 uint32_t id = mockDev->GetNode()->GetId();
126 Time(1),
128 mockDev,
129 incomingPkt.packet,
130 protocol,
131 destination,
132 source,
133 packetType);
134 }
135 return true;
136}
137
138bool
140 Ptr<const Packet> packet,
141 uint16_t protocol,
142 const Address& source,
143 const Address& destination,
144 NetDevice::PacketType packetType)
145{
146 Data incomingPkt;
147 incomingPkt.packet = packet->Copy();
148 incomingPkt.src = source;
149 incomingPkt.dst = destination;
150 m_rxPackets.push_back(incomingPkt);
151
152 return true;
153}
154
155void
157 Ipv6Address from,
158 Ipv6Address to)
159{
160 Ptr<Packet> pkt = Create<Packet>(10);
161 Ipv6Header ipHdr;
162 ipHdr.SetSource(from);
163 ipHdr.SetDestination(to);
164 ipHdr.SetHopLimit(64);
165 ipHdr.SetPayloadLength(10);
166 ipHdr.SetNextHeader(0xff);
167 pkt->AddHeader(ipHdr);
168
169 device->Send(pkt, Mac48Address("00:00:00:00:00:02"), 0);
170}
171
172void
174{
176 nodes.Create(2);
177
178 // First node, setup NetDevices.
180 nodes.Get(0)->AddDevice(mockNetDevice0);
181 mockNetDevice0->SetNode(nodes.Get(0));
182 mockNetDevice0->SetAddress(Mac48Address("00:00:00:00:00:01"));
183 mockNetDevice0->SetMtu(150);
184 mockNetDevice0->SetSendCallback(
186 m_mockDevices.Add(mockNetDevice0);
187
188 // Second node, setup NetDevices.
190 nodes.Get(1)->AddDevice(mockNetDevice1);
191 mockNetDevice1->SetNode(nodes.Get(1));
192 mockNetDevice1->SetAddress(Mac48Address("00:00:00:00:00:02"));
193 mockNetDevice1->SetMtu(150);
194 m_mockDevices.Add(mockNetDevice1);
195
196 InternetStackHelper internetv6;
197 internetv6.Install(nodes);
198
199 SixLowPanHelper sixlowpan;
201 m_sixDevices.Get(1)->SetPromiscReceiveCallback(
203
205 ipv6.SetBase(Ipv6Address("2001:2::"), Ipv6Prefix(64));
206 Ipv6InterfaceContainer deviceInterfaces;
207 deviceInterfaces = ipv6.Assign(m_sixDevices);
208
209 // This is a hack to prevent Router Solicitations and Duplicate Address Detection being sent.
210 for (auto i = nodes.Begin(); i != nodes.End(); i++)
211 {
212 Ptr<Node> node = *i;
213 Ptr<Ipv6L3Protocol> ipv6L3 = (*i)->GetObject<Ipv6L3Protocol>();
214 if (ipv6L3)
215 {
216 ipv6L3->SetAttribute("IpForward", BooleanValue(true));
217 ipv6L3->SetAttribute("SendIcmpv6Redirect", BooleanValue(false));
218 }
219 Ptr<Icmpv6L4Protocol> icmpv6 = (*i)->GetObject<Icmpv6L4Protocol>();
220 if (icmpv6)
221 {
222 icmpv6->SetAttribute("DAD", BooleanValue(false));
223 }
224 }
225
226 sixlowpan.AddContext(m_sixDevices, 0, Ipv6Prefix("2001:2::", 64), Time(Minutes(30)));
227 sixlowpan.AddContext(m_sixDevices, 1, Ipv6Prefix("2001:1::", 64), Time(Minutes(30)));
228
229 Ipv6Address srcElided = deviceInterfaces.GetAddress(0, 1);
230 Ipv6Address dstElided =
232 Ipv6Prefix("2001:2::", 64));
233
236 this,
237 m_sixDevices.Get(0),
239 dstElided);
240
243 this,
244 m_sixDevices.Get(0),
245 Ipv6Address("2001:2::f00d:f00d:cafe:cafe"),
246 Ipv6Address("2001:1::0000:00ff:fe00:cafe"));
247
250 this,
251 m_sixDevices.Get(0),
252 Ipv6Address("2001:1::0000:00ff:fe00:cafe"),
253 Ipv6Address("2001:1::f00d:f00d:cafe:cafe"));
254
257 this,
258 m_sixDevices.Get(0),
259 srcElided,
260 Ipv6Address("2001:1::f00d:f00d:cafe:cafe"));
261
263
266
267 // ------ Now the tests ------------
268
269 SixLowPanIphc iphcHdr;
270 Ipv6Header ipv6Hdr;
271
272 // first packet sent, expected CID(0) SAC(1) SAM (0) M(0) DAC(1) DAM (3)
273 m_txPackets[0].packet->RemoveHeader(iphcHdr);
274 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetCid(), false, "CID should be false, is true");
275 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSac(), true, "SAC should be true, is false");
278 "SAM should be HC_INLINE, it is not");
279 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetM(), false, "M should be false, is true");
280 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDac(), true, "DAC should be true, is false");
281 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSrcContextId(), 0, "Src context should be 0, it is not");
282 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDstContextId(), 0, "Dst context should be 0, it is not");
285 "DAM should be HC_COMPR_0, it is not");
286
287 // first packet received, expected :: -> dstElided
288 m_rxPackets[0].packet->RemoveHeader(ipv6Hdr);
291 "Src address wrongly rebuilt");
292 NS_TEST_EXPECT_MSG_EQ(ipv6Hdr.GetDestination(), dstElided, "Dst address wrongly rebuilt");
293
294 // second packet sent, expected CID(1) SAC(1) SAM (1) M(0) DAC(1) DAM (2)
295 m_txPackets[1].packet->RemoveHeader(iphcHdr);
296 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetCid(), true, "CID should be true, is false");
297 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSac(), true, "SAC should be true, is false");
300 "SAM should be HC_COMPR_64, it is not");
301 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetM(), false, "M should be false, is true");
302 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDac(), true, "DAC should be true, is false");
303 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSrcContextId(), 0, "Src context should be 0, it is not");
304 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDstContextId(), 1, "Dst context should be 1, it is not");
307 "DAM should be HC_COMPR_16, it is not");
308
309 // second packet received, expected 2001:2::f00d:f00d:cafe:cafe -> 2001:1::0000:00ff:fe00:cafe
310 m_rxPackets[1].packet->RemoveHeader(ipv6Hdr);
312 Ipv6Address("2001:2::f00d:f00d:cafe:cafe"),
313 "Src address wrongly rebuilt");
315 Ipv6Address("2001:1::0000:00ff:fe00:cafe"),
316 "Dst address wrongly rebuilt");
317
318 // third packet sent, expected CID(17) SAC(1) SAM (2) M(0) DAC(1) DAM (1)
319 m_txPackets[2].packet->RemoveHeader(iphcHdr);
320 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetCid(), true, "CID should be true, is false");
321 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSac(), true, "SAC should be true, is false");
324 "SAM should be HC_COMPR_16, it is not");
325 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetM(), false, "M should be false, is true");
326 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDac(), true, "DAC should be true, is false");
327 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSrcContextId(), 1, "Src context should be 1, it is not");
328 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDstContextId(), 1, "Dst context should be 1, it is not");
331 "DAM should be HC_COMPR_64, it is not");
332
333 // third packet received, expected 2001:1::0000:00ff:fe00:cafe -> 2001:1::f00d:f00d:cafe:cafe
334 m_rxPackets[2].packet->RemoveHeader(ipv6Hdr);
336 Ipv6Address("2001:1::0000:00ff:fe00:cafe"),
337 "Src address wrongly rebuilt");
339 Ipv6Address("2001:1::f00d:f00d:cafe:cafe"),
340 "Dst address wrongly rebuilt");
341
342 // fourth packet sent, expected CID(1) SAC(1) SAM (3) M(0) DAC(1) DAM (1)
343 m_txPackets[3].packet->RemoveHeader(iphcHdr);
344 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetCid(), true, "CID should be true, is false");
345 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSac(), true, "SAC should be true, is false");
348 "SAM should be HC_COMPR_0, it is not");
349 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetM(), false, "M should be false, is true");
350 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDac(), true, "DAC should be true, is false");
351 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetSrcContextId(), 0, "Src context should be 0, it is not");
352 NS_TEST_EXPECT_MSG_EQ(iphcHdr.GetDstContextId(), 1, "Dst context should be 1, it is not");
355 "DAM should be HC_COMPR_64, it is not");
356
357 // fourth packet received, expected srcElided -> 2001:1::f00d:f00d:cafe:cafe
358 m_rxPackets[3].packet->RemoveHeader(ipv6Hdr);
359 NS_TEST_EXPECT_MSG_EQ(ipv6Hdr.GetSource(), srcElided, "Src address wrongly rebuilt");
361 Ipv6Address("2001:1::f00d:f00d:cafe:cafe"),
362 "Dst address wrongly rebuilt");
363
364 m_rxPackets.clear();
365 m_txPackets.clear();
366}
367
368/**
369 * \ingroup sixlowpan-tests
370 *
371 * \brief 6LoWPAN IPHC TestSuite
372 */
374{
375 public:
377
378 private:
379};
380
382 : TestSuite("sixlowpan-iphc-stateful", Type::UNIT)
383{
384 AddTestCase(new SixlowpanIphcStatefulImplTest(), TestCase::Duration::QUICK);
385}
386
388 g_sixlowpanIphcStatefulTestSuite; //!< Static variable for test initialization
6LoWPAN IPHC stateful compression Test
std::vector< Data > m_txPackets
Transmitted packets.
bool ReceiveFromMockDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &source, const Address &destination, NetDevice::PacketType packetType)
Receive from a MockDevice.
void DoRun() override
Implementation to actually run this TestCase.
bool PromiscReceiveFromSixLowPanDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &source, const Address &destination, NetDevice::PacketType packetType)
Promiscuous receive from a SixLowPanNetDevice.
NetDeviceContainer m_mockDevices
MockNetDevice container.
std::vector< Data > m_rxPackets
Received packets.
void SendOnePacket(Ptr< NetDevice > device, Ipv6Address from, Ipv6Address to)
Send one packet.
NetDeviceContainer m_sixDevices
SixLowPanNetDevice container.
a polymophic address class
Definition address.h:90
An implementation of the ICMPv6 protocol.
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.
void SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))
Set the base network number, network prefix, and base interface ID.
Ipv6InterfaceContainer Assign(const NetDeviceContainer &c)
Allocate an Ipv6InterfaceContainer with auto-assigned addresses.
Describes an IPv6 address.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
static Ipv6Address MakeAutoconfiguredAddress(Address addr, Ipv6Address prefix)
Make the autoconfigured IPv6 address from a Mac address.
Packet header for IPv6.
Definition ipv6-header.h:24
void SetDestination(Ipv6Address dst)
Set the "Destination address" field.
void SetSource(Ipv6Address src)
Set the "Source address" field.
void SetHopLimit(uint8_t limit)
Set the "Hop limit" field (TTL).
Ipv6Address GetDestination() const
Get the "Destination address" field.
void SetPayloadLength(uint16_t len)
Set the "Payload length" field.
Ipv6Address GetSource() const
Get the "Source address" field.
void SetNextHeader(uint8_t next)
Set the "Next header" field.
Keep track of a set of IPv6 interfaces.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
IPv6 layer implementation.
Describes an IPv6 prefix.
an EUI-48 address
void Receive(Ptr< Packet > packet, uint16_t protocol, Address to, Address from, NetDevice::PacketType packetType)
Pretend that a packet has been received from a connected Channel.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
PacketType
Packet types are used as they are in Linux.
Definition net-device.h:289
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition node.cc:124
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
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
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Setup a sixlowpan stack to be used as a shim between IPv6 and a generic NetDevice.
NetDeviceContainer Install(NetDeviceContainer c)
Install the SixLoWPAN stack on top of an existing NetDevice.
void AddContext(NetDeviceContainer c, uint8_t contextId, Ipv6Prefix context, Time validity)
Adds a compression Context to a set of NetDevices.
LOWPAN_IPHC base Encoding - see RFC 6282 .
bool GetSac() const
Get the SAC (Source Address Compression) compression.
HeaderCompression_e GetDam() const
Get the DAM (Destination Address Mode) compression.
bool GetDac() const
Get the DAC (Destination Address Compression) compression.
bool GetM() const
Get the M (Multicast) compression.
HeaderCompression_e GetSam() const
Get the SAM (Source Address Mode) compression.
bool GetCid() const
Get the CID (Context Identifier Extension) compression.
uint8_t GetSrcContextId() const
Get the SrcContextId.
uint8_t GetDstContextId() const
Get the DstContextId.
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
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
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_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
Time Minutes(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1296
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:580
static SixlowpanIphcStatefulTestSuite g_sixlowpanIphcStatefulTestSuite
Static variable for test initialization.
Structure to hold the Rx/Tx packets.