vsock.c 20 KB

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