Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tcp: Add tcp_inq to get available receive bytes on socket

Create a common kernel function to get the number of bytes available
on a TCP socket. This is based on code in INQ getsockopt and we now call
the function for that getsockopt.

Signed-off-by: Tom Herbert <tom@herbertland.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Tom Herbert and committed by
David S. Miller
473bd239 fa9835e5

+25 -14
+24
include/net/tcp.h
··· 1816 1816 skb->truesize = 2; 1817 1817 } 1818 1818 1819 + static inline int tcp_inq(struct sock *sk) 1820 + { 1821 + struct tcp_sock *tp = tcp_sk(sk); 1822 + int answ; 1823 + 1824 + if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 1825 + answ = 0; 1826 + } else if (sock_flag(sk, SOCK_URGINLINE) || 1827 + !tp->urg_data || 1828 + before(tp->urg_seq, tp->copied_seq) || 1829 + !before(tp->urg_seq, tp->rcv_nxt)) { 1830 + 1831 + answ = tp->rcv_nxt - tp->copied_seq; 1832 + 1833 + /* Subtract 1, if FIN was received */ 1834 + if (answ && sock_flag(sk, SOCK_DONE)) 1835 + answ--; 1836 + } else { 1837 + answ = tp->urg_seq - tp->copied_seq; 1838 + } 1839 + 1840 + return answ; 1841 + } 1842 + 1819 1843 #endif /* _TCP_H */
+1 -14
net/ipv4/tcp.c
··· 556 556 return -EINVAL; 557 557 558 558 slow = lock_sock_fast(sk); 559 - if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) 560 - answ = 0; 561 - else if (sock_flag(sk, SOCK_URGINLINE) || 562 - !tp->urg_data || 563 - before(tp->urg_seq, tp->copied_seq) || 564 - !before(tp->urg_seq, tp->rcv_nxt)) { 565 - 566 - answ = tp->rcv_nxt - tp->copied_seq; 567 - 568 - /* Subtract 1, if FIN was received */ 569 - if (answ && sock_flag(sk, SOCK_DONE)) 570 - answ--; 571 - } else 572 - answ = tp->urg_seq - tp->copied_seq; 559 + answ = tcp_inq(sk); 573 560 unlock_sock_fast(sk, slow); 574 561 break; 575 562 case SIOCATMARK: