From: Patrick Schaaf Subject: Re: conntrack performance/DoS formula Date: Sat, 29 Jun 2002 14:34:06 +0200 Sender: netfilter-devel-admin@lists.samba.org Message-ID: <20020629143406.O4136@oknodo.bof.de> References: <15643.31140.525551.843205@isis.cs3-inc.com> <20020628082339.I2890@oknodo.bof.de> <15644.41633.528107.723418@isis.cs3-inc.com> <20020628203624.A4136@oknodo.bof.de> <15644.45834.373409.220647@isis.cs3-inc.com> <20020628213520.C4136@oknodo.bof.de> <15644.53466.193145.914415@isis.cs3-inc.com> <20020628232819.H4136@oknodo.bof.de> <15644.58258.483223.56527@isis.cs3-inc.com> <20020629140729.N4136@oknodo.bof.de> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="uh9ZiVrAOUUm9fzH" Cc: Don Cohen , netfilter-devel@lists.samba.org Return-path: To: Patrick Schaaf Content-Disposition: inline In-Reply-To: <20020629140729.N4136@oknodo.bof.de>; from bof@bof.de on Sat, Jun 29, 2002 at 02:07:29PM +0200 Errors-To: netfilter-devel-admin@lists.samba.org List-Help: List-Post: List-Subscribe: , List-Unsubscribe: , List-Archive: List-Id: netfilter-devel.vger.kernel.org --uh9ZiVrAOUUm9fzH Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi again, one more elaboration of the cttest program, before I go offline in preparation for the fourth German football world championship... This version adds a '-b X' option. If you specify that, all TCP conntracks with timeouts less than X get ignored. For example, use "-b 431940" to only count the conntracks active within the last 60 seconds. Incidentally, this makes the current ip_conntrack hash function look a lot less bad than before. I appears that the "long bucket" packets mostly belong to some kind of port scan. They will time out eventually, and they will NOT contribute significantly to CPU usage, because they will be rarely hit. All is well, it seems. At least looking at my 33000 entry sample from yesterday. I'm looking forward to results from other workloads! best regards Patrick --uh9ZiVrAOUUm9fzH Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="cttest.c" /* cttest.c - parse /proc/net/ip_conntrack, compute bucket occupation * * This is how lines look: * * udp 17 0 src=213.6.73.215 dst=62.104.191.241 sport=1858 dport=53 src=62.104.191.241 dst=213.6.73.215 sport=53 dport=1858 use=1 * tcp 6 114 TIME_WAIT src=62.104.211.64 dst=212.7.144.66 sport=34685 dport=80 src=212.7.144.66 dst=62.104.211.64 sport=80 dport=34685 [ASSURED] use=1 */ #include #include #include #include #include #include #include #include typedef unsigned long u32; typedef unsigned short u16; typedef unsigned char u8; struct ct_key { u32 sip; u32 dip; u16 sport; u16 dport; u8 proto; }; static u32 hash_conntrack(struct ct_key *key) { return ntohl(key->sip + key->dip + key->sport + key->dport + key->proto) + ntohs(key->sport); } #include "crc32.c" static u32 hash_crc32(struct ct_key *key) { u32 crc = crc32(0, (const char *) &key->proto, sizeof(key->proto)); u32 v32; u16 v16; v32 = ntohl(key->sip); crc = crc32(crc, (const char *) &v32, sizeof(v32)); v32 = ntohl(key->dip); crc = crc32(crc, (const char *) &v32, sizeof(v32)); v16 = ntohs(key->sport); crc = crc32(crc, (const char *) &v16, sizeof(v16)); v16 = ntohs(key->dport); crc = crc32(crc, (const char *) &v16, sizeof(v16)); return crc; } static u32 hash_random(struct ct_key *key) { return (u32) random(); } struct hash_def { char *name; u32 (*hash_f)(struct ct_key *); }; #define NR_HASHES (sizeof(hashes)/sizeof(hashes[0])) static struct hash_def hashes[] = { { "original", hash_conntrack }, { "random", hash_random }, { "crc32", hash_crc32 }, }; static u32 htable_size = 7168; static u32 *occupation; static struct hash_def *test_hash = hashes; static void do_init(void) { u32 idx; occupation = malloc(sizeof(*occupation) * htable_size); for (idx=0; idxhash_f(key) % htable_size; occupation[idx]++; return ((int)idx) == display_target; } static u32 parse_ip(char *t) { char *p = strchr(t, '='); if (!p++) return 0; return inet_addr(p); } static u16 parse_port(char *t) { char *p = strchr(t, '='); if (!p++) return 0; return (u16) strtoul(p, 0, 10); } static u32 ignore_tcp_timeout_bound = 0; static int ignore_by_timeout(struct ct_key *key, char *tok) { return key->proto == 6 && strtoul(tok, 0, 10) < ignore_tcp_timeout_bound; } static int unidir; static int process(char *p) { struct ct_key key[1]; char *tok; int res; strtok(p, " \t\n"); /* prot text */ tok = strtok(0, " \t\n"); key->proto = (u8) strtoul(tok, 0, 10); /* prot num */ tok = strtok(0, " \t\n"); /* timeout */ if (ignore_by_timeout(key, tok)) return 0; switch (key->proto) { case 6: strtok(0, " \t\n"); /* tcp state */ case 17: break; default: return 0; } /* forward */ tok = strtok(0, " \t\n"); key->sip = parse_ip(tok); /* src ip */ tok = strtok(0, " \t\n"); key->dip = parse_ip(tok); /* dst ip */ tok = strtok(0, " \t\n"); key->sport = parse_port(tok); /* src port */ tok = strtok(0, " \t\n"); key->dport = parse_port(tok); /* dst port */ res = do_count(key); if (unidir) return res; /* reverse */ tok = strtok(0, " \t\n"); key->sip = parse_ip(tok); /* src ip */ tok = strtok(0, " \t\n"); key->dip = parse_ip(tok); /* dst ip */ tok = strtok(0, " \t\n"); key->sport = parse_port(tok); /* src port */ tok = strtok(0, " \t\n"); key->dport = parse_port(tok); /* dst port */ return res | do_count(key); } static int occupation_target = -1; static void show_occupation(void) { u32 idx, maxlen, total, partial, *agg; for (maxlen=0, total=0, idx=0; idx maxlen) maxlen = occupation[idx]; } printf("hash function: %s\n", test_hash->name); printf("maximum bucket list length: %lu\n", maxlen); printf("total number of entries: %lu\n", total); agg = malloc(sizeof(*agg) * (maxlen+1)); for (idx=0; idx<=maxlen; idx++) agg[idx] = 0; for (idx=0; idx0; idx--) { double d; if (agg[idx] == 0) continue; partial += idx * agg[idx]; d = (100.0 * partial) / (1.0 * total); printf("%lu\t%lu\t%.02f%%\n", agg[idx], idx, d); } printf("%lu\t0\t100.00%%\n", agg[idx]); } static struct hash_def *find_hash(char *name) { u32 idx; for (idx=0; idx> 8); #define DO2(buf) DO1(buf); DO1(buf); #define DO4(buf) DO2(buf); DO2(buf); #define DO8(buf) DO4(buf); DO4(buf); /* ========================================================================= */ u32 crc32(crc, buf, len) u32 crc; const char *buf; int len; { crc = crc ^ 0xffffffffL; while (len >= 8) { DO8(buf); len -= 8; } if (len) do { DO1(buf); } while (--len); return crc ^ 0xffffffffL; } --uh9ZiVrAOUUm9fzH--