1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2024 Meta
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_core_read.h>
7 #include <bpf/bpf_endian.h>
8 #include "bpf_tracing_net.h"
9 #include "bpf_kfuncs.h"
11 #define ATTR __always_inline
12 #include "test_jhash.h"
14 static bool ipv6_addr_loopback(const struct in6_addr *a)
16 return (a->s6_addr32[0] | a->s6_addr32[1] |
17 a->s6_addr32[2] | (a->s6_addr32[3] ^ bpf_htonl(1))) == 0;
20 volatile const __u16 ports[2];
21 unsigned int bucket[2];
24 int iter_tcp_soreuse(struct bpf_iter__tcp *ctx)
26 struct sock *sk = (struct sock *)ctx->sk_common;
27 struct inet_hashinfo *hinfo;
35 sk = bpf_core_cast(sk, struct sock);
36 if (sk->sk_family != AF_INET6 ||
37 sk->sk_state != TCP_LISTEN ||
38 !ipv6_addr_loopback(&sk->sk_v6_rcv_saddr))
41 if (sk->sk_num == ports[0])
43 else if (sk->sk_num == ports[1])
48 /* bucket selection as in inet_lhash2_bucket_sk() */
50 hash = jhash2(sk->sk_v6_rcv_saddr.s6_addr32, 4, net->hash_mix);
52 hinfo = net->ipv4.tcp_death_row.hashinfo;
53 bucket[idx] = hash & hinfo->lhash2_mask;
54 bpf_seq_write(ctx->meta->seq, &idx, sizeof(idx));
59 #define udp_sk(ptr) container_of(ptr, struct udp_sock, inet.sk)
62 int iter_udp_soreuse(struct bpf_iter__udp *ctx)
64 struct sock *sk = (struct sock *)ctx->udp_sk;
65 struct udp_table *udptable;
71 sk = bpf_core_cast(sk, struct sock);
72 if (sk->sk_family != AF_INET6 ||
73 !ipv6_addr_loopback(&sk->sk_v6_rcv_saddr))
76 if (sk->sk_num == ports[0])
78 else if (sk->sk_num == ports[1])
83 /* bucket selection as in udp_hashslot2() */
84 udptable = sk->sk_net.net->ipv4.udp_table;
85 bucket[idx] = udp_sk(sk)->udp_portaddr_hash & udptable->mask;
86 bpf_seq_write(ctx->meta->seq, &idx, sizeof(idx));
91 char _license[] SEC("license") = "GPL";