Asias He | 80a19e3 | 2015-12-02 14:44:00 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This header, excluding the #ifdef __KERNEL__ part, is BSD licensed so |
| 3 | * anyone can use the definitions to implement compatible drivers/servers: |
| 4 | * |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. Neither the name of IBM nor the names of its contributors |
| 15 | * may be used to endorse or promote products derived from this software |
| 16 | * without specific prior written permission. |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' |
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | * |
| 29 | * Copyright (C) Red Hat, Inc., 2013-2015 |
| 30 | * Copyright (C) Asias He <asias@redhat.com>, 2013 |
| 31 | * Copyright (C) Stefan Hajnoczi <stefanha@redhat.com>, 2015 |
| 32 | */ |
| 33 | |
| 34 | #ifndef _LINUX_VIRTIO_VSOCK_H |
| 35 | #define _LINUX_VIRTIO_VSOCK_H |
| 36 | |
| 37 | #include <uapi/linux/virtio_vsock.h> |
| 38 | #include <linux/socket.h> |
| 39 | #include <net/sock.h> |
| 40 | |
| 41 | #define VIRTIO_VSOCK_DEFAULT_MIN_BUF_SIZE 128 |
| 42 | #define VIRTIO_VSOCK_DEFAULT_BUF_SIZE (1024 * 256) |
| 43 | #define VIRTIO_VSOCK_DEFAULT_MAX_BUF_SIZE (1024 * 256) |
| 44 | #define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE (1024 * 4) |
| 45 | #define VIRTIO_VSOCK_MAX_BUF_SIZE 0xFFFFFFFFUL |
| 46 | #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64) |
| 47 | #define VIRTIO_VSOCK_MAX_TX_BUF_SIZE (1024 * 1024 * 16) |
| 48 | #define VIRTIO_VSOCK_MAX_DGRAM_SIZE (1024 * 64) |
| 49 | |
| 50 | struct vsock_transport_recv_notify_data; |
| 51 | struct vsock_transport_send_notify_data; |
| 52 | struct sockaddr_vm; |
| 53 | struct vsock_sock; |
| 54 | |
| 55 | enum { |
| 56 | VSOCK_VQ_CTRL = 0, |
| 57 | VSOCK_VQ_RX = 1, /* for host to guest data */ |
| 58 | VSOCK_VQ_TX = 2, /* for guest to host data */ |
| 59 | VSOCK_VQ_MAX = 3, |
| 60 | }; |
| 61 | |
| 62 | /* virtio transport socket state */ |
| 63 | struct virtio_transport { |
| 64 | struct virtio_transport_pkt_ops *ops; |
| 65 | struct vsock_sock *vsk; |
| 66 | |
| 67 | u32 buf_size; |
| 68 | u32 buf_size_min; |
| 69 | u32 buf_size_max; |
| 70 | |
| 71 | struct mutex tx_lock; |
| 72 | struct mutex rx_lock; |
| 73 | |
| 74 | struct list_head rx_queue; |
| 75 | u32 rx_bytes; |
| 76 | |
| 77 | /* Protected by trans->tx_lock */ |
| 78 | u32 tx_cnt; |
| 79 | u32 buf_alloc; |
| 80 | u32 peer_fwd_cnt; |
| 81 | u32 peer_buf_alloc; |
| 82 | /* Protected by trans->rx_lock */ |
| 83 | u32 fwd_cnt; |
| 84 | |
| 85 | /* Protected by sk_lock */ |
| 86 | u16 dgram_id; |
| 87 | struct list_head incomplete_dgrams; /* dgram fragments */ |
| 88 | }; |
| 89 | |
| 90 | struct virtio_vsock_pkt { |
| 91 | struct virtio_vsock_hdr hdr; |
| 92 | struct virtio_transport *trans; |
| 93 | struct work_struct work; |
| 94 | struct list_head list; |
| 95 | void *buf; |
| 96 | u32 len; |
| 97 | u32 off; |
| 98 | }; |
| 99 | |
| 100 | struct virtio_vsock_pkt_info { |
| 101 | u32 remote_cid, remote_port; |
| 102 | struct msghdr *msg; |
| 103 | u32 pkt_len; |
| 104 | u16 type; |
| 105 | u16 op; |
| 106 | u32 flags; |
| 107 | u16 dgram_id; |
| 108 | u16 dgram_len; |
| 109 | }; |
| 110 | |
| 111 | struct virtio_transport_pkt_ops { |
| 112 | int (*send_pkt)(struct vsock_sock *vsk, |
| 113 | struct virtio_vsock_pkt_info *info); |
| 114 | }; |
| 115 | |
| 116 | void virtio_vsock_dumppkt(const char *func, |
| 117 | const struct virtio_vsock_pkt *pkt); |
| 118 | |
| 119 | struct sock * |
| 120 | virtio_transport_get_pending(struct sock *listener, |
| 121 | struct virtio_vsock_pkt *pkt); |
| 122 | struct virtio_vsock_pkt * |
| 123 | virtio_transport_alloc_pkt(struct vsock_sock *vsk, |
| 124 | struct virtio_vsock_pkt_info *info, |
| 125 | size_t len, |
| 126 | u32 src_cid, |
| 127 | u32 src_port, |
| 128 | u32 dst_cid, |
| 129 | u32 dst_port); |
| 130 | ssize_t |
| 131 | virtio_transport_stream_dequeue(struct vsock_sock *vsk, |
| 132 | struct msghdr *msg, |
| 133 | size_t len, |
| 134 | int type); |
| 135 | int |
| 136 | virtio_transport_dgram_dequeue(struct vsock_sock *vsk, |
| 137 | struct msghdr *msg, |
| 138 | size_t len, int flags); |
| 139 | |
| 140 | s64 virtio_transport_stream_has_data(struct vsock_sock *vsk); |
| 141 | s64 virtio_transport_stream_has_space(struct vsock_sock *vsk); |
| 142 | |
| 143 | int virtio_transport_do_socket_init(struct vsock_sock *vsk, |
| 144 | struct vsock_sock *psk); |
| 145 | u64 virtio_transport_get_buffer_size(struct vsock_sock *vsk); |
| 146 | u64 virtio_transport_get_min_buffer_size(struct vsock_sock *vsk); |
| 147 | u64 virtio_transport_get_max_buffer_size(struct vsock_sock *vsk); |
| 148 | void virtio_transport_set_buffer_size(struct vsock_sock *vsk, u64 val); |
| 149 | void virtio_transport_set_min_buffer_size(struct vsock_sock *vsk, u64 val); |
| 150 | void virtio_transport_set_max_buffer_size(struct vsock_sock *vs, u64 val); |
| 151 | int |
| 152 | virtio_transport_notify_poll_in(struct vsock_sock *vsk, |
| 153 | size_t target, |
| 154 | bool *data_ready_now); |
| 155 | int |
| 156 | virtio_transport_notify_poll_out(struct vsock_sock *vsk, |
| 157 | size_t target, |
| 158 | bool *space_available_now); |
| 159 | |
| 160 | int virtio_transport_notify_recv_init(struct vsock_sock *vsk, |
| 161 | size_t target, struct vsock_transport_recv_notify_data *data); |
| 162 | int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk, |
| 163 | size_t target, struct vsock_transport_recv_notify_data *data); |
| 164 | int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk, |
| 165 | size_t target, struct vsock_transport_recv_notify_data *data); |
| 166 | int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk, |
| 167 | size_t target, ssize_t copied, bool data_read, |
| 168 | struct vsock_transport_recv_notify_data *data); |
| 169 | int virtio_transport_notify_send_init(struct vsock_sock *vsk, |
| 170 | struct vsock_transport_send_notify_data *data); |
| 171 | int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk, |
| 172 | struct vsock_transport_send_notify_data *data); |
| 173 | int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk, |
| 174 | struct vsock_transport_send_notify_data *data); |
| 175 | int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk, |
| 176 | ssize_t written, struct vsock_transport_send_notify_data *data); |
| 177 | |
| 178 | u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk); |
| 179 | bool virtio_transport_stream_is_active(struct vsock_sock *vsk); |
| 180 | bool virtio_transport_stream_allow(u32 cid, u32 port); |
| 181 | int virtio_transport_dgram_bind(struct vsock_sock *vsk, |
| 182 | struct sockaddr_vm *addr); |
| 183 | bool virtio_transport_dgram_allow(u32 cid, u32 port); |
| 184 | |
| 185 | int virtio_transport_connect(struct vsock_sock *vsk); |
| 186 | |
| 187 | int virtio_transport_shutdown(struct vsock_sock *vsk, int mode); |
| 188 | |
| 189 | void virtio_transport_release(struct vsock_sock *vsk); |
| 190 | |
| 191 | ssize_t |
| 192 | virtio_transport_stream_enqueue(struct vsock_sock *vsk, |
| 193 | struct msghdr *msg, |
| 194 | size_t len); |
| 195 | int |
| 196 | virtio_transport_dgram_enqueue(struct vsock_sock *vsk, |
| 197 | struct sockaddr_vm *remote_addr, |
| 198 | struct msghdr *msg, |
| 199 | size_t len); |
| 200 | |
| 201 | void virtio_transport_destruct(struct vsock_sock *vsk); |
| 202 | |
| 203 | void virtio_transport_recv_pkt(struct virtio_vsock_pkt *pkt); |
| 204 | void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt); |
| 205 | void virtio_transport_inc_tx_pkt(struct virtio_vsock_pkt *pkt); |
| 206 | void virtio_transport_dec_tx_pkt(struct virtio_vsock_pkt *pkt); |
| 207 | u32 virtio_transport_get_credit(struct virtio_transport *trans, u32 wanted); |
| 208 | void virtio_transport_put_credit(struct virtio_transport *trans, u32 credit); |
| 209 | #endif /* _LINUX_VIRTIO_VSOCK_H */ |