Repository navigation
PIFO trees: shaping (ShapingFields) - #128
Merged
Merged
Conversation
The shaping transactions of the PIFO paper (Sivaraman et al., SIGCOMM 2016) for P4PifoTreeScheduler. The new attribute ShapingFields names a field per level, from the root, with a time in the switch's clock (ns, as ingress_global_timestamp) before which the packet's element of that level does not enter its node. At level 0 the element is the root's reference to the packet's class, so a time there shapes the class; at the leaf's level it paces the packet. An element waits at least as long as the one below it. While all packets of a port wait, the port stays idle, and the switch wakes it through GetNextDequeueTime() when the root gets its next element. Without ShapingFields (the default) nothing changes. - PifoTree: Push() takes a time per level and the current time; waiting elements are kept per node by time and let in by Pop(now) and NextDequeueTime(now). Waiting packets count against the capacity and in LeafSize() (enq_qdepth, deq_qdepth). - P4QueueMetadata::timestamp: the switch's clock at enqueue, so a scheduler can turn times of the P4 program into simulation time. MAX_FIELDS 8 -> 16 (a 4-level tree with shaping reads 9 fields). - A time beyond the end of ns-3's time holds its packet for good, with a warning once. - Unlike the paper, all ranks and times come from the ingress, not from the parent's transactions when an element leaves the shaper (documented). - Example p4-pifo-shaping: strict priority for class A with a rate limit (shaping.p4: per class, the time from which its next packet may go, moved on by each packet's length at the limit, in ns x 1024). Two UDP flows of 80 Mbps into 100 Mbps; with a limit of 30 Mbps A gets 30.0 and B the rest, and A stays at 30.0 when B stops (the port idles); without the limit A takes what it sends. - Tests (suite p4-queue-scheduler, 4 new cases): PifoTree with times by hand and random operations with times against the reference (NaiveTree, now with entry times); in the switch (program tree with a delay per level; the switch starts at 1 ms, so its clock differs from the simulation's), held frames let others pass and leave at their time, pacing with the queue depths of waiting frames, a shaped class with priority, shaping fields without delays change nothing. - Docs: switch.md (shaping), examples.md, README (40 examples), TODO.md, test/Readme.md (264 cases). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without the limit A1 gets exactly what its Poisson source sends, which varies by about 1 % from window to window (82.9, then 81.3 Mbps with the default seed; 81.3 to 85.2 Mbps in the second window over twelve seeds). The example compares A1 with what it sent, so the randomness does not matter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Test: a time at a lower level (shape1 holds node a's references in
root(a(a1,a2),b)), also with level 0 unnamed (",shape1"). Before, only
shape0 was ever set in the switch, so a wrong index for the other levels
passed the suite.
- P4PifoTreeScheduler: the latest waiting time is the end of ns-3's time at
its resolution (Time::Max()), not INT64_MAX ns; the warning for such a time
says that a packet stays in the queues.
- Docs: what follows from computing all times in the ingress and from
references that name nodes (an element that waited keeps its place of
arrival; a reference may let out a longer packet of its class); the
minimum-rate item of TODO.md; examples.md (the table's column, A1's 83.0
against 82.9); shaping.p4 counts per class over all ports.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The PIFO trees of #127 schedule: whenever the port can send, the tree picks a packet. Many policies also need to hold packets back: a rate limit per class ("class A may use at most 30 Mbps, even when the port is idle") or pacing ("send this packet no earlier than t"). The PIFO paper (Sivaraman et al., SIGCOMM 2016) calls these shaping transactions, the second kind of node besides scheduling. This PR adds them to
P4PifoTreeScheduler. A shaped tree is not work-conserving: the port may stay idle while packets wait. Without the new attribute nothing changes.How it works
One time per level. In #127 a packet has one element on each level of its path: the packet itself in its leaf, and above it a reference to the child on its path, each ordered by that level's rank. Now the P4 program may also give each level a time (
ShapingFields, e.g.shape0). Before that time the element does not enter its node, so the node cannot schedule it. A time at level 0 holds the root's reference to the packet's class, which shapes the whole class; a time at the leaf's level holds the packet itself, which paces it.An element waits at least as long as the one below it. It cannot enter its node before its child's element has entered, so the element of level d enters at
max(arrival, time of the leaf's level, ..., time of level d). All entry times are therefore known when the packet arrives; nothing has to cascade later.A reference names a node, not a packet (as in #127). When a shaped class reference enters the root and is given out, the class sends its smallest-rank packet, not necessarily the one that brought the reference. So every packet of a shaped class needs its time: a packet without one lets one of the class's waiting packets out at once.
shaping.p4drops packets that have no class for this reason.Difference from the paper. In the paper the parent's transactions run when an element comes out of the shaper. Here the P4 program computes all ranks and times in the ingress, when the packet arrives. Two things follow from this and from references that name nodes, both documented: an element that waited keeps its place of arrival in its node, so of equal ranks it goes before elements that came while it waited; and a reference spaced for one packet may let out another, longer packet of the class, a short burst. The limit holds for the bytes exactly when the class sends in its order of arrival, as in
shaping.p4.Data flow.
The core already reschedules a port at
GetNextDequeueTime()when a dequeue finds nothing eligible (the rate limits of the priority queues use it), so it needs no change except setting the timestamp.Review guide
In this order:
1. Interface (
utils/p4-queue-scheduler.h,model/p4-core-v1model.cc)P4QueueMetadata::timestamp(new)EnqueueNow(one line).MAX_FIELDS8 → 162. Data structure (
utils/p4-pifo-tree.h, the core of the PR)Times,NEVERNEVER= empty tree.Push(leaf, ranks, item, times = {}, now = 0)nowgoes into its node's waiting set. Waiting packets count against the capacity.Pop(item, leaf, popped, now = 0)Release(now)); returns false if the root's PIFO is empty, even if packets wait.NextDequeueTime(now)nowif the root's PIFO has an element, else the root's earliest waiting time,NEVERif empty.Waiting()LeafSize()enq_qdepth/deq_qdepthcount them.waitingRefs,waitingItems,Release(now)Releasemoves due elements in and returns at once when nothing waits.CheckInvariant()The invariant to check: for each node n and child c, the references to c in n (in the PIFO plus waiting) equal the packets in c's subtree, and the references to c in n's PIFO are at most the elements in c's PIFO. The second part means
Popnever descends into an empty node; it holds because an element never enters before the one below it.3. Scheduler (
utils/p4-pifo-tree-scheduler.{h,cc})ShapingFieldsConfigure).GetInputFields()[0]the leaf (unnamed for a one-leaf tree, so it reads 0),[1..L]the ranks, then the shaping times.Enqueue()now + (time - timestamp)iftime > timestamp, else no wait; capped at the end of ns-3's time, which is never reached, so a packet stays in the queues (one warning).Dequeue()Pop(..., Now()).GetNextDequeueTime()NextDequeueTime.Now(),MaxTime()4. Tests (
test/p4-queue-scheduler-test-suite.cc, 4 new cases, 264 in all)tree_v1model.p4gets three 32-bit delays at the end of its header (ns, 0 = none); the ingress turns them intoshapeN = ingress_global_timestamp + delayN. The existing fields keep their offsets.NaiveTree(the reference): every element keeps its entry time andPop(now)only sees elements whose time has come; a list with linear search, unlike the implementation's maps and waiting sets.PifoTreeShapingTestCase(new, by hand): a held class lets the other pass and comes at its time; an element waits for the one below it; a class reference lets out the class's smallest packet; pacing in one node by time, then by rank; waiting packets count in the leaf and against the capacity.PifoTreeRandomTestCase(true)(new instance): random times on four shapes againstNaiveTree: the same packets, leaves, ranks and next dequeue times, and the invariant after each step.PifoTreeSchedulerTestCase(128, true)(new instance):ShapingFieldsset but no delays gives exactly the same frames as without.PifoTreeShapingSchedulerTestCase(new, real switch, built at 1 ms so its clock differs from the simulation's): held frames let others pass and leave 500 us later while the port idles; pacing spaces frames by 100 us and counts waiting frames in the queue depths; a shaped class with priority leaves at its times while the other class fills the gaps; a time at a lower level (shape1inroot(a(a1,a2),b), also with level 0 unnamed) holds that node's references.5. Example (
examples/p4-pifo-shaping.cc,examples/p4src/shaping/)shaping.p4usesroot(a,b): rank0 is the class's priority (A before B), andshape0comes from a rate limiter per class:next = max(next, now),shape0 = ceil(next), thennext += packet length x cost, in ns x 1024 because P4 has no division at run time. The example writes the cost into the registershape_cost(0 = no limit). Each class may have 32 packets in the queues (the ingress drops more, so a held class cannot fill the shared buffer), and packets without a route or class are dropped.The example builds the same network twice with the same traffic; only the register differs. Two UDP flows of 80 Mbps into 100 Mbps, B1 stops after 1 s. Rates of the Ethernet frames, Mbps:
Without the limit, A1 gets what it sends (it has priority and sends less than the port takes; 83.0 against 82.9 because a few packets queued at the window's start arrive in it). Its Poisson source varies by about 1 % from window to window: 82.9 Mbps in the first window and 81.3 in the second with the default seed, which is why A1 seems to get less once B1 stops. It does not; over twelve seeds (the default one included) the second window gives 81.3 to 85.2 Mbps, in half of them more than the first. With the limit, A1 gets exactly 30.0 Mbps in both windows.
The example checks that A gets what it sent, at most its limit (within 2 %, against snapshots of what it sent in each window, so the source's randomness does not matter), and that B gets the rest of the port, as far as it sent.
6. Docs:
switch.md(shaping, the interface),examples.md, README (40 examples),TODO.md(open: a minimum-rate example, the parent's transactions at release time),test/Readme.md(264 cases).Scope
PifoTreeandP4PifoTreeScheduler, the interface addition, the example, 4 test cases, docs.run-tests.shpasses: 24 suites, 264 cases (4 new), 48 example runs; build without warnings.p4-pifo-shapingwith 12 seeds on each version: A gets 30.0 Mbps in both phases, the same numbers on both versions; limits of 5, 10, 50 and 90 Mbps also pass.shaping.p4, the field index of the lower levels), all caught; two of them by a hang of the suite.shaping.p4, test gaps (the switch clock, times at lower levels), the end of ns-3's time at other resolutions, docs.Notes for reviewers
PifoTree; the scheduler only converts times and the core sets one field.PopandNextDequeueTimewalks all nodes of the port's tree (a few to a dozen); without waiting elements it returns at once.MAX_FIELDSandtimestampare additions; existing schedulers are unaffected.Two reviews are needed before merging.
🤖 Generated with Claude Code