nixos-config/machines/shinobu/services/router/tc.nix

50 lines
1.9 KiB
Nix
Raw Normal View History

# 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 = ((pkgs.callPackage ./common.nix { }).tc);
mkClass =
{ minor
, rate
, ceil ? cfg.rate
, burst ? "15k"
, qdiscArgs ? [ "fq_codel" ]
, prio
}: ''
tc class add dev ${cfg.interface} parent ${lib.toHexString cfg.major}:1 classid ${lib.toHexString cfg.major}:${lib.toHexString minor} htb rate ${rate} ceil ${ceil} burst ${burst} prio ${toString prio}
tc qdisc add dev ${cfg.interface} parent ${lib.toHexString cfg.major}:${lib.toHexString minor} handle ${lib.toHexString 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 ${lib.toHexString cfg.major}:0 htb default 2
tc class add dev ${cfg.interface} parent ${lib.toHexString cfg.major}:0 classid ${lib.toHexString cfg.major}:1 htb rate ${toString cfg.rate} burst 15k
${lib.concatMapStrings mkClass cfg.classes}
'';
serviceConfig = {
Type = "oneshot";
};
};
}