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 vsock_deliver_tap(virtio_transport_build_skb, pkt);
162 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt);
164 /* This function can only be used on connecting/connected sockets,
165 * since a socket assigned to a transport is required.
167 * Do not use on listener sockets!
169 static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
170 struct virtio_vsock_pkt_info *info)
172 u32 src_cid, src_port, dst_cid, dst_port;
173 const struct virtio_transport *t_ops;
174 struct virtio_vsock_sock *vvs;
175 struct virtio_vsock_pkt *pkt;
176 u32 pkt_len = info->pkt_len;
178 t_ops = virtio_transport_get_ops(vsk);
179 if (unlikely(!t_ops))
182 src_cid = t_ops->transport.get_local_cid();
183 src_port = vsk->local_addr.svm_port;
184 if (!info->remote_cid) {
185 dst_cid = vsk->remote_addr.svm_cid;
186 dst_port = vsk->remote_addr.svm_port;
188 dst_cid = info->remote_cid;
189 dst_port = info->remote_port;
194 /* we can send less than pkt_len bytes */
195 if (pkt_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE)
196 pkt_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
198 /* virtio_transport_get_credit might return less than pkt_len credit */
199 pkt_len = virtio_transport_get_credit(vvs, pkt_len);
201 /* Do not send zero length OP_RW pkt */
202 if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW)
205 pkt = virtio_transport_alloc_pkt(info, pkt_len,
209 virtio_transport_put_credit(vvs, pkt_len);
213 virtio_transport_inc_tx_pkt(vvs, pkt);
215 return t_ops->send_pkt(pkt);
218 static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
219 struct virtio_vsock_pkt *pkt)
221 if (vvs->rx_bytes + pkt->len > vvs->buf_alloc)
224 vvs->rx_bytes += pkt->len;
228 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs,
229 struct virtio_vsock_pkt *pkt)
231 vvs->rx_bytes -= pkt->len;
232 vvs->fwd_cnt += pkt->len;
235 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct virtio_vsock_pkt *pkt)
237 spin_lock_bh(&vvs->rx_lock);
238 vvs->last_fwd_cnt = vvs->fwd_cnt;
239 pkt->hdr.fwd_cnt = cpu_to_le32(vvs->fwd_cnt);
240 pkt->hdr.buf_alloc = cpu_to_le32(vvs->buf_alloc);
241 spin_unlock_bh(&vvs->rx_lock);
243 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt);
245 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
249 spin_lock_bh(&vvs->tx_lock);
250 ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
254 spin_unlock_bh(&vvs->tx_lock);
258 EXPORT_SYMBOL_GPL(virtio_transport_get_credit);
260 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit)
262 spin_lock_bh(&vvs->tx_lock);
263 vvs->tx_cnt -= credit;
264 spin_unlock_bh(&vvs->tx_lock);
266 EXPORT_SYMBOL_GPL(virtio_transport_put_credit);
268 static int virtio_transport_send_credit_update(struct vsock_sock *vsk,
270 struct virtio_vsock_hdr *hdr)
272 struct virtio_vsock_pkt_info info = {
273 .op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
278 return virtio_transport_send_pkt_info(vsk, &info);
282 virtio_transport_stream_do_peek(struct vsock_sock *vsk,
286 struct virtio_vsock_sock *vvs = vsk->trans;
287 struct virtio_vsock_pkt *pkt;
288 size_t bytes, total = 0, off;
291 spin_lock_bh(&vvs->rx_lock);
293 list_for_each_entry(pkt, &vvs->rx_queue, list) {
299 while (total < len && off < pkt->len) {
301 if (bytes > pkt->len - off)
302 bytes = pkt->len - off;
304 /* sk_lock is held by caller so no one else can dequeue.
305 * Unlock rx_lock since memcpy_to_msg() may sleep.
307 spin_unlock_bh(&vvs->rx_lock);
309 err = memcpy_to_msg(msg, pkt->buf + off, bytes);
313 spin_lock_bh(&vvs->rx_lock);
320 spin_unlock_bh(&vvs->rx_lock);
331 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
335 struct virtio_vsock_sock *vvs = vsk->trans;
336 struct virtio_vsock_pkt *pkt;
337 size_t bytes, total = 0;
341 spin_lock_bh(&vvs->rx_lock);
342 while (total < len && !list_empty(&vvs->rx_queue)) {
343 pkt = list_first_entry(&vvs->rx_queue,
344 struct virtio_vsock_pkt, list);
347 if (bytes > pkt->len - pkt->off)
348 bytes = pkt->len - pkt->off;
350 /* sk_lock is held by caller so no one else can dequeue.
351 * Unlock rx_lock since memcpy_to_msg() may sleep.
353 spin_unlock_bh(&vvs->rx_lock);
355 err = memcpy_to_msg(msg, pkt->buf + pkt->off, bytes);
359 spin_lock_bh(&vvs->rx_lock);
363 if (pkt->off == pkt->len) {
364 virtio_transport_dec_rx_pkt(vvs, pkt);
365 list_del(&pkt->list);
366 virtio_transport_free_pkt(pkt);
370 free_space = vvs->buf_alloc - (vvs->fwd_cnt - vvs->last_fwd_cnt);
372 spin_unlock_bh(&vvs->rx_lock);
374 /* To reduce the number of credit update messages,
375 * don't update credits as long as lots of space is available.
376 * Note: the limit chosen here is arbitrary. Setting the limit
377 * too high causes extra messages. Too low causes transmitter
378 * stalls. As stalls are in theory more expensive than extra
379 * messages, we set the limit to a high value. TODO: experiment
380 * with different values.
382 if (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
383 virtio_transport_send_credit_update(vsk,
384 VIRTIO_VSOCK_TYPE_STREAM,
397 virtio_transport_stream_dequeue(struct vsock_sock *vsk,
399 size_t len, int flags)
401 if (flags & MSG_PEEK)
402 return virtio_transport_stream_do_peek(vsk, msg, len);
404 return virtio_transport_stream_do_dequeue(vsk, msg, len);
406 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
409 virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
411 size_t len, int flags)
415 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue);
417 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk)
419 struct virtio_vsock_sock *vvs = vsk->trans;
422 spin_lock_bh(&vvs->rx_lock);
423 bytes = vvs->rx_bytes;
424 spin_unlock_bh(&vvs->rx_lock);
428 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data);
430 static s64 virtio_transport_has_space(struct vsock_sock *vsk)
432 struct virtio_vsock_sock *vvs = vsk->trans;
435 bytes = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
442 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk)
444 struct virtio_vsock_sock *vvs = vsk->trans;
447 spin_lock_bh(&vvs->tx_lock);
448 bytes = virtio_transport_has_space(vsk);
449 spin_unlock_bh(&vvs->tx_lock);
453 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space);
455 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
456 struct vsock_sock *psk)
458 struct virtio_vsock_sock *vvs;
460 vvs = kzalloc(sizeof(*vvs), GFP_KERNEL);
466 if (psk && psk->trans) {
467 struct virtio_vsock_sock *ptrans = psk->trans;
469 vvs->peer_buf_alloc = ptrans->peer_buf_alloc;
472 if (vsk->buffer_size > VIRTIO_VSOCK_MAX_BUF_SIZE)
473 vsk->buffer_size = VIRTIO_VSOCK_MAX_BUF_SIZE;
475 vvs->buf_alloc = vsk->buffer_size;
477 spin_lock_init(&vvs->rx_lock);
478 spin_lock_init(&vvs->tx_lock);
479 INIT_LIST_HEAD(&vvs->rx_queue);
483 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init);
485 /* sk_lock held by the caller */
486 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val)
488 struct virtio_vsock_sock *vvs = vsk->trans;
490 if (*val > VIRTIO_VSOCK_MAX_BUF_SIZE)
491 *val = VIRTIO_VSOCK_MAX_BUF_SIZE;
493 vvs->buf_alloc = *val;
495 virtio_transport_send_credit_update(vsk, VIRTIO_VSOCK_TYPE_STREAM,
498 EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size);
501 virtio_transport_notify_poll_in(struct vsock_sock *vsk,
503 bool *data_ready_now)
505 if (vsock_stream_has_data(vsk))
506 *data_ready_now = true;
508 *data_ready_now = false;
512 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in);
515 virtio_transport_notify_poll_out(struct vsock_sock *vsk,
517 bool *space_avail_now)
521 free_space = vsock_stream_has_space(vsk);
523 *space_avail_now = true;
524 else if (free_space == 0)
525 *space_avail_now = false;
529 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out);
531 int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
532 size_t target, struct vsock_transport_recv_notify_data *data)
536 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init);
538 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
539 size_t target, struct vsock_transport_recv_notify_data *data)
543 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block);
545 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
546 size_t target, struct vsock_transport_recv_notify_data *data)
550 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue);
552 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
553 size_t target, ssize_t copied, bool data_read,
554 struct vsock_transport_recv_notify_data *data)
558 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue);
560 int virtio_transport_notify_send_init(struct vsock_sock *vsk,
561 struct vsock_transport_send_notify_data *data)
565 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init);
567 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
568 struct vsock_transport_send_notify_data *data)
572 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block);
574 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
575 struct vsock_transport_send_notify_data *data)
579 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue);
581 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
582 ssize_t written, struct vsock_transport_send_notify_data *data)
586 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue);
588 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk)
590 return vsk->buffer_size;
592 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat);
594 bool virtio_transport_stream_is_active(struct vsock_sock *vsk)
598 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active);
600 bool virtio_transport_stream_allow(u32 cid, u32 port)
604 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow);
606 int virtio_transport_dgram_bind(struct vsock_sock *vsk,
607 struct sockaddr_vm *addr)
611 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind);
613 bool virtio_transport_dgram_allow(u32 cid, u32 port)
617 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow);
619 int virtio_transport_connect(struct vsock_sock *vsk)
621 struct virtio_vsock_pkt_info info = {
622 .op = VIRTIO_VSOCK_OP_REQUEST,
623 .type = VIRTIO_VSOCK_TYPE_STREAM,
627 return virtio_transport_send_pkt_info(vsk, &info);
629 EXPORT_SYMBOL_GPL(virtio_transport_connect);
631 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode)
633 struct virtio_vsock_pkt_info info = {
634 .op = VIRTIO_VSOCK_OP_SHUTDOWN,
635 .type = VIRTIO_VSOCK_TYPE_STREAM,
636 .flags = (mode & RCV_SHUTDOWN ?
637 VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
638 (mode & SEND_SHUTDOWN ?
639 VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
643 return virtio_transport_send_pkt_info(vsk, &info);
645 EXPORT_SYMBOL_GPL(virtio_transport_shutdown);
648 virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
649 struct sockaddr_vm *remote_addr,
655 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue);
658 virtio_transport_stream_enqueue(struct vsock_sock *vsk,
662 struct virtio_vsock_pkt_info info = {
663 .op = VIRTIO_VSOCK_OP_RW,
664 .type = VIRTIO_VSOCK_TYPE_STREAM,
670 return virtio_transport_send_pkt_info(vsk, &info);
672 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue);
674 void virtio_transport_destruct(struct vsock_sock *vsk)
676 struct virtio_vsock_sock *vvs = vsk->trans;
680 EXPORT_SYMBOL_GPL(virtio_transport_destruct);
682 static int virtio_transport_reset(struct vsock_sock *vsk,
683 struct virtio_vsock_pkt *pkt)
685 struct virtio_vsock_pkt_info info = {
686 .op = VIRTIO_VSOCK_OP_RST,
687 .type = VIRTIO_VSOCK_TYPE_STREAM,
692 /* Send RST only if the original pkt is not a RST pkt */
693 if (pkt && le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_RST)
696 return virtio_transport_send_pkt_info(vsk, &info);
699 /* Normally packets are associated with a socket. There may be no socket if an
700 * attempt was made to connect to a socket that does not exist.
702 static int virtio_transport_reset_no_sock(const struct virtio_transport *t,
703 struct virtio_vsock_pkt *pkt)
705 struct virtio_vsock_pkt *reply;
706 struct virtio_vsock_pkt_info info = {
707 .op = VIRTIO_VSOCK_OP_RST,
708 .type = le16_to_cpu(pkt->hdr.type),
712 /* Send RST only if the original pkt is not a RST pkt */
713 if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_RST)
716 reply = virtio_transport_alloc_pkt(&info, 0,
717 le64_to_cpu(pkt->hdr.dst_cid),
718 le32_to_cpu(pkt->hdr.dst_port),
719 le64_to_cpu(pkt->hdr.src_cid),
720 le32_to_cpu(pkt->hdr.src_port));
725 virtio_transport_free_pkt(reply);
729 return t->send_pkt(reply);
732 static void virtio_transport_wait_close(struct sock *sk, long timeout)
735 DEFINE_WAIT_FUNC(wait, woken_wake_function);
737 add_wait_queue(sk_sleep(sk), &wait);
740 if (sk_wait_event(sk, &timeout,
741 sock_flag(sk, SOCK_DONE), &wait))
743 } while (!signal_pending(current) && timeout);
745 remove_wait_queue(sk_sleep(sk), &wait);
749 static void virtio_transport_do_close(struct vsock_sock *vsk,
752 struct sock *sk = sk_vsock(vsk);
754 sock_set_flag(sk, SOCK_DONE);
755 vsk->peer_shutdown = SHUTDOWN_MASK;
756 if (vsock_stream_has_data(vsk) <= 0)
757 sk->sk_state = TCP_CLOSING;
758 sk->sk_state_change(sk);
760 if (vsk->close_work_scheduled &&
761 (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) {
762 vsk->close_work_scheduled = false;
764 vsock_remove_sock(vsk);
766 /* Release refcnt obtained when we scheduled the timeout */
771 static void virtio_transport_close_timeout(struct work_struct *work)
773 struct vsock_sock *vsk =
774 container_of(work, struct vsock_sock, close_work.work);
775 struct sock *sk = sk_vsock(vsk);
780 if (!sock_flag(sk, SOCK_DONE)) {
781 (void)virtio_transport_reset(vsk, NULL);
783 virtio_transport_do_close(vsk, false);
786 vsk->close_work_scheduled = false;
792 /* User context, vsk->sk is locked */
793 static bool virtio_transport_close(struct vsock_sock *vsk)
795 struct sock *sk = &vsk->sk;
797 if (!(sk->sk_state == TCP_ESTABLISHED ||
798 sk->sk_state == TCP_CLOSING))
801 /* Already received SHUTDOWN from peer, reply with RST */
802 if ((vsk->peer_shutdown & SHUTDOWN_MASK) == SHUTDOWN_MASK) {
803 (void)virtio_transport_reset(vsk, NULL);
807 if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
808 (void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK);
810 if (sock_flag(sk, SOCK_LINGER) && !(current->flags & PF_EXITING))
811 virtio_transport_wait_close(sk, sk->sk_lingertime);
813 if (sock_flag(sk, SOCK_DONE)) {
818 INIT_DELAYED_WORK(&vsk->close_work,
819 virtio_transport_close_timeout);
820 vsk->close_work_scheduled = true;
821 schedule_delayed_work(&vsk->close_work, VSOCK_CLOSE_TIMEOUT);
825 void virtio_transport_release(struct vsock_sock *vsk)
827 struct virtio_vsock_sock *vvs = vsk->trans;
828 struct virtio_vsock_pkt *pkt, *tmp;
829 struct sock *sk = &vsk->sk;
830 bool remove_sock = true;
832 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
833 if (sk->sk_type == SOCK_STREAM)
834 remove_sock = virtio_transport_close(vsk);
836 list_for_each_entry_safe(pkt, tmp, &vvs->rx_queue, list) {
837 list_del(&pkt->list);
838 virtio_transport_free_pkt(pkt);
843 vsock_remove_sock(vsk);
845 EXPORT_SYMBOL_GPL(virtio_transport_release);
848 virtio_transport_recv_connecting(struct sock *sk,
849 struct virtio_vsock_pkt *pkt)
851 struct vsock_sock *vsk = vsock_sk(sk);
855 switch (le16_to_cpu(pkt->hdr.op)) {
856 case VIRTIO_VSOCK_OP_RESPONSE:
857 sk->sk_state = TCP_ESTABLISHED;
858 sk->sk_socket->state = SS_CONNECTED;
859 vsock_insert_connected(vsk);
860 sk->sk_state_change(sk);
862 case VIRTIO_VSOCK_OP_INVALID:
864 case VIRTIO_VSOCK_OP_RST:
876 virtio_transport_reset(vsk, pkt);
877 sk->sk_state = TCP_CLOSE;
879 sk->sk_error_report(sk);
884 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
885 struct virtio_vsock_pkt *pkt)
887 struct virtio_vsock_sock *vvs = vsk->trans;
888 bool can_enqueue, free_pkt = false;
890 pkt->len = le32_to_cpu(pkt->hdr.len);
893 spin_lock_bh(&vvs->rx_lock);
895 can_enqueue = virtio_transport_inc_rx_pkt(vvs, pkt);
901 /* Try to copy small packets into the buffer of last packet queued,
902 * to avoid wasting memory queueing the entire buffer with a small
905 if (pkt->len <= GOOD_COPY_LEN && !list_empty(&vvs->rx_queue)) {
906 struct virtio_vsock_pkt *last_pkt;
908 last_pkt = list_last_entry(&vvs->rx_queue,
909 struct virtio_vsock_pkt, list);
911 /* If there is space in the last packet queued, we copy the
912 * new packet in its buffer.
914 if (pkt->len <= last_pkt->buf_len - last_pkt->len) {
915 memcpy(last_pkt->buf + last_pkt->len, pkt->buf,
917 last_pkt->len += pkt->len;
923 list_add_tail(&pkt->list, &vvs->rx_queue);
926 spin_unlock_bh(&vvs->rx_lock);
928 virtio_transport_free_pkt(pkt);
932 virtio_transport_recv_connected(struct sock *sk,
933 struct virtio_vsock_pkt *pkt)
935 struct vsock_sock *vsk = vsock_sk(sk);
938 switch (le16_to_cpu(pkt->hdr.op)) {
939 case VIRTIO_VSOCK_OP_RW:
940 virtio_transport_recv_enqueue(vsk, pkt);
941 sk->sk_data_ready(sk);
943 case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
944 sk->sk_write_space(sk);
946 case VIRTIO_VSOCK_OP_SHUTDOWN:
947 if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SHUTDOWN_RCV)
948 vsk->peer_shutdown |= RCV_SHUTDOWN;
949 if (le32_to_cpu(pkt->hdr.flags) & VIRTIO_VSOCK_SHUTDOWN_SEND)
950 vsk->peer_shutdown |= SEND_SHUTDOWN;
951 if (vsk->peer_shutdown == SHUTDOWN_MASK &&
952 vsock_stream_has_data(vsk) <= 0 &&
953 !sock_flag(sk, SOCK_DONE)) {
954 (void)virtio_transport_reset(vsk, NULL);
956 virtio_transport_do_close(vsk, true);
958 if (le32_to_cpu(pkt->hdr.flags))
959 sk->sk_state_change(sk);
961 case VIRTIO_VSOCK_OP_RST:
962 virtio_transport_do_close(vsk, true);
969 virtio_transport_free_pkt(pkt);
974 virtio_transport_recv_disconnecting(struct sock *sk,
975 struct virtio_vsock_pkt *pkt)
977 struct vsock_sock *vsk = vsock_sk(sk);
979 if (le16_to_cpu(pkt->hdr.op) == VIRTIO_VSOCK_OP_RST)
980 virtio_transport_do_close(vsk, true);
984 virtio_transport_send_response(struct vsock_sock *vsk,
985 struct virtio_vsock_pkt *pkt)
987 struct virtio_vsock_pkt_info info = {
988 .op = VIRTIO_VSOCK_OP_RESPONSE,
989 .type = VIRTIO_VSOCK_TYPE_STREAM,
990 .remote_cid = le64_to_cpu(pkt->hdr.src_cid),
991 .remote_port = le32_to_cpu(pkt->hdr.src_port),
996 return virtio_transport_send_pkt_info(vsk, &info);
999 static bool virtio_transport_space_update(struct sock *sk,
1000 struct virtio_vsock_pkt *pkt)
1002 struct vsock_sock *vsk = vsock_sk(sk);
1003 struct virtio_vsock_sock *vvs = vsk->trans;
1004 bool space_available;
1006 /* Listener sockets are not associated with any transport, so we are
1007 * not able to take the state to see if there is space available in the
1008 * remote peer, but since they are only used to receive requests, we
1009 * can assume that there is always space available in the other peer.
1014 /* buf_alloc and fwd_cnt is always included in the hdr */
1015 spin_lock_bh(&vvs->tx_lock);
1016 vvs->peer_buf_alloc = le32_to_cpu(pkt->hdr.buf_alloc);
1017 vvs->peer_fwd_cnt = le32_to_cpu(pkt->hdr.fwd_cnt);
1018 space_available = virtio_transport_has_space(vsk);
1019 spin_unlock_bh(&vvs->tx_lock);
1020 return space_available;
1023 /* Handle server socket */
1025 virtio_transport_recv_listen(struct sock *sk, struct virtio_vsock_pkt *pkt,
1026 struct virtio_transport *t)
1028 struct vsock_sock *vsk = vsock_sk(sk);
1029 struct vsock_sock *vchild;
1033 if (le16_to_cpu(pkt->hdr.op) != VIRTIO_VSOCK_OP_REQUEST) {
1034 virtio_transport_reset_no_sock(t, pkt);
1038 if (sk_acceptq_is_full(sk)) {
1039 virtio_transport_reset_no_sock(t, pkt);
1043 child = vsock_create_connected(sk);
1045 virtio_transport_reset_no_sock(t, pkt);
1049 sk_acceptq_added(sk);
1051 lock_sock_nested(child, SINGLE_DEPTH_NESTING);
1053 child->sk_state = TCP_ESTABLISHED;
1055 vchild = vsock_sk(child);
1056 vsock_addr_init(&vchild->local_addr, le64_to_cpu(pkt->hdr.dst_cid),
1057 le32_to_cpu(pkt->hdr.dst_port));
1058 vsock_addr_init(&vchild->remote_addr, le64_to_cpu(pkt->hdr.src_cid),
1059 le32_to_cpu(pkt->hdr.src_port));
1061 ret = vsock_assign_transport(vchild, vsk);
1062 /* Transport assigned (looking at remote_addr) must be the same
1063 * where we received the request.
1065 if (ret || vchild->transport != &t->transport) {
1066 release_sock(child);
1067 virtio_transport_reset_no_sock(t, pkt);
1072 if (virtio_transport_space_update(child, pkt))
1073 child->sk_write_space(child);
1075 vsock_insert_connected(vchild);
1076 vsock_enqueue_accept(sk, child);
1077 virtio_transport_send_response(vchild, pkt);
1079 release_sock(child);
1081 sk->sk_data_ready(sk);
1085 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
1088 void virtio_transport_recv_pkt(struct virtio_transport *t,
1089 struct virtio_vsock_pkt *pkt)
1091 struct sockaddr_vm src, dst;
1092 struct vsock_sock *vsk;
1094 bool space_available;
1096 vsock_addr_init(&src, le64_to_cpu(pkt->hdr.src_cid),
1097 le32_to_cpu(pkt->hdr.src_port));
1098 vsock_addr_init(&dst, le64_to_cpu(pkt->hdr.dst_cid),
1099 le32_to_cpu(pkt->hdr.dst_port));
1101 trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,
1102 dst.svm_cid, dst.svm_port,
1103 le32_to_cpu(pkt->hdr.len),
1104 le16_to_cpu(pkt->hdr.type),
1105 le16_to_cpu(pkt->hdr.op),
1106 le32_to_cpu(pkt->hdr.flags),
1107 le32_to_cpu(pkt->hdr.buf_alloc),
1108 le32_to_cpu(pkt->hdr.fwd_cnt));
1110 if (le16_to_cpu(pkt->hdr.type) != VIRTIO_VSOCK_TYPE_STREAM) {
1111 (void)virtio_transport_reset_no_sock(t, pkt);
1115 /* The socket must be in connected or bound table
1116 * otherwise send reset back
1118 sk = vsock_find_connected_socket(&src, &dst);
1120 sk = vsock_find_bound_socket(&dst);
1122 (void)virtio_transport_reset_no_sock(t, pkt);
1129 space_available = virtio_transport_space_update(sk, pkt);
1133 /* Update CID in case it has changed after a transport reset event */
1134 vsk->local_addr.svm_cid = dst.svm_cid;
1136 if (space_available)
1137 sk->sk_write_space(sk);
1139 switch (sk->sk_state) {
1141 virtio_transport_recv_listen(sk, pkt, t);
1142 virtio_transport_free_pkt(pkt);
1145 virtio_transport_recv_connecting(sk, pkt);
1146 virtio_transport_free_pkt(pkt);
1148 case TCP_ESTABLISHED:
1149 virtio_transport_recv_connected(sk, pkt);
1152 virtio_transport_recv_disconnecting(sk, pkt);
1153 virtio_transport_free_pkt(pkt);
1156 virtio_transport_free_pkt(pkt);
1162 /* Release refcnt obtained when we fetched this socket out of the
1163 * bound or connected list.
1169 virtio_transport_free_pkt(pkt);
1171 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);
1173 void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt)
1178 EXPORT_SYMBOL_GPL(virtio_transport_free_pkt);
1180 MODULE_LICENSE("GPL v2");
1181 MODULE_AUTHOR("Asias He");
1182 MODULE_DESCRIPTION("common code for virtio vsock");