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 static struct sock_reuseport *__reuseport_alloc(unsigned int max_socks)
23 unsigned int size = sizeof(struct sock_reuseport) +
24 sizeof(struct sock *) * max_socks;
25 struct sock_reuseport *reuse = kzalloc(size, GFP_ATOMIC);
30 reuse->max_socks = max_socks;
32 RCU_INIT_POINTER(reuse->prog, NULL);
36 int reuseport_alloc(struct sock *sk, bool bind_inany)
38 struct sock_reuseport *reuse;
41 /* bh lock used since this function call may precede hlist lock in
42 * soft irq of receive path or setsockopt from process context
44 spin_lock_bh(&reuseport_lock);
46 /* Allocation attempts can occur concurrently via the setsockopt path
47 * and the bind/hash path. Nothing to do when we lose the race.
49 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
50 lockdep_is_held(&reuseport_lock));
52 /* Only set reuse->bind_inany if the bind_inany is true.
53 * Otherwise, it will overwrite the reuse->bind_inany
54 * which was set by the bind/hash path.
57 reuse->bind_inany = bind_inany;
61 reuse = __reuseport_alloc(INIT_SOCKS);
67 id = ida_alloc(&reuseport_ida, GFP_ATOMIC);
74 reuse->reuseport_id = id;
77 reuse->bind_inany = bind_inany;
78 rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
81 spin_unlock_bh(&reuseport_lock);
85 EXPORT_SYMBOL(reuseport_alloc);
87 static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse)
89 struct sock_reuseport *more_reuse;
90 u32 more_socks_size, i;
92 more_socks_size = reuse->max_socks * 2U;
93 if (more_socks_size > U16_MAX)
96 more_reuse = __reuseport_alloc(more_socks_size);
100 more_reuse->num_socks = reuse->num_socks;
101 more_reuse->prog = reuse->prog;
102 more_reuse->reuseport_id = reuse->reuseport_id;
103 more_reuse->bind_inany = reuse->bind_inany;
105 memcpy(more_reuse->socks, reuse->socks,
106 reuse->num_socks * sizeof(struct sock *));
107 more_reuse->synq_overflow_ts = READ_ONCE(reuse->synq_overflow_ts);
109 for (i = 0; i < reuse->num_socks; ++i)
110 rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
113 /* Note: we use kfree_rcu here instead of reuseport_free_rcu so
114 * that reuse and more_reuse can temporarily share a reference
117 kfree_rcu(reuse, rcu);
121 static void reuseport_free_rcu(struct rcu_head *head)
123 struct sock_reuseport *reuse;
125 reuse = container_of(head, struct sock_reuseport, rcu);
126 sk_reuseport_prog_free(rcu_dereference_protected(reuse->prog, 1));
127 ida_free(&reuseport_ida, reuse->reuseport_id);
132 * reuseport_add_sock - Add a socket to the reuseport group of another.
133 * @sk: New socket to add to the group.
134 * @sk2: Socket belonging to the existing reuseport group.
135 * @bind_inany: Whether or not the group is bound to a local INANY address.
137 * May return ENOMEM and not add socket to group under memory pressure.
139 int reuseport_add_sock(struct sock *sk, struct sock *sk2, bool bind_inany)
141 struct sock_reuseport *old_reuse, *reuse;
143 if (!rcu_access_pointer(sk2->sk_reuseport_cb)) {
144 int err = reuseport_alloc(sk2, bind_inany);
150 spin_lock_bh(&reuseport_lock);
151 reuse = rcu_dereference_protected(sk2->sk_reuseport_cb,
152 lockdep_is_held(&reuseport_lock));
153 old_reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
154 lockdep_is_held(&reuseport_lock));
155 if (old_reuse && old_reuse->num_socks != 1) {
156 spin_unlock_bh(&reuseport_lock);
160 if (reuse->num_socks == reuse->max_socks) {
161 reuse = reuseport_grow(reuse);
163 spin_unlock_bh(&reuseport_lock);
168 reuse->socks[reuse->num_socks] = sk;
169 /* paired with smp_rmb() in reuseport_select_sock() */
172 rcu_assign_pointer(sk->sk_reuseport_cb, reuse);
174 spin_unlock_bh(&reuseport_lock);
177 call_rcu(&old_reuse->rcu, reuseport_free_rcu);
180 EXPORT_SYMBOL(reuseport_add_sock);
182 void reuseport_detach_sock(struct sock *sk)
184 struct sock_reuseport *reuse;
187 spin_lock_bh(&reuseport_lock);
188 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
189 lockdep_is_held(&reuseport_lock));
191 /* Notify the bpf side. The sk may be added to a sockarray
192 * map. If so, sockarray logic will remove it from the map.
194 * Other bpf map types that work with reuseport, like sockmap,
195 * don't need an explicit callback from here. They override sk
196 * unhash/close ops to remove the sk from the map before we
199 bpf_sk_reuseport_detach(sk);
201 rcu_assign_pointer(sk->sk_reuseport_cb, NULL);
203 for (i = 0; i < reuse->num_socks; i++) {
204 if (reuse->socks[i] == sk) {
205 reuse->socks[i] = reuse->socks[reuse->num_socks - 1];
207 if (reuse->num_socks == 0)
208 call_rcu(&reuse->rcu, reuseport_free_rcu);
212 spin_unlock_bh(&reuseport_lock);
214 EXPORT_SYMBOL(reuseport_detach_sock);
216 static struct sock *run_bpf_filter(struct sock_reuseport *reuse, u16 socks,
217 struct bpf_prog *prog, struct sk_buff *skb,
220 struct sk_buff *nskb = NULL;
223 if (skb_shared(skb)) {
224 nskb = skb_clone(skb, GFP_ATOMIC);
230 /* temporarily advance data past protocol header */
231 if (!pskb_pull(skb, hdr_len)) {
235 index = bpf_prog_run_save_cb(prog, skb);
236 __skb_push(skb, hdr_len);
243 return reuse->socks[index];
247 * reuseport_select_sock - Select a socket from an SO_REUSEPORT group.
248 * @sk: First socket in the group.
249 * @hash: When no BPF filter is available, use this hash to select.
250 * @skb: skb to run through BPF filter.
251 * @hdr_len: BPF filter expects skb data pointer at payload data. If
252 * the skb does not yet point at the payload, this parameter represents
253 * how far the pointer needs to advance to reach the payload.
254 * Returns a socket that should receive the packet (or NULL on error).
256 struct sock *reuseport_select_sock(struct sock *sk,
261 struct sock_reuseport *reuse;
262 struct bpf_prog *prog;
263 struct sock *sk2 = NULL;
267 reuse = rcu_dereference(sk->sk_reuseport_cb);
269 /* if memory allocation failed or add call is not yet complete */
273 prog = rcu_dereference(reuse->prog);
274 socks = READ_ONCE(reuse->num_socks);
276 /* paired with smp_wmb() in reuseport_add_sock() */
282 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
283 sk2 = bpf_run_sk_reuseport(reuse, sk, prog, skb, hash);
285 sk2 = run_bpf_filter(reuse, socks, prog, skb, hdr_len);
288 /* no bpf or invalid bpf result: fall back to hash usage */
292 i = j = reciprocal_scale(hash, socks);
293 while (reuse->socks[i]->sk_state == TCP_ESTABLISHED) {
295 if (i >= reuse->num_socks)
300 sk2 = reuse->socks[i];
308 EXPORT_SYMBOL(reuseport_select_sock);
310 int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog)
312 struct sock_reuseport *reuse;
313 struct bpf_prog *old_prog;
315 if (sk_unhashed(sk) && sk->sk_reuseport) {
316 int err = reuseport_alloc(sk, false);
320 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
321 /* The socket wasn't bound with SO_REUSEPORT */
325 spin_lock_bh(&reuseport_lock);
326 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
327 lockdep_is_held(&reuseport_lock));
328 old_prog = rcu_dereference_protected(reuse->prog,
329 lockdep_is_held(&reuseport_lock));
330 rcu_assign_pointer(reuse->prog, prog);
331 spin_unlock_bh(&reuseport_lock);
333 sk_reuseport_prog_free(old_prog);
336 EXPORT_SYMBOL(reuseport_attach_prog);
338 int reuseport_detach_prog(struct sock *sk)
340 struct sock_reuseport *reuse;
341 struct bpf_prog *old_prog;
343 if (!rcu_access_pointer(sk->sk_reuseport_cb))
344 return sk->sk_reuseport ? -ENOENT : -EINVAL;
347 spin_lock_bh(&reuseport_lock);
348 reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
349 lockdep_is_held(&reuseport_lock));
350 old_prog = rcu_replace_pointer(reuse->prog, old_prog,
351 lockdep_is_held(&reuseport_lock));
352 spin_unlock_bh(&reuseport_lock);
357 sk_reuseport_prog_free(old_prog);
360 EXPORT_SYMBOL(reuseport_detach_prog);