xref: /linux/net/vmw_vsock/virtio_transport_common.c (revision 3d5ad2d4eca337e80f38df77de89614aa5aaceb9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * common code for virtio vsock
4  *
5  * Copyright (C) 2013-2015 Red Hat, Inc.
6  * Author: Asias He <asias@redhat.com>
7  *         Stefan Hajnoczi <stefanha@redhat.com>
8  */
9 #include <linux/spinlock.h>
10 #include <linux/module.h>
11 #include <linux/sched/signal.h>
12 #include <linux/ctype.h>
13 #include <linux/list.h>
14 #include <linux/virtio_vsock.h>
15 #include <uapi/linux/vsockmon.h>
16 
17 #include <net/sock.h>
18 #include <net/af_vsock.h>
19 
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/vsock_virtio_transport_common.h>
22 
23 /* How long to wait for graceful shutdown of a connection */
24 #define VSOCK_CLOSE_TIMEOUT (8 * HZ)
25 
26 /* Threshold for detecting small packets to copy */
27 #define GOOD_COPY_LEN  128
28 
29 static const struct virtio_transport *
virtio_transport_get_ops(struct vsock_sock * vsk)30 virtio_transport_get_ops(struct vsock_sock *vsk)
31 {
32 	const struct vsock_transport *t = vsock_core_get_transport(vsk);
33 
34 	if (WARN_ON(!t))
35 		return NULL;
36 
37 	return container_of(t, struct virtio_transport, transport);
38 }
39 
virtio_transport_can_zcopy(const struct virtio_transport * t_ops,struct virtio_vsock_pkt_info * info,size_t pkt_len)40 static bool virtio_transport_can_zcopy(const struct virtio_transport *t_ops,
41 				       struct virtio_vsock_pkt_info *info,
42 				       size_t pkt_len)
43 {
44 	struct iov_iter *iov_iter;
45 
46 	if (!info->msg)
47 		return false;
48 
49 	iov_iter = &info->msg->msg_iter;
50 
51 	if (iov_iter->iov_offset)
52 		return false;
53 
54 	/* We can't send whole iov. */
55 	if (iov_iter->count > pkt_len)
56 		return false;
57 
58 	/* Check that transport can send data in zerocopy mode. */
59 	t_ops = virtio_transport_get_ops(info->vsk);
60 
61 	if (t_ops->can_msgzerocopy) {
62 		int pages_to_send = iov_iter_npages(iov_iter, MAX_SKB_FRAGS);
63 
64 		/* +1 is for packet header. */
65 		return t_ops->can_msgzerocopy(pages_to_send + 1);
66 	}
67 
68 	return true;
69 }
70 
virtio_transport_init_zcopy_skb(struct vsock_sock * vsk,struct sk_buff * skb,struct msghdr * msg,bool zerocopy)71 static int virtio_transport_init_zcopy_skb(struct vsock_sock *vsk,
72 					   struct sk_buff *skb,
73 					   struct msghdr *msg,
74 					   bool zerocopy)
75 {
76 	struct ubuf_info *uarg;
77 
78 	if (msg->msg_ubuf) {
79 		uarg = msg->msg_ubuf;
80 		net_zcopy_get(uarg);
81 	} else {
82 		struct iov_iter *iter = &msg->msg_iter;
83 		struct ubuf_info_msgzc *uarg_zc;
84 
85 		uarg = msg_zerocopy_realloc(sk_vsock(vsk),
86 					    iter->count,
87 					    NULL);
88 		if (!uarg)
89 			return -1;
90 
91 		uarg_zc = uarg_to_msgzc(uarg);
92 		uarg_zc->zerocopy = zerocopy ? 1 : 0;
93 	}
94 
95 	skb_zcopy_init(skb, uarg);
96 
97 	return 0;
98 }
99 
virtio_transport_fill_skb(struct sk_buff * skb,struct virtio_vsock_pkt_info * info,size_t len,bool zcopy)100 static int virtio_transport_fill_skb(struct sk_buff *skb,
101 				     struct virtio_vsock_pkt_info *info,
102 				     size_t len,
103 				     bool zcopy)
104 {
105 	if (zcopy)
106 		return __zerocopy_sg_from_iter(info->msg, NULL, skb,
107 					       &info->msg->msg_iter,
108 					       len);
109 
110 	return memcpy_from_msg(skb_put(skb, len), info->msg, len);
111 }
112 
virtio_transport_init_hdr(struct sk_buff * skb,struct virtio_vsock_pkt_info * info,size_t payload_len,u32 src_cid,u32 src_port,u32 dst_cid,u32 dst_port)113 static void virtio_transport_init_hdr(struct sk_buff *skb,
114 				      struct virtio_vsock_pkt_info *info,
115 				      size_t payload_len,
116 				      u32 src_cid,
117 				      u32 src_port,
118 				      u32 dst_cid,
119 				      u32 dst_port)
120 {
121 	struct virtio_vsock_hdr *hdr;
122 
123 	hdr = virtio_vsock_hdr(skb);
124 	hdr->type	= cpu_to_le16(info->type);
125 	hdr->op		= cpu_to_le16(info->op);
126 	hdr->src_cid	= cpu_to_le64(src_cid);
127 	hdr->dst_cid	= cpu_to_le64(dst_cid);
128 	hdr->src_port	= cpu_to_le32(src_port);
129 	hdr->dst_port	= cpu_to_le32(dst_port);
130 	hdr->flags	= cpu_to_le32(info->flags);
131 	hdr->len	= cpu_to_le32(payload_len);
132 	hdr->buf_alloc	= cpu_to_le32(0);
133 	hdr->fwd_cnt	= cpu_to_le32(0);
134 }
135 
virtio_transport_copy_nonlinear_skb(const struct sk_buff * skb,void * dst,size_t len)136 static void virtio_transport_copy_nonlinear_skb(const struct sk_buff *skb,
137 						void *dst,
138 						size_t len)
139 {
140 	struct iov_iter iov_iter = { 0 };
141 	struct kvec kvec;
142 	size_t to_copy;
143 
144 	kvec.iov_base = dst;
145 	kvec.iov_len = len;
146 
147 	iov_iter.iter_type = ITER_KVEC;
148 	iov_iter.kvec = &kvec;
149 	iov_iter.nr_segs = 1;
150 
151 	to_copy = min_t(size_t, len, skb->len);
152 
153 	skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset,
154 			       &iov_iter, to_copy);
155 }
156 
157 /* Packet capture */
virtio_transport_build_skb(void * opaque)158 static struct sk_buff *virtio_transport_build_skb(void *opaque)
159 {
160 	struct virtio_vsock_hdr *pkt_hdr;
161 	struct sk_buff *pkt = opaque;
162 	struct af_vsockmon_hdr *hdr;
163 	struct sk_buff *skb;
164 	size_t payload_len;
165 
166 	/* A packet could be split to fit the RX buffer, so we can retrieve
167 	 * the payload length from the header and the buffer pointer taking
168 	 * care of the offset in the original packet.
169 	 */
170 	pkt_hdr = virtio_vsock_hdr(pkt);
171 	payload_len = pkt->len;
172 
173 	skb = alloc_skb(sizeof(*hdr) + sizeof(*pkt_hdr) + payload_len,
174 			GFP_ATOMIC);
175 	if (!skb)
176 		return NULL;
177 
178 	hdr = skb_put(skb, sizeof(*hdr));
179 
180 	/* pkt->hdr is little-endian so no need to byteswap here */
181 	hdr->src_cid = pkt_hdr->src_cid;
182 	hdr->src_port = pkt_hdr->src_port;
183 	hdr->dst_cid = pkt_hdr->dst_cid;
184 	hdr->dst_port = pkt_hdr->dst_port;
185 
186 	hdr->transport = cpu_to_le16(AF_VSOCK_TRANSPORT_VIRTIO);
187 	hdr->len = cpu_to_le16(sizeof(*pkt_hdr));
188 	memset(hdr->reserved, 0, sizeof(hdr->reserved));
189 
190 	switch (le16_to_cpu(pkt_hdr->op)) {
191 	case VIRTIO_VSOCK_OP_REQUEST:
192 	case VIRTIO_VSOCK_OP_RESPONSE:
193 		hdr->op = cpu_to_le16(AF_VSOCK_OP_CONNECT);
194 		break;
195 	case VIRTIO_VSOCK_OP_RST:
196 	case VIRTIO_VSOCK_OP_SHUTDOWN:
197 		hdr->op = cpu_to_le16(AF_VSOCK_OP_DISCONNECT);
198 		break;
199 	case VIRTIO_VSOCK_OP_RW:
200 		hdr->op = cpu_to_le16(AF_VSOCK_OP_PAYLOAD);
201 		break;
202 	case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
203 	case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
204 		hdr->op = cpu_to_le16(AF_VSOCK_OP_CONTROL);
205 		break;
206 	default:
207 		hdr->op = cpu_to_le16(AF_VSOCK_OP_UNKNOWN);
208 		break;
209 	}
210 
211 	skb_put_data(skb, pkt_hdr, sizeof(*pkt_hdr));
212 
213 	if (payload_len) {
214 		if (skb_is_nonlinear(pkt)) {
215 			void *data = skb_put(skb, payload_len);
216 
217 			virtio_transport_copy_nonlinear_skb(pkt, data, payload_len);
218 		} else {
219 			skb_put_data(skb, pkt->data, payload_len);
220 		}
221 	}
222 
223 	return skb;
224 }
225 
virtio_transport_deliver_tap_pkt(struct sk_buff * skb)226 void virtio_transport_deliver_tap_pkt(struct sk_buff *skb)
227 {
228 	if (virtio_vsock_skb_tap_delivered(skb))
229 		return;
230 
231 	vsock_deliver_tap(virtio_transport_build_skb, skb);
232 	virtio_vsock_skb_set_tap_delivered(skb);
233 }
234 EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt);
235 
virtio_transport_get_type(struct sock * sk)236 static u16 virtio_transport_get_type(struct sock *sk)
237 {
238 	if (sk->sk_type == SOCK_STREAM)
239 		return VIRTIO_VSOCK_TYPE_STREAM;
240 	else
241 		return VIRTIO_VSOCK_TYPE_SEQPACKET;
242 }
243 
244 /* Returns new sk_buff on success, otherwise returns NULL. */
virtio_transport_alloc_skb(struct virtio_vsock_pkt_info * info,size_t payload_len,bool zcopy,u32 src_cid,u32 src_port,u32 dst_cid,u32 dst_port)245 static struct sk_buff *virtio_transport_alloc_skb(struct virtio_vsock_pkt_info *info,
246 						  size_t payload_len,
247 						  bool zcopy,
248 						  u32 src_cid,
249 						  u32 src_port,
250 						  u32 dst_cid,
251 						  u32 dst_port)
252 {
253 	struct vsock_sock *vsk;
254 	struct sk_buff *skb;
255 	size_t skb_len;
256 
257 	skb_len = VIRTIO_VSOCK_SKB_HEADROOM;
258 
259 	if (!zcopy)
260 		skb_len += payload_len;
261 
262 	skb = virtio_vsock_alloc_skb(skb_len, GFP_KERNEL);
263 	if (!skb)
264 		return NULL;
265 
266 	virtio_transport_init_hdr(skb, info, payload_len, src_cid, src_port,
267 				  dst_cid, dst_port);
268 
269 	vsk = info->vsk;
270 
271 	/* If 'vsk' != NULL then payload is always present, so we
272 	 * will never call '__zerocopy_sg_from_iter()' below without
273 	 * setting skb owner in 'skb_set_owner_w()'. The only case
274 	 * when 'vsk' == NULL is VIRTIO_VSOCK_OP_RST control message
275 	 * without payload.
276 	 */
277 	WARN_ON_ONCE(!(vsk && (info->msg && payload_len)) && zcopy);
278 
279 	/* Set owner here, because '__zerocopy_sg_from_iter()' uses
280 	 * owner of skb without check to update 'sk_wmem_alloc'.
281 	 */
282 	if (vsk)
283 		skb_set_owner_w(skb, sk_vsock(vsk));
284 
285 	if (info->msg && payload_len > 0) {
286 		int err;
287 
288 		err = virtio_transport_fill_skb(skb, info, payload_len, zcopy);
289 		if (err)
290 			goto out;
291 
292 		if (msg_data_left(info->msg) == 0 &&
293 		    info->type == VIRTIO_VSOCK_TYPE_SEQPACKET) {
294 			struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
295 
296 			hdr->flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOM);
297 
298 			if (info->msg->msg_flags & MSG_EOR)
299 				hdr->flags |= cpu_to_le32(VIRTIO_VSOCK_SEQ_EOR);
300 		}
301 	}
302 
303 	if (info->reply)
304 		virtio_vsock_skb_set_reply(skb);
305 
306 	trace_virtio_transport_alloc_pkt(src_cid, src_port,
307 					 dst_cid, dst_port,
308 					 payload_len,
309 					 info->type,
310 					 info->op,
311 					 info->flags,
312 					 zcopy);
313 
314 	return skb;
315 out:
316 	kfree_skb(skb);
317 	return NULL;
318 }
319 
320 /* This function can only be used on connecting/connected sockets,
321  * since a socket assigned to a transport is required.
322  *
323  * Do not use on listener sockets!
324  */
virtio_transport_send_pkt_info(struct vsock_sock * vsk,struct virtio_vsock_pkt_info * info)325 static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
326 					  struct virtio_vsock_pkt_info *info)
327 {
328 	u32 max_skb_len = VIRTIO_VSOCK_MAX_PKT_BUF_SIZE;
329 	u32 src_cid, src_port, dst_cid, dst_port;
330 	const struct virtio_transport *t_ops;
331 	struct virtio_vsock_sock *vvs;
332 	u32 pkt_len = info->pkt_len;
333 	bool can_zcopy = false;
334 	u32 rest_len;
335 	int ret;
336 
337 	info->type = virtio_transport_get_type(sk_vsock(vsk));
338 
339 	t_ops = virtio_transport_get_ops(vsk);
340 	if (unlikely(!t_ops))
341 		return -EFAULT;
342 
343 	src_cid = t_ops->transport.get_local_cid();
344 	src_port = vsk->local_addr.svm_port;
345 	if (!info->remote_cid) {
346 		dst_cid	= vsk->remote_addr.svm_cid;
347 		dst_port = vsk->remote_addr.svm_port;
348 	} else {
349 		dst_cid = info->remote_cid;
350 		dst_port = info->remote_port;
351 	}
352 
353 	vvs = vsk->trans;
354 
355 	/* virtio_transport_get_credit might return less than pkt_len credit */
356 	pkt_len = virtio_transport_get_credit(vvs, pkt_len);
357 
358 	/* Do not send zero length OP_RW pkt */
359 	if (pkt_len == 0 && info->op == VIRTIO_VSOCK_OP_RW)
360 		return pkt_len;
361 
362 	if (info->msg) {
363 		/* If zerocopy is not enabled by 'setsockopt()', we behave as
364 		 * there is no MSG_ZEROCOPY flag set.
365 		 */
366 		if (!sock_flag(sk_vsock(vsk), SOCK_ZEROCOPY))
367 			info->msg->msg_flags &= ~MSG_ZEROCOPY;
368 
369 		if (info->msg->msg_flags & MSG_ZEROCOPY)
370 			can_zcopy = virtio_transport_can_zcopy(t_ops, info, pkt_len);
371 
372 		if (can_zcopy)
373 			max_skb_len = min_t(u32, VIRTIO_VSOCK_MAX_PKT_BUF_SIZE,
374 					    (MAX_SKB_FRAGS * PAGE_SIZE));
375 	}
376 
377 	rest_len = pkt_len;
378 
379 	do {
380 		struct sk_buff *skb;
381 		size_t skb_len;
382 
383 		skb_len = min(max_skb_len, rest_len);
384 
385 		skb = virtio_transport_alloc_skb(info, skb_len, can_zcopy,
386 						 src_cid, src_port,
387 						 dst_cid, dst_port);
388 		if (!skb) {
389 			ret = -ENOMEM;
390 			break;
391 		}
392 
393 		/* We process buffer part by part, allocating skb on
394 		 * each iteration. If this is last skb for this buffer
395 		 * and MSG_ZEROCOPY mode is in use - we must allocate
396 		 * completion for the current syscall.
397 		 */
398 		if (info->msg && info->msg->msg_flags & MSG_ZEROCOPY &&
399 		    skb_len == rest_len && info->op == VIRTIO_VSOCK_OP_RW) {
400 			if (virtio_transport_init_zcopy_skb(vsk, skb,
401 							    info->msg,
402 							    can_zcopy)) {
403 				ret = -ENOMEM;
404 				break;
405 			}
406 		}
407 
408 		virtio_transport_inc_tx_pkt(vvs, skb);
409 
410 		ret = t_ops->send_pkt(skb);
411 		if (ret < 0)
412 			break;
413 
414 		/* Both virtio and vhost 'send_pkt()' returns 'skb_len',
415 		 * but for reliability use 'ret' instead of 'skb_len'.
416 		 * Also if partial send happens (e.g. 'ret' != 'skb_len')
417 		 * somehow, we break this loop, but account such returned
418 		 * value in 'virtio_transport_put_credit()'.
419 		 */
420 		rest_len -= ret;
421 
422 		if (WARN_ONCE(ret != skb_len,
423 			      "'send_pkt()' returns %i, but %zu expected\n",
424 			      ret, skb_len))
425 			break;
426 	} while (rest_len);
427 
428 	virtio_transport_put_credit(vvs, rest_len);
429 
430 	/* Return number of bytes, if any data has been sent. */
431 	if (rest_len != pkt_len)
432 		ret = pkt_len - rest_len;
433 
434 	return ret;
435 }
436 
virtio_transport_inc_rx_pkt(struct virtio_vsock_sock * vvs,u32 len)437 static bool virtio_transport_inc_rx_pkt(struct virtio_vsock_sock *vvs,
438 					u32 len)
439 {
440 	if (vvs->rx_bytes + len > vvs->buf_alloc)
441 		return false;
442 
443 	vvs->rx_bytes += len;
444 	return true;
445 }
446 
virtio_transport_dec_rx_pkt(struct virtio_vsock_sock * vvs,u32 len)447 static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs,
448 					u32 len)
449 {
450 	vvs->rx_bytes -= len;
451 	vvs->fwd_cnt += len;
452 }
453 
virtio_transport_inc_tx_pkt(struct virtio_vsock_sock * vvs,struct sk_buff * skb)454 void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct sk_buff *skb)
455 {
456 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
457 
458 	spin_lock_bh(&vvs->rx_lock);
459 	vvs->last_fwd_cnt = vvs->fwd_cnt;
460 	hdr->fwd_cnt = cpu_to_le32(vvs->fwd_cnt);
461 	hdr->buf_alloc = cpu_to_le32(vvs->buf_alloc);
462 	spin_unlock_bh(&vvs->rx_lock);
463 }
464 EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt);
465 
virtio_transport_consume_skb_sent(struct sk_buff * skb,bool consume)466 void virtio_transport_consume_skb_sent(struct sk_buff *skb, bool consume)
467 {
468 	struct sock *s = skb->sk;
469 
470 	if (s && skb->len) {
471 		struct vsock_sock *vs = vsock_sk(s);
472 		struct virtio_vsock_sock *vvs;
473 
474 		vvs = vs->trans;
475 
476 		spin_lock_bh(&vvs->tx_lock);
477 		vvs->bytes_unsent -= skb->len;
478 		spin_unlock_bh(&vvs->tx_lock);
479 	}
480 
481 	if (consume)
482 		consume_skb(skb);
483 }
484 EXPORT_SYMBOL_GPL(virtio_transport_consume_skb_sent);
485 
virtio_transport_get_credit(struct virtio_vsock_sock * vvs,u32 credit)486 u32 virtio_transport_get_credit(struct virtio_vsock_sock *vvs, u32 credit)
487 {
488 	u32 ret;
489 
490 	if (!credit)
491 		return 0;
492 
493 	spin_lock_bh(&vvs->tx_lock);
494 	ret = vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
495 	if (ret > credit)
496 		ret = credit;
497 	vvs->tx_cnt += ret;
498 	vvs->bytes_unsent += ret;
499 	spin_unlock_bh(&vvs->tx_lock);
500 
501 	return ret;
502 }
503 EXPORT_SYMBOL_GPL(virtio_transport_get_credit);
504 
virtio_transport_put_credit(struct virtio_vsock_sock * vvs,u32 credit)505 void virtio_transport_put_credit(struct virtio_vsock_sock *vvs, u32 credit)
506 {
507 	if (!credit)
508 		return;
509 
510 	spin_lock_bh(&vvs->tx_lock);
511 	vvs->tx_cnt -= credit;
512 	vvs->bytes_unsent -= credit;
513 	spin_unlock_bh(&vvs->tx_lock);
514 }
515 EXPORT_SYMBOL_GPL(virtio_transport_put_credit);
516 
virtio_transport_send_credit_update(struct vsock_sock * vsk)517 static int virtio_transport_send_credit_update(struct vsock_sock *vsk)
518 {
519 	struct virtio_vsock_pkt_info info = {
520 		.op = VIRTIO_VSOCK_OP_CREDIT_UPDATE,
521 		.vsk = vsk,
522 	};
523 
524 	return virtio_transport_send_pkt_info(vsk, &info);
525 }
526 
527 static ssize_t
virtio_transport_stream_do_peek(struct vsock_sock * vsk,struct msghdr * msg,size_t len)528 virtio_transport_stream_do_peek(struct vsock_sock *vsk,
529 				struct msghdr *msg,
530 				size_t len)
531 {
532 	struct virtio_vsock_sock *vvs = vsk->trans;
533 	struct sk_buff *skb;
534 	size_t total = 0;
535 	int err;
536 
537 	spin_lock_bh(&vvs->rx_lock);
538 
539 	skb_queue_walk(&vvs->rx_queue, skb) {
540 		size_t bytes;
541 
542 		bytes = len - total;
543 		if (bytes > skb->len)
544 			bytes = skb->len;
545 
546 		spin_unlock_bh(&vvs->rx_lock);
547 
548 		/* sk_lock is held by caller so no one else can dequeue.
549 		 * Unlock rx_lock since skb_copy_datagram_iter() may sleep.
550 		 */
551 		err = skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset,
552 					     &msg->msg_iter, bytes);
553 		if (err)
554 			goto out;
555 
556 		total += bytes;
557 
558 		spin_lock_bh(&vvs->rx_lock);
559 
560 		if (total == len)
561 			break;
562 	}
563 
564 	spin_unlock_bh(&vvs->rx_lock);
565 
566 	return total;
567 
568 out:
569 	if (total)
570 		err = total;
571 	return err;
572 }
573 
574 static ssize_t
virtio_transport_stream_do_dequeue(struct vsock_sock * vsk,struct msghdr * msg,size_t len)575 virtio_transport_stream_do_dequeue(struct vsock_sock *vsk,
576 				   struct msghdr *msg,
577 				   size_t len)
578 {
579 	struct virtio_vsock_sock *vvs = vsk->trans;
580 	size_t bytes, total = 0;
581 	struct sk_buff *skb;
582 	u32 fwd_cnt_delta;
583 	bool low_rx_bytes;
584 	int err = -EFAULT;
585 	u32 free_space;
586 
587 	spin_lock_bh(&vvs->rx_lock);
588 
589 	if (WARN_ONCE(skb_queue_empty(&vvs->rx_queue) && vvs->rx_bytes,
590 		      "rx_queue is empty, but rx_bytes is non-zero\n")) {
591 		spin_unlock_bh(&vvs->rx_lock);
592 		return err;
593 	}
594 
595 	while (total < len && !skb_queue_empty(&vvs->rx_queue)) {
596 		skb = skb_peek(&vvs->rx_queue);
597 
598 		bytes = min_t(size_t, len - total,
599 			      skb->len - VIRTIO_VSOCK_SKB_CB(skb)->offset);
600 
601 		/* sk_lock is held by caller so no one else can dequeue.
602 		 * Unlock rx_lock since skb_copy_datagram_iter() may sleep.
603 		 */
604 		spin_unlock_bh(&vvs->rx_lock);
605 
606 		err = skb_copy_datagram_iter(skb,
607 					     VIRTIO_VSOCK_SKB_CB(skb)->offset,
608 					     &msg->msg_iter, bytes);
609 		if (err)
610 			goto out;
611 
612 		spin_lock_bh(&vvs->rx_lock);
613 
614 		total += bytes;
615 
616 		VIRTIO_VSOCK_SKB_CB(skb)->offset += bytes;
617 
618 		if (skb->len == VIRTIO_VSOCK_SKB_CB(skb)->offset) {
619 			u32 pkt_len = le32_to_cpu(virtio_vsock_hdr(skb)->len);
620 
621 			virtio_transport_dec_rx_pkt(vvs, pkt_len);
622 			__skb_unlink(skb, &vvs->rx_queue);
623 			consume_skb(skb);
624 		}
625 	}
626 
627 	fwd_cnt_delta = vvs->fwd_cnt - vvs->last_fwd_cnt;
628 	free_space = vvs->buf_alloc - fwd_cnt_delta;
629 	low_rx_bytes = (vvs->rx_bytes <
630 			sock_rcvlowat(sk_vsock(vsk), 0, INT_MAX));
631 
632 	spin_unlock_bh(&vvs->rx_lock);
633 
634 	/* To reduce the number of credit update messages,
635 	 * don't update credits as long as lots of space is available.
636 	 * Note: the limit chosen here is arbitrary. Setting the limit
637 	 * too high causes extra messages. Too low causes transmitter
638 	 * stalls. As stalls are in theory more expensive than extra
639 	 * messages, we set the limit to a high value. TODO: experiment
640 	 * with different values. Also send credit update message when
641 	 * number of bytes in rx queue is not enough to wake up reader.
642 	 */
643 	if (fwd_cnt_delta &&
644 	    (free_space < VIRTIO_VSOCK_MAX_PKT_BUF_SIZE || low_rx_bytes))
645 		virtio_transport_send_credit_update(vsk);
646 
647 	return total;
648 
649 out:
650 	if (total)
651 		err = total;
652 	return err;
653 }
654 
655 static ssize_t
virtio_transport_seqpacket_do_peek(struct vsock_sock * vsk,struct msghdr * msg)656 virtio_transport_seqpacket_do_peek(struct vsock_sock *vsk,
657 				   struct msghdr *msg)
658 {
659 	struct virtio_vsock_sock *vvs = vsk->trans;
660 	struct sk_buff *skb;
661 	size_t total, len;
662 
663 	spin_lock_bh(&vvs->rx_lock);
664 
665 	if (!vvs->msg_count) {
666 		spin_unlock_bh(&vvs->rx_lock);
667 		return 0;
668 	}
669 
670 	total = 0;
671 	len = msg_data_left(msg);
672 
673 	skb_queue_walk(&vvs->rx_queue, skb) {
674 		struct virtio_vsock_hdr *hdr;
675 
676 		if (total < len) {
677 			size_t bytes;
678 			int err;
679 
680 			bytes = len - total;
681 			if (bytes > skb->len)
682 				bytes = skb->len;
683 
684 			spin_unlock_bh(&vvs->rx_lock);
685 
686 			/* sk_lock is held by caller so no one else can dequeue.
687 			 * Unlock rx_lock since skb_copy_datagram_iter() may sleep.
688 			 */
689 			err = skb_copy_datagram_iter(skb, VIRTIO_VSOCK_SKB_CB(skb)->offset,
690 						     &msg->msg_iter, bytes);
691 			if (err)
692 				return err;
693 
694 			spin_lock_bh(&vvs->rx_lock);
695 		}
696 
697 		total += skb->len;
698 		hdr = virtio_vsock_hdr(skb);
699 
700 		if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) {
701 			if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOR)
702 				msg->msg_flags |= MSG_EOR;
703 
704 			break;
705 		}
706 	}
707 
708 	spin_unlock_bh(&vvs->rx_lock);
709 
710 	return total;
711 }
712 
virtio_transport_seqpacket_do_dequeue(struct vsock_sock * vsk,struct msghdr * msg,int flags)713 static int virtio_transport_seqpacket_do_dequeue(struct vsock_sock *vsk,
714 						 struct msghdr *msg,
715 						 int flags)
716 {
717 	struct virtio_vsock_sock *vvs = vsk->trans;
718 	int dequeued_len = 0;
719 	size_t user_buf_len = msg_data_left(msg);
720 	bool msg_ready = false;
721 	struct sk_buff *skb;
722 
723 	spin_lock_bh(&vvs->rx_lock);
724 
725 	if (vvs->msg_count == 0) {
726 		spin_unlock_bh(&vvs->rx_lock);
727 		return 0;
728 	}
729 
730 	while (!msg_ready) {
731 		struct virtio_vsock_hdr *hdr;
732 		size_t pkt_len;
733 
734 		skb = __skb_dequeue(&vvs->rx_queue);
735 		if (!skb)
736 			break;
737 		hdr = virtio_vsock_hdr(skb);
738 		pkt_len = (size_t)le32_to_cpu(hdr->len);
739 
740 		if (dequeued_len >= 0) {
741 			size_t bytes_to_copy;
742 
743 			bytes_to_copy = min(user_buf_len, pkt_len);
744 
745 			if (bytes_to_copy) {
746 				int err;
747 
748 				/* sk_lock is held by caller so no one else can dequeue.
749 				 * Unlock rx_lock since skb_copy_datagram_iter() may sleep.
750 				 */
751 				spin_unlock_bh(&vvs->rx_lock);
752 
753 				err = skb_copy_datagram_iter(skb, 0,
754 							     &msg->msg_iter,
755 							     bytes_to_copy);
756 				if (err) {
757 					/* Copy of message failed. Rest of
758 					 * fragments will be freed without copy.
759 					 */
760 					dequeued_len = err;
761 				} else {
762 					user_buf_len -= bytes_to_copy;
763 				}
764 
765 				spin_lock_bh(&vvs->rx_lock);
766 			}
767 
768 			if (dequeued_len >= 0)
769 				dequeued_len += pkt_len;
770 		}
771 
772 		if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM) {
773 			msg_ready = true;
774 			vvs->msg_count--;
775 
776 			if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOR)
777 				msg->msg_flags |= MSG_EOR;
778 		}
779 
780 		virtio_transport_dec_rx_pkt(vvs, pkt_len);
781 		kfree_skb(skb);
782 	}
783 
784 	spin_unlock_bh(&vvs->rx_lock);
785 
786 	virtio_transport_send_credit_update(vsk);
787 
788 	return dequeued_len;
789 }
790 
791 ssize_t
virtio_transport_stream_dequeue(struct vsock_sock * vsk,struct msghdr * msg,size_t len,int flags)792 virtio_transport_stream_dequeue(struct vsock_sock *vsk,
793 				struct msghdr *msg,
794 				size_t len, int flags)
795 {
796 	if (flags & MSG_PEEK)
797 		return virtio_transport_stream_do_peek(vsk, msg, len);
798 	else
799 		return virtio_transport_stream_do_dequeue(vsk, msg, len);
800 }
801 EXPORT_SYMBOL_GPL(virtio_transport_stream_dequeue);
802 
803 ssize_t
virtio_transport_seqpacket_dequeue(struct vsock_sock * vsk,struct msghdr * msg,int flags)804 virtio_transport_seqpacket_dequeue(struct vsock_sock *vsk,
805 				   struct msghdr *msg,
806 				   int flags)
807 {
808 	if (flags & MSG_PEEK)
809 		return virtio_transport_seqpacket_do_peek(vsk, msg);
810 	else
811 		return virtio_transport_seqpacket_do_dequeue(vsk, msg, flags);
812 }
813 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_dequeue);
814 
815 int
virtio_transport_seqpacket_enqueue(struct vsock_sock * vsk,struct msghdr * msg,size_t len)816 virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk,
817 				   struct msghdr *msg,
818 				   size_t len)
819 {
820 	struct virtio_vsock_sock *vvs = vsk->trans;
821 
822 	spin_lock_bh(&vvs->tx_lock);
823 
824 	if (len > vvs->peer_buf_alloc) {
825 		spin_unlock_bh(&vvs->tx_lock);
826 		return -EMSGSIZE;
827 	}
828 
829 	spin_unlock_bh(&vvs->tx_lock);
830 
831 	return virtio_transport_stream_enqueue(vsk, msg, len);
832 }
833 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue);
834 
835 int
virtio_transport_dgram_dequeue(struct vsock_sock * vsk,struct msghdr * msg,size_t len,int flags)836 virtio_transport_dgram_dequeue(struct vsock_sock *vsk,
837 			       struct msghdr *msg,
838 			       size_t len, int flags)
839 {
840 	return -EOPNOTSUPP;
841 }
842 EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue);
843 
virtio_transport_stream_has_data(struct vsock_sock * vsk)844 s64 virtio_transport_stream_has_data(struct vsock_sock *vsk)
845 {
846 	struct virtio_vsock_sock *vvs = vsk->trans;
847 	s64 bytes;
848 
849 	spin_lock_bh(&vvs->rx_lock);
850 	bytes = vvs->rx_bytes;
851 	spin_unlock_bh(&vvs->rx_lock);
852 
853 	return bytes;
854 }
855 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_data);
856 
virtio_transport_seqpacket_has_data(struct vsock_sock * vsk)857 u32 virtio_transport_seqpacket_has_data(struct vsock_sock *vsk)
858 {
859 	struct virtio_vsock_sock *vvs = vsk->trans;
860 	u32 msg_count;
861 
862 	spin_lock_bh(&vvs->rx_lock);
863 	msg_count = vvs->msg_count;
864 	spin_unlock_bh(&vvs->rx_lock);
865 
866 	return msg_count;
867 }
868 EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_has_data);
869 
virtio_transport_has_space(struct vsock_sock * vsk)870 static s64 virtio_transport_has_space(struct vsock_sock *vsk)
871 {
872 	struct virtio_vsock_sock *vvs = vsk->trans;
873 	s64 bytes;
874 
875 	bytes = (s64)vvs->peer_buf_alloc - (vvs->tx_cnt - vvs->peer_fwd_cnt);
876 	if (bytes < 0)
877 		bytes = 0;
878 
879 	return bytes;
880 }
881 
virtio_transport_stream_has_space(struct vsock_sock * vsk)882 s64 virtio_transport_stream_has_space(struct vsock_sock *vsk)
883 {
884 	struct virtio_vsock_sock *vvs = vsk->trans;
885 	s64 bytes;
886 
887 	spin_lock_bh(&vvs->tx_lock);
888 	bytes = virtio_transport_has_space(vsk);
889 	spin_unlock_bh(&vvs->tx_lock);
890 
891 	return bytes;
892 }
893 EXPORT_SYMBOL_GPL(virtio_transport_stream_has_space);
894 
virtio_transport_do_socket_init(struct vsock_sock * vsk,struct vsock_sock * psk)895 int virtio_transport_do_socket_init(struct vsock_sock *vsk,
896 				    struct vsock_sock *psk)
897 {
898 	struct virtio_vsock_sock *vvs;
899 
900 	vvs = kzalloc(sizeof(*vvs), GFP_KERNEL);
901 	if (!vvs)
902 		return -ENOMEM;
903 
904 	vsk->trans = vvs;
905 	vvs->vsk = vsk;
906 	if (psk && psk->trans) {
907 		struct virtio_vsock_sock *ptrans = psk->trans;
908 
909 		vvs->peer_buf_alloc = ptrans->peer_buf_alloc;
910 	}
911 
912 	if (vsk->buffer_size > VIRTIO_VSOCK_MAX_BUF_SIZE)
913 		vsk->buffer_size = VIRTIO_VSOCK_MAX_BUF_SIZE;
914 
915 	vvs->buf_alloc = vsk->buffer_size;
916 
917 	spin_lock_init(&vvs->rx_lock);
918 	spin_lock_init(&vvs->tx_lock);
919 	skb_queue_head_init(&vvs->rx_queue);
920 
921 	return 0;
922 }
923 EXPORT_SYMBOL_GPL(virtio_transport_do_socket_init);
924 
925 /* sk_lock held by the caller */
virtio_transport_notify_buffer_size(struct vsock_sock * vsk,u64 * val)926 void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val)
927 {
928 	struct virtio_vsock_sock *vvs = vsk->trans;
929 
930 	if (*val > VIRTIO_VSOCK_MAX_BUF_SIZE)
931 		*val = VIRTIO_VSOCK_MAX_BUF_SIZE;
932 
933 	vvs->buf_alloc = *val;
934 
935 	virtio_transport_send_credit_update(vsk);
936 }
937 EXPORT_SYMBOL_GPL(virtio_transport_notify_buffer_size);
938 
939 int
virtio_transport_notify_poll_in(struct vsock_sock * vsk,size_t target,bool * data_ready_now)940 virtio_transport_notify_poll_in(struct vsock_sock *vsk,
941 				size_t target,
942 				bool *data_ready_now)
943 {
944 	*data_ready_now = vsock_stream_has_data(vsk) >= target;
945 
946 	return 0;
947 }
948 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_in);
949 
950 int
virtio_transport_notify_poll_out(struct vsock_sock * vsk,size_t target,bool * space_avail_now)951 virtio_transport_notify_poll_out(struct vsock_sock *vsk,
952 				 size_t target,
953 				 bool *space_avail_now)
954 {
955 	s64 free_space;
956 
957 	free_space = vsock_stream_has_space(vsk);
958 	if (free_space > 0)
959 		*space_avail_now = true;
960 	else if (free_space == 0)
961 		*space_avail_now = false;
962 
963 	return 0;
964 }
965 EXPORT_SYMBOL_GPL(virtio_transport_notify_poll_out);
966 
virtio_transport_notify_recv_init(struct vsock_sock * vsk,size_t target,struct vsock_transport_recv_notify_data * data)967 int virtio_transport_notify_recv_init(struct vsock_sock *vsk,
968 	size_t target, struct vsock_transport_recv_notify_data *data)
969 {
970 	return 0;
971 }
972 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_init);
973 
virtio_transport_notify_recv_pre_block(struct vsock_sock * vsk,size_t target,struct vsock_transport_recv_notify_data * data)974 int virtio_transport_notify_recv_pre_block(struct vsock_sock *vsk,
975 	size_t target, struct vsock_transport_recv_notify_data *data)
976 {
977 	return 0;
978 }
979 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_block);
980 
virtio_transport_notify_recv_pre_dequeue(struct vsock_sock * vsk,size_t target,struct vsock_transport_recv_notify_data * data)981 int virtio_transport_notify_recv_pre_dequeue(struct vsock_sock *vsk,
982 	size_t target, struct vsock_transport_recv_notify_data *data)
983 {
984 	return 0;
985 }
986 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_pre_dequeue);
987 
virtio_transport_notify_recv_post_dequeue(struct vsock_sock * vsk,size_t target,ssize_t copied,bool data_read,struct vsock_transport_recv_notify_data * data)988 int virtio_transport_notify_recv_post_dequeue(struct vsock_sock *vsk,
989 	size_t target, ssize_t copied, bool data_read,
990 	struct vsock_transport_recv_notify_data *data)
991 {
992 	return 0;
993 }
994 EXPORT_SYMBOL_GPL(virtio_transport_notify_recv_post_dequeue);
995 
virtio_transport_notify_send_init(struct vsock_sock * vsk,struct vsock_transport_send_notify_data * data)996 int virtio_transport_notify_send_init(struct vsock_sock *vsk,
997 	struct vsock_transport_send_notify_data *data)
998 {
999 	return 0;
1000 }
1001 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_init);
1002 
virtio_transport_notify_send_pre_block(struct vsock_sock * vsk,struct vsock_transport_send_notify_data * data)1003 int virtio_transport_notify_send_pre_block(struct vsock_sock *vsk,
1004 	struct vsock_transport_send_notify_data *data)
1005 {
1006 	return 0;
1007 }
1008 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_block);
1009 
virtio_transport_notify_send_pre_enqueue(struct vsock_sock * vsk,struct vsock_transport_send_notify_data * data)1010 int virtio_transport_notify_send_pre_enqueue(struct vsock_sock *vsk,
1011 	struct vsock_transport_send_notify_data *data)
1012 {
1013 	return 0;
1014 }
1015 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_pre_enqueue);
1016 
virtio_transport_notify_send_post_enqueue(struct vsock_sock * vsk,ssize_t written,struct vsock_transport_send_notify_data * data)1017 int virtio_transport_notify_send_post_enqueue(struct vsock_sock *vsk,
1018 	ssize_t written, struct vsock_transport_send_notify_data *data)
1019 {
1020 	return 0;
1021 }
1022 EXPORT_SYMBOL_GPL(virtio_transport_notify_send_post_enqueue);
1023 
virtio_transport_stream_rcvhiwat(struct vsock_sock * vsk)1024 u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk)
1025 {
1026 	return vsk->buffer_size;
1027 }
1028 EXPORT_SYMBOL_GPL(virtio_transport_stream_rcvhiwat);
1029 
virtio_transport_stream_is_active(struct vsock_sock * vsk)1030 bool virtio_transport_stream_is_active(struct vsock_sock *vsk)
1031 {
1032 	return true;
1033 }
1034 EXPORT_SYMBOL_GPL(virtio_transport_stream_is_active);
1035 
virtio_transport_stream_allow(u32 cid,u32 port)1036 bool virtio_transport_stream_allow(u32 cid, u32 port)
1037 {
1038 	return true;
1039 }
1040 EXPORT_SYMBOL_GPL(virtio_transport_stream_allow);
1041 
virtio_transport_dgram_bind(struct vsock_sock * vsk,struct sockaddr_vm * addr)1042 int virtio_transport_dgram_bind(struct vsock_sock *vsk,
1043 				struct sockaddr_vm *addr)
1044 {
1045 	return -EOPNOTSUPP;
1046 }
1047 EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind);
1048 
virtio_transport_dgram_allow(u32 cid,u32 port)1049 bool virtio_transport_dgram_allow(u32 cid, u32 port)
1050 {
1051 	return false;
1052 }
1053 EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow);
1054 
virtio_transport_connect(struct vsock_sock * vsk)1055 int virtio_transport_connect(struct vsock_sock *vsk)
1056 {
1057 	struct virtio_vsock_pkt_info info = {
1058 		.op = VIRTIO_VSOCK_OP_REQUEST,
1059 		.vsk = vsk,
1060 	};
1061 
1062 	return virtio_transport_send_pkt_info(vsk, &info);
1063 }
1064 EXPORT_SYMBOL_GPL(virtio_transport_connect);
1065 
virtio_transport_shutdown(struct vsock_sock * vsk,int mode)1066 int virtio_transport_shutdown(struct vsock_sock *vsk, int mode)
1067 {
1068 	struct virtio_vsock_pkt_info info = {
1069 		.op = VIRTIO_VSOCK_OP_SHUTDOWN,
1070 		.flags = (mode & RCV_SHUTDOWN ?
1071 			  VIRTIO_VSOCK_SHUTDOWN_RCV : 0) |
1072 			 (mode & SEND_SHUTDOWN ?
1073 			  VIRTIO_VSOCK_SHUTDOWN_SEND : 0),
1074 		.vsk = vsk,
1075 	};
1076 
1077 	return virtio_transport_send_pkt_info(vsk, &info);
1078 }
1079 EXPORT_SYMBOL_GPL(virtio_transport_shutdown);
1080 
1081 int
virtio_transport_dgram_enqueue(struct vsock_sock * vsk,struct sockaddr_vm * remote_addr,struct msghdr * msg,size_t dgram_len)1082 virtio_transport_dgram_enqueue(struct vsock_sock *vsk,
1083 			       struct sockaddr_vm *remote_addr,
1084 			       struct msghdr *msg,
1085 			       size_t dgram_len)
1086 {
1087 	return -EOPNOTSUPP;
1088 }
1089 EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue);
1090 
1091 ssize_t
virtio_transport_stream_enqueue(struct vsock_sock * vsk,struct msghdr * msg,size_t len)1092 virtio_transport_stream_enqueue(struct vsock_sock *vsk,
1093 				struct msghdr *msg,
1094 				size_t len)
1095 {
1096 	struct virtio_vsock_pkt_info info = {
1097 		.op = VIRTIO_VSOCK_OP_RW,
1098 		.msg = msg,
1099 		.pkt_len = len,
1100 		.vsk = vsk,
1101 	};
1102 
1103 	return virtio_transport_send_pkt_info(vsk, &info);
1104 }
1105 EXPORT_SYMBOL_GPL(virtio_transport_stream_enqueue);
1106 
virtio_transport_destruct(struct vsock_sock * vsk)1107 void virtio_transport_destruct(struct vsock_sock *vsk)
1108 {
1109 	struct virtio_vsock_sock *vvs = vsk->trans;
1110 
1111 	kfree(vvs);
1112 }
1113 EXPORT_SYMBOL_GPL(virtio_transport_destruct);
1114 
virtio_transport_unsent_bytes(struct vsock_sock * vsk)1115 ssize_t virtio_transport_unsent_bytes(struct vsock_sock *vsk)
1116 {
1117 	struct virtio_vsock_sock *vvs = vsk->trans;
1118 	size_t ret;
1119 
1120 	spin_lock_bh(&vvs->tx_lock);
1121 	ret = vvs->bytes_unsent;
1122 	spin_unlock_bh(&vvs->tx_lock);
1123 
1124 	return ret;
1125 }
1126 EXPORT_SYMBOL_GPL(virtio_transport_unsent_bytes);
1127 
virtio_transport_reset(struct vsock_sock * vsk,struct sk_buff * skb)1128 static int virtio_transport_reset(struct vsock_sock *vsk,
1129 				  struct sk_buff *skb)
1130 {
1131 	struct virtio_vsock_pkt_info info = {
1132 		.op = VIRTIO_VSOCK_OP_RST,
1133 		.reply = !!skb,
1134 		.vsk = vsk,
1135 	};
1136 
1137 	/* Send RST only if the original pkt is not a RST pkt */
1138 	if (skb && le16_to_cpu(virtio_vsock_hdr(skb)->op) == VIRTIO_VSOCK_OP_RST)
1139 		return 0;
1140 
1141 	return virtio_transport_send_pkt_info(vsk, &info);
1142 }
1143 
1144 /* Normally packets are associated with a socket.  There may be no socket if an
1145  * attempt was made to connect to a socket that does not exist.
1146  */
virtio_transport_reset_no_sock(const struct virtio_transport * t,struct sk_buff * skb)1147 static int virtio_transport_reset_no_sock(const struct virtio_transport *t,
1148 					  struct sk_buff *skb)
1149 {
1150 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1151 	struct virtio_vsock_pkt_info info = {
1152 		.op = VIRTIO_VSOCK_OP_RST,
1153 		.type = le16_to_cpu(hdr->type),
1154 		.reply = true,
1155 	};
1156 	struct sk_buff *reply;
1157 
1158 	/* Send RST only if the original pkt is not a RST pkt */
1159 	if (le16_to_cpu(hdr->op) == VIRTIO_VSOCK_OP_RST)
1160 		return 0;
1161 
1162 	if (!t)
1163 		return -ENOTCONN;
1164 
1165 	reply = virtio_transport_alloc_skb(&info, 0, false,
1166 					   le64_to_cpu(hdr->dst_cid),
1167 					   le32_to_cpu(hdr->dst_port),
1168 					   le64_to_cpu(hdr->src_cid),
1169 					   le32_to_cpu(hdr->src_port));
1170 	if (!reply)
1171 		return -ENOMEM;
1172 
1173 	return t->send_pkt(reply);
1174 }
1175 
1176 /* This function should be called with sk_lock held and SOCK_DONE set */
virtio_transport_remove_sock(struct vsock_sock * vsk)1177 static void virtio_transport_remove_sock(struct vsock_sock *vsk)
1178 {
1179 	struct virtio_vsock_sock *vvs = vsk->trans;
1180 
1181 	/* We don't need to take rx_lock, as the socket is closing and we are
1182 	 * removing it.
1183 	 */
1184 	__skb_queue_purge(&vvs->rx_queue);
1185 	vsock_remove_sock(vsk);
1186 }
1187 
virtio_transport_wait_close(struct sock * sk,long timeout)1188 static void virtio_transport_wait_close(struct sock *sk, long timeout)
1189 {
1190 	if (timeout) {
1191 		DEFINE_WAIT_FUNC(wait, woken_wake_function);
1192 
1193 		add_wait_queue(sk_sleep(sk), &wait);
1194 
1195 		do {
1196 			if (sk_wait_event(sk, &timeout,
1197 					  sock_flag(sk, SOCK_DONE), &wait))
1198 				break;
1199 		} while (!signal_pending(current) && timeout);
1200 
1201 		remove_wait_queue(sk_sleep(sk), &wait);
1202 	}
1203 }
1204 
virtio_transport_do_close(struct vsock_sock * vsk,bool cancel_timeout)1205 static void virtio_transport_do_close(struct vsock_sock *vsk,
1206 				      bool cancel_timeout)
1207 {
1208 	struct sock *sk = sk_vsock(vsk);
1209 
1210 	sock_set_flag(sk, SOCK_DONE);
1211 	vsk->peer_shutdown = SHUTDOWN_MASK;
1212 	if (vsock_stream_has_data(vsk) <= 0)
1213 		sk->sk_state = TCP_CLOSING;
1214 	sk->sk_state_change(sk);
1215 
1216 	if (vsk->close_work_scheduled &&
1217 	    (!cancel_timeout || cancel_delayed_work(&vsk->close_work))) {
1218 		vsk->close_work_scheduled = false;
1219 
1220 		virtio_transport_remove_sock(vsk);
1221 
1222 		/* Release refcnt obtained when we scheduled the timeout */
1223 		sock_put(sk);
1224 	}
1225 }
1226 
virtio_transport_close_timeout(struct work_struct * work)1227 static void virtio_transport_close_timeout(struct work_struct *work)
1228 {
1229 	struct vsock_sock *vsk =
1230 		container_of(work, struct vsock_sock, close_work.work);
1231 	struct sock *sk = sk_vsock(vsk);
1232 
1233 	sock_hold(sk);
1234 	lock_sock(sk);
1235 
1236 	if (!sock_flag(sk, SOCK_DONE)) {
1237 		(void)virtio_transport_reset(vsk, NULL);
1238 
1239 		virtio_transport_do_close(vsk, false);
1240 	}
1241 
1242 	vsk->close_work_scheduled = false;
1243 
1244 	release_sock(sk);
1245 	sock_put(sk);
1246 }
1247 
1248 /* User context, vsk->sk is locked */
virtio_transport_close(struct vsock_sock * vsk)1249 static bool virtio_transport_close(struct vsock_sock *vsk)
1250 {
1251 	struct sock *sk = &vsk->sk;
1252 
1253 	if (!(sk->sk_state == TCP_ESTABLISHED ||
1254 	      sk->sk_state == TCP_CLOSING))
1255 		return true;
1256 
1257 	/* Already received SHUTDOWN from peer, reply with RST */
1258 	if ((vsk->peer_shutdown & SHUTDOWN_MASK) == SHUTDOWN_MASK) {
1259 		(void)virtio_transport_reset(vsk, NULL);
1260 		return true;
1261 	}
1262 
1263 	if ((sk->sk_shutdown & SHUTDOWN_MASK) != SHUTDOWN_MASK)
1264 		(void)virtio_transport_shutdown(vsk, SHUTDOWN_MASK);
1265 
1266 	if (sock_flag(sk, SOCK_LINGER) && !(current->flags & PF_EXITING))
1267 		virtio_transport_wait_close(sk, sk->sk_lingertime);
1268 
1269 	if (sock_flag(sk, SOCK_DONE)) {
1270 		return true;
1271 	}
1272 
1273 	sock_hold(sk);
1274 	INIT_DELAYED_WORK(&vsk->close_work,
1275 			  virtio_transport_close_timeout);
1276 	vsk->close_work_scheduled = true;
1277 	schedule_delayed_work(&vsk->close_work, VSOCK_CLOSE_TIMEOUT);
1278 	return false;
1279 }
1280 
virtio_transport_release(struct vsock_sock * vsk)1281 void virtio_transport_release(struct vsock_sock *vsk)
1282 {
1283 	struct sock *sk = &vsk->sk;
1284 	bool remove_sock = true;
1285 
1286 	if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)
1287 		remove_sock = virtio_transport_close(vsk);
1288 
1289 	if (remove_sock) {
1290 		sock_set_flag(sk, SOCK_DONE);
1291 		virtio_transport_remove_sock(vsk);
1292 	}
1293 }
1294 EXPORT_SYMBOL_GPL(virtio_transport_release);
1295 
1296 static int
virtio_transport_recv_connecting(struct sock * sk,struct sk_buff * skb)1297 virtio_transport_recv_connecting(struct sock *sk,
1298 				 struct sk_buff *skb)
1299 {
1300 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1301 	struct vsock_sock *vsk = vsock_sk(sk);
1302 	int skerr;
1303 	int err;
1304 
1305 	switch (le16_to_cpu(hdr->op)) {
1306 	case VIRTIO_VSOCK_OP_RESPONSE:
1307 		sk->sk_state = TCP_ESTABLISHED;
1308 		sk->sk_socket->state = SS_CONNECTED;
1309 		vsock_insert_connected(vsk);
1310 		sk->sk_state_change(sk);
1311 		break;
1312 	case VIRTIO_VSOCK_OP_INVALID:
1313 		break;
1314 	case VIRTIO_VSOCK_OP_RST:
1315 		skerr = ECONNRESET;
1316 		err = 0;
1317 		goto destroy;
1318 	default:
1319 		skerr = EPROTO;
1320 		err = -EINVAL;
1321 		goto destroy;
1322 	}
1323 	return 0;
1324 
1325 destroy:
1326 	virtio_transport_reset(vsk, skb);
1327 	sk->sk_state = TCP_CLOSE;
1328 	sk->sk_err = skerr;
1329 	sk_error_report(sk);
1330 	return err;
1331 }
1332 
1333 static void
virtio_transport_recv_enqueue(struct vsock_sock * vsk,struct sk_buff * skb)1334 virtio_transport_recv_enqueue(struct vsock_sock *vsk,
1335 			      struct sk_buff *skb)
1336 {
1337 	struct virtio_vsock_sock *vvs = vsk->trans;
1338 	bool can_enqueue, free_pkt = false;
1339 	struct virtio_vsock_hdr *hdr;
1340 	u32 len;
1341 
1342 	hdr = virtio_vsock_hdr(skb);
1343 	len = le32_to_cpu(hdr->len);
1344 
1345 	spin_lock_bh(&vvs->rx_lock);
1346 
1347 	can_enqueue = virtio_transport_inc_rx_pkt(vvs, len);
1348 	if (!can_enqueue) {
1349 		free_pkt = true;
1350 		goto out;
1351 	}
1352 
1353 	if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
1354 		vvs->msg_count++;
1355 
1356 	/* Try to copy small packets into the buffer of last packet queued,
1357 	 * to avoid wasting memory queueing the entire buffer with a small
1358 	 * payload.
1359 	 */
1360 	if (len <= GOOD_COPY_LEN && !skb_queue_empty(&vvs->rx_queue)) {
1361 		struct virtio_vsock_hdr *last_hdr;
1362 		struct sk_buff *last_skb;
1363 
1364 		last_skb = skb_peek_tail(&vvs->rx_queue);
1365 		last_hdr = virtio_vsock_hdr(last_skb);
1366 
1367 		/* If there is space in the last packet queued, we copy the
1368 		 * new packet in its buffer. We avoid this if the last packet
1369 		 * queued has VIRTIO_VSOCK_SEQ_EOM set, because this is
1370 		 * delimiter of SEQPACKET message, so 'pkt' is the first packet
1371 		 * of a new message.
1372 		 */
1373 		if (skb->len < skb_tailroom(last_skb) &&
1374 		    !(le32_to_cpu(last_hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)) {
1375 			memcpy(skb_put(last_skb, skb->len), skb->data, skb->len);
1376 			free_pkt = true;
1377 			last_hdr->flags |= hdr->flags;
1378 			le32_add_cpu(&last_hdr->len, len);
1379 			goto out;
1380 		}
1381 	}
1382 
1383 	__skb_queue_tail(&vvs->rx_queue, skb);
1384 
1385 out:
1386 	spin_unlock_bh(&vvs->rx_lock);
1387 	if (free_pkt)
1388 		kfree_skb(skb);
1389 }
1390 
1391 static int
virtio_transport_recv_connected(struct sock * sk,struct sk_buff * skb)1392 virtio_transport_recv_connected(struct sock *sk,
1393 				struct sk_buff *skb)
1394 {
1395 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1396 	struct vsock_sock *vsk = vsock_sk(sk);
1397 	int err = 0;
1398 
1399 	switch (le16_to_cpu(hdr->op)) {
1400 	case VIRTIO_VSOCK_OP_RW:
1401 		virtio_transport_recv_enqueue(vsk, skb);
1402 		vsock_data_ready(sk);
1403 		return err;
1404 	case VIRTIO_VSOCK_OP_CREDIT_REQUEST:
1405 		virtio_transport_send_credit_update(vsk);
1406 		break;
1407 	case VIRTIO_VSOCK_OP_CREDIT_UPDATE:
1408 		sk->sk_write_space(sk);
1409 		break;
1410 	case VIRTIO_VSOCK_OP_SHUTDOWN:
1411 		if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_RCV)
1412 			vsk->peer_shutdown |= RCV_SHUTDOWN;
1413 		if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SHUTDOWN_SEND)
1414 			vsk->peer_shutdown |= SEND_SHUTDOWN;
1415 		if (vsk->peer_shutdown == SHUTDOWN_MASK) {
1416 			if (vsock_stream_has_data(vsk) <= 0 && !sock_flag(sk, SOCK_DONE)) {
1417 				(void)virtio_transport_reset(vsk, NULL);
1418 				virtio_transport_do_close(vsk, true);
1419 			}
1420 			/* Remove this socket anyway because the remote peer sent
1421 			 * the shutdown. This way a new connection will succeed
1422 			 * if the remote peer uses the same source port,
1423 			 * even if the old socket is still unreleased, but now disconnected.
1424 			 */
1425 			vsock_remove_sock(vsk);
1426 		}
1427 		if (le32_to_cpu(virtio_vsock_hdr(skb)->flags))
1428 			sk->sk_state_change(sk);
1429 		break;
1430 	case VIRTIO_VSOCK_OP_RST:
1431 		virtio_transport_do_close(vsk, true);
1432 		break;
1433 	default:
1434 		err = -EINVAL;
1435 		break;
1436 	}
1437 
1438 	kfree_skb(skb);
1439 	return err;
1440 }
1441 
1442 static void
virtio_transport_recv_disconnecting(struct sock * sk,struct sk_buff * skb)1443 virtio_transport_recv_disconnecting(struct sock *sk,
1444 				    struct sk_buff *skb)
1445 {
1446 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1447 	struct vsock_sock *vsk = vsock_sk(sk);
1448 
1449 	if (le16_to_cpu(hdr->op) == VIRTIO_VSOCK_OP_RST)
1450 		virtio_transport_do_close(vsk, true);
1451 }
1452 
1453 static int
virtio_transport_send_response(struct vsock_sock * vsk,struct sk_buff * skb)1454 virtio_transport_send_response(struct vsock_sock *vsk,
1455 			       struct sk_buff *skb)
1456 {
1457 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1458 	struct virtio_vsock_pkt_info info = {
1459 		.op = VIRTIO_VSOCK_OP_RESPONSE,
1460 		.remote_cid = le64_to_cpu(hdr->src_cid),
1461 		.remote_port = le32_to_cpu(hdr->src_port),
1462 		.reply = true,
1463 		.vsk = vsk,
1464 	};
1465 
1466 	return virtio_transport_send_pkt_info(vsk, &info);
1467 }
1468 
virtio_transport_space_update(struct sock * sk,struct sk_buff * skb)1469 static bool virtio_transport_space_update(struct sock *sk,
1470 					  struct sk_buff *skb)
1471 {
1472 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1473 	struct vsock_sock *vsk = vsock_sk(sk);
1474 	struct virtio_vsock_sock *vvs = vsk->trans;
1475 	bool space_available;
1476 
1477 	/* Listener sockets are not associated with any transport, so we are
1478 	 * not able to take the state to see if there is space available in the
1479 	 * remote peer, but since they are only used to receive requests, we
1480 	 * can assume that there is always space available in the other peer.
1481 	 */
1482 	if (!vvs)
1483 		return true;
1484 
1485 	/* buf_alloc and fwd_cnt is always included in the hdr */
1486 	spin_lock_bh(&vvs->tx_lock);
1487 	vvs->peer_buf_alloc = le32_to_cpu(hdr->buf_alloc);
1488 	vvs->peer_fwd_cnt = le32_to_cpu(hdr->fwd_cnt);
1489 	space_available = virtio_transport_has_space(vsk);
1490 	spin_unlock_bh(&vvs->tx_lock);
1491 	return space_available;
1492 }
1493 
1494 /* Handle server socket */
1495 static int
virtio_transport_recv_listen(struct sock * sk,struct sk_buff * skb,struct virtio_transport * t)1496 virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb,
1497 			     struct virtio_transport *t)
1498 {
1499 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1500 	struct vsock_sock *vsk = vsock_sk(sk);
1501 	struct vsock_sock *vchild;
1502 	struct sock *child;
1503 	int ret;
1504 
1505 	if (le16_to_cpu(hdr->op) != VIRTIO_VSOCK_OP_REQUEST) {
1506 		virtio_transport_reset_no_sock(t, skb);
1507 		return -EINVAL;
1508 	}
1509 
1510 	if (sk_acceptq_is_full(sk)) {
1511 		virtio_transport_reset_no_sock(t, skb);
1512 		return -ENOMEM;
1513 	}
1514 
1515 	child = vsock_create_connected(sk);
1516 	if (!child) {
1517 		virtio_transport_reset_no_sock(t, skb);
1518 		return -ENOMEM;
1519 	}
1520 
1521 	sk_acceptq_added(sk);
1522 
1523 	lock_sock_nested(child, SINGLE_DEPTH_NESTING);
1524 
1525 	child->sk_state = TCP_ESTABLISHED;
1526 
1527 	vchild = vsock_sk(child);
1528 	vsock_addr_init(&vchild->local_addr, le64_to_cpu(hdr->dst_cid),
1529 			le32_to_cpu(hdr->dst_port));
1530 	vsock_addr_init(&vchild->remote_addr, le64_to_cpu(hdr->src_cid),
1531 			le32_to_cpu(hdr->src_port));
1532 
1533 	ret = vsock_assign_transport(vchild, vsk);
1534 	/* Transport assigned (looking at remote_addr) must be the same
1535 	 * where we received the request.
1536 	 */
1537 	if (ret || vchild->transport != &t->transport) {
1538 		release_sock(child);
1539 		virtio_transport_reset_no_sock(t, skb);
1540 		sock_put(child);
1541 		return ret;
1542 	}
1543 
1544 	if (virtio_transport_space_update(child, skb))
1545 		child->sk_write_space(child);
1546 
1547 	vsock_insert_connected(vchild);
1548 	vsock_enqueue_accept(sk, child);
1549 	virtio_transport_send_response(vchild, skb);
1550 
1551 	release_sock(child);
1552 
1553 	sk->sk_data_ready(sk);
1554 	return 0;
1555 }
1556 
virtio_transport_valid_type(u16 type)1557 static bool virtio_transport_valid_type(u16 type)
1558 {
1559 	return (type == VIRTIO_VSOCK_TYPE_STREAM) ||
1560 	       (type == VIRTIO_VSOCK_TYPE_SEQPACKET);
1561 }
1562 
1563 /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex
1564  * lock.
1565  */
virtio_transport_recv_pkt(struct virtio_transport * t,struct sk_buff * skb)1566 void virtio_transport_recv_pkt(struct virtio_transport *t,
1567 			       struct sk_buff *skb)
1568 {
1569 	struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb);
1570 	struct sockaddr_vm src, dst;
1571 	struct vsock_sock *vsk;
1572 	struct sock *sk;
1573 	bool space_available;
1574 
1575 	vsock_addr_init(&src, le64_to_cpu(hdr->src_cid),
1576 			le32_to_cpu(hdr->src_port));
1577 	vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid),
1578 			le32_to_cpu(hdr->dst_port));
1579 
1580 	trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port,
1581 					dst.svm_cid, dst.svm_port,
1582 					le32_to_cpu(hdr->len),
1583 					le16_to_cpu(hdr->type),
1584 					le16_to_cpu(hdr->op),
1585 					le32_to_cpu(hdr->flags),
1586 					le32_to_cpu(hdr->buf_alloc),
1587 					le32_to_cpu(hdr->fwd_cnt));
1588 
1589 	if (!virtio_transport_valid_type(le16_to_cpu(hdr->type))) {
1590 		(void)virtio_transport_reset_no_sock(t, skb);
1591 		goto free_pkt;
1592 	}
1593 
1594 	/* The socket must be in connected or bound table
1595 	 * otherwise send reset back
1596 	 */
1597 	sk = vsock_find_connected_socket(&src, &dst);
1598 	if (!sk) {
1599 		sk = vsock_find_bound_socket(&dst);
1600 		if (!sk) {
1601 			(void)virtio_transport_reset_no_sock(t, skb);
1602 			goto free_pkt;
1603 		}
1604 	}
1605 
1606 	if (virtio_transport_get_type(sk) != le16_to_cpu(hdr->type)) {
1607 		(void)virtio_transport_reset_no_sock(t, skb);
1608 		sock_put(sk);
1609 		goto free_pkt;
1610 	}
1611 
1612 	if (!skb_set_owner_sk_safe(skb, sk)) {
1613 		WARN_ONCE(1, "receiving vsock socket has sk_refcnt == 0\n");
1614 		goto free_pkt;
1615 	}
1616 
1617 	vsk = vsock_sk(sk);
1618 
1619 	lock_sock(sk);
1620 
1621 	/* Check if sk has been closed before lock_sock */
1622 	if (sock_flag(sk, SOCK_DONE)) {
1623 		(void)virtio_transport_reset_no_sock(t, skb);
1624 		release_sock(sk);
1625 		sock_put(sk);
1626 		goto free_pkt;
1627 	}
1628 
1629 	space_available = virtio_transport_space_update(sk, skb);
1630 
1631 	/* Update CID in case it has changed after a transport reset event */
1632 	if (vsk->local_addr.svm_cid != VMADDR_CID_ANY)
1633 		vsk->local_addr.svm_cid = dst.svm_cid;
1634 
1635 	if (space_available)
1636 		sk->sk_write_space(sk);
1637 
1638 	switch (sk->sk_state) {
1639 	case TCP_LISTEN:
1640 		virtio_transport_recv_listen(sk, skb, t);
1641 		kfree_skb(skb);
1642 		break;
1643 	case TCP_SYN_SENT:
1644 		virtio_transport_recv_connecting(sk, skb);
1645 		kfree_skb(skb);
1646 		break;
1647 	case TCP_ESTABLISHED:
1648 		virtio_transport_recv_connected(sk, skb);
1649 		break;
1650 	case TCP_CLOSING:
1651 		virtio_transport_recv_disconnecting(sk, skb);
1652 		kfree_skb(skb);
1653 		break;
1654 	default:
1655 		(void)virtio_transport_reset_no_sock(t, skb);
1656 		kfree_skb(skb);
1657 		break;
1658 	}
1659 
1660 	release_sock(sk);
1661 
1662 	/* Release refcnt obtained when we fetched this socket out of the
1663 	 * bound or connected list.
1664 	 */
1665 	sock_put(sk);
1666 	return;
1667 
1668 free_pkt:
1669 	kfree_skb(skb);
1670 }
1671 EXPORT_SYMBOL_GPL(virtio_transport_recv_pkt);
1672 
1673 /* Remove skbs found in a queue that have a vsk that matches.
1674  *
1675  * Each skb is freed.
1676  *
1677  * Returns the count of skbs that were reply packets.
1678  */
virtio_transport_purge_skbs(void * vsk,struct sk_buff_head * queue)1679 int virtio_transport_purge_skbs(void *vsk, struct sk_buff_head *queue)
1680 {
1681 	struct sk_buff_head freeme;
1682 	struct sk_buff *skb, *tmp;
1683 	int cnt = 0;
1684 
1685 	skb_queue_head_init(&freeme);
1686 
1687 	spin_lock_bh(&queue->lock);
1688 	skb_queue_walk_safe(queue, skb, tmp) {
1689 		if (vsock_sk(skb->sk) != vsk)
1690 			continue;
1691 
1692 		__skb_unlink(skb, queue);
1693 		__skb_queue_tail(&freeme, skb);
1694 
1695 		if (virtio_vsock_skb_reply(skb))
1696 			cnt++;
1697 	}
1698 	spin_unlock_bh(&queue->lock);
1699 
1700 	__skb_queue_purge(&freeme);
1701 
1702 	return cnt;
1703 }
1704 EXPORT_SYMBOL_GPL(virtio_transport_purge_skbs);
1705 
virtio_transport_read_skb(struct vsock_sock * vsk,skb_read_actor_t recv_actor)1706 int virtio_transport_read_skb(struct vsock_sock *vsk, skb_read_actor_t recv_actor)
1707 {
1708 	struct virtio_vsock_sock *vvs = vsk->trans;
1709 	struct sock *sk = sk_vsock(vsk);
1710 	struct virtio_vsock_hdr *hdr;
1711 	struct sk_buff *skb;
1712 	int off = 0;
1713 	int err;
1714 
1715 	spin_lock_bh(&vvs->rx_lock);
1716 	/* Use __skb_recv_datagram() for race-free handling of the receive. It
1717 	 * works for types other than dgrams.
1718 	 */
1719 	skb = __skb_recv_datagram(sk, &vvs->rx_queue, MSG_DONTWAIT, &off, &err);
1720 	if (!skb) {
1721 		spin_unlock_bh(&vvs->rx_lock);
1722 		return err;
1723 	}
1724 
1725 	hdr = virtio_vsock_hdr(skb);
1726 	if (le32_to_cpu(hdr->flags) & VIRTIO_VSOCK_SEQ_EOM)
1727 		vvs->msg_count--;
1728 
1729 	virtio_transport_dec_rx_pkt(vvs, le32_to_cpu(hdr->len));
1730 	spin_unlock_bh(&vvs->rx_lock);
1731 
1732 	virtio_transport_send_credit_update(vsk);
1733 
1734 	return recv_actor(sk, skb);
1735 }
1736 EXPORT_SYMBOL_GPL(virtio_transport_read_skb);
1737 
virtio_transport_notify_set_rcvlowat(struct vsock_sock * vsk,int val)1738 int virtio_transport_notify_set_rcvlowat(struct vsock_sock *vsk, int val)
1739 {
1740 	struct virtio_vsock_sock *vvs = vsk->trans;
1741 	bool send_update;
1742 
1743 	spin_lock_bh(&vvs->rx_lock);
1744 
1745 	/* If number of available bytes is less than new SO_RCVLOWAT value,
1746 	 * kick sender to send more data, because sender may sleep in its
1747 	 * 'send()' syscall waiting for enough space at our side. Also
1748 	 * don't send credit update when peer already knows actual value -
1749 	 * such transmission will be useless.
1750 	 */
1751 	send_update = (vvs->rx_bytes < val) &&
1752 		      (vvs->fwd_cnt != vvs->last_fwd_cnt);
1753 
1754 	spin_unlock_bh(&vvs->rx_lock);
1755 
1756 	if (send_update) {
1757 		int err;
1758 
1759 		err = virtio_transport_send_credit_update(vsk);
1760 		if (err < 0)
1761 			return err;
1762 	}
1763 
1764 	return 0;
1765 }
1766 EXPORT_SYMBOL_GPL(virtio_transport_notify_set_rcvlowat);
1767 
1768 MODULE_LICENSE("GPL v2");
1769 MODULE_AUTHOR("Asias He");
1770 MODULE_DESCRIPTION("common code for virtio vsock");
1771