test.c 7.1 KB

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