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