sock_reuseport.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * To speed up listener socket lookup, create an array to store all sockets
  4. * listening on the same port. This allows a decision to be made after finding
  5. * the first socket. An optional BPF program can also be configured for
  6. * selecting the socket index from the array of available sockets.
  7. */
  8. #include <net/sock_reuseport.h>
  9. #include <linux/bpf.h>
  10. #include <linux/idr.h>
  11. #include <linux/filter.h>
  12. #include <linux/rcupdate.h>
  13. #define INIT_SOCKS 128
  14. DEFINE_SPINLOCK(reuseport_lock);
  15. #define REUSEPORT_MIN_ID 1
  16. static DEFINE_IDA(reuseport_ida);
  17. int reuseport_get_id(struct sock_reuseport *reuse)
  18. {
  19. int id;
  20. if (reuse->reuseport_id)
  21. return reuse->reuseport_id;
  22. id = ida_simple_get(&reuseport_ida, REUSEPORT_MIN_ID, 0,
  23. /* Called under reuseport_lock */
  24. GFP_ATOMIC);
  25. if (id < 0)
  26. return id;
  27. reuse->reuseport_id = id;
  28. return reuse->reuseport_id;
  29. }
  30. static struct sock_reuseport *__reuseport_alloc(unsigned int max_socks)
  31. {
  32. unsigned int size = sizeof(struct sock_reuseport) +
  33. sizeof(struct sock *) * max_socks;
  34. struct sock_reuseport *reuse = kzalloc(size, GFP_ATOMIC);
  35. if (!reuse)
  36. return NULL;
  37. reuse->max_socks = max_socks;
  38. RCU_INIT_POINTER(reuse->prog, NULL);
  39. return reuse;
  40. }
  41. int reuseport_alloc(struct sock *sk, bool bind_inany)
  42. {
  43. struct sock_reuseport *reuse;
  44. /* bh lock used since this function call may precede hlist lock in
  45. * soft irq of receive path or setsockopt from process context
  46. */
  47. spin_lock_bh(&reuseport_lock);
  48. /* Allocation attempts can occur concurrently via the setsockopt path
  49. * and the bind/hash path. Nothing to do when we lose the race.
  50. */
  51. reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
  52. lockdep_is_held(&reuseport_lock));
  53. if (reuse) {
  54. /* Only set reuse->bind_inany if the bind_inany is true.
  55. * Otherwise, it will overwrite the reuse->bind_inany
  56. * which was set by the bind/hash path.
  57. */
  58. if (bind_inany)
  59. reuse->bind_inany = bind_inany;
  60. goto out;
  61. }
  62. reuse = __reuseport_alloc(INIT_SOCKS);
  63. if (!reuse) {
  64. spin_unlock_bh(&reuseport_lock);
  65. return -ENOMEM;
  66. }
  67. reuse->socks[0] = sk;
  68. reuse->num_socks = 1;
  69. reuse->bind_inany = bind_inany;
  70. rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
  71. out:
  72. spin_unlock_bh(&reuseport_lock);
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(reuseport_alloc);
  76. static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse)
  77. {
  78. struct sock_reuseport *more_reuse;
  79. u32 more_socks_size, i;
  80. more_socks_size = reuse->max_socks * 2U;
  81. if (more_socks_size > U16_MAX)
  82. return NULL;
  83. more_reuse = __reuseport_alloc(more_socks_size);
  84. if (!more_reuse)
  85. return NULL;
  86. more_reuse->max_socks = more_socks_size;
  87. more_reuse->num_socks = reuse->num_socks;
  88. more_reuse->prog = reuse->prog;
  89. more_reuse->reuseport_id = reuse->reuseport_id;
  90. more_reuse->bind_inany = reuse->bind_inany;
  91. memcpy(more_reuse->socks, reuse->socks,
  92. reuse->num_socks * sizeof(struct sock *));
  93. more_reuse->synq_overflow_ts = READ_ONCE(reuse->synq_overflow_ts);
  94. for (i = 0; i < reuse->num_socks; ++i)
  95. rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
  96. more_reuse);
  97. /* Note: we use kfree_rcu here instead of reuseport_free_rcu so
  98. * that reuse and more_reuse can temporarily share a reference
  99. * to prog.
  100. */
  101. kfree_rcu(reuse, rcu);
  102. return more_reuse;
  103. }
  104. static void reuseport_free_rcu(struct rcu_head *head)
  105. {
  106. struct sock_reuseport *reuse;
  107. reuse = container_of(head, struct sock_reuseport, rcu);
  108. sk_reuseport_prog_free(rcu_dereference_protected(reuse->prog, 1));
  109. if (reuse->reuseport_id)
  110. ida_simple_remove(&reuseport_ida, reuse->reuseport_id);
  111. kfree(reuse);
  112. }
  113. /**
  114. * reuseport_add_sock - Add a socket to the reuseport group of another.
  115. * @sk: New socket to add to the group.
  116. * @sk2: Socket belonging to the existing reuseport group.
  117. * May return ENOMEM and not add socket to group under memory pressure.
  118. */
  119. int reuseport_add_sock(struct sock *sk, struct sock *sk2, bool bind_inany)
  120. {
  121. struct sock_reuseport *old_reuse, *reuse;
  122. if (!rcu_access_pointer(sk2->sk_reuseport_cb)) {
  123. int err = reuseport_alloc(sk2, bind_inany);
  124. if (err)
  125. return err;
  126. }
  127. spin_lock_bh(&reuseport_lock);
  128. reuse = rcu_dereference_protected(sk2->sk_reuseport_cb,
  129. lockdep_is_held(&reuseport_lock));
  130. old_reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
  131. lockdep_is_held(&reuseport_lock));
  132. if (old_reuse && old_reuse->num_socks != 1) {
  133. spin_unlock_bh(&reuseport_lock);
  134. return -EBUSY;
  135. }
  136. if (reuse->num_socks == reuse->max_socks) {
  137. reuse = reuseport_grow(reuse);
  138. if (!reuse) {
  139. spin_unlock_bh(&reuseport_lock);
  140. return -ENOMEM;
  141. }
  142. }
  143. reuse->socks[reuse->num_socks] = sk;
  144. /* paired with smp_rmb() in reuseport_select_sock() */
  145. smp_wmb();
  146. reuse->num_socks++;
  147. rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
  148. spin_unlock_bh(&reuseport_lock);
  149. if (old_reuse)
  150. call_rcu(&old_reuse->rcu, reuseport_free_rcu);
  151. return 0;
  152. }
  153. void reuseport_detach_sock(struct sock *sk)
  154. {
  155. struct sock_reuseport *reuse;
  156. int i;
  157. spin_lock_bh(&reuseport_lock);
  158. reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
  159. lockdep_is_held(&reuseport_lock));
  160. /* At least one of the sk in this reuseport group is added to
  161. * a bpf map. Notify the bpf side. The bpf map logic will
  162. * remove the sk if it is indeed added to a bpf map.
  163. */
  164. if (reuse->reuseport_id)
  165. bpf_sk_reuseport_detach(sk);
  166. rcu_assign_pointer(sk->sk_reuseport_cb, NULL);
  167. for (i = 0; i < reuse->num_socks; i++) {
  168. if (reuse->socks[i] == sk) {
  169. reuse->socks[i] = reuse->socks[reuse->num_socks - 1];
  170. reuse->num_socks--;
  171. if (reuse->num_socks == 0)
  172. call_rcu(&reuse->rcu, reuseport_free_rcu);
  173. break;
  174. }
  175. }
  176. spin_unlock_bh(&reuseport_lock);
  177. }
  178. EXPORT_SYMBOL(reuseport_detach_sock);
  179. static struct sock *run_bpf_filter(struct sock_reuseport *reuse, u16 socks,
  180. struct bpf_prog *prog, struct sk_buff *skb,
  181. int hdr_len)
  182. {
  183. struct sk_buff *nskb = NULL;
  184. u32 index;
  185. if (skb_shared(skb)) {
  186. nskb = skb_clone(skb, GFP_ATOMIC);
  187. if (!nskb)
  188. return NULL;
  189. skb = nskb;
  190. }
  191. /* temporarily advance data past protocol header */
  192. if (!pskb_pull(skb, hdr_len)) {
  193. kfree_skb(nskb);
  194. return NULL;
  195. }
  196. index = bpf_prog_run_save_cb(prog, skb);
  197. __skb_push(skb, hdr_len);
  198. consume_skb(nskb);
  199. if (index >= socks)
  200. return NULL;
  201. return reuse->socks[index];
  202. }
  203. /**
  204. * reuseport_select_sock - Select a socket from an SO_REUSEPORT group.
  205. * @sk: First socket in the group.
  206. * @hash: When no BPF filter is available, use this hash to select.
  207. * @skb: skb to run through BPF filter.
  208. * @hdr_len: BPF filter expects skb data pointer at payload data. If
  209. * the skb does not yet point at the payload, this parameter represents
  210. * how far the pointer needs to advance to reach the payload.
  211. * Returns a socket that should receive the packet (or NULL on error).
  212. */
  213. struct sock *reuseport_select_sock(struct sock *sk,
  214. u32 hash,
  215. struct sk_buff *skb,
  216. int hdr_len)
  217. {
  218. struct sock_reuseport *reuse;
  219. struct bpf_prog *prog;
  220. struct sock *sk2 = NULL;
  221. u16 socks;
  222. rcu_read_lock();
  223. reuse = rcu_dereference(sk->sk_reuseport_cb);
  224. /* if memory allocation failed or add call is not yet complete */
  225. if (!reuse)
  226. goto out;
  227. prog = rcu_dereference(reuse->prog);
  228. socks = READ_ONCE(reuse->num_socks);
  229. if (likely(socks)) {
  230. /* paired with smp_wmb() in reuseport_add_sock() */
  231. smp_rmb();
  232. if (!prog || !skb)
  233. goto select_by_hash;
  234. if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
  235. sk2 = bpf_run_sk_reuseport(reuse, sk, prog, skb, hash);
  236. else
  237. sk2 = run_bpf_filter(reuse, socks, prog, skb, hdr_len);
  238. select_by_hash:
  239. /* no bpf or invalid bpf result: fall back to hash usage */
  240. if (!sk2)
  241. sk2 = reuse->socks[reciprocal_scale(hash, socks)];
  242. }
  243. out:
  244. rcu_read_unlock();
  245. return sk2;
  246. }
  247. EXPORT_SYMBOL(reuseport_select_sock);
  248. int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog)
  249. {
  250. struct sock_reuseport *reuse;
  251. struct bpf_prog *old_prog;
  252. if (sk_unhashed(sk) && sk->sk_reuseport) {
  253. int err = reuseport_alloc(sk, false);
  254. if (err)
  255. return err;
  256. } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
  257. /* The socket wasn't bound with SO_REUSEPORT */
  258. return -EINVAL;
  259. }
  260. spin_lock_bh(&reuseport_lock);
  261. reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
  262. lockdep_is_held(&reuseport_lock));
  263. old_prog = rcu_dereference_protected(reuse->prog,
  264. lockdep_is_held(&reuseport_lock));
  265. rcu_assign_pointer(reuse->prog, prog);
  266. spin_unlock_bh(&reuseport_lock);
  267. sk_reuseport_prog_free(old_prog);
  268. return 0;
  269. }
  270. EXPORT_SYMBOL(reuseport_attach_prog);