add support for queue target
Subsystems (2)
PATCH 1/1 add support for queue target
Hello,
To follow is a series of patches for kernel, libnftables and nftables which bring nfnetlink queue support to nftables.
Kernel patches:
include/uapi/linux/netfilter/nf_tables.h | 21 ++++ net/netfilter/Kconfig | 9 ++ net/netfilter/Makefile | 1 + net/netfilter/nf_tables_core.c | 5 +- net/netfilter/nft_queue.c | 189 +++++++++++++++++++++++++++++++ 5 files changed, 224 insertions(+), 1 deletion(-)
Libnftables patch:
include/libnftables/expr.h | 5 + include/linux/netfilter/nf_tables.h | 20 +++ src/Makefile.am | 1 + src/expr/queue.c | 254 ++++++++++++++++++++++++++++++++++++ tests/Makefile.am | 4 + tests/nft-expr_queue-test.c | 99 ++++++++++++++ 6 files changed, 383 insertions(+)
nftables patch:
include/linux/netfilter/nf_tables.h | 20 ++++++++++++ include/statement.h | 11 +++++++ src/evaluate.c | 2 ++ src/netlink_delinearize.c | 15 +++++++++ src/netlink_linearize.c | 22 +++++++++++++ src/parser.y | 61 ++++++++++++++++++++++++++++++++++--- src/scanner.l | 8 ++++- src/statement.c | 31 +++++++++++++++++++ 8 files changed, 164 insertions(+), 6 deletions(-)
BR,
Signature · 1 line
Eric Leblond <[email protected]>
PATCH 1/2 netfilter: nft: fix issue with verdict support
The test on verdict was simply done on the value of the verdict which is not correct as far as queue is concern. In fact, the test of verdict test must be done with respect to the verdict mask for verdicts which are not internal to nftables.
--- net/netfilter/nf_tables_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_core.c +4 -1
@@ -164,7 +164,7 @@ next_rule:break ; } -switch (data[NFT_REG_VERDICT ].verdict) { +switch (data[NFT_REG_VERDICT ].verdict &NF_VERDICT_MASK ) {case NF_ACCEPT :case NF_DROP :case NF_QUEUE :
@@ -172,6 +172,9 @@ next_rule:nft_trace_packet (pkt, chain, rulenum,NFT_TRACE_RULE );return data[NFT_REG_VERDICT ].verdict; + } + +switch (data[NFT_REG_VERDICT ].verdict) {case NFT_JUMP :if (unlikely (pkt->skb->nf_trace))nft_trace_packet (pkt, chain, rulenum,NFT_TRACE_RULE );
Signature · 1 line
1.8.4.4
PATCH 2/2 netfilter: nft: add queue module
This patch adds a new nft module named "nft_queue" which is a nftables target sending packet to nfnetlink_queue subsystem. It has the same level of functionnality as is iptables ancestor and share some code with it.
--- include/uapi/linux/netfilter/nf_tables.h | 21 ++++ net/netfilter/Kconfig | 9 ++ net/netfilter/Makefile | 1 + net/netfilter/nft_queue.c | 189 +++++++++++++++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 net/netfilter/nft_queue.c
include/uapi/linux/netfilter/nf_tables.h +21 -0
@@ -658,6 +658,26 @@ enum nft_log_attributes {#define NFTA_LOG_MAX (__NFTA_LOG_MAX - 1)/** +* enum nft_queue_attributes - nf_tables queue expression netlink attributes +* +* @NFTA_QUEUE_NUM: netlink queue to send messages to (NLA_U16) +* @NFTA_QUEUE_TOTAL: number of queues to load balance packets on (NLA_U16) +* @NFTA_QUEUE_FLAGS: various flags (NLA_U16) +*/ +enum nft_queue_attributes { +NFTA_QUEUE_UNSPEC , +NFTA_QUEUE_NUM , +NFTA_QUEUE_TOTAL , +NFTA_QUEUE_FLAGS , + __NFTA_QUEUE_MAX +}; +#define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1) + +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03 + +/** * enum nft_reject_types - nf_tables reject expression reject types * * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable
@@ -682,6 +702,7 @@ enum nft_reject_attributes { };#define NFTA_REJECT_MAX (__NFTA_REJECT_MAX - 1) +/** * enum nft_nat_types - nf_tables nat expression NAT types *
net/netfilter/Kconfig +9 -0
@@ -456,6 +456,15 @@ config NFT_NAT depends on NF_NAT tristate "Netfilter nf_tables nat module" +config NFT_QUEUE + depends on NF_TABLES + depends on NETFILTER_XTABLES + depends on NETFILTER_NETLINK_QUEUE + tristate "Netfilter nf_tables queue module" + help + This is required if you intend to use nfnetlink queue + on more than the default queue 0 or with the other features + config NFT_COMPAT depends on NF_TABLES depends on NETFILTER_XTABLES
net/netfilter/Makefile +1 -0
@@ -76,6 +76,7 @@ obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o +obj-$(CONFIG_NFT_QUEUE) += nft_queue.o #nf_tables-objs += nft_meta_target.o obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o obj-$(CONFIG_NFT_HASH) += nft_hash.o
net/netfilter/nft_queue.c new file +189 -0
189 lines not on this page · raw message
Signature · 1 line
1.8.4.4
Florian Westphal
maintainerRe: [PATCH 2/2] netfilter: nft: add queue module
Quoted 6 lines · Eric Leblond
This patch adds a new nft module named "
nft_queue" which is a nftables target sending packet tonfnetlink_queuesubsystem. It has the same level of functionnality as is iptables ancestor and share some code with it.+static u32
jhash_initval__read_mostly;
Consider adding something like
net/netfilter/nft_queue.c line 120
Quoted 5 lines
net/netfilter/nft_queue.c new file +5
-0+static int nft_queue_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]) +{ +struct nft_queue *priv =nft_expr_priv (expr);
while (jhash_initval == 0) jhash_initval = prandom_u32();
Other than this, it looks good to me.
Thanks for spending time on this!
PATCH v2 1/1 netfilter: nft: add queue module
This patch adds a new nft module named "nft_queue" which is a nftables target sending packet to nfnetlink_queue subsystem. It has the same level of functionnality as is iptables ancestor and share some code with it.
--- include/uapi/linux/netfilter/nf_tables.h | 20 ++++ net/netfilter/Kconfig | 9 ++ net/netfilter/Makefile | 1 + net/netfilter/nft_queue.c | 192 +++++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+) create mode 100644 net/netfilter/nft_queue.c
include/uapi/linux/netfilter/nf_tables.h +20 -0
@@ -658,6 +658,26 @@ enum nft_log_attributes {#define NFTA_LOG_MAX (__NFTA_LOG_MAX - 1)/** +* enum nft_queue_attributes - nf_tables queue expression netlink attributes +* +* @NFTA_QUEUE_NUM: netlink queue to send messages to (NLA_U16) +* @NFTA_QUEUE_TOTAL: number of queues to load balance packets on (NLA_U16) +* @NFTA_QUEUE_FLAGS: various flags (NLA_U16) +*/ +enum nft_queue_attributes { +NFTA_QUEUE_UNSPEC , +NFTA_QUEUE_NUM , +NFTA_QUEUE_TOTAL , +NFTA_QUEUE_FLAGS , + __NFTA_QUEUE_MAX +}; +#define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1) + +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03 + +/** * enum nft_reject_types - nf_tables reject expression reject types * * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable
net/netfilter/Kconfig +9 -0
@@ -456,6 +456,15 @@ config NFT_NAT depends on NF_NAT tristate "Netfilter nf_tables nat module" +config NFT_QUEUE + depends on NF_TABLES + depends on NETFILTER_XTABLES + depends on NETFILTER_NETLINK_QUEUE + tristate "Netfilter nf_tables queue module" + help + This is required if you intend to use nfnetlink queue + on more than the default queue 0 or with the other features + config NFT_COMPAT depends on NF_TABLES depends on NETFILTER_XTABLES
net/netfilter/Makefile +1 -0
@@ -76,6 +76,7 @@ obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o +obj-$(CONFIG_NFT_QUEUE) += nft_queue.o #nf_tables-objs += nft_meta_target.o obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o obj-$(CONFIG_NFT_HASH) += nft_hash.o
net/netfilter/nft_queue.c new file +192 -0
192 lines not on this page · raw message
Signature · 1 line
1.8.4.4
Pablo Neira Ayuso
maintainerRe: [PATCHv2] netfilter: nft: add queue module
Hi Eric,
First off, thanks for this patch. Some comments below.
Quoted 101 lines · Eric Leblond
This patch adds a new nft module named "
nft_queue" which is a nftables target sending packet tonfnetlink_queuesubsystem. It has the same level of functionnality as is iptables ancestor and share some code with it.Signed-off-by Eric Leblond <[email protected]>--- include/uapi/linux/netfilter/nf_tables.h | 20 ++++ net/netfilter/Kconfig | 9 ++ net/netfilter/Makefile | 1 + net/netfilter/nft_queue.c | 192 +++++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+) create mode 100644 net/netfilter/nft_queue.cinclude/uapi/linux/netfilter/nf_tables.h +20
-0@@ -658,6 +658,26 @@ enum nft_log_attributes {#define NFTA_LOG_MAX (__NFTA_LOG_MAX - 1)/** +* enum nft_queue_attributes - nf_tables queue expression netlink attributes +* +* @NFTA_QUEUE_NUM: netlink queue to send messages to (NLA_U16) +* @NFTA_QUEUE_TOTAL: number of queues to load balance packets on (NLA_U16) +* @NFTA_QUEUE_FLAGS: various flags (NLA_U16) +*/ +enum nft_queue_attributes { +NFTA_QUEUE_UNSPEC , +NFTA_QUEUE_NUM , +NFTA_QUEUE_TOTAL , +NFTA_QUEUE_FLAGS , + __NFTA_QUEUE_MAX +}; +#define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1) + +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03 + +/** * enum nft_reject_types - nf_tables reject expression reject types * * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable net/netfilter/Kconfig +9
-0@@ -456,6 +456,15 @@ config NFT_NAT depends on NF_NAT tristate "Netfilter nf_tables nat module" +config NFT_QUEUE + depends on NF_TABLES + depends on NETFILTER_XTABLES + depends on NETFILTER_NETLINK_QUEUE + tristate "Netfilter nf_tables queue module" + help + This is required if you intend to use nfnetlink queue + on more than the default queue 0 or with the other features + config NFT_COMPAT depends on NF_TABLES depends on NETFILTER_XTABLESnet/netfilter/Makefile +1
-0@@ -76,6 +76,7 @@ obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o +obj-$(CONFIG_NFT_QUEUE) += nft_queue.o #nf_tables-objs += nft_meta_target.o obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o obj-$(CONFIG_NFT_HASH) += nft_hash.onet/netfilter/nft_queue.c new file +18
-0@@ -0,0 +1,192 @@ +/* +* Copyright (c) 2013 Eric Leblond <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ + +#include <linux/kernel.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/netlink.h> + +#include <linux/ip.h> +#include <linux/ipv6.h> +#include <linux/jhash.h> +
net/netfilter/nft_queue.c line 28
Quoted 10 lines
net/netfilter/nft_queue.c new file +10
-0+#include <linux/jhash.h> +#include <linux/netfilter.h> +#include <linux/netfilter/nf_tables.h> +#include <net/netfilter/nf_tables.h> + +struct nft_queue { +u16 queuenum; +u16 queues_total; +u16 flags; +int family;
^
This structure is very important that it doesn't have any hole (at its best) to reduce the size of the expression area in the rule. My proposal:
...
{
u16 queue_num;
u16 queue_total;
u16 flags;
u8 family;
}Quoted 48 lines
net/netfilter/nft_queue.c new file +3
-0+}; + +static u32 jhash_initval__read_mostly ;From here:
net/netfilter/nft_queue.c new file +42
-0+static u32 hash_v4 (const struct sk_buff *skb) +{ +const struct iphdr *iph =ip_hdr (skb); + +/* packets in either direction go into same queue */ +if ((__force u32 )iph->saddr < (__force u32 )iph->daddr) +return jhash_3words ((__force u32 )iph->saddr, + (__force u32 )iph->daddr, iph->protocol, jhash_initval); + +return jhash_3words ((__force u32 )iph->daddr, + (__force u32 )iph->saddr, iph->protocol, jhash_initval); +} + +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) +static u32 hash_v6 (const struct sk_buff *skb) +{ +const struct ipv6hdr *ip6h =ipv6_hdr (skb); +u32 a, b, c; + +if ((__force u32 )ip6h->saddr.s6_addr32[3 ] < + (__force u32 )ip6h->daddr.s6_addr32[3 ]) { + a = (__force u32 ) ip6h->saddr.s6_addr32[3 ]; + b = (__force u32 ) ip6h->daddr.s6_addr32[3 ]; + }else { + b = (__force u32 ) ip6h->saddr.s6_addr32[3 ]; + a = (__force u32 ) ip6h->daddr.s6_addr32[3 ]; + } + +if ((__force u32 )ip6h->saddr.s6_addr32[1 ] < + (__force u32 )ip6h->daddr.s6_addr32[1 ]) + c = (__force u32 ) ip6h->saddr.s6_addr32[1 ]; +else + c = (__force u32 ) ip6h->daddr.s6_addr32[1 ]; + +return jhash_3words (a, b, c, jhash_initval); +} +#endif + +static u32 +nfqueue_hash (const struct nft_pktinfo *pkt,const struct nft_queue *priv) +{ +u32 queue = priv->queuenum;
net/netfilter/nft_queue.c line 84
Quoted 10 lines
net/netfilter/nft_queue.c new file +10
-0+ +if (priv->family ==AF_INET ) + queue += ((u64 )hash_v4 (pkt->skb) * priv->queues_total) >>32 ; +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) + else if (priv->family == AF_INET6) + queue += ((u64) hash_v6(pkt->skb) * priv->queues_total) >> 32; +#endif + +return queue; +}
to there, it looks very similar to NFQUEUE. You can move these functions to net/netfilter/nf_queue.h. You'll have to inline them and add the jhash_initval parameter. You'll need an initial patch to prepare this change and adapt xt_NFQUEUE.c
Quoted 19 lines
net/netfilter/nft_queue.c new file +19
-0+ +static void nft_queue_eval (const struct nft_expr *expr, +struct nft_data data[NFT_REG_MAX +1 ], +const struct nft_pktinfo *pkt) +{ +struct nft_queue *priv =nft_expr_priv (expr); +u32 queue = priv->queuenum; +u32 ret; + +if (priv->queues_total >1 ) { +if (priv->flags &NFT_QUEUE_FLAG_CPU_FANOUT ) { +int cpu =smp_processor_id (); + + queue = priv->queuenum + cpu % priv->queues_total; + }else + queue =nfqueue_hash (pkt, priv); + } + + ret =NF_QUEUE_NR (queue);
net/netfilter/nft_queue.c line 113
Quoted 10 lines
net/netfilter/nft_queue.c new file +10
-0+if (priv->flags &NFT_QUEUE_FLAG_BYPASS ) + ret |=NF_VERDICT_FLAG_QUEUE_BYPASS ; + + data[NFT_REG_VERDICT ].verdict = ret; +} + +static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX +1 ] = { + [NFTA_QUEUE_NUM ] = { .type =NLA_U16 }, + [NFTA_QUEUE_TOTAL ] = { .type =NLA_U16 }, + [NFTA_QUEUE_FLAGS ] = { .type =NLA_U16 },
^------------^
Comestic: I think one tab should be fine.
net/netfilter/nft_queue.c line 123
Quoted 10 lines
net/netfilter/nft_queue.c new file +10
-0+}; + +static int nft_queue_init (const struct nft_ctx *ctx, +const struct nft_expr *expr, +const struct nlattr *const tb[]) +{ +struct nft_queue *priv =nft_expr_priv (expr); + +while (jhash_initval ==0 ) + jhash_initval =prandom_u32 ();
Different initialization approach with regards to xt_NFQUEUE, any reason for that change?
Quoted 19 lines
net/netfilter/nft_queue.c new file +19
-0+ + priv->family = ctx->afi->family; + +if (tb[NFTA_QUEUE_NUM ] ==NULL ) +return -EINVAL ; + priv->queuenum =ntohs (nla_get_be16 (tb[NFTA_QUEUE_NUM ])); + +if (tb[NFTA_QUEUE_TOTAL ] !=NULL ) + priv->queues_total =ntohs (nla_get_be16 (tb[NFTA_QUEUE_TOTAL ])); +if (tb[NFTA_QUEUE_FLAGS ] !=NULL ) { + priv->flags =ntohs (nla_get_be16 (tb[NFTA_QUEUE_FLAGS ])); +if (priv->flags & ~NFT_QUEUE_FLAG_MASK ) +return -EINVAL ; + } +return 0 ; +} + +static int nft_queue_dump (struct sk_buff *skb,const struct nft_expr *expr) +{
net/netfilter/nft_queue.c line 152
Quoted 10 lines
net/netfilter/nft_queue.c new file +10
-0+const struct nft_queue *priv =nft_expr_priv (expr); + +if (nla_put_be16 (skb,NFTA_QUEUE_NUM ,htons (priv->queuenum))) + goto nla_put_failure; + +if (nla_put_be16 (skb,NFTA_QUEUE_TOTAL ,htons (priv->queues_total))) + goto nla_put_failure; + +if (nla_put_be16 (skb,NFTA_QUEUE_FLAGS ,htons (priv->flags))) + goto nla_put_failure;
Comestic: Perhaps you can squash these three put lines in one single if to save a couple of lines, the code will be still quite readable.
Quoted 43 lines
net/netfilter/nft_queue.c new file +40
-0+ +return 0 ; + +nla_put_failure: +return -1 ; +} + +static struct nft_expr_type nft_queue_type; +static const struct nft_expr_ops nft_queue_ops = { + .type = &nft_queue_type, + .size =NFT_EXPR_SIZE (sizeof (struct nft_queue )), + .eval = nft_queue_eval, + .init = nft_queue_init, + .dump = nft_queue_dump, +}; + +static struct nft_expr_type nft_queue_type__read_mostly = { + .name ="queue" , + .ops = &nft_queue_ops, + .policy = nft_queue_policy, + .maxattr =NFTA_QUEUE_MAX , + .owner =THIS_MODULE , +}; + +static int __init nft_queue_module_init (void ) +{ +return nft_register_expr (&nft_queue_type); +} + +static void __exit nft_queue_module_exit(void) +{ +nft_unregister_expr (&nft_queue_type); +} + +module_init (nft_queue_module_init); +module_exit (nft_queue_module_exit); + +MODULE_LICENSE ("GPL" ); +MODULE_AUTHOR ("Eric Leblond <[email protected]>" ); +MODULE_ALIAS_NFT_EXPR ("queue" );Signature · 1 line
1.8.4.4
Florian Westphal
maintainerRe: [PATCHv2] netfilter: nft: add queue module
Quoted 5 lines · Pablo Neira Ayuso
net/netfilter/nft_queue.c line 123
Quoted 2 lines
net/netfilter/nft_queue.c new file +2
-0+while (jhash_initval ==0 ) + jhash_initval =prandom_u32 ();Different initialization approach with regards to
xt_NFQUEUE, any reason for that change?
I suggested this -- get_random_bytes is overkill for nfqueue purposes.
In fact, I think xt_NFQUEUE should be switched to prandom interface as well.
Pablo Neira Ayuso
maintainerRe: [PATCHv2] netfilter: nft: add queue module
Quoted 7 lines · Florian Westphal
Quoted 5 lines · Pablo Neira Ayuso
net/netfilter/nft_queue.c line 123
Quoted 2 lines
net/netfilter/nft_queue.c new file +2
-0+while (jhash_initval ==0 ) + jhash_initval =prandom_u32 ();Different initialization approach with regards to
xt_NFQUEUE, any reason for that change?
Quoted 5 lines
I suggested this --
get_random_bytesis overkill for nfqueue purposes.In fact, I think
xt_NFQUEUEshould be switched to prandom interface as well.
I see. For consistency, it would be good to get a patch so both are in sync.
Re: [PATCHv2] netfilter: nft: add queue module
In reply to Pablo Neira AyusoHi,
Quoted 13 lines · Pablo Neira Ayuso
Hi Eric,
First off, thanks for this patch. Some comments below.
Quoted 8 lines · Eric Leblond
This patch adds a new nft module named "
nft_queue" which is a nftables target sending packet tonfnetlink_queuesubsystem. It has the same level of functionnality as is iptables ancestor and share some code with it.Signed-off-by Eric Leblond <[email protected]>--- include/uapi/linux/netfilter/nf_tables.h | 20 ++++
...
Quoted 11 lines
net/netfilter/nft_queue.c line 28
Quoted 10 lines
net/netfilter/nft_queue.c new file +10
-0+#include <linux/jhash.h> +#include <linux/netfilter.h> +#include <linux/netfilter/nf_tables.h> +#include <net/netfilter/nf_tables.h> + +struct nft_queue { +u16 queuenum; +u16 queues_total; +u16 flags; +int family;^
Quoted 3 lines
This structure is very important that it doesn't have any hole (at its best) to reduce the size of the expression area in the rule. My proposal:
OK, fixing this.
Quoted 63 lines
...
{ u16 queue_num; u16 queue_total; u16 flags; u8 family; }net/netfilter/nft_queue.c line 31
Quoted 3 lines
net/netfilter/nft_queue.c new file +3
-0+}; + +static u32 jhash_initval__read_mostly ;From here:
Quoted 49 lines
net/netfilter/nft_queue.c new file +49
-0+static u32 hash_v4 (const struct sk_buff *skb) +{ +const struct iphdr *iph =ip_hdr (skb); + +/* packets in either direction go into same queue */ +if ((__force u32 )iph->saddr < (__force u32 )iph->daddr) +return jhash_3words ((__force u32 )iph->saddr, + (__force u32 )iph->daddr, iph->protocol, jhash_initval); + +return jhash_3words ((__force u32 )iph->daddr, + (__force u32 )iph->saddr, iph->protocol, jhash_initval); +} + +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) +static u32 hash_v6 (const struct sk_buff *skb) +{ +const struct ipv6hdr *ip6h =ipv6_hdr (skb); +u32 a, b, c; + +if ((__force u32 )ip6h->saddr.s6_addr32[3 ] < + (__force u32 )ip6h->daddr.s6_addr32[3 ]) { + a = (__force u32 ) ip6h->saddr.s6_addr32[3 ]; + b = (__force u32 ) ip6h->daddr.s6_addr32[3 ]; + }else { + b = (__force u32 ) ip6h->saddr.s6_addr32[3 ]; + a = (__force u32 ) ip6h->daddr.s6_addr32[3 ]; + } + +if ((__force u32 )ip6h->saddr.s6_addr32[1 ] < + (__force u32 )ip6h->daddr.s6_addr32[1 ]) + c = (__force u32 ) ip6h->saddr.s6_addr32[1 ]; +else + c = (__force u32 ) ip6h->daddr.s6_addr32[1 ]; + +return jhash_3words (a, b, c, jhash_initval); +} +#endif + +static u32 +nfqueue_hash (const struct nft_pktinfo *pkt,const struct nft_queue *priv) +{ +u32 queue = priv->queuenum; + +if (priv->family ==AF_INET ) + queue += ((u64 )hash_v4 (pkt->skb) * priv->queues_total) >>32 ; +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) + else if (priv->family == AF_INET6) + queue += ((u64) hash_v6(pkt->skb) * priv->queues_total) >> 32; +#endif
Quoted 8 lines
net/netfilter/nft_queue.c line 84
Quoted 3 lines
net/netfilter/nft_queue.c new file +3
-0+ +return queue; +}to there, it looks very similar to NFQUEUE. You can move these functions to
net/netfilter/nf_queue.h. You'll have to inline them and add thejhash_initvalparameter. You'll need an initial patch to prepare this change and adaptxt_NFQUEUE.c
I'm ok with the two hash functions but the nfqueue_hash the second argument is different. Do you want me to introduce a new common structure for xt_NFQUEUE and nft_queue ?
Quoted 6 lines
Quoted 6 lines
net/netfilter/nft_queue.c new file +6
-0+ +static void nft_queue_eval (const struct nft_expr *expr, +struct nft_data data[NFT_REG_MAX +1 ], +const struct nft_pktinfo *pkt) +{ +struct nft_queue *priv =nft_expr_priv (expr);
...
Quoted 12 lines
ret =
NF_QUEUE_NR(queue);net/netfilter/nft_queue.c line 113
Quoted 10 lines
net/netfilter/nft_queue.c new file +10
-0+if (priv->flags &NFT_QUEUE_FLAG_BYPASS ) + ret |=NF_VERDICT_FLAG_QUEUE_BYPASS ; + + data[NFT_REG_VERDICT ].verdict = ret; +} + +static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX +1 ] = { + [NFTA_QUEUE_NUM ] = { .type =NLA_U16 }, + [NFTA_QUEUE_TOTAL ] = { .type =NLA_U16 }, + [NFTA_QUEUE_FLAGS ] = { .type =NLA_U16 },^------------^
Quoted 1 line
Comestic: I think one tab should be fine.
OK.
Quoted 4 lines
Quoted 4 lines
net/netfilter/nft_queue.c new file +4
-0+}; + +static int nft_queue_init(const struct nft_ctx *ctx, + const struct nft_expr *expr,
..
Quoted 9 lines
Quoted 9 lines
net/netfilter/nft_queue.c new file +9
-0+} + +static int nft_queue_dump (struct sk_buff *skb,const struct nft_expr *expr) +{ +const struct nft_queue *priv =nft_expr_priv (expr); + +if (nla_put_be16 (skb,NFTA_QUEUE_NUM ,htons (priv->queuenum))) + goto nla_put_failure; +
Quoted 8 lines
net/netfilter/nft_queue.c line 152
Quoted 5 lines
net/netfilter/nft_queue.c new file +5
-0+if (nla_put_be16 (skb,NFTA_QUEUE_TOTAL ,htons (priv->queues_total))) + goto nla_put_failure; + +if (nla_put_be16 (skb,NFTA_QUEUE_FLAGS ,htons (priv->flags))) + goto nla_put_failure;Comestic: Perhaps you can squash these three put lines in one single if to save a couple of lines, the code will be still quite readable.
Done.
BR
Signature · 1 line
Eric Leblond <[email protected]>
Pablo Neira Ayuso
maintainerRe: [PATCHv2] netfilter: nft: add queue module
Hi Eric,
On Wed, Dec 04, 2013 at 01:47:43PM +0100, Eric Leblond wrote:
[...]
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
net/netfilter/nft_queue.c new file +11
-0+static u32 +nfqueue_hash (const struct nft_pktinfo *pkt,const struct nft_queue *priv) +{ +u32 queue = priv->queuenum; + +if (priv->family ==AF_INET ) + queue += ((u64 )hash_v4 (pkt->skb) * priv->queues_total) >>32 ; +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) + else if (priv->family == AF_INET6) + queue += ((u64) hash_v6(pkt->skb) * priv->queues_total) >> 32; +#endif Quoted 8 lines
net/netfilter/nft_queue.c line 84
Quoted 3 lines
net/netfilter/nft_queue.c new file +3
-0+ +return queue; +}to there, it looks very similar to NFQUEUE. You can move these functions to
net/netfilter/nf_queue.h. You'll have to inline them and add thejhash_initvalparameter. You'll need an initial patch to prepare this change and adaptxt_NFQUEUE.c
Quoted 3 lines
I'm ok with the two hash functions but the
nfqueue_hashthe second argument is different. Do you want me to introduce a new common structure forxt_NFQUEUEandnft_queue?
My suggestion is to add something like:
static inline u32 nfqueue_hash(const struct sk_buff *skb, u16 queuenum,
u16 queues_total, u8 family)
that can be used by both, it will also be defined in net/netfilter/nf_queue.h
v3 cover add nft_queue module
Hello,
This patch is a rework of previous patchset which fix comments sent by Pablo. Code commons to xf_NFQUEUE and nft_queue has been put in a common header. Other minor points have been fixed to.
BR,
--
Eric
PATCH v3 1/3 netfilter: nft: fix issue with verdict support
The test on verdict was simply done on the value of the verdict which is not correct as far as queue is concern. In fact, the test of verdict test must be done with respect to the verdict mask for verdicts which are not internal to nftables.
--- net/netfilter/nf_tables_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_core.c +4 -1
@@ -164,7 +164,7 @@ next_rule:break ; } -switch (data[NFT_REG_VERDICT ].verdict) { +switch (data[NFT_REG_VERDICT ].verdict &NF_VERDICT_MASK ) {case NF_ACCEPT :case NF_DROP :case NF_QUEUE :
@@ -172,6 +172,9 @@ next_rule:nft_trace_packet (pkt, chain, rulenum,NFT_TRACE_RULE );return data[NFT_REG_VERDICT ].verdict; + } + +switch (data[NFT_REG_VERDICT ].verdict) {case NFT_JUMP :if (unlikely (pkt->skb->nf_trace))nft_trace_packet (pkt, chain, rulenum,NFT_TRACE_RULE );
Signature · 1 line
1.8.5
PATCH v3 2/3 netfilter: xt_NFQUEUE: separate reusable code
In reply to Eric LeblondThis patch prepares the adding of nft_queue module by putting reusable code into a header file.
--- net/netfilter/nf_queue.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++ net/netfilter/xt_NFQUEUE.c | 75 +++++--------------------------------------- 2 files changed, 85 insertions(+), 67 deletions(-) create mode 100644 net/netfilter/nf_queue.h
net/netfilter/nf_queue.h new file +77 -0
@@ -0,0 +1,77 @@ +/* +* +* (C) 2013 by Eric Leblond <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ + +#ifndef _NF_QUEUE_H +#define _NF_QUEUE_H + +#include <linux/ip.h> +#include <linux/ipv6.h> +#include <linux/jhash.h> + +static u32 jhash_initval__read_mostly ; + +static inline void init_hashrandom (void ) +{ +while (jhash_initval ==0 ) + jhash_initval =prandom_u32 (); +} + +static inline u32 hash_v4 (const struct sk_buff *skb) +{ +const struct iphdr *iph =ip_hdr (skb); + +/* packets in either direction go into same queue */ +if ((__force u32 )iph->saddr < (__force u32 )iph->daddr) +return jhash_3words ((__force u32 )iph->saddr, + (__force u32 )iph->daddr, iph->protocol, jhash_initval); + +return jhash_3words ((__force u32 )iph->daddr, + (__force u32 )iph->saddr, iph->protocol, jhash_initval); +} + +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) +static inline u32 hash_v6 (const struct sk_buff *skb) +{ +const struct ipv6hdr *ip6h =ipv6_hdr (skb); +u32 a, b, c; + +if ((__force u32 )ip6h->saddr.s6_addr32[3 ] < + (__force u32 )ip6h->daddr.s6_addr32[3 ]) { + a = (__force u32 ) ip6h->saddr.s6_addr32[3 ]; + b = (__force u32 ) ip6h->daddr.s6_addr32[3 ]; + }else { + b = (__force u32 ) ip6h->saddr.s6_addr32[3 ]; + a = (__force u32 ) ip6h->daddr.s6_addr32[3 ]; + } + +if ((__force u32 )ip6h->saddr.s6_addr32[1 ] < + (__force u32 )ip6h->daddr.s6_addr32[1 ]) + c = (__force u32 ) ip6h->saddr.s6_addr32[1 ]; +else + c = (__force u32 ) ip6h->daddr.s6_addr32[1 ]; + +return jhash_3words (a, b, c, jhash_initval); +} +#endif + +static inline u32 +nfqueue_hash (const struct sk_buff *skb,u16 queue,u16 queues_total,u8 family) +{ +if (family ==NFPROTO_IPV4 ) + queue += ((u64 )hash_v4 (skb) * queues_total) >>32 ; +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) + else if (family == NFPROTO_IPV6) + queue += ((u64) hash_v6(skb) * queues_total) >> 32; +#endif + +return queue; +} + +#endif /* _NF_QUEUE_H */
net/netfilter/xt_NFQUEUE.c +8 -67
116 lines not on this page · raw message
Signature · 1 line
1.8.5
Pablo Neira Ayuso
maintainerRe: [PATCHv3 2/3] netfilter: xt_NFQUEUE: separate reusable code
net/netfilter/nf_queue.h
Quoted 15 lines · Eric Leblond
This patch prepares the adding of
nft_queuemodule by putting reusable code into a header file.Signed-off-by Eric Leblond <[email protected]>--- net/netfilter/nf_queue.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++ net/netfilter/xt_NFQUEUE.c | 75 +++++--------------------------------------- 2 files changed, 85 insertions(+), 67 deletions(-) create mode 100644 net/netfilter/nf_queue.hnet/netfilter/nf_queue.h new file +0
-0
Ops, sorry I meant to point to the existing include/net/netfilter/nf_queue.h
v4 cover add nft_queue module
Hello,
OK, here's a new version not adding a useless file when putting reusable code in a header.
Sorry for missing that.
BR,
--
Eric
PATCH v4 1/3 netfilter: nft: fix issue with verdict support
The test on verdict was simply done on the value of the verdict which is not correct as far as queue is concern. In fact, the test of verdict test must be done with respect to the verdict mask for verdicts which are not internal to nftables.
--- net/netfilter/nf_tables_core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_core.c +4 -1
@@ -164,7 +164,7 @@ next_rule:break ; } -switch (data[NFT_REG_VERDICT ].verdict) { +switch (data[NFT_REG_VERDICT ].verdict &NF_VERDICT_MASK ) {case NF_ACCEPT :case NF_DROP :case NF_QUEUE :
@@ -172,6 +172,9 @@ next_rule:nft_trace_packet (pkt, chain, rulenum,NFT_TRACE_RULE );return data[NFT_REG_VERDICT ].verdict; + } + +switch (data[NFT_REG_VERDICT ].verdict) {case NFT_JUMP :if (unlikely (pkt->skb->nf_trace))nft_trace_packet (pkt, chain, rulenum,NFT_TRACE_RULE );
Signature · 1 line
1.8.5
PATCH v4 2/3 netfilter: xt_NFQUEUE: separate reusable code
In reply to Eric LeblondThis patch prepares the adding of nft_queue module by putting reusable code into a header file.
--- include/net/netfilter/nf_queue.h | 63 +++++++++++++++++++++++++++++++++ net/netfilter/xt_NFQUEUE.c | 75 +++++----------------------------------- 2 files changed, 71 insertions(+), 67 deletions(-)
include/net/netfilter/nf_queue.h +63 -0
@@ -1,6 +1,10 @@ #ifndef _NF_QUEUE_H #define _NF_QUEUE_H +#include <linux/ip.h> +#include <linux/ipv6.h> +#include <linux/jhash.h> + /* Each queued (to userspace) skbuff has one of these. */ struct nf_queue_entry { struct list_head list;
@@ -33,4 +37,63 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);bool nf_queue_entry_get_refs (struct nf_queue_entry *entry);void nf_queue_entry_release_refs (struct nf_queue_entry *entry); +static u32 jhash_initval__read_mostly ; + +static inline void init_hashrandom (void ) +{ +while (jhash_initval ==0 ) + jhash_initval =prandom_u32 (); +} + +static inline u32 hash_v4 (const struct sk_buff *skb) +{ +const struct iphdr *iph =ip_hdr (skb); + +/* packets in either direction go into same queue */ +if ((__force u32 )iph->saddr < (__force u32 )iph->daddr) +return jhash_3words ((__force u32 )iph->saddr, + (__force u32 )iph->daddr, iph->protocol, jhash_initval); + +return jhash_3words ((__force u32 )iph->daddr, + (__force u32 )iph->saddr, iph->protocol, jhash_initval); +} + +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) +static inline u32 hash_v6 (const struct sk_buff *skb) +{ +const struct ipv6hdr *ip6h =ipv6_hdr (skb); +u32 a, b, c; + +if ((__force u32 )ip6h->saddr.s6_addr32[3 ] < + (__force u32 )ip6h->daddr.s6_addr32[3 ]) { + a = (__force u32 ) ip6h->saddr.s6_addr32[3 ]; + b = (__force u32 ) ip6h->daddr.s6_addr32[3 ]; + }else { + b = (__force u32 ) ip6h->saddr.s6_addr32[3 ]; + a = (__force u32 ) ip6h->daddr.s6_addr32[3 ]; + } + +if ((__force u32 )ip6h->saddr.s6_addr32[1 ] < + (__force u32 )ip6h->daddr.s6_addr32[1 ]) + c = (__force u32 ) ip6h->saddr.s6_addr32[1 ]; +else + c = (__force u32 ) ip6h->daddr.s6_addr32[1 ]; + +return jhash_3words (a, b, c, jhash_initval); +} +#endif + +static inline u32 +nfqueue_hash (const struct sk_buff *skb,u16 queue,u16 queues_total,u8 family) +{ +if (family ==NFPROTO_IPV4 ) + queue += ((u64 )hash_v4 (skb) * queues_total) >>32 ; +#if IS_ENABLED (CONFIG_IP6_NF_IPTABLES ) + else if (family == NFPROTO_IPV6) + queue += ((u64) hash_v6(skb) * queues_total) >> 32; +#endif + +return queue; +} +#endif /* _NF_QUEUE_H */
net/netfilter/xt_NFQUEUE.c +8 -67
116 lines not on this page · raw message
Signature · 1 line
1.8.5
Pablo Neira Ayuso
maintainerRe: [PATCHv4 2/3] netfilter: xt_NFQUEUE: separate reusable code
include/net/netfilter/nf_queue.h
Quoted 13 lines · Eric Leblond
This patch prepares the adding of
nft_queuemodule by putting reusable code into a header file.Signed-off-by Eric Leblond <[email protected]>--- include/net/netfilter/nf_queue.h | 63 +++++++++++++++++++++++++++++++++ net/netfilter/xt_NFQUEUE.c | 75 +++++----------------------------------- 2 files changed, 71 insertions(+), 67 deletions(-)include/net/netfilter/nf_queue.h +0
-0
This header file is also used by net/netfilter/nfnetlink_queue_core, then...
Quoted 12 lines
include/net/netfilter/nf_queue.h +4
-0@@ -1,6 +1,10 @@ #ifndef _NF_QUEUE_H #define _NF_QUEUE_H +#include <linux/ip.h> +#include <linux/ipv6.h> +#include <linux/jhash.h> + /* Each queued (to userspace) skbuff has one of these. */ struct nf_queue_entry { struct list_head list;
include/net/netfilter/nf_queue.h void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict); · line 40
Quoted 4 lines
include/net/netfilter/nf_queue.h +1
-0@@ -33,4 +37,63 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);bool nf_queue_entry_get_refs (struct nf_queue_entry *entry);void nf_queue_entry_release_refs (struct nf_queue_entry *entry); +static u32 jhash_initval__read_mostly ;
This variable will be also reserved in that module as well. I have fixed this by adding a jhash_initval parameter to init_hashrandom, hash_v4, hash_v6 and nfqueue_hash.
Please, no need to send a new patchset, check my nftables tree and tell if you find any issue, otherwise I'll pull these changes to nf-next. Thanks.
PATCH v4 3/3 netfilter: nft: add queue module
In reply to Eric LeblondThis patch adds a new nft module named "nft_queue" which is a nftables target sending packet to nfnetlink_queue subsystem. It has the same level of functionnality as is iptables ancestor and share some code with it.
--- include/uapi/linux/netfilter/nf_tables.h | 20 +++++ net/netfilter/Kconfig | 9 +++ net/netfilter/Makefile | 1 + net/netfilter/nft_queue.c | 134 +++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 net/netfilter/nft_queue.c
include/uapi/linux/netfilter/nf_tables.h +20 -0
@@ -658,6 +658,26 @@ enum nft_log_attributes {#define NFTA_LOG_MAX (__NFTA_LOG_MAX - 1)/** +* enum nft_queue_attributes - nf_tables queue expression netlink attributes +* +* @NFTA_QUEUE_NUM: netlink queue to send messages to (NLA_U16) +* @NFTA_QUEUE_TOTAL: number of queues to load balance packets on (NLA_U16) +* @NFTA_QUEUE_FLAGS: various flags (NLA_U16) +*/ +enum nft_queue_attributes { +NFTA_QUEUE_UNSPEC , +NFTA_QUEUE_NUM , +NFTA_QUEUE_TOTAL , +NFTA_QUEUE_FLAGS , + __NFTA_QUEUE_MAX +}; +#define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1) + +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03 + +/** * enum nft_reject_types - nf_tables reject expression reject types * * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable
net/netfilter/Kconfig +9 -0
@@ -456,6 +456,15 @@ config NFT_NAT depends on NF_NAT tristate "Netfilter nf_tables nat module" +config NFT_QUEUE + depends on NF_TABLES + depends on NETFILTER_XTABLES + depends on NETFILTER_NETLINK_QUEUE + tristate "Netfilter nf_tables queue module" + help + This is required if you intend to use nfnetlink queue + on more than the default queue 0 or with the other features + config NFT_COMPAT depends on NF_TABLES depends on NETFILTER_XTABLES
net/netfilter/Makefile +1 -0
@@ -76,6 +76,7 @@ obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o +obj-$(CONFIG_NFT_QUEUE) += nft_queue.o #nf_tables-objs += nft_meta_target.o obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o obj-$(CONFIG_NFT_HASH) += nft_hash.o
net/netfilter/nft_queue.c new file +134 -0
134 lines not on this page · raw message
Signature · 1 line
1.8.5
Pablo Neira Ayuso
maintainerRe: [PATCHv4 0/3] add nft_queue module
In reply to Eric LeblondQuoted 5 lines · Eric Leblond
Hello,
OK, here's a new version not adding a useless file when putting reusable code in a header.
Applied with changes, please check:
http://git.kernel.org/cgit/linux/kernel/git/pablo/nftables.git
and let me know if you have any concern with my changes. Thanks.
Re: [PATCHv4 0/3] add nft_queue module
Hello,
Quoted 4 lines · Pablo Neira Ayuso
Quoted 3 lines · Eric Leblond
Hello,
Quoted 8 lines
Quoted 2 lines
OK, here's a new version not adding a useless file when putting reusable code in a header.
Applied with changes, please check:
http://git.kernel.org/cgit/linux/kernel/git/pablo/nftables.git
and let me know if you have any concern with my changes. Thanks.
Good for me. Thanks a lot for the review and fixes.
BR,
Signature · 1 line
Eric Leblond <[email protected]>
PATCH v3 3/3 netfilter: nft: add queue module
In reply to Eric LeblondThis patch adds a new nft module named "nft_queue" which is a nftables target sending packet to nfnetlink_queue subsystem. It has the same level of functionnality as is iptables ancestor and share some code with it.
--- include/uapi/linux/netfilter/nf_tables.h | 20 +++++ net/netfilter/Kconfig | 9 +++ net/netfilter/Makefile | 1 + net/netfilter/nft_queue.c | 134 +++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 net/netfilter/nft_queue.c
include/uapi/linux/netfilter/nf_tables.h +20 -0
@@ -658,6 +658,26 @@ enum nft_log_attributes {#define NFTA_LOG_MAX (__NFTA_LOG_MAX - 1)/** +* enum nft_queue_attributes - nf_tables queue expression netlink attributes +* +* @NFTA_QUEUE_NUM: netlink queue to send messages to (NLA_U16) +* @NFTA_QUEUE_TOTAL: number of queues to load balance packets on (NLA_U16) +* @NFTA_QUEUE_FLAGS: various flags (NLA_U16) +*/ +enum nft_queue_attributes { +NFTA_QUEUE_UNSPEC , +NFTA_QUEUE_NUM , +NFTA_QUEUE_TOTAL , +NFTA_QUEUE_FLAGS , + __NFTA_QUEUE_MAX +}; +#define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1) + +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03 + +/** * enum nft_reject_types - nf_tables reject expression reject types * * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable
net/netfilter/Kconfig +9 -0
@@ -456,6 +456,15 @@ config NFT_NAT depends on NF_NAT tristate "Netfilter nf_tables nat module" +config NFT_QUEUE + depends on NF_TABLES + depends on NETFILTER_XTABLES + depends on NETFILTER_NETLINK_QUEUE + tristate "Netfilter nf_tables queue module" + help + This is required if you intend to use nfnetlink queue + on more than the default queue 0 or with the other features + config NFT_COMPAT depends on NF_TABLES depends on NETFILTER_XTABLES
net/netfilter/Makefile +1 -0
@@ -76,6 +76,7 @@ obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o +obj-$(CONFIG_NFT_QUEUE) += nft_queue.o #nf_tables-objs += nft_meta_target.o obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o obj-$(CONFIG_NFT_HASH) += nft_hash.o
net/netfilter/nft_queue.c new file +134 -0
134 lines not on this page · raw message
Signature · 1 line
1.8.5
Re: [PATCH 2/2] netfilter: nft: add queue module
In reply to Eric LeblondHi Eric,
include/uapi/linux/netfilter/nf_tables.h enum nft_log_attributes { · line 678
Quoted 4 lines
include/uapi/linux/netfilter/nf_tables.h +4
-0+ +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03
Why not declaring them in an enum, as for nft_rule_compat_flags, nft_set_flags or nft_set_elem_flags?
Tomasz
Re: [PATCH 2/2] netfilter: nft: add queue module
Hello,
Quoted 9 lines · Tomasz Bursztyka
Hi Eric,
include/uapi/linux/netfilter/nf_tables.h enum nft_log_attributes { · line 678
Quoted 4 lines
include/uapi/linux/netfilter/nf_tables.h +4
-0+ +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03Why not declaring them in an enum, as for
nft_rule_compat_flags,nft_set_flagsornft_set_elem_flags?
Simple answer: It was not an enum in the original NFQUEUE code ;)
Real answer: We have only two values, third one is a mask. So it may be a bit overkill to use an enum here.
BR,
Signature · 1 line
Eric Leblond <[email protected]>
Re: [PATCH 2/2] netfilter: nft: add queue module
Hi Eric,
Quoted 6 lines
Quoted 2 lines
Why not declaring them in an enum, as for
nft_rule_compat_flags,Quoted 1 line
nft_set_flagsornft_set_elem_flags?Simple answer: It was not an enum in the original NFQUEUE code;)
Real answer: We have only two values, third one is a mask. So it may be a bit overkill to use an enum here.
I understand, but this would be diverging from what has been the "rule" so far. It's a minor detail I agree.
Btw, nft_set_elem_flags owns only one value ^^
Tomasz
PATCH 1/2 expr: add support for nfnetlink queue
In reply to Eric LeblondThis patch adds a support of the queue target.
--- include/libnftables/expr.h | 5 + include/linux/netfilter/nf_tables.h | 20 +++ src/Makefile.am | 1 + src/expr/queue.c | 254 ++++++++++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+) create mode 100644 src/expr/queue.c
include/libnftables/expr.h +5 -0
@@ -143,6 +143,11 @@ enum { NFT_EXPR_REJECT_CODE, }; +enum { +NFT_EXPR_QUEUE_NUM =NFT_RULE_EXPR_ATTR_BASE , +NFT_EXPR_QUEUE_TOTAL , +NFT_EXPR_QUEUE_FLAGS , +}; #ifdef __cplusplus }/* extern "C" */ #endif
include/linux/netfilter/nf_tables.h +20 -0
@@ -580,6 +580,26 @@ enum nft_log_attributes {#define NFTA_LOG_MAX (__NFTA_LOG_MAX - 1)/** +* enum nft_queue_attributes - nf_tables queue expression netlink attributes +* +* @NFTA_QUEUE_NUM: netlink queue to send messages to (NLA_U16) +* @NFTA_QUEUE_TOTAL: number of queues to load balance packets on (NLA_U16) +* @NFTA_QUEUE_FLAGS: various flags (NLA_U16) +*/ +enum nft_queue_attributes { +NFTA_QUEUE_UNSPEC , +NFTA_QUEUE_NUM , +NFTA_QUEUE_TOTAL , +NFTA_QUEUE_FLAGS , + __NFTA_QUEUE_MAX +}; +#define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1) + +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03 + +/** * enum nft_reject_types - nf_tables reject expression reject types * * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable
src/Makefile.am +1 -0
@@ -31,6 +31,7 @@ libnftables_la_SOURCES = utils.c \ expr/meta.c \ expr/nat.c \ expr/payload.c \ + expr/queue.c \ expr/reject.c \ expr/target.c \ expr/data_reg.h \
src/expr/queue.c new file +254 -0
254 lines not on this page · raw message
Signature · 1 line
1.8.4.4
PATCH 2/2 test: add tests for expr queue
--- tests/Makefile.am | 4 ++ tests/nft-expr_queue-test.c | 99 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/nft-expr_queue-test.c
tests/Makefile.am +4 -0
@@ -19,6 +19,7 @@ check_PROGRAMS = nft-parsing-test \ nft-expr_meta-test \ nft-expr_nat-test \ nft-expr_payload-test \ + nft-expr_queue-test \ nft-expr_reject-test \ nft-expr_target-test
@@ -79,6 +80,9 @@ nft_expr_nat_test_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} nft_expr_payload_test_SOURCES = nft-expr_payload-test.c nft_expr_payload_test_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} +nft_expr_queue_test_SOURCES = nft-expr_queue-test.c +nft_expr_queue_test_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} + nft_expr_reject_test_SOURCES = nft-expr_reject-test.c nft_expr_reject_test_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
tests/nft-expr_queue-test.c new file +99 -0
99 lines not on this page · raw message
Signature · 1 line
1.8.4.4
Re: [libnftables PATCH 1/2] expr: add support for nfnetlink queue
In reply to Eric LeblondQuoted 1 line · Eric Leblond
This patch adds a support of the queue target.
Applied these two libnftables patches to the next-3.14 branch:
http://git.netfilter.org/libnftables/log/?h=next-3.14
Will merge this to master once 3.13 is out as this feature is scheduled for 3.14.
PATCH 1/1 Add support for queue target
In reply to Eric LeblondThis patch adds support for the queue target. It is now possible to specify rule sending packet to a given queue and using load balancing:
nft add rule filter output queue num 3 total 2 options fanout
--- include/linux/netfilter/nf_tables.h | 20 ++++++++++++ include/statement.h | 11 +++++++ src/evaluate.c | 2 ++ src/netlink_delinearize.c | 15 +++++++++ src/netlink_linearize.c | 22 +++++++++++++ src/parser.y | 61 ++++++++++++++++++++++++++++++++++--- src/scanner.l | 8 ++++- src/statement.c | 31 +++++++++++++++++++ 8 files changed, 164 insertions(+), 6 deletions(-)
include/linux/netfilter/nf_tables.h +20 -0
@@ -587,6 +587,26 @@ enum nft_log_attributes {#define NFTA_LOG_MAX (__NFTA_LOG_MAX - 1)/** +* enum nft_queue_attributes - nf_tables queue expression netlink attributes +* +* @NFTA_QUEUE_NUM: netlink group to send messages to (NLA_U32) +* @NFTA_QUEUE_TOTAL: prefix to prepend to log messages (NLA_STRING) +* @NFTA_QUEUE_FLAGS: length of payload to include in netlink message (NLA_U32) +*/ +enum nft_queue_attributes { +NFTA_QUEUE_UNSPEC , +NFTA_QUEUE_NUM , +NFTA_QUEUE_TOTAL , +NFTA_QUEUE_FLAGS , + __NFTA_QUEUE_MAX +}; +#define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1) + +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03 + +/** * enum nft_reject_types - nf_tables reject expression reject types * * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable
include/statement.h +11 -0
@@ -59,6 +59,14 @@ struct nat_stmt {extern struct stmt *nat_stmt_alloc (const struct location *loc); +struct queue_stmt { +uint16_t queuenum; +uint16_t queues_total; +uint16_t flags; +}; + +extern struct stmt *queue_stmt_alloc (const struct location *loc); +/** * enum stmt_types - statement types *
@@ -71,6 +79,7 @@ extern struct stmt *nat_stmt_alloc(const struct location *loc);* @STMT_LOG: log statement * @STMT_REJECT: REJECT statement * @STMT_NAT: NAT statement +* @STMT_QUEUE: QUEUE statement */ enum stmt_types {STMT_INVALID ,
@@ -82,6 +91,7 @@ enum stmt_types {STMT_LOG ,STMT_REJECT ,STMT_NAT , + STMT_QUEUE, };/**
@@ -127,6 +137,7 @@ struct stmt {struct limit_stmt limit;struct reject_stmt reject;struct nat_stmt nat; +struct queue_stmt queue; }; };
src/evaluate.c +2 -0
@@ -1174,6 +1174,8 @@ static int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)return stmt_evaluate_reject (ctx, stmt);case STMT_NAT :return stmt_evaluate_nat (ctx, stmt); +case STMT_QUEUE : +return 0 ;default :BUG ("unknown statement type %s\n" , stmt->ops->name); }
src/netlink_delinearize.c +15 -0
@@ -481,6 +481,20 @@ static void netlink_parse_nat(struct netlink_parse_ctx *ctx,list_add_tail (&stmt->list, &ctx->rule->stmts); } +static void netlink_parse_queue (struct netlink_parse_ctx *ctx, +const struct location *loc, +const struct nft_rule_expr *nle) +{ +struct stmt *stmt; + + stmt =queue_stmt_alloc (loc); + stmt->queue.queuenum =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_NUM ); + stmt->queue.queues_total = +nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_TOTAL ); + stmt->queue.flags =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_FLAGS ); +list_add_tail (&stmt->list, &ctx->rule->stmts); +} + static const struct { const char *name; void (*parse)(struct netlink_parse_ctx *ctx,
@@ -501,6 +515,7 @@ static const struct { { .name = "limit", .parse = netlink_parse_limit }, { .name = "reject", .parse = netlink_parse_reject }, { .name = "nat", .parse = netlink_parse_nat }, + { .name = "queue", .parse = netlink_parse_queue }, }; static const struct input_descriptor indesc_netlink = {
src/netlink_linearize.c +22 -0
@@ -634,6 +634,26 @@ static void netlink_gen_nat_stmt(struct netlink_linearize_ctx *ctx,nft_rule_add_expr (ctx->nlr, nle); } +static void netlink_gen_queue_stmt (struct netlink_linearize_ctx *ctx, +const struct stmt *stmt) +{ +struct nft_rule_expr *nle; + + nle =alloc_nft_expr ("queue" ); + +nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_NUM , + stmt->queue.queuenum); +if (stmt->queue.queues_total) { +nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_TOTAL , + stmt->queue.queues_total); + } +if (stmt->queue.flags) { +nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_FLAGS , + stmt->queue.flags); + } +nft_rule_add_expr (ctx->nlr, nle); +} +static void netlink_gen_stmt (struct netlink_linearize_ctx *ctx,const struct stmt *stmt) {
@@ -654,6 +674,8 @@ static void netlink_gen_stmt(struct netlink_linearize_ctx *ctx,return netlink_gen_reject_stmt (ctx, stmt);case STMT_NAT :return netlink_gen_nat_stmt (ctx, stmt); +case STMT_QUEUE : +return netlink_gen_queue_stmt (ctx, stmt);default :BUG ("unknown statement type %s\n" , stmt->ops->name); }
src/parser.y +56 -5
97 lines not on this page · raw message
src/scanner.l +7 -1
@@ -234,7 +234,6 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "jump" { return JUMP; } "goto" { return GOTO; } "return" { return RETURN; } -"queue" { return QUEUE; } "add" { return ADD; } "insert" { return INSERT; }
@@ -255,6 +254,13 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "snaplen" { return SNAPLEN; } "queue-threshold" { return QUEUE_THRESHOLD; } +"queue" { return QUEUE;} +"num" { return QUEUENUM;} +"total" { return QUEUETOTAL;} +"bypass" { return QUEUEBYPASS;} +"fanout" { return QUEUECPUFANOUT;} +"options" { return OPTIONS;} + "limit" { return LIMIT; } "rate" { return RATE; }
src/statement.c +31 -0
@@ -172,6 +172,37 @@ struct stmt *limit_stmt_alloc(const struct location *loc)return stmt_alloc (loc, &limit_stmt_ops); } +static void queue_stmt_print (const struct stmt *stmt) +{ +int one =0 ; + +printf ("queue num %u total %u" , + stmt->queue.queuenum, stmt->queue.queues_total); +if (stmt->queue.flags) +printf (" options " ); +if (stmt->queue.flags &NFT_QUEUE_FLAG_BYPASS ) { +printf ("bypass" ); + one =1 ; + } +if (stmt->queue.flags &NFT_QUEUE_FLAG_CPU_FANOUT ) { +if (one) +printf ("," ); +printf ("fanout" ); + } + +} + +static const struct stmt_ops queue_stmt_ops = { + .type =STMT_QUEUE , + .name ="queue" , + .print = queue_stmt_print, +}; + +struct stmt *queue_stmt_alloc (const struct location *loc) +{ +return stmt_alloc (loc, &queue_stmt_ops); +} +static void reject_stmt_print (const struct stmt *stmt) {printf ("reject" );
Signature · 1 line
1.8.4.4
v2 cover Support for queue target
Hello,
Here's a small update of previous patch. Only change is the fix of a warning in parser.y.
BR,
--
Eric
PATCH v2 1/1 Add support for queue target
This patch adds support for the queue target. It is now possible to specify rule sending packet to a given queue and using load balancing:
nft add rule filter output queue num 3 total 2 options fanout
--- include/linux/netfilter/nf_tables.h | 20 +++++++++++ include/statement.h | 11 ++++++ src/evaluate.c | 2 ++ src/netlink_delinearize.c | 15 +++++++++ src/netlink_linearize.c | 22 ++++++++++++ src/parser.y | 67 ++++++++++++++++++++++++++++++++++--- src/scanner.l | 8 ++++- src/statement.c | 31 +++++++++++++++++ 8 files changed, 170 insertions(+), 6 deletions(-)
include/linux/netfilter/nf_tables.h +20 -0
@@ -587,6 +587,26 @@ enum nft_log_attributes {#define NFTA_LOG_MAX (__NFTA_LOG_MAX - 1)/** +* enum nft_queue_attributes - nf_tables queue expression netlink attributes +* +* @NFTA_QUEUE_NUM: netlink group to send messages to (NLA_U32) +* @NFTA_QUEUE_TOTAL: prefix to prepend to log messages (NLA_STRING) +* @NFTA_QUEUE_FLAGS: length of payload to include in netlink message (NLA_U32) +*/ +enum nft_queue_attributes { +NFTA_QUEUE_UNSPEC , +NFTA_QUEUE_NUM , +NFTA_QUEUE_TOTAL , +NFTA_QUEUE_FLAGS , + __NFTA_QUEUE_MAX +}; +#define NFTA_QUEUE_MAX (__NFTA_QUEUE_MAX - 1) + +#define NFT_QUEUE_FLAG_BYPASS 0x01/* for compatibility with v2 */ +#define NFT_QUEUE_FLAG_CPU_FANOUT 0x02/* use current CPU (no hashing) */ +#define NFT_QUEUE_FLAG_MASK 0x03 + +/** * enum nft_reject_types - nf_tables reject expression reject types * * @NFT_REJECT_ICMP_UNREACH: reject using ICMP unreachable
include/statement.h +11 -0
@@ -59,6 +59,14 @@ struct nat_stmt {extern struct stmt *nat_stmt_alloc (const struct location *loc); +struct queue_stmt { +uint16_t queuenum; +uint16_t queues_total; +uint16_t flags; +}; + +extern struct stmt *queue_stmt_alloc (const struct location *loc); +/** * enum stmt_types - statement types *
@@ -71,6 +79,7 @@ extern struct stmt *nat_stmt_alloc(const struct location *loc);* @STMT_LOG: log statement * @STMT_REJECT: REJECT statement * @STMT_NAT: NAT statement +* @STMT_QUEUE: QUEUE statement */ enum stmt_types {STMT_INVALID ,
@@ -82,6 +91,7 @@ enum stmt_types {STMT_LOG ,STMT_REJECT ,STMT_NAT , + STMT_QUEUE, };/**
@@ -127,6 +137,7 @@ struct stmt {struct limit_stmt limit;struct reject_stmt reject;struct nat_stmt nat; +struct queue_stmt queue; }; };
src/evaluate.c +2 -0
@@ -1174,6 +1174,8 @@ static int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)return stmt_evaluate_reject (ctx, stmt);case STMT_NAT :return stmt_evaluate_nat (ctx, stmt); +case STMT_QUEUE : +return 0 ;default :BUG ("unknown statement type %s\n" , stmt->ops->name); }
src/netlink_delinearize.c +15 -0
@@ -508,6 +508,20 @@ static void netlink_parse_nat(struct netlink_parse_ctx *ctx,list_add_tail (&stmt->list, &ctx->rule->stmts); } +static void netlink_parse_queue (struct netlink_parse_ctx *ctx, +const struct location *loc, +const struct nft_rule_expr *nle) +{ +struct stmt *stmt; + + stmt =queue_stmt_alloc (loc); + stmt->queue.queuenum =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_NUM ); + stmt->queue.queues_total = +nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_TOTAL ); + stmt->queue.flags =nft_rule_expr_get_u16 (nle,NFT_EXPR_QUEUE_FLAGS ); +list_add_tail (&stmt->list, &ctx->rule->stmts); +} + static const struct { const char *name; void (*parse)(struct netlink_parse_ctx *ctx,
@@ -528,6 +542,7 @@ static const struct { { .name = "limit", .parse = netlink_parse_limit }, { .name = "reject", .parse = netlink_parse_reject }, { .name = "nat", .parse = netlink_parse_nat }, + { .name = "queue", .parse = netlink_parse_queue }, }; static const struct input_descriptor indesc_netlink = {
src/netlink_linearize.c +22 -0
@@ -636,6 +636,26 @@ static void netlink_gen_nat_stmt(struct netlink_linearize_ctx *ctx,nft_rule_add_expr (ctx->nlr, nle); } +static void netlink_gen_queue_stmt (struct netlink_linearize_ctx *ctx, +const struct stmt *stmt) +{ +struct nft_rule_expr *nle; + + nle =alloc_nft_expr ("queue" ); + +nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_NUM , + stmt->queue.queuenum); +if (stmt->queue.queues_total) { +nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_TOTAL , + stmt->queue.queues_total); + } +if (stmt->queue.flags) { +nft_rule_expr_set_u16 (nle,NFT_EXPR_QUEUE_FLAGS , + stmt->queue.flags); + } +nft_rule_add_expr (ctx->nlr, nle); +} +static void netlink_gen_stmt (struct netlink_linearize_ctx *ctx,const struct stmt *stmt) {
@@ -656,6 +676,8 @@ static void netlink_gen_stmt(struct netlink_linearize_ctx *ctx,return netlink_gen_reject_stmt (ctx, stmt);case STMT_NAT :return netlink_gen_nat_stmt (ctx, stmt); +case STMT_QUEUE : +return netlink_gen_queue_stmt (ctx, stmt);default :BUG ("unknown statement type %s\n" , stmt->ops->name); }
src/parser.y +62 -5
103 lines not on this page · raw message
src/scanner.l +7 -1
@@ -234,7 +234,6 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "jump" { return JUMP; } "goto" { return GOTO; } "return" { return RETURN; } -"queue" { return QUEUE; } "add" { return ADD; } "insert" { return INSERT; }
@@ -255,6 +254,13 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "snaplen" { return SNAPLEN; } "queue-threshold" { return QUEUE_THRESHOLD; } +"queue" { return QUEUE;} +"num" { return QUEUENUM;} +"total" { return QUEUETOTAL;} +"bypass" { return QUEUEBYPASS;} +"fanout" { return QUEUECPUFANOUT;} +"options" { return OPTIONS;} + "limit" { return LIMIT; } "rate" { return RATE; }
src/statement.c +31 -0
@@ -172,6 +172,37 @@ struct stmt *limit_stmt_alloc(const struct location *loc)return stmt_alloc (loc, &limit_stmt_ops); } +static void queue_stmt_print (const struct stmt *stmt) +{ +int one =0 ; + +printf ("queue num %u total %u" , + stmt->queue.queuenum, stmt->queue.queues_total); +if (stmt->queue.flags) +printf (" options " ); +if (stmt->queue.flags &NFT_QUEUE_FLAG_BYPASS ) { +printf ("bypass" ); + one =1 ; + } +if (stmt->queue.flags &NFT_QUEUE_FLAG_CPU_FANOUT ) { +if (one) +printf ("," ); +printf ("fanout" ); + } + +} + +static const struct stmt_ops queue_stmt_ops = { + .type =STMT_QUEUE , + .name ="queue" , + .print = queue_stmt_print, +}; + +struct stmt *queue_stmt_alloc (const struct location *loc) +{ +return stmt_alloc (loc, &queue_stmt_ops); +} +static void reject_stmt_print (const struct stmt *stmt) {printf ("reject" );
Signature · 1 line
1.8.5.2
Pablo Neira Ayuso
maintainerRe: [nftables PATCHv2] Add support for queue target
Quoted 5 lines · Eric Leblond
This patch adds support for the queue target. It is now possible to specify rule sending packet to a given queue and using load balancing:
nft add rule filter output queue num 3 total 2 options fanout
Enqueued this to 3.14, thanks Eric!