Skip to content

PIFO trees: token buckets and backlog limits per node - #130

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

Mingyumaz merged 3 commits into
mainfrom
feat/pifo-token-bucket

Conversation

@Mingyumaz

@Mingyumaz Mingyumaz commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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, new P4QueueMetadata::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 Linux tbf and ns-3's TbfQueueDisc. The arithmetic is the shaper form of GCRA in 128-bit integers: exact, no drift.
  • Unlike 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 in p4-pifo-shaping) or with the root's rate. A bucket at the output is noted in TODO.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")).
  • The tree now moves a packet's elements up one level at a time, lazily and in the global order of their times: an element waits for its time from the P4 program (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-shaping uses NodeRates=a:<limit>:<burst> and NodeLimits=a:32,b:32 (new --burst); shaping.p4 only classifies. Same numbers and drops as the register version of PIFO trees: shaping (ShapingFields) #128. ShapingFields stays, documented with an EDT-pacing example.

Review guide

  1. utils/p4-pifo-tree.h: BucketTime() / Pass() (the bucket), Push() (limits), Wait() / Enter() / Release() (lazy upward steps), Pop() and NextDequeueTime() (root bucket), CheckInvariant().
  2. utils/p4-pifo-tree-scheduler.h/.cc: ParseNodeRates(), ParseNodeLimits(), GetTree(), Enqueue(), GetNodeDrops().
  3. utils/p4-queue-scheduler.h + model/p4-core-v1model.cc: bytes.
  4. test/p4-queue-scheduler-test-suite.cc: NaiveShapedTree (reference), PifoTreeRateTestCase, PifoTreeRateRandomTestCase, PifoTreeRateSchedulerTestCase.
  5. examples/p4-pifo-shaping.cc, examples/p4src/shaping/shaping.p4, then doc/switch.md, doc/examples.md.

Scope

  • In: per-node token bucket (rate, burst) and backlog limit as scheduler attributes; lazy tree; example and docs switched over.
  • Out: a bucket at the output for nodes below the root, different rates per port, changing rates at run time (no CLI command), borrowing between nodes (HTB-style), PSA (its branch must fill P4QueueMetadata::bytes; the scheduler warns once if a packet comes without its length).
  • Tested (ns-3.39 and ns-3.49, Docker, with main 24b1452 merged in, v1model core: find PHV fields and P4 objects once per program #129 included): build 0 warnings, 24 suites / 268 cases, all 48 example runs (outputs identical to main except the renamed drop line of p4-pifo-shaping); p4-pifo-shaping over 12 seeds, --limit=10/50/90Mbps, --burst=3000; 27 mutants, all caught. Two independent reviews: the first found that NextDequeueTime() 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 a tbf. 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).
  • Not tested: rates on more than two tree levels in a real network (only in the random test), very high port rates (> 100 Gbps), malformed attribute strings beyond the hand checks (they abort, so no unit test), the new warning (v1model always fills the length).

Notes for reviewers

🤖 Generated with Claude Code

Mingyumaz and others added 3 commits December 2, 2026 14:54
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>
@Mingyumaz
Mingyumaz merged commit df0a9d6 into main Dec 2, 2026
2 checks passed
@Mingyumaz
Mingyumaz deleted the feat/pifo-token-bucket 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