Skip to contentSkip to conversation

netfilter: nf_conntrack: speed up reads from nf_conntrack proc file

PATCH Superseded Florian Westphal 2 participants 5 messages Feb 11, 2025 – Mar 12, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_conntrack: speed up reads from nf_conntrack proc file

Dumping all conntrack entries via proc interface can take hours due to linear search to skip entries dumped so far in each cycle.

Apply same strategy used to speed up ipvs proc reading done in commit 178883fd039d ("ipvs: speed up reads from ip_vs_conn proc file") to nf_conntrack.

Note that the ctnetlink interface doesn't suffer from this problem.

---
 net/netfilter/nf_conntrack_standalone.c | 73 +++++++++++++------------
 1 file changed, 38 insertions(+), 35 deletions(-)
net/netfilter/nf_conntrack_standalone.c +38 -35
index 502cf10aab41..2a79e690470a 100644
@@ -98,51 +98,34 @@ struct ct_iter_state {
 	struct seq_net_private p;
 	struct hlist_nulls_head *hash;
 	unsigned int htable_size;
+	unsigned int skip_elems;
 	unsigned int bucket;
 	u_int64_t time_now;
 };
 
-static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
+static struct nf_conntrack_tuple_hash *ct_get_next(struct ct_iter_state *st)
 {
-	struct ct_iter_state *st = seq->private;
+	struct nf_conntrack_tuple_hash *h;
 	struct hlist_nulls_node *n;
+	unsigned int i;
 
-	for (st->bucket = 0;
-	     st->bucket < st->htable_size;
-	     st->bucket++) {
-		n = rcu_dereference(
-			hlist_nulls_first_rcu(&st->hash[st->bucket]));
-		if (!is_a_nulls(n))
-			return n;
-	}
-	return NULL;
-}
+	for (i = st->bucket; i < st->htable_size; i++) {
+		unsigned int skip = 0;
 
-static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
-				      struct hlist_nulls_node *head)
-{
-	struct ct_iter_state *st = seq->private;
+		hlist_nulls_for_each_entry(h, n, &st->hash[i], hnnode) {
+			if (skip >= st->skip_elems) {
+				st->bucket = i;
+				return h;
+			}
 
-	head = rcu_dereference(hlist_nulls_next_rcu(head));
-	while (is_a_nulls(head)) {
-		if (likely(get_nulls_value(head) == st->bucket)) {
-			if (++st->bucket >= st->htable_size)
-				return NULL;
+			++skip;
 		}
-		head = rcu_dereference(
-			hlist_nulls_first_rcu(&st->hash[st->bucket]));
-	}
-	return head;
-}
 
-static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
-{
-	struct hlist_nulls_node *head = ct_get_first(seq);
+		st->skip_elems = 0;
+	}
 
-	if (head)
-		while (pos && (head = ct_get_next(seq, head)))
-			pos--;
-	return pos ? NULL : head;
+	st->bucket = i;
+	return NULL;
 }
 
 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
@@ -154,13 +137,33 @@ static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
 	rcu_read_lock();
 
 	nf_conntrack_get_ht(&st->hash, &st->htable_size);
-	return ct_get_idx(seq, *pos);
+
+	if (*pos == 0) {
+		st->skip_elems = 0;
+		st->bucket = 0;
+	}
+
+	return ct_get_next(st);
 }
 
 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
 {
+	struct nf_conntrack_tuple_hash *h = v;
+	struct ct_iter_state *st = s->private;
+	struct hlist_nulls_node *n;
+
 	(*pos)++;
-	return ct_get_next(s, v);
+
+	/* more on same hash chain? */
+	n = rcu_dereference(hlist_nulls_next_rcu(&h->hnnode));
+	if (n && !is_a_nulls(n)) {
+		st->skip_elems++;
+		return hlist_nulls_entry(n, struct nf_conntrack_tuple_hash, hnnode);
+	}
+
+	st->skip_elems = 0;
+	st->bucket++;
+	return ct_get_next(st);
 }
 
 static void ct_seq_stop(struct seq_file *s, void *v)
Signature · 1 line
2.45.3
#1 lore raw

Hi Florian,

a few comments below.

Quoted 43 lines · Florian Westphal

Dumping all conntrack entries via proc interface can take hours due to linear search to skip entries dumped so far in each cycle.

Apply same strategy used to speed up ipvs proc reading done in commit 178883fd039d ("ipvs: speed up reads from ip_vs_conn proc file") to nf_conntrack.

Note that the ctnetlink interface doesn't suffer from this problem.

---
 net/netfilter/nf_conntrack_standalone.c | 73 +++++++++++++------------
 1 file changed, 38 insertions(+), 35 deletions(-)
net/netfilter/nf_conntrack_standalone.c +4 -10
index 502cf10aab41..2a79e690470a 100644
@@ -98,51 +98,34 @@ struct ct_iter_state {
 	struct seq_net_private p;
 	struct hlist_nulls_head *hash;
 	unsigned int htable_size;
+	unsigned int skip_elems;
 	unsigned int bucket;
 	u_int64_t time_now;
 };
 
-static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
+static struct nf_conntrack_tuple_hash *ct_get_next(struct ct_iter_state *st)
 {
-	struct ct_iter_state *st = seq->private;
+	struct nf_conntrack_tuple_hash *h;
 	struct hlist_nulls_node *n;
+	unsigned int i;
 
-	for (st->bucket = 0;
-	     st->bucket < st->htable_size;
-	     st->bucket++) {
-		n = rcu_dereference(
-			hlist_nulls_first_rcu(&st->hash[st->bucket]));
-		if (!is_a_nulls(n))
-			return n;
-	}

net/netfilter/nf_conntrack_standalone.c struct ct_iter_state { · line 115

Quoted 10 lines
net/netfilter/nf_conntrack_standalone.c +3 -6
-	return NULL;
-}
+	for (i = st->bucket; i < st->htable_size; i++) {
+		unsigned int skip = 0;
 
-static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
-				      struct hlist_nulls_node *head)
-{
-	struct ct_iter_state *st = seq->private;
+		hlist_nulls_for_each_entry(h, n, &st->hash[i], hnnode) {

hlist_nulls_for_each_entry_rcu ?

Quoted 4 lines
net/netfilter/nf_conntrack_standalone.c +4 -0
+			if (skip >= st->skip_elems) {
+				st->bucket = i;
+				return h;
+			}

net/netfilter/nf_conntrack_standalone.c struct ct_iter_state { · line 133

Quoted 10 lines
net/netfilter/nf_conntrack_standalone.c +1 -7
 
-	head = rcu_dereference(hlist_nulls_next_rcu(head));
-	while (is_a_nulls(head)) {
-		if (likely(get_nulls_value(head) == st->bucket)) {
-			if (++st->bucket >= st->htable_size)
-				return NULL;
+			++skip;
 		}
-		head = rcu_dereference(
-			hlist_nulls_first_rcu(&st->hash[st->bucket]));

This does not rewind if get_nulls_value(head) != st->bucket), not needed anymore?

Quoted 59 lines
net/netfilter/nf_conntrack_standalone.c +26 -12
-	}
-	return head;
-}
 
-static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
-{
-	struct hlist_nulls_node *head = ct_get_first(seq);
+		st->skip_elems = 0;
+	}
 
-	if (head)
-		while (pos && (head = ct_get_next(seq, head)))
-			pos--;
-	return pos ? NULL : head;
+	st->bucket = i;
+	return NULL;
 }
 
 static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
@@ -154,13 +137,33 @@ static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
 	rcu_read_lock();
 
 	nf_conntrack_get_ht(&st->hash, &st->htable_size);
-	return ct_get_idx(seq, *pos);
+
+	if (*pos == 0) {
+		st->skip_elems = 0;
+		st->bucket = 0;
+	}
+
+	return ct_get_next(st);
 }
 
 static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
 {
+	struct nf_conntrack_tuple_hash *h = v;
+	struct ct_iter_state *st = s->private;
+	struct hlist_nulls_node *n;
+
 	(*pos)++;
-	return ct_get_next(s, v);
+
+	/* more on same hash chain? */
+	n = rcu_dereference(hlist_nulls_next_rcu(&h->hnnode));
+	if (n && !is_a_nulls(n)) {
+		st->skip_elems++;
+		return hlist_nulls_entry(n, struct nf_conntrack_tuple_hash, hnnode);
+	}
+
+	st->skip_elems = 0;
+	st->bucket++;
+	return ct_get_next(st);
 }
 
 static void ct_seq_stop(struct seq_file *s, void *v)
Signature · 1 line
2.45.3
author maintainer
#2 lore raw
Quoted 4 lines · Pablo Neira Ayuso

Review

Quoted 2 lines
(quoted diff) +1 -1
-	struct ct_iter_state *st = seq->private;
+		hlist_nulls_for_each_entry(h, n, &st->hash[i], hnnode) {

hlist_nulls_for_each_entry_rcu ?

Yes.

Quoted 10 lines

net/netfilter/nf_conntrack_standalone.c struct ct_iter_state { · line 133

Quoted 7 lines
net/netfilter/nf_conntrack_standalone.c +1 -5
-		if (likely(get_nulls_value(head) == st->bucket)) {
-			if (++st->bucket >= st->htable_size)
-				return NULL;
+			++skip;
 		}
-		head = rcu_dereference(
-			hlist_nulls_first_rcu(&st->hash[st->bucket]));

This does not rewind if get_nulls_value(head) != st->bucket), not needed anymore?

There are only two choices:
1. rewind and (possibly) dump entries more than once
2. skip to next and miss an entry

I'm not sure whats worse/better.

#3 lore raw
Quoted 19 lines · Florian Westphal
Quoted 4 lines · Pablo Neira Ayuso

Review

Quoted 2 lines
(quoted diff) +1 -1
-	struct ct_iter_state *st = seq->private;
+		hlist_nulls_for_each_entry(h, n, &st->hash[i], hnnode) {

hlist_nulls_for_each_entry_rcu ?

Yes.

Quoted 10 lines

net/netfilter/nf_conntrack_standalone.c struct ct_iter_state { · line 133

Quoted 7 lines
net/netfilter/nf_conntrack_standalone.c +1 -5
-		if (likely(get_nulls_value(head) == st->bucket)) {
-			if (++st->bucket >= st->htable_size)
-				return NULL;
+			++skip;
 		}
-		head = rcu_dereference(
-			hlist_nulls_first_rcu(&st->hash[st->bucket]));

This does not rewind if get_nulls_value(head) != st->bucket), not needed anymore?

Quoted 3 lines

There are only two choices:
1. rewind and (possibly) dump entries more than once
2. skip to next and miss an entry

I think we can still display duplicates in 2. too since nulls check if the iteration finished on another bucket? Then 2. means skipped entries and duplicates.

Quoted 1 line

I'm not sure whats worse/better.

Skipping looks simpler, it is less code. But if entries are duplicated then userspace has a chance to deduplicate?

I am not sure how many entries could be skipped without the nulls check in practise TBH.

If you prefer to simple and skip entries, I suggest to add this to the patch description, this is a change in the behaviour that is worth documenting IMO.

Thanks.

author maintainer
#4 lore raw
Quoted 5 lines · Pablo Neira Ayuso
Quoted 1 line

2. skip to next and miss an entry

I think we can still display duplicates in 2. too since nulls check if the iteration finished on another bucket? Then 2. means skipped entries and duplicates.

Yes, but its not a change in behaviour, this was never a stable walk.

Quoted 4 lines
Quoted 1 line

I'm not sure whats worse/better.

Skipping looks simpler, it is less code. But if entries are duplicated then userspace has a chance to deduplicate?

Lets just give up and nuke the entire functionality, i.e. /proc interface removal.