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