Skip to contentSkip to conversation

DATA_FIN support

PATCH Superseded Mat Martineau 3 participants 29 messages Jul 16, 2020 – Jul 22, 2020

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mat Martineau

author
#0 lore raw

cover DATA_FIN support

Here is a series adding DATA_FIN support with these improvements relative to the RFC patch:

* Split in to a series
* A few enhancements suggested by Paolo
* Lots of fixes, including handling of fallback sockets and connections that are already doing the disconnect handshake before accept() even returns.
* Self tests pass. I have not yet tested with mptcp_trunk...

See the patch 8 commit message for information about the disconnection state machine.

The TCP fallback tests would hang at disconnect when I removed the subflow EOF handling altogether. Instead I only enabled that code for fallback sockets (see patch 9).

Mat Martineau (12):
mptcp: Allow DATA_FIN in headers without TCP FIN
mptcp: Return EPIPE if sending is shut down during a sendmsg
mptcp: Add mptcp_close_state() helper
mptcp: Track received DATA_FIN sequence number and add related helpers
mptcp: Use MPTCP-level flag for sending DATA_FIN
mptcp: Add helper to process acks of DATA_FIN
mptcp: Send DATA_FIN in appropriate states during shutdown or close
mptcp: Use full MPTCP-level disconnect state machine
mptcp: Only use subflow EOF signaling on fallback connections
mptcp: Skip unnecessary skb extension allocation for bare acks
mptcp: Safely read sequence number when lock isn't held
mptcp: Safely store sequence number when sending data

 net/mptcp/options.c  |  54 ++++++--
 net/mptcp/protocol.c | 301 ++++++++++++++++++++++++++++++++++++-------
 net/mptcp/protocol.h |   6 +-
 net/mptcp/subflow.c  |  14 +-
 4 files changed, 309 insertions(+), 66 deletions(-)

base-commit: 9389d1bcffad728d53fc5860d8201c14d1c86042

Signature · 1 line
2.27.0
author
#1 lore raw

PATCH 1/12 mptcp: Allow DATA_FIN in headers without TCP FIN

RFC 8684-compliant DATA_FIN needs to be sent and ack'd before subflows are closed with TCP FIN, so write DATA_FIN DSS headers whenever their transmission has been enabled by the MPTCP connection-level socket.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
net/mptcp/options.c +3 -10
index 018025ae9f36..756a636d3d3a 100644
@@ -482,17 +482,10 @@ static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
 	struct mptcp_sock *msk;
 	unsigned int ack_size;
 	bool ret = false;
-	u8 tcp_fin;
 
-	if (skb) {
-		mpext = mptcp_get_ext(skb);
-		tcp_fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
-	} else {
-		mpext = NULL;
-		tcp_fin = 0;
-	}
+	mpext = skb ? mptcp_get_ext(skb) : NULL;
 
-	if (!skb || (mpext && mpext->use_map) || tcp_fin) {
+	if (!skb || (mpext && mpext->use_map) || subflow->data_fin_tx_enable) {
 		unsigned int map_size;
 
 		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
@@ -502,7 +495,7 @@ static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
 		if (mpext)
 			opts->ext_copy = *mpext;
 
-		if (skb && tcp_fin && subflow->data_fin_tx_enable)
+		if (skb && subflow->data_fin_tx_enable)
 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
 		ret = true;
 	}
Signature · 1 line
2.27.0
author
#2 lore raw

PATCH 2/12 mptcp: Return EPIPE if sending is shut down during a sendmsg

In reply to Mat Martineau

A MPTCP socket where sending has been shut down should not attempt to send additional data, since DATA_FIN has already been sent.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 5 +++++
 1 file changed, 5 insertions(+)
net/mptcp/protocol.c +5 -0
index 65d984f27fbb..7cdf451ea3da 100644
@@ -748,6 +748,11 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 restart:
 	mptcp_clean_una(sk);
 
+	if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
+		ret = -EPIPE;
+		goto out;
+	}
+
 wait_for_sndbuf:
 	__mptcp_flush_join_list(msk);
 	ssk = mptcp_subflow_get_send(msk);
Signature · 1 line
2.27.0
author
#3 lore raw

PATCH 3/12 mptcp: Add mptcp_close_state() helper

In reply to Mat Martineau

This will be used to transition to the appropriate state on close and determine if a DATA_FIN needs to be sent for that state transition.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +27 -1
index 7cdf451ea3da..69100187490f 100644
@@ -1424,7 +1424,33 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how,
 	release_sock(ssk);
 }
 
-/* Called with msk lock held, releases such lock before returning */
+static const unsigned char new_state[16] = {
+  /* current state:        new state:      action:	*/
+  [0 /* (Invalid) */]	= TCP_CLOSE,
+  [TCP_ESTABLISHED]	= TCP_FIN_WAIT1 | TCP_ACTION_FIN,
+  [TCP_SYN_SENT]	= TCP_CLOSE,
+  [TCP_SYN_RECV]	= TCP_FIN_WAIT1 | TCP_ACTION_FIN,
+  [TCP_FIN_WAIT1]	= TCP_FIN_WAIT1,
+  [TCP_FIN_WAIT2]	= TCP_FIN_WAIT2,
+  [TCP_TIME_WAIT]	= TCP_CLOSE,	/* should not happen ! */
+  [TCP_CLOSE]		= TCP_CLOSE,
+  [TCP_CLOSE_WAIT]	= TCP_LAST_ACK  | TCP_ACTION_FIN,
+  [TCP_LAST_ACK]	= TCP_LAST_ACK,
+  [TCP_LISTEN]		= TCP_CLOSE,
+  [TCP_CLOSING]		= TCP_CLOSING,
+  [TCP_NEW_SYN_RECV]	= TCP_CLOSE,	/* should not happen ! */
+};
+
+static int mptcp_close_state(struct sock *sk)
+{
+	int next = (int)new_state[sk->sk_state];
+	int ns = next & TCP_STATE_MASK;
+
+	inet_sk_state_store(sk, ns);
+
+	return next & TCP_ACTION_FIN;
+}
+
 static void mptcp_close(struct sock *sk, long timeout)
 {
 	struct mptcp_subflow_context *subflow, *tmp;
Signature · 1 line
2.27.0
#4 lore raw

[MPTCP] Re: [PATCH mptcp-next 03/12] mptcp: Add mptcp_close_state() helper

Quoted 13 lines · Mat Martineau

This will be used to transition to the appropriate state on close and determine if a DATA_FIN needs to be sent for that state transition.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +0 -0
index 7cdf451ea3da..69100187490f 100644

net/mptcp/protocol.c static void mptcp_subflow_shutdown(struct sock *ssk, int how, · line 1427

Quoted 4 lines
net/mptcp/protocol.c +0 -1
@@ -1424,7 +1424,33 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how,
 	release_sock(ssk);
 }
 
-/* Called with msk lock held, releases such lock before returning */

Beware! this removes the above commit message, likely in an unintended way.

/P

author
#5 lore raw

[MPTCP] Re: [PATCH mptcp-next 03/12] mptcp: Add mptcp_close_state() helper

Quoted 17 lines · Paolo Abeni
Quoted 16 lines · Mat Martineau

This will be used to transition to the appropriate state on close and determine if a DATA_FIN needs to be sent for that state transition.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +0 -0
index 7cdf451ea3da..69100187490f 100644
@@ -1424,7 +1424,33 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how,
 	release_sock(ssk);
 }
 
Quoted 5 lines

net/mptcp/protocol.c static void mptcp_subflow_shutdown(struct sock *ssk, int how, · line 1427

Quoted 1 line
net/mptcp/protocol.c +0 -1
-/* Called with msk lock held, releases such lock before returning */

Beware! this removes the above commit message, likely in an unintended way.

Looks like I didn't split this commit correctly. The comment looked invalid since the msk lock is aquired at the beginning of mptcp_close, if the function was called with the msk lock held it would deadlock.

--
Mat Martineau
Intel

author
#6 lore raw

PATCH 4/12 mptcp: Track received DATA_FIN sequence number and add related helpers

In reply to Mat Martineau

Incoming DATA_FIN headers need to propagate the presence of the DATA_FIN bit and the associated sequence number to the MPTCP layer, even when arriving on a bare ACK that does not get added to the receive queue. Add structure members to store the DATA_FIN information and helpers to set and check those values.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c  |  16 +++++++
 net/mptcp/protocol.c | 100 ++++++++++++++++++++++++++++++++++++++-----
 net/mptcp/protocol.h |   3 ++
 3 files changed, 109 insertions(+), 10 deletions(-)
net/mptcp/options.c +16 -0
index 756a636d3d3a..db34306eb73f 100644
@@ -775,6 +775,22 @@ static void update_una(struct mptcp_sock *msk,
 	}
 }
 
+bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq)
+{
+	/* Skip if DATA_FIN was already received.
+	 * If updating simultaneously with the recvmsg loop, values
+	 * should match. If they mismatch, the peer is misbehaving and
+	 * we will prefer the most recent information.
+	 */
+	if (READ_ONCE(msk->rcv_data_fin) || !READ_ONCE(msk->first))
+		return false;
+
+	WRITE_ONCE(msk->rcv_data_fin_seq, data_fin_seq);
+	WRITE_ONCE(msk->rcv_data_fin, 1);
+
+	return true;
+}
+
 static bool add_addr_hmac_valid(struct mptcp_sock *msk,
 				struct mptcp_options_received *mp_opt)
 {
net/mptcp/protocol.c +90 -10

118 lines not on this page · raw message

net/mptcp/protocol.h +3 -0
index 08198b61b8a6..813c9d5c59f2 100644
@@ -193,11 +193,13 @@ struct mptcp_sock {
 	u64		remote_key;
 	u64		write_seq;
 	u64		ack_seq;
+	u64		rcv_data_fin_seq;
 	atomic64_t	snd_una;
 	unsigned long	timer_ival;
 	u32		token;
 	unsigned long	flags;
 	bool		can_ack;
+	bool		rcv_data_fin;
 	spinlock_t	join_list_lock;
 	struct work_struct work;
 	struct list_head conn_list;
@@ -380,6 +382,7 @@ void mptcp_data_ready(struct sock *sk, struct sock *ssk);
 bool mptcp_finish_join(struct sock *sk);
 void mptcp_data_acked(struct sock *sk);
 void mptcp_subflow_eof(struct sock *sk);
+bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq);
 
 void __init mptcp_token_init(void);
 static inline void mptcp_token_init_request(struct request_sock *req)
Signature · 1 line
2.27.0
#7 lore raw

[MPTCP] Re: [PATCH mptcp-next 04/12] mptcp: Track received DATA_FIN sequence number and add related helpers

Quoted 93 lines · Mat Martineau

Incoming DATA_FIN headers need to propagate the presence of the DATA_FIN bit and the associated sequence number to the MPTCP layer, even when arriving on a bare ACK that does not get added to the receive queue. Add structure members to store the DATA_FIN information and helpers to set and check those values.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c  |  16 +++++++
 net/mptcp/protocol.c | 100 ++++++++++++++++++++++++++++++++++++++-----
 net/mptcp/protocol.h |   3 ++
 3 files changed, 109 insertions(+), 10 deletions(-)
net/mptcp/options.c +16 -0
index 756a636d3d3a..db34306eb73f 100644
@@ -775,6 +775,22 @@ static void update_una(struct mptcp_sock *msk,
 	}
 }
 
+bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq)
+{
+	/* Skip if DATA_FIN was already received.
+	 * If updating simultaneously with the recvmsg loop, values
+	 * should match. If they mismatch, the peer is misbehaving and
+	 * we will prefer the most recent information.
+	 */
+	if (READ_ONCE(msk->rcv_data_fin) || !READ_ONCE(msk->first))
+		return false;
+
+	WRITE_ONCE(msk->rcv_data_fin_seq, data_fin_seq);
+	WRITE_ONCE(msk->rcv_data_fin, 1);
+
+	return true;
+}
+
 static bool add_addr_hmac_valid(struct mptcp_sock *msk,
 				struct mptcp_options_received *mp_opt)
 {
net/mptcp/protocol.c +38 -0
index 69100187490f..43e48327902e 100644
@@ -16,6 +16,7 @@
 #include <net/inet_hashtables.h>
 #include <net/protocol.h>
 #include <net/tcp.h>
+#include <net/tcp_states.h>
 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
 #include <net/transp_v6.h>
 #endif
@@ -163,6 +164,95 @@ static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk,
 	return mptcp_subflow_data_available(ssk);
 }
 
+static bool mptcp_pending_data_fin(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	return (READ_ONCE(msk->rcv_data_fin) &&
+		((1 << sk->sk_state) &
+		 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2)) &&
+		msk->ack_seq == READ_ONCE(msk->rcv_data_fin_seq));
+}
+
+static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
+{
+	long tout = ssk && inet_csk(ssk)->icsk_pending ?
+				      inet_csk(ssk)->icsk_timeout - jiffies : 0;
+
+	if (tout <= 0)
+		tout = mptcp_sk(sk)->timer_ival;
+	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
+}
+
+static void mptcp_check_data_fin(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	if (__mptcp_check_fallback(msk) || !msk->first)
+		return;
+
+	/* Need to ack a DATA_FIN received from a peer while this side
+	 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
+	 * msk->rcv_data_fin was set when parsing the incoming options
+	 * at the subflow level and the msk lock was not held, so this
+	 * is the first opportunity to act on the DATA_FIN and change
+	 * the msk state.
+	 *
+	 * If we are caught up to the sequence number of the incoming
+	 * DATA_FIN, send the DATA_ACK now and do state transition.  If
+	 * not caught up, do nothing and let the recv code send DATA_ACK

net/mptcp/protocol.c static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, · line 213

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+	 * when catching up.
+	 */
+
+	if (READ_ONCE(msk->rcv_data_fin) &&
+	    ((1 << sk->sk_state) &
+	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
+		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
+		struct mptcp_subflow_context *subflow;
+
+		if (msk->ack_seq + 1 == rcv_data_fin_seq) {

This is quite alike mptcp_pending_data_fin(), so perhaps the helper could be reused here? see also the comment on patch 8/12.

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+			msk->ack_seq++;
+			WRITE_ONCE(msk->rcv_data_fin, 0);
+
+			sk->sk_shutdown |= RCV_SHUTDOWN;
+
+			switch (sk->sk_state) {
+			case TCP_ESTABLISHED:
+				inet_sk_state_store(sk, TCP_CLOSE_WAIT);
+				break;
+			case TCP_FIN_WAIT1:

net/mptcp/protocol.c static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, · line 233

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+				inet_sk_state_store(sk, TCP_CLOSING);
+				break;
+			case TCP_FIN_WAIT2:
+				inet_sk_state_store(sk, TCP_CLOSE);
+				// @@ Close subflows now?
+				break;
+			default:
+				/* Other states not expected */
+				WARN_ON_ONCE(1);
+				break;

it's a pity we can't use mptcp_close_state() here.

Quoted 50 lines
net/mptcp/protocol.c +22 -10
+			}
+
+			mptcp_set_timeout(sk, NULL);
+			mptcp_for_each_subflow(msk, subflow) {
+				struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
+
+				lock_sock(ssk);
+				tcp_send_ack(ssk);
+				release_sock(ssk);
+			}
+
+			sk->sk_state_change(sk);
+
+			if (sk->sk_shutdown == SHUTDOWN_MASK ||
+			    sk->sk_state == TCP_CLOSE)
+				sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
+			else
+				sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
+		}
+	}
+}
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk,
 					   unsigned int *bytes)
@@ -303,16 +393,6 @@ static void __mptcp_flush_join_list(struct mptcp_sock *msk)
 	spin_unlock_bh(&msk->join_list_lock);
 }
 
-static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
-{
-	long tout = ssk && inet_csk(ssk)->icsk_pending ?
-				      inet_csk(ssk)->icsk_timeout - jiffies : 0;
-
-	if (tout <= 0)
-		tout = mptcp_sk(sk)->timer_ival;
-	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
-}
-
 static bool mptcp_timer_pending(struct sock *sk)
 {
 	return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
net/mptcp/protocol.h +0 -0
index 08198b61b8a6..813c9d5c59f2 100644
@@ -193,11 +193,13 @@ struct mptcp_sock {
 	u64		remote_key;
 	u64		write_seq;
 	u64		ack_seq;

net/mptcp/protocol.h struct mptcp_sock { · line 205

Quoted 10 lines
net/mptcp/protocol.h +2 -0
+	u64		rcv_data_fin_seq;
 	atomic64_t	snd_una;
 	unsigned long	timer_ival;
 	u32		token;
 	unsigned long	flags;
 	bool		can_ack;
+	bool		rcv_data_fin;
 	spinlock_t	join_list_lock;
 	struct work_struct work;
 	struct list_head conn_list;

Note: we should revisit mptcp_sock binary layout someday ;)

/P

author
#8 lore raw

[MPTCP] Re: [PATCH mptcp-next 04/12] mptcp: Track received DATA_FIN sequence number and add related helpers

Quoted 99 lines · Paolo Abeni
Quoted 98 lines · Mat Martineau

Incoming DATA_FIN headers need to propagate the presence of the DATA_FIN bit and the associated sequence number to the MPTCP layer, even when arriving on a bare ACK that does not get added to the receive queue. Add structure members to store the DATA_FIN information and helpers to set and check those values.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c  |  16 +++++++
 net/mptcp/protocol.c | 100 ++++++++++++++++++++++++++++++++++++++-----
 net/mptcp/protocol.h |   3 ++
 3 files changed, 109 insertions(+), 10 deletions(-)
net/mptcp/options.c +16 -0
index 756a636d3d3a..db34306eb73f 100644
@@ -775,6 +775,22 @@ static void update_una(struct mptcp_sock *msk,
 	}
 }
 
+bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq)
+{
+	/* Skip if DATA_FIN was already received.
+	 * If updating simultaneously with the recvmsg loop, values
+	 * should match. If they mismatch, the peer is misbehaving and
+	 * we will prefer the most recent information.
+	 */
+	if (READ_ONCE(msk->rcv_data_fin) || !READ_ONCE(msk->first))
+		return false;
+
+	WRITE_ONCE(msk->rcv_data_fin_seq, data_fin_seq);
+	WRITE_ONCE(msk->rcv_data_fin, 1);
+
+	return true;
+}
+
 static bool add_addr_hmac_valid(struct mptcp_sock *msk,
 				struct mptcp_options_received *mp_opt)
 {
net/mptcp/protocol.c +43 -0
index 69100187490f..43e48327902e 100644
@@ -16,6 +16,7 @@
 #include <net/inet_hashtables.h>
 #include <net/protocol.h>
 #include <net/tcp.h>
+#include <net/tcp_states.h>
 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
 #include <net/transp_v6.h>
 #endif
@@ -163,6 +164,95 @@ static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk,
 	return mptcp_subflow_data_available(ssk);
 }
 
+static bool mptcp_pending_data_fin(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	return (READ_ONCE(msk->rcv_data_fin) &&
+		((1 << sk->sk_state) &
+		 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2)) &&
+		msk->ack_seq == READ_ONCE(msk->rcv_data_fin_seq));
+}
+
+static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
+{
+	long tout = ssk && inet_csk(ssk)->icsk_pending ?
+				      inet_csk(ssk)->icsk_timeout - jiffies : 0;
+
+	if (tout <= 0)
+		tout = mptcp_sk(sk)->timer_ival;
+	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
+}
+
+static void mptcp_check_data_fin(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	if (__mptcp_check_fallback(msk) || !msk->first)
+		return;
+
+	/* Need to ack a DATA_FIN received from a peer while this side
+	 * of the connection is in ESTABLISHED, FIN_WAIT1, or FIN_WAIT2.
+	 * msk->rcv_data_fin was set when parsing the incoming options
+	 * at the subflow level and the msk lock was not held, so this
+	 * is the first opportunity to act on the DATA_FIN and change
+	 * the msk state.
+	 *
+	 * If we are caught up to the sequence number of the incoming
+	 * DATA_FIN, send the DATA_ACK now and do state transition.  If
+	 * not caught up, do nothing and let the recv code send DATA_ACK
+	 * when catching up.
+	 */
+
+	if (READ_ONCE(msk->rcv_data_fin) &&
+	    ((1 << sk->sk_state) &
Quoted 9 lines

net/mptcp/protocol.c static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, · line 213

Quoted 5 lines
net/mptcp/protocol.c +5 -0
+	     (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2))) {
+		u64 rcv_data_fin_seq = READ_ONCE(msk->rcv_data_fin_seq);
+		struct mptcp_subflow_context *subflow;
+
+		if (msk->ack_seq + 1 == rcv_data_fin_seq) {

This is quite alike mptcp_pending_data_fin(), so perhaps the helper could be reused here? see also the comment on patch 8/12.

Right, I'll fix that.

Quoted 14 lines
Quoted 14 lines
net/mptcp/protocol.c +14 -0
+			msk->ack_seq++;
+			WRITE_ONCE(msk->rcv_data_fin, 0);
+
+			sk->sk_shutdown |= RCV_SHUTDOWN;
+
+			switch (sk->sk_state) {
+			case TCP_ESTABLISHED:
+				inet_sk_state_store(sk, TCP_CLOSE_WAIT);
+				break;
+			case TCP_FIN_WAIT1:
+				inet_sk_state_store(sk, TCP_CLOSING);
+				break;
+			case TCP_FIN_WAIT2:
+				inet_sk_state_store(sk, TCP_CLOSE);
Quoted 8 lines

net/mptcp/protocol.c static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk, · line 233

Quoted 6 lines
net/mptcp/protocol.c +6 -0
+				// @@ Close subflows now?
+				break;
+			default:
+				/* Other states not expected */
+				WARN_ON_ONCE(1);
+				break;

it's a pity we can't use mptcp_close_state() here.

Yeah, this spot is more like tcp_fin(), which has different transitions from tcp_close_state(). Taking a second look at that, the WARN_ON_ONCE(1) doesn't seem right, since there may be retransmissions of DATA_FIN in a few other states.

Quoted 55 lines
Quoted 55 lines
net/mptcp/protocol.c +22 -10
+			}
+
+			mptcp_set_timeout(sk, NULL);
+			mptcp_for_each_subflow(msk, subflow) {
+				struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
+
+				lock_sock(ssk);
+				tcp_send_ack(ssk);
+				release_sock(ssk);
+			}
+
+			sk->sk_state_change(sk);
+
+			if (sk->sk_shutdown == SHUTDOWN_MASK ||
+			    sk->sk_state == TCP_CLOSE)
+				sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
+			else
+				sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
+		}
+	}
+}
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk,
 					   unsigned int *bytes)
@@ -303,16 +393,6 @@ static void __mptcp_flush_join_list(struct mptcp_sock *msk)
 	spin_unlock_bh(&msk->join_list_lock);
 }
 
-static void mptcp_set_timeout(const struct sock *sk, const struct sock *ssk)
-{
-	long tout = ssk && inet_csk(ssk)->icsk_pending ?
-				      inet_csk(ssk)->icsk_timeout - jiffies : 0;
-
-	if (tout <= 0)
-		tout = mptcp_sk(sk)->timer_ival;
-	mptcp_sk(sk)->timer_ival = tout > 0 ? tout : TCP_RTO_MIN;
-}
-
 static bool mptcp_timer_pending(struct sock *sk)
 {
 	return timer_pending(&inet_csk(sk)->icsk_retransmit_timer);
net/mptcp/protocol.h +1 -0
index 08198b61b8a6..813c9d5c59f2 100644
@@ -193,11 +193,13 @@ struct mptcp_sock {
 	u64		remote_key;
 	u64		write_seq;
 	u64		ack_seq;
+	u64		rcv_data_fin_seq;
 	atomic64_t	snd_una;
 	unsigned long	timer_ival;
 	u32		token;
Quoted 8 lines

net/mptcp/protocol.h struct mptcp_sock { · line 205

Quoted 6 lines
net/mptcp/protocol.h +1 -0
 	unsigned long	flags;
 	bool		can_ack;
+	bool		rcv_data_fin;
 	spinlock_t	join_list_lock;
 	struct work_struct work;
 	struct list_head conn_list;

Note: we should revisit mptcp_sock binary layout someday ;)

Agreed!

--
Mat Martineau
Intel

author
#9 lore raw

PATCH 5/12 mptcp: Use MPTCP-level flag for sending DATA_FIN

In reply to Mat Martineau

Since DATA_FIN information is the same for every subflow, store it only in the mptcp_sock.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c  | 15 +++++++++------
 net/mptcp/protocol.c | 21 +++++----------------
 net/mptcp/protocol.h |  3 +--
 3 files changed, 15 insertions(+), 24 deletions(-)
net/mptcp/options.c +9 -6
index db34306eb73f..4e2378369691 100644
@@ -451,6 +451,8 @@ static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
 static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
 				 struct sk_buff *skb, struct mptcp_ext *ext)
 {
+	u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq);
+
 	if (!ext->use_map || !skb->len) {
 		/* RFC6824 requires a DSS mapping with specific values
 		 * if DATA_FIN is set but no data payload is mapped
@@ -458,10 +460,10 @@ static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
 		ext->data_fin = 1;
 		ext->use_map = 1;
 		ext->dsn64 = 1;
-		ext->data_seq = subflow->data_fin_tx_seq;
+		ext->data_seq = data_fin_tx_seq;
 		ext->subflow_seq = 0;
 		ext->data_len = 1;
-	} else if (ext->data_seq + ext->data_len == subflow->data_fin_tx_seq) {
+	} else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
 		/* If there's an existing DSS mapping and it is the
 		 * final mapping, DATA_FIN consumes 1 additional byte of
 		 * mapping space.
@@ -477,15 +479,17 @@ static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
 					  struct mptcp_out_options *opts)
 {
 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
+	struct mptcp_sock *msk = mptcp_sk(subflow->conn);
 	unsigned int dss_size = 0;
+	u64 snd_data_fin_enable;
 	struct mptcp_ext *mpext;
-	struct mptcp_sock *msk;
 	unsigned int ack_size;
 	bool ret = false;
 
 	mpext = skb ? mptcp_get_ext(skb) : NULL;
+	snd_data_fin_enable = READ_ONCE(msk->snd_data_fin_enable);
 
-	if (!skb || (mpext && mpext->use_map) || subflow->data_fin_tx_enable) {
+	if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
 		unsigned int map_size;
 
 		map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
@@ -495,7 +499,7 @@ static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
 		if (mpext)
 			opts->ext_copy = *mpext;
 
-		if (skb && subflow->data_fin_tx_enable)
+		if (skb && snd_data_fin_enable)
 			mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
 		ret = true;
 	}
@@ -504,7 +508,6 @@ static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
 	 * if the first subflow may have the already the remote key handy
 	 */
 	opts->ext_copy.use_ack = 0;
-	msk = mptcp_sk(subflow->conn);
 	if (!READ_ONCE(msk->can_ack)) {
 		*size = ALIGN(dss_size, 4);
 		return ret;
net/mptcp/protocol.c +5 -16
index 43e48327902e..71d2a59665f8 100644
@@ -1474,8 +1474,7 @@ static void mptcp_cancel_work(struct sock *sk)
 		sock_put(sk);
 }
 
-static void mptcp_subflow_shutdown(struct sock *ssk, int how,
-				   bool data_fin_tx_enable, u64 data_fin_tx_seq)
+static void mptcp_subflow_shutdown(struct sock *ssk, int how)
 {
 	lock_sock(ssk);
 
@@ -1488,14 +1487,6 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how,
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		if (data_fin_tx_enable) {
-			struct mptcp_subflow_context *subflow;
-
-			subflow = mptcp_subflow_ctx(ssk);
-			subflow->data_fin_tx_seq = data_fin_tx_seq;
-			subflow->data_fin_tx_enable = 1;
-		}
-
 		ssk->sk_shutdown |= how;
 		tcp_shutdown(ssk, how);
 		break;
@@ -1536,7 +1527,6 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
-	u64 data_fin_tx_seq;
 
 	lock_sock(sk);
 
@@ -1550,7 +1540,7 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 
-	data_fin_tx_seq = msk->write_seq;
+	msk->snd_data_fin_enable = 1;
 
 	__mptcp_clear_xmit(sk);
 
@@ -1558,9 +1548,6 @@ static void mptcp_close(struct sock *sk, long timeout)
 
 	list_for_each_entry_safe(subflow, tmp, &conn_list, node) {
 		struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
-
-		subflow->data_fin_tx_seq = data_fin_tx_seq;
-		subflow->data_fin_tx_enable = 1;
 		__mptcp_close_ssk(sk, ssk, subflow, timeout);
 	}
 
@@ -2257,10 +2244,12 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	}
 
 	__mptcp_flush_join_list(msk);
+	msk->snd_data_fin_enable = 1;
+
 	mptcp_for_each_subflow(msk, subflow) {
 		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
 
-		mptcp_subflow_shutdown(tcp_sk, how, 1, msk->write_seq);
+		mptcp_subflow_shutdown(tcp_sk, how);
 	}
 
 	/* Wake up anyone sleeping in poll. */
net/mptcp/protocol.h +1 -2
index 813c9d5c59f2..d75aadeb0e7f 100644
@@ -200,6 +200,7 @@ struct mptcp_sock {
 	unsigned long	flags;
 	bool		can_ack;
 	bool		rcv_data_fin;
+	bool		snd_data_fin_enable;
 	spinlock_t	join_list_lock;
 	struct work_struct work;
 	struct list_head conn_list;
@@ -292,10 +293,8 @@ struct mptcp_subflow_context {
 		backup : 1,
 		data_avail : 1,
 		rx_eof : 1,
-		data_fin_tx_enable : 1,
 		use_64bit_ack : 1, /* Set when we received a 64-bit DSN */
 		can_ack : 1;	    /* only after processing the remote a key */
-	u64	data_fin_tx_seq;
 	u32	remote_nonce;
 	u64	thmac;
 	u32	local_nonce;
Signature · 1 line
2.27.0
author
#10 lore raw

PATCH 6/12 mptcp: Add helper to process acks of DATA_FIN

In reply to Mat Martineau

After DATA_FIN has been sent, the peer will acknowledge it. An ack of the relevant MPTCP-level sequence number will update the MPTCP connection state appropriately.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 54 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 46 insertions(+), 8 deletions(-)
net/mptcp/protocol.c +46 -8
index 71d2a59665f8..2a93976fdb58 100644
@@ -143,6 +143,14 @@ static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,
 	MPTCP_SKB_CB(skb)->offset = offset;
 }
 
+static void mptcp_stop_timer(struct sock *sk)
+{
+	struct inet_connection_sock *icsk = inet_csk(sk);
+
+	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
+	mptcp_sk(sk)->timer_ival = 0;
+}
+
 /* both sockets must be locked */
 static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk,
 				    struct sock *ssk)
@@ -164,6 +172,42 @@ static bool mptcp_subflow_dsn_valid(const struct mptcp_sock *msk,
 	return mptcp_subflow_data_available(ssk);
 }
 
+static void mptcp_check_data_fin_ack(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	if (__mptcp_check_fallback(msk))
+		return;
+
+	/* Look for an acknowledged DATA_FIN */
+	if (((1 << sk->sk_state) &
+	    (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK)) &&
+	    (msk->write_seq == atomic64_read(&msk->snd_una))) {
+		mptcp_stop_timer(sk);
+
+		WRITE_ONCE(msk->snd_data_fin_enable, 0);
+
+		switch (sk->sk_state) {
+		case TCP_FIN_WAIT1:
+			inet_sk_state_store(sk, TCP_FIN_WAIT2);
+			sk->sk_state_change(sk);
+			break;
+		case TCP_CLOSING:
+			fallthrough;
+		case TCP_LAST_ACK:
+			inet_sk_state_store(sk, TCP_CLOSE);
+			sk->sk_state_change(sk);
+			break;
+		}
+
+		if (sk->sk_shutdown == SHUTDOWN_MASK ||
+		    sk->sk_state == TCP_CLOSE)
+			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
+		else
+			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
+	}
+}
+
 static bool mptcp_pending_data_fin(struct sock *sk)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -215,6 +259,8 @@ static void mptcp_check_data_fin(struct sock *sk)
 			WRITE_ONCE(msk->rcv_data_fin, 0);
 
 			sk->sk_shutdown |= RCV_SHUTDOWN;
+			smp_mb__before_atomic(); /* SHUTDOWN must be visible first */
+			set_bit(MPTCP_DATA_READY, &msk->flags);
 
 			switch (sk->sk_state) {
 			case TCP_ESTABLISHED:
@@ -449,14 +495,6 @@ static void mptcp_check_for_eof(struct mptcp_sock *msk)
 	}
 }
 
-static void mptcp_stop_timer(struct sock *sk)
-{
-	struct inet_connection_sock *icsk = inet_csk(sk);
-
-	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
-	mptcp_sk(sk)->timer_ival = 0;
-}
-
 static bool mptcp_ext_cache_refill(struct mptcp_sock *msk)
 {
 	const struct sock *sk = (const struct sock *)msk;
Signature · 1 line
2.27.0
author
#11 lore raw

PATCH 7/12 mptcp: Send DATA_FIN in appropriate states during shutdown or close

In reply to Mat Martineau

mptcp_shutdown now sends DATA_FIN on the subflows without shutting the subflows themselves down. The subflows need to stay connected while DATA_FIN is acked or possibly retransmitted.

mptcp_close should only send DATA_FIN if the MPTCP-level connection state requires it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
net/mptcp/protocol.c +32 -12
index 2a93976fdb58..9eef1fbe300b 100644
@@ -1512,7 +1512,7 @@ static void mptcp_cancel_work(struct sock *sk)
 		sock_put(sk);
 }
 
-static void mptcp_subflow_shutdown(struct sock *ssk, int how)
+static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
 {
 	lock_sock(ssk);
 
@@ -1525,8 +1525,8 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how)
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		ssk->sk_shutdown |= how;
-		tcp_shutdown(ssk, how);
+		mptcp_set_timeout(sk, ssk);
+		tcp_send_ack(ssk);
 		break;
 	}
 
@@ -1565,9 +1565,11 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
+	int prev_state;
 
 	lock_sock(sk);
 
+	prev_state = sk->sk_state;
 	inet_sk_state_store(sk, TCP_CLOSE);
 
 	/* be sure to always acquire the join list lock, to sync vs
@@ -1578,7 +1580,15 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 
-	msk->snd_data_fin_enable = 1;
+	if ((1 << prev_state) &
+	    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
+		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+		WRITE_ONCE(msk->snd_data_fin_enable, 1);
+	} else {
+		/* DATA_FIN has already been sent in other states */
+		WRITE_ONCE(msk->snd_data_fin_enable, 0);
+	}
 
 	__mptcp_clear_xmit(sk);
 
@@ -2263,11 +2273,8 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	pr_debug("sk=%p, how=%d", msk, how);
 
 	lock_sock(sock->sk);
-	if (how == SHUT_WR || how == SHUT_RDWR)
-		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
 	how++;
-
 	if ((how & ~SHUTDOWN_MASK) || !how) {
 		ret = -EINVAL;
 		goto out_unlock;
@@ -2281,13 +2288,26 @@ static int mptcp_shutdown(struct socket *sock, int how)
 			sock->state = SS_CONNECTED;
 	}
 
-	__mptcp_flush_join_list(msk);
-	msk->snd_data_fin_enable = 1;
+	/* If we've already sent a FIN, or it's a closed state, skip this. */
+	if ((how & SEND_SHUTDOWN) &&
+	    ((1 << sock->sk->sk_state) &
+	     (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	      TCPF_SYN_RECV | TCPF_CLOSE_WAIT))) {
 
-	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+		if (mptcp_close_state(sock->sk)) {
+			__mptcp_flush_join_list(msk);
 
-		mptcp_subflow_shutdown(tcp_sk, how);
+			WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+			WRITE_ONCE(msk->snd_data_fin_enable, 1);
+
+			mptcp_for_each_subflow(msk, subflow) {
+				struct sock *tcp_sk;
+
+				tcp_sk = mptcp_subflow_tcp_sock(subflow);
+
+				mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
+			}
+		}
 	}
 
 	/* Wake up anyone sleeping in poll. */
Signature · 1 line
2.27.0
#12 lore raw

[MPTCP] Re: [PATCH mptcp-next 07/12] mptcp: Send DATA_FIN in appropriate states during shutdown or close

Quoted 52 lines · Mat Martineau

mptcp_shutdown now sends DATA_FIN on the subflows without shutting the subflows themselves down. The subflows need to stay connected while DATA_FIN is acked or possibly retransmitted.

mptcp_close should only send DATA_FIN if the MPTCP-level connection state requires it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
net/mptcp/protocol.c +5 -3
index 2a93976fdb58..9eef1fbe300b 100644
@@ -1512,7 +1512,7 @@ static void mptcp_cancel_work(struct sock *sk)
 		sock_put(sk);
 }
 
-static void mptcp_subflow_shutdown(struct sock *ssk, int how)
+static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
 {
 	lock_sock(ssk);
 
@@ -1525,8 +1525,8 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how)
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		ssk->sk_shutdown |= how;
-		tcp_shutdown(ssk, how);
+		mptcp_set_timeout(sk, ssk);
+		tcp_send_ack(ssk);
 		break;
 	}
 
@@ -1565,9 +1565,11 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
+	int prev_state;
 
 	lock_sock(sk);
 
+	prev_state = sk->sk_state;
 	inet_sk_state_store(sk, TCP_CLOSE);
 
 	/* be sure to always acquire the join list lock, to sync vs
@@ -1578,7 +1580,15 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 

net/mptcp/protocol.c static void mptcp_close(struct sock *sk, long timeout) · line 1591

Quoted 10 lines
net/mptcp/protocol.c +9 -1
-	msk->snd_data_fin_enable = 1;
+	if ((1 << prev_state) &
+	    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
+		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+		WRITE_ONCE(msk->snd_data_fin_enable, 1);
+	} else {
+		/* DATA_FIN has already been sent in other states */
+		WRITE_ONCE(msk->snd_data_fin_enable, 0);
+	}

Why do we need to explicitly clear 'snd_data_fin_enable' here?

Why don't we need an 'mptcp_close_state()' call here, replacing the inet_sk_state_store() above?

Quoted 24 lines
net/mptcp/protocol.c +4 -5
 	__mptcp_clear_xmit(sk);
 
@@ -2263,11 +2273,8 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	pr_debug("sk=%p, how=%d", msk, how);
 
 	lock_sock(sock->sk);
-	if (how == SHUT_WR || how == SHUT_RDWR)
-		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
 	how++;
-
 	if ((how & ~SHUTDOWN_MASK) || !how) {
 		ret = -EINVAL;
 		goto out_unlock;
@@ -2281,13 +2288,26 @@ static int mptcp_shutdown(struct socket *sock, int how)
 			sock->state = SS_CONNECTED;
 	}
 
-	__mptcp_flush_join_list(msk);
-	msk->snd_data_fin_enable = 1;
+	/* If we've already sent a FIN, or it's a closed state, skip this. */
+	if ((how & SEND_SHUTDOWN) &&
+	    ((1 << sock->sk->sk_state) &
+	     (TCPF_ESTABLISHED | TCPF_SYN_SENT |

net/mptcp/protocol.c static int mptcp_shutdown(struct socket *sock, int how) · line 2301

Quoted 10 lines
net/mptcp/protocol.c +5 -3
+	      TCPF_SYN_RECV | TCPF_CLOSE_WAIT))) {
 
-	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+		if (mptcp_close_state(sock->sk)) {
+			__mptcp_flush_join_list(msk);
 
-		mptcp_subflow_shutdown(tcp_sk, how);
+			WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+			WRITE_ONCE(msk->snd_data_fin_enable, 1);

This is quite alike the code inside mptcp_close(), except for the mptcp_close_state() call. Can we factor out the code in an helper?

/P

author
#13 lore raw

[MPTCP] Re: [PATCH mptcp-next 07/12] mptcp: Send DATA_FIN in appropriate states during shutdown or close

Quoted 66 lines · Paolo Abeni
Quoted 52 lines · Mat Martineau

mptcp_shutdown now sends DATA_FIN on the subflows without shutting the subflows themselves down. The subflows need to stay connected while DATA_FIN is acked or possibly retransmitted.

mptcp_close should only send DATA_FIN if the MPTCP-level connection state requires it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
net/mptcp/protocol.c +5 -3
index 2a93976fdb58..9eef1fbe300b 100644
@@ -1512,7 +1512,7 @@ static void mptcp_cancel_work(struct sock *sk)
 		sock_put(sk);
 }
 
-static void mptcp_subflow_shutdown(struct sock *ssk, int how)
+static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
 {
 	lock_sock(ssk);
 
@@ -1525,8 +1525,8 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how)
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		ssk->sk_shutdown |= how;
-		tcp_shutdown(ssk, how);
+		mptcp_set_timeout(sk, ssk);
+		tcp_send_ack(ssk);
 		break;
 	}
 
@@ -1565,9 +1565,11 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
+	int prev_state;
 
 	lock_sock(sk);
 
+	prev_state = sk->sk_state;
 	inet_sk_state_store(sk, TCP_CLOSE);
 
 	/* be sure to always acquire the join list lock, to sync vs
@@ -1578,7 +1580,15 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 

net/mptcp/protocol.c static void mptcp_close(struct sock *sk, long timeout) · line 1591

Quoted 10 lines
net/mptcp/protocol.c +9 -1
-	msk->snd_data_fin_enable = 1;
+	if ((1 << prev_state) &
+	    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
+		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+		WRITE_ONCE(msk->snd_data_fin_enable, 1);
+	} else {
+		/* DATA_FIN has already been sent in other states */
+		WRITE_ONCE(msk->snd_data_fin_enable, 0);
+	}

Why do we need to explicitly clear 'snd_data_fin_enable' here?

Quoted 2 lines

Why don't we need an 'mptcp_close_state()' call here, replacing the inet_sk_state_store() above?

Both these issues are introduced here and immediately clobbered in the next commit. My intent was to align the mptcp_close behavior with the change to mptcp_shutdown made in this commit so the close wouldn't duplicate what's happening in shutdown. But it probably adds more confusion than value.

I'll drop this hunk and just leave mptcp_close unchanged in this commit, leaving the real changes in the next commit.

Quoted 32 lines
Quoted 32 lines
net/mptcp/protocol.c +7 -7
 	__mptcp_clear_xmit(sk);
 
@@ -2263,11 +2273,8 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	pr_debug("sk=%p, how=%d", msk, how);
 
 	lock_sock(sock->sk);
-	if (how == SHUT_WR || how == SHUT_RDWR)
-		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
 	how++;
-
 	if ((how & ~SHUTDOWN_MASK) || !how) {
 		ret = -EINVAL;
 		goto out_unlock;
@@ -2281,13 +2288,26 @@ static int mptcp_shutdown(struct socket *sock, int how)
 			sock->state = SS_CONNECTED;
 	}
 
-	__mptcp_flush_join_list(msk);
-	msk->snd_data_fin_enable = 1;
+	/* If we've already sent a FIN, or it's a closed state, skip this. */
+	if ((how & SEND_SHUTDOWN) &&
+	    ((1 << sock->sk->sk_state) &
+	     (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	      TCPF_SYN_RECV | TCPF_CLOSE_WAIT))) {
 
-	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+		if (mptcp_close_state(sock->sk)) {
+			__mptcp_flush_join_list(msk);
 
Quoted 7 lines

net/mptcp/protocol.c static int mptcp_shutdown(struct socket *sock, int how) · line 2301

Quoted 3 lines
net/mptcp/protocol.c +2 -1
-		mptcp_subflow_shutdown(tcp_sk, how);
+			WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+			WRITE_ONCE(msk->snd_data_fin_enable, 1);

This is quite alike the code inside mptcp_close(), except for the mptcp_close_state() call. Can we factor out the code in an helper?

Sure.

--
Mat Martineau
Intel

#14 lore raw

[MPTCP] Re: [PATCH mptcp-next 07/12] mptcp: Send DATA_FIN in appropriate states during shutdown or close

In reply to Mat Martineau
Quoted 96 lines · Mat Martineau

mptcp_shutdown now sends DATA_FIN on the subflows without shutting the subflows themselves down. The subflows need to stay connected while DATA_FIN is acked or possibly retransmitted.

mptcp_close should only send DATA_FIN if the MPTCP-level connection state requires it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
net/mptcp/protocol.c +22 -12
index 2a93976fdb58..9eef1fbe300b 100644
@@ -1512,7 +1512,7 @@ static void mptcp_cancel_work(struct sock *sk)
 		sock_put(sk);
 }
 
-static void mptcp_subflow_shutdown(struct sock *ssk, int how)
+static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
 {
 	lock_sock(ssk);
 
@@ -1525,8 +1525,8 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how)
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		ssk->sk_shutdown |= how;
-		tcp_shutdown(ssk, how);
+		mptcp_set_timeout(sk, ssk);
+		tcp_send_ack(ssk);
 		break;
 	}
 
@@ -1565,9 +1565,11 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
+	int prev_state;
 
 	lock_sock(sk);
 
+	prev_state = sk->sk_state;
 	inet_sk_state_store(sk, TCP_CLOSE);
 
 	/* be sure to always acquire the join list lock, to sync vs
@@ -1578,7 +1580,15 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 
-	msk->snd_data_fin_enable = 1;
+	if ((1 << prev_state) &
+	    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
+		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+		WRITE_ONCE(msk->snd_data_fin_enable, 1);
+	} else {
+		/* DATA_FIN has already been sent in other states */
+		WRITE_ONCE(msk->snd_data_fin_enable, 0);
+	}
 
 	__mptcp_clear_xmit(sk);
 
@@ -2263,11 +2273,8 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	pr_debug("sk=%p, how=%d", msk, how);
 
 	lock_sock(sock->sk);
-	if (how == SHUT_WR || how == SHUT_RDWR)
-		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
 	how++;
-
 	if ((how & ~SHUTDOWN_MASK) || !how) {
 		ret = -EINVAL;
 		goto out_unlock;
@@ -2281,13 +2288,26 @@ static int mptcp_shutdown(struct socket *sock, int how)
 			sock->state = SS_CONNECTED;
 	}
 
-	__mptcp_flush_join_list(msk);
-	msk->snd_data_fin_enable = 1;
+	/* If we've already sent a FIN, or it's a closed state, skip this. */
+	if ((how & SEND_SHUTDOWN) &&
+	    ((1 << sock->sk->sk_state) &
+	     (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	      TCPF_SYN_RECV | TCPF_CLOSE_WAIT))) {
 
-	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+		if (mptcp_close_state(sock->sk)) {
+			__mptcp_flush_join_list(msk);
 
-		mptcp_subflow_shutdown(tcp_sk, how);
+			WRITE_ONCE(msk->write_seq, msk->write_seq + 1);

net/mptcp/protocol.c static int mptcp_shutdown(struct socket *sock, int how) · line 2310

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+			WRITE_ONCE(msk->snd_data_fin_enable, 1);
+
+			mptcp_for_each_subflow(msk, subflow) {
+				struct sock *tcp_sk;
+
+				tcp_sk = mptcp_subflow_tcp_sock(subflow);
+
+				mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
+			}
+		}

I just notice the above is problematic in case we are using multiple subflows for xmit, and we need reinjection after mptcp level shutdown(SEND_SHUTDOWN).

Since each subflow is closed for sending, we can't reinject the data...

@Christoph does mptcp.org handle the above scenario in some specific way?

Thanks!

/P

author
#15 lore raw

[MPTCP] Re: [PATCH mptcp-next 07/12] mptcp: Send DATA_FIN in appropriate states during shutdown or close

Quoted 112 lines · Paolo Abeni
Quoted 96 lines · Mat Martineau

mptcp_shutdown now sends DATA_FIN on the subflows without shutting the subflows themselves down. The subflows need to stay connected while DATA_FIN is acked or possibly retransmitted.

mptcp_close should only send DATA_FIN if the MPTCP-level connection state requires it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
net/mptcp/protocol.c +22 -12
index 2a93976fdb58..9eef1fbe300b 100644
@@ -1512,7 +1512,7 @@ static void mptcp_cancel_work(struct sock *sk)
 		sock_put(sk);
 }
 
-static void mptcp_subflow_shutdown(struct sock *ssk, int how)
+static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
 {
 	lock_sock(ssk);
 
@@ -1525,8 +1525,8 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how)
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		ssk->sk_shutdown |= how;
-		tcp_shutdown(ssk, how);
+		mptcp_set_timeout(sk, ssk);
+		tcp_send_ack(ssk);
 		break;
 	}
 
@@ -1565,9 +1565,11 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
+	int prev_state;
 
 	lock_sock(sk);
 
+	prev_state = sk->sk_state;
 	inet_sk_state_store(sk, TCP_CLOSE);
 
 	/* be sure to always acquire the join list lock, to sync vs
@@ -1578,7 +1580,15 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 
-	msk->snd_data_fin_enable = 1;
+	if ((1 << prev_state) &
+	    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
+		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+		WRITE_ONCE(msk->snd_data_fin_enable, 1);
+	} else {
+		/* DATA_FIN has already been sent in other states */
+		WRITE_ONCE(msk->snd_data_fin_enable, 0);
+	}
 
 	__mptcp_clear_xmit(sk);
 
@@ -2263,11 +2273,8 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	pr_debug("sk=%p, how=%d", msk, how);
 
 	lock_sock(sock->sk);
-	if (how == SHUT_WR || how == SHUT_RDWR)
-		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
 	how++;
-
 	if ((how & ~SHUTDOWN_MASK) || !how) {
 		ret = -EINVAL;
 		goto out_unlock;
@@ -2281,13 +2288,26 @@ static int mptcp_shutdown(struct socket *sock, int how)
 			sock->state = SS_CONNECTED;
 	}
 
-	__mptcp_flush_join_list(msk);
-	msk->snd_data_fin_enable = 1;
+	/* If we've already sent a FIN, or it's a closed state, skip this. */
+	if ((how & SEND_SHUTDOWN) &&
+	    ((1 << sock->sk->sk_state) &
+	     (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	      TCPF_SYN_RECV | TCPF_CLOSE_WAIT))) {
 
-	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+		if (mptcp_close_state(sock->sk)) {
+			__mptcp_flush_join_list(msk);
 
-		mptcp_subflow_shutdown(tcp_sk, how);
+			WRITE_ONCE(msk->write_seq, msk->write_seq + 1);

net/mptcp/protocol.c static int mptcp_shutdown(struct socket *sock, int how) · line 2310

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+			WRITE_ONCE(msk->snd_data_fin_enable, 1);
+
+			mptcp_for_each_subflow(msk, subflow) {
+				struct sock *tcp_sk;
+
+				tcp_sk = mptcp_subflow_tcp_sock(subflow);
+
+				mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
+			}
+		}

I just notice the above is problematic in case we are using multiple subflows for xmit, and we need reinjection after mptcp level shutdown(SEND_SHUTDOWN).

Quoted 1 line

Since each subflow is closed for sending, we can't reinject the data...

mptcp_subflow_shutdown() only closes subflows for sending if the connection is in TCP fallback. In this patch set, mptcp_subflow_shutdown is modified to only send ACK packets with DATA_FIN included. The subflows are still open for reinjection.

Quoted 4 lines

@Christoph does mptcp.org handle the above scenario in some specific way?

--
Mat Martineau
Intel

#16 lore raw

[MPTCP] Re: [PATCH mptcp-next 07/12] mptcp: Send DATA_FIN in appropriate states during shutdown or close

In reply to Paolo Abeni
Quoted 112 lines · Paolo Abeni
Quoted 96 lines · Mat Martineau

mptcp_shutdown now sends DATA_FIN on the subflows without shutting the subflows themselves down. The subflows need to stay connected while DATA_FIN is acked or possibly retransmitted.

mptcp_close should only send DATA_FIN if the MPTCP-level connection state requires it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 44 ++++++++++++++++++++++++++++++++------------
 1 file changed, 32 insertions(+), 12 deletions(-)
net/mptcp/protocol.c +22 -12
index 2a93976fdb58..9eef1fbe300b 100644
@@ -1512,7 +1512,7 @@ static void mptcp_cancel_work(struct sock *sk)
 		sock_put(sk);
 }
 
-static void mptcp_subflow_shutdown(struct sock *ssk, int how)
+static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
 {
 	lock_sock(ssk);
 
@@ -1525,8 +1525,8 @@ static void mptcp_subflow_shutdown(struct sock *ssk, int how)
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		ssk->sk_shutdown |= how;
-		tcp_shutdown(ssk, how);
+		mptcp_set_timeout(sk, ssk);
+		tcp_send_ack(ssk);
 		break;
 	}
 
@@ -1565,9 +1565,11 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
+	int prev_state;
 
 	lock_sock(sk);
 
+	prev_state = sk->sk_state;
 	inet_sk_state_store(sk, TCP_CLOSE);
 
 	/* be sure to always acquire the join list lock, to sync vs
@@ -1578,7 +1580,15 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 
-	msk->snd_data_fin_enable = 1;
+	if ((1 << prev_state) &
+	    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
+		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+		WRITE_ONCE(msk->snd_data_fin_enable, 1);
+	} else {
+		/* DATA_FIN has already been sent in other states */
+		WRITE_ONCE(msk->snd_data_fin_enable, 0);
+	}
 
 	__mptcp_clear_xmit(sk);
 
@@ -2263,11 +2273,8 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	pr_debug("sk=%p, how=%d", msk, how);
 
 	lock_sock(sock->sk);
-	if (how == SHUT_WR || how == SHUT_RDWR)
-		inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
 
 	how++;
-
 	if ((how & ~SHUTDOWN_MASK) || !how) {
 		ret = -EINVAL;
 		goto out_unlock;
@@ -2281,13 +2288,26 @@ static int mptcp_shutdown(struct socket *sock, int how)
 			sock->state = SS_CONNECTED;
 	}
 
-	__mptcp_flush_join_list(msk);
-	msk->snd_data_fin_enable = 1;
+	/* If we've already sent a FIN, or it's a closed state, skip this. */
+	if ((how & SEND_SHUTDOWN) &&
+	    ((1 << sock->sk->sk_state) &
+	     (TCPF_ESTABLISHED | TCPF_SYN_SENT |
+	      TCPF_SYN_RECV | TCPF_CLOSE_WAIT))) {
 
-	mptcp_for_each_subflow(msk, subflow) {
-		struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+		if (mptcp_close_state(sock->sk)) {
+			__mptcp_flush_join_list(msk);
 
-		mptcp_subflow_shutdown(tcp_sk, how);
+			WRITE_ONCE(msk->write_seq, msk->write_seq + 1);

net/mptcp/protocol.c static int mptcp_shutdown(struct socket *sock, int how) · line 2310

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+			WRITE_ONCE(msk->snd_data_fin_enable, 1);
+
+			mptcp_for_each_subflow(msk, subflow) {
+				struct sock *tcp_sk;
+
+				tcp_sk = mptcp_subflow_tcp_sock(subflow);
+
+				mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
+			}
+		}

I just notice the above is problematic in case we are using multiple subflows for xmit, and we need reinjection after mptcp level shutdown(SEND_SHUTDOWN).

Quoted 3 lines

Since each subflow is closed for sending, we can't reinject the data...

@Christoph does mptcp.org handle the above scenario in some specific

The RFC says:

It is only permissible to combine these signals on one subflow if there is no data outstanding on other subflows. Otherwise, it may be necessary to retransmit data on different subflows. Essentially, a host MUST NOT close all functioning subflows unless it is safe to do so, i.e., until all outstanding data has been DATA_ACKed or until the segment with the DATA_FIN flag set is the only outstanding segment.

So, in mptcp.org, we tag the DATA_FIN along with a FIN only if the segment being sent is the last segment of the MPTCP send-queue. Unless that, we never set the FIN-flag on a subflow and only close the subflows after the DATA_FIN has been DATA_ACK'd.

Christoph

Quoted 6 lines

way?

Thanks!

/P

author
#17 lore raw

PATCH 8/12 mptcp: Use full MPTCP-level disconnect state machine

In reply to Mat Martineau

RFC 8684 appendix D describes the connection state machine for MPTCP. This patch implements the DATA_FIN / DATA_ACK exchanges and MPTCP-level socket state changes described in that appendix, rather than simply sending DATA_FIN along with TCP FIN when disconnecting subflows.

DATA_FIN is now sent and acknowledged before shutting down the subflows. Received DATA_FIN information (if not part of a data packet) is written to the MPTCP socket when the incoming DSS option is parsed by the subflow, and the MPTCP worker is scheduled to process the flag. DATA_FIN received as part of a full DSS mapping will be handled when the mapping is processed.

The DATA_FIN is acknowledged by the worker if the reader is caught up. If there is still data to be moved to the MPTCP-level queue, ack_seq will be incremented to account for the DATA_FIN when it reaches the end of the stream and a DATA_ACK will be sent to the peer.

In this RFC patch, DATA_FIN is sent when calling shutdown(fd, SHUT_WR) on the MPTCP socket. mptcp_close() has not yet been updated to wait for the peer to DATA_ACK before forcing all the subflows to be closed and cleaned up. The handshake is working with both peers using test programs with shutdown() and delays, but is not yet working consistently with the self tests. It also doesn't currently detect if fallback has happened, so there are extra acks sent on fallback connections.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c  | 11 ++++++
 net/mptcp/protocol.c | 83 +++++++++++++++++++++++++++++++++++---------
 net/mptcp/subflow.c  | 11 ++++--
 3 files changed, 86 insertions(+), 19 deletions(-)
net/mptcp/options.c +11 -0
index 4e2378369691..72d41c239acf 100644
@@ -864,6 +864,17 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 	if (mp_opt.use_ack)
 		update_una(msk, &mp_opt);
 
+	/* Zero-length packets, like bare ACKs carrying a DATA_FIN, are
+	 * dropped by the caller and not propagated to the MPTCP layer.
+	 * Copy the DATA_FIN information now.
+	 */
+	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
+		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
+		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq) &&
+		    schedule_work(&msk->work))
+			sock_hold(subflow->conn);
+	}
+
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 	if (!mpext)
 		return;
net/mptcp/protocol.c +67 -16

148 lines not on this page · raw message

net/mptcp/subflow.c +8 -3
index 07c6b3a65bb6..d2b61dbbf503 100644
@@ -594,7 +594,8 @@ static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
 	return true;
 }
 
-static enum mapping_status get_mapping_status(struct sock *ssk)
+static enum mapping_status get_mapping_status(struct sock *ssk,
+					      struct mptcp_sock *msk)
 {
 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
 	struct mptcp_ext *mpext;
@@ -644,7 +645,8 @@ static enum mapping_status get_mapping_status(struct sock *ssk)
 
 	if (mpext->data_fin == 1) {
 		if (data_len == 1) {
-			pr_debug("DATA_FIN with no payload");
+			mptcp_update_rcv_data_fin(msk, mpext->data_seq);
+			pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
 			if (subflow->map_valid) {
 				/* A DATA_FIN might arrive in a DSS
 				 * option before the previous mapping
@@ -656,6 +658,9 @@ static enum mapping_status get_mapping_status(struct sock *ssk)
 			} else {
 				return MAPPING_DATA_FIN;
 			}
+		} else {
+			mptcp_update_rcv_data_fin(msk, mpext->data_seq + data_len);
+			pr_debug("DATA_FIN with mapping seq=%llu", mpext->data_seq + data_len);
 		}
 
 		/* Adjust for DATA_FIN using 1 byte of sequence space */
@@ -744,7 +749,7 @@ static bool subflow_check_data_avail(struct sock *ssk)
 		u64 ack_seq;
 		u64 old_ack;
 
-		status = get_mapping_status(ssk);
+		status = get_mapping_status(ssk, msk);
 		pr_debug("msk=%p ssk=%p status=%d", msk, ssk, status);
 		if (status == MAPPING_INVALID) {
 			ssk->sk_err = EBADMSG;
Signature · 1 line
2.27.0
author
#18 lore raw

[MPTCP] Re: [PATCH mptcp-next 08/12] mptcp: Use full MPTCP-level disconnect state machine

Quoted 72 lines · Mat Martineau

RFC 8684 appendix D describes the connection state machine for MPTCP. This patch implements the DATA_FIN / DATA_ACK exchanges and MPTCP-level socket state changes described in that appendix, rather than simply sending DATA_FIN along with TCP FIN when disconnecting subflows.

DATA_FIN is now sent and acknowledged before shutting down the subflows. Received DATA_FIN information (if not part of a data packet) is written to the MPTCP socket when the incoming DSS option is parsed by the subflow, and the MPTCP worker is scheduled to process the flag. DATA_FIN received as part of a full DSS mapping will be handled when the mapping is processed.

The DATA_FIN is acknowledged by the worker if the reader is caught up. If there is still data to be moved to the MPTCP-level queue, ack_seq will be incremented to account for the DATA_FIN when it reaches the end of the stream and a DATA_ACK will be sent to the peer.

In this RFC patch, DATA_FIN is sent when calling shutdown(fd, SHUT_WR) on the MPTCP socket. mptcp_close() has not yet been updated to wait for the peer to DATA_ACK before forcing all the subflows to be closed and cleaned up. The handshake is working with both peers using test programs with shutdown() and delays, but is not yet working consistently with the self tests. It also doesn't currently detect if fallback has happened, so there are extra acks sent on fallback connections.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>

---

net/mptcp/options.c  | 11 ++++++
net/mptcp/protocol.c | 83 +++++++++++++++++++++++++++++++++++---------
net/mptcp/subflow.c  | 11 ++++--
3 files changed, 86 insertions(+), 19 deletions(-)
net/mptcp/options.c +11 -0
index 4e2378369691..72d41c239acf 100644
@@ -864,6 +864,17 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 	if (mp_opt.use_ack)
 		update_una(msk, &mp_opt);
 
+	/* Zero-length packets, like bare ACKs carrying a DATA_FIN, are
+	 * dropped by the caller and not propagated to the MPTCP layer.
+	 * Copy the DATA_FIN information now.
+	 */
+	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
+		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
+		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq) &&
+		    schedule_work(&msk->work))
+			sock_hold(subflow->conn);
+	}
+
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 	if (!mpext)
 		return;
net/mptcp/protocol.c +6 -0
index 9eef1fbe300b..d19e7ba5163c 100644
@@ -299,6 +299,12 @@ static void mptcp_check_data_fin(struct sock *sk)
 	}
 }
 
+static void mptcp_schedule_work(struct sock *sk)
+{
+	if (schedule_work(&mptcp_sk(sk)->work))
+		sock_hold(sk);
+}
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk,
 					   unsigned int *bytes)

net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 367

Quoted 6 lines
net/mptcp/protocol.c +3 -0
@@ -356,6 +362,9 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 			seq += len;
 			moved += len;
 
+			if (mptcp_pending_data_fin(sk))
+				mptcp_schedule_work(sk);
+

I noticed after sending that this check is redundant with the hunk of code below. Only the one check at the end of the function is needed.

Quoted 10 lines
net/mptcp/protocol.c +3 -0
 			if (WARN_ON_ONCE(map_remaining < len))
 				break;
 		} else {
@@ -375,6 +384,16 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 
 	*bytes = moved;
 
+	/* If the moves have caught up with the DATA_FIN sequence number
+	 * it's time to ack the DATA_FIN and change socket state, but
+	 * this is not a good place to change state. Let the workqueue

net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 399

Quoted 10 lines
net/mptcp/protocol.c +7 -0
+	 * do it.
+	 */
+	if (READ_ONCE(msk->rcv_data_fin) &&
+	    msk->ack_seq + 1 == READ_ONCE(msk->rcv_data_fin_seq) &&
+	    schedule_work(&msk->work))
+		sock_hold(sk);
+
 	return done;
 }
 

--
Mat Martineau
Intel

#19 lore raw

[MPTCP] Re: [PATCH mptcp-next 08/12] mptcp: Use full MPTCP-level disconnect state machine

Quoted 77 lines · Mat Martineau
Quoted 75 lines · Mat Martineau

RFC 8684 appendix D describes the connection state machine for MPTCP. This patch implements the DATA_FIN / DATA_ACK exchanges and MPTCP-level socket state changes described in that appendix, rather than simply sending DATA_FIN along with TCP FIN when disconnecting subflows.

DATA_FIN is now sent and acknowledged before shutting down the subflows. Received DATA_FIN information (if not part of a data packet) is written to the MPTCP socket when the incoming DSS option is parsed by the subflow, and the MPTCP worker is scheduled to process the flag. DATA_FIN received as part of a full DSS mapping will be handled when the mapping is processed.

The DATA_FIN is acknowledged by the worker if the reader is caught up. If there is still data to be moved to the MPTCP-level queue, ack_seq will be incremented to account for the DATA_FIN when it reaches the end of the stream and a DATA_ACK will be sent to the peer.

In this RFC patch, DATA_FIN is sent when calling shutdown(fd, SHUT_WR) on the MPTCP socket. mptcp_close() has not yet been updated to wait for the peer to DATA_ACK before forcing all the subflows to be closed and cleaned up. The handshake is working with both peers using test programs with shutdown() and delays, but is not yet working consistently with the self tests. It also doesn't currently detect if fallback has happened, so there are extra acks sent on fallback connections.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>

---

net/mptcp/options.c  | 11 ++++++
net/mptcp/protocol.c | 83 +++++++++++++++++++++++++++++++++++---------
net/mptcp/subflow.c  | 11 ++++--
3 files changed, 86 insertions(+), 19 deletions(-)
net/mptcp/options.c +11 -0
index 4e2378369691..72d41c239acf 100644
@@ -864,6 +864,17 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 	if (mp_opt.use_ack)
 		update_una(msk, &mp_opt);
 
+	/* Zero-length packets, like bare ACKs carrying a DATA_FIN, are
+	 * dropped by the caller and not propagated to the MPTCP layer.
+	 * Copy the DATA_FIN information now.
+	 */
+	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
+		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
+		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq) &&
+		    schedule_work(&msk->work))
+			sock_hold(subflow->conn);
+	}
+
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 	if (!mpext)
 		return;
net/mptcp/protocol.c +6 -0
index 9eef1fbe300b..d19e7ba5163c 100644
@@ -299,6 +299,12 @@ static void mptcp_check_data_fin(struct sock *sk)
 	}
 }
 
+static void mptcp_schedule_work(struct sock *sk)
+{
+	if (schedule_work(&mptcp_sk(sk)->work))
+		sock_hold(sk);
+}
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk,
 					   unsigned int *bytes)
@@ -356,6 +362,9 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 			seq += len;
 			moved += len;
 
Quoted 6 lines

net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 367

Quoted 3 lines
net/mptcp/protocol.c +3 -0
+			if (mptcp_pending_data_fin(sk))
+				mptcp_schedule_work(sk);
+

I noticed after sending that this check is redundant with the hunk of code below. Only the one check at the end of the function is needed.

Looks like the checked condition is slightly different ?!?

	msk->ack_seq == READ_ONCE(msk->rcv_data_fin_seq)

in mptcp_pending_data_fin()...

Quoted 8 lines
Quoted 8 lines
net/mptcp/protocol.c +0 -0
 			if (WARN_ON_ONCE(map_remaining < len))
 				break;
 		} else {
@@ -375,6 +384,16 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 
 	*bytes = moved;
 
Quoted 7 lines

net/mptcp/protocol.c static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk, · line 393

Quoted 7 lines
net/mptcp/protocol.c +7 -0
+	/* If the moves have caught up with the DATA_FIN sequence number
+	 * it's time to ack the DATA_FIN and change socket state, but
+	 * this is not a good place to change state. Let the workqueue
+	 * do it.
+	 */
+	if (READ_ONCE(msk->rcv_data_fin) &&
+	    msk->ack_seq + 1 == READ_ONCE(msk->rcv_data_fin_seq) &&

... here we have 'msk->ack_seq + 1'. I guess this latter is the correct expression???

If so I suggest to modify mptcp_pending_data_fin() accordingly, and reuse it here.

/P

#20 lore raw

[MPTCP] Re: [PATCH mptcp-next 08/12] mptcp: Use full MPTCP-level disconnect state machine

In reply to Mat Martineau
Quoted 156 lines · Mat Martineau

RFC 8684 appendix D describes the connection state machine for MPTCP. This patch implements the DATA_FIN / DATA_ACK exchanges and MPTCP-level socket state changes described in that appendix, rather than simply sending DATA_FIN along with TCP FIN when disconnecting subflows.

DATA_FIN is now sent and acknowledged before shutting down the subflows. Received DATA_FIN information (if not part of a data packet) is written to the MPTCP socket when the incoming DSS option is parsed by the subflow, and the MPTCP worker is scheduled to process the flag. DATA_FIN received as part of a full DSS mapping will be handled when the mapping is processed.

The DATA_FIN is acknowledged by the worker if the reader is caught up. If there is still data to be moved to the MPTCP-level queue, ack_seq will be incremented to account for the DATA_FIN when it reaches the end of the stream and a DATA_ACK will be sent to the peer.

In this RFC patch, DATA_FIN is sent when calling shutdown(fd, SHUT_WR) on the MPTCP socket. mptcp_close() has not yet been updated to wait for the peer to DATA_ACK before forcing all the subflows to be closed and cleaned up. The handshake is working with both peers using test programs with shutdown() and delays, but is not yet working consistently with the self tests. It also doesn't currently detect if fallback has happened, so there are extra acks sent on fallback connections.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c  | 11 ++++++
 net/mptcp/protocol.c | 83 +++++++++++++++++++++++++++++++++++---------
 net/mptcp/subflow.c  | 11 ++++--
 3 files changed, 86 insertions(+), 19 deletions(-)
net/mptcp/options.c +11 -0
index 4e2378369691..72d41c239acf 100644
@@ -864,6 +864,17 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 	if (mp_opt.use_ack)
 		update_una(msk, &mp_opt);
 
+	/* Zero-length packets, like bare ACKs carrying a DATA_FIN, are
+	 * dropped by the caller and not propagated to the MPTCP layer.
+	 * Copy the DATA_FIN information now.
+	 */
+	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
+		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
+		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq) &&
+		    schedule_work(&msk->work))
+			sock_hold(subflow->conn);
+	}
+
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 	if (!mpext)
 		return;
net/mptcp/protocol.c +39 -4
index 9eef1fbe300b..d19e7ba5163c 100644
@@ -299,6 +299,12 @@ static void mptcp_check_data_fin(struct sock *sk)
 	}
 }
 
+static void mptcp_schedule_work(struct sock *sk)
+{
+	if (schedule_work(&mptcp_sk(sk)->work))
+		sock_hold(sk);
+}
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk,
 					   unsigned int *bytes)
@@ -356,6 +362,9 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 			seq += len;
 			moved += len;
 
+			if (mptcp_pending_data_fin(sk))
+				mptcp_schedule_work(sk);
+
 			if (WARN_ON_ONCE(map_remaining < len))
 				break;
 		} else {
@@ -375,6 +384,16 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 
 	*bytes = moved;
 
+	/* If the moves have caught up with the DATA_FIN sequence number
+	 * it's time to ack the DATA_FIN and change socket state, but
+	 * this is not a good place to change state. Let the workqueue
+	 * do it.
+	 */
+	if (READ_ONCE(msk->rcv_data_fin) &&
+	    msk->ack_seq + 1 == READ_ONCE(msk->rcv_data_fin_seq) &&
+	    schedule_work(&msk->work))
+		sock_hold(sk);
+
 	return done;
 }
 
@@ -460,7 +479,8 @@ void mptcp_data_acked(struct sock *sk)
 {
 	mptcp_reset_timer(sk);
 
-	if (!sk_stream_is_writeable(sk) &&
+	if ((!sk_stream_is_writeable(sk) ||
+	     (inet_sk_state_load(sk) != TCP_ESTABLISHED)) &&
 	    schedule_work(&mptcp_sk(sk)->work))
 		sock_hold(sk);
 }
@@ -1378,6 +1398,7 @@ static void mptcp_worker(struct work_struct *work)
 
 	lock_sock(sk);
 	mptcp_clean_una(sk);
+	mptcp_check_data_fin_ack(sk);
 	__mptcp_flush_join_list(msk);
 	__mptcp_move_skbs(msk);
 
@@ -1387,6 +1408,8 @@ static void mptcp_worker(struct work_struct *work)
 	if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
 		mptcp_check_for_eof(msk);
 
+	mptcp_check_data_fin(sk);
+
 	if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
 		goto unlock;
 
@@ -1525,8 +1548,15 @@ static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		mptcp_set_timeout(sk, ssk);
-		tcp_send_ack(ssk);
+		if (__mptcp_check_fallback(mptcp_sk(sk))) {
+			pr_debug("Fallback");
+			ssk->sk_shutdown |= how;
+			tcp_shutdown(ssk, how);
+		} else {
+			pr_debug("Sending DATA_FIN on subflow %p", ssk);
+			mptcp_set_timeout(sk, ssk);
+			tcp_send_ack(ssk);
+		}
 		break;
 	}
 
@@ -1565,13 +1595,36 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
-	int prev_state;
 
 	lock_sock(sk);
+	sk->sk_shutdown = SHUTDOWN_MASK;
+
+	if (sk->sk_state == TCP_LISTEN) {
+		inet_sk_state_store(sk, TCP_CLOSE);
+		goto cleanup;
+	} else if (sk->sk_state == TCP_CLOSE) {

net/mptcp/protocol.c static void mptcp_close(struct sock *sk, long timeout) · line 1614

Quoted 10 lines
net/mptcp/protocol.c +8 -1
+		goto cleanup;
+	}
 
-	prev_state = sk->sk_state;
+	if (__mptcp_check_fallback(msk)) {
+		goto update_state;
+	} else if (mptcp_close_state(sk)) {
+		pr_debug("Sending DATA_FIN sk=%p", sk);
+		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+		WRITE_ONCE(msk->snd_data_fin_enable, 1);

Why don't we need anymore to check the prev_state ?!?

Quoted 20 lines
net/mptcp/protocol.c +11 -0
+
+		mptcp_for_each_subflow(msk, subflow) {
+			struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+			mptcp_subflow_shutdown(sk, tcp_sk, SHUTDOWN_MASK);
+		}
+	}
+
+	sk_stream_wait_close(sk, timeout);
+
+update_state:
 	inet_sk_state_store(sk, TCP_CLOSE);
 
+cleanup:
 	/* be sure to always acquire the join list lock, to sync vs
 	 * mptcp_finish_join().
 	 */
@@ -1580,16 +1633,6 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 

net/mptcp/protocol.c static void mptcp_close(struct sock *sk, long timeout) · line 1592

Quoted 10 lines
net/mptcp/protocol.c +0 -10
-	if ((1 << prev_state) &
-	    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
-	     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
-		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
-		WRITE_ONCE(msk->snd_data_fin_enable, 1);
-	} else {
-		/* DATA_FIN has already been sent in other states */
-		WRITE_ONCE(msk->snd_data_fin_enable, 0);
-	}
-

Is a bit confusing that this patch undos [part of] the code introduced by the previous one. Perhaps this chunk and the _shutdown one should be moved to the previous one ? Or perhaps the 2 patches could be squashed?

Quoted 63 lines
net/mptcp/protocol.c +9 -1
 	__mptcp_clear_xmit(sk);
 
 	release_sock(sk);
@@ -2289,7 +2332,15 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	}
 
 	/* If we've already sent a FIN, or it's a closed state, skip this. */
-	if ((how & SEND_SHUTDOWN) &&
+	if (__mptcp_check_fallback(msk)) {
+		if (how == SHUT_WR || how == SHUT_RDWR)
+			inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
+
+		mptcp_for_each_subflow(msk, subflow) {
+			struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+			mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
+		}
+	} else if ((how & SEND_SHUTDOWN) &&
 	    ((1 << sock->sk->sk_state) &
 	     (TCPF_ESTABLISHED | TCPF_SYN_SENT |
 	      TCPF_SYN_RECV | TCPF_CLOSE_WAIT))) {
net/mptcp/subflow.c +8 -3
index 07c6b3a65bb6..d2b61dbbf503 100644
@@ -594,7 +594,8 @@ static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
 	return true;
 }
 
-static enum mapping_status get_mapping_status(struct sock *ssk)
+static enum mapping_status get_mapping_status(struct sock *ssk,
+					      struct mptcp_sock *msk)
 {
 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
 	struct mptcp_ext *mpext;
@@ -644,7 +645,8 @@ static enum mapping_status get_mapping_status(struct sock *ssk)
 
 	if (mpext->data_fin == 1) {
 		if (data_len == 1) {
-			pr_debug("DATA_FIN with no payload");
+			mptcp_update_rcv_data_fin(msk, mpext->data_seq);
+			pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
 			if (subflow->map_valid) {
 				/* A DATA_FIN might arrive in a DSS
 				 * option before the previous mapping
@@ -656,6 +658,9 @@ static enum mapping_status get_mapping_status(struct sock *ssk)
 			} else {
 				return MAPPING_DATA_FIN;
 			}
+		} else {
+			mptcp_update_rcv_data_fin(msk, mpext->data_seq + data_len);
+			pr_debug("DATA_FIN with mapping seq=%llu", mpext->data_seq + data_len);
 		}
 
 		/* Adjust for DATA_FIN using 1 byte of sequence space */
@@ -744,7 +749,7 @@ static bool subflow_check_data_avail(struct sock *ssk)
 		u64 ack_seq;
 		u64 old_ack;
 
-		status = get_mapping_status(ssk);
+		status = get_mapping_status(ssk, msk);
 		pr_debug("msk=%p ssk=%p status=%d", msk, ssk, status);
 		if (status == MAPPING_INVALID) {
 			ssk->sk_err = EBADMSG;
author
#21 lore raw

[MPTCP] Re: [PATCH mptcp-next 08/12] mptcp: Use full MPTCP-level disconnect state machine

Quoted 160 lines · Paolo Abeni
Quoted 159 lines · Mat Martineau

RFC 8684 appendix D describes the connection state machine for MPTCP. This patch implements the DATA_FIN / DATA_ACK exchanges and MPTCP-level socket state changes described in that appendix, rather than simply sending DATA_FIN along with TCP FIN when disconnecting subflows.

DATA_FIN is now sent and acknowledged before shutting down the subflows. Received DATA_FIN information (if not part of a data packet) is written to the MPTCP socket when the incoming DSS option is parsed by the subflow, and the MPTCP worker is scheduled to process the flag. DATA_FIN received as part of a full DSS mapping will be handled when the mapping is processed.

The DATA_FIN is acknowledged by the worker if the reader is caught up. If there is still data to be moved to the MPTCP-level queue, ack_seq will be incremented to account for the DATA_FIN when it reaches the end of the stream and a DATA_ACK will be sent to the peer.

In this RFC patch, DATA_FIN is sent when calling shutdown(fd, SHUT_WR) on the MPTCP socket. mptcp_close() has not yet been updated to wait for the peer to DATA_ACK before forcing all the subflows to be closed and cleaned up. The handshake is working with both peers using test programs with shutdown() and delays, but is not yet working consistently with the self tests. It also doesn't currently detect if fallback has happened, so there are extra acks sent on fallback connections.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c  | 11 ++++++
 net/mptcp/protocol.c | 83 +++++++++++++++++++++++++++++++++++---------
 net/mptcp/subflow.c  | 11 ++++--
 3 files changed, 86 insertions(+), 19 deletions(-)
net/mptcp/options.c +11 -0
index 4e2378369691..72d41c239acf 100644
@@ -864,6 +864,17 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 	if (mp_opt.use_ack)
 		update_una(msk, &mp_opt);
 
+	/* Zero-length packets, like bare ACKs carrying a DATA_FIN, are
+	 * dropped by the caller and not propagated to the MPTCP layer.
+	 * Copy the DATA_FIN information now.
+	 */
+	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
+		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
+		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq) &&
+		    schedule_work(&msk->work))
+			sock_hold(subflow->conn);
+	}
+
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 	if (!mpext)
 		return;
net/mptcp/protocol.c +41 -4
index 9eef1fbe300b..d19e7ba5163c 100644
@@ -299,6 +299,12 @@ static void mptcp_check_data_fin(struct sock *sk)
 	}
 }
 
+static void mptcp_schedule_work(struct sock *sk)
+{
+	if (schedule_work(&mptcp_sk(sk)->work))
+		sock_hold(sk);
+}
+
 static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 					   struct sock *ssk,
 					   unsigned int *bytes)
@@ -356,6 +362,9 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 			seq += len;
 			moved += len;
 
+			if (mptcp_pending_data_fin(sk))
+				mptcp_schedule_work(sk);
+
 			if (WARN_ON_ONCE(map_remaining < len))
 				break;
 		} else {
@@ -375,6 +384,16 @@ static bool __mptcp_move_skbs_from_subflow(struct mptcp_sock *msk,
 
 	*bytes = moved;
 
+	/* If the moves have caught up with the DATA_FIN sequence number
+	 * it's time to ack the DATA_FIN and change socket state, but
+	 * this is not a good place to change state. Let the workqueue
+	 * do it.
+	 */
+	if (READ_ONCE(msk->rcv_data_fin) &&
+	    msk->ack_seq + 1 == READ_ONCE(msk->rcv_data_fin_seq) &&
+	    schedule_work(&msk->work))
+		sock_hold(sk);
+
 	return done;
 }
 
@@ -460,7 +479,8 @@ void mptcp_data_acked(struct sock *sk)
 {
 	mptcp_reset_timer(sk);
 
-	if (!sk_stream_is_writeable(sk) &&
+	if ((!sk_stream_is_writeable(sk) ||
+	     (inet_sk_state_load(sk) != TCP_ESTABLISHED)) &&
 	    schedule_work(&mptcp_sk(sk)->work))
 		sock_hold(sk);
 }
@@ -1378,6 +1398,7 @@ static void mptcp_worker(struct work_struct *work)
 
 	lock_sock(sk);
 	mptcp_clean_una(sk);
+	mptcp_check_data_fin_ack(sk);
 	__mptcp_flush_join_list(msk);
 	__mptcp_move_skbs(msk);
 
@@ -1387,6 +1408,8 @@ static void mptcp_worker(struct work_struct *work)
 	if (test_and_clear_bit(MPTCP_WORK_EOF, &msk->flags))
 		mptcp_check_for_eof(msk);
 
+	mptcp_check_data_fin(sk);
+
 	if (!test_and_clear_bit(MPTCP_WORK_RTX, &msk->flags))
 		goto unlock;
 
@@ -1525,8 +1548,15 @@ static void mptcp_subflow_shutdown(struct sock *sk, struct sock *ssk, int how)
 		tcp_disconnect(ssk, O_NONBLOCK);
 		break;
 	default:
-		mptcp_set_timeout(sk, ssk);
-		tcp_send_ack(ssk);
+		if (__mptcp_check_fallback(mptcp_sk(sk))) {
+			pr_debug("Fallback");
+			ssk->sk_shutdown |= how;
+			tcp_shutdown(ssk, how);
+		} else {
+			pr_debug("Sending DATA_FIN on subflow %p", ssk);
+			mptcp_set_timeout(sk, ssk);
+			tcp_send_ack(ssk);
+		}
 		break;
 	}
 
@@ -1565,13 +1595,36 @@ static void mptcp_close(struct sock *sk, long timeout)
 	struct mptcp_subflow_context *subflow, *tmp;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	LIST_HEAD(conn_list);
-	int prev_state;
 
 	lock_sock(sk);
+	sk->sk_shutdown = SHUTDOWN_MASK;
+
+	if (sk->sk_state == TCP_LISTEN) {
+		inet_sk_state_store(sk, TCP_CLOSE);
+		goto cleanup;
+	} else if (sk->sk_state == TCP_CLOSE) {
+		goto cleanup;
+	}
 
Quoted 9 lines

net/mptcp/protocol.c static void mptcp_close(struct sock *sk, long timeout) · line 1614

Quoted 7 lines
net/mptcp/protocol.c +6 -1
-	prev_state = sk->sk_state;
+	if (__mptcp_check_fallback(msk)) {
+		goto update_state;
+	} else if (mptcp_close_state(sk)) {
+		pr_debug("Sending DATA_FIN sk=%p", sk);
+		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
+		WRITE_ONCE(msk->snd_data_fin_enable, 1);

Why don't we need anymore to check the prev_state ?!?

That's handled inside mptcp_close_state() (return value tells this function whether DATA_FIN needs to be sent), and the inet_sk_state_store has not yet happened at this point in the code.

Quoted 20 lines
Quoted 20 lines
net/mptcp/protocol.c +11 -0
+
+		mptcp_for_each_subflow(msk, subflow) {
+			struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+			mptcp_subflow_shutdown(sk, tcp_sk, SHUTDOWN_MASK);
+		}
+	}
+
+	sk_stream_wait_close(sk, timeout);
+
+update_state:
 	inet_sk_state_store(sk, TCP_CLOSE);
 
+cleanup:
 	/* be sure to always acquire the join list lock, to sync vs
 	 * mptcp_finish_join().
 	 */
@@ -1580,16 +1633,6 @@ static void mptcp_close(struct sock *sk, long timeout)
 	spin_unlock_bh(&msk->join_list_lock);
 	list_splice_init(&msk->conn_list, &conn_list);
 
Quoted 14 lines

net/mptcp/protocol.c static void mptcp_close(struct sock *sk, long timeout) · line 1592

Quoted 10 lines
net/mptcp/protocol.c +0 -10
-	if ((1 << prev_state) &
-	    (TCPF_ESTABLISHED | TCPF_SYN_SENT |
-	     TCPF_SYN_RECV | TCPF_CLOSE_WAIT)) {
-		WRITE_ONCE(msk->write_seq, msk->write_seq + 1);
-		WRITE_ONCE(msk->snd_data_fin_enable, 1);
-	} else {
-		/* DATA_FIN has already been sent in other states */
-		WRITE_ONCE(msk->snd_data_fin_enable, 0);
-	}
-

Is a bit confusing that this patch undos [part of] the code introduced by the previous one. Perhaps this chunk and the _shutdown one should be moved to the previous one ? Or perhaps the 2 patches could be squashed?

I should have read ahead before replying to the previous patch. Squashing the two sounds good to me.

Quoted 59 lines
Quoted 59 lines
net/mptcp/protocol.c +9 -1
 	__mptcp_clear_xmit(sk);
 
 	release_sock(sk);
@@ -2289,7 +2332,15 @@ static int mptcp_shutdown(struct socket *sock, int how)
 	}
 
 	/* If we've already sent a FIN, or it's a closed state, skip this. */
-	if ((how & SEND_SHUTDOWN) &&
+	if (__mptcp_check_fallback(msk)) {
+		if (how == SHUT_WR || how == SHUT_RDWR)
+			inet_sk_state_store(sock->sk, TCP_FIN_WAIT1);
+
+		mptcp_for_each_subflow(msk, subflow) {
+			struct sock *tcp_sk = mptcp_subflow_tcp_sock(subflow);
+			mptcp_subflow_shutdown(sock->sk, tcp_sk, how);
+		}
+	} else if ((how & SEND_SHUTDOWN) &&
 	    ((1 << sock->sk->sk_state) &
 	     (TCPF_ESTABLISHED | TCPF_SYN_SENT |
 	      TCPF_SYN_RECV | TCPF_CLOSE_WAIT))) {
net/mptcp/subflow.c +7 -2
index 07c6b3a65bb6..d2b61dbbf503 100644
@@ -594,7 +594,8 @@ static bool validate_mapping(struct sock *ssk, struct sk_buff *skb)
 	return true;
 }
 
-static enum mapping_status get_mapping_status(struct sock *ssk)
+static enum mapping_status get_mapping_status(struct sock *ssk,
+					      struct mptcp_sock *msk)
 {
 	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
 	struct mptcp_ext *mpext;
@@ -644,7 +645,8 @@ static enum mapping_status get_mapping_status(struct sock *ssk)
 
 	if (mpext->data_fin == 1) {
 		if (data_len == 1) {
-			pr_debug("DATA_FIN with no payload");
+			mptcp_update_rcv_data_fin(msk, mpext->data_seq);
+			pr_debug("DATA_FIN with no payload seq=%llu", mpext->data_seq);
 			if (subflow->map_valid) {
 				/* A DATA_FIN might arrive in a DSS
 				 * option before the previous mapping
@@ -656,6 +658,9 @@ static enum mapping_status get_mapping_status(struct sock *ssk)
 			} else {
 				return MAPPING_DATA_FIN;
 			}
+		} else {
+			mptcp_update_rcv_data_fin(msk, mpext->data_seq + data_len);
+			pr_debug("DATA_FIN with mapping seq=%llu", mpext->data_seq + data_len);
 		}
 
 		/* Adjust for DATA_FIN using 1 byte of sequence space */
@@ -744,7 +749,7 @@ static bool subflow_check_data_avail(struct sock *ssk)
 		u64 ack_seq;
 		u64 old_ack;
 
Quoted 8 lines

net/mptcp/subflow.c static bool subflow_check_data_avail(struct sock *ssk) · line 755

Quoted 5 lines
net/mptcp/subflow.c +1 -1
-		status = get_mapping_status(ssk);
+		status = get_mapping_status(ssk, msk);
 		pr_debug("msk=%p ssk=%p status=%d", msk, ssk, status);
 		if (status == MAPPING_INVALID) {
 			ssk->sk_err = EBADMSG;

--
Mat Martineau
Intel

author
#22 lore raw

PATCH 9/12 mptcp: Only use subflow EOF signaling on fallback connections

In reply to Mat Martineau

The MPTCP state machine handles disconnections on non-fallback connections, but the mptcp_sock still needs to get notified when fallback subflows disconnect.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/subflow.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/mptcp/subflow.c +2 -1
index d2b61dbbf503..68f4034ff35b 100644
@@ -1154,7 +1154,8 @@ static void subflow_state_change(struct sock *sk)
 	if (mptcp_subflow_data_available(sk))
 		mptcp_data_ready(parent, sk);
 
-	if (!(parent->sk_shutdown & RCV_SHUTDOWN) &&
+	if (__mptcp_check_fallback(mptcp_sk(parent)) &&
+	    !(parent->sk_shutdown & RCV_SHUTDOWN) &&
 	    !subflow->rx_eof && subflow_is_done(sk)) {
 		subflow->rx_eof = 1;
 		mptcp_subflow_eof(parent);
Signature · 1 line
2.27.0
author
#23 lore raw

PATCH 10/12 mptcp: Skip unnecessary skb extension allocation for bare acks

In reply to Mat Martineau

Bare TCP ack skbs are freed right after MPTCP sees them, so the work to allocate, zero, and populate the MPTCP skb extension is wasted. Detect these skbs and do not add skb extensions to them.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/options.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
net/mptcp/options.c +6 -3
index 72d41c239acf..edd56d61d29f 100644
@@ -864,15 +864,18 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 	if (mp_opt.use_ack)
 		update_una(msk, &mp_opt);
 
-	/* Zero-length packets, like bare ACKs carrying a DATA_FIN, are
-	 * dropped by the caller and not propagated to the MPTCP layer.
-	 * Copy the DATA_FIN information now.
+	/* Zero-data-length packets are dropped by the caller and not
+	 * propagated to the MPTCP layer, so the skb extension does not
+	 * need to be allocated or populated. DATA_FIN information, if
+	 * present, needs to be updated here before the skb is freed.
 	 */
 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
 		if (mp_opt.data_fin && mp_opt.data_len == 1 &&
 		    mptcp_update_rcv_data_fin(msk, mp_opt.data_seq) &&
 		    schedule_work(&msk->work))
 			sock_hold(subflow->conn);
+
+		return;
 	}
 
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
Signature · 1 line
2.27.0
author
#24 lore raw

PATCH 11/12 mptcp: Safely read sequence number when lock isn't held

In reply to Mat Martineau

The MPTCP socket's write_seq member should be read with READ_ONCE() when the msk lock is not held.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/mptcp/protocol.c +1 -1
index d19e7ba5163c..30c5810771dd 100644
@@ -1273,7 +1273,7 @@ static void mptcp_retransmit_handler(struct sock *sk)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 
-	if (atomic64_read(&msk->snd_una) == msk->write_seq) {
+	if (atomic64_read(&msk->snd_una) == READ_ONCE(msk->write_seq)) {
 		mptcp_stop_timer(sk);
 	} else {
 		set_bit(MPTCP_WORK_RTX, &msk->flags);
Signature · 1 line
2.27.0
author
#25 lore raw

PATCH 12/12 mptcp: Safely store sequence number when sending data

In reply to Mat Martineau

The MPTCP socket's write_seq member can be read without the msk lock held, so use WRITE_ONCE() to store it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/mptcp/protocol.c +1 -1
index 30c5810771dd..ed3ca80d72f8 100644
@@ -797,7 +797,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 out:
 	if (!retransmission)
 		pfrag->offset += frag_truesize;
-	*write_seq += ret;
+	WRITE_ONCE(*write_seq, *write_seq + ret);
 	mptcp_subflow_ctx(ssk)->rel_write_seq += ret;
 
 	return ret;
Signature · 1 line
2.27.0
#26 lore raw

[MPTCP] Re: [PATCH mptcp-next 12/12] mptcp: Safely store sequence number when sending data

Quoted 13 lines · Mat Martineau

The MPTCP socket's write_seq member can be read without the msk lock held, so use WRITE_ONCE() to store it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/mptcp/protocol.c +0 -0
index 30c5810771dd..ed3ca80d72f8 100644

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 801

Quoted 6 lines
net/mptcp/protocol.c +1 -1
@@ -797,7 +797,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 out:
 	if (!retransmission)
 		pfrag->offset += frag_truesize;
-	*write_seq += ret;
+	WRITE_ONCE(*write_seq, *write_seq + ret);
 	mptcp_subflow_ctx(ssk)->rel_write_seq += ret;

I think the msk lock should be already held here ?!?

/P

author
#27 lore raw

[MPTCP] Re: [PATCH mptcp-next 12/12] mptcp: Safely store sequence number when sending data

Quoted 9 lines · Paolo Abeni
Quoted 8 lines · Mat Martineau

The MPTCP socket's write_seq member can be read without the msk lock held, so use WRITE_ONCE() to store it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 801

Quoted 11 lines
net/mptcp/protocol.c +1 -1
index 30c5810771dd..ed3ca80d72f8 100644
@@ -797,7 +797,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 out:
 	if (!retransmission)
 		pfrag->offset += frag_truesize;
-	*write_seq += ret;
+	WRITE_ONCE(*write_seq, *write_seq + ret);
 	mptcp_subflow_ctx(ssk)->rel_write_seq += ret;

I think the msk lock should be already held here ?!?

Yes, but write_seq might be concurrently read by another CPU, and we want don't want the compiler to invent stores as described in memory-barriers.txt

My (possibly flawed?) understanding of READ_ONCE/WRITE_ONCE has led me to these rules of thumb:

* Always use WRITE_ONCE to avoid storing weird intermediate values that
can be seen by concurrent readers

* For values that are only written with the lock held, but have
concurrent readers, READ_ONCE only required when not locked. The compiler will correctly synchronize with any readers/writers on the same locked thread.

* For values that have writes without the lock held (access is not
coordinated by locks at all), always use READ_ONCE. The compiler doesn't know when any write will update the memory location.

I know the full story for READ_ONCE/WRITE_ONCE syncronization is more complicated, but for the way we've been using them in MPTCP it seems like the above rules work ok. If I'm mistaken I'd really like to fix that so I write correct code :)

--
Mat Martineau
Intel

#28 lore raw

[MPTCP] Re: [PATCH mptcp-next 12/12] mptcp: Safely store sequence number when sending data

Quoted 25 lines · Mat Martineau
Quoted 9 lines · Paolo Abeni
Quoted 8 lines · Mat Martineau

The MPTCP socket's write_seq member can be read without the msk lock held, so use WRITE_ONCE() to store it.

Signed-off-by Mat Martineau <mathew.j.martineau(a)linux.intel.com>
---
 net/mptcp/protocol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

net/mptcp/protocol.c static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk, · line 801

Quoted 11 lines
net/mptcp/protocol.c +1 -1
index 30c5810771dd..ed3ca80d72f8 100644
@@ -797,7 +797,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 out:
 	if (!retransmission)
 		pfrag->offset += frag_truesize;
-	*write_seq += ret;
+	WRITE_ONCE(*write_seq, *write_seq + ret);
 	mptcp_subflow_ctx(ssk)->rel_write_seq += ret;

I think the msk lock should be already held here ?!?

Quoted 3 lines

Yes, but write_seq might be concurrently read by another CPU, and we want don't want the compiler to invent stores as described in memory-barriers.txt

Yep, you are right. I misread the commit message and wend ahead too hastly. The patch LGTM.

Thanks,

Paolo