-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtc_cake.nix
184 lines (149 loc) · 5.2 KB
/
tc_cake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.networking.tc_cake;
generateUnit = name: opts: nameValuePair "tc_cake-${name}" {
description = "AQM (Cake) rules for ${name}.";
bindsTo = [ "sys-subsystem-net-devices-${name}.device" ];
after = [ "sys-subsystem-net-devices-${name}.device" "network-pre.target" ];
requires = [ "sys-subsystem-net-devices-${name}.device" ];
before = [ "network.target" ];
wantedBy = [ "sys-subsystem-net-devices-${name}.device" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = pkgs.writeTextFile {
name = "tc-${name}-start";
executable = true;
text = ''
#! ${pkgs.runtimeShell} -e
# Offloading.
${optionalString opts.disableOffload ''
${pkgs.ethtool}/bin/ethtool -K ${name} gro off gso off tso off
''}
# Egress control.
${optionalString (opts.shapeEgress.bandwidth != null) ''
${pkgs.iproute}/bin/tc qdisc add dev ${name} root cake bandwidth ${opts.shapeEgress.bandwidth} ${opts.shapeEgress.extraArgs}
''}
# Ingress control.
${optionalString (opts.shapeIngress.bandwidth != null) ''
${pkgs.iproute}/bin/tc qdisc add dev ${name} handle ffff: ingress
${pkgs.iproute}/bin/tc filter add dev ${name} parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ${opts.shapeIngress.ifb}
${pkgs.iproute}/bin/tc qdisc add dev ${opts.shapeIngress.ifb} root cake bandwidth ${opts.shapeIngress.bandwidth} ingress
''}
'';
};
ExecStop = pkgs.writeTextFile {
name = "tc-${name}-stop";
executable = true;
text = ''
#! ${pkgs.runtimeShell} -e
# Ingress control.
${optionalString (opts.shapeIngress.bandwidth != null) ''
${pkgs.iproute}/bin/tc qdisc del dev ${opts.shapeIngress.ifb} root
${pkgs.iproute}/bin/tc qdisc del dev ${name} parent ffff:
''}
# Egress control.
${optionalString (opts.shapeEgress.bandwidth != null) ''
${pkgs.iproute}/bin/tc qdisc del dev ${name} root
''}
# Offloading.
${optionalString opts.disableOffload ''
${pkgs.ethtool}/bin/ethtool -K ${name} gro on gso on tso on
''}
'';
};
};
restartIfChanged = true;
};
in
{
###### interface
options = {
networking.tc_cake = mkOption {
default = {};
type = types.attrsOf (types.submodule {
options = {
disableOffload = mkOption {
default = false;
type = types.bool;
description = ''
Enabling this will ensure all hardware offloading (to the NIC) is disabled.
'';
};
shapeEgress = mkOption {
type = (types.submodule {
options = {
bandwidth = mkOption {
default = null;
type = types.nullOr types.str;
example = "16mbit";
description = ''
A string describing the available outgoing bandwidth, compatible with `tc`.
'';
};
extraArgs = mkOption {
default = "";
type = types.str;
example = "nat overhead 18 mpu 64 noatm ack-filter";
description = ''
Additional arguments/flags for the cake qdisc creation.
'';
};
};
});
default = {
bandwidth = null;
extraArgs = "";
};
description = ''
Submodule describing how to shape egress traffic.
'';
};
shapeIngress = mkOption {
type = (types.submodule {
options = {
bandwidth = mkOption {
default = null;
type = types.nullOr types.str;
example = "75mbit";
description = ''
A string describing the available incoming bandwidth, compatible with `tc`.
'';
};
ifb = mkOption {
default = "ifb0";
type = types.str;
example = "ifb0";
description = ''
The IFB device to use during ingress shaping. Must be unique to this interface.
'';
};
};
});
default = {
bandwidth = null;
ifb = "ifb0";
};
description = ''
Submodule describing how to shape ingress traffic.
'';
};
};
});
description = ''
The list of traffic control commands, one entry per interface.
'';
};
};
###### Implementation
config = mkIf (cfg != {}) {
# systemd.services = mapAttrs generateUnit cfg;
systemd.services = listToAttrs (mapAttrsToList generateUnit cfg);
boot.kernelModules = [
"ifb"
"sch_cake"
"mirred"
];
};
}