]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_bpf.c
tc: {f,m}_bpf: allow to retrieve uds path from env
[mirror_iproute2.git] / tc / m_bpf.c
1 /*
2 * m_bpf.c BPF based action module
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>
10 * Daniel Borkmann <daniel@iogearbox.net>
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <stdbool.h>
18 #include <libgen.h>
19 #include <linux/bpf.h>
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
27 static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_ACT;
28
29 static void explain(void)
30 {
31 fprintf(stderr, "Usage: ... bpf ...\n");
32 fprintf(stderr, "\n");
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");
38 fprintf(stderr, " object-file FILE [ section ACT_NAME ] [ export UDS_FILE ]");
39 fprintf(stderr, " [ verbose ]\n");
40 fprintf(stderr, "\n");
41 fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
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");
52 }
53
54 static void usage(void)
55 {
56 explain();
57 exit(-1);
58 }
59
60 static int parse_bpf(struct action_util *a, int *argc_p, char ***argv_p,
61 int tca_id, struct nlmsghdr *n)
62 {
63 char **argv = *argv_p, bpf_name[256];
64 struct rtattr *tail;
65 struct tc_act_bpf parm = { 0 };
66 struct sock_filter bpf_ops[BPF_MAXINSNS];
67 bool ebpf = false, seen_run = false;
68 const char *bpf_uds_name = NULL;
69 const char *bpf_sec_name = NULL;
70 char *bpf_obj = NULL;
71 int argc = *argc_p, ret = 0;
72 __u16 bpf_len = 0;
73 __u32 bpf_fd = 0;
74
75 if (matches(*argv, "bpf") != 0)
76 return -1;
77
78 NEXT_ARG();
79
80 while (argc > 0) {
81 if (matches(*argv, "run") == 0) {
82 bool from_file, bpf_verbose;
83 int ret;
84
85 NEXT_ARG();
86 opt_bpf:
87 bpf_sec_name = bpf_default_section(bpf_type);
88 bpf_verbose = false;
89 seen_run = true;
90
91 if (strcmp(*argv, "bytecode-file") == 0 ||
92 strcmp(*argv, "bcf") == 0) {
93 from_file = true;
94 } else if (strcmp(*argv, "bytecode") == 0 ||
95 strcmp(*argv, "bc") == 0) {
96 from_file = false;
97 } else if (strcmp(*argv, "object-file") == 0 ||
98 strcmp(*argv, "obj") == 0) {
99 ebpf = true;
100 } else {
101 fprintf(stderr, "unexpected \"%s\"\n", *argv);
102 explain();
103 return -1;
104 }
105
106 NEXT_ARG();
107 if (ebpf) {
108 bpf_uds_name = secure_getenv(BPF_ENV_UDS);
109 bpf_obj = *argv;
110 NEXT_ARG();
111
112 if (strcmp(*argv, "section") == 0 ||
113 strcmp(*argv, "sec") == 0) {
114 NEXT_ARG();
115 bpf_sec_name = *argv;
116 NEXT_ARG();
117 }
118 if (!bpf_uds_name &&
119 (strcmp(*argv, "export") == 0 ||
120 strcmp(*argv, "exp") == 0)) {
121 NEXT_ARG();
122 bpf_uds_name = *argv;
123 NEXT_ARG();
124 }
125 if (strcmp(*argv, "verbose") == 0 ||
126 strcmp(*argv, "verb") == 0) {
127 bpf_verbose = true;
128 NEXT_ARG();
129 }
130
131 PREV_ARG();
132 }
133
134 ret = ebpf ? bpf_open_object(bpf_obj, bpf_type, bpf_sec_name,
135 bpf_verbose) :
136 bpf_parse_ops(argc, argv, bpf_ops, from_file);
137 if (ret < 0) {
138 fprintf(stderr, "%s\n", ebpf ?
139 "Could not load object" :
140 "Illegal \"bytecode\"");
141 return -1;
142 }
143
144 if (ebpf) {
145 bpf_obj = basename(bpf_obj);
146
147 snprintf(bpf_name, sizeof(bpf_name), "%s:[%s]",
148 bpf_obj, bpf_sec_name);
149
150 bpf_fd = ret;
151 } else {
152 bpf_len = ret;
153 }
154 } else if (matches(*argv, "help") == 0) {
155 usage();
156 } else {
157 if (!seen_run)
158 goto opt_bpf;
159 break;
160 }
161 argc--;
162 argv++;
163 }
164
165 parm.action = TC_ACT_PIPE;
166 if (argc) {
167 if (matches(*argv, "reclassify") == 0) {
168 parm.action = TC_ACT_RECLASSIFY;
169 argc--;
170 argv++;
171 } else if (matches(*argv, "pipe") == 0) {
172 parm.action = TC_ACT_PIPE;
173 argc--;
174 argv++;
175 } else if (matches(*argv, "drop") == 0 ||
176 matches(*argv, "shot") == 0) {
177 parm.action = TC_ACT_SHOT;
178 argc--;
179 argv++;
180 } else if (matches(*argv, "continue") == 0) {
181 parm.action = TC_ACT_UNSPEC;
182 argc--;
183 argv++;
184 } else if (matches(*argv, "pass") == 0) {
185 parm.action = TC_ACT_OK;
186 argc--;
187 argv++;
188 }
189 }
190
191 if (argc) {
192 if (matches(*argv, "index") == 0) {
193 NEXT_ARG();
194 if (get_u32(&parm.index, *argv, 10)) {
195 fprintf(stderr, "bpf: Illegal \"index\"\n");
196 return -1;
197 }
198 argc--;
199 argv++;
200 }
201 }
202
203 if ((!bpf_len && !ebpf) || (!bpf_fd && ebpf)) {
204 fprintf(stderr, "bpf: Bytecode needs to be passed\n");
205 explain();
206 return -1;
207 }
208
209 tail = NLMSG_TAIL(n);
210
211 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
212 addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm));
213
214 if (ebpf) {
215 addattr32(n, MAX_MSG, TCA_ACT_BPF_FD, bpf_fd);
216 addattrstrz(n, MAX_MSG, TCA_ACT_BPF_NAME, bpf_name);
217 } else {
218 addattr16(n, MAX_MSG, TCA_ACT_BPF_OPS_LEN, bpf_len);
219 addattr_l(n, MAX_MSG, TCA_ACT_BPF_OPS, &bpf_ops,
220 bpf_len * sizeof(struct sock_filter));
221 }
222
223 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
224
225 *argc_p = argc;
226 *argv_p = argv;
227
228 if (bpf_uds_name)
229 ret = bpf_send_map_fds(bpf_uds_name, bpf_obj);
230
231 return ret;
232 }
233
234 static int print_bpf(struct action_util *au, FILE *f, struct rtattr *arg)
235 {
236 struct rtattr *tb[TCA_ACT_BPF_MAX + 1];
237 struct tc_act_bpf *parm;
238 SPRINT_BUF(action_buf);
239
240 if (arg == NULL)
241 return -1;
242
243 parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg);
244
245 if (!tb[TCA_ACT_BPF_PARMS]) {
246 fprintf(f, "[NULL bpf parameters]");
247 return -1;
248 }
249
250 parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]);
251
252 fprintf(f, "bpf ");
253
254 if (tb[TCA_ACT_BPF_NAME])
255 fprintf(f, "%s ", rta_getattr_str(tb[TCA_ACT_BPF_NAME]));
256 else if (tb[TCA_ACT_BPF_FD])
257 fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_ACT_BPF_FD]));
258
259 if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) {
260 bpf_print_ops(f, tb[TCA_ACT_BPF_OPS],
261 rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN]));
262 fprintf(f, " ");
263 }
264
265 fprintf(f, "default-action %s\n", action_n2a(parm->action, action_buf,
266 sizeof(action_buf)));
267 fprintf(f, "\tindex %d ref %d bind %d", parm->index, parm->refcnt,
268 parm->bindcnt);
269
270 if (show_stats) {
271 if (tb[TCA_ACT_BPF_TM]) {
272 struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]);
273 print_tm(f, tm);
274 }
275 }
276
277 fprintf(f, "\n ");
278
279 return 0;
280 }
281
282 struct action_util bpf_action_util = {
283 .id = "bpf",
284 .parse_aopt = parse_bpf,
285 .print_aopt = print_bpf,
286 };