Skip to content

Commit 0757e28

Browse files
JasonXingSasha Levin
authored andcommitted
tcp: count CLOSE-WAIT sockets for TCP_MIB_CURRESTAB
[ Upstream commit a46d0ea ] According to RFC 1213, we should also take CLOSE-WAIT sockets into consideration: "tcpCurrEstab OBJECT-TYPE ... The number of TCP connections for which the current state is either ESTABLISHED or CLOSE- WAIT." After this, CurrEstab counter will display the total number of ESTABLISHED and CLOSE-WAIT sockets. The logic of counting When we increment the counter? a) if we change the state to ESTABLISHED. b) if we change the state from SYN-RECEIVED to CLOSE-WAIT. When we decrement the counter? a) if the socket leaves ESTABLISHED and will never go into CLOSE-WAIT, say, on the client side, changing from ESTABLISHED to FIN-WAIT-1. b) if the socket leaves CLOSE-WAIT, say, on the server side, changing from CLOSE-WAIT to LAST-ACK. Please note: there are two chances that old state of socket can be changed to CLOSE-WAIT in tcp_fin(). One is SYN-RECV, the other is ESTABLISHED. So we have to take care of the former case. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Jason Xing <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent f0bf701 commit 0757e28

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

net/ipv4/tcp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,10 @@ void tcp_set_state(struct sock *sk, int state)
27582758
if (oldstate != TCP_ESTABLISHED)
27592759
TCP_INC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
27602760
break;
2761+
case TCP_CLOSE_WAIT:
2762+
if (oldstate == TCP_SYN_RECV)
2763+
TCP_INC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
2764+
break;
27612765

27622766
case TCP_CLOSE:
27632767
if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
@@ -2769,7 +2773,7 @@ void tcp_set_state(struct sock *sk, int state)
27692773
inet_put_port(sk);
27702774
fallthrough;
27712775
default:
2772-
if (oldstate == TCP_ESTABLISHED)
2776+
if (oldstate == TCP_ESTABLISHED || oldstate == TCP_CLOSE_WAIT)
27732777
TCP_DEC_STATS(sock_net(sk), TCP_MIB_CURRESTAB);
27742778
}
27752779

0 commit comments

Comments
 (0)