2 * f_bpf.c BPF-based Classifier
4 * This program is free software; you can distribute 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: Daniel Borkmann <daniel@iogearbox.net>
15 #include <linux/bpf.h>
22 static const enum bpf_prog_type bpf_type
= BPF_PROG_TYPE_SCHED_CLS
;
24 static void explain(void)
26 fprintf(stderr
, "Usage: ... bpf ...\n");
27 fprintf(stderr
, "\n");
28 fprintf(stderr
, "BPF use case:\n");
29 fprintf(stderr
, " bytecode BPF_BYTECODE\n");
30 fprintf(stderr
, " bytecode-file FILE\n");
31 fprintf(stderr
, "\n");
32 fprintf(stderr
, "eBPF use case:\n");
33 fprintf(stderr
, " object-file FILE [ section CLS_NAME ] [ export UDS_FILE ]");
34 fprintf(stderr
, " [ verbose ] [ direct-action ] [ skip_hw | skip_sw ]\n");
35 fprintf(stderr
, " object-pinned FILE [ direct-action ] [ skip_hw | skip_sw ]\n");
36 fprintf(stderr
, "\n");
37 fprintf(stderr
, "Common remaining options:\n");
38 fprintf(stderr
, " [ action ACTION_SPEC ]\n");
39 fprintf(stderr
, " [ classid CLASSID ]\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, or a\n");
46 fprintf(stderr
, "pinned eBPF program.\n");
47 fprintf(stderr
, "\n");
48 fprintf(stderr
, "Where CLS_NAME refers to the section name containing the\n");
49 fprintf(stderr
, "classifier (default \'%s\').\n", bpf_prog_to_default_section(bpf_type
));
50 fprintf(stderr
, "\n");
51 fprintf(stderr
, "Where UDS_FILE points to a unix domain socket file in order\n");
52 fprintf(stderr
, "to hand off control of all created eBPF maps to an agent.\n");
53 fprintf(stderr
, "\n");
54 fprintf(stderr
, "ACTION_SPEC := ... look at individual actions\n");
55 fprintf(stderr
, "NOTE: CLASSID is parsed as hexadecimal input.\n");
58 static void bpf_cbpf_cb(void *nl
, const struct sock_filter
*ops
, int ops_len
)
60 addattr16(nl
, MAX_MSG
, TCA_BPF_OPS_LEN
, ops_len
);
61 addattr_l(nl
, MAX_MSG
, TCA_BPF_OPS
, ops
,
62 ops_len
* sizeof(struct sock_filter
));
65 static void bpf_ebpf_cb(void *nl
, int fd
, const char *annotation
)
67 addattr32(nl
, MAX_MSG
, TCA_BPF_FD
, fd
);
68 addattrstrz(nl
, MAX_MSG
, TCA_BPF_NAME
, annotation
);
71 static const struct bpf_cfg_ops bpf_cb_ops
= {
72 .cbpf_cb
= bpf_cbpf_cb
,
73 .ebpf_cb
= bpf_ebpf_cb
,
76 static int bpf_parse_opt(struct filter_util
*qu
, char *handle
,
77 int argc
, char **argv
, struct nlmsghdr
*n
)
79 const char *bpf_obj
= NULL
, *bpf_uds_name
= NULL
;
80 struct tcmsg
*t
= NLMSG_DATA(n
);
81 unsigned int bpf_gen_flags
= 0;
82 unsigned int bpf_flags
= 0;
83 struct bpf_cfg_in cfg
= {};
84 bool seen_run
= false;
89 if (get_u32(&t
->tcm_handle
, handle
, 0)) {
90 fprintf(stderr
, "Illegal \"handle\"\n");
98 tail
= (struct rtattr
*)(((void *)n
) + NLMSG_ALIGN(n
->nlmsg_len
));
99 addattr_l(n
, MAX_MSG
, TCA_OPTIONS
, NULL
, 0);
102 if (matches(*argv
, "run") == 0) {
109 if (bpf_parse_common(bpf_type
, &cfg
, &bpf_cb_ops
, n
))
115 bpf_obj
= cfg
.object
;
116 bpf_uds_name
= cfg
.uds
;
117 } else if (matches(*argv
, "classid") == 0 ||
118 matches(*argv
, "flowid") == 0) {
122 if (get_tc_classid(&handle
, *argv
)) {
123 fprintf(stderr
, "Illegal \"classid\"\n");
126 addattr32(n
, MAX_MSG
, TCA_BPF_CLASSID
, handle
);
127 } else if (matches(*argv
, "direct-action") == 0 ||
128 matches(*argv
, "da") == 0) {
129 bpf_flags
|= TCA_BPF_FLAG_ACT_DIRECT
;
130 } else if (matches(*argv
, "skip_hw") == 0) {
131 bpf_gen_flags
|= TCA_CLS_FLAGS_SKIP_HW
;
132 } else if (matches(*argv
, "skip_sw") == 0) {
133 bpf_gen_flags
|= TCA_CLS_FLAGS_SKIP_SW
;
134 } else if (matches(*argv
, "action") == 0) {
136 if (parse_action(&argc
, &argv
, TCA_BPF_ACT
, n
)) {
137 fprintf(stderr
, "Illegal \"action\"\n");
141 } else if (matches(*argv
, "police") == 0) {
143 if (parse_police(&argc
, &argv
, TCA_BPF_POLICE
, n
)) {
144 fprintf(stderr
, "Illegal \"police\"\n");
148 } else if (matches(*argv
, "help") == 0) {
155 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
164 addattr32(n
, MAX_MSG
, TCA_BPF_FLAGS_GEN
, bpf_gen_flags
);
166 addattr32(n
, MAX_MSG
, TCA_BPF_FLAGS
, bpf_flags
);
168 tail
->rta_len
= (((void *)n
) + n
->nlmsg_len
) - (void *)tail
;
171 ret
= bpf_send_map_fds(bpf_uds_name
, bpf_obj
);
176 static int bpf_print_opt(struct filter_util
*qu
, FILE *f
,
177 struct rtattr
*opt
, __u32 handle
)
179 struct rtattr
*tb
[TCA_BPF_MAX
+ 1];
185 parse_rtattr_nested(tb
, TCA_BPF_MAX
, opt
);
188 fprintf(f
, "handle 0x%x ", handle
);
190 if (tb
[TCA_BPF_CLASSID
]) {
192 fprintf(f
, "flowid %s ",
193 sprint_tc_classid(rta_getattr_u32(tb
[TCA_BPF_CLASSID
]), b1
));
196 if (tb
[TCA_BPF_NAME
])
197 fprintf(f
, "%s ", rta_getattr_str(tb
[TCA_BPF_NAME
]));
199 if (tb
[TCA_BPF_FLAGS
]) {
200 unsigned int flags
= rta_getattr_u32(tb
[TCA_BPF_FLAGS
]);
202 if (flags
& TCA_BPF_FLAG_ACT_DIRECT
)
203 fprintf(f
, "direct-action ");
206 if (tb
[TCA_BPF_FLAGS_GEN
]) {
208 rta_getattr_u32(tb
[TCA_BPF_FLAGS_GEN
]);
210 if (flags
& TCA_CLS_FLAGS_SKIP_HW
)
211 fprintf(f
, "skip_hw ");
212 if (flags
& TCA_CLS_FLAGS_SKIP_SW
)
213 fprintf(f
, "skip_sw ");
215 if (flags
& TCA_CLS_FLAGS_IN_HW
)
216 fprintf(f
, "in_hw ");
217 else if (flags
& TCA_CLS_FLAGS_NOT_IN_HW
)
218 fprintf(f
, "not_in_hw ");
221 if (tb
[TCA_BPF_OPS
] && tb
[TCA_BPF_OPS_LEN
])
222 bpf_print_ops(f
, tb
[TCA_BPF_OPS
],
223 rta_getattr_u16(tb
[TCA_BPF_OPS_LEN
]));
226 dump_ok
= bpf_dump_prog_info(f
, rta_getattr_u32(tb
[TCA_BPF_ID
]));
227 if (!dump_ok
&& tb
[TCA_BPF_TAG
]) {
230 fprintf(f
, "tag %s ",
231 hexstring_n2a(RTA_DATA(tb
[TCA_BPF_TAG
]),
232 RTA_PAYLOAD(tb
[TCA_BPF_TAG
]),
236 if (tb
[TCA_BPF_POLICE
]) {
238 tc_print_police(f
, tb
[TCA_BPF_POLICE
]);
242 tc_print_action(f
, tb
[TCA_BPF_ACT
], 0);
247 struct filter_util bpf_filter_util
= {
249 .parse_fopt
= bpf_parse_opt
,
250 .print_fopt
= bpf_print_opt
,