]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_bpf.c
treewide: refactor help messages
[mirror_iproute2.git] / tc / f_bpf.c
CommitLineData
d05df686
DB
1/*
2 * f_bpf.c BPF-based Classifier
3 *
4 * This program is free software; you can distribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
e4225669 9 * Authors: Daniel Borkmann <daniel@iogearbox.net>
d05df686
DB
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
32e93fb7
DB
14
15#include <linux/bpf.h>
d05df686
DB
16
17#include "utils.h"
e4225669 18
d05df686 19#include "tc_util.h"
e4225669 20#include "bpf_util.h"
d05df686 21
6256f8c9
DB
22static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_CLS;
23
d05df686
DB
24static void explain(void)
25{
8589eb4e
MC
26 fprintf(stderr,
27 "Usage: ... bpf ...\n"
28 "\n"
29 "BPF use case:\n"
30 " bytecode BPF_BYTECODE\n"
31 " bytecode-file FILE\n"
32 "\n"
33 "eBPF use case:\n"
34 " object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]"
35 " [ verbose ] [ direct-action ] [ skip_hw | skip_sw ]\n"
36 " object-pinned FILE [ direct-action ] [ skip_hw | skip_sw ]\n"
37 "\n"
38 "Common remaining options:\n"
39 " [ action ACTION_SPEC ]\n"
40 " [ classid CLASSID ]\n"
41 "\n"
42 "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n"
43 "c,t,f,k and s are decimals; s denotes number of 4-tuples\n"
44 "\n"
45 "Where FILE points to a file containing the BPF_BYTECODE string,\n"
46 "an ELF file containing eBPF map definitions and bytecode, or a\n"
47 "pinned eBPF program.\n"
48 "\n"
49 "Where CLS_NAME refers to the section name containing the\n"
50 "classifier (default \'%s\').\n"
51 "\n"
52 "Where UDS_FILE points to a unix domain socket file in order\n"
53 "to hand off control of all created eBPF maps to an agent.\n"
54 "\n"
55 "ACTION_SPEC := ... look at individual actions\n"
56 "NOTE: CLASSID is parsed as hexadecimal input.\n",
57 bpf_prog_to_default_section(bpf_type));
d05df686
DB
58}
59
e4225669
DB
60static void bpf_cbpf_cb(void *nl, const struct sock_filter *ops, int ops_len)
61{
62 addattr16(nl, MAX_MSG, TCA_BPF_OPS_LEN, ops_len);
63 addattr_l(nl, MAX_MSG, TCA_BPF_OPS, ops,
64 ops_len * sizeof(struct sock_filter));
65}
66
67static void bpf_ebpf_cb(void *nl, int fd, const char *annotation)
68{
69 addattr32(nl, MAX_MSG, TCA_BPF_FD, fd);
70 addattrstrz(nl, MAX_MSG, TCA_BPF_NAME, annotation);
71}
72
73static const struct bpf_cfg_ops bpf_cb_ops = {
74 .cbpf_cb = bpf_cbpf_cb,
75 .ebpf_cb = bpf_ebpf_cb,
76};
77
d05df686
DB
78static int bpf_parse_opt(struct filter_util *qu, char *handle,
79 int argc, char **argv, struct nlmsghdr *n)
80{
32e93fb7 81 const char *bpf_obj = NULL, *bpf_uds_name = NULL;
d05df686 82 struct tcmsg *t = NLMSG_DATA(n);
87e46a51 83 unsigned int bpf_gen_flags = 0;
faa8a463 84 unsigned int bpf_flags = 0;
e4225669 85 struct bpf_cfg_in cfg = {};
6256f8c9 86 bool seen_run = false;
eb91c557 87 bool skip_sw = false;
32e93fb7 88 struct rtattr *tail;
6256f8c9 89 int ret = 0;
d05df686 90
d05df686 91 if (handle) {
32e93fb7
DB
92 if (get_u32(&t->tcm_handle, handle, 0)) {
93 fprintf(stderr, "Illegal \"handle\"\n");
d05df686
DB
94 return -1;
95 }
96 }
97
1a032072
DB
98 if (argc == 0)
99 return 0;
100
6256f8c9 101 tail = (struct rtattr *)(((void *)n) + NLMSG_ALIGN(n->nlmsg_len));
d05df686
DB
102 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
103
104 while (argc > 0) {
105 if (matches(*argv, "run") == 0) {
d05df686 106 NEXT_ARG();
67c857df
JK
107
108 if (seen_run)
109 duparg("run", *argv);
6256f8c9 110opt_bpf:
6256f8c9 111 seen_run = true;
658cfebc 112 cfg.type = bpf_type;
e4225669
DB
113 cfg.argc = argc;
114 cfg.argv = argv;
115
eb91c557
JK
116 if (bpf_parse_common(&cfg, &bpf_cb_ops) < 0) {
117 fprintf(stderr,
118 "Unable to parse bpf command line\n");
d05df686 119 return -1;
eb91c557 120 }
e4225669
DB
121
122 argc = cfg.argc;
123 argv = cfg.argv;
124
125 bpf_obj = cfg.object;
126 bpf_uds_name = cfg.uds;
d05df686 127 } else if (matches(*argv, "classid") == 0 ||
32e93fb7 128 matches(*argv, "flowid") == 0) {
6256f8c9
DB
129 unsigned int handle;
130
d05df686
DB
131 NEXT_ARG();
132 if (get_tc_classid(&handle, *argv)) {
133 fprintf(stderr, "Illegal \"classid\"\n");
134 return -1;
135 }
faa8a463
DB
136 addattr32(n, MAX_MSG, TCA_BPF_CLASSID, handle);
137 } else if (matches(*argv, "direct-action") == 0 ||
138 matches(*argv, "da") == 0) {
139 bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
87e46a51
JK
140 } else if (matches(*argv, "skip_hw") == 0) {
141 bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_HW;
142 } else if (matches(*argv, "skip_sw") == 0) {
143 bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_SW;
eb91c557 144 skip_sw = true;
d05df686
DB
145 } else if (matches(*argv, "action") == 0) {
146 NEXT_ARG();
147 if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) {
148 fprintf(stderr, "Illegal \"action\"\n");
149 return -1;
150 }
151 continue;
152 } else if (matches(*argv, "police") == 0) {
153 NEXT_ARG();
154 if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) {
155 fprintf(stderr, "Illegal \"police\"\n");
156 return -1;
157 }
158 continue;
32e93fb7 159 } else if (matches(*argv, "help") == 0) {
d05df686
DB
160 explain();
161 return -1;
162 } else {
6256f8c9
DB
163 if (!seen_run)
164 goto opt_bpf;
165
d05df686
DB
166 fprintf(stderr, "What is \"%s\"?\n", *argv);
167 explain();
168 return -1;
169 }
faa8a463
DB
170
171 NEXT_ARG_FWD();
d05df686
DB
172 }
173
eb91c557
JK
174 if (skip_sw)
175 cfg.ifindex = t->tcm_ifindex;
176 if (bpf_load_common(&cfg, &bpf_cb_ops, n) < 0) {
177 fprintf(stderr, "Unable to load program\n");
178 return -1;
179 }
180
87e46a51
JK
181 if (bpf_gen_flags)
182 addattr32(n, MAX_MSG, TCA_BPF_FLAGS_GEN, bpf_gen_flags);
e4225669 183 if (bpf_flags)
faa8a463
DB
184 addattr32(n, MAX_MSG, TCA_BPF_FLAGS, bpf_flags);
185
6256f8c9
DB
186 tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail;
187
188 if (bpf_uds_name)
4bd62446 189 ret = bpf_send_map_fds(bpf_uds_name, bpf_obj);
6256f8c9
DB
190
191 return ret;
d05df686
DB
192}
193
194static int bpf_print_opt(struct filter_util *qu, FILE *f,
195 struct rtattr *opt, __u32 handle)
196{
197 struct rtattr *tb[TCA_BPF_MAX + 1];
a0b5b7cf 198 int dump_ok = 0;
d05df686
DB
199
200 if (opt == NULL)
201 return 0;
202
203 parse_rtattr_nested(tb, TCA_BPF_MAX, opt);
204
205 if (handle)
206 fprintf(f, "handle 0x%x ", handle);
207
208 if (tb[TCA_BPF_CLASSID]) {
209 SPRINT_BUF(b1);
210 fprintf(f, "flowid %s ",
211 sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1));
212 }
213
11c39b5e
DB
214 if (tb[TCA_BPF_NAME])
215 fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME]));
11c39b5e 216
faa8a463
DB
217 if (tb[TCA_BPF_FLAGS]) {
218 unsigned int flags = rta_getattr_u32(tb[TCA_BPF_FLAGS]);
219
220 if (flags & TCA_BPF_FLAG_ACT_DIRECT)
221 fprintf(f, "direct-action ");
222 }
223
87e46a51
JK
224 if (tb[TCA_BPF_FLAGS_GEN]) {
225 unsigned int flags =
226 rta_getattr_u32(tb[TCA_BPF_FLAGS_GEN]);
227
228 if (flags & TCA_CLS_FLAGS_SKIP_HW)
229 fprintf(f, "skip_hw ");
230 if (flags & TCA_CLS_FLAGS_SKIP_SW)
231 fprintf(f, "skip_sw ");
e57285b8
OG
232
233 if (flags & TCA_CLS_FLAGS_IN_HW)
234 fprintf(f, "in_hw ");
235 else if (flags & TCA_CLS_FLAGS_NOT_IN_HW)
236 fprintf(f, "not_in_hw ");
87e46a51
JK
237 }
238
e4225669 239 if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN])
52d57f6b 240 bpf_print_ops(tb[TCA_BPF_OPS],
d05df686
DB
241 rta_getattr_u16(tb[TCA_BPF_OPS_LEN]));
242
a0b5b7cf
DB
243 if (tb[TCA_BPF_ID])
244 dump_ok = bpf_dump_prog_info(f, rta_getattr_u32(tb[TCA_BPF_ID]));
245 if (!dump_ok && tb[TCA_BPF_TAG]) {
e37d706b
DB
246 SPRINT_BUF(b);
247
248 fprintf(f, "tag %s ",
249 hexstring_n2a(RTA_DATA(tb[TCA_BPF_TAG]),
250 RTA_PAYLOAD(tb[TCA_BPF_TAG]),
251 b, sizeof(b)));
252 }
253
d05df686
DB
254 if (tb[TCA_BPF_POLICE]) {
255 fprintf(f, "\n");
256 tc_print_police(f, tb[TCA_BPF_POLICE]);
257 }
258
e4225669 259 if (tb[TCA_BPF_ACT])
9e713525 260 tc_print_action(f, tb[TCA_BPF_ACT], 0);
d05df686
DB
261
262 return 0;
263}
264
265struct filter_util bpf_filter_util = {
32e93fb7
DB
266 .id = "bpf",
267 .parse_fopt = bpf_parse_opt,
268 .print_fopt = bpf_print_opt,
d05df686 269};