github.com/cilium/cilium@v1.16.2/bpf/lib/hash.h (about)

     1  /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
     2  /* Copyright Authors of Cilium */
     3  
     4  #pragma once
     5  
     6  #include "common.h"
     7  #include "jhash.h"
     8  
     9  /* The daddr is explicitly excluded from the hash here in order to allow for
    10   * backend selection to choose the same backend even on different service VIPs.
    11   */
    12  static __always_inline __u32 hash_from_tuple_v4(const struct ipv4_ct_tuple *tuple)
    13  {
    14  	return jhash_3words(tuple->saddr,
    15  			    ((__u32)tuple->dport << 16) | tuple->sport,
    16  			    tuple->nexthdr, HASH_INIT4_SEED);
    17  }
    18  
    19  static __always_inline __u32 hash_from_tuple_v6(const struct ipv6_ct_tuple *tuple)
    20  {
    21  	__u32 a, b, c;
    22  
    23  	a = tuple->saddr.p1;
    24  	b = tuple->saddr.p2;
    25  	c = tuple->saddr.p3;
    26  	__jhash_mix(a, b, c);
    27  	a += tuple->saddr.p4;
    28  	b += ((__u32)tuple->dport << 16) | tuple->sport;
    29  	c += tuple->nexthdr;
    30  	__jhash_mix(a, b, c);
    31  	a += HASH_INIT6_SEED;
    32  	__jhash_final(a, b, c);
    33  	return c;
    34  }