Skip to contentSkip to conversation

prepare interfaces for kTLS and sockmap support

PATCH New Geliang Tang 2 participants 6 messages Dec 3

Subsystems (3)
lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

v9 cover prepare interfaces for kTLS and sockmap support

From: Geliang Tang <[email protected]>

v9:
- Move SIOCINQ patches out of this set.
- patch 1, add app_limited flag in struct mptcp_sock, call tcp_rate_check_app_limited in __subflow_push_pending instead of mptcp_sendmsg.

v8:
- patch 1: change mptcp_inq_hint() to maintain TCP semantics by adding 1
to hint_val when RCV_SHUTDOWN is set or sk_state is TCP_CLOSE, matching TCP behavior where FIN consumes a sequence number.
- patch 2: update the test.
- patches 3-7: no changes.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v7:
- patch 1: add 1 to hint_val when RCV_SHUTDOWN is set to include the FIN
byte, matching the previous ack_seq - map_seq behavior. Simplify the
EOF condition accordingly.
- patch 2: replace poll()-based check with a wait_for_remote_key() helper
that polls MPTCP_INFO until MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED is set, ensuring the third ACK has been processed before checking SIOCINQ. Add SOL_MPTCP fallback definition and handle ENOPROTOOPT/EOPNOTSUPP for
TCP or fallback sockets.
- patches 3-7: no changes.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v6:
- patch 2: add Fixes tag, and use '-net' prefix.
- patch 3: remove poll() call, check SIOCINQ directly after accept().
- patch 6: add READ_ONCE() for msk->flags and msk->cb_flags reads in mptcp_rate_check_app_limited() to annotate intentionally racy accesses.
- update commit logs.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v5:
- patch 2: new patch - rewrite mptcp_inq_hint() to use mptcp_data_avail() instead of ack_seq - map_seq, completely avoiding TFO overflow issues.
- patch 3: new patch - add TFO SIOCINQ overflow detection in
mptcp_connect test to catch INT_MAX returns on TFO connections.
- patch 4: mptcp_inq() now returns INT_MAX instead of 0 when avail >=
INT_MAX. Removed mptcp_inq_hint() changes (moved to patch 2).
- patch 6: replace mptcp_rtx_head() check with MPTCP_WORK_RTX and MPTCP_RETRANSMIT flag checks in mptcp_rate_check_app_limited() to
properly detect pending retransmissions without data race issues.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v4:
- patch 2: fix mptcp_inq_hint() to return 0 instead of INT_MAX on
passive TFO overflow (ack_seq - map_seq >= INT_MAX).
- patch 4: add mptcp_rtx_head(sk) to the early-return check in mptcp_rate_check_app_limited() so pending retransmissions also
prevent false app-limited marking.
- patch 5: update comment message.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v3:
- patch 2: drop DATA_FIN subtraction in mptcp_inq(), remove the
intermediate 'answ' variable and return (unsigned int)avail
directly. Update commit message accordingly.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v2:
- drop the patch "prevent dummy DATA_FIN on RST in fallback mode".
- keep mptcp_inq_hint in mptcp_ioctl unchanged.
- use mptcp_data_avail in mptcp_inq.
- add msk_owned_by_me check in mptcp_sendmsg_locked.
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

v1:
- https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

This series was previously posted as part of "Reduce the differences between TCP and MPTCP for TLS usage" v15. It creates the mptcp_inq() and mptcp_sendmsg_locked() interfaces that are required for the upcoming MPTCP kTLS and MPTCP sockmap implementations.

The series has been rebased and no longer depends on Paolo's CB layout refactoring patches (mptcp: drop the mptcp_ooo_try_coalesce() helper,
mptcp: drop the cant_coalesce CB field, mptcp: remove CB offset field,
mptcp: sync mptcp skb cb layout with tcp one, mptcp: defer read_sock cleanup to mptcp_worker). Those patches are more complex and will be sent in a separate series.

Patch overview:

1. mptcp: update mptcp_check_readable
- Align with TCP's sock_is_readable for better TLS compatibility

2. mptcp: prevent dummy DATA_FIN on RST in fallback mode
- Fix semantic issue where RST on fallback connections would create dummy DATA_FIN, causing unnecessary state transitions
- Add sk_err check to subflow_is_done() to distinguish abortive closes (RST) from clean closes (FIN)

3. mptcp: implement peek_len for proto_ops
- Add mptcp_inq() to report readable bytes at MPTCP level
- Wire into .peek_len proto_ops for both IPv4 and IPv6
- Handle 32-bit torn reads by returning 0 instead of INT_MAX
- Prepare for TLS strparser integration

4. mptcp: add sendmsg_locked to proto_ops
- Split mptcp_sendmsg() into locked/unlocked variants
- Expose .sendmsg_locked in proto_ops for TLS send path

5. mptcp: track app-limited state in mptcp_sendmsg
- Add mptcp_rate_check_app_limited() to mark subflows as app-limited
- Bail out when MPTCP has unsent data (write_seq != snd_nxt or mptcp_send_head() != NULL) to avoid false app-limited marking
- Enable accurate delivery rate estimation for BBR

6. selftests: mptcp: sockopt: check app_limited
- Add regression test for app-limited accounting
- Verify tcpi_delivery_rate_app_limited == 1 after small transfers

Gang Yan (1):
mptcp: update mptcp_check_readable

Geliang Tang (3):
mptcp: track app-limited state in mptcp_sendmsg
selftests: mptcp: sockopt: check app_limited
mptcp: add sendmsg_locked to proto_ops

 net/mptcp/protocol.c                          | 49 ++++++++++++++++---
 net/mptcp/protocol.h                          |  3 +-
 .../selftests/net/mptcp/mptcp_sockopt.c       |  7 +++
 3 files changed, 51 insertions(+), 8 deletions(-)
Signature · 1 line
2.55.0
author reviewer
#1 lore raw

PATCH v9 1/4 mptcp: track app-limited state in mptcp_sendmsg

From: Geliang Tang <[email protected]>

Add mptcp_rate_check_app_limited() that evaluates MPTCP-level conditions (write_seq == snd_nxt, no pending dfrags, no pending retransmission) and stores the result in msk->app_limited. Called from mptcp_sendmsg() before the data copy loop, when the send queue reflects the state from the previous push. In __mptcp_push_pending(), if msk->app_limited is set, call tcp_rate_check_app_limited() on each scheduled subflow before pushing data. The subflow lock is already held, so no extra locking is needed. This allows BBR and similar congestion controllers to ignore rate samples taken while the connection is genuinely application-limited.

---
 net/mptcp/protocol.c | 20 ++++++++++++++++++++
 net/mptcp/protocol.h |  3 ++-
 2 files changed, 22 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +20 -0
index b9c6e5a615c8..56ecb38652d9 100644
@@ -1815,6 +1815,9 @@ static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
 	struct mptcp_data_frag *dfrag;
 	int len, copied = 0, err = 0;
 
+	if (msk->app_limited)
+		tcp_rate_check_app_limited(ssk);
+
 	while ((dfrag = mptcp_send_head(sk))) {
 		info->sent = dfrag->already_sent;
 		info->limit = dfrag->data_len;
@@ -2112,6 +2115,20 @@ static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
 	}
 }
 
+static void mptcp_rate_check_app_limited(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	unsigned long flags, cb_flags;
+
+	flags = READ_ONCE(msk->flags);
+	cb_flags = READ_ONCE(msk->cb_flags);
+
+	msk->app_limited = msk->write_seq == msk->snd_nxt &&
+			   !mptcp_send_head(sk) &&
+			   !test_bit(MPTCP_WORK_RTX, &flags) &&
+			   !test_bit(MPTCP_RETRANSMIT, &cb_flags);
+}
+
 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -2142,6 +2159,9 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 
 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
 
+	/* is sending application-limited? */
+	mptcp_rate_check_app_limited(sk);
+
 	if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
 		ret = sk_stream_wait_connect(sk, &timeo);
 		if (ret)
net/mptcp/protocol.h +2 -1
index 7cdd0a7e8296..97c6e3281acc 100644
@@ -349,7 +349,8 @@ struct mptcp_sock {
 			in_accept_queue:1,
 			free_first:1,
 			rcvspace_init:1,
-			fastclosing:1;
+			fastclosing:1,
+			app_limited:1;
 	u32		notsent_lowat;
 	int		keepalive_cnt;
 	int		keepalive_idle;
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH mptcp-next v9 1/4] mptcp: track app-limited state in mptcp_sendmsg

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mptcp: stale msk->app_limited across pushes in __subflow_push_pending() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mptcp: stale msk->app_limited across pushes in __subflow_push_pending()
--

commit 78a7bf230ea83e645dd5621be256b86ac1607b21
Author: Geliang Tang <[email protected]>

mptcp: track app-limited state in mptcp_sendmsg

This commit checks whether the MPTCP send queue is empty at the start of mptcp_sendmsg(), records that state in msk->app_limited, and calls tcp_rate_check_app_limited() on scheduled subflows in __subflow_push_pending().

Quoted 36 lines
net/mptcp/protocol.c +17 -0
index b9c6e5a615c8b..56ecb38652d9b 100644
@@ -1815,6 +1815,9 @@ static int __subflow_push_pending(struct sock *sk, struct sock *ssk,
 	struct mptcp_data_frag *dfrag;
 	int len, copied = 0, err = 0;
 
+	if (msk->app_limited)
+		tcp_rate_check_app_limited(ssk);
+
 	while ((dfrag = mptcp_send_head(sk))) {
 		info->sent = dfrag->already_sent;
 		info->limit = dfrag->data_len;
@@ -2112,6 +2115,20 @@ static void mptcp_rps_record_subflows(const struct mptcp_sock *msk)
 	}
 }
 
+static void mptcp_rate_check_app_limited(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	unsigned long flags, cb_flags;
+
+	flags = READ_ONCE(msk->flags);
+	cb_flags = READ_ONCE(msk->cb_flags);
+
+	msk->app_limited = msk->write_seq == msk->snd_nxt &&
+			   !mptcp_send_head(sk) &&
+			   !test_bit(MPTCP_WORK_RTX, &flags) &&
+			   !test_bit(MPTCP_RETRANSMIT, &cb_flags);
+}
+
 static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);

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

Quoted 5 lines
net/mptcp/protocol.c +2 -0
@@ -2142,6 +2159,9 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 
 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
 
+	/* is sending application-limited? */
+	mptcp_rate_check_app_limited(sk);

[Severity: Medium]
Does msk->app_limited stay set across subsequent pushes after an initial send on an idle socket?

mptcp_rate_check_app_limited() updates msk->app_limited only at the entry of mptcp_sendmsg(), and nothing clears it once data is queued in msk->rtx_queue and pushed. When a large mptcp_sendmsg() starts on an idle socket, msk->app_limited is set to 1 and remains 1 across wait_for_memory pushes and asynchronous ACK or NAPI pushes (__mptcp_check_push() -> __mptcp_subflow_push_pending() -> __subflow_push_pending()) while unsent dfrags are still waiting in msk->rtx_queue.

Because MPTCP spools dfrags into each subflow in bursts, an incoming ACK that drains a subflow's previous burst leaves that subflow with no queued write data right before __subflow_push_pending() copies the next dfrag. Seeing the stale msk->app_limited, tcp_rate_check_app_limited(ssk) marks tp->app_limited during bulk transfers, causing rate-based congestion controllers such as BBR to treat non-app-limited delivery samples as application-limited.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw

PATCH v9 2/4 selftests: mptcp: sockopt: check app_limited

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

After a small data exchange, assert tcpi_delivery_rate_app_limited == 1 and tcpi_delivery_rate > 0 when expect_all_features() is enabled.

rate_app_limited starts at 1 (tcp_init_sock) and is cleared only by non-app-limited rate samples. The preceding commit evaluates MPTCP-level conditions in mptcp_rate_check_app_limited(), stores the result in msk->app_limited, and calls tcp_rate_check_app_limited() on each scheduled subflow during push. This marks outgoing skbs as app-limited so rate samples don't clear rate_app_limited.

The tcpi_delivery_rate > 0 check confirms a rate sample was actually measured, not just the initial value of 1.

---
 tools/testing/selftests/net/mptcp/mptcp_sockopt.c | 7 +++++++
 1 file changed, 7 insertions(+)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +7 -0
index b8158a1198eb..f5605c7f3b99 100644
@@ -635,6 +635,13 @@ static void connect_one_server(int fd, int pipefd)
 	do_getsockopts(&s, fd, total, total);
 
 	assert(s.mptcpi_rcv_delta == (uint64_t)total);
+
+	/* app-limited accounting only checked under EXPECT_ALL_FEATURES */
+	if (expect_all_features()) {
+		assert(s.tcp_info.tcpi_delivery_rate_app_limited == 1);
+		assert(s.tcp_info.tcpi_delivery_rate > 0);
+	}
+
 	close(fd);
 }
 
Signature · 1 line
2.55.0
author reviewer
#4 lore raw

PATCH v9 3/4 mptcp: add sendmsg_locked to proto_ops

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

MPTCP currently provides a standard sendmsg() implementation which acquires and releases the socket lock internally. However, certain upper layers need to call the sendmsg method while the socket lock is already held.

Split the existing mptcp_sendmsg() into mptcp_sendmsg_locked() which assumes the caller holds the socket lock, and a tiny wrapper mptcp_sendmsg() that acquires the lock and calls the locked version. Add msk_owned_by_me() assertion to enforce the locking requirement. Expose .sendmsg_locked in both mptcp_stream_ops and mptcp_v6_stream_ops.

Unlike tcp_sendmsg_locked(), mptcp_sendmsg_locked() drops the MSG_SPLICE_PAGES flag from the mask. Future kTLS and sockmap support will require additional changes to call through proto_ops->sendmsg_locked instead of tcp_sendmsg_locked() directly.

---
 net/mptcp/protocol.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
net/mptcp/protocol.c +16 -4
index 56ecb38652d9..95be185dad75 100644
@@ -2129,7 +2129,7 @@ static void mptcp_rate_check_app_limited(struct sock *sk)
 			   !test_bit(MPTCP_RETRANSMIT, &cb_flags);
 }
 
-static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
+static int mptcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t len)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct page_frag *pfrag;
@@ -2137,12 +2137,12 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	int ret = 0;
 	long timeo;
 
+	msk_owned_by_me(msk);
+
 	/* silently ignore everything else */
 	msg->msg_flags &= MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
 			  MSG_FASTOPEN | MSG_EOR;
 
-	lock_sock(sk);
-
 	mptcp_rps_record_subflows(msk);
 
 	if (unlikely(inet_test_bit(DEFER_CONNECT, sk) ||
@@ -2261,7 +2261,6 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	}
 
 out:
-	release_sock(sk);
 	return copied;
 
 do_error:
@@ -2274,6 +2273,17 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	goto out;
 }
 
+static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
+{
+	int ret;
+
+	lock_sock(sk);
+	ret = mptcp_sendmsg_locked(sk, msg, len);
+	release_sock(sk);
+
+	return ret;
+}
+
 static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied);
 
 static void mptcp_eat_recv_skb(struct sock *sk, struct sk_buff *skb)
@@ -5092,6 +5102,7 @@ static const struct proto_ops mptcp_stream_ops = {
 	.set_rcvlowat	   = mptcp_set_rcvlowat,
 	.read_sock	   = mptcp_read_sock,
 	.splice_read	   = mptcp_splice_read,
+	.sendmsg_locked	   = mptcp_sendmsg_locked,
 };
 
 static struct inet_protosw mptcp_protosw = {
@@ -5211,6 +5222,7 @@ static const struct proto_ops mptcp_v6_stream_ops = {
 	.set_rcvlowat	   = mptcp_set_rcvlowat,
 	.read_sock	   = mptcp_read_sock,
 	.splice_read	   = mptcp_splice_read,
+	.sendmsg_locked	   = mptcp_sendmsg_locked,
 };
 
 static struct proto mptcp_v6_prot;
Signature · 1 line
2.55.0
author reviewer
#5 lore raw

PATCH v9 4/4 mptcp: update mptcp_check_readable

In reply to Geliang Tang

From: Gang Yan <[email protected]>

This patch makes mptcp_check_readable() aligned with TCP, and renames it to mptcp_stream_is_readable(). It will be used in the case of KTLS, because 'prot' will be modified, tls_sw_sock_is_readable() is expected to be called from prot->sock_is_readable().

---
 net/mptcp/protocol.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +6 -3
index 95be185dad75..164dfeee8669 100644
@@ -3736,9 +3736,11 @@ void __mptcp_unaccepted_force_close(struct sock *sk)
 	__mptcp_destroy_sock(sk);
 }
 
-static __poll_t mptcp_check_readable(struct sock *sk)
+static bool mptcp_stream_is_readable(struct sock *sk)
 {
-	return mptcp_epollin_ready(sk) ? EPOLLIN | EPOLLRDNORM : 0;
+	if (mptcp_epollin_ready(sk))
+		return true;
+	return sk_is_readable(sk);
 }
 
 static void mptcp_check_listen_stop(struct sock *sk)
@@ -4866,7 +4868,8 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 		mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
 
 	if (state != TCP_SYN_SENT && state != TCP_SYN_RECV) {
-		mask |= mptcp_check_readable(sk);
+		if (mptcp_stream_is_readable(sk))
+			mask |= EPOLLIN | EPOLLRDNORM;
 		if (shutdown & SEND_SHUTDOWN)
 			mask |= EPOLLOUT | EPOLLWRNORM;
 		else
Signature · 1 line
2.55.0