1 // SPDX-License-Identifier: GPL-2.0-only
3 * common code for virtio vsock
5 * Copyright (C) 2013-2015 Red Hat, Inc.
9 #include <linux/spinlock.h>
10 #include <linux/module.h>
11 #include <linux/sched/signal.h>
12 #include <linux/ctype.h>
13 #include <linux/list.h>
14 #include <linux/virtio_vsock.h>
15 #include <uapi/linux/vsockmon.h>
18 #include <net/af_vsock.h>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/vsock_virtio_transport_common.h>
23 /* How long to wait for graceful shutdown of a connection */
24 #define VSOCK_CLOSE_TIMEOUT (8 * HZ)
26 /* Threshold for detecting small packets to copy */
27 #define GOOD_COPY_LEN 128
29 static const struct virtio_transport *
30 virtio_transport_get_ops(struct vsock_sock *vsk)
32 const struct vsock_transport *t = vsock_core_get_transport(vsk);
37 return container_of(t, struct virtio_transport, transport);
40 static bool virtio_transport_can_zcopy(const struct virtio_transport *t_ops,
41 struct virtio_vsock_pkt_info *info,
44 struct iov_iter *iov_iter;
49 iov_iter = &info->msg->msg_iter;
51 if (iov_iter->iov_offset)
54 /* We can't send whole iov. */
55 if (iov_iter->count > pkt_len)
58 /* Check that transport can send data in zerocopy mode. */
59 t_ops = virtio_transport_get_ops(info->vsk);
61 if (t_ops->can_msgzerocopy) {
62 int pages_to_send = iov_iter_npages(iov_iter, MAX_SKB_FRAGS);
64 /* +1 is for packet header. */
65 return t_ops->can_msgzerocopy(pages_to_send + 1);
71 static int virtio_transport_init_zcopy_skb(struct vsock_sock *vsk,
76 struct ubuf_info *uarg;
82 struct iov_iter *iter = &msg->msg_iter;
83 struct ubuf_info_msgzc *uarg_zc;
85 uarg = msg_zerocopy_realloc(sk_vsock(vsk),
91 uarg_zc = uarg_to_msgzc(uarg);
92 uarg_zc->zerocopy = zerocopy ? 1 : 0;
95 skb_zcopy_init(skb, uarg);
100 static int virtio_transport_fill_skb(struct sk_buff *skb,
101 struct virtio_vsock_pkt_info *info,
106 return __zerocopy_sg_from_iter(info->msg, NULL, skb,
107 &info->msg->msg_iter,
110 return memcpy_from_msg(skb_put(skb, len), info->msg, len);
113 static void virtio_transport_init_hdr(struct sk_buff *skb,
114 struct virtio_vsock_pkt_info *info,
121 struct virtio_vsock_hdr *hdr;
123 hdr = virtio_vsock_hdr(skb);
124 hdr->type = cpu_to_le16(info->type);
125 hdr->op = cpu_to_le16(info->op);
126 hdr->src_cid = cpu_to_le64(src_cid);
127 hdr->dst_cid = cpu_to_le64(dst_cid);
128 hdr->src_port = cpu_to_le32(src_port);
129 hdr->dst_port = cpu_to_le32(dst_port);
130 hdr->flags = cpu_to_le32(info->flags);
131 hdr->len = cpu_to_le32(payload_len);
132 hdr->buf_alloc = cpu_to_le32(0);
133 hdr->fwd_cnt = cpu_to_le32(0);
136 static void virtio_transport_copy_nonlinear_skb(const struct sk_buff *skb,
140 struct iov_iter iov_iter = { 0 };
147 iov_iter.iter_type = ITER_KVEC;
148 iov_iter.kvec = &kvec;
149 iov_iter.nr_segs = 1;
151 to_copy = min_t(size_t, len, skb->len);
153 skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset,
158 static struct sk_buff *virtio_transport_build_skb(void *opaque)
160 struct virtio_vsock_hdr *pkt_hdr;
161 struct sk_buff *pkt = opaque;
162 struct af_vsockmon_hdr *hdr;
166 /* A packet could be split to fit the RX buffer, so we can retrieve
167 * the payload length from the header and the buffer pointer taking
168 * care of the offset in the original packet.
170 pkt_hdr = virtio_vsock_hdr(pkt);
171 payload_len = pkt->len;
173 skb = alloc_skb(sizeof(*hdr) + sizeof(*pkt_hdr) + payload_len,
178 hdr = skb_put(skb, sizeof(*hdr));
180 /* pkt->hdr is little-endian so no need to byteswap here */
181 hdr->src_cid = pkt_hdr->src_cid;
182 hdr->src_port = pkt_hdr->src_port;
183 hdr->dst_cid = pkt_hdr->dst_cid;
184 hdr->dst_port = pkt_hdr->dst_port;
186 hdr->transport = cpu_to_le16(AF_VSOCK_TRANSPORT_VIRTIO);
187 hdr->len = cpu_to_le16(sizeof(*pkt_hdr));
188 memset(hdr->reserved, 0, sizeof(hdr->reserved));
190 switch (le16_to_cpu(pkt_hdr->op)) {
191 case VIRTIO_VSOCK_OP_REQUEST:
192 case VIRTIO_VSOCK_OP_RESPONSE:
193 hdr->op = cpu_to_le16(AF_VSOCK_OP_CONNECT);
195 case VIRTIO_VSOCK_OP_RST:
196 case VIRTIO_VSOCK_OP_SHUTDOWN:
197 hdr->op = cpu_to_le16(AF_VSOCK_OP_DISCONNECT);
199 case VIRTIO_VSOCK_OP_RW:
200 hdr->op = cpu_to_le16(AF_VSOCK_OP_PAYLOAD);
202 case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
203 case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
204 hdr->op = cpu_to_le16(AF_VSOCK_OP_CONTROL);
207 hdr->op = cpu_to_le16(AF_VSOCK_OP_UNKNOWN);
211 skb_put_data(skb, pkt_hdr, sizeof(*pkt_hdr));
214 if (skb_is_nonlinear(pkt)) {
215 void *data = skb_put(skb, payload_len);
217 virtio_transport_copy_nonlinear_skb(pkt, data, payload_len);
219 skb_put_data(skb, pkt->data, payload_len);
226 void virtio_transport_deliver_tap_pkt(struct sk_buff *skb)
228 if (virtio_vsock_skb_tap_delivered(skb))
231 vsock_deliver_tap(virtio_transport_build_skb, skb);
232 virtio_vsock_skb_set_tap_delivered(skb);
234 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt);
236 static u16 virtio_transport_get_type(struct sock *sk)
238 if (sk->sk_type == SOCK_STREAM)
239 return VIRTIO_VSOCK_TYPE_STREAM;
241 return VIRTIO_VSOCK_TYPE_SEQPACKET;
244 /* Returns new sk_buff on success, otherwise returns NULL. */
245 static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *info,
253 struct vsock_sock *vsk;
257 skb_len = VIRTIO_VSOCK_SKB_HEADROOM;
260 skb_len += payload_len;
262 skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
266 virtio_transport_init_hdr(skb, info, payload_len, src_cid, src_port,
271 /* If 'vsk' != NULL then payload is always present, so we
272 * will never call '__zerocopy_sg_from_iter()' below without
273 * setting skb owner in 'skb_set_owner_w()'. The only case
274 * when 'vsk' == NULL is VIRTIO_VSOCK_OP_RST control message
277 WARN_ON_ONCE(!(vsk && (info->msg && payload_len)) && zcopy);
279 /* Set owner here, because '__zerocopy_sg_from_iter()' uses
280 * owner of skb without check to update 'sk_wmem_alloc'.
283 skb_set_owner_w(skb, sk_vsock(vsk));
285 if (info->msg && payload_len > 0) {
288 err = virtio_transport_fill_skb(skb, info, payload_len, zcopy);
292 if (msg_data_left(info->msg) == 0 &&
293 info->type == VIRTIO_VSOCK_TYPE_SEQPACKET) {
294 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
296 hdr->flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOM);
298 if (info->msg->msg_flags & MSG_EOR)
299 hdr->flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOR);
304 virtio_vsock_skb_set_reply(skb);
306 trace_virtio_transport_alloc_pkt(src_cid, src_port,
320 /* This function can only be used on connecting/connected sockets,
321 * since a socket assigned to a transport is required.
323 * Do not use on listener sockets!
325 static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
326 struct virtio_vsock_pkt_info *info)
328 u32 max_skb_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
329 u32 src_cid, src_port, dst_cid, dst_port;
330 const struct virtio_transport *t_ops;
331 struct virtio_vsock_sock *vvs;
332 u32 pkt_len = info->pkt_len;
333 bool can_zcopy = false;
337 info->type = virtio_transport_get_type(sk_vsock(vsk));
339 t_ops = virtio_transport_get_ops(vsk);
340 if (unlikely(!t_ops))
343 src_cid = t_ops->transport.get_local_cid();
344 src_port = vsk->local_addr.svm_port;
345 if (!info->remote_cid) {
346 dst_cid = vsk->remote_addr.svm_cid;
347 dst_port = vsk->remote_addr.svm_port;
349 dst_cid = info->remote_cid;
350 dst_port = info->remote_port;
355 /* virtio_transport_get_credit might return less than pkt_len credit */
356 pkt_len = virtio_transport_get_credit(vvs, pkt_len);
358 /* Do not send zero length OP_RW pkt */
359 if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW)
363 /* If zerocopy is not enabled by 'setsockopt()', we behave as
364 * there is no MSG_ZEROCOPY flag set.
366 if (!sock_flag(sk_vsock(vsk), SOCK_ZEROCOPY))
367 info->msg->msg_flags &= ~MSG_ZEROCOPY;
369 if (info->msg->msg_flags & MSG_ZEROCOPY)
370 can_zcopy = virtio_transport_can_zcopy(t_ops, info, pkt_len);
373 max_skb_len = min_t(u32, VIRTIO_VSOCK_MAX_PKT_BUF_SIZE,
374 (MAX_SKB_FRAGS * PAGE_SIZE));
383 skb_len = min(max_skb_len, rest_len);
385 skb = virtio_transport_alloc_skb(info, skb_len, can_zcopy,
393 /* We process buffer part by part, allocating skb on
394 * each iteration. If this is last skb for this buffer
395 * and MSG_ZEROCOPY mode is in use - we must allocate
396 * completion for the current syscall.
398 if (info->msg && info->msg->msg_flags & MSG_ZEROCOPY &&
399 skb_len == rest_len && info->op == VIRTIO_VSOCK_OP_RW) {
400 if (virtio_transport_init_zcopy_skb(vsk, skb,
409 virtio_transport_inc_tx_pkt(vvs, skb);
411 ret = t_ops->send_pkt(skb);
415 /* Both virtio and vhost 'send_pkt()' returns 'skb_len',
416 * but for reliability use 'ret' instead of 'skb_len'.
417 * Also if partial send happens (e.g. 'ret' != 'skb_len')
418 * somehow, we break this loop, but account such returned
419 * value in 'virtio_transport_put_credit()'.
423 if (WARN_ONCE(ret != skb_len,
424 "'send_pkt()' returns %i, but %zu expected\n",
429 virtio_transport_put_credit(vvs, rest_len);
431 /* Return number of bytes, if any data has been sent. */
432 if (rest_len != pkt_len)
433 ret = pkt_len - rest_len;
438 static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
441 if (vvs->rx_bytes + len > vvs->buf_alloc)
444 vvs->rx_bytes += len;
448 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs,
451 vvs->rx_bytes -= len;
455 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff *skb)
457 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
459 spin_lock_bh(&vvs->rx_lock);
460 vvs->last_fwd_cnt = vvs->fwd_cnt;
461 hdr->fwd_cnt = cpu_to_le32(vvs->fwd_cnt);
462 hdr->buf_alloc = cpu_to_le32(vvs->buf_alloc);
463 spin_unlock_bh(&vvs->rx_lock);
465 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt);
467 void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
469 struct sock *s = skb->sk;
472 struct vsock_sock *vs = vsock_sk(s);
473 struct virtio_vsock_sock *vvs;
477 spin_lock_bh(&vvs->tx_lock);
478 vvs->bytes_unsent -= skb->len;
479 spin_unlock_bh(&vvs->tx_lock);
485 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
487 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
494 spin_lock_bh(&vvs->tx_lock);
495 ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
499 vvs->bytes_unsent += ret;
500 spin_unlock_bh(&vvs->tx_lock);
504 EXPORT_SYMBOL_GPL(virtio_transport_get_credit);
506 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit)
511 spin_lock_bh(&vvs->tx_lock);
512 vvs->tx_cnt -= credit;
513 vvs->bytes_unsent -= credit;
514 spin_unlock_bh(&vvs->tx_lock);
516 EXPORT_SYMBOL_GPL(virtio_transport_put_credit);
518 static int virtio_transport_send_credit_update(struct vsock_sock *vsk)
520 struct virtio_vsock_pkt_info info = {
521 .op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
525 return virtio_transport_send_pkt_info(vsk, &info);
529 virtio_transport_stream_do_peek(struct vsock_sock *vsk,
533 struct virtio_vsock_sock *vvs = vsk->trans;
538 spin_lock_bh(&vvs->rx_lock);
540 skb_queue_walk(&vvs->rx_queue, skb) {
544 if (bytes > skb->len)
547 spin_unlock_bh(&vvs->rx_lock);
549 /* sk_lock is held by caller so no one else can dequeue.
550 * Unlock rx_lock since skb_copy_datagram_iter() may sleep.
552 err = skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset,
553 &msg->msg_iter, bytes);
559 spin_lock_bh(&vvs->rx_lock);
565 spin_unlock_bh(&vvs->rx_lock);
576 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
580 struct virtio_vsock_sock *vvs = vsk->trans;
581 size_t bytes, total = 0;
588 spin_lock_bh(&vvs->rx_lock);
590 if (WARN_ONCE(skb_queue_empty(&vvs->rx_queue) && vvs->rx_bytes,
591 "rx_queue is empty, but rx_bytes is non-zero\n")) {
592 spin_unlock_bh(&vvs->rx_lock);
596 while (total < len && !skb_queue_empty(&vvs->rx_queue)) {
597 skb = skb_peek(&vvs->rx_queue);
599 bytes = min_t(size_t, len - total,
600 skb->len - VIRTIO_VSOCK_SKB_CB(skb)->offset);
602 /* sk_lock is held by caller so no one else can dequeue.
603 * Unlock rx_lock since skb_copy_datagram_iter() may sleep.
605 spin_unlock_bh(&vvs->rx_lock);
607 err = skb_copy_datagram_iter(skb,
608 VIRTIO_VSOCK_SKB_CB(skb)->offset,
609 &msg->msg_iter, bytes);
613 spin_lock_bh(&vvs->rx_lock);
617 VIRTIO_VSOCK_SKB_CB(skb)->offset += bytes;
619 if (skb->len == VIRTIO_VSOCK_SKB_CB(skb)->offset) {
620 u32 pkt_len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
622 virtio_transport_dec_rx_pkt(vvs, pkt_len);
623 __skb_unlink(skb, &vvs->rx_queue);
628 fwd_cnt_delta = vvs->fwd_cnt - vvs->last_fwd_cnt;
629 free_space = vvs->buf_alloc - fwd_cnt_delta;
630 low_rx_bytes = (vvs->rx_bytes <
631 sock_rcvlowat(sk_vsock(vsk), 0, INT_MAX));
633 spin_unlock_bh(&vvs->rx_lock);
635 /* To reduce the number of credit update messages,
636 * don't update credits as long as lots of space is available.
637 * Note: the limit chosen here is arbitrary. Setting the limit
638 * too high causes extra messages. Too low causes transmitter
639 * stalls. As stalls are in theory more expensive than extra
640 * messages, we set the limit to a high value. TODO: experiment
641 * with different values. Also send credit update message when
642 * number of bytes in rx queue is not enough to wake up reader.
645 (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE || low_rx_bytes))
646 virtio_transport_send_credit_update(vsk);
657 virtio_transport_seqpacket_do_peek(struct vsock_sock *vsk,
660 struct virtio_vsock_sock *vvs = vsk->trans;
664 spin_lock_bh(&vvs->rx_lock);
666 if (!vvs->msg_count) {
667 spin_unlock_bh(&vvs->rx_lock);
672 len = msg_data_left(msg);
674 skb_queue_walk(&vvs->rx_queue, skb) {
675 struct virtio_vsock_hdr *hdr;
682 if (bytes > skb->len)
685 spin_unlock_bh(&vvs->rx_lock);
687 /* sk_lock is held by caller so no one else can dequeue.
688 * Unlock rx_lock since skb_copy_datagram_iter() may sleep.
690 err = skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset,
691 &msg->msg_iter, bytes);
695 spin_lock_bh(&vvs->rx_lock);
699 hdr = virtio_vsock_hdr(skb);
701 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) {
702 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOR)
703 msg->msg_flags |= MSG_EOR;
709 spin_unlock_bh(&vvs->rx_lock);
714 static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
718 struct virtio_vsock_sock *vvs = vsk->trans;
719 int dequeued_len = 0;
720 size_t user_buf_len = msg_data_left(msg);
721 bool msg_ready = false;
724 spin_lock_bh(&vvs->rx_lock);
726 if (vvs->msg_count == 0) {
727 spin_unlock_bh(&vvs->rx_lock);
732 struct virtio_vsock_hdr *hdr;
735 skb = __skb_dequeue(&vvs->rx_queue);
738 hdr = virtio_vsock_hdr(skb);
739 pkt_len = (size_t)le32_to_cpu(hdr->len);
741 if (dequeued_len >= 0) {
742 size_t bytes_to_copy;
744 bytes_to_copy = min(user_buf_len, pkt_len);
749 /* sk_lock is held by caller so no one else can dequeue.
750 * Unlock rx_lock since skb_copy_datagram_iter() may sleep.
752 spin_unlock_bh(&vvs->rx_lock);
754 err = skb_copy_datagram_iter(skb, 0,
758 /* Copy of message failed. Rest of
759 * fragments will be freed without copy.
763 user_buf_len -= bytes_to_copy;
766 spin_lock_bh(&vvs->rx_lock);
769 if (dequeued_len >= 0)
770 dequeued_len += pkt_len;
773 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) {
777 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOR)
778 msg->msg_flags |= MSG_EOR;
781 virtio_transport_dec_rx_pkt(vvs, pkt_len);
785 spin_unlock_bh(&vvs->rx_lock);
787 virtio_transport_send_credit_update(vsk);
793 virtio_transport_stream_dequeue(struct vsock_sock *vsk,
795 size_t len, int flags)
797 if (flags & MSG_PEEK)
798 return virtio_transport_stream_do_peek(vsk, msg, len);
800 return virtio_transport_stream_do_dequeue(vsk, msg, len);
802 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
805 virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
809 if (flags & MSG_PEEK)
810 return virtio_transport_seqpacket_do_peek(vsk, msg);
812 return virtio_transport_seqpacket_do_dequeue(vsk, msg, flags);
814 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
817 virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
821 struct virtio_vsock_sock *vvs = vsk->trans;
823 spin_lock_bh(&vvs->tx_lock);
825 if (len > vvs->peer_buf_alloc) {
826 spin_unlock_bh(&vvs->tx_lock);
830 spin_unlock_bh(&vvs->tx_lock);
832 return virtio_transport_stream_enqueue(vsk, msg, len);
834 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue);
837 virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
839 size_t len, int flags)
843 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue);
845 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk)
847 struct virtio_vsock_sock *vvs = vsk->trans;
850 spin_lock_bh(&vvs->rx_lock);
851 bytes = vvs->rx_bytes;
852 spin_unlock_bh(&vvs->rx_lock);
856 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data);
858 u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk)
860 struct virtio_vsock_sock *vvs = vsk->trans;
863 spin_lock_bh(&vvs->rx_lock);
864 msg_count = vvs->msg_count;
865 spin_unlock_bh(&vvs->rx_lock);
869 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_has_data);
871 static s64 virtio_transport_has_space(struct vsock_sock *vsk)
873 struct virtio_vsock_sock *vvs = vsk->trans;
876 bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
883 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk)
885 struct virtio_vsock_sock *vvs = vsk->trans;
888 spin_lock_bh(&vvs->tx_lock);
889 bytes = virtio_transport_has_space(vsk);
890 spin_unlock_bh(&vvs->tx_lock);
894 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space);
896 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
897 struct vsock_sock *psk)
899 struct virtio_vsock_sock *vvs;
901 vvs = kzalloc(sizeof(*vvs), GFP_KERNEL);
907 if (psk && psk->trans) {
908 struct virtio_vsock_sock *ptrans = psk->trans;
910 vvs->peer_buf_alloc = ptrans->peer_buf_alloc;
913 if (vsk->buffer_size > VIRTIO_VSOCK_MAX_BUF_SIZE)
914 vsk->buffer_size = VIRTIO_VSOCK_MAX_BUF_SIZE;
916 vvs->buf_alloc = vsk->buffer_size;
918 spin_lock_init(&vvs->rx_lock);
919 spin_lock_init(&vvs->tx_lock);
920 skb_queue_head_init(&vvs->rx_queue);
924 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init);
926 /* sk_lock held by the caller */
927 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val)
929 struct virtio_vsock_sock *vvs = vsk->trans;
931 if (*val > VIRTIO_VSOCK_MAX_BUF_SIZE)
932 *val = VIRTIO_VSOCK_MAX_BUF_SIZE;
934 vvs->buf_alloc = *val;
936 virtio_transport_send_credit_update(vsk);
938 EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size);
941 virtio_transport_notify_poll_in(struct vsock_sock *vsk,
943 bool *data_ready_now)
945 *data_ready_now = vsock_stream_has_data(vsk) >= target;
949 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in);
952 virtio_transport_notify_poll_out(struct vsock_sock *vsk,
954 bool *space_avail_now)
958 free_space = vsock_stream_has_space(vsk);
960 *space_avail_now = true;
961 else if (free_space == 0)
962 *space_avail_now = false;
966 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out);
968 int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
969 size_t target, struct vsock_transport_recv_notify_data *data)
973 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init);
975 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
976 size_t target, struct vsock_transport_recv_notify_data *data)
980 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block);
982 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
983 size_t target, struct vsock_transport_recv_notify_data *data)
987 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue);
989 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
990 size_t target, ssize_t copied, bool data_read,
991 struct vsock_transport_recv_notify_data *data)
995 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue);
997 int virtio_transport_notify_send_init(struct vsock_sock *vsk,
998 struct vsock_transport_send_notify_data *data)
1002 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init);
1004 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
1005 struct vsock_transport_send_notify_data *data)
1009 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block);
1011 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
1012 struct vsock_transport_send_notify_data *data)
1016 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue);
1018 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
1019 ssize_t written, struct vsock_transport_send_notify_data *data)
1023 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue);
1025 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk)
1027 return vsk->buffer_size;
1029 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat);
1031 bool virtio_transport_stream_is_active(struct vsock_sock *vsk)
1035 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active);
1037 bool virtio_transport_stream_allow(u32 cid, u32 port)
1041 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow);
1043 int virtio_transport_dgram_bind(struct vsock_sock *vsk,
1044 struct sockaddr_vm *addr)
1048 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind);
1050 bool virtio_transport_dgram_allow(u32 cid, u32 port)
1054 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow);
1056 int virtio_transport_connect(struct vsock_sock *vsk)
1058 struct virtio_vsock_pkt_info info = {
1059 .op = VIRTIO_VSOCK_OP_REQUEST,
1063 return virtio_transport_send_pkt_info(vsk, &info);
1065 EXPORT_SYMBOL_GPL(virtio_transport_connect);
1067 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode)
1069 struct virtio_vsock_pkt_info info = {
1070 .op = VIRTIO_VSOCK_OP_SHUTDOWN,
1071 .flags = (mode & RCV_SHUTDOWN ?
1072 VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
1073 (mode & SEND_SHUTDOWN ?
1074 VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
1078 return virtio_transport_send_pkt_info(vsk, &info);
1080 EXPORT_SYMBOL_GPL(virtio_transport_shutdown);
1083 virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
1084 struct sockaddr_vm *remote_addr,
1090 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue);
1093 virtio_transport_stream_enqueue(struct vsock_sock *vsk,
1097 struct virtio_vsock_pkt_info info = {
1098 .op = VIRTIO_VSOCK_OP_RW,
1104 return virtio_transport_send_pkt_info(vsk, &info);
1106 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue);
1108 void virtio_transport_destruct(struct vsock_sock *vsk)
1110 struct virtio_vsock_sock *vvs = vsk->trans;
1115 EXPORT_SYMBOL_GPL(virtio_transport_destruct);
1117 ssize_t virtio_transport_unsent_bytes(struct vsock_sock *vsk)
1119 struct virtio_vsock_sock *vvs = vsk->trans;
1122 spin_lock_bh(&vvs->tx_lock);
1123 ret = vvs->bytes_unsent;
1124 spin_unlock_bh(&vvs->tx_lock);
1128 EXPORT_SYMBOL_GPL(virtio_transport_unsent_bytes);
1130 static int virtio_transport_reset(struct vsock_sock *vsk,
1131 struct sk_buff *skb)
1133 struct virtio_vsock_pkt_info info = {
1134 .op = VIRTIO_VSOCK_OP_RST,
1139 /* Send RST only if the original pkt is not a RST pkt */
1140 if (skb && le16_to_cpu(virtio_vsock_hdr(skb)->op) == VIRTIO_VSOCK_OP_RST)
1143 return virtio_transport_send_pkt_info(vsk, &info);
1146 /* Normally packets are associated with a socket. There may be no socket if an
1147 * attempt was made to connect to a socket that does not exist.
1149 static int virtio_transport_reset_no_sock(const struct virtio_transport *t,
1150 struct sk_buff *skb)
1152 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1153 struct virtio_vsock_pkt_info info = {
1154 .op = VIRTIO_VSOCK_OP_RST,
1155 .type = le16_to_cpu(hdr->type),
1158 struct sk_buff *reply;
1160 /* Send RST only if the original pkt is not a RST pkt */
1161 if (le16_to_cpu(hdr->op) == VIRTIO_VSOCK_OP_RST)
1167 reply = virtio_transport_alloc_skb(&info, 0, false,
1168 le64_to_cpu(hdr->dst_cid),
1169 le32_to_cpu(hdr->dst_port),
1170 le64_to_cpu(hdr->src_cid),
1171 le32_to_cpu(hdr->src_port));
1175 return t->send_pkt(reply);
1178 /* This function should be called with sk_lock held and SOCK_DONE set */
1179 static void virtio_transport_remove_sock(struct vsock_sock *vsk)
1181 struct virtio_vsock_sock *vvs = vsk->trans;
1183 /* We don't need to take rx_lock, as the socket is closing and we are
1186 __skb_queue_purge(&vvs->rx_queue);
1187 vsock_remove_sock(vsk);
1190 static void virtio_transport_wait_close(struct sock *sk, long timeout)
1193 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1195 add_wait_queue(sk_sleep(sk), &wait);
1198 if (sk_wait_event(sk, &timeout,
1199 sock_flag(sk, SOCK_DONE), &wait))
1201 } while (!signal_pending(current) && timeout);
1203 remove_wait_queue(sk_sleep(sk), &wait);
1207 static void virtio_transport_do_close(struct vsock_sock *vsk,
1208 bool cancel_timeout)
1210 struct sock *sk = sk_vsock(vsk);
1212 sock_set_flag(sk, SOCK_DONE);
1213 vsk->peer_shutdown = SHUTDOWN_MASK;
1214 if (vsock_stream_has_data(vsk) <= 0)
1215 sk->sk_state = TCP_CLOSING;
1216 sk->sk_state_change(sk);
1218 if (vsk->close_work_scheduled &&
1219 (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) {
1220 vsk->close_work_scheduled = false;
1222 virtio_transport_remove_sock(vsk);
1224 /* Release refcnt obtained when we scheduled the timeout */
1229 static void virtio_transport_close_timeout(struct work_struct *work)
1231 struct vsock_sock *vsk =
1232 container_of(work, struct vsock_sock, close_work.work);
1233 struct sock *sk = sk_vsock(vsk);
1238 if (!sock_flag(sk, SOCK_DONE)) {
1239 (void)virtio_transport_reset(vsk, NULL);
1241 virtio_transport_do_close(vsk, false);
1244 vsk->close_work_scheduled = false;
1250 /* User context, vsk->sk is locked */
1251 static bool virtio_transport_close(struct vsock_sock *vsk)
1253 struct sock *sk = &vsk->sk;
1255 if (!(sk->sk_state == TCP_ESTABLISHED ||
1256 sk->sk_state == TCP_CLOSING))
1259 /* Already received SHUTDOWN from peer, reply with RST */
1260 if ((vsk->peer_shutdown & SHUTDOWN_MASK) == SHUTDOWN_MASK) {
1261 (void)virtio_transport_reset(vsk, NULL);
1265 if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
1266 (void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK);
1268 if (sock_flag(sk, SOCK_LINGER) && !(current->flags & PF_EXITING))
1269 virtio_transport_wait_close(sk, sk->sk_lingertime);
1271 if (sock_flag(sk, SOCK_DONE)) {
1276 INIT_DELAYED_WORK(&vsk->close_work,
1277 virtio_transport_close_timeout);
1278 vsk->close_work_scheduled = true;
1279 schedule_delayed_work(&vsk->close_work, VSOCK_CLOSE_TIMEOUT);
1283 void virtio_transport_release(struct vsock_sock *vsk)
1285 struct sock *sk = &vsk->sk;
1286 bool remove_sock = true;
1288 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)
1289 remove_sock = virtio_transport_close(vsk);
1292 sock_set_flag(sk, SOCK_DONE);
1293 virtio_transport_remove_sock(vsk);
1296 EXPORT_SYMBOL_GPL(virtio_transport_release);
1299 virtio_transport_recv_connecting(struct sock *sk,
1300 struct sk_buff *skb)
1302 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1303 struct vsock_sock *vsk = vsock_sk(sk);
1307 switch (le16_to_cpu(hdr->op)) {
1308 case VIRTIO_VSOCK_OP_RESPONSE:
1309 sk->sk_state = TCP_ESTABLISHED;
1310 sk->sk_socket->state = SS_CONNECTED;
1311 vsock_insert_connected(vsk);
1312 sk->sk_state_change(sk);
1314 case VIRTIO_VSOCK_OP_INVALID:
1316 case VIRTIO_VSOCK_OP_RST:
1328 virtio_transport_reset(vsk, skb);
1329 sk->sk_state = TCP_CLOSE;
1331 sk_error_report(sk);
1336 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
1337 struct sk_buff *skb)
1339 struct virtio_vsock_sock *vvs = vsk->trans;
1340 bool can_enqueue, free_pkt = false;
1341 struct virtio_vsock_hdr *hdr;
1344 hdr = virtio_vsock_hdr(skb);
1345 len = le32_to_cpu(hdr->len);
1347 spin_lock_bh(&vvs->rx_lock);
1349 can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
1355 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
1358 /* Try to copy small packets into the buffer of last packet queued,
1359 * to avoid wasting memory queueing the entire buffer with a small
1362 if (len <= GOOD_COPY_LEN && !skb_queue_empty(&vvs->rx_queue)) {
1363 struct virtio_vsock_hdr *last_hdr;
1364 struct sk_buff *last_skb;
1366 last_skb = skb_peek_tail(&vvs->rx_queue);
1367 last_hdr = virtio_vsock_hdr(last_skb);
1369 /* If there is space in the last packet queued, we copy the
1370 * new packet in its buffer. We avoid this if the last packet
1371 * queued has VIRTIO_VSOCK_SEQ_EOM set, because this is
1372 * delimiter of SEQPACKET message, so 'pkt' is the first packet
1375 if (skb->len < skb_tailroom(last_skb) &&
1376 !(le32_to_cpu(last_hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)) {
1377 memcpy(skb_put(last_skb, skb->len), skb->data, skb->len);
1379 last_hdr->flags |= hdr->flags;
1380 le32_add_cpu(&last_hdr->len, len);
1385 __skb_queue_tail(&vvs->rx_queue, skb);
1388 spin_unlock_bh(&vvs->rx_lock);
1394 virtio_transport_recv_connected(struct sock *sk,
1395 struct sk_buff *skb)
1397 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1398 struct vsock_sock *vsk = vsock_sk(sk);
1401 switch (le16_to_cpu(hdr->op)) {
1402 case VIRTIO_VSOCK_OP_RW:
1403 virtio_transport_recv_enqueue(vsk, skb);
1404 vsock_data_ready(sk);
1406 case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
1407 virtio_transport_send_credit_update(vsk);
1409 case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
1410 sk->sk_write_space(sk);
1412 case VIRTIO_VSOCK_OP_SHUTDOWN:
1413 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_RCV)
1414 vsk->peer_shutdown |= RCV_SHUTDOWN;
1415 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_SEND)
1416 vsk->peer_shutdown |= SEND_SHUTDOWN;
1417 if (vsk->peer_shutdown == SHUTDOWN_MASK) {
1418 if (vsock_stream_has_data(vsk) <= 0 && !sock_flag(sk, SOCK_DONE)) {
1419 (void)virtio_transport_reset(vsk, NULL);
1420 virtio_transport_do_close(vsk, true);
1422 /* Remove this socket anyway because the remote peer sent
1423 * the shutdown. This way a new connection will succeed
1424 * if the remote peer uses the same source port,
1425 * even if the old socket is still unreleased, but now disconnected.
1427 vsock_remove_sock(vsk);
1429 if (le32_to_cpu(virtio_vsock_hdr(skb)->flags))
1430 sk->sk_state_change(sk);
1432 case VIRTIO_VSOCK_OP_RST:
1433 virtio_transport_do_close(vsk, true);
1445 virtio_transport_recv_disconnecting(struct sock *sk,
1446 struct sk_buff *skb)
1448 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1449 struct vsock_sock *vsk = vsock_sk(sk);
1451 if (le16_to_cpu(hdr->op) == VIRTIO_VSOCK_OP_RST)
1452 virtio_transport_do_close(vsk, true);
1456 virtio_transport_send_response(struct vsock_sock *vsk,
1457 struct sk_buff *skb)
1459 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1460 struct virtio_vsock_pkt_info info = {
1461 .op = VIRTIO_VSOCK_OP_RESPONSE,
1462 .remote_cid = le64_to_cpu(hdr->src_cid),
1463 .remote_port = le32_to_cpu(hdr->src_port),
1468 return virtio_transport_send_pkt_info(vsk, &info);
1471 static bool virtio_transport_space_update(struct sock *sk,
1472 struct sk_buff *skb)
1474 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1475 struct vsock_sock *vsk = vsock_sk(sk);
1476 struct virtio_vsock_sock *vvs = vsk->trans;
1477 bool space_available;
1479 /* Listener sockets are not associated with any transport, so we are
1480 * not able to take the state to see if there is space available in the
1481 * remote peer, but since they are only used to receive requests, we
1482 * can assume that there is always space available in the other peer.
1487 /* buf_alloc and fwd_cnt is always included in the hdr */
1488 spin_lock_bh(&vvs->tx_lock);
1489 vvs->peer_buf_alloc = le32_to_cpu(hdr->buf_alloc);
1490 vvs->peer_fwd_cnt = le32_to_cpu(hdr->fwd_cnt);
1491 space_available = virtio_transport_has_space(vsk);
1492 spin_unlock_bh(&vvs->tx_lock);
1493 return space_available;
1496 /* Handle server socket */
1498 virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,
1499 struct virtio_transport *t)
1501 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1502 struct vsock_sock *vsk = vsock_sk(sk);
1503 struct vsock_sock *vchild;
1507 if (le16_to_cpu(hdr->op) != VIRTIO_VSOCK_OP_REQUEST) {
1508 virtio_transport_reset_no_sock(t, skb);
1512 if (sk_acceptq_is_full(sk)) {
1513 virtio_transport_reset_no_sock(t, skb);
1517 /* __vsock_release() might have already flushed accept_queue.
1518 * Subsequent enqueues would lead to a memory leak.
1520 if (sk->sk_shutdown == SHUTDOWN_MASK) {
1521 virtio_transport_reset_no_sock(t, skb);
1525 child = vsock_create_connected(sk);
1527 virtio_transport_reset_no_sock(t, skb);
1531 sk_acceptq_added(sk);
1533 lock_sock_nested(child, SINGLE_DEPTH_NESTING);
1535 child->sk_state = TCP_ESTABLISHED;
1537 vchild = vsock_sk(child);
1538 vsock_addr_init(&vchild->local_addr, le64_to_cpu(hdr->dst_cid),
1539 le32_to_cpu(hdr->dst_port));
1540 vsock_addr_init(&vchild->remote_addr, le64_to_cpu(hdr->src_cid),
1541 le32_to_cpu(hdr->src_port));
1543 ret = vsock_assign_transport(vchild, vsk);
1544 /* Transport assigned (looking at remote_addr) must be the same
1545 * where we received the request.
1547 if (ret || vchild->transport != &t->transport) {
1548 release_sock(child);
1549 virtio_transport_reset_no_sock(t, skb);
1554 if (virtio_transport_space_update(child, skb))
1555 child->sk_write_space(child);
1557 vsock_insert_connected(vchild);
1558 vsock_enqueue_accept(sk, child);
1559 virtio_transport_send_response(vchild, skb);
1561 release_sock(child);
1563 sk->sk_data_ready(sk);
1567 static bool virtio_transport_valid_type(u16 type)
1569 return (type == VIRTIO_VSOCK_TYPE_STREAM) ||
1570 (type == VIRTIO_VSOCK_TYPE_SEQPACKET);
1573 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
1576 void virtio_transport_recv_pkt(struct virtio_transport *t,
1577 struct sk_buff *skb)
1579 struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1580 struct sockaddr_vm src, dst;
1581 struct vsock_sock *vsk;
1583 bool space_available;
1585 vsock_addr_init(&src, le64_to_cpu(hdr->src_cid),
1586 le32_to_cpu(hdr->src_port));
1587 vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid),
1588 le32_to_cpu(hdr->dst_port));
1590 trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,
1591 dst.svm_cid, dst.svm_port,
1592 le32_to_cpu(hdr->len),
1593 le16_to_cpu(hdr->type),
1594 le16_to_cpu(hdr->op),
1595 le32_to_cpu(hdr->flags),
1596 le32_to_cpu(hdr->buf_alloc),
1597 le32_to_cpu(hdr->fwd_cnt));
1599 if (!virtio_transport_valid_type(le16_to_cpu(hdr->type))) {
1600 (void)virtio_transport_reset_no_sock(t, skb);
1604 /* The socket must be in connected or bound table
1605 * otherwise send reset back
1607 sk = vsock_find_connected_socket(&src, &dst);
1609 sk = vsock_find_bound_socket(&dst);
1611 (void)virtio_transport_reset_no_sock(t, skb);
1616 if (virtio_transport_get_type(sk) != le16_to_cpu(hdr->type)) {
1617 (void)virtio_transport_reset_no_sock(t, skb);
1622 if (!skb_set_owner_sk_safe(skb, sk)) {
1623 WARN_ONCE(1, "receiving vsock socket has sk_refcnt == 0\n");
1631 /* Check if sk has been closed or assigned to another transport before
1632 * lock_sock (note: listener sockets are not assigned to any transport)
1634 if (sock_flag(sk, SOCK_DONE) ||
1635 (sk->sk_state != TCP_LISTEN && vsk->transport != &t->transport)) {
1636 (void)virtio_transport_reset_no_sock(t, skb);
1642 space_available = virtio_transport_space_update(sk, skb);
1644 /* Update CID in case it has changed after a transport reset event */
1645 if (vsk->local_addr.svm_cid != VMADDR_CID_ANY)
1646 vsk->local_addr.svm_cid = dst.svm_cid;
1648 if (space_available)
1649 sk->sk_write_space(sk);
1651 switch (sk->sk_state) {
1653 virtio_transport_recv_listen(sk, skb, t);
1657 virtio_transport_recv_connecting(sk, skb);
1660 case TCP_ESTABLISHED:
1661 virtio_transport_recv_connected(sk, skb);
1664 virtio_transport_recv_disconnecting(sk, skb);
1668 (void)virtio_transport_reset_no_sock(t, skb);
1675 /* Release refcnt obtained when we fetched this socket out of the
1676 * bound or connected list.
1684 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);
1686 /* Remove skbs found in a queue that have a vsk that matches.
1688 * Each skb is freed.
1690 * Returns the count of skbs that were reply packets.
1692 int virtio_transport_purge_skbs(void *vsk, struct sk_buff_head *queue)
1694 struct sk_buff_head freeme;
1695 struct sk_buff *skb, *tmp;
1698 skb_queue_head_init(&freeme);
1700 spin_lock_bh(&queue->lock);
1701 skb_queue_walk_safe(queue, skb, tmp) {
1702 if (vsock_sk(skb->sk) != vsk)
1705 __skb_unlink(skb, queue);
1706 __skb_queue_tail(&freeme, skb);
1708 if (virtio_vsock_skb_reply(skb))
1711 spin_unlock_bh(&queue->lock);
1713 __skb_queue_purge(&freeme);
1717 EXPORT_SYMBOL_GPL(virtio_transport_purge_skbs);
1719 int virtio_transport_read_skb(struct vsock_sock *vsk, skb_read_actor_t recv_actor)
1721 struct virtio_vsock_sock *vvs = vsk->trans;
1722 struct sock *sk = sk_vsock(vsk);
1723 struct virtio_vsock_hdr *hdr;
1724 struct sk_buff *skb;
1728 spin_lock_bh(&vvs->rx_lock);
1729 /* Use __skb_recv_datagram() for race-free handling of the receive. It
1730 * works for types other than dgrams.
1732 skb = __skb_recv_datagram(sk, &vvs->rx_queue, MSG_DONTWAIT, &off, &err);
1734 spin_unlock_bh(&vvs->rx_lock);
1738 hdr = virtio_vsock_hdr(skb);
1739 if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
1742 virtio_transport_dec_rx_pkt(vvs, le32_to_cpu(hdr->len));
1743 spin_unlock_bh(&vvs->rx_lock);
1745 virtio_transport_send_credit_update(vsk);
1747 return recv_actor(sk, skb);
1749 EXPORT_SYMBOL_GPL(virtio_transport_read_skb);
1751 int virtio_transport_notify_set_rcvlowat(struct vsock_sock *vsk, int val)
1753 struct virtio_vsock_sock *vvs = vsk->trans;
1756 spin_lock_bh(&vvs->rx_lock);
1758 /* If number of available bytes is less than new SO_RCVLOWAT value,
1759 * kick sender to send more data, because sender may sleep in its
1760 * 'send()' syscall waiting for enough space at our side. Also
1761 * don't send credit update when peer already knows actual value -
1762 * such transmission will be useless.
1764 send_update = (vvs->rx_bytes < val) &&
1765 (vvs->fwd_cnt != vvs->last_fwd_cnt);
1767 spin_unlock_bh(&vvs->rx_lock);
1772 err = virtio_transport_send_credit_update(vsk);
1779 EXPORT_SYMBOL_GPL(virtio_transport_notify_set_rcvlowat);
1781 MODULE_LICENSE("GPL v2");
1782 MODULE_AUTHOR("Asias He");
1783 MODULE_DESCRIPTION("common code for virtio vsock");