netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Evgeniy Polyakov <johnpol@2ka.mipt.ru>
To: David Miller <davem@davemloft.net>
Cc: draghuram@rocketmail.com, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org,
	"Brian F. G. Bidulock" <bidulock@openss7.org>
Subject: Re: Question about tcp hash function tcp_hashfn()
Date: Thu, 1 Jun 2006 10:04:24 +0400	[thread overview]
Message-ID: <20060601060424.GA28087@2ka.mipt.ru> (raw)
In-Reply-To: <20060531.114127.14356069.davem@davemloft.net>

[-- Attachment #1: Type: text/plain, Size: 1092 bytes --]

On Wed, May 31, 2006 at 11:41:27AM -0700, David Miller (davem@davemloft.net) wrote:
> > >   Worse: he folded the jenkins algorith result with
> > > 
> > >    h ^= h >> 16;
> > >    h ^= h >> 8;
> > > 
> > >   Destroying the coverage of the function.
> > 
> > It was done to simulate socket code which uses the same folding.
> > Leaving 32bit space is just wrong, consider hash table size with that
> > index.
> 
> You absolutely show not do this shifting on the jenkins hash
> result, you destroy the distribution entirely.  Just mask
> it with the hash mask and that's all you need to do.
> 
> Brian is right, this is absolutely critical to using the Jenkins hash
> correctly.  You're "unmixing" the bits it worked so hard to mix.

That is wrong. And I have a code and picture to show that, 
and you dont - prove me wrong :)

Attached code and picture which shows _exactly_ the same distribution for
folded and not folded Jenkins hash distribution, and it's artifact
compared to XOR hash.

Fairly distributed values being XORed produce still fairly distributed
value.

-- 
	Evgeniy Polyakov

[-- Attachment #2: hash_comparison.png --]
[-- Type: image/png, Size: 7231 bytes --]

[-- Attachment #3: test.c --]
[-- Type: text/plain, Size: 2015 bytes --]

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;

typedef unsigned int __u32;
typedef unsigned short __u16;
typedef unsigned char __u8;

#include "jhash.h"

struct hash_entry
{
	unsigned long		counter;
};

static inline num2ip(__u8 a1, __u8 a2, __u8 a3, __u8 a4)
{
	__u32 a = 0;

	a |= a1;
	a << 8;
	a |= a2;
	a << 8;
	a |= a3;
	a << 8;
	a |= a4;

	return a;
}

static inline __u8 get_random_byte(void)
{
	return 1 + (int) (255.0 * (rand() / (RAND_MAX + 1.0)));
}

static inline __u16 get_random_word(void)
{
	return 1 + (int) (65536.0 * (rand() / (RAND_MAX + 1.0)));
}

unsigned int hash_addr(__u32 faddr, __u16 fport, __u32 laddr, __u16 lport)
{
	unsigned int h = (laddr ^ lport) ^ (faddr ^ fport);
	h ^= h >> 16;
	h ^= h >> 8;
	return h;
}

unsigned int hash_addr1(__u32 faddr, __u16 fport, __u32 laddr, __u16 lport)
{
	u32 ports;
	unsigned int h;

	ports = lport;
	ports <<= 16;
	ports |= fport;

	h = jhash_2words(faddr, laddr, ports);
	//h ^= h >> 16;
	//h ^= h >> 8;

	return h;
}

int main()
{
	struct hash_entry *table;
	__u32 saddr, daddr;
	__u16 sport, dport;
	unsigned int hash, i, *results;
	unsigned int hash_size = 65536, iter_num = 100;

	table = malloc(hash_size * sizeof(struct hash_entry));
	if (!table)
		return -ENOMEM;
	
	results = malloc(hash_size * sizeof(unsigned int));
	if (!results)
		return -ENOMEM;

	for (i=0; i<hash_size; ++i) {
		results[i] = 0;
		table[i].counter = 0;
	}

	srand(time(NULL));

	daddr = num2ip(192, 168, 0, 1);
	dport = htons(80);
	
	for (i=0; i<hash_size*iter_num; ++i) {
		saddr = num2ip(get_random_byte(), get_random_byte(), get_random_byte(), get_random_byte());
		sport = get_random_word();

		hash = hash_addr(saddr, sport, daddr, dport);
		hash &= (hash_size - 1);

		table[hash].counter++;
	}

	for (i=0; i<hash_size; ++i)
		results[table[i].counter]++;
	
	for (i=0; i<hash_size; ++i)
		printf("%u %u\n", i, results[i]);
		//printf("%u %u\n", i, table[i].counter);
}

  reply	other threads:[~2006-06-01  6:04 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20060531042908.10463.qmail@web51410.mail.yahoo.com>
     [not found] ` <20060530235525.A30563@openss7.org>
2006-05-31  7:10   ` Question about tcp hash function tcp_hashfn() David Miller
2006-05-31  7:45     ` Brian F. G. Bidulock
2006-05-31  7:49       ` David Miller
2006-05-31  8:00         ` Brian F. G. Bidulock
2006-05-31  9:03           ` Evgeniy Polyakov
2006-05-31  9:12             ` David Miller
2006-05-31  9:44               ` Evgeniy Polyakov
2006-05-31  9:51             ` Brian F. G. Bidulock
2006-05-31 10:58               ` Evgeniy Polyakov
2006-05-31 11:04                 ` Evgeniy Polyakov
2006-05-31 13:06                   ` Evgeniy Polyakov
2006-05-31 18:29                     ` Brian F. G. Bidulock
2006-06-01  6:12                       ` Evgeniy Polyakov
2006-06-01  6:18                         ` David Miller
2006-06-01  6:22                           ` Brian F. G. Bidulock
2006-06-01  6:24                             ` David Miller
2006-05-31 18:41                 ` David Miller
2006-06-01  6:04                   ` Evgeniy Polyakov [this message]
2006-06-01  6:18                     ` Brian F. G. Bidulock
2006-06-01  6:30                       ` Evgeniy Polyakov
2006-06-01  6:46                         ` Brian F. G. Bidulock
2006-06-01  7:01                           ` Evgeniy Polyakov
2006-06-01  7:11                             ` Brian F. G. Bidulock
2006-06-01  8:38                               ` Evgeniy Polyakov
2006-06-01 10:24                                 ` Brian F. G. Bidulock
2006-06-01 11:06                                   ` Evgeniy Polyakov
2006-06-01 18:40                                     ` Brian F. G. Bidulock
2006-06-01 20:21                                       ` David Miller
2006-06-02  7:01                                       ` Evgeniy Polyakov
2006-06-02  5:40                     ` Florian Weimer
2006-06-02  7:48                       ` Evgeniy Polyakov
2006-06-02 15:10                         ` Brian F. G. Bidulock
2006-06-02 17:26                         ` Florian Weimer
2006-06-02 17:37                           ` Brian F. G. Bidulock
2006-05-31  9:52             ` Brian F. G. Bidulock
2006-05-31  8:49         ` Brian F. G. Bidulock
2006-05-31  9:02           ` David Miller
2006-05-31  9:39             ` Brian F. G. Bidulock

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=20060601060424.GA28087@2ka.mipt.ru \
    --to=johnpol@2ka.mipt.ru \
    --cc=bidulock@openss7.org \
    --cc=davem@davemloft.net \
    --cc=draghuram@rocketmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).