cake-ingress: start says active only when the shaper is in place

Every step ran under set -u only, so a refused sudo, a kernel without sch_cake or a
RATE_DOWN tc cannot read went on to "active" and exit 0. A failed step now names
itself, takes down what was set, and exits 1. Found in review, 2026-09-24 (T15).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Hasan BasbunarandClaude Opus 5.5 committed 2026-09-24 19:12:40 +02:00
1 parent fe012c7458
commit d9bc02f0fb
2 files changed
+79 -6

No files matched your search

+14 -6
View File
@@ -46,6 +46,14 @@ teardown() {
$TC qdisc del dev "$IFB" root 2>/dev/null || true
}
# A step that fails ends the start, with what was set taken down again: without this the
# script went on and said "active" over a shaper that was never installed (a refused sudo,
# a kernel without sch_cake, a RATE_DOWN tc cannot read), and exited 0 (found in review,
# 2026-09-24).
must() {
"$@" || { echo "cake-ingress: failed: $*" >&2; teardown; exit 1; }
}
start() {
[ -e "/sys/class/net/$IF" ] || { echo "cake-ingress: interface $IF does not exist" >&2; exit 1; }
if [ -z "$RATE_DOWN" ]; then
@@ -53,17 +61,17 @@ start() {
echo " examples: 500 Mb/s link -> RATE_DOWN=475Mbit ; 1 Gb/s -> 950Mbit ; 2.5 Gb/s -> 2350Mbit" >&2
exit 1
fi
$MODPROBE ifb numifbs=1
$MODPROBE sch_cake
must $MODPROBE ifb numifbs=1
must $MODPROBE sch_cake
$IP link add "$IFB" type ifb 2>/dev/null || true
$IP link set "$IFB" up
must $IP link set "$IFB" up
teardown
# all ingress of $IF is redirected to $IFB, where CAKE can shape it
$TC qdisc add dev "$IF" clsact
$TC filter add dev "$IF" ingress protocol all matchall \
must $TC qdisc add dev "$IF" clsact
must $TC filter add dev "$IF" ingress protocol all matchall \
action mirred egress redirect dev "$IFB"
# shellcheck disable=SC2086
$TC qdisc add dev "$IFB" root cake bandwidth "$RATE_DOWN" $CAKE_DOWN
must $TC qdisc add dev "$IFB" root cake bandwidth "$RATE_DOWN" $CAKE_DOWN
echo "cake-ingress: active $IF -> $IFB at $RATE_DOWN ($CAKE_DOWN)"
}
+65
View File
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
"""cake-ingress start says active only when the shaper is in place.
It ran every step under `set -u` only, so a refused sudo, a kernel without sch_cake or a
RATE_DOWN tc cannot read went on to "cake-ingress: active" and exit 0, and the unit read
as started (found in review, 2026-09-24). As a user the script calls its tools through
`sudo -n`, so a fake sudo first on PATH plays them here and nothing reaches the system:
every call is logged, none is executed.
"""
import os
import pathlib
import shutil
import subprocess
import tempfile
import unittest
SCRIPT = pathlib.Path(__file__).resolve().parents[1] / "extras" / "cake-ingress" / "cake-ingress"
# Logs its argv; fails (exit 2) on a call whose text contains $FAKE_FAIL.
FAKE_SUDO = """#!/bin/sh
echo "$*" >> "$FAKE_LOG"
case "$*" in *"$FAKE_FAIL"*) [ -n "$FAKE_FAIL" ] && { echo "RTNETLINK answers: Operation not supported" >&2; exit 2; } ;; esac
exit 0
"""
@unittest.skipIf(os.geteuid() == 0, "as root the script calls the real tools, not sudo")
class TheStart(unittest.TestCase):
def setUp(self):
self.t = pathlib.Path(tempfile.mkdtemp(prefix="cake-"))
self.addCleanup(shutil.rmtree, self.t, ignore_errors=True)
(self.t / "sudo").write_text(FAKE_SUDO)
(self.t / "sudo").chmod(0o755)
self.log = self.t / "calls.log"
def start(self, fail=""):
env = {**os.environ, "PATH": f"{self.t}:{os.environ['PATH']}", "FAKE_LOG": str(self.log),
"FAKE_FAIL": fail, "IF": "lo", "IFB": "ifbtest9", "RATE_DOWN": "950Mbit"}
r = subprocess.run(["bash", str(SCRIPT), "start"], env=env, capture_output=True, text=True, timeout=30)
calls = self.log.read_text().splitlines() if self.log.exists() else []
self.assertTrue(calls and all(c.startswith("-n /usr/sbin/") for c in calls), calls)
return r.returncode, r.stdout + r.stderr, calls
def test_every_step_done_is_active(self):
rc, out, _ = self.start()
self.assertEqual(rc, 0, out)
self.assertIn("active", out)
def test_a_shaper_that_was_not_installed_is_not_active(self):
rc, out, calls = self.start(fail="root cake bandwidth")
self.assertEqual(rc, 1, out)
self.assertNotIn("active", out)
self.assertIn("failed", out)
# what had been set is taken down again
self.assertTrue(any("qdisc del dev lo clsact" in c for c in calls[calls.index(next(
c for c in calls if "root cake bandwidth" in c)):]), calls)
def test_a_missing_module_stops_before_anything_is_redirected(self):
rc, out, calls = self.start(fail="sch_cake")
self.assertEqual(rc, 1, out)
self.assertFalse(any("mirred" in c for c in calls), calls)
if __name__ == "__main__":
unittest.main()