A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sixlowpan-compression-threshold-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 SRM Institute of Science and Technology
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Usham Roy <ushamroy80@gmail.com>
7 */
8
9#include "mock-net-device.h"
10
11#include "ns3/ipv6-header.h"
12#include "ns3/log.h"
13#include "ns3/mac48-address.h"
14#include "ns3/node.h"
15#include "ns3/packet.h"
16#include "ns3/simulator.h"
17#include "ns3/sixlowpan-header.h"
18#include "ns3/sixlowpan-net-device.h"
19#include "ns3/test.h"
20#include "ns3/uinteger.h"
21
22using namespace ns3;
23
24/**
25 * @ingroup sixlowpan-tests
26 *
27 * @brief SixLowPanNetDevice compression threshold test.
28 *
29 * Packets whose compressed form is smaller than the underlying device
30 * padding threshold must be sent with the uncompressed IPv6 dispatch:
31 * link-layer padding is indistinguishable from data on such links, and it
32 * would corrupt the reconstruction of the elided IPv6 Payload Length
33 * (issue #1361).
34 */
36{
37 uint8_t m_dispatch{0}; //!< First byte of the last transmitted frame
38
39 /**
40 * Callback for frames handed to the underlying mock device.
41 *
42 * @param device The mock device.
43 * @param packet The transmitted packet.
44 * @param protocol The protocol number.
45 * @param source The source address.
46 * @param destination The destination address.
47 * @param packetType The packet type.
48 * @return true
49 */
51 Ptr<const Packet> packet,
52 uint16_t protocol,
53 const Address& source,
54 const Address& destination,
55 NetDevice::PacketType packetType);
56
57 /**
58 * Send one packet over a SixLowPanNetDevice.
59 *
60 * @param paddingThreshold Padding threshold reported by the mock device.
61 * @param compressionThreshold Value for the CompressionThreshold attribute.
62 * @param payloadSize IPv6 payload size in bytes.
63 * @return The first dispatch byte of the transmitted frame.
64 */
65 uint8_t SendOnePacket(uint16_t paddingThreshold,
66 uint32_t compressionThreshold,
67 uint32_t payloadSize);
68
69 public:
71 void DoRun() override;
72};
73
75 : TestCase("SixLowPanNetDevice compression threshold from device padding threshold")
76{
77}
78
79bool
81 Ptr<const Packet> packet,
82 uint16_t protocol,
83 const Address& source,
84 const Address& destination,
85 NetDevice::PacketType packetType)
86{
87 packet->CopyData(&m_dispatch, sizeof(m_dispatch));
88 return true;
89}
90
91uint8_t
93 uint32_t compressionThreshold,
94 uint32_t payloadSize)
95{
96 auto node = CreateObject<Node>();
97
98 auto mock = CreateObject<MockNetDevice>();
99 node->AddDevice(mock);
100 mock->SetNode(node);
101 mock->SetAddress(Mac48Address("00:00:00:00:00:01"));
102 mock->SetPaddingThreshold(paddingThreshold);
103 mock->SetSendCallback(
105
107 six->SetAttribute("CompressionThreshold", UintegerValue(compressionThreshold));
108 node->AddDevice(six);
109 six->SetNetDevice(mock);
110
111 // With inline (incompressible) addresses the IPHC-compressed frame is
112 // about 35 bytes plus the payload.
113 Ptr<Packet> pkt = Create<Packet>(payloadSize);
114 Ipv6Header ipHdr;
115 ipHdr.SetSource(Ipv6Address("2001:1::1"));
116 ipHdr.SetDestination(Ipv6Address("2001:1::2"));
117 ipHdr.SetHopLimit(64);
118 ipHdr.SetPayloadLength(payloadSize);
119 ipHdr.SetNextHeader(0xff);
120 pkt->AddHeader(ipHdr);
121
122 // Send() forwards the frame synchronously to the mock device, whose
123 // send callback (ReceiveFromMockDevice) stores the first byte of the
124 // frame in m_dispatch, overwriting the zero (not a 6LoWPAN dispatch)
125 // set here.
126 m_dispatch = 0;
127 six->Send(pkt, Mac48Address("00:00:00:00:00:02"), 0);
128
130 return m_dispatch;
131}
132
133void
135{
136 constexpr uint16_t noPadding = 0;
137 constexpr uint16_t ethernetPadding = 46;
138 constexpr uint32_t defaultThreshold = 0;
139 constexpr uint32_t ethernetThreshold = 46;
140 // The resulting IPv6 packets (40-byte header + payload) are 44 and 60
141 // bytes respectively.
142 constexpr uint32_t smallPayload = 4;
143 constexpr uint32_t largePayload = 20;
144
145 // No padding threshold: the packet is IPHC-compressed.
146 uint8_t dispatch = SendOnePacket(noPadding, defaultThreshold, smallPayload);
149 "Expected IPHC dispatch on a non-padding device");
150
151 // Device padding threshold above the compressed size: the packet must
152 // go out uncompressed even though the CompressionThreshold attribute
153 // is set to the zero default.
154 dispatch = SendOnePacket(ethernetPadding, defaultThreshold, smallPayload);
157 "Expected uncompressed IPv6 dispatch on a padding device");
158
159 // Converse of the previous case: setting the CompressionThreshold
160 // attribute alone must force the uncompressed dispatch.
161 dispatch = SendOnePacket(noPadding, ethernetThreshold, smallPayload);
164 "Expected uncompressed IPv6 dispatch with the attribute set");
165
166 // A payload large enough to exceed the threshold once compressed must
167 // still be IPHC-compressed.
168 dispatch = SendOnePacket(noPadding, ethernetThreshold, largePayload);
171 "Expected IPHC dispatch for a packet above the threshold");
172}
173
174/**
175 * @ingroup sixlowpan-tests
176 *
177 * @brief SixLowPanNetDevice compression threshold TestSuite.
178 */
184
190
191/// Static variable for test initialization
SixLowPanNetDevice compression threshold test.
bool ReceiveFromMockDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &source, const Address &destination, NetDevice::PacketType packetType)
Callback for frames handed to the underlying mock device.
uint8_t SendOnePacket(uint16_t paddingThreshold, uint32_t compressionThreshold, uint32_t payloadSize)
Send one packet over a SixLowPanNetDevice.
uint8_t m_dispatch
First byte of the last transmitted frame.
void DoRun() override
Implementation to actually run this TestCase.
SixLowPanNetDevice compression threshold TestSuite.
a polymophic address class
Definition address.h:114
Describes an IPv6 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).
void SetPayloadLength(uint16_t len)
Set the "Payload length" field.
void SetNextHeader(uint8_t next)
Set the "Next header" field.
an EUI-48 address
PacketType
Packet types are used as they are in Linux.
Definition net-device.h:314
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static Dispatch_e GetDispatchType(uint8_t dispatch)
Get the Dispatch type.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Hold an unsigned integer type.
Definition uinteger.h:34
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:690
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#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:240
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static SixLowPanCompressionThresholdTestSuite g_sixLowPanCompressionThresholdTestSuite
Static variable for test initialization.