vsock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. size_t i;
  300. int ret;
  301. mutex_lock(&vsock->dev.mutex);
  302. ret = vhost_dev_check_owner(&vsock->dev);
  303. if (ret)
  304. goto err;
  305. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  306. struct vhost_virtqueue *vq = &vsock->vqs[i];
  307. mutex_lock(&vq->mutex);
  308. if (!vhost_vq_access_ok(vq)) {
  309. ret = -EFAULT;
  310. mutex_unlock(&vq->mutex);
  311. goto err_vq;
  312. }
  313. if (!vq->private_data) {
  314. vq->private_data = vsock;
  315. vhost_vq_init_access(vq);
  316. }
  317. mutex_unlock(&vq->mutex);
  318. }
  319. mutex_unlock(&vsock->dev.mutex);
  320. return 0;
  321. err_vq:
  322. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  323. struct vhost_virtqueue *vq = &vsock->vqs[i];
  324. mutex_lock(&vq->mutex);
  325. vq->private_data = NULL;
  326. mutex_unlock(&vq->mutex);
  327. }
  328. err:
  329. mutex_unlock(&vsock->dev.mutex);
  330. return ret;
  331. }
  332. static int vhost_vsock_stop(struct vhost_vsock *vsock)
  333. {
  334. size_t i;
  335. int ret;
  336. mutex_lock(&vsock->dev.mutex);
  337. ret = vhost_dev_check_owner(&vsock->dev);
  338. if (ret)
  339. goto err;
  340. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  341. struct vhost_virtqueue *vq = &vsock->vqs[i];
  342. mutex_lock(&vq->mutex);
  343. vq->private_data = NULL;
  344. mutex_unlock(&vq->mutex);
  345. }
  346. err:
  347. mutex_unlock(&vsock->dev.mutex);
  348. return ret;
  349. }
  350. static void vhost_vsock_free(struct vhost_vsock *vsock)
  351. {
  352. kvfree(vsock);
  353. }
  354. static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
  355. {
  356. struct vhost_virtqueue **vqs;
  357. struct vhost_vsock *vsock;
  358. int ret;
  359. /* This struct is large and allocation could fail, fall back to vmalloc
  360. * if there is no other way.
  361. */
  362. vsock = kzalloc(sizeof(*vsock), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  363. if (!vsock) {
  364. vsock = vmalloc(sizeof(*vsock));
  365. if (!vsock)
  366. return -ENOMEM;
  367. }
  368. vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
  369. if (!vqs) {
  370. ret = -ENOMEM;
  371. goto out;
  372. }
  373. atomic_set(&vsock->queued_replies, 0);
  374. vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
  375. vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
  376. vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
  377. vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
  378. vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs));
  379. file->private_data = vsock;
  380. spin_lock_init(&vsock->send_pkt_list_lock);
  381. INIT_LIST_HEAD(&vsock->send_pkt_list);
  382. vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
  383. spin_lock_bh(&vhost_vsock_lock);
  384. list_add_tail(&vsock->list, &vhost_vsock_list);
  385. spin_unlock_bh(&vhost_vsock_lock);
  386. return 0;
  387. out:
  388. vhost_vsock_free(vsock);
  389. return ret;
  390. }
  391. static void vhost_vsock_flush(struct vhost_vsock *vsock)
  392. {
  393. int i;
  394. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
  395. if (vsock->vqs[i].handle_kick)
  396. vhost_poll_flush(&vsock->vqs[i].poll);
  397. vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
  398. }
  399. static void vhost_vsock_reset_orphans(struct sock *sk)
  400. {
  401. struct vsock_sock *vsk = vsock_sk(sk);
  402. /* vmci_transport.c doesn't take sk_lock here either. At least we're
  403. * under vsock_table_lock so the sock cannot disappear while we're
  404. * executing.
  405. */
  406. if (!vhost_vsock_get(vsk->remote_addr.svm_cid)) {
  407. sock_set_flag(sk, SOCK_DONE);
  408. vsk->peer_shutdown = SHUTDOWN_MASK;
  409. sk->sk_state = SS_UNCONNECTED;
  410. sk->sk_err = ECONNRESET;
  411. sk->sk_error_report(sk);
  412. }
  413. }
  414. static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
  415. {
  416. struct vhost_vsock *vsock = file->private_data;
  417. spin_lock_bh(&vhost_vsock_lock);
  418. list_del(&vsock->list);
  419. spin_unlock_bh(&vhost_vsock_lock);
  420. /* Iterating over all connections for all CIDs to find orphans is
  421. * inefficient. Room for improvement here. */
  422. vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
  423. vhost_vsock_stop(vsock);
  424. vhost_vsock_flush(vsock);
  425. vhost_dev_stop(&vsock->dev);
  426. spin_lock_bh(&vsock->send_pkt_list_lock);
  427. while (!list_empty(&vsock->send_pkt_list)) {
  428. struct virtio_vsock_pkt *pkt;
  429. pkt = list_first_entry(&vsock->send_pkt_list,
  430. struct virtio_vsock_pkt, list);
  431. list_del_init(&pkt->list);
  432. virtio_transport_free_pkt(pkt);
  433. }
  434. spin_unlock_bh(&vsock->send_pkt_list_lock);
  435. vhost_dev_cleanup(&vsock->dev, false);
  436. kfree(vsock->dev.vqs);
  437. vhost_vsock_free(vsock);
  438. return 0;
  439. }
  440. static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
  441. {
  442. struct vhost_vsock *other;
  443. /* Refuse reserved CIDs */
  444. if (guest_cid <= VMADDR_CID_HOST ||
  445. guest_cid == U32_MAX)
  446. return -EINVAL;
  447. /* 64-bit CIDs are not yet supported */
  448. if (guest_cid > U32_MAX)
  449. return -EINVAL;
  450. /* Refuse if CID is already in use */
  451. spin_lock_bh(&vhost_vsock_lock);
  452. other = __vhost_vsock_get(guest_cid);
  453. if (other && other != vsock) {
  454. spin_unlock_bh(&vhost_vsock_lock);
  455. return -EADDRINUSE;
  456. }
  457. vsock->guest_cid = guest_cid;
  458. spin_unlock_bh(&vhost_vsock_lock);
  459. return 0;
  460. }
  461. static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
  462. {
  463. struct vhost_virtqueue *vq;
  464. int i;
  465. if (features & ~VHOST_VSOCK_FEATURES)
  466. return -EOPNOTSUPP;
  467. mutex_lock(&vsock->dev.mutex);
  468. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  469. !vhost_log_access_ok(&vsock->dev)) {
  470. mutex_unlock(&vsock->dev.mutex);
  471. return -EFAULT;
  472. }
  473. for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
  474. vq = &vsock->vqs[i];
  475. mutex_lock(&vq->mutex);
  476. vq->acked_features = features;
  477. mutex_unlock(&vq->mutex);
  478. }
  479. mutex_unlock(&vsock->dev.mutex);
  480. return 0;
  481. }
  482. static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
  483. unsigned long arg)
  484. {
  485. struct vhost_vsock *vsock = f->private_data;
  486. void __user *argp = (void __user *)arg;
  487. u64 guest_cid;
  488. u64 features;
  489. int start;
  490. int r;
  491. switch (ioctl) {
  492. case VHOST_VSOCK_SET_GUEST_CID:
  493. if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
  494. return -EFAULT;
  495. return vhost_vsock_set_cid(vsock, guest_cid);
  496. case VHOST_VSOCK_SET_RUNNING:
  497. if (copy_from_user(&start, argp, sizeof(start)))
  498. return -EFAULT;
  499. if (start)
  500. return vhost_vsock_start(vsock);
  501. else
  502. return vhost_vsock_stop(vsock);
  503. case VHOST_GET_FEATURES:
  504. features = VHOST_VSOCK_FEATURES;
  505. if (copy_to_user(argp, &features, sizeof(features)))
  506. return -EFAULT;
  507. return 0;
  508. case VHOST_SET_FEATURES:
  509. if (copy_from_user(&features, argp, sizeof(features)))
  510. return -EFAULT;
  511. return vhost_vsock_set_features(vsock, features);
  512. default:
  513. mutex_lock(&vsock->dev.mutex);
  514. r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
  515. if (r == -ENOIOCTLCMD)
  516. r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
  517. else
  518. vhost_vsock_flush(vsock);
  519. mutex_unlock(&vsock->dev.mutex);
  520. return r;
  521. }
  522. }
  523. static const struct file_operations vhost_vsock_fops = {
  524. .owner = THIS_MODULE,
  525. .open = vhost_vsock_dev_open,
  526. .release = vhost_vsock_dev_release,
  527. .llseek = noop_llseek,
  528. .unlocked_ioctl = vhost_vsock_dev_ioctl,
  529. };
  530. static struct miscdevice vhost_vsock_misc = {
  531. .minor = MISC_DYNAMIC_MINOR,
  532. .name = "vhost-vsock",
  533. .fops = &vhost_vsock_fops,
  534. };
  535. static struct virtio_transport vhost_transport = {
  536. .transport = {
  537. .get_local_cid = vhost_transport_get_local_cid,
  538. .init = virtio_transport_do_socket_init,
  539. .destruct = virtio_transport_destruct,
  540. .release = virtio_transport_release,
  541. .connect = virtio_transport_connect,
  542. .shutdown = virtio_transport_shutdown,
  543. .dgram_enqueue = virtio_transport_dgram_enqueue,
  544. .dgram_dequeue = virtio_transport_dgram_dequeue,
  545. .dgram_bind = virtio_transport_dgram_bind,
  546. .dgram_allow = virtio_transport_dgram_allow,
  547. .stream_enqueue = virtio_transport_stream_enqueue,
  548. .stream_dequeue = virtio_transport_stream_dequeue,
  549. .stream_has_data = virtio_transport_stream_has_data,
  550. .stream_has_space = virtio_transport_stream_has_space,
  551. .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
  552. .stream_is_active = virtio_transport_stream_is_active,
  553. .stream_allow = virtio_transport_stream_allow,
  554. .notify_poll_in = virtio_transport_notify_poll_in,
  555. .notify_poll_out = virtio_transport_notify_poll_out,
  556. .notify_recv_init = virtio_transport_notify_recv_init,
  557. .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
  558. .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
  559. .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
  560. .notify_send_init = virtio_transport_notify_send_init,
  561. .notify_send_pre_block = virtio_transport_notify_send_pre_block,
  562. .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
  563. .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
  564. .set_buffer_size = virtio_transport_set_buffer_size,
  565. .set_min_buffer_size = virtio_transport_set_min_buffer_size,
  566. .set_max_buffer_size = virtio_transport_set_max_buffer_size,
  567. .get_buffer_size = virtio_transport_get_buffer_size,
  568. .get_min_buffer_size = virtio_transport_get_min_buffer_size,
  569. .get_max_buffer_size = virtio_transport_get_max_buffer_size,
  570. },
  571. .send_pkt = vhost_transport_send_pkt,
  572. };
  573. static int __init vhost_vsock_init(void)
  574. {
  575. int ret;
  576. ret = vsock_core_init(&vhost_transport.transport);
  577. if (ret < 0)
  578. return ret;
  579. return misc_register(&vhost_vsock_misc);
  580. };
  581. static void __exit vhost_vsock_exit(void)
  582. {
  583. misc_deregister(&vhost_vsock_misc);
  584. vsock_core_exit();
  585. };
  586. module_init(vhost_vsock_init);
  587. module_exit(vhost_vsock_exit);
  588. MODULE_LICENSE("GPL v2");
  589. MODULE_AUTHOR("Asias He");
  590. MODULE_DESCRIPTION("vhost transport for vsock ");