Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 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> |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 14 | |
| 15 | #include <linux/bpf.h> |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 16 | |
| 17 | #include "utils.h" |
| 18 | #include "tc_util.h" |
Jiri Pirko | 1d129d1 | 2015-01-19 16:56:29 +0100 | [diff] [blame] | 19 | #include "tc_bpf.h" |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 20 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 21 | static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_CLS; |
| 22 | |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 23 | static 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 Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 30 | static void explain(void) |
| 31 | { |
| 32 | fprintf(stderr, "Usage: ... bpf ...\n"); |
| 33 | fprintf(stderr, "\n"); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 34 | fprintf(stderr, "BPF use case:\n"); |
| 35 | fprintf(stderr, " bytecode BPF_BYTECODE\n"); |
| 36 | fprintf(stderr, " bytecode-file FILE\n"); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 37 | fprintf(stderr, "\n"); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 38 | fprintf(stderr, "eBPF use case:\n"); |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame] | 39 | fprintf(stderr, " object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]"); |
Jakub Kicinski | 87e46a5 | 2016-10-12 16:46:36 +0100 | [diff] [blame] | 40 | fprintf(stderr, " [ verbose ] [ direct-action ] [ skip_hw | skip_sw ]\n"); |
| 41 | fprintf(stderr, " object-pinned FILE [ direct-action ] [ skip_hw | skip_sw ]\n"); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 42 | fprintf(stderr, "\n"); |
| 43 | fprintf(stderr, "Common remaining options:\n"); |
| 44 | fprintf(stderr, " [ action ACTION_SPEC ]\n"); |
| 45 | fprintf(stderr, " [ classid CLASSID ]\n"); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 46 | fprintf(stderr, "\n"); |
| 47 | fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n"); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 48 | fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n"); |
| 49 | fprintf(stderr, "\n"); |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 50 | fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n"); |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 51 | fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode, or a\n"); |
| 52 | fprintf(stderr, "pinned eBPF program.\n"); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 53 | 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 Salim | 863ecb0 | 2014-10-06 07:41:21 -0400 | [diff] [blame] | 61 | fprintf(stderr, "NOTE: CLASSID is parsed as hexadecimal input.\n"); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 64 | static int bpf_parse_opt(struct filter_util *qu, char *handle, |
| 65 | int argc, char **argv, struct nlmsghdr *n) |
| 66 | { |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 67 | const char *bpf_obj = NULL, *bpf_uds_name = NULL; |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 68 | struct tcmsg *t = NLMSG_DATA(n); |
Jakub Kicinski | 87e46a5 | 2016-10-12 16:46:36 +0100 | [diff] [blame] | 69 | unsigned int bpf_gen_flags = 0; |
Daniel Borkmann | faa8a46 | 2015-09-25 12:32:41 +0200 | [diff] [blame] | 70 | unsigned int bpf_flags = 0; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 71 | bool seen_run = false; |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 72 | struct rtattr *tail; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 73 | int ret = 0; |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 74 | |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 75 | if (handle) { |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 76 | if (get_u32(&t->tcm_handle, handle, 0)) { |
| 77 | fprintf(stderr, "Illegal \"handle\"\n"); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | } |
| 81 | |
Daniel Borkmann | 1a03207 | 2016-05-18 11:58:41 +0200 | [diff] [blame] | 82 | if (argc == 0) |
| 83 | return 0; |
| 84 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 85 | tail = (struct rtattr *)(((void *)n) + NLMSG_ALIGN(n->nlmsg_len)); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 86 | addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); |
| 87 | |
| 88 | while (argc > 0) { |
| 89 | if (matches(*argv, "run") == 0) { |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 90 | NEXT_ARG(); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 91 | opt_bpf: |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 92 | seen_run = true; |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 93 | if (bpf_parse_common(&argc, &argv, nla_tbl, bpf_type, |
| 94 | &bpf_obj, &bpf_uds_name, n)) { |
| 95 | fprintf(stderr, "Failed to retrieve (e)BPF data!\n"); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 96 | return -1; |
| 97 | } |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 98 | } else if (matches(*argv, "classid") == 0 || |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 99 | matches(*argv, "flowid") == 0) { |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 100 | unsigned int handle; |
| 101 | |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 102 | NEXT_ARG(); |
| 103 | if (get_tc_classid(&handle, *argv)) { |
| 104 | fprintf(stderr, "Illegal \"classid\"\n"); |
| 105 | return -1; |
| 106 | } |
Daniel Borkmann | faa8a46 | 2015-09-25 12:32:41 +0200 | [diff] [blame] | 107 | addattr32(n, MAX_MSG, TCA_BPF_CLASSID, handle); |
| 108 | } else if (matches(*argv, "direct-action") == 0 || |
| 109 | matches(*argv, "da") == 0) { |
| 110 | bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; |
Jakub Kicinski | 87e46a5 | 2016-10-12 16:46:36 +0100 | [diff] [blame] | 111 | } else if (matches(*argv, "skip_hw") == 0) { |
| 112 | bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_HW; |
| 113 | } else if (matches(*argv, "skip_sw") == 0) { |
| 114 | bpf_gen_flags |= TCA_CLS_FLAGS_SKIP_SW; |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 115 | } else if (matches(*argv, "action") == 0) { |
| 116 | NEXT_ARG(); |
| 117 | if (parse_action(&argc, &argv, TCA_BPF_ACT, n)) { |
| 118 | fprintf(stderr, "Illegal \"action\"\n"); |
| 119 | return -1; |
| 120 | } |
| 121 | continue; |
| 122 | } else if (matches(*argv, "police") == 0) { |
| 123 | NEXT_ARG(); |
| 124 | if (parse_police(&argc, &argv, TCA_BPF_POLICE, n)) { |
| 125 | fprintf(stderr, "Illegal \"police\"\n"); |
| 126 | return -1; |
| 127 | } |
| 128 | continue; |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 129 | } else if (matches(*argv, "help") == 0) { |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 130 | explain(); |
| 131 | return -1; |
| 132 | } else { |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 133 | if (!seen_run) |
| 134 | goto opt_bpf; |
| 135 | |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 136 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 137 | explain(); |
| 138 | return -1; |
| 139 | } |
Daniel Borkmann | faa8a46 | 2015-09-25 12:32:41 +0200 | [diff] [blame] | 140 | |
| 141 | NEXT_ARG_FWD(); |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Jakub Kicinski | 87e46a5 | 2016-10-12 16:46:36 +0100 | [diff] [blame] | 144 | if (bpf_gen_flags) |
| 145 | addattr32(n, MAX_MSG, TCA_BPF_FLAGS_GEN, bpf_gen_flags); |
Daniel Borkmann | faa8a46 | 2015-09-25 12:32:41 +0200 | [diff] [blame] | 146 | if (bpf_obj && bpf_flags) |
| 147 | addattr32(n, MAX_MSG, TCA_BPF_FLAGS, bpf_flags); |
| 148 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 149 | tail->rta_len = (((void *)n) + n->nlmsg_len) - (void *)tail; |
| 150 | |
| 151 | if (bpf_uds_name) |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 152 | ret = bpf_send_map_fds(bpf_uds_name, bpf_obj); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 153 | |
| 154 | return ret; |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static int bpf_print_opt(struct filter_util *qu, FILE *f, |
| 158 | struct rtattr *opt, __u32 handle) |
| 159 | { |
| 160 | struct rtattr *tb[TCA_BPF_MAX + 1]; |
| 161 | |
| 162 | if (opt == NULL) |
| 163 | return 0; |
| 164 | |
| 165 | parse_rtattr_nested(tb, TCA_BPF_MAX, opt); |
| 166 | |
| 167 | if (handle) |
| 168 | fprintf(f, "handle 0x%x ", handle); |
| 169 | |
| 170 | if (tb[TCA_BPF_CLASSID]) { |
| 171 | SPRINT_BUF(b1); |
| 172 | fprintf(f, "flowid %s ", |
| 173 | sprint_tc_classid(rta_getattr_u32(tb[TCA_BPF_CLASSID]), b1)); |
| 174 | } |
| 175 | |
Daniel Borkmann | 11c39b5 | 2015-03-16 19:37:41 +0100 | [diff] [blame] | 176 | if (tb[TCA_BPF_NAME]) |
| 177 | fprintf(f, "%s ", rta_getattr_str(tb[TCA_BPF_NAME])); |
| 178 | else if (tb[TCA_BPF_FD]) |
| 179 | fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_BPF_FD])); |
| 180 | |
Daniel Borkmann | faa8a46 | 2015-09-25 12:32:41 +0200 | [diff] [blame] | 181 | if (tb[TCA_BPF_FLAGS]) { |
| 182 | unsigned int flags = rta_getattr_u32(tb[TCA_BPF_FLAGS]); |
| 183 | |
| 184 | if (flags & TCA_BPF_FLAG_ACT_DIRECT) |
| 185 | fprintf(f, "direct-action "); |
| 186 | } |
| 187 | |
Jakub Kicinski | 87e46a5 | 2016-10-12 16:46:36 +0100 | [diff] [blame] | 188 | if (tb[TCA_BPF_FLAGS_GEN]) { |
| 189 | unsigned int flags = |
| 190 | rta_getattr_u32(tb[TCA_BPF_FLAGS_GEN]); |
| 191 | |
| 192 | if (flags & TCA_CLS_FLAGS_SKIP_HW) |
| 193 | fprintf(f, "skip_hw "); |
| 194 | if (flags & TCA_CLS_FLAGS_SKIP_SW) |
| 195 | fprintf(f, "skip_sw "); |
| 196 | } |
| 197 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 198 | if (tb[TCA_BPF_OPS] && tb[TCA_BPF_OPS_LEN]) { |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 199 | bpf_print_ops(f, tb[TCA_BPF_OPS], |
| 200 | rta_getattr_u16(tb[TCA_BPF_OPS_LEN])); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 201 | fprintf(f, "\n"); |
| 202 | } |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 203 | |
| 204 | if (tb[TCA_BPF_POLICE]) { |
| 205 | fprintf(f, "\n"); |
| 206 | tc_print_police(f, tb[TCA_BPF_POLICE]); |
| 207 | } |
| 208 | |
| 209 | if (tb[TCA_BPF_ACT]) { |
| 210 | tc_print_action(f, tb[TCA_BPF_ACT]); |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | struct filter_util bpf_filter_util = { |
Daniel Borkmann | 32e93fb | 2015-11-13 00:39:29 +0100 | [diff] [blame] | 217 | .id = "bpf", |
| 218 | .parse_fopt = bpf_parse_opt, |
| 219 | .print_fopt = bpf_print_opt, |
Daniel Borkmann | d05df68 | 2013-10-28 12:35:33 +0100 | [diff] [blame] | 220 | }; |