This article links through two diagrams to comb the TCP-IP
knowledge of the protocol. The TCP
communication process consists of three steps: establishing a TCP
connection channel, transmitting data, disconnecting the TCP
connection channel. As shown in 1, the TCP
communication process is given.
Figure 1 mainly consists of three parts: establishing a connection , transmitting data , disconnecting from a connection .
Establishing TCP
a connection is simple, with three handshakes to establish a connection.
Once the connection is established, the data transfer begins. TCP
There are many concepts involved in data transfer: time-out retransmission , fast retransmission, flow control , congestion control , and so on.
The process of disconnecting is also simple, through a four-time handshake to complete the disconnection process.
three-time handshake to establish a connection:
First handshake: The client sends the syn
packet ( seq=x
) to the server and enters the SYN_SEND
state, waiting for the server to confirm;
Second handshake: The server receives the syn
packet, it must confirm the customer's SYN
( ack=x+1
), and also send a SYN
package ( seq=y
), that is, the SYN+ACK
package, the server enters the state at this time SYN_RECV
;
Third handshake: The client receives the server's SYN+ACK
package, sends a confirmation packet to the server ACK
( ack=y+1
), the packet is sent, the client and server enter ESTABLISHED
the state, and three handshakes are completed.
The data is not included in the packets that are delivered during the handshake, and the client and server formally begin transmitting the data after the three handshake is complete. Ideally, once the connection is established, the connection TCP
will be maintained until either side of the communication actively closes the connection TCP
.
Transfer data process:
- Time-out retransmission
The time-out retransmission mechanism is used to guarantee TCP
the transmission reliability. Each time a packet is sent, the sent datagram has a seq
number, the receiving end receives the data, will reply ack
to confirm, indicating that a seq
number of data has been received. After sending a package, the sender waits for a period of seq
time, and if it does not receive the corresponding ack
reply, it will think the message is lost and will retransmit the packet.
- Fast re-transmission
The recipient of the data found that the packet was lost. The message will be sent ack
to send end multiplicity to transmit the lost message. if the sending side continuously receives the same label ack
, it triggers a fast retransmission of the client (without waiting for the timeout timer to end to confirm that the packet has been lost). Comparing time-out retransmission and fast retransmission, it can be found that time-out retransmission is the sending side in the silly timeout, and then trigger retransmission, and the fast retransmission is the receiving side actively tell the sender to send the data confiscated, and then trigger the transmission end multiplicity.
- Flow control
The main point here is the TCP
sliding window flow control. In TCP
the head there is a field called Window
, also called Advertised-Window
, this field is the receiving end tells the sender how many buffers you can receive data . The sender can then send the data based on the processing power of the receiving end, without causing the receiver to process it. Sliding windows can be a TCP
mechanism for improving transmission efficiency.
- Congestion control
The sliding window is used for flow control. flow control only focuses on the status of the sender and the receiver itself, without regard to the communication of the entire network . Congestion control is considered based on the entire network. Consider this scenario: a time delay in the network on the sudden increase, then the TCP
response to this matter only retransmission data, but the retransmission will lead to a heavier burden on the network, resulting in greater delay and more drops, so this situation will enter a vicious circle is constantly magnified. Imagine that if there are thousands of connections within a network that TCP
are acting like this, a "cyber storm" will soon form, and TCP
the protocol will drag down the entire network. Therefore, TCP
the congestion control strategy is introduced. Congestion strategy algorithm mainly includes: slow start , congestion avoidance , congestion occurrence , fast recovery .
Four-time handshake disconnect:
First wave: The active shut-off party sends a FIN
, to shut down the active side to the passive shutdown of the data transfer, that is, the active shutdown to tell the passive shut-off party: I will no longer send you the data (of course, the fin
data sent before the package, if not received the corresponding ack
confirmation message , the active shutdown will still re-send the data), but the active shut-off party can also accept the data.
Second wave: The passive closing party receives the FIN
package, sends a ACK
to the other, confirms the serial number to receive the serial number +1
(with the SYN
same, one FIN
occupies a serial number).
Third wave: The passive shut-down party sends a FIN
, used to close the passive shut-off side to the active shutdown of the data transfer, that is, to tell the active shutdown, my data is sent out, will not send you data.
The fourth wave: The active closing party received FIN
, send a ACK
to the passive closed side, confirm the serial number to receive the serial number, so far +1
, four times to complete the wave.
state transition Diagram
Figure 2 shows the TCP
state transition diagram in the communication process, and understanding this diagram is the key to our understanding TCP-IP
of the Protocol.
Detailed explanation of the state diagram:
CLOSED
: The starting point, which enters this state when the time-out or connection is closed.
LISTEN
: The service side waits for the connection to come in the state , the service side to call for this, socket
bind
,, listen
function, can enter this state. This is called the application passive open (waiting for the client to connect).
SYN_SENT
: The client initiates the connection and sends it SYN
to the server side. If the server side cannot connect, it enters the CLOSED
state directly.
SYN_RCVD
: Corresponds to 3, server side accepts client's SYN
request, server driven by LISTEN
state enters SYN_RCVD
state. At the same time the server side to respond to one ACK
, while sending one SYN
to the client; in another case, the client SYN
receives the server-side request at the same time, and the client is sent SYN
to the SYN_SENT
SYN_RCVD
state.
ESTABLISHED
: The server side and the client have completed 3 handshake entry States, indicating that data can already be transferred.
These are the state transfer instructions generated by the server side and the client when the connection is established . Relatively simple and straightforward, if you are familiar with the three-time handshake, it is easy to understand the state transfer when establishing a connection.
Let's take a look at the state transfer at the close of the connection, close the need to do 4 interaction between the two sides, but also to deal with some aftercare work ( TIME_WAIT
State), note that the active shutdown here or the passive closed side is not referring to the server side or the client, is relative to who initiated the shutdown request:
FIN_WAIT_1
: The active closed party, which enters this state by state 5. The specific action is sent FIN
to each other.
FIN_WAIT_2
: A party that actively shuts down receives the other party's FIN-ACK
(that is, fin
the packet's response packet) and enters this state.
CLOSE_WAIT
: After receiving FIN
, the passive closed party enters this state. The specific action is received FIN
and sent at the same time ACK
. (The reason close_wait
for This is that the passive shut-down party is waiting for the upper application to issue a close connection instruction )
LAST_ACK
: The passive closed party, initiating a shutdown request, enters this state by state 8. The specific action is sent FIN
to the other side, at the same time ACK
when received into the CLOSED
state.
CLOSING
: This state is entered when both sides initiate a close request FIN_WAIT_1
. The specific action is to receive the FIN
request while responding to one ACK
.
TIME_WAIT
: The most tangled state has come. As can be seen from the state diagram, there are 3 states that can be transformed into it, and we hit analyze:
A. By FIN_WAIT_2
entering this state: In the case where both parties are not simultaneously initiating FIN
, the active closed party receives the back entry state of the passive closed party after completing its own initiated closing request FIN
.
B. By CLOSING
State entry: Both parties initiate the closure at the same time, both of which have made the initiating FIN
request, while receiving FIN
and doing ACK
the case, entered by the CLOSING
state.
C. FIN_WAIT_1
enter by state: At the same time received FIN
(the other party initiates), ACK
(itself initiates the FIN
response), and b
the difference lies in the response itself initiated FIN
before the ACK
other side of the FIN
request arrived, and b
is FIN
arrive first. The probability of this is minimal.
The most difficult to understand state of closing 4 connections is TIME_WAIT
that there are TIME_WAIT
2 reasons:
TCP
the termination of a full-duplex connection is reliably implemented.
- Allow old repeating sections to fade in the network.
Attached: Slow hot start Algorithm –
Slow Start
First, let's take TCP
a look at the slow hot start. Slow start means that the connection just joined the network, 1.1 points to speed up, do not come up like those privileged cars as overbearing to fill the road. New students on the high-speed or slow, do not put in the high-speed order to mess up.
The algorithm for slow start is as follows (full cwnd
name Congestion Window)
:
1) The start of the connection is first initialized cwnd = 1
, indicating that a size data can be transmitted MSS
.
2) Whenever one ACK
is received, a cwnd++
linear rise
3) When one is over RTT
cwnd = cwnd*2
;
4) There is also a ssthresh
( slow start threshold
), is an upper limit , when cwnd >= ssthresh
, will go into the "Congestion Avoidance algorithm" (the algorithm will be said later)
So, we can see that if the speed of the Internet is fast, it will also ACK
return quickly, RTT
also short, then this slow start is not slow.
Congestion avoidance algorithm –congestion avoidance
As mentioned earlier, there ssthresh
is one ( slow start threshold
), which is an upper limit, when cwnd >= ssthresh
it goes into "congestion avoidance algorithm". ssthresh
The general value is 65535, the unit is byte, when cwnd
this value is reached, the algorithm is as follows:
1) When one ACK
is received,cwnd = cwnd + 1/cwnd
2) When each one RTT
is over,cwnd = cwnd + 1
In this way, you can avoid increasing the network congestion and slowly increase the optimal value of the adjustment to the network. Obviously, it is a linear ascending algorithm.
The algorithm of the congestion state
As we said earlier, there are two situations when a packet is dropped:
1) Wait RTO
for the timeout to retransmit the packet. TCP
think this situation is too bad, the reaction is very strong.
sshthresh = cwnd /2cwnd 重置为 1进入慢启动过程
2) Fast Retransmit
algorithm, that is, when you receive 3 duplicate ACK
, you turn on retransmission without waiting for a RTO
timeout.
TCP Tahoe
Implementation is the RTO
same as the timeout.
TCP Reno的实现是:cwnd = cwnd /2sshthresh = cwnd进入快速恢复算法——Fast Recovery
Above we can see that RTO
after the time-out, sshthresh
will become cwnd
half, which means that if the loss of the cwnd<=sshthresh
packet, then TCP
the loss sshthresh
will be half, and then quickly and exponentially cwnd
increased to climb to this place, it will become slowly linear increase. We can see TCP
how the balance of website traffic is quickly and carefully found through this intense shock.
Fast Recovery algorithm –fast Recovery
This algorithm is defined in the RFC5681
. Fast retransmission and fast recovery algorithms are commonly used simultaneously. The fast recovery algorithm is thinking that you have 3 other Duplicated Acks
instructions that the network is not so bad, so there is no need to be as strong as the RTO
timeout. Note, as mentioned earlier, that Fast Recovery
before entering, cwnd
and sshthresh
has been updated:
cwnd = cwnd /2sshthresh = cwnd
Then, the real Fast Recovery
algorithm is as follows:
cwnd = sshthresh + 3 * MSS
(3 means to confirm that 3 packets have been received)
Retransmit Duplicated ACKs
the specified packet. If you receive it again duplicated Acks
, then cwnd = cwnd +1
. If you receive a new Ack
, then, cwnd = sshthresh
then you enter the algorithm for congestion avoidance.
If you think about the algorithm above, you will know that there is a problem with the algorithm above--it relies on 3 repetitions Acks
. Note that 3 duplicates Acks
do not mean that only one packet has been lost, most likely lost a lot of packets. But this algorithm will only retransmit one, and the remaining packets only wait for the RTO
timeout, so, into the nightmare mode-time out a window to halve, multiple timeouts will exceed TCP
the transmission speed is a series decline, and will not trigger Fast Recovery
the algorithm.
TCP New Reno
So, 1995, TCP New Reno
(see RFC 6582
) algorithm proposed, mainly in the absence SACK
of support to improve Fast Recovery
the algorithm--
When sender
this side receives 3 Duplicated Acks
, enter the Fast Retransimit
mode and develop the packet to re-transmit Acks
the repeated instructions. If only this one bag is lost, then the retransmission of this package will return the Ack
entire data that has been sender
transmitted ack
. If not, it means that several packages have been lost. We call this ACK
for Partial ACK
.
Once Sender
this is found Partial ACK
, then, sender
it can be inferred that a number of packages have been lost, so continue to re sliding window
-transmit ack
the first packet. It was not until it was received Partial Ack
that the process really ended Fast Recovery
.
We can see that this “Fast Recovery的变更”
is a very radical play, and he has extended the process at the same time Fast Retransmit
Fast Recovery
.
Schematic TCP-IP protocol