Skip to content

PIFO trees: shaping (ShapingFields) - #128

Merged
Mingyumaz merged 3 commits into
mainfrom
feat/pifo-shaping
Dec 2, 2026
Merged

Mingyumaz merged 3 commits into
mainfrom
feat/pifo-shaping

Conversation

@Mingyumaz

@Mingyumaz Mingyumaz commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

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.

Tree root(a,b), class A rate-limited, a packet of A arrives:
  leaf a:  the packet enters at once
  root:    the reference to a waits until shape0
  meanwhile the root only sees b, so B's packets go first; at shape0 A may go again

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.p4 drops 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.

ingress: the program writes leaf, rank0.., shape0.. (switch clock in ns, as ingress_global_timestamp)
EnqueueNow: the core reads the fields and the switch clock (P4QueueMetadata::timestamp)
Enqueue: each time becomes simulation time: now + (shape - timestamp); a time not after the enqueue holds nothing
PifoTree::Push: each level's element goes into its node's PIFO, or into the node's waiting set
port free -> Dequeue -> Pop(now): waiting elements whose time has come enter; then pop from the root
nothing visible at the root -> GetNextDequeueTime -> the root's earliest waiting time
  -> the core schedules the next dequeue then (the port idles until then)

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)

Change Why
P4QueueMetadata::timestamp (new) The switch's clock at enqueue, so a scheduler can turn the program's times into simulation time. Set in EnqueueNow (one line).
MAX_FIELDS 8 → 16 A 4-level tree with shaping reads 9 input fields (leaf, 4 ranks, 4 times).

2. Data structure (utils/p4-pifo-tree.h, the core of the PR)

Function / member Change
Times, NEVER New: a time per level; NEVER = empty tree.
Push(leaf, ranks, item, times = {}, now = 0) Two new parameters with defaults (old calls unchanged). Computes the entry time per level as the running maximum from the leaf up; an element with an entry time after now goes into its node's waiting set. Waiting packets count against the capacity.
Pop(item, leaf, popped, now = 0) First lets waiting elements in (Release(now)); returns false if the root's PIFO is empty, even if packets wait.
NextDequeueTime(now) New: now if the root's PIFO has an element, else the root's earliest waiting time, NEVER if empty.
Waiting() New: waiting elements (packets and references), for tests.
LeafSize() Now includes the packets waiting at the leaf, so enq_qdepth / deq_qdepth count them.
waitingRefs, waitingItems, Release(now) New per-node waiting sets ordered by (time, arrival), each entry with its PIFO key; Release moves due elements in and returns at once when nothing waits.
CheckInvariant() Extended, see below.

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 Pop never 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})

Function / attribute Change
ShapingFields New attribute, default empty; one field per level from the root, an empty name skips its level; more fields than levels stop the simulation (Configure).
GetInputFields() Layout: [0] the leaf (unnamed for a one-leaf tree, so it reads 0), [1..L] the ranks, then the shaping times.
Enqueue() Converts each time: now + (time - timestamp) if time > 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() Calls Pop(..., Now()).
GetNextDequeueTime() Was "now if the tree is not empty"; now the tree's NextDequeueTime.
Now(), MaxTime() New private helpers: simulation time in ns (the trees' clock) and the end of ns-3's time at its resolution.

4. Tests (test/p4-queue-scheduler-test-suite.cc, 4 new cases, 264 in all)

  • tree_v1model.p4 gets three 32-bit delays at the end of its header (ns, 0 = none); the ingress turns them into shapeN = ingress_global_timestamp + delayN. The existing fields keep their offsets.
  • NaiveTree (the reference): every element keeps its entry time and Pop(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 against NaiveTree: the same packets, leaves, ranks and next dequeue times, and the invariant after each step.
  • PifoTreeSchedulerTestCase(128, true) (new instance): ShapingFields set 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 (shape1 in root(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.p4 uses root(a,b): rank0 is the class's priority (A before B), and shape0 comes from a rate limiter per class: next = max(next, now), shape0 = ceil(next), then next += packet length x cost, in ns x 1024 because P4 has no division at run time. The example writes the cost into the register shape_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:

A1 B1 without B1: A1
priority (A1 sent 82.9, then 81.3) 83.0 17.0 81.3
priority + shaping, A ≤ 30 Mbps 30.0 70.0 30.0 (port 70 % idle)

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

  • In: shaping in PifoTree and P4PifoTreeScheduler, the interface addition, the example, 4 test cases, docs.
  • Out: a minimum-rate example; the parent's transactions at release time (as in the paper); PSA; the VOQ fabric.
  • Tested (ns-3.39 and ns-3.49, Docker, on main 1ddce0c):
    • run-tests.sh passes: 24 suites, 264 cases (4 new), 48 example runs; build without warnings.
    • The 47 example runs that main has give the same exit codes and files; their output differs only in wall-clock lines (nothing changes without shaping).
    • p4-pifo-shaping with 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.
    • 19 mutants (tree, scheduler, core, shaping.p4, the field index of the lower levels), all caught; two of them by a hang of the suite.
    • Two independent reviews; their findings are fixed: a backlog leak and unclassified packets in shaping.p4, test gaps (the switch clock, times at lower levels), the end of ns-3's time at other resolutions, docs.
  • Not tested: shaping across a program swap (a swap waits until the switch holds no packets); times near the end of ns-3's time other than by reading the code.

Notes for reviewers

  • The risk is in PifoTree; the scheduler only converts times and the core sets one field.
  • While elements wait, every Pop and NextDequeueTime walks all nodes of the port's tree (a few to a dozen); without waiting elements it returns at once.
  • MAX_FIELDS and timestamp are additions; existing schedulers are unaffected.

Two reviews are needed before merging.

🤖 Generated with Claude Code

Mingyumaz and others added 3 commits December 1, 2026 22:07
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>
@Mingyumaz
Mingyumaz merged commit 0fec981 into main Dec 2, 2026
2 checks passed
Mingyumaz added a commit that referenced this pull request Dec 2, 2026
Conflicts:
- model/p4-core-v1model.cc, EnqueueNow(): keeps this branch's fields
  resolved once per program and adds #128's meta.timestamp.
- test/Readme.md: 265 cases (260 + 1 of this branch + 4 of #128).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mingyumaz
Mingyumaz deleted the feat/pifo-shaping branch December 2, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant