| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* f_cgroup.c Control Group Classifier |
| 4 |
* |
| 5 |
* Authors: Thomas Graf <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include "utils.h" |
| 11 |
#include "tc_util.h" |
| 12 |
#include "m_ematch.h" |
| 13 |
|
| 14 |
static void explain(void) |
| 15 |
{ |
| 16 |
fprintf(stderr, "Usage: ... cgroup [ match EMATCH_TREE ]\n"); |
| 17 |
fprintf(stderr, " [ action ACTION_SPEC ]\n"); |
| 18 |
} |
| 19 |
|
| 20 |
static int cgroup_parse_opt(const struct filter_util *qu, char *handle, |
| 21 |
int argc, char **argv, struct nlmsghdr *n) |
| 22 |
{ |
| 23 |
struct tcmsg *t = NLMSG_DATA(n); |
| 24 |
struct rtattr *tail; |
| 25 |
long h = 0; |
| 26 |
|
| 27 |
if (handle) { |
| 28 |
h = strtol(handle, NULL, 0); |
| 29 |
if (h == LONG_MIN || h == LONG_MAX) { |
| 30 |
fprintf(stderr, "Illegal handle \"%s\", must be numeric.\n", |
| 31 |
handle); |
| 32 |
return -1; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
t->tcm_handle = h; |
| 37 |
|
| 38 |
tail = (struct rtattr *)(((void *)n)+NLMSG_ALIGN(n->nlmsg_len)); |
| 39 |
addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); |
| 40 |
|
| 41 |
while (argc > 0) { |
| 42 |
if (matches(*argv, "match") == 0) { |
| 43 |
NEXT_ARG(); |
| 44 |
if (parse_ematch(&argc, &argv, TCA_CGROUP_EMATCHES, n)) { |
| 45 |
fprintf(stderr, "Illegal \"ematch\"\n"); |
| 46 |
return -1; |
| 47 |
} |
| 48 |
continue; |
| 49 |
} else if (matches(*argv, "action") == 0) { |
| 50 |
NEXT_ARG(); |
| 51 |
if (parse_action(&argc, &argv, TCA_CGROUP_ACT, n)) { |
| 52 |
fprintf(stderr, "Illegal \"action\"\n"); |
| 53 |
return -1; |
| 54 |
} |
| 55 |
continue; |
| 56 |
|
| 57 |
} else if (matches(*argv, "police") == 0) { |
| 58 |
NEXT_ARG(); |
| 59 |
if (parse_police(&argc, &argv, TCA_CGROUP_POLICE, n)) { |
| 60 |
fprintf(stderr, "Illegal \"police\"\n"); |
| 61 |
return -1; |
| 62 |
} |
| 63 |
continue; |
| 64 |
} else if (strcmp(*argv, "help") == 0) { |
| 65 |
explain(); |
| 66 |
return -1; |
| 67 |
} else { |
| 68 |
fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 69 |
explain(); |
| 70 |
return -1; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail; |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static int cgroup_print_opt(const struct filter_util *qu, FILE *f, |
| 79 |
struct rtattr *opt, __u32 handle) |
| 80 |
{ |
| 81 |
struct rtattr *tb[TCA_CGROUP_MAX+1]; |
| 82 |
|
| 83 |
if (opt == NULL) |
| 84 |
return 0; |
| 85 |
|
| 86 |
parse_rtattr_nested(tb, TCA_CGROUP_MAX, opt); |
| 87 |
|
| 88 |
if (handle) |
| 89 |
print_0xhex(PRINT_ANY, "handle", "handle %#llx ", handle); |
| 90 |
|
| 91 |
if (tb[TCA_CGROUP_EMATCHES]) |
| 92 |
print_ematch(f, tb[TCA_CGROUP_EMATCHES]); |
| 93 |
|
| 94 |
if (tb[TCA_CGROUP_POLICE]) { |
| 95 |
print_nl(); |
| 96 |
tc_print_police(tb[TCA_CGROUP_POLICE]); |
| 97 |
} |
| 98 |
|
| 99 |
if (tb[TCA_CGROUP_ACT]) |
| 100 |
tc_print_action(f, tb[TCA_CGROUP_ACT], 0); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
struct filter_util cgroup_filter_util = { |
| 106 |
.id = "cgroup", |
| 107 |
.parse_fopt = cgroup_parse_opt, |
| 108 |
.print_fopt = cgroup_print_opt, |
| 109 |
}; |