Skip to contentSkip to conversation

mptcp: sendmsg scheduler skeleton

RFC Superseded Florian Westphal 3 participants 13 messages Oct 14, 2019 – Oct 17, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author
#0 lore raw

cover mptcp: sendmsg scheduler skeleton

This patch series adds minimal support for multiple subflows on the writer side.

Instead of always picking the first ssk (and reliance on tcp to block the mptcp sendmsg in case the subflows write buffer is full), grab a suitable/writeable subflow from the list.

The main point here isn't the 'chose the right subflow' strategy, we will likely want to adapt/change it later -- but the handling of the 'no suitable subflows present' case.

See patch #6 for details on how userspace is blocked/unblocked.

Patch #2 is silly but needed until subflows are expected to close the connection on the mptcp level rather than relying on tcp fin.

Paolo, please also have a closr look at patch 5 since it impacts the retransmission strategy: no more mptcp-level retransmissions if ssk still has skbs in its write queue.

At this time, there is a theoretical problem remaining:

1. userspace calls poll(POLLOUT), one subflow available -> POLLOUT revent
2. retransmission kicks in and fills subflow
3. userspace calls write(), which might block even though poll() said otherwise

This is unlikely because most programs will not mix blocking IO with event driven (select/poll/epoll) models.

But still, its something to keep in mind -- might be enough to prevent retrans from using up the entire write queue for instance.

 include/net/tcp.h    |    1
 net/ipv4/tcp.c       |   12 ++-
 net/mptcp/protocol.c |  154 +++++++++++++++++++++++++++++++++++++++++++++++----
 net/mptcp/protocol.h |    2
 net/mptcp/subflow.c  |   46 +++++++++++++--
 5 files changed, 197 insertions(+), 18 deletions(-)
author
#1 lore raw

RFC PATCH 1/6 mptcp: add and use subflow_write_space

This will be used to unblock tasks waiting for POLLOUT event once mptcp_poll is altered to not wait for each subflow anymore.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/subflow.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/mptcp/subflow.c +11 -0
index b0cda95fbe20..2920fe01b264 100644
@@ -330,6 +330,16 @@ static void subflow_data_ready(struct sock *sk)
 	}
 }
 
+static void subflow_write_space(struct sock *sk)
+{
+	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
+	struct sock *parent = subflow->conn;
+
+	sk_stream_write_space(sk);
+	if (parent)
+		sk_stream_write_space(parent);
+}
+
 int mptcp_subflow_connect(struct sock *sk, struct sockaddr_in *local,
 			  struct sockaddr_in *remote, u8 remote_id)
 {
@@ -455,6 +465,7 @@ static int subflow_ulp_init(struct sock *sk)
 	icsk->icsk_af_ops = &subflow_specific;
 	ctx->tcp_sk_data_ready = sk->sk_data_ready;
 	sk->sk_data_ready = subflow_data_ready;
+	sk->sk_write_space = subflow_write_space;
 out:
 	return err;
 }
Signature · 1 line
2.21.0
author
#2 lore raw

RFC PATCH 2/6 subflow: wake parent mptcp socket on subflow state change

In reply to Florian Westphal

We need to wakeup readers blocking for POLLIN, as no more data will arrive they will never be awoken otherwise.

This is needed at least until proper MPTCP-Level fin/reset signalling gets added.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/subflow.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
net/mptcp/subflow.c +23 -0
index 2920fe01b264..2db2857a1ec6 100644
@@ -438,6 +438,28 @@ static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk,
 	return ctx;
 }
 
+static void __subflow_state_change(struct sock *sk)
+{
+	struct socket_wq *wq;
+
+	rcu_read_lock();
+	wq = rcu_dereference(sk->sk_wq);
+	if (skwq_has_sleeper(wq))
+		wake_up_interruptible_all(&wq->wait);
+	rcu_read_unlock();
+}
+
+static void subflow_state_change(struct sock *sk)
+{
+	struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
+	struct sock *parent = subflow->conn;
+
+	__subflow_state_change(sk);
+
+	if (parent)
+		__subflow_state_change(parent);
+}
+
 static int subflow_ulp_init(struct sock *sk)
 {
 	struct inet_connection_sock *icsk = inet_csk(sk);
@@ -466,6 +488,7 @@ static int subflow_ulp_init(struct sock *sk)
 	ctx->tcp_sk_data_ready = sk->sk_data_ready;
 	sk->sk_data_ready = subflow_data_ready;
 	sk->sk_write_space = subflow_write_space;
+	sk->sk_state_change = subflow_state_change;
 out:
 	return err;
 }
Signature · 1 line
2.21.0
author
#3 lore raw

RFC PATCH 3/6 net: tcp: add __tcp_poll helper

In reply to Florian Westphal

MPTCP will need to be able to call tcp_poll to check if the tcp socket is available for reading/writing, but without adding the task to the waitlist.

This splits tcp_poll into two functions so __tcp_poll can be called by mptcp in followup patch.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 include/net/tcp.h |  1 +
 net/ipv4/tcp.c    | 12 ++++++++----
 2 files changed, 9 insertions(+), 4 deletions(-)
include/net/tcp.h +1 -0
index 7f254765074f..e9dcf6806877 100644
@@ -396,6 +396,7 @@ void tcp_init_sock(struct sock *sk);
 void tcp_init_transfer(struct sock *sk, int bpf_op);
 __poll_t tcp_poll(struct file *file, struct socket *sock,
 		      struct poll_table_struct *wait);
+__poll_t __tcp_poll(struct sock *sk);
 int tcp_getsockopt(struct sock *sk, int level, int optname,
 		   char __user *optval, int __user *optlen);
 int tcp_setsockopt(struct sock *sk, int level, int optname,
net/ipv4/tcp.c +8 -4
index 70402e5882fa..d149fcb2dcc9 100644
@@ -490,15 +490,12 @@ static inline bool tcp_stream_is_readable(const struct tcp_sock *tp,
  *	take care of normal races (between the test and the event) and we don't
  *	go look at any of the socket buffers directly.
  */
-__poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
+__poll_t __tcp_poll(struct sock *sk)
 {
 	__poll_t mask;
-	struct sock *sk = sock->sk;
 	const struct tcp_sock *tp = tcp_sk(sk);
 	int state;
 
-	sock_poll_wait(file, sock, wait);
-
 	state = inet_sk_state_load(sk);
 	if (state == TCP_LISTEN)
 		return inet_csk_listen_poll(sk);
@@ -590,6 +587,13 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
 
 	return mask;
 }
+
+__poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
+{
+	sock_poll_wait(file, sock, wait);
+
+	return __tcp_poll(sock->sk);
+}
 EXPORT_SYMBOL(tcp_poll);
 
 int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
Signature · 1 line
2.21.0
author
#4 lore raw

RFC PATCH 4/6 mptcp: switch mptcp_poll to mptcp socket wait queue

In reply to Florian Westphal

tcp_poll places current task on a wait queue. This can cause us to block forever -- some subflows might never receive any data because they're used as a backup/fallback path.

This makes mptcp_poll wait on the mptcp socket wait queue instead. __tcp_poll is used instead to avoid blocking current task again after parent mptcp socket has been woken from the subflow layer.

The series is incomplete, mptcp_should_wake_parent() should be extended to take packets off the subflow queue and add them to the mptcp socket queue, i.e. move most of the work currently done at recvmsg time into the wakeup function.

This is also needed to not wake userspace in case the subflow tcp data was stale (e.g. because of too early mptcp-level retransmit).

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 9 ++++++++-
 net/mptcp/protocol.h | 1 +
 net/mptcp/subflow.c  | 5 ++---
 3 files changed, 11 insertions(+), 4 deletions(-)
net/mptcp/protocol.c +8 -1
index c4be647a8057..259a2743db91 100644
@@ -523,6 +523,11 @@ static enum mapping_status mptcp_get_mapping(struct sock *ssk)
 	return ret;
 }
 
+bool mptcp_should_wake_parent(struct mptcp_subflow_context *subflow)
+{
+	return true;
+}
+
 static void mptcp_wait_data(struct sock *sk, long *timeo)
 {
 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
@@ -1378,11 +1383,13 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
 		return ret;
 	}
 
+	sock_poll_wait(file, sock, wait);
+
 	mptcp_for_each_subflow(msk, subflow) {
 		struct socket *tcp_sock;
 
 		tcp_sock = mptcp_subflow_tcp_socket(subflow);
-		ret |= tcp_poll(file, tcp_sock, wait);
+		ret |= __tcp_poll(tcp_sock->sk);
 	}
 	release_sock(sk);
 
net/mptcp/protocol.h +1 -0
index 991182578b22..73f79d82f76e 100644
@@ -262,6 +262,7 @@ void mptcp_get_options(const struct sk_buff *skb,
 void mptcp_finish_connect(struct sock *sk, int mp_capable);
 void mptcp_finish_join(struct sock *sk);
 void mptcp_reset_timer(struct sock *sk);
+bool mptcp_should_wake_parent(struct mptcp_subflow_context *ctx);
 
 int mptcp_token_new_request(struct request_sock *req);
 void mptcp_token_destroy_request(u32 token);
net/mptcp/subflow.c +2 -3
index 2db2857a1ec6..c3429f8eca6e 100644
@@ -320,13 +320,12 @@ static void subflow_data_ready(struct sock *sk)
 	subflow->tcp_sk_data_ready(sk);
 
 	if (parent) {
-		pr_debug("parent=%p", parent);
-
 		smp_mb__before_atomic();
 		set_bit(MPTCP_DATA_READY, &mptcp_sk(parent)->flags);
 		smp_mb__after_atomic();
 
-		parent->sk_data_ready(parent);
+		if (mptcp_should_wake_parent(subflow))
+			parent->sk_data_ready(parent);
 	}
 }
 
Signature · 1 line
2.21.0
author
#5 lore raw

RFC PATCH 5/6 mptcp: add and use mptcp_subflow_get_retrans

In reply to Florian Westphal

Instead of having retransmit worker grab first subflow on the list, make it pick the first subflow that has empty tcp write queue.

Rationale is that it makes no sense to attempt to retransmit at mptcp level if there is still unsent data in its write queue.

Backup flows are skipped, unless all other sockets have been removed and a backup flow is the only remaining type.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +30 -1
index 259a2743db91..e003608af237 100644
@@ -781,6 +781,35 @@ static void mptcp_retransmit_timer(struct timer_list *t)
 	sock_put(sk);
 }
 
+/* return first subflow that has no data outstanding at tcp level.
+ *
+ * Never return a backup subflow unless thats the only kind left.
+ */
+static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *backup = NULL;
+	bool use_backup = true;
+
+	sock_owned_by_me((const struct sock *)msk);
+
+	mptcp_for_each_subflow(msk, subflow) {
+		struct sock *ssk = mptcp_subflow_tcp_socket(subflow)->sk;
+
+		if (!subflow->backup)
+			use_backup = false;
+
+		if (tcp_write_queue_empty(ssk)) {
+			if (!subflow->backup)
+				return ssk;
+
+			backup = ssk;
+		}
+	}
+
+	return use_backup ? backup : NULL;
+}
+
 static void mptcp_retransmit(struct work_struct *work)
 {
 	int orig_len, orig_offset, ret, mss_now = 0, size_goal = 0;
@@ -801,7 +830,7 @@ static void mptcp_retransmit(struct work_struct *work)
 	if (!dfrag)
 		goto unlock;
 
-	ssk = mptcp_subflow_get(msk);
+	ssk = mptcp_subflow_get_retrans(msk);
 	if (!ssk)
 		goto reset_unlock;
 
Signature · 1 line
2.21.0
#6 lore raw

[MPTCP] Re: [RFC PATCH 5/6] mptcp: add and use mptcp_subflow_get_retrans

Hi,

Quoted 19 lines · Florian Westphal
net/mptcp/protocol.c +11 -0
index 259a2743db91..e003608af237 100644
@@ -781,6 +781,35 @@ static void mptcp_retransmit_timer(struct timer_list *t)
 	sock_put(sk);
 }
 
+/* return first subflow that has no data outstanding at tcp level.
+ *
+ * Never return a backup subflow unless thats the only kind left.
+ */
+static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *backup = NULL;
+	bool use_backup = true;
+
+	sock_owned_by_me((const struct sock *)msk);

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

Quoted 10 lines
net/mptcp/protocol.c +10 -0
+
+	mptcp_for_each_subflow(msk, subflow) {
+		struct sock *ssk = mptcp_subflow_tcp_socket(subflow)->sk;
+
+		if (!subflow->backup)
+			use_backup = false;
+
+		if (tcp_write_queue_empty(ssk)) {
+			if (!subflow->backup)
+				return ssk;

I think that to be as accurate as possible we could try to check only vs the write queue of the subflow used to send the current dfrag - otherwise we could end-up with spurious/unneded retransmissions on some corner cases.

Additionally we may want to explicitly exclude/penalize such subflow from the selection here.

Storing the subflow ptr into a new/additional dfrag field at transmission time should allow the above.

Cheers,

Paolo

author
#7 lore raw

[MPTCP] Re: [RFC PATCH 5/6] mptcp: add and use mptcp_subflow_get_retrans

Quoted 29 lines · Paolo Abeni

Hi,

Quoted 26 lines · Florian Westphal
net/mptcp/protocol.c +18 -0
index 259a2743db91..e003608af237 100644
@@ -781,6 +781,35 @@ static void mptcp_retransmit_timer(struct timer_list *t)
 	sock_put(sk);
 }
 
+/* return first subflow that has no data outstanding at tcp level.
+ *
+ * Never return a backup subflow unless thats the only kind left.
+ */
+static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *backup = NULL;
+	bool use_backup = true;
+
+	sock_owned_by_me((const struct sock *)msk);
+
+	mptcp_for_each_subflow(msk, subflow) {
+		struct sock *ssk = mptcp_subflow_tcp_socket(subflow)->sk;
+
+		if (!subflow->backup)
+			use_backup = false;
+
Quoted 8 lines

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

Quoted 3 lines
net/mptcp/protocol.c +3 -0
+		if (tcp_write_queue_empty(ssk)) {
+			if (!subflow->backup)
+				return ssk;

I think that to be as accurate as possible we could try to check only vs the write queue of the subflow used to send the current dfrag - otherwise we could end-up with spurious/unneded retransmissions on some corner cases.

I'm not following, sorry.

So I think that what you'd like is something like this:

Quoted 2 lines

Additionally we may want to explicitly exclude/penalize such subflow from the selection here.

static struct sock *
mptcp_subflow_get_retrans(const struct mptcp_sock *msk, struct sock *dfrag_origsk)

{
	[..]

	if (ssk == dfrag_origsk)
		continue; /* never retrans on same subflow */

	if (!subflow->backup)
		use_backup = false;

	if (!best || compare_sk(ssk, best))
		best = ssk;

...

Is that right?

Rationale for the approach done in patch 5 was that MPTCP-Level retransmits are useless in virtually all cases.

The retransmit would only be needed in case we sent data on ssk x but peer went dead after xmit started.

I wanted to make sure we only retransmit on ssks that have no inflight packets, under the assumption that the dfrag has been acked by the next time the retransmit timer kicks in.

What we could do is re-use mptcp_subflow_get_send() from patch 6, adding a 'struct sock *ignore' argument, which would be NULL for normal case and dfrag->orig_sk for retransmit case.

That would make retransmits more aggressive for active/active scenarios when one subflow is 'stuck' and the other has pending packets (but the subflow can accept more data).

Quoted 2 lines

Storing the subflow ptr into a new/additional dfrag field at transmission time should allow the above.

Yes, we can store the ssk that was used for orig transmission, question I have is why :-)

#8 lore raw

[MPTCP] Re: [RFC PATCH 5/6] mptcp: add and use mptcp_subflow_get_retrans

Quoted 57 lines · Florian Westphal
Quoted 27 lines · Paolo Abeni
Quoted 26 lines · Florian Westphal
net/mptcp/protocol.c +18 -0
index 259a2743db91..e003608af237 100644
@@ -781,6 +781,35 @@ static void mptcp_retransmit_timer(struct timer_list *t)
 	sock_put(sk);
 }
 
+/* return first subflow that has no data outstanding at tcp level.
+ *
+ * Never return a backup subflow unless thats the only kind left.
+ */
+static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *backup = NULL;
+	bool use_backup = true;
+
+	sock_owned_by_me((const struct sock *)msk);
+
+	mptcp_for_each_subflow(msk, subflow) {
+		struct sock *ssk = mptcp_subflow_tcp_socket(subflow)->sk;
+
+		if (!subflow->backup)
+			use_backup = false;
+
Quoted 8 lines

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

Quoted 3 lines
net/mptcp/protocol.c +3 -0
+		if (tcp_write_queue_empty(ssk)) {
+			if (!subflow->backup)
+				return ssk;

I think that to be as accurate as possible we could try to check only vs the write queue of the subflow used to send the current dfrag - otherwise we could end-up with spurious/unneded retransmissions on some corner cases.

I'm not following, sorry.

So I think that what you'd like is something like this:

Quoted 2 lines

Additionally we may want to explicitly exclude/penalize such subflow from the selection here.

static struct sock *
mptcp_subflow_get_retrans(const struct mptcp_sock *msk, struct sock *dfrag_origsk)

{
	[..]

	if (ssk == dfrag_origsk)
		continue; /* never retrans on same subflow */

	if (!subflow->backup)
		use_backup = false;

	if (!best || compare_sk(ssk, best))
		best = ssk;
Quoted 3 lines

...

Is that right?

Yes, exactly!

Quoted 5 lines

Rationale for the approach done in patch 5 was that MPTCP-Level retransmits are useless in virtually all cases.

The retransmit would only be needed in case we sent data on ssk x but peer went dead after xmit started.

uhmm... I think that it will suffice if peer goes dead after we create/enqueue the skb (mptcp_sendmsg() completes). Could/should happen quite consistently e.g. when the client switches from wifi to 4G, and the flow is almost unidirectional from the server.

Quoted 7 lines

I wanted to make sure we only retransmit on ssks that have no inflight packets, under the assumption that the dfrag has been acked by the next time the retransmit timer kicks in.

What we could do is re-use mptcp_subflow_get_send() from patch 6, adding a 'struct sock *ignore' argument, which would be NULL for normal case and dfrag->orig_sk for retransmit case.

I was in the act of suggesting the above on patch 6 :) I think it would simplify the code having a single algorithm to pick-up the next subflow.

Apropos: another thing I would eventually consider for patch 6 is [try to] reuse the subflow we used last, if it has a non empty window. If we can't, we do the full lookup. That should allow the peer to reconstruct more easily the MPTCP level data sequence, with possibly less switches from one subflow to another.

Quoted 3 lines

That would make retransmits more aggressive for active/active scenarios when one subflow is 'stuck' and the other has pending packets (but the subflow can accept more data).

I think we can mitigate/optimize out the unneeded retransmissions some way. e.g. no need to retransmit if the relevant ssn increased since last check/enqueue time.

In some scenarios retransmitting the data on a different subflow could possibly inprove the tput/latency even if the first transmission has not been lost - e.g. if the subflow we used first is experiencing congestion.

Quoted 5 lines
Quoted 2 lines

Storing the subflow ptr into a new/additional dfrag field at transmission time should allow the above.

Yes, we can store the ssk that was used for orig transmission, question I have is why :-)

I think having relevant subflow handy will help with the above heuristics.

I can agree we can do this kind of tuning/optimization/heuristic[/over- design???] later, I'm wondering if we should consider some room for them now?

Thanks,

Paolo

#9 lore raw

[MPTCP] Re: [RFC PATCH 5/6] mptcp: add and use mptcp_subflow_get_retrans

Quoted 67 lines · Paolo Abeni
Quoted 57 lines · Florian Westphal
Quoted 27 lines · Paolo Abeni
Quoted 26 lines · Florian Westphal
net/mptcp/protocol.c +18 -0
index 259a2743db91..e003608af237 100644
@@ -781,6 +781,35 @@ static void mptcp_retransmit_timer(struct timer_list *t)
 	sock_put(sk);
 }
 
+/* return first subflow that has no data outstanding at tcp level.
+ *
+ * Never return a backup subflow unless thats the only kind left.
+ */
+static struct sock *mptcp_subflow_get_retrans(const struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	struct sock *backup = NULL;
+	bool use_backup = true;
+
+	sock_owned_by_me((const struct sock *)msk);
+
+	mptcp_for_each_subflow(msk, subflow) {
+		struct sock *ssk = mptcp_subflow_tcp_socket(subflow)->sk;
+
+		if (!subflow->backup)
+			use_backup = false;
+
Quoted 8 lines

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

Quoted 3 lines
net/mptcp/protocol.c +3 -0
+		if (tcp_write_queue_empty(ssk)) {
+			if (!subflow->backup)
+				return ssk;

I think that to be as accurate as possible we could try to check only vs the write queue of the subflow used to send the current dfrag - otherwise we could end-up with spurious/unneded retransmissions on some corner cases.

I'm not following, sorry.

So I think that what you'd like is something like this:

Quoted 2 lines

Additionally we may want to explicitly exclude/penalize such subflow from the selection here.

static struct sock *
mptcp_subflow_get_retrans(const struct mptcp_sock *msk, struct sock *dfrag_origsk)

{
	[..]

	if (ssk == dfrag_origsk)
		continue; /* never retrans on same subflow */

	if (!subflow->backup)
		use_backup = false;

	if (!best || compare_sk(ssk, best))
		best = ssk;
Quoted 3 lines

...

Is that right?

Yes, exactly!

Quoted 3 lines

Rationale for the approach done in patch 5 was that MPTCP-Level retransmits are useless in virtually all cases.

Quoted 11 lines
Quoted 2 lines

The retransmit would only be needed in case we sent data on ssk x but peer went dead after xmit started.

uhmm... I think that it will suffice if peer goes dead after we create/enqueue the skb (mptcp_sendmsg() completes). Could/should happen quite consistently e.g. when the client switches from wifi to 4G, and the flow is almost unidirectional from the server.

Quoted 3 lines

I wanted to make sure we only retransmit on ssks that have no inflight packets, under the assumption that the dfrag has been acked by the next time the retransmit timer kicks in.

That might make it a good idea to not push more new data in to the ssks once MPTCP-level retransmission kicks in? We don't want the subflow buffers filled up with new data when the MPTCP socket is trying to retransmit.

Quoted 8 lines
Quoted 4 lines

What we could do is re-use mptcp_subflow_get_send() from patch 6, adding a 'struct sock *ignore' argument, which would be NULL for normal case and dfrag->orig_sk for retransmit case.

I was in the act of suggesting the above on patch 6 :) I think it would simplify the code having a single algorithm to pick-up the next subflow.

Simplifying sounds good to me! However, I'm not sure it's good to completely ignore the dfrag->orig_sk socket, it could be the only subflow (or only non-backup) available. Our MPTCP-level reassembly code doesn't currently have an out-of-order queue, so it can flush incoming data received on a working subflow when the actual network loss was on a different subflow.

Quoted 7 lines

Apropos: another thing I would eventually consider for patch 6 is [try to] reuse the subflow we used last, if it has a non empty window. If we can't, we do the full lookup. That should allow the peer to reconstruct more easily the MPTCP level data sequence, with possibly less switches from one subflow to another.

In the LPC presentation and paper we proposed keeping the scheduler extremely simple, with the focus being on the server use case with a limited number of subflows (maybe even just two, allowing for a primary and backup). The idea for the first patch set was to focus on MPTCP for failover rather than aggregating multiple subflows to maximize throughput.

That could mean simply transmitting on the first available non-backup subflow, leaving others on the list idle.

Quoted 19 lines
Quoted 3 lines

That would make retransmits more aggressive for active/active scenarios when one subflow is 'stuck' and the other has pending packets (but the subflow can accept more data).

I think we can mitigate/optimize out the unneeded retransmissions some way. e.g. no need to retransmit if the relevant ssn increased since last check/enqueue time.

In some scenarios retransmitting the data on a different subflow could possibly inprove the tput/latency even if the first transmission has not been lost - e.g. if the subflow we used first is experiencing congestion.

Quoted 5 lines
Quoted 2 lines

Storing the subflow ptr into a new/additional dfrag field at transmission time should allow the above.

Yes, we can store the ssk that was used for orig transmission, question I have is why :-)

Quoted 7 lines

I think having relevant subflow handy will help with the above heuristics.

I can agree we can do this kind of tuning/optimization/heuristic[/over- design???] later, I'm wondering if we should consider some room for them now?

As this email thread shows, there are a lot of potential inputs for making a scheduling decision and there isn't a one-size-fits all scheduling algorithm. The multipath-tcp.org kernel handles this with different scheduler modules, and we do have configurable schedulers later in the roadmap.

My suggestion is to keep it very, very simple for now!

--
Mat Martineau
Intel

#10 lore raw

[MPTCP] Re: [RFC PATCH 5/6] mptcp: add and use mptcp_subflow_get_retrans

Quoted 7 lines · Mat Martineau

As this email thread shows, there are a lot of potential inputs for making a scheduling decision and there isn't a one-size-fits all scheduling algorithm. The multipath-tcp.org kernel handles this with different scheduler modules, and we do have configurable schedulers later in the roadmap.

My suggestion is to keep it very, very simple for now!

Agreed.

I really should fight harder my tendency to overdesign ;)

Let's go the this path and ev. we can adjust later, if needed.

Thanks,

Paolo

author
#11 lore raw

[MPTCP] Re: [RFC PATCH 5/6] mptcp: add and use mptcp_subflow_get_retrans

In reply to Mat Martineau
Quoted 7 lines · Mat Martineau
Quoted 4 lines · Paolo Abeni
Quoted 3 lines · Florian Westphal

Rationale for the approach done in patch 5 was that MPTCP-Level retransmits are useless in virtually all cases.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

The retransmit would only be needed in case we sent data on ssk x but peer went dead after xmit started.

uhmm... I think that it will suffice if peer goes dead after we create/enqueue the skb (mptcp_sendmsg() completes). Could/should happen quite consistently e.g. when the client switches from wifi to 4G, and the flow is almost unidirectional from the server.

Yes, but how often do you expect clients to bounce back and forth between two paths?

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines

I wanted to make sure we only retransmit on ssks that have no inflight packets, under the assumption that the dfrag has been acked by the next time the retransmit timer kicks in.

That might make it a good idea to not push more new data in to the ssks once MPTCP-level retransmission kicks in? We don't want the subflow buffers filled up with new data when the MPTCP socket is trying to retransmit.

What about this?

net/mptcp/options.c +2 -0
@@ -567,6 +567,8 @@ static void update_una(struct mptcp_sock *msk,
 		if (old_snd_una == snd_una) {
 			mptcp_reset_timer((struct sock *)msk);
 			break;
+		} else if (test_bit(MPTCP_RETRANS, &msk->flags)) {
+			sk_stream_write_space((struct sock *)msk);
 		}
 	}
 }
net/mptcp/protocol.c +35 -1
@@ -210,7 +142,7 @@ static void dfrag_clear(struct sock *sk, struct mptcp_data_frag *dfrag)
 	put_page(dfrag->page);
 }
 
-static void mptcp_clean_una(struct sock *sk)
+static bool mptcp_clean_una(struct sock *sk)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct mptcp_data_frag *dtmp, *dfrag;
@@ -223,6 +155,8 @@ static void mptcp_clean_una(struct sock *sk)
 		dfrag_clear(sk, dfrag);
 	}
 	sk_mem_reclaim_partial(sk);
+
+	return list_empty(&msk->rtx_queue);
 }
 
 /* ensure we get enough memory for the frag hdr, beyond some minimal amount of
@@ -413,6 +347,80 @@ static int mptcp_sendmsg_frag(struct sock *sk, struct sock *ssk,
 	return ret;
 }
 
+/* returns < 0 if a should be preferred to b */
+static int sock_compare(const struct sock *a, const struct sock *b)
+{
+	int cmp = inet_csk(a)->icsk_ca_state - inet_csk(b)->icsk_ca_state;
+
+	if (cmp)
+		return cmp;
+
+	cmp = tcp_packets_in_flight(tcp_sk(a)) - tcp_packets_in_flight(tcp_sk(b));
+	if (cmp)
+		return cmp;
+
+	return 0;
+}
+
+static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	bool non_backup_present = false;
+	struct sock *best, *backup;
+
+	sock_owned_by_me((const struct sock *)msk);
+
+	backup = NULL;
+	best = NULL;
+
+	/* don't send new data with ongoing MPTCP retransmit */
+	if (unlikely(test_bit(MPTCP_RETRANS, &msk->flags))) {
+		if (!mptcp_clean_una((void *)msk))
+			return NULL;
+		clear_bit(MPTCP_RETRANS, &msk->flags);
+	}
 
 [ remaining unchanged ]
 

static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) {
int mss_now = 0, size_goal = 0, ret = 0;
@@ -687,6 +695,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) {
+ clear_bit(MPTCP_RETRANS, &msk->flags);
mptcp_stop_timer(sk);
} else {
if (schedule_work(&msk->rtx_work))
@@ -783,9 +792,11 @@ static void mptcp_retransmit(struct work_struct *work)
dfrag->data_len -= ret;
dfrag->offset += ret;
}
- if (copied)
+ if (copied) {
+ set_bit(MPTCP_RETRANS, &msk->flags);
tcp_push(ssk, msg.msg_flags, mss_now, tcp_sk(ssk)->nonagle,
size_goal);
+ }

dfrag->data_seq = orig_write_seq;
dfrag->offset = orig_offset;
@@ -1164,6 +1175,12 @@ static bool mptcp_memory_free(const struct sock *sk, int wake)
{
struct mptcp_sock *msk = mptcp_sk(sk);

+ if (unlikely(test_bit(MPTCP_RETRANS, &msk->flags))) {
+ /* can't clear una here, sk is const */
+ if (atomic64_read(&msk->snd_una) == READ_ONCE(msk->write_seq))
+ return true;
+ }
+
return test_bit(MPTCP_SEND_SPACE, &msk->flags);
}

@@ -1348,10 +1365,15 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,
const struct mptcp_sock *msk;
struct sock *sk = sock->sk;
struct socket *ssock;
+ bool retrans = false;
__poll_t ret = 0;

msk = mptcp_sk(sk);
lock_sock(sk);
+
+ if (test_bit(MPTCP_RETRANS, &msk->flags) && !mptcp_clean_una(sk))
+ retrans = true;
+
ssock = __mptcp_fallback_get_ref(msk);
if (ssock) {
release_sock(sk);
@@ -1364,9 +1386,14 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,

mptcp_for_each_subflow(msk, subflow) {
struct socket *tcp_sock;
+ int r;

tcp_sock = mptcp_subflow_tcp_socket(subflow);
- ret |= __tcp_poll(tcp_sock->sk);
+ r = __tcp_poll(tcp_sock->sk);
+ if (retrans && (r & (EPOLLOUT|EPOLLWRNORM)))
+ r &= ~(EPOLLOUT|EPOLLWRNORM);
+ ret |= r;
+
}
release_sock(sk);

net/mptcp/protocol.h +1 -0
index db3007ca50a7..768fad073b07 100644
@@ -77,6 +77,7 @@
 /* MPTCP socket flags */
 #define MPTCP_DATA_READY	BIT(0)
 #define MPTCP_SEND_SPACE	BIT(1)
+#define MPTCP_RETRANS		BIT(2)
 
 
 

------

Main problem is that when MPTCP-level retransmission kicks in, this blocks writes until snd_una has caught up with mptcp write_seq again.

The use of the MPTCP_RETRANS flag prevents this from occuring for normal write operations while retrans timer is pending but hasn't done any actual resends yet.

Might be good enough for now. If not, we will need to track more state, e.g. the last/highest retransmitted mptcp-sequence number. It would allow us to signal a wakeup from mptcp_memory_free() earlier, when the last 'retransmitted' sequence was acked, rather than the all outstanding data.

Quoted 5 lines

In the LPC presentation and paper we proposed keeping the scheduler extremely simple, with the focus being on the server use case with a limited number of subflows (maybe even just two, allowing for a primary and backup). The idea for the first patch set was to focus on MPTCP for failover rather than aggregating multiple subflows to maximize throughput.

I agree.

Quoted 2 lines

That could mean simply transmitting on the first available non-backup subflow, leaving others on the list idle.

Yes, I can alter the patch accordingly.

Quoted 5 lines

As this email thread shows, there are a lot of potential inputs for making a scheduling decision and there isn't a one-size-fits all scheduling algorithm. The multipath-tcp.org kernel handles this with different scheduler modules, and we do have configurable schedulers later in the roadmap.

I know. It wasn't my intention do make a scheduler, I wanted to get the infra ready to block at mptcp level rather than relying on tcp blocking on first ssk.

author
#12 lore raw

RFC PATCH 6/6 mptcp: pick best-suited subflow for transmit

In reply to Florian Westphal

Because we only have one subflow active at the moment, the major change isn't how the ssk is chosen but what happens when no subflow is available (e.g. when only active subflow has its wmem maxed out).

If this happens we need to either signal -EAGAIN to userspace (nonblock case), or we need to wait until a subflow becomes available.

This is done by blocking the current thread via sk_stream_wait_memory() and then make the subflow sk_write_space() unblock the parent mptcp socket.

We can't acquire the mptcp socket lock from the subflow callbacks, but we can use the mptcp->flags to for signalling.

Before this change, we always picked the first ssk on the list and then had do_tcp_sendpages wait until more space became available.

Signed-off-by Florian Westphal <fw(a)strlen.de>
---
 net/mptcp/protocol.c | 114 ++++++++++++++++++++++++++++++++++++++++---
 net/mptcp/protocol.h |   1 +
 net/mptcp/subflow.c  |   7 ++-
 3 files changed, 113 insertions(+), 9 deletions(-)
net/mptcp/protocol.c +106 -8
index e003608af237..89c49efa8fdd 100644
@@ -92,6 +92,83 @@ static struct sock *mptcp_subflow_get(const struct mptcp_sock *msk)
 	return NULL;
 }
 
+/* returns < 0 if a should be preferred to b */
+static int sock_compare(const struct sock *a, const struct sock *b)
+{
+	int cmp = inet_csk(a)->icsk_ca_state - inet_csk(b)->icsk_ca_state;
+
+	if (cmp)
+		return cmp;
+
+	cmp = tcp_is_cwnd_limited(a) - tcp_is_cwnd_limited(b);
+	if (cmp)
+		return cmp;
+
+	cmp = skb_queue_len(&a->sk_write_queue) -
+	      skb_queue_len(&b->sk_write_queue);
+	if (cmp)
+		return cmp;
+
+	cmp = tcp_packets_in_flight(tcp_sk(a)) - tcp_packets_in_flight(tcp_sk(b));
+	if (cmp)
+		return cmp;
+
+	return 0;
+}
+
+static struct sock *mptcp_subflow_get_send(const struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	bool non_backup_present = false;
+	struct sock *best, *backup;
+
+	sock_owned_by_me((const struct sock *)msk);
+
+	backup = NULL;
+	best = NULL;
+
+	mptcp_for_each_subflow(msk, subflow) {
+		struct sock *ssk = mptcp_subflow_tcp_socket(subflow)->sk;
+
+		if (!sk_stream_is_writeable(ssk)) {
+			struct socket *sock = ssk->sk_socket;
+
+			if (sock)
+				set_bit(SOCK_NOSPACE, &sock->flags);
+
+			if (!subflow->backup)
+				non_backup_present = true;
+
+			continue;
+		}
+
+		if (subflow->backup) {
+			if (!backup)
+				backup = ssk;
+
+			continue;
+		}
+
+		if (tcp_sk(ssk)->inet_conn.icsk_ca_state == TCP_CA_Open) {
+			/* Not backup and empty write queue -> use it */
+			if (tcp_write_queue_empty(ssk))
+				return ssk;
+		}
+
+		if (!best || sock_compare(ssk, best) < 0)
+			best = ssk;
+	}
+
+	if (best)
+		return best;
+
+	/* prefer to wait unless no non-backup sockets exist. */
+	if (!non_backup_present)
+		return backup;
+
+	return NULL;
+}
+
 static inline bool mptcp_skb_can_collapse_to(u64 write_seq,
 					     const struct sk_buff *skb,
 					     const struct mptcp_ext *mpext)
@@ -343,23 +420,36 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		return ret;
 	}
 
-	ssk = mptcp_subflow_get(msk);
-	if (!ssk) {
-		release_sock(sk);
-		return -ENOTCONN;
-	}
-
-	if (!msg_data_left(msg)) {
+	if (unlikely(!msg_data_left(msg))) {
+		ssk = mptcp_subflow_get(msk);
 		pr_debug("empty send");
 		ret = sock_sendmsg(ssk->sk_socket, msg);
 		goto out;
 	}
 
+	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
+
+	smp_mb__before_atomic();
+	clear_bit(MPTCP_SEND_SPACE, &msk->flags);
+	smp_mb__after_atomic();
+
+	ssk = mptcp_subflow_get_send(msk);
+	while (!ssk) {
+		ret = sk_stream_wait_memory(sk, &timeo);
+		if (ret)
+			goto out;
+
+		ssk = mptcp_subflow_get_send(msk);
+		if (list_empty(&msk->conn_list)) {
+			ret = -ENOTCONN;
+			goto out;
+		}
+	}
+
 	pr_debug("conn_list->subflow=%p", ssk);
 
 	lock_sock(ssk);
 	mptcp_clean_una(sk);
-	timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
 	while (msg_data_left(msg)) {
 		ret = mptcp_sendmsg_frag(sk, ssk, msg, NULL, &timeo, &mss_now,
 					 &size_goal);
@@ -1220,6 +1310,13 @@ bool mptcp_sk_is_subflow(const struct sock *sk)
 	return subflow->mp_join == 1;
 }
 
+static bool mptcp_memory_free(const struct sock *sk, int wake)
+{
+	struct mptcp_sock *msk = mptcp_sk(sk);
+
+	return test_bit(MPTCP_SEND_SPACE, &msk->flags);
+}
+
 static struct proto mptcp_prot = {
 	.name		= "MPTCP",
 	.owner		= THIS_MODULE,
@@ -1240,6 +1337,7 @@ static struct proto mptcp_prot = {
 	.sockets_allocated	= &mptcp_sockets_allocated,
 	.memory_allocated	= &tcp_memory_allocated,
 	.memory_pressure	= &tcp_memory_pressure,
+	.stream_memory_free	= mptcp_memory_free,
 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_wmem),
 	.sysctl_mem	= sysctl_tcp_mem,
 	.obj_size	= sizeof(struct mptcp_sock),
net/mptcp/protocol.h +1 -0
index 73f79d82f76e..c9a772773a42 100644
@@ -76,6 +76,7 @@
 
 /* MPTCP socket flags */
 #define MPTCP_DATA_READY	BIT(0)
+#define MPTCP_SEND_SPACE	BIT(1)
 
 static inline __be32 mptcp_option(u8 subopt, u8 len, u8 nib, u8 field)
 {
net/mptcp/subflow.c +6 -1
index c3429f8eca6e..a7832f86d301 100644
@@ -335,8 +335,13 @@ static void subflow_write_space(struct sock *sk)
 	struct sock *parent = subflow->conn;
 
 	sk_stream_write_space(sk);
-	if (parent)
+	if (parent) {
+		smp_mb__before_atomic();
+		set_bit(MPTCP_SEND_SPACE, &mptcp_sk(parent)->flags);
+		smp_mb__after_atomic();
+
 		sk_stream_write_space(parent);
+	}
 }
 
 int mptcp_subflow_connect(struct sock *sk, struct sockaddr_in *local,
Signature · 1 line
2.21.0