vsock.c 18 KB

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