test.c 7.5 KB

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