blob: 2e37097916b5b1df76a471b83446205083d62c85 [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
Jason Wang66c8d9d2019-08-17 00:00:44 +010024/* Max number of bytes transferred before requeueing the job.
25 * Using this limit prevents one virtqueue from starving others. */
26#define VHOST_VSOCK_WEIGHT 0x80000
27/* Max number of packets transferred before requeueing the job.
28 * Using this limit prevents one virtqueue from starving others with
29 * small pkts.
30 */
31#define VHOST_VSOCK_PKT_WEIGHT 256
Asias He433fc582016-07-28 15:36:34 +010032
33enum {
34 VHOST_VSOCK_FEATURES = VHOST_FEATURES,
35};
36
37/* Used to track all the vhost_vsock instances on the system. */
38static DEFINE_SPINLOCK(vhost_vsock_lock);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000039static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8);
Asias He433fc582016-07-28 15:36:34 +010040
41struct vhost_vsock {
42 struct vhost_dev dev;
43 struct vhost_virtqueue vqs[2];
44
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000045 /* Link to global vhost_vsock_hash, writes use vhost_vsock_lock */
46 struct hlist_node hash;
Asias He433fc582016-07-28 15:36:34 +010047
48 struct vhost_work send_pkt_work;
49 spinlock_t send_pkt_list_lock;
50 struct list_head send_pkt_list; /* host->guest pending packets */
51
52 atomic_t queued_replies;
53
54 u32 guest_cid;
55};
56
57static u32 vhost_transport_get_local_cid(void)
58{
59 return VHOST_VSOCK_DEFAULT_HOST_CID;
60}
61
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000062/* Callers that dereference the return value must hold vhost_vsock_lock or the
63 * RCU read lock.
64 */
65static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
Asias He433fc582016-07-28 15:36:34 +010066{
67 struct vhost_vsock *vsock;
68
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +000069 hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid) {
Asias He433fc582016-07-28 15:36:34 +010070 u32 other_cid = vsock->guest_cid;
71
72 /* Skip instances that have no CID yet */
73 if (other_cid == 0)
74 continue;
75
76 if (other_cid == guest_cid) {
Asias He433fc582016-07-28 15:36:34 +010077 return vsock;
78 }
79 }
Asias He433fc582016-07-28 15:36:34 +010080
81 return NULL;
82}
83
84static void
85vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
86 struct vhost_virtqueue *vq)
87{
88 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
89 bool added = false;
90 bool restart_tx = false;
91
92 mutex_lock(&vq->mutex);
93
94 if (!vq->private_data)
95 goto out;
96
97 /* Avoid further vmexits, we're already processing the virtqueue */
98 vhost_disable_notify(&vsock->dev, vq);
99
100 for (;;) {
101 struct virtio_vsock_pkt *pkt;
102 struct iov_iter iov_iter;
103 unsigned out, in;
104 size_t nbytes;
105 size_t len;
106 int head;
107
108 spin_lock_bh(&vsock->send_pkt_list_lock);
109 if (list_empty(&vsock->send_pkt_list)) {
110 spin_unlock_bh(&vsock->send_pkt_list_lock);
111 vhost_enable_notify(&vsock->dev, vq);
112 break;
113 }
114
115 pkt = list_first_entry(&vsock->send_pkt_list,
116 struct virtio_vsock_pkt, list);
117 list_del_init(&pkt->list);
118 spin_unlock_bh(&vsock->send_pkt_list_lock);
119
120 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
121 &out, &in, NULL, NULL);
122 if (head < 0) {
123 spin_lock_bh(&vsock->send_pkt_list_lock);
124 list_add(&pkt->list, &vsock->send_pkt_list);
125 spin_unlock_bh(&vsock->send_pkt_list_lock);
126 break;
127 }
128
129 if (head == vq->num) {
130 spin_lock_bh(&vsock->send_pkt_list_lock);
131 list_add(&pkt->list, &vsock->send_pkt_list);
132 spin_unlock_bh(&vsock->send_pkt_list_lock);
133
134 /* We cannot finish yet if more buffers snuck in while
135 * re-enabling notify.
136 */
137 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
138 vhost_disable_notify(&vsock->dev, vq);
139 continue;
140 }
141 break;
142 }
143
144 if (out) {
145 virtio_transport_free_pkt(pkt);
146 vq_err(vq, "Expected 0 output buffers, got %u\n", out);
147 break;
148 }
149
150 len = iov_length(&vq->iov[out], in);
151 iov_iter_init(&iov_iter, READ, &vq->iov[out], in, len);
152
153 nbytes = copy_to_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
154 if (nbytes != sizeof(pkt->hdr)) {
155 virtio_transport_free_pkt(pkt);
156 vq_err(vq, "Faulted on copying pkt hdr\n");
157 break;
158 }
159
160 nbytes = copy_to_iter(pkt->buf, pkt->len, &iov_iter);
161 if (nbytes != pkt->len) {
162 virtio_transport_free_pkt(pkt);
163 vq_err(vq, "Faulted on copying pkt buf\n");
164 break;
165 }
166
167 vhost_add_used(vq, head, sizeof(pkt->hdr) + pkt->len);
168 added = true;
169
170 if (pkt->reply) {
171 int val;
172
173 val = atomic_dec_return(&vsock->queued_replies);
174
175 /* Do we have resources to resume tx processing? */
176 if (val + 1 == tx_vq->num)
177 restart_tx = true;
178 }
179
180 virtio_transport_free_pkt(pkt);
181 }
182 if (added)
183 vhost_signal(&vsock->dev, vq);
184
185out:
186 mutex_unlock(&vq->mutex);
187
188 if (restart_tx)
189 vhost_poll_queue(&tx_vq->poll);
190}
191
192static void vhost_transport_send_pkt_work(struct vhost_work *work)
193{
194 struct vhost_virtqueue *vq;
195 struct vhost_vsock *vsock;
196
197 vsock = container_of(work, struct vhost_vsock, send_pkt_work);
198 vq = &vsock->vqs[VSOCK_VQ_RX];
199
200 vhost_transport_do_send_pkt(vsock, vq);
201}
202
203static int
204vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
205{
206 struct vhost_vsock *vsock;
207 struct vhost_virtqueue *vq;
208 int len = pkt->len;
209
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000210 rcu_read_lock();
211
Asias He433fc582016-07-28 15:36:34 +0100212 /* Find the vhost_vsock according to guest context id */
213 vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
214 if (!vsock) {
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000215 rcu_read_unlock();
Asias He433fc582016-07-28 15:36:34 +0100216 virtio_transport_free_pkt(pkt);
217 return -ENODEV;
218 }
219
220 vq = &vsock->vqs[VSOCK_VQ_RX];
221
222 if (pkt->reply)
223 atomic_inc(&vsock->queued_replies);
224
225 spin_lock_bh(&vsock->send_pkt_list_lock);
226 list_add_tail(&pkt->list, &vsock->send_pkt_list);
227 spin_unlock_bh(&vsock->send_pkt_list_lock);
228
229 vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000230
231 rcu_read_unlock();
Asias He433fc582016-07-28 15:36:34 +0100232 return len;
233}
234
Peng Tao482b3f92017-03-15 09:32:15 +0800235static int
236vhost_transport_cancel_pkt(struct vsock_sock *vsk)
237{
238 struct vhost_vsock *vsock;
239 struct virtio_vsock_pkt *pkt, *n;
240 int cnt = 0;
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000241 int ret = -ENODEV;
Peng Tao482b3f92017-03-15 09:32:15 +0800242 LIST_HEAD(freeme);
243
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000244 rcu_read_lock();
245
Peng Tao482b3f92017-03-15 09:32:15 +0800246 /* Find the vhost_vsock according to guest context id */
247 vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
248 if (!vsock)
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000249 goto out;
Peng Tao482b3f92017-03-15 09:32:15 +0800250
251 spin_lock_bh(&vsock->send_pkt_list_lock);
252 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
253 if (pkt->vsk != vsk)
254 continue;
255 list_move(&pkt->list, &freeme);
256 }
257 spin_unlock_bh(&vsock->send_pkt_list_lock);
258
259 list_for_each_entry_safe(pkt, n, &freeme, list) {
260 if (pkt->reply)
261 cnt++;
262 list_del(&pkt->list);
263 virtio_transport_free_pkt(pkt);
264 }
265
266 if (cnt) {
267 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
268 int new_cnt;
269
270 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
271 if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
272 vhost_poll_queue(&tx_vq->poll);
273 }
274
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000275 ret = 0;
276out:
277 rcu_read_unlock();
278 return ret;
Peng Tao482b3f92017-03-15 09:32:15 +0800279}
280
Asias He433fc582016-07-28 15:36:34 +0100281static struct virtio_vsock_pkt *
282vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
283 unsigned int out, unsigned int in)
284{
285 struct virtio_vsock_pkt *pkt;
286 struct iov_iter iov_iter;
287 size_t nbytes;
288 size_t len;
289
290 if (in != 0) {
291 vq_err(vq, "Expected 0 input buffers, got %u\n", in);
292 return NULL;
293 }
294
295 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
296 if (!pkt)
297 return NULL;
298
299 len = iov_length(vq->iov, out);
300 iov_iter_init(&iov_iter, WRITE, vq->iov, out, len);
301
302 nbytes = copy_from_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
303 if (nbytes != sizeof(pkt->hdr)) {
304 vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n",
305 sizeof(pkt->hdr), nbytes);
306 kfree(pkt);
307 return NULL;
308 }
309
310 if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
311 pkt->len = le32_to_cpu(pkt->hdr.len);
312
313 /* No payload */
314 if (!pkt->len)
315 return pkt;
316
317 /* The pkt is too big */
318 if (pkt->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
319 kfree(pkt);
320 return NULL;
321 }
322
323 pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
324 if (!pkt->buf) {
325 kfree(pkt);
326 return NULL;
327 }
328
329 nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
330 if (nbytes != pkt->len) {
331 vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
332 pkt->len, nbytes);
333 virtio_transport_free_pkt(pkt);
334 return NULL;
335 }
336
337 return pkt;
338}
339
340/* Is there space left for replies to rx packets? */
341static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
342{
343 struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX];
344 int val;
345
346 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
347 val = atomic_read(&vsock->queued_replies);
348
349 return val < vq->num;
350}
351
352static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
353{
354 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
355 poll.work);
356 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
357 dev);
358 struct virtio_vsock_pkt *pkt;
359 int head;
360 unsigned int out, in;
361 bool added = false;
362
363 mutex_lock(&vq->mutex);
364
365 if (!vq->private_data)
366 goto out;
367
368 vhost_disable_notify(&vsock->dev, vq);
369 for (;;) {
Stefan Hajnoczi3fda5d62016-08-04 14:52:53 +0100370 u32 len;
371
Asias He433fc582016-07-28 15:36:34 +0100372 if (!vhost_vsock_more_replies(vsock)) {
373 /* Stop tx until the device processes already
374 * pending replies. Leave tx virtqueue
375 * callbacks disabled.
376 */
377 goto no_more_replies;
378 }
379
380 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
381 &out, &in, NULL, NULL);
382 if (head < 0)
383 break;
384
385 if (head == vq->num) {
386 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
387 vhost_disable_notify(&vsock->dev, vq);
388 continue;
389 }
390 break;
391 }
392
393 pkt = vhost_vsock_alloc_pkt(vq, out, in);
394 if (!pkt) {
395 vq_err(vq, "Faulted on pkt\n");
396 continue;
397 }
398
Stefan Hajnoczi3fda5d62016-08-04 14:52:53 +0100399 len = pkt->len;
400
Asias He433fc582016-07-28 15:36:34 +0100401 /* Only accept correctly addressed packets */
Stefano Garzarella0a8f4212019-12-06 15:39:12 +0100402 if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid &&
403 le64_to_cpu(pkt->hdr.dst_cid) ==
404 vhost_transport_get_local_cid())
Asias He433fc582016-07-28 15:36:34 +0100405 virtio_transport_recv_pkt(pkt);
406 else
407 virtio_transport_free_pkt(pkt);
408
Stefan Hajnoczi3fda5d62016-08-04 14:52:53 +0100409 vhost_add_used(vq, head, sizeof(pkt->hdr) + len);
Asias He433fc582016-07-28 15:36:34 +0100410 added = true;
411 }
412
413no_more_replies:
414 if (added)
415 vhost_signal(&vsock->dev, vq);
416
417out:
418 mutex_unlock(&vq->mutex);
419}
420
421static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
422{
423 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
424 poll.work);
425 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
426 dev);
427
428 vhost_transport_do_send_pkt(vsock, vq);
429}
430
431static int vhost_vsock_start(struct vhost_vsock *vsock)
432{
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000433 struct vhost_virtqueue *vq;
Asias He433fc582016-07-28 15:36:34 +0100434 size_t i;
435 int ret;
436
437 mutex_lock(&vsock->dev.mutex);
438
439 ret = vhost_dev_check_owner(&vsock->dev);
440 if (ret)
441 goto err;
442
443 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000444 vq = &vsock->vqs[i];
Asias He433fc582016-07-28 15:36:34 +0100445
446 mutex_lock(&vq->mutex);
447
448 if (!vhost_vq_access_ok(vq)) {
449 ret = -EFAULT;
Asias He433fc582016-07-28 15:36:34 +0100450 goto err_vq;
451 }
452
453 if (!vq->private_data) {
454 vq->private_data = vsock;
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000455 ret = vhost_vq_init_access(vq);
456 if (ret)
457 goto err_vq;
Asias He433fc582016-07-28 15:36:34 +0100458 }
459
460 mutex_unlock(&vq->mutex);
461 }
462
463 mutex_unlock(&vsock->dev.mutex);
464 return 0;
465
466err_vq:
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000467 vq->private_data = NULL;
468 mutex_unlock(&vq->mutex);
469
Asias He433fc582016-07-28 15:36:34 +0100470 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
Stefan Hajnocziae36f6a2017-01-19 10:43:53 +0000471 vq = &vsock->vqs[i];
Asias He433fc582016-07-28 15:36:34 +0100472
473 mutex_lock(&vq->mutex);
474 vq->private_data = NULL;
475 mutex_unlock(&vq->mutex);
476 }
477err:
478 mutex_unlock(&vsock->dev.mutex);
479 return ret;
480}
481
482static int vhost_vsock_stop(struct vhost_vsock *vsock)
483{
484 size_t i;
485 int ret;
486
487 mutex_lock(&vsock->dev.mutex);
488
489 ret = vhost_dev_check_owner(&vsock->dev);
490 if (ret)
491 goto err;
492
493 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
494 struct vhost_virtqueue *vq = &vsock->vqs[i];
495
496 mutex_lock(&vq->mutex);
497 vq->private_data = NULL;
498 mutex_unlock(&vq->mutex);
499 }
500
501err:
502 mutex_unlock(&vsock->dev.mutex);
503 return ret;
504}
505
506static void vhost_vsock_free(struct vhost_vsock *vsock)
507{
Wei Yongjunb226aca2016-08-02 13:50:42 +0000508 kvfree(vsock);
Asias He433fc582016-07-28 15:36:34 +0100509}
510
511static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
512{
513 struct vhost_virtqueue **vqs;
514 struct vhost_vsock *vsock;
515 int ret;
516
517 /* This struct is large and allocation could fail, fall back to vmalloc
518 * if there is no other way.
519 */
520 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
521 if (!vsock) {
522 vsock = vmalloc(sizeof(*vsock));
523 if (!vsock)
524 return -ENOMEM;
525 }
526
527 vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
528 if (!vqs) {
529 ret = -ENOMEM;
530 goto out;
531 }
532
Stefan Hajnoczi258d8542017-11-09 13:29:10 +0000533 vsock->guest_cid = 0; /* no CID assigned yet */
534
Asias He433fc582016-07-28 15:36:34 +0100535 atomic_set(&vsock->queued_replies, 0);
536
537 vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
538 vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
539 vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
540 vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
541
Jason Wang66c8d9d2019-08-17 00:00:44 +0100542 vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs),
543 VHOST_VSOCK_PKT_WEIGHT,
544 VHOST_VSOCK_WEIGHT);
Asias He433fc582016-07-28 15:36:34 +0100545
546 file->private_data = vsock;
547 spin_lock_init(&vsock->send_pkt_list_lock);
548 INIT_LIST_HEAD(&vsock->send_pkt_list);
549 vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
Asias He433fc582016-07-28 15:36:34 +0100550 return 0;
551
552out:
553 vhost_vsock_free(vsock);
554 return ret;
555}
556
557static void vhost_vsock_flush(struct vhost_vsock *vsock)
558{
559 int i;
560
561 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
562 if (vsock->vqs[i].handle_kick)
563 vhost_poll_flush(&vsock->vqs[i].poll);
564 vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
565}
566
567static void vhost_vsock_reset_orphans(struct sock *sk)
568{
569 struct vsock_sock *vsk = vsock_sk(sk);
570
571 /* vmci_transport.c doesn't take sk_lock here either. At least we're
572 * under vsock_table_lock so the sock cannot disappear while we're
573 * executing.
574 */
575
Stefan Hajnoczi06ec6672018-12-06 19:14:34 +0000576 /* If the peer is still valid, no need to reset connection */
577 if (vhost_vsock_get(vsk->remote_addr.svm_cid))
578 return;
579
580 /* If the close timeout is pending, let it expire. This avoids races
581 * with the timeout callback.
582 */
583 if (vsk->close_work_scheduled)
584 return;
585
586 sock_set_flag(sk, SOCK_DONE);
587 vsk->peer_shutdown = SHUTDOWN_MASK;
588 sk->sk_state = SS_UNCONNECTED;
589 sk->sk_err = ECONNRESET;
590 sk->sk_error_report(sk);
Asias He433fc582016-07-28 15:36:34 +0100591}
592
593static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
594{
595 struct vhost_vsock *vsock = file->private_data;
596
597 spin_lock_bh(&vhost_vsock_lock);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000598 if (vsock->guest_cid)
599 hash_del_rcu(&vsock->hash);
Asias He433fc582016-07-28 15:36:34 +0100600 spin_unlock_bh(&vhost_vsock_lock);
601
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000602 /* Wait for other CPUs to finish using vsock */
603 synchronize_rcu();
604
Asias He433fc582016-07-28 15:36:34 +0100605 /* Iterating over all connections for all CIDs to find orphans is
606 * inefficient. Room for improvement here. */
607 vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
608
609 vhost_vsock_stop(vsock);
610 vhost_vsock_flush(vsock);
611 vhost_dev_stop(&vsock->dev);
612
613 spin_lock_bh(&vsock->send_pkt_list_lock);
614 while (!list_empty(&vsock->send_pkt_list)) {
615 struct virtio_vsock_pkt *pkt;
616
617 pkt = list_first_entry(&vsock->send_pkt_list,
618 struct virtio_vsock_pkt, list);
619 list_del_init(&pkt->list);
620 virtio_transport_free_pkt(pkt);
621 }
622 spin_unlock_bh(&vsock->send_pkt_list_lock);
623
624 vhost_dev_cleanup(&vsock->dev, false);
625 kfree(vsock->dev.vqs);
626 vhost_vsock_free(vsock);
627 return 0;
628}
629
630static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
631{
632 struct vhost_vsock *other;
633
634 /* Refuse reserved CIDs */
635 if (guest_cid <= VMADDR_CID_HOST ||
636 guest_cid == U32_MAX)
637 return -EINVAL;
638
639 /* 64-bit CIDs are not yet supported */
640 if (guest_cid > U32_MAX)
641 return -EINVAL;
642
643 /* Refuse if CID is already in use */
Asias He433fc582016-07-28 15:36:34 +0100644 spin_lock_bh(&vhost_vsock_lock);
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000645 other = vhost_vsock_get(guest_cid);
Gao feng2d5a1b32016-12-14 19:24:36 +0800646 if (other && other != vsock) {
647 spin_unlock_bh(&vhost_vsock_lock);
648 return -EADDRINUSE;
649 }
Stefan Hajnoczi569fc4f2018-11-05 10:35:47 +0000650
651 if (vsock->guest_cid)
652 hash_del_rcu(&vsock->hash);
653
Asias He433fc582016-07-28 15:36:34 +0100654 vsock->guest_cid = guest_cid;
Zha Bin5ebcee92019-01-08 16:07:03 +0800655 hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
Asias He433fc582016-07-28 15:36:34 +0100656 spin_unlock_bh(&vhost_vsock_lock);
657
658 return 0;
659}
660
661static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
662{
663 struct vhost_virtqueue *vq;
664 int i;
665
666 if (features & ~VHOST_VSOCK_FEATURES)
667 return -EOPNOTSUPP;
668
669 mutex_lock(&vsock->dev.mutex);
670 if ((features & (1 << VHOST_F_LOG_ALL)) &&
671 !vhost_log_access_ok(&vsock->dev)) {
672 mutex_unlock(&vsock->dev.mutex);
673 return -EFAULT;
674 }
675
676 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
677 vq = &vsock->vqs[i];
678 mutex_lock(&vq->mutex);
679 vq->acked_features = features;
680 mutex_unlock(&vq->mutex);
681 }
682 mutex_unlock(&vsock->dev.mutex);
683 return 0;
684}
685
686static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
687 unsigned long arg)
688{
689 struct vhost_vsock *vsock = f->private_data;
690 void __user *argp = (void __user *)arg;
691 u64 guest_cid;
692 u64 features;
693 int start;
694 int r;
695
696 switch (ioctl) {
697 case VHOST_VSOCK_SET_GUEST_CID:
698 if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
699 return -EFAULT;
700 return vhost_vsock_set_cid(vsock, guest_cid);
701 case VHOST_VSOCK_SET_RUNNING:
702 if (copy_from_user(&start, argp, sizeof(start)))
703 return -EFAULT;
704 if (start)
705 return vhost_vsock_start(vsock);
706 else
707 return vhost_vsock_stop(vsock);
708 case VHOST_GET_FEATURES:
709 features = VHOST_VSOCK_FEATURES;
710 if (copy_to_user(argp, &features, sizeof(features)))
711 return -EFAULT;
712 return 0;
713 case VHOST_SET_FEATURES:
714 if (copy_from_user(&features, argp, sizeof(features)))
715 return -EFAULT;
716 return vhost_vsock_set_features(vsock, features);
717 default:
718 mutex_lock(&vsock->dev.mutex);
719 r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
720 if (r == -ENOIOCTLCMD)
721 r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
722 else
723 vhost_vsock_flush(vsock);
724 mutex_unlock(&vsock->dev.mutex);
725 return r;
726 }
727}
728
729static const struct file_operations vhost_vsock_fops = {
730 .owner = THIS_MODULE,
731 .open = vhost_vsock_dev_open,
732 .release = vhost_vsock_dev_release,
733 .llseek = noop_llseek,
734 .unlocked_ioctl = vhost_vsock_dev_ioctl,
735};
736
737static struct miscdevice vhost_vsock_misc = {
738 .minor = MISC_DYNAMIC_MINOR,
739 .name = "vhost-vsock",
740 .fops = &vhost_vsock_fops,
741};
742
743static struct virtio_transport vhost_transport = {
744 .transport = {
745 .get_local_cid = vhost_transport_get_local_cid,
746
747 .init = virtio_transport_do_socket_init,
748 .destruct = virtio_transport_destruct,
749 .release = virtio_transport_release,
750 .connect = virtio_transport_connect,
751 .shutdown = virtio_transport_shutdown,
Peng Tao482b3f92017-03-15 09:32:15 +0800752 .cancel_pkt = vhost_transport_cancel_pkt,
Asias He433fc582016-07-28 15:36:34 +0100753
754 .dgram_enqueue = virtio_transport_dgram_enqueue,
755 .dgram_dequeue = virtio_transport_dgram_dequeue,
756 .dgram_bind = virtio_transport_dgram_bind,
757 .dgram_allow = virtio_transport_dgram_allow,
758
759 .stream_enqueue = virtio_transport_stream_enqueue,
760 .stream_dequeue = virtio_transport_stream_dequeue,
761 .stream_has_data = virtio_transport_stream_has_data,
762 .stream_has_space = virtio_transport_stream_has_space,
763 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
764 .stream_is_active = virtio_transport_stream_is_active,
765 .stream_allow = virtio_transport_stream_allow,
766
767 .notify_poll_in = virtio_transport_notify_poll_in,
768 .notify_poll_out = virtio_transport_notify_poll_out,
769 .notify_recv_init = virtio_transport_notify_recv_init,
770 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
771 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
772 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
773 .notify_send_init = virtio_transport_notify_send_init,
774 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
775 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
776 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
777
778 .set_buffer_size = virtio_transport_set_buffer_size,
779 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
780 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
781 .get_buffer_size = virtio_transport_get_buffer_size,
782 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
783 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
784 },
785
786 .send_pkt = vhost_transport_send_pkt,
787};
788
789static int __init vhost_vsock_init(void)
790{
791 int ret;
792
793 ret = vsock_core_init(&vhost_transport.transport);
794 if (ret < 0)
795 return ret;
796 return misc_register(&vhost_vsock_misc);
797};
798
799static void __exit vhost_vsock_exit(void)
800{
801 misc_deregister(&vhost_vsock_misc);
802 vsock_core_exit();
803};
804
805module_init(vhost_vsock_init);
806module_exit(vhost_vsock_exit);
807MODULE_LICENSE("GPL v2");
808MODULE_AUTHOR("Asias He");
809MODULE_DESCRIPTION("vhost transport for vsock ");