vsock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * vhost transport for vsock
  3. *
  4. * Copyright (C) 2013-2015 Red Hat, Inc.
  5. * Author: Asias He <asias@redhat.com>
  6. * Stefan Hajnoczi <stefanha@redhat.com>
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2.
  9. */
  10. #include <linux/miscdevice.h>
  11. #include <linux/atomic.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/vmalloc.h>
  15. #include <net/sock.h>
  16. #include <linux/virtio_vsock.h>
  17. #include <linux/vhost.h>
  18. #include <net/af_vsock.h>
  19. #include "vhost.h"
  20. #define VHOST_VSOCK_DEFAULT_HOST_CID 2
  21. enum {
  22. VHOST_VSOCK_FEATURES = VHOST_FEATURES,
  23. };
  24. /* Used to track all the vhost_vsock instances on the system. */
  25. static DEFINE_SPINLOCK(vhost_vsock_lock);
  26. static LIST_HEAD(vhost_vsock_list);
  27. struct vhost_vsock {
  28. struct vhost_dev dev;
  29. struct vhost_virtqueue vqs[2];
  30. /* Link to global vhost_vsock_list, protected by vhost_vsock_lock */
  31. struct list_head list;
  32. struct vhost_work send_pkt_work;
  33. spinlock_t send_pkt_list_lock;
  34. struct list_head send_pkt_list; /* host->guest pending packets */
  35. atomic_t queued_replies;
  36. u32 guest_cid;
  37. };
  38. static u32 vhost_transport_get_local_cid(void)
  39. {
  40. return VHOST_VSOCK_DEFAULT_HOST_CID;
  41. }
  42. static struct vhost_vsock *__vhost_vsock_get(u32 guest_cid)
  43. {
  44. struct vhost_vsock *vsock;
  45. list_for_each_entry(vsock, &vhost_vsock_list, list) {
  46. u32 other_cid = vsock->guest_cid;
  47. /* Skip instances that have no CID yet */
  48. if (other_cid == 0)
  49. continue;
  50. if (other_cid == guest_cid)
  51. return vsock;
  52. }
  53. return NULL;
  54. }
  55. static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
  56. {
  57. struct vhost_vsock *vsock;
  58. spin_lock_bh(&vhost_vsock_lock);
  59. vsock = __vhost_vsock_get(guest_cid);
  60. spin_unlock_bh(&vhost_vsock_lock);
  61. return vsock;
  62. }
  63. static void
  64. vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
  65. struct vhost_virtqueue *vq)
  66. {
  67. struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
  68. bool added = false;
  69. bool restart_tx = false;
  70. mutex_lock(&vq->mutex);
  71. if (!vq->private_data)
  72. goto out;
  73. /* Avoid further vmexits, we're already processing the virtqueue */
  74. vhost_disable_notify(&vsock->dev, vq);
  75. for (;;) {
  76. struct virtio_vsock_pkt *pkt;
  77. struct iov_iter iov_iter;
  78. unsigned out, in;
  79. size_t nbytes;
  80. size_t len;
  81. int head;
  82. spin_lock_bh(&vsock->send_pkt_list_lock);
  83. if (list_empty(&vsock->send_pkt_list)) {
  84. spin_unlock_bh(&vsock->send_pkt_list_lock);
  85. vhost_enable_notify(&vsock->dev, vq);
  86. break;
  87. }
  88. pkt = list_first_entry(&vsock->send_pkt_list,
  89. struct virtio_vsock_pkt, list);
  90. list_del_init(&pkt->list);
  91. spin_unlock_bh(&vsock->send_pkt_list_lock);
  92. head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  93. &out, &in, NULL, NULL);
  94. if (head < 0) {
  95. spin_lock_bh(&vsock->send_pkt_list_lock);
  96. list_add(&pkt->list, &vsock->send_pkt_list);
  97. spin_unlock_bh(&vsock->send_pkt_list_lock);
  98. break;
  99. }
  100. if (head == vq->num) {
  101. spin_lock_bh(&vsock->send_pkt_list_lock);
  102. list_add(&pkt->list, &vsock->send_pkt_list);
  103. spin_unlock_bh(&vsock->send_pkt_list_lock);
  104. /* We cannot finish yet if more buffers snuck in while
  105. * re-enabling notify.
  106. */
  107. if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
  108. vhost_disable_notify(&vsock->dev, vq);
  109. continue;
  110. }
  111. break;
  112. }
  113. if (out) {
  114. virtio_transport_free_pkt(pkt);
  115. vq_err(vq, "Expected 0 output buffers, got %u\n", out);
  116. break;
  117. }
  118. len = iov_length(&vq->iov[out], in);
  119. iov_iter_init(&iov_iter, READ, &vq->iov[out], in, len);
  120. nbytes = copy_to_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
  121. if (nbytes != sizeof(pkt->hdr)) {
  122. virtio_transport_free_pkt(pkt);
  123. vq_err(vq, "Faulted on copying pkt hdr\n");
  124. break;
  125. }
  126. nbytes = copy_to_iter(pkt->buf, pkt->len, &iov_iter);
  127. if (nbytes != pkt->len) {
  128. virtio_transport_free_pkt(pkt);
  129. vq_err(vq, "Faulted on copying pkt buf\n");
  130. break;
  131. }
  132. vhost_add_used(vq, head, sizeof(pkt->hdr) + pkt->len);
  133. added = true;
  134. if (pkt->reply) {
  135. int val;
  136. val = atomic_dec_return(&vsock->queued_replies);
  137. /* Do we have resources to resume tx processing? */
  138. if (val + 1 == tx_vq->num)
  139. restart_tx = true;
  140. }
  141. /* Deliver to monitoring devices all correctly transmitted
  142. * packets.
  143. */
  144. virtio_transport_deliver_tap_pkt(pkt);
  145. virtio_transport_free_pkt(pkt);
  146. }
  147. if (added)
  148. vhost_signal(&vsock->dev, vq);
  149. out:
  150. mutex_unlock(&vq->mutex);
  151. if (restart_tx)
  152. vhost_poll_queue(&tx_vq->poll);
  153. }
  154. static void vhost_transport_send_pkt_work(struct vhost_work *work)
  155. {
  156. struct vhost_virtqueue *vq;
  157. struct vhost_vsock *vsock;
  158. vsock = container_of(work, struct vhost_vsock, send_pkt_work);
  159. vq = &vsock->vqs[VSOCK_VQ_RX];
  160. vhost_transport_do_send_pkt(vsock, vq);
  161. }
  162. static int
  163. vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
  164. {
  165. struct vhost_vsock *vsock;
  166. int len = pkt->len;
  167. /* Find the vhost_vsock according to guest context id */
  168. vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
  169. if (!vsock) {
  170. virtio_transport_free_pkt(pkt);
  171. return -ENODEV;
  172. }
  173. if (pkt->reply)
  174. atomic_inc(&vsock->queued_replies);
  175. spin_lock_bh(&vsock->send_pkt_list_lock);
  176. list_add_tail(&pkt->list, &vsock->send_pkt_list);
  177. spin_unlock_bh(&vsock->send_pkt_list_lock);
  178. vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
  179. return len;
  180. }
  181. static int
  182. vhost_transport_cancel_pkt(struct vsock_sock *vsk)
  183. {
  184. struct vhost_vsock *vsock;
  185. struct virtio_vsock_pkt *pkt, *n;
  186. int cnt = 0;
  187. LIST_HEAD(freeme);
  188. /* Find the vhost_vsock according to guest context id */
  189. vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
  190. if (!vsock)
  191. return -ENODEV;
  192. spin_lock_bh(&vsock->send_pkt_list_lock);
  193. list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
  194. if (pkt->vsk != vsk)
  195. continue;
  196. list_move(&pkt->list, &freeme);
  197. }
  198. spin_unlock_bh(&vsock->send_pkt_list_lock);
  199. list_for_each_entry_safe(pkt, n, &freeme, list) {
  200. if (pkt->reply)
  201. cnt++;
  202. list_del(&pkt->list);
  203. virtio_transport_free_pkt(pkt);
  204. }
  205. if (cnt) {
  206. struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
  207. int new_cnt;
  208. new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
  209. if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
  210. vhost_poll_queue(&tx_vq->poll);
  211. }
  212. return 0;
  213. }
  214. static struct virtio_vsock_pkt *
  215. vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
  216. unsigned int out, unsigned int in)
  217. {
  218. struct virtio_vsock_pkt *pkt;
  219. struct iov_iter iov_iter;
  220. size_t nbytes;
  221. size_t len;
  222. if (in != 0) {
  223. vq_err(vq, "Expected 0 input buffers, got %u\n", in);
  224. return NULL;
  225. }
  226. pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
  227. if (!pkt)
  228. return NULL;
  229. len = iov_length(vq->iov, out);
  230. iov_iter_init(&iov_iter, WRITE, vq->iov, out, len);
  231. nbytes = copy_from_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
  232. if (nbytes != sizeof(pkt->hdr)) {
  233. vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n",
  234. sizeof(pkt->hdr), nbytes);
  235. kfree(pkt);
  236. return NULL;
  237. }
  238. if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
  239. pkt->len = le32_to_cpu(pkt->hdr.len);
  240. /* No payload */
  241. if (!pkt->len)
  242. return pkt;
  243. /* The pkt is too big */
  244. if (pkt->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
  245. kfree(pkt);
  246. return NULL;
  247. }
  248. pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
  249. if (!pkt->buf) {
  250. kfree(pkt);
  251. return NULL;
  252. }
  253. nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
  254. if (nbytes != pkt->len) {
  255. vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
  256. pkt->len, nbytes);
  257. virtio_transport_free_pkt(pkt);
  258. return NULL;
  259. }
  260. return pkt;
  261. }
  262. /* Is there space left for replies to rx packets? */
  263. static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
  264. {
  265. struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX];
  266. int val;
  267. smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
  268. val = atomic_read(&vsock->queued_replies);
  269. return val < vq->num;
  270. }
  271. static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
  272. {
  273. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  274. poll.work);
  275. struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
  276. dev);
  277. struct virtio_vsock_pkt *pkt;
  278. int head;
  279. unsigned int out, in;
  280. bool added = false;
  281. mutex_lock(&vq->mutex);
  282. if (!vq->private_data)
  283. goto out;
  284. vhost_disable_notify(&vsock->dev, vq);
  285. for (;;) {
  286. u32 len;
  287. if (!vhost_vsock_more_replies(vsock)) {
  288. /* Stop tx until the device processes already
  289. * pending replies. Leave tx virtqueue
  290. * callbacks disabled.
  291. */
  292. goto no_more_replies;
  293. }
  294. head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  295. &out, &in, NULL, NULL);
  296. if (head < 0)
  297. break;
  298. if (head == vq->num) {
  299. if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
  300. vhost_disable_notify(&vsock->dev, vq);
  301. continue;
  302. }
  303. break;
  304. }
  305. pkt = vhost_vsock_alloc_pkt(vq, out, in);
  306. if (!pkt) {
  307. vq_err(vq, "Faulted on pkt\n");
  308. continue;
  309. }
  310. len = pkt->len;
  311. /* Deliver to monitoring devices all received packets */
  312. virtio_transport_deliver_tap_pkt(pkt);
  313. /* Only accept correctly addressed packets */
  314. if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
  315. virtio_transport_recv_pkt(pkt);
  316. else
  317. virtio_transport_free_pkt(pkt);
  318. vhost_add_used(vq, head, sizeof(pkt->hdr) + len);
  319. added = true;
  320. }
  321. no_more_replies:
  322. if (added)
  323. vhost_signal(&vsock->dev, vq);
  324. out:
  325. mutex_unlock(&vq->mutex);
  326. }
  327. static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
  328. {
  329. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  330. poll.work);
  331. struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
  332. dev);
  333. vhost_transport_do_send_pkt(vsock, vq);
  334. }
  335. static int vhost_vsock_start(struct vhost_vsock *vsock)
  336. {
  337. struct vhost_virtqueue *vq;
  338. size_t i;
  339. int ret;
  340. mutex_lock(&vsock->dev.mutex);
  341. ret = vhost_dev_check_owner(&vsock->dev);
  342. if (ret)
  343. goto err;
  344. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  345. vq = &vsock->vqs[i];
  346. mutex_lock(&vq->mutex);
  347. if (!vhost_vq_access_ok(vq)) {
  348. ret = -EFAULT;
  349. goto err_vq;
  350. }
  351. if (!vq->private_data) {
  352. vq->private_data = vsock;
  353. ret = vhost_vq_init_access(vq);
  354. if (ret)
  355. goto err_vq;
  356. }
  357. mutex_unlock(&vq->mutex);
  358. }
  359. mutex_unlock(&vsock->dev.mutex);
  360. return 0;
  361. err_vq:
  362. vq->private_data = NULL;
  363. mutex_unlock(&vq->mutex);
  364. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  365. vq = &vsock->vqs[i];
  366. mutex_lock(&vq->mutex);
  367. vq->private_data = NULL;
  368. mutex_unlock(&vq->mutex);
  369. }
  370. err:
  371. mutex_unlock(&vsock->dev.mutex);
  372. return ret;
  373. }
  374. static int vhost_vsock_stop(struct vhost_vsock *vsock)
  375. {
  376. size_t i;
  377. int ret;
  378. mutex_lock(&vsock->dev.mutex);
  379. ret = vhost_dev_check_owner(&vsock->dev);
  380. if (ret)
  381. goto err;
  382. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  383. struct vhost_virtqueue *vq = &vsock->vqs[i];
  384. mutex_lock(&vq->mutex);
  385. vq->private_data = NULL;
  386. mutex_unlock(&vq->mutex);
  387. }
  388. err:
  389. mutex_unlock(&vsock->dev.mutex);
  390. return ret;
  391. }
  392. static void vhost_vsock_free(struct vhost_vsock *vsock)
  393. {
  394. kvfree(vsock);
  395. }
  396. static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
  397. {
  398. struct vhost_virtqueue **vqs;
  399. struct vhost_vsock *vsock;
  400. int ret;
  401. /* This struct is large and allocation could fail, fall back to vmalloc
  402. * if there is no other way.
  403. */
  404. vsock = kvmalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  405. if (!vsock)
  406. return -ENOMEM;
  407. vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
  408. if (!vqs) {
  409. ret = -ENOMEM;
  410. goto out;
  411. }
  412. vsock->guest_cid = 0; /* no CID assigned yet */
  413. atomic_set(&vsock->queued_replies, 0);
  414. vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
  415. vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
  416. vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
  417. vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
  418. vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs));
  419. file->private_data = vsock;
  420. spin_lock_init(&vsock->send_pkt_list_lock);
  421. INIT_LIST_HEAD(&vsock->send_pkt_list);
  422. vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
  423. spin_lock_bh(&vhost_vsock_lock);
  424. list_add_tail(&vsock->list, &vhost_vsock_list);
  425. spin_unlock_bh(&vhost_vsock_lock);
  426. return 0;
  427. out:
  428. vhost_vsock_free(vsock);
  429. return ret;
  430. }
  431. static void vhost_vsock_flush(struct vhost_vsock *vsock)
  432. {
  433. int i;
  434. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
  435. if (vsock->vqs[i].handle_kick)
  436. vhost_poll_flush(&vsock->vqs[i].poll);
  437. vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
  438. }
  439. static void vhost_vsock_reset_orphans(struct sock *sk)
  440. {
  441. struct vsock_sock *vsk = vsock_sk(sk);
  442. /* vmci_transport.c doesn't take sk_lock here either. At least we're
  443. * under vsock_table_lock so the sock cannot disappear while we're
  444. * executing.
  445. */
  446. if (!vhost_vsock_get(vsk->remote_addr.svm_cid)) {
  447. sock_set_flag(sk, SOCK_DONE);
  448. vsk->peer_shutdown = SHUTDOWN_MASK;
  449. sk->sk_state = SS_UNCONNECTED;
  450. sk->sk_err = ECONNRESET;
  451. sk->sk_error_report(sk);
  452. }
  453. }
  454. static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
  455. {
  456. struct vhost_vsock *vsock = file->private_data;
  457. spin_lock_bh(&vhost_vsock_lock);
  458. list_del(&vsock->list);
  459. spin_unlock_bh(&vhost_vsock_lock);
  460. /* Iterating over all connections for all CIDs to find orphans is
  461. * inefficient. Room for improvement here. */
  462. vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
  463. vhost_vsock_stop(vsock);
  464. vhost_vsock_flush(vsock);
  465. vhost_dev_stop(&vsock->dev);
  466. spin_lock_bh(&vsock->send_pkt_list_lock);
  467. while (!list_empty(&vsock->send_pkt_list)) {
  468. struct virtio_vsock_pkt *pkt;
  469. pkt = list_first_entry(&vsock->send_pkt_list,
  470. struct virtio_vsock_pkt, list);
  471. list_del_init(&pkt->list);
  472. virtio_transport_free_pkt(pkt);
  473. }
  474. spin_unlock_bh(&vsock->send_pkt_list_lock);
  475. vhost_dev_cleanup(&vsock->dev);
  476. kfree(vsock->dev.vqs);
  477. vhost_vsock_free(vsock);
  478. return 0;
  479. }
  480. static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
  481. {
  482. struct vhost_vsock *other;
  483. /* Refuse reserved CIDs */
  484. if (guest_cid <= VMADDR_CID_HOST ||
  485. guest_cid == U32_MAX)
  486. return -EINVAL;
  487. /* 64-bit CIDs are not yet supported */
  488. if (guest_cid > U32_MAX)
  489. return -EINVAL;
  490. /* Refuse if CID is already in use */
  491. spin_lock_bh(&vhost_vsock_lock);
  492. other = __vhost_vsock_get(guest_cid);
  493. if (other && other != vsock) {
  494. spin_unlock_bh(&vhost_vsock_lock);
  495. return -EADDRINUSE;
  496. }
  497. vsock->guest_cid = guest_cid;
  498. spin_unlock_bh(&vhost_vsock_lock);
  499. return 0;
  500. }
  501. static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
  502. {
  503. struct vhost_virtqueue *vq;
  504. int i;
  505. if (features & ~VHOST_VSOCK_FEATURES)
  506. return -EOPNOTSUPP;
  507. mutex_lock(&vsock->dev.mutex);
  508. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  509. !vhost_log_access_ok(&vsock->dev)) {
  510. mutex_unlock(&vsock->dev.mutex);
  511. return -EFAULT;
  512. }
  513. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  514. vq = &vsock->vqs[i];
  515. mutex_lock(&vq->mutex);
  516. vq->acked_features = features;
  517. mutex_unlock(&vq->mutex);
  518. }
  519. mutex_unlock(&vsock->dev.mutex);
  520. return 0;
  521. }
  522. static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
  523. unsigned long arg)
  524. {
  525. struct vhost_vsock *vsock = f->private_data;
  526. void __user *argp = (void __user *)arg;
  527. u64 guest_cid;
  528. u64 features;
  529. int start;
  530. int r;
  531. switch (ioctl) {
  532. case VHOST_VSOCK_SET_GUEST_CID:
  533. if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
  534. return -EFAULT;
  535. return vhost_vsock_set_cid(vsock, guest_cid);
  536. case VHOST_VSOCK_SET_RUNNING:
  537. if (copy_from_user(&start, argp, sizeof(start)))
  538. return -EFAULT;
  539. if (start)
  540. return vhost_vsock_start(vsock);
  541. else
  542. return vhost_vsock_stop(vsock);
  543. case VHOST_GET_FEATURES:
  544. features = VHOST_VSOCK_FEATURES;
  545. if (copy_to_user(argp, &features, sizeof(features)))
  546. return -EFAULT;
  547. return 0;
  548. case VHOST_SET_FEATURES:
  549. if (copy_from_user(&features, argp, sizeof(features)))
  550. return -EFAULT;
  551. return vhost_vsock_set_features(vsock, features);
  552. default:
  553. mutex_lock(&vsock->dev.mutex);
  554. r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
  555. if (r == -ENOIOCTLCMD)
  556. r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
  557. else
  558. vhost_vsock_flush(vsock);
  559. mutex_unlock(&vsock->dev.mutex);
  560. return r;
  561. }
  562. }
  563. #ifdef CONFIG_COMPAT
  564. static long vhost_vsock_dev_compat_ioctl(struct file *f, unsigned int ioctl,
  565. unsigned long arg)
  566. {
  567. return vhost_vsock_dev_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  568. }
  569. #endif
  570. static const struct file_operations vhost_vsock_fops = {
  571. .owner = THIS_MODULE,
  572. .open = vhost_vsock_dev_open,
  573. .release = vhost_vsock_dev_release,
  574. .llseek = noop_llseek,
  575. .unlocked_ioctl = vhost_vsock_dev_ioctl,
  576. #ifdef CONFIG_COMPAT
  577. .compat_ioctl = vhost_vsock_dev_compat_ioctl,
  578. #endif
  579. };
  580. static struct miscdevice vhost_vsock_misc = {
  581. .minor = VHOST_VSOCK_MINOR,
  582. .name = "vhost-vsock",
  583. .fops = &vhost_vsock_fops,
  584. };
  585. static struct virtio_transport vhost_transport = {
  586. .transport = {
  587. .get_local_cid = vhost_transport_get_local_cid,
  588. .init = virtio_transport_do_socket_init,
  589. .destruct = virtio_transport_destruct,
  590. .release = virtio_transport_release,
  591. .connect = virtio_transport_connect,
  592. .shutdown = virtio_transport_shutdown,
  593. .cancel_pkt = vhost_transport_cancel_pkt,
  594. .dgram_enqueue = virtio_transport_dgram_enqueue,
  595. .dgram_dequeue = virtio_transport_dgram_dequeue,
  596. .dgram_bind = virtio_transport_dgram_bind,
  597. .dgram_allow = virtio_transport_dgram_allow,
  598. .stream_enqueue = virtio_transport_stream_enqueue,
  599. .stream_dequeue = virtio_transport_stream_dequeue,
  600. .stream_has_data = virtio_transport_stream_has_data,
  601. .stream_has_space = virtio_transport_stream_has_space,
  602. .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
  603. .stream_is_active = virtio_transport_stream_is_active,
  604. .stream_allow = virtio_transport_stream_allow,
  605. .notify_poll_in = virtio_transport_notify_poll_in,
  606. .notify_poll_out = virtio_transport_notify_poll_out,
  607. .notify_recv_init = virtio_transport_notify_recv_init,
  608. .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
  609. .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
  610. .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
  611. .notify_send_init = virtio_transport_notify_send_init,
  612. .notify_send_pre_block = virtio_transport_notify_send_pre_block,
  613. .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
  614. .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
  615. .set_buffer_size = virtio_transport_set_buffer_size,
  616. .set_min_buffer_size = virtio_transport_set_min_buffer_size,
  617. .set_max_buffer_size = virtio_transport_set_max_buffer_size,
  618. .get_buffer_size = virtio_transport_get_buffer_size,
  619. .get_min_buffer_size = virtio_transport_get_min_buffer_size,
  620. .get_max_buffer_size = virtio_transport_get_max_buffer_size,
  621. },
  622. .send_pkt = vhost_transport_send_pkt,
  623. };
  624. static int __init vhost_vsock_init(void)
  625. {
  626. int ret;
  627. ret = vsock_core_init(&vhost_transport.transport);
  628. if (ret < 0)
  629. return ret;
  630. return misc_register(&vhost_vsock_misc);
  631. };
  632. static void __exit vhost_vsock_exit(void)
  633. {
  634. misc_deregister(&vhost_vsock_misc);
  635. vsock_core_exit();
  636. };
  637. module_init(vhost_vsock_init);
  638. module_exit(vhost_vsock_exit);
  639. MODULE_LICENSE("GPL v2");
  640. MODULE_AUTHOR("Asias He");
  641. MODULE_DESCRIPTION("vhost transport for vsock ");
  642. MODULE_ALIAS_MISCDEV(VHOST_VSOCK_MINOR);
  643. MODULE_ALIAS("devname:vhost-vsock");