vsock.c 18 KB

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