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_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 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 if (pkt
->tap_delivered
)
163 vsock_deliver_tap(virtio_transport_build_skb
, pkt
);
164 pkt
->tap_delivered
= true;
166 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt
);
168 /* This function can only be used on connecting/connected sockets,
169 * since a socket assigned to a transport is required.
171 * Do not use on listener sockets!
173 static int virtio_transport_send_pkt_info(struct vsock_sock
*vsk
,
174 struct virtio_vsock_pkt_info
*info
)
176 u32 src_cid
, src_port
, dst_cid
, dst_port
;
177 const struct virtio_transport
*t_ops
;
178 struct virtio_vsock_sock
*vvs
;
179 struct virtio_vsock_pkt
*pkt
;
180 u32 pkt_len
= info
->pkt_len
;
182 t_ops
= virtio_transport_get_ops(vsk
);
183 if (unlikely(!t_ops
))
186 src_cid
= t_ops
->transport
.get_local_cid();
187 src_port
= vsk
->local_addr
.svm_port
;
188 if (!info
->remote_cid
) {
189 dst_cid
= vsk
->remote_addr
.svm_cid
;
190 dst_port
= vsk
->remote_addr
.svm_port
;
192 dst_cid
= info
->remote_cid
;
193 dst_port
= info
->remote_port
;
198 /* we can send less than pkt_len bytes */
199 if (pkt_len
> VIRTIO_VSOCK_MAX_PKT_BUF_SIZE
)
200 pkt_len
= VIRTIO_VSOCK_MAX_PKT_BUF_SIZE
;
202 /* virtio_transport_get_credit might return less than pkt_len credit */
203 pkt_len
= virtio_transport_get_credit(vvs
, pkt_len
);
205 /* Do not send zero length OP_RW pkt */
206 if (pkt_len
== 0 && info
->op
== VIRTIO_VSOCK_OP_RW
)
209 pkt
= virtio_transport_alloc_pkt(info
, pkt_len
,
213 virtio_transport_put_credit(vvs
, pkt_len
);
217 virtio_transport_inc_tx_pkt(vvs
, pkt
);
219 return t_ops
->send_pkt(pkt
);
222 static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock
*vvs
,
223 struct virtio_vsock_pkt
*pkt
)
225 if (vvs
->rx_bytes
+ pkt
->len
> vvs
->buf_alloc
)
228 vvs
->rx_bytes
+= pkt
->len
;
232 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock
*vvs
,
233 struct virtio_vsock_pkt
*pkt
)
235 vvs
->rx_bytes
-= pkt
->len
;
236 vvs
->fwd_cnt
+= pkt
->len
;
239 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock
*vvs
, struct virtio_vsock_pkt
*pkt
)
241 spin_lock_bh(&vvs
->rx_lock
);
242 vvs
->last_fwd_cnt
= vvs
->fwd_cnt
;
243 pkt
->hdr
.fwd_cnt
= cpu_to_le32(vvs
->fwd_cnt
);
244 pkt
->hdr
.buf_alloc
= cpu_to_le32(vvs
->buf_alloc
);
245 spin_unlock_bh(&vvs
->rx_lock
);
247 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt
);
249 u32
virtio_transport_get_credit(struct virtio_vsock_sock
*vvs
, u32 credit
)
253 spin_lock_bh(&vvs
->tx_lock
);
254 ret
= vvs
->peer_buf_alloc
- (vvs
->tx_cnt
- vvs
->peer_fwd_cnt
);
258 spin_unlock_bh(&vvs
->tx_lock
);
262 EXPORT_SYMBOL_GPL(virtio_transport_get_credit
);
264 void virtio_transport_put_credit(struct virtio_vsock_sock
*vvs
, u32 credit
)
266 spin_lock_bh(&vvs
->tx_lock
);
267 vvs
->tx_cnt
-= credit
;
268 spin_unlock_bh(&vvs
->tx_lock
);
270 EXPORT_SYMBOL_GPL(virtio_transport_put_credit
);
272 static int virtio_transport_send_credit_update(struct vsock_sock
*vsk
,
274 struct virtio_vsock_hdr
*hdr
)
276 struct virtio_vsock_pkt_info info
= {
277 .op
= VIRTIO_VSOCK_OP_CREDIT_UPDATE
,
282 return virtio_transport_send_pkt_info(vsk
, &info
);
286 virtio_transport_stream_do_peek(struct vsock_sock
*vsk
,
290 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
291 struct virtio_vsock_pkt
*pkt
;
292 size_t bytes
, total
= 0, off
;
295 spin_lock_bh(&vvs
->rx_lock
);
297 list_for_each_entry(pkt
, &vvs
->rx_queue
, list
) {
303 while (total
< len
&& off
< pkt
->len
) {
305 if (bytes
> pkt
->len
- off
)
306 bytes
= pkt
->len
- off
;
308 /* sk_lock is held by caller so no one else can dequeue.
309 * Unlock rx_lock since memcpy_to_msg() may sleep.
311 spin_unlock_bh(&vvs
->rx_lock
);
313 err
= memcpy_to_msg(msg
, pkt
->buf
+ off
, bytes
);
317 spin_lock_bh(&vvs
->rx_lock
);
324 spin_unlock_bh(&vvs
->rx_lock
);
335 virtio_transport_stream_do_dequeue(struct vsock_sock
*vsk
,
339 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
340 struct virtio_vsock_pkt
*pkt
;
341 size_t bytes
, total
= 0;
345 spin_lock_bh(&vvs
->rx_lock
);
346 while (total
< len
&& !list_empty(&vvs
->rx_queue
)) {
347 pkt
= list_first_entry(&vvs
->rx_queue
,
348 struct virtio_vsock_pkt
, list
);
351 if (bytes
> pkt
->len
- pkt
->off
)
352 bytes
= pkt
->len
- pkt
->off
;
354 /* sk_lock is held by caller so no one else can dequeue.
355 * Unlock rx_lock since memcpy_to_msg() may sleep.
357 spin_unlock_bh(&vvs
->rx_lock
);
359 err
= memcpy_to_msg(msg
, pkt
->buf
+ pkt
->off
, bytes
);
363 spin_lock_bh(&vvs
->rx_lock
);
367 if (pkt
->off
== pkt
->len
) {
368 virtio_transport_dec_rx_pkt(vvs
, pkt
);
369 list_del(&pkt
->list
);
370 virtio_transport_free_pkt(pkt
);
374 free_space
= vvs
->buf_alloc
- (vvs
->fwd_cnt
- vvs
->last_fwd_cnt
);
376 spin_unlock_bh(&vvs
->rx_lock
);
378 /* To reduce the number of credit update messages,
379 * don't update credits as long as lots of space is available.
380 * Note: the limit chosen here is arbitrary. Setting the limit
381 * too high causes extra messages. Too low causes transmitter
382 * stalls. As stalls are in theory more expensive than extra
383 * messages, we set the limit to a high value. TODO: experiment
384 * with different values.
386 if (free_space
< VIRTIO_VSOCK_MAX_PKT_BUF_SIZE
) {
387 virtio_transport_send_credit_update(vsk
,
388 VIRTIO_VSOCK_TYPE_STREAM
,
401 virtio_transport_stream_dequeue(struct vsock_sock
*vsk
,
403 size_t len
, int flags
)
405 if (flags
& MSG_PEEK
)
406 return virtio_transport_stream_do_peek(vsk
, msg
, len
);
408 return virtio_transport_stream_do_dequeue(vsk
, msg
, len
);
410 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue
);
413 virtio_transport_dgram_dequeue(struct vsock_sock
*vsk
,
415 size_t len
, int flags
)
419 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue
);
421 s64
virtio_transport_stream_has_data(struct vsock_sock
*vsk
)
423 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
426 spin_lock_bh(&vvs
->rx_lock
);
427 bytes
= vvs
->rx_bytes
;
428 spin_unlock_bh(&vvs
->rx_lock
);
432 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data
);
434 static s64
virtio_transport_has_space(struct vsock_sock
*vsk
)
436 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
439 bytes
= vvs
->peer_buf_alloc
- (vvs
->tx_cnt
- vvs
->peer_fwd_cnt
);
446 s64
virtio_transport_stream_has_space(struct vsock_sock
*vsk
)
448 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
451 spin_lock_bh(&vvs
->tx_lock
);
452 bytes
= virtio_transport_has_space(vsk
);
453 spin_unlock_bh(&vvs
->tx_lock
);
457 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space
);
459 int virtio_transport_do_socket_init(struct vsock_sock
*vsk
,
460 struct vsock_sock
*psk
)
462 struct virtio_vsock_sock
*vvs
;
464 vvs
= kzalloc(sizeof(*vvs
), GFP_KERNEL
);
470 if (psk
&& psk
->trans
) {
471 struct virtio_vsock_sock
*ptrans
= psk
->trans
;
473 vvs
->peer_buf_alloc
= ptrans
->peer_buf_alloc
;
476 if (vsk
->buffer_size
> VIRTIO_VSOCK_MAX_BUF_SIZE
)
477 vsk
->buffer_size
= VIRTIO_VSOCK_MAX_BUF_SIZE
;
479 vvs
->buf_alloc
= vsk
->buffer_size
;
481 spin_lock_init(&vvs
->rx_lock
);
482 spin_lock_init(&vvs
->tx_lock
);
483 INIT_LIST_HEAD(&vvs
->rx_queue
);
487 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init
);
489 /* sk_lock held by the caller */
490 void virtio_transport_notify_buffer_size(struct vsock_sock
*vsk
, u64
*val
)
492 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
494 if (*val
> VIRTIO_VSOCK_MAX_BUF_SIZE
)
495 *val
= VIRTIO_VSOCK_MAX_BUF_SIZE
;
497 vvs
->buf_alloc
= *val
;
499 virtio_transport_send_credit_update(vsk
, VIRTIO_VSOCK_TYPE_STREAM
,
502 EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size
);
505 virtio_transport_notify_poll_in(struct vsock_sock
*vsk
,
507 bool *data_ready_now
)
509 if (vsock_stream_has_data(vsk
))
510 *data_ready_now
= true;
512 *data_ready_now
= false;
516 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in
);
519 virtio_transport_notify_poll_out(struct vsock_sock
*vsk
,
521 bool *space_avail_now
)
525 free_space
= vsock_stream_has_space(vsk
);
527 *space_avail_now
= true;
528 else if (free_space
== 0)
529 *space_avail_now
= false;
533 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out
);
535 int virtio_transport_notify_recv_init(struct vsock_sock
*vsk
,
536 size_t target
, struct vsock_transport_recv_notify_data
*data
)
540 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init
);
542 int virtio_transport_notify_recv_pre_block(struct vsock_sock
*vsk
,
543 size_t target
, struct vsock_transport_recv_notify_data
*data
)
547 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block
);
549 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock
*vsk
,
550 size_t target
, struct vsock_transport_recv_notify_data
*data
)
554 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue
);
556 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock
*vsk
,
557 size_t target
, ssize_t copied
, bool data_read
,
558 struct vsock_transport_recv_notify_data
*data
)
562 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue
);
564 int virtio_transport_notify_send_init(struct vsock_sock
*vsk
,
565 struct vsock_transport_send_notify_data
*data
)
569 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init
);
571 int virtio_transport_notify_send_pre_block(struct vsock_sock
*vsk
,
572 struct vsock_transport_send_notify_data
*data
)
576 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block
);
578 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock
*vsk
,
579 struct vsock_transport_send_notify_data
*data
)
583 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue
);
585 int virtio_transport_notify_send_post_enqueue(struct vsock_sock
*vsk
,
586 ssize_t written
, struct vsock_transport_send_notify_data
*data
)
590 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue
);
592 u64
virtio_transport_stream_rcvhiwat(struct vsock_sock
*vsk
)
594 return vsk
->buffer_size
;
596 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat
);
598 bool virtio_transport_stream_is_active(struct vsock_sock
*vsk
)
602 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active
);
604 bool virtio_transport_stream_allow(u32 cid
, u32 port
)
608 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow
);
610 int virtio_transport_dgram_bind(struct vsock_sock
*vsk
,
611 struct sockaddr_vm
*addr
)
615 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind
);
617 bool virtio_transport_dgram_allow(u32 cid
, u32 port
)
621 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow
);
623 int virtio_transport_connect(struct vsock_sock
*vsk
)
625 struct virtio_vsock_pkt_info info
= {
626 .op
= VIRTIO_VSOCK_OP_REQUEST
,
627 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
631 return virtio_transport_send_pkt_info(vsk
, &info
);
633 EXPORT_SYMBOL_GPL(virtio_transport_connect
);
635 int virtio_transport_shutdown(struct vsock_sock
*vsk
, int mode
)
637 struct virtio_vsock_pkt_info info
= {
638 .op
= VIRTIO_VSOCK_OP_SHUTDOWN
,
639 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
640 .flags
= (mode
& RCV_SHUTDOWN
?
641 VIRTIO_VSOCK_SHUTDOWN_RCV
: 0) |
642 (mode
& SEND_SHUTDOWN
?
643 VIRTIO_VSOCK_SHUTDOWN_SEND
: 0),
647 return virtio_transport_send_pkt_info(vsk
, &info
);
649 EXPORT_SYMBOL_GPL(virtio_transport_shutdown
);
652 virtio_transport_dgram_enqueue(struct vsock_sock
*vsk
,
653 struct sockaddr_vm
*remote_addr
,
659 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue
);
662 virtio_transport_stream_enqueue(struct vsock_sock
*vsk
,
666 struct virtio_vsock_pkt_info info
= {
667 .op
= VIRTIO_VSOCK_OP_RW
,
668 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
674 return virtio_transport_send_pkt_info(vsk
, &info
);
676 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue
);
678 void virtio_transport_destruct(struct vsock_sock
*vsk
)
680 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
684 EXPORT_SYMBOL_GPL(virtio_transport_destruct
);
686 static int virtio_transport_reset(struct vsock_sock
*vsk
,
687 struct virtio_vsock_pkt
*pkt
)
689 struct virtio_vsock_pkt_info info
= {
690 .op
= VIRTIO_VSOCK_OP_RST
,
691 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
696 /* Send RST only if the original pkt is not a RST pkt */
697 if (pkt
&& le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
700 return virtio_transport_send_pkt_info(vsk
, &info
);
703 /* Normally packets are associated with a socket. There may be no socket if an
704 * attempt was made to connect to a socket that does not exist.
706 static int virtio_transport_reset_no_sock(const struct virtio_transport
*t
,
707 struct virtio_vsock_pkt
*pkt
)
709 struct virtio_vsock_pkt
*reply
;
710 struct virtio_vsock_pkt_info info
= {
711 .op
= VIRTIO_VSOCK_OP_RST
,
712 .type
= le16_to_cpu(pkt
->hdr
.type
),
716 /* Send RST only if the original pkt is not a RST pkt */
717 if (le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
720 reply
= virtio_transport_alloc_pkt(&info
, 0,
721 le64_to_cpu(pkt
->hdr
.dst_cid
),
722 le32_to_cpu(pkt
->hdr
.dst_port
),
723 le64_to_cpu(pkt
->hdr
.src_cid
),
724 le32_to_cpu(pkt
->hdr
.src_port
));
729 virtio_transport_free_pkt(reply
);
733 return t
->send_pkt(reply
);
736 static void virtio_transport_wait_close(struct sock
*sk
, long timeout
)
739 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
741 add_wait_queue(sk_sleep(sk
), &wait
);
744 if (sk_wait_event(sk
, &timeout
,
745 sock_flag(sk
, SOCK_DONE
), &wait
))
747 } while (!signal_pending(current
) && timeout
);
749 remove_wait_queue(sk_sleep(sk
), &wait
);
753 static void virtio_transport_do_close(struct vsock_sock
*vsk
,
756 struct sock
*sk
= sk_vsock(vsk
);
758 sock_set_flag(sk
, SOCK_DONE
);
759 vsk
->peer_shutdown
= SHUTDOWN_MASK
;
760 if (vsock_stream_has_data(vsk
) <= 0)
761 sk
->sk_state
= TCP_CLOSING
;
762 sk
->sk_state_change(sk
);
764 if (vsk
->close_work_scheduled
&&
765 (!cancel_timeout
|| cancel_delayed_work(&vsk
->close_work
))) {
766 vsk
->close_work_scheduled
= false;
768 vsock_remove_sock(vsk
);
770 /* Release refcnt obtained when we scheduled the timeout */
775 static void virtio_transport_close_timeout(struct work_struct
*work
)
777 struct vsock_sock
*vsk
=
778 container_of(work
, struct vsock_sock
, close_work
.work
);
779 struct sock
*sk
= sk_vsock(vsk
);
784 if (!sock_flag(sk
, SOCK_DONE
)) {
785 (void)virtio_transport_reset(vsk
, NULL
);
787 virtio_transport_do_close(vsk
, false);
790 vsk
->close_work_scheduled
= false;
796 /* User context, vsk->sk is locked */
797 static bool virtio_transport_close(struct vsock_sock
*vsk
)
799 struct sock
*sk
= &vsk
->sk
;
801 if (!(sk
->sk_state
== TCP_ESTABLISHED
||
802 sk
->sk_state
== TCP_CLOSING
))
805 /* Already received SHUTDOWN from peer, reply with RST */
806 if ((vsk
->peer_shutdown
& SHUTDOWN_MASK
) == SHUTDOWN_MASK
) {
807 (void)virtio_transport_reset(vsk
, NULL
);
811 if ((sk
->sk_shutdown
& SHUTDOWN_MASK
) != SHUTDOWN_MASK
)
812 (void)virtio_transport_shutdown(vsk
, SHUTDOWN_MASK
);
814 if (sock_flag(sk
, SOCK_LINGER
) && !(current
->flags
& PF_EXITING
))
815 virtio_transport_wait_close(sk
, sk
->sk_lingertime
);
817 if (sock_flag(sk
, SOCK_DONE
)) {
822 INIT_DELAYED_WORK(&vsk
->close_work
,
823 virtio_transport_close_timeout
);
824 vsk
->close_work_scheduled
= true;
825 schedule_delayed_work(&vsk
->close_work
, VSOCK_CLOSE_TIMEOUT
);
829 void virtio_transport_release(struct vsock_sock
*vsk
)
831 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
832 struct virtio_vsock_pkt
*pkt
, *tmp
;
833 struct sock
*sk
= &vsk
->sk
;
834 bool remove_sock
= true;
836 if (sk
->sk_type
== SOCK_STREAM
)
837 remove_sock
= virtio_transport_close(vsk
);
839 list_for_each_entry_safe(pkt
, tmp
, &vvs
->rx_queue
, list
) {
840 list_del(&pkt
->list
);
841 virtio_transport_free_pkt(pkt
);
845 vsock_remove_sock(vsk
);
847 EXPORT_SYMBOL_GPL(virtio_transport_release
);
850 virtio_transport_recv_connecting(struct sock
*sk
,
851 struct virtio_vsock_pkt
*pkt
)
853 struct vsock_sock
*vsk
= vsock_sk(sk
);
857 switch (le16_to_cpu(pkt
->hdr
.op
)) {
858 case VIRTIO_VSOCK_OP_RESPONSE
:
859 sk
->sk_state
= TCP_ESTABLISHED
;
860 sk
->sk_socket
->state
= SS_CONNECTED
;
861 vsock_insert_connected(vsk
);
862 sk
->sk_state_change(sk
);
864 case VIRTIO_VSOCK_OP_INVALID
:
866 case VIRTIO_VSOCK_OP_RST
:
878 virtio_transport_reset(vsk
, pkt
);
879 sk
->sk_state
= TCP_CLOSE
;
881 sk
->sk_error_report(sk
);
886 virtio_transport_recv_enqueue(struct vsock_sock
*vsk
,
887 struct virtio_vsock_pkt
*pkt
)
889 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
890 bool can_enqueue
, free_pkt
= false;
892 pkt
->len
= le32_to_cpu(pkt
->hdr
.len
);
895 spin_lock_bh(&vvs
->rx_lock
);
897 can_enqueue
= virtio_transport_inc_rx_pkt(vvs
, pkt
);
903 /* Try to copy small packets into the buffer of last packet queued,
904 * to avoid wasting memory queueing the entire buffer with a small
907 if (pkt
->len
<= GOOD_COPY_LEN
&& !list_empty(&vvs
->rx_queue
)) {
908 struct virtio_vsock_pkt
*last_pkt
;
910 last_pkt
= list_last_entry(&vvs
->rx_queue
,
911 struct virtio_vsock_pkt
, list
);
913 /* If there is space in the last packet queued, we copy the
914 * new packet in its buffer.
916 if (pkt
->len
<= last_pkt
->buf_len
- last_pkt
->len
) {
917 memcpy(last_pkt
->buf
+ last_pkt
->len
, pkt
->buf
,
919 last_pkt
->len
+= pkt
->len
;
925 list_add_tail(&pkt
->list
, &vvs
->rx_queue
);
928 spin_unlock_bh(&vvs
->rx_lock
);
930 virtio_transport_free_pkt(pkt
);
934 virtio_transport_recv_connected(struct sock
*sk
,
935 struct virtio_vsock_pkt
*pkt
)
937 struct vsock_sock
*vsk
= vsock_sk(sk
);
940 switch (le16_to_cpu(pkt
->hdr
.op
)) {
941 case VIRTIO_VSOCK_OP_RW
:
942 virtio_transport_recv_enqueue(vsk
, pkt
);
943 sk
->sk_data_ready(sk
);
945 case VIRTIO_VSOCK_OP_CREDIT_UPDATE
:
946 sk
->sk_write_space(sk
);
948 case VIRTIO_VSOCK_OP_SHUTDOWN
:
949 if (le32_to_cpu(pkt
->hdr
.flags
) & VIRTIO_VSOCK_SHUTDOWN_RCV
)
950 vsk
->peer_shutdown
|= RCV_SHUTDOWN
;
951 if (le32_to_cpu(pkt
->hdr
.flags
) & VIRTIO_VSOCK_SHUTDOWN_SEND
)
952 vsk
->peer_shutdown
|= SEND_SHUTDOWN
;
953 if (vsk
->peer_shutdown
== SHUTDOWN_MASK
&&
954 vsock_stream_has_data(vsk
) <= 0 &&
955 !sock_flag(sk
, SOCK_DONE
)) {
956 (void)virtio_transport_reset(vsk
, NULL
);
958 virtio_transport_do_close(vsk
, true);
960 if (le32_to_cpu(pkt
->hdr
.flags
))
961 sk
->sk_state_change(sk
);
963 case VIRTIO_VSOCK_OP_RST
:
964 virtio_transport_do_close(vsk
, true);
971 virtio_transport_free_pkt(pkt
);
976 virtio_transport_recv_disconnecting(struct sock
*sk
,
977 struct virtio_vsock_pkt
*pkt
)
979 struct vsock_sock
*vsk
= vsock_sk(sk
);
981 if (le16_to_cpu(pkt
->hdr
.op
) == VIRTIO_VSOCK_OP_RST
)
982 virtio_transport_do_close(vsk
, true);
986 virtio_transport_send_response(struct vsock_sock
*vsk
,
987 struct virtio_vsock_pkt
*pkt
)
989 struct virtio_vsock_pkt_info info
= {
990 .op
= VIRTIO_VSOCK_OP_RESPONSE
,
991 .type
= VIRTIO_VSOCK_TYPE_STREAM
,
992 .remote_cid
= le64_to_cpu(pkt
->hdr
.src_cid
),
993 .remote_port
= le32_to_cpu(pkt
->hdr
.src_port
),
998 return virtio_transport_send_pkt_info(vsk
, &info
);
1001 static bool virtio_transport_space_update(struct sock
*sk
,
1002 struct virtio_vsock_pkt
*pkt
)
1004 struct vsock_sock
*vsk
= vsock_sk(sk
);
1005 struct virtio_vsock_sock
*vvs
= vsk
->trans
;
1006 bool space_available
;
1008 /* Listener sockets are not associated with any transport, so we are
1009 * not able to take the state to see if there is space available in the
1010 * remote peer, but since they are only used to receive requests, we
1011 * can assume that there is always space available in the other peer.
1016 /* buf_alloc and fwd_cnt is always included in the hdr */
1017 spin_lock_bh(&vvs
->tx_lock
);
1018 vvs
->peer_buf_alloc
= le32_to_cpu(pkt
->hdr
.buf_alloc
);
1019 vvs
->peer_fwd_cnt
= le32_to_cpu(pkt
->hdr
.fwd_cnt
);
1020 space_available
= virtio_transport_has_space(vsk
);
1021 spin_unlock_bh(&vvs
->tx_lock
);
1022 return space_available
;
1025 /* Handle server socket */
1027 virtio_transport_recv_listen(struct sock
*sk
, struct virtio_vsock_pkt
*pkt
,
1028 struct virtio_transport
*t
)
1030 struct vsock_sock
*vsk
= vsock_sk(sk
);
1031 struct vsock_sock
*vchild
;
1035 if (le16_to_cpu(pkt
->hdr
.op
) != VIRTIO_VSOCK_OP_REQUEST
) {
1036 virtio_transport_reset_no_sock(t
, pkt
);
1040 if (sk_acceptq_is_full(sk
)) {
1041 virtio_transport_reset_no_sock(t
, pkt
);
1045 child
= vsock_create_connected(sk
);
1047 virtio_transport_reset_no_sock(t
, pkt
);
1051 sk_acceptq_added(sk
);
1053 lock_sock_nested(child
, SINGLE_DEPTH_NESTING
);
1055 child
->sk_state
= TCP_ESTABLISHED
;
1057 vchild
= vsock_sk(child
);
1058 vsock_addr_init(&vchild
->local_addr
, le64_to_cpu(pkt
->hdr
.dst_cid
),
1059 le32_to_cpu(pkt
->hdr
.dst_port
));
1060 vsock_addr_init(&vchild
->remote_addr
, le64_to_cpu(pkt
->hdr
.src_cid
),
1061 le32_to_cpu(pkt
->hdr
.src_port
));
1063 ret
= vsock_assign_transport(vchild
, vsk
);
1064 /* Transport assigned (looking at remote_addr) must be the same
1065 * where we received the request.
1067 if (ret
|| vchild
->transport
!= &t
->transport
) {
1068 release_sock(child
);
1069 virtio_transport_reset_no_sock(t
, pkt
);
1074 if (virtio_transport_space_update(child
, pkt
))
1075 child
->sk_write_space(child
);
1077 vsock_insert_connected(vchild
);
1078 vsock_enqueue_accept(sk
, child
);
1079 virtio_transport_send_response(vchild
, pkt
);
1081 release_sock(child
);
1083 sk
->sk_data_ready(sk
);
1087 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
1090 void virtio_transport_recv_pkt(struct virtio_transport
*t
,
1091 struct virtio_vsock_pkt
*pkt
)
1093 struct sockaddr_vm src
, dst
;
1094 struct vsock_sock
*vsk
;
1096 bool space_available
;
1098 vsock_addr_init(&src
, le64_to_cpu(pkt
->hdr
.src_cid
),
1099 le32_to_cpu(pkt
->hdr
.src_port
));
1100 vsock_addr_init(&dst
, le64_to_cpu(pkt
->hdr
.dst_cid
),
1101 le32_to_cpu(pkt
->hdr
.dst_port
));
1103 trace_virtio_transport_recv_pkt(src
.svm_cid
, src
.svm_port
,
1104 dst
.svm_cid
, dst
.svm_port
,
1105 le32_to_cpu(pkt
->hdr
.len
),
1106 le16_to_cpu(pkt
->hdr
.type
),
1107 le16_to_cpu(pkt
->hdr
.op
),
1108 le32_to_cpu(pkt
->hdr
.flags
),
1109 le32_to_cpu(pkt
->hdr
.buf_alloc
),
1110 le32_to_cpu(pkt
->hdr
.fwd_cnt
));
1112 if (le16_to_cpu(pkt
->hdr
.type
) != VIRTIO_VSOCK_TYPE_STREAM
) {
1113 (void)virtio_transport_reset_no_sock(t
, pkt
);
1117 /* The socket must be in connected or bound table
1118 * otherwise send reset back
1120 sk
= vsock_find_connected_socket(&src
, &dst
);
1122 sk
= vsock_find_bound_socket(&dst
);
1124 (void)virtio_transport_reset_no_sock(t
, pkt
);
1131 space_available
= virtio_transport_space_update(sk
, pkt
);
1135 /* Update CID in case it has changed after a transport reset event */
1136 vsk
->local_addr
.svm_cid
= dst
.svm_cid
;
1138 if (space_available
)
1139 sk
->sk_write_space(sk
);
1141 switch (sk
->sk_state
) {
1143 virtio_transport_recv_listen(sk
, pkt
, t
);
1144 virtio_transport_free_pkt(pkt
);
1147 virtio_transport_recv_connecting(sk
, pkt
);
1148 virtio_transport_free_pkt(pkt
);
1150 case TCP_ESTABLISHED
:
1151 virtio_transport_recv_connected(sk
, pkt
);
1154 virtio_transport_recv_disconnecting(sk
, pkt
);
1155 virtio_transport_free_pkt(pkt
);
1158 (void)virtio_transport_reset_no_sock(t
, pkt
);
1159 virtio_transport_free_pkt(pkt
);
1165 /* Release refcnt obtained when we fetched this socket out of the
1166 * bound or connected list.
1172 virtio_transport_free_pkt(pkt
);
1174 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt
);
1176 void virtio_transport_free_pkt(struct virtio_vsock_pkt
*pkt
)
1181 EXPORT_SYMBOL_GPL(virtio_transport_free_pkt
);
1183 MODULE_LICENSE("GPL v2");
1184 MODULE_AUTHOR("Asias He");
1185 MODULE_DESCRIPTION("common code for virtio vsock");