2 * m_bpf.c BFP 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 ]\n");
39 fprintf(stderr
, "\n");
40 fprintf(stderr
, "Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n");
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");
53 static void usage(void)
59 static int parse_bpf(struct action_util
*a
, int *argc_p
, char ***argv_p
,
60 int tca_id
, struct nlmsghdr
*n
)
62 char **argv
= *argv_p
, bpf_name
[256];
64 struct tc_act_bpf parm
= { 0 };
65 struct sock_filter bpf_ops
[BPF_MAXINSNS
];
66 bool ebpf
= false, seen_run
= false;
67 const char *bpf_uds_name
= NULL
;
68 const char *bpf_sec_name
= NULL
;
70 int argc
= *argc_p
, ret
= 0;
74 if (matches(*argv
, "bpf") != 0)
80 if (matches(*argv
, "run") == 0) {
86 bpf_sec_name
= bpf_default_section(bpf_type
);
89 if (strcmp(*argv
, "bytecode-file") == 0 ||
90 strcmp(*argv
, "bcf") == 0) {
92 } else if (strcmp(*argv
, "bytecode") == 0 ||
93 strcmp(*argv
, "bc") == 0) {
95 } else if (strcmp(*argv
, "object-file") == 0 ||
96 strcmp(*argv
, "obj") == 0) {
99 fprintf(stderr
, "unexpected \"%s\"\n", *argv
);
109 if (strcmp(*argv
, "section") == 0 ||
110 strcmp(*argv
, "sec") == 0) {
112 bpf_sec_name
= *argv
;
115 if (strcmp(*argv
, "export") == 0 ||
116 strcmp(*argv
, "exp") == 0) {
118 bpf_uds_name
= *argv
;
125 ret
= ebpf
? bpf_open_object(bpf_obj
, bpf_type
, bpf_sec_name
) :
126 bpf_parse_ops(argc
, argv
, bpf_ops
, from_file
);
128 fprintf(stderr
, "%s\n", ebpf
?
129 "Could not load object" :
130 "Illegal \"bytecode\"");
135 bpf_obj
= basename(bpf_obj
);
137 snprintf(bpf_name
, sizeof(bpf_name
), "%s:[%s]",
138 bpf_obj
, bpf_sec_name
);
144 } else if (matches(*argv
, "help") == 0) {
155 parm
.action
= TC_ACT_PIPE
;
157 if (matches(*argv
, "reclassify") == 0) {
158 parm
.action
= TC_ACT_RECLASSIFY
;
161 } else if (matches(*argv
, "pipe") == 0) {
162 parm
.action
= TC_ACT_PIPE
;
165 } else if (matches(*argv
, "drop") == 0 ||
166 matches(*argv
, "shot") == 0) {
167 parm
.action
= TC_ACT_SHOT
;
170 } else if (matches(*argv
, "continue") == 0) {
171 parm
.action
= TC_ACT_UNSPEC
;
174 } else if (matches(*argv
, "pass") == 0) {
175 parm
.action
= TC_ACT_OK
;
182 if (matches(*argv
, "index") == 0) {
184 if (get_u32(&parm
.index
, *argv
, 10)) {
185 fprintf(stderr
, "bpf: Illegal \"index\"\n");
193 if ((!bpf_len
&& !ebpf
) || (!bpf_fd
&& ebpf
)) {
194 fprintf(stderr
, "bpf: Bytecode needs to be passed\n");
199 tail
= NLMSG_TAIL(n
);
201 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
202 addattr_l(n
, MAX_MSG
, TCA_ACT_BPF_PARMS
, &parm
, sizeof(parm
));
205 addattr32(n
, MAX_MSG
, TCA_ACT_BPF_FD
, bpf_fd
);
206 addattrstrz(n
, MAX_MSG
, TCA_ACT_BPF_NAME
, bpf_name
);
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
));
213 tail
->rta_len
= (char *)NLMSG_TAIL(n
) - (char *)tail
;
219 ret
= bpf_send_map_fds(bpf_uds_name
, bpf_obj
);
224 static int print_bpf(struct action_util
*au
, FILE *f
, struct rtattr
*arg
)
226 struct rtattr
*tb
[TCA_ACT_BPF_MAX
+ 1];
227 struct tc_act_bpf
*parm
;
228 SPRINT_BUF(action_buf
);
233 parse_rtattr_nested(tb
, TCA_ACT_BPF_MAX
, arg
);
235 if (!tb
[TCA_ACT_BPF_PARMS
]) {
236 fprintf(f
, "[NULL bpf parameters]");
240 parm
= RTA_DATA(tb
[TCA_ACT_BPF_PARMS
]);
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
]));
249 if (tb
[TCA_ACT_BPF_OPS
] && tb
[TCA_ACT_BPF_OPS_LEN
]) {
250 bpf_print_ops(f
, tb
[TCA_ACT_BPF_OPS
],
251 rta_getattr_u16(tb
[TCA_ACT_BPF_OPS_LEN
]));
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
,
261 if (tb
[TCA_ACT_BPF_TM
]) {
262 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_ACT_BPF_TM
]);
272 struct action_util bpf_action_util
= {
274 .parse_aopt
= parse_bpf
,
275 .print_aopt
= print_bpf
,