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 */ 1940a1227eSMartin KaFai Lau /* The last synq overflow event timestamp of this 2040a1227eSMartin KaFai Lau * reuse->socks[] group. 2140a1227eSMartin KaFai Lau */ 2240a1227eSMartin KaFai Lau unsigned int synq_overflow_ts; 23736b4602SMartin KaFai Lau /* ID stays the same even after the size of socks[] grows. */ 24736b4602SMartin KaFai Lau unsigned int reuseport_id; 25acdcecc6SWillem de Bruijn unsigned int bind_inany:1; 26acdcecc6SWillem de Bruijn unsigned int has_conns:1; 27538950a1SCraig Gallek struct bpf_prog __rcu *prog; /* optional BPF sock selector */ 282603c29eSGustavo A. R. Silva struct sock *socks[]; /* array of sock pointers */ 29ef456144SCraig Gallek }; 30ef456144SCraig Gallek 312dbb9b9eSMartin KaFai Lau extern int reuseport_alloc(struct sock *sk, bool bind_inany); 322dbb9b9eSMartin KaFai Lau extern int reuseport_add_sock(struct sock *sk, struct sock *sk2, 332dbb9b9eSMartin KaFai Lau bool bind_inany); 34ef456144SCraig Gallek extern void reuseport_detach_sock(struct sock *sk); 35333bb73fSKuniyuki Iwashima void reuseport_stop_listen_sock(struct sock *sk); 36538950a1SCraig Gallek extern struct sock *reuseport_select_sock(struct sock *sk, 37538950a1SCraig Gallek u32 hash, 38538950a1SCraig Gallek struct sk_buff *skb, 39538950a1SCraig Gallek int hdr_len); 401cd62c21SKuniyuki Iwashima struct sock *reuseport_migrate_sock(struct sock *sk, 411cd62c21SKuniyuki Iwashima struct sock *migrating_sk, 421cd62c21SKuniyuki Iwashima struct sk_buff *skb); 438217ca65SMartin KaFai Lau extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog); 4499f3a064SMartin KaFai Lau extern int reuseport_detach_prog(struct sock *sk); 4599f3a064SMartin KaFai Lau 46*69421bf9SKuniyuki Iwashima static inline bool reuseport_has_conns(struct sock *sk) 47acdcecc6SWillem de Bruijn { 48acdcecc6SWillem de Bruijn struct sock_reuseport *reuse; 49acdcecc6SWillem de Bruijn bool ret = false; 50acdcecc6SWillem de Bruijn 51acdcecc6SWillem de Bruijn rcu_read_lock(); 52acdcecc6SWillem de Bruijn reuse = rcu_dereference(sk->sk_reuseport_cb); 53*69421bf9SKuniyuki Iwashima if (reuse && reuse->has_conns) 54*69421bf9SKuniyuki Iwashima ret = true; 55acdcecc6SWillem de Bruijn rcu_read_unlock(); 56acdcecc6SWillem de Bruijn 57acdcecc6SWillem de Bruijn return ret; 58acdcecc6SWillem de Bruijn } 59acdcecc6SWillem de Bruijn 60*69421bf9SKuniyuki Iwashima void reuseport_has_conns_set(struct sock *sk); 61*69421bf9SKuniyuki Iwashima 62ef456144SCraig Gallek #endif /* _SOCK_REUSEPORT_H */ 63