Skip to content

Commit 05dc72a

Browse files
Eric Dumazetdavem330
authored andcommitted
tcp: factorize logic into tcp_epollin_ready()
Both tcp_data_ready() and tcp_stream_is_readable() share the same logic. Add tcp_epollin_ready() helper to avoid duplication. Signed-off-by: Eric Dumazet <[email protected]> Cc: Arjun Roy <[email protected]> Cc: Wei Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f969dc5 commit 05dc72a

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

include/net/tcp.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,18 @@ static inline bool tcp_rmem_pressure(const struct sock *sk)
14421442
return atomic_read(&sk->sk_rmem_alloc) > threshold;
14431443
}
14441444

1445+
static inline bool tcp_epollin_ready(const struct sock *sk, int target)
1446+
{
1447+
const struct tcp_sock *tp = tcp_sk(sk);
1448+
int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq);
1449+
1450+
if (avail <= 0)
1451+
return false;
1452+
1453+
return (avail >= target) || tcp_rmem_pressure(sk) ||
1454+
(tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss);
1455+
}
1456+
14451457
extern void tcp_openreq_init_rwin(struct request_sock *req,
14461458
const struct sock *sk_listener,
14471459
const struct dst_entry *dst);

net/ipv4/tcp.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -481,19 +481,11 @@ static void tcp_tx_timestamp(struct sock *sk, u16 tsflags)
481481
}
482482
}
483483

484-
static inline bool tcp_stream_is_readable(const struct tcp_sock *tp,
485-
int target, struct sock *sk)
484+
static bool tcp_stream_is_readable(struct sock *sk, int target)
486485
{
487-
int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq);
488-
489-
if (avail > 0) {
490-
if (avail >= target)
491-
return true;
492-
if (tcp_rmem_pressure(sk))
493-
return true;
494-
if (tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss)
495-
return true;
496-
}
486+
if (tcp_epollin_ready(sk, target))
487+
return true;
488+
497489
if (sk->sk_prot->stream_memory_read)
498490
return sk->sk_prot->stream_memory_read(sk);
499491
return false;
@@ -568,7 +560,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
568560
tp->urg_data)
569561
target++;
570562

571-
if (tcp_stream_is_readable(tp, target, sk))
563+
if (tcp_stream_is_readable(sk, target))
572564
mask |= EPOLLIN | EPOLLRDNORM;
573565

574566
if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {

net/ipv4/tcp_input.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4924,15 +4924,8 @@ int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
49244924

49254925
void tcp_data_ready(struct sock *sk)
49264926
{
4927-
const struct tcp_sock *tp = tcp_sk(sk);
4928-
int avail = tp->rcv_nxt - tp->copied_seq;
4929-
4930-
if (avail < sk->sk_rcvlowat && !tcp_rmem_pressure(sk) &&
4931-
!sock_flag(sk, SOCK_DONE) &&
4932-
tcp_receive_window(tp) > inet_csk(sk)->icsk_ack.rcv_mss)
4933-
return;
4934-
4935-
sk->sk_data_ready(sk);
4927+
if (tcp_epollin_ready(sk, sk->sk_rcvlowat))
4928+
sk->sk_data_ready(sk);
49364929
}
49374930

49384931
static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)

0 commit comments

Comments
 (0)