tc: add BPF based action
Subsystems (5)
Jiri Pirko
maintainerPATCH 1/1 tc: add BPF based action
This action provides a possibility to exec custom BPF code.
--- include/net/tc_act/tc_bpf.h | 25 +++++ include/uapi/linux/tc_act/Kbuild | 1 + include/uapi/linux/tc_act/tc_bpf.h | 31 ++++++ net/sched/Kconfig | 11 +++ net/sched/Makefile | 1 + net/sched/act_bpf.c | 196 +++++++++++++++++++++++++++++++++++++ 6 files changed, 265 insertions(+) create mode 100644 include/net/tc_act/tc_bpf.h create mode 100644 include/uapi/linux/tc_act/tc_bpf.h create mode 100644 net/sched/act_bpf.c
include/net/tc_act/tc_bpf.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* Copyright (c) 2015 Jiri Pirko <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +*/ + +#ifndef __NET_TC_BPF_H +#define __NET_TC_BPF_H + +#include <linux/filter.h> +#include <net/act_api.h> + +struct tcf_bpf { +struct tcf_common common; +struct bpf_prog *filter; +struct sock_filter *bpf_ops; +u16 bpf_len; +}; +#define to_bpf (a) \ + container_of(a->priv, struct tcf_bpf, common) + +#endif /* __NET_TC_BPF_H */
include/uapi/linux/tc_act/Kbuild +1 -0
@@ -8,3 +8,4 @@ header-y += tc_nat.h header-y += tc_pedit.h header-y += tc_skbedit.h header-y += tc_vlan.h +header-y += tc_bpf.h
include/uapi/linux/tc_act/tc_bpf.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* Copyright (c) 2015 Jiri Pirko <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +*/ + +#ifndef __LINUX_TC_BPF_H +#define __LINUX_TC_BPF_H + +#include <linux/pkt_cls.h> + +#define TCA_ACT_BPF 13 + +struct tc_act_bpf { +tc_gen ; +}; + +enum { +TCA_ACT_BPF_UNSPEC , +TCA_ACT_BPF_TM , +TCA_ACT_BPF_PARMS , +TCA_ACT_BPF_OPS_LEN , +TCA_ACT_BPF_OPS , + __TCA_ACT_BPF_MAX, +}; +#define TCA_ACT_BPF_MAX (__TCA_ACT_BPF_MAX - 1) + +#endif
net/sched/Kconfig +11 -0
@@ -698,6 +698,17 @@ config NET_ACT_VLAN To compile this code as a module, choose M here: the module will be called act_vlan. +config NET_ACT_BPF + tristate "BPF based action" + depends on NET_CLS_ACT + ---help--- + Say Y here to execute BFP code on packets. + + If unsure, say N. + + To compile this code as a module, choose M here: the + module will be called act_bpf. + config NET_CLS_IND bool "Incoming device classification" depends on NET_CLS_U32 || NET_CLS_FW
net/sched/Makefile +1 -0
@@ -17,6 +17,7 @@ obj-$(CONFIG_NET_ACT_SIMP) += act_simple.o obj-$(CONFIG_NET_ACT_SKBEDIT) += act_skbedit.o obj-$(CONFIG_NET_ACT_CSUM) += act_csum.o obj-$(CONFIG_NET_ACT_VLAN) += act_vlan.o +obj-$(CONFIG_NET_ACT_BPF) += act_bpf.o obj-$(CONFIG_NET_SCH_FIFO) += sch_fifo.o obj-$(CONFIG_NET_SCH_CBQ) += sch_cbq.o obj-$(CONFIG_NET_SCH_HTB) += sch_htb.o
net/sched/act_bpf.c new file +196 -0
196 lines not on this page · raw message
Signature · 1 line
1.9.3
Jiri Pirko
maintainerPATCH 1/2 tc: push bpf common code into separate file
--- tc/Makefile | 2 +- tc/f_bpf.c | 136 +++++-------------------------------------------------- tc/tc_bpf.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tc/tc_bpf.h | 28 ++++++++++++ 4 files changed, 186 insertions(+), 126 deletions(-) create mode 100644 tc/tc_bpf.c create mode 100644 tc/tc_bpf.h
tc/Makefile +1 -1
@@ -1,5 +1,5 @@ TCOBJ= tc.o tc_qdisc.o tc_class.o tc_filter.o tc_util.o \ - tc_monitor.o m_police.o m_estimator.o m_action.o \ + tc_monitor.o tc_bpf.o m_police.o m_estimator.o m_action.o \ m_ematch.o emp_ematch.yacc.o emp_ematch.lex.o include ../Config
tc/f_bpf.c +11 -125
163 lines not on this page · raw message
tc/tc_bpf.c new file +146 -0
146 lines not on this page · raw message
tc/tc_bpf.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* tc_bpf.h BPF common code +* +* This program is free software; you can distribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version +* 2 of the License, or (at your option) any later version. +* +* Authors: Daniel Borkmann <[email protected]> +* Jiri Pirko <[email protected]> +*/ + +#ifndef _TC_BPF_H_ +#define _TC_BPF_H_ 1 + +#include <stdio.h> +#include <linux/filter.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> + +int bpf_parse_string (char *arg,bool from_file,__u16 *bpf_len, +char **bpf_string,bool *need_release, +const char separator); +int bpf_parse_ops (int argc,char **argv,struct sock_filter *bpf_ops, +bool from_file); +void bpf_print_ops (FILE *f,struct rtattr *bpf_ops,__u16 len); + +#endif
Signature · 1 line
1.9.3
Jiri Pirko
maintainerPATCH 2/2 tc: add support for BPF based actions
--- include/linux/tc_act/tc_bpf.h | 31 +++++++ tc/Makefile | 1 + tc/m_bpf.c | 183 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 include/linux/tc_act/tc_bpf.h create mode 100644 tc/m_bpf.c
include/linux/tc_act/tc_bpf.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* Copyright (c) 2015 Jiri Pirko <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +*/ + +#ifndef __LINUX_TC_BPF_H +#define __LINUX_TC_BPF_H + +#include <linux/pkt_cls.h> + +#define TCA_ACT_BPF 13 + +struct tc_act_bpf { +tc_gen ; +}; + +enum { +TCA_ACT_BPF_UNSPEC , +TCA_ACT_BPF_TM , +TCA_ACT_BPF_PARMS , +TCA_ACT_BPF_OPS_LEN , +TCA_ACT_BPF_OPS , + __TCA_ACT_BPF_MAX, +}; +#define TCA_ACT_BPF_MAX (__TCA_ACT_BPF_MAX - 1) + +#endif
tc/Makefile +1 -0
@@ -41,6 +41,7 @@ TCMODULES += m_skbedit.o TCMODULES += m_csum.o TCMODULES += m_simple.o TCMODULES += m_vlan.o +TCMODULES += m_bpf.o TCMODULES += p_ip.o TCMODULES += p_icmp.o TCMODULES += p_tcp.o
tc/m_bpf.c new file +183 -0
183 lines not on this page · raw message
Signature · 1 line
1.9.3
Re: [patch iproute2 2/2] tc: add support for BPF based actions
tc/m_bpf.c line 35
Quoted 10 lines · Jiri Pirko
tc/m_bpf.c new file +10
-0+fprintf (stderr,"Usage: ... bpf ...\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr," [inline]: run bytecode BPF_BYTECODE\n" ); +fprintf (stderr," [from file]: run bytecode-file FILE\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr,"Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n" ); +fprintf (stderr," c,t,f,k and s are decimals; s denotes number of 4-tuples\n" ); +fprintf (stderr,"Where FILE points to a file containing the BPF_BYTECODE string\n" ); +fprintf (stderr,"\nACTION_SPEC := ... look at individual actions\n" ); +fprintf (stderr,"NOTE: CLASSID is parsed as hexadecimal input.\n" );
Can we just use BPF transparently for gact? It is never user-friendly to use this kind of bytecode even though I know there is a tool to "compile" BPF.
Jiri Pirko
maintainerRe: [patch iproute2 2/2] tc: add support for BPF based actions
Quoted 12 lines · Wed, Jan 07, 2015 at 07:50:47PM CET, [email protected] wrote:
tc/m_bpf.c line 35
Quoted 10 lines · Jiri Pirko
tc/m_bpf.c new file +10
-0+fprintf (stderr,"Usage: ... bpf ...\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr," [inline]: run bytecode BPF_BYTECODE\n" ); +fprintf (stderr," [from file]: run bytecode-file FILE\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr,"Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n" ); +fprintf (stderr," c,t,f,k and s are decimals; s denotes number of 4-tuples\n" ); +fprintf (stderr,"Where FILE points to a file containing the BPF_BYTECODE string\n" ); +fprintf (stderr,"\nACTION_SPEC := ... look at individual actions\n" ); +fprintf (stderr,"NOTE: CLASSID is parsed as hexadecimal input.\n" );
Quoted 1 line
Can we just use BPF transparently for gact?
Why to squash it there? I think it is much clearer to do this separatelly.
Quoted 3 lines
It is never user-friendly to
use this kind of bytecode even though I know there is a tool to "compile" BPF.
Please see cls_bpf. It's already in-tree for some time. act_bpf just completes this.
Re: [patch iproute2 2/2] tc: add support for BPF based actions
Quoted 13 lines · Jiri Pirko
Quoted 12 lines · Wed, Jan 07, 2015 at 07:50:47PM CET, [email protected] wrote:
Quoted 10 lines · Jiri Pirko
tc/m_bpf.c new file +10
-0+fprintf (stderr,"Usage: ... bpf ...\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr," [inline]: run bytecode BPF_BYTECODE\n" ); +fprintf (stderr," [from file]: run bytecode-file FILE\n" ); +fprintf (stderr,"\n" ); +fprintf (stderr,"Where BPF_BYTECODE := \'s,c t f k,c t f k,c t f k,...\'\n" ); +fprintf (stderr," c,t,f,k and s are decimals; s denotes number of 4-tuples\n" ); +fprintf (stderr,"Where FILE points to a file containing the BPF_BYTECODE string\n" ); +fprintf (stderr,"\nACTION_SPEC := ... look at individual actions\n" ); +fprintf (stderr,"NOTE: CLASSID is parsed as hexadecimal input.\n" );
Quoted 5 lines
Quoted 1 line
Can we just use BPF transparently for gact?
Why to squash it there? I think it is much clearer to do this separatelly.
Because they are both intended to drop/pass/pipe packets, we don't have to make a separated one just because one is using BPF one isn't.
Quoted 6 lines
Quoted 3 lines
It is never user-friendly to
use this kind of bytecode even though I know there is a tool to "compile" BPF.Please see
cls_bpf. It's already in-tree for some time.act_bpfjust completes this.
Yeah, that is what I hate too.
Re: [patch iproute2 2/2] tc: add support for BPF based actions
In reply to Jiri PirkoQuoted 3 lines · On Wed, 7 Jan 2015 17:47:24 +0100 Jiri Pirko <[email protected]> wrote:
Signed-off-by Jiri Pirko <[email protected]>--- include/linux/tc_act/tc_bpf.h | 31 +++++++
The overall concept is fine, but the mechanics of file management gets in the way.
All header files in the linux directory of iproute2 must come from sanitized version of kernel headers that are in uapi/linux in the kernel source.
I take headers from 'make install_headers' and copy them over to keep in sync. The headers for master branch are kept in sync with upstream kernel (from Linus tree), and for net-next branch I derive them from Davem's net-next tree.
If you want to use tc_bpf.h it has to be in upstream kernel first.
Sorry
Jiri Pirko
maintainerRe: [patch iproute2 2/2] tc: add support for BPF based actions
Quoted 13 lines · Wed, Jan 14, 2015 at 02:19:17AM CET, [email protected] wrote:
Quoted 3 lines · On Wed, 7 Jan 2015 17:47:24 +0100 Jiri Pirko <[email protected]> wrote:
Signed-off-by Jiri Pirko <[email protected]>--- include/linux/tc_act/tc_bpf.h | 31 +++++++The overall concept is fine, but the mechanics of file management gets in the way.
All header files in the linux directory of iproute2 must come from sanitized version of kernel headers that are in uapi/linux in the kernel source.
Quoted 6 lines
I take headers from 'make
install_headers' and copy them over to keep in sync. The headers for master branch are kept in sync with upstream kernel (from Linus tree), and for net-next branch I derive them from Davem's net-next tree.If you want to use
tc_bpf.hit has to be in upstream kernel first.
Sure. I will reposet these patches once act_bpf code is applied to net-next kernel tree. Thanks!
Quoted 1 line · Jiri Pirko
This action provides a possibility to exec custom BPF code.
Can you elaborate a bit more on the particular use-case, and what scenarios are unsolveable with the BPF filter we already have in tc? Just wondering, since you're using BPF for the purpose of classifying (but just from the context of actions) what about a possibility of a generic container for reusing (any) classifier from the framework, so we would not need to duplicate code?
On the other hand, I would understand if it's at some point in time eBPF which would f.e. mangle the packet, but the API you propose is clearly classic BPF. ;)
Thanks,
Daniel
Quoted 5 lines · Daniel Borkmann
Quoted 2 lines · Jiri Pirko
This action provides a possibility to exec custom BPF code.
Quoted 8 lines
Can you elaborate a bit more on the particular use-case, and what scenarios are unsolveable with the BPF filter we already have in tc? Just wondering, since you're using BPF for the purpose of classifying (but just from the context of actions) what about a possibility of a generic container for reusing (any) classifier from the framework, so we would not need to duplicate code?
+1
Also as its name tells, BPF is a filter, why it could be used as an action here? (I don't follow eBPF though.)
Jiri Pirko
maintainerQuoted 11 lines · Wed, Jan 07, 2015 at 07:33:39PM CET, [email protected] wrote:
Quoted 1 line · Jiri Pirko
This action provides a possibility to exec custom BPF code.
Can you elaborate a bit more on the particular use-case, and what scenarios are unsolveable with the BPF filter we already have in tc? Just wondering, since you're using BPF for the purpose of classifying (but just from the context of actions) what about a possibility of a generic container for reusing (any) classifier from the framework, so we would not need to duplicate code?
Quoted 3 lines
On the other hand, I would understand if it's at some point in time eBPF which would f.e. mangle the packet, but the API you propose is clearly classic BPF. ;)
Exactly. I would like to extend cls_bpf and act_bpf to handle eBPF right after. That is the point.
Quoted 3 lines
Thanks,
Daniel
Quoted 15 lines · Jiri Pirko
net/sched/act_bpf.c new file +15
-0+static int tcf_bpf (struct sk_buff *skb,const struct tc_action *a, +struct tcf_result *res) +{ +struct tcf_bpf *b = a->priv; +int action; +int filter_res; + +spin_lock (&b->tcf_lock); + b->tcf_tm.lastuse = jiffies; +bstats_update (&b->tcf_bstats, skb); + action = b->tcf_action; + + filter_res =BPF_PROG_RUN (b->filter, skb); +if (filter_res ==-1 ) + goto drop;
net/sched/act_bpf.c line 48
Quoted 10 lines
net/sched/act_bpf.c new file +10
-0+ + goto unlock; + +drop: + action =TC_ACT_SHOT ; + b->tcf_qstats.drops++; +unlock: +spin_unlock (&b->tcf_lock); +return action; +}
In theory this could be like:
filter_res = BPF_PROG_RUN(b->filter, skb);
spin_lock(&b->tcf_lock);
<update stats...>
if (filter_res == -1)
goto drop;
action = b->tcf_action;
...
to keep BPF_PROG_RUN out of the spin_lock?
Bye,
Hannes
Jiri Pirko
maintainerQuoted 34 lines · Thu, Jan 08, 2015 at 03:55:37PM CET, [email protected] wrote:
Quoted 15 lines · Jiri Pirko
net/sched/act_bpf.c new file +15
-0+static int tcf_bpf (struct sk_buff *skb,const struct tc_action *a, +struct tcf_result *res) +{ +struct tcf_bpf *b = a->priv; +int action; +int filter_res; + +spin_lock (&b->tcf_lock); + b->tcf_tm.lastuse = jiffies; +bstats_update (&b->tcf_bstats, skb); + action = b->tcf_action; + + filter_res =BPF_PROG_RUN (b->filter, skb); +if (filter_res ==-1 ) + goto drop;net/sched/act_bpf.c line 48
Quoted 10 lines
net/sched/act_bpf.c new file +10
-0+ + goto unlock; + +drop: + action =TC_ACT_SHOT ; + b->tcf_qstats.drops++; +unlock: +spin_unlock (&b->tcf_lock); +return action; +}In theory this could be like:
filter_res=BPF_PROG_RUN(b->filter, skb);
spin_lock(&b->tcf_lock);
<update stats...>
Quoted 7 lines
if (
filter_res== -1)
goto drop;action =
b->tcf_action;
...to keep
BPF_PROG_RUNout of thespin_lock?
Okay. That makes sense. Will include this change into v2.
Thanks.
Quoted 5 lines
Bye,
Hannes