Skip to contentSkip to conversation

mptcp: implement retransmit infrastructure

RFC PATCH Superseded Paolo Abeni 2 participants 14 messages Aug 22, 2019 – Aug 24, 2019

Subsystems (2)
lore
Share & tools
lore

Paolo Abeni

author
#0 lore raw

v2 cover mptcp: implement retransmit infrastructure

Note this is still early draft, only lightly tested. It builds and somtimes it passes the selftests

In addition to the first iteration now:
- implement actual retransmission infrastructure and trigger it when MPTCP timer expires (patches 7,8)
- fixes several bugs in many patches - but others are still there

RFC v1:
- updates the MPTCP unacked sequence number (patches 1-3) on sendmsg()
- also try to cope with 32 bit sequence number
- queue data (page frags) in MPTCP retransmission queue
- cleanup acked data from retransmission queue and schedule a timeout to update MPTCP unacked sequence number (patch 5)

Open points:
- IIRC, the RFC mandate to always retransmit even on the failing subflow (?!?) why? Should we preserve the original subflow seq number, so that retransmission there will get the same mapping as the original packet?

Paolo Abeni (8):
mptcp: move before/after64 into the header file
mptcp: update per subflow unacked sequence on pkt reception
mptcp: update msk unacked sequence in sendmsg()
mptcp: queue data for mptcp level retransmission
mptcp: use retransmission timer to update msk una
mptcp: implement memory accounting for mptcp rtx queue
mptcp: rework mptcp_sendmsg_frag to accept optional dfrag
mptcp: implement and use MPTCP-level retransmission

 net/mptcp/options.c  |  31 +++-
 net/mptcp/protocol.c | 405 +++++++++++++++++++++++++++++++++++++++----
 net/mptcp/protocol.h |  41 +++++
 3 files changed, 437 insertions(+), 40 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

RFC PATCH v2 1/8 mptcp: move before/after64 into the header file

So that we can reuse in multiple files

Signed-off-by: Paolo Abeni <pabeni(a)redhat.com>
--
Squash to "mptcp: Implement MPTCP receive path"

---
 net/mptcp/protocol.c | 7 -------
 net/mptcp/protocol.h | 7 +++++++
 2 files changed, 7 insertions(+), 7 deletions(-)
net/mptcp/protocol.c +0 -7
index aaa71b161c1a..5b7ddbe64e4b 100644
@@ -17,13 +17,6 @@
 #include <net/mptcp.h>
 #include "protocol.h"
 
-static inline bool before64(__u64 seq1, __u64 seq2)
-{
-	return (__s64)(seq1 - seq2) < 0;
-}
-
-#define after64(seq2, seq1)	before64(seq1, seq2)
-
 static struct socket *__mptcp_fallback_get_ref(const struct mptcp_sock *msk)
 {
 	sock_owned_by_me((const struct sock *)msk);
net/mptcp/protocol.h +7 -0
index 68877e952780..9f261ee31917 100644
@@ -275,4 +275,11 @@ static inline struct mptcp_ext *mptcp_get_ext(struct sk_buff *skb)
 	return (struct mptcp_ext *)skb_ext_find(skb, SKB_EXT_MPTCP);
 }
 
+static inline bool before64(__u64 seq1, __u64 seq2)
+{
+	return (__s64)(seq1 - seq2) < 0;
+}
+
+#define after64(seq2, seq1)	before64(seq1, seq2)
+
 #endif /* __MPTCP_PROTOCOL_H */
Signature · 1 line
2.21.0
author
#2 lore raw

RFC PATCH v2 2/8 mptcp: update per subflow unacked sequence on pkt reception

In reply to Paolo Abeni

So that we keep per subflow unacked sequence number consistent; since we update only per socket data, no additional locking is required.

Subflow and msk unacked seq are updated at different stages to keep the locking schema simple.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
rfc v1 -> rfc v2:
- initialize subflow snd_una on subflow connect()/accept()

---
 net/mptcp/options.c  | 31 +++++++++++++++++++++++++------
 net/mptcp/protocol.c |  2 ++
 net/mptcp/protocol.h |  1 +
 3 files changed, 28 insertions(+), 6 deletions(-)
net/mptcp/options.c +25 -6
index 0e7abcea5bae..e0895834ff82 100644
@@ -524,6 +524,21 @@ bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
 	return false;
 }
 
+static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
+{
+	u32 old_ack32, cur_ack32;
+
+	if (use_64bit)
+		return cur_ack;
+
+	old_ack32 = (u32)old_ack;
+	cur_ack32 = (u32)cur_ack;
+	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
+	if (unlikely(before(cur_ack32, old_ack32)))
+		return cur_ack + (1LL<<32);
+	return cur_ack;
+}
+
 void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 			    struct tcp_options_received *opt_rx)
 {
@@ -537,6 +552,16 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 	if (!mp_opt->dss)
 		return;
 
+	/* we can't wait for recvmsg() to update the ack_seq, otherwise
+	 * monodirectional flows will stuck
+	 */
+	if (mp_opt->use_ack) {
+		struct subflow_context *subflow = subflow_ctx(sk);
+
+		subflow->snd_una = expand_ack(subflow->snd_una,
+					      mp_opt->data_ack, mp_opt->ack64);
+	}
+
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 	if (!mpext)
 		return;
@@ -553,12 +578,6 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 		mpext->use_checksum = mp_opt->use_checksum;
 	}
 
-	if (mp_opt->use_ack) {
-		mpext->data_ack = mp_opt->data_ack;
-		mpext->use_ack = 1;
-		mpext->ack64 = mp_opt->ack64;
-	}
-
 	mpext->data_fin = mp_opt->data_fin;
 
 	if (msk)
net/mptcp/protocol.c +2 -0
index 5b7ddbe64e4b..65f7cc4712ef 100644
@@ -674,6 +674,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
 		subflow->tcp_sock = new_sock;
+		subflow->snd_una = msk->write_seq;
 		newsk = new_mptcp_sock;
 		subflow->conn = new_mptcp_sock;
 		list_add(&subflow->node, &msk->conn_list);
@@ -809,6 +810,7 @@ void mptcp_finish_connect(struct sock *sk, int mp_capable)
 		subflow->map_seq = ack_seq;
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
+		subflow->snd_una = msk->write_seq;
 
 		list_add(&subflow->node, &msk->conn_list);
 		msk->subflow = NULL;
net/mptcp/protocol.h +1 -0
index 9f261ee31917..e37fd4bcd904 100644
@@ -165,6 +165,7 @@ struct subflow_context {
 	u32	token;
 	u32     rel_write_seq;
 	u64     idsn;
+	u64	snd_una;
 	u64	map_seq;
 	u32	map_subflow_seq;
 	u32	ssn_offset;
Signature · 1 line
2.21.0
author
#6 lore raw

RFC PATCH v2 3/8 mptcp: update msk unacked sequence in sendmsg()

In reply to Paolo Abeni

We need to walk all the subflows to update the msk unacked; so we try to avoid doing it too often using the following heuristic:
we do the update only when the current value is outside the current subflow's window size.

Note that any retransmission policy must be consistent with the above heuristic.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
rfc v1 -> rfc v2:
- protect against wrap-around in mptcp_update_subflow_una() (Florian)
- use ssk snd_cwnd, not sk's one (fix crash)

---
 net/mptcp/protocol.c | 38 ++++++++++++++++++++++++++++++++++++++
 net/mptcp/protocol.h |  1 +
 2 files changed, 39 insertions(+)
net/mptcp/protocol.c +38 -0
index 65f7cc4712ef..3137bae504e4 100644
@@ -67,6 +67,40 @@ static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk,
 	return mpext && mpext->data_seq + mpext->data_len == msk->write_seq;
 }
 
+static u64 mptcp_update_msk_una(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct subflow_context *subflow;
+	u64 snd_una = msk->snd_una;
+
+	mptcp_for_each_subflow(msk, subflow) {
+		u64 subflow_snd_una = READ_ONCE(subflow->snd_una);
+
+		if (after64(subflow_snd_una, snd_una))
+			snd_una = subflow_snd_una;
+	}
+	msk->snd_una = snd_una;
+	return snd_una;
+}
+
+static void mptcp_update_subflow_una(struct sock *sk, struct sock *ssk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct subflow_context *subflow;
+	u64 snd_una = msk->snd_una;
+
+	/* heuristic to avoid doing the expensive data ack update frequently */
+	if (before64(snd_una + tcp_sk(ssk)->snd_cwnd, msk->write_seq))
+		snd_una = mptcp_update_msk_una(sk);
+
+	/* if we msk just switched to this subflow, update subflow's una
+	 * to allow 32 bit ack update to be more accurate
+	 */
+	subflow = subflow_ctx(ssk);
+	if (after64(snd_una, subflow->snd_una))
+		subflow->snd_una = snd_una;
+}
+
 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 			      struct msghdr *msg, long *timeo, int *pmss_now,
 			      int *ps_goal)
@@ -209,6 +243,8 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	}
 
 	lock_sock(ssk);
+	mptcp_update_subflow_una(sk, ssk);
+
 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
 	while (msg_data_left(msg)) {
 		ret = mptcp_sendmsg_frag(sk, ssk, msg, &timeo, &mss_now,
@@ -668,6 +704,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 
 		crypto_key_sha1(msk->remote_key, NULL, &ack_seq);
 		msk->write_seq = subflow->idsn + 1;
+		msk->snd_una = msk->write_seq;
 		ack_seq++;
 		msk->ack_seq = ack_seq;
 		subflow->map_seq = ack_seq;
@@ -805,6 +842,7 @@ void mptcp_finish_connect(struct sock *sk, int mp_capable)
 
 		crypto_key_sha1(msk->remote_key, NULL, &ack_seq);
 		msk->write_seq = subflow->idsn + 1;
+		msk->snd_una = msk->write_seq;
 		ack_seq++;
 		msk->ack_seq = ack_seq;
 		subflow->map_seq = ack_seq;
net/mptcp/protocol.h +1 -0
index e37fd4bcd904..9220afe0d362 100644
@@ -116,6 +116,7 @@ struct mptcp_sock {
 	u64		remote_key;
 	u64		write_seq;
 	u64		ack_seq;
+	u64		snd_una;
 	u32		token;
 	u16		dport;
 	struct list_head conn_list;
Signature · 1 line
2.21.0
author
#7 lore raw

RFC PATCH v2 4/8 mptcp: queue data for mptcp level retransmission

In reply to Paolo Abeni

keep the send page fragment on an MPTCP level retransmission queue. the queue entries are allocated inside the page frag allocator, acquiring an additional reference to the page for each list entry.

Also switch to a custom page frag refill function, to ensure that the current page fragment can always host an MPTCP rtx queue entry.

The MPTCP rtx queue is flushed at disconnect() and close() time

Note that now we need to call __mptcp_init_sock() regarless of mptcp enable status, as the destructor will try to walk the rtx_queue.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
rfc v1 -> rfc v2:
- rtx_queue_tail is now static inline (fix link issue)
- simpler ALIGN() usage (Florian)
- fix pfrag accounting, adding rtx entry overhead when allocating a new one
- fixed page -> virt conversion
- always init mptcp data structs
- move rtx queue flushing into both disconnect() and close()

---
 net/mptcp/protocol.c | 106 +++++++++++++++++++++++++++++++++++++++----
 net/mptcp/protocol.h |  20 ++++++++
 2 files changed, 118 insertions(+), 8 deletions(-)
net/mptcp/protocol.c +98 -8
index 3137bae504e4..21547a21b4f2 100644
@@ -67,6 +67,14 @@ static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk,
 	return mpext && mpext->data_seq + mpext->data_len == msk->write_seq;
 }
 
+static inline bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
+					      const struct page_frag *pfrag,
+					      const struct mptcp_data_frag *df)
+{
+	return df && pfrag->page == df->page &&
+		df->data_seq + df->data_len == msk->write_seq;
+}
+
 static u64 mptcp_update_msk_una(struct sock *sk)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -101,14 +109,46 @@ static void mptcp_update_subflow_una(struct sock *sk, struct sock *ssk)
 		subflow->snd_una = snd_una;
 }
 
+/* ensure we get enough memory for the frag hdr, beyond some minimal amount of
+ * data
+ */
+bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
+{
+	if (likely(skb_page_frag_refill(32U + sizeof(struct mptcp_data_frag),
+					pfrag, sk->sk_allocation)))
+		return true;
+
+	sk->sk_prot->enter_memory_pressure(sk);
+	sk_stream_moderate_sndbuf(sk);
+	return false;
+}
+
+static inline struct mptcp_data_frag *
+mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
+		      int orig_offset)
+{
+	int offset = ALIGN(orig_offset, sizeof(long));
+	struct mptcp_data_frag *dfrag;
+
+	dfrag = (struct mptcp_data_frag *)(page_to_virt(pfrag->page) + offset);
+	dfrag->data_len = 0;
+	dfrag->data_seq = msk->write_seq;
+	dfrag->overhead = offset - orig_offset + sizeof(struct mptcp_data_frag);
+	dfrag->offset = offset + sizeof(struct mptcp_data_frag);
+	dfrag->page = pfrag->page;
+
+	return dfrag;
+}
+
 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 			      struct msghdr *msg, long *timeo, int *pmss_now,
 			      int *ps_goal)
 {
-	int mss_now, avail_size, size_goal, ret;
+	int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0;
+	bool dfrag_collapsed, collapsed, can_collapse = false;
 	struct mptcp_sock *msk = mptcp_sk(sk);
-	bool collapsed, can_collapse = false;
 	struct mptcp_ext *mpext = NULL;
+	struct mptcp_data_frag *dfrag;
 	struct page_frag *pfrag;
 	struct sk_buff *skb;
 	size_t psize;
@@ -117,7 +157,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	 * from one substream to another, but do per subflow memory accounting
 	 */
 	pfrag = sk_page_frag(sk);
-	while (!sk_page_frag_refill(ssk, pfrag)) {
+	while (!mptcp_page_frag_refill(ssk, pfrag)) {
 		ret = sk_stream_wait_memory(ssk, timeo);
 		if (ret)
 			return ret;
@@ -147,11 +187,22 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 		else
 			can_collapse = false;
 	}
-	psize = min_t(size_t, pfrag->size - pfrag->offset, avail_size);
+
+	/* reuse tail pfrag, if possible, or carve a new one from the page
+	 * allocator
+	 */
+	dfrag = mptcp_rtx_tail(sk);
+	offset = pfrag->offset;
+	dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
+	if (!dfrag_collapsed) {
+		dfrag = mptcp_carve_data_frag(msk, pfrag, offset);
+		offset = dfrag->offset;
+	}
+	psize = min_t(size_t, pfrag->size - offset, avail_size);
 
 	/* Copy to page */
 	pr_debug("left=%zu", msg_data_left(msg));
-	psize = copy_page_from_iter(pfrag->page, pfrag->offset,
+	psize = copy_page_from_iter(pfrag->page, offset,
 				    min_t(size_t, msg_data_left(msg), psize),
 				    &msg->msg_iter);
 	pr_debug("left=%zu", msg_data_left(msg));
@@ -161,13 +212,24 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	/* tell the TCP stack to delay the push so that we can safely
 	 * access the skb after the sendpages call
 	 */
-	ret = do_tcp_sendpages(ssk, pfrag->page, pfrag->offset, psize,
+	ret = do_tcp_sendpages(ssk, pfrag->page, offset, psize,
 			       msg->msg_flags | MSG_SENDPAGE_NOTLAST);
 	if (ret <= 0)
 		return ret;
+
+	frag_truesize += ret;
 	if (unlikely(ret < psize))
 		iov_iter_revert(&msg->msg_iter, psize - ret);
 
+	/* send succesful, keep track of sent data for mptcp-level
+	 * retransmission
+	 */
+	dfrag->data_len += ret;
+	if (!dfrag_collapsed) {
+		get_page(dfrag->page);
+		list_add_tail(&dfrag->list, &msk->rtx_queue);
+	}
+
 	collapsed = skb == tcp_write_queue_tail(ssk);
 	BUG_ON(collapsed && !can_collapse);
 	if (collapsed) {
@@ -196,7 +258,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	 */
 
 out:
-	pfrag->offset += ret;
+	pfrag->offset += frag_truesize;
 	msk->write_seq += ret;
 	subflow_ctx(ssk)->rel_write_seq += ret;
 
@@ -615,16 +677,33 @@ static int __mptcp_init_sock(struct sock *sk)
 	pr_debug("msk=%p", msk);
 
 	INIT_LIST_HEAD(&msk->conn_list);
+	INIT_LIST_HEAD(&msk->rtx_queue);
 
 	return 0;
 }
 
 static int mptcp_init_sock(struct sock *sk)
 {
+	int ret = __mptcp_init_sock(sk);
+
+	if (ret)
+		return ret;
+
 	if (!mptcp_is_enabled(sock_net(sk)))
 		return -ENOPROTOOPT;
 
-	return __mptcp_init_sock(sk);
+	return 0;
+}
+
+static void __mptcp_clear_xmit(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct mptcp_data_frag *dtmp, *dfrag;
+
+	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
+		list_del(&dfrag->list);
+		put_page(dfrag->page);
+	}
 }
 
 static void mptcp_close(struct sock *sk, long timeout)
@@ -651,12 +730,22 @@ static void mptcp_close(struct sock *sk, long timeout)
 		pr_debug("conn_list->subflow=%p", subflow);
 		sock_release(mptcp_subflow_tcp_socket(subflow));
 	}
+
+	__mptcp_clear_xmit(sk);
 	release_sock(sk);
 
 	sock_orphan(sk);
 	sock_put(sk);
 }
 
+static int mptcp_disconnect(struct sock *sk, int flags)
+{
+	lock_sock(sk);
+	__mptcp_clear_xmit(sk);
+	release_sock(sk);
+	return tcp_disconnect(sk, flags);
+}
+
 static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 				 bool kern)
 {
@@ -884,6 +973,7 @@ static struct proto mptcp_prot = {
 	.name		= "MPTCP",
 	.owner		= THIS_MODULE,
 	.init		= mptcp_init_sock,
+	.disconnect	= mptcp_disconnect,
 	.close		= mptcp_close,
 	.accept		= mptcp_accept,
 	.setsockopt	= mptcp_setsockopt,
net/mptcp/protocol.h +20 -0
index 9220afe0d362..8b3811b550dd 100644
@@ -108,6 +108,15 @@ struct mptcp_pm_data {
 	u32	token;
 };
 
+struct mptcp_data_frag {
+	struct list_head list;
+	u64 data_seq;
+	int data_len;
+	int offset;
+	int overhead;
+	struct page *page;
+};
+
 /* MPTCP connection sock */
 struct mptcp_sock {
 	/* inet_connection_sock must be the first member */
@@ -120,6 +129,7 @@ struct mptcp_sock {
 	u32		token;
 	u16		dport;
 	struct list_head conn_list;
+	struct list_head rtx_queue;
 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
 	struct mptcp_pm_data	pm;
 	u8		addr_signal;
@@ -133,6 +143,16 @@ static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
 	return (struct mptcp_sock *)sk;
 }
 
+static inline struct mptcp_data_frag *mptcp_rtx_tail(const struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	if (list_empty(&msk->rtx_queue))
+		return NULL;
+
+	return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
+}
+
 struct subflow_request_sock {
 	struct	tcp_request_sock sk;
 	u8	mp_capable : 1,
Signature · 1 line
2.21.0
author
#9 lore raw

RFC PATCH v2 5/8 mptcp: use retransmission timer to update msk una

In reply to Paolo Abeni

plugging unacked sequence update into sendmsg() is not enough; use the msk icsk retransmit timer to trigger una updates. The timer freqeuncy is based on the current subflow RTO estimation.

The timer is clearer for good by __mptcp_clear_xmit()

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
rfc v1 -> rfc v2:
- protect against wrap-around while cleaning-up dfrags in mptcp_update_msk_una()

---
 net/mptcp/protocol.c | 116 ++++++++++++++++++++++++++++++++++++++++++-
 net/mptcp/protocol.h |   1 +
 2 files changed, 115 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +114 -2
index 21547a21b4f2..b77669b0c79a 100644
@@ -17,6 +17,33 @@
 #include <net/mptcp.h>
 #include "protocol.h"
 
+static unsigned long mptcp_timeout(const struct sock *sk,
+				   const struct sock *ssk)
+{
+	unsigned long tout = inet_csk(ssk)->icsk_pending ?
+			     inet_csk(ssk)->icsk_timeout - jiffies : 0;
+
+	if (!tout)
+		tout = inet_csk(sk)->icsk_timeout;
+	return tout ?: TCP_RTO_MIN;
+}
+
+static void mptcp_reset_timer(struct sock *sk, unsigned long tout)
+{
+	struct inet_connection_sock *icsk = inet_csk(sk);
+
+	sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + tout);
+	icsk->icsk_timeout = tout;
+}
+
+static void mptcp_stop_timer(struct sock *sk)
+{
+	struct inet_connection_sock *icsk = inet_csk(sk);
+
+	sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
+	icsk->icsk_timeout = 0;
+}
+
 static struct socket *__mptcp_fallback_get_ref(const struct mptcp_sock *msk)
 {
 	sock_owned_by_me((const struct sock *)msk);
@@ -78,6 +105,7 @@ static inline bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
 static u64 mptcp_update_msk_una(struct sock *sk)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct mptcp_data_frag *dtmp, *dfrag;
 	struct subflow_context *subflow;
 	u64 snd_una = msk->snd_una;
 
@@ -87,7 +115,19 @@ static u64 mptcp_update_msk_una(struct sock *sk)
 		if (after64(subflow_snd_una, snd_una))
 			snd_una = subflow_snd_una;
 	}
-	msk->snd_una = snd_una;
+
+	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
+		if (after64(dfrag->data_seq + dfrag->data_len, snd_una))
+			break;
+
+		list_del(&dfrag->list);
+		put_page(dfrag->page);
+	}
+
+	if (msk->snd_una != snd_una) {
+		msk->snd_una = snd_una;
+		msk->snd_una_tstamp = jiffies;
+	}
 	return snd_una;
 }
 
@@ -98,8 +138,15 @@ static void mptcp_update_subflow_una(struct sock *sk, struct sock *ssk)
 	u64 snd_una = msk->snd_una;
 
 	/* heuristic to avoid doing the expensive data ack update frequently */
-	if (before64(snd_una + tcp_sk(ssk)->snd_cwnd, msk->write_seq))
+	if (before64(snd_una + tcp_sk(ssk)->snd_cwnd, msk->write_seq)) {
+		u64 old_snd_una = snd_una;
+
 		snd_una = mptcp_update_msk_una(sk);
+		if (snd_una == msk->write_seq)
+			mptcp_stop_timer(sk);
+		else if (snd_una != old_snd_una)
+			mptcp_reset_timer(sk, mptcp_timeout(sk, ssk));
+	}
 
 	/* if we msk just switched to this subflow, update subflow's una
 	 * to allow 32 bit ack update to be more accurate
@@ -317,9 +364,15 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		copied += ret;
 	}
 	if (copied) {
+		struct inet_connection_sock *icsk = inet_csk(sk);
+
 		ret = copied;
 		tcp_push(ssk, msg->msg_flags, mss_now, tcp_sk(ssk)->nonagle,
 			 size_goal);
+
+		/* start the timer, if it's not pending */
+		if (!icsk->icsk_timeout)
+			mptcp_reset_timer(sk, mptcp_timeout(sk, ssk));
 	}
 
 	release_sock(ssk);
@@ -670,6 +723,36 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 	return copied;
 }
 
+static void mptcp_retransmit_handler(struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	mptcp_update_msk_una(sk);
+
+	if (msk->snd_una == msk->write_seq)
+		mptcp_stop_timer(sk);
+	else
+		mptcp_reset_timer(sk, msk->sk.icsk_timeout);
+}
+
+static void mptcp_retransmit_timer(struct timer_list *t)
+{
+	struct inet_connection_sock *icsk = from_timer(icsk, t,
+						       icsk_retransmit_timer);
+	struct sock *sk = &icsk->icsk_inet.sk;
+
+	bh_lock_sock(sk);
+	if (!sock_owned_by_user(sk)) {
+		mptcp_retransmit_handler(sk);
+	} else {
+		/* delegate our work to tcp_release_cb() */
+		if (!test_and_set_bit(TCP_WRITE_TIMER_DEFERRED,
+				      &sk->sk_tsq_flags))
+			sock_hold(sk);
+	}
+	bh_unlock_sock(sk);
+}
+
 static int __mptcp_init_sock(struct sock *sk)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -679,6 +762,9 @@ static int __mptcp_init_sock(struct sock *sk)
 	INIT_LIST_HEAD(&msk->conn_list);
 	INIT_LIST_HEAD(&msk->rtx_queue);
 
+	/* re-use the csk retrans timer for MPTCP-level retrans */
+	timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
+
 	return 0;
 }
 
@@ -700,6 +786,8 @@ static void __mptcp_clear_xmit(struct sock *sk)
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct mptcp_data_frag *dtmp, *dfrag;
 
+	sk_stop_timer(sk, &msk->sk.icsk_retransmit_timer);
+
 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
 		list_del(&dfrag->list);
 		put_page(dfrag->page);
@@ -881,6 +969,30 @@ static int mptcp_getsockopt(struct sock *sk, int level, int optname,
 	return 0;
 }
 
+#define MPTCP_DEFERRED_ALL TCPF_WRITE_TIMER_DEFERRED
+
+/* this is very alike tcp_release_cb() but we must handle differently a
+ * different set of events
+ */
+void mptcp_release_cb(struct sock *sk)
+{
+	unsigned long flags, nflags;
+
+	do {
+		flags = sk->sk_tsq_flags;
+		if (!(flags & MPTCP_DEFERRED_ALL))
+			return;
+		nflags = flags & ~MPTCP_DEFERRED_ALL;
+	} while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
+
+	sock_release_ownership(sk);
+
+	if (flags & TCPF_WRITE_TIMER_DEFERRED) {
+		mptcp_retransmit_handler(sk);
+		__sock_put(sk);
+	}
+}
+
 static int mptcp_get_port(struct sock *sk, unsigned short snum)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
net/mptcp/protocol.h +1 -0
index 8b3811b550dd..4d1b4d1e2a3c 100644
@@ -126,6 +126,7 @@ struct mptcp_sock {
 	u64		write_seq;
 	u64		ack_seq;
 	u64		snd_una;
+	unsigned long	snd_una_tstamp;
 	u32		token;
 	u16		dport;
 	struct list_head conn_list;
Signature · 1 line
2.21.0
author
#10 lore raw

RFC PATCH v2 6/8 mptcp: implement memory accounting for mptcp rtx queue

In reply to Paolo Abeni

Charge the data on the rtx queue to the master MPTCP socket, too. Such memory in uncharged when the data is acked/dequeued.

Do not interfer with memory pressure accounting, TCP will deal with that at subflow level.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
rfc v1 -> rfc v2:
- initialize memory_allocated/sysctl_mem fields in struct proto, required to have mem accounting primitives do their work
- deal with mem scheduling failure, bailing out the xmit

---
 net/mptcp/protocol.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
net/mptcp/protocol.c +14 -0
index b77669b0c79a..c9caa0692f41 100644
@@ -121,8 +121,10 @@ static u64 mptcp_update_msk_una(struct sock *sk)
 			break;
 
 		list_del(&dfrag->list);
+		sk_mem_uncharge(sk, dfrag->data_len + dfrag->overhead);
 		put_page(dfrag->page);
 	}
+	sk_mem_reclaim_partial(sk);
 
 	if (msk->snd_una != snd_una) {
 		msk->snd_una = snd_una;
@@ -244,6 +246,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	if (!dfrag_collapsed) {
 		dfrag = mptcp_carve_data_frag(msk, pfrag, offset);
 		offset = dfrag->offset;
+		frag_truesize = dfrag->overhead;
 	}
 	psize = min_t(size_t, pfrag->size - offset, avail_size);
 
@@ -256,6 +259,9 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	if (!psize)
 		return -EINVAL;
 
+	if (!sk_wmem_schedule(sk, psize + dfrag->overhead))
+		return -ENOMEM;
+
 	/* tell the TCP stack to delay the push so that we can safely
 	 * access the skb after the sendpages call
 	 */
@@ -277,6 +283,11 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 		list_add_tail(&dfrag->list, &msk->rtx_queue);
 	}
 
+	/* charge data on mptcp rtx queue to the master socket
+	 * Note: we charge such data both to sk and ssk
+	 */
+	sk->sk_forward_alloc -= frag_truesize;
+
 	collapsed = skb == tcp_write_queue_tail(ssk);
 	BUG_ON(collapsed && !can_collapse);
 	if (collapsed) {
@@ -790,6 +801,7 @@ static void __mptcp_clear_xmit(struct sock *sk)
 
 	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
 		list_del(&dfrag->list);
+		sk_mem_uncharge(sk, dfrag->data_len + dfrag->overhead);
 		put_page(dfrag->page);
 	}
 }
@@ -1097,6 +1109,8 @@ static struct proto mptcp_prot = {
 	.hash		= inet_hash,
 	.unhash		= inet_unhash,
 	.get_port	= mptcp_get_port,
+	.memory_allocated = &tcp_memory_allocated,
+	.sysctl_mem	= sysctl_tcp_mem,
 	.obj_size	= sizeof(struct mptcp_sock),
 	.no_autobind	= 1,
 };
Signature · 1 line
2.21.0
author
#11 lore raw

RFC PATCH v2 7/8 mptcp: rework mptcp_sendmsg_frag to accept optional dfrag

In reply to Paolo Abeni

This will simplify mptcp-level retransmission implementation in the next patch. If dfrag is provided by the caller, skip kernel space memory allocation and use data and metadata provided by the dfrag itself.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 127 +++++++++++++++++++++++++------------------
 1 file changed, 74 insertions(+), 53 deletions(-)
net/mptcp/protocol.c +74 -53
index c9caa0692f41..b628ab326883 100644
@@ -83,7 +83,7 @@ static struct sock *mptcp_subflow_get_ref(const struct mptcp_sock *msk)
 	return NULL;
 }
 
-static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk,
+static inline bool mptcp_skb_can_collapse_to(u64 write_seq,
 					     const struct sk_buff *skb,
 					     const struct mptcp_ext *mpext)
 {
@@ -91,7 +91,7 @@ static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk,
 		return false;
 
 	/* can collapse only if MPTCP level sequence is in order */
-	return mpext && mpext->data_seq + mpext->data_len == msk->write_seq;
+	return mpext && mpext->data_seq + mpext->data_len == write_seq;
 }
 
 static inline bool mptcp_frag_can_collapse_to(const struct mptcp_sock *msk,
@@ -190,26 +190,37 @@ mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
 }
 
 static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
-			      struct msghdr *msg, long *timeo, int *pmss_now,
+			      struct msghdr *msg, struct mptcp_data_frag *dfrag,
+			      long *timeo, int *pmss_now,
 			      int *ps_goal)
 {
 	int mss_now, avail_size, size_goal, offset, ret, frag_truesize = 0;
 	bool dfrag_collapsed, collapsed, can_collapse = false;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct mptcp_ext *mpext = NULL;
-	struct mptcp_data_frag *dfrag;
+	bool retransmission = !!dfrag;
 	struct page_frag *pfrag;
 	struct sk_buff *skb;
+	struct page *page;
+	u64 *write_seq;
 	size_t psize;
+	int *poffset;
 
-	/* use the mptcp page cache so that we can easily move the data
-	 * from one substream to another, but do per subflow memory accounting
-	 */
 	pfrag = sk_page_frag(sk);
-	while (!mptcp_page_frag_refill(ssk, pfrag)) {
-		ret = sk_stream_wait_memory(ssk, timeo);
-		if (ret)
-			return ret;
+	if (!retransmission) {
+		/* use the mptcp page cache so that we can easily move the data
+		 * from one substream to another, but do per subflow memory accounting
+		 */
+		while (!mptcp_page_frag_refill(ssk, pfrag)) {
+			ret = sk_stream_wait_memory(ssk, timeo);
+			if (ret)
+				return ret;
+		}
+		write_seq = &msk->write_seq;
+		page = pfrag->page;
+	} else {
+		write_seq = &dfrag->data_seq;
+		page = dfrag->page;
 	}
 
 	/* compute copy limit */
@@ -228,7 +239,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 		 * is collapsing was allowed: collapsing is effectively
 		 * disabled.
 		 */
-		can_collapse = mptcp_skb_can_collapse_to(msk, skb, mpext);
+		can_collapse = mptcp_skb_can_collapse_to(*write_seq, skb, mpext);
 		if (!can_collapse)
 			TCP_SKB_CB(skb)->eor = 1;
 		else if (size_goal - skb->len > 0)
@@ -237,56 +248,66 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 			can_collapse = false;
 	}
 
-	/* reuse tail pfrag, if possible, or carve a new one from the page
-	 * allocator
-	 */
-	dfrag = mptcp_rtx_tail(sk);
-	offset = pfrag->offset;
-	dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
-	if (!dfrag_collapsed) {
-		dfrag = mptcp_carve_data_frag(msk, pfrag, offset);
+	if (!retransmission) {
+		/* reuse tail pfrag, if possible, or carve a new one from the
+		 * page allocator
+		 */
+		dfrag = mptcp_rtx_tail(sk);
+		offset = pfrag->offset;
+		dfrag_collapsed = mptcp_frag_can_collapse_to(msk, pfrag, dfrag);
+		if (!dfrag_collapsed) {
+			dfrag = mptcp_carve_data_frag(msk, pfrag, offset);
+			offset = dfrag->offset;
+			frag_truesize = dfrag->overhead;
+		}
+		poffset = &pfrag->offset;
+		psize = min_t(size_t, pfrag->size - offset, avail_size);
+
+		/* Copy to page */
+		pr_debug("left=%zu", msg_data_left(msg));
+		psize = copy_page_from_iter(pfrag->page, offset,
+					    min_t(size_t, msg_data_left(msg),
+						  psize),
+					    &msg->msg_iter);
+		pr_debug("left=%zu", msg_data_left(msg));
+		if (!psize)
+			return -EINVAL;
+
+		if (!sk_wmem_schedule(sk, psize + dfrag->overhead))
+			return -ENOMEM;
+	} else {
 		offset = dfrag->offset;
-		frag_truesize = dfrag->overhead;
+		poffset = &dfrag->offset;
+		psize = min_t(size_t, dfrag->data_len, avail_size);
 	}
-	psize = min_t(size_t, pfrag->size - offset, avail_size);
-
-	/* Copy to page */
-	pr_debug("left=%zu", msg_data_left(msg));
-	psize = copy_page_from_iter(pfrag->page, offset,
-				    min_t(size_t, msg_data_left(msg), psize),
-				    &msg->msg_iter);
-	pr_debug("left=%zu", msg_data_left(msg));
-	if (!psize)
-		return -EINVAL;
-
-	if (!sk_wmem_schedule(sk, psize + dfrag->overhead))
-		return -ENOMEM;
 
 	/* tell the TCP stack to delay the push so that we can safely
 	 * access the skb after the sendpages call
 	 */
-	ret = do_tcp_sendpages(ssk, pfrag->page, offset, psize,
+	ret = do_tcp_sendpages(ssk, page, offset, psize,
 			       msg->msg_flags | MSG_SENDPAGE_NOTLAST);
 	if (ret <= 0)
 		return ret;
 
 	frag_truesize += ret;
-	if (unlikely(ret < psize))
-		iov_iter_revert(&msg->msg_iter, psize - ret);
+	if (!retransmission) {
+		if (unlikely(ret < psize))
+			iov_iter_revert(&msg->msg_iter, psize - ret);
 
-	/* send succesful, keep track of sent data for mptcp-level
-	 * retransmission
-	 */
-	dfrag->data_len += ret;
-	if (!dfrag_collapsed) {
-		get_page(dfrag->page);
-		list_add_tail(&dfrag->list, &msk->rtx_queue);
-	}
+		/* send succesful, keep track of sent data for mptcp-level
+		 * retransmission
+		 */
+		dfrag->data_len += ret;
+		if (!dfrag_collapsed) {
+			get_page(dfrag->page);
+			list_add_tail(&dfrag->list, &msk->rtx_queue);
+		}
 
-	/* charge data on mptcp rtx queue to the master socket
-	 * Note: we charge such data both to sk and ssk
-	 */
-	sk->sk_forward_alloc -= frag_truesize;
+		/* charge data on mptcp rtx queue to the master socket
+		 * Note: we charge such data both to sk and ssk
+		 */
+		sk->sk_forward_alloc -= frag_truesize;
+	}
 
 	collapsed = skb == tcp_write_queue_tail(ssk);
 	BUG_ON(collapsed && !can_collapse);
@@ -300,7 +321,7 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 	if (mpext) {
 		memset(mpext, 0, sizeof(*mpext));
-		mpext->data_seq = msk->write_seq;
+		mpext->data_seq = *write_seq;
 		mpext->subflow_seq = subflow_ctx(ssk)->rel_write_seq;
 		mpext->data_len = ret;
 		mpext->checksum = 0xbeef;
@@ -316,8 +337,8 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	 */
 
 out:
-	pfrag->offset += frag_truesize;
-	msk->write_seq += ret;
+	*poffset += frag_truesize;
+	*write_seq += ret;
 	subflow_ctx(ssk)->rel_write_seq += ret;
 
 	return ret;
@@ -367,7 +388,7 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 
 	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
 	while (msg_data_left(msg)) {
-		ret = mptcp_sendmsg_frag(sk, ssk, msg, &timeo, &mss_now,
+		ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now,
 					 &size_goal);
 		if (ret < 0)
 			break;
Signature · 1 line
2.21.0
author
#12 lore raw

RFC PATCH v2 8/8 mptcp: implement and use MPTCP-level retransmission

In reply to Paolo Abeni

On timeout event, schedule a work queue to do the retransmission. Retransmission code resemple closely sendmsg() implementation and re-uses mptcp_sendmsg_frag, providing a dummy msghdr - for flags' sake - and peeking the relevant dfrag from the rtx head.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
rfc v1 -> rfc v2:
- rtx_queue_head is now static inline (fix link issue)
- cancel on mptcp_clear_xmit
- (hopefully) fix msk refcnt

---
 net/mptcp/protocol.c | 73 ++++++++++++++++++++++++++++++++++++++++++--
 net/mptcp/protocol.h | 11 +++++++
 2 files changed, 81 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +70 -3
index b628ab326883..481983addcf5 100644
@@ -761,10 +761,12 @@ static void mptcp_retransmit_handler(struct sock *sk)
 
 	mptcp_update_msk_una(sk);
 
-	if (msk->snd_una == msk->write_seq)
+	if (msk->snd_una == msk->write_seq) {
 		mptcp_stop_timer(sk);
-	else
-		mptcp_reset_timer(sk, msk->sk.icsk_timeout);
+	} else {
+		if (schedule_work(&msk->rtx_work))
+			sock_hold(sk);
+	}
 }
 
 static void mptcp_retransmit_timer(struct timer_list *t)
@@ -785,6 +787,66 @@ static void mptcp_retransmit_timer(struct timer_list *t)
 	bh_unlock_sock(sk);
 }
 
+static void mptcp_retransmit(struct work_struct *work)
+{
+	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, rtx_work);
+	struct sock *ssk, *sk = &msk->sk.icsk_inet.sk;
+	int orig_len, orig_offset, orig_write_seq;
+	int ret, mss_now = 0, size_goal = 0;
+	struct inet_connection_sock *icsk;
+	struct mptcp_data_frag *dfrag;
+	size_t copied = 0;
+	struct msghdr msg;
+	long timeo = 0;
+
+	lock_sock(sk);
+	ssk = mptcp_subflow_get_ref(msk);
+	if (!ssk) {
+		release_sock(sk);
+		goto put_out;
+	}
+
+	lock_sock(ssk);
+
+	mptcp_update_subflow_una(sk, ssk);
+	dfrag = mptcp_rtx_head(sk);
+	if (!dfrag)
+		goto unlock;
+
+	msg.msg_flags = MSG_DONTWAIT;
+	orig_len = dfrag->data_len;
+	orig_offset = dfrag->offset;
+	orig_write_seq = dfrag->data_seq;
+	while (dfrag->data_len > 0) {
+		ret = mptcp_sendmsg_frag(sk, ssk, &msg, dfrag, &timeo, &mss_now,
+				   &size_goal);
+		if (ret < 0)
+			break;
+
+		copied += ret;
+		dfrag->data_len -= ret;
+	}
+	if (copied)
+		tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle,
+			 size_goal);
+
+	dfrag->data_seq = orig_write_seq;
+	dfrag->offset = orig_offset;
+	dfrag->data_len = orig_len;
+
+unlock:
+	release_sock(ssk);
+	release_sock(sk);
+	sock_put(ssk);
+put_out:
+	/* start the timer, if it's not pending */
+	icsk = inet_csk(sk);
+	if (!icsk->icsk_timeout)
+		mptcp_reset_timer(sk, mptcp_timeout(sk, ssk));
+
+	sock_put(sk);
+}
+
 static int __mptcp_init_sock(struct sock *sk)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
@@ -794,6 +856,8 @@ static int __mptcp_init_sock(struct sock *sk)
 	INIT_LIST_HEAD(&msk->conn_list);
 	INIT_LIST_HEAD(&msk->rtx_queue);
 
+	INIT_WORK(&msk->rtx_work, mptcp_retransmit);
+
 	/* re-use the csk retrans timer for MPTCP-level retrans */
 	timer_setup(&msk->sk.icsk_retransmit_timer, mptcp_retransmit_timer, 0);
 
@@ -825,6 +889,9 @@ static void __mptcp_clear_xmit(struct sock *sk)
 		sk_mem_uncharge(sk, dfrag->data_len + dfrag->overhead);
 		put_page(dfrag->page);
 	}
+
+	if (cancel_work_sync(&msk->rtx_work))
+		sock_put(sk);
 }
 
 static void mptcp_close(struct sock *sk, long timeout)
net/mptcp/protocol.h +11 -0
index 4d1b4d1e2a3c..4457c281fc75 100644
@@ -129,6 +129,7 @@ struct mptcp_sock {
 	unsigned long	snd_una_tstamp;
 	u32		token;
 	u16		dport;
+	struct work_struct rtx_work;
 	struct list_head conn_list;
 	struct list_head rtx_queue;
 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
@@ -154,6 +155,16 @@ static inline struct mptcp_data_frag *mptcp_rtx_tail(const struct sock *sk)
 	return list_last_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
 }
 
+static inline struct mptcp_data_frag *mptcp_rtx_head(const struct sock *sk)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	if (list_empty(&msk->rtx_queue))
+		return NULL;
+
+	return list_first_entry(&msk->rtx_queue, struct mptcp_data_frag, list);
+}
+
 struct subflow_request_sock {
 	struct	tcp_request_sock sk;
 	u8	mp_capable : 1,
Signature · 1 line
2.21.0
author
#13 lore raw

Re: [MPTCP] [RFC PATCH v2 8/8] mptcp: implement and use MPTCP-level retransmission

net/mptcp/protocol.c static void mptcp_retransmit_timer(struct timer_list *t) · line 794

Quoted 5 lines · Paolo Abeni
net/mptcp/protocol.c +5 -0
+static void mptcp_retransmit(struct work_struct *work)
+{
+	struct mptcp_sock *msk = container_of(work, struct mptcp_sock, rtx_work);
+	struct sock *ssk, *sk = &msk->sk.icsk_inet.sk;
+	int orig_len, orig_offset, orig_write_seq;
        ^^^^                       ^^^^^^^^^^^^^^

For the records, the above is broken, 'orig_write_seq' must be an u64.

It looks like there is still some other issue, self-tests still fail, sometime.

Regards,

Paolo

#3 lore raw

Re: [MPTCP] [RFC PATCH v2 2/8] mptcp: update per subflow unacked sequence on pkt reception

In reply to Paolo Abeni
Quoted 29 lines · Paolo Abeni

So that we keep per subflow unacked sequence number consistent; since we update only per socket data, no additional locking is required.

Subflow and msk unacked seq are updated at different stages to keep the locking schema simple.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
rfc v1 -> rfc v2:
- initialize subflow snd_una on subflow connect()/accept()
---

net/mptcp/options.c  | 31 +++++++++++++++++++++++++------
net/mptcp/protocol.c |  2 ++
net/mptcp/protocol.h |  1 +
3 files changed, 28 insertions(+), 6 deletions(-)
net/mptcp/options.c +4 -0
index 0e7abcea5bae..e0895834ff82 100644
@@ -524,6 +524,21 @@ bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
 	return false;
 }
 
+static u64 expand_ack(u64 old_ack, u64 cur_ack, bool use_64bit)
+{
+	u32 old_ack32, cur_ack32;
+

net/mptcp/options.c bool mptcp_synack_options(const struct request_sock *req, unsigned int *size, · line 540

Quoted 10 lines
net/mptcp/options.c +10 -0
+	if (use_64bit)
+		return cur_ack;
+
+	old_ack32 = (u32)old_ack;
+	cur_ack32 = (u32)cur_ack;
+	cur_ack = (old_ack & GENMASK_ULL(63, 32)) + cur_ack32;
+	if (unlikely(before(cur_ack32, old_ack32)))
+		return cur_ack + (1LL<<32);
+	return cur_ack;
+}

Expanding the 32-bit sequence numbers correctly and consistently has been a concern of mine for a while, and trying to think through all the issues in the context of ack processing has only made me realize it's even trickier!

With the 32-bit data acks, we can encounter delayed acks on any subflow. Even timely acks seen on one subflow might have large changes in value due to the peer's transmit scheduling choices (acks might only show up on other subflows for a while). The penalty for incorrectly accepting a data ack is significant, since we would discard data from our MPTCP-level retransmission buffer that the peer will know it doesn't have (leading to an unrecoverable stall). Incorrectly discarding a data ack would be recoverable and likely fix itself through additional acks but maybe lead to an unnecessary reinjection by the peer.

I point this out in order to say that expanding 32-bit acks seems like a hard problem, and by decoupling the subflow->snd_una values it becomes even harder :) . I also understand that the possible locking problems from trying to share snd_una across subflows would be difficult too.

Rather than trying to sync subflow->snd_una values for the purpose of expanding them to 64 bits, the subflow could record the 32-bit value and let the MPTCP-level socket do the expansion? Then the subflows only need to track their most recent data ack: the ack value, size (32 or 64), and whether it's been updated since the last access by the MPTCP-level socket. The MPTCP-level socket has more information to know whether to reject an ambiguous 32-bit ack. Latency between the update of subflow->snd_una and the check of that value by the MPTCP socket is still an issue, but could be more manageable.

Side note: I know we have to handle what compliant peers send to us, but maybe we should keep sending only 64-bit acks!

Mat

Quoted 74 lines
net/mptcp/options.c +11 -6
+
 void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 			    struct tcp_options_received *opt_rx)
 {
@@ -537,6 +552,16 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 	if (!mp_opt->dss)
 		return;
 
+	/* we can't wait for recvmsg() to update the ack_seq, otherwise
+	 * monodirectional flows will stuck
+	 */
+	if (mp_opt->use_ack) {
+		struct subflow_context *subflow = subflow_ctx(sk);
+
+		subflow->snd_una = expand_ack(subflow->snd_una,
+					      mp_opt->data_ack, mp_opt->ack64);
+	}
+
 	mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
 	if (!mpext)
 		return;
@@ -553,12 +578,6 @@ void mptcp_incoming_options(struct sock *sk, struct sk_buff *skb,
 		mpext->use_checksum = mp_opt->use_checksum;
 	}
 
-	if (mp_opt->use_ack) {
-		mpext->data_ack = mp_opt->data_ack;
-		mpext->use_ack = 1;
-		mpext->ack64 = mp_opt->ack64;
-	}
-
 	mpext->data_fin = mp_opt->data_fin;
 
 	if (msk)
net/mptcp/protocol.c +2 -0
index 5b7ddbe64e4b..65f7cc4712ef 100644
@@ -674,6 +674,7 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
 		subflow->tcp_sock = new_sock;
+		subflow->snd_una = msk->write_seq;
 		newsk = new_mptcp_sock;
 		subflow->conn = new_mptcp_sock;
 		list_add(&subflow->node, &msk->conn_list);
@@ -809,6 +810,7 @@ void mptcp_finish_connect(struct sock *sk, int mp_capable)
 		subflow->map_seq = ack_seq;
 		subflow->map_subflow_seq = 1;
 		subflow->rel_write_seq = 1;
+		subflow->snd_una = msk->write_seq;
 
 		list_add(&subflow->node, &msk->conn_list);
 		msk->subflow = NULL;
net/mptcp/protocol.h +1 -0
index 9f261ee31917..e37fd4bcd904 100644
@@ -165,6 +165,7 @@ struct subflow_context {
 	u32	token;
 	u32     rel_write_seq;
 	u64     idsn;
+	u64	snd_una;
 	u64	map_seq;
 	u32	map_subflow_seq;
 	u32	ssn_offset;
Signature · 6 lines
2.21.0

_______________________________________________
mptcp mailing list
mptcp(a)lists.01.org
https://lists.01.org/mailman/listinfo/mptcp

--
Mat Martineau
Intel

author
#4 lore raw

Re: [MPTCP] [RFC PATCH v2 2/8] mptcp: update per subflow unacked sequence on pkt reception

Hi,

Thank you for the feedback!

Quoted 5 lines · Mat Martineau

Rather than trying to sync subflow->snd_una values for the purpose of expanding them to 64 bits, the subflow could record the 32-bit value and let the MPTCP-level socket do the expansion? Then the subflows only need to track their most recent data ack: the ack value, size (32 or 64), and whether it's been updated since the last access by the MPTCP-level socket.

I think that tracking the most recent data ack can become almost as tricky, if the peer interleaves 32 and 64 bits ack (IIRC the RFC, it can).

Quoted 4 lines

The MPTCP-level socket has more information to know whether to reject an ambiguous 32-bit ack. Latency between the update of subflow->snd_una and the check of that value by the MPTCP socket is still an issue, but could be more manageable.

Since expanding to 64 bits looks safer with msk->snd_una, and moving the msk->snd_una updates into mptcp_incoming_options() should be quite doable. What about that option?

Yep, we need an additional lock/atomic operation, but <quote> Premature optimization is the root of all evil </quote>

Quoted 2 lines

Side note: I know we have to handle what compliant peers send to us, but maybe we should keep sending only 64-bit acks!

I think we already do that ?!?

Cheers,

Paolo

#5 lore raw

Re: [MPTCP] [RFC PATCH v2 2/8] mptcp: update per subflow unacked sequence on pkt reception

Quoted 11 lines · Paolo Abeni

Hi,

Thank you for the feedback!

Quoted 5 lines · Mat Martineau

Rather than trying to sync subflow->snd_una values for the purpose of expanding them to 64 bits, the subflow could record the 32-bit value and let the MPTCP-level socket do the expansion? Then the subflows only need to track their most recent data ack: the ack value, size (32 or 64), and whether it's been updated since the last access by the MPTCP-level socket.

Quoted 3 lines

I think that tracking the most recent data ack can become almost as tricky, if the peer interleaves 32 and 64 bits ack (IIRC the RFC, it can).

Yes, the sender can interleave.

At the subflow, it's still simple (any new ack replaces the previous one regardless of bit width). Since the ack always moves forward, we don't card about the old value even if it was "better" (64-bit). And the expansion of a 32-bit ack on one subflow doesn't depend on the previous value seen on that subflow, it depends on msk->snd_una.

Quoted 6 lines
Quoted 5 lines

The MPTCP-level socket has more information to know whether to reject an ambiguous 32-bit ack. Latency between the update of subflow->snd_una and the check of that value by the MPTCP socket is still an issue, but could be more manageable.

Quoted 6 lines

Since expanding to 64 bits looks safer with msk->snd_una, and moving the msk->snd_una updates into mptcp_incoming_options() should be quite doable. What about that option?

Yep, we need an additional lock/atomic operation, but <quote> Premature optimization is the root of all evil </quote>

Sounds good to me.

Quoted 5 lines
Quoted 3 lines

Side note: I know we have to handle what compliant peers send to us, but maybe we should keep sending only 64-bit acks!

I think we already do that ?!?

Yes, that is what we currently do. My suggestion is that we continue with only 64-bit acks in the future.

--
Mat Martineau
Intel

#8 lore raw

Re: [MPTCP] [RFC PATCH v2 4/8] mptcp: queue data for mptcp level retransmission

In reply to Paolo Abeni
Quoted 25 lines · Paolo Abeni

keep the send page fragment on an MPTCP level retransmission queue. the queue entries are allocated inside the page frag allocator, acquiring an additional reference to the page for each list entry.

Also switch to a custom page frag refill function, to ensure that the current page fragment can always host an MPTCP rtx queue entry.

The MPTCP rtx queue is flushed at disconnect() and close() time

Note that now we need to call __mptcp_init_sock() regarless of mptcp enable status, as the destructor will try to walk the rtx_queue.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>

---
rfc v1 -> rfc v2:
- rtx_queue_tail is now static inline (fix link issue)
- simpler ALIGN() usage (Florian)
- fix pfrag accounting, adding rtx entry overhead when allocating a new one
- fixed page -> virt conversion
- always init mptcp data structs
- move rtx queue flushing into both disconnect() and close()
---
net/mptcp/protocol.c | 106 +++++++++++++++++++++++++++++++++++++++---- net/mptcp/protocol.h | 20 ++++++++

Quoted 2 lines
2 files changed, 118 insertions(+), 8 deletions(-)

This looks like a good approach to me, including the mptcp_data_frag structure in the page frag works well.

I started to think about possible adjustments to make it easier to introduce zerocopy later, but quickly realized that would be premature.

Thanks,

--
Mat Martineau
Intel