ip_ct_selective_cleanup algorithm
Hi,
While reading through the nfnetlink code, I've encountered the ip_ct_selective_cleanup() function, which is used to delete entries from the conntrack table selectively. A comment states that the algorithm is n^2, but in fact it is even worse:
static struct ip_conntrack_tuple_hash *
get_next_corpse(int (*kill)(const struct ip_conntrack *i, void *data),
void *data)
{
struct ip_conntrack_tuple_hash *h = NULL;
unsigned int i;
READ_LOCK(&ip_conntrack_lock);
for (i = 0; !h && i < ip_conntrack_htable_size; i++) {
h = LIST_FIND(&ip_conntrack_hash[i], do_kill,
struct ip_conntrack_tuple_hash *, kill, data);
}
if (h)
atomic_inc(&h->ctrack->ct_general.use);
READ_UNLOCK(&ip_conntrack_lock);
return h;
}ip_ct_selective_cleanup(int (*kill)(const struct ip_conntrack *i, void *data),
void *data)
{
struct ip_conntrack_tuple_hash *h;
/* This is order n^2, by the way. */
while ((h = get_next_corpse(kill, data)) != NULL) {
/* Time to push up daises... */
if (del_timer(&h->ctrack->timeout))
death_by_timeout((unsigned long)h->ctrack);
/* ... else the timer will get him soon. */
ip_conntrack_put(h->ctrack);
}
}get_next_corpse() restarts scanning the hash table every time it is called. The reason seems to be to avoid races (as the hashtable might change while this function is processing entries)
Currently this function is called from:
- masquerading, when an interface or IP address is removed
- from nlconntrack when a conntrack entry is removed
- from NAT when a helper is removed
Is it worth changing the behaviour to grab the lock once, traverse the whole hashtable in one go, and then release the lock?
Signature · 2 lines
Bazsi PGP info: KeyID 9AF8D0A9 Fingerprint CD27 CFB0 802C 0944 9CFD 804E C82C 8EB1
Quoted 9 lines · Balazs Scheidler
Hi,
While reading through the nfnetlink code, I've encountered the
ip_ct_selective_cleanup()function, which is used to delete entries from the conntrack table selectively. A comment states that the algorithm is n^2, but in fact it is even worse:
get_next_corpse()restarts scanning the hash table every time it is called.
mh, you are right
Quoted 7 lines
Currently this function is called from:
- masquerading, when an interface or IP address is removed
- from nlconntrack when a conntrack entry is removed
- from NAT when a helper is removedIs it worth changing the behaviour to grab the lock once, traverse the whole hashtable in one go, and then release the lock?
It is certainly not worth the effort for the first and the third case. How often do you have interfaces for MASQUERADE go up and down? And how often do you unload NAT helper modules?
But for the ctnetlink case it might become interesting... depending on how frequently your application might delete conntrack entries.
Quoted 1 line
Bazsi
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie