Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 1 | /* |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame^] | 2 | * m_bpf.c BPF based action module |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute 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: Jiri Pirko <jiri@resnulli.us> |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 10 | * Daniel Borkmann <daniel@iogearbox.net> |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <string.h> |
| 17 | #include <stdbool.h> |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 18 | #include <libgen.h> |
| 19 | #include <linux/bpf.h> |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 20 | #include <linux/tc_act/tc_bpf.h> |
| 21 | |
| 22 | #include "utils.h" |
| 23 | #include "rt_names.h" |
| 24 | #include "tc_util.h" |
| 25 | #include "tc_bpf.h" |
| 26 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 27 | static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_ACT; |
| 28 | |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 29 | static void explain(void) |
| 30 | { |
| 31 | fprintf(stderr, "Usage: ... bpf ...\n"); |
| 32 | fprintf(stderr, "\n"); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 33 | fprintf(stderr, "BPF use case:\n"); |
| 34 | fprintf(stderr, " bytecode BPF_BYTECODE\n"); |
| 35 | fprintf(stderr, " bytecode-file FILE\n"); |
| 36 | fprintf(stderr, "\n"); |
| 37 | fprintf(stderr, "eBPF use case:\n"); |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame^] | 38 | fprintf(stderr, " object-file FILE [ section ACT_NAME ] [ export UDS_FILE ]"); |
| 39 | fprintf(stderr, " [ verbose ]\n"); |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 40 | fprintf(stderr, "\n"); |
| 41 | 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] | 42 | fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n"); |
| 43 | fprintf(stderr, "\n"); |
| 44 | fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n"); |
| 45 | fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode.\n"); |
| 46 | fprintf(stderr, "\n"); |
| 47 | fprintf(stderr, "Where ACT_NAME refers to the section name containing the\n"); |
| 48 | fprintf(stderr, "action (default \'%s\').\n", bpf_default_section(bpf_type)); |
| 49 | fprintf(stderr, "\n"); |
| 50 | fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n"); |
| 51 | fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n"); |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void usage(void) |
| 55 | { |
| 56 | explain(); |
| 57 | exit(-1); |
| 58 | } |
| 59 | |
| 60 | static int parse_bpf(struct action_util *a, int *argc_p, char ***argv_p, |
| 61 | int tca_id, struct nlmsghdr *n) |
| 62 | { |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 63 | char **argv = *argv_p, bpf_name[256]; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 64 | struct rtattr *tail; |
| 65 | struct tc_act_bpf parm = { 0 }; |
| 66 | struct sock_filter bpf_ops[BPF_MAXINSNS]; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 67 | bool ebpf = false, seen_run = false; |
| 68 | const char *bpf_uds_name = NULL; |
| 69 | const char *bpf_sec_name = NULL; |
| 70 | char *bpf_obj = NULL; |
| 71 | int argc = *argc_p, ret = 0; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 72 | __u16 bpf_len = 0; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 73 | __u32 bpf_fd = 0; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 74 | |
| 75 | if (matches(*argv, "bpf") != 0) |
| 76 | return -1; |
| 77 | |
| 78 | NEXT_ARG(); |
| 79 | |
| 80 | while (argc > 0) { |
| 81 | if (matches(*argv, "run") == 0) { |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame^] | 82 | bool from_file, bpf_verbose; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 83 | int ret; |
| 84 | |
| 85 | NEXT_ARG(); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 86 | opt_bpf: |
| 87 | bpf_sec_name = bpf_default_section(bpf_type); |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame^] | 88 | bpf_verbose = false; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 89 | seen_run = true; |
| 90 | |
| 91 | if (strcmp(*argv, "bytecode-file") == 0 || |
| 92 | strcmp(*argv, "bcf") == 0) { |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 93 | from_file = true; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 94 | } else if (strcmp(*argv, "bytecode") == 0 || |
| 95 | strcmp(*argv, "bc") == 0) { |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 96 | from_file = false; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 97 | } else if (strcmp(*argv, "object-file") == 0 || |
| 98 | strcmp(*argv, "obj") == 0) { |
| 99 | ebpf = true; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 100 | } else { |
| 101 | fprintf(stderr, "unexpected \"%s\"\n", *argv); |
| 102 | explain(); |
| 103 | return -1; |
| 104 | } |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 105 | |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 106 | NEXT_ARG(); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 107 | if (ebpf) { |
| 108 | bpf_obj = *argv; |
| 109 | NEXT_ARG(); |
| 110 | |
| 111 | if (strcmp(*argv, "section") == 0 || |
| 112 | strcmp(*argv, "sec") == 0) { |
| 113 | NEXT_ARG(); |
| 114 | bpf_sec_name = *argv; |
| 115 | NEXT_ARG(); |
| 116 | } |
| 117 | if (strcmp(*argv, "export") == 0 || |
| 118 | strcmp(*argv, "exp") == 0) { |
| 119 | NEXT_ARG(); |
| 120 | bpf_uds_name = *argv; |
| 121 | NEXT_ARG(); |
| 122 | } |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame^] | 123 | if (strcmp(*argv, "verbose") == 0 || |
| 124 | strcmp(*argv, "verb") == 0) { |
| 125 | bpf_verbose = true; |
| 126 | NEXT_ARG(); |
| 127 | } |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 128 | |
| 129 | PREV_ARG(); |
| 130 | } |
| 131 | |
Daniel Borkmann | d937a74 | 2015-04-28 13:37:42 +0200 | [diff] [blame^] | 132 | ret = ebpf ? bpf_open_object(bpf_obj, bpf_type, bpf_sec_name, |
| 133 | bpf_verbose) : |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 134 | bpf_parse_ops(argc, argv, bpf_ops, from_file); |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 135 | if (ret < 0) { |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 136 | fprintf(stderr, "%s\n", ebpf ? |
| 137 | "Could not load object" : |
| 138 | "Illegal \"bytecode\""); |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 139 | return -1; |
| 140 | } |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 141 | |
| 142 | if (ebpf) { |
| 143 | bpf_obj = basename(bpf_obj); |
| 144 | |
| 145 | snprintf(bpf_name, sizeof(bpf_name), "%s:[%s]", |
| 146 | bpf_obj, bpf_sec_name); |
| 147 | |
| 148 | bpf_fd = ret; |
| 149 | } else { |
| 150 | bpf_len = ret; |
| 151 | } |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 152 | } else if (matches(*argv, "help") == 0) { |
| 153 | usage(); |
| 154 | } else { |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 155 | if (!seen_run) |
| 156 | goto opt_bpf; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | argc--; |
| 160 | argv++; |
| 161 | } |
| 162 | |
| 163 | parm.action = TC_ACT_PIPE; |
| 164 | if (argc) { |
| 165 | if (matches(*argv, "reclassify") == 0) { |
| 166 | parm.action = TC_ACT_RECLASSIFY; |
Daniel Borkmann | 51cf367 | 2015-03-18 10:13:34 +0100 | [diff] [blame] | 167 | argc--; |
| 168 | argv++; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 169 | } else if (matches(*argv, "pipe") == 0) { |
| 170 | parm.action = TC_ACT_PIPE; |
Daniel Borkmann | 51cf367 | 2015-03-18 10:13:34 +0100 | [diff] [blame] | 171 | argc--; |
| 172 | argv++; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 173 | } else if (matches(*argv, "drop") == 0 || |
| 174 | matches(*argv, "shot") == 0) { |
| 175 | parm.action = TC_ACT_SHOT; |
Daniel Borkmann | 51cf367 | 2015-03-18 10:13:34 +0100 | [diff] [blame] | 176 | argc--; |
| 177 | argv++; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 178 | } else if (matches(*argv, "continue") == 0) { |
| 179 | parm.action = TC_ACT_UNSPEC; |
Daniel Borkmann | 51cf367 | 2015-03-18 10:13:34 +0100 | [diff] [blame] | 180 | argc--; |
| 181 | argv++; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 182 | } else if (matches(*argv, "pass") == 0) { |
| 183 | parm.action = TC_ACT_OK; |
Daniel Borkmann | 51cf367 | 2015-03-18 10:13:34 +0100 | [diff] [blame] | 184 | argc--; |
| 185 | argv++; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | if (argc) { |
| 190 | if (matches(*argv, "index") == 0) { |
| 191 | NEXT_ARG(); |
| 192 | if (get_u32(&parm.index, *argv, 10)) { |
| 193 | fprintf(stderr, "bpf: Illegal \"index\"\n"); |
| 194 | return -1; |
| 195 | } |
| 196 | argc--; |
| 197 | argv++; |
| 198 | } |
| 199 | } |
| 200 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 201 | if ((!bpf_len && !ebpf) || (!bpf_fd && ebpf)) { |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 202 | fprintf(stderr, "bpf: Bytecode needs to be passed\n"); |
| 203 | explain(); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | tail = NLMSG_TAIL(n); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 208 | |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 209 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); |
| 210 | addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm)); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 211 | |
| 212 | if (ebpf) { |
| 213 | addattr32(n, MAX_MSG, TCA_ACT_BPF_FD, bpf_fd); |
| 214 | addattrstrz(n, MAX_MSG, TCA_ACT_BPF_NAME, bpf_name); |
| 215 | } else { |
| 216 | addattr16(n, MAX_MSG, TCA_ACT_BPF_OPS_LEN, bpf_len); |
| 217 | addattr_l(n, MAX_MSG, TCA_ACT_BPF_OPS, &bpf_ops, |
| 218 | bpf_len * sizeof(struct sock_filter)); |
| 219 | } |
| 220 | |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 221 | tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail; |
| 222 | |
| 223 | *argc_p = argc; |
| 224 | *argv_p = argv; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 225 | |
| 226 | if (bpf_uds_name) |
Daniel Borkmann | 4bd6244 | 2015-04-16 21:20:06 +0200 | [diff] [blame] | 227 | ret = bpf_send_map_fds(bpf_uds_name, bpf_obj); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 228 | |
| 229 | return ret; |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static int print_bpf(struct action_util *au, FILE *f, struct rtattr *arg) |
| 233 | { |
| 234 | struct rtattr *tb[TCA_ACT_BPF_MAX + 1]; |
| 235 | struct tc_act_bpf *parm; |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 236 | SPRINT_BUF(action_buf); |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 237 | |
| 238 | if (arg == NULL) |
| 239 | return -1; |
| 240 | |
| 241 | parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg); |
| 242 | |
| 243 | if (!tb[TCA_ACT_BPF_PARMS]) { |
| 244 | fprintf(f, "[NULL bpf parameters]"); |
| 245 | return -1; |
| 246 | } |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 247 | |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 248 | parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]); |
| 249 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 250 | fprintf(f, "bpf "); |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 251 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 252 | if (tb[TCA_ACT_BPF_NAME]) |
| 253 | fprintf(f, "%s ", rta_getattr_str(tb[TCA_ACT_BPF_NAME])); |
| 254 | else if (tb[TCA_ACT_BPF_FD]) |
| 255 | fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_ACT_BPF_FD])); |
| 256 | |
| 257 | if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) { |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 258 | bpf_print_ops(f, tb[TCA_ACT_BPF_OPS], |
| 259 | rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN])); |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 260 | fprintf(f, " "); |
| 261 | } |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 262 | |
Daniel Borkmann | 6256f8c | 2015-04-01 17:57:44 +0200 | [diff] [blame] | 263 | fprintf(f, "default-action %s\n", action_n2a(parm->action, action_buf, |
| 264 | sizeof(action_buf))); |
| 265 | fprintf(f, "\tindex %d ref %d bind %d", parm->index, parm->refcnt, |
Jiri Pirko | 86ab59a | 2015-01-19 16:56:30 +0100 | [diff] [blame] | 266 | parm->bindcnt); |
| 267 | |
| 268 | if (show_stats) { |
| 269 | if (tb[TCA_ACT_BPF_TM]) { |
| 270 | struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]); |
| 271 | print_tm(f, tm); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | fprintf(f, "\n "); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | struct action_util bpf_action_util = { |
| 281 | .id = "bpf", |
| 282 | .parse_aopt = parse_bpf, |
| 283 | .print_aopt = print_bpf, |
| 284 | }; |