]>
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; |
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 | 104 | opt_bpf: |
6256f8c9 | 105 | seen_run = true; |
658cfebc | 106 | cfg.type = bpf_type; |
e4225669 DB |
107 | cfg.argc = argc; |
108 | cfg.argv = argv; | |
109 | ||
399db839 | 110 | if (bpf_parse_and_load_common(&cfg, &bpf_cb_ops, n)) |
d05df686 | 111 | return -1; |
e4225669 DB |
112 | |
113 | argc = cfg.argc; | |
114 | argv = cfg.argv; | |
115 | ||
116 | bpf_obj = cfg.object; | |
117 | bpf_uds_name = cfg.uds; | |
d05df686 | 118 | } else if (matches(*argv, "classid") == 0 || |
32e93fb7 | 119 | matches(*argv, "flowid") == 0) { |
6256f8c9 DB |
120 | unsigned int handle; |
121 | ||
d05df686 DB |
122 | NEXT_ARG(); |
123 | if (get_tc_classid(&handle, *argv)) { | |
124 | fprintf(stderr, "Illegal \"classid\"\n"); | |
125 | return -1; | |
126 | } | |
faa8a463 DB |
127 | addattr32(n, MAX_MSG, TCA_BPF_CLASSID, handle); |
128 | } else if (matches(*argv, "direct-action") == 0 || | |
129 | matches(*argv, "da") == 0) { | |
130 | bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; | |
87e46a51 JK |
131 | } else if (matches(*argv, "skip_hw") == 0) { |
132 | bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_HW; | |
133 | } else if (matches(*argv, "skip_sw") == 0) { | |
134 | bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_SW; | |
d05df686 DB |
135 | } else if (matches(*argv, "action") == 0) { |
136 | NEXT_ARG(); | |
137 | if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) { | |
138 | fprintf(stderr, "Illegal \"action\"\n"); | |
139 | return -1; | |
140 | } | |
141 | continue; | |
142 | } else if (matches(*argv, "police") == 0) { | |
143 | NEXT_ARG(); | |
144 | if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) { | |
145 | fprintf(stderr, "Illegal \"police\"\n"); | |
146 | return -1; | |
147 | } | |
148 | continue; | |
32e93fb7 | 149 | } else if (matches(*argv, "help") == 0) { |
d05df686 DB |
150 | explain(); |
151 | return -1; | |
152 | } else { | |
6256f8c9 DB |
153 | if (!seen_run) |
154 | goto opt_bpf; | |
155 | ||
d05df686 DB |
156 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
157 | explain(); | |
158 | return -1; | |
159 | } | |
faa8a463 DB |
160 | |
161 | NEXT_ARG_FWD(); | |
d05df686 DB |
162 | } |
163 | ||
87e46a51 JK |
164 | if (bpf_gen_flags) |
165 | addattr32(n, MAX_MSG, TCA_BPF_FLAGS_GEN, bpf_gen_flags); | |
e4225669 | 166 | if (bpf_flags) |
faa8a463 DB |
167 | addattr32(n, MAX_MSG, TCA_BPF_FLAGS, bpf_flags); |
168 | ||
6256f8c9 DB |
169 | tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail; |
170 | ||
171 | if (bpf_uds_name) | |
4bd62446 | 172 | ret = bpf_send_map_fds(bpf_uds_name, bpf_obj); |
6256f8c9 DB |
173 | |
174 | return ret; | |
d05df686 DB |
175 | } |
176 | ||
177 | static int bpf_print_opt(struct filter_util *qu, FILE *f, | |
178 | struct rtattr *opt, __u32 handle) | |
179 | { | |
180 | struct rtattr *tb[TCA_BPF_MAX + 1]; | |
a0b5b7cf | 181 | int dump_ok = 0; |
d05df686 DB |
182 | |
183 | if (opt == NULL) | |
184 | return 0; | |
185 | ||
186 | parse_rtattr_nested(tb, TCA_BPF_MAX, opt); | |
187 | ||
188 | if (handle) | |
189 | fprintf(f, "handle 0x%x ", handle); | |
190 | ||
191 | if (tb[TCA_BPF_CLASSID]) { | |
192 | SPRINT_BUF(b1); | |
193 | fprintf(f, "flowid %s ", | |
194 | sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1)); | |
195 | } | |
196 | ||
11c39b5e DB |
197 | if (tb[TCA_BPF_NAME]) |
198 | fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME])); | |
11c39b5e | 199 | |
faa8a463 DB |
200 | if (tb[TCA_BPF_FLAGS]) { |
201 | unsigned int flags = rta_getattr_u32(tb[TCA_BPF_FLAGS]); | |
202 | ||
203 | if (flags & TCA_BPF_FLAG_ACT_DIRECT) | |
204 | fprintf(f, "direct-action "); | |
205 | } | |
206 | ||
87e46a51 JK |
207 | if (tb[TCA_BPF_FLAGS_GEN]) { |
208 | unsigned int flags = | |
209 | rta_getattr_u32(tb[TCA_BPF_FLAGS_GEN]); | |
210 | ||
211 | if (flags & TCA_CLS_FLAGS_SKIP_HW) | |
212 | fprintf(f, "skip_hw "); | |
213 | if (flags & TCA_CLS_FLAGS_SKIP_SW) | |
214 | fprintf(f, "skip_sw "); | |
e57285b8 OG |
215 | |
216 | if (flags & TCA_CLS_FLAGS_IN_HW) | |
217 | fprintf(f, "in_hw "); | |
218 | else if (flags & TCA_CLS_FLAGS_NOT_IN_HW) | |
219 | fprintf(f, "not_in_hw "); | |
87e46a51 JK |
220 | } |
221 | ||
e4225669 | 222 | if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN]) |
d05df686 DB |
223 | bpf_print_ops(f, tb[TCA_BPF_OPS], |
224 | rta_getattr_u16(tb[TCA_BPF_OPS_LEN])); | |
225 | ||
a0b5b7cf DB |
226 | if (tb[TCA_BPF_ID]) |
227 | dump_ok = bpf_dump_prog_info(f, rta_getattr_u32(tb[TCA_BPF_ID])); | |
228 | if (!dump_ok && tb[TCA_BPF_TAG]) { | |
e37d706b DB |
229 | SPRINT_BUF(b); |
230 | ||
231 | fprintf(f, "tag %s ", | |
232 | hexstring_n2a(RTA_DATA(tb[TCA_BPF_TAG]), | |
233 | RTA_PAYLOAD(tb[TCA_BPF_TAG]), | |
234 | b, sizeof(b))); | |
235 | } | |
236 | ||
d05df686 DB |
237 | if (tb[TCA_BPF_POLICE]) { |
238 | fprintf(f, "\n"); | |
239 | tc_print_police(f, tb[TCA_BPF_POLICE]); | |
240 | } | |
241 | ||
e4225669 | 242 | if (tb[TCA_BPF_ACT]) |
9e713525 | 243 | tc_print_action(f, tb[TCA_BPF_ACT], 0); |
d05df686 DB |
244 | |
245 | return 0; | |
246 | } | |
247 | ||
248 | struct filter_util bpf_filter_util = { | |
32e93fb7 DB |
249 | .id = "bpf", |
250 | .parse_fopt = bpf_parse_opt, | |
251 | .print_fopt = bpf_print_opt, | |
d05df686 | 252 | }; |