2 * m_bpf.c BPF based action module
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.
9 * Authors: Jiri Pirko <jiri@resnulli.us>
10 * Daniel Borkmann <daniel@iogearbox.net>
19 #include <linux/bpf.h>
20 #include <linux/tc_act/tc_bpf.h>
27 static const enum bpf_prog_type bpf_type
= BPF_PROG_TYPE_SCHED_ACT
;
29 static void explain(void)
31 fprintf(stderr
, "Usage: ... bpf ...\n");
32 fprintf(stderr
, "\n");
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 ]");
39 fprintf(stderr
, " [ verbose ]\n");
40 fprintf(stderr
, "\n");
41 fprintf(stderr
, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
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");
54 static void usage(void)
60 static int parse_bpf(struct action_util
*a
, int *argc_p
, char ***argv_p
,
61 int tca_id
, struct nlmsghdr
*n
)
63 char **argv
= *argv_p
, bpf_name
[256];
65 struct tc_act_bpf parm
= { 0 };
66 struct sock_filter bpf_ops
[BPF_MAXINSNS
];
67 bool ebpf
= false, seen_run
= false;
68 const char *bpf_uds_name
= NULL
;
69 const char *bpf_sec_name
= NULL
;
71 int argc
= *argc_p
, ret
= 0;
75 if (matches(*argv
, "bpf") != 0)
81 if (matches(*argv
, "run") == 0) {
82 bool from_file
, bpf_verbose
;
87 bpf_sec_name
= bpf_default_section(bpf_type
);
91 if (strcmp(*argv
, "bytecode-file") == 0 ||
92 strcmp(*argv
, "bcf") == 0) {
94 } else if (strcmp(*argv
, "bytecode") == 0 ||
95 strcmp(*argv
, "bc") == 0) {
97 } else if (strcmp(*argv
, "object-file") == 0 ||
98 strcmp(*argv
, "obj") == 0) {
101 fprintf(stderr
, "unexpected \"%s\"\n", *argv
);
111 if (strcmp(*argv
, "section") == 0 ||
112 strcmp(*argv
, "sec") == 0) {
114 bpf_sec_name
= *argv
;
117 if (strcmp(*argv
, "export") == 0 ||
118 strcmp(*argv
, "exp") == 0) {
120 bpf_uds_name
= *argv
;
123 if (strcmp(*argv
, "verbose") == 0 ||
124 strcmp(*argv
, "verb") == 0) {
132 ret
= ebpf
? bpf_open_object(bpf_obj
, bpf_type
, bpf_sec_name
,
134 bpf_parse_ops(argc
, argv
, bpf_ops
, from_file
);
136 fprintf(stderr
, "%s\n", ebpf
?
137 "Could not load object" :
138 "Illegal \"bytecode\"");
143 bpf_obj
= basename(bpf_obj
);
145 snprintf(bpf_name
, sizeof(bpf_name
), "%s:[%s]",
146 bpf_obj
, bpf_sec_name
);
152 } else if (matches(*argv
, "help") == 0) {
163 parm
.action
= TC_ACT_PIPE
;
165 if (matches(*argv
, "reclassify") == 0) {
166 parm
.action
= TC_ACT_RECLASSIFY
;
169 } else if (matches(*argv
, "pipe") == 0) {
170 parm
.action
= TC_ACT_PIPE
;
173 } else if (matches(*argv
, "drop") == 0 ||
174 matches(*argv
, "shot") == 0) {
175 parm
.action
= TC_ACT_SHOT
;
178 } else if (matches(*argv
, "continue") == 0) {
179 parm
.action
= TC_ACT_UNSPEC
;
182 } else if (matches(*argv
, "pass") == 0) {
183 parm
.action
= TC_ACT_OK
;
190 if (matches(*argv
, "index") == 0) {
192 if (get_u32(&parm
.index
, *argv
, 10)) {
193 fprintf(stderr
, "bpf: Illegal \"index\"\n");
201 if ((!bpf_len
&& !ebpf
) || (!bpf_fd
&& ebpf
)) {
202 fprintf(stderr
, "bpf: Bytecode needs to be passed\n");
207 tail
= NLMSG_TAIL(n
);
209 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
210 addattr_l(n
, MAX_MSG
, TCA_ACT_BPF_PARMS
, &parm
, sizeof(parm
));
213 addattr32(n
, MAX_MSG
, TCA_ACT_BPF_FD
, bpf_fd
);
214 addattrstrz(n
, MAX_MSG
, TCA_ACT_BPF_NAME
, bpf_name
);
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
));
221 tail
->rta_len
= (char *)NLMSG_TAIL(n
) - (char *)tail
;
227 ret
= bpf_send_map_fds(bpf_uds_name
, bpf_obj
);
232 static int print_bpf(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
234 struct rtattr
*tb
[TCA_ACT_BPF_MAX
+ 1];
235 struct tc_act_bpf
*parm
;
236 SPRINT_BUF(action_buf
);
241 parse_rtattr_nested(tb
, TCA_ACT_BPF_MAX
, arg
);
243 if (!tb
[TCA_ACT_BPF_PARMS
]) {
244 fprintf(f
, "[NULL bpf parameters]");
248 parm
= RTA_DATA(tb
[TCA_ACT_BPF_PARMS
]);
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
]));
257 if (tb
[TCA_ACT_BPF_OPS
] && tb
[TCA_ACT_BPF_OPS_LEN
]) {
258 bpf_print_ops(f
, tb
[TCA_ACT_BPF_OPS
],
259 rta_getattr_u16(tb
[TCA_ACT_BPF_OPS_LEN
]));
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
,
269 if (tb
[TCA_ACT_BPF_TM
]) {
270 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_ACT_BPF_TM
]);
280 struct action_util bpf_action_util
= {
282 .parse_aopt
= parse_bpf
,
283 .print_aopt
= print_bpf
,