hash function for src dest ip + port

前端 未结 4 1052
轻奢々
轻奢々 2020-12-14 23:38

So, I am looking at different hash functions to use for hashing a 4 tuple ip and port to identify flows.

One I came across was

((size_t)(key.src.s_a         


        
4条回答
  •  不思量自难忘°
    2020-12-14 23:59

    Brian Gideon pretty much sums it up; the multiplication and the shift are intended as a symmetry breaker. So this catches the hypothetical case of machine A telnetting to machine B and vice versa and they happen to chose the same ephemeral portnum. Not very common, but not impossible. Much of the 5-tuple is pretty constant: protocol comes from a very small domain, and so does one half of {address,portnum}.

    Assuming a prime-sized hashtable, the magic constant 59 could be replaced by any prime, IMO. The (port << 16) could also be replaced by another shift, as long as no bits fall off or even by a (port * some_other_prime) term.

    For a power-of-two sized hashtable, all(minus one) members of the 5-tuple should be multiplied by a (different) prime. (in the old days, when division was expensive that would have been an option)

提交回复
热议问题