blob: 5c97c863d15aa2ceefef9eafc2a317ad3e159826 [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 *
9 * Authors: Daniel Borkmann <dborkman@redhat.com>
10 */
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"
18#include "tc_util.h"
Jiri Pirko1d129d12015-01-19 16:56:29 +010019#include "tc_bpf.h"
Daniel Borkmannd05df682013-10-28 12:35:33 +010020
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020021static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_CLS;
22
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010023static const int nla_tbl[BPF_NLA_MAX] = {
24 [BPF_NLA_OPS_LEN] = TCA_BPF_OPS_LEN,
25 [BPF_NLA_OPS] = TCA_BPF_OPS,
26 [BPF_NLA_FD] = TCA_BPF_FD,
27 [BPF_NLA_NAME] = TCA_BPF_NAME,
28};
29
Daniel Borkmannd05df682013-10-28 12:35:33 +010030static void explain(void)
31{
32 fprintf(stderr, "Usage: ... bpf ...\n");
33 fprintf(stderr, "\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020034 fprintf(stderr, "BPF use case:\n");
35 fprintf(stderr, " bytecode BPF_BYTECODE\n");
36 fprintf(stderr, " bytecode-file FILE\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010037 fprintf(stderr, "\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020038 fprintf(stderr, "eBPF use case:\n");
Daniel Borkmannd937a742015-04-28 13:37:42 +020039 fprintf(stderr, " object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]");
Daniel Borkmannfaa8a462015-09-25 12:32:41 +020040 fprintf(stderr, " [ verbose ] [ direct-action ]\n");
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010041 fprintf(stderr, " object-pinned FILE [ direct-action ]\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020042 fprintf(stderr, "\n");
43 fprintf(stderr, "Common remaining options:\n");
44 fprintf(stderr, " [ action ACTION_SPEC ]\n");
45 fprintf(stderr, " [ classid CLASSID ]\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010046 fprintf(stderr, "\n");
47 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 +020048 fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n");
49 fprintf(stderr, "\n");
Daniel Borkmann11c39b52015-03-16 19:37:41 +010050 fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n");
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010051 fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode, or a\n");
52 fprintf(stderr, "pinned eBPF program.\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020053 fprintf(stderr, "\n");
54 fprintf(stderr, "Where CLS_NAME refers to the section name containing the\n");
55 fprintf(stderr, "classifier (default \'%s\').\n", bpf_default_section(bpf_type));
56 fprintf(stderr, "\n");
57 fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n");
58 fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n");
59 fprintf(stderr, "\n");
60 fprintf(stderr, "ACTION_SPEC := ... look at individual actions\n");
Jamal Hadi Salim863ecb02014-10-06 07:41:21 -040061 fprintf(stderr, "NOTE: CLASSID is parsed as hexadecimal input.\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010062}
63
Daniel Borkmannd05df682013-10-28 12:35:33 +010064static int bpf_parse_opt(struct filter_util *qu, char *handle,
65 int argc, char **argv, struct nlmsghdr *n)
66{
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010067 const char *bpf_obj = NULL, *bpf_uds_name = NULL;
Daniel Borkmannd05df682013-10-28 12:35:33 +010068 struct tcmsg *t = NLMSG_DATA(n);
Daniel Borkmannfaa8a462015-09-25 12:32:41 +020069 unsigned int bpf_flags = 0;
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020070 bool seen_run = false;
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010071 struct rtattr *tail;
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020072 int ret = 0;
Daniel Borkmannd05df682013-10-28 12:35:33 +010073
Daniel Borkmannd05df682013-10-28 12:35:33 +010074 if (handle) {
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010075 if (get_u32(&t->tcm_handle, handle, 0)) {
76 fprintf(stderr, "Illegal \"handle\"\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010077 return -1;
78 }
79 }
80
Daniel Borkmann1a032072016-05-18 11:58:41 +020081 if (argc == 0)
82 return 0;
83
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020084 tail = (struct rtattr *)(((void *)n) + NLMSG_ALIGN(n->nlmsg_len));
Daniel Borkmannd05df682013-10-28 12:35:33 +010085 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
86
87 while (argc > 0) {
88 if (matches(*argv, "run") == 0) {
Daniel Borkmannd05df682013-10-28 12:35:33 +010089 NEXT_ARG();
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020090opt_bpf:
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020091 seen_run = true;
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010092 if (bpf_parse_common(&argc, &argv, nla_tbl, bpf_type,
93 &bpf_obj, &bpf_uds_name, n)) {
94 fprintf(stderr, "Failed to retrieve (e)BPF data!\n");
Daniel Borkmannd05df682013-10-28 12:35:33 +010095 return -1;
96 }
Daniel Borkmannd05df682013-10-28 12:35:33 +010097 } else if (matches(*argv, "classid") == 0 ||
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010098 matches(*argv, "flowid") == 0) {
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020099 unsigned int handle;
100
Daniel Borkmannd05df682013-10-28 12:35:33 +0100101 NEXT_ARG();
102 if (get_tc_classid(&handle, *argv)) {
103 fprintf(stderr, "Illegal \"classid\"\n");
104 return -1;
105 }
Daniel Borkmannfaa8a462015-09-25 12:32:41 +0200106 addattr32(n, MAX_MSG, TCA_BPF_CLASSID, handle);
107 } else if (matches(*argv, "direct-action") == 0 ||
108 matches(*argv, "da") == 0) {
109 bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
Daniel Borkmannd05df682013-10-28 12:35:33 +0100110 } else if (matches(*argv, "action") == 0) {
111 NEXT_ARG();
112 if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) {
113 fprintf(stderr, "Illegal \"action\"\n");
114 return -1;
115 }
116 continue;
117 } else if (matches(*argv, "police") == 0) {
118 NEXT_ARG();
119 if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) {
120 fprintf(stderr, "Illegal \"police\"\n");
121 return -1;
122 }
123 continue;
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100124 } else if (matches(*argv, "help") == 0) {
Daniel Borkmannd05df682013-10-28 12:35:33 +0100125 explain();
126 return -1;
127 } else {
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200128 if (!seen_run)
129 goto opt_bpf;
130
Daniel Borkmannd05df682013-10-28 12:35:33 +0100131 fprintf(stderr, "What is \"%s\"?\n", *argv);
132 explain();
133 return -1;
134 }
Daniel Borkmannfaa8a462015-09-25 12:32:41 +0200135
136 NEXT_ARG_FWD();
Daniel Borkmannd05df682013-10-28 12:35:33 +0100137 }
138
Daniel Borkmannfaa8a462015-09-25 12:32:41 +0200139 if (bpf_obj && bpf_flags)
140 addattr32(n, MAX_MSG, TCA_BPF_FLAGS, bpf_flags);
141
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200142 tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail;
143
144 if (bpf_uds_name)
Daniel Borkmann4bd62442015-04-16 21:20:06 +0200145 ret = bpf_send_map_fds(bpf_uds_name, bpf_obj);
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200146
147 return ret;
Daniel Borkmannd05df682013-10-28 12:35:33 +0100148}
149
150static int bpf_print_opt(struct filter_util *qu, FILE *f,
151 struct rtattr *opt, __u32 handle)
152{
153 struct rtattr *tb[TCA_BPF_MAX + 1];
154
155 if (opt == NULL)
156 return 0;
157
158 parse_rtattr_nested(tb, TCA_BPF_MAX, opt);
159
160 if (handle)
161 fprintf(f, "handle 0x%x ", handle);
162
163 if (tb[TCA_BPF_CLASSID]) {
164 SPRINT_BUF(b1);
165 fprintf(f, "flowid %s ",
166 sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1));
167 }
168
Daniel Borkmann11c39b52015-03-16 19:37:41 +0100169 if (tb[TCA_BPF_NAME])
170 fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME]));
171 else if (tb[TCA_BPF_FD])
172 fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_BPF_FD]));
173
Daniel Borkmannfaa8a462015-09-25 12:32:41 +0200174 if (tb[TCA_BPF_FLAGS]) {
175 unsigned int flags = rta_getattr_u32(tb[TCA_BPF_FLAGS]);
176
177 if (flags & TCA_BPF_FLAG_ACT_DIRECT)
178 fprintf(f, "direct-action ");
179 }
180
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200181 if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN]) {
Daniel Borkmannd05df682013-10-28 12:35:33 +0100182 bpf_print_ops(f, tb[TCA_BPF_OPS],
183 rta_getattr_u16(tb[TCA_BPF_OPS_LEN]));
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200184 fprintf(f, "\n");
185 }
Daniel Borkmannd05df682013-10-28 12:35:33 +0100186
187 if (tb[TCA_BPF_POLICE]) {
188 fprintf(f, "\n");
189 tc_print_police(f, tb[TCA_BPF_POLICE]);
190 }
191
192 if (tb[TCA_BPF_ACT]) {
193 tc_print_action(f, tb[TCA_BPF_ACT]);
194 }
195
196 return 0;
197}
198
199struct filter_util bpf_filter_util = {
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100200 .id = "bpf",
201 .parse_fopt = bpf_parse_opt,
202 .print_fopt = bpf_print_opt,
Daniel Borkmannd05df682013-10-28 12:35:33 +0100203};