test.c 7.4 KB

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