| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* f_bpf.c BPF-based Classifier |
| 4 |
* |
| 5 |
* Authors: Daniel Borkmann <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
|
| 11 |
#include <linux/bpf.h> |
| 12 |
|
| 13 |
#include "utils.h" |
| 14 |
|
| 15 |
#include "tc_util.h" |
| 16 |
#include "bpf_util.h" |
| 17 |
|
| 18 |
static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_CLS; |
| 19 |
|
| 20 |
static void explain(void) |
| 21 |
{ |
| 22 |
fprintf(stderr, |
| 23 |
"Usage: ... bpf ...\n" |
| 24 |
"\n" |
| 25 |
"BPF use case:\n" |
| 26 |
" bytecode BPF_BYTECODE\n" |
| 27 |
" bytecode-file FILE\n" |
| 28 |
"\n" |
| 29 |
"eBPF use case:\n" |
| 30 |
" object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]" |
| 31 |
" [ verbose ] [ direct-action ] [ skip_hw | skip_sw ]\n" |
| 32 |
" object-pinned FILE [ direct-action ] [ skip_hw | skip_sw ]\n" |
| 33 |
"\n" |
| 34 |
"Common remaining options:\n" |
| 35 |
" [ action ACTION_SPEC ]\n" |
| 36 |
" [ classid CLASSID ]\n" |
| 37 |
"\n" |
| 38 |
"Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n" |
| 39 |
"c,t,f,k and s are decimals; s denotes number of 4-tuples\n" |
| 40 |
"\n" |
| 41 |
"Where FILE points to a file containing the BPF_BYTECODE string,\n" |
| 42 |
"an ELF file containing eBPF map definitions and bytecode, or a\n" |
| 43 |
"pinned eBPF program.\n" |
| 44 |
"\n" |
| 45 |
"Where CLS_NAME refers to the section name containing the\n" |
| 46 |
"classifier (default \'%s\').\n" |
| 47 |
"\n" |
| 48 |
"Where UDS_FILE points to a unix domain socket file in order\n" |
| 49 |
"to hand off control of all created eBPF maps to an agent.\n" |
| 50 |
"\n" |
| 51 |
"ACTION_SPEC := ... look at individual actions\n" |
| 52 |
"NOTE: CLASSID is parsed as hexadecimal input.\n", |
| 53 |
bpf_prog_to_default_section(bpf_type)); |
| 54 |
} |
| 55 |
|
| 56 |
static void bpf_cbpf_cb(void *nl, const struct sock_filter *ops, int ops_len) |
| 57 |
{ |
| 58 |
addattr16(nl, MAX_MSG, TCA_BPF_OPS_LEN, ops_len); |
| 59 |
addattr_l(nl, MAX_MSG, TCA_BPF_OPS, ops, |
| 60 |
ops_len * sizeof(struct sock_filter)); |
| 61 |
} |
| 62 |
|
| 63 |
static void bpf_ebpf_cb(void *nl, int fd, const char *annotation) |
| 64 |
{ |
| 65 |
addattr32(nl, MAX_MSG, TCA_BPF_FD, fd); |
| 66 |
addattrstrz(nl, MAX_MSG, TCA_BPF_NAME, annotation); |
| 67 |
} |
| 68 |
|
| 69 |
static const struct bpf_cfg_ops bpf_cb_ops = { |
| 70 |
.cbpf_cb = bpf_cbpf_cb, |
| 71 |
.ebpf_cb = bpf_ebpf_cb, |
| 72 |
}; |
| 73 |
|
| 74 |
static int bpf_parse_opt(const struct filter_util *qu, char *handle, |
| 75 |
int argc, char **argv, struct nlmsghdr *n) |
| 76 |
{ |
| 77 |
const char *bpf_obj = NULL, *bpf_uds_name = NULL; |
| 78 |
struct tcmsg *t = NLMSG_DATA(n); |
| 79 |
unsigned int bpf_gen_flags = 0; |
| 80 |
unsigned int bpf_flags = 0; |
| 81 |
struct bpf_cfg_in cfg = {}; |
| 82 |
bool seen_run = false; |
| 83 |
bool skip_sw = false; |
| 84 |
struct rtattr *tail; |
| 85 |
int ret = 0; |
| 86 |
|
| 87 |
if (handle) { |
| 88 |
if (get_u32(&t->tcm_handle, handle, 0)) { |
| 89 |
fprintf(stderr, "Illegal \"handle\"\n"); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (argc == 0) |
| 95 |
return 0; |
| 96 |
|
| 97 |
tail = (struct rtattr *)(((void *)n) + NLMSG_ALIGN(n->nlmsg_len)); |
| 98 |
addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); |
| 99 |
|
| 100 |
while (argc > 0) { |
| 101 |
if (matches(*argv, "run") == 0) { |
| 102 |
NEXT_ARG(); |
| 103 |
|
| 104 |
if (seen_run) |
| 105 |
duparg("run", *argv); |
| 106 |
opt_bpf: |
| 107 |
seen_run = true; |
| 108 |
cfg.type = bpf_type; |
| 109 |
cfg.argc = argc; |
| 110 |
cfg.argv = argv; |
| 111 |
|
| 112 |
if (bpf_parse_common(&cfg, &bpf_cb_ops) < 0) { |
| 113 |
fprintf(stderr, |
| 114 |
"Unable to parse bpf command line\n"); |
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
argc = cfg.argc; |
| 119 |
argv = cfg.argv; |
| 120 |
|
| 121 |
bpf_obj = cfg.object; |
| 122 |
bpf_uds_name = cfg.uds; |
| 123 |
} else if (matches(*argv, "classid") == 0 || |
| 124 |
matches(*argv, "flowid") == 0) { |
| 125 |
unsigned int classid; |
| 126 |
|
| 127 |
NEXT_ARG(); |
| 128 |
if (get_tc_classid(&classid, *argv)) { |
| 129 |
fprintf(stderr, "Illegal \"classid\"\n"); |
| 130 |
return -1; |
| 131 |
} |
| 132 |
addattr32(n, MAX_MSG, TCA_BPF_CLASSID, classid); |
| 133 |
} else if (matches(*argv, "direct-action") == 0 || |
| 134 |
matches(*argv, "da") == 0) { |
| 135 |
bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; |
| 136 |
} else if (matches(*argv, "skip_hw") == 0) { |
| 137 |
bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_HW; |
| 138 |
} else if (matches(*argv, "skip_sw") == 0) { |
| 139 |
bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_SW; |
| 140 |
skip_sw = true; |
| 141 |
} else if (matches(*argv, "action") == 0) { |
| 142 |
NEXT_ARG(); |
| 143 |
if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) { |
| 144 |
fprintf(stderr, "Illegal \"action\"\n"); |
| 145 |
return -1; |
| 146 |
} |
| 147 |
continue; |
| 148 |
} else if (matches(*argv, "police") == 0) { |
| 149 |
NEXT_ARG(); |
| 150 |
if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) { |
| 151 |
fprintf(stderr, "Illegal \"police\"\n"); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
continue; |
| 155 |
} else if (matches(*argv, "help") == 0) { |
| 156 |
explain(); |
| 157 |
return -1; |
| 158 |
} else { |
| 159 |
if (!seen_run) |
| 160 |
goto opt_bpf; |
| 161 |
|
| 162 |
fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 163 |
explain(); |
| 164 |
return -1; |
| 165 |
} |
| 166 |
|
| 167 |
NEXT_ARG_FWD(); |
| 168 |
} |
| 169 |
|
| 170 |
if (skip_sw) |
| 171 |
cfg.ifindex = t->tcm_ifindex; |
| 172 |
if (bpf_load_common(&cfg, &bpf_cb_ops, n) < 0) { |
| 173 |
fprintf(stderr, "Unable to load program\n"); |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
if (bpf_gen_flags) |
| 178 |
addattr32(n, MAX_MSG, TCA_BPF_FLAGS_GEN, bpf_gen_flags); |
| 179 |
if (bpf_flags) |
| 180 |
addattr32(n, MAX_MSG, TCA_BPF_FLAGS, bpf_flags); |
| 181 |
|
| 182 |
tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail; |
| 183 |
|
| 184 |
if (bpf_uds_name) |
| 185 |
ret = bpf_send_map_fds(bpf_uds_name, bpf_obj); |
| 186 |
|
| 187 |
return ret; |
| 188 |
} |
| 189 |
|
| 190 |
static int bpf_print_opt(const struct filter_util *qu, FILE *f, |
| 191 |
struct rtattr *opt, __u32 handle) |
| 192 |
{ |
| 193 |
struct rtattr *tb[TCA_BPF_MAX + 1]; |
| 194 |
int dump_ok = 0; |
| 195 |
|
| 196 |
if (opt == NULL) |
| 197 |
return 0; |
| 198 |
|
| 199 |
parse_rtattr_nested(tb, TCA_BPF_MAX, opt); |
| 200 |
|
| 201 |
if (handle) |
| 202 |
print_0xhex(PRINT_ANY, "handle", "handle %#llx ", handle); |
| 203 |
|
| 204 |
if (tb[TCA_BPF_CLASSID]) { |
| 205 |
SPRINT_BUF(b1); |
| 206 |
print_string(PRINT_ANY, "flowid", "flowid %s ", |
| 207 |
sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1)); |
| 208 |
} |
| 209 |
|
| 210 |
if (tb[TCA_BPF_NAME]) |
| 211 |
print_string(PRINT_ANY, "bpf_name", "%s ", |
| 212 |
rta_getattr_str(tb[TCA_BPF_NAME])); |
| 213 |
|
| 214 |
if (tb[TCA_BPF_FLAGS]) { |
| 215 |
unsigned int flags = rta_getattr_u32(tb[TCA_BPF_FLAGS]); |
| 216 |
|
| 217 |
if (flags & TCA_BPF_FLAG_ACT_DIRECT) |
| 218 |
print_bool(PRINT_ANY, |
| 219 |
"direct-action", "direct-action ", true); |
| 220 |
} |
| 221 |
|
| 222 |
if (tb[TCA_BPF_FLAGS_GEN]) { |
| 223 |
unsigned int flags = |
| 224 |
rta_getattr_u32(tb[TCA_BPF_FLAGS_GEN]); |
| 225 |
|
| 226 |
if (flags & TCA_CLS_FLAGS_SKIP_HW) |
| 227 |
print_bool(PRINT_ANY, "skip_hw", "skip_hw ", true); |
| 228 |
if (flags & TCA_CLS_FLAGS_SKIP_SW) |
| 229 |
print_bool(PRINT_ANY, "skip_sw", "skip_sw ", true); |
| 230 |
if (flags & TCA_CLS_FLAGS_IN_HW) |
| 231 |
print_bool(PRINT_ANY, "in_hw", "in_hw ", true); |
| 232 |
else if (flags & TCA_CLS_FLAGS_NOT_IN_HW) |
| 233 |
print_bool(PRINT_ANY, |
| 234 |
"not_in_hw", "not_in_hw ", true); |
| 235 |
} |
| 236 |
|
| 237 |
if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN]) |
| 238 |
bpf_print_ops(tb[TCA_BPF_OPS], |
| 239 |
rta_getattr_u16(tb[TCA_BPF_OPS_LEN])); |
| 240 |
|
| 241 |
if (tb[TCA_BPF_ID]) |
| 242 |
dump_ok = bpf_dump_prog_info(f, rta_getattr_u32(tb[TCA_BPF_ID])); |
| 243 |
if (!dump_ok && tb[TCA_BPF_TAG]) { |
| 244 |
SPRINT_BUF(b); |
| 245 |
|
| 246 |
print_string(PRINT_ANY, "tag", "tag %s ", |
| 247 |
hexstring_n2a(RTA_DATA(tb[TCA_BPF_TAG]), |
| 248 |
RTA_PAYLOAD(tb[TCA_BPF_TAG]), b, sizeof(b))); |
| 249 |
} |
| 250 |
|
| 251 |
if (tb[TCA_BPF_POLICE]) { |
| 252 |
print_nl(); |
| 253 |
tc_print_police(tb[TCA_BPF_POLICE]); |
| 254 |
} |
| 255 |
|
| 256 |
if (tb[TCA_BPF_ACT]) |
| 257 |
tc_print_action(f, tb[TCA_BPF_ACT], 0); |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
struct filter_util bpf_filter_util = { |
| 263 |
.id = "bpf", |
| 264 |
.parse_fopt = bpf_parse_opt, |
| 265 |
.print_fopt = bpf_print_opt, |
| 266 |
}; |