1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_VIRTIO_VSOCK_H 3 #define _LINUX_VIRTIO_VSOCK_H 4 5 #include <uapi/linux/virtio_vsock.h> 6 #include <linux/socket.h> 7 #include <net/sock.h> 8 #include <net/af_vsock.h> 9 10 #define VIRTIO_VSOCK_DEFAULT_MIN_BUF_SIZE 128 11 #define VIRTIO_VSOCK_DEFAULT_BUF_SIZE (1024 * 256) 12 #define VIRTIO_VSOCK_DEFAULT_MAX_BUF_SIZE (1024 * 256) 13 #define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE (1024 * 4) 14 #define VIRTIO_VSOCK_MAX_BUF_SIZE 0xFFFFFFFFUL 15 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE virtio_transport_max_vsock_pkt_buf_size 16 extern uint virtio_transport_max_vsock_pkt_buf_size; 17 18 enum { 19 VSOCK_VQ_RX = 0, /* for host to guest data */ 20 VSOCK_VQ_TX = 1, /* for guest to host data */ 21 VSOCK_VQ_EVENT = 2, 22 VSOCK_VQ_MAX = 3, 23 }; 24 25 /* Per-socket state (accessed via vsk->trans) */ 26 struct virtio_vsock_sock { 27 struct vsock_sock *vsk; 28 29 /* Protected by lock_sock(sk_vsock(trans->vsk)) */ 30 u32 buf_size; 31 u32 buf_size_min; 32 u32 buf_size_max; 33 34 spinlock_t tx_lock; 35 spinlock_t rx_lock; 36 37 /* Protected by tx_lock */ 38 u32 tx_cnt; 39 u32 peer_fwd_cnt; 40 u32 peer_buf_alloc; 41 42 /* Protected by rx_lock */ 43 u32 fwd_cnt; 44 u32 last_fwd_cnt; 45 u32 rx_bytes; 46 u32 buf_alloc; 47 struct list_head rx_queue; 48 }; 49 50 struct virtio_vsock_pkt { 51 struct virtio_vsock_hdr hdr; 52 struct list_head list; 53 /* socket refcnt not held, only use for cancellation */ 54 struct vsock_sock *vsk; 55 void *buf; 56 u32 buf_len; 57 u32 len; 58 u32 off; 59 bool reply; 60 }; 61 62 struct virtio_vsock_pkt_info { 63 u32 remote_cid, remote_port; 64 struct vsock_sock *vsk; 65 struct msghdr *msg; 66 u32 pkt_len; 67 u16 type; 68 u16 op; 69 u32 flags; 70 bool reply; 71 }; 72 73 struct virtio_transport { 74 /* This must be the first field */ 75 struct vsock_transport transport; 76 77 /* Takes ownership of the packet */ 78 int (*send_pkt)(struct virtio_vsock_pkt *pkt); 79 }; 80 81 ssize_t 82 virtio_transport_stream_dequeue(struct vsock_sock *vsk, 83 struct msghdr *msg, 84 size_t len, 85 int type); 86 int 87 virtio_transport_dgram_dequeue(struct vsock_sock *vsk, 88 struct msghdr *msg, 89 size_t len, int flags); 90 91 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk); 92 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk); 93 94 int virtio_transport_do_socket_init(struct vsock_sock *vsk, 95 struct vsock_sock *psk); 96 u64 virtio_transport_get_buffer_size(struct vsock_sock *vsk); 97 u64 virtio_transport_get_min_buffer_size(struct vsock_sock *vsk); 98 u64 virtio_transport_get_max_buffer_size(struct vsock_sock *vsk); 99 void virtio_transport_set_buffer_size(struct vsock_sock *vsk, u64 val); 100 void virtio_transport_set_min_buffer_size(struct vsock_sock *vsk, u64 val); 101 void virtio_transport_set_max_buffer_size(struct vsock_sock *vs, u64 val); 102 int 103 virtio_transport_notify_poll_in(struct vsock_sock *vsk, 104 size_t target, 105 bool *data_ready_now); 106 int 107 virtio_transport_notify_poll_out(struct vsock_sock *vsk, 108 size_t target, 109 bool *space_available_now); 110 111 int virtio_transport_notify_recv_init(struct vsock_sock *vsk, 112 size_t target, struct vsock_transport_recv_notify_data *data); 113 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk, 114 size_t target, struct vsock_transport_recv_notify_data *data); 115 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk, 116 size_t target, struct vsock_transport_recv_notify_data *data); 117 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk, 118 size_t target, ssize_t copied, bool data_read, 119 struct vsock_transport_recv_notify_data *data); 120 int virtio_transport_notify_send_init(struct vsock_sock *vsk, 121 struct vsock_transport_send_notify_data *data); 122 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk, 123 struct vsock_transport_send_notify_data *data); 124 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk, 125 struct vsock_transport_send_notify_data *data); 126 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk, 127 ssize_t written, struct vsock_transport_send_notify_data *data); 128 129 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk); 130 bool virtio_transport_stream_is_active(struct vsock_sock *vsk); 131 bool virtio_transport_stream_allow(u32 cid, u32 port); 132 int virtio_transport_dgram_bind(struct vsock_sock *vsk, 133 struct sockaddr_vm *addr); 134 bool virtio_transport_dgram_allow(u32 cid, u32 port); 135 136 int virtio_transport_connect(struct vsock_sock *vsk); 137 138 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode); 139 140 void virtio_transport_release(struct vsock_sock *vsk); 141 142 ssize_t 143 virtio_transport_stream_enqueue(struct vsock_sock *vsk, 144 struct msghdr *msg, 145 size_t len); 146 int 147 virtio_transport_dgram_enqueue(struct vsock_sock *vsk, 148 struct sockaddr_vm *remote_addr, 149 struct msghdr *msg, 150 size_t len); 151 152 void virtio_transport_destruct(struct vsock_sock *vsk); 153 154 void virtio_transport_recv_pkt(struct virtio_transport *t, 155 struct virtio_vsock_pkt *pkt); 156 void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt); 157 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct virtio_vsock_pkt *pkt); 158 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted); 159 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit); 160 void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt); 161 162 #endif /* _LINUX_VIRTIO_VSOCK_H */ 163