Skip to contentSkip to conversation

mptcp: perform mptcp ack update from work queue

RFC Superseded Florian Westphal 5 participants 13 messages Feb 13, 2020 – Feb 17, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Florian Westphal

author
#0 lore raw

cover mptcp: perform mptcp ack update from work queue

This series adds support for async draining of subflow rx queue.

This allows mptcp to send up-to-data mptcp-level ack sequence even if userspace doesn't call recv for some time.

Originally I wanted to keep skbs accounted to the subflow socket, so as to not open the tcp receive window.
Problem with that is that we can trip over debug asserts via tcp_done() path, as in that case it might observe non-zero forward alloc counter to the tcp sk.

Therefore, this series orphans the skb instead and then charges it vs. the mptcp socket.

Note that the 2nd patch is not a must-have, if gets rejected patch 4 needs to be reworked to clone the skb. In such case we could consider using a delayed work queue to give the mptcp-program a head start to just drain the ssk directly.

See individual patches for more details, and as usual:
Comments welcome.

author
#1 lore raw

RFC 1/5 mptcp: perform mptcp ack update during read operations

tcp_read_sock() will drain the receive queue, i.e. it frees memory. Therefore, at the end of the function it can generate ACKs to open up the tcp receive window.

Unfortunately, the MPTCP ack sequence was updated later, i.e. those ACKs carry an outdated mptcp ack sequence.

This isn't a bug per se, but we can improve this and update the mptcp ack in the read actor callback, thus allowing the acks sent by tcp_read_sock() to include the most recent mptcp ack sequence.

Suggested-by Paolo Abeni <pabeni(a)redhat.com>
Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 4 +++-
 net/mptcp/protocol.h | 1 +
 2 files changed, 4 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +3 -1
index 1f405b812243..f68ee86376f0 100644
@@ -608,6 +608,8 @@ int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb,
 			desc->error = err;
 			return err;
 		}
+
+		arg->msk->ack_seq += copy_len;
 	} else {
 		pr_debug("Flushing skb payload");
 	}
@@ -664,6 +666,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	}
 
 	arg.msg = msg;
+	arg.msk = msk;
 	desc.arg.data = &arg;
 	desc.error = 0;
 
@@ -704,7 +707,6 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 
 			pr_debug("msk ack_seq=%llx -> %llx", msk->ack_seq,
 				 msk->ack_seq + bytes_read);
-			msk->ack_seq += bytes_read;
 			copied += bytes_read;
 			if (copied >= len) {
 				done = true;
net/mptcp/protocol.h +1 -0
index 3fbb33deb764..06a57bf6d5f0 100644
@@ -305,6 +305,7 @@ int mptcp_proto_v6_init(void);
 
 struct mptcp_read_arg {
 	struct msghdr *msg;
+	struct mptcp_sock *msk;
 };
 
 int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb,
Signature · 1 line
2.24.1
author
#3 lore raw

RFC 2/5 tcp: permit skb stealing in recv actor

In reply to Florian Westphal

Normal actor callbacks will copy skb data to some buffer, tcp_read_sock then frees those skbs.

For MPTCP, it makes sense to move skbs from the subflow socket receive queue to the mptcp socket receive queue.
It would be good to do so without the need for skb_clone or any other dynamic memory allocation.

This allows the mptcp actor to dequeue the skb from the ssk rx queue and add it to the tail of the msk rx queue.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/ipv4/tcp.c | 3 +++
 1 file changed, 3 insertions(+)
net/ipv4/tcp.c +3 -0
index eb2d80519f8e..bb374424a07c 100644
@@ -1581,6 +1581,9 @@ static struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off)
 	u32 offset;
 
 	while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
+		/* This can happen when sk_read_actor_t stole the skb */
+		if (unlikely(after(TCP_SKB_CB(skb)->seq, seq)))
+			break;
 		offset = seq - TCP_SKB_CB(skb)->seq;
 		if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
 			pr_err_once("%s: found a SYN, please report !\n", __func__);
Signature · 1 line
2.24.1
author
#4 lore raw

RFC 3/5 mptcp: add and use mptcp_data_ready helper

In reply to Florian Westphal

allows us to schedule the work queue to drain the ssk receive queue in a followup patch.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c |  8 ++++++++
 net/mptcp/protocol.h |  1 +
 net/mptcp/subflow.c  | 14 ++++----------
 3 files changed, 13 insertions(+), 10 deletions(-)
net/mptcp/protocol.c +8 -0
index f68ee86376f0..1ce5105e980a 100644
@@ -156,6 +156,14 @@ void mptcp_subflow_eof(struct sock *sk)
 		sock_hold(sk);
 }
 
+void mptcp_data_ready(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	set_bit(MPTCP_DATA_READY, &msk->flags);
+	sk->sk_data_ready(sk);
+}
+
 static void mptcp_stop_timer(struct sock *sk)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
net/mptcp/protocol.h +1 -0
index 06a57bf6d5f0..760607e3e65f 100644
@@ -318,6 +318,7 @@ void mptcp_finish_connect(struct sock *sk);
 void mptcp_subflow_eof(struct sock *sk);
 bool mptcp_finish_join(struct sock *sk);
 void mptcp_data_acked(struct sock *sk);
+void mptcp_data_ready(struct sock *sk);
 
 int mptcp_token_new_request(struct request_sock *req);
 void mptcp_token_destroy_request(u32 token);
net/mptcp/subflow.c +4 -10
index 154fb2b42243..c448a622e4c2 100644
@@ -723,11 +723,8 @@ static void subflow_data_ready(struct sock *sk)
 		return;
 	}
 
-	if (mptcp_subflow_data_available(sk)) {
-		set_bit(MPTCP_DATA_READY, &mptcp_sk(parent)->flags);
-
-		parent->sk_data_ready(parent);
-	}
+	if (mptcp_subflow_data_available(sk))
+		mptcp_data_ready(parent);
 }
 
 static void subflow_write_space(struct sock *sk)
@@ -919,11 +916,8 @@ static void subflow_state_change(struct sock *sk)
 	 * a fin packet carrying a DSS can be unnoticed if we don't trigger
 	 * the data available machinery here.
 	 */
-	if (parent && subflow->mp_capable && mptcp_subflow_data_available(sk)) {
-		set_bit(MPTCP_DATA_READY, &mptcp_sk(parent)->flags);
-
-		parent->sk_data_ready(parent);
-	}
+	if (parent && subflow->mp_capable && mptcp_subflow_data_available(sk))
+		mptcp_data_ready(parent);
 
 	if (parent && !(parent->sk_shutdown & RCV_SHUTDOWN) &&
 	    !subflow->rx_eof && subflow_is_done(sk)) {
Signature · 1 line
2.24.1
author
#5 lore raw

RFC 4/5 mptcp: update mptcp ack sequence from work queue

In reply to Florian Westphal

This adds a new worker flag to indicate when the work queue should drain a subflow socket.

skbs on the subflow socket are then placed on the mptcp socket receive queue (which was not used so far).
This allows us to announce the correct mptcp ack sequence in the tcp acks that we send back to the peer, even when the application does not call recv() on the mptcp socket for some time.

We still wake the userspace task that poll for events -- we do not depend on the work queue to have run: if the mptcp level receive queue is empty, skbs are taken from in-sequence subflow sockets.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 162 +++++++++++++++++++++++++++++++++++++++++++
 net/mptcp/protocol.h |   1 +
 2 files changed, 163 insertions(+)
net/mptcp/protocol.c +162 -0
index 1ce5105e980a..e9e87e62a184 100644
@@ -34,6 +34,12 @@ struct mptcp6_sock {
 
 static struct percpu_counter mptcp_sockets_allocated;
 
+struct mptcp_skb_cb {
+	u32 offset;
+};
+
+#define MPTCP_SKB_CB(__skb)	((struct mptcp_skb_cb *)&((__skb)->cb[0]))
+
 /* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not
  * completed yet or has failed, return the subflow socket.
  * Otherwise return NULL.
@@ -162,6 +168,13 @@ void mptcp_data_ready(struct sock *sk)
 
 	set_bit(MPTCP_DATA_READY, &msk->flags);
 	sk->sk_data_ready(sk);
+
+	if (test_and_set_bit(MPTCP_WORK_DATA_READY, &msk->flags))
+		return;
+
+	sock_hold(sk);
+	if (!schedule_work(&msk->rtx_work))
+		sock_put(sk);
 }
 
 static void mptcp_stop_timer(struct sock *sk)
@@ -643,6 +656,45 @@ static void mptcp_wait_data(struct sock *sk, long *timeo)
 	remove_wait_queue(sk_sleep(sk), &wait);
 }
 
+static int __mptcp_recvmsg_mskq(struct mptcp_sock *msk,
+				struct msghdr *msg,
+				size_t len)
+{
+	struct sock *sk = (struct sock *)msk;
+	struct sk_buff *skb;
+	int copied = 0;
+
+	while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
+		u32 offset = MPTCP_SKB_CB(skb)->offset;
+		u32 data_len = skb->len - offset;
+		u32 count = min_t(size_t, len - copied, data_len);
+		int err;
+
+		err = skb_copy_datagram_msg(skb, offset, msg, count);
+		if (unlikely(err < 0)) {
+			if (!copied)
+				return err;
+			break;
+		}
+
+		copied += count;
+
+		if (count < data_len) {
+			MPTCP_SKB_CB(skb)->offset += count;
+			break;
+		}
+
+		__skb_unlink(skb, &sk->sk_receive_queue);
+		__kfree_skb(skb);
+
+		if (copied >= len)
+			break;
+		break;
+	}
+
+	return copied;
+}
+
 static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 			 int nonblock, int flags, int *addr_len)
 {
@@ -688,6 +740,24 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 		u32 map_remaining;
 		int bytes_read;
 
+		bytes_read = __mptcp_recvmsg_mskq(msk, msg, len - copied);
+		if (unlikely(bytes_read < 0)) {
+			if (!copied)
+				copied = bytes_read;
+
+			goto out_err;
+		}
+
+		copied += bytes_read;
+
+		if (!skb_queue_empty(&sk->sk_receive_queue)) {
+			if (copied == len) {
+				more_data_avail = true;
+				done = true;
+			}
+			continue;
+		}
+
 		ssk = mptcp_subflow_recv_lookup(msk);
 		pr_debug("msk=%p ssk=%p", msk, ssk);
 		if (!ssk)
@@ -793,6 +863,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 			set_bit(MPTCP_DATA_READY, &msk->flags);
 	}
 
+out_err:
 	release_sock(sk);
 	return copied;
 }
@@ -906,6 +977,90 @@ static void mptcp_check_for_eof(struct mptcp_sock *msk)
 	}
 }
 
+struct mptcp_steal_arg {
+	struct mptcp_sock *msk;
+	struct sock *ssk;
+	bool fin;
+};
+
+static int mptcp_steal_actor(read_descriptor_t *desc, struct sk_buff *skb,
+			     unsigned int offset, size_t len)
+{
+	struct mptcp_steal_arg *arg = desc->arg.data;
+	struct sock *sk, *ssk;
+	size_t copy_len = min(desc->count, len);
+
+	sk = (struct sock *)arg->msk;
+	ssk = arg->ssk;
+
+	/* skb_orphan is not good (should keep skb charged to subflow).
+	 * Removing it causes WARN at ssk close time, as tcp_done() call
+	 * path may see ssk->sk_forward_alloc > 0.
+	 */
+	__skb_unlink(skb, &ssk->sk_receive_queue);
+	skb_orphan(skb);
+	__skb_queue_tail(&sk->sk_receive_queue, skb);
+
+	arg->msk->ack_seq += copy_len;
+	desc->count -= copy_len;
+	MPTCP_SKB_CB(skb)->offset = offset;
+	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
+		arg->fin = true;
+
+	return (int)copy_len;
+}
+
+static void __mptcp_move_skbs(struct mptcp_sock *msk)
+{
+	struct mptcp_steal_arg arg = {
+		.msk = msk,
+		.fin = false,
+	};
+	read_descriptor_t desc = {
+		.arg.data = &arg,
+	};
+
+	for (;;) {
+		struct mptcp_subflow_context *subflow;
+		bool more_data_avail;
+
+		arg.ssk = mptcp_subflow_recv_lookup(msk);
+		if (!arg.ssk)
+			break;
+
+		subflow = mptcp_subflow_ctx(arg.ssk);
+
+		lock_sock(arg.ssk);
+
+		do {
+			u32 map_remaining;
+			int bytes_read;
+
+			/* try to read as much data as available */
+			map_remaining = subflow->map_data_len -
+					mptcp_subflow_get_map_offset(subflow);
+			desc.count = map_remaining;
+
+			bytes_read = tcp_read_sock(arg.ssk, &desc, mptcp_steal_actor);
+			if (bytes_read <= 0) {
+				release_sock(arg.ssk);
+				return;
+			}
+
+			if (arg.fin) {
+				struct tcp_sock *tp = tcp_sk(arg.ssk);
+				u32 seq = READ_ONCE(tp->copied_seq);
+
+				WRITE_ONCE(tp->copied_seq, seq + 1);
+			}
+
+			more_data_avail = mptcp_subflow_data_available(arg.ssk);
+		} while (more_data_avail);
+
+		release_sock(arg.ssk);
+	}
+}
+
 static void mptcp_worker(struct work_struct *work)
 {
 	int orig_len, orig_offset, ret, mss_now = 0, size_goal = 0;
@@ -923,6 +1078,9 @@ static void mptcp_worker(struct work_struct *work)
 	lock_sock(sk);
 	mptcp_clean_una(sk);
 
+	if (test_and_clear_bit(MPTCP_WORK_DATA_READY, &msk->flags))
+		__mptcp_move_skbs(msk);
+
 	if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
 		mptcp_check_for_eof(msk);
 
@@ -1087,6 +1245,8 @@ static void mptcp_close(struct sock *sk, long timeout)
 
 	mptcp_cancel_work(sk);
 
+	__skb_queue_purge(&sk->sk_receive_queue);
+
 	sk_common_release(sk);
 }
 
@@ -1757,6 +1917,8 @@ void mptcp_proto_init(void)
 		panic("Failed to register MPTCP proto.\n");
 
 	inet_register_protosw(&mptcp_protosw);
+
+	BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb));
 }
 
 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
net/mptcp/protocol.h +1 -0
index 760607e3e65f..e755ed4f273d 100644
@@ -82,6 +82,7 @@
 #define MPTCP_SEND_SPACE	BIT(1)
 #define MPTCP_WORK_RTX		BIT(2)
 #define MPTCP_WORK_EOF		BIT(3)
+#define MPTCP_WORK_DATA_READY	BIT(4)
 
 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
 {
Signature · 1 line
2.24.1
author
#10 lore raw

RFC 5/5 mptcp: add rmem queue accounting

In reply to Florian Westphal
Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +13 -1
index e9e87e62a184..0583429b36ac 100644
@@ -172,6 +172,10 @@ void mptcp_data_ready(struct sock *sk)
 	if (test_and_set_bit(MPTCP_WORK_DATA_READY, &msk->flags))
 		return;
 
+	/* don't schedule if mptcp sk is (still) over limit */
+	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
+		return;
+
 	sock_hold(sk);
 	if (!schedule_work(&msk->rtx_work))
 		sock_put(sk);
@@ -993,12 +997,20 @@ static int mptcp_steal_actor(read_descriptor_t *desc, struct sk_buff *skb,
 	sk = (struct sock *)arg->msk;
 	ssk = arg->ssk;
 
+	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
+	    !sk_rmem_schedule(sk, skb, skb->truesize)) {
+		if (ssk->sk_rcvbuf <= sk->sk_rcvbuf)
+			return 0;
+
+		sk->sk_rcvbuf = ssk->sk_rcvbuf;
+	}
+
 	/* skb_orphan is not good (should keep skb charged to subflow).
 	 * Removing it causes WARN at ssk close time, as tcp_done() call
 	 * path may see ssk->sk_forward_alloc > 0.
 	 */
 	__skb_unlink(skb, &ssk->sk_receive_queue);
-	skb_orphan(skb);
+	skb_set_owner_r(skb, sk);
 	__skb_queue_tail(&sk->sk_receive_queue, skb);
 
 	arg->msk->ack_seq += copy_len;
Signature · 1 line
2.24.1
#6 lore raw

[MPTCP] Re: [RFC 4/5] mptcp: update mptcp ack sequence from work queue

In reply to Florian Westphal
Quoted 40 lines · Florian Westphal
net/mptcp/protocol.c +40 -0
+static void __mptcp_move_skbs(struct mptcp_sock *msk)
+{
+	struct mptcp_steal_arg arg = {
+		.msk = msk,
+		.fin = false,
+	};
+	read_descriptor_t desc = {
+		.arg.data = &arg,
+	};
+
+	for (;;) {
+		struct mptcp_subflow_context *subflow;
+		bool more_data_avail;
+
+		arg.ssk = mptcp_subflow_recv_lookup(msk);
+		if (!arg.ssk)
+			break;
+
+		subflow = mptcp_subflow_ctx(arg.ssk);
+
+		lock_sock(arg.ssk);
+
+		do {
+			u32 map_remaining;
+			int bytes_read;
+
+			/* try to read as much data as available */
+			map_remaining = subflow->map_data_len -
+					mptcp_subflow_get_map_offset(subflow);
+			desc.count = map_remaining;
+
+			bytes_read = tcp_read_sock(arg.ssk, &desc, mptcp_steal_actor);
+			if (bytes_read <= 0) {
+				release_sock(arg.ssk);
+				return;
+			}
+
+			if (arg.fin) {
+				struct tcp_sock *tp = tcp_sk(arg.ssk);
+				u32 seq = READ_ONCE(tp->copied_seq);

net/mptcp/protocol.c static void mptcp_check_for_eof(struct mptcp_sock *msk) · line 1062

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+
+				WRITE_ONCE(tp->copied_seq, seq + 1);
+			}
+
+			more_data_avail = mptcp_subflow_data_available(arg.ssk);
+		} while (more_data_avail);
+
+		release_sock(arg.ssk);
+	}
+}

Overall the above move the pending skbs to the msk receive queue, increment ssk copied_seq and eventually let TCP send ack via tcp_cleanup_rbuf().

I think we could avoid touching tcp_read_sock() in patch 2/5 moving the skb directly from __mptcp_move_skbs() - no call to tcp_read_sock(), just queue manipulation and copied_seq accounting.

Than __mptcp_move_skbs() will call directly, as needed - need to export it.

We could additionally clean-up the code calling __mptcp_move_skbs() from mptcp_recvmsg() before looking into the msk receive queue. Than no need for the current recvmsg inner loop.

I fear we need some tweek to handle TCP fallback. If I read the RFC correctly the msk can fallback to TCP even after receiving some DSS data (e.g. msk receive queue contains some skbs and the msk fallback to TCP. We can't call anymore sock_recvmsg())

WDYT?

Thanks,

Paolo

#11 lore raw

[MPTCP] Re: [RFC 5/5] mptcp: add rmem queue accounting

In reply to Florian Westphal
Quoted 10 lines · Florian Westphal
Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +0 -0
index e9e87e62a184..0583429b36ac 100644

net/mptcp/protocol.c void mptcp_data_ready(struct sock *sk) · line 178

Quoted 7 lines
net/mptcp/protocol.c +4 -0
@@ -172,6 +172,10 @@ void mptcp_data_ready(struct sock *sk)
 	if (test_and_set_bit(MPTCP_WORK_DATA_READY, &msk->flags))
 		return;
 
+	/* don't schedule if mptcp sk is (still) over limit */
+	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
+		return;
+

I think we need a READ_ONCE() around sk->sk_rcvbuf, as we don't held the sk socket lock here.

net/mptcp/protocol.c static int mptcp_steal_actor(read_descriptor_t *desc, struct sk_buff *skb, · line 1001

Quoted 9 lines
net/mptcp/protocol.c +2 -0
 	sock_hold(sk);
 	if (!schedule_work(&msk->rtx_work))
 		sock_put(sk);
@@ -993,12 +997,20 @@ static int mptcp_steal_actor(read_descriptor_t *desc, struct sk_buff *skb,
 	sk = (struct sock *)arg->msk;
 	ssk = arg->ssk;
 
+	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
+	    !sk_rmem_schedule(sk, skb, skb->truesize)) {

This has the side effect of possibly updating/decreasing 'sk_sndbuf' via sk_stream_moderate_sndbuf(). Do we need a similar update schema for msk->sk_sndbuf, too?

net/mptcp/protocol.c static int mptcp_steal_actor(read_descriptor_t *desc, struct sk_buff *skb, · line 1007

Quoted 6 lines
net/mptcp/protocol.c +6 -0
+		if (ssk->sk_rcvbuf <= sk->sk_rcvbuf)
+			return 0;
+
+		sk->sk_rcvbuf = ssk->sk_rcvbuf;
+	}
+

As an alternative, could we just do:

	rcvbuf = max(sk->sk_rcvbuf, ssk->sk_rcvbuf)
	if (rcvbuf > sk->sk_rcvbuf)
		sk->sk_rcvbuf = rcvbuf

before looping?

Thanks,

Paolo

#12 lore raw
In reply to Florian Westphal
Quoted 4 lines · Florian Westphal

This series adds support for async draining of subflow rx queue.

This allows mptcp to send up-to-data mptcp-level ack sequence even if userspace doesn't call recv for some time.

tested with:

// connection initiated by packetdrill
--tolerance_usecs=100000
../common/defaults.sh

+0 socket(..., SOCK_STREAM, IPPROTO_MPTCP) = 3
+0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0

+0 bind(3, ..., ...) = 0
+0 listen(3, 1) = 0

+0      < S 0:0(0) win 32792 <mss 1460,sackOK,nop,nop,nop,wscale 7,mpcapable v1 flags[flag_h] nokey>
+0      > S. 0:0(0) ack 1 <mss 1460,nop,nop,sackOK,nop,wscale 8,mpcapable v1 flags[flag_h] key[skey] >
+0.01   < . 1:1(0) ack 1 win 257 <mpcapable v1 flags[flag_h] key[ckey=2,skey]>

+0 accept(3, ..., ...) = 4

// ack 2 data segments, then read them

+0      < P. 1:101(100) ack 1 win 225 <dss dack8=1 dsn8=1 ssn=1 dll=100 nocs, nop, nop>
+0.01   > .  1:1(0) ack 101 <dss dack8=1 nocs>
+0.1    < P. 101:201(100) ack 1 win 225 <dss dack8=1 dsn8=101 ssn=101 dll=100 nocs, nop, nop>
+0.01   > .  1:1(0) ack 201 <dss dack8=101 nocs>

0.3 read(4, ..., 200) = 200
+0 close(4) = 0
+0.01 > F. 1:1(0) ack 201 <dss dack8=201 dsn8=1 ssn=0 dll=1 nocs fin, nop, nop>

Signature · 1 line
davide
#2 lore raw

[MPTCP] Re: [RFC 1/5] mptcp: perform mptcp ack update during read operations

In reply to Florian Westphal
Quoted 40 lines · Florian Westphal

tcp_read_sock() will drain the receive queue, i.e. it frees memory. Therefore, at the end of the function it can generate ACKs to open up the tcp receive window.

Unfortunately, the MPTCP ack sequence was updated later, i.e. those ACKs carry an outdated mptcp ack sequence.

This isn't a bug per se, but we can improve this and update the mptcp ack in the read actor callback, thus allowing the acks sent by tcp_read_sock() to include the most recent mptcp ack sequence.

Suggested-by Paolo Abeni <pabeni(a)redhat.com>
Signed-off-by Florian Westphal <fw(a)strlen.de>

---
net/mptcp/protocol.c | 4 +++-
net/mptcp/protocol.h | 1 +

2 files changed, 4 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +3 -0
index 1f405b812243..f68ee86376f0 100644
@@ -608,6 +608,8 @@ int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb,
 			desc->error = err;
 			return err;
 		}
+
+		arg->msk->ack_seq += copy_len;
 	} else {
 		pr_debug("Flushing skb payload");
 	}
@@ -664,6 +666,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	}
 
 	arg.msg = msg;
+	arg.msk = msk;
 	desc.arg.data = &arg;
 	desc.error = 0;
 

net/mptcp/protocol.c static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 709

Quoted 3 lines
net/mptcp/protocol.c +0 -0
@@ -704,7 +707,6 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 
 			pr_debug("msk ack_seq=%llx -> %llx", msk->ack_seq,
 				 msk->ack_seq + bytes_read);

This debug print will show misleading information because ack_seq has already advanced.

Quoted 18 lines
net/mptcp/protocol.c +0 -1
-			msk->ack_seq += bytes_read;
 			copied += bytes_read;
 			if (copied >= len) {
 				done = true;
net/mptcp/protocol.h +1 -0
index 3fbb33deb764..06a57bf6d5f0 100644
@@ -305,6 +305,7 @@ int mptcp_proto_v6_init(void);
 
 struct mptcp_read_arg {
 	struct msghdr *msg;
+	struct mptcp_sock *msk;
 };
 
 int mptcp_read_actor(read_descriptor_t *desc, struct sk_buff *skb,
Signature · 1 line
2.24.1

--
Mat Martineau
Intel

#7 lore raw

[MPTCP] Re: [RFC 4/5] mptcp: update mptcp ack sequence from work queue

In reply to Paolo Abeni
Quoted 47 lines · Paolo Abeni
Quoted 46 lines · Florian Westphal
net/mptcp/protocol.c +46 -0
+static void __mptcp_move_skbs(struct mptcp_sock *msk)
+{
+	struct mptcp_steal_arg arg = {
+		.msk = msk,
+		.fin = false,
+	};
+	read_descriptor_t desc = {
+		.arg.data = &arg,
+	};
+
+	for (;;) {
+		struct mptcp_subflow_context *subflow;
+		bool more_data_avail;
+
+		arg.ssk = mptcp_subflow_recv_lookup(msk);
+		if (!arg.ssk)
+			break;
+
+		subflow = mptcp_subflow_ctx(arg.ssk);
+
+		lock_sock(arg.ssk);
+
+		do {
+			u32 map_remaining;
+			int bytes_read;
+
+			/* try to read as much data as available */
+			map_remaining = subflow->map_data_len -
+					mptcp_subflow_get_map_offset(subflow);
+			desc.count = map_remaining;
+
+			bytes_read = tcp_read_sock(arg.ssk, &desc, mptcp_steal_actor);
+			if (bytes_read <= 0) {
+				release_sock(arg.ssk);
+				return;
+			}
+
+			if (arg.fin) {
+				struct tcp_sock *tp = tcp_sk(arg.ssk);
+				u32 seq = READ_ONCE(tp->copied_seq);
+
+				WRITE_ONCE(tp->copied_seq, seq + 1);
+			}
+
+			more_data_avail = mptcp_subflow_data_available(arg.ssk);
+		} while (more_data_avail);
Quoted 9 lines

net/mptcp/protocol.c static void mptcp_check_for_eof(struct mptcp_sock *msk) · line 1055

Quoted 4 lines
net/mptcp/protocol.c +4 -0
+
+		release_sock(arg.ssk);
+	}
+}

Overall the above move the pending skbs to the msk receive queue, increment ssk copied_seq and eventually let TCP send ack via tcp_cleanup_rbuf().

It's only moving in-order skbs, right?

Quoted 6 lines

I think we could avoid touching tcp_read_sock() in patch 2/5 moving the skb directly from __mptcp_move_skbs() - no call to tcp_read_sock(), just queue manipulation and copied_seq accounting.

Than __mptcp_move_skbs() will call directly, as needed - need to export it.

This makes sense to me. We have to do another layer of reassembly for MPTCP and we already know the skbs are in-order for the subflow stream, and there's no need to handle partial skb reads like tcp_read_sock does.

Quoted 9 lines

We could additionally clean-up the code calling __mptcp_move_skbs() from mptcp_recvmsg() before looking into the msk receive queue. Than no need for the current recvmsg inner loop.

I fear we need some tweek to handle TCP fallback. If I read the RFC correctly the msk can fallback to TCP even after receiving some DSS data (e.g. msk receive queue contains some skbs and the msk fallback to TCP. We can't call anymore sock_recvmsg())

RFC 8684 section 3.7 (Fallback) says that "If a subflow breaks during operation... the subflow SHOULD be treated as broken and closed with a RST". Does that cover your concern, or are you referring to a fallback at connection time?

--
Mat Martineau
Intel

#8 lore raw

[MPTCP] Re: [RFC 4/5] mptcp: update mptcp ack sequence from work queue

Quoted 81 lines · Mat Martineau
Quoted 47 lines · Paolo Abeni
Quoted 46 lines · Florian Westphal
net/mptcp/protocol.c +46 -0
+static void __mptcp_move_skbs(struct mptcp_sock *msk)
+{
+	struct mptcp_steal_arg arg = {
+		.msk = msk,
+		.fin = false,
+	};
+	read_descriptor_t desc = {
+		.arg.data = &arg,
+	};
+
+	for (;;) {
+		struct mptcp_subflow_context *subflow;
+		bool more_data_avail;
+
+		arg.ssk = mptcp_subflow_recv_lookup(msk);
+		if (!arg.ssk)
+			break;
+
+		subflow = mptcp_subflow_ctx(arg.ssk);
+
+		lock_sock(arg.ssk);
+
+		do {
+			u32 map_remaining;
+			int bytes_read;
+
+			/* try to read as much data as available */
+			map_remaining = subflow->map_data_len -
+					mptcp_subflow_get_map_offset(subflow);
+			desc.count = map_remaining;
+
+			bytes_read = tcp_read_sock(arg.ssk, &desc, mptcp_steal_actor);
+			if (bytes_read <= 0) {
+				release_sock(arg.ssk);
+				return;
+			}
+
+			if (arg.fin) {
+				struct tcp_sock *tp = tcp_sk(arg.ssk);
+				u32 seq = READ_ONCE(tp->copied_seq);
+
+				WRITE_ONCE(tp->copied_seq, seq + 1);
+			}
+
+			more_data_avail = mptcp_subflow_data_available(arg.ssk);
+		} while (more_data_avail);
Quoted 9 lines

net/mptcp/protocol.c static void mptcp_check_for_eof(struct mptcp_sock *msk) · line 1055

Quoted 4 lines
net/mptcp/protocol.c +4 -0
+
+		release_sock(arg.ssk);
+	}
+}

Overall the above move the pending skbs to the msk receive queue, increment ssk copied_seq and eventually let TCP send ack via tcp_cleanup_rbuf().

It's only moving in-order skbs, right?

Quoted 6 lines

I think we could avoid touching tcp_read_sock() in patch 2/5 moving the skb directly from __mptcp_move_skbs() - no call to tcp_read_sock(), just queue manipulation and copied_seq accounting.

Than __mptcp_move_skbs() will call directly, as needed - need to export it.

This makes sense to me. We have to do another layer of reassembly for MPTCP and we already know the skbs are in-order for the subflow stream, and there's no need to handle partial skb reads like tcp_read_sock does.

Quoted 8 lines

We could additionally clean-up the code calling __mptcp_move_skbs() from mptcp_recvmsg() before looking into the msk receive queue. Than no need for the current recvmsg inner loop.

I fear we need some tweek to handle TCP fallback. If I read the RFC correctly the msk can fallback to TCP even after receiving some DSS data (e.g. msk receive queue contains some skbs and the msk fallback to TCP. We can't call anymore sock_recvmsg())

Quoted 4 lines

RFC 8684 section 3.7 (Fallback) says that "If a subflow breaks during operation... the subflow SHOULD be treated as broken and closed with a RST". Does that cover your concern, or are you referring to a fallback at connection time?

uhm... it looks like fallback scenarios are quite rich/complex.

@Florian, I think this discussion in important, but does not relate strictly to this series; the problem is independent, and could be addresses separatelly - if we need to address anything ;)

The scenario I was thinking about is (e.g.):

- the client send a data segment + DSS mapping
- the server do not send DACKs, just plain TCP acks (because e.g. MPTCP options are allowed by middle-box after syn)
- the client should fall-back (we currently don't do any check there, so we don't) and sends some additional plain TCP data segments
- the server sends some data seg, and should fall-back for the same resons
- the server tries to read from the msk socket. The older data is in the msk receive queue, the newer in the subflow receive queue, if we call sock_recvmsg(), that will corrupt the stream.

I'm unsure if the above wording from 3.7 applies here: it think that the sentence "If a subflow breaks during operation" applies to the msk socket after that the first data segment is DACK-ed other, otherwise the 2 paragraph just before that[1] would be unneeded, right?

@Christoph, could you please assert the correct interpretation here? And what about the other critical fallback scenario discussed during the last mtg? (fallback due to out-of-order DACK / plain TCP ACK)

Thanks,

Paolo

[1] "If, however, an ACK is received for data (not just for the SYN)"...

#9 lore raw

[MPTCP] Re: [RFC 4/5] mptcp: update mptcp ack sequence from work queue

Hello,

Quoted 87 lines · Paolo Abeni
Quoted 81 lines · Mat Martineau
Quoted 47 lines · Paolo Abeni
Quoted 46 lines · Florian Westphal
net/mptcp/protocol.c +46 -0
+static void __mptcp_move_skbs(struct mptcp_sock *msk)
+{
+	struct mptcp_steal_arg arg = {
+		.msk = msk,
+		.fin = false,
+	};
+	read_descriptor_t desc = {
+		.arg.data = &arg,
+	};
+
+	for (;;) {
+		struct mptcp_subflow_context *subflow;
+		bool more_data_avail;
+
+		arg.ssk = mptcp_subflow_recv_lookup(msk);
+		if (!arg.ssk)
+			break;
+
+		subflow = mptcp_subflow_ctx(arg.ssk);
+
+		lock_sock(arg.ssk);
+
+		do {
+			u32 map_remaining;
+			int bytes_read;
+
+			/* try to read as much data as available */
+			map_remaining = subflow->map_data_len -
+					mptcp_subflow_get_map_offset(subflow);
+			desc.count = map_remaining;
+
+			bytes_read = tcp_read_sock(arg.ssk, &desc, mptcp_steal_actor);
+			if (bytes_read <= 0) {
+				release_sock(arg.ssk);
+				return;
+			}
+
+			if (arg.fin) {
+				struct tcp_sock *tp = tcp_sk(arg.ssk);
+				u32 seq = READ_ONCE(tp->copied_seq);
+
+				WRITE_ONCE(tp->copied_seq, seq + 1);
+			}
+
+			more_data_avail = mptcp_subflow_data_available(arg.ssk);
+		} while (more_data_avail);
Quoted 9 lines

net/mptcp/protocol.c static void mptcp_check_for_eof(struct mptcp_sock *msk) · line 1055

Quoted 4 lines
net/mptcp/protocol.c +4 -0
+
+		release_sock(arg.ssk);
+	}
+}

Overall the above move the pending skbs to the msk receive queue, increment ssk copied_seq and eventually let TCP send ack via tcp_cleanup_rbuf().

It's only moving in-order skbs, right?

Quoted 6 lines

I think we could avoid touching tcp_read_sock() in patch 2/5 moving the skb directly from __mptcp_move_skbs() - no call to tcp_read_sock(), just queue manipulation and copied_seq accounting.

Than __mptcp_move_skbs() will call directly, as needed - need to export it.

This makes sense to me. We have to do another layer of reassembly for MPTCP and we already know the skbs are in-order for the subflow stream, and there's no need to handle partial skb reads like tcp_read_sock does.

Quoted 8 lines

We could additionally clean-up the code calling __mptcp_move_skbs() from mptcp_recvmsg() before looking into the msk receive queue. Than no need for the current recvmsg inner loop.

I fear we need some tweek to handle TCP fallback. If I read the RFC correctly the msk can fallback to TCP even after receiving some DSS data (e.g. msk receive queue contains some skbs and the msk fallback to TCP. We can't call anymore sock_recvmsg())

Quoted 4 lines

RFC 8684 section 3.7 (Fallback) says that "If a subflow breaks during operation... the subflow SHOULD be treated as broken and closed with a RST". Does that cover your concern, or are you referring to a fallback at connection time?

Quoted 8 lines

uhm... it looks like fallback scenarios are quite rich/complex.

@Florian, I think this discussion in important, but does not relate strictly to this series; the problem is independent, and could be addresses separatelly - if we need to address anything ;)

The scenario I was thinking about is (e.g.):

I'm numbering the steps for easier reference:

Quoted 7 lines

1. the client send a data segment + DSS mapping
2. the server do not send DACKs, just plain TCP acks (because e.g. MPTCP options are allowed by middle-box after syn)
3. the client should fall-back (we currently don't do any check there, so we don't) and sends some additional plain TCP data segments
4. the server sends some data seg, and should fall-back for the same resons

*if* in Step 2 the server received data without a DSS-option he already fell back to regular TCP right away. Because at the beginning a sender MUST include the DSS-options in each segment:

"A sender MUST include a DSS option with data sequence mapping in
every segment until one of the sent segments has been acknowledged with a DSS option containing a Data ACK."

Nevertheless, the following steps can still happen if the middlebox is just in one direction (aka., server to client and thus the DATA_ACK gets removed).

Quoted 8 lines

5. the server tries to read from the msk socket. The older data is in the msk receive queue, the newer in the subflow receive queue, if we call sock_recvmsg(), that will corrupt the stream.

I'm unsure if the above wording from 3.7 applies here: it think that the sentence "If a subflow breaks during operation" applies to the msk socket after that the first data segment is DACK-ed other, otherwise the 2 paragraph just before that[1] would be unneeded, right?

Yes, this scenario here is about the start of the connection. and the paragraph [1] applies, as well as "In the case of such an ACK being received on the first subflow...".

Thus, once the sender receives the ACK without DATA_ACK, he sends an infinite mapping option (DSS With data-len == 0) to the server. That is the indication that it has to fallback. It is guaranteed that all data is in-order because the client should not yet have established a second subflow or sent data in an out-of-order way on the subflow.

Quoted 3 lines

@Christoph, could you please assert the correct interpretation here? And what about the other critical fallback scenario discussed during the last mtg? (fallback due to out-of-order DACK / plain TCP ACK)

If that happens, the sender will receive the plain TCP ACK and fallback to regular TCP. It will indicate that to the receiver with an infinite mapping. (Cfr.: "The sender will send one final data sequence mapping...").

Once the receiver receives that, he knows that he also has to fallback to regular TCP.

In general, it is best for a host to always include a DATA_ACK option in every segment that it sends. That avoids these kind of issues :-)

Christoph

Quoted 11 lines

Thanks,

Paolo

[1] "If, however, an ACK is received for data (not just for the SYN)"...