netfilter: add ebpf translation infrastructure
Subsystems (5)
Florian Westphal
maintainercover netfilter: add ebpf translation infrastructure
This patch series adds a JIT layer to translate nft expressions to ebpf programs.
Quoted 1 line
From commit phase, spawn a userspace program (using recently added UMH
infrastructure).
We then provide rules that came in this transaction to the helper via pipe, using same nf_tables netlink that nftables already uses.
The userspace helper translates the rules, and, if successful, installs the generated program(s) via bpf syscall.
For each rule a small response containing the corresponding epbf file descriptor (can be -1 on failure) and a attribute count (how many expressions were jitted) gets sent back to kernel via pipe.
If translation fails, the rule is will be processed by nf_tables interpreter (as before this patch).
If translation succeeded, nf_tables fetches the bpf program using the file descriptor identifier, allocates a new rule blob containing the new 'ebpf' expression (and possible trailing un-translated expressions).
It then replaces the original rule in the transaction log with the new 'ebpf-rule'. The original rule is retained in a private area inside the epbf expression to be able to present the original expressions back to userspace on 'nft list ruleset'.
For easier review, this contains the kernel-side only. nf_tables_jit_work() will not do anything, yet.
Unresolved issues:
- maps and sets.
It might be possible to add a new ebpf map type that just wraps the nft set infrastructure for lookups.
This would allow nft userspace to continue to work as-is while not requiring new ebpf helper.
Anonymous set should be a lot easier as they're immutable and could probably be handled already by existing infra.
- BPF_PROG_RUN() is bolted into nft main loop via a middleman expression. I'm also abusing skb->cb[] to pass network and transport header offsets. Its not 'public' api so this can be changed later.
- always uses BPF_PROG_TYPE_SCHED_CLS.
This is because it "works" for current RFC purposes.
- we should eventually support translating multiple (adjacent) rules into single program.
If we do this kernel will need to track mapping of rules to program (to re-jit when a rule is changed. This isn't implemented so far, but can be added later. Alternatively, one could also add a 'readonly' table switch to just prevent further updates.
We will also need to dump the 'next' generation of the to-be-translated table. The kernel has this information, so its only a matter of serializing it back to userspace from the commit phase.
The jitter is still limited. So far it supports:
* payload expression for network and transport header
* meta mark, nfproto, l4proto
* 32 bit immediates
* 32 bit bitmask ops
* accept/drop verdicts
As this uses netlink, there is also no technical requirement for libnftnl, its simply used here for convienience.
It doesn't need any userspace changes. Patches for libnftnl and nftables make debug info available (e.g. to map rule to its bpf prog id).
Comments welcome.
Florian Westphal (5):
bpf: add bpf_prog_get_type_dev_file
netfilter: nf_tables: add ebpf expression
netfilter: nf_tables: add rule ebpf jit infrastructure
netfilter: nf_tables_jit: add dumping of original rule
netfilter: nf_tables_jit: add userspace nft to ebpf translator
include/linux/bpf.h | 11 include/net/netfilter/nf_tables_core.h | 22 include/uapi/linux/netfilter/nf_tables.h | 18 kernel/bpf/syscall.c | 18 net/netfilter/Kconfig | 7 net/netfilter/Makefile | 5 net/netfilter/nf_tables_api.c | 16 net/netfilter/nf_tables_core.c | 61 + net/netfilter/nf_tables_jit.c | 242 +++ net/netfilter/nf_tables_jit/Makefile | 19 net/netfilter/nf_tables_jit/imr.c | 1401 +++++++++++++++++++++++ net/netfilter/nf_tables_jit/imr.h | 96 + net/netfilter/nf_tables_jit/main.c | 579 +++++++++ net/netfilter/nf_tables_jit/nf_tables_jit_kern.c | 175 ++ 14 files changed, 2670 insertions(+)
Florian Westphal
maintainerRFC 1/5 bpf: add bpf_prog_get_type_dev_file
Same as bpf_prog_get_type_dev, but gets struct file* instead of fd.
In case of nf_tables jit, a file descriptor representing the ebpf program gets passed to kernel via a pipe from the (userspace) jit helper, not 'current', so existing bpf_prog_get_type_dev() doesn't work.
--- include/linux/bpf.h | 11 +++++++++++ kernel/bpf/syscall.c | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+)
include/linux/bpf.h +11 -0
@@ -24,6 +24,7 @@ struct bpf_map;struct sock ;struct seq_file ;struct btf ; +struct file ;/* map is generic key/value storage optionally accesible by eBPF programs */ struct bpf_map_ops {
@@ -417,6 +418,9 @@ extern const struct bpf_verifier_ops xdp_analyzer_ops;struct bpf_prog *bpf_prog_get (u32 ufd);struct bpf_prog *bpf_prog_get_type_dev (u32 ufd,enum bpf_prog_type type,bool attach_drv); +struct bpf_prog *bpf_prog_get_type_dev_file (struct file *, +enum bpf_prog_type type, +bool attach_drv);struct bpf_prog *__must_check bpf_prog_add (struct bpf_prog *prog,int i);void bpf_prog_sub (struct bpf_prog *prog,int i);struct bpf_prog *__must_check bpf_prog_inc (struct bpf_prog *prog);
@@ -523,6 +527,13 @@ static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,return ERR_PTR (-EOPNOTSUPP ); } +static inline struct bpf_prog *bpf_prog_get_type_dev_file (struct file *f, +enum bpf_prog_type type, +bool b) +{ +return ERR_PTR (-EOPNOTSUPP ); +} +static inline struct bpf_prog *__must_check bpf_prog_add (struct bpf_prog *prog,int i) {
kernel/bpf/syscall.c +18 -0
@@ -1203,6 +1203,24 @@ struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type, }EXPORT_SYMBOL_GPL (bpf_prog_get_type_dev); +struct bpf_prog *bpf_prog_get_type_dev_file (struct file *f, +enum bpf_prog_type type, +bool attach_drv) +{ +struct bpf_prog *prog; + +if (f->f_op != &bpf_prog_fops) +return ERR_PTR (-EINVAL ); + + prog = f->private_data; + +if (!bpf_prog_get_ok (prog, &type, attach_drv)) +return ERR_PTR (-EINVAL ); + +return bpf_prog_inc (prog); +} +EXPORT_SYMBOL_GPL (bpf_prog_get_type_dev_file); +/* Initially all BPF programs could be loaded w/o specifying * expected_attach_type. Later for some of them specifying expected_attach_type * at load time became required so that program could be validated properly.
Signature · 1 line
2.16.4
Florian Westphal
maintainerRFC 2/5 netfilter: nf_tables: add ebpf expression
In reply to Florian WestphalThis expression serves two purposes:
1. a middleman to invoke BPF_PROG_RUN() from nf_tables main eval loop
2. to expose the bpf program id via netlink, so userspace can map nftables rules to their corresponding ebpf program.
2) is added in a followup patch.
Its currently not possible to attach arbitrary ebpf programs from userspace, but this limitation is easy to remove if needed.
--- include/net/netfilter/nf_tables_core.h | 9 ++++++ include/uapi/linux/netfilter/nf_tables.h | 18 ++++++++++++ net/netfilter/Makefile | 3 +- net/netfilter/nf_tables_core.c | 33 ++++++++++++++++++++++ net/netfilter/nf_tables_jit.c | 48 ++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 net/netfilter/nf_tables_jit.c
include/net/netfilter/nf_tables_core.h +9 -0
@@ -15,6 +15,7 @@ extern struct nft_expr_type nft_range_type;extern struct nft_expr_type nft_meta_type;extern struct nft_expr_type nft_rt_type;extern struct nft_expr_type nft_exthdr_type; +extern struct nft_expr_type nft_ebpf_type;int nf_tables_core_module_init (void );void nf_tables_core_module_exit (void );
@@ -62,6 +63,14 @@ struct nft_payload_set {extern const struct nft_expr_ops nft_payload_fast_ops; +struct nft_ebpf { +struct bpf_prog *prog; +u8 expressions; +const struct nft_rule *original; +}; + +extern const struct nft_expr_ops nft_ebpf_fast_ops; +extern struct static_key_false nft_counters_enabled;extern struct static_key_false nft_trace_enabled;
include/uapi/linux/netfilter/nf_tables.h +18 -0
@@ -718,6 +718,24 @@ enum nft_payload_attributes { };#define NFTA_PAYLOAD_MAX (__NFTA_PAYLOAD_MAX - 1) +/** +* enum nft_ebpf_attributes - nf_tables ebpf expression netlink attributes +* +* @NFTA_EBPF_FD: file descriptor holding ebpf program (NLA_S32) +* @NFTA_EBPF_ID: bpf program id (NLA_U32) +* @NFTA_EBPF_TAG: bpf tag (NLA_BINARY) +* @NFTA_EBPF_TAG: expressions covered by this jit (NLA_U32) +*/ +enum nft_ebpf_attributes { +NFTA_EBPF_UNSPEC , +NFTA_EBPF_FD , +NFTA_EBPF_ID , +NFTA_EBPF_TAG , +NFTA_EBPF_EXPR_COUNT , + __NFTA_EBPF_MAX, +}; +#define NFTA_EBPF_MAX (__NFTA_EBPF_MAX - 1) +enum nft_exthdr_flags {NFT_EXTHDR_F_PRESENT = (1 <<0 ), };
net/netfilter/Makefile +2 -1
@@ -76,7 +76,8 @@ obj-$(CONFIG_NF_DUP_NETDEV) += nf_dup_netdev.o nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \ nf_tables_trace.o nft_immediate.o nft_cmp.o nft_range.o \ nft_bitwise.o nft_byteorder.o nft_payload.o nft_lookup.o \ - nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o + nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o \ + nf_tables_jit.o obj-$(CONFIG_NF_TABLES) += nf_tables.o obj-$(CONFIG_NFT_COMPAT) += nft_compat.o
net/netfilter/nf_tables_core.c +33 -0
@@ -11,6 +11,7 @@#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> +#include <linux/filter.h> #include <linux/list.h> #include <linux/rculist.h> #include <linux/skbuff.h>
@@ -92,6 +93,35 @@ static bool nft_payload_fast_eval(const struct nft_expr *expr,return true; } +static void nft_ebpf_fast_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt) +{ +const struct nft_ebpf *priv =nft_expr_priv (expr); +struct bpf_skb_data_end cb_saved; +int ret; + +memcpy (&cb_saved, pkt->skb->cb,sizeof (cb_saved)); +bpf_compute_data_pointers (pkt->skb); + + ret =BPF_PROG_RUN (priv->prog, pkt->skb); + +memcpy (pkt->skb->cb, &cb_saved,sizeof (cb_saved)); + +switch (ret) { +case NF_DROP : +case NF_ACCEPT : +case NFT_BREAK : + regs->verdict.code = ret; +return ; +case NFT_CONTINUE : +return ; +default : +pr_debug ("Unknown verdict %d\n" , ret); + regs->verdict.code =NF_DROP ; +break ; + } +}DEFINE_STATIC_KEY_FALSE (nft_counters_enabled); static noinline void nft_update_chain_stats(const struct nft_chain *chain,
@@ -151,6 +181,8 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)nft_rule_for_each_expr (expr, last, rule) {if (expr->ops == &nft_cmp_fast_ops)nft_cmp_fast_eval (expr, ®s); +else if (expr->ops == &nft_ebpf_fast_ops) +nft_ebpf_fast_eval (expr, ®s, pkt);else if (expr->ops != &nft_payload_fast_ops || !nft_payload_fast_eval (expr, ®s, pkt)) expr->ops->eval (expr, ®s, pkt);
@@ -232,6 +264,7 @@ static struct nft_expr_type *nft_basic_types[] = { &nft_meta_type, &nft_rt_type, &nft_exthdr_type, + &nft_ebpf_type, };int __init nf_tables_core_module_init (void )
net/netfilter/nf_tables_jit.c new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.16.4
Florian Westphal
maintainerRFC 3/5 netfilter: nf_tables: add rule ebpf jit infrastructure
In reply to Florian WestphalThis adds a JIT helper infrastructure to translate nft expressions to ebpf programs.
Quoted 1 line
From commit phase, we spawn jit module (a userspace program), and then
provide the rules that came in this transaction to that program via a pipe (in nf_tables netlink format).
The userspace helper translates the rules if possible, and installs the program(s) via bpf syscall.
For each rule a small response containing the corresponding file descriptor (can be -1 on failure) and a attribute count (how many expressions were jitted) gets sent back to kernel via pipe.
If translation fails, the rule is will be processed by nf_tables interpreter (as before this patch).
If translation succeeded, nf_tables fetches the bpf program using the file descriptor identifier, allocates a new rule blob containing the new 'ebpf' expression (and possible trailing un-translated expressions).
It then replaces the original rule in the transaction log with the new 'ebpf-rule'.
The original rule is retained in a private area inside the epbf expression to be able to present the original expressions to userspace when 'nft list ruleset' is called.
For easier review, this contains the kernel-side only. nf_tables_jit_work() will not do anything, yet.
Unresolved issues:
- maps and sets.
It might be possible to add a new ebpf map type that just wraps the nft set infrastructure for lookups.
This would allow nft userspace to continue to work as-is while not requiring new ebpf helper.
- we should eventually support translating multiple (adjacent) rules into single program.
If we do this kernel will need to track mapping of rules to program (to re-jit when a rule is changed. This isn't implemented so far, but can be added later.
We will also need to dump the 'next' generation of the to-be-translated table. The kernel has this information, so its only a matter of serializing it back to userspace from the commit phase.
--- include/net/netfilter/nf_tables_core.h | 12 ++ net/netfilter/Kconfig | 7 ++ net/netfilter/Makefile | 8 +- net/netfilter/nf_tables_api.c | 5 + net/netfilter/nf_tables_core.c | 31 ++++- net/netfilter/nf_tables_jit.c | 139 +++++++++++++++++++++++ net/netfilter/nf_tables_jit/Makefile | 18 +++ net/netfilter/nf_tables_jit/main.c | 21 ++++ net/netfilter/nf_tables_jit/nf_tables_jit_kern.c | 33 ++++++ 9 files changed, 270 insertions(+), 4 deletions(-) create mode 100644 net/netfilter/nf_tables_jit/Makefile create mode 100644 net/netfilter/nf_tables_jit/main.c create mode 100644 net/netfilter/nf_tables_jit/nf_tables_jit_kern.c
include/net/netfilter/nf_tables_core.h +12 -0
@@ -71,6 +71,18 @@ struct nft_ebpf {extern const struct nft_expr_ops nft_ebpf_fast_ops; +struct nft_jit_data_from_user { +int ebpf_fd;/* fd to get program from, or < 0 if jitter error */ +u32 expr_count;/* number of translated expressions */ +}; + +#if IS_ENABLED (CONFIG_NF_TABLES_JIT ) +int nft_jit_commit (struct net *net); +#else +static inline int nft_jit_commit (struct net *net) {return 0 ; } +#endif +int nf_tables_jit_work (const struct sk_buff *nlskb,struct nft_ebpf *e); +extern struct static_key_false nft_counters_enabled;extern struct static_key_false nft_trace_enabled;
net/netfilter/Kconfig +7 -0
@@ -473,6 +473,13 @@ config NF_TABLES_NETDEV help This option enables support for the "netdev" table. +config NF_TABLES_JIT + bool "Netfilter nf_tables jit infrastructure" + depends on BPF + help + This option enables support for translation of nf_tables + expressions to ebpf. + config NFT_NUMGEN tristate "Netfilter nf_tables number generator module" help
net/netfilter/Makefile +6 -2
@@ -76,8 +76,12 @@ obj-$(CONFIG_NF_DUP_NETDEV) += nf_dup_netdev.o nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \ nf_tables_trace.o nft_immediate.o nft_cmp.o nft_range.o \ nft_bitwise.o nft_byteorder.o nft_payload.o nft_lookup.o \ - nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o \ - nf_tables_jit.o + nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o + +obj-$(CONFIG_NF_TABLES_JIT) += nf_tables_jit/ +nf_tables-$(CONFIG_NF_TABLES_JIT) += nf_tables_jit.o +nf_tables-$(CONFIG_NF_TABLES_JIT) += nf_tables_jit/nf_tables_jit_kern.o +nf_tables-$(CONFIG_NF_TABLES_JIT) += nf_tables_jit/nf_tables_jit_umh.o obj-$(CONFIG_NF_TABLES) += nf_tables.o obj-$(CONFIG_NFT_COMPAT) += nft_compat.o
net/netfilter/nf_tables_api.c +5 -0
@@ -6092,6 +6092,11 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)struct nft_trans_elem *te;struct nft_chain *chain;struct nft_table *table; +int ret; + + ret =nft_jit_commit (net); +if (ret <0 ) +return ret;/* 1. Allocate space for next generation rules_gen_X[] */ list_for_each_entry_safe (trans, next, &net->nft.commit_list, list) {
net/netfilter/nf_tables_core.c +29 -2
@@ -93,19 +93,46 @@ static bool nft_payload_fast_eval(const struct nft_expr *expr,return true; } +/* Dirty hack: pass nft_pktinfo in skb->cb[] */ +struct nft_jit_args_inet_cb { +/* cb[0] */ +u16 thoff;/* 0: unset */ +u16 lloff;/* 0: unset */ + +/* cb[1] */ +u16 l4proto;/* thoff = 0? unset */ +u16 reserved; + +/* 12 bytes left */ +}; +static void nft_ebpf_fast_eval (const struct nft_expr *expr,struct nft_regs *regs,const struct nft_pktinfo *pkt) {const struct nft_ebpf *priv =nft_expr_priv (expr); +struct nft_jit_args_inet_cb *jit_args;struct bpf_skb_data_end cb_saved;int ret; +BUILD_BUG_ON (sizeof (struct nft_jit_args_inet_cb ) >QDISC_CB_PRIV_LEN ); +memcpy (&cb_saved, pkt->skb->cb,sizeof (cb_saved)); + + jit_args = (void *)bpf_skb_cb (pkt->skb); +memset (jit_args,0 ,sizeof (*jit_args)); + +if (skb_mac_header_was_set (pkt->skb)) + jit_args->lloff =skb_mac_header_len (pkt->skb); + +if (pkt->tprot_set) { + jit_args->thoff = pkt->xt.thoff; + jit_args->l4proto = pkt->tprot; + } +bpf_compute_data_pointers (pkt->skb); ret =BPF_PROG_RUN (priv->prog, pkt->skb); -memcpy (pkt->skb->cb, &cb_saved,sizeof (cb_saved));switch (ret) {
@@ -119,9 +146,9 @@ static void nft_ebpf_fast_eval(const struct nft_expr *expr,default :pr_debug ("Unknown verdict %d\n" , ret); regs->verdict.code =NF_DROP ; -break ; } } +DEFINE_STATIC_KEY_FALSE (nft_counters_enabled); static noinline void nft_update_chain_stats(const struct nft_chain *chain,
net/netfilter/nf_tables_jit.c +139 -0
152 lines not on this page · raw message
net/netfilter/nf_tables_jit/Makefile new file +18 -0
@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0 +# + +hostprogs-y := nf_tables_jit_umh +nf_tables_jit_umh-objs := main.o +HOSTCFLAGS += -I. -Itools/include/ + +quiet_cmd_copy_umh = GEN $@ + cmd_copy_umh = echo ':' > $(obj)/.nf_tables_jit_umh.o.cmd; \ + $(OBJCOPY) -I binary -O $(CONFIG_OUTPUT_FORMAT) \ + -B `$(OBJDUMP) -f $<|grep architecture|cut -d, -f1|cut -d' ' -f2` \ + --rename-section .data=.rodata $< $@ + +$(obj)/nf_tables_jit_umh.o: $(obj)/nf_tables_jit_umh + $(call cmd,copy_umh) + +obj-$(CONFIG_NF_TABLES_JIT) += nf_tables_jit.o +nf_tables_jit-objs += nf_tables_jit_kern.o nf_tables_jit_umh.o
net/netfilter/nf_tables_jit/main.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <unistd.h> + +int main (void ) +{ +static struct { +int fd, count; + } response; + + response.fd =-1 ; +for (;;) { +char buf[8192 ]; + +if (read (0 , buf,sizeof (buf)) <0 ) +return 1 ; +if (write (1 , &response,sizeof (response)) !=sizeof (response)) +return 2 ; + } + +return 0 ; +}
net/netfilter/nf_tables_jit/nf_tables_jit_kern.c new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/umh.h> +#include <linux/netfilter/nfnetlink.h> +#include <linux/netfilter/nf_tables.h> +#include <net/netfilter/nf_tables_core.h> + +#define UMH_start _binary_net_netfilter_nf_tables_jit_nf_tables_jit_umh_start +#define UMH_end _binary_net_netfilter_nf_tables_jit_nf_tables_jit_umh_end + +extern char UMH_start; +extern char UMH_end; + +static struct umh_info info; + +static int nft_jit_load_umh (void ) +{ +return fork_usermode_blob (&UMH_start, &UMH_end - &UMH_start, &info); +} + +int nf_tables_jit_work (const struct sk_buff *nlskb,struct nft_ebpf *e) +{ +if (!info.pipe_to_umh) { +int ret =nft_jit_load_umh (); +if (ret) +return ret; + +if (WARN_ON (!info.pipe_to_umh)) +return -EINVAL ; + } + +return 0 ; +}
Signature · 1 line
2.16.4
Florian Westphal
maintainerRFC 4/5 netfilter: nf_tables_jit: add dumping of original rule
In reply to Florian WestphalAfter previous patch userspace can't discover the original rules anymore when listing the rule.
This change adds a dump callback to the ebpf expression and a special handling in the main dumper loop.
When we see an ebpf expression in a rule, we skip normal dump handling and leave it the the nft ebpf expression -- it has a copy of the original expressions and can then simply add them back.
In order to allow userspace to discover presence of auto-jit, and to map the rule to the ebpf program, we still include the ebpf expression itself as the first expression in the dump.
For now, we expose the ebpf tag and the ebpf id plus the number of expressions that are supposedly covered by the program.
--- net/netfilter/nf_tables_api.c | 11 +++++++++ net/netfilter/nf_tables_jit.c | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+)
net/netfilter/nf_tables_api.c +11 -0
@@ -2054,6 +2054,17 @@ static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,if (list ==NULL ) goto nla_put_failure;nft_rule_for_each_expr (expr, next, rule) { +/* +* special case: ebpf_fast_ops will add original expressions +* to the netlink message, it will call +* nf_tables_fill_expr_info() itself. +*/ +if (expr->ops == &nft_ebpf_fast_ops) { +if (expr->ops->dump (skb, expr) <0 ) + goto nla_put_failure; +break ; + } +if (nft_expr_dump (skb,NFTA_LIST_ELEM , expr) <0 ) goto nla_put_failure; }
net/netfilter/nf_tables_jit.c +55 -0
66 lines not on this page · raw message
Signature · 1 line
2.16.4
Florian Westphal
maintainerRFC 5/5 netfilter: nf_tables_jit: add userspace nft to ebpf translator
In reply to Florian Westphalcurrently rather limited.
It supports:
* payload expression for network and transport header
* meta mark, nfproto, l4proto
* 32 bit immediates
* 32 bit bitmask ops
* accept/drop verdicts
Currently kernel will emit each rule on its own. However, jitter is (eventually) supposed to also cope with complete chains (including goto/jump).
It also lacks support for any kind of sets; anonymous sets would be a good initial target as they can't change.
As this uses netlink, there is also no technical requirement for libnftnl, its simply used for convienience.
This doesn't need any userspace changes to work, however, a libnftnl and nft patch will make debug info available (e.g. to match a rule with its bpf program id).
--- include/net/netfilter/nf_tables_core.h | 1 + net/netfilter/nf_tables_core.c | 1 + net/netfilter/nf_tables_jit/Makefile | 3 +- net/netfilter/nf_tables_jit/imr.c | 1401 ++++++++++++++++++++++ net/netfilter/nf_tables_jit/imr.h | 96 ++ net/netfilter/nf_tables_jit/main.c | 582 ++++++++- net/netfilter/nf_tables_jit/nf_tables_jit_kern.c | 146 ++- 7 files changed, 2215 insertions(+), 15 deletions(-) create mode 100644 net/netfilter/nf_tables_jit/imr.c create mode 100644 net/netfilter/nf_tables_jit/imr.h
include/net/netfilter/nf_tables_core.h +1 -0
@@ -82,6 +82,7 @@ int nft_jit_commit(struct net *net);static inline int nft_jit_commit (struct net *net) {return 0 ; }#endif int nf_tables_jit_work (const struct sk_buff *nlskb,struct nft_ebpf *e); +void nft_jit_stop_umh (void );extern struct static_key_false nft_counters_enabled;extern struct static_key_false nft_trace_enabled;
net/netfilter/nf_tables_core.c +1 -0
@@ -319,4 +319,5 @@ void nf_tables_core_module_exit(void) i =ARRAY_SIZE (nft_basic_types);while (i-- >0 )nft_unregister_expr (nft_basic_types[i]); +nft_jit_stop_umh (); }
net/netfilter/nf_tables_jit/Makefile +2 -1
@@ -2,8 +2,9 @@ # hostprogs-y := nf_tables_jit_umh -nf_tables_jit_umh-objs := main.o +nf_tables_jit_umh-objs := main.o imr.o HOSTCFLAGS += -I. -Itools/include/ +HOSTLOADLIBES_nf_tables_jit_umh = `pkg-config --libs libnftnl libmnl` quiet_cmd_copy_umh = GEN $@ cmd_copy_umh = echo ':' > $(obj)/.nf_tables_jit_umh.o.cmd; \
net/netfilter/nf_tables_jit/imr.c new file +1401 -0
1,401 lines not on this page · raw message
net/netfilter/nf_tables_jit/imr.h new file +96 -0
96 lines not on this page · raw message
net/netfilter/nf_tables_jit/main.c +570 -12
590 lines not on this page · raw message
net/netfilter/nf_tables_jit/nf_tables_jit_kern.c +144 -2
163 lines not on this page · raw message
Signature · 1 line
2.16.4
Quoted 67 lines · Florian Westphal
This patch series adds a JIT layer to translate nft expressions to ebpf programs.
From commit phase, spawn a userspace program (using recently added UMH infrastructure).
We then provide rules that came in this transaction to the helper via pipe, using same
nf_tablesnetlink that nftables already uses.The userspace helper translates the rules, and, if successful, installs the generated program(s) via bpf syscall.
For each rule a small response containing the corresponding epbf file descriptor (can be -1 on failure) and a attribute count (how many expressions were jitted) gets sent back to kernel via pipe.
If translation fails, the rule is will be processed by
nf_tablesinterpreter (as before this patch).If translation succeeded,
nf_tablesfetches the bpf program using the file descriptor identifier, allocates a new rule blob containing the new 'ebpf' expression (and possible trailing un-translated expressions).It then replaces the original rule in the transaction log with the new 'ebpf-rule'. The original rule is retained in a private area inside the epbf expression to be able to present the original expressions back to userspace on 'nft list ruleset'.
For easier review, this contains the kernel-side only.
nf_tables_jit_work()will not do anything, yet.Unresolved issues:
- maps and sets.
It might be possible to add a new ebpf map type that just wraps the nft set infrastructure for lookups.
This would allow nft userspace to continue to work as-is while not requiring new ebpf helper.
Anonymous set should be a lot easier as they're immutable and could probably be handled already by existing infra.-
BPF_PROG_RUN()is bolted into nft main loop via a middleman expression. I'm also abusing skb->cb[] to pass network and transport header offsets. Its not 'public' api so this can be changed later.- always uses
BPF_PROG_TYPE_SCHED_CLS.
This is because it "works" for current RFC purposes.- we should eventually support translating multiple (adjacent) rules into single program.
If we do this kernel will need to track mapping of rules to program (to re-jit when a rule is changed. This isn't implemented so far, but can be added later. Alternatively, one could also add a 'readonly' table switch to just prevent further updates.
We will also need to dump the 'next' generation of the to-be-translated table. The kernel has this information, so its only a matter of serializing it back to userspace from the commit phase.
The jitter is still limited. So far it supports:
* payload expression for network and transport header
* meta mark, nfproto, l4proto
* 32 bit immediates
* 32 bit bitmask ops
* accept/drop verdicts
Quoted 7 lines
As this uses netlink, there is also no technical requirement for libnftnl, its simply used here for convienience.
It doesn't need any userspace changes. Patches for libnftnl and nftables make debug info available (e.g. to map rule to its bpf prog id).
Comments welcome.
The implementation of patch 5 looks good to me, but I'm concerned with patch 2 that adds 'ebpf expression' to nft. I see no reason to do so. It seems existing support for infinite number of nft expressions is used as a way to execute infinite number of bpf programs sequentially. I don't think it was a scalable approach before and won't scale in the future. I think the algorithm should consider all nft rules at once and generate a program or two that will execute fast even when number of rules is large. We have the same scalability issue with bpfilter RFC patches. That's why they're still in RFC stage, since we need to figure out a way to support thousands of iptable rules in scalable way.
There are papers on scalable packet classification algorithms that use decision trees (hicuts, hypercuts, efficuts, etc)
Imo that is the direction should we should be looking at.
If we implement one of the algorithms as a tree(trie) with a generic lookup it will be usuable from bpf(bpfilter), from XDP, and other places inside the kernel.
We can even have multiple algorithms implemented and pick and choose depending on the size of ruleset and its properties, since one size doesn't always fit all.
I'm imagining umh will be doing iptables->trie+bpf conversion and nft->trie+bpf conversion where bpf progs will be dealing with pieces of logic that don't fit into trie lookup and provide generic mechanism for parsing the packet in the specific way suited for trie lookup for the given ruleset. The trie will be sized differently depending on tuples needed in the lookup. Like if there is no ipv6 in the ruleset the bpf prog won't be parsing that to prepare a tuple for given trie. Just like bpf hash map can be of different key/value size, this new trie will be customized for specific ruleset on the fly by umh. At the end the trie lookup is fully generic and bpf progs before and after are generic as well.
imo this way majority of iptables/nft rules can be converted and performance will be great even with large rulesets.
Florian Westphal
maintainerQuoted 62 lines · Alexei Starovoitov
Quoted 61 lines · Florian Westphal
The userspace helper translates the rules, and, if successful, installs the generated program(s) via bpf syscall.
For each rule a small response containing the corresponding epbf file descriptor (can be -1 on failure) and a attribute count (how many expressions were jitted) gets sent back to kernel via pipe.
If translation fails, the rule is will be processed by
nf_tablesinterpreter (as before this patch).If translation succeeded,
nf_tablesfetches the bpf program using the file descriptor identifier, allocates a new rule blob containing the new 'ebpf' expression (and possible trailing un-translated expressions).It then replaces the original rule in the transaction log with the new 'ebpf-rule'. The original rule is retained in a private area inside the epbf expression to be able to present the original expressions back to userspace on 'nft list ruleset'.
For easier review, this contains the kernel-side only.
nf_tables_jit_work()will not do anything, yet.Unresolved issues:
- maps and sets.
It might be possible to add a new ebpf map type that just wraps the nft set infrastructure for lookups.
This would allow nft userspace to continue to work as-is while not requiring new ebpf helper.
Anonymous set should be a lot easier as they're immutable and could probably be handled already by existing infra.-
BPF_PROG_RUN()is bolted into nft main loop via a middleman expression. I'm also abusing skb->cb[] to pass network and transport header offsets. Its not 'public' api so this can be changed later.- always uses
BPF_PROG_TYPE_SCHED_CLS.
This is because it "works" for current RFC purposes.- we should eventually support translating multiple (adjacent) rules into single program.
If we do this kernel will need to track mapping of rules to program (to re-jit when a rule is changed. This isn't implemented so far, but can be added later. Alternatively, one could also add a 'readonly' table switch to just prevent further updates.
We will also need to dump the 'next' generation of the to-be-translated table. The kernel has this information, so its only a matter of serializing it back to userspace from the commit phase.
The jitter is still limited. So far it supports:
* payload expression for network and transport header
* meta mark, nfproto, l4proto
* 32 bit immediates
* 32 bit bitmask ops
* accept/drop verdictsAs this uses netlink, there is also no technical requirement for libnftnl, its simply used here for convienience.
Quoted 7 lines
Quoted 4 lines
It doesn't need any userspace changes. Patches for libnftnl and nftables make debug info available (e.g. to map rule to its bpf prog id).
Comments welcome.
The implementation of patch 5 looks good to me, but I'm concerned with patch 2 that adds 'ebpf expression' to nft. I see no reason to do so.
I think its important user(space) can see which rules are jitted, and which ebpf prog corresponds to which rule(s), using an expression as container allows to re-use existing nft config plane code to serialze this via netlink attributes.
Quoted 2 lines
It seems existing support for infinite number of nft expressions is used as a way to execute infinite number of bpf programs sequentially.
In this RFC, yes.
Quoted 3 lines
I don't think it was a scalable approach before and won't scale in the future. I think the algorithm should consider all nft rules at once and generate a program or two that will execute fast even when number of rules is large.
Yes, but existence of the epbf expression doesn't prevent doing this in the future. Doing it now complicates things and given unresolved issues (see above cover letter) I'm reluctant to implement this already. The UMH in this RFC can translate only a very small subset of expressions. To make full-table realistic I think issues outlined above need to be addressed first.
It can be done, in such case the epbf expression would replace not just rule but possibly all of them.
Netlink dump of such a fully-translated table would have the epbf expression at the beginning of the first rule, exposing epbf program id/tag, and a list of the nft rule IDs that it replaced. In the extreme (ideal) case, it would thus list all rule handle IDs of the chain (including those reachable via jump-to-user-defined-chains).
Rest of dump would be as if ebpf did not exist, but these rules would all be "dead" from packet-path point of view. They are linked from via the nft epbf pseudo-expression, but no different from an arbitrary cookie/comment.
As explained above, this also needs kernel to track mapping of n nft rules to m ebpf progs, rather than the simple 1:1 mapping done in this RFC.
The 1:1 mapping is not being set stone here, its just the inital step to get the needed plumbing in, also see "Unresolved issues" in cover letter above.
So:
Step 1: 1:1 mapping, an nft rule has at most one ebpf prog. Step 2: figure out how to handle maps, sets, and how to cope with
not-yet-translateable expressions
Step 3: m:n mapping: kernel provides adjacent rules to the UMH for
jitting. Example: user appends rules a, b, c. UMH creates single ebpf prog from a/b/c.
nft-pseudo-expression replaces a/b/c in the
packet path, original rules a/b/c are linked from the pseudo expression for tracking. If user deletes rule b, we provide a/c to UMH to create new epbf prog that replaces new sequence a/c.
Step 4: always provide entire future base chain and all reachable chains
to the umh. Ideally all of it is replaced by single program.
Eventually, entire eval loop could be replaced by ebpf prog. But it will need some time to get there -- at this point existing nft expressions would no longer provide an ->eval() function.
Does that make sense to you?
If you see this as flawed, please let me know, but as I have no idea how to resolve these issues going from 0 to 4 makes no sense to me.
Quoted 3 lines
There are papers on scalable packet classification algorithms that use decision trees (hicuts, hypercuts, efficuts, etc) Imo that is the direction should we should be looking at.
Okay, but without any idea how to consider existing expressions, sets, maps etc. I'm not sure it makes sense to work on that at this point.
We also have the second problem that the netfilter base hook infra (NF_HOOK) already imposes indirect calls on us.
Is there a plan to have a away to replace those indirect calls with direct ones? We can't do that easily because most of the functions are in modules, but AFAIU ebpf could rewrite that to a sequence of direct calls.
[..]
Quoted 2 lines
imo this way majority of iptables/nft rules can be converted and performance will be great even with large rulesets.
Oh, I do not doubt that multiple rules can be compiled into single program, sorry if the RFC 1:1 mapping was confusing or gave that impression.
Quoted 68 lines · Florian Westphal
Quoted 67 lines · Alexei Starovoitov
Quoted 65 lines · Florian Westphal
The userspace helper translates the rules, and, if successful, installs the generated program(s) via bpf syscall.
For each rule a small response containing the corresponding epbf file descriptor (can be -1 on failure) and a attribute count (how many expressions were jitted) gets sent back to kernel via pipe.
If translation fails, the rule is will be processed by
nf_tablesinterpreter (as before this patch).If translation succeeded,
nf_tablesfetches the bpf program using the file descriptor identifier, allocates a new rule blob containing the new 'ebpf' expression (and possible trailing un-translated expressions).It then replaces the original rule in the transaction log with the new 'ebpf-rule'. The original rule is retained in a private area inside the epbf expression to be able to present the original expressions back to userspace on 'nft list ruleset'.
For easier review, this contains the kernel-side only.
nf_tables_jit_work()will not do anything, yet.Unresolved issues:
- maps and sets.
It might be possible to add a new ebpf map type that just wraps the nft set infrastructure for lookups.
This would allow nft userspace to continue to work as-is while not requiring new ebpf helper.
Anonymous set should be a lot easier as they're immutable and could probably be handled already by existing infra.-
BPF_PROG_RUN()is bolted into nft main loop via a middleman expression. I'm also abusing skb->cb[] to pass network and transport header offsets. Its not 'public' api so this can be changed later.- always uses
BPF_PROG_TYPE_SCHED_CLS.
This is because it "works" for current RFC purposes.- we should eventually support translating multiple (adjacent) rules into single program.
If we do this kernel will need to track mapping of rules to program (to re-jit when a rule is changed. This isn't implemented so far, but can be added later. Alternatively, one could also add a 'readonly' table switch to just prevent further updates.
We will also need to dump the 'next' generation of the to-be-translated table. The kernel has this information, so its only a matter of serializing it back to userspace from the commit phase.
The jitter is still limited. So far it supports:
* payload expression for network and transport header
* meta mark, nfproto, l4proto
* 32 bit immediates
* 32 bit bitmask ops
* accept/drop verdictsAs this uses netlink, there is also no technical requirement for libnftnl, its simply used here for convienience.
It doesn't need any userspace changes. Patches for libnftnl and nftables make debug info available (e.g. to map rule to its bpf prog id).
Comments welcome.
Quoted 7 lines
Quoted 2 lines
The implementation of patch 5 looks good to me, but I'm concerned with patch 2 that adds 'ebpf expression' to nft. I see no reason to do so.
I think its important user(space) can see which rules are jitted, and which ebpf prog corresponds to which rule(s), using an expression as container allows to re-use existing nft config plane code to serialze this via netlink attributes.
In my mind it would be all or nothing. I don't think it helps to convert some rules and not all.
Quoted 16 lines
Quoted 2 lines
It seems existing support for infinite number of nft expressions is used as a way to execute infinite number of bpf programs sequentially.
In this RFC, yes.
Quoted 3 lines
I don't think it was a scalable approach before and won't scale in the future. I think the algorithm should consider all nft rules at once and generate a program or two that will execute fast even when number of rules is large.
Yes, but existence of the epbf expression doesn't prevent doing this in the future. Doing it now complicates things and given unresolved issues (see above cover letter) I'm reluctant to implement this already. The UMH in this RFC can translate only a very small subset of expressions. To make full-table realistic I think issues outlined above need to be addressed first.
Quoted 2 lines
It can be done, in such case the epbf expression would replace not just rule but possibly all of them.
I think 'all of them' is mandatory. Same for bpfilter. Existing iptables/nft work as fallback already.
Only when converting all rules we get performance benefit. Partial converstion only makes things harder to debug and confuse users.
Quoted 21 lines
Netlink dump of such a fully-translated table would have the epbf expression at the beginning of the first rule, exposing epbf program id/tag, and a list of the nft rule IDs that it replaced. In the extreme (ideal) case, it would thus list all rule handle IDs of the chain (including those reachable via jump-to-user-defined-chains).
Rest of dump would be as if ebpf did not exist, but these rules would all be "dead" from packet-path point of view. They are linked from via the nft epbf pseudo-expression, but no different from an arbitrary cookie/comment.
As explained above, this also needs kernel to track mapping of n nft rules to m ebpf progs, rather than the simple 1:1 mapping done in this RFC.
The 1:1 mapping is not being set stone here, its just the inital step to get the needed plumbing in, also see "Unresolved issues" in cover letter above.
So:
Quoted 13 lines
Step 1: 1:1 mapping, an nft rule has at most one ebpf prog. Step 2: figure out how to handle maps, sets, and how to cope with
not-yet-translateable expressions
Step 3: m:n mapping: kernel provides adjacent rules to the UMH for
jitting. Example: user appends rules a, b, c. UMH creates single ebpf prog from a/b/c.
nft-pseudo-expression replaces a/b/c in the
packet path, original rules a/b/c are linked from the pseudo expression for tracking. If user deletes rule b, we provide a/c to UMH to create new epbf prog that replaces new sequence a/c.
Step 4: always provide entire future base chain and all reachable chains
to the umh. Ideally all of it is replaced by single program.
Right. I think the first implementation of converter should be translating all rules at once. Not necessarily all features, but all rules. Even if 60% of rules can be translated as bpf+trie there is not much benefit to do that and somehow mix and match the other 40% of old style iterative rule evaluation. Algorithms are too different. Iterative will be a drag on trie.
Quoted 9 lines
Eventually, entire eval loop could be replaced by ebpf prog. But it will need some time to get there -- at this point existing nft expressions would no longer provide an ->eval() function.
Does that make sense to you?
If you see this as flawed, please let me know, but as I have no idea how to resolve these issues going from 0 to 4 makes no sense to me.
I think the challenge is how to implement 4 without doing step 1, right? imo doing such 1:1 (single rule to single bpf prog) translation does not help to break hard problem into smaller pieces. Such 1:1 is great for prototype, but not to land upstream.
For the same reasons in bpfilter we did single iptable rule to single bpf prog translation, but such code doesn't belong in upstream tree, since it's not a scalable approach.
It's too easy to follow that road, but it goes nowhere. Hence my proposal to invest time into building decision tree based algorithm coupled with pre- and post- bpf progs that supply 'key' into decision trie lookup and interpret the result.
This way thousands of basic firewall rules will be translated in efficient way, but even tiny ruleset with complex features (like nat) won't be translated and that's ok.
We can build on top algorithm that considers all rules at once, but not on top of translator that does one rule at a time.
Quoted 7 lines
Quoted 3 lines
There are papers on scalable packet classification algorithms that use decision trees (hicuts, hypercuts, efficuts, etc) Imo that is the direction should we should be looking at.
Okay, but without any idea how to consider existing expressions, sets, maps etc. I'm not sure it makes sense to work on that at this point.
I think sets and ipset (in case of iptables) fit well into trie model.
Quoted 7 lines
We also have the second problem that the netfilter base hook infra (
NF_HOOK) already imposes indirect calls on us.Is there a plan to have a away to replace those indirect calls with direct ones? We can't do that easily because most of the functions are in modules, but AFAIU ebpf could rewrite that to a sequence of direct calls.
Yes. abundance of indirect calls is a separate, but equally important problem. We need to address both of them.
Quoted 8 lines
[..]
Quoted 2 lines
imo this way majority of iptables/nft rules can be converted and performance will be great even with large rulesets.
Oh, I do not doubt that multiple rules can be compiled into single program, sorry if the RFC 1:1 mapping was confusing or gave that impression.
I think bpfilter RFC also made folks believe that translating iptables rules one by one is what we're going to do as well. I hope this confusion is now resolved.
The kernel doesn't need another sequential match firewall.
Florian Westphal
maintainerQuoted 8 lines · Alexei Starovoitov
Quoted 4 lines · Florian Westphal
I think its important user(space) can see which rules are jitted, and which ebpf prog corresponds to which rule(s), using an expression as container allows to re-use existing nft config plane code to serialze this via netlink attributes.
In my mind it would be all or nothing. I don't think it helps to convert some rules and not all.
Ok. Still, even in that case I think it would be good if we'd be able to tell userspace the ebpf program id that corresponds to the ruleset.
Quoted 13 lines
Quoted 13 lines
Step 1: 1:1 mapping, an nft rule has at most one ebpf prog. Step 2: figure out how to handle maps, sets, and how to cope with
not-yet-translateable expressions
Step 3: m:n mapping: kernel provides adjacent rules to the UMH for
jitting. Example: user appends rules a, b, c. UMH creates single ebpf prog from a/b/c.
nft-pseudo-expression replaces a/b/c in the
packet path, original rules a/b/c are linked from the pseudo expression for tracking. If user deletes rule b, we provide a/c to UMH to create new epbf prog that replaces new sequence a/c.
Step 4: always provide entire future base chain and all reachable chains
to the umh. Ideally all of it is replaced by single program.
[..]
Quoted 6 lines
Quoted 4 lines
Does that make sense to you?
If you see this as flawed, please let me know, but as I have no idea how to resolve these issues going from 0 to 4 makes no sense to me.
I think the challenge is how to implement 4 without doing step 1, right?
Yes.
Quoted 6 lines
imo doing such 1:1 (single rule to single bpf prog) translation does not help to break hard problem into smaller pieces. Such 1:1 is great for prototype, but not to land upstream.
For the same reasons in bpfilter we did single iptable rule to single bpf prog translation, but such code doesn't belong in upstream tree, since it's not a scalable approach.
[..]
Quoted 5 lines
Quoted 3 lines
Okay, but without any idea how to consider existing expressions, sets, maps etc. I'm not sure it makes sense to work on that at this point.
I think sets and ipset (in case of iptables) fit well into trie model.
Yes, but thats going to be a lot of effort to handle properly without breaking (or replacing) userland plumbing.
For nft we could aim for full-translation for the ingress hook initially as that takes stateful filering out of the picture (ingress occurs before conntrack).
We could also ignore sets for now and only deal with anonymous sets (they are immutable and data stored in such sets can be made available to UMH).
I can rework the RFC to emit "future table" to UMH instead of individual rules, but I don't know yet when i will have time to work on it again.
Thanks,
Florian