Skip to contentSkip to conversation

netfilter: conntrack: remove timer from ecache extension

PATCH In review Florian Westphal 2 participants 5 messages Mar 27, 2014 – Mar 28, 2014

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: conntrack: remove timer from ecache extension

This brings the (per-conntrack) ecache extension back to 24 bytes in size (was 152 byte on x86_64 with lockdep on).

When event delivery fails, re-delivery is attempted via work queue. As long as the work queue has events to deliver, and at least one delivery succeeded, it is rescheduled without delay, if no pending event was delivered after 0.1 seconds to avoid hogging cpu.

As the dying list also contains entries that do not need event redelivery, a new status bit is added to identify these conntracks.

We cannot use !IPS_DYING_BIT, as entries whose event was already sent can be recycled at any time due to SLAB_DESTROY_BY_RCU.

When userspace is heavily backlogged/overloaded, redelivery attempts every 0.1 seconds are not enough. To avoid this, the ecache work is scheduled for immediate execution iff we have pending conntracks and a conntrack expired successfully (i.e., userspace consumed the event and is thus likely to accept more messages).

---
This is not replacement for 'u16 len' patch submitted recently because this is not stable material.

Adding new status bit is not nice, but only alternative is adding new 'ecache redelivery' list, which would mean we alter current lifecycle (unconfirmed list -> hash list -> dying list).

Would also need to add ability to dump new list via ctnetlink.

I'm mainly interested if you think timer removal is worthwile, it works well in practice from usability POV.

 include/net/netfilter/nf_conntrack.h               |  7 ++
 include/net/netfilter/nf_conntrack_ecache.h        | 26 +++++-
 include/net/netns/conntrack.h                      |  6 +-
 include/uapi/linux/netfilter/nf_conntrack_common.h |  8 +-
 net/netfilter/nf_conntrack_core.c                  | 83 ++++---------------
 net/netfilter/nf_conntrack_ecache.c                | 96 +++++++++++++++++++---
 6 files changed, 143 insertions(+), 83 deletions(-)
include/net/netfilter/nf_conntrack.h +7 -0
index 37252f7..1822d4a 100644
@@ -71,6 +71,13 @@ struct nf_conn_help {
 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
 
+/*
+ * We need to use special "null" values, not used in hash table
+ */
+#define NFCT_UNCONFIRMED_NULLS_VAL	((1<<30)+0)
+#define NFCT_DYING_NULLS_VAL		((1<<30)+1)
+#define NFCT_TEMPLATE_NULLS_VAL		((1<<30)+2)
+
 struct nf_conn {
 	/* Usage count in here is 1 for hash table/destruct timer, 1 per skb,
 	 * plus 1 for any connection(s) we are `master' for
include/net/netfilter/nf_conntrack_ecache.h +24 -2
index 0e3d08e..57c8803 100644
@@ -18,7 +18,6 @@ struct nf_conntrack_ecache {
 	u16 ctmask;		/* bitmask of ct events to be delivered */
 	u16 expmask;		/* bitmask of expect events to be delivered */
 	u32 portid;		/* netlink portid of destroyer */
-	struct timer_list timeout;
 };
 
 static inline struct nf_conntrack_ecache *
@@ -216,8 +215,23 @@ void nf_conntrack_ecache_pernet_fini(struct net *net);
 
 int nf_conntrack_ecache_init(void);
 void nf_conntrack_ecache_fini(void);
-#else /* CONFIG_NF_CONNTRACK_EVENTS */
 
+static inline void nf_conntrack_ecache_delayed_work(struct net *net)
+{
+	if (!delayed_work_pending(&net->ct.ecache_dwork)) {
+		schedule_delayed_work(&net->ct.ecache_dwork, HZ);
+		net->ct.ecache_dwork_pending = true;
+	}
+}
+
+static inline void nf_conntrack_ecache_work(struct net *net)
+{
+	if (net->ct.ecache_dwork_pending) {
+		net->ct.ecache_dwork_pending = false;
+		mod_delayed_work(system_wq, &net->ct.ecache_dwork, 0);
+	}
+}
+#else /* CONFIG_NF_CONNTRACK_EVENTS */
 static inline void nf_conntrack_event_cache(enum ip_conntrack_events event,
 					    struct nf_conn *ct) {}
 static inline int nf_conntrack_eventmask_report(unsigned int eventmask,
@@ -255,6 +269,14 @@ static inline int nf_conntrack_ecache_init(void)
 static inline void nf_conntrack_ecache_fini(void)
 {
 }
+
+static inline void nf_conntrack_ecache_delayed_work(struct net *net)
+{
+}
+
+static inline void nf_conntrack_ecache_work(struct net *net)
+{
+}
 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
 
 #endif /*_NF_CONNTRACK_ECACHE_H*/
include/net/netns/conntrack.h +5 -1
index 773cce3..29d6a94 100644
@@ -4,6 +4,7 @@
 #include <linux/list.h>
 #include <linux/list_nulls.h>
 #include <linux/atomic.h>
+#include <linux/workqueue.h>
 #include <linux/netfilter/nf_conntrack_tcp.h>
 #include <linux/seqlock.h>
 
@@ -73,6 +74,10 @@ struct ct_pcpu {
 struct netns_ct {
 	atomic_t		count;
 	unsigned int		expect_count;
+#ifdef CONFIG_NF_CONNTRACK_EVENTS
+	struct delayed_work ecache_dwork;
+	bool ecache_dwork_pending;
+#endif
 #ifdef CONFIG_SYSCTL
 	struct ctl_table_header	*sysctl_header;
 	struct ctl_table_header	*acct_sysctl_header;
@@ -82,7 +87,6 @@ struct netns_ct {
 #endif
 	char			*slabname;
 	unsigned int		sysctl_log_invalid; /* Log invalid packets */
-	unsigned int		sysctl_events_retry_timeout;
 	int			sysctl_events;
 	int			sysctl_acct;
 	int			sysctl_auto_assign_helper;
include/uapi/linux/netfilter/nf_conntrack_common.h +7 -1
index 319f471..f21618d 100644
@@ -72,7 +72,9 @@ enum ip_conntrack_status {
 	/* Both together */
 	IPS_NAT_DONE_MASK = (IPS_DST_NAT_DONE | IPS_SRC_NAT_DONE),
 
-	/* Connection is dying (removed from lists), can not be unset. */
+	/* Connection is dying (removed from hash) and netlink destroy
+	 * event was sent sucessfully.  Cannot be unset.
+	 */
 	IPS_DYING_BIT = 9,
 	IPS_DYING = (1 << IPS_DYING_BIT),
 
@@ -91,6 +93,10 @@ enum ip_conntrack_status {
 	/* Conntrack got a helper explicitly attached via CT target. */
 	IPS_HELPER_BIT = 13,
 	IPS_HELPER = (1 << IPS_HELPER_BIT),
+
+	/* Removed from hash, but destroy event must be re-sent */
+	IPS_ECACHE_REDELIVER_BIT = 14,
+	IPS_ECACHE_REDELIVER = (1 << IPS_ECACHE_REDELIVER_BIT),
 };
 
 /* Connection tracking event types */
net/netfilter/nf_conntrack_core.c +14 -69

124 lines not on this page · raw message

net/netfilter/nf_conntrack_ecache.c +86 -10

137 lines not on this page · raw message

Signature · 1 line
1.8.1.5
#1 lore raw
Quoted 25 lines · Florian Westphal

This brings the (per-conntrack) ecache extension back to 24 bytes in size (was 152 byte on x86_64 with lockdep on).

When event delivery fails, re-delivery is attempted via work queue. As long as the work queue has events to deliver, and at least one delivery succeeded, it is rescheduled without delay, if no pending event was delivered after 0.1 seconds to avoid hogging cpu.

As the dying list also contains entries that do not need event redelivery, a new status bit is added to identify these conntracks.

We cannot use !IPS_DYING_BIT, as entries whose event was already sent can be recycled at any time due to SLAB_DESTROY_BY_RCU.

When userspace is heavily backlogged/overloaded, redelivery attempts every 0.1 seconds are not enough. To avoid this, the ecache work is scheduled for immediate execution iff we have pending conntracks and a conntrack expired successfully (i.e., userspace consumed the event and is thus likely to accept more messages).

---
This is not replacement for 'u16 len' patch submitted recently because this is not stable material.

Quoted 8 lines

Adding new status bit is not nice, but only alternative is adding new 'ecache redelivery' list, which would mean we alter current lifecycle (unconfirmed list -> hash list -> dying list).

Would also need to add ability to dump new list via ctnetlink.

I'm mainly interested if you think timer removal is worthwile, it works well in practice from usability POV.

Thanks for looking again into this. We definitely have to get rid of that timer.

Regarding the new flag, perhaps we can avoid exposing this to userspace? I mean, we can define some mask of internal flags that we don't include via dump_status in ctnetlink.

author maintainer
#2 lore raw
Quoted 9 lines · Pablo Neira Ayuso
Quoted 2 lines

I'm mainly interested if you think timer removal is worthwile, it works well in practice from usability POV.

Thanks for looking again into this. We definitely have to get rid of that timer.

Regarding the new flag, perhaps we can avoid exposing this to userspace? I mean, we can define some mask of internal flags that we don't include via dump_status in ctnetlink.

What is your rationale for supressing this information? [ or, why is exposing this to userspace bad? ]

Is it so we don't have to keep dummy flag when we find a different solution later?

#3 lore raw
Quoted 7 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines

I'm mainly interested if you think timer removal is worthwile, it works well in practice from usability POV.

Thanks for looking again into this. We definitely have to get rid of that timer.

Quoted 6 lines
Quoted 3 lines

Regarding the new flag, perhaps we can avoid exposing this to userspace? I mean, we can define some mask of internal flags that we don't include via dump_status in ctnetlink.

What is your rationale for supressing this information? [ or, why is exposing this to userspace bad? ]

I think that flag provides no useful information to userspace.

Quoted 2 lines

Is it so we don't have to keep dummy flag when we find a different solution later?

Right, that's another good reason not to expose that information to userspace.

author maintainer
#4 lore raw
Quoted 8 lines · Pablo Neira Ayuso
Quoted 7 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines

I'm mainly interested if you think timer removal is worthwile, it works well in practice from usability POV.

Thanks for looking again into this. We definitely have to get rid of that timer.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Regarding the new flag, perhaps we can avoid exposing this to userspace? I mean, we can define some mask of internal flags that we don't include via dump_status in ctnetlink.

What is your rationale for supressing this information? [ or, why is exposing this to userspace bad? ]

I think that flag provides no useful information to userspace.

Fair enough.

Quoted 5 lines
Quoted 2 lines

Is it so we don't have to keep dummy flag when we find a different solution later?

Right, that's another good reason not to expose that information to userspace.

Alright, I'll create a separate patch that adds mask of 'public' flags that will be dumped to userspace.

Will send it along with another spin of this patch next week or so.