summaryrefslogtreecommitdiffstats
path: root/src/network/tc/htb.c
blob: 14fb1f92e34fac50658a76a36514040a28353d39 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <linux/pkt_sched.h>

#include "alloc-util.h"
#include "conf-parser.h"
#include "netlink-util.h"
#include "parse-util.h"
#include "qdisc.h"
#include "htb.h"
#include "string-util.h"
#include "tc-util.h"

#define HTB_DEFAULT_RATE_TO_QUANTUM  10
#define HTB_DEFAULT_MTU              1600  /* Ethernet packet length */

static int hierarchy_token_bucket_fill_message(Link *link, QDisc *qdisc, sd_netlink_message *req) {
        HierarchyTokenBucket *htb;
        struct tc_htb_glob opt = {
                .version = 3,
        };
        int r;

        assert(link);
        assert(qdisc);
        assert(req);

        htb = HTB(qdisc);

        opt.rate2quantum = htb->rate_to_quantum;
        opt.defcls = htb->default_class;

        r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "htb");
        if (r < 0)
                return log_link_error_errno(link, r, "Could not open container TCA_OPTIONS: %m");

        r = sd_netlink_message_append_data(req, TCA_HTB_INIT, &opt, sizeof(opt));
        if (r < 0)
                return log_link_error_errno(link, r, "Could not append TCA_HTB_INIT attribute: %m");

        r = sd_netlink_message_close_container(req);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not close container TCA_OPTIONS: %m");
        return 0;
}

int config_parse_hierarchy_token_bucket_default_class(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL;
        HierarchyTokenBucket *htb;
        Network *network = data;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        r = qdisc_new_static(QDISC_KIND_HTB, network, filename, section_line, &qdisc);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "More than one kind of queueing discipline, ignoring assignment: %m");
                return 0;
        }

        htb = HTB(qdisc);

        if (isempty(rvalue)) {
                htb->default_class = 0;

                TAKE_PTR(qdisc);
                return 0;
        }

        r = safe_atou32_full(rvalue, 16, &htb->default_class);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse '%s=', ignoring assignment: %s",
                           lvalue, rvalue);
                return 0;
        }

        TAKE_PTR(qdisc);

        return 0;
}

int config_parse_hierarchy_token_bucket_u32(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        _cleanup_(qdisc_free_or_set_invalidp) QDisc *qdisc = NULL;
        HierarchyTokenBucket *htb;
        Network *network = data;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        r = qdisc_new_static(QDISC_KIND_HTB, network, filename, section_line, &qdisc);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "More than one kind of queueing discipline, ignoring assignment: %m");
                return 0;
        }

        htb = HTB(qdisc);

        if (isempty(rvalue)) {
                htb->rate_to_quantum = HTB_DEFAULT_RATE_TO_QUANTUM;

                TAKE_PTR(qdisc);
                return 0;
        }

        r = safe_atou32(rvalue, &htb->rate_to_quantum);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse '%s=', ignoring assignment: %s",
                           lvalue, rvalue);
                return 0;
        }

        TAKE_PTR(qdisc);

        return 0;
}

static int hierarchy_token_bucket_init(QDisc *qdisc) {
        HierarchyTokenBucket *htb;

        assert(qdisc);

        htb = HTB(qdisc);

        htb->rate_to_quantum = HTB_DEFAULT_RATE_TO_QUANTUM;

        return 0;
}

const QDiscVTable htb_vtable = {
        .object_size = sizeof(HierarchyTokenBucket),
        .tca_kind = "htb",
        .fill_message = hierarchy_token_bucket_fill_message,
        .init = hierarchy_token_bucket_init,
};

static int hierarchy_token_bucket_class_fill_message(Link *link, TClass *tclass, sd_netlink_message *req) {
        HierarchyTokenBucketClass *htb;
        struct tc_htb_opt opt = {};
        uint32_t rtab[256], ctab[256];
        int r;

        assert(link);
        assert(tclass);
        assert(req);

        htb = TCLASS_TO_HTB(tclass);

        opt.prio = htb->priority;
        opt.quantum = htb->quantum;
        opt.rate.rate = (htb->rate >= (1ULL << 32)) ? ~0U : htb->rate;
        opt.ceil.rate = (htb->ceil_rate >= (1ULL << 32)) ? ~0U : htb->ceil_rate;
        opt.rate.overhead = htb->overhead;
        opt.ceil.overhead = htb->overhead;

        r = tc_transmit_time(htb->rate, htb->buffer, &opt.buffer);
        if (r < 0)
                return log_link_error_errno(link, r, "Failed to calculate buffer size: %m");

        r = tc_transmit_time(htb->ceil_rate, htb->ceil_buffer, &opt.cbuffer);
        if (r < 0)
                return log_link_error_errno(link, r, "Failed to calculate ceil buffer size: %m");

        r = tc_fill_ratespec_and_table(&opt.rate, rtab, htb->mtu);
        if (r < 0)
                return log_link_error_errno(link, r, "Failed to calculate rate table: %m");

        r = tc_fill_ratespec_and_table(&opt.ceil, ctab, htb->mtu);
        if (r < 0)
                return log_link_error_errno(link, r, "Failed to calculate ceil rate table: %m");

        r = sd_netlink_message_open_container_union(req, TCA_OPTIONS, "htb");
        if (r < 0)
                return log_link_error_errno(link, r, "Could not open container TCA_OPTIONS: %m");

        r = sd_netlink_message_append_data(req, TCA_HTB_PARMS, &opt, sizeof(opt));
        if (r < 0)
                return log_link_error_errno(link, r, "Could not append TCA_HTB_PARMS attribute: %m");

        if (htb->rate >= (1ULL << 32)) {
                r = sd_netlink_message_append_u64(req, TCA_HTB_RATE64, htb->rate);
                if (r < 0)
                        return log_link_error_errno(link, r, "Could not append TCA_HTB_RATE64 attribute: %m");
        }

        if (htb->ceil_rate >= (1ULL << 32)) {
                r = sd_netlink_message_append_u64(req, TCA_HTB_CEIL64, htb->ceil_rate);
                if (r < 0)
                        return log_link_error_errno(link, r, "Could not append TCA_HTB_CEIL64 attribute: %m");
        }

        r = sd_netlink_message_append_data(req, TCA_HTB_RTAB, rtab, sizeof(rtab));
        if (r < 0)
                return log_link_error_errno(link, r, "Could not append TCA_HTB_RTAB attribute: %m");

        r = sd_netlink_message_append_data(req, TCA_HTB_CTAB, ctab, sizeof(ctab));
        if (r < 0)
                return log_link_error_errno(link, r, "Could not append TCA_HTB_CTAB attribute: %m");

        r = sd_netlink_message_close_container(req);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not close container TCA_OPTIONS: %m");
        return 0;
}

int config_parse_hierarchy_token_bucket_class_u32(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        _cleanup_(tclass_free_or_set_invalidp) TClass *tclass = NULL;
        HierarchyTokenBucketClass *htb;
        Network *network = data;
        uint32_t v;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        r = tclass_new_static(TCLASS_KIND_HTB, network, filename, section_line, &tclass);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to create traffic control class, ignoring assignment: %m");
                return 0;
        }

        htb = TCLASS_TO_HTB(tclass);

        if (isempty(rvalue)) {
                htb->priority = 0;
                tclass = NULL;
                return 0;
        }

        r = safe_atou32(rvalue, &v);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse '%s=', ignoring assignment: %s",
                           lvalue, rvalue);
                return 0;
        }

        htb->priority = v;
        tclass = NULL;

        return 0;
}

int config_parse_hierarchy_token_bucket_class_size(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        _cleanup_(tclass_free_or_set_invalidp) TClass *tclass = NULL;
        HierarchyTokenBucketClass *htb;
        Network *network = data;
        uint64_t v;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        r = tclass_new_static(TCLASS_KIND_HTB, network, filename, section_line, &tclass);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to create traffic control class, ignoring assignment: %m");
                return 0;
        }

        htb = TCLASS_TO_HTB(tclass);

        if (isempty(rvalue)) {
                if (streq(lvalue, "QuantumBytes"))
                        htb->quantum = 0;
                else if (streq(lvalue, "MTUBytes"))
                        htb->mtu = HTB_DEFAULT_MTU;
                else if (streq(lvalue, "OverheadBytes"))
                        htb->overhead = 0;
                else if (streq(lvalue, "BufferBytes"))
                        htb->buffer = 0;
                else if (streq(lvalue, "CeilBufferBytes"))
                        htb->ceil_buffer = 0;
                else
                        assert_not_reached();

                tclass = NULL;
                return 0;
        }

        r = parse_size(rvalue, 1024, &v);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse '%s=', ignoring assignment: %s",
                           lvalue, rvalue);
                return 0;
        }
        if ((streq(lvalue, "OverheadBytes") && v > UINT16_MAX) || v > UINT32_MAX) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "Invalid '%s=', ignoring assignment: %s",
                           lvalue, rvalue);
                return 0;
        }

        if (streq(lvalue, "QuantumBytes"))
                htb->quantum = v;
        else if (streq(lvalue, "OverheadBytes"))
                htb->overhead = v;
        else if (streq(lvalue, "MTUBytes"))
                htb->mtu = v;
        else if (streq(lvalue, "BufferBytes"))
                htb->buffer = v;
        else if (streq(lvalue, "CeilBufferBytes"))
                htb->ceil_buffer = v;
        else
                assert_not_reached();

        tclass = NULL;

        return 0;
}

int config_parse_hierarchy_token_bucket_class_rate(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        _cleanup_(tclass_free_or_set_invalidp) TClass *tclass = NULL;
        HierarchyTokenBucketClass *htb;
        Network *network = data;
        uint64_t *v;
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);
        assert(data);

        r = tclass_new_static(TCLASS_KIND_HTB, network, filename, section_line, &tclass);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to create traffic control class, ignoring assignment: %m");
                return 0;
        }

        htb = TCLASS_TO_HTB(tclass);
        if (streq(lvalue, "Rate"))
                v = &htb->rate;
        else if (streq(lvalue, "CeilRate"))
                v = &htb->ceil_rate;
        else
                assert_not_reached();

        if (isempty(rvalue)) {
                *v = 0;

                tclass = NULL;
                return 0;
        }

        r = parse_size(rvalue, 1000, v);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse '%s=', ignoring assignment: %s",
                           lvalue, rvalue);
                return 0;
        }

        *v /= 8;
        tclass = NULL;

        return 0;
}

static int hierarchy_token_bucket_class_init(TClass *tclass) {
        HierarchyTokenBucketClass *htb;

        assert(tclass);

        htb = TCLASS_TO_HTB(tclass);

        htb->mtu = HTB_DEFAULT_MTU;

        return 0;
}

static int hierarchy_token_bucket_class_verify(TClass *tclass) {
        HierarchyTokenBucketClass *htb;
        uint32_t hz;
        int r;

        assert(tclass);

        htb = TCLASS_TO_HTB(tclass);

        if (htb->rate == 0)
                return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
                                         "%s: Rate= is mandatory. "
                                         "Ignoring [HierarchyTokenBucketClass] section from line %u.",
                                         tclass->section->filename, tclass->section->line);

        /* if CeilRate= setting is missing, use the same as Rate= */
        if (htb->ceil_rate == 0)
                htb->ceil_rate = htb->rate;

        r = tc_init(NULL, &hz);
        if (r < 0)
                return log_error_errno(r, "Failed to read /proc/net/psched: %m");

        if (htb->buffer == 0)
                htb->buffer = htb->rate / hz + htb->mtu;
        if (htb->ceil_buffer == 0)
                htb->ceil_buffer = htb->ceil_rate / hz + htb->mtu;

        return 0;
}

const TClassVTable htb_tclass_vtable = {
        .object_size = sizeof(HierarchyTokenBucketClass),
        .tca_kind = "htb",
        .fill_message = hierarchy_token_bucket_class_fill_message,
        .init = hierarchy_token_bucket_class_init,
        .verify = hierarchy_token_bucket_class_verify,
};