test.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2009 Red Hat, Inc.
  3. * Author: Michael S. Tsirkin <mst@redhat.com>
  4. *
  5. * test virtio server in host kernel.
  6. */
  7. #include <linux/compat.h>
  8. #include <linux/eventfd.h>
  9. #include <linux/vhost.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/file.h>
  15. #include <linux/slab.h>
  16. #include "test.h"
  17. #include "vhost.h"
  18. /* Max number of bytes transferred before requeueing the job.
  19. * Using this limit prevents one virtqueue from starving others. */
  20. #define VHOST_TEST_WEIGHT 0x80000
  21. /* Max number of packets transferred before requeueing the job.
  22. * Using this limit prevents one virtqueue from starving others with
  23. * pkts.
  24. */
  25. #define VHOST_TEST_PKT_WEIGHT 256
  26. static const int vhost_test_bits[] = {
  27. VHOST_FEATURES
  28. };
  29. #define VHOST_TEST_FEATURES VHOST_FEATURES_U64(vhost_test_bits, 0)
  30. enum {
  31. VHOST_TEST_VQ = 0,
  32. VHOST_TEST_VQ_MAX = 1,
  33. };
  34. struct vhost_test {
  35. struct vhost_dev dev;
  36. struct vhost_virtqueue vqs[VHOST_TEST_VQ_MAX];
  37. };
  38. /* Expects to be always run from workqueue - which acts as
  39. * read-size critical section for our kind of RCU. */
  40. static void handle_vq(struct vhost_test *n)
  41. {
  42. struct vhost_virtqueue *vq = &n->vqs[VHOST_TEST_VQ];
  43. unsigned out, in;
  44. int head;
  45. size_t len, total_len = 0;
  46. void *private;
  47. mutex_lock(&vq->mutex);
  48. private = vhost_vq_get_backend(vq);
  49. if (!private) {
  50. mutex_unlock(&vq->mutex);
  51. return;
  52. }
  53. vhost_disable_notify(&n->dev, vq);
  54. for (;;) {
  55. head = vhost_get_vq_desc(vq, vq->iov,
  56. ARRAY_SIZE(vq->iov),
  57. &out, &in,
  58. NULL, NULL);
  59. /* On error, stop handling until the next kick. */
  60. if (unlikely(head < 0))
  61. break;
  62. /* Nothing new? Wait for eventfd to tell us they refilled. */
  63. if (head == vq->num) {
  64. if (unlikely(vhost_enable_notify(&n->dev, vq))) {
  65. vhost_disable_notify(&n->dev, vq);
  66. continue;
  67. }
  68. break;
  69. }
  70. if (in) {
  71. vq_err(vq, "Unexpected descriptor format for TX: "
  72. "out %d, int %d\n", out, in);
  73. break;
  74. }
  75. len = iov_length(vq->iov, out);
  76. /* Sanity check */
  77. if (!len) {
  78. vq_err(vq, "Unexpected 0 len for TX\n");
  79. break;
  80. }
  81. vhost_add_used_and_signal(&n->dev, vq, head, 0);
  82. total_len += len;
  83. if (unlikely(vhost_exceeds_weight(vq, 0, total_len)))
  84. break;
  85. }
  86. mutex_unlock(&vq->mutex);
  87. }
  88. static void handle_vq_kick(struct vhost_work *work)
  89. {
  90. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  91. poll.work);
  92. struct vhost_test *n = container_of(vq->dev, struct vhost_test, dev);
  93. handle_vq(n);
  94. }
  95. static int vhost_test_open(struct inode *inode, struct file *f)
  96. {
  97. struct vhost_test *n = kmalloc_obj(*n);
  98. struct vhost_dev *dev;
  99. struct vhost_virtqueue **vqs;
  100. if (!n)
  101. return -ENOMEM;
  102. vqs = kmalloc_objs(*vqs, VHOST_TEST_VQ_MAX);
  103. if (!vqs) {
  104. kfree(n);
  105. return -ENOMEM;
  106. }
  107. dev = &n->dev;
  108. vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
  109. n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
  110. vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
  111. VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT, true, NULL);
  112. f->private_data = n;
  113. return 0;
  114. }
  115. static void *vhost_test_stop_vq(struct vhost_test *n,
  116. struct vhost_virtqueue *vq)
  117. {
  118. void *private;
  119. mutex_lock(&vq->mutex);
  120. private = vhost_vq_get_backend(vq);
  121. vhost_vq_set_backend(vq, NULL);
  122. mutex_unlock(&vq->mutex);
  123. return private;
  124. }
  125. static void vhost_test_stop(struct vhost_test *n, void **privatep)
  126. {
  127. *privatep = vhost_test_stop_vq(n, n->vqs + VHOST_TEST_VQ);
  128. }
  129. static void vhost_test_flush(struct vhost_test *n)
  130. {
  131. vhost_dev_flush(&n->dev);
  132. }
  133. static int vhost_test_release(struct inode *inode, struct file *f)
  134. {
  135. struct vhost_test *n = f->private_data;
  136. void *private;
  137. vhost_test_stop(n, &private);
  138. vhost_test_flush(n);
  139. vhost_dev_stop(&n->dev);
  140. vhost_dev_cleanup(&n->dev);
  141. kfree(n->dev.vqs);
  142. kfree(n);
  143. return 0;
  144. }
  145. static long vhost_test_run(struct vhost_test *n, int test)
  146. {
  147. void *priv, *oldpriv;
  148. struct vhost_virtqueue *vq;
  149. int r, index;
  150. if (test < 0 || test > 1)
  151. return -EINVAL;
  152. mutex_lock(&n->dev.mutex);
  153. r = vhost_dev_check_owner(&n->dev);
  154. if (r)
  155. goto err;
  156. for (index = 0; index < n->dev.nvqs; ++index) {
  157. /* Verify that ring has been setup correctly. */
  158. if (!vhost_vq_access_ok(&n->vqs[index])) {
  159. r = -EFAULT;
  160. goto err;
  161. }
  162. }
  163. for (index = 0; index < n->dev.nvqs; ++index) {
  164. vq = n->vqs + index;
  165. mutex_lock(&vq->mutex);
  166. priv = test ? n : NULL;
  167. /* start polling new socket */
  168. oldpriv = vhost_vq_get_backend(vq);
  169. vhost_vq_set_backend(vq, priv);
  170. r = vhost_vq_init_access(&n->vqs[index]);
  171. mutex_unlock(&vq->mutex);
  172. if (r)
  173. goto err;
  174. if (oldpriv) {
  175. vhost_test_flush(n);
  176. }
  177. }
  178. mutex_unlock(&n->dev.mutex);
  179. return 0;
  180. err:
  181. mutex_unlock(&n->dev.mutex);
  182. return r;
  183. }
  184. static long vhost_test_reset_owner(struct vhost_test *n)
  185. {
  186. void *priv = NULL;
  187. long err;
  188. struct vhost_iotlb *umem;
  189. mutex_lock(&n->dev.mutex);
  190. err = vhost_dev_check_owner(&n->dev);
  191. if (err)
  192. goto done;
  193. umem = vhost_dev_reset_owner_prepare();
  194. if (!umem) {
  195. err = -ENOMEM;
  196. goto done;
  197. }
  198. vhost_test_stop(n, &priv);
  199. vhost_test_flush(n);
  200. vhost_dev_stop(&n->dev);
  201. vhost_dev_reset_owner(&n->dev, umem);
  202. done:
  203. mutex_unlock(&n->dev.mutex);
  204. return err;
  205. }
  206. static int vhost_test_set_features(struct vhost_test *n, u64 features)
  207. {
  208. struct vhost_virtqueue *vq;
  209. mutex_lock(&n->dev.mutex);
  210. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  211. !vhost_log_access_ok(&n->dev)) {
  212. mutex_unlock(&n->dev.mutex);
  213. return -EFAULT;
  214. }
  215. vq = &n->vqs[VHOST_TEST_VQ];
  216. mutex_lock(&vq->mutex);
  217. vq->acked_features = features;
  218. mutex_unlock(&vq->mutex);
  219. mutex_unlock(&n->dev.mutex);
  220. return 0;
  221. }
  222. static long vhost_test_set_backend(struct vhost_test *n, unsigned index, int fd)
  223. {
  224. static void *backend;
  225. const bool enable = fd != -1;
  226. struct vhost_virtqueue *vq;
  227. int r;
  228. mutex_lock(&n->dev.mutex);
  229. r = vhost_dev_check_owner(&n->dev);
  230. if (r)
  231. goto err;
  232. if (index >= VHOST_TEST_VQ_MAX) {
  233. r = -ENOBUFS;
  234. goto err;
  235. }
  236. vq = &n->vqs[index];
  237. mutex_lock(&vq->mutex);
  238. /* Verify that ring has been setup correctly. */
  239. if (!vhost_vq_access_ok(vq)) {
  240. r = -EFAULT;
  241. goto err_vq;
  242. }
  243. if (!enable) {
  244. vhost_poll_stop(&vq->poll);
  245. backend = vhost_vq_get_backend(vq);
  246. vhost_vq_set_backend(vq, NULL);
  247. } else {
  248. vhost_vq_set_backend(vq, backend);
  249. r = vhost_vq_init_access(vq);
  250. if (r == 0)
  251. r = vhost_poll_start(&vq->poll, vq->kick);
  252. }
  253. mutex_unlock(&vq->mutex);
  254. if (enable) {
  255. vhost_test_flush(n);
  256. }
  257. mutex_unlock(&n->dev.mutex);
  258. return 0;
  259. err_vq:
  260. mutex_unlock(&vq->mutex);
  261. err:
  262. mutex_unlock(&n->dev.mutex);
  263. return r;
  264. }
  265. static long vhost_test_ioctl(struct file *f, unsigned int ioctl,
  266. unsigned long arg)
  267. {
  268. struct vhost_vring_file backend;
  269. struct vhost_test *n = f->private_data;
  270. void __user *argp = (void __user *)arg;
  271. u64 __user *featurep = argp;
  272. int test;
  273. u64 features;
  274. int r;
  275. switch (ioctl) {
  276. case VHOST_TEST_RUN:
  277. if (copy_from_user(&test, argp, sizeof test))
  278. return -EFAULT;
  279. return vhost_test_run(n, test);
  280. case VHOST_TEST_SET_BACKEND:
  281. if (copy_from_user(&backend, argp, sizeof backend))
  282. return -EFAULT;
  283. return vhost_test_set_backend(n, backend.index, backend.fd);
  284. case VHOST_GET_FEATURES:
  285. features = VHOST_TEST_FEATURES;
  286. if (copy_to_user(featurep, &features, sizeof features))
  287. return -EFAULT;
  288. return 0;
  289. case VHOST_SET_FEATURES:
  290. if (copy_from_user(&features, featurep, sizeof features))
  291. return -EFAULT;
  292. if (features & ~VHOST_TEST_FEATURES)
  293. return -EOPNOTSUPP;
  294. return vhost_test_set_features(n, features);
  295. case VHOST_RESET_OWNER:
  296. return vhost_test_reset_owner(n);
  297. default:
  298. mutex_lock(&n->dev.mutex);
  299. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  300. if (r == -ENOIOCTLCMD)
  301. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  302. vhost_test_flush(n);
  303. mutex_unlock(&n->dev.mutex);
  304. return r;
  305. }
  306. }
  307. static const struct file_operations vhost_test_fops = {
  308. .owner = THIS_MODULE,
  309. .release = vhost_test_release,
  310. .unlocked_ioctl = vhost_test_ioctl,
  311. .compat_ioctl = compat_ptr_ioctl,
  312. .open = vhost_test_open,
  313. .llseek = noop_llseek,
  314. };
  315. static struct miscdevice vhost_test_misc = {
  316. MISC_DYNAMIC_MINOR,
  317. "vhost-test",
  318. &vhost_test_fops,
  319. };
  320. module_misc_device(vhost_test_misc);
  321. MODULE_VERSION("0.0.1");
  322. MODULE_LICENSE("GPL v2");
  323. MODULE_AUTHOR("Michael S. Tsirkin");
  324. MODULE_DESCRIPTION("Host kernel side for virtio simulator");