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