xref: /OK3568_Linux_fs/kernel/include/net/sock_reuseport.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _SOCK_REUSEPORT_H
3*4882a593Smuzhiyun #define _SOCK_REUSEPORT_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/filter.h>
6*4882a593Smuzhiyun #include <linux/skbuff.h>
7*4882a593Smuzhiyun #include <linux/types.h>
8*4882a593Smuzhiyun #include <linux/spinlock.h>
9*4882a593Smuzhiyun #include <net/sock.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun extern spinlock_t reuseport_lock;
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun struct sock_reuseport {
14*4882a593Smuzhiyun 	struct rcu_head		rcu;
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun 	u16			max_socks;	/* length of socks */
17*4882a593Smuzhiyun 	u16			num_socks;	/* elements in socks */
18*4882a593Smuzhiyun 	/* The last synq overflow event timestamp of this
19*4882a593Smuzhiyun 	 * reuse->socks[] group.
20*4882a593Smuzhiyun 	 */
21*4882a593Smuzhiyun 	unsigned int		synq_overflow_ts;
22*4882a593Smuzhiyun 	/* ID stays the same even after the size of socks[] grows. */
23*4882a593Smuzhiyun 	unsigned int		reuseport_id;
24*4882a593Smuzhiyun 	unsigned int		bind_inany:1;
25*4882a593Smuzhiyun 	unsigned int		has_conns:1;
26*4882a593Smuzhiyun 	struct bpf_prog __rcu	*prog;		/* optional BPF sock selector */
27*4882a593Smuzhiyun 	struct sock		*socks[];	/* array of sock pointers */
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun extern int reuseport_alloc(struct sock *sk, bool bind_inany);
31*4882a593Smuzhiyun extern int reuseport_add_sock(struct sock *sk, struct sock *sk2,
32*4882a593Smuzhiyun 			      bool bind_inany);
33*4882a593Smuzhiyun extern void reuseport_detach_sock(struct sock *sk);
34*4882a593Smuzhiyun extern struct sock *reuseport_select_sock(struct sock *sk,
35*4882a593Smuzhiyun 					  u32 hash,
36*4882a593Smuzhiyun 					  struct sk_buff *skb,
37*4882a593Smuzhiyun 					  int hdr_len);
38*4882a593Smuzhiyun extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
39*4882a593Smuzhiyun extern int reuseport_detach_prog(struct sock *sk);
40*4882a593Smuzhiyun 
reuseport_has_conns(struct sock * sk)41*4882a593Smuzhiyun static inline bool reuseport_has_conns(struct sock *sk)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	struct sock_reuseport *reuse;
44*4882a593Smuzhiyun 	bool ret = false;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	rcu_read_lock();
47*4882a593Smuzhiyun 	reuse = rcu_dereference(sk->sk_reuseport_cb);
48*4882a593Smuzhiyun 	if (reuse && reuse->has_conns)
49*4882a593Smuzhiyun 		ret = true;
50*4882a593Smuzhiyun 	rcu_read_unlock();
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	return ret;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun void reuseport_has_conns_set(struct sock *sk);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun #endif  /* _SOCK_REUSEPORT_H */
58