All of lore.kernel.org
 help / color / mirror / Atom feed
From: Olaf Rempel <razzor@kopf-tisch.de>
To: netfilter-devel@lists.netfilter.org
Subject: flushing conntrack-table
Date: Sat, 12 Mar 2005 00:34:18 +0100	[thread overview]
Message-ID: <20050312003418.6a582ad9@coruscant> (raw)

Hi list

I've build a modul to flush some/all conntracks from the ct-table, but have 
some questions about the used functions and locking issues.

I'm sending a "search pattern" (proto + srcip/mask + sport-range + dstip/mask 
+ dport-range) via ioctl (yeah, i know..) to the kernel, and delete all matching
conntracks. 

As far as I understand, I don't need hold a lock while iterating over the 
hash-buckets, only when accessing the list inside.

To actually delete a conntrack (via del_timer + ct->timeout.function) the code 
must NOT hold the readlock, because death_by_timeout() grabs the writelock. 
Correct?

idea 1:
if a matching tuple is found, abort the search, delete conntrack and restart 
search in same bucket (assuming that there could be another matching tuple):

while (bucket < ip_conntrack_htable_size) {
	struct ip_conntrack *ct = NULL;

	READ_LOCK(&ip_conntrack_lock);
	list_for_each_entry(hash_tuple, &ip_conntrack_hash[bucket], list) {
		if (match_pattern(hash_tuple, pattern)) {
			ct = tuplehash_to_ctrack(hash_tuple);
			break;
		}
	}
	READ_UNLOCK(&ip_conntrack_lock);

	if (ct) {
		if (del_timer(&ct->timeout))
			ct->timeout.function((unsigned long)ct);

	} else {
		bucket++;
	}
}

idea 2:
same as idea 1, but storing N matching conntracks in an array. if the array is 
full, or the search in this bucket is complete then delete the found conntracks.
if array was full, restart search in same bucket else do next bucket.
N is small (~4), just to avoid search-restart in the same bucket with 2-3 matching
tuples. (don't know if it's needed)

idea 3:
use list_for_each_entry_safe(), when a matching tuple is found, 
release the readlock, delete the conntrack, grab readlock, resume search:

	READ_LOCK(&ip_conntrack_lock);
	list_for_each_entry_safe(hash_tuple, tmp, &ip_conntrack_hash[bucket], list) {
		if (match_pattern(hash_tuple, pattern)) {
			ct = tuplehash_to_ctrack(hash_tuple);
			READ_UNLOCK(&ip_conntrack_lock);

			if (del_timer(&ct->timeout))
				ct->timeout.function((unsigned long)ct);

			READ_LOCK(&ip_conntrack_lock);
		}
	}
	READ_UNLOCK(&ip_conntrack_lock);

I know I could do this with ctnetlink or something equal, but I want to try this
in kernel. The code from idea #2 works for me, but I just want to know if it's
a good way.

So, which of these ideas is "the best"?
Or am I missing something (== my code is crap :)

Olaf

             reply	other threads:[~2005-03-11 23:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-11 23:34 Olaf Rempel [this message]
2005-03-12  0:51 ` flushing conntrack-table Tobias DiPasquale

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050312003418.6a582ad9@coruscant \
    --to=razzor@kopf-tisch.de \
    --cc=netfilter-devel@lists.netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.