]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_bpf.c
mroute: remove invalid check against NLM_F_MULTI
[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 *
9 * Authors: Daniel Borkmann <dborkman@redhat.com>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <syslog.h>
16#include <fcntl.h>
6256f8c9 17#include <libgen.h>
d05df686
DB
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include <stdbool.h>
23#include <errno.h>
dd9cc0ee 24#include <limits.h>
d05df686
DB
25#include <linux/filter.h>
26#include <linux/if.h>
27
28#include "utils.h"
29#include "tc_util.h"
1d129d19 30#include "tc_bpf.h"
d05df686 31
6256f8c9
DB
32static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_CLS;
33
d05df686
DB
34static void explain(void)
35{
36 fprintf(stderr, "Usage: ... bpf ...\n");
37 fprintf(stderr, "\n");
6256f8c9
DB
38 fprintf(stderr, "BPF use case:\n");
39 fprintf(stderr, " bytecode BPF_BYTECODE\n");
40 fprintf(stderr, " bytecode-file FILE\n");
41 fprintf(stderr, "\n");
42 fprintf(stderr, "eBPF use case:\n");
43 fprintf(stderr, " object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]\n");
d05df686 44 fprintf(stderr, "\n");
6256f8c9
DB
45 fprintf(stderr, "Common remaining options:\n");
46 fprintf(stderr, " [ action ACTION_SPEC ]\n");
47 fprintf(stderr, " [ classid CLASSID ]\n");
d05df686
DB
48 fprintf(stderr, "\n");
49 fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
6256f8c9
DB
50 fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n");
51 fprintf(stderr, "\n");
11c39b5e 52 fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n");
6256f8c9
DB
53 fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode.\n");
54 fprintf(stderr, "\n");
55 fprintf(stderr, "Where CLS_NAME refers to the section name containing the\n");
56 fprintf(stderr, "classifier (default \'%s\').\n", bpf_default_section(bpf_type));
57 fprintf(stderr, "\n");
58 fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n");
59 fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n");
60 fprintf(stderr, "\n");
61 fprintf(stderr, "ACTION_SPEC := ... look at individual actions\n");
863ecb04 62 fprintf(stderr, "NOTE: CLASSID is parsed as hexadecimal input.\n");
d05df686
DB
63}
64
d05df686
DB
65static int bpf_parse_opt(struct filter_util *qu, char *handle,
66 int argc, char **argv, struct nlmsghdr *n)
67{
68 struct tcmsg *t = NLMSG_DATA(n);
6256f8c9
DB
69 const char *bpf_uds_name = NULL;
70 const char *bpf_sec_name = NULL;
71 char *bpf_obj = NULL;
d05df686 72 struct rtattr *tail;
6256f8c9 73 bool seen_run = false;
d05df686 74 long h = 0;
6256f8c9 75 int ret = 0;
d05df686
DB
76
77 if (argc == 0)
78 return 0;
79
80 if (handle) {
81 h = strtol(handle, NULL, 0);
82 if (h == LONG_MIN || h == LONG_MAX) {
83 fprintf(stderr, "Illegal handle \"%s\", must be "
84 "numeric.\n", handle);
85 return -1;
86 }
87 }
88
89 t->tcm_handle = h;
90
6256f8c9 91 tail = (struct rtattr *)(((void *)n) + NLMSG_ALIGN(n->nlmsg_len));
d05df686
DB
92 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
93
94 while (argc > 0) {
95 if (matches(*argv, "run") == 0) {
1d129d19 96 struct sock_filter bpf_ops[BPF_MAXINSNS];
6256f8c9 97 bool from_file, ebpf;
1d129d19
JP
98 int ret;
99
d05df686 100 NEXT_ARG();
6256f8c9
DB
101opt_bpf:
102 bpf_sec_name = bpf_default_section(bpf_type);
103 ebpf = false;
104 seen_run = true;
105
106 if (strcmp(*argv, "bytecode-file") == 0 ||
107 strcmp(*argv, "bcf") == 0) {
108 from_file = true;
109 } else if (strcmp(*argv, "bytecode") == 0 ||
110 strcmp(*argv, "bc") == 0) {
d05df686 111 from_file = false;
6256f8c9
DB
112 } else if (strcmp(*argv, "object-file") == 0 ||
113 strcmp(*argv, "obj") == 0) {
11c39b5e 114 ebpf = true;
d05df686
DB
115 } else {
116 fprintf(stderr, "What is \"%s\"?\n", *argv);
117 explain();
118 return -1;
119 }
6256f8c9 120
d05df686 121 NEXT_ARG();
6256f8c9
DB
122 if (ebpf) {
123 bpf_obj = *argv;
124 NEXT_ARG();
125
126 if (strcmp(*argv, "section") == 0 ||
127 strcmp(*argv, "sec") == 0) {
128 NEXT_ARG();
129 bpf_sec_name = *argv;
130 NEXT_ARG();
131 }
132 if (strcmp(*argv, "export") == 0 ||
133 strcmp(*argv, "exp") == 0) {
134 NEXT_ARG();
135 bpf_uds_name = *argv;
136 NEXT_ARG();
137 }
138
139 PREV_ARG();
140 }
141
142 ret = ebpf ? bpf_open_object(bpf_obj, bpf_type, bpf_sec_name) :
143 bpf_parse_ops(argc, argv, bpf_ops, from_file);
1d129d19 144 if (ret < 0) {
11c39b5e
DB
145 fprintf(stderr, "%s\n", ebpf ?
146 "Could not load object" :
147 "Illegal \"bytecode\"");
d05df686
DB
148 return -1;
149 }
6256f8c9 150
11c39b5e 151 if (ebpf) {
6256f8c9
DB
152 char bpf_name[256];
153
154 bpf_obj = basename(bpf_obj);
155
156 snprintf(bpf_name, sizeof(bpf_name), "%s:[%s]",
157 bpf_obj, bpf_sec_name);
158
11c39b5e 159 addattr32(n, MAX_MSG, TCA_BPF_FD, ret);
6256f8c9 160 addattrstrz(n, MAX_MSG, TCA_BPF_NAME, bpf_name);
11c39b5e
DB
161 } else {
162 addattr16(n, MAX_MSG, TCA_BPF_OPS_LEN, ret);
163 addattr_l(n, MAX_MSG, TCA_BPF_OPS, &bpf_ops,
164 ret * sizeof(struct sock_filter));
165 }
d05df686
DB
166 } else if (matches(*argv, "classid") == 0 ||
167 strcmp(*argv, "flowid") == 0) {
6256f8c9
DB
168 unsigned int handle;
169
d05df686
DB
170 NEXT_ARG();
171 if (get_tc_classid(&handle, *argv)) {
172 fprintf(stderr, "Illegal \"classid\"\n");
173 return -1;
174 }
175 addattr_l(n, MAX_MSG, TCA_BPF_CLASSID, &handle, 4);
176 } else if (matches(*argv, "action") == 0) {
177 NEXT_ARG();
178 if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) {
179 fprintf(stderr, "Illegal \"action\"\n");
180 return -1;
181 }
182 continue;
183 } else if (matches(*argv, "police") == 0) {
184 NEXT_ARG();
185 if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) {
186 fprintf(stderr, "Illegal \"police\"\n");
187 return -1;
188 }
189 continue;
190 } else if (strcmp(*argv, "help") == 0) {
191 explain();
192 return -1;
193 } else {
6256f8c9
DB
194 if (!seen_run)
195 goto opt_bpf;
196
d05df686
DB
197 fprintf(stderr, "What is \"%s\"?\n", *argv);
198 explain();
199 return -1;
200 }
6256f8c9
DB
201 argc--;
202 argv++;
d05df686
DB
203 }
204
6256f8c9
DB
205 tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail;
206
207 if (bpf_uds_name)
208 ret = bpf_handoff_map_fds(bpf_uds_name, bpf_obj);
209
210 return ret;
d05df686
DB
211}
212
213static int bpf_print_opt(struct filter_util *qu, FILE *f,
214 struct rtattr *opt, __u32 handle)
215{
216 struct rtattr *tb[TCA_BPF_MAX + 1];
217
218 if (opt == NULL)
219 return 0;
220
221 parse_rtattr_nested(tb, TCA_BPF_MAX, opt);
222
223 if (handle)
224 fprintf(f, "handle 0x%x ", handle);
225
226 if (tb[TCA_BPF_CLASSID]) {
227 SPRINT_BUF(b1);
228 fprintf(f, "flowid %s ",
229 sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1));
230 }
231
11c39b5e
DB
232 if (tb[TCA_BPF_NAME])
233 fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME]));
234 else if (tb[TCA_BPF_FD])
235 fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_BPF_FD]));
236
6256f8c9 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]));
6256f8c9
DB
240 fprintf(f, "\n");
241 }
d05df686
DB
242
243 if (tb[TCA_BPF_POLICE]) {
244 fprintf(f, "\n");
245 tc_print_police(f, tb[TCA_BPF_POLICE]);
246 }
247
248 if (tb[TCA_BPF_ACT]) {
249 tc_print_action(f, tb[TCA_BPF_ACT]);
250 }
251
252 return 0;
253}
254
255struct filter_util bpf_filter_util = {
256 .id = "bpf",
257 .parse_fopt = bpf_parse_opt,
258 .print_fopt = bpf_print_opt,
259};