blob: 3cefd602b5b13073443c56b75efd06834aac83de [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
Stefan Hajnoczi258d8542017-11-09 13:29:10 +0000523 vsock->guest_cid = 0; /* no CID assigned yet */
524
Asias He433fc582016-07-28 15:36:34 +0100525 atomic_set(&vsock->queued_replies, 0);
526
527 vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
528 vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
529 vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
530 vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
531
532 vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs));
533
534 file->private_data = vsock;
535 spin_lock_init(&vsock->send_pkt_list_lock);
536 INIT_LIST_HEAD(&vsock->send_pkt_list);
537 vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
Asias He433fc582016-07-28 15:36:34 +0100538 return 0;
539
540out:
541 vhost_vsock_free(vsock);
542 return ret;
543}
544
545static void vhost_vsock_flush(struct vhost_vsock *vsock)
546{
547 int i;
548
549 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
550 if (vsock->vqs[i].handle_kick)
551 vhost_poll_flush(&vsock->vqs[i].poll);
552 vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
553}
554
555static void vhost_vsock_reset_orphans(struct sock *sk)
556{
557 struct vsock_sock *vsk = vsock_sk(sk);
558
559 /* vmci_transport.c doesn't take sk_lock here either. At least we're
560 * under vsock_table_lock so the sock cannot disappear while we're
561 * executing.
562 */
563
Stefan Hajnoczi06ec6672018-12-06 19:14:34 +0000564 /* If the peer is still valid, no need to reset connection */
565 if (vhost_vsock_get(vsk->remote_addr.svm_cid))
566 return;
567
568 /* If the close timeout is pending, let it expire. This avoids races
569 * with the timeout callback.
570 */
571 if (vsk->close_work_scheduled)
572 return;
573
574 sock_set_flag(sk, SOCK_DONE);
575 vsk->peer_shutdown = SHUTDOWN_MASK;
576 sk->sk_state = SS_UNCONNECTED;
577 sk->sk_err = ECONNRESET;
578 sk->sk_error_report(sk);
Asias He433fc582016-07-28 15:36:34 +0100579}
580
581static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
582{
583 struct vhost_vsock *vsock = file->private_data;
584
585 spin_lock_bh(&vhost_vsock_lock);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000586 if (vsock->guest_cid)
587 hash_del_rcu(&vsock->hash);
Asias He433fc582016-07-28 15:36:34 +0100588 spin_unlock_bh(&vhost_vsock_lock);
589
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000590 /* Wait for other CPUs to finish using vsock */
591 synchronize_rcu();
592
Asias He433fc582016-07-28 15:36:34 +0100593 /* Iterating over all connections for all CIDs to find orphans is
594 * inefficient. Room for improvement here. */
595 vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
596
597 vhost_vsock_stop(vsock);
598 vhost_vsock_flush(vsock);
599 vhost_dev_stop(&vsock->dev);
600
601 spin_lock_bh(&vsock->send_pkt_list_lock);
602 while (!list_empty(&vsock->send_pkt_list)) {
603 struct virtio_vsock_pkt *pkt;
604
605 pkt = list_first_entry(&vsock->send_pkt_list,
606 struct virtio_vsock_pkt, list);
607 list_del_init(&pkt->list);
608 virtio_transport_free_pkt(pkt);
609 }
610 spin_unlock_bh(&vsock->send_pkt_list_lock);
611
612 vhost_dev_cleanup(&vsock->dev, false);
613 kfree(vsock->dev.vqs);
614 vhost_vsock_free(vsock);
615 return 0;
616}
617
618static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
619{
620 struct vhost_vsock *other;
621
622 /* Refuse reserved CIDs */
623 if (guest_cid <= VMADDR_CID_HOST ||
624 guest_cid == U32_MAX)
625 return -EINVAL;
626
627 /* 64-bit CIDs are not yet supported */
628 if (guest_cid > U32_MAX)
629 return -EINVAL;
630
631 /* Refuse if CID is already in use */
Asias He433fc582016-07-28 15:36:34 +0100632 spin_lock_bh(&vhost_vsock_lock);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000633 other = vhost_vsock_get(guest_cid);
Gao feng2d5a1b32016-12-14 19:24:36 +0800634 if (other && other != vsock) {
635 spin_unlock_bh(&vhost_vsock_lock);
636 return -EADDRINUSE;
637 }
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000638
639 if (vsock->guest_cid)
640 hash_del_rcu(&vsock->hash);
641
Asias He433fc582016-07-28 15:36:34 +0100642 vsock->guest_cid = guest_cid;
Zha Bin5ebcee92019-01-08 16:07:03 +0800643 hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
Asias He433fc582016-07-28 15:36:34 +0100644 spin_unlock_bh(&vhost_vsock_lock);
645
646 return 0;
647}
648
649static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
650{
651 struct vhost_virtqueue *vq;
652 int i;
653
654 if (features & ~VHOST_VSOCK_FEATURES)
655 return -EOPNOTSUPP;
656
657 mutex_lock(&vsock->dev.mutex);
658 if ((features & (1 << VHOST_F_LOG_ALL)) &&
659 !vhost_log_access_ok(&vsock->dev)) {
660 mutex_unlock(&vsock->dev.mutex);
661 return -EFAULT;
662 }
663
664 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
665 vq = &vsock->vqs[i];
666 mutex_lock(&vq->mutex);
667 vq->acked_features = features;
668 mutex_unlock(&vq->mutex);
669 }
670 mutex_unlock(&vsock->dev.mutex);
671 return 0;
672}
673
674static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
675 unsigned long arg)
676{
677 struct vhost_vsock *vsock = f->private_data;
678 void __user *argp = (void __user *)arg;
679 u64 guest_cid;
680 u64 features;
681 int start;
682 int r;
683
684 switch (ioctl) {
685 case VHOST_VSOCK_SET_GUEST_CID:
686 if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
687 return -EFAULT;
688 return vhost_vsock_set_cid(vsock, guest_cid);
689 case VHOST_VSOCK_SET_RUNNING:
690 if (copy_from_user(&start, argp, sizeof(start)))
691 return -EFAULT;
692 if (start)
693 return vhost_vsock_start(vsock);
694 else
695 return vhost_vsock_stop(vsock);
696 case VHOST_GET_FEATURES:
697 features = VHOST_VSOCK_FEATURES;
698 if (copy_to_user(argp, &features, sizeof(features)))
699 return -EFAULT;
700 return 0;
701 case VHOST_SET_FEATURES:
702 if (copy_from_user(&features, argp, sizeof(features)))
703 return -EFAULT;
704 return vhost_vsock_set_features(vsock, features);
705 default:
706 mutex_lock(&vsock->dev.mutex);
707 r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
708 if (r == -ENOIOCTLCMD)
709 r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
710 else
711 vhost_vsock_flush(vsock);
712 mutex_unlock(&vsock->dev.mutex);
713 return r;
714 }
715}
716
717static const struct file_operations vhost_vsock_fops = {
718 .owner = THIS_MODULE,
719 .open = vhost_vsock_dev_open,
720 .release = vhost_vsock_dev_release,
721 .llseek = noop_llseek,
722 .unlocked_ioctl = vhost_vsock_dev_ioctl,
723};
724
725static struct miscdevice vhost_vsock_misc = {
726 .minor = MISC_DYNAMIC_MINOR,
727 .name = "vhost-vsock",
728 .fops = &vhost_vsock_fops,
729};
730
731static struct virtio_transport vhost_transport = {
732 .transport = {
733 .get_local_cid = vhost_transport_get_local_cid,
734
735 .init = virtio_transport_do_socket_init,
736 .destruct = virtio_transport_destruct,
737 .release = virtio_transport_release,
738 .connect = virtio_transport_connect,
739 .shutdown = virtio_transport_shutdown,
Peng Tao482b3f92017-03-15 09:32:15 +0800740 .cancel_pkt = vhost_transport_cancel_pkt,
Asias He433fc582016-07-28 15:36:34 +0100741
742 .dgram_enqueue = virtio_transport_dgram_enqueue,
743 .dgram_dequeue = virtio_transport_dgram_dequeue,
744 .dgram_bind = virtio_transport_dgram_bind,
745 .dgram_allow = virtio_transport_dgram_allow,
746
747 .stream_enqueue = virtio_transport_stream_enqueue,
748 .stream_dequeue = virtio_transport_stream_dequeue,
749 .stream_has_data = virtio_transport_stream_has_data,
750 .stream_has_space = virtio_transport_stream_has_space,
751 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
752 .stream_is_active = virtio_transport_stream_is_active,
753 .stream_allow = virtio_transport_stream_allow,
754
755 .notify_poll_in = virtio_transport_notify_poll_in,
756 .notify_poll_out = virtio_transport_notify_poll_out,
757 .notify_recv_init = virtio_transport_notify_recv_init,
758 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
759 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
760 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
761 .notify_send_init = virtio_transport_notify_send_init,
762 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
763 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
764 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
765
766 .set_buffer_size = virtio_transport_set_buffer_size,
767 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
768 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
769 .get_buffer_size = virtio_transport_get_buffer_size,
770 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
771 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
772 },
773
774 .send_pkt = vhost_transport_send_pkt,
775};
776
777static int __init vhost_vsock_init(void)
778{
779 int ret;
780
781 ret = vsock_core_init(&vhost_transport.transport);
782 if (ret < 0)
783 return ret;
784 return misc_register(&vhost_vsock_misc);
785};
786
787static void __exit vhost_vsock_exit(void)
788{
789 misc_deregister(&vhost_vsock_misc);
790 vsock_core_exit();
791};
792
793module_init(vhost_vsock_init);
794module_exit(vhost_vsock_exit);
795MODULE_LICENSE("GPL v2");
796MODULE_AUTHOR("Asias He");
797MODULE_DESCRIPTION("vhost transport for vsock ");