Dezio/deploy/shape-upload.sh
astar76 efe6d7d9f5 Upload shaping stays enabled across reboots
The service's stop step used `off`, which also disabled the service, so
the first shutdown turned shaping off for good.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 00:23:02 -06:00

69 lines
2.5 KiB
Shell
Executable file

#!/bin/bash
# Keeps voice smooth on the Chromebook's slow upload (about 0.9 Mbit/s).
# Without this, one picture upload fills the router's queue and voice packets wait behind it: latency to Discord went
# from 39 ms to 450-785 ms while a 664 KB GIF uploaded. This caps internet-bound traffic just under the line's speed so
# the queue stays here, where fq_codel lets small flows like voice go first. Traffic to the home network isn't capped.
#
# shape-upload.sh turn it on
# shape-upload.sh off turn it off
# shape-upload.sh install turn it on now and at every boot
#
# It turns itself off again after 2 minutes unless `keep` is the second word, so a mistake can't lock anyone out.
set -eu
DEV=wlan0
RATE=800kbit
off() { tc qdisc del dev "$DEV" root 2>/dev/null || true; }
on() {
off
tc qdisc add dev "$DEV" root handle 1: htb default 20
tc class add dev "$DEV" parent 1: classid 1:1 htb rate 100mbit
tc class add dev "$DEV" parent 1:1 classid 1:10 htb rate 90mbit ceil 100mbit
tc class add dev "$DEV" parent 1:1 classid 1:20 htb rate "$RATE" ceil "$RATE"
tc qdisc add dev "$DEV" parent 1:10 fq_codel
tc qdisc add dev "$DEV" parent 1:20 fq_codel
tc filter add dev "$DEV" parent 1: protocol ip prio 1 u32 match ip dst 192.168.0.0/16 flowid 1:10
tc filter add dev "$DEV" parent 1: protocol ipv6 prio 2 u32 match ip6 dst fe80::/10 flowid 1:10
}
case "${1:-on}" in
stop)
off # what the service runs at shutdown: it must stay enabled for the next boot
;;
off)
off
systemctl disable --now dezio-shape.service 2>/dev/null || true
echo "Upload shaping is off"
;;
install)
install -m 755 "$0" /usr/local/bin/dezio-shape
cat > /etc/systemd/system/dezio-shape.service <<UNIT
[Unit]
Description=Keep voice smooth on the slow upload
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/dezio-shape on keep
ExecStop=/usr/local/bin/dezio-shape stop
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable --now dezio-shape.service
echo "Upload shaping is on, now and at every boot"
;;
*)
on
if [ "${2:-}" = keep ]; then
echo "Upload shaping is on"
else
nohup sh -c "sleep 120; tc qdisc del dev $DEV root" >/dev/null 2>&1 &
echo "Upload shaping is on for 2 minutes as a test"
fi
;;
esac