A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-net-device-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7 */
8
9#include "ns3/address.h"
10#include "ns3/boolean.h"
11#include "ns3/callback.h"
12#include "ns3/data-rate.h"
13#include "ns3/double.h"
14#include "ns3/enum.h"
15#include "ns3/error-model.h"
16#include "ns3/mac48-address.h"
17#include "ns3/node.h"
18#include "ns3/packet.h"
19#include "ns3/pointer.h"
20#include "ns3/queue.h"
21#include "ns3/simple-channel.h"
22#include "ns3/simple-net-device.h"
23#include "ns3/simulator.h"
24#include "ns3/string.h"
25#include "ns3/test.h"
26
27using namespace ns3;
28
29/**
30 * @ingroup network-test
31 * @ingroup tests
32 *
33 * @brief Regression test for @issueid{370}: a received packet with no matching
34 * protocol handler must be traced, not dropped.
35 *
36 * The receiver device is installed on a Node (so its receive callback is the
37 * real Node::NonPromiscReceiveFromDevice), but no protocol handler is
38 * registered for the chosen protocol number. A unicast packet addressed to the
39 * receiver is sent; the device must honor the @c false return and fire the
40 * MacRxDrop trace.
41 */
43{
44 public:
46
47 private:
48 void DoRun() override;
49
50 /**
51 * MacRxDrop trace sink.
52 * @param p The dropped packet.
53 */
55
56 uint32_t m_drops{0}; //!< Number of MacRxDrop events observed.
57};
58
60 : TestCase("Packet dropped for missing protocol handler fires MacRxDrop (issue #370)")
61{
62}
63
64void
69
70void
72{
73 auto a = CreateObject<Node>();
74 auto b = CreateObject<Node>();
75
76 auto sender = CreateObject<SimpleNetDevice>();
77 auto receiver = CreateObject<SimpleNetDevice>();
78 auto channel = CreateObject<SimpleChannel>();
79
80 ObjectFactory queueFactory("ns3::DropTailQueue<Packet>");
81 sender->SetQueue(queueFactory.Create<Queue<Packet>>());
82 receiver->SetQueue(queueFactory.Create<Queue<Packet>>());
83
84 // AddDevice() binds the receive callback to Node::NonPromiscReceiveFromDevice.
85 // No RegisterProtocolHandler() is called on node b, so no handler matches.
86 a->AddDevice(sender);
87 b->AddDevice(receiver);
88
89 sender->SetNode(a);
90 sender->SetChannel(channel);
91 sender->SetAddress(Mac48Address::Allocate());
92
93 receiver->SetNode(b);
94 receiver->SetChannel(channel);
95 receiver->SetAddress(Mac48Address::Allocate());
96
97 receiver->TraceConnectWithoutContext(
98 "MacRxDrop",
100
101 // Unicast to the receiver's own address (classified PACKET_HOST) with a
102 // protocol number that has no registered handler on node b.
103 auto pkt = Create<Packet>(64);
106 sender,
107 pkt,
108 receiver->GetAddress(),
109 static_cast<uint16_t>(0x8888));
110
113
115 1,
116 "Packet dropped due to missing protocol handler was not traced "
117 "(issue #370): expected the MacRxDrop trace to fire once.");
118}
119
120/**
121 * @ingroup network-test
122 * @ingroup tests
123 *
124 * @brief Tests that a ReceiveErrorModel discards the packet and fires PhyRxDrop.
125 *
126 * With a receive error model that corrupts every packet, a frame handed to the
127 * receiver must be dropped at the physical layer: the PhyRxDrop trace fires and
128 * the packet is never delivered to the receive callback. The control case (no
129 * error model) confirms the same frame is otherwise delivered.
130 */
132{
133 public:
135
136 private:
137 void DoRun() override;
138
139 /**
140 * PhyRxDrop trace sink.
141 * @param p The dropped packet.
142 */
144
145 /**
146 * Receive callback counting delivered packets.
147 * @param d The receiving device.
148 * @param p The received packet.
149 * @param proto The protocol number.
150 * @param from The source address.
151 * @return Always true (packet accepted).
152 */
153 bool Rx(Ptr<NetDevice> d, Ptr<const Packet> p, uint16_t proto, const Address& from);
154
155 uint32_t m_phyDrops{0}; //!< Number of PhyRxDrop events observed.
156 uint32_t m_deliveries{0}; //!< Number of packets delivered to the callback.
157};
158
160 : TestCase("ReceiveErrorModel drops a packet and fires PhyRxDrop")
161{
162}
163
164void
169
170bool
173 uint16_t proto,
174 const Address& from)
175{
176 m_deliveries++;
177 return true;
178}
179
180void
182{
183 for (bool corrupt : {false, true})
184 {
185 m_phyDrops = 0;
186 m_deliveries = 0;
187
188 auto sender = CreateObject<SimpleNetDevice>();
189 auto receiver = CreateObject<SimpleNetDevice>();
190 auto channel = CreateObject<SimpleChannel>();
191
192 ObjectFactory queueFactory("ns3::DropTailQueue<Packet>");
193 sender->SetQueue(queueFactory.Create<Queue<Packet>>());
194 receiver->SetQueue(queueFactory.Create<Queue<Packet>>());
195
196 // Devices need a Node so SimpleChannel can deliver in the node's context.
197 // Node::AddDevice (rather than SetNode) is used so the node owns the
198 // devices and disposes them on Simulator::Destroy, breaking the
199 // device/channel reference cycle. The custom receive callback set
200 // below still wins, as it is installed after AddDevice.
201 CreateObject<Node>()->AddDevice(sender);
202 CreateObject<Node>()->AddDevice(receiver);
203
204 sender->SetChannel(channel);
205 sender->SetAddress(Mac48Address::Allocate());
206 receiver->SetChannel(channel);
207 receiver->SetAddress(Mac48Address::Allocate());
208
209 if (corrupt)
210 {
211 // Packet-level rate of 1.0 corrupts every packet deterministically.
213 em->SetAttribute("ErrorUnit", EnumValue(RateErrorModel::ERROR_UNIT_PACKET));
214 em->SetAttribute("ErrorRate", DoubleValue(1.0));
215 receiver->SetAttribute("ReceiveErrorModel", PointerValue(em));
216 }
217
218 receiver->SetReceiveCallback(MakeCallback(&SimpleNetDeviceErrorModelTestCase::Rx, this));
219 receiver->TraceConnectWithoutContext(
220 "PhyRxDrop",
222
223 auto pkt = Create<Packet>(64);
226 sender,
227 pkt,
228 receiver->GetAddress(),
229 static_cast<uint16_t>(0x0800));
230
233
234 if (corrupt)
235 {
236 NS_TEST_ASSERT_MSG_EQ(m_phyDrops, 1, "Corrupt packet should fire PhyRxDrop once");
237 NS_TEST_ASSERT_MSG_EQ(m_deliveries, 0, "Corrupt packet must not be delivered");
238 }
239 else
240 {
241 NS_TEST_ASSERT_MSG_EQ(m_phyDrops, 0, "Uncorrupted packet should not fire PhyRxDrop");
242 NS_TEST_ASSERT_MSG_EQ(m_deliveries, 1, "Uncorrupted packet should be delivered once");
243 }
244 }
245}
246
247/**
248 * @ingroup network-test
249 * @ingroup tests
250 *
251 * @brief Tests that the DataRate attribute governs the transmission time.
252 *
253 * A packet sent through a device configured with a finite DataRate is delivered
254 * after packetSize / DataRate seconds (the channel delay is zero). Sending a
255 * 125-byte (1000-bit) packet at 1 Mbps must complete at exactly 1 ms.
256 */
258{
259 public:
261
262 private:
263 void DoRun() override;
264
265 /**
266 * Receive callback recording the reception time.
267 * @param d The receiving device.
268 * @param p The received packet.
269 * @param proto The protocol number.
270 * @param from The source address.
271 * @return Always true (packet accepted).
272 */
273 bool Rx(Ptr<NetDevice> d, Ptr<const Packet> p, uint16_t proto, const Address& from);
274
275 Time m_rxTime{Time::Min()}; //!< Time the packet was received.
276};
277
279 : TestCase("DataRate attribute governs the transmission time")
280{
281}
282
283bool
286 uint16_t proto,
287 const Address& from)
288{
290 return true;
291}
292
293void
295{
296 auto sender = CreateObject<SimpleNetDevice>();
297 auto receiver = CreateObject<SimpleNetDevice>();
298 auto channel = CreateObject<SimpleChannel>();
299
300 ObjectFactory queueFactory("ns3::DropTailQueue<Packet>");
301 sender->SetQueue(queueFactory.Create<Queue<Packet>>());
302 receiver->SetQueue(queueFactory.Create<Queue<Packet>>());
303
304 // Devices need a Node so SimpleChannel can deliver in the node's context.
305 // Node::AddDevice (rather than SetNode) is used so the node owns the
306 // devices and disposes them on Simulator::Destroy, breaking the
307 // device/channel reference cycle. The custom receive callback set below
308 // still wins, as it is installed after AddDevice.
309 CreateObject<Node>()->AddDevice(sender);
310 CreateObject<Node>()->AddDevice(receiver);
311
312 // The transmission time is computed from the sending device's DataRate.
313 sender->SetAttribute("DataRate", DataRateValue(DataRate("1Mbps")));
314
315 sender->SetChannel(channel);
316 sender->SetAddress(Mac48Address::Allocate());
317 receiver->SetChannel(channel);
318 receiver->SetAddress(Mac48Address::Allocate());
319
320 receiver->SetReceiveCallback(MakeCallback(&SimpleNetDeviceDataRateTestCase::Rx, this));
321
322 // 125 bytes = 1000 bits; at 1 Mbps the transmission takes exactly 1 ms.
323 auto pkt = Create<Packet>(125);
326 sender,
327 pkt,
328 receiver->GetAddress(),
329 static_cast<uint16_t>(0x0800));
330
333
335 MilliSeconds(1),
336 "A 1000-bit packet at 1 Mbps should be received after 1 ms");
337}
338
339/**
340 * @ingroup network-test
341 * @ingroup tests
342 *
343 * @brief Test suite exercising SimpleNetDevice reception behavior.
344 */
346{
347 public:
349};
350
358
359static SimpleNetDeviceTestSuite g_simpleNetDeviceTestSuite; //!< Static variable for test init
Tests that the DataRate attribute governs the transmission time.
Time m_rxTime
Time the packet was received.
void DoRun() override
Implementation to actually run this TestCase.
bool Rx(Ptr< NetDevice > d, Ptr< const Packet > p, uint16_t proto, const Address &from)
Receive callback recording the reception time.
Tests that a ReceiveErrorModel discards the packet and fires PhyRxDrop.
void PhyDrop(Ptr< const Packet > p)
PhyRxDrop trace sink.
uint32_t m_deliveries
Number of packets delivered to the callback.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_phyDrops
Number of PhyRxDrop events observed.
bool Rx(Ptr< NetDevice > d, Ptr< const Packet > p, uint16_t proto, const Address &from)
Receive callback counting delivered packets.
Regression test for Issue 370: a received packet with no matching protocol handler must be traced,...
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_drops
Number of MacRxDrop events observed.
void DropEvent(Ptr< const Packet > p)
MacRxDrop trace sink.
Test suite exercising SimpleNetDevice reception behavior.
a polymophic address class
Definition address.h:114
Class for representing data rates.
Definition data-rate.h:78
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Hold variables of type enum.
Definition enum.h:52
static Mac48Address Allocate()
Allocate a new Mac48Address.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
AttributeValue implementation for Pointer.
Definition pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Template class for packet Queues.
Definition queue.h:257
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Run()
Run the simulation.
Definition simulator.cc:161
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
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
static Time Min()
Minimum representable Time Not to be confused with Min(Time,Time).
Definition nstime.h:277
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_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:133
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1330
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static SimpleNetDeviceTestSuite g_simpleNetDeviceTestSuite
Static variable for test init.