blob: c4764d8f0a048f91f72a930d2f97e51dfc0be4d0 [file] [log] [blame]
Daniel Borkmannd05df682013-10-28 12:35:33 +01001/*
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 *
Daniel Borkmanne4225662016-11-10 01:20:59 +01009 * Authors: Daniel Borkmann <daniel@iogearbox.net>
Daniel Borkmannd05df682013-10-28 12:35:33 +010010 */
11
12#include <stdio.h>
13#include <stdlib.h>
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010014
15#include <linux/bpf.h>
Daniel Borkmannd05df682013-10-28 12:35:33 +010016
17#include "utils.h"
Daniel Borkmanne4225662016-11-10 01:20:59 +010018
Daniel Borkmannd05df682013-10-28 12:35:33 +010019#include "tc_util.h"
Daniel Borkmanne4225662016-11-10 01:20:59 +010020#include "bpf_util.h"
Daniel Borkmannd05df682013-10-28 12:35:33 +010021
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020022static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_CLS;
23
Daniel Borkmannd05df682013-10-28 12:35:33 +010024static void explain(void)
25{
26 fprintf(stderr, "Usage: ... bpf ...\n");
27 fprintf(stderr, "\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020028 fprintf(stderr, "BPF use case:\n");
29 fprintf(stderr, " bytecode BPF_BYTECODE\n");
30 fprintf(stderr, " bytecode-file FILE\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010031 fprintf(stderr, "\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020032 fprintf(stderr, "eBPF use case:\n");
Daniel Borkmannd937a742015-04-28 13:37:42 +020033 fprintf(stderr, " object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]");
Jakub Kicinski87e46a52016-10-12 16:46:36 +010034 fprintf(stderr, " [ verbose ] [ direct-action ] [ skip_hw | skip_sw ]\n");
35 fprintf(stderr, " object-pinned FILE [ direct-action ] [ skip_hw | skip_sw ]\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020036 fprintf(stderr, "\n");
37 fprintf(stderr, "Common remaining options:\n");
38 fprintf(stderr, " [ action ACTION_SPEC ]\n");
39 fprintf(stderr, " [ classid CLASSID ]\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010040 fprintf(stderr, "\n");
41 fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020042 fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n");
43 fprintf(stderr, "\n");
Daniel Borkmann11c39b52015-03-16 19:37:41 +010044 fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n");
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010045 fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode, or a\n");
46 fprintf(stderr, "pinned eBPF program.\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020047 fprintf(stderr, "\n");
48 fprintf(stderr, "Where CLS_NAME refers to the section name containing the\n");
Daniel Borkmanne4225662016-11-10 01:20:59 +010049 fprintf(stderr, "classifier (default \'%s\').\n", bpf_prog_to_default_section(bpf_type));
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020050 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");
Jamal Hadi Salim863ecb02014-10-06 07:41:21 -040055 fprintf(stderr, "NOTE: CLASSID is parsed as hexadecimal input.\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010056}
57
Daniel Borkmanne4225662016-11-10 01:20:59 +010058static 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
65static 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
71static const struct bpf_cfg_ops bpf_cb_ops = {
72 .cbpf_cb = bpf_cbpf_cb,
73 .ebpf_cb = bpf_ebpf_cb,
74};
75
Daniel Borkmannd05df682013-10-28 12:35:33 +010076static int bpf_parse_opt(struct filter_util *qu, char *handle,
77 int argc, char **argv, struct nlmsghdr *n)
78{
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010079 const char *bpf_obj = NULL, *bpf_uds_name = NULL;
Daniel Borkmannd05df682013-10-28 12:35:33 +010080 struct tcmsg *t = NLMSG_DATA(n);
Jakub Kicinski87e46a52016-10-12 16:46:36 +010081 unsigned int bpf_gen_flags = 0;
Daniel Borkmannfaa8a462015-09-25 12:32:41 +020082 unsigned int bpf_flags = 0;
Daniel Borkmanne4225662016-11-10 01:20:59 +010083 struct bpf_cfg_in cfg = {};
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020084 bool seen_run = false;
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010085 struct rtattr *tail;
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020086 int ret = 0;
Daniel Borkmannd05df682013-10-28 12:35:33 +010087
Daniel Borkmannd05df682013-10-28 12:35:33 +010088 if (handle) {
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010089 if (get_u32(&t->tcm_handle, handle, 0)) {
90 fprintf(stderr, "Illegal \"handle\"\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010091 return -1;
92 }
93 }
94
Daniel Borkmann1a032072016-05-18 11:58:41 +020095 if (argc == 0)
96 return 0;
97
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020098 tail = (struct rtattr *)(((void *)n) + NLMSG_ALIGN(n->nlmsg_len));
Daniel Borkmannd05df682013-10-28 12:35:33 +010099 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
100
101 while (argc > 0) {
102 if (matches(*argv, "run") == 0) {
Daniel Borkmannd05df682013-10-28 12:35:33 +0100103 NEXT_ARG();
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200104opt_bpf:
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200105 seen_run = true;
Daniel Borkmanne4225662016-11-10 01:20:59 +0100106 cfg.argc = argc;
107 cfg.argv = argv;
108
109 if (bpf_parse_common(bpf_type, &cfg, &bpf_cb_ops, n))
Daniel Borkmannd05df682013-10-28 12:35:33 +0100110 return -1;
Daniel Borkmanne4225662016-11-10 01:20:59 +0100111
112 argc = cfg.argc;
113 argv = cfg.argv;
114
115 bpf_obj = cfg.object;
116 bpf_uds_name = cfg.uds;
Daniel Borkmannd05df682013-10-28 12:35:33 +0100117 } else if (matches(*argv, "classid") == 0 ||
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100118 matches(*argv, "flowid") == 0) {
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200119 unsigned int handle;
120
Daniel Borkmannd05df682013-10-28 12:35:33 +0100121 NEXT_ARG();
122 if (get_tc_classid(&handle, *argv)) {
123 fprintf(stderr, "Illegal \"classid\"\n");
124 return -1;
125 }
Daniel Borkmannfaa8a462015-09-25 12:32:41 +0200126 addattr32(n, MAX_MSG, TCA_BPF_CLASSID, handle);
127 } else if (matches(*argv, "direct-action") == 0 ||
128 matches(*argv, "da") == 0) {
129 bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
Jakub Kicinski87e46a52016-10-12 16:46:36 +0100130 } else if (matches(*argv, "skip_hw") == 0) {
131 bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_HW;
132 } else if (matches(*argv, "skip_sw") == 0) {
133 bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_SW;
Daniel Borkmannd05df682013-10-28 12:35:33 +0100134 } else if (matches(*argv, "action") == 0) {
135 NEXT_ARG();
136 if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) {
137 fprintf(stderr, "Illegal \"action\"\n");
138 return -1;
139 }
140 continue;
141 } else if (matches(*argv, "police") == 0) {
142 NEXT_ARG();
143 if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) {
144 fprintf(stderr, "Illegal \"police\"\n");
145 return -1;
146 }
147 continue;
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100148 } else if (matches(*argv, "help") == 0) {
Daniel Borkmannd05df682013-10-28 12:35:33 +0100149 explain();
150 return -1;
151 } else {
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200152 if (!seen_run)
153 goto opt_bpf;
154
Daniel Borkmannd05df682013-10-28 12:35:33 +0100155 fprintf(stderr, "What is \"%s\"?\n", *argv);
156 explain();
157 return -1;
158 }
Daniel Borkmannfaa8a462015-09-25 12:32:41 +0200159
160 NEXT_ARG_FWD();
Daniel Borkmannd05df682013-10-28 12:35:33 +0100161 }
162
Jakub Kicinski87e46a52016-10-12 16:46:36 +0100163 if (bpf_gen_flags)
164 addattr32(n, MAX_MSG, TCA_BPF_FLAGS_GEN, bpf_gen_flags);
Daniel Borkmanne4225662016-11-10 01:20:59 +0100165 if (bpf_flags)
Daniel Borkmannfaa8a462015-09-25 12:32:41 +0200166 addattr32(n, MAX_MSG, TCA_BPF_FLAGS, bpf_flags);
167
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200168 tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail;
169
170 if (bpf_uds_name)
Daniel Borkmann4bd62442015-04-16 21:20:06 +0200171 ret = bpf_send_map_fds(bpf_uds_name, bpf_obj);
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200172
173 return ret;
Daniel Borkmannd05df682013-10-28 12:35:33 +0100174}
175
176static int bpf_print_opt(struct filter_util *qu, FILE *f,
177 struct rtattr *opt, __u32 handle)
178{
179 struct rtattr *tb[TCA_BPF_MAX + 1];
180
181 if (opt == NULL)
182 return 0;
183
184 parse_rtattr_nested(tb, TCA_BPF_MAX, opt);
185
186 if (handle)
187 fprintf(f, "handle 0x%x ", handle);
188
189 if (tb[TCA_BPF_CLASSID]) {
190 SPRINT_BUF(b1);
191 fprintf(f, "flowid %s ",
192 sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1));
193 }
194
Daniel Borkmann11c39b52015-03-16 19:37:41 +0100195 if (tb[TCA_BPF_NAME])
196 fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME]));
Daniel Borkmann11c39b52015-03-16 19:37:41 +0100197
Daniel Borkmannfaa8a462015-09-25 12:32:41 +0200198 if (tb[TCA_BPF_FLAGS]) {
199 unsigned int flags = rta_getattr_u32(tb[TCA_BPF_FLAGS]);
200
201 if (flags & TCA_BPF_FLAG_ACT_DIRECT)
202 fprintf(f, "direct-action ");
203 }
204
Jakub Kicinski87e46a52016-10-12 16:46:36 +0100205 if (tb[TCA_BPF_FLAGS_GEN]) {
206 unsigned int flags =
207 rta_getattr_u32(tb[TCA_BPF_FLAGS_GEN]);
208
209 if (flags & TCA_CLS_FLAGS_SKIP_HW)
210 fprintf(f, "skip_hw ");
211 if (flags & TCA_CLS_FLAGS_SKIP_SW)
212 fprintf(f, "skip_sw ");
213 }
214
Daniel Borkmanne4225662016-11-10 01:20:59 +0100215 if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN])
Daniel Borkmannd05df682013-10-28 12:35:33 +0100216 bpf_print_ops(f, tb[TCA_BPF_OPS],
217 rta_getattr_u16(tb[TCA_BPF_OPS_LEN]));
Daniel Borkmannd05df682013-10-28 12:35:33 +0100218
219 if (tb[TCA_BPF_POLICE]) {
220 fprintf(f, "\n");
221 tc_print_police(f, tb[TCA_BPF_POLICE]);
222 }
223
Daniel Borkmanne4225662016-11-10 01:20:59 +0100224 if (tb[TCA_BPF_ACT])
Daniel Borkmannd05df682013-10-28 12:35:33 +0100225 tc_print_action(f, tb[TCA_BPF_ACT]);
Daniel Borkmannd05df682013-10-28 12:35:33 +0100226
227 return 0;
228}
229
230struct filter_util bpf_filter_util = {
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100231 .id = "bpf",
232 .parse_fopt = bpf_parse_opt,
233 .print_fopt = bpf_print_opt,
Daniel Borkmannd05df682013-10-28 12:35:33 +0100234};