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);
78 if (msg_data_left(info->msg) == 0 &&
79 info->type == VIRTIO_VSOCK_TYPE_SEQPACKET)
80 pkt->hdr.flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOR);
83 trace_virtio_transport_alloc_pkt(src_cid, src_port,
100 static struct sk_buff *virtio_transport_build_skb(void *opaque)
102 struct virtio_vsock_pkt *pkt = opaque;
103 struct af_vsockmon_hdr *hdr;
108 /* A packet could be split to fit the RX buffer, so we can retrieve
109 * the payload length from the header and the buffer pointer taking
110 * care of the offset in the original packet.
112 payload_len = le32_to_cpu(pkt->hdr.len);
113 payload_buf = pkt->buf + pkt->off;
115 skb = alloc_skb(sizeof(*hdr) + sizeof(pkt->hdr) + payload_len,
120 hdr = skb_put(skb, sizeof(*hdr));
122 /* pkt->hdr is little-endian so no need to byteswap here */
123 hdr->src_cid = pkt->hdr.src_cid;
124 hdr->src_port = pkt->hdr.src_port;
125 hdr->dst_cid = pkt->hdr.dst_cid;
126 hdr->dst_port = pkt->hdr.dst_port;
128 hdr->transport = cpu_to_le16(AF_VSOCK_TRANSPORT_VIRTIO);
129 hdr->len = cpu_to_le16(sizeof(pkt->hdr));
130 memset(hdr->reserved, 0, sizeof(hdr->reserved));
132 switch (le16_to_cpu(pkt->hdr.op)) {
133 case VIRTIO_VSOCK_OP_REQUEST:
134 case VIRTIO_VSOCK_OP_RESPONSE:
135 hdr->op = cpu_to_le16(AF_VSOCK_OP_CONNECT);
137 case VIRTIO_VSOCK_OP_RST:
138 case VIRTIO_VSOCK_OP_SHUTDOWN:
139 hdr->op = cpu_to_le16(AF_VSOCK_OP_DISCONNECT);
141 case VIRTIO_VSOCK_OP_RW:
142 hdr->op = cpu_to_le16(AF_VSOCK_OP_PAYLOAD);
144 case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
145 case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
146 hdr->op = cpu_to_le16(AF_VSOCK_OP_CONTROL);
149 hdr->op = cpu_to_le16(AF_VSOCK_OP_UNKNOWN);
153 skb_put_data(skb, &pkt->hdr, sizeof(pkt->hdr));
156 skb_put_data(skb, payload_buf, payload_len);
162 void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt)
164 if (pkt->tap_delivered)
167 vsock_deliver_tap(virtio_transport_build_skb, pkt);
168 pkt->tap_delivered = true;
170 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt);
172 static u16 virtio_transport_get_type(struct sock *sk)
174 if (sk->sk_type == SOCK_STREAM)
175 return VIRTIO_VSOCK_TYPE_STREAM;
177 return VIRTIO_VSOCK_TYPE_SEQPACKET;
180 /* This function can only be used on connecting/connected sockets,
181 * since a socket assigned to a transport is required.
183 * Do not use on listener sockets!
185 static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
186 struct virtio_vsock_pkt_info *info)
188 u32 src_cid, src_port, dst_cid, dst_port;
189 const struct virtio_transport *t_ops;
190 struct virtio_vsock_sock *vvs;
191 struct virtio_vsock_pkt *pkt;
192 u32 pkt_len = info->pkt_len;
194 info->type = virtio_transport_get_type(sk_vsock(vsk));
196 t_ops = virtio_transport_get_ops(vsk);
197 if (unlikely(!t_ops))
200 src_cid = t_ops->transport.get_local_cid();
201 src_port = vsk->local_addr.svm_port;
202 if (!info->remote_cid) {
203 dst_cid = vsk->remote_addr.svm_cid;
204 dst_port = vsk->remote_addr.svm_port;
206 dst_cid = info->remote_cid;
207 dst_port = info->remote_port;
212 /* we can send less than pkt_len bytes */
213 if (pkt_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE)
214 pkt_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
216 /* virtio_transport_get_credit might return less than pkt_len credit */
217 pkt_len = virtio_transport_get_credit(vvs, pkt_len);
219 /* Do not send zero length OP_RW pkt */
220 if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW)
223 pkt = virtio_transport_alloc_pkt(info, pkt_len,
227 virtio_transport_put_credit(vvs, pkt_len);
231 virtio_transport_inc_tx_pkt(vvs, pkt);
233 return t_ops->send_pkt(pkt);
236 static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
237 struct virtio_vsock_pkt *pkt)
239 if (vvs->rx_bytes + pkt->len > vvs->buf_alloc)
242 vvs->rx_bytes += pkt->len;
246 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs,
247 struct virtio_vsock_pkt *pkt)
249 vvs->rx_bytes -= pkt->len;
250 vvs->fwd_cnt += pkt->len;
253 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct virtio_vsock_pkt *pkt)
255 spin_lock_bh(&vvs->rx_lock);
256 vvs->last_fwd_cnt = vvs->fwd_cnt;
257 pkt->hdr.fwd_cnt = cpu_to_le32(vvs->fwd_cnt);
258 pkt->hdr.buf_alloc = cpu_to_le32(vvs->buf_alloc);
259 spin_unlock_bh(&vvs->rx_lock);
261 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt);
263 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
267 spin_lock_bh(&vvs->tx_lock);
268 ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
272 spin_unlock_bh(&vvs->tx_lock);
276 EXPORT_SYMBOL_GPL(virtio_transport_get_credit);
278 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit)
280 spin_lock_bh(&vvs->tx_lock);
281 vvs->tx_cnt -= credit;
282 spin_unlock_bh(&vvs->tx_lock);
284 EXPORT_SYMBOL_GPL(virtio_transport_put_credit);
286 static int virtio_transport_send_credit_update(struct vsock_sock *vsk)
288 struct virtio_vsock_pkt_info info = {
289 .op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
293 return virtio_transport_send_pkt_info(vsk, &info);
297 virtio_transport_stream_do_peek(struct vsock_sock *vsk,
301 struct virtio_vsock_sock *vvs = vsk->trans;
302 struct virtio_vsock_pkt *pkt;
303 size_t bytes, total = 0, off;
306 spin_lock_bh(&vvs->rx_lock);
308 list_for_each_entry(pkt, &vvs->rx_queue, list) {
314 while (total < len && off < pkt->len) {
316 if (bytes > pkt->len - off)
317 bytes = pkt->len - off;
319 /* sk_lock is held by caller so no one else can dequeue.
320 * Unlock rx_lock since memcpy_to_msg() may sleep.
322 spin_unlock_bh(&vvs->rx_lock);
324 err = memcpy_to_msg(msg, pkt->buf + off, bytes);
328 spin_lock_bh(&vvs->rx_lock);
335 spin_unlock_bh(&vvs->rx_lock);
346 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
350 struct virtio_vsock_sock *vvs = vsk->trans;
351 struct virtio_vsock_pkt *pkt;
352 size_t bytes, total = 0;
356 spin_lock_bh(&vvs->rx_lock);
357 while (total < len && !list_empty(&vvs->rx_queue)) {
358 pkt = list_first_entry(&vvs->rx_queue,
359 struct virtio_vsock_pkt, list);
362 if (bytes > pkt->len - pkt->off)
363 bytes = pkt->len - pkt->off;
365 /* sk_lock is held by caller so no one else can dequeue.
366 * Unlock rx_lock since memcpy_to_msg() may sleep.
368 spin_unlock_bh(&vvs->rx_lock);
370 err = memcpy_to_msg(msg, pkt->buf + pkt->off, bytes);
374 spin_lock_bh(&vvs->rx_lock);
378 if (pkt->off == pkt->len) {
379 virtio_transport_dec_rx_pkt(vvs, pkt);
380 list_del(&pkt->list);
381 virtio_transport_free_pkt(pkt);
385 free_space = vvs->buf_alloc - (vvs->fwd_cnt - vvs->last_fwd_cnt);
387 spin_unlock_bh(&vvs->rx_lock);
389 /* To reduce the number of credit update messages,
390 * don't update credits as long as lots of space is available.
391 * Note: the limit chosen here is arbitrary. Setting the limit
392 * too high causes extra messages. Too low causes transmitter
393 * stalls. As stalls are in theory more expensive than extra
394 * messages, we set the limit to a high value. TODO: experiment
395 * with different values.
397 if (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE)
398 virtio_transport_send_credit_update(vsk);
408 static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
412 struct virtio_vsock_sock *vvs = vsk->trans;
413 struct virtio_vsock_pkt *pkt;
414 int dequeued_len = 0;
415 size_t user_buf_len = msg_data_left(msg);
416 bool msg_ready = false;
418 spin_lock_bh(&vvs->rx_lock);
420 if (vvs->msg_count == 0) {
421 spin_unlock_bh(&vvs->rx_lock);
426 pkt = list_first_entry(&vvs->rx_queue, struct virtio_vsock_pkt, list);
428 if (dequeued_len >= 0) {
430 size_t bytes_to_copy;
432 pkt_len = (size_t)le32_to_cpu(pkt->hdr.len);
433 bytes_to_copy = min(user_buf_len, pkt_len);
438 /* sk_lock is held by caller so no one else can dequeue.
439 * Unlock rx_lock since memcpy_to_msg() may sleep.
441 spin_unlock_bh(&vvs->rx_lock);
443 err = memcpy_to_msg(msg, pkt->buf, bytes_to_copy);
445 /* Copy of message failed. Rest of
446 * fragments will be freed without copy.
450 user_buf_len -= bytes_to_copy;
453 spin_lock_bh(&vvs->rx_lock);
456 if (dequeued_len >= 0)
457 dequeued_len += pkt_len;
460 if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR) {
465 virtio_transport_dec_rx_pkt(vvs, pkt);
466 list_del(&pkt->list);
467 virtio_transport_free_pkt(pkt);
470 spin_unlock_bh(&vvs->rx_lock);
472 virtio_transport_send_credit_update(vsk);
478 virtio_transport_stream_dequeue(struct vsock_sock *vsk,
480 size_t len, int flags)
482 if (flags & MSG_PEEK)
483 return virtio_transport_stream_do_peek(vsk, msg, len);
485 return virtio_transport_stream_do_dequeue(vsk, msg, len);
487 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
490 virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
494 if (flags & MSG_PEEK)
497 return virtio_transport_seqpacket_do_dequeue(vsk, msg, flags);
499 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
502 virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
506 struct virtio_vsock_sock *vvs = vsk->trans;
508 spin_lock_bh(&vvs->tx_lock);
510 if (len > vvs->peer_buf_alloc) {
511 spin_unlock_bh(&vvs->tx_lock);
515 spin_unlock_bh(&vvs->tx_lock);
517 return virtio_transport_stream_enqueue(vsk, msg, len);
519 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue);
522 virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
524 size_t len, int flags)
528 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue);
530 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk)
532 struct virtio_vsock_sock *vvs = vsk->trans;
535 spin_lock_bh(&vvs->rx_lock);
536 bytes = vvs->rx_bytes;
537 spin_unlock_bh(&vvs->rx_lock);
541 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data);
543 u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk)
545 struct virtio_vsock_sock *vvs = vsk->trans;
548 spin_lock_bh(&vvs->rx_lock);
549 msg_count = vvs->msg_count;
550 spin_unlock_bh(&vvs->rx_lock);
554 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_has_data);
556 static s64 virtio_transport_has_space(struct vsock_sock *vsk)
558 struct virtio_vsock_sock *vvs = vsk->trans;
561 bytes = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
568 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk)
570 struct virtio_vsock_sock *vvs = vsk->trans;
573 spin_lock_bh(&vvs->tx_lock);
574 bytes = virtio_transport_has_space(vsk);
575 spin_unlock_bh(&vvs->tx_lock);
579 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space);
581 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
582 struct vsock_sock *psk)
584 struct virtio_vsock_sock *vvs;
586 vvs = kzalloc(sizeof(*vvs), GFP_KERNEL);
592 if (psk && psk->trans) {
593 struct virtio_vsock_sock *ptrans = psk->trans;
595 vvs->peer_buf_alloc = ptrans->peer_buf_alloc;
598 if (vsk->buffer_size > VIRTIO_VSOCK_MAX_BUF_SIZE)
599 vsk->buffer_size = VIRTIO_VSOCK_MAX_BUF_SIZE;
601 vvs->buf_alloc = vsk->buffer_size;
603 spin_lock_init(&vvs->rx_lock);
604 spin_lock_init(&vvs->tx_lock);
605 INIT_LIST_HEAD(&vvs->rx_queue);
609 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init);
611 /* sk_lock held by the caller */
612 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val)
614 struct virtio_vsock_sock *vvs = vsk->trans;
616 if (*val > VIRTIO_VSOCK_MAX_BUF_SIZE)
617 *val = VIRTIO_VSOCK_MAX_BUF_SIZE;
619 vvs->buf_alloc = *val;
621 virtio_transport_send_credit_update(vsk);
623 EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size);
626 virtio_transport_notify_poll_in(struct vsock_sock *vsk,
628 bool *data_ready_now)
630 if (vsock_stream_has_data(vsk))
631 *data_ready_now = true;
633 *data_ready_now = false;
637 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in);
640 virtio_transport_notify_poll_out(struct vsock_sock *vsk,
642 bool *space_avail_now)
646 free_space = vsock_stream_has_space(vsk);
648 *space_avail_now = true;
649 else if (free_space == 0)
650 *space_avail_now = false;
654 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out);
656 int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
657 size_t target, struct vsock_transport_recv_notify_data *data)
661 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init);
663 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
664 size_t target, struct vsock_transport_recv_notify_data *data)
668 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block);
670 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
671 size_t target, struct vsock_transport_recv_notify_data *data)
675 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue);
677 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
678 size_t target, ssize_t copied, bool data_read,
679 struct vsock_transport_recv_notify_data *data)
683 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue);
685 int virtio_transport_notify_send_init(struct vsock_sock *vsk,
686 struct vsock_transport_send_notify_data *data)
690 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init);
692 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
693 struct vsock_transport_send_notify_data *data)
697 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block);
699 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
700 struct vsock_transport_send_notify_data *data)
704 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue);
706 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
707 ssize_t written, struct vsock_transport_send_notify_data *data)
711 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue);
713 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk)
715 return vsk->buffer_size;
717 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat);
719 bool virtio_transport_stream_is_active(struct vsock_sock *vsk)
723 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active);
725 bool virtio_transport_stream_allow(u32 cid, u32 port)
729 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow);
731 int virtio_transport_dgram_bind(struct vsock_sock *vsk,
732 struct sockaddr_vm *addr)
736 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind);
738 bool virtio_transport_dgram_allow(u32 cid, u32 port)
742 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow);
744 int virtio_transport_connect(struct vsock_sock *vsk)
746 struct virtio_vsock_pkt_info info = {
747 .op = VIRTIO_VSOCK_OP_REQUEST,
751 return virtio_transport_send_pkt_info(vsk, &info);
753 EXPORT_SYMBOL_GPL(virtio_transport_connect);
755 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode)
757 struct virtio_vsock_pkt_info info = {
758 .op = VIRTIO_VSOCK_OP_SHUTDOWN,
759 .flags = (mode & RCV_SHUTDOWN ?
760 VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
761 (mode & SEND_SHUTDOWN ?
762 VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
766 return virtio_transport_send_pkt_info(vsk, &info);
768 EXPORT_SYMBOL_GPL(virtio_transport_shutdown);
771 virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
772 struct sockaddr_vm *remote_addr,
778 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue);
781 virtio_transport_stream_enqueue(struct vsock_sock *vsk,
785 struct virtio_vsock_pkt_info info = {
786 .op = VIRTIO_VSOCK_OP_RW,
792 return virtio_transport_send_pkt_info(vsk, &info);
794 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue);
796 void virtio_transport_destruct(struct vsock_sock *vsk)
798 struct virtio_vsock_sock *vvs = vsk->trans;
802 EXPORT_SYMBOL_GPL(virtio_transport_destruct);
804 static int virtio_transport_reset(struct vsock_sock *vsk,
805 struct virtio_vsock_pkt *pkt)
807 struct virtio_vsock_pkt_info info = {
808 .op = VIRTIO_VSOCK_OP_RST,
813 /* Send RST only if the original pkt is not a RST pkt */
814 if (pkt && le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_RST)
817 return virtio_transport_send_pkt_info(vsk, &info);
820 /* Normally packets are associated with a socket. There may be no socket if an
821 * attempt was made to connect to a socket that does not exist.
823 static int virtio_transport_reset_no_sock(const struct virtio_transport *t,
824 struct virtio_vsock_pkt *pkt)
826 struct virtio_vsock_pkt *reply;
827 struct virtio_vsock_pkt_info info = {
828 .op = VIRTIO_VSOCK_OP_RST,
829 .type = le16_to_cpu(pkt->hdr.type),
833 /* Send RST only if the original pkt is not a RST pkt */
834 if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_RST)
837 reply = virtio_transport_alloc_pkt(&info, 0,
838 le64_to_cpu(pkt->hdr.dst_cid),
839 le32_to_cpu(pkt->hdr.dst_port),
840 le64_to_cpu(pkt->hdr.src_cid),
841 le32_to_cpu(pkt->hdr.src_port));
846 virtio_transport_free_pkt(reply);
850 return t->send_pkt(reply);
853 /* This function should be called with sk_lock held and SOCK_DONE set */
854 static void virtio_transport_remove_sock(struct vsock_sock *vsk)
856 struct virtio_vsock_sock *vvs = vsk->trans;
857 struct virtio_vsock_pkt *pkt, *tmp;
859 /* We don't need to take rx_lock, as the socket is closing and we are
862 list_for_each_entry_safe(pkt, tmp, &vvs->rx_queue, list) {
863 list_del(&pkt->list);
864 virtio_transport_free_pkt(pkt);
867 vsock_remove_sock(vsk);
870 static void virtio_transport_wait_close(struct sock *sk, long timeout)
873 DEFINE_WAIT_FUNC(wait, woken_wake_function);
875 add_wait_queue(sk_sleep(sk), &wait);
878 if (sk_wait_event(sk, &timeout,
879 sock_flag(sk, SOCK_DONE), &wait))
881 } while (!signal_pending(current) && timeout);
883 remove_wait_queue(sk_sleep(sk), &wait);
887 static void virtio_transport_do_close(struct vsock_sock *vsk,
890 struct sock *sk = sk_vsock(vsk);
892 sock_set_flag(sk, SOCK_DONE);
893 vsk->peer_shutdown = SHUTDOWN_MASK;
894 if (vsock_stream_has_data(vsk) <= 0)
895 sk->sk_state = TCP_CLOSING;
896 sk->sk_state_change(sk);
898 if (vsk->close_work_scheduled &&
899 (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) {
900 vsk->close_work_scheduled = false;
902 virtio_transport_remove_sock(vsk);
904 /* Release refcnt obtained when we scheduled the timeout */
909 static void virtio_transport_close_timeout(struct work_struct *work)
911 struct vsock_sock *vsk =
912 container_of(work, struct vsock_sock, close_work.work);
913 struct sock *sk = sk_vsock(vsk);
918 if (!sock_flag(sk, SOCK_DONE)) {
919 (void)virtio_transport_reset(vsk, NULL);
921 virtio_transport_do_close(vsk, false);
924 vsk->close_work_scheduled = false;
930 /* User context, vsk->sk is locked */
931 static bool virtio_transport_close(struct vsock_sock *vsk)
933 struct sock *sk = &vsk->sk;
935 if (!(sk->sk_state == TCP_ESTABLISHED ||
936 sk->sk_state == TCP_CLOSING))
939 /* Already received SHUTDOWN from peer, reply with RST */
940 if ((vsk->peer_shutdown & SHUTDOWN_MASK) == SHUTDOWN_MASK) {
941 (void)virtio_transport_reset(vsk, NULL);
945 if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
946 (void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK);
948 if (sock_flag(sk, SOCK_LINGER) && !(current->flags & PF_EXITING))
949 virtio_transport_wait_close(sk, sk->sk_lingertime);
951 if (sock_flag(sk, SOCK_DONE)) {
956 INIT_DELAYED_WORK(&vsk->close_work,
957 virtio_transport_close_timeout);
958 vsk->close_work_scheduled = true;
959 schedule_delayed_work(&vsk->close_work, VSOCK_CLOSE_TIMEOUT);
963 void virtio_transport_release(struct vsock_sock *vsk)
965 struct sock *sk = &vsk->sk;
966 bool remove_sock = true;
968 if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)
969 remove_sock = virtio_transport_close(vsk);
972 sock_set_flag(sk, SOCK_DONE);
973 virtio_transport_remove_sock(vsk);
976 EXPORT_SYMBOL_GPL(virtio_transport_release);
979 virtio_transport_recv_connecting(struct sock *sk,
980 struct virtio_vsock_pkt *pkt)
982 struct vsock_sock *vsk = vsock_sk(sk);
986 switch (le16_to_cpu(pkt->hdr.op)) {
987 case VIRTIO_VSOCK_OP_RESPONSE:
988 sk->sk_state = TCP_ESTABLISHED;
989 sk->sk_socket->state = SS_CONNECTED;
990 vsock_insert_connected(vsk);
991 sk->sk_state_change(sk);
993 case VIRTIO_VSOCK_OP_INVALID:
995 case VIRTIO_VSOCK_OP_RST:
1007 virtio_transport_reset(vsk, pkt);
1008 sk->sk_state = TCP_CLOSE;
1010 sk_error_report(sk);
1015 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
1016 struct virtio_vsock_pkt *pkt)
1018 struct virtio_vsock_sock *vvs = vsk->trans;
1019 bool can_enqueue, free_pkt = false;
1021 pkt->len = le32_to_cpu(pkt->hdr.len);
1024 spin_lock_bh(&vvs->rx_lock);
1026 can_enqueue = virtio_transport_inc_rx_pkt(vvs, pkt);
1032 if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)
1035 /* Try to copy small packets into the buffer of last packet queued,
1036 * to avoid wasting memory queueing the entire buffer with a small
1039 if (pkt->len <= GOOD_COPY_LEN && !list_empty(&vvs->rx_queue)) {
1040 struct virtio_vsock_pkt *last_pkt;
1042 last_pkt = list_last_entry(&vvs->rx_queue,
1043 struct virtio_vsock_pkt, list);
1045 /* If there is space in the last packet queued, we copy the
1046 * new packet in its buffer. We avoid this if the last packet
1047 * queued has VIRTIO_VSOCK_SEQ_EOR set, because this is
1048 * delimiter of SEQPACKET record, so 'pkt' is the first packet
1051 if ((pkt->len <= last_pkt->buf_len - last_pkt->len) &&
1052 !(le32_to_cpu(last_pkt->hdr.flags) & VIRTIO_VSOCK_SEQ_EOR)) {
1053 memcpy(last_pkt->buf + last_pkt->len, pkt->buf,
1055 last_pkt->len += pkt->len;
1057 last_pkt->hdr.flags |= pkt->hdr.flags;
1062 list_add_tail(&pkt->list, &vvs->rx_queue);
1065 spin_unlock_bh(&vvs->rx_lock);
1067 virtio_transport_free_pkt(pkt);
1071 virtio_transport_recv_connected(struct sock *sk,
1072 struct virtio_vsock_pkt *pkt)
1074 struct vsock_sock *vsk = vsock_sk(sk);
1077 switch (le16_to_cpu(pkt->hdr.op)) {
1078 case VIRTIO_VSOCK_OP_RW:
1079 virtio_transport_recv_enqueue(vsk, pkt);
1080 sk->sk_data_ready(sk);
1082 case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
1083 virtio_transport_send_credit_update(vsk);
1085 case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
1086 sk->sk_write_space(sk);
1088 case VIRTIO_VSOCK_OP_SHUTDOWN:
1089 if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SHUTDOWN_RCV)
1090 vsk->peer_shutdown |= RCV_SHUTDOWN;
1091 if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SHUTDOWN_SEND)
1092 vsk->peer_shutdown |= SEND_SHUTDOWN;
1093 if (vsk->peer_shutdown == SHUTDOWN_MASK &&
1094 vsock_stream_has_data(vsk) <= 0 &&
1095 !sock_flag(sk, SOCK_DONE)) {
1096 (void)virtio_transport_reset(vsk, NULL);
1098 virtio_transport_do_close(vsk, true);
1100 if (le32_to_cpu(pkt->hdr.flags))
1101 sk->sk_state_change(sk);
1103 case VIRTIO_VSOCK_OP_RST:
1104 virtio_transport_do_close(vsk, true);
1111 virtio_transport_free_pkt(pkt);
1116 virtio_transport_recv_disconnecting(struct sock *sk,
1117 struct virtio_vsock_pkt *pkt)
1119 struct vsock_sock *vsk = vsock_sk(sk);
1121 if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_RST)
1122 virtio_transport_do_close(vsk, true);
1126 virtio_transport_send_response(struct vsock_sock *vsk,
1127 struct virtio_vsock_pkt *pkt)
1129 struct virtio_vsock_pkt_info info = {
1130 .op = VIRTIO_VSOCK_OP_RESPONSE,
1131 .remote_cid = le64_to_cpu(pkt->hdr.src_cid),
1132 .remote_port = le32_to_cpu(pkt->hdr.src_port),
1137 return virtio_transport_send_pkt_info(vsk, &info);
1140 static bool virtio_transport_space_update(struct sock *sk,
1141 struct virtio_vsock_pkt *pkt)
1143 struct vsock_sock *vsk = vsock_sk(sk);
1144 struct virtio_vsock_sock *vvs = vsk->trans;
1145 bool space_available;
1147 /* Listener sockets are not associated with any transport, so we are
1148 * not able to take the state to see if there is space available in the
1149 * remote peer, but since they are only used to receive requests, we
1150 * can assume that there is always space available in the other peer.
1155 /* buf_alloc and fwd_cnt is always included in the hdr */
1156 spin_lock_bh(&vvs->tx_lock);
1157 vvs->peer_buf_alloc = le32_to_cpu(pkt->hdr.buf_alloc);
1158 vvs->peer_fwd_cnt = le32_to_cpu(pkt->hdr.fwd_cnt);
1159 space_available = virtio_transport_has_space(vsk);
1160 spin_unlock_bh(&vvs->tx_lock);
1161 return space_available;
1164 /* Handle server socket */
1166 virtio_transport_recv_listen(struct sock *sk, struct virtio_vsock_pkt *pkt,
1167 struct virtio_transport *t)
1169 struct vsock_sock *vsk = vsock_sk(sk);
1170 struct vsock_sock *vchild;
1174 if (le16_to_cpu(pkt->hdr.op) != VIRTIO_VSOCK_OP_REQUEST) {
1175 virtio_transport_reset_no_sock(t, pkt);
1179 if (sk_acceptq_is_full(sk)) {
1180 virtio_transport_reset_no_sock(t, pkt);
1184 child = vsock_create_connected(sk);
1186 virtio_transport_reset_no_sock(t, pkt);
1190 sk_acceptq_added(sk);
1192 lock_sock_nested(child, SINGLE_DEPTH_NESTING);
1194 child->sk_state = TCP_ESTABLISHED;
1196 vchild = vsock_sk(child);
1197 vsock_addr_init(&vchild->local_addr, le64_to_cpu(pkt->hdr.dst_cid),
1198 le32_to_cpu(pkt->hdr.dst_port));
1199 vsock_addr_init(&vchild->remote_addr, le64_to_cpu(pkt->hdr.src_cid),
1200 le32_to_cpu(pkt->hdr.src_port));
1202 ret = vsock_assign_transport(vchild, vsk);
1203 /* Transport assigned (looking at remote_addr) must be the same
1204 * where we received the request.
1206 if (ret || vchild->transport != &t->transport) {
1207 release_sock(child);
1208 virtio_transport_reset_no_sock(t, pkt);
1213 if (virtio_transport_space_update(child, pkt))
1214 child->sk_write_space(child);
1216 vsock_insert_connected(vchild);
1217 vsock_enqueue_accept(sk, child);
1218 virtio_transport_send_response(vchild, pkt);
1220 release_sock(child);
1222 sk->sk_data_ready(sk);
1226 static bool virtio_transport_valid_type(u16 type)
1228 return (type == VIRTIO_VSOCK_TYPE_STREAM) ||
1229 (type == VIRTIO_VSOCK_TYPE_SEQPACKET);
1232 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
1235 void virtio_transport_recv_pkt(struct virtio_transport *t,
1236 struct virtio_vsock_pkt *pkt)
1238 struct sockaddr_vm src, dst;
1239 struct vsock_sock *vsk;
1241 bool space_available;
1243 vsock_addr_init(&src, le64_to_cpu(pkt->hdr.src_cid),
1244 le32_to_cpu(pkt->hdr.src_port));
1245 vsock_addr_init(&dst, le64_to_cpu(pkt->hdr.dst_cid),
1246 le32_to_cpu(pkt->hdr.dst_port));
1248 trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,
1249 dst.svm_cid, dst.svm_port,
1250 le32_to_cpu(pkt->hdr.len),
1251 le16_to_cpu(pkt->hdr.type),
1252 le16_to_cpu(pkt->hdr.op),
1253 le32_to_cpu(pkt->hdr.flags),
1254 le32_to_cpu(pkt->hdr.buf_alloc),
1255 le32_to_cpu(pkt->hdr.fwd_cnt));
1257 if (!virtio_transport_valid_type(le16_to_cpu(pkt->hdr.type))) {
1258 (void)virtio_transport_reset_no_sock(t, pkt);
1262 /* The socket must be in connected or bound table
1263 * otherwise send reset back
1265 sk = vsock_find_connected_socket(&src, &dst);
1267 sk = vsock_find_bound_socket(&dst);
1269 (void)virtio_transport_reset_no_sock(t, pkt);
1274 if (virtio_transport_get_type(sk) != le16_to_cpu(pkt->hdr.type)) {
1275 (void)virtio_transport_reset_no_sock(t, pkt);
1284 /* Check if sk has been closed before lock_sock */
1285 if (sock_flag(sk, SOCK_DONE)) {
1286 (void)virtio_transport_reset_no_sock(t, pkt);
1292 space_available = virtio_transport_space_update(sk, pkt);
1294 /* Update CID in case it has changed after a transport reset event */
1295 vsk->local_addr.svm_cid = dst.svm_cid;
1297 if (space_available)
1298 sk->sk_write_space(sk);
1300 switch (sk->sk_state) {
1302 virtio_transport_recv_listen(sk, pkt, t);
1303 virtio_transport_free_pkt(pkt);
1306 virtio_transport_recv_connecting(sk, pkt);
1307 virtio_transport_free_pkt(pkt);
1309 case TCP_ESTABLISHED:
1310 virtio_transport_recv_connected(sk, pkt);
1313 virtio_transport_recv_disconnecting(sk, pkt);
1314 virtio_transport_free_pkt(pkt);
1317 (void)virtio_transport_reset_no_sock(t, pkt);
1318 virtio_transport_free_pkt(pkt);
1324 /* Release refcnt obtained when we fetched this socket out of the
1325 * bound or connected list.
1331 virtio_transport_free_pkt(pkt);
1333 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);
1335 void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt)
1340 EXPORT_SYMBOL_GPL(virtio_transport_free_pkt);
1342 MODULE_LICENSE("GPL v2");
1343 MODULE_AUTHOR("Asias He");
1344 MODULE_DESCRIPTION("common code for virtio vsock");