]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/vmw_vsock/virtio_transport.c
VSOCK: Introduce virtio-vsock.ko
[karo-tx-linux.git] / net / vmw_vsock / virtio_transport.c
1 /*
2  * virtio transport for vsock
3  *
4  * Copyright (C) 2013-2015 Red Hat, Inc.
5  * Author: Asias He <asias@redhat.com>
6  *         Stefan Hajnoczi <stefanha@redhat.com>
7  *
8  * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
9  * early virtio-vsock proof-of-concept bits.
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.
12  */
13 #include <linux/spinlock.h>
14 #include <linux/module.h>
15 #include <linux/list.h>
16 #include <linux/virtio.h>
17 #include <linux/virtio_ids.h>
18 #include <linux/virtio_config.h>
19 #include <linux/virtio_vsock.h>
20 #include <net/sock.h>
21 #include <linux/mutex.h>
22 #include <net/af_vsock.h>
23
24 static struct workqueue_struct *virtio_vsock_workqueue;
25 static struct virtio_vsock *the_virtio_vsock;
26 static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
27 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock);
28
29 struct virtio_vsock {
30         /* Virtio device */
31         struct virtio_device *vdev;
32         /* Virtio virtqueue */
33         struct virtqueue *vqs[VSOCK_VQ_MAX];
34         /* Wait queue for send pkt */
35         wait_queue_head_t queue_wait;
36         /* Work item to send pkt */
37         struct work_struct tx_work;
38         /* Work item to recv pkt */
39         struct work_struct rx_work;
40         /* Mutex to protect send pkt*/
41         struct mutex tx_lock;
42         /* Mutex to protect recv pkt*/
43         struct mutex rx_lock;
44         /* Number of recv buffers */
45         int rx_buf_nr;
46         /* Number of max recv buffers */
47         int rx_buf_max_nr;
48         /* Used for global tx buf limitation */
49         u32 total_tx_buf;
50         /* Guest context id, just like guest ip address */
51         u32 guest_cid;
52 };
53
54 static struct virtio_vsock *virtio_vsock_get(void)
55 {
56         return the_virtio_vsock;
57 }
58
59 static u32 virtio_transport_get_local_cid(void)
60 {
61         struct virtio_vsock *vsock = virtio_vsock_get();
62
63         return vsock->guest_cid;
64 }
65
66 static int
67 virtio_transport_send_pkt(struct vsock_sock *vsk,
68                           struct virtio_vsock_pkt_info *info)
69 {
70         u32 src_cid, src_port, dst_cid, dst_port;
71         int ret, in_sg = 0, out_sg = 0;
72         struct virtio_transport *trans;
73         struct virtio_vsock_pkt *pkt;
74         struct virtio_vsock *vsock;
75         struct scatterlist hdr, buf, *sgs[2];
76         struct virtqueue *vq;
77         u32 pkt_len = info->pkt_len;
78         DEFINE_WAIT(wait);
79
80         vsock = virtio_vsock_get();
81         if (!vsock)
82                 return -ENODEV;
83
84         src_cid = virtio_transport_get_local_cid();
85         src_port = vsk->local_addr.svm_port;
86         if (!info->remote_cid) {
87                 dst_cid = vsk->remote_addr.svm_cid;
88                 dst_port = vsk->remote_addr.svm_port;
89         } else {
90                 dst_cid = info->remote_cid;
91                 dst_port = info->remote_port;
92         }
93
94         trans = vsk->trans;
95         vq = vsock->vqs[VSOCK_VQ_TX];
96
97         if (pkt_len > VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE)
98                 pkt_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
99         pkt_len = virtio_transport_get_credit(trans, pkt_len);
100         /* Do not send zero length OP_RW pkt*/
101         if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW)
102                 return pkt_len;
103
104         /* Respect global tx buf limitation */
105         mutex_lock(&vsock->tx_lock);
106         while (pkt_len + vsock->total_tx_buf > VIRTIO_VSOCK_MAX_TX_BUF_SIZE) {
107                 prepare_to_wait_exclusive(&vsock->queue_wait, &wait,
108                                           TASK_UNINTERRUPTIBLE);
109                 mutex_unlock(&vsock->tx_lock);
110                 schedule();
111                 mutex_lock(&vsock->tx_lock);
112                 finish_wait(&vsock->queue_wait, &wait);
113         }
114         vsock->total_tx_buf += pkt_len;
115         mutex_unlock(&vsock->tx_lock);
116
117         pkt = virtio_transport_alloc_pkt(vsk, info, pkt_len,
118                                          src_cid, src_port,
119                                          dst_cid, dst_port);
120         if (!pkt) {
121                 mutex_lock(&vsock->tx_lock);
122                 vsock->total_tx_buf -= pkt_len;
123                 mutex_unlock(&vsock->tx_lock);
124                 virtio_transport_put_credit(trans, pkt_len);
125                 return -ENOMEM;
126         }
127
128         pr_debug("%s:info->pkt_len= %d\n", __func__, info->pkt_len);
129
130         /* Will be released in virtio_transport_send_pkt_work */
131         sock_hold(&trans->vsk->sk);
132         virtio_transport_inc_tx_pkt(pkt);
133
134         /* Put pkt in the virtqueue */
135         sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
136         sgs[out_sg++] = &hdr;
137         if (info->msg && info->pkt_len > 0) {
138                 sg_init_one(&buf, pkt->buf, pkt->len);
139                 sgs[out_sg++] = &buf;
140         }
141
142         mutex_lock(&vsock->tx_lock);
143         while ((ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt,
144                                         GFP_KERNEL)) < 0) {
145                 prepare_to_wait_exclusive(&vsock->queue_wait, &wait,
146                                           TASK_UNINTERRUPTIBLE);
147                 mutex_unlock(&vsock->tx_lock);
148                 schedule();
149                 mutex_lock(&vsock->tx_lock);
150                 finish_wait(&vsock->queue_wait, &wait);
151         }
152         virtqueue_kick(vq);
153         mutex_unlock(&vsock->tx_lock);
154
155         return pkt_len;
156 }
157
158 static struct virtio_transport_pkt_ops virtio_ops = {
159         .send_pkt = virtio_transport_send_pkt,
160 };
161
162 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
163 {
164         int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
165         struct virtio_vsock_pkt *pkt;
166         struct scatterlist hdr, buf, *sgs[2];
167         struct virtqueue *vq;
168         int ret;
169
170         vq = vsock->vqs[VSOCK_VQ_RX];
171
172         do {
173                 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
174                 if (!pkt) {
175                         pr_debug("%s: fail to allocate pkt\n", __func__);
176                         goto out;
177                 }
178
179                 /* TODO: use mergeable rx buffer */
180                 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
181                 if (!pkt->buf) {
182                         pr_debug("%s: fail to allocate pkt->buf\n", __func__);
183                         goto err;
184                 }
185
186                 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
187                 sgs[0] = &hdr;
188
189                 sg_init_one(&buf, pkt->buf, buf_len);
190                 sgs[1] = &buf;
191                 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
192                 if (ret)
193                         goto err;
194                 vsock->rx_buf_nr++;
195         } while (vq->num_free);
196         if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
197                 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
198 out:
199         virtqueue_kick(vq);
200         return;
201 err:
202         virtqueue_kick(vq);
203         virtio_transport_free_pkt(pkt);
204         return;
205 }
206
207 static void virtio_transport_send_pkt_work(struct work_struct *work)
208 {
209         struct virtio_vsock *vsock =
210                 container_of(work, struct virtio_vsock, tx_work);
211         struct virtio_vsock_pkt *pkt;
212         bool added = false;
213         struct virtqueue *vq;
214         unsigned int len;
215         struct sock *sk;
216
217         vq = vsock->vqs[VSOCK_VQ_TX];
218         mutex_lock(&vsock->tx_lock);
219         do {
220                 virtqueue_disable_cb(vq);
221                 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
222                         sk = &pkt->trans->vsk->sk;
223                         virtio_transport_dec_tx_pkt(pkt);
224                         /* Release refcnt taken in virtio_transport_send_pkt */
225                         sock_put(sk);
226                         vsock->total_tx_buf -= pkt->len;
227                         virtio_transport_free_pkt(pkt);
228                         added = true;
229                 }
230         } while (!virtqueue_enable_cb(vq));
231         mutex_unlock(&vsock->tx_lock);
232
233         if (added)
234                 wake_up(&vsock->queue_wait);
235 }
236
237 static void virtio_transport_recv_pkt_work(struct work_struct *work)
238 {
239         struct virtio_vsock *vsock =
240                 container_of(work, struct virtio_vsock, rx_work);
241         struct virtio_vsock_pkt *pkt;
242         struct virtqueue *vq;
243         unsigned int len;
244
245         vq = vsock->vqs[VSOCK_VQ_RX];
246         mutex_lock(&vsock->rx_lock);
247         do {
248                 virtqueue_disable_cb(vq);
249                 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
250                         pkt->len = len;
251                         virtio_transport_recv_pkt(pkt);
252                         vsock->rx_buf_nr--;
253                 }
254         } while (!virtqueue_enable_cb(vq));
255
256         if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
257                 virtio_vsock_rx_fill(vsock);
258         mutex_unlock(&vsock->rx_lock);
259 }
260
261 static void virtio_vsock_ctrl_done(struct virtqueue *vq)
262 {
263 }
264
265 static void virtio_vsock_tx_done(struct virtqueue *vq)
266 {
267         struct virtio_vsock *vsock = vq->vdev->priv;
268
269         if (!vsock)
270                 return;
271         queue_work(virtio_vsock_workqueue, &vsock->tx_work);
272 }
273
274 static void virtio_vsock_rx_done(struct virtqueue *vq)
275 {
276         struct virtio_vsock *vsock = vq->vdev->priv;
277
278         if (!vsock)
279                 return;
280         queue_work(virtio_vsock_workqueue, &vsock->rx_work);
281 }
282
283 static int
284 virtio_transport_socket_init(struct vsock_sock *vsk, struct vsock_sock *psk)
285 {
286         struct virtio_transport *trans;
287         int ret;
288
289         ret = virtio_transport_do_socket_init(vsk, psk);
290         if (ret)
291                 return ret;
292
293         trans = vsk->trans;
294         trans->ops = &virtio_ops;
295         return ret;
296 }
297
298 static struct vsock_transport virtio_transport = {
299         .get_local_cid            = virtio_transport_get_local_cid,
300
301         .init                     = virtio_transport_socket_init,
302         .destruct                 = virtio_transport_destruct,
303         .release                  = virtio_transport_release,
304         .connect                  = virtio_transport_connect,
305         .shutdown                 = virtio_transport_shutdown,
306
307         .dgram_bind               = virtio_transport_dgram_bind,
308         .dgram_dequeue            = virtio_transport_dgram_dequeue,
309         .dgram_enqueue            = virtio_transport_dgram_enqueue,
310         .dgram_allow              = virtio_transport_dgram_allow,
311
312         .stream_dequeue           = virtio_transport_stream_dequeue,
313         .stream_enqueue           = virtio_transport_stream_enqueue,
314         .stream_has_data          = virtio_transport_stream_has_data,
315         .stream_has_space         = virtio_transport_stream_has_space,
316         .stream_rcvhiwat          = virtio_transport_stream_rcvhiwat,
317         .stream_is_active         = virtio_transport_stream_is_active,
318         .stream_allow             = virtio_transport_stream_allow,
319
320         .notify_poll_in           = virtio_transport_notify_poll_in,
321         .notify_poll_out          = virtio_transport_notify_poll_out,
322         .notify_recv_init         = virtio_transport_notify_recv_init,
323         .notify_recv_pre_block    = virtio_transport_notify_recv_pre_block,
324         .notify_recv_pre_dequeue  = virtio_transport_notify_recv_pre_dequeue,
325         .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
326         .notify_send_init         = virtio_transport_notify_send_init,
327         .notify_send_pre_block    = virtio_transport_notify_send_pre_block,
328         .notify_send_pre_enqueue  = virtio_transport_notify_send_pre_enqueue,
329         .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
330
331         .set_buffer_size          = virtio_transport_set_buffer_size,
332         .set_min_buffer_size      = virtio_transport_set_min_buffer_size,
333         .set_max_buffer_size      = virtio_transport_set_max_buffer_size,
334         .get_buffer_size          = virtio_transport_get_buffer_size,
335         .get_min_buffer_size      = virtio_transport_get_min_buffer_size,
336         .get_max_buffer_size      = virtio_transport_get_max_buffer_size,
337 };
338
339 static int virtio_vsock_probe(struct virtio_device *vdev)
340 {
341         vq_callback_t *callbacks[] = {
342                 virtio_vsock_ctrl_done,
343                 virtio_vsock_rx_done,
344                 virtio_vsock_tx_done,
345         };
346         const char *names[] = {
347                 "ctrl",
348                 "rx",
349                 "tx",
350         };
351         struct virtio_vsock *vsock = NULL;
352         u32 guest_cid;
353         int ret;
354
355         ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
356         if (ret)
357                 return ret;
358
359         /* Only one virtio-vsock device per guest is supported */
360         if (the_virtio_vsock) {
361                 ret = -EBUSY;
362                 goto out;
363         }
364
365         vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
366         if (!vsock) {
367                 ret = -ENOMEM;
368                 goto out;
369         }
370
371         vsock->vdev = vdev;
372
373         ret = vsock->vdev->config->find_vqs(vsock->vdev, VSOCK_VQ_MAX,
374                                             vsock->vqs, callbacks, names);
375         if (ret < 0)
376                 goto out;
377
378         vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
379                           &guest_cid, sizeof(guest_cid));
380         vsock->guest_cid = le32_to_cpu(guest_cid);
381         pr_debug("%s:guest_cid=%d\n", __func__, vsock->guest_cid);
382
383         ret = vsock_core_init(&virtio_transport);
384         if (ret < 0)
385                 goto out_vqs;
386
387         vsock->rx_buf_nr = 0;
388         vsock->rx_buf_max_nr = 0;
389
390         vdev->priv = the_virtio_vsock = vsock;
391         init_waitqueue_head(&vsock->queue_wait);
392         mutex_init(&vsock->tx_lock);
393         mutex_init(&vsock->rx_lock);
394         INIT_WORK(&vsock->rx_work, virtio_transport_recv_pkt_work);
395         INIT_WORK(&vsock->tx_work, virtio_transport_send_pkt_work);
396
397         mutex_lock(&vsock->rx_lock);
398         virtio_vsock_rx_fill(vsock);
399         mutex_unlock(&vsock->rx_lock);
400
401         mutex_unlock(&the_virtio_vsock_mutex);
402         return 0;
403
404 out_vqs:
405         vsock->vdev->config->del_vqs(vsock->vdev);
406 out:
407         kfree(vsock);
408         mutex_unlock(&the_virtio_vsock_mutex);
409         return ret;
410 }
411
412 static void virtio_vsock_remove(struct virtio_device *vdev)
413 {
414         struct virtio_vsock *vsock = vdev->priv;
415
416         mutex_lock(&the_virtio_vsock_mutex);
417         the_virtio_vsock = NULL;
418         vsock_core_exit();
419         mutex_unlock(&the_virtio_vsock_mutex);
420
421         kfree(vsock);
422 }
423
424 static struct virtio_device_id id_table[] = {
425         { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
426         { 0 },
427 };
428
429 static unsigned int features[] = {
430 };
431
432 static struct virtio_driver virtio_vsock_driver = {
433         .feature_table = features,
434         .feature_table_size = ARRAY_SIZE(features),
435         .driver.name = KBUILD_MODNAME,
436         .driver.owner = THIS_MODULE,
437         .id_table = id_table,
438         .probe = virtio_vsock_probe,
439         .remove = virtio_vsock_remove,
440 };
441
442 static int __init virtio_vsock_init(void)
443 {
444         int ret;
445
446         virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
447         if (!virtio_vsock_workqueue)
448                 return -ENOMEM;
449         ret = register_virtio_driver(&virtio_vsock_driver);
450         if (ret)
451                 destroy_workqueue(virtio_vsock_workqueue);
452         return ret;
453 }
454
455 static void __exit virtio_vsock_exit(void)
456 {
457         unregister_virtio_driver(&virtio_vsock_driver);
458         destroy_workqueue(virtio_vsock_workqueue);
459 }
460
461 module_init(virtio_vsock_init);
462 module_exit(virtio_vsock_exit);
463 MODULE_LICENSE("GPL v2");
464 MODULE_AUTHOR("Asias He");
465 MODULE_DESCRIPTION("virtio transport for vsock");
466 MODULE_DEVICE_TABLE(virtio, id_table);