]>
Commit | Line | Data |
---|---|---|
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 |
22 | static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_CLS; |
23 | ||
d05df686 DB |
24 | static 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 |
58 | static 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 | ||
65 | static 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 | ||
71 | static const struct bpf_cfg_ops bpf_cb_ops = { | |
72 | .cbpf_cb = bpf_cbpf_cb, | |
73 | .ebpf_cb = bpf_ebpf_cb, | |
74 | }; | |
75 | ||
d05df686 DB |
76 | static 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; |
eb91c557 | 85 | bool skip_sw = false; |
32e93fb7 | 86 | struct rtattr *tail; |
6256f8c9 | 87 | int ret = 0; |
d05df686 | 88 | |
d05df686 | 89 | if (handle) { |
32e93fb7 DB |
90 | if (get_u32(&t->tcm_handle, handle, 0)) { |
91 | fprintf(stderr, "Illegal \"handle\"\n"); | |
d05df686 DB |
92 | return -1; |
93 | } | |
94 | } | |
95 | ||
1a032072 DB |
96 | if (argc == 0) |
97 | return 0; | |
98 | ||
6256f8c9 | 99 | tail = (struct rtattr *)(((void *)n) + NLMSG_ALIGN(n->nlmsg_len)); |
d05df686 DB |
100 | addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); |
101 | ||
102 | while (argc > 0) { | |
103 | if (matches(*argv, "run") == 0) { | |
d05df686 | 104 | NEXT_ARG(); |
67c857df JK |
105 | |
106 | if (seen_run) | |
107 | duparg("run", *argv); | |
6256f8c9 | 108 | opt_bpf: |
6256f8c9 | 109 | seen_run = true; |
658cfebc | 110 | cfg.type = bpf_type; |
e4225669 DB |
111 | cfg.argc = argc; |
112 | cfg.argv = argv; | |
113 | ||
eb91c557 JK |
114 | if (bpf_parse_common(&cfg, &bpf_cb_ops) < 0) { |
115 | fprintf(stderr, | |
116 | "Unable to parse bpf command line\n"); | |
d05df686 | 117 | return -1; |
eb91c557 | 118 | } |
e4225669 DB |
119 | |
120 | argc = cfg.argc; | |
121 | argv = cfg.argv; | |
122 | ||
123 | bpf_obj = cfg.object; | |
124 | bpf_uds_name = cfg.uds; | |
d05df686 | 125 | } else if (matches(*argv, "classid") == 0 || |
32e93fb7 | 126 | matches(*argv, "flowid") == 0) { |
6256f8c9 DB |
127 | unsigned int handle; |
128 | ||
d05df686 DB |
129 | NEXT_ARG(); |
130 | if (get_tc_classid(&handle, *argv)) { | |
131 | fprintf(stderr, "Illegal \"classid\"\n"); | |
132 | return -1; | |
133 | } | |
faa8a463 DB |
134 | addattr32(n, MAX_MSG, TCA_BPF_CLASSID, handle); |
135 | } else if (matches(*argv, "direct-action") == 0 || | |
136 | matches(*argv, "da") == 0) { | |
137 | bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; | |
87e46a51 JK |
138 | } else if (matches(*argv, "skip_hw") == 0) { |
139 | bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_HW; | |
140 | } else if (matches(*argv, "skip_sw") == 0) { | |
141 | bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_SW; | |
eb91c557 | 142 | skip_sw = true; |
d05df686 DB |
143 | } else if (matches(*argv, "action") == 0) { |
144 | NEXT_ARG(); | |
145 | if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) { | |
146 | fprintf(stderr, "Illegal \"action\"\n"); | |
147 | return -1; | |
148 | } | |
149 | continue; | |
150 | } else if (matches(*argv, "police") == 0) { | |
151 | NEXT_ARG(); | |
152 | if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) { | |
153 | fprintf(stderr, "Illegal \"police\"\n"); | |
154 | return -1; | |
155 | } | |
156 | continue; | |
32e93fb7 | 157 | } else if (matches(*argv, "help") == 0) { |
d05df686 DB |
158 | explain(); |
159 | return -1; | |
160 | } else { | |
6256f8c9 DB |
161 | if (!seen_run) |
162 | goto opt_bpf; | |
163 | ||
d05df686 DB |
164 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
165 | explain(); | |
166 | return -1; | |
167 | } | |
faa8a463 DB |
168 | |
169 | NEXT_ARG_FWD(); | |
d05df686 DB |
170 | } |
171 | ||
eb91c557 JK |
172 | if (skip_sw) |
173 | cfg.ifindex = t->tcm_ifindex; | |
174 | if (bpf_load_common(&cfg, &bpf_cb_ops, n) < 0) { | |
175 | fprintf(stderr, "Unable to load program\n"); | |
176 | return -1; | |
177 | } | |
178 | ||
87e46a51 JK |
179 | if (bpf_gen_flags) |
180 | addattr32(n, MAX_MSG, TCA_BPF_FLAGS_GEN, bpf_gen_flags); | |
e4225669 | 181 | if (bpf_flags) |
faa8a463 DB |
182 | addattr32(n, MAX_MSG, TCA_BPF_FLAGS, bpf_flags); |
183 | ||
6256f8c9 DB |
184 | tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail; |
185 | ||
186 | if (bpf_uds_name) | |
4bd62446 | 187 | ret = bpf_send_map_fds(bpf_uds_name, bpf_obj); |
6256f8c9 DB |
188 | |
189 | return ret; | |
d05df686 DB |
190 | } |
191 | ||
192 | static int bpf_print_opt(struct filter_util *qu, FILE *f, | |
193 | struct rtattr *opt, __u32 handle) | |
194 | { | |
195 | struct rtattr *tb[TCA_BPF_MAX + 1]; | |
a0b5b7cf | 196 | int dump_ok = 0; |
d05df686 DB |
197 | |
198 | if (opt == NULL) | |
199 | return 0; | |
200 | ||
201 | parse_rtattr_nested(tb, TCA_BPF_MAX, opt); | |
202 | ||
203 | if (handle) | |
204 | fprintf(f, "handle 0x%x ", handle); | |
205 | ||
206 | if (tb[TCA_BPF_CLASSID]) { | |
207 | SPRINT_BUF(b1); | |
208 | fprintf(f, "flowid %s ", | |
209 | sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1)); | |
210 | } | |
211 | ||
11c39b5e DB |
212 | if (tb[TCA_BPF_NAME]) |
213 | fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME])); | |
11c39b5e | 214 | |
faa8a463 DB |
215 | if (tb[TCA_BPF_FLAGS]) { |
216 | unsigned int flags = rta_getattr_u32(tb[TCA_BPF_FLAGS]); | |
217 | ||
218 | if (flags & TCA_BPF_FLAG_ACT_DIRECT) | |
219 | fprintf(f, "direct-action "); | |
220 | } | |
221 | ||
87e46a51 JK |
222 | if (tb[TCA_BPF_FLAGS_GEN]) { |
223 | unsigned int flags = | |
224 | rta_getattr_u32(tb[TCA_BPF_FLAGS_GEN]); | |
225 | ||
226 | if (flags & TCA_CLS_FLAGS_SKIP_HW) | |
227 | fprintf(f, "skip_hw "); | |
228 | if (flags & TCA_CLS_FLAGS_SKIP_SW) | |
229 | fprintf(f, "skip_sw "); | |
e57285b8 OG |
230 | |
231 | if (flags & TCA_CLS_FLAGS_IN_HW) | |
232 | fprintf(f, "in_hw "); | |
233 | else if (flags & TCA_CLS_FLAGS_NOT_IN_HW) | |
234 | fprintf(f, "not_in_hw "); | |
87e46a51 JK |
235 | } |
236 | ||
e4225669 | 237 | if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN]) |
d05df686 DB |
238 | bpf_print_ops(f, tb[TCA_BPF_OPS], |
239 | rta_getattr_u16(tb[TCA_BPF_OPS_LEN])); | |
240 | ||
a0b5b7cf DB |
241 | if (tb[TCA_BPF_ID]) |
242 | dump_ok = bpf_dump_prog_info(f, rta_getattr_u32(tb[TCA_BPF_ID])); | |
243 | if (!dump_ok && tb[TCA_BPF_TAG]) { | |
e37d706b DB |
244 | SPRINT_BUF(b); |
245 | ||
246 | fprintf(f, "tag %s ", | |
247 | hexstring_n2a(RTA_DATA(tb[TCA_BPF_TAG]), | |
248 | RTA_PAYLOAD(tb[TCA_BPF_TAG]), | |
249 | b, sizeof(b))); | |
250 | } | |
251 | ||
d05df686 DB |
252 | if (tb[TCA_BPF_POLICE]) { |
253 | fprintf(f, "\n"); | |
254 | tc_print_police(f, tb[TCA_BPF_POLICE]); | |
255 | } | |
256 | ||
e4225669 | 257 | if (tb[TCA_BPF_ACT]) |
9e713525 | 258 | tc_print_action(f, tb[TCA_BPF_ACT], 0); |
d05df686 DB |
259 | |
260 | return 0; | |
261 | } | |
262 | ||
263 | struct filter_util bpf_filter_util = { | |
32e93fb7 DB |
264 | .id = "bpf", |
265 | .parse_fopt = bpf_parse_opt, | |
266 | .print_fopt = bpf_print_opt, | |
d05df686 | 267 | }; |