Debian Codegraph @

tc/q_taprio.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * q_taprio.c Time Aware Priority Scheduler
4 *
5 * Authors: Vinicius Costa Gomes <[email protected]>
6 * Jesus Sanchez-Palencia <[email protected]>
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <syslog.h>
13 #include <fcntl.h>
14 #include <inttypes.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <string.h>
19
20 #include "utils.h"
21 #include "tc_util.h"
22 #include "list.h"
23
24 struct sched_entry {
25 struct list_head list;
26 uint32_t index;
27 uint32_t interval;
28 uint32_t gatemask;
29 uint8_t cmd;
30 };
31
32 static void explain(void)
33 {
34 fprintf(stderr,
35 "Usage: ... taprio clockid CLOCKID\n"
36 " [num_tc NUMBER] [map P0 P1 ...]\n"
37 " [queues COUNT@OFFSET COUNT@OFFSET COUNT@OFFSET ...]\n"
38 " [ [sched-entry index cmd gate-mask interval] ... ]\n"
39 " [base-time time] [txtime-delay delay]\n"
40 " [fp FP0 FP1 FP2 ...]\n"
41 "\n"
42 "CLOCKID must be a valid SYS-V id (i.e. CLOCK_TAI)\n");
43 }
44
45 static void explain_clockid(const char *val)
46 {
47 fprintf(stderr, "taprio: illegal value for \"clockid\": \"%s\".\n", val);
48 fprintf(stderr, "It must be a valid SYS-V id (i.e. CLOCK_TAI)\n");
49 }
50
51 static const char *entry_cmd_to_str(__u8 cmd)
52 {
53 switch (cmd) {
54 case TC_TAPRIO_CMD_SET_GATES:
55 return "S";
56 case TC_TAPRIO_CMD_SET_AND_HOLD:
57 return "H";
58 case TC_TAPRIO_CMD_SET_AND_RELEASE:
59 return "R";
60 default:
61 return "Invalid";
62 }
63 }
64
65 static int str_to_entry_cmd(const char *str)
66 {
67 if (strcmp(str, "S") == 0)
68 return TC_TAPRIO_CMD_SET_GATES;
69 else if (strcmp(str, "H") == 0)
70 return TC_TAPRIO_CMD_SET_AND_HOLD;
71 else if (strcmp(str, "R") == 0)
72 return TC_TAPRIO_CMD_SET_AND_RELEASE;
73
74 return -1;
75 }
76
77 static int add_sched_list(struct list_head *sched_entries, struct nlmsghdr *n)
78 {
79 struct sched_entry *e;
80
81 list_for_each_entry(e, sched_entries, list) {
82 struct rtattr *a;
83
84 a = addattr_nest(n, 1024, TCA_TAPRIO_SCHED_ENTRY);
85
86 addattr_l(n, 1024, TCA_TAPRIO_SCHED_ENTRY_CMD, &e->cmd, sizeof(e->cmd));
87 addattr_l(n, 1024, TCA_TAPRIO_SCHED_ENTRY_GATE_MASK, &e->gatemask, sizeof(e->gatemask));
88 addattr_l(n, 1024, TCA_TAPRIO_SCHED_ENTRY_INTERVAL, &e->interval, sizeof(e->interval));
89
90 addattr_nest_end(n, a);
91 }
92
93 return 0;
94 }
95
96 static void explain_sched_entry(void)
97 {
98 fprintf(stderr, "Usage: ... taprio ... sched-entry <cmd> <gate mask> <interval>\n");
99 }
100
101 static struct sched_entry *create_entry(uint32_t gatemask, uint32_t interval, uint8_t cmd)
102 {
103 struct sched_entry *e;
104
105 e = calloc(1, sizeof(*e));
106 if (!e)
107 return NULL;
108
109 e->gatemask = gatemask;
110 e->interval = interval;
111 e->cmd = cmd;
112
113 return e;
114 }
115
116 static void add_tc_entries(struct nlmsghdr *n, __u32 max_sdu[TC_QOPT_MAX_QUEUE],
117 int num_max_sdu_entries, __u32 fp[TC_QOPT_MAX_QUEUE],
118 int num_fp_entries)
119 {
120 struct rtattr *l;
121 int num_tc;
122 __u32 tc;
123
124 num_tc = max(num_max_sdu_entries, num_fp_entries);
125
126 for (tc = 0; tc < num_tc; tc++) {
127 l = addattr_nest(n, 1024, TCA_TAPRIO_ATTR_TC_ENTRY | NLA_F_NESTED);
128
129 addattr_l(n, 1024, TCA_TAPRIO_TC_ENTRY_INDEX, &tc, sizeof(tc));
130
131 if (tc < num_max_sdu_entries) {
132 addattr_l(n, 1024, TCA_TAPRIO_TC_ENTRY_MAX_SDU,
133 &max_sdu[tc], sizeof(max_sdu[tc]));
134 }
135
136 if (tc < num_fp_entries) {
137 addattr_l(n, 1024, TCA_TAPRIO_TC_ENTRY_FP, &fp[tc],
138 sizeof(fp[tc]));
139 }
140
141 addattr_nest_end(n, l);
142 }
143 }
144
145 static int taprio_parse_opt(const struct qdisc_util *qu, int argc,
146 char **argv, struct nlmsghdr *n, const char *dev)
147 {
148 __u32 max_sdu[TC_QOPT_MAX_QUEUE] = { };
149 __u32 fp[TC_QOPT_MAX_QUEUE] = { };
150 __s32 clockid = CLOCKID_INVALID;
151 struct tc_mqprio_qopt opt = { };
152 __s64 cycle_time_extension = 0;
153 struct list_head sched_entries;
154 bool have_tc_entries = false;
155 int num_max_sdu_entries = 0;
156 struct rtattr *tail, *l;
157 int num_fp_entries = 0;
158 __u32 taprio_flags = 0;
159 __u32 txtime_delay = 0;
160 __s64 cycle_time = 0;
161 __s64 base_time = 0;
162 int err, idx;
163
164 INIT_LIST_HEAD(&sched_entries);
165
166 while (argc > 0) {
167 idx = 0;
168 if (strcmp(*argv, "num_tc") == 0) {
169 NEXT_ARG();
170 if (get_u8(&opt.num_tc, *argv, 10)) {
171 fprintf(stderr, "Illegal \"num_tc\"\n");
172 return -1;
173 }
174 } else if (strcmp(*argv, "map") == 0) {
175 while (idx < TC_QOPT_MAX_QUEUE && NEXT_ARG_OK()) {
176 NEXT_ARG();
177 if (get_u8(&opt.prio_tc_map[idx], *argv, 10)) {
178 PREV_ARG();
179 break;
180 }
181 idx++;
182 }
183 for ( ; idx < TC_QOPT_MAX_QUEUE; idx++)
184 opt.prio_tc_map[idx] = 0;
185 } else if (strcmp(*argv, "queues") == 0) {
186 char *tmp, *tok;
187
188 while (idx < TC_QOPT_MAX_QUEUE && NEXT_ARG_OK()) {
189 NEXT_ARG();
190
191 tmp = strdup(*argv);
192 if (!tmp)
193 break;
194
195 tok = strtok(tmp, "@");
196 if (get_u16(&opt.count[idx], tok, 10)) {
197 free(tmp);
198 PREV_ARG();
199 break;
200 }
201 tok = strtok(NULL, "@");
202 if (get_u16(&opt.offset[idx], tok, 10)) {
203 free(tmp);
204 PREV_ARG();
205 break;
206 }
207 free(tmp);
208 idx++;
209 }
210 } else if (strcmp(*argv, "fp") == 0) {
211 while (idx < TC_QOPT_MAX_QUEUE && NEXT_ARG_OK()) {
212 NEXT_ARG();
213 if (strcmp(*argv, "E") == 0) {
214 fp[idx] = TC_FP_EXPRESS;
215 } else if (strcmp(*argv, "P") == 0) {
216 fp[idx] = TC_FP_PREEMPTIBLE;
217 } else {
218 PREV_ARG();
219 break;
220 }
221 num_fp_entries++;
222 idx++;
223 }
224 have_tc_entries = true;
225 } else if (strcmp(*argv, "max-sdu") == 0) {
226 while (idx < TC_QOPT_MAX_QUEUE && NEXT_ARG_OK()) {
227 NEXT_ARG();
228 if (get_u32(&max_sdu[idx], *argv, 10)) {
229 PREV_ARG();
230 break;
231 }
232 num_max_sdu_entries++;
233 idx++;
234 }
235 have_tc_entries = true;
236 } else if (strcmp(*argv, "sched-entry") == 0) {
237 uint32_t mask, interval;
238 struct sched_entry *e;
239 uint8_t cmd;
240
241 NEXT_ARG();
242 err = str_to_entry_cmd(*argv);
243 if (err < 0) {
244 explain_sched_entry();
245 return -1;
246 }
247 cmd = err;
248
249 NEXT_ARG();
250 if (get_u32(&mask, *argv, 16)) {
251 explain_sched_entry();
252 return -1;
253 }
254
255 NEXT_ARG();
256 if (get_u32(&interval, *argv, 0)) {
257 explain_sched_entry();
258 return -1;
259 }
260
261 e = create_entry(mask, interval, cmd);
262 if (!e) {
263 fprintf(stderr, "taprio: not enough memory for new schedule entry\n");
264 return -1;
265 }
266
267 list_add_tail(&e->list, &sched_entries);
268
269 } else if (strcmp(*argv, "base-time") == 0) {
270 NEXT_ARG();
271 if (get_s64(&base_time, *argv, 10)) {
272 PREV_ARG();
273 break;
274 }
275 } else if (strcmp(*argv, "cycle-time") == 0) {
276 NEXT_ARG();
277 if (cycle_time) {
278 fprintf(stderr, "taprio: duplicate \"cycle-time\" specification\n");
279 return -1;
280 }
281
282 if (get_s64(&cycle_time, *argv, 10)) {
283 PREV_ARG();
284 break;
285 }
286
287 } else if (strcmp(*argv, "cycle-time-extension") == 0) {
288 NEXT_ARG();
289 if (cycle_time_extension) {
290 fprintf(stderr, "taprio: duplicate \"cycle-time-extension\" specification\n");
291 return -1;
292 }
293
294 if (get_s64(&cycle_time_extension, *argv, 10)) {
295 PREV_ARG();
296 break;
297 }
298 } else if (strcmp(*argv, "clockid") == 0) {
299 NEXT_ARG();
300 if (clockid != CLOCKID_INVALID) {
301 fprintf(stderr, "taprio: duplicate \"clockid\" specification\n");
302 return -1;
303 }
304 if (get_clockid(&clockid, *argv)) {
305 explain_clockid(*argv);
306 return -1;
307 }
308 } else if (strcmp(*argv, "flags") == 0) {
309 NEXT_ARG();
310 if (taprio_flags) {
311 fprintf(stderr, "taprio: duplicate \"flags\" specification\n");
312 return -1;
313 }
314 if (get_u32(&taprio_flags, *argv, 0)) {
315 PREV_ARG();
316 return -1;
317 }
318
319 } else if (strcmp(*argv, "txtime-delay") == 0) {
320 NEXT_ARG();
321 if (txtime_delay != 0) {
322 fprintf(stderr, "taprio: duplicate \"txtime-delay\" specification\n");
323 return -1;
324 }
325 if (get_u32(&txtime_delay, *argv, 0)) {
326 PREV_ARG();
327 return -1;
328 }
329
330 } else if (strcmp(*argv, "help") == 0) {
331 explain();
332 return -1;
333 } else {
334 fprintf(stderr, "Unknown argument\n");
335 return -1;
336 }
337 argc--; argv++;
338 }
339
340 tail = NLMSG_TAIL(n);
341 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
342
343 if (clockid != CLOCKID_INVALID)
344 addattr_l(n, 1024, TCA_TAPRIO_ATTR_SCHED_CLOCKID, &clockid, sizeof(clockid));
345
346 if (taprio_flags)
347 addattr_l(n, 1024, TCA_TAPRIO_ATTR_FLAGS, &taprio_flags, sizeof(taprio_flags));
348
349 if (opt.num_tc > 0)
350 addattr_l(n, 1024, TCA_TAPRIO_ATTR_PRIOMAP, &opt, sizeof(opt));
351
352 if (txtime_delay)
353 addattr_l(n, 1024, TCA_TAPRIO_ATTR_TXTIME_DELAY, &txtime_delay, sizeof(txtime_delay));
354
355 if (base_time)
356 addattr_l(n, 1024, TCA_TAPRIO_ATTR_SCHED_BASE_TIME, &base_time, sizeof(base_time));
357
358 if (cycle_time)
359 addattr_l(n, 1024, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME,
360 &cycle_time, sizeof(cycle_time));
361
362 if (cycle_time_extension)
363 addattr_l(n, 1024, TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION,
364 &cycle_time_extension, sizeof(cycle_time_extension));
365
366 if (have_tc_entries)
367 add_tc_entries(n, max_sdu, num_max_sdu_entries, fp, num_fp_entries);
368
369 l = addattr_nest(n, 1024, TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST | NLA_F_NESTED);
370
371 err = add_sched_list(&sched_entries, n);
372 if (err < 0) {
373 fprintf(stderr, "Could not add schedule to netlink message\n");
374 return -1;
375 }
376
377 addattr_nest_end(n, l);
378
379 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
380
381 return 0;
382 }
383
384 static void print_sched_list(FILE *f, struct rtattr *list)
385 {
386 struct rtattr *item, *nla;
387 int rem;
388
389 rem = RTA_PAYLOAD(list);
390
391 open_json_array(PRINT_JSON, "schedule");
392
393 print_nl();
394
395 for (item = RTA_DATA(list); RTA_OK(item, rem); item = RTA_NEXT(item, rem)) {
396 struct rtattr *tb[TCA_TAPRIO_SCHED_ENTRY_MAX + 1];
397
398 parse_rtattr_nested(tb, TCA_TAPRIO_SCHED_ENTRY_MAX, item);
399
400 open_json_object(NULL);
401
402 nla = tb[TCA_TAPRIO_SCHED_ENTRY_INDEX];
403 if (nla) {
404 __u32 index = rta_getattr_u32(nla);
405
406 print_uint(PRINT_ANY, "index", "\tindex %u", index);
407 }
408
409 nla = tb[TCA_TAPRIO_SCHED_ENTRY_CMD];
410 if (nla) {
411 __u8 command = rta_getattr_u8(nla);
412
413 print_string(PRINT_ANY, "cmd", " cmd %s",
414 entry_cmd_to_str(command));
415 }
416
417 nla = tb[TCA_TAPRIO_SCHED_ENTRY_GATE_MASK];
418 if (nla) {
419 __u32 gatemask = rta_getattr_u32(nla);
420
421 print_0xhex(PRINT_ANY, "gatemask", " gatemask %#llx",
422 gatemask);
423 }
424
425 nla = tb[TCA_TAPRIO_SCHED_ENTRY_INTERVAL];
426 if (nla) {
427 __u32 interval = rta_getattr_u32(nla);
428
429 print_uint(PRINT_ANY, "interval", " interval %u",
430 interval);
431 }
432
433 close_json_object();
434
435 print_nl();
436 }
437
438 close_json_array(PRINT_ANY, "");
439 }
440
441 static int print_schedule(FILE *f, struct rtattr **tb)
442 {
443 struct rtattr *nla;
444
445 nla = tb[TCA_TAPRIO_ATTR_SCHED_BASE_TIME];
446 if (nla) {
447 int64_t base_time = rta_getattr_s64(nla);
448
449 print_lluint(PRINT_ANY, "base_time", "\tbase-time %lld",
450 base_time);
451 }
452
453 nla = tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME];
454 if (nla) {
455 int64_t cycle_time = rta_getattr_s64(nla);
456
457 print_lluint(PRINT_ANY, "cycle_time", " cycle-time %lld",
458 cycle_time);
459 }
460
461 nla = tb[TCA_TAPRIO_ATTR_SCHED_CYCLE_TIME_EXTENSION];
462 if (nla) {
463 int64_t cycle_time_extension = rta_getattr_s64(nla);
464
465 print_lluint(PRINT_ANY, "cycle_time_extension",
466 " cycle-time-extension %lld",
467 cycle_time_extension);
468 }
469
470 nla = tb[TCA_TAPRIO_ATTR_SCHED_ENTRY_LIST];
471 if (nla)
472 print_sched_list(f, nla);
473
474 return 0;
475 }
476
477 static void dump_tc_entry(struct rtattr *item,
478 __u32 max_sdu[TC_QOPT_MAX_QUEUE],
479 __u32 fp[TC_QOPT_MAX_QUEUE],
480 int *max_tc_max_sdu, int *max_tc_fp)
481 {
482 struct rtattr *tb[TCA_TAPRIO_TC_ENTRY_MAX + 1];
483 __u32 tc, val = 0;
484
485 parse_rtattr_nested(tb, TCA_TAPRIO_TC_ENTRY_MAX, item);
486
487 if (!tb[TCA_TAPRIO_TC_ENTRY_INDEX]) {
488 fprintf(stderr, "Missing tc entry index\n");
489 return;
490 }
491
492 tc = rta_getattr_u32(tb[TCA_TAPRIO_TC_ENTRY_INDEX]);
493 /* Prevent array out of bounds access */
494 if (tc >= TC_QOPT_MAX_QUEUE) {
495 fprintf(stderr, "Unexpected tc entry index %d\n", tc);
496 return;
497 }
498
499 if (tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]) {
500 val = rta_getattr_u32(tb[TCA_TAPRIO_TC_ENTRY_MAX_SDU]);
501 max_sdu[tc] = val;
502 if (*max_tc_max_sdu < (int)tc)
503 *max_tc_max_sdu = tc;
504 }
505
506 if (tb[TCA_TAPRIO_TC_ENTRY_FP]) {
507 val = rta_getattr_u32(tb[TCA_TAPRIO_TC_ENTRY_FP]);
508 fp[tc] = val;
509
510 if (*max_tc_fp < (int)tc)
511 *max_tc_fp = tc;
512 }
513 }
514
515 static void dump_tc_entries(FILE *f, struct rtattr *opt)
516 {
517 __u32 max_sdu[TC_QOPT_MAX_QUEUE] = {};
518 __u32 fp[TC_QOPT_MAX_QUEUE] = {};
519 int max_tc_max_sdu = -1;
520 int max_tc_fp = -1;
521 struct rtattr *i;
522 int tc, rem;
523
524 rem = RTA_PAYLOAD(opt);
525
526 for (i = RTA_DATA(opt); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
527 if (i->rta_type != (TCA_TAPRIO_ATTR_TC_ENTRY | NLA_F_NESTED))
528 continue;
529
530 dump_tc_entry(i, max_sdu, fp, &max_tc_max_sdu, &max_tc_fp);
531 }
532
533 if (max_tc_max_sdu >= 0) {
534 open_json_array(PRINT_ANY, "max-sdu");
535 for (tc = 0; tc <= max_tc_max_sdu; tc++)
536 print_uint(PRINT_ANY, NULL, " %u", max_sdu[tc]);
537 close_json_array(PRINT_ANY, "");
538
539 print_nl();
540 }
541
542 if (max_tc_fp >= 0) {
543 open_json_array(PRINT_ANY, "fp");
544 for (tc = 0; tc <= max_tc_fp; tc++) {
545 print_string(PRINT_ANY, NULL, " %s",
546 fp[tc] == TC_FP_PREEMPTIBLE ? "P" :
547 fp[tc] == TC_FP_EXPRESS ? "E" :
548 "?");
549 }
550 close_json_array(PRINT_ANY, "");
551
552 print_nl();
553 }
554 }
555
556 static int taprio_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt)
557 {
558 struct rtattr *tb[TCA_TAPRIO_ATTR_MAX + 1];
559 struct tc_mqprio_qopt *qopt = 0;
560 int i;
561
562 if (opt == NULL)
563 return 0;
564
565 parse_rtattr_nested(tb, TCA_TAPRIO_ATTR_MAX, opt);
566
567 if (tb[TCA_TAPRIO_ATTR_PRIOMAP] == NULL)
568 return -1;
569
570 qopt = RTA_DATA(tb[TCA_TAPRIO_ATTR_PRIOMAP]);
571
572 print_uint(PRINT_ANY, "tc", "tc %u ", qopt->num_tc);
573
574 open_json_array(PRINT_ANY, "map");
575 for (i = 0; i <= TC_PRIO_MAX; i++)
576 print_uint(PRINT_ANY, NULL, " %u", qopt->prio_tc_map[i]);
577 close_json_array(PRINT_ANY, "");
578
579 print_nl();
580
581 open_json_array(PRINT_ANY, "queues");
582 for (i = 0; i < qopt->num_tc; i++) {
583 open_json_object(NULL);
584 print_uint(PRINT_ANY, "offset", " offset %u", qopt->offset[i]);
585 print_uint(PRINT_ANY, "count", " count %u", qopt->count[i]);
586 close_json_object();
587 }
588 close_json_array(PRINT_ANY, "");
589
590 print_nl();
591
592 if (tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]) {
593 __s32 clockid;
594
595 clockid = rta_getattr_s32(tb[TCA_TAPRIO_ATTR_SCHED_CLOCKID]);
596 print_string(PRINT_ANY, "clockid", "clockid %s",
597 get_clock_name(clockid));
598 }
599
600 if (tb[TCA_TAPRIO_ATTR_FLAGS]) {
601 __u32 flags;
602
603 flags = rta_getattr_u32(tb[TCA_TAPRIO_ATTR_FLAGS]);
604 print_0xhex(PRINT_ANY, "flags", " flags %#x", flags);
605 }
606
607 if (tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]) {
608 __u32 txtime_delay;
609
610 txtime_delay = rta_getattr_s32(tb[TCA_TAPRIO_ATTR_TXTIME_DELAY]);
611 print_uint(PRINT_ANY, "txtime_delay", " txtime delay %d", txtime_delay);
612 }
613
614 print_schedule(f, tb);
615
616 if (tb[TCA_TAPRIO_ATTR_ADMIN_SCHED]) {
617 struct rtattr *t[TCA_TAPRIO_ATTR_MAX + 1];
618
619 parse_rtattr_nested(t, TCA_TAPRIO_ATTR_MAX,
620 tb[TCA_TAPRIO_ATTR_ADMIN_SCHED]);
621
622 open_json_object("admin");
623
624 print_schedule(f, t);
625
626 close_json_object();
627 }
628
629 dump_tc_entries(f, opt);
630
631 return 0;
632 }
633
634 static int taprio_print_xstats(const struct qdisc_util *qu, FILE *f,
635 struct rtattr *xstats)
636 {
637 struct rtattr *tb[TCA_TAPRIO_OFFLOAD_STATS_MAX + 1], *nla;
638
639 if (!xstats)
640 return 0;
641
642 parse_rtattr_nested(tb, TCA_TAPRIO_OFFLOAD_STATS_MAX, xstats);
643
644 nla = tb[TCA_TAPRIO_OFFLOAD_STATS_WINDOW_DROPS];
645 if (nla)
646 print_lluint(PRINT_ANY, "window_drops", " window_drops %llu",
647 rta_getattr_u64(nla));
648
649 nla = tb[TCA_TAPRIO_OFFLOAD_STATS_TX_OVERRUNS];
650 if (nla)
651 print_lluint(PRINT_ANY, "tx_overruns", " tx_overruns %llu",
652 rta_getattr_u64(nla));
653
654 return 0;
655 }
656
657 struct qdisc_util taprio_qdisc_util = {
658 .id = "taprio",
659 .parse_qopt = taprio_parse_opt,
660 .print_qopt = taprio_print_opt,
661 .print_xstats = taprio_print_xstats,
662 };