vhost-backend.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #include "qemu/osdep.h"
  11. #include "hw/virtio/vhost.h"
  12. #include "hw/virtio/vhost-backend.h"
  13. #include "qemu/error-report.h"
  14. #include "qemu/main-loop.h"
  15. #include "standard-headers/linux/vhost_types.h"
  16. #include "hw/virtio/vhost-vdpa.h"
  17. #ifdef CONFIG_VHOST_KERNEL
  18. #include <linux/vhost.h>
  19. #include <sys/ioctl.h>
  20. static int vhost_kernel_call(struct vhost_dev *dev, unsigned long int request,
  21. void *arg)
  22. {
  23. int fd = (uintptr_t) dev->opaque;
  24. assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
  25. return ioctl(fd, request, arg);
  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. dev->opaque = opaque;
  31. return 0;
  32. }
  33. static int vhost_kernel_cleanup(struct vhost_dev *dev)
  34. {
  35. int fd = (uintptr_t) dev->opaque;
  36. assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_KERNEL);
  37. return close(fd);
  38. }
  39. static int vhost_kernel_memslots_limit(struct vhost_dev *dev)
  40. {
  41. int limit = 64;
  42. char *s;
  43. if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions",
  44. &s, NULL, NULL)) {
  45. uint64_t val = g_ascii_strtoull(s, NULL, 10);
  46. if (!((val == G_MAXUINT64 || !val) && errno)) {
  47. g_free(s);
  48. return val;
  49. }
  50. error_report("ignoring invalid max_mem_regions value in vhost module:"
  51. " %s", s);
  52. }
  53. g_free(s);
  54. return limit;
  55. }
  56. static int vhost_kernel_net_set_backend(struct vhost_dev *dev,
  57. struct vhost_vring_file *file)
  58. {
  59. return vhost_kernel_call(dev, VHOST_NET_SET_BACKEND, file);
  60. }
  61. static int vhost_kernel_scsi_set_endpoint(struct vhost_dev *dev,
  62. struct vhost_scsi_target *target)
  63. {
  64. return vhost_kernel_call(dev, VHOST_SCSI_SET_ENDPOINT, target);
  65. }
  66. static int vhost_kernel_scsi_clear_endpoint(struct vhost_dev *dev,
  67. struct vhost_scsi_target *target)
  68. {
  69. return vhost_kernel_call(dev, VHOST_SCSI_CLEAR_ENDPOINT, target);
  70. }
  71. static int vhost_kernel_scsi_get_abi_version(struct vhost_dev *dev, int *version)
  72. {
  73. return vhost_kernel_call(dev, VHOST_SCSI_GET_ABI_VERSION, version);
  74. }
  75. static int vhost_kernel_set_log_base(struct vhost_dev *dev, uint64_t base,
  76. struct vhost_log *log)
  77. {
  78. return vhost_kernel_call(dev, VHOST_SET_LOG_BASE, &base);
  79. }
  80. static int vhost_kernel_set_mem_table(struct vhost_dev *dev,
  81. struct vhost_memory *mem)
  82. {
  83. return vhost_kernel_call(dev, VHOST_SET_MEM_TABLE, mem);
  84. }
  85. static int vhost_kernel_set_vring_addr(struct vhost_dev *dev,
  86. struct vhost_vring_addr *addr)
  87. {
  88. return vhost_kernel_call(dev, VHOST_SET_VRING_ADDR, addr);
  89. }
  90. static int vhost_kernel_set_vring_endian(struct vhost_dev *dev,
  91. struct vhost_vring_state *ring)
  92. {
  93. return vhost_kernel_call(dev, VHOST_SET_VRING_ENDIAN, ring);
  94. }
  95. static int vhost_kernel_set_vring_num(struct vhost_dev *dev,
  96. struct vhost_vring_state *ring)
  97. {
  98. return vhost_kernel_call(dev, VHOST_SET_VRING_NUM, ring);
  99. }
  100. static int vhost_kernel_set_vring_base(struct vhost_dev *dev,
  101. struct vhost_vring_state *ring)
  102. {
  103. return vhost_kernel_call(dev, VHOST_SET_VRING_BASE, ring);
  104. }
  105. static int vhost_kernel_get_vring_base(struct vhost_dev *dev,
  106. struct vhost_vring_state *ring)
  107. {
  108. return vhost_kernel_call(dev, VHOST_GET_VRING_BASE, ring);
  109. }
  110. static int vhost_kernel_set_vring_kick(struct vhost_dev *dev,
  111. struct vhost_vring_file *file)
  112. {
  113. return vhost_kernel_call(dev, VHOST_SET_VRING_KICK, file);
  114. }
  115. static int vhost_kernel_set_vring_call(struct vhost_dev *dev,
  116. struct vhost_vring_file *file)
  117. {
  118. return vhost_kernel_call(dev, VHOST_SET_VRING_CALL, file);
  119. }
  120. static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev *dev,
  121. struct vhost_vring_state *s)
  122. {
  123. return vhost_kernel_call(dev, VHOST_SET_VRING_BUSYLOOP_TIMEOUT, s);
  124. }
  125. static int vhost_kernel_set_features(struct vhost_dev *dev,
  126. uint64_t features)
  127. {
  128. return vhost_kernel_call(dev, VHOST_SET_FEATURES, &features);
  129. }
  130. static int vhost_kernel_get_features(struct vhost_dev *dev,
  131. uint64_t *features)
  132. {
  133. return vhost_kernel_call(dev, VHOST_GET_FEATURES, features);
  134. }
  135. static int vhost_kernel_set_owner(struct vhost_dev *dev)
  136. {
  137. return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
  138. }
  139. static int vhost_kernel_reset_device(struct vhost_dev *dev)
  140. {
  141. return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
  142. }
  143. static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx)
  144. {
  145. assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
  146. return idx - dev->vq_index;
  147. }
  148. #ifdef CONFIG_VHOST_VSOCK
  149. static int vhost_kernel_vsock_set_guest_cid(struct vhost_dev *dev,
  150. uint64_t guest_cid)
  151. {
  152. return vhost_kernel_call(dev, VHOST_VSOCK_SET_GUEST_CID, &guest_cid);
  153. }
  154. static int vhost_kernel_vsock_set_running(struct vhost_dev *dev, int start)
  155. {
  156. return vhost_kernel_call(dev, VHOST_VSOCK_SET_RUNNING, &start);
  157. }
  158. #endif /* CONFIG_VHOST_VSOCK */
  159. static void vhost_kernel_iotlb_read(void *opaque)
  160. {
  161. struct vhost_dev *dev = opaque;
  162. struct vhost_msg msg;
  163. ssize_t len;
  164. while ((len = read((uintptr_t)dev->opaque, &msg, sizeof msg)) > 0) {
  165. if (len < sizeof msg) {
  166. error_report("Wrong vhost message len: %d", (int)len);
  167. break;
  168. }
  169. if (msg.type != VHOST_IOTLB_MSG) {
  170. error_report("Unknown vhost iotlb message type");
  171. break;
  172. }
  173. vhost_backend_handle_iotlb_msg(dev, &msg.iotlb);
  174. }
  175. }
  176. static int vhost_kernel_send_device_iotlb_msg(struct vhost_dev *dev,
  177. struct vhost_iotlb_msg *imsg)
  178. {
  179. struct vhost_msg msg;
  180. msg.type = VHOST_IOTLB_MSG;
  181. msg.iotlb = *imsg;
  182. if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) {
  183. error_report("Fail to update device iotlb");
  184. return -EFAULT;
  185. }
  186. return 0;
  187. }
  188. static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
  189. int enabled)
  190. {
  191. if (enabled)
  192. qemu_set_fd_handler((uintptr_t)dev->opaque,
  193. vhost_kernel_iotlb_read, NULL, dev);
  194. else
  195. qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
  196. }
  197. static const VhostOps kernel_ops = {
  198. .backend_type = VHOST_BACKEND_TYPE_KERNEL,
  199. .vhost_backend_init = vhost_kernel_init,
  200. .vhost_backend_cleanup = vhost_kernel_cleanup,
  201. .vhost_backend_memslots_limit = vhost_kernel_memslots_limit,
  202. .vhost_net_set_backend = vhost_kernel_net_set_backend,
  203. .vhost_scsi_set_endpoint = vhost_kernel_scsi_set_endpoint,
  204. .vhost_scsi_clear_endpoint = vhost_kernel_scsi_clear_endpoint,
  205. .vhost_scsi_get_abi_version = vhost_kernel_scsi_get_abi_version,
  206. .vhost_set_log_base = vhost_kernel_set_log_base,
  207. .vhost_set_mem_table = vhost_kernel_set_mem_table,
  208. .vhost_set_vring_addr = vhost_kernel_set_vring_addr,
  209. .vhost_set_vring_endian = vhost_kernel_set_vring_endian,
  210. .vhost_set_vring_num = vhost_kernel_set_vring_num,
  211. .vhost_set_vring_base = vhost_kernel_set_vring_base,
  212. .vhost_get_vring_base = vhost_kernel_get_vring_base,
  213. .vhost_set_vring_kick = vhost_kernel_set_vring_kick,
  214. .vhost_set_vring_call = vhost_kernel_set_vring_call,
  215. .vhost_set_vring_busyloop_timeout =
  216. vhost_kernel_set_vring_busyloop_timeout,
  217. .vhost_set_features = vhost_kernel_set_features,
  218. .vhost_get_features = vhost_kernel_get_features,
  219. .vhost_set_owner = vhost_kernel_set_owner,
  220. .vhost_reset_device = vhost_kernel_reset_device,
  221. .vhost_get_vq_index = vhost_kernel_get_vq_index,
  222. #ifdef CONFIG_VHOST_VSOCK
  223. .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
  224. .vhost_vsock_set_running = vhost_kernel_vsock_set_running,
  225. #endif /* CONFIG_VHOST_VSOCK */
  226. .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
  227. .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg,
  228. };
  229. #endif
  230. int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)
  231. {
  232. int r = 0;
  233. switch (backend_type) {
  234. #ifdef CONFIG_VHOST_KERNEL
  235. case VHOST_BACKEND_TYPE_KERNEL:
  236. dev->vhost_ops = &kernel_ops;
  237. break;
  238. #endif
  239. #ifdef CONFIG_VHOST_USER
  240. case VHOST_BACKEND_TYPE_USER:
  241. dev->vhost_ops = &user_ops;
  242. break;
  243. #endif
  244. #ifdef CONFIG_VHOST_VDPA
  245. case VHOST_BACKEND_TYPE_VDPA:
  246. dev->vhost_ops = &vdpa_ops;
  247. break;
  248. #endif
  249. default:
  250. error_report("Unknown vhost backend type");
  251. r = -1;
  252. }
  253. return r;
  254. }
  255. int vhost_backend_update_device_iotlb(struct vhost_dev *dev,
  256. uint64_t iova, uint64_t uaddr,
  257. uint64_t len,
  258. IOMMUAccessFlags perm)
  259. {
  260. struct vhost_iotlb_msg imsg;
  261. imsg.iova = iova;
  262. imsg.uaddr = uaddr;
  263. imsg.size = len;
  264. imsg.type = VHOST_IOTLB_UPDATE;
  265. switch (perm) {
  266. case IOMMU_RO:
  267. imsg.perm = VHOST_ACCESS_RO;
  268. break;
  269. case IOMMU_WO:
  270. imsg.perm = VHOST_ACCESS_WO;
  271. break;
  272. case IOMMU_RW:
  273. imsg.perm = VHOST_ACCESS_RW;
  274. break;
  275. default:
  276. return -EINVAL;
  277. }
  278. if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
  279. return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
  280. return -ENODEV;
  281. }
  282. int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev,
  283. uint64_t iova, uint64_t len)
  284. {
  285. struct vhost_iotlb_msg imsg;
  286. imsg.iova = iova;
  287. imsg.size = len;
  288. imsg.type = VHOST_IOTLB_INVALIDATE;
  289. if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg)
  290. return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg);
  291. return -ENODEV;
  292. }
  293. int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev,
  294. struct vhost_iotlb_msg *imsg)
  295. {
  296. int ret = 0;
  297. switch (imsg->type) {
  298. case VHOST_IOTLB_MISS:
  299. ret = vhost_device_iotlb_miss(dev, imsg->iova,
  300. imsg->perm != VHOST_ACCESS_RO);
  301. break;
  302. case VHOST_IOTLB_ACCESS_FAIL:
  303. /* FIXME: report device iotlb error */
  304. error_report("Access failure IOTLB message type not supported");
  305. ret = -ENOTSUP;
  306. break;
  307. case VHOST_IOTLB_UPDATE:
  308. case VHOST_IOTLB_INVALIDATE:
  309. default:
  310. error_report("Unexpected IOTLB message type");
  311. ret = -EINVAL;
  312. break;
  313. }
  314. return ret;
  315. }