Repository navigation
PIFO trees: token buckets and backlog limits per node - #130
Merged
Merged
Conversation
P4PifoTreeScheduler gets two attributes, so that a rate limit or a
backlog limit per class needs no P4 code:
- NodeRates=node:rate[:burst],...: a token bucket per tree node. A
node's rate limits how often its parent schedules it; the root's rate
shapes the port. Burst 0 spaces the packets by their lengths (a leaky
bucket); a burst of B bytes lets B bytes pass at once after an idle
time, as Linux tbf and TbfQueueDisc. The buckets count the frame as it
entered the switch (new P4QueueMetadata::bytes = packet_length) and
compute in 128-bit integers (the shaper form of GCRA), without drift.
- NodeLimits=node:packets,...: at most so many packets in a node's
subtree, the waiting ones too; the lowest full node counts the drop
(GetNodeDrops("a")).
The tree now moves a packet's elements up one level at a time, lazily
and in the order of their times: an element waits for its time from the
program (ShapingFields), then for the bucket of the node below, then
enters the parent. Nested rates thus work as nested shapers.
NextDequeueTime() may be early with rates below the root (the switch asks
again), never late.
Tests: NaiveShapedTree, a reference with linear scans and the bucket in
another form, against PifoTree on random operations (plus a check of the
next dequeue time without the reference); hand cases for rates, bursts,
idle refill, nesting, the bucket after a P4 time, limits, the root's
rate and burst; switch cases with real frames (rate, rate with burst,
limit, the root's rate).
p4-pifo-shaping uses NodeRates=a:<limit>:<burst> and
NodeLimits=a:32,b:32 (new option --burst); shaping.p4 only classifies,
no registers. Same numbers and drops as the register version. switch.md
documents both attributes and keeps ShapingFields, with EDT pacing as
its example.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflicts: - model/p4-core-v1model.cc, EnqueueNow(): keeps #129's fields resolved once per program and adds this branch's meta.bytes. - test/Readme.md: 268 cases (265 on main + 3 of this branch). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Docs (switch.md, the scheduler's header, NodeRates' help): a bucket below the root holds its node's references in front of the parent, not its packets at the output. References that waited there behind a sibling leave back to back, so the rate holds over time, and exactly at the output when the node has priority (new hand case). Exact byte counts also need no rate below the node. NextDequeueTime(): the cases in which it may be early. - Warn once when a packet comes without its length while NodeRates is set (a switch core that does not fill P4QueueMetadata::bytes). - The set_queue_rate warning names NodeRates. - Random test: a copy of the tree gives out nothing between now and the next dequeue time; now and then the time jumps by up to 5 us. - TODO: a bucket at the output, as tbf. 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
With #128, a rate limit per class in a PIFO tree needs rate-limiter code in P4 (registers that compute a shaping time per packet) and a class limit in the ingress. A fixed rate and a backlog cap per tree node are the common case, so the scheduler now does both itself, set by attributes: "class a at most 30 Mbps with bursts up to 3000 bytes, at most 32 packets" needs no P4 code.
How it works
NodeRates=a:30Mbps[:burst],...gives a node a token bucket. It limits how fast references to the node enter its parent (the shaping of the PIFO paper); the root's rate shapes the port. The bucket counts the frame as it entered the switch (packet_length, newP4QueueMetadata::bytes). Burst 0 spaces the references by their packets' lengths (a leaky bucket); a burst of B bytes lets B bytes pass at once after an idle time, as Linuxtbfand ns-3'sTbfQueueDisc. The arithmetic is the shaper form of GCRA in 128-bit integers: exact, no drift.tbf, a bucket below the root sits in front of the parent, not at the output: the parent schedules the references that passed it by rank, so while it serves a sibling first they wait there and then leave back to back. The rate holds over time, and exactly at the output when the node has priority (as class A inp4-pifo-shaping) or with the root's rate. A bucket at the output is noted inTODO.md.NodeLimits=a:32,...caps the packets in a node's subtree, the waiting ones too. A packet beyond it is dropped on arrival; the lowest full node counts it (GetNodeDrops("a")).ShapingFields), then for the bucket of the node below, then enters the parent. Nested rates therefore act as nested shapers (a flow's backlog waits in its own node and does not hold its siblings).NextDequeueTime()is never late; with rates it may be early (the switch then asks again).p4-pifo-shapingusesNodeRates=a:<limit>:<burst>andNodeLimits=a:32,b:32(new--burst);shaping.p4only classifies. Same numbers and drops as the register version of PIFO trees: shaping (ShapingFields) #128.ShapingFieldsstays, documented with an EDT-pacing example.Review guide
utils/p4-pifo-tree.h:BucketTime()/Pass()(the bucket),Push()(limits),Wait()/Enter()/Release()(lazy upward steps),Pop()andNextDequeueTime()(root bucket),CheckInvariant().utils/p4-pifo-tree-scheduler.h/.cc:ParseNodeRates(),ParseNodeLimits(),GetTree(),Enqueue(),GetNodeDrops().utils/p4-queue-scheduler.h+model/p4-core-v1model.cc:bytes.test/p4-queue-scheduler-test-suite.cc:NaiveShapedTree(reference),PifoTreeRateTestCase,PifoTreeRateRandomTestCase,PifoTreeRateSchedulerTestCase.examples/p4-pifo-shaping.cc,examples/p4src/shaping/shaping.p4, thendoc/switch.md,doc/examples.md.Scope
P4QueueMetadata::bytes; the scheduler warns once if a packet comes without its length).p4-pifo-shaping);p4-pifo-shapingover 12 seeds,--limit=10/50/90Mbps,--burst=3000; 27 mutants, all caught. Two independent reviews: the first found thatNextDequeueTime()could be late with a burst at the root and that"a:30 Mbps"(with a space) was read as 30 bit/s; the second found no code bug but docs that called the bucket below the root atbf. All fixed, with tests (a hand case for references waiting behind a sibling; the random test checks on a copy of the tree that nothing leaves before the next dequeue time).Notes for reviewers
EnqueueNow()and the case count).🤖 Generated with Claude Code