1 // SPDX-License-Identifier: GPL-2.0-only
3 * common code for virtio vsock
5 * Copyright (C) 2013-2015 Red Hat, Inc.
6 * Author: Asias He <asias@redhat.com>
7 * Stefan Hajnoczi <stefanha@redhat.com>
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.h>
15 #include <linux/virtio_ids.h>
16 #include <linux/virtio_config.h>
17 #include <linux/virtio_vsock.h>
18 #include <uapi/linux/vsockmon.h>
21 #include <net/af_vsock.h>
23 #define CREATE_TRACE_POINTS
24 #include <trace/events/vsock_virtio_transport_common.h>
26 /* How long to wait for graceful shutdown of a connection */
27 #define VSOCK_CLOSE_TIMEOUT (8 * HZ)
29 /* Threshold for detecting small packets to copy */
30 #define GOOD_COPY_LEN 128
32 static const struct virtio_transport
*
33 virtio_transport_get_ops(struct vsock_sock
*vsk
)
35 const struct vsock_transport
*t
= vsock_core_get_transport(vsk
);
37 return container_of(t
, struct virtio_transport
, transport
);
40 static struct virtio_vsock_pkt
*
41 virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info
*info
,
48 struct virtio_vsock_pkt
*pkt
;
51 pkt
= kzalloc(sizeof(*pkt
), GFP_KERNEL
);
55 pkt
->hdr
.type
= cpu_to_le16(info
->type
);
56 pkt
->hdr
.op
= cpu_to_le16(info
->op
);
57 pkt
->hdr
.src_cid
= cpu_to_le64(src_cid
);
58 pkt
->hdr
.dst_cid
= cpu_to_le64(dst_cid
);
59 pkt
->hdr
.src_port
= cpu_to_le32(src_port
);
60 pkt
->hdr
.dst_port
= cpu_to_le32(dst_port
);
61 pkt
->hdr
.flags
= cpu_to_le32(info
->flags
);
63 pkt
->hdr
.len
= cpu_to_le32(len
);
64 pkt
->reply
= info
->reply
;
67 if (info
->msg
&& len
> 0) {
68 pkt
->buf
= kmalloc(len
, GFP_KERNEL
);
74 err
= memcpy_from_msg(pkt
->buf
, info
->msg
, len
);
79 trace_virtio_transport_alloc_pkt(src_cid
, src_port
,
96 static struct sk_buff
*virtio_transport_build_skb(void *opaque
)
98 struct virtio_vsock_pkt
*pkt
= opaque
;
99 struct af_vsockmon_hdr
*hdr
;
104 /* A packet could be split to fit the RX buffer, so we can retrieve
105 * the payload length from the header and the buffer pointer taking
106 * care of the offset in the original packet.
108 payload_len
= le32_to_cpu(pkt
->hdr
.len
);
109 payload_buf
= pkt
->buf
+ pkt
->off
;
111 skb
= alloc_skb(sizeof(*hdr
) + sizeof(pkt
->hdr
) + payload_len
,
116 hdr
= skb_put(skb
, sizeof(*hdr
));
118 /* pkt->hdr is little-endian so no need to byteswap here */
119 hdr
->src_cid
= pkt
->hdr
.src_cid
;
120 hdr
->src_port
= pkt
->hdr
.src_port
;
121 hdr
->dst_cid
= pkt
->hdr
.dst_cid
;
122 hdr
->dst_port
= pkt
->hdr
.dst_port
;
124 hdr
->transport
= cpu_to_le16(AF_VSOCK_TRANSPORT_VIRTIO
);
125 hdr
->len
= cpu_to_le16(sizeof(pkt
->hdr
));
126 memset(hdr
->reserved
, 0, sizeof(hdr
->reserved
));
128 switch (le16_to_cpu(pkt
->hdr
.op
)) {
129 case VIRTIO_VSOCK_OP_REQUEST
:
130 case VIRTIO_VSOCK_OP_RESPONSE
:
131 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_CONNECT
);
133 case VIRTIO_VSOCK_OP_RST
:
134 case VIRTIO_VSOCK_OP_SHUTDOWN
:
135 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_DISCONNECT
);
137 case VIRTIO_VSOCK_OP_RW
:
138 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_PAYLOAD
);
140 case VIRTIO_VSOCK_OP_CREDIT_UPDATE
:
141 case VIRTIO_VSOCK_OP_CREDIT_REQUEST
:
142 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_CONTROL
);
145 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_UNKNOWN
);
149 skb_put_data(skb
, &pkt
->hdr
, sizeof(pkt
->hdr
));
152 skb_put_data(skb
, payload_buf
, payload_len
);
158 void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt
*pkt
)
160 vsock_deliver_tap(virtio_transport_build_skb
, pkt
);
162 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt
);
164 static int virtio_transport_send_pkt_info(struct vsock_sock
*vsk
,
165 struct virtio_vsock_pkt_info
*info
)
167 u32 src_cid
, src_port
, dst_cid
, dst_port
;
168 struct virtio_vsock_sock
*vvs
;
169 struct virtio_vsock_pkt
*pkt
;
170 u32 pkt_len
= info
->pkt_len
;
172 src_cid
= virtio_transport_get_ops(vsk
)->transport
.get_local_cid();
173 src_port
= vsk
->local_addr
.svm_port
;
174 if (!info
->remote_cid
) {
175 dst_cid
= vsk
->remote_addr
.svm_cid
;
176 dst_port
= vsk
->remote_addr
.svm_port
;
178 dst_cid
= info
->remote_cid
;
179 dst_port
= info
->remote_port
;
184 /* we can send less than pkt_len bytes */
185 if (pkt_len
> VIRTIO_VSOCK_MAX_PKT_BUF_SIZE
)
186 pkt_len
= VIRTIO_VSOCK_MAX_PKT_BUF_SIZE
;
188 /* virtio_transport_get_credit might return less than pkt_len credit */
189 pkt_len
= virtio_transport_get_credit(vvs
, pkt_len
);
191 /* Do not send zero length OP_RW pkt */
192 if (pkt_len
== 0 && info
->op
== VIRTIO_VSOCK_OP_RW
)
195 pkt
= virtio_transport_alloc_pkt(info
, pkt_len
,
199 virtio_transport_put_credit(vvs
, pkt_len
);
203 virtio_transport_inc_tx_pkt(vvs
, pkt
);
205 return virtio_transport_get_ops(vsk
)->send_pkt(pkt
);
208 static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock
*vvs
,
209 struct virtio_vsock_pkt
*pkt
)
211 if (vvs
->rx_bytes
+ pkt
->len
> vvs
->buf_alloc
)
214 vvs
->rx_bytes
+= pkt
->len
;
218 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock
*vvs
,
219 struct virtio_vsock_pkt
*pkt
)
221 vvs
->rx_bytes
-= pkt
->len
;
222 vvs
->fwd_cnt
+= pkt
->len
;
225 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock
*vvs
, struct virtio_vsock_pkt
*pkt
)
227 spin_lock_bh(&vvs
->rx_lock
);
228 vvs
->last_fwd_cnt
= vvs
->fwd_cnt
;
229 pkt
->hdr
.fwd_cnt
= cpu_to_le32(vvs
->fwd_cnt
);
230 pkt
->hdr
.buf_alloc
= cpu_to_le32(vvs
->buf_alloc
);
231 spin_unlock_bh(&vvs
->rx_lock
);
233 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt
);
235 u32
virtio_transport_get_credit(struct virtio_vsock_sock
*vvs
, u32 credit
)
239 spin_lock_bh(&vvs
->tx_lock
);
240 ret
= vvs
->peer_buf_alloc
- (vvs
->tx_cnt
- vvs
->peer_fwd_cnt
);
244 spin_unlock_bh(&vvs
->tx_lock
);
248 EXPORT_SYMBOL_GPL(virtio_transport_get_credit
);
250 void virtio_transport_put_credit(struct virtio_vsock_sock
*vvs
, u32 credit
)
252 spin_lock_bh(&vvs
->tx_lock
);
253 vvs
->tx_cnt
-= credit
;
254 spin_unlock_bh(&vvs
->tx_lock
);
256 EXPORT_SYMBOL_GPL(virtio_transport_put_credit
);
258 static int virtio_transport_send_credit_update(struct vsock_sock
*vsk
,
260 struct virtio_vsock_hdr
*hdr
)
262 struct virtio_vsock_pkt_info info
= {
263 .op
= VIRTIO_VSOCK_OP_CREDIT_UPDATE
,
268 return virtio_transport_send_pkt_info(vsk
, &info
);
272 virtio_transport_stream_do_peek(struct vsock_sock
*vsk
,
276 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
277 struct virtio_vsock_pkt
*pkt
;
278 size_t bytes
, total
= 0, off
;
281 spin_lock_bh(&vvs
->rx_lock
);
283 list_for_each_entry(pkt
, &vvs
->rx_queue
, list
) {
289 while (total
< len
&& off
< pkt
->len
) {
291 if (bytes
> pkt
->len
- off
)
292 bytes
= pkt
->len
- off
;
294 /* sk_lock is held by caller so no one else can dequeue.
295 * Unlock rx_lock since memcpy_to_msg() may sleep.
297 spin_unlock_bh(&vvs
->rx_lock
);
299 err
= memcpy_to_msg(msg
, pkt
->buf
+ off
, bytes
);
303 spin_lock_bh(&vvs
->rx_lock
);
310 spin_unlock_bh(&vvs
->rx_lock
);
321 virtio_transport_stream_do_dequeue(struct vsock_sock
*vsk
,
325 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
326 struct virtio_vsock_pkt
*pkt
;
327 size_t bytes
, total
= 0;
331 spin_lock_bh(&vvs
->rx_lock
);
332 while (total
< len
&& !list_empty(&vvs
->rx_queue
)) {
333 pkt
= list_first_entry(&vvs
->rx_queue
,
334 struct virtio_vsock_pkt
, list
);
337 if (bytes
> pkt
->len
- pkt
->off
)
338 bytes
= pkt
->len
- pkt
->off
;
340 /* sk_lock is held by caller so no one else can dequeue.
341 * Unlock rx_lock since memcpy_to_msg() may sleep.
343 spin_unlock_bh(&vvs
->rx_lock
);
345 err
= memcpy_to_msg(msg
, pkt
->buf
+ pkt
->off
, bytes
);
349 spin_lock_bh(&vvs
->rx_lock
);
353 if (pkt
->off
== pkt
->len
) {
354 virtio_transport_dec_rx_pkt(vvs
, pkt
);
355 list_del(&pkt
->list
);
356 virtio_transport_free_pkt(pkt
);
360 free_space
= vvs
->buf_alloc
- (vvs
->fwd_cnt
- vvs
->last_fwd_cnt
);
362 spin_unlock_bh(&vvs
->rx_lock
);
364 /* To reduce the number of credit update messages,
365 * don't update credits as long as lots of space is available.
366 * Note: the limit chosen here is arbitrary. Setting the limit
367 * too high causes extra messages. Too low causes transmitter
368 * stalls. As stalls are in theory more expensive than extra
369 * messages, we set the limit to a high value. TODO: experiment
370 * with different values.
372 if (free_space
< VIRTIO_VSOCK_MAX_PKT_BUF_SIZE
) {
373 virtio_transport_send_credit_update(vsk
,
374 VIRTIO_VSOCK_TYPE_STREAM
,
387 virtio_transport_stream_dequeue(struct vsock_sock
*vsk
,
389 size_t len
, int flags
)
391 if (flags
& MSG_PEEK
)
392 return virtio_transport_stream_do_peek(vsk
, msg
, len
);
394 return virtio_transport_stream_do_dequeue(vsk
, msg
, len
);
396 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue
);
399 virtio_transport_dgram_dequeue(struct vsock_sock
*vsk
,
401 size_t len
, int flags
)
405 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue
);
407 s64
virtio_transport_stream_has_data(struct vsock_sock
*vsk
)
409 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
412 spin_lock_bh(&vvs
->rx_lock
);
413 bytes
= vvs
->rx_bytes
;
414 spin_unlock_bh(&vvs
->rx_lock
);
418 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data
);
420 static s64
virtio_transport_has_space(struct vsock_sock
*vsk
)
422 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
425 bytes
= vvs
->peer_buf_alloc
- (vvs
->tx_cnt
- vvs
->peer_fwd_cnt
);
432 s64
virtio_transport_stream_has_space(struct vsock_sock
*vsk
)
434 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
437 spin_lock_bh(&vvs
->tx_lock
);
438 bytes
= virtio_transport_has_space(vsk
);
439 spin_unlock_bh(&vvs
->tx_lock
);
443 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space
);
445 int virtio_transport_do_socket_init(struct vsock_sock
*vsk
,
446 struct vsock_sock
*psk
)
448 struct virtio_vsock_sock
*vvs
;
450 vvs
= kzalloc(sizeof(*vvs
), GFP_KERNEL
);
456 if (psk
&& psk
->trans
) {
457 struct virtio_vsock_sock
*ptrans
= psk
->trans
;
459 vvs
->peer_buf_alloc
= ptrans
->peer_buf_alloc
;
462 if (vsk
->buffer_size
> VIRTIO_VSOCK_MAX_BUF_SIZE
)
463 vsk
->buffer_size
= VIRTIO_VSOCK_MAX_BUF_SIZE
;
465 vvs
->buf_alloc
= vsk
->buffer_size
;
467 spin_lock_init(&vvs
->rx_lock
);
468 spin_lock_init(&vvs
->tx_lock
);
469 INIT_LIST_HEAD(&vvs
->rx_queue
);
473 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init
);
475 /* sk_lock held by the caller */
476 void virtio_transport_notify_buffer_size(struct vsock_sock
*vsk
, u64
*val
)
478 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
480 if (*val
> VIRTIO_VSOCK_MAX_BUF_SIZE
)
481 *val
= VIRTIO_VSOCK_MAX_BUF_SIZE
;
483 vvs
->buf_alloc
= *val
;
485 virtio_transport_send_credit_update(vsk
, VIRTIO_VSOCK_TYPE_STREAM
,
488 EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size
);
491 virtio_transport_notify_poll_in(struct vsock_sock
*vsk
,
493 bool *data_ready_now
)
495 if (vsock_stream_has_data(vsk
))
496 *data_ready_now
= true;
498 *data_ready_now
= false;
502 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in
);
505 virtio_transport_notify_poll_out(struct vsock_sock
*vsk
,
507 bool *space_avail_now
)
511 free_space
= vsock_stream_has_space(vsk
);
513 *space_avail_now
= true;
514 else if (free_space
== 0)
515 *space_avail_now
= false;
519 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out
);
521 int virtio_transport_notify_recv_init(struct vsock_sock
*vsk
,
522 size_t target
, struct vsock_transport_recv_notify_data
*data
)
526 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init
);
528 int virtio_transport_notify_recv_pre_block(struct vsock_sock
*vsk
,
529 size_t target
, struct vsock_transport_recv_notify_data
*data
)
533 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block
);
535 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock
*vsk
,
536 size_t target
, struct vsock_transport_recv_notify_data
*data
)
540 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue
);
542 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock
*vsk
,
543 size_t target
, ssize_t copied
, bool data_read
,
544 struct vsock_transport_recv_notify_data
*data
)
548 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue
);
550 int virtio_transport_notify_send_init(struct vsock_sock
*vsk
,
551 struct vsock_transport_send_notify_data
*data
)
555 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init
);
557 int virtio_transport_notify_send_pre_block(struct vsock_sock
*vsk
,
558 struct vsock_transport_send_notify_data
*data
)
562 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block
);
564 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock
*vsk
,
565 struct vsock_transport_send_notify_data
*data
)
569 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue
);
571 int virtio_transport_notify_send_post_enqueue(struct vsock_sock
*vsk
,
572 ssize_t written
, struct vsock_transport_send_notify_data
*data
)
576 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue
);
578 u64
virtio_transport_stream_rcvhiwat(struct vsock_sock
*vsk
)
580 return vsk
->buffer_size
;
582 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat
);
584 bool virtio_transport_stream_is_active(struct vsock_sock
*vsk
)
588 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active
);
590 bool virtio_transport_stream_allow(u32 cid
, u32 port
)
594 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow
);
596 int virtio_transport_dgram_bind(struct vsock_sock
*vsk
,
597 struct sockaddr_vm
*addr
)
601 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind
);
603 bool virtio_transport_dgram_allow(u32 cid
, u32 port
)
607 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow
);
609 int virtio_transport_connect(struct vsock_sock
*vsk
)
611 struct virtio_vsock_pkt_info info
= {
612 .op
= VIRTIO_VSOCK_OP_REQUEST
,
613 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
617 return virtio_transport_send_pkt_info(vsk
, &info
);
619 EXPORT_SYMBOL_GPL(virtio_transport_connect
);
621 int virtio_transport_shutdown(struct vsock_sock
*vsk
, int mode
)
623 struct virtio_vsock_pkt_info info
= {
624 .op
= VIRTIO_VSOCK_OP_SHUTDOWN
,
625 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
626 .flags
= (mode
& RCV_SHUTDOWN
?
627 VIRTIO_VSOCK_SHUTDOWN_RCV
: 0) |
628 (mode
& SEND_SHUTDOWN
?
629 VIRTIO_VSOCK_SHUTDOWN_SEND
: 0),
633 return virtio_transport_send_pkt_info(vsk
, &info
);
635 EXPORT_SYMBOL_GPL(virtio_transport_shutdown
);
638 virtio_transport_dgram_enqueue(struct vsock_sock
*vsk
,
639 struct sockaddr_vm
*remote_addr
,
645 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue
);
648 virtio_transport_stream_enqueue(struct vsock_sock
*vsk
,
652 struct virtio_vsock_pkt_info info
= {
653 .op
= VIRTIO_VSOCK_OP_RW
,
654 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
660 return virtio_transport_send_pkt_info(vsk
, &info
);
662 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue
);
664 void virtio_transport_destruct(struct vsock_sock
*vsk
)
666 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
670 EXPORT_SYMBOL_GPL(virtio_transport_destruct
);
672 static int virtio_transport_reset(struct vsock_sock
*vsk
,
673 struct virtio_vsock_pkt
*pkt
)
675 struct virtio_vsock_pkt_info info
= {
676 .op
= VIRTIO_VSOCK_OP_RST
,
677 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
682 /* Send RST only if the original pkt is not a RST pkt */
683 if (pkt
&& le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
686 return virtio_transport_send_pkt_info(vsk
, &info
);
689 /* Normally packets are associated with a socket. There may be no socket if an
690 * attempt was made to connect to a socket that does not exist.
692 static int virtio_transport_reset_no_sock(const struct virtio_transport
*t
,
693 struct virtio_vsock_pkt
*pkt
)
695 struct virtio_vsock_pkt
*reply
;
696 struct virtio_vsock_pkt_info info
= {
697 .op
= VIRTIO_VSOCK_OP_RST
,
698 .type
= le16_to_cpu(pkt
->hdr
.type
),
702 /* Send RST only if the original pkt is not a RST pkt */
703 if (le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
706 reply
= virtio_transport_alloc_pkt(&info
, 0,
707 le64_to_cpu(pkt
->hdr
.dst_cid
),
708 le32_to_cpu(pkt
->hdr
.dst_port
),
709 le64_to_cpu(pkt
->hdr
.src_cid
),
710 le32_to_cpu(pkt
->hdr
.src_port
));
715 virtio_transport_free_pkt(reply
);
719 return t
->send_pkt(reply
);
722 static void virtio_transport_wait_close(struct sock
*sk
, long timeout
)
725 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
727 add_wait_queue(sk_sleep(sk
), &wait
);
730 if (sk_wait_event(sk
, &timeout
,
731 sock_flag(sk
, SOCK_DONE
), &wait
))
733 } while (!signal_pending(current
) && timeout
);
735 remove_wait_queue(sk_sleep(sk
), &wait
);
739 static void virtio_transport_do_close(struct vsock_sock
*vsk
,
742 struct sock
*sk
= sk_vsock(vsk
);
744 sock_set_flag(sk
, SOCK_DONE
);
745 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
746 if (vsock_stream_has_data(vsk
) <= 0)
747 sk
->sk_state
= TCP_CLOSING
;
748 sk
->sk_state_change(sk
);
750 if (vsk
->close_work_scheduled
&&
751 (!cancel_timeout
|| cancel_delayed_work(&vsk
->close_work
))) {
752 vsk
->close_work_scheduled
= false;
754 vsock_remove_sock(vsk
);
756 /* Release refcnt obtained when we scheduled the timeout */
761 static void virtio_transport_close_timeout(struct work_struct
*work
)
763 struct vsock_sock
*vsk
=
764 container_of(work
, struct vsock_sock
, close_work
.work
);
765 struct sock
*sk
= sk_vsock(vsk
);
770 if (!sock_flag(sk
, SOCK_DONE
)) {
771 (void)virtio_transport_reset(vsk
, NULL
);
773 virtio_transport_do_close(vsk
, false);
776 vsk
->close_work_scheduled
= false;
782 /* User context, vsk->sk is locked */
783 static bool virtio_transport_close(struct vsock_sock
*vsk
)
785 struct sock
*sk
= &vsk
->sk
;
787 if (!(sk
->sk_state
== TCP_ESTABLISHED
||
788 sk
->sk_state
== TCP_CLOSING
))
791 /* Already received SHUTDOWN from peer, reply with RST */
792 if ((vsk
->peer_shutdown
& SHUTDOWN_MASK
) == SHUTDOWN_MASK
) {
793 (void)virtio_transport_reset(vsk
, NULL
);
797 if ((sk
->sk_shutdown
& SHUTDOWN_MASK
) != SHUTDOWN_MASK
)
798 (void)virtio_transport_shutdown(vsk
, SHUTDOWN_MASK
);
800 if (sock_flag(sk
, SOCK_LINGER
) && !(current
->flags
& PF_EXITING
))
801 virtio_transport_wait_close(sk
, sk
->sk_lingertime
);
803 if (sock_flag(sk
, SOCK_DONE
)) {
808 INIT_DELAYED_WORK(&vsk
->close_work
,
809 virtio_transport_close_timeout
);
810 vsk
->close_work_scheduled
= true;
811 schedule_delayed_work(&vsk
->close_work
, VSOCK_CLOSE_TIMEOUT
);
815 void virtio_transport_release(struct vsock_sock
*vsk
)
817 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
818 struct virtio_vsock_pkt
*pkt
, *tmp
;
819 struct sock
*sk
= &vsk
->sk
;
820 bool remove_sock
= true;
822 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
823 if (sk
->sk_type
== SOCK_STREAM
)
824 remove_sock
= virtio_transport_close(vsk
);
826 list_for_each_entry_safe(pkt
, tmp
, &vvs
->rx_queue
, list
) {
827 list_del(&pkt
->list
);
828 virtio_transport_free_pkt(pkt
);
833 vsock_remove_sock(vsk
);
835 EXPORT_SYMBOL_GPL(virtio_transport_release
);
838 virtio_transport_recv_connecting(struct sock
*sk
,
839 struct virtio_vsock_pkt
*pkt
)
841 struct vsock_sock
*vsk
= vsock_sk(sk
);
845 switch (le16_to_cpu(pkt
->hdr
.op
)) {
846 case VIRTIO_VSOCK_OP_RESPONSE
:
847 sk
->sk_state
= TCP_ESTABLISHED
;
848 sk
->sk_socket
->state
= SS_CONNECTED
;
849 vsock_insert_connected(vsk
);
850 sk
->sk_state_change(sk
);
852 case VIRTIO_VSOCK_OP_INVALID
:
854 case VIRTIO_VSOCK_OP_RST
:
866 virtio_transport_reset(vsk
, pkt
);
867 sk
->sk_state
= TCP_CLOSE
;
869 sk
->sk_error_report(sk
);
874 virtio_transport_recv_enqueue(struct vsock_sock
*vsk
,
875 struct virtio_vsock_pkt
*pkt
)
877 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
878 bool can_enqueue
, free_pkt
= false;
880 pkt
->len
= le32_to_cpu(pkt
->hdr
.len
);
883 spin_lock_bh(&vvs
->rx_lock
);
885 can_enqueue
= virtio_transport_inc_rx_pkt(vvs
, pkt
);
891 /* Try to copy small packets into the buffer of last packet queued,
892 * to avoid wasting memory queueing the entire buffer with a small
895 if (pkt
->len
<= GOOD_COPY_LEN
&& !list_empty(&vvs
->rx_queue
)) {
896 struct virtio_vsock_pkt
*last_pkt
;
898 last_pkt
= list_last_entry(&vvs
->rx_queue
,
899 struct virtio_vsock_pkt
, list
);
901 /* If there is space in the last packet queued, we copy the
902 * new packet in its buffer.
904 if (pkt
->len
<= last_pkt
->buf_len
- last_pkt
->len
) {
905 memcpy(last_pkt
->buf
+ last_pkt
->len
, pkt
->buf
,
907 last_pkt
->len
+= pkt
->len
;
913 list_add_tail(&pkt
->list
, &vvs
->rx_queue
);
916 spin_unlock_bh(&vvs
->rx_lock
);
918 virtio_transport_free_pkt(pkt
);
922 virtio_transport_recv_connected(struct sock
*sk
,
923 struct virtio_vsock_pkt
*pkt
)
925 struct vsock_sock
*vsk
= vsock_sk(sk
);
928 switch (le16_to_cpu(pkt
->hdr
.op
)) {
929 case VIRTIO_VSOCK_OP_RW
:
930 virtio_transport_recv_enqueue(vsk
, pkt
);
931 sk
->sk_data_ready(sk
);
933 case VIRTIO_VSOCK_OP_CREDIT_UPDATE
:
934 sk
->sk_write_space(sk
);
936 case VIRTIO_VSOCK_OP_SHUTDOWN
:
937 if (le32_to_cpu(pkt
->hdr
.flags
) & VIRTIO_VSOCK_SHUTDOWN_RCV
)
938 vsk
->peer_shutdown
|= RCV_SHUTDOWN
;
939 if (le32_to_cpu(pkt
->hdr
.flags
) & VIRTIO_VSOCK_SHUTDOWN_SEND
)
940 vsk
->peer_shutdown
|= SEND_SHUTDOWN
;
941 if (vsk
->peer_shutdown
== SHUTDOWN_MASK
&&
942 vsock_stream_has_data(vsk
) <= 0 &&
943 !sock_flag(sk
, SOCK_DONE
)) {
944 (void)virtio_transport_reset(vsk
, NULL
);
946 virtio_transport_do_close(vsk
, true);
948 if (le32_to_cpu(pkt
->hdr
.flags
))
949 sk
->sk_state_change(sk
);
951 case VIRTIO_VSOCK_OP_RST
:
952 virtio_transport_do_close(vsk
, true);
959 virtio_transport_free_pkt(pkt
);
964 virtio_transport_recv_disconnecting(struct sock
*sk
,
965 struct virtio_vsock_pkt
*pkt
)
967 struct vsock_sock
*vsk
= vsock_sk(sk
);
969 if (le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
970 virtio_transport_do_close(vsk
, true);
974 virtio_transport_send_response(struct vsock_sock
*vsk
,
975 struct virtio_vsock_pkt
*pkt
)
977 struct virtio_vsock_pkt_info info
= {
978 .op
= VIRTIO_VSOCK_OP_RESPONSE
,
979 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
980 .remote_cid
= le64_to_cpu(pkt
->hdr
.src_cid
),
981 .remote_port
= le32_to_cpu(pkt
->hdr
.src_port
),
986 return virtio_transport_send_pkt_info(vsk
, &info
);
989 static bool virtio_transport_space_update(struct sock
*sk
,
990 struct virtio_vsock_pkt
*pkt
)
992 struct vsock_sock
*vsk
= vsock_sk(sk
);
993 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
994 bool space_available
;
996 /* Listener sockets are not associated with any transport, so we are
997 * not able to take the state to see if there is space available in the
998 * remote peer, but since they are only used to receive requests, we
999 * can assume that there is always space available in the other peer.
1004 /* buf_alloc and fwd_cnt is always included in the hdr */
1005 spin_lock_bh(&vvs
->tx_lock
);
1006 vvs
->peer_buf_alloc
= le32_to_cpu(pkt
->hdr
.buf_alloc
);
1007 vvs
->peer_fwd_cnt
= le32_to_cpu(pkt
->hdr
.fwd_cnt
);
1008 space_available
= virtio_transport_has_space(vsk
);
1009 spin_unlock_bh(&vvs
->tx_lock
);
1010 return space_available
;
1013 /* Handle server socket */
1015 virtio_transport_recv_listen(struct sock
*sk
, struct virtio_vsock_pkt
*pkt
,
1016 struct virtio_transport
*t
)
1018 struct vsock_sock
*vsk
= vsock_sk(sk
);
1019 struct vsock_sock
*vchild
;
1023 if (le16_to_cpu(pkt
->hdr
.op
) != VIRTIO_VSOCK_OP_REQUEST
) {
1024 virtio_transport_reset(vsk
, pkt
);
1028 if (sk_acceptq_is_full(sk
)) {
1029 virtio_transport_reset(vsk
, pkt
);
1033 child
= vsock_create_connected(sk
);
1035 virtio_transport_reset(vsk
, pkt
);
1039 sk_acceptq_added(sk
);
1041 lock_sock_nested(child
, SINGLE_DEPTH_NESTING
);
1043 child
->sk_state
= TCP_ESTABLISHED
;
1045 vchild
= vsock_sk(child
);
1046 vsock_addr_init(&vchild
->local_addr
, le64_to_cpu(pkt
->hdr
.dst_cid
),
1047 le32_to_cpu(pkt
->hdr
.dst_port
));
1048 vsock_addr_init(&vchild
->remote_addr
, le64_to_cpu(pkt
->hdr
.src_cid
),
1049 le32_to_cpu(pkt
->hdr
.src_port
));
1051 ret
= vsock_assign_transport(vchild
, vsk
);
1052 /* Transport assigned (looking at remote_addr) must be the same
1053 * where we received the request.
1055 if (ret
|| vchild
->transport
!= &t
->transport
) {
1056 release_sock(child
);
1057 virtio_transport_reset(vsk
, pkt
);
1062 if (virtio_transport_space_update(child
, pkt
))
1063 child
->sk_write_space(child
);
1065 vsock_insert_connected(vchild
);
1066 vsock_enqueue_accept(sk
, child
);
1067 virtio_transport_send_response(vchild
, pkt
);
1069 release_sock(child
);
1071 sk
->sk_data_ready(sk
);
1075 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
1078 void virtio_transport_recv_pkt(struct virtio_transport
*t
,
1079 struct virtio_vsock_pkt
*pkt
)
1081 struct sockaddr_vm src
, dst
;
1082 struct vsock_sock
*vsk
;
1084 bool space_available
;
1086 vsock_addr_init(&src
, le64_to_cpu(pkt
->hdr
.src_cid
),
1087 le32_to_cpu(pkt
->hdr
.src_port
));
1088 vsock_addr_init(&dst
, le64_to_cpu(pkt
->hdr
.dst_cid
),
1089 le32_to_cpu(pkt
->hdr
.dst_port
));
1091 trace_virtio_transport_recv_pkt(src
.svm_cid
, src
.svm_port
,
1092 dst
.svm_cid
, dst
.svm_port
,
1093 le32_to_cpu(pkt
->hdr
.len
),
1094 le16_to_cpu(pkt
->hdr
.type
),
1095 le16_to_cpu(pkt
->hdr
.op
),
1096 le32_to_cpu(pkt
->hdr
.flags
),
1097 le32_to_cpu(pkt
->hdr
.buf_alloc
),
1098 le32_to_cpu(pkt
->hdr
.fwd_cnt
));
1100 if (le16_to_cpu(pkt
->hdr
.type
) != VIRTIO_VSOCK_TYPE_STREAM
) {
1101 (void)virtio_transport_reset_no_sock(t
, pkt
);
1105 /* The socket must be in connected or bound table
1106 * otherwise send reset back
1108 sk
= vsock_find_connected_socket(&src
, &dst
);
1110 sk
= vsock_find_bound_socket(&dst
);
1112 (void)virtio_transport_reset_no_sock(t
, pkt
);
1119 space_available
= virtio_transport_space_update(sk
, pkt
);
1123 /* Update CID in case it has changed after a transport reset event */
1124 vsk
->local_addr
.svm_cid
= dst
.svm_cid
;
1126 if (space_available
)
1127 sk
->sk_write_space(sk
);
1129 switch (sk
->sk_state
) {
1131 virtio_transport_recv_listen(sk
, pkt
, t
);
1132 virtio_transport_free_pkt(pkt
);
1135 virtio_transport_recv_connecting(sk
, pkt
);
1136 virtio_transport_free_pkt(pkt
);
1138 case TCP_ESTABLISHED
:
1139 virtio_transport_recv_connected(sk
, pkt
);
1142 virtio_transport_recv_disconnecting(sk
, pkt
);
1143 virtio_transport_free_pkt(pkt
);
1146 virtio_transport_free_pkt(pkt
);
1152 /* Release refcnt obtained when we fetched this socket out of the
1153 * bound or connected list.
1159 virtio_transport_free_pkt(pkt
);
1161 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt
);
1163 void virtio_transport_free_pkt(struct virtio_vsock_pkt
*pkt
)
1168 EXPORT_SYMBOL_GPL(virtio_transport_free_pkt
);
1170 MODULE_LICENSE("GPL v2");
1171 MODULE_AUTHOR("Asias He");
1172 MODULE_DESCRIPTION("common code for virtio vsock");