]> Git Repo - linux.git/blame - include/linux/skmsg.h
Merge tag 'sched_ext-for-6.14-rc2-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / include / linux / skmsg.h
CommitLineData
604326b4
DB
1/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
3
4#ifndef _LINUX_SKMSG_H
5#define _LINUX_SKMSG_H
6
7#include <linux/bpf.h>
8#include <linux/filter.h>
9#include <linux/scatterlist.h>
10#include <linux/skbuff.h>
11
12#include <net/sock.h>
13#include <net/tcp.h>
14#include <net/strparser.h>
15
16#define MAX_MSG_FRAGS MAX_SKB_FRAGS
031097d9 17#define NR_MSG_FRAG_IDS (MAX_MSG_FRAGS + 1)
604326b4
DB
18
19enum __sk_action {
20 __SK_DROP = 0,
21 __SK_PASS,
22 __SK_REDIRECT,
23 __SK_NONE,
24};
25
26struct sk_msg_sg {
27 u32 start;
28 u32 curr;
29 u32 end;
30 u32 size;
31 u32 copybreak;
5a8fb33e 32 DECLARE_BITMAP(copy, MAX_MSG_FRAGS + 2);
031097d9
JK
33 /* The extra two elements:
34 * 1) used for chaining the front and sections when the list becomes
35 * partitioned (e.g. end < start). The crypto APIs require the
36 * chaining;
37 * 2) to chain tailer SG entries after the message.
d3b18ad3 38 */
031097d9 39 struct scatterlist data[MAX_MSG_FRAGS + 2];
604326b4
DB
40};
41
7a69c0f2 42/* UAPI in filter.c depends on struct sk_msg_sg being first element. */
604326b4
DB
43struct sk_msg {
44 struct sk_msg_sg sg;
45 void *data;
46 void *data_end;
47 u32 apply_bytes;
48 u32 cork_bytes;
49 u32 flags;
50 struct sk_buff *skb;
51 struct sock *sk_redir;
52 struct sock *sk;
53 struct list_head list;
54};
55
56struct sk_psock_progs {
57 struct bpf_prog *msg_parser;
ae8b8332
CW
58 struct bpf_prog *stream_parser;
59 struct bpf_prog *stream_verdict;
a7ba4558 60 struct bpf_prog *skb_verdict;
699c23f0
YS
61 struct bpf_link *msg_parser_link;
62 struct bpf_link *stream_parser_link;
63 struct bpf_link *stream_verdict_link;
64 struct bpf_link *skb_verdict_link;
604326b4
DB
65};
66
67enum sk_psock_state_bits {
68 SK_PSOCK_TX_ENABLED,
809e4dc7 69 SK_PSOCK_RX_STRP_ENABLED,
604326b4
DB
70};
71
72struct sk_psock_link {
73 struct list_head list;
74 struct bpf_map *map;
75 void *link_raw;
76};
77
604326b4 78struct sk_psock_work_state {
604326b4
DB
79 u32 len;
80 u32 off;
81};
82
83struct sk_psock {
84 struct sock *sk;
85 struct sock *sk_redir;
86 u32 apply_bytes;
87 u32 cork_bytes;
88 u32 eval;
a351d608 89 bool redir_ingress; /* undefined if sk_redir is null */
604326b4
DB
90 struct sk_msg *cork;
91 struct sk_psock_progs progs;
5a685cd9
CW
92#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
93 struct strparser strp;
94#endif
604326b4
DB
95 struct sk_buff_head ingress_skb;
96 struct list_head ingress_msg;
b01fd6e8 97 spinlock_t ingress_lock;
604326b4
DB
98 unsigned long state;
99 struct list_head link;
100 spinlock_t link_lock;
101 refcount_t refcnt;
102 void (*saved_unhash)(struct sock *sk);
d8616ee2 103 void (*saved_destroy)(struct sock *sk);
604326b4
DB
104 void (*saved_close)(struct sock *sk, long timeout);
105 void (*saved_write_space)(struct sock *sk);
5a685cd9 106 void (*saved_data_ready)(struct sock *sk);
7865dfb1
JF
107 /* psock_update_sk_prot may be called with restore=false many times
108 * so the handler must be safe for this case. It will be called
109 * exactly once with restore=true when the psock is being destroyed
110 * and psock refcnt is zero, but before an RCU grace period.
111 */
51e0158a
CW
112 int (*psock_update_sk_prot)(struct sock *sk, struct sk_psock *psock,
113 bool restore);
604326b4 114 struct proto *sk_proto;
799aa7f9 115 struct mutex work_mutex;
604326b4 116 struct sk_psock_work_state work_state;
29173d07 117 struct delayed_work work;
8866730a 118 struct sock *sk_pair;
7786dfc4 119 struct rcu_work rwork;
604326b4
DB
120};
121
122int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
123 int elem_first_coalesce);
d829e9c4
DB
124int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
125 u32 off, u32 len);
604326b4
DB
126void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len);
127int sk_msg_free(struct sock *sk, struct sk_msg *msg);
128int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg);
129void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes);
130void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
131 u32 bytes);
132
133void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes);
d3b18ad3 134void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes);
604326b4
DB
135
136int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
137 struct sk_msg *msg, u32 bytes);
138int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
139 struct sk_msg *msg, u32 bytes);
2bc793e3
CW
140int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg,
141 int len, int flags);
fb4e0a5e 142bool sk_msg_is_readable(struct sock *sk);
604326b4
DB
143
144static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes)
145{
146 WARN_ON(i == msg->sg.end && bytes);
147}
148
149static inline void sk_msg_apply_bytes(struct sk_psock *psock, u32 bytes)
150{
151 if (psock->apply_bytes) {
152 if (psock->apply_bytes < bytes)
153 psock->apply_bytes = 0;
154 else
155 psock->apply_bytes -= bytes;
156 }
157}
158
683916f6
JK
159static inline u32 sk_msg_iter_dist(u32 start, u32 end)
160{
031097d9 161 return end >= start ? end - start : end + (NR_MSG_FRAG_IDS - start);
683916f6
JK
162}
163
604326b4
DB
164#define sk_msg_iter_var_prev(var) \
165 do { \
166 if (var == 0) \
031097d9 167 var = NR_MSG_FRAG_IDS - 1; \
604326b4
DB
168 else \
169 var--; \
170 } while (0)
171
172#define sk_msg_iter_var_next(var) \
173 do { \
174 var++; \
031097d9 175 if (var == NR_MSG_FRAG_IDS) \
604326b4
DB
176 var = 0; \
177 } while (0)
178
179#define sk_msg_iter_prev(msg, which) \
180 sk_msg_iter_var_prev(msg->sg.which)
181
182#define sk_msg_iter_next(msg, which) \
183 sk_msg_iter_var_next(msg->sg.which)
184
604326b4
DB
185static inline void sk_msg_init(struct sk_msg *msg)
186{
031097d9 187 BUILD_BUG_ON(ARRAY_SIZE(msg->sg.data) - 1 != NR_MSG_FRAG_IDS);
604326b4 188 memset(msg, 0, sizeof(*msg));
031097d9 189 sg_init_marker(msg->sg.data, NR_MSG_FRAG_IDS);
604326b4
DB
190}
191
192static inline void sk_msg_xfer(struct sk_msg *dst, struct sk_msg *src,
193 int which, u32 size)
194{
195 dst->sg.data[which] = src->sg.data[which];
196 dst->sg.data[which].length = size;
3f4c3127 197 dst->sg.size += size;
81aabbb9 198 src->sg.size -= size;
604326b4
DB
199 src->sg.data[which].length -= size;
200 src->sg.data[which].offset += size;
201}
202
d3b18ad3
JF
203static inline void sk_msg_xfer_full(struct sk_msg *dst, struct sk_msg *src)
204{
205 memcpy(dst, src, sizeof(*src));
206 sk_msg_init(src);
207}
208
8734a162
JF
209static inline bool sk_msg_full(const struct sk_msg *msg)
210{
031097d9 211 return sk_msg_iter_dist(msg->sg.start, msg->sg.end) == MAX_MSG_FRAGS;
8734a162
JF
212}
213
604326b4
DB
214static inline u32 sk_msg_elem_used(const struct sk_msg *msg)
215{
683916f6 216 return sk_msg_iter_dist(msg->sg.start, msg->sg.end);
604326b4
DB
217}
218
604326b4
DB
219static inline struct scatterlist *sk_msg_elem(struct sk_msg *msg, int which)
220{
221 return &msg->sg.data[which];
222}
223
6fff607e
JF
224static inline struct scatterlist sk_msg_elem_cpy(struct sk_msg *msg, int which)
225{
226 return msg->sg.data[which];
227}
228
604326b4
DB
229static inline struct page *sk_msg_page(struct sk_msg *msg, int which)
230{
231 return sg_page(sk_msg_elem(msg, which));
232}
233
234static inline bool sk_msg_to_ingress(const struct sk_msg *msg)
235{
236 return msg->flags & BPF_F_INGRESS;
237}
238
239static inline void sk_msg_compute_data_pointers(struct sk_msg *msg)
240{
241 struct scatterlist *sge = sk_msg_elem(msg, msg->sg.start);
242
5a8fb33e 243 if (test_bit(msg->sg.start, msg->sg.copy)) {
604326b4
DB
244 msg->data = NULL;
245 msg->data_end = NULL;
246 } else {
247 msg->data = sg_virt(sge);
248 msg->data_end = msg->data + sge->length;
249 }
250}
251
252static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page,
253 u32 len, u32 offset)
254{
255 struct scatterlist *sge;
256
257 get_page(page);
258 sge = sk_msg_elem(msg, msg->sg.end);
259 sg_set_page(sge, page, len, offset);
260 sg_unmark_end(sge);
261
5a8fb33e 262 __set_bit(msg->sg.end, msg->sg.copy);
604326b4
DB
263 msg->sg.size += len;
264 sk_msg_iter_next(msg, end);
265}
266
d3b18ad3
JF
267static inline void sk_msg_sg_copy(struct sk_msg *msg, u32 i, bool copy_state)
268{
269 do {
163ab96b 270 if (copy_state)
5a8fb33e 271 __set_bit(i, msg->sg.copy);
163ab96b 272 else
5a8fb33e 273 __clear_bit(i, msg->sg.copy);
d3b18ad3
JF
274 sk_msg_iter_var_next(i);
275 if (i == msg->sg.end)
276 break;
277 } while (1);
278}
279
280static inline void sk_msg_sg_copy_set(struct sk_msg *msg, u32 start)
281{
282 sk_msg_sg_copy(msg, start, true);
283}
284
285static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start)
286{
287 sk_msg_sg_copy(msg, start, false);
288}
289
604326b4
DB
290static inline struct sk_psock *sk_psock(const struct sock *sk)
291{
2a013372
HJ
292 return __rcu_dereference_sk_user_data_with_flags(sk,
293 SK_USER_DATA_PSOCK);
604326b4
DB
294}
295
9635720b
JF
296static inline void sk_psock_set_state(struct sk_psock *psock,
297 enum sk_psock_state_bits bit)
298{
299 set_bit(bit, &psock->state);
300}
301
302static inline void sk_psock_clear_state(struct sk_psock *psock,
303 enum sk_psock_state_bits bit)
304{
305 clear_bit(bit, &psock->state);
306}
307
308static inline bool sk_psock_test_state(const struct sk_psock *psock,
309 enum sk_psock_state_bits bit)
310{
311 return test_bit(bit, &psock->state);
312}
313
314static inline void sock_drop(struct sock *sk, struct sk_buff *skb)
315{
316 sk_drops_add(sk, skb);
317 kfree_skb(skb);
318}
319
d888b7af 320static inline bool sk_psock_queue_msg(struct sk_psock *psock,
604326b4
DB
321 struct sk_msg *msg)
322{
d888b7af
ZZ
323 bool ret;
324
b01fd6e8 325 spin_lock_bh(&psock->ingress_lock);
d888b7af 326 if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
9635720b 327 list_add_tail(&msg->list, &psock->ingress_msg);
d888b7af
ZZ
328 ret = true;
329 } else {
938d3480
WY
330 sk_msg_free(psock->sk, msg);
331 kfree(msg);
d888b7af 332 ret = false;
938d3480 333 }
b01fd6e8 334 spin_unlock_bh(&psock->ingress_lock);
d888b7af 335 return ret;
b01fd6e8
CW
336}
337
338static inline struct sk_msg *sk_psock_dequeue_msg(struct sk_psock *psock)
339{
340 struct sk_msg *msg;
341
342 spin_lock_bh(&psock->ingress_lock);
343 msg = list_first_entry_or_null(&psock->ingress_msg, struct sk_msg, list);
344 if (msg)
345 list_del(&msg->list);
346 spin_unlock_bh(&psock->ingress_lock);
347 return msg;
348}
349
350static inline struct sk_msg *sk_psock_peek_msg(struct sk_psock *psock)
351{
352 struct sk_msg *msg;
353
354 spin_lock_bh(&psock->ingress_lock);
355 msg = list_first_entry_or_null(&psock->ingress_msg, struct sk_msg, list);
356 spin_unlock_bh(&psock->ingress_lock);
357 return msg;
358}
359
360static inline struct sk_msg *sk_psock_next_msg(struct sk_psock *psock,
361 struct sk_msg *msg)
362{
363 struct sk_msg *ret;
364
365 spin_lock_bh(&psock->ingress_lock);
366 if (list_is_last(&msg->list, &psock->ingress_msg))
367 ret = NULL;
368 else
369 ret = list_next_entry(msg, list);
370 spin_unlock_bh(&psock->ingress_lock);
371 return ret;
604326b4
DB
372}
373
d3b18ad3
JF
374static inline bool sk_psock_queue_empty(const struct sk_psock *psock)
375{
376 return psock ? list_empty(&psock->ingress_msg) : true;
377}
378
b01fd6e8
CW
379static inline void kfree_sk_msg(struct sk_msg *msg)
380{
381 if (msg->skb)
382 consume_skb(msg->skb);
383 kfree(msg);
384}
385
604326b4
DB
386static inline void sk_psock_report_error(struct sk_psock *psock, int err)
387{
388 struct sock *sk = psock->sk;
389
390 sk->sk_err = err;
e3ae2365 391 sk_error_report(sk);
604326b4
DB
392}
393
394struct sk_psock *sk_psock_init(struct sock *sk, int node);
8bbabb3f 395void sk_psock_stop(struct sk_psock *psock);
604326b4 396
88759609 397#if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
604326b4
DB
398int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
399void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock);
400void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock);
88759609
CW
401#else
402static inline int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)
403{
404 return -EOPNOTSUPP;
405}
406
407static inline void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
408{
409}
410
411static inline void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
412{
413}
414#endif
415
ef565928
JF
416void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock);
417void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock);
604326b4
DB
418
419int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
420 struct sk_msg *msg);
421
3b0ba54d
SB
422/*
423 * This specialized allocator has to be a macro for its allocations to be
424 * accounted separately (to have a separate alloc_tag). The typecast is
425 * intentional to enforce typesafety.
426 */
2c321f3f
SB
427#define sk_psock_init_link() \
428 ((struct sk_psock_link *)kzalloc(sizeof(struct sk_psock_link), \
429 GFP_ATOMIC | __GFP_NOWARN))
604326b4
DB
430
431static inline void sk_psock_free_link(struct sk_psock_link *link)
432{
433 kfree(link);
434}
435
436struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock);
604326b4 437
604326b4
DB
438static inline void sk_psock_cork_free(struct sk_psock *psock)
439{
440 if (psock->cork) {
441 sk_msg_free(psock->sk, psock->cork);
442 kfree(psock->cork);
443 psock->cork = NULL;
444 }
445}
446
604326b4
DB
447static inline void sk_psock_restore_proto(struct sock *sk,
448 struct sk_psock *psock)
449{
8a59f9d1 450 if (psock->psock_update_sk_prot)
51e0158a 451 psock->psock_update_sk_prot(sk, psock, true);
604326b4
DB
452}
453
604326b4
DB
454static inline struct sk_psock *sk_psock_get(struct sock *sk)
455{
456 struct sk_psock *psock;
457
458 rcu_read_lock();
459 psock = sk_psock(sk);
460 if (psock && !refcount_inc_not_zero(&psock->refcnt))
461 psock = NULL;
462 rcu_read_unlock();
463 return psock;
464}
465
604326b4
DB
466void sk_psock_drop(struct sock *sk, struct sk_psock *psock);
467
468static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock)
469{
470 if (refcount_dec_and_test(&psock->refcnt))
471 sk_psock_drop(sk, psock);
472}
473
552de910
JF
474static inline void sk_psock_data_ready(struct sock *sk, struct sk_psock *psock)
475{
6648e613 476 read_lock_bh(&sk->sk_callback_lock);
5a685cd9
CW
477 if (psock->saved_data_ready)
478 psock->saved_data_ready(sk);
552de910
JF
479 else
480 sk->sk_data_ready(sk);
6648e613 481 read_unlock_bh(&sk->sk_callback_lock);
552de910
JF
482}
483
604326b4
DB
484static inline void psock_set_prog(struct bpf_prog **pprog,
485 struct bpf_prog *prog)
486{
487 prog = xchg(pprog, prog);
488 if (prog)
489 bpf_prog_put(prog);
490}
491
bb0de313
LB
492static inline int psock_replace_prog(struct bpf_prog **pprog,
493 struct bpf_prog *prog,
494 struct bpf_prog *old)
495{
496 if (cmpxchg(pprog, old, prog) != old)
497 return -ENOENT;
498
499 if (old)
500 bpf_prog_put(old);
501
502 return 0;
503}
504
604326b4
DB
505static inline void psock_progs_drop(struct sk_psock_progs *progs)
506{
507 psock_set_prog(&progs->msg_parser, NULL);
ae8b8332
CW
508 psock_set_prog(&progs->stream_parser, NULL);
509 psock_set_prog(&progs->stream_verdict, NULL);
a7ba4558 510 psock_set_prog(&progs->skb_verdict, NULL);
604326b4
DB
511}
512
e91de6af
JF
513int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb);
514
515static inline bool sk_psock_strp_enabled(struct sk_psock *psock)
516{
517 if (!psock)
518 return false;
5a685cd9 519 return !!psock->saved_data_ready;
e91de6af 520}
e3526bb9
CW
521
522#if IS_ENABLED(CONFIG_NET_SOCK_MSG)
523
7303524e
LJ
524#define BPF_F_STRPARSER (1UL << 1)
525
526/* We only have two bits so far. */
527#define BPF_F_PTR_MASK ~(BPF_F_INGRESS | BPF_F_STRPARSER)
528
529static inline bool skb_bpf_strparser(const struct sk_buff *skb)
530{
531 unsigned long sk_redir = skb->_sk_redir;
532
533 return sk_redir & BPF_F_STRPARSER;
534}
535
536static inline void skb_bpf_set_strparser(struct sk_buff *skb)
537{
538 skb->_sk_redir |= BPF_F_STRPARSER;
539}
e3526bb9
CW
540
541static inline bool skb_bpf_ingress(const struct sk_buff *skb)
542{
543 unsigned long sk_redir = skb->_sk_redir;
544
545 return sk_redir & BPF_F_INGRESS;
546}
547
548static inline void skb_bpf_set_ingress(struct sk_buff *skb)
549{
550 skb->_sk_redir |= BPF_F_INGRESS;
551}
552
553static inline void skb_bpf_set_redir(struct sk_buff *skb, struct sock *sk_redir,
554 bool ingress)
555{
556 skb->_sk_redir = (unsigned long)sk_redir;
557 if (ingress)
558 skb->_sk_redir |= BPF_F_INGRESS;
559}
560
561static inline struct sock *skb_bpf_redirect_fetch(const struct sk_buff *skb)
562{
563 unsigned long sk_redir = skb->_sk_redir;
564
565 return (struct sock *)(sk_redir & BPF_F_PTR_MASK);
566}
567
568static inline void skb_bpf_redirect_clear(struct sk_buff *skb)
569{
570 skb->_sk_redir = 0;
571}
572#endif /* CONFIG_NET_SOCK_MSG */
604326b4 573#endif /* _LINUX_SKMSG_H */
This page took 1.337023 seconds and 4 git commands to generate.