Skip to content

Commit 7d1d65c

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: sched: cls_bpf: add BPF-based classifier
This work contains a lightweight BPF-based traffic classifier that can serve as a flexible alternative to ematch-based tree classification, i.e. now that BPF filter engine can also be JITed in the kernel. Naturally, tc actions and policies are supported as well with cls_bpf. Multiple BPF programs/filter can be attached for a class, or they can just as well be written within a single BPF program, that's really up to the user how he wishes to run/optimize the code, e.g. also for inversion of verdicts etc. The notion of a BPF program's return/exit codes is being kept as follows: 0: No match -1: Select classid given in "tc filter ..." command else: flowid, overwrite the default one As a minimal usage example with iproute2, we use a 3 band prio root qdisc on a router with sfq each as leave, and assign ssh and icmp bpf-based filters to band 1, http traffic to band 2 and the rest to band 3. For the first two bands we load the bytecode from a file, in the 2nd we load it inline as an example: echo 1 > /proc/sys/net/core/bpf_jit_enable tc qdisc del dev em1 root tc qdisc add dev em1 root handle 1: prio bands 3 priomap 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 tc qdisc add dev em1 parent 1:1 sfq perturb 16 tc qdisc add dev em1 parent 1:2 sfq perturb 16 tc qdisc add dev em1 parent 1:3 sfq perturb 16 tc filter add dev em1 parent 1: bpf run bytecode-file /etc/tc/ssh.bpf flowid 1:1 tc filter add dev em1 parent 1: bpf run bytecode-file /etc/tc/icmp.bpf flowid 1:1 tc filter add dev em1 parent 1: bpf run bytecode-file /etc/tc/http.bpf flowid 1:2 tc filter add dev em1 parent 1: bpf run bytecode "`bpfc -f tc -i misc.ops`" flowid 1:3 BPF programs can be easily created and passed to tc, either as inline 'bytecode' or 'bytecode-file'. There are a couple of front-ends that can compile opcodes, for example: 1) People familiar with tcpdump-like filters: tcpdump -iem1 -ddd port 22 | tr '\n' ',' > /etc/tc/ssh.bpf 2) People that want to low-level program their filters or use BPF extensions that lack support by libpcap's compiler: bpfc -f tc -i ssh.ops > /etc/tc/ssh.bpf ssh.ops example code: ldh [12] jne #0x800, drop ldb [23] jneq coolya#6, drop ldh [20] jset #0x1fff, drop ldxb 4 * ([14] & 0xf) ldh [%x + 14] jeq #0x16, pass ldh [%x + 16] jne #0x16, drop pass: ret #-1 drop: ret #0 It was chosen to load bytecode into tc, since the reverse operation, tc filter list dev em1, is then able to show the exact commands again. Possible follow-up work could also include a small expression compiler for iproute2. Tested with the help of bmon. This idea came up during the Netfilter Workshop 2013 in Copenhagen. Also thanks to feedback from Eric Dumazet! Signed-off-by: Daniel Borkmann <[email protected]> Cc: Thomas Graf <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d549c76 commit 7d1d65c

4 files changed

Lines changed: 410 additions & 0 deletions

File tree

include/uapi/linux/pkt_cls.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,20 @@ enum {
388388

389389
#define TCA_CGROUP_MAX (__TCA_CGROUP_MAX - 1)
390390

391+
/* BPF classifier */
392+
393+
enum {
394+
TCA_BPF_UNSPEC,
395+
TCA_BPF_ACT,
396+
TCA_BPF_POLICE,
397+
TCA_BPF_CLASSID,
398+
TCA_BPF_OPS_LEN,
399+
TCA_BPF_OPS,
400+
__TCA_BPF_MAX,
401+
};
402+
403+
#define TCA_BPF_MAX (__TCA_BPF_MAX - 1)
404+
391405
/* Extended Matches */
392406

393407
struct tcf_ematch_tree_hdr {

net/sched/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,16 @@ config NET_CLS_CGROUP
443443
To compile this code as a module, choose M here: the
444444
module will be called cls_cgroup.
445445

446+
config NET_CLS_BPF
447+
tristate "BPF-based classifier"
448+
select NET_CLS
449+
---help---
450+
If you say Y here, you will be able to classify packets based on
451+
programmable BPF (JIT'ed) filters as an alternative to ematches.
452+
453+
To compile this code as a module, choose M here: the module will
454+
be called cls_bpf.
455+
446456
config NET_EMATCH
447457
bool "Extended Matches"
448458
select NET_CLS

net/sched/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ obj-$(CONFIG_NET_CLS_RSVP6) += cls_rsvp6.o
5050
obj-$(CONFIG_NET_CLS_BASIC) += cls_basic.o
5151
obj-$(CONFIG_NET_CLS_FLOW) += cls_flow.o
5252
obj-$(CONFIG_NET_CLS_CGROUP) += cls_cgroup.o
53+
obj-$(CONFIG_NET_CLS_BPF) += cls_bpf.o
5354
obj-$(CONFIG_NET_EMATCH) += ematch.o
5455
obj-$(CONFIG_NET_EMATCH_CMP) += em_cmp.o
5556
obj-$(CONFIG_NET_EMATCH_NBYTE) += em_nbyte.o

0 commit comments

Comments
 (0)