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