sock_reuseport.h 1.5 KB

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