1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2ef456144SCraig Gallek #ifndef _SOCK_REUSEPORT_H
3ef456144SCraig Gallek #define _SOCK_REUSEPORT_H
4ef456144SCraig Gallek
5538950a1SCraig Gallek #include <linux/filter.h>
6538950a1SCraig Gallek #include <linux/skbuff.h>
7ef456144SCraig Gallek #include <linux/types.h>
8736b4602SMartin KaFai Lau #include <linux/spinlock.h>
9ef456144SCraig Gallek #include <net/sock.h>
10ef456144SCraig Gallek
11736b4602SMartin KaFai Lau extern spinlock_t reuseport_lock;
12736b4602SMartin KaFai Lau
13ef456144SCraig Gallek struct sock_reuseport {
14ef456144SCraig Gallek struct rcu_head rcu;
15ef456144SCraig Gallek
16ef456144SCraig Gallek u16 max_socks; /* length of socks */
17ef456144SCraig Gallek u16 num_socks; /* elements in socks */
185c040eafSKuniyuki Iwashima u16 num_closed_socks; /* closed elements in socks */
19*b261eda8SKuniyuki Iwashima u16 incoming_cpu;
2040a1227eSMartin KaFai Lau /* The last synq overflow event timestamp of this
2140a1227eSMartin KaFai Lau * reuse->socks[] group.
2240a1227eSMartin KaFai Lau */
2340a1227eSMartin KaFai Lau unsigned int synq_overflow_ts;
24736b4602SMartin KaFai Lau /* ID stays the same even after the size of socks[] grows. */
25736b4602SMartin KaFai Lau unsigned int reuseport_id;
26acdcecc6SWillem de Bruijn unsigned int bind_inany:1;
27acdcecc6SWillem de Bruijn unsigned int has_conns:1;
28538950a1SCraig Gallek struct bpf_prog __rcu *prog; /* optional BPF sock selector */
292603c29eSGustavo A. R. Silva struct sock *socks[]; /* array of sock pointers */
30ef456144SCraig Gallek };
31ef456144SCraig Gallek
322dbb9b9eSMartin KaFai Lau extern int reuseport_alloc(struct sock *sk, bool bind_inany);
332dbb9b9eSMartin KaFai Lau extern int reuseport_add_sock(struct sock *sk, struct sock *sk2,
342dbb9b9eSMartin KaFai Lau bool bind_inany);
35ef456144SCraig Gallek extern void reuseport_detach_sock(struct sock *sk);
36333bb73fSKuniyuki Iwashima void reuseport_stop_listen_sock(struct sock *sk);
37538950a1SCraig Gallek extern struct sock *reuseport_select_sock(struct sock *sk,
38538950a1SCraig Gallek u32 hash,
39538950a1SCraig Gallek struct sk_buff *skb,
40538950a1SCraig Gallek int hdr_len);
411cd62c21SKuniyuki Iwashima struct sock *reuseport_migrate_sock(struct sock *sk,
421cd62c21SKuniyuki Iwashima struct sock *migrating_sk,
431cd62c21SKuniyuki Iwashima struct sk_buff *skb);
448217ca65SMartin KaFai Lau extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
4599f3a064SMartin KaFai Lau extern int reuseport_detach_prog(struct sock *sk);
4699f3a064SMartin KaFai Lau
reuseport_has_conns(struct sock * sk)4769421bf9SKuniyuki Iwashima static inline bool reuseport_has_conns(struct sock *sk)
48acdcecc6SWillem de Bruijn {
49acdcecc6SWillem de Bruijn struct sock_reuseport *reuse;
50acdcecc6SWillem de Bruijn bool ret = false;
51acdcecc6SWillem de Bruijn
52acdcecc6SWillem de Bruijn rcu_read_lock();
53acdcecc6SWillem de Bruijn reuse = rcu_dereference(sk->sk_reuseport_cb);
5469421bf9SKuniyuki Iwashima if (reuse && reuse->has_conns)
5569421bf9SKuniyuki Iwashima ret = true;
56acdcecc6SWillem de Bruijn rcu_read_unlock();
57acdcecc6SWillem de Bruijn
58acdcecc6SWillem de Bruijn return ret;
59acdcecc6SWillem de Bruijn }
60acdcecc6SWillem de Bruijn
6169421bf9SKuniyuki Iwashima void reuseport_has_conns_set(struct sock *sk);
62*b261eda8SKuniyuki Iwashima void reuseport_update_incoming_cpu(struct sock *sk, int val);
6369421bf9SKuniyuki Iwashima
64ef456144SCraig Gallek #endif /* _SOCK_REUSEPORT_H */
65