Debian Codegraph @

tc/f_matchall.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * f_matchall.c Match-all Classifier
4 *
5 * Authors: Jiri Pirko <[email protected]>, Yotam Gigi <[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 #include <linux/if.h>
17
18 #include "utils.h"
19 #include "tc_util.h"
20
21 static void explain(void)
22 {
23 fprintf(stderr,
24 "Usage: ... matchall [skip_sw | skip_hw]\n"
25 " [ action ACTION_SPEC ] [ classid CLASSID ]\n"
26 "\n"
27 "Where: SELECTOR := SAMPLE SAMPLE ...\n"
28 " FILTERID := X:Y:Z\n"
29 " ACTION_SPEC := ... look at individual actions\n"
30 "\n"
31 "NOTE: CLASSID is parsed as hexadecimal input.\n");
32 }
33
34 static int matchall_parse_opt(const struct filter_util *qu, char *handle,
35 int argc, char **argv, struct nlmsghdr *n)
36 {
37 struct tcmsg *t = NLMSG_DATA(n);
38 struct rtattr *tail;
39 __u32 flags = 0;
40 long h = 0;
41
42 if (handle) {
43 h = strtol(handle, NULL, 0);
44 if (h == LONG_MIN || h == LONG_MAX) {
45 fprintf(stderr, "Illegal handle \"%s\", must be numeric.\n",
46 handle);
47 return -1;
48 }
49 }
50 t->tcm_handle = h;
51
52 if (argc == 0)
53 return 0;
54
55 tail = (struct rtattr *)(((void *)n)+NLMSG_ALIGN(n->nlmsg_len));
56 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
57
58 while (argc > 0) {
59 if (matches(*argv, "classid") == 0 ||
60 strcmp(*argv, "flowid") == 0) {
61 unsigned int classid;
62
63 NEXT_ARG();
64 if (get_tc_classid(&classid, *argv)) {
65 fprintf(stderr, "Illegal \"classid\"\n");
66 return -1;
67 }
68 addattr_l(n, MAX_MSG, TCA_MATCHALL_CLASSID, &classid, 4);
69 } else if (matches(*argv, "action") == 0) {
70 NEXT_ARG();
71 if (parse_action(&argc, &argv, TCA_MATCHALL_ACT, n)) {
72 fprintf(stderr, "Illegal \"action\"\n");
73 return -1;
74 }
75 continue;
76
77 } else if (strcmp(*argv, "skip_hw") == 0) {
78 NEXT_ARG();
79 flags |= TCA_CLS_FLAGS_SKIP_HW;
80 continue;
81 } else if (strcmp(*argv, "skip_sw") == 0) {
82 NEXT_ARG();
83 flags |= TCA_CLS_FLAGS_SKIP_SW;
84 continue;
85 } else if (strcmp(*argv, "help") == 0) {
86 explain();
87 return -1;
88 } else {
89 fprintf(stderr, "What is \"%s\"?\n", *argv);
90 explain();
91 return -1;
92 }
93 argc--; argv++;
94 }
95
96 if (flags) {
97 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW |
98 TCA_CLS_FLAGS_SKIP_SW))) {
99 fprintf(stderr,
100 "skip_hw and skip_sw are mutually exclusive\n");
101 return -1;
102 }
103 addattr_l(n, MAX_MSG, TCA_MATCHALL_FLAGS, &flags, 4);
104 }
105
106 tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail;
107 return 0;
108 }
109
110 static int matchall_print_opt(const struct filter_util *qu, FILE *f,
111 struct rtattr *opt, __u32 handle)
112 {
113 struct rtattr *tb[TCA_MATCHALL_MAX+1];
114 struct tc_matchall_pcnt *pf = NULL;
115
116 if (opt == NULL)
117 return 0;
118
119 parse_rtattr_nested(tb, TCA_MATCHALL_MAX, opt);
120
121 if (handle)
122 print_uint(PRINT_ANY, "handle", "handle 0x%x ", handle);
123
124 if (tb[TCA_MATCHALL_CLASSID]) {
125 SPRINT_BUF(b1);
126 print_string(PRINT_ANY, "flowid", "flowid %s ",
127 sprint_tc_classid(rta_getattr_u32(tb[TCA_MATCHALL_CLASSID]), b1));
128 }
129
130 if (tb[TCA_MATCHALL_FLAGS]) {
131 __u32 flags = rta_getattr_u32(tb[TCA_MATCHALL_FLAGS]);
132
133 if (flags & TCA_CLS_FLAGS_SKIP_HW)
134 print_bool(PRINT_ANY, "skip_hw", "\n skip_hw", true);
135 if (flags & TCA_CLS_FLAGS_SKIP_SW)
136 print_bool(PRINT_ANY, "skip_sw", "\n skip_sw", true);
137
138 if (flags & TCA_CLS_FLAGS_IN_HW)
139 print_bool(PRINT_ANY, "in_hw", "\n in_hw", true);
140 else if (flags & TCA_CLS_FLAGS_NOT_IN_HW)
141 print_bool(PRINT_ANY, "not_in_hw", "\n not_in_hw", true);
142 }
143
144 if (tb[TCA_MATCHALL_PCNT]) {
145 if (RTA_PAYLOAD(tb[TCA_MATCHALL_PCNT]) < sizeof(*pf)) {
146 print_string(PRINT_FP, NULL, "Broken perf counters\n", NULL);
147 return -1;
148 }
149 pf = RTA_DATA(tb[TCA_MATCHALL_PCNT]);
150 }
151
152 if (show_stats && NULL != pf)
153 print_u64(PRINT_ANY, "rule_hit", " (rule hit %llu)",
154 (unsigned long long) pf->rhit);
155
156
157 if (tb[TCA_MATCHALL_ACT])
158 tc_print_action(f, tb[TCA_MATCHALL_ACT], 0);
159
160 return 0;
161 }
162
163 struct filter_util matchall_filter_util = {
164 .id = "matchall",
165 .parse_fopt = matchall_parse_opt,
166 .print_fopt = matchall_print_opt,
167 };