test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. printk(KERN_ERR "1\n");
  240. if (copy_from_user(&features, featurep, sizeof features))
  241. return -EFAULT;
  242. printk(KERN_ERR "2\n");
  243. if (features & ~VHOST_FEATURES)
  244. return -EOPNOTSUPP;
  245. printk(KERN_ERR "3\n");
  246. return vhost_test_set_features(n, features);
  247. case VHOST_RESET_OWNER:
  248. return vhost_test_reset_owner(n);
  249. default:
  250. mutex_lock(&n->dev.mutex);
  251. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  252. if (r == -ENOIOCTLCMD)
  253. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  254. vhost_test_flush(n);
  255. mutex_unlock(&n->dev.mutex);
  256. return r;
  257. }
  258. }
  259. #ifdef CONFIG_COMPAT
  260. static long vhost_test_compat_ioctl(struct file *f, unsigned int ioctl,
  261. unsigned long arg)
  262. {
  263. return vhost_test_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  264. }
  265. #endif
  266. static const struct file_operations vhost_test_fops = {
  267. .owner = THIS_MODULE,
  268. .release = vhost_test_release,
  269. .unlocked_ioctl = vhost_test_ioctl,
  270. #ifdef CONFIG_COMPAT
  271. .compat_ioctl = vhost_test_compat_ioctl,
  272. #endif
  273. .open = vhost_test_open,
  274. .llseek = noop_llseek,
  275. };
  276. static struct miscdevice vhost_test_misc = {
  277. MISC_DYNAMIC_MINOR,
  278. "vhost-test",
  279. &vhost_test_fops,
  280. };
  281. static int vhost_test_init(void)
  282. {
  283. return misc_register(&vhost_test_misc);
  284. }
  285. module_init(vhost_test_init);
  286. static void vhost_test_exit(void)
  287. {
  288. misc_deregister(&vhost_test_misc);
  289. }
  290. module_exit(vhost_test_exit);
  291. MODULE_VERSION("0.0.1");
  292. MODULE_LICENSE("GPL v2");
  293. MODULE_AUTHOR("Michael S. Tsirkin");
  294. MODULE_DESCRIPTION("Host kernel side for virtio simulator");