Skip to contentSkip to conversation

Flow offload infrastructure

RFC PATCH Superseded Pablo Neira Ayuso 5 participants 14 messages Nov 3, 2017 – Nov 14, 2017

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover Flow offload infrastructure

Hi,

This patch adds the flow offload infrastructure for Netfilter. This adds a new 'nf_flow_offload' module that registers a hook at ingress. Every packet that hits the flow table is forwarded to where the flow table entry specifies in terms of destination/gateway and netdevice. In case of flow table miss, the packet follows the classic forward path.

This flow table is populated via the new nftables VM action 'flow_offload', so the user can selectively specify what flows are placed into the flow table, an example ruleset would look like this:

        table inet x {
                chain y {
                        type filter hook forward priority 0; policy accept;
                        ip protocol tcp flow offload counter
                        counter
                }
        }

The 'flow offload' action adds the flow entry once the flow is in established state, according to the connection tracking definition, ie. we have seen traffic in both directions. Therefore, only initial packets of the flow follow the classic forwarding path.

* Patch 1/5 is nothing really interesting, just a little preparation change.

* Patch 2/5 adds a software flow table representation. It uses the rhashtable and an API to operate with it, it also introduces the 'struct flow_offload' that represents a flow table entry. There's a garbage collector kernel thread that cleans up entries for which we have not seen any packet for a while.

* Patch 3/5 Just adds the missing bits to integrate the software flow table with conntrack. The software flow table owns the conntrack object, so it is basically responsible for releasing it. Conntrack entries that have been offloaded in the conntrack table will look like this:

ipv4 2 tcp 6 src=10.141.10.2 dst=147.75.205.195 sport=36392 dport=443 src=147.75.205.195 dst=192.168.2.195 sport=443 dport=36392 [OFFLOAD] use=2

* Patch 4/5 adds the extension for nf_tables that can be used to select what flows are offloaded through policy.

* Patch 5/5 Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while. This patch adds the ndo hooks to populate hardware flow table. This patchs a workqueue to configure from user context - we need to hold the mdio mutex for this. There will be a little time until packets will follow the hardware path. So packets will be following the software flow table path for a little while until the start going through hardware.

I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed.

TODO, still many things:

* Only IPv4 at this time.
* Only IPv4 SNAT is supported.
* No netns support yet.
* Missing netlink interface to operate with the flow table, to force the handover of flow to the software path.
* Higher configurability, instead of registering the flow table inconditionally, add an interface to specify software flow table properties.
* No flow counters at this time.

This should serve a number of usecases where we can rely on this kernel bypass. Packets that need fragmentation / PMTU / IP option handling / ... and any specific handling, then we should pass them up to the forwarding classic path.

Comments welcome,
Thanks.

Pablo Neira Ayuso (5):
netfilter: nf_conntrack: move nf_ct_netns_{get,put}() to core
netfilter: add software flow offload infrastructure
netfilter: nf_flow_offload: integration with conntrack
netfilter: nf_tables: flow offload expression
netfilter: nft_flow_offload: add ndo hooks for hardware offload

 include/linux/netdevice.h                          |   4 +
 include/net/flow_offload.h                         |  67 ++++
 include/net/netfilter/nf_conntrack.h               |   3 +-
 include/uapi/linux/netfilter/nf_conntrack_common.h |   4 +
 include/uapi/linux/netfilter/nf_tables.h           |   9 +
 net/netfilter/Kconfig                              |  14 +
 net/netfilter/Makefile                             |   4 +
 net/netfilter/nf_conntrack_core.c                  |   7 +-
 net/netfilter/nf_conntrack_netlink.c               |  15 +-
 net/netfilter/nf_conntrack_proto.c                 |  37 +-
 net/netfilter/nf_conntrack_proto_tcp.c             |   3 +
 net/netfilter/nf_conntrack_standalone.c            |  12 +-
 net/netfilter/nf_flow_offload.c                    | 421 ++++++++++++++++++++
 net/netfilter/nft_ct.c                             |  39 +-
 net/netfilter/nft_flow_offload.c                   | 430 +++++++++++++++++++++
 15 files changed, 1024 insertions(+), 45 deletions(-)
 create mode 100644 include/net/flow_offload.h
 create mode 100644 net/netfilter/nf_flow_offload.c
 create mode 100644 net/netfilter/nft_flow_offload.c
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

RFC PATCH 1/5 netfilter: nf_conntrack: move nf_ct_netns_{get,put}() to core

So we can call this from other expression that need conntrack in place to work.

---
 net/netfilter/nf_conntrack_proto.c | 37 ++++++++++++++++++++++++++++++++++--
 net/netfilter/nft_ct.c             | 39 +++-----------------------------------
 2 files changed, 38 insertions(+), 38 deletions(-)
net/netfilter/nf_conntrack_proto.c +35 -2
index b3e489c859ec..4379f1244154 100644
@@ -125,7 +125,7 @@ void nf_ct_l3proto_module_put(unsigned short l3proto)
 }
 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
 
-int nf_ct_netns_get(struct net *net, u8 nfproto)
+static int nf_ct_netns_do_get(struct net *net, u8 nfproto)
 {
 	const struct nf_conntrack_l3proto *l3proto;
 	int ret;
@@ -150,9 +150,33 @@ int nf_ct_netns_get(struct net *net, u8 nfproto)
 
 	return ret;
 }
+
+int nf_ct_netns_get(struct net *net, u8 nfproto)
+{
+	int err;
+
+	if (nfproto == NFPROTO_INET) {
+		err = nf_ct_netns_do_get(net, NFPROTO_IPV4);
+		if (err < 0)
+			goto err1;
+		err = nf_ct_netns_do_get(net, NFPROTO_IPV6);
+		if (err < 0)
+			goto err2;
+	} else {
+		err = nf_ct_netns_do_get(net, nfproto);
+		if (err < 0)
+			goto err1;
+	}
+	return 0;
+
+err2:
+	nf_ct_netns_put(net, NFPROTO_IPV4);
+err1:
+	return err;
+}
 EXPORT_SYMBOL_GPL(nf_ct_netns_get);
 
-void nf_ct_netns_put(struct net *net, u8 nfproto)
+static void nf_ct_netns_do_put(struct net *net, u8 nfproto)
 {
 	const struct nf_conntrack_l3proto *l3proto;
 
@@ -171,6 +195,15 @@ void nf_ct_netns_put(struct net *net, u8 nfproto)
 
 	nf_ct_l3proto_module_put(nfproto);
 }
+
+void nf_ct_netns_put(struct net *net, uint8_t nfproto)
+{
+	if (nfproto == NFPROTO_INET) {
+		nf_ct_netns_do_put(net, NFPROTO_IPV4);
+		nf_ct_netns_do_put(net, NFPROTO_IPV6);
+	} else
+		nf_ct_netns_do_put(net, nfproto);
+}
 EXPORT_SYMBOL_GPL(nf_ct_netns_put);
 
 const struct nf_conntrack_l4proto *
net/netfilter/nft_ct.c +3 -36
index bd0975d7dd6f..2647b895f4b0 100644
@@ -312,39 +312,6 @@ static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
 	[NFTA_CT_SREG]		= { .type = NLA_U32 },
 };
 
-static int nft_ct_netns_get(struct net *net, uint8_t family)
-{
-	int err;
-
-	if (family == NFPROTO_INET) {
-		err = nf_ct_netns_get(net, NFPROTO_IPV4);
-		if (err < 0)
-			goto err1;
-		err = nf_ct_netns_get(net, NFPROTO_IPV6);
-		if (err < 0)
-			goto err2;
-	} else {
-		err = nf_ct_netns_get(net, family);
-		if (err < 0)
-			goto err1;
-	}
-	return 0;
-
-err2:
-	nf_ct_netns_put(net, NFPROTO_IPV4);
-err1:
-	return err;
-}
-
-static void nft_ct_netns_put(struct net *net, uint8_t family)
-{
-	if (family == NFPROTO_INET) {
-		nf_ct_netns_put(net, NFPROTO_IPV4);
-		nf_ct_netns_put(net, NFPROTO_IPV6);
-	} else
-		nf_ct_netns_put(net, family);
-}
-
 #ifdef CONFIG_NF_CONNTRACK_ZONES
 static void nft_ct_tmpl_put_pcpu(void)
 {
@@ -489,7 +456,7 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,
 	if (err < 0)
 		return err;
 
-	err = nft_ct_netns_get(ctx->net, ctx->afi->family);
+	err = nf_ct_netns_get(ctx->net, ctx->afi->family);
 	if (err < 0)
 		return err;
 
@@ -583,7 +550,7 @@ static int nft_ct_set_init(const struct nft_ctx *ctx,
 	if (err < 0)
 		goto err1;
 
-	err = nft_ct_netns_get(ctx->net, ctx->afi->family);
+	err = nf_ct_netns_get(ctx->net, ctx->afi->family);
 	if (err < 0)
 		goto err1;
 
@@ -606,7 +573,7 @@ static void nft_ct_set_destroy(const struct nft_ctx *ctx,
 	struct nft_ct *priv = nft_expr_priv(expr);
 
 	__nft_ct_set_destroy(ctx, priv);
-	nft_ct_netns_put(ctx->net, ctx->afi->family);
+	nf_ct_netns_put(ctx->net, ctx->afi->family);
 }
 
 static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
Signature · 1 line
2.11.0
author maintainer
#3 lore raw

RFC PATCH 2/5 netfilter: add software flow offload infrastructure

In reply to Pablo Neira Ayuso

This patch adds the generic software flow offload infrastructure. This allows users to configure fast path for established flows that will not follow the classic forwarding path.

This adds a new hook at netfilter ingress for each existing interface. For each packet that hits the hook, we look up for an existing flow in the table, if there is a hit, the packet is forwarded by using the gateway and interfaces that are cached in the flow table entry.

This comes with a kernel thread to release flow table entries if no packets are seen after a little while, so the flow table entry is released.

---
 include/net/flow_offload.h      |  67 +++++++
 net/netfilter/Kconfig           |   7 +
 net/netfilter/Makefile          |   3 +
 net/netfilter/nf_flow_offload.c | 386 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 463 insertions(+)
 create mode 100644 include/net/flow_offload.h
 create mode 100644 net/netfilter/nf_flow_offload.c
include/net/flow_offload.h new file +67 -0
new file mode 100644
index 000000000000..30bfca7ed3f1
@@ -0,0 +1,67 @@
+#ifndef _FLOW_OFFLOAD_H
+#define _FLOW_OFFLOAD_H
+
+#include <linux/in.h>
+#include <linux/in6.h>
+#include <linux/netdevice.h>
+#include <linux/rhashtable.h>
+#include <linux/rcupdate.h>
+
+enum flow_offload_tuple_dir {
+	FLOW_OFFLOAD_DIR_ORIGINAL,
+	FLOW_OFFLOAD_DIR_REPLY,
+	__FLOW_OFFLOAD_DIR_MAX		= FLOW_OFFLOAD_DIR_REPLY,
+};
+#define FLOW_OFFLOAD_DIR_MAX	(__FLOW_OFFLOAD_DIR_MAX + 1)
+
+struct flow_offload_tuple {
+	union {
+		struct in_addr		src_v4;
+		struct in6_addr		src_v6;
+	};
+	union {
+		struct in_addr		dst_v4;
+		struct in6_addr		dst_v6;
+	};
+	struct {
+		__be16			src_port;
+		__be16			dst_port;
+	};
+
+	u8				l3proto;
+	u8				l4proto;
+	u8				dir;
+
+	int				iifidx;
+	int				oifidx;
+
+	union {
+		__be32			gateway;
+		struct in6_addr		gateway6;
+	};
+};
+
+struct flow_offload_tuple_rhash {
+	struct rhash_head		node;
+	struct flow_offload_tuple	tuple;
+};
+
+#define	FLOW_OFFLOAD_SNAT	0x1
+#define	FLOW_OFFLOAD_DNAT	0x2
+#define	FLOW_OFFLOAD_HW		0x4
+
+struct flow_offload {
+	struct flow_offload_tuple_rhash		tuplehash[FLOW_OFFLOAD_DIR_MAX];
+	u32					flags;
+	union {
+		/* Your private driver data here. */
+		u32		timeout;
+	};
+	struct rcu_head				rcu_head;
+};
+
+int flow_offload_add(struct flow_offload *flow);
+void flow_offload_del(struct flow_offload *flow);
+struct flow_offload_tuple_rhash *flow_offload_lookup(struct flow_offload_tuple *tuple);
+
+#endif /* _FLOW_OFFLOAD_H */
net/netfilter/Kconfig +7 -0
index e4a13cc8a2e7..f022ca91f49d 100644
@@ -436,6 +436,13 @@ config NETFILTER_SYNPROXY
 
 endif # NF_CONNTRACK
 
+config NF_FLOW_OFFLOAD
+	tristate "Netfilter Generic Flow Offload (GFO) module"
+	help
+	  This option adds the flow table core infrastructure.
+
+	  To compile it as a module, choose M here.
+
 config NF_TABLES
 	select NETFILTER_NETLINK
 	tristate "Netfilter nf_tables support"
net/netfilter/Makefile +3 -0
index d3891c93edd6..518f54113e06 100644
@@ -69,6 +69,9 @@ obj-$(CONFIG_NETFILTER_SYNPROXY) += nf_synproxy_core.o
 # generic packet duplication from netdev family
 obj-$(CONFIG_NF_DUP_NETDEV)	+= nf_dup_netdev.o
 
+# generic flow table
+obj-$(CONFIG_NF_FLOW_OFFLOAD)+= nf_flow_offload.o
+
 # nf_tables
 nf_tables-objs := nf_tables_core.o nf_tables_api.o nf_tables_trace.o \
 		  nft_immediate.o nft_cmp.o nft_range.o nft_bitwise.o \
net/netfilter/nf_flow_offload.c new file +386 -0

386 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#5 lore raw

RFC PATCH 3/5 netfilter: nf_flow_offload: integration with conntrack

In reply to Pablo Neira Ayuso

This patch adds the IPS_OFFLOAD status bit, this new bit tells us that the conntrack entry is owned by the flow offload infrastructure. The timer of such conntrack entries is stopped - the conntrack garbage collector skips them - and they display no internal state in the case of TCP flows.

 # cat /proc/net/nf_conntrack
 ipv4     2 tcp      6 src=10.141.10.2 dst=147.75.205.195 sport=36392 dport=443 src=147.75.205.195 dst=192.168.2.195 sport=443 dport=36392 [OFFLOAD] mark=0 zone=0 use=2

Note the [OFFLOAD] tag in the listing.

Conntrack entries that have been offloaded to the flow table infrastructure cannot be deleted/flushed via ctnetlink. The flow table infrastructure is also responsible for releasing this conntrack entry.

---
Instead of nf_flow_release_ct(), I'd rather keep a pointer reference to the conntrack object from the flow_offload entry, so we can skip the conntrack look up.

 include/net/netfilter/nf_conntrack.h               |  3 +-
 include/uapi/linux/netfilter/nf_conntrack_common.h |  4 +++
 net/netfilter/nf_conntrack_core.c                  |  7 ++++-
 net/netfilter/nf_conntrack_netlink.c               | 15 ++++++++-
 net/netfilter/nf_conntrack_proto_tcp.c             |  3 ++
 net/netfilter/nf_conntrack_standalone.c            | 12 +++++---
 net/netfilter/nf_flow_offload.c                    | 36 ++++++++++++++++++++--
 7 files changed, 71 insertions(+), 9 deletions(-)
include/net/netfilter/nf_conntrack.h +2 -1
index 8f3bd30511de..9af4bb0c2f46 100644
@@ -272,7 +272,8 @@ static inline unsigned long nf_ct_expires(const struct nf_conn *ct)
 
 static inline bool nf_ct_is_expired(const struct nf_conn *ct)
 {
-	return (__s32)(ct->timeout - nfct_time_stamp) <= 0;
+	return (__s32)(ct->timeout - nfct_time_stamp) <= 0 &&
+	       !test_bit(IPS_OFFLOAD_BIT, &ct->status);
 }
 
 /* use after obtaining a reference count */
include/uapi/linux/netfilter/nf_conntrack_common.h +4 -0
index dc947e59d03a..6b463b88182d 100644
@@ -100,6 +100,10 @@ enum ip_conntrack_status {
 	IPS_HELPER_BIT = 13,
 	IPS_HELPER = (1 << IPS_HELPER_BIT),
 
+	/* Conntrack has been offloaded to flow table. */
+	IPS_OFFLOAD_BIT = 14,
+	IPS_OFFLOAD = (1 << IPS_OFFLOAD_BIT),
+
 	/* Be careful here, modifying these bits can make things messy,
 	 * so don't let users modify them directly.
 	 */
net/netfilter/nf_conntrack_core.c +6 -1
index 01130392b7c0..48f36c4fb756 100644
@@ -901,6 +901,9 @@ static unsigned int early_drop_list(struct net *net,
 	hlist_nulls_for_each_entry_rcu(h, n, head, hnnode) {
 		tmp = nf_ct_tuplehash_to_ctrack(h);
 
+		if (test_bit(IPS_OFFLOAD_BIT, &tmp->status))
+			continue;
+
 		if (nf_ct_is_expired(tmp)) {
 			nf_ct_gc_expired(tmp);
 			continue;
@@ -1011,12 +1014,14 @@ static void gc_worker(struct work_struct *work)
 			tmp = nf_ct_tuplehash_to_ctrack(h);
 
 			scanned++;
+			if (test_bit(IPS_OFFLOAD_BIT, &tmp->status))
+				continue;
+
 			if (nf_ct_is_expired(tmp)) {
 				nf_ct_gc_expired(tmp);
 				expired_count++;
 				continue;
 			}
-
 			if (nf_conntrack_max95 == 0 || gc_worker_skip_ct(tmp))
 				continue;
 
net/netfilter/nf_conntrack_netlink.c +14 -1
index de4053d84364..79a74aec7c1e 100644
@@ -1105,6 +1105,14 @@ static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
 				    .len = NF_CT_LABELS_MAX_SIZE },
 };
 
+static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
+{
+	if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
+		return 0;
+
+	return ctnetlink_filter_match(ct, data);
+}
+
 static int ctnetlink_flush_conntrack(struct net *net,
 				     const struct nlattr * const cda[],
 				     u32 portid, int report)
@@ -1117,7 +1125,7 @@ static int ctnetlink_flush_conntrack(struct net *net,
 			return PTR_ERR(filter);
 	}
 
-	nf_ct_iterate_cleanup_net(net, ctnetlink_filter_match, filter,
+	nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
 				  portid, report);
 	kfree(filter);
 
@@ -1163,6 +1171,11 @@ static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
 
 	ct = nf_ct_tuplehash_to_ctrack(h);
 
+	if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) {
+		nf_ct_put(ct);
+		return -EBUSY;
+	}
+
 	if (cda[CTA_ID]) {
 		u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
 		if (id != (u32)(unsigned long)ct) {
net/netfilter/nf_conntrack_proto_tcp.c +3 -0
index cba1c6ffe51a..156f529d1668 100644
@@ -305,6 +305,9 @@ static bool tcp_invert_tuple(struct nf_conntrack_tuple *tuple,
 /* Print out the private part of the conntrack. */
 static void tcp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
 {
+	if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
+		return;
+
 	seq_printf(s, "%s ", tcp_conntrack_names[ct->proto.tcp.state]);
 }
 #endif
net/netfilter/nf_conntrack_standalone.c +8 -4
index 5a101caa3e12..46d32baad095 100644
@@ -309,10 +309,12 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	WARN_ON(!l4proto);
 
 	ret = -ENOSPC;
-	seq_printf(s, "%-8s %u %-8s %u %ld ",
+	seq_printf(s, "%-8s %u %-8s %u ",
 		   l3proto_name(l3proto->l3proto), nf_ct_l3num(ct),
-		   l4proto_name(l4proto->l4proto), nf_ct_protonum(ct),
-		   nf_ct_expires(ct)  / HZ);
+		   l4proto_name(l4proto->l4proto), nf_ct_protonum(ct));
+
+	if (!test_bit(IPS_OFFLOAD_BIT, &ct->status))
+		seq_printf(s, "%ld ", nf_ct_expires(ct)  / HZ);
 
 	if (l4proto->print_conntrack)
 		l4proto->print_conntrack(s, ct);
@@ -339,7 +341,9 @@ static int ct_seq_show(struct seq_file *s, void *v)
 	if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
 		goto release;
 
-	if (test_bit(IPS_ASSURED_BIT, &ct->status))
+	if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
+		seq_puts(s, "[OFFLOAD] ");
+	else if (test_bit(IPS_ASSURED_BIT, &ct->status))
 		seq_puts(s, "[ASSURED] ");
 
 	if (seq_has_overflowed(s))
net/netfilter/nf_flow_offload.c +34 -2
index c967b29d11a6..f4a3fbe11b69 100644
@@ -13,6 +13,9 @@
 #include <linux/udp.h>
 #include <linux/icmpv6.h>
 
+#include <net/netfilter/nf_conntrack_core.h>
+#include <net/netfilter/nf_conntrack_tuple.h>
+
 static struct rhashtable flow_table;
 
 static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
@@ -91,6 +94,34 @@ static inline bool nf_flow_has_expired(const struct flow_offload *flow)
 	return (__s32)(flow->timeout - (u32)jiffies) <= 0;
 }
 
+static void nf_flow_release_ct(const struct flow_offload_tuple_rhash *th)
+{
+	struct nf_conntrack_tuple tuple = {};
+	struct nf_conntrack_tuple_hash *h;
+	struct nf_conntrack_zone zone;
+	struct nf_conn *ct;
+
+	nf_ct_zone_init(&zone, NF_CT_DEFAULT_ZONE_ID,
+			NF_CT_DEFAULT_ZONE_DIR, 0);
+
+	tuple.src.u3.ip		= th->tuple.src_v4.s_addr;
+	tuple.dst.u3.ip		= th->tuple.dst_v4.s_addr;
+	tuple.src.u.all		= th->tuple.src_port;
+	tuple.dst.u.all		= th->tuple.dst_port;
+	tuple.src.l3num		= th->tuple.l3proto;
+	tuple.dst.protonum	= th->tuple.l4proto;
+	tuple.dst.dir		= IP_CT_DIR_ORIGINAL;
+
+	h = nf_conntrack_find_get(&init_net, &zone, &tuple);
+	if (!h) {
+		pr_err("cannot find conntrack for flow hash %p\n", th);
+		return;
+	}
+	ct = nf_ct_tuplehash_to_ctrack(h);
+	nf_ct_delete(ct, 0, 0);
+	nf_ct_put(ct);
+}
+
 static void nf_flow_offload_work_gc(struct work_struct *work)
 {
 	struct flow_offload_tuple_rhash *tuplehash;
@@ -116,9 +147,10 @@ static void nf_flow_offload_work_gc(struct work_struct *work)
 
 		flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
 
-		if (nf_flow_has_expired(flow))
+		if (nf_flow_has_expired(flow)) {
 			flow_offload_del(flow);
-
+			nf_flow_release_ct(tuplehash);
+		}
 		counter++;
 	}
 
Signature · 1 line
2.11.0
author maintainer
#7 lore raw

RFC PATCH 4/5 netfilter: nf_tables: flow offload expression

In reply to Pablo Neira Ayuso

Add new instruction for the nf_tables VM that allows us to specify what flows are offloaded. This has an explicit dependency with the conntrack subsystem.

---
 include/uapi/linux/netfilter/nf_tables.h |   9 +
 net/netfilter/Kconfig                    |   7 +
 net/netfilter/Makefile                   |   1 +
 net/netfilter/nft_flow_offload.c         | 331 +++++++++++++++++++++++++++++++
 4 files changed, 348 insertions(+)
 create mode 100644 net/netfilter/nft_flow_offload.c
include/uapi/linux/netfilter/nf_tables.h +9 -0
index 871afa4871bf..2edde548de68 100644
@@ -948,6 +948,15 @@ enum nft_ct_attributes {
 };
 #define NFTA_CT_MAX		(__NFTA_CT_MAX - 1)
 
+/**
+ * enum nft_ct_offload_attributes - ct offload expression attributes
+ */
+enum nft_offload_attributes {
+	NFTA_CT_OFFLOAD_UNSPEC,
+	__NFTA_CT_OFFLOAD_MAX,
+};
+#define NFTA_CT_OFFLOAD_MAX	(__NFTA_CT_OFFLOAD_MAX - 1)
+
 enum nft_limit_type {
 	NFT_LIMIT_PKTS,
 	NFT_LIMIT_PKT_BYTES
net/netfilter/Kconfig +7 -0
index f022ca91f49d..0a5c33cfaeb8 100644
@@ -504,6 +504,13 @@ config NFT_CT
 	  This option adds the "ct" expression that you can use to match
 	  connection tracking information such as the flow state.
 
+config NFT_FLOW_OFFLOAD
+	depends on NF_CONNTRACK
+	tristate "Netfilter nf_tables hardware flow offload module"
+	help
+	  This option adds the "flow_offload" expression that you can use to
+	  choose what flows are placed into the hardware.
+
 config NFT_SET_RBTREE
 	tristate "Netfilter nf_tables rbtree set module"
 	help
net/netfilter/Makefile +1 -0
index 518f54113e06..801ce5c25e5d 100644
@@ -86,6 +86,7 @@ obj-$(CONFIG_NFT_META)		+= nft_meta.o
 obj-$(CONFIG_NFT_RT)		+= nft_rt.o
 obj-$(CONFIG_NFT_NUMGEN)	+= nft_numgen.o
 obj-$(CONFIG_NFT_CT)		+= nft_ct.o
+obj-$(CONFIG_NFT_FLOW_OFFLOAD)	+= nft_flow_offload.o
 obj-$(CONFIG_NFT_LIMIT)		+= nft_limit.o
 obj-$(CONFIG_NFT_NAT)		+= nft_nat.o
 obj-$(CONFIG_NFT_OBJREF)	+= nft_objref.o
net/netfilter/nft_flow_offload.c new file +331 -0

331 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#9 lore raw

RFC PATCH 5/5 netfilter: nft_flow_offload: add ndo hooks for hardware offload

In reply to Pablo Neira Ayuso

This patch adds the infrastructure to offload flows to hardware, in case the nic/switch comes with built-in flow tables capabilities.

If the hardware comes with not hardware flow tables or they have limitations in terms of features, this falls back to the software generic flow table implementation.

The software flow table aging thread skips entries that resides in the hardware, so the hardware will be responsible for releasing this flow table entry too.

---
 include/linux/netdevice.h        |  4 ++
 net/netfilter/nf_flow_offload.c  |  3 ++
 net/netfilter/nft_flow_offload.c | 99 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+)
include/linux/netdevice.h +4 -0
index f535779d9dc1..0787f53374b3 100644
@@ -826,6 +826,8 @@ struct xfrmdev_ops {
 };
 #endif
 
+struct flow_offload;
+
 /*
  * This structure defines the management hooks for network devices.
  * The following hooks can be defined; unless noted otherwise, they are
@@ -1281,6 +1283,8 @@ struct net_device_ops {
 	int			(*ndo_bridge_dellink)(struct net_device *dev,
 						      struct nlmsghdr *nlh,
 						      u16 flags);
+	int			(*ndo_flow_add)(struct flow_offload *flow);
+	int			(*ndo_flow_del)(struct flow_offload *flow);
 	int			(*ndo_change_carrier)(struct net_device *dev,
 						      bool new_carrier);
 	int			(*ndo_get_phys_port_id)(struct net_device *dev,
net/netfilter/nf_flow_offload.c +3 -0
index f4a3fbe11b69..ac5786976dbb 100644
@@ -147,6 +147,9 @@ static void nf_flow_offload_work_gc(struct work_struct *work)
 
 		flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
 
+		if (flow->flags & FLOW_OFFLOAD_HW)
+			continue;
+
 		if (nf_flow_has_expired(flow)) {
 			flow_offload_del(flow);
 			nf_flow_release_ct(tuplehash);
net/netfilter/nft_flow_offload.c +99 -0

149 lines not on this page · raw message

Signature · 1 line
2.11.0
maintainer
#6 lore raw

Re: [PATCH RFC,WIP 3/5] netfilter: nf_flow_offload: integration with conntrack

In reply to Pablo Neira Ayuso
Quoted 10 lines · Pablo Neira Ayuso

This patch adds the IPS_OFFLOAD status bit, this new bit tells us that the conntrack entry is owned by the flow offload infrastructure. The timer of such conntrack entries is stopped - the conntrack garbage collector skips them - and they display no internal state in the case of TCP flows.

Conntrack entries that have been offloaded to the flow table infrastructure cannot be deleted/flushed via ctnetlink. The flow table infrastructure is also responsible for releasing this conntrack entry.

Quoted 5 lines

---
Instead of nf_flow_release_ct(), I'd rather keep a pointer reference to the conntrack object from the flow_offload entry, so we can skip the conntrack look up.

I agree, this would make sense.

include/net/netfilter/nf_conntrack.h static inline unsigned long nf_ct_expires(const struct nf_conn *ct) · line 276

Quoted 11 lines
include/net/netfilter/nf_conntrack.h +2 -1
index 8f3bd30511de..9af4bb0c2f46 100644
@@ -272,7 +272,8 @@ static inline unsigned long nf_ct_expires(const struct nf_conn *ct)
 
 static inline bool nf_ct_is_expired(const struct nf_conn *ct)
 {
-	return (__s32)(ct->timeout - nfct_time_stamp) <= 0;
+	return (__s32)(ct->timeout - nfct_time_stamp) <= 0 &&
+	       !test_bit(IPS_OFFLOAD_BIT, &ct->status);

An alternative would be to not touch nf_ct_is_expired() and instead ...

include/net/netfilter/nf_conntrack.h static void gc_worker(struct work_struct *work) · line 1018

Quoted 8 lines
include/net/netfilter/nf_conntrack.h +2 -0
 }
 
@@ -1011,12 +1014,14 @@ static void gc_worker(struct work_struct *work)
 			tmp = nf_ct_tuplehash_to_ctrack(h);
 
 			scanned++;
+			if (test_bit(IPS_OFFLOAD_BIT, &tmp->status))
+				continue;

... advance/refresh ct->timeout from gc worker, i.e.

 if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) {
     ct->timeout = nfct_time_stamp + (1 DAY);
     continue;

}

Would prevent normal path to ever see offloaded entry
as 'timed out', without having to check for the flag in lookup path (OTOH the check should not be an issue either because lookup path
has to access ct->status anyway).

maintainer
#4 lore raw

Re: [PATCH RFC,WIP 2/5] netfilter: add software flow offload infrastructure

In reply to Pablo Neira Ayuso

net/netfilter/nf_flow_offload.c line 327

Quoted 8 lines · Pablo Neira Ayuso
net/netfilter/nf_flow_offload.c new file +8 -0
+static int __init nf_flow_offload_module_init(void)
+{
+	struct rhashtable_params params = flow_offload_rhash_params;
+	struct nf_hook_ops flow_offload_hook = {
+		.hook		= nf_flow_offload_hook,
+		.pf		= NFPROTO_NETDEV,
+		.hooknum	= NF_NETDEV_INGRESS,
+		.priority	= -100,

Magic number. Should this be documented in nft?

Alternatively we could reject NETDEV_INGRESS base chains from userspace if prio < 0 to prevent userspace rules from messing with this flow offlaod infrastructure.

I guess the rationale of using auto-builtin hook is to avoid forcing users to configure this with nftables rules?

net/netfilter/nf_flow_offload.c line 343

Quoted 6 lines
net/netfilter/nf_flow_offload.c new file +6 -0
+	rtnl_lock();
+	for_each_netdev(&init_net, dev) {
+		entry = kmalloc(sizeof(*entry), GFP_KERNEL);
+		if (!entry) {
+			rtnl_unlock();
+			return -ENOMEM;

This would need error unwinding (Unregistering the already-registered hooks).

net/netfilter/nf_flow_offload.c line 351

Quoted 3 lines
net/netfilter/nf_flow_offload.c new file +3 -0
+		err = nf_register_net_hook(&init_net, &entry->ops);
+		if (err < 0)
+			return err;

And here as well.

maintainer
#10 lore raw

Re: [PATCH RFC,WIP 5/5] netfilter: nft_flow_offload: add ndo hooks for hardware offload

In reply to Pablo Neira Ayuso

net/netfilter/nft_flow_offload.c flow_offload_alloc(const struct nf_conn *ct, int iifindex, int oifindex, · line 228

Quoted 7 lines · Pablo Neira Ayuso
net/netfilter/nft_flow_offload.c +7 -0
+static void flow_offload_work(struct work_struct *work)
+{
+	struct flow_hw_offload *offload, *next;
+
+	spin_lock_bh(&flow_hw_offload_lock);
+	list_for_each_entry_safe(offload, next, &flow_hw_offload_pending_list, list) {
+		do_flow_offload(offload->flow);

This should not offload flows that already have DYING bit set.

net/netfilter/nft_flow_offload.c flow_offload_alloc(const struct nf_conn *ct, int iifindex, int oifindex, · line 236

Quoted 8 lines
net/netfilter/nft_flow_offload.c +8 -0
+		nf_conntrack_put(&offload->ct->ct_general);
+		list_del(&offload->list);
+		kfree(offload);
+	}
+	spin_unlock_bh(&flow_hw_offload_lock);
+
+	queue_delayed_work(system_power_efficient_wq, &nft_flow_offload_dwork, HZ);
+}

Missed this on first round, 1 second is quite large.

[..]

Quoted 21 lines

static int nft_flow_route(const struct nft_pktinfo *pkt,

net/netfilter/nft_flow_offload.c +7 -0
 			  const struct nf_conn *ct,
 			  union flow_gateway *orig_gw,
@@ -211,6 +290,7 @@ static void nft_flow_offload_eval(const struct nft_expr *expr,
 	union flow_gateway orig_gateway, reply_gateway;
 	struct net_device *outdev = pkt->xt.state->out;
 	struct net_device *indev = pkt->xt.state->in;
+	struct flow_hw_offload *offload;
 	enum ip_conntrack_info ctinfo;
 	struct flow_offload *flow;
 	struct nf_conn *ct;
@@ -250,6 +330,21 @@ static void nft_flow_offload_eval(const struct nft_expr *expr,
 	if (ret < 0)
 		goto err2;
 
+	if (!indev->netdev_ops->ndo_flow_add)
+		return;
+
+	offload = kmalloc(sizeof(struct flow_hw_offload), GFP_ATOMIC);
+	if (!offload)
+		return;

net/netfilter/nft_flow_offload.c static void nft_flow_offload_eval(const struct nft_expr *expr, · line 348

Quoted 10 lines
net/netfilter/nft_flow_offload.c +9 -0
+
+	nf_conntrack_get(&ct->ct_general);
+	offload->ct = ct;
+	offload->flow = flow;
+
+	spin_lock_bh(&flow_hw_offload_lock);
+	list_add_tail(&offload->list, &flow_hw_offload_pending_list);
+	spin_unlock_bh(&flow_hw_offload_lock);
+
 	return;

So this aims for lazy offloading (up to 1 second delay). Is this intentional, e.g. to avoid offloading short-lived 'RR' flows?

I would have expected this to schedule the workqueue here, and not use delayed wq at all (i.e., also no self-rescheduling from flow_offload_work()).

maintainer
#8 lore raw

Re: [PATCH RFC,WIP 4/5] netfilter: nf_tables: flow offload expression

In reply to Pablo Neira Ayuso
Quoted 4 lines · Pablo Neira Ayuso
net/netfilter/nft_flow_offload.c new file +4 -0
+static void nft_flow_offload_eval(const struct nft_expr *expr,
+				  struct nft_regs *regs,
+				  const struct nft_pktinfo *pkt)
+{

[..]

net/netfilter/nft_flow_offload.c line 236

Quoted 6 lines
net/netfilter/nft_flow_offload.c new file +6 -0
+	if (test_bit(IPS_HELPER_BIT, &ct->status))
+		goto out;
+
+	if (ctinfo == IP_CT_NEW ||
+	    ctinfo == IP_CT_RELATED)
+		goto out;

Would it make sense to delay offload decision until l4 tracker has set ASSURED bit?

#12 lore raw
In reply to Pablo Neira Ayuso

Hi Pablo,

Quoted 68 lines · Pablo Neira Ayuso

Hi,

This patch adds the flow offload infrastructure for Netfilter. This adds a new 'nf_flow_offload' module that registers a hook at ingress. Every packet that hits the flow table is forwarded to where the flow table entry specifies in terms of destination/gateway and netdevice. In case of flow table miss, the packet follows the classic forward path.

This flow table is populated via the new nftables VM action 'flow_offload', so the user can selectively specify what flows are placed into the flow table, an example ruleset would look like this:

        table inet x {
                chain y {
                        type filter hook forward priority 0; policy accept;
                        ip protocol tcp flow offload counter
                        counter
                }
        }

The 'flow offload' action adds the flow entry once the flow is in established state, according to the connection tracking definition, ie. we have seen traffic in both directions. Therefore, only initial packets of the flow follow the classic forwarding path.

* Patch 1/5 is nothing really interesting, just a little preparation change.

* Patch 2/5 adds a software flow table representation. It uses the rhashtable and an API to operate with it, it also introduces the 'struct flow_offload' that represents a flow table entry. There's a garbage collector kernel thread that cleans up entries for which we have not seen any packet for a while.

* Patch 3/5 Just adds the missing bits to integrate the software flow table with conntrack. The software flow table owns the conntrack object, so it is basically responsible for releasing it. Conntrack entries that have been offloaded in the conntrack table will look like this:

ipv4 2 tcp 6 src=10.141.10.2 dst=147.75.205.195 sport=36392 dport=443 src=147.75.205.195 dst=192.168.2.195 sport=443 dport=36392 [OFFLOAD] use=2

* Patch 4/5 adds the extension for nf_tables that can be used to select what flows are offloaded through policy.

* Patch 5/5 Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while. This patch adds the ndo hooks to populate hardware flow table. This patchs a workqueue to configure from user context - we need to hold the mdio mutex for this. There will be a little time until packets will follow the hardware path. So packets will be following the software flow table path for a little while until the start going through hardware.

I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed.

TODO, still many things:

* Only IPv4 at this time.
* Only IPv4 SNAT is supported.
* No netns support yet.
* Missing netlink interface to operate with the flow table, to force the handover of flow to the software path.
* Higher configurability, instead of registering the flow table inconditionally, add an interface to specify software flow table properties.
* No flow counters at this time.

Quoted 6 lines

This should serve a number of usecases where we can rely on this kernel bypass. Packets that need fragmentation / PMTU / IP option handling / ... and any specific handling, then we should pass them up to the forwarding classic path.

Comments welcome,

A lot of us have been waiting for this for some time, so thanks a lot for posting the patches. At first glance this seems to cover most of the HW that I know about out there and it does so without that much code added which is great. Did you have a particular platform you did experiment this with and if so, should we expect patches to be posted to see how it integrates with real hardware?

Thanks!

Quoted 29 lines

Thanks.

Pablo Neira Ayuso (5):
netfilter: nf_conntrack: move nf_ct_netns_{get,put}() to core
netfilter: add software flow offload infrastructure
netfilter: nf_flow_offload: integration with conntrack
netfilter: nf_tables: flow offload expression
netfilter: nft_flow_offload: add ndo hooks for hardware offload

 include/linux/netdevice.h                          |   4 +
 include/net/flow_offload.h                         |  67 ++++
 include/net/netfilter/nf_conntrack.h               |   3 +-
 include/uapi/linux/netfilter/nf_conntrack_common.h |   4 +
 include/uapi/linux/netfilter/nf_tables.h           |   9 +
 net/netfilter/Kconfig                              |  14 +
 net/netfilter/Makefile                             |   4 +
 net/netfilter/nf_conntrack_core.c                  |   7 +-
 net/netfilter/nf_conntrack_netlink.c               |  15 +-
 net/netfilter/nf_conntrack_proto.c                 |  37 +-
 net/netfilter/nf_conntrack_proto_tcp.c             |   3 +
 net/netfilter/nf_conntrack_standalone.c            |  12 +-
 net/netfilter/nf_flow_offload.c                    | 421 ++++++++++++++++++++
 net/netfilter/nft_ct.c                             |  39 +-
 net/netfilter/nft_flow_offload.c                   | 430 +++++++++++++++++++++
 15 files changed, 1024 insertions(+), 45 deletions(-)
 create mode 100644 include/net/flow_offload.h
 create mode 100644 net/netfilter/nf_flow_offload.c
 create mode 100644 net/netfilter/nft_flow_offload.c
Signature · 1 line
Florian
#11 lore raw

Re: [PATCH RFC,WIP 5/5] netfilter: nft_flow_offload: add ndo hooks for hardware offload

In reply to Pablo Neira Ayuso
Quoted 7 lines · Pablo Neira Ayuso

This patch adds the infrastructure to offload flows to hardware, in case the nic/switch comes with built-in flow tables capabilities.

If the hardware comes with not hardware flow tables or they have limitations in terms of features, this falls back to the software generic flow table implementation.

Quoted 5 lines

The software flow table aging thread skips entries that resides in the hardware, so the hardware will be responsible for releasing this flow table entry too.

Hi Pablo,

I'd like to start playing with those patches in OpenWrt/LEDE soon. I'm also considering making a patch that adds iptables support. For that to work, I think it would be a good idea to keep the code that tries to offload flows to hardware in nf_flow_offload.c instead, so that it can be shared with iptables integration.

By the way, do you have a git tree where you keep the current version of your patch set?

Thanks,

- Felix

#13 lore raw
In reply to Pablo Neira Ayuso
Quoted 15 lines · Pablo Neira Ayuso

I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed.

TODO, still many things:

* Only IPv4 at this time.
* Only IPv4 SNAT is supported.
* No netns support yet.
* Missing netlink interface to operate with the flow table, to force the handover of flow to the software path.
* Higher configurability, instead of registering the flow table inconditionally, add an interface to specify software flow table properties.
* No flow counters at this time.

Quoted 4 lines

This should serve a number of usecases where we can rely on this kernel bypass. Packets that need fragmentation / PMTU / IP option handling / ... and any specific handling, then we should pass them up to the forwarding classic path.

I didn't realize it from this patch set, but it was mentioned at the conference that this patch set is completely stateless. I.e. things like TCP window tracking are not included here. IMHO that's a big concern, because offloading flows is trivial when compared to state sync. IMHO state sync is *the* challenge in implementing connection tacking offload...