test.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2009 Red Hat, Inc.
  3. * Author: Michael S. Tsirkin <[email protected]>
  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(struct vhost_test *n)
  126. {
  127. vhost_dev_flush(&n->dev);
  128. }
  129. static int vhost_test_release(struct inode *inode, struct file *f)
  130. {
  131. struct vhost_test *n = f->private_data;
  132. void *private;
  133. vhost_test_stop(n, &private);
  134. vhost_test_flush(n);
  135. vhost_dev_stop(&n->dev);
  136. vhost_dev_cleanup(&n->dev);
  137. kfree(n->dev.vqs);
  138. kfree(n);
  139. return 0;
  140. }
  141. static long vhost_test_run(struct vhost_test *n, int test)
  142. {
  143. void *priv, *oldpriv;
  144. struct vhost_virtqueue *vq;
  145. int r, index;
  146. if (test < 0 || test > 1)
  147. return -EINVAL;
  148. mutex_lock(&n->dev.mutex);
  149. r = vhost_dev_check_owner(&n->dev);
  150. if (r)
  151. goto err;
  152. for (index = 0; index < n->dev.nvqs; ++index) {
  153. /* Verify that ring has been setup correctly. */
  154. if (!vhost_vq_access_ok(&n->vqs[index])) {
  155. r = -EFAULT;
  156. goto err;
  157. }
  158. }
  159. for (index = 0; index < n->dev.nvqs; ++index) {
  160. vq = n->vqs + index;
  161. mutex_lock(&vq->mutex);
  162. priv = test ? n : NULL;
  163. /* start polling new socket */
  164. oldpriv = vhost_vq_get_backend(vq);
  165. vhost_vq_set_backend(vq, priv);
  166. r = vhost_vq_init_access(&n->vqs[index]);
  167. mutex_unlock(&vq->mutex);
  168. if (r)
  169. goto err;
  170. if (oldpriv) {
  171. vhost_test_flush(n);
  172. }
  173. }
  174. mutex_unlock(&n->dev.mutex);
  175. return 0;
  176. err:
  177. mutex_unlock(&n->dev.mutex);
  178. return r;
  179. }
  180. static long vhost_test_reset_owner(struct vhost_test *n)
  181. {
  182. void *priv = NULL;
  183. long err;
  184. struct vhost_iotlb *umem;
  185. mutex_lock(&n->dev.mutex);
  186. err = vhost_dev_check_owner(&n->dev);
  187. if (err)
  188. goto done;
  189. umem = vhost_dev_reset_owner_prepare();
  190. if (!umem) {
  191. err = -ENOMEM;
  192. goto done;
  193. }
  194. vhost_test_stop(n, &priv);
  195. vhost_test_flush(n);
  196. vhost_dev_stop(&n->dev);
  197. vhost_dev_reset_owner(&n->dev, umem);
  198. done:
  199. mutex_unlock(&n->dev.mutex);
  200. return err;
  201. }
  202. static int vhost_test_set_features(struct vhost_test *n, u64 features)
  203. {
  204. struct vhost_virtqueue *vq;
  205. mutex_lock(&n->dev.mutex);
  206. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  207. !vhost_log_access_ok(&n->dev)) {
  208. mutex_unlock(&n->dev.mutex);
  209. return -EFAULT;
  210. }
  211. vq = &n->vqs[VHOST_TEST_VQ];
  212. mutex_lock(&vq->mutex);
  213. vq->acked_features = features;
  214. mutex_unlock(&vq->mutex);
  215. mutex_unlock(&n->dev.mutex);
  216. return 0;
  217. }
  218. static long vhost_test_set_backend(struct vhost_test *n, unsigned index, int fd)
  219. {
  220. static void *backend;
  221. const bool enable = fd != -1;
  222. struct vhost_virtqueue *vq;
  223. int r;
  224. mutex_lock(&n->dev.mutex);
  225. r = vhost_dev_check_owner(&n->dev);
  226. if (r)
  227. goto err;
  228. if (index >= VHOST_TEST_VQ_MAX) {
  229. r = -ENOBUFS;
  230. goto err;
  231. }
  232. vq = &n->vqs[index];
  233. mutex_lock(&vq->mutex);
  234. /* Verify that ring has been setup correctly. */
  235. if (!vhost_vq_access_ok(vq)) {
  236. r = -EFAULT;
  237. goto err_vq;
  238. }
  239. if (!enable) {
  240. vhost_poll_stop(&vq->poll);
  241. backend = vhost_vq_get_backend(vq);
  242. vhost_vq_set_backend(vq, NULL);
  243. } else {
  244. vhost_vq_set_backend(vq, backend);
  245. r = vhost_vq_init_access(vq);
  246. if (r == 0)
  247. r = vhost_poll_start(&vq->poll, vq->kick);
  248. }
  249. mutex_unlock(&vq->mutex);
  250. if (enable) {
  251. vhost_test_flush(n);
  252. }
  253. mutex_unlock(&n->dev.mutex);
  254. return 0;
  255. err_vq:
  256. mutex_unlock(&vq->mutex);
  257. err:
  258. mutex_unlock(&n->dev.mutex);
  259. return r;
  260. }
  261. static long vhost_test_ioctl(struct file *f, unsigned int ioctl,
  262. unsigned long arg)
  263. {
  264. struct vhost_vring_file backend;
  265. struct vhost_test *n = f->private_data;
  266. void __user *argp = (void __user *)arg;
  267. u64 __user *featurep = argp;
  268. int test;
  269. u64 features;
  270. int r;
  271. switch (ioctl) {
  272. case VHOST_TEST_RUN:
  273. if (copy_from_user(&test, argp, sizeof test))
  274. return -EFAULT;
  275. return vhost_test_run(n, test);
  276. case VHOST_TEST_SET_BACKEND:
  277. if (copy_from_user(&backend, argp, sizeof backend))
  278. return -EFAULT;
  279. return vhost_test_set_backend(n, backend.index, backend.fd);
  280. case VHOST_GET_FEATURES:
  281. features = VHOST_FEATURES;
  282. if (copy_to_user(featurep, &features, sizeof features))
  283. return -EFAULT;
  284. return 0;
  285. case VHOST_SET_FEATURES:
  286. printk(KERN_ERR "1\n");
  287. if (copy_from_user(&features, featurep, sizeof features))
  288. return -EFAULT;
  289. printk(KERN_ERR "2\n");
  290. if (features & ~VHOST_FEATURES)
  291. return -EOPNOTSUPP;
  292. printk(KERN_ERR "3\n");
  293. return vhost_test_set_features(n, features);
  294. case VHOST_RESET_OWNER:
  295. return vhost_test_reset_owner(n);
  296. default:
  297. mutex_lock(&n->dev.mutex);
  298. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  299. if (r == -ENOIOCTLCMD)
  300. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  301. vhost_test_flush(n);
  302. mutex_unlock(&n->dev.mutex);
  303. return r;
  304. }
  305. }
  306. static const struct file_operations vhost_test_fops = {
  307. .owner = THIS_MODULE,
  308. .release = vhost_test_release,
  309. .unlocked_ioctl = vhost_test_ioctl,
  310. .compat_ioctl = compat_ptr_ioctl,
  311. .open = vhost_test_open,
  312. .llseek = noop_llseek,
  313. };
  314. static struct miscdevice vhost_test_misc = {
  315. MISC_DYNAMIC_MINOR,
  316. "vhost-test",
  317. &vhost_test_fops,
  318. };
  319. module_misc_device(vhost_test_misc);
  320. MODULE_VERSION("0.0.1");
  321. MODULE_LICENSE("GPL v2");
  322. MODULE_AUTHOR("Michael S. Tsirkin");
  323. MODULE_DESCRIPTION("Host kernel side for virtio simulator");