1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
4 #include <linux/skmsg.h>
5 #include <linux/skbuff.h>
6 #include <linux/scatterlist.h>
12 static bool sk_msg_try_coalesce_ok(struct sk_msg *msg, int elem_first_coalesce)
14 if (msg->sg.end > msg->sg.start &&
15 elem_first_coalesce < msg->sg.end)
18 if (msg->sg.end < msg->sg.start &&
19 (elem_first_coalesce > msg->sg.start ||
20 elem_first_coalesce < msg->sg.end))
26 int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
27 int elem_first_coalesce)
29 struct page_frag *pfrag = sk_page_frag(sk);
34 struct scatterlist *sge;
38 if (!sk_page_frag_refill(sk, pfrag))
41 orig_offset = pfrag->offset;
42 use = min_t(int, len, pfrag->size - orig_offset);
43 if (!sk_wmem_schedule(sk, use))
47 sk_msg_iter_var_prev(i);
48 sge = &msg->sg.data[i];
50 if (sk_msg_try_coalesce_ok(msg, elem_first_coalesce) &&
51 sg_page(sge) == pfrag->page &&
52 sge->offset + sge->length == orig_offset) {
55 if (sk_msg_full(msg)) {
60 sge = &msg->sg.data[msg->sg.end];
62 sg_set_page(sge, pfrag->page, use, orig_offset);
63 get_page(pfrag->page);
64 sk_msg_iter_next(msg, end);
67 sk_mem_charge(sk, use);
75 EXPORT_SYMBOL_GPL(sk_msg_alloc);
77 int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
80 int i = src->sg.start;
81 struct scatterlist *sge = sk_msg_elem(src, i);
82 struct scatterlist *sgd = NULL;
86 if (sge->length > off)
89 sk_msg_iter_var_next(i);
90 if (i == src->sg.end && off)
92 sge = sk_msg_elem(src, i);
96 sge_len = sge->length - off;
101 sgd = sk_msg_elem(dst, dst->sg.end - 1);
104 (sg_page(sge) == sg_page(sgd)) &&
105 (sg_virt(sge) + off == sg_virt(sgd) + sgd->length)) {
106 sgd->length += sge_len;
107 dst->sg.size += sge_len;
108 } else if (!sk_msg_full(dst)) {
109 sge_off = sge->offset + off;
110 sk_msg_page_add(dst, sg_page(sge), sge_len, sge_off);
117 sk_mem_charge(sk, sge_len);
118 sk_msg_iter_var_next(i);
119 if (i == src->sg.end && len)
121 sge = sk_msg_elem(src, i);
126 EXPORT_SYMBOL_GPL(sk_msg_clone);
128 void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes)
130 int i = msg->sg.start;
133 struct scatterlist *sge = sk_msg_elem(msg, i);
135 if (bytes < sge->length) {
136 sge->length -= bytes;
137 sge->offset += bytes;
138 sk_mem_uncharge(sk, bytes);
142 sk_mem_uncharge(sk, sge->length);
143 bytes -= sge->length;
146 sk_msg_iter_var_next(i);
147 } while (bytes && i != msg->sg.end);
150 EXPORT_SYMBOL_GPL(sk_msg_return_zero);
152 void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes)
154 int i = msg->sg.start;
157 struct scatterlist *sge = &msg->sg.data[i];
158 int uncharge = (bytes < sge->length) ? bytes : sge->length;
160 sk_mem_uncharge(sk, uncharge);
162 sk_msg_iter_var_next(i);
163 } while (i != msg->sg.end);
165 EXPORT_SYMBOL_GPL(sk_msg_return);
167 static int sk_msg_free_elem(struct sock *sk, struct sk_msg *msg, u32 i,
170 struct scatterlist *sge = sk_msg_elem(msg, i);
171 u32 len = sge->length;
173 /* When the skb owns the memory we free it from consume_skb path. */
176 sk_mem_uncharge(sk, len);
177 put_page(sg_page(sge));
179 memset(sge, 0, sizeof(*sge));
183 static int __sk_msg_free(struct sock *sk, struct sk_msg *msg, u32 i,
186 struct scatterlist *sge = sk_msg_elem(msg, i);
189 while (msg->sg.size) {
190 msg->sg.size -= sge->length;
191 freed += sk_msg_free_elem(sk, msg, i, charge);
192 sk_msg_iter_var_next(i);
193 sk_msg_check_to_free(msg, i, msg->sg.size);
194 sge = sk_msg_elem(msg, i);
196 consume_skb(msg->skb);
201 int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg)
203 return __sk_msg_free(sk, msg, msg->sg.start, false);
205 EXPORT_SYMBOL_GPL(sk_msg_free_nocharge);
207 int sk_msg_free(struct sock *sk, struct sk_msg *msg)
209 return __sk_msg_free(sk, msg, msg->sg.start, true);
211 EXPORT_SYMBOL_GPL(sk_msg_free);
213 static void __sk_msg_free_partial(struct sock *sk, struct sk_msg *msg,
214 u32 bytes, bool charge)
216 struct scatterlist *sge;
217 u32 i = msg->sg.start;
220 sge = sk_msg_elem(msg, i);
223 if (bytes < sge->length) {
225 sk_mem_uncharge(sk, bytes);
226 sge->length -= bytes;
227 sge->offset += bytes;
228 msg->sg.size -= bytes;
232 msg->sg.size -= sge->length;
233 bytes -= sge->length;
234 sk_msg_free_elem(sk, msg, i, charge);
235 sk_msg_iter_var_next(i);
236 sk_msg_check_to_free(msg, i, bytes);
241 void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes)
243 __sk_msg_free_partial(sk, msg, bytes, true);
245 EXPORT_SYMBOL_GPL(sk_msg_free_partial);
247 void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
250 __sk_msg_free_partial(sk, msg, bytes, false);
253 void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len)
255 int trim = msg->sg.size - len;
263 sk_msg_iter_var_prev(i);
265 while (msg->sg.data[i].length &&
266 trim >= msg->sg.data[i].length) {
267 trim -= msg->sg.data[i].length;
268 sk_msg_free_elem(sk, msg, i, true);
269 sk_msg_iter_var_prev(i);
274 msg->sg.data[i].length -= trim;
275 sk_mem_uncharge(sk, trim);
276 /* Adjust copybreak if it falls into the trimmed part of last buf */
277 if (msg->sg.curr == i && msg->sg.copybreak > msg->sg.data[i].length)
278 msg->sg.copybreak = msg->sg.data[i].length;
280 sk_msg_iter_var_next(i);
283 /* If we trim data a full sg elem before curr pointer update
284 * copybreak and current so that any future copy operations
285 * start at new copy location.
286 * However trimed data that has not yet been used in a copy op
287 * does not require an update.
290 msg->sg.curr = msg->sg.start;
291 msg->sg.copybreak = 0;
292 } else if (sk_msg_iter_dist(msg->sg.start, msg->sg.curr) >=
293 sk_msg_iter_dist(msg->sg.start, msg->sg.end)) {
294 sk_msg_iter_var_prev(i);
296 msg->sg.copybreak = msg->sg.data[i].length;
299 EXPORT_SYMBOL_GPL(sk_msg_trim);
301 int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
302 struct sk_msg *msg, u32 bytes)
304 int i, maxpages, ret = 0, num_elems = sk_msg_elem_used(msg);
305 const int to_max_pages = MAX_MSG_FRAGS;
306 struct page *pages[MAX_MSG_FRAGS];
307 ssize_t orig, copied, use, offset;
312 maxpages = to_max_pages - num_elems;
318 copied = iov_iter_get_pages(from, pages, bytes, maxpages,
325 iov_iter_advance(from, copied);
327 msg->sg.size += copied;
330 use = min_t(int, copied, PAGE_SIZE - offset);
331 sg_set_page(&msg->sg.data[msg->sg.end],
332 pages[i], use, offset);
333 sg_unmark_end(&msg->sg.data[msg->sg.end]);
334 sk_mem_charge(sk, use);
338 sk_msg_iter_next(msg, end);
342 /* When zerocopy is mixed with sk_msg_*copy* operations we
343 * may have a copybreak set in this case clear and prefer
344 * zerocopy remainder when possible.
346 msg->sg.copybreak = 0;
347 msg->sg.curr = msg->sg.end;
350 /* Revert iov_iter updates, msg will need to use 'trim' later if it
351 * also needs to be cleared.
354 iov_iter_revert(from, msg->sg.size - orig);
357 EXPORT_SYMBOL_GPL(sk_msg_zerocopy_from_iter);
359 int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
360 struct sk_msg *msg, u32 bytes)
362 int ret = -ENOSPC, i = msg->sg.curr;
363 struct scatterlist *sge;
368 sge = sk_msg_elem(msg, i);
369 /* This is possible if a trim operation shrunk the buffer */
370 if (msg->sg.copybreak >= sge->length) {
371 msg->sg.copybreak = 0;
372 sk_msg_iter_var_next(i);
373 if (i == msg->sg.end)
375 sge = sk_msg_elem(msg, i);
378 buf_size = sge->length - msg->sg.copybreak;
379 copy = (buf_size > bytes) ? bytes : buf_size;
380 to = sg_virt(sge) + msg->sg.copybreak;
381 msg->sg.copybreak += copy;
382 if (sk->sk_route_caps & NETIF_F_NOCACHE_COPY)
383 ret = copy_from_iter_nocache(to, copy, from);
385 ret = copy_from_iter(to, copy, from);
393 msg->sg.copybreak = 0;
394 sk_msg_iter_var_next(i);
395 } while (i != msg->sg.end);
400 EXPORT_SYMBOL_GPL(sk_msg_memcopy_from_iter);
402 /* Receive sk_msg from psock->ingress_msg to @msg. */
403 int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg,
406 struct iov_iter *iter = &msg->msg_iter;
407 int peek = flags & MSG_PEEK;
408 struct sk_msg *msg_rx;
411 msg_rx = sk_psock_peek_msg(psock);
412 while (copied != len) {
413 struct scatterlist *sge;
415 if (unlikely(!msg_rx))
418 i = msg_rx->sg.start;
423 sge = sk_msg_elem(msg_rx, i);
426 if (copied + copy > len)
428 copy = copy_page_to_iter(page, sge->offset, copy, iter);
430 return copied ? copied : -EFAULT;
437 sk_mem_uncharge(sk, copy);
438 msg_rx->sg.size -= copy;
441 sk_msg_iter_var_next(i);
446 /* Lets not optimize peek case if copy_page_to_iter
447 * didn't copy the entire length lets just break.
449 if (copy != sge->length)
451 sk_msg_iter_var_next(i);
456 } while (i != msg_rx->sg.end);
458 if (unlikely(peek)) {
459 msg_rx = sk_psock_next_msg(psock, msg_rx);
465 msg_rx->sg.start = i;
466 if (!sge->length && msg_rx->sg.start == msg_rx->sg.end) {
467 msg_rx = sk_psock_dequeue_msg(psock);
468 kfree_sk_msg(msg_rx);
470 msg_rx = sk_psock_peek_msg(psock);
475 EXPORT_SYMBOL_GPL(sk_msg_recvmsg);
477 static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk,
482 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
485 if (!sk_rmem_schedule(sk, skb, skb->truesize))
488 msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_KERNEL);
496 static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
497 struct sk_psock *psock,
503 /* skb linearize may fail with ENOMEM, but lets simply try again
504 * later if this happens. Under memory pressure we don't want to
505 * drop the skb. We need to linearize the skb so that the mapping
506 * in skb_to_sgvec can not error.
508 if (skb_linearize(skb))
510 num_sge = skb_to_sgvec(skb, msg->sg.data, 0, skb->len);
511 if (unlikely(num_sge < 0))
516 msg->sg.size = copied;
517 msg->sg.end = num_sge;
520 sk_psock_queue_msg(psock, msg);
521 sk_psock_data_ready(sk, psock);
525 static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb);
527 static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb)
529 struct sock *sk = psock->sk;
533 /* If we are receiving on the same sock skb->sk is already assigned,
534 * skip memory accounting and owner transition seeing it already set
537 if (unlikely(skb->sk == sk))
538 return sk_psock_skb_ingress_self(psock, skb);
539 msg = sk_psock_create_ingress_msg(sk, skb);
543 /* This will transition ownership of the data from the socket where
544 * the BPF program was run initiating the redirect to the socket
545 * we will eventually receive this data on. The data will be released
546 * from skb_consume found in __tcp_bpf_recvmsg() after its been copied
549 skb_set_owner_r(skb, sk);
550 err = sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
556 /* Puts an skb on the ingress queue of the socket already assigned to the
557 * skb. In this case we do not need to check memory limits or skb_set_owner_r
558 * because the skb is already accounted for here.
560 static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb)
562 struct sk_msg *msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_ATOMIC);
563 struct sock *sk = psock->sk;
569 skb_set_owner_r(skb, sk);
570 err = sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
576 static int sk_psock_handle_skb(struct sk_psock *psock, struct sk_buff *skb,
577 u32 off, u32 len, bool ingress)
580 if (!sock_writeable(psock->sk))
582 return skb_send_sock(psock->sk, skb, off, len);
584 return sk_psock_skb_ingress(psock, skb);
587 static void sk_psock_skb_state(struct sk_psock *psock,
588 struct sk_psock_work_state *state,
592 spin_lock_bh(&psock->ingress_lock);
593 if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
598 sock_drop(psock->sk, skb);
600 spin_unlock_bh(&psock->ingress_lock);
603 static void sk_psock_backlog(struct work_struct *work)
605 struct sk_psock *psock = container_of(work, struct sk_psock, work);
606 struct sk_psock_work_state *state = &psock->work_state;
607 struct sk_buff *skb = NULL;
612 mutex_lock(&psock->work_mutex);
613 if (unlikely(state->skb)) {
614 spin_lock_bh(&psock->ingress_lock);
619 spin_unlock_bh(&psock->ingress_lock);
624 while ((skb = skb_dequeue(&psock->ingress_skb))) {
628 ingress = skb_bpf_ingress(skb);
629 skb_bpf_redirect_clear(skb);
632 if (!sock_flag(psock->sk, SOCK_DEAD))
633 ret = sk_psock_handle_skb(psock, skb, off,
636 if (ret == -EAGAIN) {
637 sk_psock_skb_state(psock, state, skb,
641 /* Hard errors break pipe and stop xmit. */
642 sk_psock_report_error(psock, ret ? -ret : EPIPE);
643 sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
644 sock_drop(psock->sk, skb);
655 mutex_unlock(&psock->work_mutex);
658 struct sk_psock *sk_psock_init(struct sock *sk, int node)
660 struct sk_psock *psock;
663 write_lock_bh(&sk->sk_callback_lock);
665 if (sk->sk_user_data) {
666 psock = ERR_PTR(-EBUSY);
670 psock = kzalloc_node(sizeof(*psock), GFP_ATOMIC | __GFP_NOWARN, node);
672 psock = ERR_PTR(-ENOMEM);
676 prot = READ_ONCE(sk->sk_prot);
678 psock->eval = __SK_NONE;
679 psock->sk_proto = prot;
680 psock->saved_unhash = prot->unhash;
681 psock->saved_close = prot->close;
682 psock->saved_write_space = sk->sk_write_space;
684 INIT_LIST_HEAD(&psock->link);
685 spin_lock_init(&psock->link_lock);
687 INIT_WORK(&psock->work, sk_psock_backlog);
688 mutex_init(&psock->work_mutex);
689 INIT_LIST_HEAD(&psock->ingress_msg);
690 spin_lock_init(&psock->ingress_lock);
691 skb_queue_head_init(&psock->ingress_skb);
693 sk_psock_set_state(psock, SK_PSOCK_TX_ENABLED);
694 refcount_set(&psock->refcnt, 1);
696 rcu_assign_sk_user_data_nocopy(sk, psock);
700 write_unlock_bh(&sk->sk_callback_lock);
703 EXPORT_SYMBOL_GPL(sk_psock_init);
705 struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock)
707 struct sk_psock_link *link;
709 spin_lock_bh(&psock->link_lock);
710 link = list_first_entry_or_null(&psock->link, struct sk_psock_link,
713 list_del(&link->list);
714 spin_unlock_bh(&psock->link_lock);
718 static void __sk_psock_purge_ingress_msg(struct sk_psock *psock)
720 struct sk_msg *msg, *tmp;
722 list_for_each_entry_safe(msg, tmp, &psock->ingress_msg, list) {
723 list_del(&msg->list);
724 sk_msg_free(psock->sk, msg);
729 static void __sk_psock_zap_ingress(struct sk_psock *psock)
733 while ((skb = skb_dequeue(&psock->ingress_skb)) != NULL) {
734 skb_bpf_redirect_clear(skb);
735 sock_drop(psock->sk, skb);
737 kfree_skb(psock->work_state.skb);
738 /* We null the skb here to ensure that calls to sk_psock_backlog
739 * do not pick up the free'd skb.
741 psock->work_state.skb = NULL;
742 __sk_psock_purge_ingress_msg(psock);
745 static void sk_psock_link_destroy(struct sk_psock *psock)
747 struct sk_psock_link *link, *tmp;
749 list_for_each_entry_safe(link, tmp, &psock->link, list) {
750 list_del(&link->list);
751 sk_psock_free_link(link);
755 void sk_psock_stop(struct sk_psock *psock, bool wait)
757 spin_lock_bh(&psock->ingress_lock);
758 sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
759 sk_psock_cork_free(psock);
760 __sk_psock_zap_ingress(psock);
761 spin_unlock_bh(&psock->ingress_lock);
764 cancel_work_sync(&psock->work);
767 static void sk_psock_done_strp(struct sk_psock *psock);
769 static void sk_psock_destroy(struct work_struct *work)
771 struct sk_psock *psock = container_of(to_rcu_work(work),
772 struct sk_psock, rwork);
773 /* No sk_callback_lock since already detached. */
775 sk_psock_done_strp(psock);
777 cancel_work_sync(&psock->work);
778 mutex_destroy(&psock->work_mutex);
780 psock_progs_drop(&psock->progs);
782 sk_psock_link_destroy(psock);
783 sk_psock_cork_free(psock);
786 sock_put(psock->sk_redir);
791 void sk_psock_drop(struct sock *sk, struct sk_psock *psock)
793 write_lock_bh(&sk->sk_callback_lock);
794 sk_psock_restore_proto(sk, psock);
795 rcu_assign_sk_user_data(sk, NULL);
796 if (psock->progs.stream_parser)
797 sk_psock_stop_strp(sk, psock);
798 else if (psock->progs.stream_verdict || psock->progs.skb_verdict)
799 sk_psock_stop_verdict(sk, psock);
800 write_unlock_bh(&sk->sk_callback_lock);
802 sk_psock_stop(psock, false);
804 INIT_RCU_WORK(&psock->rwork, sk_psock_destroy);
805 queue_rcu_work(system_wq, &psock->rwork);
807 EXPORT_SYMBOL_GPL(sk_psock_drop);
809 static int sk_psock_map_verd(int verdict, bool redir)
813 return redir ? __SK_REDIRECT : __SK_PASS;
822 int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
825 struct bpf_prog *prog;
829 prog = READ_ONCE(psock->progs.msg_parser);
830 if (unlikely(!prog)) {
835 sk_msg_compute_data_pointers(msg);
837 ret = bpf_prog_run_pin_on_cpu(prog, msg);
838 ret = sk_psock_map_verd(ret, msg->sk_redir);
839 psock->apply_bytes = msg->apply_bytes;
840 if (ret == __SK_REDIRECT) {
842 sock_put(psock->sk_redir);
843 psock->sk_redir = msg->sk_redir;
844 if (!psock->sk_redir) {
848 sock_hold(psock->sk_redir);
854 EXPORT_SYMBOL_GPL(sk_psock_msg_verdict);
856 static int sk_psock_skb_redirect(struct sk_psock *from, struct sk_buff *skb)
858 struct sk_psock *psock_other;
859 struct sock *sk_other;
861 sk_other = skb_bpf_redirect_fetch(skb);
862 /* This error is a buggy BPF program, it returned a redirect
863 * return code, but then didn't set a redirect interface.
865 if (unlikely(!sk_other)) {
866 sock_drop(from->sk, skb);
869 psock_other = sk_psock(sk_other);
870 /* This error indicates the socket is being torn down or had another
871 * error that caused the pipe to break. We can't send a packet on
872 * a socket that is in this state so we drop the skb.
874 if (!psock_other || sock_flag(sk_other, SOCK_DEAD)) {
875 skb_bpf_redirect_clear(skb);
876 sock_drop(from->sk, skb);
879 spin_lock_bh(&psock_other->ingress_lock);
880 if (!sk_psock_test_state(psock_other, SK_PSOCK_TX_ENABLED)) {
881 spin_unlock_bh(&psock_other->ingress_lock);
882 skb_bpf_redirect_clear(skb);
883 sock_drop(from->sk, skb);
887 skb_queue_tail(&psock_other->ingress_skb, skb);
888 schedule_work(&psock_other->work);
889 spin_unlock_bh(&psock_other->ingress_lock);
893 static void sk_psock_tls_verdict_apply(struct sk_buff *skb,
894 struct sk_psock *from, int verdict)
898 sk_psock_skb_redirect(from, skb);
907 int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb)
909 struct bpf_prog *prog;
913 prog = READ_ONCE(psock->progs.stream_verdict);
917 skb_bpf_redirect_clear(skb);
918 ret = bpf_prog_run_pin_on_cpu(prog, skb);
919 ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
922 sk_psock_tls_verdict_apply(skb, psock, ret);
926 EXPORT_SYMBOL_GPL(sk_psock_tls_strp_read);
928 static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
931 struct sock *sk_other;
937 sk_other = psock->sk;
938 if (sock_flag(sk_other, SOCK_DEAD) ||
939 !sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
943 skb_bpf_set_ingress(skb);
945 /* If the queue is empty then we can submit directly
946 * into the msg queue. If its not empty we have to
947 * queue work otherwise we may get OOO data. Otherwise,
948 * if sk_psock_skb_ingress errors will be handled by
949 * retrying later from workqueue.
951 if (skb_queue_empty(&psock->ingress_skb)) {
952 err = sk_psock_skb_ingress_self(psock, skb);
955 spin_lock_bh(&psock->ingress_lock);
956 if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED)) {
957 skb_queue_tail(&psock->ingress_skb, skb);
958 schedule_work(&psock->work);
961 spin_unlock_bh(&psock->ingress_lock);
963 skb_bpf_redirect_clear(skb);
969 err = sk_psock_skb_redirect(psock, skb);
974 sock_drop(psock->sk, skb);
980 static void sk_psock_write_space(struct sock *sk)
982 struct sk_psock *psock;
983 void (*write_space)(struct sock *sk) = NULL;
986 psock = sk_psock(sk);
988 if (sk_psock_test_state(psock, SK_PSOCK_TX_ENABLED))
989 schedule_work(&psock->work);
990 write_space = psock->saved_write_space;
997 #if IS_ENABLED(CONFIG_BPF_STREAM_PARSER)
998 static void sk_psock_strp_read(struct strparser *strp, struct sk_buff *skb)
1000 struct sk_psock *psock;
1001 struct bpf_prog *prog;
1002 int ret = __SK_DROP;
1007 psock = sk_psock(sk);
1008 if (unlikely(!psock)) {
1012 prog = READ_ONCE(psock->progs.stream_verdict);
1016 skb_bpf_redirect_clear(skb);
1017 ret = bpf_prog_run_pin_on_cpu(prog, skb);
1018 ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
1021 sk_psock_verdict_apply(psock, skb, ret);
1026 static int sk_psock_strp_read_done(struct strparser *strp, int err)
1031 static int sk_psock_strp_parse(struct strparser *strp, struct sk_buff *skb)
1033 struct sk_psock *psock = container_of(strp, struct sk_psock, strp);
1034 struct bpf_prog *prog;
1038 prog = READ_ONCE(psock->progs.stream_parser);
1040 skb->sk = psock->sk;
1041 ret = bpf_prog_run_pin_on_cpu(prog, skb);
1048 /* Called with socket lock held. */
1049 static void sk_psock_strp_data_ready(struct sock *sk)
1051 struct sk_psock *psock;
1054 psock = sk_psock(sk);
1055 if (likely(psock)) {
1056 if (tls_sw_has_ctx_rx(sk)) {
1057 psock->saved_data_ready(sk);
1059 write_lock_bh(&sk->sk_callback_lock);
1060 strp_data_ready(&psock->strp);
1061 write_unlock_bh(&sk->sk_callback_lock);
1067 int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock)
1069 static const struct strp_callbacks cb = {
1070 .rcv_msg = sk_psock_strp_read,
1071 .read_sock_done = sk_psock_strp_read_done,
1072 .parse_msg = sk_psock_strp_parse,
1075 return strp_init(&psock->strp, sk, &cb);
1078 void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock)
1080 if (psock->saved_data_ready)
1083 psock->saved_data_ready = sk->sk_data_ready;
1084 sk->sk_data_ready = sk_psock_strp_data_ready;
1085 sk->sk_write_space = sk_psock_write_space;
1088 void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock)
1090 if (!psock->saved_data_ready)
1093 sk->sk_data_ready = psock->saved_data_ready;
1094 psock->saved_data_ready = NULL;
1095 strp_stop(&psock->strp);
1098 static void sk_psock_done_strp(struct sk_psock *psock)
1100 /* Parser has been stopped */
1101 if (psock->progs.stream_parser)
1102 strp_done(&psock->strp);
1105 static void sk_psock_done_strp(struct sk_psock *psock)
1108 #endif /* CONFIG_BPF_STREAM_PARSER */
1110 static int sk_psock_verdict_recv(read_descriptor_t *desc, struct sk_buff *skb,
1111 unsigned int offset, size_t orig_len)
1113 struct sock *sk = (struct sock *)desc->arg.data;
1114 struct sk_psock *psock;
1115 struct bpf_prog *prog;
1116 int ret = __SK_DROP;
1119 /* clone here so sk_eat_skb() in tcp_read_sock does not drop our data */
1120 skb = skb_clone(skb, GFP_ATOMIC);
1122 desc->error = -ENOMEM;
1127 psock = sk_psock(sk);
1128 if (unlikely(!psock)) {
1133 prog = READ_ONCE(psock->progs.stream_verdict);
1135 prog = READ_ONCE(psock->progs.skb_verdict);
1139 skb_bpf_redirect_clear(skb);
1140 ret = bpf_prog_run_pin_on_cpu(prog, skb);
1141 ret = sk_psock_map_verd(ret, skb_bpf_redirect_fetch(skb));
1144 if (sk_psock_verdict_apply(psock, skb, ret) < 0)
1151 static void sk_psock_verdict_data_ready(struct sock *sk)
1153 struct socket *sock = sk->sk_socket;
1154 read_descriptor_t desc;
1156 if (unlikely(!sock || !sock->ops || !sock->ops->read_sock))
1163 sock->ops->read_sock(sk, &desc, sk_psock_verdict_recv);
1166 void sk_psock_start_verdict(struct sock *sk, struct sk_psock *psock)
1168 if (psock->saved_data_ready)
1171 psock->saved_data_ready = sk->sk_data_ready;
1172 sk->sk_data_ready = sk_psock_verdict_data_ready;
1173 sk->sk_write_space = sk_psock_write_space;
1176 void sk_psock_stop_verdict(struct sock *sk, struct sk_psock *psock)
1178 if (!psock->saved_data_ready)
1181 sk->sk_data_ready = psock->saved_data_ready;
1182 psock->saved_data_ready = NULL;