Skip to content

Commit 2a406e8

Browse files
YiHungWeiummakynes
authored andcommitted
netfilter: nf_conncount: Early exit for garbage collection
This patch is originally from Florian Westphal. We use an extra function with early exit for garbage collection. It is not necessary to traverse the full list for every node since it is enough to zap a couple of entries for garbage collection. Signed-off-by: Yi-Hung Wei <yihung.wei@gmail.com> Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 5d400a4 commit 2a406e8

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

net/netfilter/nf_conncount.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,42 @@ unsigned int nf_conncount_lookup(struct net *net, struct hlist_head *head,
189189
}
190190
EXPORT_SYMBOL_GPL(nf_conncount_lookup);
191191

192+
static void nf_conncount_gc_list(struct net *net,
193+
struct nf_conncount_rb *rbconn)
194+
{
195+
const struct nf_conntrack_tuple_hash *found;
196+
struct nf_conncount_tuple *conn;
197+
struct hlist_node *n;
198+
struct nf_conn *found_ct;
199+
unsigned int collected = 0;
200+
201+
hlist_for_each_entry_safe(conn, n, &rbconn->hhead, node) {
202+
found = find_or_evict(net, conn);
203+
if (IS_ERR(found)) {
204+
if (PTR_ERR(found) == -ENOENT)
205+
collected++;
206+
continue;
207+
}
208+
209+
found_ct = nf_ct_tuplehash_to_ctrack(found);
210+
if (already_closed(found_ct)) {
211+
/*
212+
* we do not care about connections which are
213+
* closed already -> ditch it
214+
*/
215+
nf_ct_put(found_ct);
216+
hlist_del(&conn->node);
217+
kmem_cache_free(conncount_conn_cachep, conn);
218+
collected++;
219+
continue;
220+
}
221+
222+
nf_ct_put(found_ct);
223+
if (collected > CONNCOUNT_GC_MAX_NODES)
224+
return;
225+
}
226+
}
227+
192228
static void tree_nodes_free(struct rb_root *root,
193229
struct nf_conncount_rb *gc_nodes[],
194230
unsigned int gc_count)
@@ -251,8 +287,7 @@ count_tree(struct net *net, struct rb_root *root,
251287
if (no_gc || gc_count >= ARRAY_SIZE(gc_nodes))
252288
continue;
253289

254-
/* only used for GC on hhead, retval and 'addit' ignored */
255-
nf_conncount_lookup(net, &rbconn->hhead, tuple, zone, &addit);
290+
nf_conncount_gc_list(net, rbconn);
256291
if (hlist_empty(&rbconn->hhead))
257292
gc_nodes[gc_count++] = rbconn;
258293
}

0 commit comments

Comments
 (0)