linux/net/vmw_vsock/vsock_loopback.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/* loopback transport for vsock using virtio_transport_common APIs
   3 *
   4 * Copyright (C) 2013-2019 Red Hat, Inc.
   5 * Authors: Asias He <asias@redhat.com>
   6 *          Stefan Hajnoczi <stefanha@redhat.com>
   7 *          Stefano Garzarella <sgarzare@redhat.com>
   8 *
   9 */
  10#include <linux/spinlock.h>
  11#include <linux/module.h>
  12#include <linux/list.h>
  13#include <linux/virtio_vsock.h>
  14
  15struct vsock_loopback {
  16        struct workqueue_struct *workqueue;
  17
  18        spinlock_t pkt_list_lock; /* protects pkt_list */
  19        struct list_head pkt_list;
  20        struct work_struct pkt_work;
  21};
  22
  23static struct vsock_loopback the_vsock_loopback;
  24
  25static u32 vsock_loopback_get_local_cid(void)
  26{
  27        return VMADDR_CID_LOCAL;
  28}
  29
  30static int vsock_loopback_send_pkt(struct virtio_vsock_pkt *pkt)
  31{
  32        struct vsock_loopback *vsock = &the_vsock_loopback;
  33        int len = pkt->len;
  34
  35        spin_lock_bh(&vsock->pkt_list_lock);
  36        list_add_tail(&pkt->list, &vsock->pkt_list);
  37        spin_unlock_bh(&vsock->pkt_list_lock);
  38
  39        queue_work(vsock->workqueue, &vsock->pkt_work);
  40
  41        return len;
  42}
  43
  44static int vsock_loopback_cancel_pkt(struct vsock_sock *vsk)
  45{
  46        struct vsock_loopback *vsock = &the_vsock_loopback;
  47        struct virtio_vsock_pkt *pkt, *n;
  48        LIST_HEAD(freeme);
  49
  50        spin_lock_bh(&vsock->pkt_list_lock);
  51        list_for_each_entry_safe(pkt, n, &vsock->pkt_list, list) {
  52                if (pkt->vsk != vsk)
  53                        continue;
  54                list_move(&pkt->list, &freeme);
  55        }
  56        spin_unlock_bh(&vsock->pkt_list_lock);
  57
  58        list_for_each_entry_safe(pkt, n, &freeme, list) {
  59                list_del(&pkt->list);
  60                virtio_transport_free_pkt(pkt);
  61        }
  62
  63        return 0;
  64}
  65
  66static bool vsock_loopback_seqpacket_allow(u32 remote_cid);
  67
  68static struct virtio_transport loopback_transport = {
  69        .transport = {
  70                .module                   = THIS_MODULE,
  71
  72                .get_local_cid            = vsock_loopback_get_local_cid,
  73
  74                .init                     = virtio_transport_do_socket_init,
  75                .destruct                 = virtio_transport_destruct,
  76                .release                  = virtio_transport_release,
  77                .connect                  = virtio_transport_connect,
  78                .shutdown                 = virtio_transport_shutdown,
  79                .cancel_pkt               = vsock_loopback_cancel_pkt,
  80
  81                .dgram_bind               = virtio_transport_dgram_bind,
  82                .dgram_dequeue            = virtio_transport_dgram_dequeue,
  83                .dgram_enqueue            = virtio_transport_dgram_enqueue,
  84                .dgram_allow              = virtio_transport_dgram_allow,
  85
  86                .stream_dequeue           = virtio_transport_stream_dequeue,
  87                .stream_enqueue           = virtio_transport_stream_enqueue,
  88                .stream_has_data          = virtio_transport_stream_has_data,
  89                .stream_has_space         = virtio_transport_stream_has_space,
  90                .stream_rcvhiwat          = virtio_transport_stream_rcvhiwat,
  91                .stream_is_active         = virtio_transport_stream_is_active,
  92                .stream_allow             = virtio_transport_stream_allow,
  93
  94                .seqpacket_dequeue        = virtio_transport_seqpacket_dequeue,
  95                .seqpacket_enqueue        = virtio_transport_seqpacket_enqueue,
  96                .seqpacket_allow          = vsock_loopback_seqpacket_allow,
  97                .seqpacket_has_data       = virtio_transport_seqpacket_has_data,
  98
  99                .notify_poll_in           = virtio_transport_notify_poll_in,
 100                .notify_poll_out          = virtio_transport_notify_poll_out,
 101                .notify_recv_init         = virtio_transport_notify_recv_init,
 102                .notify_recv_pre_block    = virtio_transport_notify_recv_pre_block,
 103                .notify_recv_pre_dequeue  = virtio_transport_notify_recv_pre_dequeue,
 104                .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
 105                .notify_send_init         = virtio_transport_notify_send_init,
 106                .notify_send_pre_block    = virtio_transport_notify_send_pre_block,
 107                .notify_send_pre_enqueue  = virtio_transport_notify_send_pre_enqueue,
 108                .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
 109                .notify_buffer_size       = virtio_transport_notify_buffer_size,
 110        },
 111
 112        .send_pkt = vsock_loopback_send_pkt,
 113};
 114
 115static bool vsock_loopback_seqpacket_allow(u32 remote_cid)
 116{
 117        return true;
 118}
 119
 120static void vsock_loopback_work(struct work_struct *work)
 121{
 122        struct vsock_loopback *vsock =
 123                container_of(work, struct vsock_loopback, pkt_work);
 124        LIST_HEAD(pkts);
 125
 126        spin_lock_bh(&vsock->pkt_list_lock);
 127        list_splice_init(&vsock->pkt_list, &pkts);
 128        spin_unlock_bh(&vsock->pkt_list_lock);
 129
 130        while (!list_empty(&pkts)) {
 131                struct virtio_vsock_pkt *pkt;
 132
 133                pkt = list_first_entry(&pkts, struct virtio_vsock_pkt, list);
 134                list_del_init(&pkt->list);
 135
 136                virtio_transport_deliver_tap_pkt(pkt);
 137                virtio_transport_recv_pkt(&loopback_transport, pkt);
 138        }
 139}
 140
 141static int __init vsock_loopback_init(void)
 142{
 143        struct vsock_loopback *vsock = &the_vsock_loopback;
 144        int ret;
 145
 146        vsock->workqueue = alloc_workqueue("vsock-loopback", 0, 0);
 147        if (!vsock->workqueue)
 148                return -ENOMEM;
 149
 150        spin_lock_init(&vsock->pkt_list_lock);
 151        INIT_LIST_HEAD(&vsock->pkt_list);
 152        INIT_WORK(&vsock->pkt_work, vsock_loopback_work);
 153
 154        ret = vsock_core_register(&loopback_transport.transport,
 155                                  VSOCK_TRANSPORT_F_LOCAL);
 156        if (ret)
 157                goto out_wq;
 158
 159        return 0;
 160
 161out_wq:
 162        destroy_workqueue(vsock->workqueue);
 163        return ret;
 164}
 165
 166static void __exit vsock_loopback_exit(void)
 167{
 168        struct vsock_loopback *vsock = &the_vsock_loopback;
 169        struct virtio_vsock_pkt *pkt;
 170
 171        vsock_core_unregister(&loopback_transport.transport);
 172
 173        flush_work(&vsock->pkt_work);
 174
 175        spin_lock_bh(&vsock->pkt_list_lock);
 176        while (!list_empty(&vsock->pkt_list)) {
 177                pkt = list_first_entry(&vsock->pkt_list,
 178                                       struct virtio_vsock_pkt, list);
 179                list_del(&pkt->list);
 180                virtio_transport_free_pkt(pkt);
 181        }
 182        spin_unlock_bh(&vsock->pkt_list_lock);
 183
 184        destroy_workqueue(vsock->workqueue);
 185}
 186
 187module_init(vsock_loopback_init);
 188module_exit(vsock_loopback_exit);
 189MODULE_LICENSE("GPL v2");
 190MODULE_AUTHOR("Stefano Garzarella <sgarzare@redhat.com>");
 191MODULE_DESCRIPTION("loopback transport for vsock");
 192MODULE_ALIAS_NETPROTO(PF_VSOCK);
 193