]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_bpf.c
tc: full JSON support for 'bpf' actions
[mirror_iproute2.git] / tc / m_bpf.c
1 /*
2 * m_bpf.c BPF based action module
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>
10 * Daniel Borkmann <daniel@iogearbox.net>
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #include <linux/bpf.h>
17 #include <linux/tc_act/tc_bpf.h>
18
19 #include "utils.h"
20
21 #include "tc_util.h"
22 #include "bpf_util.h"
23
24 static const enum bpf_prog_type bpf_type = BPF_PROG_TYPE_SCHED_ACT;
25
26 static void explain(void)
27 {
28 fprintf(stderr, "Usage: ... bpf ... [ index INDEX ]\n");
29 fprintf(stderr, "\n");
30 fprintf(stderr, "BPF use case:\n");
31 fprintf(stderr, " bytecode BPF_BYTECODE\n");
32 fprintf(stderr, " bytecode-file FILE\n");
33 fprintf(stderr, "\n");
34 fprintf(stderr, "eBPF use case:\n");
35 fprintf(stderr, " object-file FILE [ section ACT_NAME ] [ export UDS_FILE ]");
36 fprintf(stderr, " [ verbose ]\n");
37 fprintf(stderr, " object-pinned FILE\n");
38 fprintf(stderr, "\n");
39 fprintf(stderr, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
40 fprintf(stderr, "c,t,f,k and s are decimals; s denotes number of 4-tuples\n");
41 fprintf(stderr, "\n");
42 fprintf(stderr, "Where FILE points to a file containing the BPF_BYTECODE string,\n");
43 fprintf(stderr, "an ELF file containing eBPF map definitions and bytecode, or a\n");
44 fprintf(stderr, "pinned eBPF program.\n");
45 fprintf(stderr, "\n");
46 fprintf(stderr, "Where ACT_NAME refers to the section name containing the\n");
47 fprintf(stderr, "action (default \'%s\').\n", bpf_prog_to_default_section(bpf_type));
48 fprintf(stderr, "\n");
49 fprintf(stderr, "Where UDS_FILE points to a unix domain socket file in order\n");
50 fprintf(stderr, "to hand off control of all created eBPF maps to an agent.\n");
51 fprintf(stderr, "\n");
52 fprintf(stderr, "Where optionally INDEX points to an existing action, or\n");
53 fprintf(stderr, "explicitly specifies an action index upon creation.\n");
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_ACT_BPF_OPS_LEN, ops_len);
59 addattr_l(nl, MAX_MSG, TCA_ACT_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_ACT_BPF_FD, fd);
66 addattrstrz(nl, MAX_MSG, TCA_ACT_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(struct action_util *a, int *ptr_argc, char ***ptr_argv,
75 int tca_id, struct nlmsghdr *n)
76 {
77 const char *bpf_obj = NULL, *bpf_uds_name = NULL;
78 struct tc_act_bpf parm = {};
79 struct bpf_cfg_in cfg = {};
80 bool seen_run = false;
81 struct rtattr *tail;
82 int argc, ret = 0;
83 char **argv;
84
85 argv = *ptr_argv;
86 argc = *ptr_argc;
87
88 if (matches(*argv, "bpf") != 0)
89 return -1;
90
91 NEXT_ARG();
92
93 tail = addattr_nest(n, MAX_MSG, tca_id);
94
95 while (argc > 0) {
96 if (matches(*argv, "run") == 0) {
97 NEXT_ARG();
98
99 if (seen_run)
100 duparg("run", *argv);
101 opt_bpf:
102 seen_run = true;
103 cfg.type = bpf_type;
104 cfg.argc = argc;
105 cfg.argv = argv;
106
107 if (bpf_parse_and_load_common(&cfg, &bpf_cb_ops, n))
108 return -1;
109
110 argc = cfg.argc;
111 argv = cfg.argv;
112
113 bpf_obj = cfg.object;
114 bpf_uds_name = cfg.uds;
115 } else if (matches(*argv, "help") == 0) {
116 explain();
117 return -1;
118 } else if (matches(*argv, "index") == 0) {
119 break;
120 } else {
121 if (!seen_run)
122 goto opt_bpf;
123 break;
124 }
125
126 NEXT_ARG_FWD();
127 }
128
129 parse_action_control_dflt(&argc, &argv, &parm.action,
130 false, TC_ACT_PIPE);
131
132 if (argc) {
133 if (matches(*argv, "index") == 0) {
134 NEXT_ARG();
135 if (get_u32(&parm.index, *argv, 10)) {
136 fprintf(stderr, "bpf: Illegal \"index\"\n");
137 return -1;
138 }
139
140 NEXT_ARG_FWD();
141 }
142 }
143
144 addattr_l(n, MAX_MSG, TCA_ACT_BPF_PARMS, &parm, sizeof(parm));
145 addattr_nest_end(n, tail);
146
147 if (bpf_uds_name)
148 ret = bpf_send_map_fds(bpf_uds_name, bpf_obj);
149
150 *ptr_argc = argc;
151 *ptr_argv = argv;
152
153 return ret;
154 }
155
156 static int bpf_print_opt(struct action_util *au, FILE *f, struct rtattr *arg)
157 {
158 struct rtattr *tb[TCA_ACT_BPF_MAX + 1];
159 struct tc_act_bpf *parm;
160 int d_ok = 0;
161
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 }
171
172 parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]);
173 print_string(PRINT_ANY, "kind", "%s ", "bpf");
174
175 if (tb[TCA_ACT_BPF_NAME])
176 print_string(PRINT_ANY, "bpf_name", "%s ",
177 rta_getattr_str(tb[TCA_ACT_BPF_NAME]));
178 if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) {
179 bpf_print_ops(tb[TCA_ACT_BPF_OPS],
180 rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN]));
181 print_string(PRINT_FP, NULL, "%s", " ");
182 }
183
184 if (tb[TCA_ACT_BPF_ID])
185 d_ok = bpf_dump_prog_info(f,
186 rta_getattr_u32(tb[TCA_ACT_BPF_ID]));
187 if (!d_ok && tb[TCA_ACT_BPF_TAG]) {
188 SPRINT_BUF(b);
189
190 print_string(PRINT_ANY, "tag", "tag %s ",
191 hexstring_n2a(RTA_DATA(tb[TCA_ACT_BPF_TAG]),
192 RTA_PAYLOAD(tb[TCA_ACT_BPF_TAG]),
193 b, sizeof(b)));
194 }
195
196 print_action_control(f, "default-action ", parm->action, _SL_);
197 print_uint(PRINT_ANY, "index", "\t index %u", parm->index);
198 print_int(PRINT_ANY, "ref", " ref %d", parm->refcnt);
199 print_int(PRINT_ANY, "bind", " bind %d", parm->bindcnt);
200
201 if (show_stats) {
202 if (tb[TCA_ACT_BPF_TM]) {
203 struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]);
204
205 print_tm(f, tm);
206 }
207 }
208
209 fprintf(f, "\n ");
210 return 0;
211 }
212
213 struct action_util bpf_action_util = {
214 .id = "bpf",
215 .parse_aopt = bpf_parse_opt,
216 .print_aopt = bpf_print_opt,
217 };