nixos-config/machines/shinobu/services/router/tc.nix
Simon Bruder afc9013506
shinobu/router: Implement QoS using HTB
This is an initial implementation and probably still needs tuning.
2023-10-07 22:49:26 +02:00

50 lines
1.8 KiB
Nix
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# I measured the link capacity with iperf3:
# The download is pretty exactly 7.5MiB/s ≈ 62.9Mbit/s
# The upstream is more complicated.
# It initially bursts around 50% higher than the sustained speed
# and then falls down to 569KiB/s ≈ 4.66Mbit/s
# However, abound every 2 to 3 seconds, it drops to 380KiB/s ≈ 3.11Mbit/s.
# It does so pretty consistently and always at exactly that rate.
# I averaged a longer iperf3 run to around 509Kbit/s ≈ 4.17Mbit/s (excluding the initial burst).
{ lib, pkgs, utils, ... }:
let
cfg = ((import ./common.nix).tc);
mkClass =
{ minor
, rate
, ceil ? cfg.rate
, burst ? "15k"
, qdiscArgs ? [ "fq_codel" ]
, prio
}: ''
tc class add dev ${cfg.interface} parent ${toString cfg.major}:1 classid ${toString cfg.major}:${toString minor} htb rate ${rate} ceil ${ceil} burst ${burst} prio ${toString prio}
tc qdisc add dev ${cfg.interface} parent ${toString cfg.major}:${toString minor} handle ${toString minor}:1 ${lib.escapeShellArgs qdiscArgs}
'';
in
{
systemd.services.traffic-control = {
after = [ "sys-subsystem-net-devices-${utils.escapeSystemdPath cfg.interface}.device" ];
bindsTo = [ "sys-subsystem-net-devices-${utils.escapeSystemdPath cfg.interface}.device" ];
wantedBy = [ "network-online.target" ];
path = with pkgs; [ iproute2 ];
script = ''
set -euo pipefail
# deleting might fail
tc qdisc del root dev ${cfg.interface} || true
tc qdisc add dev ${cfg.interface} root handle ${toString cfg.major}:0 htb default 2
tc class add dev ${cfg.interface} parent ${toString cfg.major}:0 classid ${toString cfg.major}:1 htb rate ${toString cfg.rate} burst 15k
${lib.concatMapStrings mkClass cfg.classes}
'';
serviceConfig = {
Type = "oneshot";
};
};
}