vsock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. spin_lock_bh(&vhost_vsock_lock);
  46. list_for_each_entry(vsock, &vhost_vsock_list, list) {
  47. u32 other_cid = vsock->guest_cid;
  48. /* Skip instances that have no CID yet */
  49. if (other_cid == 0)
  50. continue;
  51. if (other_cid == guest_cid) {
  52. spin_unlock_bh(&vhost_vsock_lock);
  53. return vsock;
  54. }
  55. }
  56. spin_unlock_bh(&vhost_vsock_lock);
  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. virtio_transport_free_pkt(pkt);
  138. }
  139. if (added)
  140. vhost_signal(&vsock->dev, vq);
  141. out:
  142. mutex_unlock(&vq->mutex);
  143. if (restart_tx)
  144. vhost_poll_queue(&tx_vq->poll);
  145. }
  146. static void vhost_transport_send_pkt_work(struct vhost_work *work)
  147. {
  148. struct vhost_virtqueue *vq;
  149. struct vhost_vsock *vsock;
  150. vsock = container_of(work, struct vhost_vsock, send_pkt_work);
  151. vq = &vsock->vqs[VSOCK_VQ_RX];
  152. vhost_transport_do_send_pkt(vsock, vq);
  153. }
  154. static int
  155. vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
  156. {
  157. struct vhost_vsock *vsock;
  158. struct vhost_virtqueue *vq;
  159. int len = pkt->len;
  160. /* Find the vhost_vsock according to guest context id */
  161. vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
  162. if (!vsock) {
  163. virtio_transport_free_pkt(pkt);
  164. return -ENODEV;
  165. }
  166. vq = &vsock->vqs[VSOCK_VQ_RX];
  167. if (pkt->reply)
  168. atomic_inc(&vsock->queued_replies);
  169. spin_lock_bh(&vsock->send_pkt_list_lock);
  170. list_add_tail(&pkt->list, &vsock->send_pkt_list);
  171. spin_unlock_bh(&vsock->send_pkt_list_lock);
  172. vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
  173. return len;
  174. }
  175. static int
  176. vhost_transport_cancel_pkt(struct vsock_sock *vsk)
  177. {
  178. struct vhost_vsock *vsock;
  179. struct virtio_vsock_pkt *pkt, *n;
  180. int cnt = 0;
  181. LIST_HEAD(freeme);
  182. /* Find the vhost_vsock according to guest context id */
  183. vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
  184. if (!vsock)
  185. return -ENODEV;
  186. spin_lock_bh(&vsock->send_pkt_list_lock);
  187. list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
  188. if (pkt->vsk != vsk)
  189. continue;
  190. list_move(&pkt->list, &freeme);
  191. }
  192. spin_unlock_bh(&vsock->send_pkt_list_lock);
  193. list_for_each_entry_safe(pkt, n, &freeme, list) {
  194. if (pkt->reply)
  195. cnt++;
  196. list_del(&pkt->list);
  197. virtio_transport_free_pkt(pkt);
  198. }
  199. if (cnt) {
  200. struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
  201. int new_cnt;
  202. new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
  203. if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
  204. vhost_poll_queue(&tx_vq->poll);
  205. }
  206. return 0;
  207. }
  208. static struct virtio_vsock_pkt *
  209. vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
  210. unsigned int out, unsigned int in)
  211. {
  212. struct virtio_vsock_pkt *pkt;
  213. struct iov_iter iov_iter;
  214. size_t nbytes;
  215. size_t len;
  216. if (in != 0) {
  217. vq_err(vq, "Expected 0 input buffers, got %u\n", in);
  218. return NULL;
  219. }
  220. pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
  221. if (!pkt)
  222. return NULL;
  223. len = iov_length(vq->iov, out);
  224. iov_iter_init(&iov_iter, WRITE, vq->iov, out, len);
  225. nbytes = copy_from_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
  226. if (nbytes != sizeof(pkt->hdr)) {
  227. vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n",
  228. sizeof(pkt->hdr), nbytes);
  229. kfree(pkt);
  230. return NULL;
  231. }
  232. if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
  233. pkt->len = le32_to_cpu(pkt->hdr.len);
  234. /* No payload */
  235. if (!pkt->len)
  236. return pkt;
  237. /* The pkt is too big */
  238. if (pkt->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
  239. kfree(pkt);
  240. return NULL;
  241. }
  242. pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
  243. if (!pkt->buf) {
  244. kfree(pkt);
  245. return NULL;
  246. }
  247. nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
  248. if (nbytes != pkt->len) {
  249. vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
  250. pkt->len, nbytes);
  251. virtio_transport_free_pkt(pkt);
  252. return NULL;
  253. }
  254. return pkt;
  255. }
  256. /* Is there space left for replies to rx packets? */
  257. static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
  258. {
  259. struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX];
  260. int val;
  261. smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
  262. val = atomic_read(&vsock->queued_replies);
  263. return val < vq->num;
  264. }
  265. static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
  266. {
  267. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  268. poll.work);
  269. struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
  270. dev);
  271. struct virtio_vsock_pkt *pkt;
  272. int head;
  273. unsigned int out, in;
  274. bool added = false;
  275. mutex_lock(&vq->mutex);
  276. if (!vq->private_data)
  277. goto out;
  278. vhost_disable_notify(&vsock->dev, vq);
  279. for (;;) {
  280. u32 len;
  281. if (!vhost_vsock_more_replies(vsock)) {
  282. /* Stop tx until the device processes already
  283. * pending replies. Leave tx virtqueue
  284. * callbacks disabled.
  285. */
  286. goto no_more_replies;
  287. }
  288. head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  289. &out, &in, NULL, NULL);
  290. if (head < 0)
  291. break;
  292. if (head == vq->num) {
  293. if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
  294. vhost_disable_notify(&vsock->dev, vq);
  295. continue;
  296. }
  297. break;
  298. }
  299. pkt = vhost_vsock_alloc_pkt(vq, out, in);
  300. if (!pkt) {
  301. vq_err(vq, "Faulted on pkt\n");
  302. continue;
  303. }
  304. len = pkt->len;
  305. /* Only accept correctly addressed packets */
  306. if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
  307. virtio_transport_recv_pkt(pkt);
  308. else
  309. virtio_transport_free_pkt(pkt);
  310. vhost_add_used(vq, head, sizeof(pkt->hdr) + len);
  311. added = true;
  312. }
  313. no_more_replies:
  314. if (added)
  315. vhost_signal(&vsock->dev, vq);
  316. out:
  317. mutex_unlock(&vq->mutex);
  318. }
  319. static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
  320. {
  321. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  322. poll.work);
  323. struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
  324. dev);
  325. vhost_transport_do_send_pkt(vsock, vq);
  326. }
  327. static int vhost_vsock_start(struct vhost_vsock *vsock)
  328. {
  329. struct vhost_virtqueue *vq;
  330. size_t i;
  331. int ret;
  332. mutex_lock(&vsock->dev.mutex);
  333. ret = vhost_dev_check_owner(&vsock->dev);
  334. if (ret)
  335. goto err;
  336. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  337. vq = &vsock->vqs[i];
  338. mutex_lock(&vq->mutex);
  339. if (!vhost_vq_access_ok(vq)) {
  340. ret = -EFAULT;
  341. goto err_vq;
  342. }
  343. if (!vq->private_data) {
  344. vq->private_data = vsock;
  345. ret = vhost_vq_init_access(vq);
  346. if (ret)
  347. goto err_vq;
  348. }
  349. mutex_unlock(&vq->mutex);
  350. }
  351. mutex_unlock(&vsock->dev.mutex);
  352. return 0;
  353. err_vq:
  354. vq->private_data = NULL;
  355. mutex_unlock(&vq->mutex);
  356. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  357. vq = &vsock->vqs[i];
  358. mutex_lock(&vq->mutex);
  359. vq->private_data = NULL;
  360. mutex_unlock(&vq->mutex);
  361. }
  362. err:
  363. mutex_unlock(&vsock->dev.mutex);
  364. return ret;
  365. }
  366. static int vhost_vsock_stop(struct vhost_vsock *vsock)
  367. {
  368. size_t i;
  369. int ret;
  370. mutex_lock(&vsock->dev.mutex);
  371. ret = vhost_dev_check_owner(&vsock->dev);
  372. if (ret)
  373. goto err;
  374. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  375. struct vhost_virtqueue *vq = &vsock->vqs[i];
  376. mutex_lock(&vq->mutex);
  377. vq->private_data = NULL;
  378. mutex_unlock(&vq->mutex);
  379. }
  380. err:
  381. mutex_unlock(&vsock->dev.mutex);
  382. return ret;
  383. }
  384. static void vhost_vsock_free(struct vhost_vsock *vsock)
  385. {
  386. kvfree(vsock);
  387. }
  388. static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
  389. {
  390. struct vhost_virtqueue **vqs;
  391. struct vhost_vsock *vsock;
  392. int ret;
  393. /* This struct is large and allocation could fail, fall back to vmalloc
  394. * if there is no other way.
  395. */
  396. vsock = kzalloc(sizeof(*vsock), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  397. if (!vsock) {
  398. vsock = vmalloc(sizeof(*vsock));
  399. if (!vsock)
  400. return -ENOMEM;
  401. }
  402. vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
  403. if (!vqs) {
  404. ret = -ENOMEM;
  405. goto out;
  406. }
  407. atomic_set(&vsock->queued_replies, 0);
  408. vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
  409. vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
  410. vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
  411. vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
  412. vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs));
  413. file->private_data = vsock;
  414. spin_lock_init(&vsock->send_pkt_list_lock);
  415. INIT_LIST_HEAD(&vsock->send_pkt_list);
  416. vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
  417. spin_lock_bh(&vhost_vsock_lock);
  418. list_add_tail(&vsock->list, &vhost_vsock_list);
  419. spin_unlock_bh(&vhost_vsock_lock);
  420. return 0;
  421. out:
  422. vhost_vsock_free(vsock);
  423. return ret;
  424. }
  425. static void vhost_vsock_flush(struct vhost_vsock *vsock)
  426. {
  427. int i;
  428. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
  429. if (vsock->vqs[i].handle_kick)
  430. vhost_poll_flush(&vsock->vqs[i].poll);
  431. vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
  432. }
  433. static void vhost_vsock_reset_orphans(struct sock *sk)
  434. {
  435. struct vsock_sock *vsk = vsock_sk(sk);
  436. /* vmci_transport.c doesn't take sk_lock here either. At least we're
  437. * under vsock_table_lock so the sock cannot disappear while we're
  438. * executing.
  439. */
  440. if (!vhost_vsock_get(vsk->remote_addr.svm_cid)) {
  441. sock_set_flag(sk, SOCK_DONE);
  442. vsk->peer_shutdown = SHUTDOWN_MASK;
  443. sk->sk_state = SS_UNCONNECTED;
  444. sk->sk_err = ECONNRESET;
  445. sk->sk_error_report(sk);
  446. }
  447. }
  448. static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
  449. {
  450. struct vhost_vsock *vsock = file->private_data;
  451. spin_lock_bh(&vhost_vsock_lock);
  452. list_del(&vsock->list);
  453. spin_unlock_bh(&vhost_vsock_lock);
  454. /* Iterating over all connections for all CIDs to find orphans is
  455. * inefficient. Room for improvement here. */
  456. vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
  457. vhost_vsock_stop(vsock);
  458. vhost_vsock_flush(vsock);
  459. vhost_dev_stop(&vsock->dev);
  460. spin_lock_bh(&vsock->send_pkt_list_lock);
  461. while (!list_empty(&vsock->send_pkt_list)) {
  462. struct virtio_vsock_pkt *pkt;
  463. pkt = list_first_entry(&vsock->send_pkt_list,
  464. struct virtio_vsock_pkt, list);
  465. list_del_init(&pkt->list);
  466. virtio_transport_free_pkt(pkt);
  467. }
  468. spin_unlock_bh(&vsock->send_pkt_list_lock);
  469. vhost_dev_cleanup(&vsock->dev, false);
  470. kfree(vsock->dev.vqs);
  471. vhost_vsock_free(vsock);
  472. return 0;
  473. }
  474. static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
  475. {
  476. struct vhost_vsock *other;
  477. /* Refuse reserved CIDs */
  478. if (guest_cid <= VMADDR_CID_HOST ||
  479. guest_cid == U32_MAX)
  480. return -EINVAL;
  481. /* 64-bit CIDs are not yet supported */
  482. if (guest_cid > U32_MAX)
  483. return -EINVAL;
  484. /* Refuse if CID is already in use */
  485. other = vhost_vsock_get(guest_cid);
  486. if (other && other != vsock)
  487. return -EADDRINUSE;
  488. spin_lock_bh(&vhost_vsock_lock);
  489. vsock->guest_cid = guest_cid;
  490. spin_unlock_bh(&vhost_vsock_lock);
  491. return 0;
  492. }
  493. static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
  494. {
  495. struct vhost_virtqueue *vq;
  496. int i;
  497. if (features & ~VHOST_VSOCK_FEATURES)
  498. return -EOPNOTSUPP;
  499. mutex_lock(&vsock->dev.mutex);
  500. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  501. !vhost_log_access_ok(&vsock->dev)) {
  502. mutex_unlock(&vsock->dev.mutex);
  503. return -EFAULT;
  504. }
  505. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  506. vq = &vsock->vqs[i];
  507. mutex_lock(&vq->mutex);
  508. vq->acked_features = features;
  509. mutex_unlock(&vq->mutex);
  510. }
  511. mutex_unlock(&vsock->dev.mutex);
  512. return 0;
  513. }
  514. static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
  515. unsigned long arg)
  516. {
  517. struct vhost_vsock *vsock = f->private_data;
  518. void __user *argp = (void __user *)arg;
  519. u64 guest_cid;
  520. u64 features;
  521. int start;
  522. int r;
  523. switch (ioctl) {
  524. case VHOST_VSOCK_SET_GUEST_CID:
  525. if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
  526. return -EFAULT;
  527. return vhost_vsock_set_cid(vsock, guest_cid);
  528. case VHOST_VSOCK_SET_RUNNING:
  529. if (copy_from_user(&start, argp, sizeof(start)))
  530. return -EFAULT;
  531. if (start)
  532. return vhost_vsock_start(vsock);
  533. else
  534. return vhost_vsock_stop(vsock);
  535. case VHOST_GET_FEATURES:
  536. features = VHOST_VSOCK_FEATURES;
  537. if (copy_to_user(argp, &features, sizeof(features)))
  538. return -EFAULT;
  539. return 0;
  540. case VHOST_SET_FEATURES:
  541. if (copy_from_user(&features, argp, sizeof(features)))
  542. return -EFAULT;
  543. return vhost_vsock_set_features(vsock, features);
  544. default:
  545. mutex_lock(&vsock->dev.mutex);
  546. r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
  547. if (r == -ENOIOCTLCMD)
  548. r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
  549. else
  550. vhost_vsock_flush(vsock);
  551. mutex_unlock(&vsock->dev.mutex);
  552. return r;
  553. }
  554. }
  555. static const struct file_operations vhost_vsock_fops = {
  556. .owner = THIS_MODULE,
  557. .open = vhost_vsock_dev_open,
  558. .release = vhost_vsock_dev_release,
  559. .llseek = noop_llseek,
  560. .unlocked_ioctl = vhost_vsock_dev_ioctl,
  561. };
  562. static struct miscdevice vhost_vsock_misc = {
  563. .minor = MISC_DYNAMIC_MINOR,
  564. .name = "vhost-vsock",
  565. .fops = &vhost_vsock_fops,
  566. };
  567. static struct virtio_transport vhost_transport = {
  568. .transport = {
  569. .get_local_cid = vhost_transport_get_local_cid,
  570. .init = virtio_transport_do_socket_init,
  571. .destruct = virtio_transport_destruct,
  572. .release = virtio_transport_release,
  573. .connect = virtio_transport_connect,
  574. .shutdown = virtio_transport_shutdown,
  575. .cancel_pkt = vhost_transport_cancel_pkt,
  576. .dgram_enqueue = virtio_transport_dgram_enqueue,
  577. .dgram_dequeue = virtio_transport_dgram_dequeue,
  578. .dgram_bind = virtio_transport_dgram_bind,
  579. .dgram_allow = virtio_transport_dgram_allow,
  580. .stream_enqueue = virtio_transport_stream_enqueue,
  581. .stream_dequeue = virtio_transport_stream_dequeue,
  582. .stream_has_data = virtio_transport_stream_has_data,
  583. .stream_has_space = virtio_transport_stream_has_space,
  584. .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
  585. .stream_is_active = virtio_transport_stream_is_active,
  586. .stream_allow = virtio_transport_stream_allow,
  587. .notify_poll_in = virtio_transport_notify_poll_in,
  588. .notify_poll_out = virtio_transport_notify_poll_out,
  589. .notify_recv_init = virtio_transport_notify_recv_init,
  590. .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
  591. .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
  592. .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
  593. .notify_send_init = virtio_transport_notify_send_init,
  594. .notify_send_pre_block = virtio_transport_notify_send_pre_block,
  595. .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
  596. .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
  597. .set_buffer_size = virtio_transport_set_buffer_size,
  598. .set_min_buffer_size = virtio_transport_set_min_buffer_size,
  599. .set_max_buffer_size = virtio_transport_set_max_buffer_size,
  600. .get_buffer_size = virtio_transport_get_buffer_size,
  601. .get_min_buffer_size = virtio_transport_get_min_buffer_size,
  602. .get_max_buffer_size = virtio_transport_get_max_buffer_size,
  603. },
  604. .send_pkt = vhost_transport_send_pkt,
  605. };
  606. static int __init vhost_vsock_init(void)
  607. {
  608. int ret;
  609. ret = vsock_core_init(&vhost_transport.transport);
  610. if (ret < 0)
  611. return ret;
  612. return misc_register(&vhost_vsock_misc);
  613. };
  614. static void __exit vhost_vsock_exit(void)
  615. {
  616. misc_deregister(&vhost_vsock_misc);
  617. vsock_core_exit();
  618. };
  619. module_init(vhost_vsock_init);
  620. module_exit(vhost_vsock_exit);
  621. MODULE_LICENSE("GPL v2");
  622. MODULE_AUTHOR("Asias He");
  623. MODULE_DESCRIPTION("vhost transport for vsock ");