Skip to contentSkip to conversation

netfilter: conntrack: cache route for forwarded connections

RFC PATCH In review Florian Westphal 3 participants 5 messages Dec 2, 2014

Subsystems (3)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

RFC PATCH 1/1 netfilter: conntrack: cache route for forwarded connections

... to avoid per-packet FIB lookup if possible.

The cached dst is re-used provided the input interface is the same as that of the previous packet in the same direction.

If not, the cached dst is invalidated.

This should speed up forwarding when conntrack is already in use anyway, especially when using reverse path filtering -- active RPF enforces two FIB lookups for each packet.

Before the routing cache removal this didn't matter since RPF was performed only when route cache didn't yield a result; but without route cache it comes at high price.

---
Sending as RFC since I haven't tested this yet (aside from single-forwarded-flow), so no performance data either.

- doesn't work when iif changes (it invalidates cached dst), don't think its a problem
- auto-active when module is loaded (and always active when =y).
- cache becomes used after conntrack enters ASSURED state.

TODOs:
- integrate with netfilter ipv4/ipv6 rpfilter match (already working on this)

Things to consider:
- perhaps extend it for local connections too; could cache socket to get sk from conntrack (and then fetchg dst from sk).

 include/net/netfilter/nf_conntrack_extend.h  |   4 +
 include/net/netfilter/nf_conntrack_rtcache.h |  28 +++
 net/netfilter/Kconfig                        |  11 +
 net/netfilter/Makefile                       |   1 +
 net/netfilter/nf_conntrack_rtcache.c         | 327 +++++++++++++++++++++++++++
 5 files changed, 371 insertions(+)
 create mode 100644 include/net/netfilter/nf_conntrack_rtcache.h
 create mode 100644 net/netfilter/nf_conntrack_rtcache.c
include/net/netfilter/nf_conntrack_extend.h +4 -0
index 55d1504..1b00d57 100644
@@ -30,6 +30,9 @@ enum nf_ct_ext_id {
 #if IS_ENABLED(CONFIG_NETFILTER_SYNPROXY)
 	NF_CT_EXT_SYNPROXY,
 #endif
+#if IS_ENABLED(CONFIG_NF_CONNTRACK_RTCACHE)
+	NF_CT_EXT_RTCACHE,
+#endif
 	NF_CT_EXT_NUM,
 };
 
@@ -43,6 +46,7 @@ enum nf_ct_ext_id {
 #define NF_CT_EXT_TIMEOUT_TYPE struct nf_conn_timeout
 #define NF_CT_EXT_LABELS_TYPE struct nf_conn_labels
 #define NF_CT_EXT_SYNPROXY_TYPE struct nf_conn_synproxy
+#define NF_CT_EXT_RTCACHE_TYPE struct nf_conn_rtcache
 
 /* Extensions: optional stuff which isn't permanently in struct. */
 struct nf_ct_ext {
include/net/netfilter/nf_conntrack_rtcache.h new file +28 -0
new file mode 100644
index 0000000..e8d215d
@@ -0,0 +1,28 @@
+#include <linux/gfp.h>
+#include <net/netfilter/nf_conntrack.h>
+#include <net/netfilter/nf_conntrack_extend.h>
+
+struct dst_entry;
+union nf_conn_cache_ptr {
+	struct dst_entry *dst;
+};
+
+struct nf_conn_rtcache {
+	union nf_conn_cache_ptr ptr[IP_CT_DIR_MAX];
+	int iif[IP_CT_DIR_MAX];
+};
+
+static inline struct nf_conn_rtcache *nf_ct_rtcache_find(const struct nf_conn *ct)
+{
+#if IS_ENABLED(CONFIG_NF_CONNTRACK_RTCACHE)
+	return nf_ct_ext_find(ct, NF_CT_EXT_RTCACHE);
+#else
+	return NULL;
+#endif
+}
+
+static inline int nf_conn_rtcache_iif_get(const struct nf_conn_rtcache *rtc,
+					  enum ip_conntrack_dir dir)
+{
+	return rtc->iif[dir];
+}
net/netfilter/Kconfig +11 -0
index b02660f..4a8b7e7 100644
@@ -106,6 +106,17 @@ config NF_CONNTRACK_EVENTS
 
 	  If unsure, say `N'.
 
+config NF_CONNTRACK_RTCACHE
+	tristate "Cache route entries in conntrack objects"
+	depends on NETFILTER_ADVANCED
+	depends on NF_CONNTRACK
+	help
+	  If this option is enabled, the connection tracking code will
+	  cache routing information for each connection that is being
+	  forwarded.
+
+	  If unsure, say `N'.
+
 config NF_CONNTRACK_TIMEOUT
 	bool  'Connection tracking timeout'
 	depends on NETFILTER_ADVANCED
net/netfilter/Makefile +1 -0
index 89f73a9..ac830c9 100644
@@ -5,6 +5,7 @@ nf_conntrack-$(CONFIG_NF_CONNTRACK_TIMEOUT) += nf_conntrack_timeout.o
 nf_conntrack-$(CONFIG_NF_CONNTRACK_TIMESTAMP) += nf_conntrack_timestamp.o
 nf_conntrack-$(CONFIG_NF_CONNTRACK_EVENTS) += nf_conntrack_ecache.o
 nf_conntrack-$(CONFIG_NF_CONNTRACK_LABELS) += nf_conntrack_labels.o
+nf_conntrack-$(CONFIG_NF_CONNTRACK_RTCACHE) += nf_conntrack_rtcache.o
 
 obj-$(CONFIG_NETFILTER) = netfilter.o
 
net/netfilter/nf_conntrack_rtcache.c new file +327 -0

327 lines not on this page · raw message

Signature · 1 line
2.0.4
#1 lore raw
Quoted 11 lines · Florian Westphal

... to avoid per-packet FIB lookup if possible.

The cached dst is re-used provided the input interface is the same as that of the previous packet in the same direction.

If not, the cached dst is invalidated.

This should speed up forwarding when conntrack is already in use anyway, especially when using reverse path filtering -- active RPF enforces two FIB lookups for each packet.

Quoted 6 lines

Before the routing cache removal this didn't matter since RPF was performed only when route cache didn't yield a result; but without route cache it comes at high price.

---

Seems a good idea (but you might need some IPv6 care, as ( dst = dst_check(dst, 0); ) seems to handle IPv4 only)

Another idea would be to re-use TCP ehash so that regular IP early demux can be used, with a single lookup for both local and forwarded sessions.

(That would probably require a bit more memory, as you would need to insert into TCP ehash some kind of 'tiny sockets' )

#3 lore raw
In reply to Florian Westphal

Hello,

Quoted 15 lines · Florian Westphal

... to avoid per-packet FIB lookup if possible.

The cached dst is re-used provided the input interface is the same as that of the previous packet in the same direction.

If not, the cached dst is invalidated.

This should speed up forwarding when conntrack is already in use anyway, especially when using reverse path filtering -- active RPF enforces two FIB lookups for each packet.

Before the routing cache removal this didn't matter since RPF was performed only when route cache didn't yield a result; but without route cache it comes at high price.

Quoted 7 lines

---
Sending as RFC since I haven't tested this yet (aside from single-forwarded-flow), so no performance data either.

- doesn't work when iif changes (it invalidates cached dst), don't think its a problem

The idea is good. But code that caches dsts should
also handle at least NETDEV_UNREGISTER (NETDEV_DOWN being another option) to release dsts. Holding dsts for frozen conns in EST state for long time is a problem. IIRC, such dsts are not under dst_dev_event() control. nf_nat_masquerade_ipv4.c has something like this but for masq_index.

Regards

--
Julian Anastasov <[email protected]>

author maintainer
#2 lore raw
In reply to Eric Dumazet
Quoted 16 lines · Eric Dumazet
Quoted 15 lines · Florian Westphal

... to avoid per-packet FIB lookup if possible.

The cached dst is re-used provided the input interface is the same as that of the previous packet in the same direction.

If not, the cached dst is invalidated.

This should speed up forwarding when conntrack is already in use anyway, especially when using reverse path filtering -- active RPF enforces two FIB lookups for each packet.

Before the routing cache removal this didn't matter since RPF was performed only when route cache didn't yield a result; but without route cache it comes at high price.

Quoted 5 lines
Quoted 2 lines

---

Seems a good idea (but you might need some IPv6 care, as ( dst = dst_check(dst, 0); ) seems to handle IPv4 only)

As usual, you're right...

AFAICS its enough to stash fib sernum of the rt6info too and pass that as the cookie, phew :-)

Quoted 2 lines

Another idea would be to re-use TCP ehash so that regular IP early demux can be used, with a single lookup for both local and forwarded sessions.

Hmm, I'll look at this. Maybe...

Quoted 2 lines

(That would probably require a bit more memory, as you would need to insert into TCP ehash some kind of 'tiny sockets' )

... such tiny socket could be stored/tied to the conntrack extension area.

I think we need to be careful to not re-add the route cache (and the DoS issues associated with it).

Thanks Eric!

author maintainer
#4 lore raw
In reply to Julian Anastasov
Quoted 18 lines · Julian Anastasov
Quoted 18 lines

The cached dst is re-used provided the input interface is the same as that of the previous packet in the same direction.

If not, the cached dst is invalidated.

This should speed up forwarding when conntrack is already in use anyway, especially when using reverse path filtering -- active RPF enforces two FIB lookups for each packet.

Before the routing cache removal this didn't matter since RPF was performed only when route cache didn't yield a result; but without route cache it comes at high price.

---
Sending as RFC since I haven't tested this yet (aside from single-forwarded-flow), so no performance data either.

Quoted 7 lines
Quoted 2 lines

- doesn't work when iif changes (it invalidates cached dst), don't think its a problem

The idea is good. But code that caches dsts should
also handle at least NETDEV_UNREGISTER (NETDEV_DOWN being another option) to release dsts. Holding dsts for frozen conns in EST state for long time is a problem.

Okay, point taken. Thanks Julian.