blob: f800f89068dbf93ed928998b026dffbdfacbd043 [file] [log] [blame]
Asias He433fc582016-07-28 15:36:34 +01001/*
2 * vhost 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 * This work is licensed under the terms of the GNU GPL, version 2.
9 */
10#include <linux/miscdevice.h>
11#include <linux/atomic.h>
12#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/vmalloc.h>
15#include <net/sock.h>
16#include <linux/virtio_vsock.h>
17#include <linux/vhost.h>
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000018#include <linux/hashtable.h>
Asias He433fc582016-07-28 15:36:34 +010019
20#include <net/af_vsock.h>
21#include "vhost.h"
22
23#define VHOST_VSOCK_DEFAULT_HOST_CID 2
24
25enum {
26 VHOST_VSOCK_FEATURES = VHOST_FEATURES,
27};
28
29/* Used to track all the vhost_vsock instances on the system. */
30static DEFINE_SPINLOCK(vhost_vsock_lock);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000031static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8);
Asias He433fc582016-07-28 15:36:34 +010032
33struct vhost_vsock {
34 struct vhost_dev dev;
35 struct vhost_virtqueue vqs[2];
36
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000037 /* Link to global vhost_vsock_hash, writes use vhost_vsock_lock */
38 struct hlist_node hash;
Asias He433fc582016-07-28 15:36:34 +010039
40 struct vhost_work send_pkt_work;
41 spinlock_t send_pkt_list_lock;
42 struct list_head send_pkt_list; /* host->guest pending packets */
43
44 atomic_t queued_replies;
45
46 u32 guest_cid;
47};
48
49static u32 vhost_transport_get_local_cid(void)
50{
51 return VHOST_VSOCK_DEFAULT_HOST_CID;
52}
53
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000054/* Callers that dereference the return value must hold vhost_vsock_lock or the
55 * RCU read lock.
56 */
57static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
Asias He433fc582016-07-28 15:36:34 +010058{
59 struct vhost_vsock *vsock;
60
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000061 hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid) {
Asias He433fc582016-07-28 15:36:34 +010062 u32 other_cid = vsock->guest_cid;
63
64 /* Skip instances that have no CID yet */
65 if (other_cid == 0)
66 continue;
67
68 if (other_cid == guest_cid) {
Asias He433fc582016-07-28 15:36:34 +010069 return vsock;
70 }
71 }
Asias He433fc582016-07-28 15:36:34 +010072
73 return NULL;
74}
75
76static void
77vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
78 struct vhost_virtqueue *vq)
79{
80 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
81 bool added = false;
82 bool restart_tx = false;
83
84 mutex_lock(&vq->mutex);
85
86 if (!vq->private_data)
87 goto out;
88
89 /* Avoid further vmexits, we're already processing the virtqueue */
90 vhost_disable_notify(&vsock->dev, vq);
91
92 for (;;) {
93 struct virtio_vsock_pkt *pkt;
94 struct iov_iter iov_iter;
95 unsigned out, in;
96 size_t nbytes;
97 size_t len;
98 int head;
99
100 spin_lock_bh(&vsock->send_pkt_list_lock);
101 if (list_empty(&vsock->send_pkt_list)) {
102 spin_unlock_bh(&vsock->send_pkt_list_lock);
103 vhost_enable_notify(&vsock->dev, vq);
104 break;
105 }
106
107 pkt = list_first_entry(&vsock->send_pkt_list,
108 struct virtio_vsock_pkt, list);
109 list_del_init(&pkt->list);
110 spin_unlock_bh(&vsock->send_pkt_list_lock);
111
112 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
113 &out, &in, NULL, NULL);
114 if (head < 0) {
115 spin_lock_bh(&vsock->send_pkt_list_lock);
116 list_add(&pkt->list, &vsock->send_pkt_list);
117 spin_unlock_bh(&vsock->send_pkt_list_lock);
118 break;
119 }
120
121 if (head == vq->num) {
122 spin_lock_bh(&vsock->send_pkt_list_lock);
123 list_add(&pkt->list, &vsock->send_pkt_list);
124 spin_unlock_bh(&vsock->send_pkt_list_lock);
125
126 /* We cannot finish yet if more buffers snuck in while
127 * re-enabling notify.
128 */
129 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
130 vhost_disable_notify(&vsock->dev, vq);
131 continue;
132 }
133 break;
134 }
135
136 if (out) {
137 virtio_transport_free_pkt(pkt);
138 vq_err(vq, "Expected 0 output buffers, got %u\n", out);
139 break;
140 }
141
142 len = iov_length(&vq->iov[out], in);
143 iov_iter_init(&iov_iter, READ, &vq->iov[out], in, len);
144
145 nbytes = copy_to_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
146 if (nbytes != sizeof(pkt->hdr)) {
147 virtio_transport_free_pkt(pkt);
148 vq_err(vq, "Faulted on copying pkt hdr\n");
149 break;
150 }
151
152 nbytes = copy_to_iter(pkt->buf, pkt->len, &iov_iter);
153 if (nbytes != pkt->len) {
154 virtio_transport_free_pkt(pkt);
155 vq_err(vq, "Faulted on copying pkt buf\n");
156 break;
157 }
158
159 vhost_add_used(vq, head, sizeof(pkt->hdr) + pkt->len);
160 added = true;
161
162 if (pkt->reply) {
163 int val;
164
165 val = atomic_dec_return(&vsock->queued_replies);
166
167 /* Do we have resources to resume tx processing? */
168 if (val + 1 == tx_vq->num)
169 restart_tx = true;
170 }
171
172 virtio_transport_free_pkt(pkt);
173 }
174 if (added)
175 vhost_signal(&vsock->dev, vq);
176
177out:
178 mutex_unlock(&vq->mutex);
179
180 if (restart_tx)
181 vhost_poll_queue(&tx_vq->poll);
182}
183
184static void vhost_transport_send_pkt_work(struct vhost_work *work)
185{
186 struct vhost_virtqueue *vq;
187 struct vhost_vsock *vsock;
188
189 vsock = container_of(work, struct vhost_vsock, send_pkt_work);
190 vq = &vsock->vqs[VSOCK_VQ_RX];
191
192 vhost_transport_do_send_pkt(vsock, vq);
193}
194
195static int
196vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
197{
198 struct vhost_vsock *vsock;
199 struct vhost_virtqueue *vq;
200 int len = pkt->len;
201
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000202 rcu_read_lock();
203
Asias He433fc582016-07-28 15:36:34 +0100204 /* Find the vhost_vsock according to guest context id */
205 vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
206 if (!vsock) {
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000207 rcu_read_unlock();
Asias He433fc582016-07-28 15:36:34 +0100208 virtio_transport_free_pkt(pkt);
209 return -ENODEV;
210 }
211
212 vq = &vsock->vqs[VSOCK_VQ_RX];
213
214 if (pkt->reply)
215 atomic_inc(&vsock->queued_replies);
216
217 spin_lock_bh(&vsock->send_pkt_list_lock);
218 list_add_tail(&pkt->list, &vsock->send_pkt_list);
219 spin_unlock_bh(&vsock->send_pkt_list_lock);
220
221 vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000222
223 rcu_read_unlock();
Asias He433fc582016-07-28 15:36:34 +0100224 return len;
225}
226
Peng Tao482b3f92017-03-15 09:32:15 +0800227static int
228vhost_transport_cancel_pkt(struct vsock_sock *vsk)
229{
230 struct vhost_vsock *vsock;
231 struct virtio_vsock_pkt *pkt, *n;
232 int cnt = 0;
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000233 int ret = -ENODEV;
Peng Tao482b3f92017-03-15 09:32:15 +0800234 LIST_HEAD(freeme);
235
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000236 rcu_read_lock();
237
Peng Tao482b3f92017-03-15 09:32:15 +0800238 /* Find the vhost_vsock according to guest context id */
239 vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
240 if (!vsock)
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000241 goto out;
Peng Tao482b3f92017-03-15 09:32:15 +0800242
243 spin_lock_bh(&vsock->send_pkt_list_lock);
244 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
245 if (pkt->vsk != vsk)
246 continue;
247 list_move(&pkt->list, &freeme);
248 }
249 spin_unlock_bh(&vsock->send_pkt_list_lock);
250
251 list_for_each_entry_safe(pkt, n, &freeme, list) {
252 if (pkt->reply)
253 cnt++;
254 list_del(&pkt->list);
255 virtio_transport_free_pkt(pkt);
256 }
257
258 if (cnt) {
259 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
260 int new_cnt;
261
262 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
263 if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
264 vhost_poll_queue(&tx_vq->poll);
265 }
266
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000267 ret = 0;
268out:
269 rcu_read_unlock();
270 return ret;
Peng Tao482b3f92017-03-15 09:32:15 +0800271}
272
Asias He433fc582016-07-28 15:36:34 +0100273static struct virtio_vsock_pkt *
274vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
275 unsigned int out, unsigned int in)
276{
277 struct virtio_vsock_pkt *pkt;
278 struct iov_iter iov_iter;
279 size_t nbytes;
280 size_t len;
281
282 if (in != 0) {
283 vq_err(vq, "Expected 0 input buffers, got %u\n", in);
284 return NULL;
285 }
286
287 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
288 if (!pkt)
289 return NULL;
290
291 len = iov_length(vq->iov, out);
292 iov_iter_init(&iov_iter, WRITE, vq->iov, out, len);
293
294 nbytes = copy_from_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
295 if (nbytes != sizeof(pkt->hdr)) {
296 vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n",
297 sizeof(pkt->hdr), nbytes);
298 kfree(pkt);
299 return NULL;
300 }
301
302 if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
303 pkt->len = le32_to_cpu(pkt->hdr.len);
304
305 /* No payload */
306 if (!pkt->len)
307 return pkt;
308
309 /* The pkt is too big */
310 if (pkt->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
311 kfree(pkt);
312 return NULL;
313 }
314
315 pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
316 if (!pkt->buf) {
317 kfree(pkt);
318 return NULL;
319 }
320
321 nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
322 if (nbytes != pkt->len) {
323 vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
324 pkt->len, nbytes);
325 virtio_transport_free_pkt(pkt);
326 return NULL;
327 }
328
329 return pkt;
330}
331
332/* Is there space left for replies to rx packets? */
333static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
334{
335 struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX];
336 int val;
337
338 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
339 val = atomic_read(&vsock->queued_replies);
340
341 return val < vq->num;
342}
343
344static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
345{
346 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
347 poll.work);
348 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
349 dev);
350 struct virtio_vsock_pkt *pkt;
351 int head;
352 unsigned int out, in;
353 bool added = false;
354
355 mutex_lock(&vq->mutex);
356
357 if (!vq->private_data)
358 goto out;
359
360 vhost_disable_notify(&vsock->dev, vq);
361 for (;;) {
Stefan Hajnoczi3fda5d62016-08-04 14:52:53 +0100362 u32 len;
363
Asias He433fc582016-07-28 15:36:34 +0100364 if (!vhost_vsock_more_replies(vsock)) {
365 /* Stop tx until the device processes already
366 * pending replies. Leave tx virtqueue
367 * callbacks disabled.
368 */
369 goto no_more_replies;
370 }
371
372 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
373 &out, &in, NULL, NULL);
374 if (head < 0)
375 break;
376
377 if (head == vq->num) {
378 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
379 vhost_disable_notify(&vsock->dev, vq);
380 continue;
381 }
382 break;
383 }
384
385 pkt = vhost_vsock_alloc_pkt(vq, out, in);
386 if (!pkt) {
387 vq_err(vq, "Faulted on pkt\n");
388 continue;
389 }
390
Stefan Hajnoczi3fda5d62016-08-04 14:52:53 +0100391 len = pkt->len;
392
Asias He433fc582016-07-28 15:36:34 +0100393 /* Only accept correctly addressed packets */
394 if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
395 virtio_transport_recv_pkt(pkt);
396 else
397 virtio_transport_free_pkt(pkt);
398
Stefan Hajnoczi3fda5d62016-08-04 14:52:53 +0100399 vhost_add_used(vq, head, sizeof(pkt->hdr) + len);
Asias He433fc582016-07-28 15:36:34 +0100400 added = true;
401 }
402
403no_more_replies:
404 if (added)
405 vhost_signal(&vsock->dev, vq);
406
407out:
408 mutex_unlock(&vq->mutex);
409}
410
411static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
412{
413 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
414 poll.work);
415 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
416 dev);
417
418 vhost_transport_do_send_pkt(vsock, vq);
419}
420
421static int vhost_vsock_start(struct vhost_vsock *vsock)
422{
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000423 struct vhost_virtqueue *vq;
Asias He433fc582016-07-28 15:36:34 +0100424 size_t i;
425 int ret;
426
427 mutex_lock(&vsock->dev.mutex);
428
429 ret = vhost_dev_check_owner(&vsock->dev);
430 if (ret)
431 goto err;
432
433 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000434 vq = &vsock->vqs[i];
Asias He433fc582016-07-28 15:36:34 +0100435
436 mutex_lock(&vq->mutex);
437
438 if (!vhost_vq_access_ok(vq)) {
439 ret = -EFAULT;
Asias He433fc582016-07-28 15:36:34 +0100440 goto err_vq;
441 }
442
443 if (!vq->private_data) {
444 vq->private_data = vsock;
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000445 ret = vhost_vq_init_access(vq);
446 if (ret)
447 goto err_vq;
Asias He433fc582016-07-28 15:36:34 +0100448 }
449
450 mutex_unlock(&vq->mutex);
451 }
452
453 mutex_unlock(&vsock->dev.mutex);
454 return 0;
455
456err_vq:
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000457 vq->private_data = NULL;
458 mutex_unlock(&vq->mutex);
459
Asias He433fc582016-07-28 15:36:34 +0100460 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000461 vq = &vsock->vqs[i];
Asias He433fc582016-07-28 15:36:34 +0100462
463 mutex_lock(&vq->mutex);
464 vq->private_data = NULL;
465 mutex_unlock(&vq->mutex);
466 }
467err:
468 mutex_unlock(&vsock->dev.mutex);
469 return ret;
470}
471
472static int vhost_vsock_stop(struct vhost_vsock *vsock)
473{
474 size_t i;
475 int ret;
476
477 mutex_lock(&vsock->dev.mutex);
478
479 ret = vhost_dev_check_owner(&vsock->dev);
480 if (ret)
481 goto err;
482
483 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
484 struct vhost_virtqueue *vq = &vsock->vqs[i];
485
486 mutex_lock(&vq->mutex);
487 vq->private_data = NULL;
488 mutex_unlock(&vq->mutex);
489 }
490
491err:
492 mutex_unlock(&vsock->dev.mutex);
493 return ret;
494}
495
496static void vhost_vsock_free(struct vhost_vsock *vsock)
497{
Wei Yongjunb226aca2016-08-02 13:50:42 +0000498 kvfree(vsock);
Asias He433fc582016-07-28 15:36:34 +0100499}
500
501static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
502{
503 struct vhost_virtqueue **vqs;
504 struct vhost_vsock *vsock;
505 int ret;
506
507 /* This struct is large and allocation could fail, fall back to vmalloc
508 * if there is no other way.
509 */
510 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
511 if (!vsock) {
512 vsock = vmalloc(sizeof(*vsock));
513 if (!vsock)
514 return -ENOMEM;
515 }
516
517 vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
518 if (!vqs) {
519 ret = -ENOMEM;
520 goto out;
521 }
522
523 atomic_set(&vsock->queued_replies, 0);
524
525 vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
526 vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
527 vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
528 vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
529
530 vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs));
531
532 file->private_data = vsock;
533 spin_lock_init(&vsock->send_pkt_list_lock);
534 INIT_LIST_HEAD(&vsock->send_pkt_list);
535 vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
Asias He433fc582016-07-28 15:36:34 +0100536 return 0;
537
538out:
539 vhost_vsock_free(vsock);
540 return ret;
541}
542
543static void vhost_vsock_flush(struct vhost_vsock *vsock)
544{
545 int i;
546
547 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
548 if (vsock->vqs[i].handle_kick)
549 vhost_poll_flush(&vsock->vqs[i].poll);
550 vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
551}
552
553static void vhost_vsock_reset_orphans(struct sock *sk)
554{
555 struct vsock_sock *vsk = vsock_sk(sk);
556
557 /* vmci_transport.c doesn't take sk_lock here either. At least we're
558 * under vsock_table_lock so the sock cannot disappear while we're
559 * executing.
560 */
561
Peng Taoc4587632016-12-09 01:10:46 +0800562 if (!vhost_vsock_get(vsk->remote_addr.svm_cid)) {
Asias He433fc582016-07-28 15:36:34 +0100563 sock_set_flag(sk, SOCK_DONE);
564 vsk->peer_shutdown = SHUTDOWN_MASK;
565 sk->sk_state = SS_UNCONNECTED;
566 sk->sk_err = ECONNRESET;
567 sk->sk_error_report(sk);
568 }
569}
570
571static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
572{
573 struct vhost_vsock *vsock = file->private_data;
574
575 spin_lock_bh(&vhost_vsock_lock);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000576 if (vsock->guest_cid)
577 hash_del_rcu(&vsock->hash);
Asias He433fc582016-07-28 15:36:34 +0100578 spin_unlock_bh(&vhost_vsock_lock);
579
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000580 /* Wait for other CPUs to finish using vsock */
581 synchronize_rcu();
582
Asias He433fc582016-07-28 15:36:34 +0100583 /* Iterating over all connections for all CIDs to find orphans is
584 * inefficient. Room for improvement here. */
585 vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
586
587 vhost_vsock_stop(vsock);
588 vhost_vsock_flush(vsock);
589 vhost_dev_stop(&vsock->dev);
590
591 spin_lock_bh(&vsock->send_pkt_list_lock);
592 while (!list_empty(&vsock->send_pkt_list)) {
593 struct virtio_vsock_pkt *pkt;
594
595 pkt = list_first_entry(&vsock->send_pkt_list,
596 struct virtio_vsock_pkt, list);
597 list_del_init(&pkt->list);
598 virtio_transport_free_pkt(pkt);
599 }
600 spin_unlock_bh(&vsock->send_pkt_list_lock);
601
602 vhost_dev_cleanup(&vsock->dev, false);
603 kfree(vsock->dev.vqs);
604 vhost_vsock_free(vsock);
605 return 0;
606}
607
608static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
609{
610 struct vhost_vsock *other;
611
612 /* Refuse reserved CIDs */
613 if (guest_cid <= VMADDR_CID_HOST ||
614 guest_cid == U32_MAX)
615 return -EINVAL;
616
617 /* 64-bit CIDs are not yet supported */
618 if (guest_cid > U32_MAX)
619 return -EINVAL;
620
621 /* Refuse if CID is already in use */
Asias He433fc582016-07-28 15:36:34 +0100622 spin_lock_bh(&vhost_vsock_lock);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000623 other = vhost_vsock_get(guest_cid);
Gao feng2d5a1b32016-12-14 19:24:36 +0800624 if (other && other != vsock) {
625 spin_unlock_bh(&vhost_vsock_lock);
626 return -EADDRINUSE;
627 }
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000628
629 if (vsock->guest_cid)
630 hash_del_rcu(&vsock->hash);
631
Asias He433fc582016-07-28 15:36:34 +0100632 vsock->guest_cid = guest_cid;
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000633 hash_add_rcu(vhost_vsock_hash, &vsock->hash, guest_cid);
Asias He433fc582016-07-28 15:36:34 +0100634 spin_unlock_bh(&vhost_vsock_lock);
635
636 return 0;
637}
638
639static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
640{
641 struct vhost_virtqueue *vq;
642 int i;
643
644 if (features & ~VHOST_VSOCK_FEATURES)
645 return -EOPNOTSUPP;
646
647 mutex_lock(&vsock->dev.mutex);
648 if ((features & (1 << VHOST_F_LOG_ALL)) &&
649 !vhost_log_access_ok(&vsock->dev)) {
650 mutex_unlock(&vsock->dev.mutex);
651 return -EFAULT;
652 }
653
654 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
655 vq = &vsock->vqs[i];
656 mutex_lock(&vq->mutex);
657 vq->acked_features = features;
658 mutex_unlock(&vq->mutex);
659 }
660 mutex_unlock(&vsock->dev.mutex);
661 return 0;
662}
663
664static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
665 unsigned long arg)
666{
667 struct vhost_vsock *vsock = f->private_data;
668 void __user *argp = (void __user *)arg;
669 u64 guest_cid;
670 u64 features;
671 int start;
672 int r;
673
674 switch (ioctl) {
675 case VHOST_VSOCK_SET_GUEST_CID:
676 if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
677 return -EFAULT;
678 return vhost_vsock_set_cid(vsock, guest_cid);
679 case VHOST_VSOCK_SET_RUNNING:
680 if (copy_from_user(&start, argp, sizeof(start)))
681 return -EFAULT;
682 if (start)
683 return vhost_vsock_start(vsock);
684 else
685 return vhost_vsock_stop(vsock);
686 case VHOST_GET_FEATURES:
687 features = VHOST_VSOCK_FEATURES;
688 if (copy_to_user(argp, &features, sizeof(features)))
689 return -EFAULT;
690 return 0;
691 case VHOST_SET_FEATURES:
692 if (copy_from_user(&features, argp, sizeof(features)))
693 return -EFAULT;
694 return vhost_vsock_set_features(vsock, features);
695 default:
696 mutex_lock(&vsock->dev.mutex);
697 r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
698 if (r == -ENOIOCTLCMD)
699 r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
700 else
701 vhost_vsock_flush(vsock);
702 mutex_unlock(&vsock->dev.mutex);
703 return r;
704 }
705}
706
707static const struct file_operations vhost_vsock_fops = {
708 .owner = THIS_MODULE,
709 .open = vhost_vsock_dev_open,
710 .release = vhost_vsock_dev_release,
711 .llseek = noop_llseek,
712 .unlocked_ioctl = vhost_vsock_dev_ioctl,
713};
714
715static struct miscdevice vhost_vsock_misc = {
716 .minor = MISC_DYNAMIC_MINOR,
717 .name = "vhost-vsock",
718 .fops = &vhost_vsock_fops,
719};
720
721static struct virtio_transport vhost_transport = {
722 .transport = {
723 .get_local_cid = vhost_transport_get_local_cid,
724
725 .init = virtio_transport_do_socket_init,
726 .destruct = virtio_transport_destruct,
727 .release = virtio_transport_release,
728 .connect = virtio_transport_connect,
729 .shutdown = virtio_transport_shutdown,
Peng Tao482b3f92017-03-15 09:32:15 +0800730 .cancel_pkt = vhost_transport_cancel_pkt,
Asias He433fc582016-07-28 15:36:34 +0100731
732 .dgram_enqueue = virtio_transport_dgram_enqueue,
733 .dgram_dequeue = virtio_transport_dgram_dequeue,
734 .dgram_bind = virtio_transport_dgram_bind,
735 .dgram_allow = virtio_transport_dgram_allow,
736
737 .stream_enqueue = virtio_transport_stream_enqueue,
738 .stream_dequeue = virtio_transport_stream_dequeue,
739 .stream_has_data = virtio_transport_stream_has_data,
740 .stream_has_space = virtio_transport_stream_has_space,
741 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
742 .stream_is_active = virtio_transport_stream_is_active,
743 .stream_allow = virtio_transport_stream_allow,
744
745 .notify_poll_in = virtio_transport_notify_poll_in,
746 .notify_poll_out = virtio_transport_notify_poll_out,
747 .notify_recv_init = virtio_transport_notify_recv_init,
748 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
749 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
750 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
751 .notify_send_init = virtio_transport_notify_send_init,
752 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
753 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
754 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
755
756 .set_buffer_size = virtio_transport_set_buffer_size,
757 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
758 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
759 .get_buffer_size = virtio_transport_get_buffer_size,
760 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
761 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
762 },
763
764 .send_pkt = vhost_transport_send_pkt,
765};
766
767static int __init vhost_vsock_init(void)
768{
769 int ret;
770
771 ret = vsock_core_init(&vhost_transport.transport);
772 if (ret < 0)
773 return ret;
774 return misc_register(&vhost_vsock_misc);
775};
776
777static void __exit vhost_vsock_exit(void)
778{
779 misc_deregister(&vhost_vsock_misc);
780 vsock_core_exit();
781};
782
783module_init(vhost_vsock_init);
784module_exit(vhost_vsock_exit);
785MODULE_LICENSE("GPL v2");
786MODULE_AUTHOR("Asias He");
787MODULE_DESCRIPTION("vhost transport for vsock ");