Lines Matching defs:vsock
3 * virtio transport for vsock
10 * early virtio-vsock proof-of-concept bits.
70 struct virtio_vsock *vsock;
74 vsock = rcu_dereference(the_virtio_vsock);
75 if (!vsock) {
80 ret = vsock->guest_cid;
89 struct virtio_vsock *vsock =
95 mutex_lock(&vsock->tx_lock);
97 if (!vsock->tx_run)
100 vq = vsock->vqs[VSOCK_VQ_TX];
108 spin_lock_bh(&vsock->send_pkt_list_lock);
109 if (list_empty(&vsock->send_pkt_list)) {
110 spin_unlock_bh(&vsock->send_pkt_list_lock);
114 pkt = list_first_entry(&vsock->send_pkt_list,
117 spin_unlock_bh(&vsock->send_pkt_list_lock);
135 spin_lock_bh(&vsock->send_pkt_list_lock);
136 list_add(&pkt->list, &vsock->send_pkt_list);
137 spin_unlock_bh(&vsock->send_pkt_list_lock);
142 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
145 val = atomic_dec_return(&vsock->queued_replies);
159 mutex_unlock(&vsock->tx_lock);
162 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
168 struct virtio_vsock *vsock;
172 vsock = rcu_dereference(the_virtio_vsock);
173 if (!vsock) {
179 if (le64_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid) {
186 atomic_inc(&vsock->queued_replies);
188 spin_lock_bh(&vsock->send_pkt_list_lock);
189 list_add_tail(&pkt->list, &vsock->send_pkt_list);
190 spin_unlock_bh(&vsock->send_pkt_list_lock);
192 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
202 struct virtio_vsock *vsock;
208 vsock = rcu_dereference(the_virtio_vsock);
209 if (!vsock) {
214 spin_lock_bh(&vsock->send_pkt_list_lock);
215 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
220 spin_unlock_bh(&vsock->send_pkt_list_lock);
230 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
233 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
236 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
246 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
254 vq = vsock->vqs[VSOCK_VQ_RX];
280 vsock->rx_buf_nr++;
282 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
283 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
289 struct virtio_vsock *vsock =
294 vq = vsock->vqs[VSOCK_VQ_TX];
295 mutex_lock(&vsock->tx_lock);
297 if (!vsock->tx_run)
312 mutex_unlock(&vsock->tx_lock);
315 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
319 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
321 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
325 val = atomic_read(&vsock->queued_replies);
331 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
337 vq = vsock->vqs[VSOCK_VQ_EVENT];
345 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
349 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
350 struct virtio_vsock_event *event = &vsock->event_list[i];
352 virtio_vsock_event_fill_one(vsock, event);
355 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
370 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
372 struct virtio_device *vdev = vsock->vdev;
377 vsock->guest_cid = le64_to_cpu(guest_cid);
381 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
386 virtio_vsock_update_guest_cid(vsock);
395 struct virtio_vsock *vsock =
399 vq = vsock->vqs[VSOCK_VQ_EVENT];
401 mutex_lock(&vsock->event_lock);
403 if (!vsock->event_run)
413 virtio_vsock_event_handle(vsock, event);
415 virtio_vsock_event_fill_one(vsock, event);
419 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
421 mutex_unlock(&vsock->event_lock);
426 struct virtio_vsock *vsock = vq->vdev->priv;
428 if (!vsock)
430 queue_work(virtio_vsock_workqueue, &vsock->event_work);
435 struct virtio_vsock *vsock = vq->vdev->priv;
437 if (!vsock)
439 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
444 struct virtio_vsock *vsock = vq->vdev->priv;
446 if (!vsock)
448 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
495 struct virtio_vsock *vsock =
499 vq = vsock->vqs[VSOCK_VQ_RX];
501 mutex_lock(&vsock->rx_lock);
503 if (!vsock->rx_run)
512 if (!virtio_transport_more_replies(vsock)) {
525 vsock->rx_buf_nr--;
541 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
542 virtio_vsock_rx_fill(vsock);
543 mutex_unlock(&vsock->rx_lock);
558 struct virtio_vsock *vsock = NULL;
565 /* Only one virtio-vsock device per guest is supported */
572 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
573 if (!vsock) {
578 vsock->vdev = vdev;
580 ret = virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX,
581 vsock->vqs, callbacks, names,
586 virtio_vsock_update_guest_cid(vsock);
588 vsock->rx_buf_nr = 0;
589 vsock->rx_buf_max_nr = 0;
590 atomic_set(&vsock->queued_replies, 0);
592 mutex_init(&vsock->tx_lock);
593 mutex_init(&vsock->rx_lock);
594 mutex_init(&vsock->event_lock);
595 spin_lock_init(&vsock->send_pkt_list_lock);
596 INIT_LIST_HEAD(&vsock->send_pkt_list);
597 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
598 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
599 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
600 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
602 mutex_lock(&vsock->tx_lock);
603 vsock->tx_run = true;
604 mutex_unlock(&vsock->tx_lock);
606 mutex_lock(&vsock->rx_lock);
607 virtio_vsock_rx_fill(vsock);
608 vsock->rx_run = true;
609 mutex_unlock(&vsock->rx_lock);
611 mutex_lock(&vsock->event_lock);
612 virtio_vsock_event_fill(vsock);
613 vsock->event_run = true;
614 mutex_unlock(&vsock->event_lock);
616 vdev->priv = vsock;
617 rcu_assign_pointer(the_virtio_vsock, vsock);
623 kfree(vsock);
630 struct virtio_vsock *vsock = vdev->priv;
646 mutex_lock(&vsock->rx_lock);
647 vsock->rx_run = false;
648 mutex_unlock(&vsock->rx_lock);
650 mutex_lock(&vsock->tx_lock);
651 vsock->tx_run = false;
652 mutex_unlock(&vsock->tx_lock);
654 mutex_lock(&vsock->event_lock);
655 vsock->event_run = false;
656 mutex_unlock(&vsock->event_lock);
663 mutex_lock(&vsock->rx_lock);
664 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
666 mutex_unlock(&vsock->rx_lock);
668 mutex_lock(&vsock->tx_lock);
669 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
671 mutex_unlock(&vsock->tx_lock);
673 spin_lock_bh(&vsock->send_pkt_list_lock);
674 while (!list_empty(&vsock->send_pkt_list)) {
675 pkt = list_first_entry(&vsock->send_pkt_list,
680 spin_unlock_bh(&vsock->send_pkt_list_lock);
686 * all works before to free the vsock object to avoid use after free.
688 flush_work(&vsock->rx_work);
689 flush_work(&vsock->tx_work);
690 flush_work(&vsock->event_work);
691 flush_work(&vsock->send_pkt_work);
695 kfree(vsock);
753 MODULE_DESCRIPTION("virtio transport for vsock");