2 * common code for virtio vsock
4 * Copyright (C) 2013-2015 Red Hat, Inc.
5 * Author: Asias He <asias@redhat.com>
6 * Stefan Hajnoczi <stefanha@redhat.com>
8 * This work is licensed under the terms of the GNU GPL, version 2.
10 #include <linux/spinlock.h>
11 #include <linux/module.h>
12 #include <linux/sched/signal.h>
13 #include <linux/ctype.h>
14 #include <linux/list.h>
15 #include <linux/virtio.h>
16 #include <linux/virtio_ids.h>
17 #include <linux/virtio_config.h>
18 #include <linux/virtio_vsock.h>
19 #include <uapi/linux/vsockmon.h>
22 #include <net/af_vsock.h>
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/vsock_virtio_transport_common.h>
27 /* How long to wait for graceful shutdown of a connection */
28 #define VSOCK_CLOSE_TIMEOUT (8 * HZ)
30 static const struct virtio_transport
*virtio_transport_get_ops(void)
32 const struct vsock_transport
*t
= vsock_core_get_transport();
34 return container_of(t
, struct virtio_transport
, transport
);
37 static struct virtio_vsock_pkt
*
38 virtio_transport_alloc_pkt(struct virtio_vsock_pkt_info
*info
,
45 struct virtio_vsock_pkt
*pkt
;
48 pkt
= kzalloc(sizeof(*pkt
), GFP_KERNEL
);
52 pkt
->hdr
.type
= cpu_to_le16(info
->type
);
53 pkt
->hdr
.op
= cpu_to_le16(info
->op
);
54 pkt
->hdr
.src_cid
= cpu_to_le64(src_cid
);
55 pkt
->hdr
.dst_cid
= cpu_to_le64(dst_cid
);
56 pkt
->hdr
.src_port
= cpu_to_le32(src_port
);
57 pkt
->hdr
.dst_port
= cpu_to_le32(dst_port
);
58 pkt
->hdr
.flags
= cpu_to_le32(info
->flags
);
60 pkt
->hdr
.len
= cpu_to_le32(len
);
61 pkt
->reply
= info
->reply
;
64 if (info
->msg
&& len
> 0) {
65 pkt
->buf
= kmalloc(len
, GFP_KERNEL
);
68 err
= memcpy_from_msg(pkt
->buf
, info
->msg
, len
);
73 trace_virtio_transport_alloc_pkt(src_cid
, src_port
,
90 static struct sk_buff
*virtio_transport_build_skb(void *opaque
)
92 struct virtio_vsock_pkt
*pkt
= opaque
;
93 struct af_vsockmon_hdr
*hdr
;
96 skb
= alloc_skb(sizeof(*hdr
) + sizeof(pkt
->hdr
) + pkt
->len
,
101 hdr
= skb_put(skb
, sizeof(*hdr
));
103 /* pkt->hdr is little-endian so no need to byteswap here */
104 hdr
->src_cid
= pkt
->hdr
.src_cid
;
105 hdr
->src_port
= pkt
->hdr
.src_port
;
106 hdr
->dst_cid
= pkt
->hdr
.dst_cid
;
107 hdr
->dst_port
= pkt
->hdr
.dst_port
;
109 hdr
->transport
= cpu_to_le16(AF_VSOCK_TRANSPORT_VIRTIO
);
110 hdr
->len
= cpu_to_le16(sizeof(pkt
->hdr
));
111 memset(hdr
->reserved
, 0, sizeof(hdr
->reserved
));
113 switch (le16_to_cpu(pkt
->hdr
.op
)) {
114 case VIRTIO_VSOCK_OP_REQUEST
:
115 case VIRTIO_VSOCK_OP_RESPONSE
:
116 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_CONNECT
);
118 case VIRTIO_VSOCK_OP_RST
:
119 case VIRTIO_VSOCK_OP_SHUTDOWN
:
120 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_DISCONNECT
);
122 case VIRTIO_VSOCK_OP_RW
:
123 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_PAYLOAD
);
125 case VIRTIO_VSOCK_OP_CREDIT_UPDATE
:
126 case VIRTIO_VSOCK_OP_CREDIT_REQUEST
:
127 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_CONTROL
);
130 hdr
->op
= cpu_to_le16(AF_VSOCK_OP_UNKNOWN
);
134 skb_put_data(skb
, &pkt
->hdr
, sizeof(pkt
->hdr
));
137 skb_put_data(skb
, pkt
->buf
, pkt
->len
);
143 void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt
*pkt
)
145 vsock_deliver_tap(virtio_transport_build_skb
, pkt
);
147 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt
);
149 static int virtio_transport_send_pkt_info(struct vsock_sock
*vsk
,
150 struct virtio_vsock_pkt_info
*info
)
152 u32 src_cid
, src_port
, dst_cid
, dst_port
;
153 struct virtio_vsock_sock
*vvs
;
154 struct virtio_vsock_pkt
*pkt
;
155 u32 pkt_len
= info
->pkt_len
;
157 src_cid
= vm_sockets_get_local_cid();
158 src_port
= vsk
->local_addr
.svm_port
;
159 if (!info
->remote_cid
) {
160 dst_cid
= vsk
->remote_addr
.svm_cid
;
161 dst_port
= vsk
->remote_addr
.svm_port
;
163 dst_cid
= info
->remote_cid
;
164 dst_port
= info
->remote_port
;
169 /* we can send less than pkt_len bytes */
170 if (pkt_len
> VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE
)
171 pkt_len
= VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE
;
173 /* virtio_transport_get_credit might return less than pkt_len credit */
174 pkt_len
= virtio_transport_get_credit(vvs
, pkt_len
);
176 /* Do not send zero length OP_RW pkt */
177 if (pkt_len
== 0 && info
->op
== VIRTIO_VSOCK_OP_RW
)
180 pkt
= virtio_transport_alloc_pkt(info
, pkt_len
,
184 virtio_transport_put_credit(vvs
, pkt_len
);
188 virtio_transport_inc_tx_pkt(vvs
, pkt
);
190 return virtio_transport_get_ops()->send_pkt(pkt
);
193 static void virtio_transport_inc_rx_pkt(struct virtio_vsock_sock
*vvs
,
194 struct virtio_vsock_pkt
*pkt
)
196 vvs
->rx_bytes
+= pkt
->len
;
199 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock
*vvs
,
200 struct virtio_vsock_pkt
*pkt
)
202 vvs
->rx_bytes
-= pkt
->len
;
203 vvs
->fwd_cnt
+= pkt
->len
;
206 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock
*vvs
, struct virtio_vsock_pkt
*pkt
)
208 spin_lock_bh(&vvs
->tx_lock
);
209 pkt
->hdr
.fwd_cnt
= cpu_to_le32(vvs
->fwd_cnt
);
210 pkt
->hdr
.buf_alloc
= cpu_to_le32(vvs
->buf_alloc
);
211 spin_unlock_bh(&vvs
->tx_lock
);
213 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt
);
215 u32
virtio_transport_get_credit(struct virtio_vsock_sock
*vvs
, u32 credit
)
219 spin_lock_bh(&vvs
->tx_lock
);
220 ret
= vvs
->peer_buf_alloc
- (vvs
->tx_cnt
- vvs
->peer_fwd_cnt
);
224 spin_unlock_bh(&vvs
->tx_lock
);
228 EXPORT_SYMBOL_GPL(virtio_transport_get_credit
);
230 void virtio_transport_put_credit(struct virtio_vsock_sock
*vvs
, u32 credit
)
232 spin_lock_bh(&vvs
->tx_lock
);
233 vvs
->tx_cnt
-= credit
;
234 spin_unlock_bh(&vvs
->tx_lock
);
236 EXPORT_SYMBOL_GPL(virtio_transport_put_credit
);
238 static int virtio_transport_send_credit_update(struct vsock_sock
*vsk
,
240 struct virtio_vsock_hdr
*hdr
)
242 struct virtio_vsock_pkt_info info
= {
243 .op
= VIRTIO_VSOCK_OP_CREDIT_UPDATE
,
248 return virtio_transport_send_pkt_info(vsk
, &info
);
252 virtio_transport_stream_do_dequeue(struct vsock_sock
*vsk
,
256 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
257 struct virtio_vsock_pkt
*pkt
;
258 size_t bytes
, total
= 0;
261 spin_lock_bh(&vvs
->rx_lock
);
262 while (total
< len
&& !list_empty(&vvs
->rx_queue
)) {
263 pkt
= list_first_entry(&vvs
->rx_queue
,
264 struct virtio_vsock_pkt
, list
);
267 if (bytes
> pkt
->len
- pkt
->off
)
268 bytes
= pkt
->len
- pkt
->off
;
270 /* sk_lock is held by caller so no one else can dequeue.
271 * Unlock rx_lock since memcpy_to_msg() may sleep.
273 spin_unlock_bh(&vvs
->rx_lock
);
275 err
= memcpy_to_msg(msg
, pkt
->buf
+ pkt
->off
, bytes
);
279 spin_lock_bh(&vvs
->rx_lock
);
283 if (pkt
->off
== pkt
->len
) {
284 virtio_transport_dec_rx_pkt(vvs
, pkt
);
285 list_del(&pkt
->list
);
286 virtio_transport_free_pkt(pkt
);
289 spin_unlock_bh(&vvs
->rx_lock
);
291 /* Send a credit pkt to peer */
292 virtio_transport_send_credit_update(vsk
, VIRTIO_VSOCK_TYPE_STREAM
,
304 virtio_transport_stream_dequeue(struct vsock_sock
*vsk
,
306 size_t len
, int flags
)
308 if (flags
& MSG_PEEK
)
311 return virtio_transport_stream_do_dequeue(vsk
, msg
, len
);
313 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue
);
316 virtio_transport_dgram_dequeue(struct vsock_sock
*vsk
,
318 size_t len
, int flags
)
322 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue
);
324 s64
virtio_transport_stream_has_data(struct vsock_sock
*vsk
)
326 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
329 spin_lock_bh(&vvs
->rx_lock
);
330 bytes
= vvs
->rx_bytes
;
331 spin_unlock_bh(&vvs
->rx_lock
);
335 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data
);
337 static s64
virtio_transport_has_space(struct vsock_sock
*vsk
)
339 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
342 bytes
= vvs
->peer_buf_alloc
- (vvs
->tx_cnt
- vvs
->peer_fwd_cnt
);
349 s64
virtio_transport_stream_has_space(struct vsock_sock
*vsk
)
351 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
354 spin_lock_bh(&vvs
->tx_lock
);
355 bytes
= virtio_transport_has_space(vsk
);
356 spin_unlock_bh(&vvs
->tx_lock
);
360 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space
);
362 int virtio_transport_do_socket_init(struct vsock_sock
*vsk
,
363 struct vsock_sock
*psk
)
365 struct virtio_vsock_sock
*vvs
;
367 vvs
= kzalloc(sizeof(*vvs
), GFP_KERNEL
);
374 struct virtio_vsock_sock
*ptrans
= psk
->trans
;
376 vvs
->buf_size
= ptrans
->buf_size
;
377 vvs
->buf_size_min
= ptrans
->buf_size_min
;
378 vvs
->buf_size_max
= ptrans
->buf_size_max
;
379 vvs
->peer_buf_alloc
= ptrans
->peer_buf_alloc
;
381 vvs
->buf_size
= VIRTIO_VSOCK_DEFAULT_BUF_SIZE
;
382 vvs
->buf_size_min
= VIRTIO_VSOCK_DEFAULT_MIN_BUF_SIZE
;
383 vvs
->buf_size_max
= VIRTIO_VSOCK_DEFAULT_MAX_BUF_SIZE
;
386 vvs
->buf_alloc
= vvs
->buf_size
;
388 spin_lock_init(&vvs
->rx_lock
);
389 spin_lock_init(&vvs
->tx_lock
);
390 INIT_LIST_HEAD(&vvs
->rx_queue
);
394 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init
);
396 u64
virtio_transport_get_buffer_size(struct vsock_sock
*vsk
)
398 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
400 return vvs
->buf_size
;
402 EXPORT_SYMBOL_GPL(virtio_transport_get_buffer_size
);
404 u64
virtio_transport_get_min_buffer_size(struct vsock_sock
*vsk
)
406 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
408 return vvs
->buf_size_min
;
410 EXPORT_SYMBOL_GPL(virtio_transport_get_min_buffer_size
);
412 u64
virtio_transport_get_max_buffer_size(struct vsock_sock
*vsk
)
414 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
416 return vvs
->buf_size_max
;
418 EXPORT_SYMBOL_GPL(virtio_transport_get_max_buffer_size
);
420 void virtio_transport_set_buffer_size(struct vsock_sock
*vsk
, u64 val
)
422 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
424 if (val
> VIRTIO_VSOCK_MAX_BUF_SIZE
)
425 val
= VIRTIO_VSOCK_MAX_BUF_SIZE
;
426 if (val
< vvs
->buf_size_min
)
427 vvs
->buf_size_min
= val
;
428 if (val
> vvs
->buf_size_max
)
429 vvs
->buf_size_max
= val
;
431 vvs
->buf_alloc
= val
;
433 EXPORT_SYMBOL_GPL(virtio_transport_set_buffer_size
);
435 void virtio_transport_set_min_buffer_size(struct vsock_sock
*vsk
, u64 val
)
437 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
439 if (val
> VIRTIO_VSOCK_MAX_BUF_SIZE
)
440 val
= VIRTIO_VSOCK_MAX_BUF_SIZE
;
441 if (val
> vvs
->buf_size
)
443 vvs
->buf_size_min
= val
;
445 EXPORT_SYMBOL_GPL(virtio_transport_set_min_buffer_size
);
447 void virtio_transport_set_max_buffer_size(struct vsock_sock
*vsk
, u64 val
)
449 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
451 if (val
> VIRTIO_VSOCK_MAX_BUF_SIZE
)
452 val
= VIRTIO_VSOCK_MAX_BUF_SIZE
;
453 if (val
< vvs
->buf_size
)
455 vvs
->buf_size_max
= val
;
457 EXPORT_SYMBOL_GPL(virtio_transport_set_max_buffer_size
);
460 virtio_transport_notify_poll_in(struct vsock_sock
*vsk
,
462 bool *data_ready_now
)
464 if (vsock_stream_has_data(vsk
))
465 *data_ready_now
= true;
467 *data_ready_now
= false;
471 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in
);
474 virtio_transport_notify_poll_out(struct vsock_sock
*vsk
,
476 bool *space_avail_now
)
480 free_space
= vsock_stream_has_space(vsk
);
482 *space_avail_now
= true;
483 else if (free_space
== 0)
484 *space_avail_now
= false;
488 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out
);
490 int virtio_transport_notify_recv_init(struct vsock_sock
*vsk
,
491 size_t target
, struct vsock_transport_recv_notify_data
*data
)
495 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init
);
497 int virtio_transport_notify_recv_pre_block(struct vsock_sock
*vsk
,
498 size_t target
, struct vsock_transport_recv_notify_data
*data
)
502 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block
);
504 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock
*vsk
,
505 size_t target
, struct vsock_transport_recv_notify_data
*data
)
509 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue
);
511 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock
*vsk
,
512 size_t target
, ssize_t copied
, bool data_read
,
513 struct vsock_transport_recv_notify_data
*data
)
517 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue
);
519 int virtio_transport_notify_send_init(struct vsock_sock
*vsk
,
520 struct vsock_transport_send_notify_data
*data
)
524 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init
);
526 int virtio_transport_notify_send_pre_block(struct vsock_sock
*vsk
,
527 struct vsock_transport_send_notify_data
*data
)
531 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block
);
533 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock
*vsk
,
534 struct vsock_transport_send_notify_data
*data
)
538 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue
);
540 int virtio_transport_notify_send_post_enqueue(struct vsock_sock
*vsk
,
541 ssize_t written
, struct vsock_transport_send_notify_data
*data
)
545 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue
);
547 u64
virtio_transport_stream_rcvhiwat(struct vsock_sock
*vsk
)
549 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
551 return vvs
->buf_size
;
553 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat
);
555 bool virtio_transport_stream_is_active(struct vsock_sock
*vsk
)
559 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active
);
561 bool virtio_transport_stream_allow(u32 cid
, u32 port
)
565 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow
);
567 int virtio_transport_dgram_bind(struct vsock_sock
*vsk
,
568 struct sockaddr_vm
*addr
)
572 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind
);
574 bool virtio_transport_dgram_allow(u32 cid
, u32 port
)
578 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow
);
580 int virtio_transport_connect(struct vsock_sock
*vsk
)
582 struct virtio_vsock_pkt_info info
= {
583 .op
= VIRTIO_VSOCK_OP_REQUEST
,
584 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
588 return virtio_transport_send_pkt_info(vsk
, &info
);
590 EXPORT_SYMBOL_GPL(virtio_transport_connect
);
592 int virtio_transport_shutdown(struct vsock_sock
*vsk
, int mode
)
594 struct virtio_vsock_pkt_info info
= {
595 .op
= VIRTIO_VSOCK_OP_SHUTDOWN
,
596 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
597 .flags
= (mode
& RCV_SHUTDOWN
?
598 VIRTIO_VSOCK_SHUTDOWN_RCV
: 0) |
599 (mode
& SEND_SHUTDOWN
?
600 VIRTIO_VSOCK_SHUTDOWN_SEND
: 0),
604 return virtio_transport_send_pkt_info(vsk
, &info
);
606 EXPORT_SYMBOL_GPL(virtio_transport_shutdown
);
609 virtio_transport_dgram_enqueue(struct vsock_sock
*vsk
,
610 struct sockaddr_vm
*remote_addr
,
616 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue
);
619 virtio_transport_stream_enqueue(struct vsock_sock
*vsk
,
623 struct virtio_vsock_pkt_info info
= {
624 .op
= VIRTIO_VSOCK_OP_RW
,
625 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
631 return virtio_transport_send_pkt_info(vsk
, &info
);
633 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue
);
635 void virtio_transport_destruct(struct vsock_sock
*vsk
)
637 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
641 EXPORT_SYMBOL_GPL(virtio_transport_destruct
);
643 static int virtio_transport_reset(struct vsock_sock
*vsk
,
644 struct virtio_vsock_pkt
*pkt
)
646 struct virtio_vsock_pkt_info info
= {
647 .op
= VIRTIO_VSOCK_OP_RST
,
648 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
653 /* Send RST only if the original pkt is not a RST pkt */
654 if (pkt
&& le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
657 return virtio_transport_send_pkt_info(vsk
, &info
);
660 /* Normally packets are associated with a socket. There may be no socket if an
661 * attempt was made to connect to a socket that does not exist.
663 static int virtio_transport_reset_no_sock(struct virtio_vsock_pkt
*pkt
)
665 const struct virtio_transport
*t
;
666 struct virtio_vsock_pkt
*reply
;
667 struct virtio_vsock_pkt_info info
= {
668 .op
= VIRTIO_VSOCK_OP_RST
,
669 .type
= le16_to_cpu(pkt
->hdr
.type
),
673 /* Send RST only if the original pkt is not a RST pkt */
674 if (le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
677 reply
= virtio_transport_alloc_pkt(&info
, 0,
678 le64_to_cpu(pkt
->hdr
.dst_cid
),
679 le32_to_cpu(pkt
->hdr
.dst_port
),
680 le64_to_cpu(pkt
->hdr
.src_cid
),
681 le32_to_cpu(pkt
->hdr
.src_port
));
685 t
= virtio_transport_get_ops();
687 virtio_transport_free_pkt(reply
);
691 return t
->send_pkt(reply
);
694 static void virtio_transport_wait_close(struct sock
*sk
, long timeout
)
697 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
699 add_wait_queue(sk_sleep(sk
), &wait
);
702 if (sk_wait_event(sk
, &timeout
,
703 sock_flag(sk
, SOCK_DONE
), &wait
))
705 } while (!signal_pending(current
) && timeout
);
707 remove_wait_queue(sk_sleep(sk
), &wait
);
711 static void virtio_transport_do_close(struct vsock_sock
*vsk
,
714 struct sock
*sk
= sk_vsock(vsk
);
716 sock_set_flag(sk
, SOCK_DONE
);
717 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
718 if (vsock_stream_has_data(vsk
) <= 0)
719 sk
->sk_state
= TCP_CLOSING
;
720 sk
->sk_state_change(sk
);
722 if (vsk
->close_work_scheduled
&&
723 (!cancel_timeout
|| cancel_delayed_work(&vsk
->close_work
))) {
724 vsk
->close_work_scheduled
= false;
726 vsock_remove_sock(vsk
);
728 /* Release refcnt obtained when we scheduled the timeout */
733 static void virtio_transport_close_timeout(struct work_struct
*work
)
735 struct vsock_sock
*vsk
=
736 container_of(work
, struct vsock_sock
, close_work
.work
);
737 struct sock
*sk
= sk_vsock(vsk
);
742 if (!sock_flag(sk
, SOCK_DONE
)) {
743 (void)virtio_transport_reset(vsk
, NULL
);
745 virtio_transport_do_close(vsk
, false);
748 vsk
->close_work_scheduled
= false;
754 /* User context, vsk->sk is locked */
755 static bool virtio_transport_close(struct vsock_sock
*vsk
)
757 struct sock
*sk
= &vsk
->sk
;
759 if (!(sk
->sk_state
== TCP_ESTABLISHED
||
760 sk
->sk_state
== TCP_CLOSING
))
763 /* Already received SHUTDOWN from peer, reply with RST */
764 if ((vsk
->peer_shutdown
& SHUTDOWN_MASK
) == SHUTDOWN_MASK
) {
765 (void)virtio_transport_reset(vsk
, NULL
);
769 if ((sk
->sk_shutdown
& SHUTDOWN_MASK
) != SHUTDOWN_MASK
)
770 (void)virtio_transport_shutdown(vsk
, SHUTDOWN_MASK
);
772 if (sock_flag(sk
, SOCK_LINGER
) && !(current
->flags
& PF_EXITING
))
773 virtio_transport_wait_close(sk
, sk
->sk_lingertime
);
775 if (sock_flag(sk
, SOCK_DONE
)) {
780 INIT_DELAYED_WORK(&vsk
->close_work
,
781 virtio_transport_close_timeout
);
782 vsk
->close_work_scheduled
= true;
783 schedule_delayed_work(&vsk
->close_work
, VSOCK_CLOSE_TIMEOUT
);
787 void virtio_transport_release(struct vsock_sock
*vsk
)
789 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
790 struct virtio_vsock_pkt
*pkt
, *tmp
;
791 struct sock
*sk
= &vsk
->sk
;
792 bool remove_sock
= true;
795 if (sk
->sk_type
== SOCK_STREAM
)
796 remove_sock
= virtio_transport_close(vsk
);
798 list_for_each_entry_safe(pkt
, tmp
, &vvs
->rx_queue
, list
) {
799 list_del(&pkt
->list
);
800 virtio_transport_free_pkt(pkt
);
805 vsock_remove_sock(vsk
);
807 EXPORT_SYMBOL_GPL(virtio_transport_release
);
810 virtio_transport_recv_connecting(struct sock
*sk
,
811 struct virtio_vsock_pkt
*pkt
)
813 struct vsock_sock
*vsk
= vsock_sk(sk
);
817 switch (le16_to_cpu(pkt
->hdr
.op
)) {
818 case VIRTIO_VSOCK_OP_RESPONSE
:
819 sk
->sk_state
= TCP_ESTABLISHED
;
820 sk
->sk_socket
->state
= SS_CONNECTED
;
821 vsock_insert_connected(vsk
);
822 sk
->sk_state_change(sk
);
824 case VIRTIO_VSOCK_OP_INVALID
:
826 case VIRTIO_VSOCK_OP_RST
:
838 virtio_transport_reset(vsk
, pkt
);
839 sk
->sk_state
= TCP_CLOSE
;
841 sk
->sk_error_report(sk
);
846 virtio_transport_recv_connected(struct sock
*sk
,
847 struct virtio_vsock_pkt
*pkt
)
849 struct vsock_sock
*vsk
= vsock_sk(sk
);
850 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
853 switch (le16_to_cpu(pkt
->hdr
.op
)) {
854 case VIRTIO_VSOCK_OP_RW
:
855 pkt
->len
= le32_to_cpu(pkt
->hdr
.len
);
858 spin_lock_bh(&vvs
->rx_lock
);
859 virtio_transport_inc_rx_pkt(vvs
, pkt
);
860 list_add_tail(&pkt
->list
, &vvs
->rx_queue
);
861 spin_unlock_bh(&vvs
->rx_lock
);
863 sk
->sk_data_ready(sk
);
865 case VIRTIO_VSOCK_OP_CREDIT_UPDATE
:
866 sk
->sk_write_space(sk
);
868 case VIRTIO_VSOCK_OP_SHUTDOWN
:
869 if (le32_to_cpu(pkt
->hdr
.flags
) & VIRTIO_VSOCK_SHUTDOWN_RCV
)
870 vsk
->peer_shutdown
|= RCV_SHUTDOWN
;
871 if (le32_to_cpu(pkt
->hdr
.flags
) & VIRTIO_VSOCK_SHUTDOWN_SEND
)
872 vsk
->peer_shutdown
|= SEND_SHUTDOWN
;
873 if (vsk
->peer_shutdown
== SHUTDOWN_MASK
&&
874 vsock_stream_has_data(vsk
) <= 0) {
875 sock_set_flag(sk
, SOCK_DONE
);
876 sk
->sk_state
= TCP_CLOSING
;
878 if (le32_to_cpu(pkt
->hdr
.flags
))
879 sk
->sk_state_change(sk
);
881 case VIRTIO_VSOCK_OP_RST
:
882 virtio_transport_do_close(vsk
, true);
889 virtio_transport_free_pkt(pkt
);
894 virtio_transport_recv_disconnecting(struct sock
*sk
,
895 struct virtio_vsock_pkt
*pkt
)
897 struct vsock_sock
*vsk
= vsock_sk(sk
);
899 if (le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
900 virtio_transport_do_close(vsk
, true);
904 virtio_transport_send_response(struct vsock_sock
*vsk
,
905 struct virtio_vsock_pkt
*pkt
)
907 struct virtio_vsock_pkt_info info
= {
908 .op
= VIRTIO_VSOCK_OP_RESPONSE
,
909 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
910 .remote_cid
= le64_to_cpu(pkt
->hdr
.src_cid
),
911 .remote_port
= le32_to_cpu(pkt
->hdr
.src_port
),
916 return virtio_transport_send_pkt_info(vsk
, &info
);
919 /* Handle server socket */
921 virtio_transport_recv_listen(struct sock
*sk
, struct virtio_vsock_pkt
*pkt
)
923 struct vsock_sock
*vsk
= vsock_sk(sk
);
924 struct vsock_sock
*vchild
;
927 if (le16_to_cpu(pkt
->hdr
.op
) != VIRTIO_VSOCK_OP_REQUEST
) {
928 virtio_transport_reset(vsk
, pkt
);
932 if (sk_acceptq_is_full(sk
)) {
933 virtio_transport_reset(vsk
, pkt
);
937 child
= __vsock_create(sock_net(sk
), NULL
, sk
, GFP_KERNEL
,
940 virtio_transport_reset(vsk
, pkt
);
944 sk
->sk_ack_backlog
++;
946 lock_sock_nested(child
, SINGLE_DEPTH_NESTING
);
948 child
->sk_state
= TCP_ESTABLISHED
;
950 vchild
= vsock_sk(child
);
951 vsock_addr_init(&vchild
->local_addr
, le64_to_cpu(pkt
->hdr
.dst_cid
),
952 le32_to_cpu(pkt
->hdr
.dst_port
));
953 vsock_addr_init(&vchild
->remote_addr
, le64_to_cpu(pkt
->hdr
.src_cid
),
954 le32_to_cpu(pkt
->hdr
.src_port
));
956 vsock_insert_connected(vchild
);
957 vsock_enqueue_accept(sk
, child
);
958 virtio_transport_send_response(vchild
, pkt
);
962 sk
->sk_data_ready(sk
);
966 static bool virtio_transport_space_update(struct sock
*sk
,
967 struct virtio_vsock_pkt
*pkt
)
969 struct vsock_sock
*vsk
= vsock_sk(sk
);
970 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
971 bool space_available
;
973 /* buf_alloc and fwd_cnt is always included in the hdr */
974 spin_lock_bh(&vvs
->tx_lock
);
975 vvs
->peer_buf_alloc
= le32_to_cpu(pkt
->hdr
.buf_alloc
);
976 vvs
->peer_fwd_cnt
= le32_to_cpu(pkt
->hdr
.fwd_cnt
);
977 space_available
= virtio_transport_has_space(vsk
);
978 spin_unlock_bh(&vvs
->tx_lock
);
979 return space_available
;
982 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
985 void virtio_transport_recv_pkt(struct virtio_vsock_pkt
*pkt
)
987 struct sockaddr_vm src
, dst
;
988 struct vsock_sock
*vsk
;
990 bool space_available
;
992 vsock_addr_init(&src
, le64_to_cpu(pkt
->hdr
.src_cid
),
993 le32_to_cpu(pkt
->hdr
.src_port
));
994 vsock_addr_init(&dst
, le64_to_cpu(pkt
->hdr
.dst_cid
),
995 le32_to_cpu(pkt
->hdr
.dst_port
));
997 trace_virtio_transport_recv_pkt(src
.svm_cid
, src
.svm_port
,
998 dst
.svm_cid
, dst
.svm_port
,
999 le32_to_cpu(pkt
->hdr
.len
),
1000 le16_to_cpu(pkt
->hdr
.type
),
1001 le16_to_cpu(pkt
->hdr
.op
),
1002 le32_to_cpu(pkt
->hdr
.flags
),
1003 le32_to_cpu(pkt
->hdr
.buf_alloc
),
1004 le32_to_cpu(pkt
->hdr
.fwd_cnt
));
1006 if (le16_to_cpu(pkt
->hdr
.type
) != VIRTIO_VSOCK_TYPE_STREAM
) {
1007 (void)virtio_transport_reset_no_sock(pkt
);
1011 /* The socket must be in connected or bound table
1012 * otherwise send reset back
1014 sk
= vsock_find_connected_socket(&src
, &dst
);
1016 sk
= vsock_find_bound_socket(&dst
);
1018 (void)virtio_transport_reset_no_sock(pkt
);
1025 space_available
= virtio_transport_space_update(sk
, pkt
);
1029 /* Update CID in case it has changed after a transport reset event */
1030 vsk
->local_addr
.svm_cid
= dst
.svm_cid
;
1032 if (space_available
)
1033 sk
->sk_write_space(sk
);
1035 switch (sk
->sk_state
) {
1037 virtio_transport_recv_listen(sk
, pkt
);
1038 virtio_transport_free_pkt(pkt
);
1041 virtio_transport_recv_connecting(sk
, pkt
);
1042 virtio_transport_free_pkt(pkt
);
1044 case TCP_ESTABLISHED
:
1045 virtio_transport_recv_connected(sk
, pkt
);
1048 virtio_transport_recv_disconnecting(sk
, pkt
);
1049 virtio_transport_free_pkt(pkt
);
1052 virtio_transport_free_pkt(pkt
);
1057 /* Release refcnt obtained when we fetched this socket out of the
1058 * bound or connected list.
1064 virtio_transport_free_pkt(pkt
);
1066 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt
);
1068 void virtio_transport_free_pkt(struct virtio_vsock_pkt
*pkt
)
1073 EXPORT_SYMBOL_GPL(virtio_transport_free_pkt
);
1075 MODULE_LICENSE("GPL v2");
1076 MODULE_AUTHOR("Asias He");
1077 MODULE_DESCRIPTION("common code for virtio vsock");