]>
Commit | Line | Data |
---|---|---|
ba5841b5 SS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ |
2 | * Copyright © 2019 VMware, Inc. */ | |
3 | ||
4 | #include <linux/pkt_sched.h> | |
5 | #include <math.h> | |
6 | ||
7 | #include "alloc-util.h" | |
8 | #include "conf-parser.h" | |
9 | #include "netem.h" | |
10 | #include "netlink-util.h" | |
11 | #include "networkd-manager.h" | |
12 | #include "parse-util.h" | |
13 | #include "qdisc.h" | |
14 | #include "string-util.h" | |
c03ef420 | 15 | #include "strv.h" |
dcfc23ae | 16 | #include "tc-util.h" |
ba5841b5 | 17 | |
60ed2dcf | 18 | static int token_bucket_filter_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) { |
dcfc23ae | 19 | uint32_t rtab[256], ptab[256]; |
ba5841b5 | 20 | struct tc_tbf_qopt opt = {}; |
60ed2dcf | 21 | TokenBucketFilter *tbf; |
ba5841b5 SS |
22 | int r; |
23 | ||
24 | assert(link); | |
e8c17dc0 | 25 | assert(qdisc); |
ba5841b5 SS |
26 | assert(req); |
27 | ||
e8c17dc0 YW |
28 | tbf = TBF(qdisc); |
29 | ||
ba5841b5 | 30 | opt.rate.rate = tbf->rate >= (1ULL << 32) ? ~0U : tbf->rate; |
dcfc23ae YW |
31 | opt.peakrate.rate = tbf->peak_rate >= (1ULL << 32) ? ~0U : tbf->peak_rate; |
32 | ||
33 | if (tbf->limit > 0) | |
34 | opt.limit = tbf->limit; | |
35 | else { | |
36 | double lim, lim2; | |
37 | ||
38 | lim = tbf->rate * (double) tbf->latency / USEC_PER_SEC + tbf->burst; | |
39 | if (tbf->peak_rate > 0) { | |
40 | lim2 = tbf->peak_rate * (double) tbf->latency / USEC_PER_SEC + tbf->mtu; | |
41 | lim = MIN(lim, lim2); | |
42 | } | |
43 | opt.limit = lim; | |
44 | } | |
45 | ||
46 | opt.rate.mpu = tbf->mpu; | |
47 | ||
48 | r = tc_fill_ratespec_and_table(&opt.rate, rtab, tbf->mtu); | |
49 | if (r < 0) | |
50 | return log_link_error_errno(link, r, "Failed to calculate ratespec: %m"); | |
51 | ||
52 | r = tc_transmit_time(opt.rate.rate, tbf->burst, &opt.buffer); | |
53 | if (r < 0) | |
54 | return log_link_error_errno(link, r, "Failed to calculate buffer size: %m"); | |
55 | ||
56 | if (opt.peakrate.rate > 0) { | |
57 | opt.peakrate.mpu = tbf->mpu; | |
58 | ||
59 | r = tc_fill_ratespec_and_table(&opt.peakrate, ptab, tbf->mtu); | |
60 | if (r < 0) | |
61 | return log_link_error_errno(link, r, "Failed to calculate ratespec: %m"); | |
62 | ||
63 | r = tc_transmit_time(opt.peakrate.rate, tbf->mtu, &opt.mtu); | |
64 | if (r < 0) | |
65 | return log_link_error_errno(link, r, "Failed to calculate mtu size: %m"); | |
66 | } | |
ba5841b5 | 67 | |
92c7593f | 68 | r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "tbf"); |
ba5841b5 SS |
69 | if (r < 0) |
70 | return log_link_error_errno(link, r, "Could not open container TCA_OPTIONS: %m"); | |
71 | ||
72 | r = sd_netlink_message_append_data(req, TCA_TBF_PARMS, &opt, sizeof(struct tc_tbf_qopt)); | |
73 | if (r < 0) | |
74 | return log_link_error_errno(link, r, "Could not append TCA_TBF_PARMS attribute: %m"); | |
75 | ||
76 | r = sd_netlink_message_append_data(req, TCA_TBF_BURST, &tbf->burst, sizeof(tbf->burst)); | |
77 | if (r < 0) | |
78 | return log_link_error_errno(link, r, "Could not append TCA_TBF_BURST attribute: %m"); | |
79 | ||
80 | if (tbf->rate >= (1ULL << 32)) { | |
92c7593f | 81 | r = sd_netlink_message_append_u64(req, TCA_TBF_RATE64, tbf->rate); |
ba5841b5 SS |
82 | if (r < 0) |
83 | return log_link_error_errno(link, r, "Could not append TCA_TBF_RATE64 attribute: %m"); | |
84 | } | |
85 | ||
dcfc23ae YW |
86 | r = sd_netlink_message_append_data(req, TCA_TBF_RTAB, rtab, sizeof(rtab)); |
87 | if (r < 0) | |
88 | return log_link_error_errno(link, r, "Could not append TCA_TBF_RTAB attribute: %m"); | |
89 | ||
90 | if (opt.peakrate.rate > 0) { | |
91 | if (tbf->peak_rate >= (1ULL << 32)) { | |
92c7593f | 92 | r = sd_netlink_message_append_u64(req, TCA_TBF_PRATE64, tbf->peak_rate); |
dcfc23ae YW |
93 | if (r < 0) |
94 | return log_link_error_errno(link, r, "Could not append TCA_TBF_PRATE64 attribute: %m"); | |
95 | } | |
96 | ||
92c7593f | 97 | r = sd_netlink_message_append_u32(req, TCA_TBF_PBURST, tbf->mtu); |
dcfc23ae YW |
98 | if (r < 0) |
99 | return log_link_error_errno(link, r, "Could not append TCA_TBF_PBURST attribute: %m"); | |
100 | ||
101 | r = sd_netlink_message_append_data(req, TCA_TBF_PTAB, ptab, sizeof(ptab)); | |
102 | if (r < 0) | |
103 | return log_link_error_errno(link, r, "Could not append TCA_TBF_PTAB attribute: %m"); | |
104 | } | |
105 | ||
ba5841b5 SS |
106 | r = sd_netlink_message_close_container(req); |
107 | if (r < 0) | |
108 | return log_link_error_errno(link, r, "Could not close container TCA_OPTIONS: %m"); | |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
60ed2dcf | 113 | int config_parse_token_bucket_filter_size( |
ba5841b5 SS |
114 | const char *unit, |
115 | const char *filename, | |
116 | unsigned line, | |
117 | const char *section, | |
118 | unsigned section_line, | |
119 | const char *lvalue, | |
120 | int ltype, | |
121 | const char *rvalue, | |
122 | void *data, | |
123 | void *userdata) { | |
124 | ||
edc54f2f | 125 | _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL; |
ba5841b5 | 126 | Network *network = data; |
60ed2dcf | 127 | TokenBucketFilter *tbf; |
ba5841b5 SS |
128 | uint64_t k; |
129 | int r; | |
130 | ||
131 | assert(filename); | |
132 | assert(lvalue); | |
133 | assert(rvalue); | |
134 | assert(data); | |
135 | ||
e8c17dc0 YW |
136 | r = qdisc_new_static(QDISC_KIND_TBF, network, filename, section_line, &qdisc); |
137 | if (r == -ENOMEM) | |
138 | return log_oom(); | |
d96edb2c YW |
139 | if (r < 0) { |
140 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
141 | "More than one kind of queueing discipline, ignoring assignment: %m"); | |
142 | return 0; | |
143 | } | |
e8c17dc0 YW |
144 | |
145 | tbf = TBF(qdisc); | |
ba5841b5 SS |
146 | |
147 | if (isempty(rvalue)) { | |
c03ef420 | 148 | if (STR_IN_SET(lvalue, "BurstBytes", "Burst")) |
e8c17dc0 | 149 | tbf->burst = 0; |
c03ef420 | 150 | else if (STR_IN_SET(lvalue, "LimitBytes", "LimitSize")) |
e8c17dc0 | 151 | tbf->limit = 0; |
18de0969 | 152 | else if (streq(lvalue, "MTUBytes")) |
e8c17dc0 | 153 | tbf->mtu = 0; |
18de0969 | 154 | else if (streq(lvalue, "MPUBytes")) |
e8c17dc0 | 155 | tbf->mpu = 0; |
c03ef420 YW |
156 | else |
157 | assert_not_reached("unknown lvalue"); | |
ba5841b5 SS |
158 | |
159 | qdisc = NULL; | |
160 | return 0; | |
161 | } | |
162 | ||
c03ef420 | 163 | r = parse_size(rvalue, 1024, &k); |
ba5841b5 | 164 | if (r < 0) { |
d96edb2c | 165 | log_syntax(unit, LOG_WARNING, filename, line, r, |
ba5841b5 SS |
166 | "Failed to parse '%s=', ignoring assignment: %s", |
167 | lvalue, rvalue); | |
168 | return 0; | |
169 | } | |
170 | ||
c03ef420 | 171 | if (STR_IN_SET(lvalue, "BurstBytes", "Burst")) |
e8c17dc0 | 172 | tbf->burst = k; |
c03ef420 | 173 | else if (STR_IN_SET(lvalue, "LimitBytes", "LimitSize")) |
e8c17dc0 | 174 | tbf->limit = k; |
18de0969 | 175 | else if (streq(lvalue, "MPUBytes")) |
e8c17dc0 | 176 | tbf->mpu = k; |
18de0969 | 177 | else if (streq(lvalue, "MTUBytes")) |
e8c17dc0 | 178 | tbf->mtu = k; |
c03ef420 YW |
179 | else |
180 | assert_not_reached("unknown lvalue"); | |
181 | ||
182 | qdisc = NULL; | |
183 | ||
184 | return 0; | |
185 | } | |
186 | ||
187 | int config_parse_token_bucket_filter_rate( | |
188 | const char *unit, | |
189 | const char *filename, | |
190 | unsigned line, | |
191 | const char *section, | |
192 | unsigned section_line, | |
193 | const char *lvalue, | |
194 | int ltype, | |
195 | const char *rvalue, | |
196 | void *data, | |
197 | void *userdata) { | |
198 | ||
199 | _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL; | |
200 | Network *network = data; | |
201 | TokenBucketFilter *tbf; | |
202 | uint64_t k, *p; | |
203 | int r; | |
204 | ||
205 | assert(filename); | |
206 | assert(lvalue); | |
207 | assert(rvalue); | |
208 | assert(data); | |
209 | ||
210 | r = qdisc_new_static(QDISC_KIND_TBF, network, filename, section_line, &qdisc); | |
211 | if (r == -ENOMEM) | |
212 | return log_oom(); | |
d96edb2c YW |
213 | if (r < 0) { |
214 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
215 | "More than one kind of queueing discipline, ignoring assignment: %m"); | |
216 | return 0; | |
217 | } | |
c03ef420 YW |
218 | |
219 | tbf = TBF(qdisc); | |
220 | if (streq(lvalue, "Rate")) | |
221 | p = &tbf->rate; | |
18de0969 | 222 | else if (streq(lvalue, "PeakRate")) |
c03ef420 YW |
223 | p = &tbf->peak_rate; |
224 | else | |
225 | assert_not_reached("unknown lvalue"); | |
226 | ||
227 | if (isempty(rvalue)) { | |
228 | *p = 0; | |
229 | ||
230 | qdisc = NULL; | |
231 | return 0; | |
232 | } | |
233 | ||
234 | r = parse_size(rvalue, 1000, &k); | |
235 | if (r < 0) { | |
d96edb2c | 236 | log_syntax(unit, LOG_WARNING, filename, line, r, |
c03ef420 YW |
237 | "Failed to parse '%s=', ignoring assignment: %s", |
238 | lvalue, rvalue); | |
239 | return 0; | |
240 | } | |
241 | ||
242 | *p = k / 8; | |
ba5841b5 | 243 | |
ba5841b5 SS |
244 | qdisc = NULL; |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
60ed2dcf | 249 | int config_parse_token_bucket_filter_latency( |
ba5841b5 SS |
250 | const char *unit, |
251 | const char *filename, | |
252 | unsigned line, | |
253 | const char *section, | |
254 | unsigned section_line, | |
255 | const char *lvalue, | |
256 | int ltype, | |
257 | const char *rvalue, | |
258 | void *data, | |
259 | void *userdata) { | |
260 | ||
edc54f2f | 261 | _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL; |
ba5841b5 | 262 | Network *network = data; |
60ed2dcf | 263 | TokenBucketFilter *tbf; |
ba5841b5 SS |
264 | usec_t u; |
265 | int r; | |
266 | ||
267 | assert(filename); | |
268 | assert(lvalue); | |
269 | assert(rvalue); | |
270 | assert(data); | |
271 | ||
e8c17dc0 YW |
272 | r = qdisc_new_static(QDISC_KIND_TBF, network, filename, section_line, &qdisc); |
273 | if (r == -ENOMEM) | |
274 | return log_oom(); | |
d96edb2c YW |
275 | if (r < 0) { |
276 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
277 | "More than one kind of queueing discipline, ignoring assignment: %m"); | |
278 | return 0; | |
279 | } | |
e8c17dc0 YW |
280 | |
281 | tbf = TBF(qdisc); | |
ba5841b5 SS |
282 | |
283 | if (isempty(rvalue)) { | |
e8c17dc0 | 284 | tbf->latency = 0; |
ba5841b5 SS |
285 | |
286 | qdisc = NULL; | |
287 | return 0; | |
288 | } | |
289 | ||
290 | r = parse_sec(rvalue, &u); | |
291 | if (r < 0) { | |
d96edb2c | 292 | log_syntax(unit, LOG_WARNING, filename, line, r, |
ba5841b5 SS |
293 | "Failed to parse '%s=', ignoring assignment: %s", |
294 | lvalue, rvalue); | |
295 | return 0; | |
296 | } | |
297 | ||
e8c17dc0 | 298 | tbf->latency = u; |
ba5841b5 | 299 | |
ba5841b5 SS |
300 | qdisc = NULL; |
301 | ||
302 | return 0; | |
303 | } | |
dcfc23ae | 304 | |
60ed2dcf ZJS |
305 | static int token_bucket_filter_verify(QDisc *qdisc) { |
306 | TokenBucketFilter *tbf = TBF(qdisc); | |
e8c17dc0 | 307 | |
dcfc23ae YW |
308 | if (tbf->limit > 0 && tbf->latency > 0) |
309 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
18de0969 | 310 | "%s: Specifying both LimitSize= and LatencySec= is not allowed. " |
60ed2dcf | 311 | "Ignoring [TokenBucketFilter] section from line %u.", |
e8c17dc0 | 312 | qdisc->section->filename, qdisc->section->line); |
dcfc23ae YW |
313 | |
314 | if (tbf->limit == 0 && tbf->latency == 0) | |
315 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
18de0969 | 316 | "%s: Either LimitSize= or LatencySec= is required. " |
60ed2dcf | 317 | "Ignoring [TokenBucketFilter] section from line %u.", |
e8c17dc0 | 318 | qdisc->section->filename, qdisc->section->line); |
dcfc23ae YW |
319 | |
320 | if (tbf->rate == 0) | |
321 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
18de0969 | 322 | "%s: Rate= is mandatory. " |
60ed2dcf | 323 | "Ignoring [TokenBucketFilter] section from line %u.", |
e8c17dc0 | 324 | qdisc->section->filename, qdisc->section->line); |
dcfc23ae YW |
325 | |
326 | if (tbf->burst == 0) | |
327 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
18de0969 | 328 | "%s: Burst= is mandatory. " |
60ed2dcf | 329 | "Ignoring [TokenBucketFilter] section from line %u.", |
e8c17dc0 | 330 | qdisc->section->filename, qdisc->section->line); |
dcfc23ae YW |
331 | |
332 | if (tbf->peak_rate > 0 && tbf->mtu == 0) | |
333 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
18de0969 | 334 | "%s: MTUBytes= is mandatory when PeakRate= is specified. " |
60ed2dcf | 335 | "Ignoring [TokenBucketFilter] section from line %u.", |
e8c17dc0 | 336 | qdisc->section->filename, qdisc->section->line); |
dcfc23ae YW |
337 | |
338 | return 0; | |
339 | } | |
e8c17dc0 YW |
340 | |
341 | const QDiscVTable tbf_vtable = { | |
60ed2dcf | 342 | .object_size = sizeof(TokenBucketFilter), |
e8c17dc0 | 343 | .tca_kind = "tbf", |
60ed2dcf ZJS |
344 | .fill_message = token_bucket_filter_fill_message, |
345 | .verify = token_bucket_filter_verify | |
e8c17dc0 | 346 | }; |