virtio_transport.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * virtio 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. * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
  9. * early virtio-vsock proof-of-concept bits.
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2.
  12. */
  13. #include <linux/spinlock.h>
  14. #include <linux/module.h>
  15. #include <linux/list.h>
  16. #include <linux/atomic.h>
  17. #include <linux/virtio.h>
  18. #include <linux/virtio_ids.h>
  19. #include <linux/virtio_config.h>
  20. #include <linux/virtio_vsock.h>
  21. #include <net/sock.h>
  22. #include <linux/mutex.h>
  23. #include <net/af_vsock.h>
  24. static struct workqueue_struct *virtio_vsock_workqueue;
  25. static struct virtio_vsock *the_virtio_vsock;
  26. static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
  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. spinlock_t send_pkt_list_lock;
  41. struct list_head send_pkt_list;
  42. struct work_struct loopback_work;
  43. spinlock_t loopback_list_lock; /* protects loopback_list */
  44. struct list_head loopback_list;
  45. atomic_t queued_replies;
  46. /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
  47. * must be accessed with rx_lock held.
  48. */
  49. struct mutex rx_lock;
  50. bool rx_run;
  51. int rx_buf_nr;
  52. int rx_buf_max_nr;
  53. /* The following fields are protected by event_lock.
  54. * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
  55. */
  56. struct mutex event_lock;
  57. bool event_run;
  58. struct virtio_vsock_event event_list[8];
  59. u32 guest_cid;
  60. };
  61. static u32 virtio_transport_get_local_cid(void)
  62. {
  63. struct virtio_vsock *vsock;
  64. u32 ret;
  65. rcu_read_lock();
  66. vsock = rcu_dereference(the_virtio_vsock);
  67. if (!vsock) {
  68. ret = VMADDR_CID_ANY;
  69. goto out_rcu;
  70. }
  71. ret = vsock->guest_cid;
  72. out_rcu:
  73. rcu_read_unlock();
  74. return ret;
  75. }
  76. static int virtio_transport_send_pkt_loopback(struct virtio_vsock *vsock,
  77. struct virtio_vsock_pkt *pkt)
  78. {
  79. int len = pkt->len;
  80. spin_lock_bh(&vsock->loopback_list_lock);
  81. list_add_tail(&pkt->list, &vsock->loopback_list);
  82. spin_unlock_bh(&vsock->loopback_list_lock);
  83. queue_work(virtio_vsock_workqueue, &vsock->loopback_work);
  84. return len;
  85. }
  86. static void
  87. virtio_transport_send_pkt_work(struct work_struct *work)
  88. {
  89. struct virtio_vsock *vsock =
  90. container_of(work, struct virtio_vsock, send_pkt_work);
  91. struct virtqueue *vq;
  92. bool added = false;
  93. bool restart_rx = false;
  94. mutex_lock(&vsock->tx_lock);
  95. if (!vsock->tx_run)
  96. goto out;
  97. vq = vsock->vqs[VSOCK_VQ_TX];
  98. for (;;) {
  99. struct virtio_vsock_pkt *pkt;
  100. struct scatterlist hdr, buf, *sgs[2];
  101. int ret, in_sg = 0, out_sg = 0;
  102. bool reply;
  103. spin_lock_bh(&vsock->send_pkt_list_lock);
  104. if (list_empty(&vsock->send_pkt_list)) {
  105. spin_unlock_bh(&vsock->send_pkt_list_lock);
  106. break;
  107. }
  108. pkt = list_first_entry(&vsock->send_pkt_list,
  109. struct virtio_vsock_pkt, list);
  110. list_del_init(&pkt->list);
  111. spin_unlock_bh(&vsock->send_pkt_list_lock);
  112. virtio_transport_deliver_tap_pkt(pkt);
  113. reply = pkt->reply;
  114. sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
  115. sgs[out_sg++] = &hdr;
  116. if (pkt->buf) {
  117. sg_init_one(&buf, pkt->buf, pkt->len);
  118. sgs[out_sg++] = &buf;
  119. }
  120. ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
  121. /* Usually this means that there is no more space available in
  122. * the vq
  123. */
  124. if (ret < 0) {
  125. spin_lock_bh(&vsock->send_pkt_list_lock);
  126. list_add(&pkt->list, &vsock->send_pkt_list);
  127. spin_unlock_bh(&vsock->send_pkt_list_lock);
  128. break;
  129. }
  130. if (reply) {
  131. struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
  132. int val;
  133. val = atomic_dec_return(&vsock->queued_replies);
  134. /* Do we now have resources to resume rx processing? */
  135. if (val + 1 == virtqueue_get_vring_size(rx_vq))
  136. restart_rx = true;
  137. }
  138. added = true;
  139. }
  140. if (added)
  141. virtqueue_kick(vq);
  142. out:
  143. mutex_unlock(&vsock->tx_lock);
  144. if (restart_rx)
  145. queue_work(virtio_vsock_workqueue, &vsock->rx_work);
  146. }
  147. static int
  148. virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
  149. {
  150. struct virtio_vsock *vsock;
  151. int len = pkt->len;
  152. rcu_read_lock();
  153. vsock = rcu_dereference(the_virtio_vsock);
  154. if (!vsock) {
  155. virtio_transport_free_pkt(pkt);
  156. len = -ENODEV;
  157. goto out_rcu;
  158. }
  159. if (le64_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid) {
  160. len = virtio_transport_send_pkt_loopback(vsock, pkt);
  161. goto out_rcu;
  162. }
  163. if (pkt->reply)
  164. atomic_inc(&vsock->queued_replies);
  165. spin_lock_bh(&vsock->send_pkt_list_lock);
  166. list_add_tail(&pkt->list, &vsock->send_pkt_list);
  167. spin_unlock_bh(&vsock->send_pkt_list_lock);
  168. queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
  169. out_rcu:
  170. rcu_read_unlock();
  171. return len;
  172. }
  173. static int
  174. virtio_transport_cancel_pkt(struct vsock_sock *vsk)
  175. {
  176. struct virtio_vsock *vsock;
  177. struct virtio_vsock_pkt *pkt, *n;
  178. int cnt = 0, ret;
  179. LIST_HEAD(freeme);
  180. rcu_read_lock();
  181. vsock = rcu_dereference(the_virtio_vsock);
  182. if (!vsock) {
  183. ret = -ENODEV;
  184. goto out_rcu;
  185. }
  186. spin_lock_bh(&vsock->send_pkt_list_lock);
  187. list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
  188. if (pkt->vsk != vsk)
  189. continue;
  190. list_move(&pkt->list, &freeme);
  191. }
  192. spin_unlock_bh(&vsock->send_pkt_list_lock);
  193. list_for_each_entry_safe(pkt, n, &freeme, list) {
  194. if (pkt->reply)
  195. cnt++;
  196. list_del(&pkt->list);
  197. virtio_transport_free_pkt(pkt);
  198. }
  199. if (cnt) {
  200. struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
  201. int new_cnt;
  202. new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
  203. if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
  204. new_cnt < virtqueue_get_vring_size(rx_vq))
  205. queue_work(virtio_vsock_workqueue, &vsock->rx_work);
  206. }
  207. ret = 0;
  208. out_rcu:
  209. rcu_read_unlock();
  210. return ret;
  211. }
  212. static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
  213. {
  214. int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
  215. struct virtio_vsock_pkt *pkt;
  216. struct scatterlist hdr, buf, *sgs[2];
  217. struct virtqueue *vq;
  218. int ret;
  219. vq = vsock->vqs[VSOCK_VQ_RX];
  220. do {
  221. pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
  222. if (!pkt)
  223. break;
  224. pkt->buf = kmalloc(buf_len, GFP_KERNEL);
  225. if (!pkt->buf) {
  226. virtio_transport_free_pkt(pkt);
  227. break;
  228. }
  229. pkt->len = buf_len;
  230. sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
  231. sgs[0] = &hdr;
  232. sg_init_one(&buf, pkt->buf, buf_len);
  233. sgs[1] = &buf;
  234. ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
  235. if (ret) {
  236. virtio_transport_free_pkt(pkt);
  237. break;
  238. }
  239. vsock->rx_buf_nr++;
  240. } while (vq->num_free);
  241. if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
  242. vsock->rx_buf_max_nr = vsock->rx_buf_nr;
  243. virtqueue_kick(vq);
  244. }
  245. static void virtio_transport_tx_work(struct work_struct *work)
  246. {
  247. struct virtio_vsock *vsock =
  248. container_of(work, struct virtio_vsock, tx_work);
  249. struct virtqueue *vq;
  250. bool added = false;
  251. vq = vsock->vqs[VSOCK_VQ_TX];
  252. mutex_lock(&vsock->tx_lock);
  253. if (!vsock->tx_run)
  254. goto out;
  255. do {
  256. struct virtio_vsock_pkt *pkt;
  257. unsigned int len;
  258. virtqueue_disable_cb(vq);
  259. while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
  260. virtio_transport_free_pkt(pkt);
  261. added = true;
  262. }
  263. } while (!virtqueue_enable_cb(vq));
  264. out:
  265. mutex_unlock(&vsock->tx_lock);
  266. if (added)
  267. queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
  268. }
  269. /* Is there space left for replies to rx packets? */
  270. static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
  271. {
  272. struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
  273. int val;
  274. smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
  275. val = atomic_read(&vsock->queued_replies);
  276. return val < virtqueue_get_vring_size(vq);
  277. }
  278. /* event_lock must be held */
  279. static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
  280. struct virtio_vsock_event *event)
  281. {
  282. struct scatterlist sg;
  283. struct virtqueue *vq;
  284. vq = vsock->vqs[VSOCK_VQ_EVENT];
  285. sg_init_one(&sg, event, sizeof(*event));
  286. return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
  287. }
  288. /* event_lock must be held */
  289. static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
  290. {
  291. size_t i;
  292. for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
  293. struct virtio_vsock_event *event = &vsock->event_list[i];
  294. virtio_vsock_event_fill_one(vsock, event);
  295. }
  296. virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
  297. }
  298. static void virtio_vsock_reset_sock(struct sock *sk)
  299. {
  300. lock_sock(sk);
  301. sk->sk_state = TCP_CLOSE;
  302. sk->sk_err = ECONNRESET;
  303. sk->sk_error_report(sk);
  304. release_sock(sk);
  305. }
  306. static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
  307. {
  308. struct virtio_device *vdev = vsock->vdev;
  309. __le64 guest_cid;
  310. vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
  311. &guest_cid, sizeof(guest_cid));
  312. vsock->guest_cid = le64_to_cpu(guest_cid);
  313. }
  314. /* event_lock must be held */
  315. static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
  316. struct virtio_vsock_event *event)
  317. {
  318. switch (le32_to_cpu(event->id)) {
  319. case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
  320. virtio_vsock_update_guest_cid(vsock);
  321. vsock_for_each_connected_socket(virtio_vsock_reset_sock);
  322. break;
  323. }
  324. }
  325. static void virtio_transport_event_work(struct work_struct *work)
  326. {
  327. struct virtio_vsock *vsock =
  328. container_of(work, struct virtio_vsock, event_work);
  329. struct virtqueue *vq;
  330. vq = vsock->vqs[VSOCK_VQ_EVENT];
  331. mutex_lock(&vsock->event_lock);
  332. if (!vsock->event_run)
  333. goto out;
  334. do {
  335. struct virtio_vsock_event *event;
  336. unsigned int len;
  337. virtqueue_disable_cb(vq);
  338. while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
  339. if (len == sizeof(*event))
  340. virtio_vsock_event_handle(vsock, event);
  341. virtio_vsock_event_fill_one(vsock, event);
  342. }
  343. } while (!virtqueue_enable_cb(vq));
  344. virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
  345. out:
  346. mutex_unlock(&vsock->event_lock);
  347. }
  348. static void virtio_vsock_event_done(struct virtqueue *vq)
  349. {
  350. struct virtio_vsock *vsock = vq->vdev->priv;
  351. if (!vsock)
  352. return;
  353. queue_work(virtio_vsock_workqueue, &vsock->event_work);
  354. }
  355. static void virtio_vsock_tx_done(struct virtqueue *vq)
  356. {
  357. struct virtio_vsock *vsock = vq->vdev->priv;
  358. if (!vsock)
  359. return;
  360. queue_work(virtio_vsock_workqueue, &vsock->tx_work);
  361. }
  362. static void virtio_vsock_rx_done(struct virtqueue *vq)
  363. {
  364. struct virtio_vsock *vsock = vq->vdev->priv;
  365. if (!vsock)
  366. return;
  367. queue_work(virtio_vsock_workqueue, &vsock->rx_work);
  368. }
  369. static struct virtio_transport virtio_transport = {
  370. .transport = {
  371. .get_local_cid = virtio_transport_get_local_cid,
  372. .init = virtio_transport_do_socket_init,
  373. .destruct = virtio_transport_destruct,
  374. .release = virtio_transport_release,
  375. .connect = virtio_transport_connect,
  376. .shutdown = virtio_transport_shutdown,
  377. .cancel_pkt = virtio_transport_cancel_pkt,
  378. .dgram_bind = virtio_transport_dgram_bind,
  379. .dgram_dequeue = virtio_transport_dgram_dequeue,
  380. .dgram_enqueue = virtio_transport_dgram_enqueue,
  381. .dgram_allow = virtio_transport_dgram_allow,
  382. .stream_dequeue = virtio_transport_stream_dequeue,
  383. .stream_enqueue = virtio_transport_stream_enqueue,
  384. .stream_has_data = virtio_transport_stream_has_data,
  385. .stream_has_space = virtio_transport_stream_has_space,
  386. .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
  387. .stream_is_active = virtio_transport_stream_is_active,
  388. .stream_allow = virtio_transport_stream_allow,
  389. .notify_poll_in = virtio_transport_notify_poll_in,
  390. .notify_poll_out = virtio_transport_notify_poll_out,
  391. .notify_recv_init = virtio_transport_notify_recv_init,
  392. .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
  393. .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
  394. .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
  395. .notify_send_init = virtio_transport_notify_send_init,
  396. .notify_send_pre_block = virtio_transport_notify_send_pre_block,
  397. .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
  398. .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
  399. .set_buffer_size = virtio_transport_set_buffer_size,
  400. .set_min_buffer_size = virtio_transport_set_min_buffer_size,
  401. .set_max_buffer_size = virtio_transport_set_max_buffer_size,
  402. .get_buffer_size = virtio_transport_get_buffer_size,
  403. .get_min_buffer_size = virtio_transport_get_min_buffer_size,
  404. .get_max_buffer_size = virtio_transport_get_max_buffer_size,
  405. },
  406. .send_pkt = virtio_transport_send_pkt,
  407. };
  408. static void virtio_transport_loopback_work(struct work_struct *work)
  409. {
  410. struct virtio_vsock *vsock =
  411. container_of(work, struct virtio_vsock, loopback_work);
  412. LIST_HEAD(pkts);
  413. spin_lock_bh(&vsock->loopback_list_lock);
  414. list_splice_init(&vsock->loopback_list, &pkts);
  415. spin_unlock_bh(&vsock->loopback_list_lock);
  416. mutex_lock(&vsock->rx_lock);
  417. if (!vsock->rx_run)
  418. goto out;
  419. while (!list_empty(&pkts)) {
  420. struct virtio_vsock_pkt *pkt;
  421. pkt = list_first_entry(&pkts, struct virtio_vsock_pkt, list);
  422. list_del_init(&pkt->list);
  423. virtio_transport_recv_pkt(&virtio_transport, pkt);
  424. }
  425. out:
  426. mutex_unlock(&vsock->rx_lock);
  427. }
  428. static void virtio_transport_rx_work(struct work_struct *work)
  429. {
  430. struct virtio_vsock *vsock =
  431. container_of(work, struct virtio_vsock, rx_work);
  432. struct virtqueue *vq;
  433. vq = vsock->vqs[VSOCK_VQ_RX];
  434. mutex_lock(&vsock->rx_lock);
  435. if (!vsock->rx_run)
  436. goto out;
  437. do {
  438. virtqueue_disable_cb(vq);
  439. for (;;) {
  440. struct virtio_vsock_pkt *pkt;
  441. unsigned int len;
  442. if (!virtio_transport_more_replies(vsock)) {
  443. /* Stop rx until the device processes already
  444. * pending replies. Leave rx virtqueue
  445. * callbacks disabled.
  446. */
  447. goto out;
  448. }
  449. pkt = virtqueue_get_buf(vq, &len);
  450. if (!pkt) {
  451. break;
  452. }
  453. vsock->rx_buf_nr--;
  454. /* Drop short/long packets */
  455. if (unlikely(len < sizeof(pkt->hdr) ||
  456. len > sizeof(pkt->hdr) + pkt->len)) {
  457. virtio_transport_free_pkt(pkt);
  458. continue;
  459. }
  460. pkt->len = len - sizeof(pkt->hdr);
  461. virtio_transport_deliver_tap_pkt(pkt);
  462. virtio_transport_recv_pkt(&virtio_transport, pkt);
  463. }
  464. } while (!virtqueue_enable_cb(vq));
  465. out:
  466. if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
  467. virtio_vsock_rx_fill(vsock);
  468. mutex_unlock(&vsock->rx_lock);
  469. }
  470. static int virtio_vsock_probe(struct virtio_device *vdev)
  471. {
  472. vq_callback_t *callbacks[] = {
  473. virtio_vsock_rx_done,
  474. virtio_vsock_tx_done,
  475. virtio_vsock_event_done,
  476. };
  477. static const char * const names[] = {
  478. "rx",
  479. "tx",
  480. "event",
  481. };
  482. struct virtio_vsock *vsock = NULL;
  483. int ret;
  484. ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
  485. if (ret)
  486. return ret;
  487. /* Only one virtio-vsock device per guest is supported */
  488. if (rcu_dereference_protected(the_virtio_vsock,
  489. lockdep_is_held(&the_virtio_vsock_mutex))) {
  490. ret = -EBUSY;
  491. goto out;
  492. }
  493. vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
  494. if (!vsock) {
  495. ret = -ENOMEM;
  496. goto out;
  497. }
  498. vsock->vdev = vdev;
  499. ret = virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX,
  500. vsock->vqs, callbacks, names,
  501. NULL);
  502. if (ret < 0)
  503. goto out;
  504. virtio_vsock_update_guest_cid(vsock);
  505. vsock->rx_buf_nr = 0;
  506. vsock->rx_buf_max_nr = 0;
  507. atomic_set(&vsock->queued_replies, 0);
  508. mutex_init(&vsock->tx_lock);
  509. mutex_init(&vsock->rx_lock);
  510. mutex_init(&vsock->event_lock);
  511. spin_lock_init(&vsock->send_pkt_list_lock);
  512. INIT_LIST_HEAD(&vsock->send_pkt_list);
  513. spin_lock_init(&vsock->loopback_list_lock);
  514. INIT_LIST_HEAD(&vsock->loopback_list);
  515. INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
  516. INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
  517. INIT_WORK(&vsock->event_work, virtio_transport_event_work);
  518. INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
  519. INIT_WORK(&vsock->loopback_work, virtio_transport_loopback_work);
  520. mutex_lock(&vsock->tx_lock);
  521. vsock->tx_run = true;
  522. mutex_unlock(&vsock->tx_lock);
  523. mutex_lock(&vsock->rx_lock);
  524. virtio_vsock_rx_fill(vsock);
  525. vsock->rx_run = true;
  526. mutex_unlock(&vsock->rx_lock);
  527. mutex_lock(&vsock->event_lock);
  528. virtio_vsock_event_fill(vsock);
  529. vsock->event_run = true;
  530. mutex_unlock(&vsock->event_lock);
  531. vdev->priv = vsock;
  532. rcu_assign_pointer(the_virtio_vsock, vsock);
  533. mutex_unlock(&the_virtio_vsock_mutex);
  534. return 0;
  535. out:
  536. kfree(vsock);
  537. mutex_unlock(&the_virtio_vsock_mutex);
  538. return ret;
  539. }
  540. static void virtio_vsock_remove(struct virtio_device *vdev)
  541. {
  542. struct virtio_vsock *vsock = vdev->priv;
  543. struct virtio_vsock_pkt *pkt;
  544. mutex_lock(&the_virtio_vsock_mutex);
  545. vdev->priv = NULL;
  546. rcu_assign_pointer(the_virtio_vsock, NULL);
  547. synchronize_rcu();
  548. flush_work(&vsock->loopback_work);
  549. flush_work(&vsock->rx_work);
  550. flush_work(&vsock->tx_work);
  551. flush_work(&vsock->event_work);
  552. flush_work(&vsock->send_pkt_work);
  553. /* Reset all connected sockets when the device disappear */
  554. vsock_for_each_connected_socket(virtio_vsock_reset_sock);
  555. /* Stop all work handlers to make sure no one is accessing the device,
  556. * so we can safely call vdev->config->reset().
  557. */
  558. mutex_lock(&vsock->rx_lock);
  559. vsock->rx_run = false;
  560. mutex_unlock(&vsock->rx_lock);
  561. mutex_lock(&vsock->tx_lock);
  562. vsock->tx_run = false;
  563. mutex_unlock(&vsock->tx_lock);
  564. mutex_lock(&vsock->event_lock);
  565. vsock->event_run = false;
  566. mutex_unlock(&vsock->event_lock);
  567. /* Flush all device writes and interrupts, device will not use any
  568. * more buffers.
  569. */
  570. vdev->config->reset(vdev);
  571. mutex_lock(&vsock->rx_lock);
  572. while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
  573. virtio_transport_free_pkt(pkt);
  574. mutex_unlock(&vsock->rx_lock);
  575. mutex_lock(&vsock->tx_lock);
  576. while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
  577. virtio_transport_free_pkt(pkt);
  578. mutex_unlock(&vsock->tx_lock);
  579. spin_lock_bh(&vsock->send_pkt_list_lock);
  580. while (!list_empty(&vsock->send_pkt_list)) {
  581. pkt = list_first_entry(&vsock->send_pkt_list,
  582. struct virtio_vsock_pkt, list);
  583. list_del(&pkt->list);
  584. virtio_transport_free_pkt(pkt);
  585. }
  586. spin_unlock_bh(&vsock->send_pkt_list_lock);
  587. spin_lock_bh(&vsock->loopback_list_lock);
  588. while (!list_empty(&vsock->loopback_list)) {
  589. pkt = list_first_entry(&vsock->loopback_list,
  590. struct virtio_vsock_pkt, list);
  591. list_del(&pkt->list);
  592. virtio_transport_free_pkt(pkt);
  593. }
  594. spin_unlock_bh(&vsock->loopback_list_lock);
  595. /* Delete virtqueues and flush outstanding callbacks if any */
  596. vdev->config->del_vqs(vdev);
  597. mutex_unlock(&the_virtio_vsock_mutex);
  598. kfree(vsock);
  599. }
  600. static struct virtio_device_id id_table[] = {
  601. { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
  602. { 0 },
  603. };
  604. static unsigned int features[] = {
  605. };
  606. static struct virtio_driver virtio_vsock_driver = {
  607. .feature_table = features,
  608. .feature_table_size = ARRAY_SIZE(features),
  609. .driver.name = KBUILD_MODNAME,
  610. .driver.owner = THIS_MODULE,
  611. .id_table = id_table,
  612. .probe = virtio_vsock_probe,
  613. .remove = virtio_vsock_remove,
  614. };
  615. static int __init virtio_vsock_init(void)
  616. {
  617. int ret;
  618. virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
  619. if (!virtio_vsock_workqueue)
  620. return -ENOMEM;
  621. ret = vsock_core_init(&virtio_transport.transport);
  622. if (ret)
  623. goto out_wq;
  624. ret = register_virtio_driver(&virtio_vsock_driver);
  625. if (ret)
  626. goto out_vci;
  627. return 0;
  628. out_vci:
  629. vsock_core_exit();
  630. out_wq:
  631. destroy_workqueue(virtio_vsock_workqueue);
  632. return ret;
  633. }
  634. static void __exit virtio_vsock_exit(void)
  635. {
  636. unregister_virtio_driver(&virtio_vsock_driver);
  637. vsock_core_exit();
  638. destroy_workqueue(virtio_vsock_workqueue);
  639. }
  640. module_init(virtio_vsock_init);
  641. module_exit(virtio_vsock_exit);
  642. MODULE_LICENSE("GPL v2");
  643. MODULE_AUTHOR("Asias He");
  644. MODULE_DESCRIPTION("virtio transport for vsock");
  645. MODULE_DEVICE_TABLE(virtio, id_table);