Skip to content

Commit

Permalink
tcp: read multiple skbs in tcp_read_skb()
Browse files Browse the repository at this point in the history
Before we switched to ->read_skb(), ->read_sock() was passed with
desc.count=1, which technically indicates we only read one skb per
->sk_data_ready() call. However, for TCP, this is not true.

TCP at least has sk_rcvlowat which intentionally holds skb's in
receive queue until this watermark is reached. This means when
->sk_data_ready() is invoked there could be multiple skb's in the
queue, therefore we have to read multiple skbs in tcp_read_skb()
instead of one.

Fixes: 965b57b ("net: Introduce a new proto_ops ->read_skb()")
Reported-by: Peilin Ye <[email protected]>
Cc: John Fastabend <[email protected]>
Cc: Jakub Sitnicki <[email protected]>
Cc: Eric Dumazet <[email protected]>
Signed-off-by: Cong Wang <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Paolo Abeni <[email protected]>
  • Loading branch information
Cong Wang authored and Paolo Abeni committed Sep 20, 2022
1 parent 90fdd1c commit db4192a
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions net/ipv4/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1761,19 +1761,28 @@ int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor)
if (sk->sk_state == TCP_LISTEN)
return -ENOTCONN;

skb = tcp_recv_skb(sk, seq, &offset);
if (!skb)
return 0;
while ((skb = tcp_recv_skb(sk, seq, &offset)) != NULL) {
u8 tcp_flags;
int used;

__skb_unlink(skb, &sk->sk_receive_queue);
WARN_ON_ONCE(!skb_set_owner_sk_safe(skb, sk));
copied = recv_actor(sk, skb);
if (copied >= 0) {
seq += copied;
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
__skb_unlink(skb, &sk->sk_receive_queue);
WARN_ON_ONCE(!skb_set_owner_sk_safe(skb, sk));
tcp_flags = TCP_SKB_CB(skb)->tcp_flags;
used = recv_actor(sk, skb);
consume_skb(skb);
if (used < 0) {
if (!copied)
copied = used;
break;
}
seq += used;
copied += used;

if (tcp_flags & TCPHDR_FIN) {
++seq;
break;
}
}
consume_skb(skb);
WRITE_ONCE(tp->copied_seq, seq);

tcp_rcv_space_adjust(sk);
Expand Down

0 comments on commit db4192a

Please sign in to comment.