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