test.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, false);
  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. r = vhost_init_used(&n->vqs[index]);
  168. mutex_unlock(&vq->mutex);
  169. if (r)
  170. goto err;
  171. if (oldpriv) {
  172. vhost_test_flush_vq(n, index);
  173. }
  174. }
  175. mutex_unlock(&n->dev.mutex);
  176. return 0;
  177. err:
  178. mutex_unlock(&n->dev.mutex);
  179. return r;
  180. }
  181. static long vhost_test_reset_owner(struct vhost_test *n)
  182. {
  183. void *priv = NULL;
  184. long err;
  185. mutex_lock(&n->dev.mutex);
  186. err = vhost_dev_check_owner(&n->dev);
  187. if (err)
  188. goto done;
  189. vhost_test_stop(n, &priv);
  190. vhost_test_flush(n);
  191. err = vhost_dev_reset_owner(&n->dev);
  192. done:
  193. mutex_unlock(&n->dev.mutex);
  194. return err;
  195. }
  196. static int vhost_test_set_features(struct vhost_test *n, u64 features)
  197. {
  198. mutex_lock(&n->dev.mutex);
  199. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  200. !vhost_log_access_ok(&n->dev)) {
  201. mutex_unlock(&n->dev.mutex);
  202. return -EFAULT;
  203. }
  204. n->dev.acked_features = features;
  205. smp_wmb();
  206. vhost_test_flush(n);
  207. mutex_unlock(&n->dev.mutex);
  208. return 0;
  209. }
  210. static long vhost_test_ioctl(struct file *f, unsigned int ioctl,
  211. unsigned long arg)
  212. {
  213. struct vhost_test *n = f->private_data;
  214. void __user *argp = (void __user *)arg;
  215. u64 __user *featurep = argp;
  216. int test;
  217. u64 features;
  218. int r;
  219. switch (ioctl) {
  220. case VHOST_TEST_RUN:
  221. if (copy_from_user(&test, argp, sizeof test))
  222. return -EFAULT;
  223. return vhost_test_run(n, test);
  224. case VHOST_GET_FEATURES:
  225. features = VHOST_FEATURES;
  226. if (copy_to_user(featurep, &features, sizeof features))
  227. return -EFAULT;
  228. return 0;
  229. case VHOST_SET_FEATURES:
  230. if (copy_from_user(&features, featurep, sizeof features))
  231. return -EFAULT;
  232. if (features & ~VHOST_FEATURES)
  233. return -EOPNOTSUPP;
  234. return vhost_test_set_features(n, features);
  235. case VHOST_RESET_OWNER:
  236. return vhost_test_reset_owner(n);
  237. default:
  238. mutex_lock(&n->dev.mutex);
  239. r = vhost_dev_ioctl(&n->dev, ioctl, arg);
  240. vhost_test_flush(n);
  241. mutex_unlock(&n->dev.mutex);
  242. return r;
  243. }
  244. }
  245. #ifdef CONFIG_COMPAT
  246. static long vhost_test_compat_ioctl(struct file *f, unsigned int ioctl,
  247. unsigned long arg)
  248. {
  249. return vhost_test_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  250. }
  251. #endif
  252. static const struct file_operations vhost_test_fops = {
  253. .owner = THIS_MODULE,
  254. .release = vhost_test_release,
  255. .unlocked_ioctl = vhost_test_ioctl,
  256. #ifdef CONFIG_COMPAT
  257. .compat_ioctl = vhost_test_compat_ioctl,
  258. #endif
  259. .open = vhost_test_open,
  260. .llseek = noop_llseek,
  261. };
  262. static struct miscdevice vhost_test_misc = {
  263. MISC_DYNAMIC_MINOR,
  264. "vhost-test",
  265. &vhost_test_fops,
  266. };
  267. static int vhost_test_init(void)
  268. {
  269. return misc_register(&vhost_test_misc);
  270. }
  271. module_init(vhost_test_init);
  272. static void vhost_test_exit(void)
  273. {
  274. misc_deregister(&vhost_test_misc);
  275. }
  276. module_exit(vhost_test_exit);
  277. MODULE_VERSION("0.0.1");
  278. MODULE_LICENSE("GPL v2");
  279. MODULE_AUTHOR("Michael S. Tsirkin");
  280. MODULE_DESCRIPTION("Host kernel side for virtio simulator");