mptcp: update mptcp ack sequence from work queue
Subsystems (3)
Florian Westphal
v2 cover mptcp: update mptcp ack sequence from work queue
Hi.
This is v2 of the attempt to allow mptcp to update its ack-sequence number even if userspace doesn't call recv() for some time.
I tried to address all comments from previous feedback.
tcp_read_sock() is not used anymore, skbs are moved off the subflow receive queue and re-linked to the mptcp receive queue.
The inner loop in mptcp_recvmsg is gone. skbs are only taken from the mptcp receive queue. In case that mptcp receive queue is empty, it will pull skbs off the subflow receive queue.
Thus, the previous patch to make tcp_read_sock actors eat skbs isn't included in this series anymore.
Patch 4 is new: after removing the inner loop from recvmsg we can also move the actor callback to subflow.c.
Patch 5 is also new, but not strictly related to this series. Patch 6 isn't strictly required, but I think its a good idea anyway to do this so that we don't need to rely on bulk traffic to generate acks/enough window updates.
Thanks,
Florian
Florian Westphal
PATCH v2 1/6 mptcp: add and use mptcp_data_ready helper
allows us to schedule the work queue to drain the ssk receive queue in a followup patch.
--- net/mptcp/protocol.c | 8 ++++++++ net/mptcp/protocol.h | 1 + net/mptcp/subflow.c | 14 ++++---------- 3 files changed, 13 insertions(+), 10 deletions(-)
net/mptcp/protocol.c +8 -0
@@ -156,6 +156,14 @@ void mptcp_subflow_eof(struct sock *sk)sock_hold (sk); } +void mptcp_data_ready (struct sock *sk) +{ +struct mptcp_sock *msk =mptcp_sk (sk); + +set_bit (MPTCP_DATA_READY , &msk->flags); + sk->sk_data_ready (sk); +} +static void mptcp_stop_timer (struct sock *sk) {struct inet_connection_sock *icsk =inet_csk (sk);
net/mptcp/protocol.h +1 -0
@@ -317,6 +317,7 @@ void mptcp_finish_connect(struct sock *sk);void mptcp_subflow_eof (struct sock *sk);bool mptcp_finish_join (struct sock *sk);void mptcp_data_acked (struct sock *sk); +void mptcp_data_ready (struct sock *sk);int mptcp_token_new_request (struct request_sock *req);void mptcp_token_destroy_request (u32 token);
net/mptcp/subflow.c +4 -10
@@ -723,11 +723,8 @@ static void subflow_data_ready(struct sock *sk)return ; } -if (mptcp_subflow_data_available (sk)) { -set_bit (MPTCP_DATA_READY , &mptcp_sk (parent)->flags); - - parent->sk_data_ready (parent); - } +if (mptcp_subflow_data_available (sk)) +mptcp_data_ready (parent); }static void subflow_write_space (struct sock *sk)
@@ -919,11 +916,8 @@ static void subflow_state_change(struct sock *sk)* a fin packet carrying a DSS can be unnoticed if we don't trigger * the data available machinery here. */ -if (parent && subflow->mp_capable &&mptcp_subflow_data_available (sk)) { -set_bit (MPTCP_DATA_READY , &mptcp_sk (parent)->flags); - - parent->sk_data_ready (parent); - } +if (parent && subflow->mp_capable &&mptcp_subflow_data_available (sk)) +mptcp_data_ready (parent);if (parent && !(parent->sk_shutdown &RCV_SHUTDOWN ) && !subflow->rx_eof &&subflow_is_done (sk)) {
Signature · 1 line
2.24.1
Florian Westphal
PATCH v2 2/6 mptcp: update mptcp ack sequence from work queue
In reply to Florian WestphalThis adds a new worker flag to indicate when the work queue should drain a subflow socket.
skbs on the subflow socket are then placed on the mptcp socket receive queue (which was not used so far).
This allows us to announce the correct mptcp ack sequence in the tcp acks that we send back to the peer, even when the application does not call recv() on the mptcp socket for some time.
We still wake userspace tasks waiting for POLLIN -- we do not depend on the work queue to have run. If the mptcp level receive queue is empty, it can be filled from in-sequence subflow sockets at recv time.
v2: remove inner recv loop in mptcp_recvmsg.
don't use tcp_read_sock, just move skbs directly.
--- net/mptcp/protocol.c | 213 ++++++++++++++++++++++++++++++++----------- net/mptcp/protocol.h | 1 + 2 files changed, 159 insertions(+), 55 deletions(-)
net/mptcp/protocol.c +158 -55
@@ -34,6 +34,12 @@ struct mptcp6_sock {static struct percpu_counter mptcp_sockets_allocated; +struct mptcp_skb_cb { +u32 offset; +}; + +#define MPTCP_SKB_CB (__skb) ((struct mptcp_skb_cb *)&((__skb)->cb[0])) +/* If msk has an initial subflow socket, and the MP_CAPABLE handshake has not * completed yet or has failed, return the subflow socket. * Otherwise return NULL.
@@ -162,6 +168,13 @@ void mptcp_data_ready(struct sock *sk)set_bit (MPTCP_DATA_READY , &msk->flags); sk->sk_data_ready (sk); + +if (test_and_set_bit (MPTCP_WORK_DATA_READY , &msk->flags)) +return ; + +sock_hold (sk); +if (!schedule_work (&msk->rtx_work)) +sock_put (sk); }static void mptcp_stop_timer (struct sock *sk)
@@ -641,18 +654,136 @@ static void mptcp_wait_data(struct sock *sk, long *timeo)remove_wait_queue (sk_sleep (sk), &wait); } +static int __mptcp_recvmsg_mskq (struct mptcp_sock *msk, +struct msghdr *msg, +size_t len) +{ +struct sock *sk = (struct sock *)msk; +struct sk_buff *skb; +int copied =0 ; + +while ((skb =skb_peek (&sk->sk_receive_queue)) !=NULL ) { +u32 offset =MPTCP_SKB_CB (skb)->offset; +u32 data_len = skb->len - offset; +u32 count =min_t (size_t, len - copied, data_len); +int err; + + err =skb_copy_datagram_msg (skb, offset, msg, count); +if (unlikely (err <0 )) { +if (!copied) +return err; +break ; + } + + copied += count; + +if (count < data_len) { +MPTCP_SKB_CB (skb)->offset += count; +break ; + } + +__skb_unlink (skb, &sk->sk_receive_queue); +__kfree_skb (skb); + +if (copied >= len) +break ; + } + +return copied; +} + +static void __mptcp_move_skb (struct mptcp_sock *msk,struct sock *ssk, +struct sk_buff *skb, +unsigned int offset,size_t copy_len) +{ +struct sock *sk = (struct sock *)msk; + +__skb_unlink (skb, &ssk->sk_receive_queue); +skb_orphan (skb); +__skb_queue_tail (&sk->sk_receive_queue, skb); + + msk->ack_seq += copy_len; +MPTCP_SKB_CB (skb)->offset = offset; +} + +static bool __mptcp_move_skbs (struct mptcp_sock *msk) +{ +struct sock *sk = &msk->sk.icsk_inet.sk; +unsigned int moved =0 ; +bool done = false; + +do { +struct mptcp_subflow_context *subflow; +struct sock *ssk; +bool more_data_avail; +struct tcp_sock *tp; + + ssk =mptcp_subflow_recv_lookup (msk); +if (!ssk) +break ; + + subflow =mptcp_subflow_ctx (ssk); + +lock_sock (ssk); + + tp =tcp_sk (ssk); +do { +u32 map_remaining, offset; +u32 seq = tp->copied_seq; +struct sk_buff *skb; +bool fin; + +/* try to move as much data as available */ + map_remaining = subflow->map_data_len - +mptcp_subflow_get_map_offset (subflow); + + skb =skb_peek (&ssk->sk_receive_queue); +if (!skb) +break ; + + offset = seq -TCP_SKB_CB (skb)->seq; + fin =TCP_SKB_CB (skb)->tcp_flags &TCPHDR_FIN ; +if (fin) { + done = true; + seq++; + } + +if (offset < skb->len) { +size_t len = skb->len - offset; + +if (tp->urg_data) + done = true; + +__mptcp_move_skb (msk, ssk, skb, offset, len); + seq += len; + moved += len; + +if (WARN_ON_ONCE (map_remaining < len)) +break ; + }else { +WARN_ON_ONCE (!fin); +sk_eat_skb (sk, skb); + done = true; + } + +WRITE_ONCE (tp->copied_seq, seq); + more_data_avail =mptcp_subflow_data_available (ssk); + }while (more_data_avail); + +release_sock (ssk); + }while (!done); + +return moved >0 ; +} + +static int mptcp_recvmsg (struct sock *sk,struct msghdr *msg,size_t len,int nonblock,int flags,int *addr_len) {struct mptcp_sock *msk =mptcp_sk (sk); -struct mptcp_subflow_context *subflow; -bool more_data_avail = false; -struct mptcp_read_arg arg; -read_descriptor_t desc; +bool more_data_avail = true;bool wait_data = false;struct socket *ssock; -struct tcp_sock *tp; -bool done = false;struct sock *ssk;int copied =0 ;int target;
@@ -671,66 +802,30 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,return copied; } - arg.msg = msg; - desc.arg.data = &arg; - desc.error =0 ; - timeo =sock_rcvtimeo (sk, nonblock); len =min_t (size_t, len,INT_MAX ); target =sock_rcvlowat (sk, flags &MSG_WAITALL , len);__mptcp_flush_join_list (msk); -while (!done) { -u32 map_remaining; +while (more_data_avail) {int bytes_read; - ssk =mptcp_subflow_recv_lookup (msk); -pr_debug ("msk=%p ssk=%p" , msk, ssk); -if (!ssk) - goto wait_for_data; - - subflow =mptcp_subflow_ctx (ssk); - tp =tcp_sk (ssk); + bytes_read =__mptcp_recvmsg_mskq (msk, msg, len - copied); +if (unlikely (bytes_read <0 )) { +if (!copied) + copied = bytes_read; + goto out_err; + } -lock_sock (ssk); -do { -/* try to read as much data as available */ - map_remaining = subflow->map_data_len - -mptcp_subflow_get_map_offset (subflow); - desc.count =min_t (size_t, len - copied, map_remaining); -pr_debug ("reading %zu bytes, copied %d" , desc.count, - copied); - bytes_read =tcp_read_sock (ssk, &desc, - mptcp_read_actor); -if (bytes_read <0 ) { -if (!copied) - copied = bytes_read; - done = true; - goto next; - } + copied += bytes_read; -pr_debug ("msk ack_seq=%llx -> %llx" , msk->ack_seq, - msk->ack_seq + bytes_read); - msk->ack_seq += bytes_read; - copied += bytes_read; -if (copied >= len) { - done = true; - goto next; - } -if (tp->urg_data && tp->urg_seq == tp->copied_seq) { -pr_err ("Urgent data present, cannot proceed" ); - done = true; - goto next; - } -next: - more_data_avail =mptcp_subflow_data_available (ssk); - }while (more_data_avail && !done); -release_sock (ssk); -continue ; +if (skb_queue_empty (&sk->sk_receive_queue)) { +if (!__mptcp_move_skbs (msk)) + more_data_avail = false; -wait_for_data: - more_data_avail = false; +continue ; + }/* only the master socket status is relevant here. The exit * conditions mirror closely tcp_recvmsg()
@@ -791,6 +886,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,set_bit (MPTCP_DATA_READY , &msk->flags); } +out_err:release_sock (sk);return copied; }
@@ -921,6 +1017,9 @@ static void mptcp_worker(struct work_struct *work)lock_sock (sk);mptcp_clean_una (sk); +if (test_and_clear_bit (MPTCP_WORK_DATA_READY , &msk->flags)) +__mptcp_move_skbs (msk); +if (test_and_clear_bit (MPTCP_WORK_EOF , &msk->flags))mptcp_check_for_eof (msk);
@@ -1085,6 +1184,8 @@ static void mptcp_close(struct sock *sk, long timeout)mptcp_cancel_work (sk); +__skb_queue_purge (&sk->sk_receive_queue); +sk_common_release (sk); }
@@ -1755,6 +1856,8 @@ void mptcp_proto_init(void)panic ("Failed to register MPTCP proto.\n" );inet_register_protosw (&mptcp_protosw); + + BUILD_BUG_ON(sizeof(struct mptcp_skb_cb) > sizeof_field(struct sk_buff, cb)); }#if IS_ENABLED (CONFIG_MPTCP_IPV6 )
net/mptcp/protocol.h +1 -0
@@ -82,6 +82,7 @@#define MPTCP_SEND_SPACE BIT(1)#define MPTCP_WORK_RTX BIT(2)#define MPTCP_WORK_EOF BIT(3) +#define MPTCP_WORK_DATA_READY BIT(4)static inline __be32 mptcp_option (u8 subopt,u8 len,u8 nib,u8 field) {
Signature · 1 line
2.24.1
Florian Westphal
PATCH v2 3/6 mptcp: add rmem queue accounting
In reply to Florian Westphalv2: adjust rcvbuf outside of loop.
--- net/mptcp/protocol.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +15 -1
@@ -172,6 +172,10 @@ void mptcp_data_ready(struct sock *sk)if (test_and_set_bit (MPTCP_WORK_DATA_READY , &msk->flags))return ; +/* don't schedule if mptcp sk is (still) over limit */ +if (atomic_read (&sk->sk_rmem_alloc) >READ_ONCE (sk->sk_rcvbuf)) +return ; +sock_hold (sk);if (!schedule_work (&msk->rtx_work))sock_put (sk);
@@ -699,7 +703,7 @@ static void __mptcp_move_skb(struct mptcp_sock *msk, struct sock *ssk,struct sock *sk = (struct sock *)msk;__skb_unlink (skb, &ssk->sk_receive_queue); -skb_orphan (skb); +skb_set_owner_r (skb, sk);__skb_queue_tail (&sk->sk_receive_queue, skb); msk->ack_seq += copy_len;
@@ -717,6 +721,7 @@ static bool __mptcp_move_skbs(struct mptcp_sock *msk)struct sock *ssk;bool more_data_avail;struct tcp_sock *tp; +int rcvbuf; ssk =mptcp_subflow_recv_lookup (msk);if (!ssk)
@@ -726,6 +731,10 @@ static bool __mptcp_move_skbs(struct mptcp_sock *msk)lock_sock (ssk); + rcvbuf =max (ssk->sk_rcvbuf, sk->sk_rcvbuf); +if (rcvbuf > sk->sk_rcvbuf) + sk->sk_rcvbuf = rcvbuf; + tp =tcp_sk (ssk); do { u32 map_remaining, offset;
@@ -741,6 +750,11 @@ static bool __mptcp_move_skbs(struct mptcp_sock *msk)if (!skb)break ; +if (!sk_rmem_schedule (sk, skb, skb->truesize)) { + done = true; +break ; + } + offset = seq -TCP_SKB_CB (skb)->seq; fin =TCP_SKB_CB (skb)->tcp_flags &TCPHDR_FIN ;if (fin) {
Signature · 1 line
2.24.1
Florian Westphal
PATCH v2 4/6 mptcp: remove mptcp_read_actor
In reply to Florian WestphalOnly used to discard stale data from the subflow, so move it where needed.
--- net/mptcp/protocol.c | 27 --------------------------- net/mptcp/protocol.h | 7 ------- net/mptcp/subflow.c | 18 +++++++++++++----- 3 files changed, 13 insertions(+), 39 deletions(-)
net/mptcp/protocol.c +0 -27
@@ -616,33 +616,6 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)return ret; } -int mptcp_read_actor (read_descriptor_t *desc,struct sk_buff *skb, -unsigned int offset,size_t len) -{ -struct mptcp_read_arg *arg = desc->arg.data; -size_t copy_len; - - copy_len =min (desc->count, len); - -if (likely (arg->msg)) { -int err; - - err =skb_copy_datagram_msg (skb, offset, arg->msg, copy_len); -if (err) { -pr_debug ("error path" ); - desc->error = err; -return err; - } - }else { -pr_debug ("Flushing skb payload" ); - } - - desc->count -= copy_len; - -pr_debug ("consumed %zu bytes, %zu left" , copy_len, desc->count); -return copy_len; -} -static void mptcp_wait_data (struct sock *sk,long *timeo) {DEFINE_WAIT_FUNC (wait, woken_wake_function);
net/mptcp/protocol.h +0 -7
@@ -304,13 +304,6 @@ void mptcp_proto_init(void);int mptcp_proto_v6_init (void );#endif -struct mptcp_read_arg { -struct msghdr *msg; -}; - -int mptcp_read_actor (read_descriptor_t *desc,struct sk_buff *skb, -unsigned int offset,size_t len); -void mptcp_get_options (const struct sk_buff *skb,struct tcp_options_received *opt_rx);
net/mptcp/subflow.c +13 -5
@@ -577,6 +577,18 @@ static enum mapping_status get_mapping_status(struct sock *ssk)return MAPPING_OK ; } +static int subflow_read_actor (read_descriptor_t *desc, +struct sk_buff *skb, +unsigned int offset,size_t len) +{ +size_t copy_len =min (desc->count, len); + + desc->count -= copy_len; + +pr_debug ("flushed %zu bytes, %zu left" , copy_len, desc->count); +return copy_len; +} +static bool subflow_check_data_avail (struct sock *ssk) {struct mptcp_subflow_context *subflow =mptcp_subflow_ctx (ssk);
@@ -651,16 +663,12 @@ static bool subflow_check_data_avail(struct sock *ssk)pr_debug ("discarding %zu bytes, current map len=%d" , delta, map_remaining);if (delta) { -struct mptcp_read_arg arg = { - .msg =NULL , - };read_descriptor_t desc = { .count = delta, - .arg.data = &arg, };int ret; - ret =tcp_read_sock (ssk, &desc, mptcp_read_actor); + ret =tcp_read_sock (ssk, &desc, subflow_read_actor);if (ret <0 ) { ssk->sk_err = -ret; goto fatal;
Signature · 1 line
2.24.1
Florian Westphal
PATCH v2 5/6 mptcp: protocol: re-check dsn before reading from subflow
In reply to Florian Westphalmptcp_subflow_data_available() is commonly called via ssk->sk_data_ready(), in this case the mptcp socket lock cannot be acquired.
Therefore, while we can safely discard subflow data that was already received up to msk->ack_seq, we cannot be sure that 'subflow->data_avail' will still be valid at the time userspace wants to read the data -- a previous read on a different subflow might have carried this data already.
In that (unlikely) event, msk->ack_seq will have been updated and will be ahead of the subflow dsn.
We can check for this condition and skip/resync to the expected sequence number.
--- net/mptcp/protocol.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
net/mptcp/protocol.c +18 -2
@@ -205,8 +205,24 @@ static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)sock_owned_by_me (sk);mptcp_for_each_subflow (msk, subflow) { -if (subflow->data_avail) -return mptcp_subflow_tcp_sock (subflow); +if (subflow->data_avail) { +struct sock *ssk =mptcp_subflow_tcp_sock (subflow); +u64 dsn =mptcp_subflow_get_mapped_dsn (subflow); + +/* revalidate data sequence number. +* +* mptcp_subflow_data_available() is usually called +* without msk lock. Its unlikely (but possible) +* that msk->ack_seq has been advanced since the last +* call found in-sequence data. +*/ +if (likely (dsn == msk->ack_seq)) +return ssk; + + subflow->data_avail =0 ; +if (mptcp_subflow_data_available (ssk)) +return ssk; + } }return NULL ;
Signature · 1 line
2.24.1
Paolo Abeni
[MPTCP] Re: [PATCH v2 5/6] mptcp: protocol: re-check dsn before reading from subflow
Quoted 4 lines · Florian Westphal
mptcp_subflow_data_available()is commonly called viassk->sk_data_ready(), in this case the mptcp socket lock cannot be acquired.
Quoted 8 lines
Therefore, while we can safely discard subflow data that was already received up to
msk->ack_seq, we cannot be sure that 'subflow->data_avail' will still be valid at the time userspace wants to read the data -- a previous read on a different subflow might have carried this data already.In that (unlikely) event,
msk->ack_seqwill have been updated and will be ahead of the subflow dsn.
Does the above mean that we can get wake-up events and found no actual data in the msk socket? (read will block)
Something alike:
- user space is blocked on mptcp_recvmsg()/mptcp_wait_data() -> wait_data == true
- subflow 1 delivers new in order data, wakes the user space which start reading
- before mptcp_recvmsg updates the msk->ack_seq, subflow 2 delivers the same DSS as subflow 1, consider that to be in order and sets the MPTCP_DATA_READY bit
- mptcp_recvmsg fills the user-space buffer and completes. Since wait_data == true, the MPTCP_DATA_READY bit is left untouched.
- next mptcp_poll will return EPOLLIN even if no data is really available.
Should we move the 'wait_data = false' initialization inside some inner loop?
Thanks,
Paolo
Florian Westphal
[MPTCP] Re: [PATCH v2 5/6] mptcp: protocol: re-check dsn before reading from subflow
Quoted 30 lines · Paolo Abeni
Quoted 4 lines · Florian Westphal
mptcp_subflow_data_available()is commonly called viassk->sk_data_ready(), in this case the mptcp socket lock cannot be acquired.Quoted 8 lines
Therefore, while we can safely discard subflow data that was already received up to
msk->ack_seq, we cannot be sure that 'subflow->data_avail' will still be valid at the time userspace wants to read the data -- a previous read on a different subflow might have carried this data already.In that (unlikely) event,
msk->ack_seqwill have been updated and will be ahead of the subflow dsn.Does the above mean that we can get wake-up events and found no actual data in the msk socket? (read will block)
Something alike:
- user space is blocked on mptcp_recvmsg()/mptcp_wait_data() ->wait_data== true
- subflow 1 delivers new in order data, wakes the user space which start reading
- beforemptcp_recvmsgupdates themsk->ack_seq, subflow 2 delivers the same DSS as subflow 1, consider that to be in order and sets theMPTCP_DATA_READYbit
-mptcp_recvmsgfills the user-space buffer and completes. Sincewait_data== true, theMPTCP_DATA_READYbit is left untouched.
- nextmptcp_pollwill return EPOLLIN even if no data is really available.
Quoted 2 lines
Should we move the '
wait_data= false' initialization inside some inner loop?
Hmm, what about this (delta, would squash this to "mptcp: update mptcp ack sequence from work queue".
In your scenario above, we would now hit the 'msk->rx_queue is empty' branch, DATA_READY gets cleared:
net/mptcp/protocol.c +11 -19
@@ -787,10 +787,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len) { struct mptcp_sock *msk = mptcp_sk(sk); -bool more_data_avail = true; -bool wait_data = false;struct socket *ssock; -struct sock *ssk;int copied =0 ;int target;long timeo;
@@ -814,7 +811,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, target =sock_rcvlowat (sk, flags &MSG_WAITALL , len);__mptcp_flush_join_list (msk); -while (more_data_avail) { +while (len > (size_t )copied) {int bytes_read; bytes_read =__mptcp_recvmsg_mskq (msk, msg, len - copied);
@@ -826,12 +823,9 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, copied += bytes_read; -if (skb_queue_empty (&sk->sk_receive_queue)) { -if (!__mptcp_move_skbs (msk)) - more_data_avail = false; - +if (skb_queue_empty (&sk->sk_receive_queue) && +__mptcp_move_skbs (msk))continue ; - }/* only the master socket status is relevant here. The exit * conditions mirror closely tcp_recvmsg()
@@ -872,26 +866,24 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, }pr_debug ("block timeout %ld" , timeo); - wait_data = true;mptcp_wait_data (sk, &timeo);if (unlikely (__mptcp_tcp_fallback (msk))) goto fallback; } -if (more_data_avail) { -if (!test_bit (MPTCP_DATA_READY , &msk->flags)) -set_bit (MPTCP_DATA_READY , &msk->flags); - }else if (!wait_data) { +if (skb_queue_empty (&sk->sk_receive_queue)) { +/* entire backlog drained, clear DATA_READY. */ clear_bit (MPTCP_DATA_READY , &msk->flags); -/* .. race-breaker: ssk might get new data after last -* data_available() returns false. +/* .. race-breaker: ssk might have gotten new data +* after last __mptcp_move_skbs() returned false. */ - ssk =mptcp_subflow_recv_lookup (msk); -if (unlikely (ssk)) +if (unlikely (__mptcp_move_skbs (msk)))set_bit (MPTCP_DATA_READY , &msk->flags); + }else if (unlikely (!test_bit (MPTCP_DATA_READY , &msk->flags))) { +/* data to read but mptcp_wait_data() cleared DATA_READY */ +set_bit (MPTCP_DATA_READY , &msk->flags); } - out_err:release_sock (sk);return copied;
Paolo Abeni
[MPTCP] Re: [PATCH v2 5/6] mptcp: protocol: re-check dsn before reading from subflow
Quoted 104 lines · Florian Westphal
Quoted 30 lines · Paolo Abeni
Quoted 4 lines · Florian Westphal
mptcp_subflow_data_available()is commonly called viassk->sk_data_ready(), in this case the mptcp socket lock cannot be acquired.Quoted 8 lines
Therefore, while we can safely discard subflow data that was already received up to
msk->ack_seq, we cannot be sure that 'subflow->data_avail' will still be valid at the time userspace wants to read the data -- a previous read on a different subflow might have carried this data already.In that (unlikely) event,
msk->ack_seqwill have been updated and will be ahead of the subflow dsn.Does the above mean that we can get wake-up events and found no actual data in the msk socket? (read will block)
Something alike:
- user space is blocked on mptcp_recvmsg()/mptcp_wait_data() ->wait_data== true
- subflow 1 delivers new in order data, wakes the user space which start reading
- beforemptcp_recvmsgupdates themsk->ack_seq, subflow 2 delivers the same DSS as subflow 1, consider that to be in order and sets theMPTCP_DATA_READYbit
-mptcp_recvmsgfills the user-space buffer and completes. Sincewait_data== true, theMPTCP_DATA_READYbit is left untouched.
- nextmptcp_pollwill return EPOLLIN even if no data is really available.Quoted 2 lines
Should we move the '
wait_data= false' initialization inside some inner loop?Hmm, what about this (delta, would squash this to "mptcp: update mptcp ack sequence from work queue".
In your scenario above, we would now hit the '
msk->rx_queueis empty' branch,DATA_READYgets cleared:net/mptcp/protocol.c +7
-18@@ -787,10 +787,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len) { struct mptcp_sock *msk = mptcp_sk(sk); -bool more_data_avail = true; -bool wait_data = false;struct socket *ssock; -struct sock *ssk;int copied =0 ;int target;long timeo;@@ -814,7 +811,7 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, target =sock_rcvlowat (sk, flags &MSG_WAITALL , len);__mptcp_flush_join_list (msk); -while (more_data_avail) { +while (len > (size_t )copied) {int bytes_read; bytes_read =__mptcp_recvmsg_mskq (msk, msg, len - copied);@@ -826,12 +823,9 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, copied += bytes_read; -if (skb_queue_empty (&sk->sk_receive_queue)) { -if (!__mptcp_move_skbs (msk)) - more_data_avail = false; - +if (skb_queue_empty (&sk->sk_receive_queue) && +__mptcp_move_skbs (msk))continue ; - }/* only the master socket status is relevant here. The exit * conditions mirror closely tcp_recvmsg() @@ -872,26 +866,24 @@ static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, }pr_debug ("block timeout %ld" , timeo); - wait_data = true;mptcp_wait_data (sk, &timeo);if (unlikely (__mptcp_tcp_fallback (msk))) goto fallback; } -if (more_data_avail) { -if (!test_bit (MPTCP_DATA_READY , &msk->flags)) -set_bit (MPTCP_DATA_READY , &msk->flags); - }else if (!wait_data) { +if (skb_queue_empty (&sk->sk_receive_queue)) { +/* entire backlog drained, clear DATA_READY. */ clear_bit (MPTCP_DATA_READY , &msk->flags); -/* .. race-breaker: ssk might get new data after last -* data_available() returns false. +/* .. race-breaker: ssk might have gotten new data +* after last __mptcp_move_skbs() returned false. */ - ssk =mptcp_subflow_recv_lookup (msk); -if (unlikely (ssk))
net/mptcp/protocol.c static int mptcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, · line 889
Quoted 10 lines
net/mptcp/protocol.c +4
-1+if (unlikely (__mptcp_move_skbs (msk)))set_bit (MPTCP_DATA_READY , &msk->flags); + }else if (unlikely (!test_bit (MPTCP_DATA_READY , &msk->flags))) { +/* data to read but mptcp_wait_data() cleared DATA_READY */ +set_bit (MPTCP_DATA_READY , &msk->flags); } - out_err:release_sock (sk);return copied;
LGTM, thanks for takling the concerns of my twisted mind ;)
Cheers,
Paolo
Florian Westphal
PATCH v2 6/6 mptcp: send acks when moving skbs to mptcp receive queue
In reply to Florian Westphal--- include/net/tcp.h | 1 + net/ipv4/tcp.c | 2 +- net/mptcp/protocol.c | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-)
include/net/tcp.h +1 -0
@@ -650,6 +650,7 @@ static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)/* tcp.c */ void tcp_get_info (struct sock *,struct tcp_info *); +void tcp_cleanup_rbuf (struct sock *sk,int copied);/* Read 'sendfile()'-style from a TCP socket */ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
net/ipv4/tcp.c +1 -1
@@ -1517,7 +1517,7 @@ static int tcp_peek_sndq(struct sock *sk, struct msghdr *msg, int len)* calculation of whether or not we must ACK for the sake of * a window update. */ -static void tcp_cleanup_rbuf (struct sock *sk,int copied) +void tcp_cleanup_rbuf (struct sock *sk,int copied) {struct tcp_sock *tp =tcp_sk (sk);bool time_to_ack = false;
net/mptcp/protocol.c +3 -0
@@ -773,6 +773,9 @@ static bool __mptcp_move_skbs(struct mptcp_sock *msk) more_data_avail =mptcp_subflow_data_available (ssk); }while (more_data_avail); +if (moved >0 ) +tcp_cleanup_rbuf (ssk, moved); +release_sock (ssk); }while (!done);
Signature · 1 line
2.24.1