Debian Codegraph @

tc/m_gate.c

raw
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2020 NXP */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <linux/if_ether.h>
9 #include "utils.h"
10 #include "rt_names.h"
11 #include "tc_util.h"
12 #include "list.h"
13 #include <linux/tc_act/tc_gate.h>
14
15 struct gate_entry {
16 struct list_head list;
17 uint8_t gate_state;
18 uint32_t interval;
19 int32_t ipv;
20 int32_t maxoctets;
21 };
22
23 static void explain(void)
24 {
25 fprintf(stderr,
26 "Usage: gate [ priority PRIO-SPEC ] [ base-time BASE-TIME ]\n"
27 " [ cycle-time CYCLE-TIME ]\n"
28 " [ cycle-time-ext CYCLE-TIME-EXT ]\n"
29 " [ clockid CLOCKID ] [flags FLAGS]\n"
30 " [ sched-entry GATE0 INTERVAL [ INTERNAL-PRIO-VALUE MAX-OCTETS ] ]\n"
31 " [ sched-entry GATE1 INTERVAL [ INTERNAL-PRIO-VALUE MAX-OCTETS ] ]\n"
32 " ......\n"
33 " [ sched-entry GATEn INTERVAL [ INTERNAL-PRIO-VALUE MAX-OCTETS ] ]\n"
34 " [ CONTROL ]\n"
35 " GATEn := open | close\n"
36 " INTERVAL : nanoseconds period of gate slot\n"
37 " INTERNAL-PRIO-VALUE : internal priority decide which\n"
38 " rx queue number direct to.\n"
39 " default to be -1 which means wildcard.\n"
40 " MAX-OCTETS : maximum number of MSDU octets that are\n"
41 " permitted to pas the gate during the\n"
42 " specified TimeInterval.\n"
43 " default to be -1 which means wildcard.\n"
44 " CONTROL := pipe | drop | continue | pass |\n"
45 " goto chain <CHAIN_INDEX>\n");
46 }
47
48 static void usage(void)
49 {
50 explain();
51 exit(-1);
52 }
53
54 static void explain_entry_format(void)
55 {
56 fprintf(stderr, "Usage: sched-entry <open | close> <interval> [ <interval ipv> <octets max bytes> ]\n");
57 }
58
59 static int parse_gate(const struct action_util *a, int *argc_p, char ***argv_p,
60 int tca_id, struct nlmsghdr *n);
61 static int print_gate(const struct action_util *au, FILE *f, struct rtattr *arg);
62
63 struct action_util gate_action_util = {
64 .id = "gate",
65 .parse_aopt = parse_gate,
66 .print_aopt = print_gate,
67 };
68
69 static int get_gate_state(__u8 *val, const char *arg)
70 {
71 if (!strcasecmp("OPEN", arg)) {
72 *val = 1;
73 return 0;
74 }
75
76 if (!strcasecmp("CLOSE", arg)) {
77 *val = 0;
78 return 0;
79 }
80
81 return -1;
82 }
83
84 static struct gate_entry *create_gate_entry(uint8_t gate_state,
85 uint32_t interval,
86 int32_t ipv,
87 int32_t maxoctets)
88 {
89 struct gate_entry *e;
90
91 e = calloc(1, sizeof(*e));
92 if (!e)
93 return NULL;
94
95 e->gate_state = gate_state;
96 e->interval = interval;
97 e->ipv = ipv;
98 e->maxoctets = maxoctets;
99
100 return e;
101 }
102
103 static int add_gate_list(struct list_head *gate_entries, struct nlmsghdr *n)
104 {
105 struct gate_entry *e;
106
107 list_for_each_entry(e, gate_entries, list) {
108 struct rtattr *a;
109
110 a = addattr_nest(n, 1024, TCA_GATE_ONE_ENTRY | NLA_F_NESTED);
111
112 if (e->gate_state)
113 addattr(n, MAX_MSG, TCA_GATE_ENTRY_GATE);
114
115 addattr_l(n, MAX_MSG, TCA_GATE_ENTRY_INTERVAL,
116 &e->interval, sizeof(e->interval));
117 addattr_l(n, MAX_MSG, TCA_GATE_ENTRY_IPV,
118 &e->ipv, sizeof(e->ipv));
119 addattr_l(n, MAX_MSG, TCA_GATE_ENTRY_MAX_OCTETS,
120 &e->maxoctets, sizeof(e->maxoctets));
121
122 addattr_nest_end(n, a);
123 }
124
125 return 0;
126 }
127
128 static void free_entries(struct list_head *gate_entries)
129 {
130 struct gate_entry *e, *n;
131
132 list_for_each_entry_safe(e, n, gate_entries, list) {
133 list_del(&e->list);
134 free(e);
135 }
136 }
137
138 static int parse_gate(const struct action_util *a, int *argc_p, char ***argv_p,
139 int tca_id, struct nlmsghdr *n)
140 {
141 struct tc_gate parm = {.action = TC_ACT_PIPE};
142 struct list_head gate_entries;
143 __s32 clockid = CLOCKID_INVALID;
144 struct rtattr *tail, *nle;
145 char **argv = *argv_p;
146 int argc = *argc_p;
147 __s64 base_time = 0;
148 __s64 cycle_time = 0;
149 __s64 cycle_time_ext = 0;
150 int entry_num = 0;
151 char *invalidarg;
152 __u32 flags = 0;
153 int prio = -1;
154
155 int err;
156
157 if (matches(*argv, "gate") != 0)
158 return -1;
159
160 NEXT_ARG();
161 if (argc <= 0)
162 return -1;
163
164 INIT_LIST_HEAD(&gate_entries);
165
166 while (argc > 0) {
167 if (matches(*argv, "index") == 0) {
168 NEXT_ARG();
169 if (get_u32(&parm.index, *argv, 10)) {
170 invalidarg = "index";
171 goto err_arg;
172 }
173 } else if (matches(*argv, "priority") == 0) {
174 NEXT_ARG();
175 if (get_s32(&prio, *argv, 0)) {
176 invalidarg = "priority";
177 goto err_arg;
178 }
179 } else if (matches(*argv, "base-time") == 0) {
180 NEXT_ARG();
181 if (get_s64(&base_time, *argv, 10) &&
182 get_time64(&base_time, *argv)) {
183 invalidarg = "base-time";
184 goto err_arg;
185 }
186 } else if (matches(*argv, "cycle-time") == 0) {
187 NEXT_ARG();
188 if (get_s64(&cycle_time, *argv, 10) &&
189 get_time64(&cycle_time, *argv)) {
190 invalidarg = "cycle-time";
191 goto err_arg;
192 }
193 } else if (matches(*argv, "cycle-time-ext") == 0) {
194 NEXT_ARG();
195 if (get_s64(&cycle_time_ext, *argv, 10) &&
196 get_time64(&cycle_time_ext, *argv)) {
197 invalidarg = "cycle-time-ext";
198 goto err_arg;
199 }
200 } else if (matches(*argv, "clockid") == 0) {
201 NEXT_ARG();
202 if (get_clockid(&clockid, *argv)) {
203 invalidarg = "clockid";
204 goto err_arg;
205 }
206 } else if (matches(*argv, "flags") == 0) {
207 NEXT_ARG();
208 if (get_u32(&flags, *argv, 0)) {
209 invalidarg = "flags";
210 goto err_arg;
211 }
212 } else if (matches(*argv, "sched-entry") == 0) {
213 unsigned int maxoctets_uint = 0;
214 int32_t maxoctets = -1;
215 struct gate_entry *e;
216 uint8_t gate_state = 0;
217 __s64 interval_s64 = 0;
218 uint32_t interval = 0;
219 int32_t ipv = -1;
220
221 if (!NEXT_ARG_OK()) {
222 explain_entry_format();
223 fprintf(stderr, "\"sched-entry\" is incomplete\n");
224 free_entries(&gate_entries);
225 return -1;
226 }
227
228 NEXT_ARG();
229
230 if (get_gate_state(&gate_state, *argv)) {
231 explain_entry_format();
232 fprintf(stderr, "\"sched-entry\" is incomplete\n");
233 free_entries(&gate_entries);
234 return -1;
235 }
236
237 if (!NEXT_ARG_OK()) {
238 explain_entry_format();
239 fprintf(stderr, "\"sched-entry\" is incomplete\n");
240 free_entries(&gate_entries);
241 return -1;
242 }
243
244 NEXT_ARG();
245
246 if (get_u32(&interval, *argv, 0) &&
247 get_time64(&interval_s64, *argv)) {
248 explain_entry_format();
249 fprintf(stderr, "\"sched-entry\" is incomplete\n");
250 free_entries(&gate_entries);
251 return -1;
252 }
253
254 if (interval_s64 > UINT_MAX) {
255 fprintf(stderr, "\"interval\" is too large\n");
256 free_entries(&gate_entries);
257 return -1;
258 } else if (interval_s64) {
259 interval = interval_s64;
260 }
261
262 if (!NEXT_ARG_OK())
263 goto create_entry;
264
265 NEXT_ARG();
266
267 if (get_s32(&ipv, *argv, 0)) {
268 PREV_ARG();
269 goto create_entry;
270 }
271
272 if (!gate_state)
273 ipv = -1;
274
275 if (!NEXT_ARG_OK())
276 goto create_entry;
277
278 NEXT_ARG();
279
280 if (get_s32(&maxoctets, *argv, 0) &&
281 get_size(&maxoctets_uint, *argv))
282 PREV_ARG();
283
284 if (maxoctets_uint > INT_MAX) {
285 fprintf(stderr, "\"maxoctets\" is too large\n");
286 free_entries(&gate_entries);
287 return -1;
288 } else if (maxoctets_uint ) {
289 maxoctets = maxoctets_uint;
290 }
291
292 if (!gate_state)
293 maxoctets = -1;
294
295 create_entry:
296 e = create_gate_entry(gate_state, interval,
297 ipv, maxoctets);
298 if (!e) {
299 fprintf(stderr, "gate: not enough memory\n");
300 free_entries(&gate_entries);
301 return -1;
302 }
303
304 list_add_tail(&e->list, &gate_entries);
305 entry_num++;
306 } else if (matches(*argv, "help") == 0) {
307 usage();
308 } else {
309 break;
310 }
311
312 argc--;
313 argv++;
314 }
315
316 parse_action_control_dflt(&argc, &argv, &parm.action,
317 false, TC_ACT_PIPE);
318
319 if (!entry_num && !parm.index) {
320 fprintf(stderr, "gate: must add at least one entry\n");
321 return -1;
322 }
323
324 tail = addattr_nest(n, MAX_MSG, tca_id | NLA_F_NESTED);
325 addattr_l(n, MAX_MSG, TCA_GATE_PARMS, &parm, sizeof(parm));
326
327 if (prio != -1)
328 addattr_l(n, MAX_MSG, TCA_GATE_PRIORITY, &prio, sizeof(prio));
329
330 if (flags)
331 addattr_l(n, MAX_MSG, TCA_GATE_FLAGS, &flags, sizeof(flags));
332
333 if (base_time)
334 addattr_l(n, MAX_MSG, TCA_GATE_BASE_TIME,
335 &base_time, sizeof(base_time));
336
337 if (cycle_time)
338 addattr_l(n, MAX_MSG, TCA_GATE_CYCLE_TIME,
339 &cycle_time, sizeof(cycle_time));
340
341 if (cycle_time_ext)
342 addattr_l(n, MAX_MSG, TCA_GATE_CYCLE_TIME_EXT,
343 &cycle_time_ext, sizeof(cycle_time_ext));
344
345 if (clockid != CLOCKID_INVALID)
346 addattr_l(n, MAX_MSG, TCA_GATE_CLOCKID,
347 &clockid, sizeof(clockid));
348
349 nle = addattr_nest(n, MAX_MSG, TCA_GATE_ENTRY_LIST | NLA_F_NESTED);
350 err = add_gate_list(&gate_entries, n);
351 if (err < 0) {
352 fprintf(stderr, "Could not add entries to netlink message\n");
353 free_entries(&gate_entries);
354 return -1;
355 }
356
357 addattr_nest_end(n, nle);
358 addattr_nest_end(n, tail);
359 free_entries(&gate_entries);
360 *argc_p = argc;
361 *argv_p = argv;
362
363 return 0;
364 err_arg:
365 invarg(invalidarg, *argv);
366 free_entries(&gate_entries);
367
368 return -1;
369 }
370
371 static int print_gate_list(struct rtattr *list)
372 {
373 struct rtattr *item;
374 int rem;
375
376 rem = RTA_PAYLOAD(list);
377
378 print_string(PRINT_FP, NULL, "%s", _SL_);
379 print_string(PRINT_FP, NULL, "\tschedule:%s", _SL_);
380 open_json_array(PRINT_JSON, "schedule");
381
382 for (item = RTA_DATA(list);
383 RTA_OK(item, rem);
384 item = RTA_NEXT(item, rem)) {
385 struct rtattr *tb[TCA_GATE_ENTRY_MAX + 1];
386 __u32 index = 0, interval = 0;
387 __u8 gate_state = 0;
388 __s32 ipv = -1, maxoctets = -1;
389 SPRINT_BUF(buf);
390
391 parse_rtattr_nested(tb, TCA_GATE_ENTRY_MAX, item);
392
393 if (tb[TCA_GATE_ENTRY_INDEX])
394 index = rta_getattr_u32(tb[TCA_GATE_ENTRY_INDEX]);
395
396 if (tb[TCA_GATE_ENTRY_GATE])
397 gate_state = 1;
398
399 if (tb[TCA_GATE_ENTRY_INTERVAL])
400 interval = rta_getattr_u32(tb[TCA_GATE_ENTRY_INTERVAL]);
401
402 if (tb[TCA_GATE_ENTRY_IPV])
403 ipv = rta_getattr_s32(tb[TCA_GATE_ENTRY_IPV]);
404
405 if (tb[TCA_GATE_ENTRY_MAX_OCTETS])
406 maxoctets = rta_getattr_s32(tb[TCA_GATE_ENTRY_MAX_OCTETS]);
407
408 open_json_object(NULL);
409 print_uint(PRINT_ANY, "number", "\t number %4u", index);
410 print_string(PRINT_ANY, "gate_state", "\tgate-state %s ",
411 gate_state ? "open" : "close");
412
413 print_uint(PRINT_JSON, "interval", NULL, interval);
414
415 memset(buf, 0, sizeof(buf));
416 print_string(PRINT_FP, NULL, "\tinterval %s",
417 sprint_time64(interval, buf));
418
419 if (ipv != -1) {
420 print_uint(PRINT_ANY, "ipv", "\t ipv %-10u", ipv);
421 } else {
422 print_int(PRINT_JSON, "ipv", NULL, ipv);
423 print_string(PRINT_FP, NULL, "\t ipv %s", "wildcard");
424 }
425
426 if (maxoctets != -1) {
427 print_size(PRINT_ANY, "max_octets", "\t max-octets %s",
428 maxoctets);
429 } else {
430 print_string(PRINT_FP, NULL,
431 "\t max-octets %s", "wildcard");
432 print_int(PRINT_JSON, "max_octets", NULL, maxoctets);
433 }
434
435 close_json_object();
436 print_string(PRINT_FP, NULL, "%s", _SL_);
437 }
438
439 close_json_array(PRINT_ANY, "");
440
441 return 0;
442 }
443
444 static int print_gate(const struct action_util *au, FILE *f, struct rtattr *arg)
445 {
446 struct tc_gate *parm;
447 struct rtattr *tb[TCA_GATE_MAX + 1];
448 __s32 clockid = CLOCKID_INVALID;
449 __s64 base_time = 0;
450 __s64 cycle_time = 0;
451 __s64 cycle_time_ext = 0;
452 SPRINT_BUF(buf);
453 int prio = -1;
454
455 if (arg == NULL)
456 return -1;
457
458 parse_rtattr_nested(tb, TCA_GATE_MAX, arg);
459
460 if (!tb[TCA_GATE_PARMS]) {
461 fprintf(stderr, "Missing gate parameters\n");
462 return -1;
463 }
464
465 print_string(PRINT_FP, NULL, "%s", "\n");
466
467 parm = RTA_DATA(tb[TCA_GATE_PARMS]);
468
469 if (tb[TCA_GATE_PRIORITY])
470 prio = rta_getattr_s32(tb[TCA_GATE_PRIORITY]);
471
472 if (prio != -1) {
473 print_int(PRINT_ANY, "priority", "\tpriority %-8d", prio);
474 } else {
475 print_string(PRINT_FP, NULL, "\tpriority %s", "wildcard");
476 print_int(PRINT_JSON, "priority", NULL, prio);
477 }
478
479 if (tb[TCA_GATE_CLOCKID])
480 clockid = rta_getattr_s32(tb[TCA_GATE_CLOCKID]);
481 print_string(PRINT_ANY, "clockid", "\tclockid %s",
482 get_clock_name(clockid));
483
484 if (tb[TCA_GATE_FLAGS]) {
485 __u32 flags;
486
487 flags = rta_getattr_u32(tb[TCA_GATE_FLAGS]);
488 print_0xhex(PRINT_ANY, "flags", "\tflags %#x", flags);
489 }
490
491 print_string(PRINT_FP, NULL, "%s", "\n");
492
493 if (tb[TCA_GATE_BASE_TIME])
494 base_time = rta_getattr_s64(tb[TCA_GATE_BASE_TIME]);
495
496 memset(buf, 0, sizeof(buf));
497 print_string(PRINT_FP, NULL, "\tbase-time %s",
498 sprint_time64(base_time, buf));
499 print_lluint(PRINT_JSON, "base_time", NULL, base_time);
500
501 if (tb[TCA_GATE_CYCLE_TIME])
502 cycle_time = rta_getattr_s64(tb[TCA_GATE_CYCLE_TIME]);
503
504 memset(buf, 0, sizeof(buf));
505 print_string(PRINT_FP, NULL,
506 "\tcycle-time %s", sprint_time64(cycle_time, buf));
507 print_lluint(PRINT_JSON, "cycle_time", NULL, cycle_time);
508
509 if (tb[TCA_GATE_CYCLE_TIME_EXT])
510 cycle_time_ext = rta_getattr_s64(tb[TCA_GATE_CYCLE_TIME_EXT]);
511
512 memset(buf, 0, sizeof(buf));
513 print_string(PRINT_FP, NULL, "\tcycle-time-ext %s",
514 sprint_time64(cycle_time_ext, buf));
515 print_lluint(PRINT_JSON, "cycle_time_ext", NULL, cycle_time_ext);
516
517 if (tb[TCA_GATE_ENTRY_LIST])
518 print_gate_list(tb[TCA_GATE_ENTRY_LIST]);
519
520 print_action_control("\t", parm->action, "");
521
522 print_uint(PRINT_ANY, "index", "\n\t index %u", parm->index);
523 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
524 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
525
526 if (show_stats) {
527 if (tb[TCA_GATE_TM]) {
528 struct tcf_t *tm = RTA_DATA(tb[TCA_GATE_TM]);
529
530 print_tm(tm);
531 }
532 }
533
534 print_string(PRINT_FP, NULL, "%s", "\n");
535
536 return 0;
537 }