virtio_transport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 + VIRTIO_VSOCK_SKB_HEADROOM;
  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. struct sk_buff *skb;
  503. unsigned int len;
  504. if (!virtio_transport_more_replies(vsock)) {
  505. /* Stop rx until the device processes already
  506. * pending replies. Leave rx virtqueue
  507. * callbacks disabled.
  508. */
  509. goto out;
  510. }
  511. skb = virtqueue_get_buf(vq, &len);
  512. if (!skb)
  513. break;
  514. vsock->rx_buf_nr--;
  515. /* Drop short/long packets */
  516. if (unlikely(len < sizeof(struct virtio_vsock_hdr) ||
  517. len > virtio_vsock_skb_len(skb))) {
  518. kfree_skb(skb);
  519. continue;
  520. }
  521. virtio_vsock_skb_rx_put(skb);
  522. virtio_transport_deliver_tap_pkt(skb);
  523. virtio_transport_recv_pkt(&virtio_transport, skb);
  524. }
  525. } while (!virtqueue_enable_cb(vq));
  526. out:
  527. if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
  528. virtio_vsock_rx_fill(vsock);
  529. mutex_unlock(&vsock->rx_lock);
  530. }
  531. static int virtio_vsock_vqs_init(struct virtio_vsock *vsock)
  532. {
  533. struct virtio_device *vdev = vsock->vdev;
  534. struct virtqueue_info vqs_info[] = {
  535. { "rx", virtio_vsock_rx_done },
  536. { "tx", virtio_vsock_tx_done },
  537. { "event", virtio_vsock_event_done },
  538. };
  539. int ret;
  540. ret = virtio_find_vqs(vdev, VSOCK_VQ_MAX, vsock->vqs, vqs_info, NULL);
  541. if (ret < 0)
  542. return ret;
  543. virtio_vsock_update_guest_cid(vsock);
  544. virtio_device_ready(vdev);
  545. return 0;
  546. }
  547. static void virtio_vsock_vqs_start(struct virtio_vsock *vsock)
  548. {
  549. mutex_lock(&vsock->tx_lock);
  550. vsock->tx_run = true;
  551. mutex_unlock(&vsock->tx_lock);
  552. mutex_lock(&vsock->rx_lock);
  553. virtio_vsock_rx_fill(vsock);
  554. vsock->rx_run = true;
  555. mutex_unlock(&vsock->rx_lock);
  556. mutex_lock(&vsock->event_lock);
  557. virtio_vsock_event_fill(vsock);
  558. vsock->event_run = true;
  559. mutex_unlock(&vsock->event_lock);
  560. /* virtio_transport_send_pkt() can queue packets once
  561. * the_virtio_vsock is set, but they won't be processed until
  562. * vsock->tx_run is set to true. We queue vsock->send_pkt_work
  563. * when initialization finishes to send those packets queued
  564. * earlier.
  565. * We don't need to queue the other workers (rx, event) because
  566. * as long as we don't fill the queues with empty buffers, the
  567. * host can't send us any notification.
  568. */
  569. queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
  570. }
  571. static void virtio_vsock_vqs_del(struct virtio_vsock *vsock)
  572. {
  573. struct virtio_device *vdev = vsock->vdev;
  574. struct sk_buff *skb;
  575. /* Reset all connected sockets when the VQs disappear */
  576. vsock_for_each_connected_socket(&virtio_transport.transport,
  577. virtio_vsock_reset_sock);
  578. /* Stop all work handlers to make sure no one is accessing the device,
  579. * so we can safely call virtio_reset_device().
  580. */
  581. mutex_lock(&vsock->rx_lock);
  582. vsock->rx_run = false;
  583. mutex_unlock(&vsock->rx_lock);
  584. mutex_lock(&vsock->tx_lock);
  585. vsock->tx_run = false;
  586. mutex_unlock(&vsock->tx_lock);
  587. mutex_lock(&vsock->event_lock);
  588. vsock->event_run = false;
  589. mutex_unlock(&vsock->event_lock);
  590. /* Flush all device writes and interrupts, device will not use any
  591. * more buffers.
  592. */
  593. virtio_reset_device(vdev);
  594. mutex_lock(&vsock->rx_lock);
  595. while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
  596. kfree_skb(skb);
  597. mutex_unlock(&vsock->rx_lock);
  598. mutex_lock(&vsock->tx_lock);
  599. while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
  600. kfree_skb(skb);
  601. mutex_unlock(&vsock->tx_lock);
  602. virtio_vsock_skb_queue_purge(&vsock->send_pkt_queue);
  603. /* Delete virtqueues and flush outstanding callbacks if any */
  604. vdev->config->del_vqs(vdev);
  605. }
  606. static int virtio_vsock_probe(struct virtio_device *vdev)
  607. {
  608. struct virtio_vsock *vsock = NULL;
  609. int ret;
  610. int i;
  611. ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
  612. if (ret)
  613. return ret;
  614. /* Only one virtio-vsock device per guest is supported */
  615. if (rcu_dereference_protected(the_virtio_vsock,
  616. lockdep_is_held(&the_virtio_vsock_mutex))) {
  617. ret = -EBUSY;
  618. goto out;
  619. }
  620. vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
  621. if (!vsock) {
  622. ret = -ENOMEM;
  623. goto out;
  624. }
  625. vsock->vdev = vdev;
  626. vsock->rx_buf_nr = 0;
  627. vsock->rx_buf_max_nr = 0;
  628. atomic_set(&vsock->queued_replies, 0);
  629. mutex_init(&vsock->tx_lock);
  630. mutex_init(&vsock->rx_lock);
  631. mutex_init(&vsock->event_lock);
  632. skb_queue_head_init(&vsock->send_pkt_queue);
  633. INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
  634. INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
  635. INIT_WORK(&vsock->event_work, virtio_transport_event_work);
  636. INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
  637. if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_SEQPACKET))
  638. vsock->seqpacket_allow = true;
  639. vdev->priv = vsock;
  640. ret = virtio_vsock_vqs_init(vsock);
  641. if (ret < 0)
  642. goto out;
  643. for (i = 0; i < ARRAY_SIZE(vsock->out_sgs); i++)
  644. vsock->out_sgs[i] = &vsock->out_bufs[i];
  645. rcu_assign_pointer(the_virtio_vsock, vsock);
  646. virtio_vsock_vqs_start(vsock);
  647. mutex_unlock(&the_virtio_vsock_mutex);
  648. return 0;
  649. out:
  650. kfree(vsock);
  651. mutex_unlock(&the_virtio_vsock_mutex);
  652. return ret;
  653. }
  654. static void virtio_vsock_remove(struct virtio_device *vdev)
  655. {
  656. struct virtio_vsock *vsock = vdev->priv;
  657. mutex_lock(&the_virtio_vsock_mutex);
  658. vdev->priv = NULL;
  659. rcu_assign_pointer(the_virtio_vsock, NULL);
  660. synchronize_rcu();
  661. virtio_vsock_vqs_del(vsock);
  662. /* Other works can be queued before 'config->del_vqs()', so we flush
  663. * all works before to free the vsock object to avoid use after free.
  664. */
  665. flush_work(&vsock->rx_work);
  666. flush_work(&vsock->tx_work);
  667. flush_work(&vsock->event_work);
  668. flush_work(&vsock->send_pkt_work);
  669. mutex_unlock(&the_virtio_vsock_mutex);
  670. kfree(vsock);
  671. }
  672. #ifdef CONFIG_PM_SLEEP
  673. static int virtio_vsock_freeze(struct virtio_device *vdev)
  674. {
  675. struct virtio_vsock *vsock = vdev->priv;
  676. mutex_lock(&the_virtio_vsock_mutex);
  677. rcu_assign_pointer(the_virtio_vsock, NULL);
  678. synchronize_rcu();
  679. virtio_vsock_vqs_del(vsock);
  680. mutex_unlock(&the_virtio_vsock_mutex);
  681. return 0;
  682. }
  683. static int virtio_vsock_restore(struct virtio_device *vdev)
  684. {
  685. struct virtio_vsock *vsock = vdev->priv;
  686. int ret;
  687. mutex_lock(&the_virtio_vsock_mutex);
  688. /* Only one virtio-vsock device per guest is supported */
  689. if (rcu_dereference_protected(the_virtio_vsock,
  690. lockdep_is_held(&the_virtio_vsock_mutex))) {
  691. ret = -EBUSY;
  692. goto out;
  693. }
  694. ret = virtio_vsock_vqs_init(vsock);
  695. if (ret < 0)
  696. goto out;
  697. rcu_assign_pointer(the_virtio_vsock, vsock);
  698. virtio_vsock_vqs_start(vsock);
  699. out:
  700. mutex_unlock(&the_virtio_vsock_mutex);
  701. return ret;
  702. }
  703. #endif /* CONFIG_PM_SLEEP */
  704. static struct virtio_device_id id_table[] = {
  705. { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
  706. { 0 },
  707. };
  708. static unsigned int features[] = {
  709. VIRTIO_VSOCK_F_SEQPACKET
  710. };
  711. static struct virtio_driver virtio_vsock_driver = {
  712. .feature_table = features,
  713. .feature_table_size = ARRAY_SIZE(features),
  714. .driver.name = KBUILD_MODNAME,
  715. .id_table = id_table,
  716. .probe = virtio_vsock_probe,
  717. .remove = virtio_vsock_remove,
  718. #ifdef CONFIG_PM_SLEEP
  719. .freeze = virtio_vsock_freeze,
  720. .restore = virtio_vsock_restore,
  721. #endif
  722. };
  723. static int __init virtio_vsock_init(void)
  724. {
  725. int ret;
  726. virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
  727. if (!virtio_vsock_workqueue)
  728. return -ENOMEM;
  729. ret = vsock_core_register(&virtio_transport.transport,
  730. VSOCK_TRANSPORT_F_G2H);
  731. if (ret)
  732. goto out_wq;
  733. ret = register_virtio_driver(&virtio_vsock_driver);
  734. if (ret)
  735. goto out_vci;
  736. return 0;
  737. out_vci:
  738. vsock_core_unregister(&virtio_transport.transport);
  739. out_wq:
  740. destroy_workqueue(virtio_vsock_workqueue);
  741. return ret;
  742. }
  743. static void __exit virtio_vsock_exit(void)
  744. {
  745. unregister_virtio_driver(&virtio_vsock_driver);
  746. vsock_core_unregister(&virtio_transport.transport);
  747. destroy_workqueue(virtio_vsock_workqueue);
  748. }
  749. module_init(virtio_vsock_init);
  750. module_exit(virtio_vsock_exit);
  751. MODULE_LICENSE("GPL v2");
  752. MODULE_AUTHOR("Asias He");
  753. MODULE_DESCRIPTION("virtio transport for vsock");
  754. MODULE_DEVICE_TABLE(virtio, id_table);