virtio_transport.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * virtio transport for vsock
  4. *
  5. * Copyright (C) 2013-2015 Red Hat, Inc.
  6. * Author: Asias He <asias@redhat.com>
  7. * Stefan Hajnoczi <stefanha@redhat.com>
  8. *
  9. * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
  10. * early virtio-vsock proof-of-concept bits.
  11. */
  12. #include <linux/spinlock.h>
  13. #include <linux/module.h>
  14. #include <linux/list.h>
  15. #include <linux/atomic.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_ids.h>
  18. #include <linux/virtio_config.h>
  19. #include <linux/virtio_vsock.h>
  20. #include <net/sock.h>
  21. #include <linux/mutex.h>
  22. #include <net/af_vsock.h>
  23. static struct workqueue_struct *virtio_vsock_workqueue;
  24. static struct virtio_vsock __rcu *the_virtio_vsock;
  25. static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
  26. static struct virtio_transport virtio_transport; /* forward declaration */
  27. struct virtio_vsock {
  28. struct virtio_device *vdev;
  29. struct virtqueue *vqs[VSOCK_VQ_MAX];
  30. /* Virtqueue processing is deferred to a workqueue */
  31. struct work_struct tx_work;
  32. struct work_struct rx_work;
  33. struct work_struct event_work;
  34. /* The following fields are protected by tx_lock. vqs[VSOCK_VQ_TX]
  35. * must be accessed with tx_lock held.
  36. */
  37. struct mutex tx_lock;
  38. bool tx_run;
  39. struct work_struct send_pkt_work;
  40. struct sk_buff_head send_pkt_queue;
  41. atomic_t queued_replies;
  42. /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
  43. * must be accessed with rx_lock held.
  44. */
  45. struct mutex rx_lock;
  46. bool rx_run;
  47. int rx_buf_nr;
  48. int rx_buf_max_nr;
  49. /* The following fields are protected by event_lock.
  50. * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
  51. */
  52. struct mutex event_lock;
  53. bool event_run;
  54. struct virtio_vsock_event event_list[8];
  55. u32 guest_cid;
  56. bool seqpacket_allow;
  57. /* These fields are used only in tx path in function
  58. * 'virtio_transport_send_pkt_work()', so to save
  59. * stack space in it, place both of them here. Each
  60. * pointer from 'out_sgs' points to the corresponding
  61. * element in 'out_bufs' - this is initialized in
  62. * 'virtio_vsock_probe()'. Both fields are protected
  63. * by 'tx_lock'. +1 is needed for packet header.
  64. */
  65. struct scatterlist *out_sgs[MAX_SKB_FRAGS + 1];
  66. struct scatterlist out_bufs[MAX_SKB_FRAGS + 1];
  67. };
  68. static u32 virtio_transport_get_local_cid(void)
  69. {
  70. struct virtio_vsock *vsock;
  71. u32 ret;
  72. rcu_read_lock();
  73. vsock = rcu_dereference(the_virtio_vsock);
  74. if (!vsock) {
  75. ret = VMADDR_CID_ANY;
  76. goto out_rcu;
  77. }
  78. ret = vsock->guest_cid;
  79. out_rcu:
  80. rcu_read_unlock();
  81. return ret;
  82. }
  83. /* Caller need to hold vsock->tx_lock on vq */
  84. static int virtio_transport_send_skb(struct sk_buff *skb, struct virtqueue *vq,
  85. struct virtio_vsock *vsock, gfp_t gfp)
  86. {
  87. int ret, in_sg = 0, out_sg = 0;
  88. struct scatterlist **sgs;
  89. sgs = vsock->out_sgs;
  90. sg_init_one(sgs[out_sg], virtio_vsock_hdr(skb),
  91. sizeof(*virtio_vsock_hdr(skb)));
  92. out_sg++;
  93. if (!skb_is_nonlinear(skb)) {
  94. if (skb->len > 0) {
  95. sg_init_one(sgs[out_sg], skb->data, skb->len);
  96. out_sg++;
  97. }
  98. } else {
  99. struct skb_shared_info *si;
  100. int i;
  101. /* If skb is nonlinear, then its buffer must contain
  102. * only header and nothing more. Data is stored in
  103. * the fragged part.
  104. */
  105. WARN_ON_ONCE(skb_headroom(skb) != sizeof(*virtio_vsock_hdr(skb)));
  106. si = skb_shinfo(skb);
  107. for (i = 0; i < si->nr_frags; i++) {
  108. skb_frag_t *skb_frag = &si->frags[i];
  109. void *va;
  110. /* We will use 'page_to_virt()' for the userspace page
  111. * here, because virtio or dma-mapping layers will call
  112. * 'virt_to_phys()' later to fill the buffer descriptor.
  113. * We don't touch memory at "virtual" address of this page.
  114. */
  115. va = page_to_virt(skb_frag_page(skb_frag));
  116. sg_init_one(sgs[out_sg],
  117. va + skb_frag_off(skb_frag),
  118. skb_frag_size(skb_frag));
  119. out_sg++;
  120. }
  121. }
  122. ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, skb, gfp);
  123. /* Usually this means that there is no more space available in
  124. * the vq
  125. */
  126. if (ret < 0)
  127. return ret;
  128. virtio_transport_deliver_tap_pkt(skb);
  129. return 0;
  130. }
  131. static void
  132. virtio_transport_send_pkt_work(struct work_struct *work)
  133. {
  134. struct virtio_vsock *vsock =
  135. container_of(work, struct virtio_vsock, send_pkt_work);
  136. struct virtqueue *vq;
  137. bool added = false;
  138. bool restart_rx = false;
  139. mutex_lock(&vsock->tx_lock);
  140. if (!vsock->tx_run)
  141. goto out;
  142. vq = vsock->vqs[VSOCK_VQ_TX];
  143. for (;;) {
  144. struct sk_buff *skb;
  145. bool reply;
  146. int ret;
  147. skb = virtio_vsock_skb_dequeue(&vsock->send_pkt_queue);
  148. if (!skb)
  149. break;
  150. reply = virtio_vsock_skb_reply(skb);
  151. ret = virtio_transport_send_skb(skb, vq, vsock, GFP_KERNEL);
  152. if (ret < 0) {
  153. virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb);
  154. break;
  155. }
  156. if (reply) {
  157. struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
  158. int val;
  159. val = atomic_dec_return(&vsock->queued_replies);
  160. /* Do we now have resources to resume rx processing? */
  161. if (val + 1 == virtqueue_get_vring_size(rx_vq))
  162. restart_rx = true;
  163. }
  164. added = true;
  165. }
  166. if (added)
  167. virtqueue_kick(vq);
  168. out:
  169. mutex_unlock(&vsock->tx_lock);
  170. if (restart_rx)
  171. queue_work(virtio_vsock_workqueue, &vsock->rx_work);
  172. }
  173. /* Caller need to hold RCU for vsock.
  174. * Returns 0 if the packet is successfully put on the vq.
  175. */
  176. static int virtio_transport_send_skb_fast_path(struct virtio_vsock *vsock, struct sk_buff *skb)
  177. {
  178. struct virtqueue *vq = vsock->vqs[VSOCK_VQ_TX];
  179. int ret;
  180. /* Inside RCU, can't sleep! */
  181. ret = mutex_trylock(&vsock->tx_lock);
  182. if (unlikely(ret == 0))
  183. return -EBUSY;
  184. ret = virtio_transport_send_skb(skb, vq, vsock, GFP_ATOMIC);
  185. if (ret == 0)
  186. virtqueue_kick(vq);
  187. mutex_unlock(&vsock->tx_lock);
  188. return ret;
  189. }
  190. static int
  191. virtio_transport_send_pkt(struct sk_buff *skb)
  192. {
  193. struct virtio_vsock_hdr *hdr;
  194. struct virtio_vsock *vsock;
  195. int len = skb->len;
  196. hdr = virtio_vsock_hdr(skb);
  197. rcu_read_lock();
  198. vsock = rcu_dereference(the_virtio_vsock);
  199. if (!vsock) {
  200. kfree_skb(skb);
  201. len = -ENODEV;
  202. goto out_rcu;
  203. }
  204. if (le64_to_cpu(hdr->dst_cid) == vsock->guest_cid) {
  205. kfree_skb(skb);
  206. len = -ENODEV;
  207. goto out_rcu;
  208. }
  209. /* If send_pkt_queue is empty, we can safely bypass this queue
  210. * because packet order is maintained and (try) to put the packet
  211. * on the virtqueue using virtio_transport_send_skb_fast_path.
  212. * If this fails we simply put the packet on the intermediate
  213. * queue and schedule the worker.
  214. */
  215. if (!skb_queue_empty_lockless(&vsock->send_pkt_queue) ||
  216. virtio_transport_send_skb_fast_path(vsock, skb)) {
  217. if (virtio_vsock_skb_reply(skb))
  218. atomic_inc(&vsock->queued_replies);
  219. virtio_vsock_skb_queue_tail(&vsock->send_pkt_queue, skb);
  220. queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
  221. }
  222. out_rcu:
  223. rcu_read_unlock();
  224. return len;
  225. }
  226. static int
  227. virtio_transport_cancel_pkt(struct vsock_sock *vsk)
  228. {
  229. struct virtio_vsock *vsock;
  230. int cnt = 0, ret;
  231. rcu_read_lock();
  232. vsock = rcu_dereference(the_virtio_vsock);
  233. if (!vsock) {
  234. ret = -ENODEV;
  235. goto out_rcu;
  236. }
  237. cnt = virtio_transport_purge_skbs(vsk, &vsock->send_pkt_queue);
  238. if (cnt) {
  239. struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
  240. int new_cnt;
  241. new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
  242. if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
  243. new_cnt < virtqueue_get_vring_size(rx_vq))
  244. queue_work(virtio_vsock_workqueue, &vsock->rx_work);
  245. }
  246. ret = 0;
  247. out_rcu:
  248. rcu_read_unlock();
  249. return ret;
  250. }
  251. static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
  252. {
  253. int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
  254. struct scatterlist pkt, *p;
  255. struct virtqueue *vq;
  256. struct sk_buff *skb;
  257. int ret;
  258. vq = vsock->vqs[VSOCK_VQ_RX];
  259. do {
  260. skb = virtio_vsock_alloc_skb(total_len, GFP_KERNEL);
  261. if (!skb)
  262. break;
  263. memset(skb->head, 0, VIRTIO_VSOCK_SKB_HEADROOM);
  264. sg_init_one(&pkt, virtio_vsock_hdr(skb), total_len);
  265. p = &pkt;
  266. ret = virtqueue_add_sgs(vq, &p, 0, 1, skb, GFP_KERNEL);
  267. if (ret < 0) {
  268. kfree_skb(skb);
  269. break;
  270. }
  271. vsock->rx_buf_nr++;
  272. } while (vq->num_free);
  273. if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
  274. vsock->rx_buf_max_nr = vsock->rx_buf_nr;
  275. virtqueue_kick(vq);
  276. }
  277. static void virtio_transport_tx_work(struct work_struct *work)
  278. {
  279. struct virtio_vsock *vsock =
  280. container_of(work, struct virtio_vsock, tx_work);
  281. struct virtqueue *vq;
  282. bool added = false;
  283. vq = vsock->vqs[VSOCK_VQ_TX];
  284. mutex_lock(&vsock->tx_lock);
  285. if (!vsock->tx_run)
  286. goto out;
  287. do {
  288. struct sk_buff *skb;
  289. unsigned int len;
  290. virtqueue_disable_cb(vq);
  291. while ((skb = virtqueue_get_buf(vq, &len)) != NULL) {
  292. virtio_transport_consume_skb_sent(skb, true);
  293. added = true;
  294. }
  295. } while (!virtqueue_enable_cb(vq));
  296. out:
  297. mutex_unlock(&vsock->tx_lock);
  298. if (added)
  299. queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
  300. }
  301. /* Is there space left for replies to rx packets? */
  302. static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
  303. {
  304. struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
  305. int val;
  306. smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
  307. val = atomic_read(&vsock->queued_replies);
  308. return val < virtqueue_get_vring_size(vq);
  309. }
  310. /* event_lock must be held */
  311. static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
  312. struct virtio_vsock_event *event)
  313. {
  314. struct scatterlist sg;
  315. struct virtqueue *vq;
  316. vq = vsock->vqs[VSOCK_VQ_EVENT];
  317. sg_init_one(&sg, event, sizeof(*event));
  318. return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
  319. }
  320. /* event_lock must be held */
  321. static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
  322. {
  323. size_t i;
  324. for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
  325. struct virtio_vsock_event *event = &vsock->event_list[i];
  326. virtio_vsock_event_fill_one(vsock, event);
  327. }
  328. virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
  329. }
  330. static void virtio_vsock_reset_sock(struct sock *sk)
  331. {
  332. /* vmci_transport.c doesn't take sk_lock here either. At least we're
  333. * under vsock_table_lock so the sock cannot disappear while we're
  334. * executing.
  335. */
  336. sk->sk_state = TCP_CLOSE;
  337. sk->sk_err = ECONNRESET;
  338. sk_error_report(sk);
  339. }
  340. static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
  341. {
  342. struct virtio_device *vdev = vsock->vdev;
  343. __le64 guest_cid;
  344. vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
  345. &guest_cid, sizeof(guest_cid));
  346. vsock->guest_cid = le64_to_cpu(guest_cid);
  347. }
  348. /* event_lock must be held */
  349. static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
  350. struct virtio_vsock_event *event)
  351. {
  352. switch (le32_to_cpu(event->id)) {
  353. case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
  354. virtio_vsock_update_guest_cid(vsock);
  355. vsock_for_each_connected_socket(&virtio_transport.transport,
  356. virtio_vsock_reset_sock);
  357. break;
  358. }
  359. }
  360. static void virtio_transport_event_work(struct work_struct *work)
  361. {
  362. struct virtio_vsock *vsock =
  363. container_of(work, struct virtio_vsock, event_work);
  364. struct virtqueue *vq;
  365. vq = vsock->vqs[VSOCK_VQ_EVENT];
  366. mutex_lock(&vsock->event_lock);
  367. if (!vsock->event_run)
  368. goto out;
  369. do {
  370. struct virtio_vsock_event *event;
  371. unsigned int len;
  372. virtqueue_disable_cb(vq);
  373. while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
  374. if (len == sizeof(*event))
  375. virtio_vsock_event_handle(vsock, event);
  376. virtio_vsock_event_fill_one(vsock, event);
  377. }
  378. } while (!virtqueue_enable_cb(vq));
  379. virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
  380. out:
  381. mutex_unlock(&vsock->event_lock);
  382. }
  383. static void virtio_vsock_event_done(struct virtqueue *vq)
  384. {
  385. struct virtio_vsock *vsock = vq->vdev->priv;
  386. if (!vsock)
  387. return;
  388. queue_work(virtio_vsock_workqueue, &vsock->event_work);
  389. }
  390. static void virtio_vsock_tx_done(struct virtqueue *vq)
  391. {
  392. struct virtio_vsock *vsock = vq->vdev->priv;
  393. if (!vsock)
  394. return;
  395. queue_work(virtio_vsock_workqueue, &vsock->tx_work);
  396. }
  397. static void virtio_vsock_rx_done(struct virtqueue *vq)
  398. {
  399. struct virtio_vsock *vsock = vq->vdev->priv;
  400. if (!vsock)
  401. return;
  402. queue_work(virtio_vsock_workqueue, &vsock->rx_work);
  403. }
  404. static bool virtio_transport_can_msgzerocopy(int bufs_num)
  405. {
  406. struct virtio_vsock *vsock;
  407. bool res = false;
  408. rcu_read_lock();
  409. vsock = rcu_dereference(the_virtio_vsock);
  410. if (vsock) {
  411. struct virtqueue *vq = vsock->vqs[VSOCK_VQ_TX];
  412. /* Check that tx queue is large enough to keep whole
  413. * data to send. This is needed, because when there is
  414. * not enough free space in the queue, current skb to
  415. * send will be reinserted to the head of tx list of
  416. * the socket to retry transmission later, so if skb
  417. * is bigger than whole queue, it will be reinserted
  418. * again and again, thus blocking other skbs to be sent.
  419. * Each page of the user provided buffer will be added
  420. * as a single buffer to the tx virtqueue, so compare
  421. * number of pages against maximum capacity of the queue.
  422. */
  423. if (bufs_num <= vq->num_max)
  424. res = true;
  425. }
  426. rcu_read_unlock();
  427. return res;
  428. }
  429. static bool virtio_transport_msgzerocopy_allow(void)
  430. {
  431. return true;
  432. }
  433. static bool virtio_transport_seqpacket_allow(u32 remote_cid);
  434. static struct virtio_transport virtio_transport = {
  435. .transport = {
  436. .module = THIS_MODULE,
  437. .get_local_cid = virtio_transport_get_local_cid,
  438. .init = virtio_transport_do_socket_init,
  439. .destruct = virtio_transport_destruct,
  440. .release = virtio_transport_release,
  441. .connect = virtio_transport_connect,
  442. .shutdown = virtio_transport_shutdown,
  443. .cancel_pkt = virtio_transport_cancel_pkt,
  444. .dgram_bind = virtio_transport_dgram_bind,
  445. .dgram_dequeue = virtio_transport_dgram_dequeue,
  446. .dgram_enqueue = virtio_transport_dgram_enqueue,
  447. .dgram_allow = virtio_transport_dgram_allow,
  448. .stream_dequeue = virtio_transport_stream_dequeue,
  449. .stream_enqueue = virtio_transport_stream_enqueue,
  450. .stream_has_data = virtio_transport_stream_has_data,
  451. .stream_has_space = virtio_transport_stream_has_space,
  452. .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
  453. .stream_is_active = virtio_transport_stream_is_active,
  454. .stream_allow = virtio_transport_stream_allow,
  455. .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
  456. .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
  457. .seqpacket_allow = virtio_transport_seqpacket_allow,
  458. .seqpacket_has_data = virtio_transport_seqpacket_has_data,
  459. .msgzerocopy_allow = virtio_transport_msgzerocopy_allow,
  460. .notify_poll_in = virtio_transport_notify_poll_in,
  461. .notify_poll_out = virtio_transport_notify_poll_out,
  462. .notify_recv_init = virtio_transport_notify_recv_init,
  463. .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
  464. .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
  465. .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
  466. .notify_send_init = virtio_transport_notify_send_init,
  467. .notify_send_pre_block = virtio_transport_notify_send_pre_block,
  468. .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
  469. .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
  470. .notify_buffer_size = virtio_transport_notify_buffer_size,
  471. .notify_set_rcvlowat = virtio_transport_notify_set_rcvlowat,
  472. .unsent_bytes = virtio_transport_unsent_bytes,
  473. .read_skb = virtio_transport_read_skb,
  474. },
  475. .send_pkt = virtio_transport_send_pkt,
  476. .can_msgzerocopy = virtio_transport_can_msgzerocopy,
  477. };
  478. static bool virtio_transport_seqpacket_allow(u32 remote_cid)
  479. {
  480. struct virtio_vsock *vsock;
  481. bool seqpacket_allow;
  482. seqpacket_allow = false;
  483. rcu_read_lock();
  484. vsock = rcu_dereference(the_virtio_vsock);
  485. if (vsock)
  486. seqpacket_allow = vsock->seqpacket_allow;
  487. rcu_read_unlock();
  488. return seqpacket_allow;
  489. }
  490. static void virtio_transport_rx_work(struct work_struct *work)
  491. {
  492. struct virtio_vsock *vsock =
  493. container_of(work, struct virtio_vsock, rx_work);
  494. struct virtqueue *vq;
  495. vq = vsock->vqs[VSOCK_VQ_RX];
  496. mutex_lock(&vsock->rx_lock);
  497. if (!vsock->rx_run)
  498. goto out;
  499. do {
  500. virtqueue_disable_cb(vq);
  501. for (;;) {
  502. unsigned int len, payload_len;
  503. struct virtio_vsock_hdr *hdr;
  504. struct sk_buff *skb;
  505. if (!virtio_transport_more_replies(vsock)) {
  506. /* Stop rx until the device processes already
  507. * pending replies. Leave rx virtqueue
  508. * callbacks disabled.
  509. */
  510. goto out;
  511. }
  512. skb = virtqueue_get_buf(vq, &len);
  513. if (!skb)
  514. break;
  515. vsock->rx_buf_nr--;
  516. /* Drop short/long packets */
  517. if (unlikely(len < sizeof(*hdr) ||
  518. len > virtio_vsock_skb_len(skb))) {
  519. kfree_skb(skb);
  520. continue;
  521. }
  522. hdr = virtio_vsock_hdr(skb);
  523. payload_len = le32_to_cpu(hdr->len);
  524. if (unlikely(payload_len > len - sizeof(*hdr))) {
  525. kfree_skb(skb);
  526. continue;
  527. }
  528. virtio_vsock_skb_rx_put(skb);
  529. virtio_transport_deliver_tap_pkt(skb);
  530. virtio_transport_recv_pkt(&virtio_transport, skb);
  531. }
  532. } while (!virtqueue_enable_cb(vq));
  533. out:
  534. if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
  535. virtio_vsock_rx_fill(vsock);
  536. mutex_unlock(&vsock->rx_lock);
  537. }
  538. static int virtio_vsock_vqs_init(struct virtio_vsock *vsock)
  539. {
  540. struct virtio_device *vdev = vsock->vdev;
  541. struct virtqueue_info vqs_info[] = {
  542. { "rx", virtio_vsock_rx_done },
  543. { "tx", virtio_vsock_tx_done },
  544. { "event", virtio_vsock_event_done },
  545. };
  546. int ret;
  547. mutex_lock(&vsock->rx_lock);
  548. vsock->rx_buf_nr = 0;
  549. vsock->rx_buf_max_nr = 0;
  550. mutex_unlock(&vsock->rx_lock);
  551. atomic_set(&vsock->queued_replies, 0);
  552. ret = virtio_find_vqs(vdev, VSOCK_VQ_MAX, vsock->vqs, vqs_info, NULL);
  553. if (ret < 0)
  554. return ret;
  555. virtio_vsock_update_guest_cid(vsock);
  556. virtio_device_ready(vdev);
  557. return 0;
  558. }
  559. static void virtio_vsock_vqs_start(struct virtio_vsock *vsock)
  560. {
  561. mutex_lock(&vsock->tx_lock);
  562. vsock->tx_run = true;
  563. mutex_unlock(&vsock->tx_lock);
  564. mutex_lock(&vsock->rx_lock);
  565. virtio_vsock_rx_fill(vsock);
  566. vsock->rx_run = true;
  567. mutex_unlock(&vsock->rx_lock);
  568. mutex_lock(&vsock->event_lock);
  569. virtio_vsock_event_fill(vsock);
  570. vsock->event_run = true;
  571. mutex_unlock(&vsock->event_lock);
  572. /* virtio_transport_send_pkt() can queue packets once
  573. * the_virtio_vsock is set, but they won't be processed until
  574. * vsock->tx_run is set to true. We queue vsock->send_pkt_work
  575. * when initialization finishes to send those packets queued
  576. * earlier.
  577. * We don't need to queue the other workers (rx, event) because
  578. * as long as we don't fill the queues with empty buffers, the
  579. * host can't send us any notification.
  580. */
  581. queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
  582. }
  583. static void virtio_vsock_vqs_del(struct virtio_vsock *vsock)
  584. {
  585. struct virtio_device *vdev = vsock->vdev;
  586. struct sk_buff *skb;
  587. /* Reset all connected sockets when the VQs disappear */
  588. vsock_for_each_connected_socket(&virtio_transport.transport,
  589. virtio_vsock_reset_sock);
  590. /* Stop all work handlers to make sure no one is accessing the device,
  591. * so we can safely call virtio_reset_device().
  592. */
  593. mutex_lock(&vsock->rx_lock);
  594. vsock->rx_run = false;
  595. mutex_unlock(&vsock->rx_lock);
  596. mutex_lock(&vsock->tx_lock);
  597. vsock->tx_run = false;
  598. mutex_unlock(&vsock->tx_lock);
  599. mutex_lock(&vsock->event_lock);
  600. vsock->event_run = false;
  601. mutex_unlock(&vsock->event_lock);
  602. /* Flush all device writes and interrupts, device will not use any
  603. * more buffers.
  604. */
  605. virtio_reset_device(vdev);
  606. mutex_lock(&vsock->rx_lock);
  607. while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
  608. kfree_skb(skb);
  609. mutex_unlock(&vsock->rx_lock);
  610. mutex_lock(&vsock->tx_lock);
  611. while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
  612. kfree_skb(skb);
  613. mutex_unlock(&vsock->tx_lock);
  614. virtio_vsock_skb_queue_purge(&vsock->send_pkt_queue);
  615. /* Delete virtqueues and flush outstanding callbacks if any */
  616. vdev->config->del_vqs(vdev);
  617. }
  618. static int virtio_vsock_probe(struct virtio_device *vdev)
  619. {
  620. struct virtio_vsock *vsock = NULL;
  621. int ret;
  622. int i;
  623. ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
  624. if (ret)
  625. return ret;
  626. /* Only one virtio-vsock device per guest is supported */
  627. if (rcu_dereference_protected(the_virtio_vsock,
  628. lockdep_is_held(&the_virtio_vsock_mutex))) {
  629. ret = -EBUSY;
  630. goto out;
  631. }
  632. vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
  633. if (!vsock) {
  634. ret = -ENOMEM;
  635. goto out;
  636. }
  637. vsock->vdev = vdev;
  638. mutex_init(&vsock->tx_lock);
  639. mutex_init(&vsock->rx_lock);
  640. mutex_init(&vsock->event_lock);
  641. skb_queue_head_init(&vsock->send_pkt_queue);
  642. INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
  643. INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
  644. INIT_WORK(&vsock->event_work, virtio_transport_event_work);
  645. INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
  646. if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_SEQPACKET))
  647. vsock->seqpacket_allow = true;
  648. vdev->priv = vsock;
  649. ret = virtio_vsock_vqs_init(vsock);
  650. if (ret < 0)
  651. goto out;
  652. for (i = 0; i < ARRAY_SIZE(vsock->out_sgs); i++)
  653. vsock->out_sgs[i] = &vsock->out_bufs[i];
  654. rcu_assign_pointer(the_virtio_vsock, vsock);
  655. virtio_vsock_vqs_start(vsock);
  656. mutex_unlock(&the_virtio_vsock_mutex);
  657. return 0;
  658. out:
  659. kfree(vsock);
  660. mutex_unlock(&the_virtio_vsock_mutex);
  661. return ret;
  662. }
  663. static void virtio_vsock_remove(struct virtio_device *vdev)
  664. {
  665. struct virtio_vsock *vsock = vdev->priv;
  666. mutex_lock(&the_virtio_vsock_mutex);
  667. vdev->priv = NULL;
  668. rcu_assign_pointer(the_virtio_vsock, NULL);
  669. synchronize_rcu();
  670. virtio_vsock_vqs_del(vsock);
  671. /* Other works can be queued before 'config->del_vqs()', so we flush
  672. * all works before to free the vsock object to avoid use after free.
  673. */
  674. flush_work(&vsock->rx_work);
  675. flush_work(&vsock->tx_work);
  676. flush_work(&vsock->event_work);
  677. flush_work(&vsock->send_pkt_work);
  678. mutex_unlock(&the_virtio_vsock_mutex);
  679. kfree(vsock);
  680. }
  681. #ifdef CONFIG_PM_SLEEP
  682. static int virtio_vsock_freeze(struct virtio_device *vdev)
  683. {
  684. struct virtio_vsock *vsock = vdev->priv;
  685. mutex_lock(&the_virtio_vsock_mutex);
  686. rcu_assign_pointer(the_virtio_vsock, NULL);
  687. synchronize_rcu();
  688. virtio_vsock_vqs_del(vsock);
  689. mutex_unlock(&the_virtio_vsock_mutex);
  690. return 0;
  691. }
  692. static int virtio_vsock_restore(struct virtio_device *vdev)
  693. {
  694. struct virtio_vsock *vsock = vdev->priv;
  695. int ret;
  696. mutex_lock(&the_virtio_vsock_mutex);
  697. /* Only one virtio-vsock device per guest is supported */
  698. if (rcu_dereference_protected(the_virtio_vsock,
  699. lockdep_is_held(&the_virtio_vsock_mutex))) {
  700. ret = -EBUSY;
  701. goto out;
  702. }
  703. ret = virtio_vsock_vqs_init(vsock);
  704. if (ret < 0)
  705. goto out;
  706. rcu_assign_pointer(the_virtio_vsock, vsock);
  707. virtio_vsock_vqs_start(vsock);
  708. out:
  709. mutex_unlock(&the_virtio_vsock_mutex);
  710. return ret;
  711. }
  712. #endif /* CONFIG_PM_SLEEP */
  713. static struct virtio_device_id id_table[] = {
  714. { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
  715. { 0 },
  716. };
  717. static unsigned int features[] = {
  718. VIRTIO_VSOCK_F_SEQPACKET
  719. };
  720. static struct virtio_driver virtio_vsock_driver = {
  721. .feature_table = features,
  722. .feature_table_size = ARRAY_SIZE(features),
  723. .driver.name = KBUILD_MODNAME,
  724. .id_table = id_table,
  725. .probe = virtio_vsock_probe,
  726. .remove = virtio_vsock_remove,
  727. #ifdef CONFIG_PM_SLEEP
  728. .freeze = virtio_vsock_freeze,
  729. .restore = virtio_vsock_restore,
  730. #endif
  731. };
  732. static int __init virtio_vsock_init(void)
  733. {
  734. int ret;
  735. virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
  736. if (!virtio_vsock_workqueue)
  737. return -ENOMEM;
  738. ret = vsock_core_register(&virtio_transport.transport,
  739. VSOCK_TRANSPORT_F_G2H);
  740. if (ret)
  741. goto out_wq;
  742. ret = register_virtio_driver(&virtio_vsock_driver);
  743. if (ret)
  744. goto out_vci;
  745. return 0;
  746. out_vci:
  747. vsock_core_unregister(&virtio_transport.transport);
  748. out_wq:
  749. destroy_workqueue(virtio_vsock_workqueue);
  750. return ret;
  751. }
  752. static void __exit virtio_vsock_exit(void)
  753. {
  754. unregister_virtio_driver(&virtio_vsock_driver);
  755. vsock_core_unregister(&virtio_transport.transport);
  756. destroy_workqueue(virtio_vsock_workqueue);
  757. }
  758. module_init(virtio_vsock_init);
  759. module_exit(virtio_vsock_exit);
  760. MODULE_LICENSE("GPL v2");
  761. MODULE_AUTHOR("Asias He");
  762. MODULE_DESCRIPTION("virtio transport for vsock");
  763. MODULE_DEVICE_TABLE(virtio, id_table);