xref: /linux/include/linux/virtio_vsock.h (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
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_SKB_HEADROOM (sizeof(struct virtio_vsock_hdr))
11 
12 struct virtio_vsock_skb_cb {
13 	bool reply;
14 	bool tap_delivered;
15 	u32 offset;
16 };
17 
18 #define VIRTIO_VSOCK_SKB_CB(skb) ((struct virtio_vsock_skb_cb *)((skb)->cb))
19 
virtio_vsock_hdr(struct sk_buff * skb)20 static inline struct virtio_vsock_hdr *virtio_vsock_hdr(struct sk_buff *skb)
21 {
22 	return (struct virtio_vsock_hdr *)skb->head;
23 }
24 
virtio_vsock_skb_reply(struct sk_buff * skb)25 static inline bool virtio_vsock_skb_reply(struct sk_buff *skb)
26 {
27 	return VIRTIO_VSOCK_SKB_CB(skb)->reply;
28 }
29 
virtio_vsock_skb_set_reply(struct sk_buff * skb)30 static inline void virtio_vsock_skb_set_reply(struct sk_buff *skb)
31 {
32 	VIRTIO_VSOCK_SKB_CB(skb)->reply = true;
33 }
34 
virtio_vsock_skb_tap_delivered(struct sk_buff * skb)35 static inline bool virtio_vsock_skb_tap_delivered(struct sk_buff *skb)
36 {
37 	return VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered;
38 }
39 
virtio_vsock_skb_set_tap_delivered(struct sk_buff * skb)40 static inline void virtio_vsock_skb_set_tap_delivered(struct sk_buff *skb)
41 {
42 	VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered = true;
43 }
44 
virtio_vsock_skb_clear_tap_delivered(struct sk_buff * skb)45 static inline void virtio_vsock_skb_clear_tap_delivered(struct sk_buff *skb)
46 {
47 	VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered = false;
48 }
49 
virtio_vsock_skb_put(struct sk_buff * skb,u32 len)50 static inline void virtio_vsock_skb_put(struct sk_buff *skb, u32 len)
51 {
52 	DEBUG_NET_WARN_ON_ONCE(skb->len);
53 
54 	if (skb_is_nonlinear(skb))
55 		skb->len = len;
56 	else
57 		skb_put(skb, len);
58 }
59 
60 static inline struct sk_buff *
__virtio_vsock_alloc_skb_with_frags(unsigned int header_len,unsigned int data_len,gfp_t mask)61 __virtio_vsock_alloc_skb_with_frags(unsigned int header_len,
62 				    unsigned int data_len,
63 				    gfp_t mask)
64 {
65 	struct sk_buff *skb;
66 	int err;
67 
68 	skb = alloc_skb_with_frags(header_len, data_len,
69 				   PAGE_ALLOC_COSTLY_ORDER, &err, mask);
70 	if (!skb)
71 		return NULL;
72 
73 	skb_reserve(skb, VIRTIO_VSOCK_SKB_HEADROOM);
74 	skb->data_len = data_len;
75 	return skb;
76 }
77 
78 static inline struct sk_buff *
virtio_vsock_alloc_linear_skb(unsigned int size,gfp_t mask)79 virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask)
80 {
81 	return __virtio_vsock_alloc_skb_with_frags(size, 0, mask);
82 }
83 
virtio_vsock_alloc_skb(unsigned int size,gfp_t mask)84 static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gfp_t mask)
85 {
86 	if (size <= SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
87 		return virtio_vsock_alloc_linear_skb(size, mask);
88 
89 	size -= VIRTIO_VSOCK_SKB_HEADROOM;
90 	return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM,
91 						   size, mask);
92 }
93 
94 static inline void
virtio_vsock_skb_queue_head(struct sk_buff_head * list,struct sk_buff * skb)95 virtio_vsock_skb_queue_head(struct sk_buff_head *list, struct sk_buff *skb)
96 {
97 	spin_lock_bh(&list->lock);
98 	__skb_queue_head(list, skb);
99 	spin_unlock_bh(&list->lock);
100 }
101 
102 static inline void
virtio_vsock_skb_queue_tail(struct sk_buff_head * list,struct sk_buff * skb)103 virtio_vsock_skb_queue_tail(struct sk_buff_head *list, struct sk_buff *skb)
104 {
105 	spin_lock_bh(&list->lock);
106 	__skb_queue_tail(list, skb);
107 	spin_unlock_bh(&list->lock);
108 }
109 
virtio_vsock_skb_dequeue(struct sk_buff_head * list)110 static inline struct sk_buff *virtio_vsock_skb_dequeue(struct sk_buff_head *list)
111 {
112 	struct sk_buff *skb;
113 
114 	spin_lock_bh(&list->lock);
115 	skb = __skb_dequeue(list);
116 	spin_unlock_bh(&list->lock);
117 
118 	return skb;
119 }
120 
virtio_vsock_skb_queue_purge(struct sk_buff_head * list)121 static inline void virtio_vsock_skb_queue_purge(struct sk_buff_head *list)
122 {
123 	spin_lock_bh(&list->lock);
124 	__skb_queue_purge(list);
125 	spin_unlock_bh(&list->lock);
126 }
127 
virtio_vsock_skb_len(struct sk_buff * skb)128 static inline size_t virtio_vsock_skb_len(struct sk_buff *skb)
129 {
130 	return (size_t)(skb_end_pointer(skb) - skb->head);
131 }
132 
133 /* Dimension the RX SKB so that the entire thing fits exactly into
134  * a single 4KiB page. This avoids wasting memory due to alloc_skb()
135  * rounding up to the next page order and also means that we
136  * don't leave higher-order pages sitting around in the RX queue.
137  */
138 #define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE	SKB_WITH_OVERHEAD(1024 * 4)
139 #define VIRTIO_VSOCK_MAX_BUF_SIZE		0xFFFFFFFFUL
140 #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE		(1024 * 64)
141 
142 enum {
143 	VSOCK_VQ_RX     = 0, /* for host to guest data */
144 	VSOCK_VQ_TX     = 1, /* for guest to host data */
145 	VSOCK_VQ_EVENT  = 2,
146 	VSOCK_VQ_MAX    = 3,
147 };
148 
149 /* Per-socket state (accessed via vsk->trans) */
150 struct virtio_vsock_sock {
151 	struct vsock_sock *vsk;
152 
153 	spinlock_t tx_lock;
154 	spinlock_t rx_lock;
155 
156 	/* Protected by tx_lock */
157 	u32 tx_cnt;
158 	u32 peer_fwd_cnt;
159 	u32 peer_buf_alloc;
160 	size_t bytes_unsent;
161 
162 	/* Protected by rx_lock */
163 	u32 fwd_cnt;
164 	u32 last_fwd_cnt;
165 	u32 rx_bytes;
166 	u32 buf_alloc;
167 	u32 buf_used;
168 	struct sk_buff_head rx_queue;
169 	u32 msg_count;
170 };
171 
172 struct virtio_vsock_pkt_info {
173 	u32 remote_cid, remote_port;
174 	struct vsock_sock *vsk;
175 	struct msghdr *msg;
176 	u32 pkt_len;
177 	u16 type;
178 	u16 op;
179 	u32 flags;
180 	bool reply;
181 };
182 
183 struct virtio_transport {
184 	/* This must be the first field */
185 	struct vsock_transport transport;
186 
187 	/* Takes ownership of the packet */
188 	int (*send_pkt)(struct sk_buff *skb);
189 
190 	/* Used in MSG_ZEROCOPY mode. Checks, that provided data
191 	 * (number of buffers) could be transmitted with zerocopy
192 	 * mode. If this callback is not implemented for the current
193 	 * transport - this means that this transport doesn't need
194 	 * extra checks and can perform zerocopy transmission by
195 	 * default.
196 	 */
197 	bool (*can_msgzerocopy)(int bufs_num);
198 };
199 
200 ssize_t
201 virtio_transport_stream_dequeue(struct vsock_sock *vsk,
202 				struct msghdr *msg,
203 				size_t len,
204 				int type);
205 int
206 virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
207 			       struct msghdr *msg,
208 			       size_t len, int flags);
209 
210 int
211 virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
212 				   struct msghdr *msg,
213 				   size_t len);
214 ssize_t
215 virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
216 				   struct msghdr *msg,
217 				   int flags);
218 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk);
219 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk);
220 u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk);
221 
222 ssize_t virtio_transport_unsent_bytes(struct vsock_sock *vsk);
223 
224 void virtio_transport_consume_skb_sent(struct sk_buff *skb,
225 				       bool consume);
226 
227 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
228 				 struct vsock_sock *psk);
229 int
230 virtio_transport_notify_poll_in(struct vsock_sock *vsk,
231 				size_t target,
232 				bool *data_ready_now);
233 int
234 virtio_transport_notify_poll_out(struct vsock_sock *vsk,
235 				 size_t target,
236 				 bool *space_available_now);
237 
238 int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
239 	size_t target, struct vsock_transport_recv_notify_data *data);
240 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
241 	size_t target, struct vsock_transport_recv_notify_data *data);
242 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
243 	size_t target, struct vsock_transport_recv_notify_data *data);
244 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
245 	size_t target, ssize_t copied, bool data_read,
246 	struct vsock_transport_recv_notify_data *data);
247 int virtio_transport_notify_send_init(struct vsock_sock *vsk,
248 	struct vsock_transport_send_notify_data *data);
249 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
250 	struct vsock_transport_send_notify_data *data);
251 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
252 	struct vsock_transport_send_notify_data *data);
253 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
254 	ssize_t written, struct vsock_transport_send_notify_data *data);
255 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val);
256 
257 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk);
258 bool virtio_transport_stream_is_active(struct vsock_sock *vsk);
259 bool virtio_transport_stream_allow(u32 cid, u32 port);
260 int virtio_transport_dgram_bind(struct vsock_sock *vsk,
261 				struct sockaddr_vm *addr);
262 bool virtio_transport_dgram_allow(u32 cid, u32 port);
263 
264 int virtio_transport_connect(struct vsock_sock *vsk);
265 
266 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode);
267 
268 void virtio_transport_release(struct vsock_sock *vsk);
269 
270 ssize_t
271 virtio_transport_stream_enqueue(struct vsock_sock *vsk,
272 				struct msghdr *msg,
273 				size_t len);
274 int
275 virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
276 			       struct sockaddr_vm *remote_addr,
277 			       struct msghdr *msg,
278 			       size_t len);
279 
280 void virtio_transport_destruct(struct vsock_sock *vsk);
281 
282 void virtio_transport_recv_pkt(struct virtio_transport *t,
283 			       struct sk_buff *skb);
284 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff *skb);
285 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 wanted);
286 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit);
287 void virtio_transport_deliver_tap_pkt(struct sk_buff *skb);
288 int virtio_transport_purge_skbs(void *vsk, struct sk_buff_head *list);
289 int virtio_transport_read_skb(struct vsock_sock *vsk, skb_read_actor_t read_actor);
290 int virtio_transport_notify_set_rcvlowat(struct vsock_sock *vsk, int val);
291 #endif /* _LINUX_VIRTIO_VSOCK_H */
292