Skip to contentSkip to conversation

mptcp: implement retransmit infrastructure

PATCH Superseded Paolo Abeni 2 participants 17 messages Aug 16, 2019 – Aug 19, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Paolo Abeni

author
#0 lore raw

cover mptcp: implement retransmit infrastructure

As suggested by the subj prefix, this is a very early draft, mainly to discuss design decisions and/or architectural issues. This is only compile-tested so far.

In the current status:
- 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 (patch 4)
- cleanup acked data from retransmission queue and schedule a timeout to update MPTCP unacked sequence number (patch 5)
- do memory accounting for the MPTCP rtx queue (patch 6)

TODO:
- do retransmissions in timer handler, with some heuristic - e.g. last seq_una update ts older than timeout*2 [1]

Open points:
- what about the current seq_una update schema (patch 2-3)? too much complex? too much loose? too much generic?
- is it too early to plug any heuristic inside the code? (as done in patch 3)
- do we really need a timer to update the unacked sequence?
- we can avoid it with an additional atomic operation in mptcp_incoming_options() - either using an atomic type for 'snd_una' or use an additional spin lock to protect it (and possibly other fields)[2]
- still we will likely need a timer to detect that a subflow has become unusable e.g. due to link down event on the peer side. Should we rely on explicit MPTCP notification only (e.g. DEL_ADDR sub-option)?
- retransmission code needs to run in process context, as we will need to acquire msk socket lock and ssh socket lock in order.
- As MPTCP-level retransmission should be considerably less frequent than TCP-level retransmission-timeout, we could use/schedule a workqueue for that.

Any kind of feedback more than welcome!

[1] After a better look at the current code, I see no issues in allocating the sk_buff hdr at retransmission time - ensuring we do that in unblocking wait, with GFP_ATOMIC, e.g. as tls_device_write_space() currently does.

[2] I choosed the current design to avoid such extra atomic operation: it will happen on a contented cache-line, while processing each MPTCP ack.

Paolo Abeni (6):
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

 net/mptcp/options.c  |  31 ++++--
 net/mptcp/protocol.c | 240 +++++++++++++++++++++++++++++++++++++++++--
 net/mptcp/protocol.h |  30 ++++++
 3 files changed, 286 insertions(+), 15 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/6 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.20.1
author
#2 lore raw

PATCH 2/6 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>
---
 net/mptcp/options.c  | 31 +++++++++++++++++++++++++------
 net/mptcp/protocol.h |  1 +
 2 files changed, 26 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.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.20.1
author
#3 lore raw

PATCH 3/6 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>
---
 net/mptcp/protocol.c | 38 ++++++++++++++++++++++++++++++++++++++
 net/mptcp/protocol.h |  1 +
 2 files changed, 39 insertions(+)
net/mptcp/protocol.c +38 -0
index 5b7ddbe64e4b..b7468f770d25 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 (snd_una + tcp_sk(sk)->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;
@@ -804,6 +841,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.20.1
#4 lore raw

Re: [MPTCP] [PRE-RFC 3/6] mptcp: update msk unacked sequence in sendmsg()

net/mptcp/protocol.c static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk, · line 94

Quoted 3 lines · Paolo Abeni
net/mptcp/protocol.c +3 -0
+	/* heuristic to avoid doing the expensive data ack update frequently */
+	if (snd_una + tcp_sk(sk)->snd_cwnd <= msk->write_seq)
+		snd_una = mptcp_update_msk_una(sk);

I think this will need to protect against sequence wraparound.

net/mptcp/protocol.c static inline bool mptcp_skb_can_collapse_to(const struct mptcp_sock *msk, · line 101

Quoted 6 lines
net/mptcp/protocol.c +6 -0
+	/* 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;

... like you did here.

author
#5 lore raw

PATCH 4/6 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 socket destroy() time.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 79 ++++++++++++++++++++++++++++++++++++++++----
 net/mptcp/protocol.h | 19 +++++++++++
 2 files changed, 92 insertions(+), 6 deletions(-)
net/mptcp/protocol.c +73 -6
index b7468f770d25..dcfffb71b545 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,45 @@ 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 offset)
+{
+	struct mptcp_data_frag *dfrag;
+
+	offset = ALIGN(offset, BITS_PER_LONG / 8);
+	dfrag = (struct mptcp_data_frag *)(pfrag->page + offset);
+	dfrag->data_len = 0;
+	dfrag->data_seq = msk->write_seq;
+	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;
+	bool dfrag_collapsed, collapsed, can_collapse = false;
+	int mss_now, avail_size, size_goal, offset, ret;
 	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 +156,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 +186,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 +211,22 @@ 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;
 	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) {
@@ -615,6 +674,7 @@ 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;
 }
@@ -651,6 +711,7 @@ static void mptcp_close(struct sock *sk, long timeout)
 		pr_debug("conn_list->subflow=%p", subflow);
 		sock_release(mptcp_subflow_tcp_socket(subflow));
 	}
+
 	release_sock(sk);
 
 	sock_orphan(sk);
@@ -731,10 +792,16 @@ static struct sock *mptcp_accept(struct sock *sk, int flags, int *err,
 static void mptcp_destroy(struct sock *sk)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
+	struct mptcp_data_frag *dtmp, *dfrag;
 
 	pr_debug("msk=%p, subflow=%p", sk, msk->subflow->sk);
 
 	token_destroy(msk->token);
+
+	list_for_each_entry_safe(dfrag, dtmp, &msk->rtx_queue, list) {
+		list_del(&dfrag->list);
+		put_page(dfrag->page);
+	}
 }
 
 static int mptcp_setsockopt(struct sock *sk, int level, int optname,
net/mptcp/protocol.h +19 -0
index 9220afe0d362..889974a4022c 100644
@@ -108,6 +108,14 @@ struct mptcp_pm_data {
 	u32	token;
 };
 
+struct mptcp_data_frag {
+	struct list_head list;
+	u64 data_seq;
+	int data_len;
+	int offset;
+	struct page *page;
+};
+
 /* MPTCP connection sock */
 struct mptcp_sock {
 	/* inet_connection_sock must be the first member */
@@ -120,6 +128,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 +142,16 @@ static inline struct mptcp_sock *mptcp_sk(const struct sock *sk)
 	return (struct mptcp_sock *)sk;
 }
 
+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.20.1
#6 lore raw

Re: [MPTCP] [PRE-RFC 4/6] mptcp: queue data for mptcp level retransmission

Quoted 9 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 socket destroy() time.

net/mptcp/protocol.c static void mptcp_update_subflow_una(struct sock *sk, struct sock *ssk) · line 118

Quoted 7 lines
net/mptcp/protocol.c +7 -0
+/* 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)))

Where is that 32U coming from?

net/mptcp/protocol.c static void mptcp_update_subflow_una(struct sock *sk, struct sock *ssk) · line 132

Quoted 7 lines
net/mptcp/protocol.c +7 -0
+static inline struct mptcp_data_frag *
+mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
+		      int offset)
+{
+	struct mptcp_data_frag *dfrag;
+
+	offset = ALIGN(offset, BITS_PER_LONG / 8);

sizeof(long)?

The concept of keeping page frags for rexmit looks good to me, thanks Paolo for working on this.

author
#7 lore raw

Re: [MPTCP] [PRE-RFC 4/6] mptcp: queue data for mptcp level retransmission

Quoted 8 lines · Florian Westphal
Quoted 7 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.

Quoted 10 lines

net/mptcp/protocol.c static void mptcp_update_subflow_una(struct sock *sk, struct sock *ssk) · line 118

Quoted 8 lines

The MPTCP rtx queue is flushed at socket destroy() time.

net/mptcp/protocol.c +7 -0
+/* 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)))

Where is that 32U coming from?

From sk_page_frag_refill(). mptcp_page_frag_refill() is almost the same, it just changes the amount of memory reserved.

Another option would have renaming sk_page_frag_refill() to __sk_page_frag_refill() adding an additional argument for the reserved memory, and implement sk_page_frag_refill() and mptcp_page_frag_refill() on top of that.

I can't recall why I explicitly avoided such option ;) (possibly to avoid touching the ntworking core more)

/P

author
#8 lore raw

PATCH 5/6 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 at destroy() time

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 116 ++++++++++++++++++++++++++++++++++++++++++-
 net/mptcp/protocol.h |   1 +
 2 files changed, 115 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +114 -2
index dcfffb71b545..5eba4e65ca93 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 (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 (snd_una + tcp_sk(sk)->snd_cwnd <= msk->write_seq)
+	if (snd_una + tcp_sk(sk)->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
@@ -314,9 +361,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);
@@ -667,6 +720,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);
@@ -676,6 +759,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;
 }
 
@@ -798,6 +884,8 @@ static void mptcp_destroy(struct sock *sk)
 
 	token_destroy(msk->token);
 
+	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);
@@ -858,6 +946,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 889974a4022c..0a6c7c39fc8b 100644
@@ -125,6 +125,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.20.1
#9 lore raw

Re: [MPTCP] [PRE-RFC 5/6] mptcp: use retransmission timer to update msk una

Quoted 3 lines · 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.

Looks fine to me, I think we will need the timer in any case to trigger mptcp level retrans so might as well handle the una update there as well.

author
#10 lore raw

PATCH 6/6 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.

Signed-off-by Paolo Abeni <pabeni(a)redhat.com>
---
 net/mptcp/protocol.c | 18 +++++++++++++++---
 net/mptcp/protocol.h |  1 +
 2 files changed, 16 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +15 -3
index 5eba4e65ca93..2776a408f3e5 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;
@@ -172,14 +174,15 @@ bool mptcp_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
 
 static inline struct mptcp_data_frag *
 mptcp_carve_data_frag(const struct mptcp_sock *msk, struct page_frag *pfrag,
-		      int offset)
+		      int orig_offset)
 {
+	int offset = ALIGN(orig_offset, BITS_PER_LONG / 8);
 	struct mptcp_data_frag *dfrag;
 
-	offset = ALIGN(offset, BITS_PER_LONG / 8);
 	dfrag = (struct mptcp_data_frag *)(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;
 
@@ -190,8 +193,8 @@ 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, offset, ret, frag_truesize = 0;
 	bool dfrag_collapsed, collapsed, can_collapse = false;
-	int mss_now, avail_size, size_goal, offset, ret;
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct mptcp_ext *mpext = NULL;
 	struct mptcp_data_frag *dfrag;
@@ -243,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);
 
@@ -274,6 +278,13 @@ 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
+	 */
+	frag_truesize += ret;
+	sk_wmem_schedule(sk, frag_truesize);
+	sk->sk_forward_alloc -= frag_truesize;
+
 	collapsed = skb == tcp_write_queue_tail(ssk);
 	BUG_ON(collapsed && !can_collapse);
 	if (collapsed) {
@@ -888,6 +899,7 @@ static void mptcp_destroy(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);
 	}
 }
net/mptcp/protocol.h +1 -0
index 0a6c7c39fc8b..3a0d266576f2 100644
@@ -113,6 +113,7 @@ struct mptcp_data_frag {
 	u64 data_seq;
 	int data_len;
 	int offset;
+	int overhead;
 	struct page *page;
 };
 
Signature · 1 line
2.20.1
#11 lore raw

Re: [MPTCP] [PRE-RFC 6/6] mptcp: implement memory accounting for mptcp rtx queue

Quoted 2 lines · 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.

Could you briefly explain why this is needed?
The data is already accounted on the subflows.

Under what circumstances is the data un-charged from a subflow, but still retained on the mptcp retrans list?

author
#12 lore raw

Re: [MPTCP] [PRE-RFC 6/6] mptcp: implement memory accounting for mptcp rtx queue

Quoted 9 lines · Florian Westphal
Quoted 2 lines · 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.

Could you briefly explain why this is needed?
The data is already accounted on the subflows.

Under what circumstances is the data un-charged from a subflow, but still retained on the mptcp retrans list?

With the proposed patches and the current MPTCP code, that happens quite often, even with a single stream and no loss.

The MPTCP-level acks arrives later than TCP-level ones, as we send (MPTCP) ack when the user space really read the data. Most/every fragment stays in the MPTCP RTX queue for a while, after that the corresponding skb has been removed for the TCP transmission queue.

Please let me know if the above is somehow human-parsable ;)

Cheers,

Paolo

#13 lore raw

Re: [MPTCP] [PRE-RFC 6/6] mptcp: implement memory accounting for mptcp rtx queue

Quoted 14 lines · Paolo Abeni
Quoted 9 lines · Florian Westphal
Quoted 2 lines · 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.

Could you briefly explain why this is needed?
The data is already accounted on the subflows.

Under what circumstances is the data un-charged from a subflow, but still retained on the mptcp retrans list?

With the proposed patches and the current MPTCP code, that happens quite often, even with a single stream and no loss.

Quoted 6 lines

The MPTCP-level acks arrives later than TCP-level ones, as we send (MPTCP) ack when the user space really read the data. Most/every fragment stays in the MPTCP RTX queue for a while, after that the corresponding skb has been removed for the TCP transmission queue.

Please let me know if the above is somehow human-parsable ;)

Yes, make sense, thanks for explaining.

#14 lore raw
In reply to Paolo Abeni
Quoted 11 lines · Paolo Abeni

As suggested by the subj prefix, this is a very early draft, mainly to discuss design decisions and/or architectural issues. This is only compile-tested so far.

In the current status:
- 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 (patch 4)
- cleanup acked data from retransmission queue and schedule a timeout to update MPTCP unacked sequence number (patch 5)
- do memory accounting for the MPTCP rtx queue (patch 6)

Can you explain how this rtx queue is expected to be used?

At this time, we only use one tcp connection for sending data, so we can rely on tcp level retransmits.

Is this for future failover case?

Quoted 4 lines

Open points:
- what about the current seq_una update schema (patch 2-3)? too much complex? too much loose? too much generic?
- is it too early to plug any heuristic inside the code? (as done in patch 3)

As long as they don't expose a particular behaviour i think its fine, it can be tweaked/refined later.

Quoted 4 lines

- do we really need a timer to update the unacked sequence?
- we can avoid it with an additional atomic operation in mptcp_incoming_options() - either using an atomic type for 'snd_una' or use an additional spin lock to protect it (and possibly other fields)[2]

I think that using a timer is very strange. I will comment in the patch itself.

Quoted 3 lines

- still we will likely need a timer to detect that a subflow has become unusable e.g. due to link down event on the peer side. Should we rely on explicit MPTCP notification only (e.g. DEL_ADDR sub-option)?

Ah, that answers my question above. I don't think we can rely on on-wire notifications only. Client can e.g. move outside of wifi range for example, so we would only notice that the tcp connection is "blocked" (e.g. outstanding data with no further acks coming in).

Quoted 5 lines

- retransmission code needs to run in process context, as we will need to acquire msk socket lock and ssh socket lock in order.
- As MPTCP-level retransmission should be considerably less frequent than TCP-level retransmission-timeout, we could use/schedule a workqueue for that.

Agree, workqueue seems right for this.

author
#15 lore raw

Hi,

Quoted 13 lines · Florian Westphal
Quoted 11 lines · Paolo Abeni

As suggested by the subj prefix, this is a very early draft, mainly to discuss design decisions and/or architectural issues. This is only compile-tested so far.

In the current status:
- 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 (patch 4)
- cleanup acked data from retransmission queue and schedule a timeout to update MPTCP unacked sequence number (patch 5)
- do memory accounting for the MPTCP rtx queue (patch 6)

Quoted 6 lines

Can you explain how this rtx queue is expected to be used?

At this time, we only use one tcp connection for sending data, so we can rely on tcp level retransmits.

Is this for future failover case?

Yes, with a single subflow, we should not use MPTCP-level retransmission, it should be triggered by scenarios using failover and/or multiple concurrent substream.

Well, to be 110% honest, I think that with a wrong/less than perfect MPTCP-level retransmission heuristic we could end-up retransmitting even with a single flow: since the receiver sends MPTCP-level ack only after that the user-space read the data, a slow receiver user-space could trigger the timeout on the sender (and now that I have wrote it down, it looks a quite bad thing to me :(

[If I'm not completely lost, this is the only question left without an answer right???]

Cheers,

Paolo

#16 lore raw
Quoted 7 lines · Paolo Abeni
Quoted 1 line · Florian Westphal

Is this for future failover case?

Yes, with a single subflow, we should not use MPTCP-level retransmission, it should be triggered by scenarios using failover and/or multiple concurrent substream.

Quoted 6 lines

Well, to be 110% honest, I think that with a wrong/less than perfect MPTCP-level retransmission heuristic we could end-up retransmitting even with a single flow: since the receiver sends MPTCP-level ack only after that the user-space read the data, a slow receiver user-space could trigger the timeout on the sender (and now that I have wrote it down, it looks a quite bad thing to me :(

Its not ideal, but I don't think its something that needs to be addressed *right now*.

Quoted 2 lines

[If I'm not completely lost, this is the only question left without an answer right???]

Yes, thanks.