]>
Commit | Line | Data |
---|---|---|
86ab59a6 | 1 | /* |
d937a74b | 2 | * m_bpf.c BPF based action module |
86ab59a6 JP |
3 | * |
4 | * This program is free software; you can redistribute 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 | * | |
9 | * Authors: Jiri Pirko <jiri@resnulli.us> | |
6256f8c9 | 10 | * Daniel Borkmann <daniel@iogearbox.net> |
86ab59a6 JP |
11 | */ |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
16 | #include <string.h> | |
17 | #include <stdbool.h> | |
6256f8c9 DB |
18 | #include <libgen.h> |
19 | #include <linux/bpf.h> | |
86ab59a6 JP |
20 | #include <linux/tc_act/tc_bpf.h> |
21 | ||
22 | #include "utils.h" | |
23 | #include "rt_names.h" | |
24 | #include "tc_util.h" | |
25 | #include "tc_bpf.h" | |
26 | ||
6256f8c9 DB |
27 | static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_ACT; |
28 | ||
86ab59a6 JP |
29 | static void explain(void) |
30 | { | |
baed9084 | 31 | fprintf(stderr, "Usage: ... bpf ... [ index INDEX ]\n"); |
86ab59a6 | 32 | fprintf(stderr, "\n"); |
6256f8c9 DB |
33 | fprintf(stderr, "BPF use case:\n"); |
34 | fprintf(stderr, " bytecode BPF_BYTECODE\n"); | |
35 | fprintf(stderr, " bytecode-file FILE\n"); | |
36 | fprintf(stderr, "\n"); | |
37 | fprintf(stderr, "eBPF use case:\n"); | |
d937a74b DB |
38 | fprintf(stderr, " object-file FILE [ section ACT_NAME ] [ export UDS_FILE ]"); |
39 | fprintf(stderr, " [ verbose ]\n"); | |
86ab59a6 JP |
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"); | |
44 | fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n"); | |
45 | fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode.\n"); | |
46 | fprintf(stderr, "\n"); | |
47 | fprintf(stderr, "Where ACT_NAME refers to the section name containing the\n"); | |
48 | fprintf(stderr, "action (default \'%s\').\n", bpf_default_section(bpf_type)); | |
49 | fprintf(stderr, "\n"); | |
50 | fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n"); | |
51 | fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n"); | |
baed9084 DB |
52 | fprintf(stderr, "\n"); |
53 | fprintf(stderr, "Where optionally INDEX points to an existing action, or\n"); | |
54 | fprintf(stderr, "explicitly specifies an action index upon creation.\n"); | |
86ab59a6 JP |
55 | } |
56 | ||
57 | static void usage(void) | |
58 | { | |
59 | explain(); | |
60 | exit(-1); | |
61 | } | |
62 | ||
63 | static int parse_bpf(struct action_util *a, int *argc_p, char ***argv_p, | |
64 | int tca_id, struct nlmsghdr *n) | |
65 | { | |
6256f8c9 | 66 | char **argv = *argv_p, bpf_name[256]; |
86ab59a6 JP |
67 | struct rtattr *tail; |
68 | struct tc_act_bpf parm = { 0 }; | |
69 | struct sock_filter bpf_ops[BPF_MAXINSNS]; | |
baed9084 | 70 | bool ebpf_fill = false, bpf_fill = false; |
6256f8c9 DB |
71 | bool ebpf = false, seen_run = false; |
72 | const char *bpf_uds_name = NULL; | |
73 | const char *bpf_sec_name = NULL; | |
74 | char *bpf_obj = NULL; | |
75 | int argc = *argc_p, ret = 0; | |
86ab59a6 | 76 | __u16 bpf_len = 0; |
6256f8c9 | 77 | __u32 bpf_fd = 0; |
86ab59a6 JP |
78 | |
79 | if (matches(*argv, "bpf") != 0) | |
80 | return -1; | |
81 | ||
82 | NEXT_ARG(); | |
83 | ||
84 | while (argc > 0) { | |
85 | if (matches(*argv, "run") == 0) { | |
d937a74b | 86 | bool from_file, bpf_verbose; |
86ab59a6 JP |
87 | int ret; |
88 | ||
89 | NEXT_ARG(); | |
6256f8c9 DB |
90 | opt_bpf: |
91 | bpf_sec_name = bpf_default_section(bpf_type); | |
d937a74b | 92 | bpf_verbose = false; |
6256f8c9 DB |
93 | seen_run = true; |
94 | ||
95 | if (strcmp(*argv, "bytecode-file") == 0 || | |
96 | strcmp(*argv, "bcf") == 0) { | |
86ab59a6 | 97 | from_file = true; |
6256f8c9 DB |
98 | } else if (strcmp(*argv, "bytecode") == 0 || |
99 | strcmp(*argv, "bc") == 0) { | |
86ab59a6 | 100 | from_file = false; |
6256f8c9 DB |
101 | } else if (strcmp(*argv, "object-file") == 0 || |
102 | strcmp(*argv, "obj") == 0) { | |
103 | ebpf = true; | |
86ab59a6 JP |
104 | } else { |
105 | fprintf(stderr, "unexpected \"%s\"\n", *argv); | |
106 | explain(); | |
107 | return -1; | |
108 | } | |
6256f8c9 | 109 | |
86ab59a6 | 110 | NEXT_ARG(); |
6256f8c9 | 111 | if (ebpf) { |
611f70b2 | 112 | bpf_uds_name = getenv(BPF_ENV_UDS); |
6256f8c9 | 113 | bpf_obj = *argv; |
6256f8c9 | 114 | |
343dc908 DB |
115 | NEXT_ARG_FWD(); |
116 | ||
117 | if (argc > 0 && | |
118 | (strcmp(*argv, "section") == 0 || | |
119 | strcmp(*argv, "sec") == 0)) { | |
6256f8c9 DB |
120 | NEXT_ARG(); |
121 | bpf_sec_name = *argv; | |
343dc908 | 122 | NEXT_ARG_FWD(); |
6256f8c9 | 123 | } |
343dc908 | 124 | if (argc > 0 && !bpf_uds_name && |
88eea539 DB |
125 | (strcmp(*argv, "export") == 0 || |
126 | strcmp(*argv, "exp") == 0)) { | |
6256f8c9 DB |
127 | NEXT_ARG(); |
128 | bpf_uds_name = *argv; | |
343dc908 | 129 | NEXT_ARG_FWD(); |
6256f8c9 | 130 | } |
343dc908 DB |
131 | if (argc > 0 && |
132 | (strcmp(*argv, "verbose") == 0 || | |
133 | strcmp(*argv, "verb") == 0)) { | |
d937a74b | 134 | bpf_verbose = true; |
343dc908 | 135 | NEXT_ARG_FWD(); |
d937a74b | 136 | } |
6256f8c9 DB |
137 | |
138 | PREV_ARG(); | |
139 | } | |
140 | ||
d937a74b DB |
141 | ret = ebpf ? bpf_open_object(bpf_obj, bpf_type, bpf_sec_name, |
142 | bpf_verbose) : | |
6256f8c9 | 143 | bpf_parse_ops(argc, argv, bpf_ops, from_file); |
86ab59a6 | 144 | if (ret < 0) { |
6256f8c9 DB |
145 | fprintf(stderr, "%s\n", ebpf ? |
146 | "Could not load object" : | |
147 | "Illegal \"bytecode\""); | |
86ab59a6 JP |
148 | return -1; |
149 | } | |
6256f8c9 DB |
150 | |
151 | if (ebpf) { | |
152 | bpf_obj = basename(bpf_obj); | |
153 | ||
154 | snprintf(bpf_name, sizeof(bpf_name), "%s:[%s]", | |
155 | bpf_obj, bpf_sec_name); | |
156 | ||
157 | bpf_fd = ret; | |
baed9084 | 158 | ebpf_fill = true; |
6256f8c9 DB |
159 | } else { |
160 | bpf_len = ret; | |
baed9084 | 161 | bpf_fill = true; |
6256f8c9 | 162 | } |
86ab59a6 JP |
163 | } else if (matches(*argv, "help") == 0) { |
164 | usage(); | |
baed9084 DB |
165 | } else if (matches(*argv, "index") == 0) { |
166 | break; | |
86ab59a6 | 167 | } else { |
6256f8c9 DB |
168 | if (!seen_run) |
169 | goto opt_bpf; | |
86ab59a6 JP |
170 | break; |
171 | } | |
343dc908 DB |
172 | |
173 | NEXT_ARG_FWD(); | |
86ab59a6 JP |
174 | } |
175 | ||
176 | parm.action = TC_ACT_PIPE; | |
177 | if (argc) { | |
178 | if (matches(*argv, "reclassify") == 0) { | |
179 | parm.action = TC_ACT_RECLASSIFY; | |
343dc908 | 180 | NEXT_ARG_FWD(); |
86ab59a6 JP |
181 | } else if (matches(*argv, "pipe") == 0) { |
182 | parm.action = TC_ACT_PIPE; | |
343dc908 | 183 | NEXT_ARG_FWD(); |
86ab59a6 JP |
184 | } else if (matches(*argv, "drop") == 0 || |
185 | matches(*argv, "shot") == 0) { | |
186 | parm.action = TC_ACT_SHOT; | |
343dc908 | 187 | NEXT_ARG_FWD(); |
86ab59a6 JP |
188 | } else if (matches(*argv, "continue") == 0) { |
189 | parm.action = TC_ACT_UNSPEC; | |
343dc908 DB |
190 | NEXT_ARG_FWD(); |
191 | } else if (matches(*argv, "pass") == 0 || | |
192 | matches(*argv, "ok") == 0) { | |
86ab59a6 | 193 | parm.action = TC_ACT_OK; |
343dc908 | 194 | NEXT_ARG_FWD(); |
86ab59a6 JP |
195 | } |
196 | } | |
197 | ||
198 | if (argc) { | |
199 | if (matches(*argv, "index") == 0) { | |
200 | NEXT_ARG(); | |
201 | if (get_u32(&parm.index, *argv, 10)) { | |
202 | fprintf(stderr, "bpf: Illegal \"index\"\n"); | |
203 | return -1; | |
204 | } | |
343dc908 DB |
205 | |
206 | NEXT_ARG_FWD(); | |
86ab59a6 JP |
207 | } |
208 | } | |
209 | ||
86ab59a6 | 210 | tail = NLMSG_TAIL(n); |
6256f8c9 | 211 | |
86ab59a6 JP |
212 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
213 | addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm)); | |
6256f8c9 | 214 | |
baed9084 | 215 | if (ebpf_fill) { |
6256f8c9 DB |
216 | addattr32(n, MAX_MSG, TCA_ACT_BPF_FD, bpf_fd); |
217 | addattrstrz(n, MAX_MSG, TCA_ACT_BPF_NAME, bpf_name); | |
baed9084 | 218 | } else if (bpf_fill) { |
6256f8c9 DB |
219 | addattr16(n, MAX_MSG, TCA_ACT_BPF_OPS_LEN, bpf_len); |
220 | addattr_l(n, MAX_MSG, TCA_ACT_BPF_OPS, &bpf_ops, | |
221 | bpf_len * sizeof(struct sock_filter)); | |
222 | } | |
223 | ||
86ab59a6 JP |
224 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
225 | ||
226 | *argc_p = argc; | |
227 | *argv_p = argv; | |
6256f8c9 DB |
228 | |
229 | if (bpf_uds_name) | |
4bd62446 | 230 | ret = bpf_send_map_fds(bpf_uds_name, bpf_obj); |
6256f8c9 DB |
231 | |
232 | return ret; | |
86ab59a6 JP |
233 | } |
234 | ||
235 | static int print_bpf(struct action_util *au, FILE *f, struct rtattr *arg) | |
236 | { | |
237 | struct rtattr *tb[TCA_ACT_BPF_MAX + 1]; | |
238 | struct tc_act_bpf *parm; | |
6256f8c9 | 239 | SPRINT_BUF(action_buf); |
86ab59a6 JP |
240 | |
241 | if (arg == NULL) | |
242 | return -1; | |
243 | ||
244 | parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg); | |
245 | ||
246 | if (!tb[TCA_ACT_BPF_PARMS]) { | |
247 | fprintf(f, "[NULL bpf parameters]"); | |
248 | return -1; | |
249 | } | |
6256f8c9 | 250 | |
86ab59a6 JP |
251 | parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]); |
252 | ||
6256f8c9 DB |
253 | fprintf(f, "bpf "); |
254 | ||
255 | if (tb[TCA_ACT_BPF_NAME]) | |
256 | fprintf(f, "%s ", rta_getattr_str(tb[TCA_ACT_BPF_NAME])); | |
257 | else if (tb[TCA_ACT_BPF_FD]) | |
258 | fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_ACT_BPF_FD])); | |
86ab59a6 | 259 | |
6256f8c9 | 260 | if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) { |
86ab59a6 JP |
261 | bpf_print_ops(f, tb[TCA_ACT_BPF_OPS], |
262 | rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN])); | |
6256f8c9 DB |
263 | fprintf(f, " "); |
264 | } | |
86ab59a6 | 265 | |
6256f8c9 DB |
266 | fprintf(f, "default-action %s\n", action_n2a(parm->action, action_buf, |
267 | sizeof(action_buf))); | |
268 | fprintf(f, "\tindex %d ref %d bind %d", parm->index, parm->refcnt, | |
86ab59a6 JP |
269 | parm->bindcnt); |
270 | ||
271 | if (show_stats) { | |
272 | if (tb[TCA_ACT_BPF_TM]) { | |
273 | struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]); | |
274 | print_tm(f, tm); | |
275 | } | |
276 | } | |
277 | ||
278 | fprintf(f, "\n "); | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
283 | struct action_util bpf_action_util = { | |
284 | .id = "bpf", | |
285 | .parse_aopt = parse_bpf, | |
286 | .print_aopt = print_bpf, | |
287 | }; |