1 // SPDX-License-Identifier: GPL-2.0
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.
9 #include <net/sock_reuseport.h>
10 #include <linux/bpf.h>
11 #include <linux/idr.h>
12 #include <linux/filter.h>
13 #include <linux/rcupdate.h>
15 #define INIT_SOCKS 128
17 DEFINE_SPINLOCK(reuseport_lock);
19 static DEFINE_IDA(reuseport_ida);
21 void reuseport_has_conns_set(struct sock *sk)
23 struct sock_reuseport *reuse;
25 if (!rcu_access_pointer(sk->sk_reuseport_cb))
28 spin_lock_bh(&reuseport_lock);
29 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
30 lockdep_is_held(&reuseport_lock));
33 spin_unlock_bh(&reuseport_lock);
35 EXPORT_SYMBOL(reuseport_has_conns_set);
37 static struct sock_reuseport *__reuseport_alloc(unsigned int max_socks)
39 unsigned int size = sizeof(struct sock_reuseport) +
40 sizeof(struct sock *) * max_socks;
41 struct sock_reuseport *reuse = kzalloc(size, GFP_ATOMIC);
46 reuse->max_socks = max_socks;
48 RCU_INIT_POINTER(reuse->prog, NULL);
52 int reuseport_alloc(struct sock *sk, bool bind_inany)
54 struct sock_reuseport *reuse;
57 /* bh lock used since this function call may precede hlist lock in
58 * soft irq of receive path or setsockopt from process context
60 spin_lock_bh(&reuseport_lock);
62 /* Allocation attempts can occur concurrently via the setsockopt path
63 * and the bind/hash path. Nothing to do when we lose the race.
65 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
66 lockdep_is_held(&reuseport_lock));
68 /* Only set reuse->bind_inany if the bind_inany is true.
69 * Otherwise, it will overwrite the reuse->bind_inany
70 * which was set by the bind/hash path.
73 reuse->bind_inany = bind_inany;
77 reuse = __reuseport_alloc(INIT_SOCKS);
83 id = ida_alloc(&reuseport_ida, GFP_ATOMIC);
90 reuse->reuseport_id = id;
93 reuse->bind_inany = bind_inany;
94 rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
97 spin_unlock_bh(&reuseport_lock);
101 EXPORT_SYMBOL(reuseport_alloc);
103 static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse)
105 struct sock_reuseport *more_reuse;
106 u32 more_socks_size, i;
108 more_socks_size = reuse->max_socks * 2U;
109 if (more_socks_size > U16_MAX)
112 more_reuse = __reuseport_alloc(more_socks_size);
116 more_reuse->num_socks = reuse->num_socks;
117 more_reuse->prog = reuse->prog;
118 more_reuse->reuseport_id = reuse->reuseport_id;
119 more_reuse->bind_inany = reuse->bind_inany;
120 more_reuse->has_conns = reuse->has_conns;
122 memcpy(more_reuse->socks, reuse->socks,
123 reuse->num_socks * sizeof(struct sock *));
124 more_reuse->synq_overflow_ts = READ_ONCE(reuse->synq_overflow_ts);
126 for (i = 0; i < reuse->num_socks; ++i)
127 rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
130 /* Note: we use kfree_rcu here instead of reuseport_free_rcu so
131 * that reuse and more_reuse can temporarily share a reference
134 kfree_rcu(reuse, rcu);
138 static void reuseport_free_rcu(struct rcu_head *head)
140 struct sock_reuseport *reuse;
142 reuse = container_of(head, struct sock_reuseport, rcu);
143 sk_reuseport_prog_free(rcu_dereference_protected(reuse->prog, 1));
144 ida_free(&reuseport_ida, reuse->reuseport_id);
149 * reuseport_add_sock - Add a socket to the reuseport group of another.
150 * @sk: New socket to add to the group.
151 * @sk2: Socket belonging to the existing reuseport group.
152 * @bind_inany: Whether or not the group is bound to a local INANY address.
154 * May return ENOMEM and not add socket to group under memory pressure.
156 int reuseport_add_sock(struct sock *sk, struct sock *sk2, bool bind_inany)
158 struct sock_reuseport *old_reuse, *reuse;
160 if (!rcu_access_pointer(sk2->sk_reuseport_cb)) {
161 int err = reuseport_alloc(sk2, bind_inany);
167 spin_lock_bh(&reuseport_lock);
168 reuse = rcu_dereference_protected(sk2->sk_reuseport_cb,
169 lockdep_is_held(&reuseport_lock));
170 old_reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
171 lockdep_is_held(&reuseport_lock));
172 if (old_reuse && old_reuse->num_socks != 1) {
173 spin_unlock_bh(&reuseport_lock);
177 if (reuse->num_socks == reuse->max_socks) {
178 reuse = reuseport_grow(reuse);
180 spin_unlock_bh(&reuseport_lock);
185 reuse->socks[reuse->num_socks] = sk;
186 /* paired with smp_rmb() in reuseport_select_sock() */
189 rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
191 spin_unlock_bh(&reuseport_lock);
194 call_rcu(&old_reuse->rcu, reuseport_free_rcu);
197 EXPORT_SYMBOL(reuseport_add_sock);
199 void reuseport_detach_sock(struct sock *sk)
201 struct sock_reuseport *reuse;
204 spin_lock_bh(&reuseport_lock);
205 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
206 lockdep_is_held(&reuseport_lock));
208 /* Notify the bpf side. The sk may be added to a sockarray
209 * map. If so, sockarray logic will remove it from the map.
211 * Other bpf map types that work with reuseport, like sockmap,
212 * don't need an explicit callback from here. They override sk
213 * unhash/close ops to remove the sk from the map before we
216 bpf_sk_reuseport_detach(sk);
218 rcu_assign_pointer(sk->sk_reuseport_cb, NULL);
220 for (i = 0; i < reuse->num_socks; i++) {
221 if (reuse->socks[i] == sk) {
222 reuse->socks[i] = reuse->socks[reuse->num_socks - 1];
224 if (reuse->num_socks == 0)
225 call_rcu(&reuse->rcu, reuseport_free_rcu);
229 spin_unlock_bh(&reuseport_lock);
231 EXPORT_SYMBOL(reuseport_detach_sock);
233 static struct sock *run_bpf_filter(struct sock_reuseport *reuse, u16 socks,
234 struct bpf_prog *prog, struct sk_buff *skb,
237 struct sk_buff *nskb = NULL;
240 if (skb_shared(skb)) {
241 nskb = skb_clone(skb, GFP_ATOMIC);
247 /* temporarily advance data past protocol header */
248 if (!pskb_pull(skb, hdr_len)) {
252 index = bpf_prog_run_save_cb(prog, skb);
253 __skb_push(skb, hdr_len);
260 return reuse->socks[index];
264 * reuseport_select_sock - Select a socket from an SO_REUSEPORT group.
265 * @sk: First socket in the group.
266 * @hash: When no BPF filter is available, use this hash to select.
267 * @skb: skb to run through BPF filter.
268 * @hdr_len: BPF filter expects skb data pointer at payload data. If
269 * the skb does not yet point at the payload, this parameter represents
270 * how far the pointer needs to advance to reach the payload.
271 * Returns a socket that should receive the packet (or NULL on error).
273 struct sock *reuseport_select_sock(struct sock *sk,
278 struct sock_reuseport *reuse;
279 struct bpf_prog *prog;
280 struct sock *sk2 = NULL;
284 reuse = rcu_dereference(sk->sk_reuseport_cb);
286 /* if memory allocation failed or add call is not yet complete */
290 prog = rcu_dereference(reuse->prog);
291 socks = READ_ONCE(reuse->num_socks);
293 /* paired with smp_wmb() in reuseport_add_sock() */
299 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
300 sk2 = bpf_run_sk_reuseport(reuse, sk, prog, skb, hash);
302 sk2 = run_bpf_filter(reuse, socks, prog, skb, hdr_len);
305 /* no bpf or invalid bpf result: fall back to hash usage */
309 i = j = reciprocal_scale(hash, socks);
310 while (reuse->socks[i]->sk_state == TCP_ESTABLISHED) {
317 sk2 = reuse->socks[i];
325 EXPORT_SYMBOL(reuseport_select_sock);
327 int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog)
329 struct sock_reuseport *reuse;
330 struct bpf_prog *old_prog;
332 if (sk_unhashed(sk) && sk->sk_reuseport) {
333 int err = reuseport_alloc(sk, false);
337 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
338 /* The socket wasn't bound with SO_REUSEPORT */
342 spin_lock_bh(&reuseport_lock);
343 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
344 lockdep_is_held(&reuseport_lock));
345 old_prog = rcu_dereference_protected(reuse->prog,
346 lockdep_is_held(&reuseport_lock));
347 rcu_assign_pointer(reuse->prog, prog);
348 spin_unlock_bh(&reuseport_lock);
350 sk_reuseport_prog_free(old_prog);
353 EXPORT_SYMBOL(reuseport_attach_prog);
355 int reuseport_detach_prog(struct sock *sk)
357 struct sock_reuseport *reuse;
358 struct bpf_prog *old_prog;
360 if (!rcu_access_pointer(sk->sk_reuseport_cb))
361 return sk->sk_reuseport ? -ENOENT : -EINVAL;
364 spin_lock_bh(&reuseport_lock);
365 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
366 lockdep_is_held(&reuseport_lock));
367 old_prog = rcu_replace_pointer(reuse->prog, old_prog,
368 lockdep_is_held(&reuseport_lock));
369 spin_unlock_bh(&reuseport_lock);
374 sk_reuseport_prog_free(old_prog);
377 EXPORT_SYMBOL(reuseport_detach_prog);