]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_bpf.c
bpf: minor cleanups for bpf_trace_pipe
[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{
26 fprintf(stderr, "Usage: ... bpf ...\n");
27 fprintf(stderr, "\n");
6256f8c9
DB
28 fprintf(stderr, "BPF use case:\n");
29 fprintf(stderr, " bytecode BPF_BYTECODE\n");
30 fprintf(stderr, " bytecode-file FILE\n");
31 fprintf(stderr, "\n");
32 fprintf(stderr, "eBPF use case:\n");
d937a74b 33 fprintf(stderr, " object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]");
87e46a51
JK
34 fprintf(stderr, " [ verbose ] [ direct-action ] [ skip_hw | skip_sw ]\n");
35 fprintf(stderr, " object-pinned FILE [ direct-action ] [ skip_hw | skip_sw ]\n");
d05df686 36 fprintf(stderr, "\n");
6256f8c9
DB
37 fprintf(stderr, "Common remaining options:\n");
38 fprintf(stderr, " [ action ACTION_SPEC ]\n");
39 fprintf(stderr, " [ classid CLASSID ]\n");
d05df686
DB
40 fprintf(stderr, "\n");
41 fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
6256f8c9
DB
42 fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n");
43 fprintf(stderr, "\n");
11c39b5e 44 fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n");
32e93fb7
DB
45 fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode, or a\n");
46 fprintf(stderr, "pinned eBPF program.\n");
6256f8c9
DB
47 fprintf(stderr, "\n");
48 fprintf(stderr, "Where CLS_NAME refers to the section name containing the\n");
e4225669 49 fprintf(stderr, "classifier (default \'%s\').\n", bpf_prog_to_default_section(bpf_type));
6256f8c9
DB
50 fprintf(stderr, "\n");
51 fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n");
52 fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n");
53 fprintf(stderr, "\n");
54 fprintf(stderr, "ACTION_SPEC := ... look at individual actions\n");
863ecb04 55 fprintf(stderr, "NOTE: CLASSID is parsed as hexadecimal input.\n");
d05df686
DB
56}
57
e4225669
DB
58static void bpf_cbpf_cb(void *nl, const struct sock_filter *ops, int ops_len)
59{
60 addattr16(nl, MAX_MSG, TCA_BPF_OPS_LEN, ops_len);
61 addattr_l(nl, MAX_MSG, TCA_BPF_OPS, ops,
62 ops_len * sizeof(struct sock_filter));
63}
64
65static void bpf_ebpf_cb(void *nl, int fd, const char *annotation)
66{
67 addattr32(nl, MAX_MSG, TCA_BPF_FD, fd);
68 addattrstrz(nl, MAX_MSG, TCA_BPF_NAME, annotation);
69}
70
71static const struct bpf_cfg_ops bpf_cb_ops = {
72 .cbpf_cb = bpf_cbpf_cb,
73 .ebpf_cb = bpf_ebpf_cb,
74};
75
d05df686
DB
76static int bpf_parse_opt(struct filter_util *qu, char *handle,
77 int argc, char **argv, struct nlmsghdr *n)
78{
32e93fb7 79 const char *bpf_obj = NULL, *bpf_uds_name = NULL;
d05df686 80 struct tcmsg *t = NLMSG_DATA(n);
87e46a51 81 unsigned int bpf_gen_flags = 0;
faa8a463 82 unsigned int bpf_flags = 0;
e4225669 83 struct bpf_cfg_in cfg = {};
6256f8c9 84 bool seen_run = false;
32e93fb7 85 struct rtattr *tail;
6256f8c9 86 int ret = 0;
d05df686 87
d05df686 88 if (handle) {
32e93fb7
DB
89 if (get_u32(&t->tcm_handle, handle, 0)) {
90 fprintf(stderr, "Illegal \"handle\"\n");
d05df686
DB
91 return -1;
92 }
93 }
94
1a032072
DB
95 if (argc == 0)
96 return 0;
97
6256f8c9 98 tail = (struct rtattr *)(((void *)n) + NLMSG_ALIGN(n->nlmsg_len));
d05df686
DB
99 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
100
101 while (argc > 0) {
102 if (matches(*argv, "run") == 0) {
d05df686 103 NEXT_ARG();
6256f8c9 104opt_bpf:
6256f8c9 105 seen_run = true;
e4225669
DB
106 cfg.argc = argc;
107 cfg.argv = argv;
108
109 if (bpf_parse_common(bpf_type, &cfg, &bpf_cb_ops, n))
d05df686 110 return -1;
e4225669
DB
111
112 argc = cfg.argc;
113 argv = cfg.argv;
114
115 bpf_obj = cfg.object;
116 bpf_uds_name = cfg.uds;
d05df686 117 } else if (matches(*argv, "classid") == 0 ||
32e93fb7 118 matches(*argv, "flowid") == 0) {
6256f8c9
DB
119 unsigned int handle;
120
d05df686
DB
121 NEXT_ARG();
122 if (get_tc_classid(&handle, *argv)) {
123 fprintf(stderr, "Illegal \"classid\"\n");
124 return -1;
125 }
faa8a463
DB
126 addattr32(n, MAX_MSG, TCA_BPF_CLASSID, handle);
127 } else if (matches(*argv, "direct-action") == 0 ||
128 matches(*argv, "da") == 0) {
129 bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
87e46a51
JK
130 } else if (matches(*argv, "skip_hw") == 0) {
131 bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_HW;
132 } else if (matches(*argv, "skip_sw") == 0) {
133 bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_SW;
d05df686
DB
134 } else if (matches(*argv, "action") == 0) {
135 NEXT_ARG();
136 if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) {
137 fprintf(stderr, "Illegal \"action\"\n");
138 return -1;
139 }
140 continue;
141 } else if (matches(*argv, "police") == 0) {
142 NEXT_ARG();
143 if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) {
144 fprintf(stderr, "Illegal \"police\"\n");
145 return -1;
146 }
147 continue;
32e93fb7 148 } else if (matches(*argv, "help") == 0) {
d05df686
DB
149 explain();
150 return -1;
151 } else {
6256f8c9
DB
152 if (!seen_run)
153 goto opt_bpf;
154
d05df686
DB
155 fprintf(stderr, "What is \"%s\"?\n", *argv);
156 explain();
157 return -1;
158 }
faa8a463
DB
159
160 NEXT_ARG_FWD();
d05df686
DB
161 }
162
87e46a51
JK
163 if (bpf_gen_flags)
164 addattr32(n, MAX_MSG, TCA_BPF_FLAGS_GEN, bpf_gen_flags);
e4225669 165 if (bpf_flags)
faa8a463
DB
166 addattr32(n, MAX_MSG, TCA_BPF_FLAGS, bpf_flags);
167
6256f8c9
DB
168 tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail;
169
170 if (bpf_uds_name)
4bd62446 171 ret = bpf_send_map_fds(bpf_uds_name, bpf_obj);
6256f8c9
DB
172
173 return ret;
d05df686
DB
174}
175
176static int bpf_print_opt(struct filter_util *qu, FILE *f,
177 struct rtattr *opt, __u32 handle)
178{
179 struct rtattr *tb[TCA_BPF_MAX + 1];
180
181 if (opt == NULL)
182 return 0;
183
184 parse_rtattr_nested(tb, TCA_BPF_MAX, opt);
185
186 if (handle)
187 fprintf(f, "handle 0x%x ", handle);
188
189 if (tb[TCA_BPF_CLASSID]) {
190 SPRINT_BUF(b1);
191 fprintf(f, "flowid %s ",
192 sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1));
193 }
194
11c39b5e
DB
195 if (tb[TCA_BPF_NAME])
196 fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME]));
11c39b5e 197
faa8a463
DB
198 if (tb[TCA_BPF_FLAGS]) {
199 unsigned int flags = rta_getattr_u32(tb[TCA_BPF_FLAGS]);
200
201 if (flags & TCA_BPF_FLAG_ACT_DIRECT)
202 fprintf(f, "direct-action ");
203 }
204
87e46a51
JK
205 if (tb[TCA_BPF_FLAGS_GEN]) {
206 unsigned int flags =
207 rta_getattr_u32(tb[TCA_BPF_FLAGS_GEN]);
208
209 if (flags & TCA_CLS_FLAGS_SKIP_HW)
210 fprintf(f, "skip_hw ");
211 if (flags & TCA_CLS_FLAGS_SKIP_SW)
212 fprintf(f, "skip_sw ");
e57285b8
OG
213
214 if (flags & TCA_CLS_FLAGS_IN_HW)
215 fprintf(f, "in_hw ");
216 else if (flags & TCA_CLS_FLAGS_NOT_IN_HW)
217 fprintf(f, "not_in_hw ");
87e46a51
JK
218 }
219
e4225669 220 if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN])
d05df686
DB
221 bpf_print_ops(f, tb[TCA_BPF_OPS],
222 rta_getattr_u16(tb[TCA_BPF_OPS_LEN]));
223
e37d706b
DB
224 if (tb[TCA_BPF_TAG]) {
225 SPRINT_BUF(b);
226
227 fprintf(f, "tag %s ",
228 hexstring_n2a(RTA_DATA(tb[TCA_BPF_TAG]),
229 RTA_PAYLOAD(tb[TCA_BPF_TAG]),
230 b, sizeof(b)));
231 }
232
779525cd
DB
233 if (tb[TCA_BPF_ID])
234 bpf_dump_prog_info(f, rta_getattr_u32(tb[TCA_BPF_ID]));
235
d05df686
DB
236 if (tb[TCA_BPF_POLICE]) {
237 fprintf(f, "\n");
238 tc_print_police(f, tb[TCA_BPF_POLICE]);
239 }
240
e4225669 241 if (tb[TCA_BPF_ACT])
d05df686 242 tc_print_action(f, tb[TCA_BPF_ACT]);
d05df686
DB
243
244 return 0;
245}
246
247struct filter_util bpf_filter_util = {
32e93fb7
DB
248 .id = "bpf",
249 .parse_fopt = bpf_parse_opt,
250 .print_fopt = bpf_print_opt,
d05df686 251};