2017年2月9日 星期四

TCP Segment and Connection on Linux


I have a Java program using Thrift that open() and close() to remote server.

Before program runs:
[ricky@pc009 ~]$ netstat -natu

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:3350          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6010            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6011            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:3389            0.0.0.0:*               LISTEN
tcp        0      0 10.1.10.112:22          10.1.10.111:62012       ESTABLISHED
tcp        0      0 10.1.10.112:22          10.1.10.111:62060       ESTABLISHED
tcp        0      0 10.1.10.112:22          10.1.10.111:62013       ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:631                 :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
tcp6       0      0 :::6010                 :::*                    LISTEN
tcp6       0      0 :::6011                 :::*                    LISTEN
udp        0      0 0.0.0.0:55270           0.0.0.0:*
udp        0      0 0.0.0.0:68              0.0.0.0:*
udp        0      0 127.0.0.1:323           0.0.0.0:*
udp        0      0 0.0.0.0:49957           0.0.0.0:*
udp        0      0 0.0.0.0:5353            0.0.0.0:*
udp6       0      0 ::1:323                 :::*
udp6       0      0 :::51781                :::*

After program runs:

tcp6       0      0 10.1.10.112:57094       27.111.195.209:7039     TIME_WAIT
tcp6       0      0 10.1.10.112:56920       27.111.195.209:7039     TIME_WAIT
tcp6       0      0 10.1.10.112:56746       27.111.195.209:7039     TIME_WAIT
tcp6       0      0 10.1.10.112:57003       27.111.195.209:7039     TIME_WAIT

The number of TCP connections increases at around 100 connections per second, and it becomes stable at 7000.

[ricky@pc009 ~]$ date && netstat -natu | wc -l
Thu Feb  9 15:40:26 HKT 2017
24
[ricky@pc009 ~]$ date && netstat -natu | wc -l
Thu Feb  9 15:40:31 HKT 2017
190
[ricky@pc009 ~]$ date && netstat -natu | wc -l
Thu Feb  9 15:40:32 HKT 2017
297
[ricky@pc009 ~]$ date && netstat -natu | wc -l
Thu Feb  9 15:41:02 HKT 2017
2179
[ricky@pc009 ~]$ date && netstat -natu | wc -l
Thu Feb  9 15:43:38 HKT 2017
6414
[ricky@pc009 ~]$ date && netstat -natu | wc -l
Thu Feb  9 15:47:39 HKT 2017
6821

What is the default ipv4 params in Centos 7.2? [ricky@pc009 ~]$ sysctl net.ipv4.ip_local_port_range
net.ipv4.ip_local_port_range = 32768    61000
[ricky@pc009 ~]$ sysctl net.ipv4.tcp_fin_timeout
net.ipv4.tcp_fin_timeout = 60
Max connection is (61000 - 32768) / 60 = 470 socket connections per second.

What is the TCP segment header?
20 Bytes, or 5 32-bit words, or 5 4-byte words, or 5 words.
Source port has 2 bytes max to 65535.
Destination port has 2 bytes max to 65535.
Sequence Number 4 bytes max 4,294,967,295
Acknowledgement Number 4 bytes max 4,294,967,295
Data offset has 4 bits, min 5 for 20 bytes header, max 15 for 60 bytes header
Reserved 3 bits
Flags 9 bits, including NS, CWR, ECE, URG urgent pointer, ACK acknowledge, PSH push, RST reset connection, SYN synchronize sequence number, FIN finish with last package.
Window Size 2 bytes max 65535, receiving window size
Checksum 2 bytes max 65535 for both header and data
Urgent Pointer 2 bytes max 65535

What is TCP 3-way handshake?
1st: Machine A sends a packet with (SYN=1, SN=123456) so that Machine B knows it is a request of creating a session by reading SYN=1, and it enters SYN_SEND state.
2nd: B sends a packet with (SYN=1, ACK=1, SN=666666, AN=123457) to A indicating that it has confirmed the request of connection by (SYN=1, ACK=1), and it enters SYN_RECV.
3rd: A reads the 2nd package and verify that AN is corrects as the expecting 123457 and ACK is set to 1. If so, A replies B a confirmation by sending a packet with (ACK=1, AN=666667). A enters ESTABLISHED after packet sent. B enters ESTABLISHED after packet received.

Or in short it exchanges the sequence numbers in both direction

1. A SYN(SN=1111), SYN_SEND
2. B SYNACK(AN=1112,SN=5555), SYN_RECV
3. A ACK(AN=5556), ESTABLISHED


沒有留言:

張貼留言

2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...