1 // SPDX-License-Identifier: GPL-2.0-only 2 /* loopback transport for vsock using virtio_transport_common APIs 3 * 4 * Copyright (C) 2013-2019 Red Hat, Inc. 5 * Authors: Asias He <asias@redhat.com> 6 * Stefan Hajnoczi <stefanha@redhat.com> 7 * Stefano Garzarella <sgarzare@redhat.com> 8 * 9 */ 10 #include <linux/spinlock.h> 11 #include <linux/module.h> 12 #include <linux/list.h> 13 #include <linux/virtio_vsock.h> 14 15 struct vsock_loopback { 16 struct workqueue_struct *workqueue; 17 18 struct sk_buff_head pkt_queue; 19 struct work_struct pkt_work; 20 }; 21 22 static struct vsock_loopback the_vsock_loopback; 23 24 static u32 vsock_loopback_get_local_cid(void) 25 { 26 return VMADDR_CID_LOCAL; 27 } 28 29 static int vsock_loopback_send_pkt(struct sk_buff *skb) 30 { 31 struct vsock_loopback *vsock = &the_vsock_loopback; 32 int len = skb->len; 33 34 virtio_vsock_skb_queue_tail(&vsock->pkt_queue, skb); 35 queue_work(vsock->workqueue, &vsock->pkt_work); 36 37 return len; 38 } 39 40 static int vsock_loopback_cancel_pkt(struct vsock_sock *vsk) 41 { 42 struct vsock_loopback *vsock = &the_vsock_loopback; 43 44 virtio_transport_purge_skbs(vsk, &vsock->pkt_queue); 45 46 return 0; 47 } 48 49 static bool vsock_loopback_seqpacket_allow(u32 remote_cid); 50 51 static struct virtio_transport loopback_transport = { 52 .transport = { 53 .module = THIS_MODULE, 54 55 .get_local_cid = vsock_loopback_get_local_cid, 56 57 .init = virtio_transport_do_socket_init, 58 .destruct = virtio_transport_destruct, 59 .release = virtio_transport_release, 60 .connect = virtio_transport_connect, 61 .shutdown = virtio_transport_shutdown, 62 .cancel_pkt = vsock_loopback_cancel_pkt, 63 64 .dgram_bind = virtio_transport_dgram_bind, 65 .dgram_dequeue = virtio_transport_dgram_dequeue, 66 .dgram_enqueue = virtio_transport_dgram_enqueue, 67 .dgram_allow = virtio_transport_dgram_allow, 68 69 .stream_dequeue = virtio_transport_stream_dequeue, 70 .stream_enqueue = virtio_transport_stream_enqueue, 71 .stream_has_data = virtio_transport_stream_has_data, 72 .stream_has_space = virtio_transport_stream_has_space, 73 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat, 74 .stream_is_active = virtio_transport_stream_is_active, 75 .stream_allow = virtio_transport_stream_allow, 76 77 .seqpacket_dequeue = virtio_transport_seqpacket_dequeue, 78 .seqpacket_enqueue = virtio_transport_seqpacket_enqueue, 79 .seqpacket_allow = vsock_loopback_seqpacket_allow, 80 .seqpacket_has_data = virtio_transport_seqpacket_has_data, 81 82 .notify_poll_in = virtio_transport_notify_poll_in, 83 .notify_poll_out = virtio_transport_notify_poll_out, 84 .notify_recv_init = virtio_transport_notify_recv_init, 85 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block, 86 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue, 87 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue, 88 .notify_send_init = virtio_transport_notify_send_init, 89 .notify_send_pre_block = virtio_transport_notify_send_pre_block, 90 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue, 91 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue, 92 .notify_buffer_size = virtio_transport_notify_buffer_size, 93 .notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat, 94 95 .read_skb = virtio_transport_read_skb, 96 }, 97 98 .send_pkt = vsock_loopback_send_pkt, 99 }; 100 101 static bool vsock_loopback_seqpacket_allow(u32 remote_cid) 102 { 103 return true; 104 } 105 106 static void vsock_loopback_work(struct work_struct *work) 107 { 108 struct vsock_loopback *vsock = 109 container_of(work, struct vsock_loopback, pkt_work); 110 struct sk_buff_head pkts; 111 struct sk_buff *skb; 112 113 skb_queue_head_init(&pkts); 114 115 spin_lock_bh(&vsock->pkt_queue.lock); 116 skb_queue_splice_init(&vsock->pkt_queue, &pkts); 117 spin_unlock_bh(&vsock->pkt_queue.lock); 118 119 while ((skb = __skb_dequeue(&pkts))) { 120 virtio_transport_deliver_tap_pkt(skb); 121 virtio_transport_recv_pkt(&loopback_transport, skb); 122 } 123 } 124 125 static int __init vsock_loopback_init(void) 126 { 127 struct vsock_loopback *vsock = &the_vsock_loopback; 128 int ret; 129 130 vsock->workqueue = alloc_workqueue("vsock-loopback", 0, 0); 131 if (!vsock->workqueue) 132 return -ENOMEM; 133 134 skb_queue_head_init(&vsock->pkt_queue); 135 INIT_WORK(&vsock->pkt_work, vsock_loopback_work); 136 137 ret = vsock_core_register(&loopback_transport.transport, 138 VSOCK_TRANSPORT_F_LOCAL); 139 if (ret) 140 goto out_wq; 141 142 return 0; 143 144 out_wq: 145 destroy_workqueue(vsock->workqueue); 146 return ret; 147 } 148 149 static void __exit vsock_loopback_exit(void) 150 { 151 struct vsock_loopback *vsock = &the_vsock_loopback; 152 153 vsock_core_unregister(&loopback_transport.transport); 154 155 flush_work(&vsock->pkt_work); 156 157 virtio_vsock_skb_queue_purge(&vsock->pkt_queue); 158 159 destroy_workqueue(vsock->workqueue); 160 } 161 162 module_init(vsock_loopback_init); 163 module_exit(vsock_loopback_exit); 164 MODULE_LICENSE("GPL v2"); 165 MODULE_AUTHOR("Stefano Garzarella <sgarzare@redhat.com>"); 166 MODULE_DESCRIPTION("loopback transport for vsock"); 167 MODULE_ALIAS_NETPROTO(PF_VSOCK); 168