xref: /linux-6.15/include/linux/virtio_vsock.h (revision f5ae2ea6)
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 };
40 
41 struct virtio_vsock_pkt {
42 	struct virtio_vsock_hdr	hdr;
43 	struct list_head list;
44 	/* socket refcnt not held, only use for cancellation */
45 	struct vsock_sock *vsk;
46 	void *buf;
47 	u32 buf_len;
48 	u32 len;
49 	u32 off;
50 	bool reply;
51 };
52 
53 struct virtio_vsock_pkt_info {
54 	u32 remote_cid, remote_port;
55 	struct vsock_sock *vsk;
56 	struct msghdr *msg;
57 	u32 pkt_len;
58 	u16 type;
59 	u16 op;
60 	u32 flags;
61 	bool reply;
62 };
63 
64 struct virtio_transport {
65 	/* This must be the first field */
66 	struct vsock_transport transport;
67 
68 	/* Takes ownership of the packet */
69 	int (*send_pkt)(struct virtio_vsock_pkt *pkt);
70 };
71 
72 ssize_t
73 virtio_transport_stream_dequeue(struct vsock_sock *vsk,
74 				struct msghdr *msg,
75 				size_t len,
76 				int type);
77 int
78 virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
79 			       struct msghdr *msg,
80 			       size_t len, int flags);
81 
82 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
83 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
84 
85 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
86 				 struct vsock_sock *psk);
87 int
88 virtio_transport_notify_poll_in(struct vsock_sock *vsk,
89 				size_t target,
90 				bool *data_ready_now);
91 int
92 virtio_transport_notify_poll_out(struct vsock_sock *vsk,
93 				 size_t target,
94 				 bool *space_available_now);
95 
96 int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
97 	size_t target, struct vsock_transport_recv_notify_data *data);
98 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
99 	size_t target, struct vsock_transport_recv_notify_data *data);
100 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
101 	size_t target, struct vsock_transport_recv_notify_data *data);
102 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
103 	size_t target, ssize_t copied, bool data_read,
104 	struct vsock_transport_recv_notify_data *data);
105 int virtio_transport_notify_send_init(struct vsock_sock *vsk,
106 	struct vsock_transport_send_notify_data *data);
107 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
108 	struct vsock_transport_send_notify_data *data);
109 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
110 	struct vsock_transport_send_notify_data *data);
111 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
112 	ssize_t written, struct vsock_transport_send_notify_data *data);
113 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val);
114 
115 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk);
116 bool virtio_transport_stream_is_active(struct vsock_sock *vsk);
117 bool virtio_transport_stream_allow(u32 cid, u32 port);
118 int virtio_transport_dgram_bind(struct vsock_sock *vsk,
119 				struct sockaddr_vm *addr);
120 bool virtio_transport_dgram_allow(u32 cid, u32 port);
121 
122 int virtio_transport_connect(struct vsock_sock *vsk);
123 
124 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode);
125 
126 void virtio_transport_release(struct vsock_sock *vsk);
127 
128 ssize_t
129 virtio_transport_stream_enqueue(struct vsock_sock *vsk,
130 				struct msghdr *msg,
131 				size_t len);
132 int
133 virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
134 			       struct sockaddr_vm *remote_addr,
135 			       struct msghdr *msg,
136 			       size_t len);
137 
138 void virtio_transport_destruct(struct vsock_sock *vsk);
139 
140 void virtio_transport_recv_pkt(struct virtio_transport *t,
141 			       struct virtio_vsock_pkt *pkt);
142 void virtio_transport_free_pkt(struct virtio_vsock_pkt *pkt);
143 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct virtio_vsock_pkt *pkt);
144 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
145 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
146 void virtio_transport_deliver_tap_pkt(struct virtio_vsock_pkt *pkt);
147 
148 #endif /* _LINUX_VIRTIO_VSOCK_H */
149