test.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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, true, 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_set_backend(struct vhost_test *n, unsigned index, int fd)
  225. {
  226. static void *backend;
  227. const bool enable = fd != -1;
  228. struct vhost_virtqueue *vq;
  229. int r;
  230. mutex_lock(&n->dev.mutex);
  231. r = vhost_dev_check_owner(&n->dev);
  232. if (r)
  233. goto err;
  234. if (index >= VHOST_TEST_VQ_MAX) {
  235. r = -ENOBUFS;
  236. goto err;
  237. }
  238. vq = &n->vqs[index];
  239. mutex_lock(&vq->mutex);
  240. /* Verify that ring has been setup correctly. */
  241. if (!vhost_vq_access_ok(vq)) {
  242. r = -EFAULT;
  243. goto err_vq;
  244. }
  245. if (!enable) {
  246. vhost_poll_stop(&vq->poll);
  247. backend = vhost_vq_get_backend(vq);
  248. vhost_vq_set_backend(vq, NULL);
  249. } else {
  250. vhost_vq_set_backend(vq, backend);
  251. r = vhost_vq_init_access(vq);
  252. if (r == 0)
  253. r = vhost_poll_start(&vq->poll, vq->kick);
  254. }
  255. mutex_unlock(&vq->mutex);
  256. if (enable) {
  257. vhost_test_flush_vq(n, index);
  258. }
  259. mutex_unlock(&n->dev.mutex);
  260. return 0;
  261. err_vq:
  262. mutex_unlock(&vq->mutex);
  263. err:
  264. mutex_unlock(&n->dev.mutex);
  265. return r;
  266. }
  267. static long vhost_test_ioctl(struct file *f, unsigned int ioctl,
  268. unsigned long arg)
  269. {
  270. struct vhost_vring_file backend;
  271. struct vhost_test *n = f->private_data;
  272. void __user *argp = (void __user *)arg;
  273. u64 __user *featurep = argp;
  274. int test;
  275. u64 features;
  276. int r;
  277. switch (ioctl) {
  278. case VHOST_TEST_RUN:
  279. if (copy_from_user(&test, argp, sizeof test))
  280. return -EFAULT;
  281. return vhost_test_run(n, test);
  282. case VHOST_TEST_SET_BACKEND:
  283. if (copy_from_user(&backend, argp, sizeof backend))
  284. return -EFAULT;
  285. return vhost_test_set_backend(n, backend.index, backend.fd);
  286. case VHOST_GET_FEATURES:
  287. features = VHOST_FEATURES;
  288. if (copy_to_user(featurep, &features, sizeof features))
  289. return -EFAULT;
  290. return 0;
  291. case VHOST_SET_FEATURES:
  292. printk(KERN_ERR "1\n");
  293. if (copy_from_user(&features, featurep, sizeof features))
  294. return -EFAULT;
  295. printk(KERN_ERR "2\n");
  296. if (features & ~VHOST_FEATURES)
  297. return -EOPNOTSUPP;
  298. printk(KERN_ERR "3\n");
  299. return vhost_test_set_features(n, features);
  300. case VHOST_RESET_OWNER:
  301. return vhost_test_reset_owner(n);
  302. default:
  303. mutex_lock(&n->dev.mutex);
  304. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  305. if (r == -ENOIOCTLCMD)
  306. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  307. vhost_test_flush(n);
  308. mutex_unlock(&n->dev.mutex);
  309. return r;
  310. }
  311. }
  312. static const struct file_operations vhost_test_fops = {
  313. .owner = THIS_MODULE,
  314. .release = vhost_test_release,
  315. .unlocked_ioctl = vhost_test_ioctl,
  316. .compat_ioctl = compat_ptr_ioctl,
  317. .open = vhost_test_open,
  318. .llseek = noop_llseek,
  319. };
  320. static struct miscdevice vhost_test_misc = {
  321. MISC_DYNAMIC_MINOR,
  322. "vhost-test",
  323. &vhost_test_fops,
  324. };
  325. module_misc_device(vhost_test_misc);
  326. MODULE_VERSION("0.0.1");
  327. MODULE_LICENSE("GPL v2");
  328. MODULE_AUTHOR("Michael S. Tsirkin");
  329. MODULE_DESCRIPTION("Host kernel side for virtio simulator");