[PATCH mptcp-next v8 4/7] mptcp: add recv_should_stop helper

Geliang Tang posted 7 patches 2 months, 1 week ago
There is a newer version of this series
[PATCH mptcp-next v8 4/7] mptcp: add recv_should_stop helper
Posted by Geliang Tang 2 months, 1 week ago
From: Geliang Tang <tanggeliang@kylinos.cn>

Factor out a new helper mptcp_recv_should_stop() from mptcp_recvmsg() to
check whether to stop receiving. It will be used in mptcp_splice_read()
too.

Suggested-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 net/mptcp/protocol.c | 59 ++++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 27 deletions(-)

diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index fc429d175ede..1fe04ec05ebd 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -2090,12 +2090,40 @@ static unsigned int mptcp_inq_hint(const struct sock *sk)
 	return 0;
 }
 
+static int mptcp_recv_should_stop(struct sock *sk, long timeo, int *shutdown)
+{
+	if (sk->sk_err)
+		return sock_error(sk);
+
+	if (sk->sk_shutdown & RCV_SHUTDOWN) {
+		*shutdown = 1;
+		/* race breaker: the shutdown could be after the
+		 * previous receive queue check
+		 */
+		if (__mptcp_move_skbs(sk))
+			*shutdown = 0;
+		return 0;
+	}
+
+	if (sk->sk_state == TCP_CLOSE)
+		return -ENOTCONN;
+
+	if (!timeo)
+		return -EAGAIN;
+
+	if (signal_pending(current))
+		return sock_intr_errno(timeo);
+
+	return 0;
+}
+
 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 			 int flags, int *addr_len)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct scm_timestamping_internal tss;
 	int copied = 0, cmsg_flags = 0;
+	int shutdown = -1;
 	int target;
 	long timeo;
 
@@ -2146,34 +2174,11 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 			    signal_pending(current))
 				break;
 		} else {
-			if (sk->sk_err) {
-				copied = sock_error(sk);
+			copied = mptcp_recv_should_stop(sk, timeo, &shutdown);
+			if (copied < 0 || shutdown == 1)
 				break;
-			}
-
-			if (sk->sk_shutdown & RCV_SHUTDOWN) {
-				/* race breaker: the shutdown could be after the
-				 * previous receive queue check
-				 */
-				if (__mptcp_move_skbs(sk))
-					continue;
-				break;
-			}
-
-			if (sk->sk_state == TCP_CLOSE) {
-				copied = -ENOTCONN;
-				break;
-			}
-
-			if (!timeo) {
-				copied = -EAGAIN;
-				break;
-			}
-
-			if (signal_pending(current)) {
-				copied = sock_intr_errno(timeo);
-				break;
-			}
+			if (shutdown == 0)
+				continue;
 		}
 
 		pr_debug("block timeout %ld\n", timeo);
-- 
2.48.1