blob: 37cd0d80b02f3d10ea7fcb717169fbe987d9028d [file] [log] [blame]
Jiri Pirko86ab59a2015-01-19 16:56:30 +01001/*
Daniel Borkmannd937a742015-04-28 13:37:42 +02002 * m_bpf.c BPF based action module
Jiri Pirko86ab59a2015-01-19 16:56:30 +01003 *
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 Borkmann6256f8c2015-04-01 17:57:44 +020010 * Daniel Borkmann <daniel@iogearbox.net>
Jiri Pirko86ab59a2015-01-19 16:56:30 +010011 */
12
13#include <stdio.h>
14#include <stdlib.h>
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010015
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020016#include <linux/bpf.h>
Jiri Pirko86ab59a2015-01-19 16:56:30 +010017#include <linux/tc_act/tc_bpf.h>
18
19#include "utils.h"
Jiri Pirko86ab59a2015-01-19 16:56:30 +010020#include "tc_util.h"
21#include "tc_bpf.h"
22
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020023static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_ACT;
24
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010025static const int nla_tbl[BPF_NLA_MAX] = {
26 [BPF_NLA_OPS_LEN] = TCA_ACT_BPF_OPS_LEN,
27 [BPF_NLA_OPS] = TCA_ACT_BPF_OPS,
28 [BPF_NLA_FD] = TCA_ACT_BPF_FD,
29 [BPF_NLA_NAME] = TCA_ACT_BPF_NAME,
30};
31
Jiri Pirko86ab59a2015-01-19 16:56:30 +010032static void explain(void)
33{
Daniel Borkmannbaed9082015-08-07 11:36:50 +020034 fprintf(stderr, "Usage: ... bpf ... [ index INDEX ]\n");
Jiri Pirko86ab59a2015-01-19 16:56:30 +010035 fprintf(stderr, "\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020036 fprintf(stderr, "BPF use case:\n");
37 fprintf(stderr, " bytecode BPF_BYTECODE\n");
38 fprintf(stderr, " bytecode-file FILE\n");
39 fprintf(stderr, "\n");
40 fprintf(stderr, "eBPF use case:\n");
Daniel Borkmannd937a742015-04-28 13:37:42 +020041 fprintf(stderr, " object-file FILE [ section ACT_NAME ] [ export UDS_FILE ]");
42 fprintf(stderr, " [ verbose ]\n");
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010043 fprintf(stderr, " object-pinned FILE\n");
Jiri Pirko86ab59a2015-01-19 16:56:30 +010044 fprintf(stderr, "\n");
45 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 +020046 fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n");
47 fprintf(stderr, "\n");
48 fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n");
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010049 fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode, or a\n");
50 fprintf(stderr, "pinned eBPF program.\n");
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020051 fprintf(stderr, "\n");
52 fprintf(stderr, "Where ACT_NAME refers to the section name containing the\n");
53 fprintf(stderr, "action (default \'%s\').\n", bpf_default_section(bpf_type));
54 fprintf(stderr, "\n");
55 fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n");
56 fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n");
Daniel Borkmannbaed9082015-08-07 11:36:50 +020057 fprintf(stderr, "\n");
58 fprintf(stderr, "Where optionally INDEX points to an existing action, or\n");
59 fprintf(stderr, "explicitly specifies an action index upon creation.\n");
Jiri Pirko86ab59a2015-01-19 16:56:30 +010060}
61
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010062static int bpf_parse_opt(struct action_util *a, int *ptr_argc, char ***ptr_argv,
63 int tca_id, struct nlmsghdr *n)
Jiri Pirko86ab59a2015-01-19 16:56:30 +010064{
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010065 const char *bpf_obj = NULL, *bpf_uds_name = NULL;
66 struct tc_act_bpf parm;
67 bool seen_run = false;
Jiri Pirko86ab59a2015-01-19 16:56:30 +010068 struct rtattr *tail;
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010069 int argc, ret = 0;
70 char **argv;
71
72 argv = *ptr_argv;
73 argc = *ptr_argc;
Jiri Pirko86ab59a2015-01-19 16:56:30 +010074
75 if (matches(*argv, "bpf") != 0)
76 return -1;
77
78 NEXT_ARG();
79
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010080 tail = NLMSG_TAIL(n);
81 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
82
Jiri Pirko86ab59a2015-01-19 16:56:30 +010083 while (argc > 0) {
84 if (matches(*argv, "run") == 0) {
Jiri Pirko86ab59a2015-01-19 16:56:30 +010085 NEXT_ARG();
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020086opt_bpf:
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020087 seen_run = true;
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010088 if (bpf_parse_common(&argc, &argv, nla_tbl, bpf_type,
89 &bpf_obj, &bpf_uds_name, n)) {
90 fprintf(stderr, "Failed to retrieve (e)BPF data!\n");
Jiri Pirko86ab59a2015-01-19 16:56:30 +010091 return -1;
92 }
Jiri Pirko86ab59a2015-01-19 16:56:30 +010093 } else if (matches(*argv, "help") == 0) {
Daniel Borkmann32e93fb2015-11-13 00:39:29 +010094 explain();
95 return -1;
Daniel Borkmannbaed9082015-08-07 11:36:50 +020096 } else if (matches(*argv, "index") == 0) {
97 break;
Jiri Pirko86ab59a2015-01-19 16:56:30 +010098 } else {
Daniel Borkmann6256f8c2015-04-01 17:57:44 +020099 if (!seen_run)
100 goto opt_bpf;
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100101 break;
102 }
Daniel Borkmann343dc902015-10-08 15:22:05 +0200103
104 NEXT_ARG_FWD();
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100105 }
106
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100107 memset(&parm, 0, sizeof(parm));
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100108 parm.action = TC_ACT_PIPE;
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100109
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100110 if (argc) {
111 if (matches(*argv, "reclassify") == 0) {
112 parm.action = TC_ACT_RECLASSIFY;
Daniel Borkmann343dc902015-10-08 15:22:05 +0200113 NEXT_ARG_FWD();
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100114 } else if (matches(*argv, "pipe") == 0) {
115 parm.action = TC_ACT_PIPE;
Daniel Borkmann343dc902015-10-08 15:22:05 +0200116 NEXT_ARG_FWD();
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100117 } else if (matches(*argv, "drop") == 0 ||
118 matches(*argv, "shot") == 0) {
119 parm.action = TC_ACT_SHOT;
Daniel Borkmann343dc902015-10-08 15:22:05 +0200120 NEXT_ARG_FWD();
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100121 } else if (matches(*argv, "continue") == 0) {
122 parm.action = TC_ACT_UNSPEC;
Daniel Borkmann343dc902015-10-08 15:22:05 +0200123 NEXT_ARG_FWD();
124 } else if (matches(*argv, "pass") == 0 ||
125 matches(*argv, "ok") == 0) {
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100126 parm.action = TC_ACT_OK;
Daniel Borkmann343dc902015-10-08 15:22:05 +0200127 NEXT_ARG_FWD();
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100128 }
129 }
130
131 if (argc) {
132 if (matches(*argv, "index") == 0) {
133 NEXT_ARG();
134 if (get_u32(&parm.index, *argv, 10)) {
135 fprintf(stderr, "bpf: Illegal \"index\"\n");
136 return -1;
137 }
Daniel Borkmann343dc902015-10-08 15:22:05 +0200138
139 NEXT_ARG_FWD();
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100140 }
141 }
142
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100143 addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm));
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100144 tail->rta_len = (char *)NLMSG_TAIL(n) - (char *)tail;
145
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200146 if (bpf_uds_name)
Daniel Borkmann4bd62442015-04-16 21:20:06 +0200147 ret = bpf_send_map_fds(bpf_uds_name, bpf_obj);
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200148
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100149 *ptr_argc = argc;
150 *ptr_argv = argv;
151
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200152 return ret;
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100153}
154
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100155static int bpf_print_opt(struct action_util *au, FILE *f, struct rtattr *arg)
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100156{
157 struct rtattr *tb[TCA_ACT_BPF_MAX + 1];
158 struct tc_act_bpf *parm;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700159
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200160 SPRINT_BUF(action_buf);
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100161
162 if (arg == NULL)
163 return -1;
164
165 parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg);
166
167 if (!tb[TCA_ACT_BPF_PARMS]) {
168 fprintf(f, "[NULL bpf parameters]");
169 return -1;
170 }
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200171
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100172 parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]);
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200173 fprintf(f, "bpf ");
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100174
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200175 if (tb[TCA_ACT_BPF_NAME])
176 fprintf(f, "%s ", rta_getattr_str(tb[TCA_ACT_BPF_NAME]));
177 else if (tb[TCA_ACT_BPF_FD])
178 fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_ACT_BPF_FD]));
179
180 if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) {
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100181 bpf_print_ops(f, tb[TCA_ACT_BPF_OPS],
182 rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN]));
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200183 fprintf(f, " ");
184 }
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100185
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200186 fprintf(f, "default-action %s\n", action_n2a(parm->action, action_buf,
187 sizeof(action_buf)));
188 fprintf(f, "\tindex %d ref %d bind %d", parm->index, parm->refcnt,
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100189 parm->bindcnt);
190
191 if (show_stats) {
192 if (tb[TCA_ACT_BPF_TM]) {
193 struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700194
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100195 print_tm(f, tm);
196 }
197 }
198
199 fprintf(f, "\n ");
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100200 return 0;
201}
202
203struct action_util bpf_action_util = {
Daniel Borkmann32e93fb2015-11-13 00:39:29 +0100204 .id = "bpf",
205 .parse_aopt = bpf_parse_opt,
206 .print_aopt = bpf_print_opt,
Jiri Pirko86ab59a2015-01-19 16:56:30 +0100207};