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 extern const VhostOps user_ops; 18 19 static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request, 20 void *arg) 21 { 22 int fd = (uintptr_t) dev->opaque; 23 24 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); 25 26 return ioctl(fd, request, arg); 27 } 28 29 static int vhost_kernel_init(struct vhost_dev *dev, void *opaque) 30 { 31 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); 32 33 dev->opaque = opaque; 34 35 return 0; 36 } 37 38 static int vhost_kernel_cleanup(struct vhost_dev *dev) 39 { 40 int fd = (uintptr_t) dev->opaque; 41 42 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL); 43 44 return close(fd); 45 } 46 47 static const VhostOps kernel_ops = { 48 .backend_type = VHOST_BACKEND_TYPE_KERNEL, 49 .vhost_call = vhost_kernel_call, 50 .vhost_backend_init = vhost_kernel_init, 51 .vhost_backend_cleanup = vhost_kernel_cleanup 52 }; 53 54 int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type) 55 { 56 int r = 0; 57 58 switch (backend_type) { 59 case VHOST_BACKEND_TYPE_KERNEL: 60 dev->vhost_ops = &kernel_ops; 61 break; 62 case VHOST_BACKEND_TYPE_USER: 63 dev->vhost_ops = &user_ops; 64 break; 65 default: 66 error_report("Unknown vhost backend type\n"); 67 r = -1; 68 } 69 70 return r; 71 } 72