1 /* 2 * vhost-backend 3 * 4 * Copyright (c) 2013 Virtual Open Systems Sarl. 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 * 9 */ 10 11 #include "hw/virtio/vhost.h" 12 #include "hw/virtio/vhost-backend.h" 13 #include "qemu/error-report.h" 14 15 #include <sys/ioctl.h> 16 17 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request, 18 void *arg) 19 { 20 int fd = (uintptr_t) dev->opaque; 21 22 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); 23 24 return ioctl(fd, request, arg); 25 } 26 27 static int vhost_kernel_init(struct vhost_dev *dev, void *opaque) 28 { 29 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); 30 31 dev->opaque = opaque; 32 33 return 0; 34 } 35 36 static int vhost_kernel_cleanup(struct vhost_dev *dev) 37 { 38 int fd = (uintptr_t) dev->opaque; 39 40 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); 41 42 return close(fd); 43 } 44 45 static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx) 46 { 47 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); 48 49 return idx - dev->vq_index; 50 } 51 52 static const VhostOps kernel_ops = { 53 .backend_type = VHOST_BACKEND_TYPE_KERNEL, 54 .vhost_call = vhost_kernel_call, 55 .vhost_backend_init = vhost_kernel_init, 56 .vhost_backend_cleanup = vhost_kernel_cleanup, 57 .vhost_backend_get_vq_index = vhost_kernel_get_vq_index, 58 }; 59 60 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type) 61 { 62 int r = 0; 63 64 switch (backend_type) { 65 case VHOST_BACKEND_TYPE_KERNEL: 66 dev->vhost_ops = &kernel_ops; 67 break; 68 case VHOST_BACKEND_TYPE_USER: 69 dev->vhost_ops = &user_ops; 70 break; 71 default: 72 error_report("Unknown vhost backend type"); 73 r = -1; 74 } 75 76 return r; 77 } 78