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