blob: 505f1e18e9bf96ceb3a660729ef6e5b01bb41c2a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Craig Gallekef456142016-01-04 17:41:45 -05002#ifndef _SOCK_REUSEPORT_H
3#define _SOCK_REUSEPORT_H
4
Craig Gallek538950a2016-01-04 17:41:47 -05005#include <linux/filter.h>
6#include <linux/skbuff.h>
Craig Gallekef456142016-01-04 17:41:45 -05007#include <linux/types.h>
Martin KaFai Lau736b4602018-08-08 01:01:22 -07008#include <linux/spinlock.h>
Craig Gallekef456142016-01-04 17:41:45 -05009#include <net/sock.h>
10
Martin KaFai Lau736b4602018-08-08 01:01:22 -070011extern spinlock_t reuseport_lock;
12
Craig Gallekef456142016-01-04 17:41:45 -050013struct sock_reuseport {
14 struct rcu_head rcu;
15
16 u16 max_socks; /* length of socks */
17 u16 num_socks; /* elements in socks */
Martin KaFai Lau40a12272018-08-08 01:01:21 -070018 /* The last synq overflow event timestamp of this
19 * reuse->socks[] group.
20 */
21 unsigned int synq_overflow_ts;
Martin KaFai Lau736b4602018-08-08 01:01:22 -070022 /* ID stays the same even after the size of socks[] grows. */
23 unsigned int reuseport_id;
Willem de Bruijnacdcecc2019-09-12 21:16:39 -040024 unsigned int bind_inany:1;
25 unsigned int has_conns:1;
Craig Gallek538950a2016-01-04 17:41:47 -050026 struct bpf_prog __rcu *prog; /* optional BPF sock selector */
Gustavo A. R. Silva2603c292020-02-28 18:11:02 -060027 struct sock *socks[]; /* array of sock pointers */
Craig Gallekef456142016-01-04 17:41:45 -050028};
29
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -070030extern int reuseport_alloc(struct sock *sk, bool bind_inany);
31extern int reuseport_add_sock(struct sock *sk, struct sock *sk2,
32 bool bind_inany);
Craig Gallekef456142016-01-04 17:41:45 -050033extern void reuseport_detach_sock(struct sock *sk);
Craig Gallek538950a2016-01-04 17:41:47 -050034extern struct sock *reuseport_select_sock(struct sock *sk,
35 u32 hash,
36 struct sk_buff *skb,
37 int hdr_len);
Martin KaFai Lau8217ca62018-08-08 01:01:26 -070038extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
Martin KaFai Lau99f3a062019-06-13 15:00:01 -070039extern int reuseport_detach_prog(struct sock *sk);
40
Willem de Bruijnacdcecc2019-09-12 21:16:39 -040041static inline bool reuseport_has_conns(struct sock *sk, bool set)
42{
43 struct sock_reuseport *reuse;
44 bool ret = false;
45
46 rcu_read_lock();
47 reuse = rcu_dereference(sk->sk_reuseport_cb);
48 if (reuse) {
49 if (set)
50 reuse->has_conns = 1;
51 ret = reuse->has_conns;
52 }
53 rcu_read_unlock();
54
55 return ret;
56}
57
Craig Gallekef456142016-01-04 17:41:45 -050058#endif /* _SOCK_REUSEPORT_H */