sock_reuseport.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * To speed up listener socket lookup, create an array to store all sockets
  3. * listening on the same port. This allows a decision to be made after finding
  4. * the first socket. An optional BPF program can also be configured for
  5. * selecting the socket index from the array of available sockets.
  6. */
  7. #include <net/sock_reuseport.h>
  8. #include <linux/bpf.h>
  9. #include <linux/rcupdate.h>
  10. #define INIT_SOCKS 128
  11. static DEFINE_SPINLOCK(reuseport_lock);
  12. static struct sock_reuseport *__reuseport_alloc(u16 max_socks)
  13. {
  14. size_t size = sizeof(struct sock_reuseport) +
  15. sizeof(struct sock *) * max_socks;
  16. struct sock_reuseport *reuse = kzalloc(size, GFP_ATOMIC);
  17. if (!reuse)
  18. return NULL;
  19. reuse->max_socks = max_socks;
  20. RCU_INIT_POINTER(reuse->prog, NULL);
  21. return reuse;
  22. }
  23. int reuseport_alloc(struct sock *sk)
  24. {
  25. struct sock_reuseport *reuse;
  26. /* bh lock used since this function call may precede hlist lock in
  27. * soft irq of receive path or setsockopt from process context
  28. */
  29. spin_lock_bh(&reuseport_lock);
  30. /* Allocation attempts can occur concurrently via the setsockopt path
  31. * and the bind/hash path. Nothing to do when we lose the race.
  32. */
  33. if (rcu_dereference_protected(sk->sk_reuseport_cb,
  34. lockdep_is_held(&reuseport_lock)))
  35. goto out;
  36. reuse = __reuseport_alloc(INIT_SOCKS);
  37. if (!reuse) {
  38. spin_unlock_bh(&reuseport_lock);
  39. return -ENOMEM;
  40. }
  41. reuse->socks[0] = sk;
  42. reuse->num_socks = 1;
  43. rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
  44. out:
  45. spin_unlock_bh(&reuseport_lock);
  46. return 0;
  47. }
  48. EXPORT_SYMBOL(reuseport_alloc);
  49. static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse)
  50. {
  51. struct sock_reuseport *more_reuse;
  52. u32 more_socks_size, i;
  53. more_socks_size = reuse->max_socks * 2U;
  54. if (more_socks_size > U16_MAX)
  55. return NULL;
  56. more_reuse = __reuseport_alloc(more_socks_size);
  57. if (!more_reuse)
  58. return NULL;
  59. more_reuse->max_socks = more_socks_size;
  60. more_reuse->num_socks = reuse->num_socks;
  61. more_reuse->prog = reuse->prog;
  62. memcpy(more_reuse->socks, reuse->socks,
  63. reuse->num_socks * sizeof(struct sock *));
  64. for (i = 0; i < reuse->num_socks; ++i)
  65. rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
  66. more_reuse);
  67. /* Note: we use kfree_rcu here instead of reuseport_free_rcu so
  68. * that reuse and more_reuse can temporarily share a reference
  69. * to prog.
  70. */
  71. kfree_rcu(reuse, rcu);
  72. return more_reuse;
  73. }
  74. static void reuseport_free_rcu(struct rcu_head *head)
  75. {
  76. struct sock_reuseport *reuse;
  77. reuse = container_of(head, struct sock_reuseport, rcu);
  78. if (reuse->prog)
  79. bpf_prog_destroy(reuse->prog);
  80. kfree(reuse);
  81. }
  82. /**
  83. * reuseport_add_sock - Add a socket to the reuseport group of another.
  84. * @sk: New socket to add to the group.
  85. * @sk2: Socket belonging to the existing reuseport group.
  86. * May return ENOMEM and not add socket to group under memory pressure.
  87. */
  88. int reuseport_add_sock(struct sock *sk, struct sock *sk2)
  89. {
  90. struct sock_reuseport *old_reuse, *reuse;
  91. if (!rcu_access_pointer(sk2->sk_reuseport_cb)) {
  92. int err = reuseport_alloc(sk2);
  93. if (err)
  94. return err;
  95. }
  96. spin_lock_bh(&reuseport_lock);
  97. reuse = rcu_dereference_protected(sk2->sk_reuseport_cb,
  98. lockdep_is_held(&reuseport_lock));
  99. old_reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
  100. lockdep_is_held(&reuseport_lock));
  101. if (old_reuse && old_reuse->num_socks != 1) {
  102. spin_unlock_bh(&reuseport_lock);
  103. return -EBUSY;
  104. }
  105. if (reuse->num_socks == reuse->max_socks) {
  106. reuse = reuseport_grow(reuse);
  107. if (!reuse) {
  108. spin_unlock_bh(&reuseport_lock);
  109. return -ENOMEM;
  110. }
  111. }
  112. reuse->socks[reuse->num_socks] = sk;
  113. /* paired with smp_rmb() in reuseport_select_sock() */
  114. smp_wmb();
  115. reuse->num_socks++;
  116. rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
  117. spin_unlock_bh(&reuseport_lock);
  118. if (old_reuse)
  119. call_rcu(&old_reuse->rcu, reuseport_free_rcu);
  120. return 0;
  121. }
  122. void reuseport_detach_sock(struct sock *sk)
  123. {
  124. struct sock_reuseport *reuse;
  125. int i;
  126. spin_lock_bh(&reuseport_lock);
  127. reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
  128. lockdep_is_held(&reuseport_lock));
  129. rcu_assign_pointer(sk->sk_reuseport_cb, NULL);
  130. for (i = 0; i < reuse->num_socks; i++) {
  131. if (reuse->socks[i] == sk) {
  132. reuse->socks[i] = reuse->socks[reuse->num_socks - 1];
  133. reuse->num_socks--;
  134. if (reuse->num_socks == 0)
  135. call_rcu(&reuse->rcu, reuseport_free_rcu);
  136. break;
  137. }
  138. }
  139. spin_unlock_bh(&reuseport_lock);
  140. }
  141. EXPORT_SYMBOL(reuseport_detach_sock);
  142. static struct sock *run_bpf(struct sock_reuseport *reuse, u16 socks,
  143. struct bpf_prog *prog, struct sk_buff *skb,
  144. int hdr_len)
  145. {
  146. struct sk_buff *nskb = NULL;
  147. u32 index;
  148. if (skb_shared(skb)) {
  149. nskb = skb_clone(skb, GFP_ATOMIC);
  150. if (!nskb)
  151. return NULL;
  152. skb = nskb;
  153. }
  154. /* temporarily advance data past protocol header */
  155. if (!pskb_pull(skb, hdr_len)) {
  156. kfree_skb(nskb);
  157. return NULL;
  158. }
  159. index = bpf_prog_run_save_cb(prog, skb);
  160. __skb_push(skb, hdr_len);
  161. consume_skb(nskb);
  162. if (index >= socks)
  163. return NULL;
  164. return reuse->socks[index];
  165. }
  166. /**
  167. * reuseport_select_sock - Select a socket from an SO_REUSEPORT group.
  168. * @sk: First socket in the group.
  169. * @hash: When no BPF filter is available, use this hash to select.
  170. * @skb: skb to run through BPF filter.
  171. * @hdr_len: BPF filter expects skb data pointer at payload data. If
  172. * the skb does not yet point at the payload, this parameter represents
  173. * how far the pointer needs to advance to reach the payload.
  174. * Returns a socket that should receive the packet (or NULL on error).
  175. */
  176. struct sock *reuseport_select_sock(struct sock *sk,
  177. u32 hash,
  178. struct sk_buff *skb,
  179. int hdr_len)
  180. {
  181. struct sock_reuseport *reuse;
  182. struct bpf_prog *prog;
  183. struct sock *sk2 = NULL;
  184. u16 socks;
  185. rcu_read_lock();
  186. reuse = rcu_dereference(sk->sk_reuseport_cb);
  187. /* if memory allocation failed or add call is not yet complete */
  188. if (!reuse)
  189. goto out;
  190. prog = rcu_dereference(reuse->prog);
  191. socks = READ_ONCE(reuse->num_socks);
  192. if (likely(socks)) {
  193. /* paired with smp_wmb() in reuseport_add_sock() */
  194. smp_rmb();
  195. if (prog && skb)
  196. sk2 = run_bpf(reuse, socks, prog, skb, hdr_len);
  197. else
  198. sk2 = reuse->socks[reciprocal_scale(hash, socks)];
  199. }
  200. out:
  201. rcu_read_unlock();
  202. return sk2;
  203. }
  204. EXPORT_SYMBOL(reuseport_select_sock);
  205. struct bpf_prog *
  206. reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog)
  207. {
  208. struct sock_reuseport *reuse;
  209. struct bpf_prog *old_prog;
  210. spin_lock_bh(&reuseport_lock);
  211. reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
  212. lockdep_is_held(&reuseport_lock));
  213. old_prog = rcu_dereference_protected(reuse->prog,
  214. lockdep_is_held(&reuseport_lock));
  215. rcu_assign_pointer(reuse->prog, prog);
  216. spin_unlock_bh(&reuseport_lock);
  217. return old_prog;
  218. }
  219. EXPORT_SYMBOL(reuseport_attach_prog);