Debian Codegraph @

tc/q_prio.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * q_prio.c PRIO.
4 *
5 * Authors: Alexey Kuznetsov, <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16
17 #include "utils.h"
18 #include "tc_util.h"
19
20 static void explain(void)
21 {
22 fprintf(stderr, "Usage: ... prio bands NUMBER priomap P1 P2...[multiqueue]\n");
23 }
24
25 static int prio_parse_opt(const struct qdisc_util *qu, int argc, char **argv,
26 struct nlmsghdr *n, const char *dev)
27 {
28 int pmap_mode = 0;
29 int idx = 0;
30 struct tc_prio_qopt opt = {3, { 1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 } };
31 struct rtattr *nest;
32 unsigned char mq = 0;
33
34 while (argc > 0) {
35 if (strcmp(*argv, "bands") == 0) {
36 if (pmap_mode)
37 explain();
38 NEXT_ARG();
39 if (get_integer(&opt.bands, *argv, 10)) {
40 fprintf(stderr, "Illegal \"bands\"\n");
41 return -1;
42 }
43 } else if (strcmp(*argv, "priomap") == 0) {
44 if (pmap_mode) {
45 fprintf(stderr, "Error: duplicate priomap\n");
46 return -1;
47 }
48 pmap_mode = 1;
49 } else if (strcmp(*argv, "multiqueue") == 0) {
50 mq = 1;
51 } else if (strcmp(*argv, "help") == 0) {
52 explain();
53 return -1;
54 } else {
55 unsigned int band;
56
57 if (!pmap_mode) {
58 fprintf(stderr, "What is \"%s\"?\n", *argv);
59 explain();
60 return -1;
61 }
62 if (get_unsigned(&band, *argv, 10)) {
63 fprintf(stderr, "Illegal \"priomap\" element\n");
64 return -1;
65 }
66 if (band >= opt.bands) {
67 fprintf(stderr, "\"priomap\" element is out of bands\n");
68 return -1;
69 }
70 if (idx > TC_PRIO_MAX) {
71 fprintf(stderr, "\"priomap\" index > TC_PRIO_MAX=%u\n", TC_PRIO_MAX);
72 return -1;
73 }
74 opt.priomap[idx++] = band;
75 }
76 argc--; argv++;
77 }
78
79 /*
80 if (pmap_mode) {
81 for (; idx < TC_PRIO_MAX; idx++)
82 opt.priomap[idx] = opt.priomap[TC_PRIO_BESTEFFORT];
83 }
84 */
85 nest = addattr_nest_compat(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
86 if (mq)
87 addattr_l(n, 1024, TCA_PRIO_MQ, NULL, 0);
88 addattr_nest_compat_end(n, nest);
89 return 0;
90 }
91
92 int prio_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt)
93 {
94 int i;
95 struct tc_prio_qopt *qopt;
96 struct rtattr *tb[TCA_PRIO_MAX+1];
97
98 if (opt == NULL)
99 return 0;
100
101 if (parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt,
102 sizeof(*qopt)))
103 return -1;
104 if (qopt == NULL)
105 return -1; /* missing data from kernel */
106
107 print_uint(PRINT_ANY, "bands", "bands %u ", qopt->bands);
108 open_json_array(PRINT_ANY, "priomap");
109 for (i = 0; i <= TC_PRIO_MAX; i++)
110 print_uint(PRINT_ANY, NULL, " %d", qopt->priomap[i]);
111 close_json_array(PRINT_ANY, "");
112
113 if (tb[TCA_PRIO_MQ])
114 print_string(PRINT_FP, NULL, " multiqueue: %s ",
115 rta_getattr_u8(tb[TCA_PRIO_MQ]) ? "on" : "off");
116 print_bool(PRINT_JSON, "multiqueue", NULL,
117 tb[TCA_PRIO_MQ] && rta_getattr_u8(tb[TCA_PRIO_MQ]));
118
119 return 0;
120 }
121
122 struct qdisc_util prio_qdisc_util = {
123 .id = "prio",
124 .parse_qopt = prio_parse_opt,
125 .print_qopt = prio_print_opt,
126 };