当前位置:网站首页>Other 7 features of TCP [sliding window mechanism ▲]

Other 7 features of TCP [sliding window mechanism ▲]

2022-07-07 08:37:00 A flower

Preface : Part 1 , We wrote TCP Three characteristics of , This article will TCP Other 7 Big features !

Sliding window mechanism , On the premise of reliability , Further improve the transmission efficiency

4. Sliding window mechanism

Recognize sliding windows

The way of sending and receiving : TCP The protocol needs to confirm the data , To send the next packet , Pictured :

 Insert picture description here
Pictured above , Every packet sent by the sender , After receiving the acknowledgement from the receiver , To send the next packet , It is a serial process of question and answer ; That is, you need to wait for a corresponding waiting time every time you transmit data , So the transmission N Copy of the data , We need to wait N Second response time , Total transmission time :N Data transmission time + N Response transmission time

The performance of one sending and one receiving mode is low , So we send multiple pieces of data at a time , It can greatly improve the performance ( In fact, it overlaps the waiting time of multiple segments ), Pictured :

 Insert picture description here
Sliding window is essentially a batch transmission of data
Total transmission time :N The data transmission time overlaps into one time ,N The transmission time of replies overlapped into one time
It is equivalent to the transmission time and waiting time of multiple copies of data ACK The time is compressed into one , The total waiting time is less , Transmission efficiency is also high

  • window

Window size : Don't wait for ACK Under the circumstances , The maximum amount of data sent in batch , Call " Window size " ( Pictured above , Namely 4000 Bytes ,4 A field )

  • When sending the first four fields , There's no need to wait for any ACK, Direct transmission ;
  • Got the first one ACK after , Slide the window back , Continue to send the data of the fifth field , By analogy ;
  • The operating system kernel maintains this sliding window ; It needs to be opened up Send buffer To record what data is not answered ; Only confirm the data that has been answered , To delete from the buffer ;
  • The greater the window , The higher the throughput of the network
  • slide :

The data within the window is waiting for these data ACK ( Has been sent )
 Insert picture description here
Pictured above , When the sender receives 2001 Of ACK, signify 1001 - 2000 The data of the other party has been received , At this time, the transmission continues immediately 5001 - 6000 The data of , Is waiting for ACK The sequence number of the packet is 2001,3001,4001,5001

Handling of packet loss

The two cases of packet loss are shown in the figure :

 Insert picture description here
situation 1: Confirm response (ACK) Packet loss
No additional processing is required ; under these circumstances , part ACK It doesn't matter if you lose it , Because it can be through subsequent ACK Into the Line confirmation

 Insert picture description here
understand " Confirm the serial number " The meaning of
Start with the current serial number , The previous data have been correct
Pictured above , yes 1001 Of ACK The loss of ,2001 Of ACK I didn't lose it , here , The sender received 2001 after , You think 1 - 1000 This data also arrived smoothly ,1001 It doesn't matter if you lose it ,2001 Of ACK Can contain 1001 ACK Information in

situation 2: Packet loss

Here's the picture , if 1001-2000 The loss of , then 2001-3000,3001-4000 Wait for the next few data to arrive smoothly , At this time, the host B Feedback ACK The confirmation number of is always 1001;
At this time, if the host A Found several consecutive ACK All are 1001, host A You know ,1001 This data is missing , It will be retransmitted 1001
Retransmit 1001 Before , The serial number of the confirmation received is 1001
When the host B received 1001 When it comes to this data , because 2001-7000 These data have been received , Next ACK From 7001 Start
Retransmission here , Just retransmit the lost data , Other data does not need additional retransmission —— The fast retransmission ( With timeout retransmission under sliding window )
 Insert picture description here
" Disorder " transmission :

 Insert picture description here

 Insert picture description here

5. flow control

flow control : According to the processing capacity of the receiver, the sending efficiency of the sender is inversely balanced ( Window size ) ( Through the... Of the receive buffer “ The size of the remaining space ” To determine the rate of the sender )

The window size cannot be infinite , The transmission rate is too fast , The receiver may not be able to handle
When using sliding window mechanism for data transmission , The sender sends packets according to the actual situation , The receiver receives packets ; Because the ability of the receiving end to process packets is different , Therefore, there may be two situations :

  • If the window is too small , The sender sends a small number of packets , The receiver will soon process , And it can handle more packets ; When transmitting large data, you need to wait for the sender , It will cause great delay

  • If the window is too large , The sender sends a large number of packets , And the receiver can't handle so many packets , Will block the link ; If you discard these packets that should have been received , It will trigger the resend mechanism

( The first window size is determined by the link bandwidth ) , Send packet , The receiver receives these packets , And return the confirmation response package , Tell the sender how many packets they want to receive next time ( New window size ), After the sender receives the acknowledgement packet , Packets will be sent at this window size

  • The receiver puts the size of the buffer it can receive into TCP In the first part “ Window size ” Field , adopt ACK End notification
    The sender
  • The larger the window size field , The higher the throughput of the network
  • Once the receiver finds that its buffer is almost full , It will set the window size to a smaller value and inform the sender
  • After the sender receives this window , It will slow down your sending speed
  • If the receiving buffer is full , Will set the window to 0; At this time, the sender no longer sends data , But it needs to be sent regularly
    A window detects data segments , Make the receiving end tell the sending end the window size

Here's the picture :

 Insert picture description here
The illustration :

  • Suppose the initial window size is 4000, The sender sends 4 A packet , Respectively 1-1000,1001-2000,2001-3000 and 3001-4000
  • The receiver receives packets , Received 1-1000 After the packet , Set the window size to 3000, Tell the sender that it can only handle 3 A packet , Next time please send 3 A packet
  • The sender receives the acknowledgement packet , Check that the size of the return window of the receiver is 3000, Know that the receiving end has handled 1 A packet ; Then continue to send : 1001-2000,2001-3000 and 3001-4000
  • Upon receipt by the receiving party , The buffer can only handle 2 A packet , The second time in the past 3 A packet 3001-4000 Not handled ; This shows that the receiver can only process 2 A packet , The first 3 Packets need to be retransmitted
  • When the window is 0 when , The sender will pause sending , It will also send a detection message regularly ; Because the data in the receiving buffer may be taken away at any time , Once removed , You can receive new data ( The sliding window size changes dynamically

Window detection :

6. Congestion control ( Security mechanism )

Congestion control is to consider the degree of congestion on the network transmission path

although TCP With the sliding window, this big killer , It can send a large amount of data efficiently and reliably ; But if you send a lot of data in the beginning , It can still cause problems ; Because there are many computers on the network , Maybe the current network state is already congested , Without knowing the current network state , Sending a lot of data rashly , It is very likely to make things worse
therefore ,TCP introduce Slow start Mechanism , Start with a small amount of data , Explore the way , Find out the current state of network congestion , And then decide how fast to transmit data

 Insert picture description here

The line increases and decreases ( And add , Product reduction )

The growth rate of congestion windows like the above , It's exponential ,“ Slow start ” It just means slow at the beginning , But the growth rate is very fast , The specific growth is shown in the figure below :

​ At the beginning, I started from 1 Exponential growth , After reaching the threshold, it starts to grow linearly , If there is network congestion , Directly reduce to the initial value , Then exponential growth again reaches a new threshold ( The new threshold is half the size of the last blocking window ), Increase linearly again until the network is blocked , Always change the loop dynamically

Why dynamic changes ??
The congestion of the network is changing rapidly , We should adjust dynamically according to the actual situation of the network at any time ( Adapt to the changing process of the network at any time )
( Mathematical derivation )
 Insert picture description here
 Insert picture description here

7. Delay response ( Efficiency mechanism )

The aim is to improve efficiency , On the basis of flow control , Try to return to a reasonable but relatively large window

We mentioned earlier , After the sender sends the data , The host receiving the data needs to return ACK The reply , At this time, if you return immediately , The window may be small ( Because the data in the buffer is only partially processed ), therefore TCP Delayed response mechanism is adopted , give an example :

Reply without delay :
There is a supermarket , Assume that the stock of instant noodles is stored at most 100 box , Currently saved 80 box , free 20 box ;
The next morning , The delivery boy came to ask :“ Boss , How many noodles do you need tomorrow ?”
Boss :“ The most you can give is 20 Just a box .”

With delayed reply :
The next morning , The delivery boy came to ask :“ Boss , How many noodles do you need tomorrow ?”
Boss :“ I'll call you in the evening to tell you how much you need .”
( It may be sold during the day 10 box , The most you can give the next day is 10 Box is enough )

Delayed response is actually to let ACK The delivery time of is a little late ( Without affecting reliability )
The delay time will give the application the opportunity to consume more data , Now it's time , Send again ACK When , The resulting window size ( The remaining space in the receive buffer will be larger )

The greater the window , The greater the network throughput , The higher the transmission efficiency ; Our goal is to improve the transmission efficiency as much as possible without congestion

ask : Can all packets be delayed ?

Definitely not
.
1. Quantitative restriction : every other N Just one response per packet (N It's usually 2)
2. The time limit : Answer once when the maximum delay time is exceeded ( Time is generally taken 200 ms, The waiting time of delayed response cannot exceed the time of timeout retransmission , Otherwise, it will be retransmitted )

8. Take a reply ( Efficiency mechanism )

On the basis of delayed response , The mechanism introduced to further improve the efficiency of program operation

In many cases , The communication mode between client and server is generally Request - Response Pattern , namely “ One question and one answer ”
Pictured :

 Insert picture description here
Be careful :
Between three handshakes SYN and ACK It's all determined by the kernel , It doesn't involve different timing
The above mentioned four wave process ,ACK It is determined by the kernel , The hair FIN (close Method ) It is the application code that determines

9. Stick package problem

Strictly speaking , The sticking problem is not TCP Its own mechanism , It is a common problem of byte stream oriented transmission
Sticky package , It refers to application layer packets , This leads to data processing , Easy to read half of the application layer packets

Byte stream oriented : It refers to reading one byte at a time , Or read two bytes at a time , Or read it once N Bytes are ok

give an example : The two sides establish a connection , You need to send data of different structures within a period of time after connecting , If connected , There are several structures

  • “ How are you ”
  • “ wonderful P”

How many bytes to read is a complete application layer datagram , This is not clear
If you read one Chinese character at a time , Read it out “ good ”; If you read three Chinese characters at a time , Read it out “ wonderful P”
The pronunciation is different , The final meaning is also very different ; Read application layer data , You should not read only half a package

How to avoid sticking package ?

In the final analysis, it's just a sentence , Define the boundary between the two packages
TCP The protocol itself does not help you distinguish application layer packets , Relatively speaking ,UDP The agreement has no such problem (UDP The protocol sends and receives data packets )

  • The way 1 - Use separator

such as , The above answer is changed to “ wonderful P;”

Use semicolon ; As the separator of two packages , Reading data , Read until the semicolon ; It is considered to be a complete application layer packet
Application layer protocol , It's the program itself , Just make sure that the separator doesn't conflict with the text **

  • The way 2 - Specify the length of the package

such as , The above use case is changed to “4 How are you 3 wonderful P”

​ First read the first four bytes , Get the length of the bag 3; Continue reading 3 The Chinese characters , Then read a complete package

HTTP Based on the agreement TCP Application layer protocol , I will deal with the sticking problem by myself , The above two methods are used :
.
about GET request , The separator is an empty line
about POST request ,Content-length To specify the length of the package

reflection : about UDP Agreement for , Is there also “ Stick package problem ” Well ?

  • about UDP, If there is no upper level delivery data yet ,UDP The length of the message is still . meanwhile ,UDP It's a handful of numbers
    Data is delivered to the application layer ; There are very clear data boundaries
  • From the perspective of application layer , Use UDP When , Or receive the full UDP message , Or not ;
    There will be no " Half " The situation of

10. The survival mechanism

The two sides establish an interactive connection , Data interaction does not always exist , Some connections are made after data interaction , Actively release the connection , And some don't , So in a long period of no data interaction , Both sides of the interaction may lose power 、 crash 、 Abnormal restart , Or the intermediate routing network is disconnected for no reason 、NAT Overtime and other accidents

In these “ Abnormal situation ” Next ,TCP There will be some special processing for connection
give an example :
1. Process breakdown : This situation ,TCP The connection will be normal for four waves ( As long as the process exits , Will automatically close the relevant files )
2. The host computer is shut down ( Shut down according to the process ): The process will be forced to kill first when shutting down , There will be four waves in the process of killing
3. The main engine is powered off / The network cable is disconnected :

  • a) The receiver is powered off
    When the peer tries to send a message , There will be no ACK The situation of — Then a timeout retransmission will be triggered — Retransmit a certain number , Will reset the connection — Give up the connection
  • b) The sender is powered off
    The opposite end attempts to receive a message , For the receiver , I didn't know when the sender would send , Just wait ?
    Solution Heartbeat bag TCP Both sides of the communication , Even in the process of no data interaction , It will also regularly transmit data to each other, which has no business significance “ Heartbeat bag ”, Just to prove “ I live ”, Once I haven't received the heartbeat package of the other party after a period of time , It can be regarded as the opposite end " hang " 了

TCP summary

TCP It's complicated , Because it must ensure reliability , At the same time, improve the performance as much as possible

 Insert picture description here
reliability :
The checksum , Serial number
Confirm response , Over time retransmission
Connection management , flow control , Congestion control

Improve performance :
The sliding window , The fast retransmission
Delay response , Take a reply

other :
Timer ( Timeout retransmission timer , Life keeping timer ,TIME_WAIT Timers, etc )

 Insert picture description here

原网站

版权声明
本文为[A flower]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130633057712.html