/drivers/vhost/test.c

http://github.com/mirrors/linux · C · 326 lines · 260 code · 45 blank · 21 comment · 29 complexity · 1d448e2f260339d71b83e536009c723f MD5 · raw file

  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. enum {
  27. VHOST_TEST_VQ = 0,
  28. VHOST_TEST_VQ_MAX = 1,
  29. };
  30. struct vhost_test {
  31. struct vhost_dev dev;
  32. struct vhost_virtqueue vqs[VHOST_TEST_VQ_MAX];
  33. };
  34. /* Expects to be always run from workqueue - which acts as
  35. * read-size critical section for our kind of RCU. */
  36. static void handle_vq(struct vhost_test *n)
  37. {
  38. struct vhost_virtqueue *vq = &n->vqs[VHOST_TEST_VQ];
  39. unsigned out, in;
  40. int head;
  41. size_t len, total_len = 0;
  42. void *private;
  43. mutex_lock(&vq->mutex);
  44. private = vhost_vq_get_backend(vq);
  45. if (!private) {
  46. mutex_unlock(&vq->mutex);
  47. return;
  48. }
  49. vhost_disable_notify(&n->dev, vq);
  50. for (;;) {
  51. head = vhost_get_vq_desc(vq, vq->iov,
  52. ARRAY_SIZE(vq->iov),
  53. &out, &in,
  54. NULL, NULL);
  55. /* On error, stop handling until the next kick. */
  56. if (unlikely(head < 0))
  57. break;
  58. /* Nothing new? Wait for eventfd to tell us they refilled. */
  59. if (head == vq->num) {
  60. if (unlikely(vhost_enable_notify(&n->dev, vq))) {
  61. vhost_disable_notify(&n->dev, vq);
  62. continue;
  63. }
  64. break;
  65. }
  66. if (in) {
  67. vq_err(vq, "Unexpected descriptor format for TX: "
  68. "out %d, int %d\n", out, in);
  69. break;
  70. }
  71. len = iov_length(vq->iov, out);
  72. /* Sanity check */
  73. if (!len) {
  74. vq_err(vq, "Unexpected 0 len for TX\n");
  75. break;
  76. }
  77. vhost_add_used_and_signal(&n->dev, vq, head, 0);
  78. total_len += len;
  79. if (unlikely(vhost_exceeds_weight(vq, 0, total_len)))
  80. break;
  81. }
  82. mutex_unlock(&vq->mutex);
  83. }
  84. static void handle_vq_kick(struct vhost_work *work)
  85. {
  86. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  87. poll.work);
  88. struct vhost_test *n = container_of(vq->dev, struct vhost_test, dev);
  89. handle_vq(n);
  90. }
  91. static int vhost_test_open(struct inode *inode, struct file *f)
  92. {
  93. struct vhost_test *n = kmalloc(sizeof *n, GFP_KERNEL);
  94. struct vhost_dev *dev;
  95. struct vhost_virtqueue **vqs;
  96. if (!n)
  97. return -ENOMEM;
  98. vqs = kmalloc_array(VHOST_TEST_VQ_MAX, sizeof(*vqs), GFP_KERNEL);
  99. if (!vqs) {
  100. kfree(n);
  101. return -ENOMEM;
  102. }
  103. dev = &n->dev;
  104. vqs[VHOST_TEST_VQ] = &n->vqs[VHOST_TEST_VQ];
  105. n->vqs[VHOST_TEST_VQ].handle_kick = handle_vq_kick;
  106. vhost_dev_init(dev, vqs, VHOST_TEST_VQ_MAX, UIO_MAXIOV,
  107. VHOST_TEST_PKT_WEIGHT, VHOST_TEST_WEIGHT, NULL);
  108. f->private_data = n;
  109. return 0;
  110. }
  111. static void *vhost_test_stop_vq(struct vhost_test *n,
  112. struct vhost_virtqueue *vq)
  113. {
  114. void *private;
  115. mutex_lock(&vq->mutex);
  116. private = vhost_vq_get_backend(vq);
  117. vhost_vq_set_backend(vq, NULL);
  118. mutex_unlock(&vq->mutex);
  119. return private;
  120. }
  121. static void vhost_test_stop(struct vhost_test *n, void **privatep)
  122. {
  123. *privatep = vhost_test_stop_vq(n, n->vqs + VHOST_TEST_VQ);
  124. }
  125. static void vhost_test_flush_vq(struct vhost_test *n, int index)
  126. {
  127. vhost_poll_flush(&n->vqs[index].poll);
  128. }
  129. static void vhost_test_flush(struct vhost_test *n)
  130. {
  131. vhost_test_flush_vq(n, VHOST_TEST_VQ);
  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. /* We do an extra flush before freeing memory,
  142. * since jobs can re-queue themselves. */
  143. vhost_test_flush(n);
  144. kfree(n);
  145. return 0;
  146. }
  147. static long vhost_test_run(struct vhost_test *n, int test)
  148. {
  149. void *priv, *oldpriv;
  150. struct vhost_virtqueue *vq;
  151. int r, index;
  152. if (test < 0 || test > 1)
  153. return -EINVAL;
  154. mutex_lock(&n->dev.mutex);
  155. r = vhost_dev_check_owner(&n->dev);
  156. if (r)
  157. goto err;
  158. for (index = 0; index < n->dev.nvqs; ++index) {
  159. /* Verify that ring has been setup correctly. */
  160. if (!vhost_vq_access_ok(&n->vqs[index])) {
  161. r = -EFAULT;
  162. goto err;
  163. }
  164. }
  165. for (index = 0; index < n->dev.nvqs; ++index) {
  166. vq = n->vqs + index;
  167. mutex_lock(&vq->mutex);
  168. priv = test ? n : NULL;
  169. /* start polling new socket */
  170. oldpriv = vhost_vq_get_backend(vq);
  171. vhost_vq_set_backend(vq, priv);
  172. r = vhost_vq_init_access(&n->vqs[index]);
  173. mutex_unlock(&vq->mutex);
  174. if (r)
  175. goto err;
  176. if (oldpriv) {
  177. vhost_test_flush_vq(n, index);
  178. }
  179. }
  180. mutex_unlock(&n->dev.mutex);
  181. return 0;
  182. err:
  183. mutex_unlock(&n->dev.mutex);
  184. return r;
  185. }
  186. static long vhost_test_reset_owner(struct vhost_test *n)
  187. {
  188. void *priv = NULL;
  189. long err;
  190. struct vhost_iotlb *umem;
  191. mutex_lock(&n->dev.mutex);
  192. err = vhost_dev_check_owner(&n->dev);
  193. if (err)
  194. goto done;
  195. umem = vhost_dev_reset_owner_prepare();
  196. if (!umem) {
  197. err = -ENOMEM;
  198. goto done;
  199. }
  200. vhost_test_stop(n, &priv);
  201. vhost_test_flush(n);
  202. vhost_dev_stop(&n->dev);
  203. vhost_dev_reset_owner(&n->dev, umem);
  204. done:
  205. mutex_unlock(&n->dev.mutex);
  206. return err;
  207. }
  208. static int vhost_test_set_features(struct vhost_test *n, u64 features)
  209. {
  210. struct vhost_virtqueue *vq;
  211. mutex_lock(&n->dev.mutex);
  212. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  213. !vhost_log_access_ok(&n->dev)) {
  214. mutex_unlock(&n->dev.mutex);
  215. return -EFAULT;
  216. }
  217. vq = &n->vqs[VHOST_TEST_VQ];
  218. mutex_lock(&vq->mutex);
  219. vq->acked_features = features;
  220. mutex_unlock(&vq->mutex);
  221. mutex_unlock(&n->dev.mutex);
  222. return 0;
  223. }
  224. static long vhost_test_ioctl(struct file *f, unsigned int ioctl,
  225. unsigned long arg)
  226. {
  227. struct vhost_test *n = f->private_data;
  228. void __user *argp = (void __user *)arg;
  229. u64 __user *featurep = argp;
  230. int test;
  231. u64 features;
  232. int r;
  233. switch (ioctl) {
  234. case VHOST_TEST_RUN:
  235. if (copy_from_user(&test, argp, sizeof test))
  236. return -EFAULT;
  237. return vhost_test_run(n, test);
  238. case VHOST_GET_FEATURES:
  239. features = VHOST_FEATURES;
  240. if (copy_to_user(featurep, &features, sizeof features))
  241. return -EFAULT;
  242. return 0;
  243. case VHOST_SET_FEATURES:
  244. printk(KERN_ERR "1\n");
  245. if (copy_from_user(&features, featurep, sizeof features))
  246. return -EFAULT;
  247. printk(KERN_ERR "2\n");
  248. if (features & ~VHOST_FEATURES)
  249. return -EOPNOTSUPP;
  250. printk(KERN_ERR "3\n");
  251. return vhost_test_set_features(n, features);
  252. case VHOST_RESET_OWNER:
  253. return vhost_test_reset_owner(n);
  254. default:
  255. mutex_lock(&n->dev.mutex);
  256. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  257. if (r == -ENOIOCTLCMD)
  258. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  259. vhost_test_flush(n);
  260. mutex_unlock(&n->dev.mutex);
  261. return r;
  262. }
  263. }
  264. static const struct file_operations vhost_test_fops = {
  265. .owner = THIS_MODULE,
  266. .release = vhost_test_release,
  267. .unlocked_ioctl = vhost_test_ioctl,
  268. .compat_ioctl = compat_ptr_ioctl,
  269. .open = vhost_test_open,
  270. .llseek = noop_llseek,
  271. };
  272. static struct miscdevice vhost_test_misc = {
  273. MISC_DYNAMIC_MINOR,
  274. "vhost-test",
  275. &vhost_test_fops,
  276. };
  277. module_misc_device(vhost_test_misc);
  278. MODULE_VERSION("0.0.1");
  279. MODULE_LICENSE("GPL v2");
  280. MODULE_AUTHOR("Michael S. Tsirkin");
  281. MODULE_DESCRIPTION("Host kernel side for virtio simulator");