22#include "ns3/codel-queue-disc.h"
23#include "ns3/double.h"
25#include "ns3/packet.h"
26#include "ns3/simulator.h"
27#include "ns3/string.h"
29#include "ns3/uinteger.h"
34#define REC_INV_SQRT_BITS_ns3 (8 * sizeof(uint16_t))
37#define REC_INV_SQRT_SHIFT_ns3 (32 - REC_INV_SQRT_BITS_ns3)
43 uint32_t invsqrt2 = ((uint64_t)invsqrt * invsqrt) >> 32;
44 uint64_t val = (3LL << 32) - ((uint64_t)count * invsqrt2);
47 val = (val * invsqrt) >> (32 - 2 + 1);
54 return (
uint32_t)(((uint64_t)val * ep_ro) >> 32);
91 m_ecnCapablePacket(ecnCapable)
124 void DoRun()
override;
131 :
TestCase(
"Basic enqueue and dequeue operations, and attribute setting")
148 "Verify that we can actually set the attribute MinBytes");
151 "Verify that we can actually set the attribute Interval");
154 "Verify that we can actually set the attribute Target");
156 if (
m_mode == QueueSizeUnit::BYTES)
160 else if (
m_mode == QueueSizeUnit::PACKETS)
167 "Verify that we can actually set the attribute MaxSize");
185 "There should be no packets in queue");
186 queue->Enqueue(Create<CodelQueueDiscTestItem>(p1, dest,
false));
189 "There should be one packet in queue");
190 queue->Enqueue(Create<CodelQueueDiscTestItem>(p2, dest,
false));
193 "There should be two packets in queue");
194 queue->Enqueue(Create<CodelQueueDiscTestItem>(p3, dest,
false));
197 "There should be three packets in queue");
198 queue->Enqueue(Create<CodelQueueDiscTestItem>(p4, dest,
false));
201 "There should be four packets in queue");
202 queue->Enqueue(Create<CodelQueueDiscTestItem>(p5, dest,
false));
205 "There should be five packets in queue");
206 queue->Enqueue(Create<CodelQueueDiscTestItem>(p6, dest,
false));
209 "There should be six packets in queue");
213 "There should be no packets being dropped due to full queue");
217 item = queue->Dequeue();
221 "There should be five packets in queue");
224 item = queue->Dequeue();
228 "There should be four packets in queue");
231 "Was this the second packet ?");
233 item = queue->Dequeue();
237 "There should be three packets in queue");
240 item = queue->Dequeue();
244 "There should be two packets in queue");
247 "Was this the fourth packet ?");
249 item = queue->Dequeue();
253 "There should be one packet in queue");
256 item = queue->Dequeue();
260 "There should be zero packet in queue");
263 item = queue->Dequeue();
269 "There should be no packet drops according to CoDel algorithm");
286 void DoRun()
override;
300 :
TestCase(
"Basic overflow behavior")
314 if (
m_mode == QueueSizeUnit::BYTES)
318 else if (
m_mode == QueueSizeUnit::PACKETS)
333 "Verify that we can actually set the attribute MaxSize");
336 "Verify that we can actually set the attribute MinBytes");
341 queue->Enqueue(Create<CodelQueueDiscTestItem>(p1, dest,
false));
342 queue->Enqueue(Create<CodelQueueDiscTestItem>(p2, dest,
false));
343 queue->Enqueue(Create<CodelQueueDiscTestItem>(p3, dest,
false));
347 "There should be 500 packets in queue");
350 "There should be three packets being dropped due to full queue");
359 queue->Enqueue(Create<CodelQueueDiscTestItem>(Create<Packet>(size), dest,
false));
372 void DoRun()
override;
376 :
TestCase(
"NewtonStep arithmetic unit test")
388 for (uint16_t recInvSqrt = 0xff; recInvSqrt > 0; recInvSqrt /= 2)
390 for (
uint32_t count = 1; count < 0xff; count *= 2)
392 result = queue->NewtonStep(recInvSqrt, count);
396 "ns-3 NewtonStep() fails to match Linux equivalent");
410 void DoRun()
override;
422 :
TestCase(
"ControlLaw arithmetic unit test")
447 for (uint16_t recInvSqrt = 0xff; recInvSqrt > 0; recInvSqrt /= 2)
449 codelTimeVal = queue->Time2CoDel(timeVal);
450 uint32_t ns3Result = queue->ControlLaw(codelTimeVal, interval, recInvSqrt);
454 "Linux result for ControlLaw should equal ns-3 result");
473 void DoRun()
override;
518 if (
m_mode == QueueSizeUnit::BYTES)
522 else if (
m_mode == QueueSizeUnit::PACKETS)
530 "Verify that we can actually set the attribute MaxSize");
537 "There should be 20 packets in queue.");
541 Time waitUntilFirstDequeue = 2 * queue->GetTarget();
549 Time waitUntilSecondDequeue = waitUntilFirstDequeue + 2 * queue->GetInterval();
582 queue->Enqueue(Create<CodelQueueDiscTestItem>(Create<Packet>(size), dest,
false));
591 uint32_t initialQSize = queue->GetCurrentSize().GetValue();
592 uint32_t initialDropNext = queue->GetDropNext();
596 if (initialDropCount > 0 && currentTime.
GetMicroSeconds() >= initialDropNext)
598 queue->TraceConnectWithoutContext(
603 if (initialQSize != 0)
606 if (initialDropCount == 0 && currentTime > queue->GetTarget())
608 if (currentTime < queue->GetInterval())
614 "We are not in dropping state."
615 "Sojourn time has just gone above target from below."
616 "Hence, there should be no packet drops");
618 initialQSize - modeSize,
619 "There should be 1 packet dequeued.");
621 else if (currentTime >= queue->GetInterval())
626 initialQSize - 2 * modeSize,
627 "Sojourn time has been above target for at least interval."
628 "We enter the dropping state, perform initial packet drop, "
629 "and dequeue the next."
630 "So there should be 2 more packets dequeued.");
634 else if (initialDropCount > 0)
641 initialQSize - modeSize,
642 "We are in dropping state."
643 "Sojourn is still above target."
644 "However, it's not time for next drop."
645 "So there should be only 1 more packet dequeued");
650 "There should still be only 1 packet drop from the last dequeue");
658 "We are in dropping state."
659 "It's time for next drop."
660 "The number of packets dequeued equals to the number of "
661 "times m_dropNext is updated plus initial dequeue");
664 "The number of drops equals to the number of times "
665 "m_dropNext is updated plus 1 from last dequeue");
685 void DoRun()
override;
743 if (
m_mode == QueueSizeUnit::BYTES)
747 else if (
m_mode == QueueSizeUnit::PACKETS)
755 "Verify that we can actually set the attribute MaxSize");
758 "Verify that we can actually set the attribute UseEcn");
766 "There should be 20 packets in queue.");
770 Time waitUntilFirstDequeue = 2 * queue->GetTarget();
779 Time waitUntilSecondDequeue = waitUntilFirstDequeue + 2 * queue->GetInterval();
791 queue = CreateObject<CoDelQueueDisc>();
795 "Verify that we can actually set the attribute MaxSize");
798 "Verify that we can actually set the attribute UseEcn");
806 "There should be 20 packets in queue.");
847 queue = CreateObject<CoDelQueueDisc>();
851 "Verify that we can actually set the attribute MaxSize");
854 "Verify that we can actually set the attribute UseEcn");
857 "Verify that we can actually set the attribute CeThreshold");
867 "There should be 20 packets in queue.");
909 queue = CreateObject<CoDelQueueDisc>();
913 "Verify that we can actually set the attribute MaxSize");
916 "Verify that we can actually set the attribute UseEcn");
919 "Verify that we can actually set the attribute CeThreshold");
927 "There should be 20 packets in queue.");
977 queue->Enqueue(Create<CodelQueueDiscTestItem>(Create<Packet>(size), dest, ecnCapable));
988 uint32_t initialQSize = queue->GetCurrentSize().GetValue();
989 uint32_t initialDropNext = queue->GetDropNext();
992 uint32_t currentTargetMarkCount = 0;
993 uint32_t currentCeThreshMarkCount = 0;
995 if (initialTargetMarkCount > 0 && currentTime.
GetMicroSeconds() >= initialDropNext &&
998 queue->TraceConnectWithoutContext(
1003 if (initialQSize != 0)
1010 if (currentDropCount == 1)
1015 else if (testCase == 2)
1017 if (initialTargetMarkCount == 0 && currentTime > queue->GetTarget())
1019 if (currentTime < queue->GetInterval())
1023 currentTargetMarkCount =
1025 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1028 initialQSize - modeSize,
1029 "There should be 1 packet dequeued.");
1032 "There should not be any packet drops");
1034 currentTargetMarkCount,
1036 "We are not in dropping state."
1037 "Sojourn time has just gone above target from below."
1038 "Hence, there should be no target exceeded marked packets");
1040 currentCeThreshMarkCount,
1042 "Marking due to CE threshold is disabled"
1043 "Hence, there should not be any CE threshold exceeded marked packet");
1045 else if (currentTime >= queue->GetInterval())
1049 currentTargetMarkCount =
1052 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1055 queue->GetCurrentSize().GetValue(),
1056 initialQSize - modeSize,
1057 "Sojourn time has been above target for at least interval."
1058 "We enter the dropping state and perform initial packet marking"
1059 "So there should be only 1 more packet dequeued.");
1062 "There should not be any packet drops");
1065 "There should be 1 target exceeded marked packet");
1067 currentCeThreshMarkCount,
1069 "There should not be any CE threshold exceeded marked packet");
1072 else if (initialTargetMarkCount > 0)
1078 currentTargetMarkCount =
1080 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1083 initialQSize - modeSize,
1084 "We are in dropping state."
1085 "Sojourn is still above target."
1086 "However, it's not time for next target exceeded mark."
1087 "So there should be only 1 more packet dequeued");
1090 "There should not be any packet drops");
1093 "There should still be only 1 target exceeded marked "
1094 "packet from the last dequeue");
1096 currentCeThreshMarkCount,
1098 "There should not be any CE threshold exceeded marked packet");
1104 currentTargetMarkCount =
1106 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1109 initialQSize - modeSize,
1110 "We are in dropping state."
1111 "It's time for packet to be marked"
1112 "So there should be only 1 more packet dequeued");
1115 "There should not be any packet drops");
1118 "There should 2 target exceeded marked packet");
1122 "Number of packets in the queue before drop should be equal"
1123 "to number of packets in the queue before first mark as the behavior "
1124 "until packet N should be the same.");
1126 currentCeThreshMarkCount,
1128 "There should not be any CE threshold exceeded marked packet");
1132 else if (testCase == 3)
1134 if (initialTargetMarkCount == 0 && currentTime > queue->GetTarget())
1136 if (currentTime < queue->GetInterval())
1140 currentTargetMarkCount =
1142 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1145 initialQSize - modeSize,
1146 "There should be 1 packet dequeued.");
1149 "There should not be any packet drops");
1151 currentTargetMarkCount,
1153 "We are not in dropping state."
1154 "Sojourn time has just gone above target from below."
1155 "Hence, there should be no target exceeded marked packets");
1157 currentCeThreshMarkCount,
1159 "Sojourn time has gone above CE threshold."
1160 "Hence, there should be 1 CE threshold exceeded marked packet");
1162 else if (currentTime >= queue->GetInterval())
1166 currentTargetMarkCount =
1168 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1171 queue->GetCurrentSize().GetValue(),
1172 initialQSize - modeSize,
1173 "Sojourn time has been above target for at least interval."
1174 "We enter the dropping state and perform initial packet marking"
1175 "So there should be only 1 more packet dequeued.");
1178 "There should not be any packet drops");
1181 "There should be 1 target exceeded marked packet");
1184 "There should be 1 CE threshold exceeded marked packets");
1187 else if (initialTargetMarkCount > 0)
1193 currentTargetMarkCount =
1195 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1198 initialQSize - modeSize,
1199 "We are in dropping state."
1200 "Sojourn is still above target."
1201 "However, it's not time for next target exceeded mark."
1202 "So there should be only 1 more packet dequeued");
1205 "There should not be any packet drops");
1208 "There should still be only 1 target exceeded marked "
1209 "packet from the last dequeue");
1212 "There should be 2 CE threshold exceeded marked packets");
1218 currentTargetMarkCount =
1220 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1223 queue->GetCurrentSize().GetValue(),
1225 "We are in dropping state."
1226 "It's time for packet to be dropped as packets are not ecnCapable"
1227 "The number of packets dequeued equals to the number of times m_dropNext "
1228 "is updated plus initial dequeue");
1232 "The number of drops equals to the number of times m_dropNext is updated");
1234 currentTargetMarkCount,
1236 "There should still be only 1 target exceeded marked packet");
1239 "There should still be 2 CE threshold exceeded marked "
1240 "packet as packets are not ecnCapable");
1244 else if (testCase == 4)
1246 if (currentTime < queue->GetTarget())
1248 if (initialCeThreshMarkCount == 0 && currentTime <
MilliSeconds(2))
1252 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1255 initialQSize - modeSize,
1256 "There should be 1 packet dequeued.");
1259 "There should not be any packet drops");
1261 currentCeThreshMarkCount,
1263 "Sojourn time has not gone above CE threshold."
1264 "Hence, there should not be any CE threshold exceeded marked packet");
1270 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1273 initialQSize - modeSize,
1274 "There should be only 1 more packet dequeued.");
1277 "There should not be any packet drops");
1280 "Sojourn time has gone above CE threshold."
1281 "There should be 1 CE threshold exceeded marked packet");
1284 else if (initialCeThreshMarkCount > 0 && currentTime < queue->GetInterval())
1286 if (initialCeThreshMarkCount < 2)
1290 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1293 initialQSize - modeSize,
1294 "There should be only 1 more packet dequeued.");
1297 "There should not be any packet drops");
1300 "There should be 2 CE threshold exceeded marked packets");
1306 currentCeThreshMarkCount = queue->GetStats().GetNMarkedPackets(
1309 initialQSize - modeSize,
1310 "There should be only 1 more packet dequeued.");
1313 "There should not be any packet drops");
1316 "There should be 3 CE threshold exceeded marked packet");
Test 5: enqueue/dequeue with drops according to CoDel algorithm.
uint32_t m_dropNextCount
count the number of times m_dropNext is recalculated
CoDelQueueDiscBasicDrop(QueueSizeUnit mode)
Constructor.
void Dequeue(Ptr< CoDelQueueDisc > queue, uint32_t modeSize)
Dequeue function.
void Enqueue(Ptr< CoDelQueueDisc > queue, uint32_t size, uint32_t nPkt)
Enqueue function.
void DoRun() override
Implementation to actually run this TestCase.
void DropNextTracer(uint32_t oldVal, uint32_t newVal)
Drop next tracer function.
Test 1: simple enqueue/dequeue with no drops.
CoDelQueueDiscBasicEnqueueDequeue(QueueSizeUnit mode)
Constructor.
void DoRun() override
Implementation to actually run this TestCase.
Test 6: enqueue/dequeue with marks according to CoDel algorithm.
uint32_t nPacketsBeforeFirstMark
Number of packets in the queue before first mark.
void Enqueue(Ptr< CoDelQueueDisc > queue, uint32_t size, uint32_t nPkt, bool ecnCapable)
Enqueue function.
uint32_t m_dropNextCount
count the number of times m_dropNext is recalculated
void DropNextTracer(uint32_t oldVal, uint32_t newVal)
Drop next tracer function.
uint32_t nPacketsBeforeFirstDrop
Number of packets in the queue before first drop.
void Dequeue(Ptr< CoDelQueueDisc > queue, uint32_t modeSize, uint32_t testCase)
Dequeue function.
void DoRun() override
Implementation to actually run this TestCase.
CoDelQueueDiscBasicMark(QueueSizeUnit mode)
Constructor.
Test 2: enqueue with drops due to queue overflow.
CoDelQueueDiscBasicOverflow(QueueSizeUnit mode)
Constructor.
void DoRun() override
Implementation to actually run this TestCase.
void Enqueue(Ptr< CoDelQueueDisc > queue, uint32_t size, uint32_t nPkt)
Enqueue function.
Test 4: ControlLaw unit test - test against explicit port of Linux implementation.
void DoRun() override
Implementation to actually run this TestCase.
CoDelQueueDiscControlLawTest()
uint32_t _codel_control_law(uint32_t t, uint32_t interval, uint32_t recInvSqrt)
Codel control law function.
Test 3: NewtonStep unit test - test against explicit port of Linux implementation.
void DoRun() override
Implementation to actually run this TestCase.
CoDelQueueDiscNewtonStepTest()
CoDel Queue Disc Test Suite.
CoDelQueueDiscTestSuite()
Codel Queue Disc Test Item.
void AddHeader() override
Add the header to the packet.
~CodelQueueDiscTestItem() override
bool Mark() override
Marks the packet as a substitute for dropping it, such as for Explicit Congestion Notification.
bool m_ecnCapablePacket
ECN capable packet?
CodelQueueDiscTestItem & operator=(const CodelQueueDiscTestItem &)=delete
CodelQueueDiscTestItem(const CodelQueueDiscTestItem &)=delete
CodelQueueDiscTestItem()=delete
a polymophic address class
AttributeValue implementation for Boolean.
static constexpr const char * OVERLIMIT_DROP
Overlimit dropped packet.
static constexpr const char * TARGET_EXCEEDED_DROP
Sojourn time above target.
static constexpr const char * CE_THRESHOLD_EXCEEDED_MARK
Sojourn time above CE threshold.
static constexpr const char * TARGET_EXCEEDED_MARK
Sojourn time above target.
Smart pointer class similar to boost::intrusive_ptr.
QueueDiscItem is the abstract base class for items that are stored in a queue disc.
Class for representing queue sizes.
AttributeValue implementation for QueueSize.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
static Time Now()
Return the current simulation virtual time.
static void Run()
Run the simulation.
Hold variables of type string.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
@ UNIT
This test suite implements a Unit Test.
Simulation virtual time values and global simulation resolution.
int64_t GetMicroSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
AttributeValue implementation for Time.
Hold an unsigned integer type.
#define REC_INV_SQRT_SHIFT_ns3
static uint16_t _codel_Newton_step(uint16_t rec_inv_sqrt, uint32_t count)
static uint32_t _reciprocal_scale(uint32_t val, uint32_t ep_ro)
QueueSizeUnit
Enumeration of the operating modes of queues.
#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.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Time Seconds(double value)
Construct a Time in the indicated unit.
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
CoDelQueueDiscTestSuite g_coDelQueueTestSuite
the test suite
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...
uint32_t pktSize
packet size used for the simulation (in bytes)