当前位置:网站首页>[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
2022-07-06 13:35:00 【Li bohuan】
One . summary
The so-called reliable transmission refers to the information in Sender and receiver accuracy 、 Accurate transmission .TCP Mainly provides Inspection and 、 Serial number / Confirm response 、 Over time retransmission 、 The sliding window 、 Congestion control and flow control And so on Reliable transmission .
- Inspection and : By means of inspection and , The receiving end can detect whether the data is Errors and anomalies , If there is any mistake, it will be lost directly Discard TCP paragraph , To resend .
- Serial number / Confirm response : The function of the serial number is not just the function of the response , With the serial number, the received data can be sorted according to the serial number , And remove the heavy Data of complex serial number . TCP In transit , Every time the receiver receives data , Will confirm and respond to the transmitting party . That is, sending ACK message , This ACK The message contains Corresponding confirmation serial number , Tell sender , What data has been received , Where to start sending the next data .
- The sliding window : Sliding window not only improves the efficiency of message transmission , It also avoids the exception that the sender sends too much data and the receiver cannot handle it normally .
- Over time retransmission : Timeout retransmission refers to the sending of data packets to the receiving Confirmation package Time between , If it exceeds this time, it will be Think it's a packet loss , Need to retransmit . The maximum timeout is calculated dynamically .
- Congestion control : In the process of data transmission , It may be due to the problem of network status , Causing network congestion , At this time, congestion control mechanism is introduced , Guarantee TCP Reliability at the same time , Improve performance .
- flow control : If the host A All the way to the host B send data , Regardless of the host B The ability to accept , May cause the host to B Acceptance of The buffer is full and can no longer accept data , This will lead to a large number of packet loss , Trigger retransmission mechanism . And in the process of retransmission , If the host B The receive buffer condition of has not improved yet , It will waste a lot of time retransmitting data , Reduce the efficiency of transmitting data . Therefore, the flow control mechanism is introduced , host B By telling the host A The size of your own receive buffer , Make the host A Control the sent Data volume . Flow control and control TCP The window size in the protocol header is related to .
Two . details
One TCP The message segment consists of The first one and Data load Two parts make up ,TCP All the functions of are embodied in it The first fields The role of , First let's look at TCP Header format of message segment ( Minimum 20 byte , Maximum 60 byte ):
The meaning of each field is as follows :
(1) Source port and destination port : Each account 2 Bytes . The source port is used to identify send out The TCP Of the message segment Application process , The destination port is used to identify receive The TCP Of the message segment Application process . The reuse and distribution functions of the transport layer can only be realized through the port .
(2) Serial number : Occupy 4 Bytes .TCP It's for Byte stream Of , therefore TCP In the data stream transmitted in the connection Each byte is numbered with a serial number ( Not content ). The sequence number field is used to indicate this TCP Sequence number of the first byte of the message segment data load , As shown in the figure below , The value of the sequence number field is 166.
(3) Confirmation no. : Occupy 4 Bytes , Is expecting to receive the other party's Next segment Of the data load The sequence number of the first byte , It's also right Confirmation of all data received before . If the confirmation number is N, Before indicating N-1 All data has been received correctly , And the expected serial number is N The data of , Only when ACK = 1 The confirmation field is valid , Otherwise it will not work .
(4) Data offset : Occupy 4 A bit , And 4 Bytes are units , It points out that TCP The starting distance of the data load of the message segment TCP How far is the beginning of the message segment , Actually pointed out Indicates the length of the head . The fixed length of the head is 20 byte , The longest is 60 byte , Binary representation as 0101 and 1111.
(5) Retain : Occupy 6 position , Don't use , So set it to 0.
(6) Emergency position URG: When its value is 1 when , Indicates that the emergency pointer field is valid . It will tell the system that there is emergency data in this message segment , It should be transmitted as soon as possible . however URG It needs to be used with emergency pointer , That is, the bytes from the first byte to the emergency pointer are emergency data .
(7) Confirmation bit ACK: Only when ACK=1 when , The confirmation number field is valid .TCP Regulations , After the connection is established, all packets must be sent ACK Set as 1.
(8) Push bit PSH:TCP received PSH=1 Message segment , Deliver and receive the application process as soon as possible , Instead of waiting for the entire cache to fill up and deliver up .
(9) Reset bit RST: When RST=1 when , indicate TCP A serious error occurred in the connection , Connection must be released , Then reestablish the transport connection .
(10) Sync bit SYN:SYN=1 Indicates that this is a connection request or connection receiving message .SYN = 1 as well as ACK = 1 by TCP Connection request acknowledgement message segment .
(11) Stop bit FIN: Used to release a connection .FIN=1 Indicates that the data of the sender of this message segment has been sent , And ask to release the transmission connection .
(12) window : Occupy 2 Bytes . It refers to the party sending this paragraph Receiving window , The data cache space of the receiver is limited , So use the window value as The receiver asks the sender to set the basis of its sending window , This is to control the sending ability of the sender based on the receiving ability of the receiver , be called flow control . The size of the sending window is the minimum of the congestion window and the receiving window .
(13) The checksum : Occupy 2 Bytes . Its inspection scope includes header and data part . Calculate in TCP Add... Before the message segment 12 Pseudo header of byte .
(14) Pointer to an emergency : Occupy 16 position , Point out how many bytes of emergency data are in this report .
(15) Options : Variable length .TCP Initially only one option was specified , That is, the maximum message segment length (MSS).
(16) fill : This is to make the whole head length 4 Integer multiples of bytes .
How to determine the timeout retransmission time ?
Timeout retransmission time RTO When it's certain TCP One of the most complicated problems , If the setting is too small, unnecessary retransmission will occur, which will increase the network load , Too large will reduce the transmission rate , As shown in the following two pictures
For the above two situations , We can draw the following conclusion , Timeout retransmission time RTO The value of should be slightly greater than the round trip time RTT Value . However, round trip time RTT Due to the complexity of the network, it will fluctuate greatly . So we need to calculate a representative round-trip time RTT, Also known as Smooth round trip time , The calculation formula is not posted , You can find it by yourself , Just remember the above concepts .
Let's talk about sliding window in detail :
In data transmission , If the data transmitted is large , You need to split it into multiple packets to send .TCP The protocol needs to confirm the data , To send the next packet ( One question and one answer ). thus , It's a waste of time waiting for an acknowledgement packet . To avoid that ,TCP The concept of window is introduced . Window size refers to the maximum number of packets that can continue to be sent without waiting for an acknowledgement response ( Ask and answer more ).
From the figure above, you can see that on the left of the sliding window is Packets sent and acknowledged , On the right side of the sliding window is It's not your turn to group . The sliding window is also divided into two parts , One is Packets that have been sent but not confirmed , The other is waiting in the window Packets to be sent . As the sent packets are continuously acknowledged , Packets waiting to be sent in the window will also be sent . The whole window will move to the right , Not yet It is the turn of the group to enter the window . It can be seen that the sliding window plays a role of current limiting , in other words Size of the current sliding window Determines the current TCP Rate of packets sent , The size of sliding window depends on the minimum value between congestion control window and flow control window .
Let's talk about congestion control in detail :
- If many resources in the network are in short supply at the same time , Network performance will change significantly , The throughput of the whole network will decrease with the increase of input load , This is congestion .
- Congestion control is to prevent too much data from being injected into the network , In this way, the router or link in the network will not be overloaded .
TCP A total of four algorithms are used to achieve congestion control :
- Slow start (slow-start);
- Congestion avoidance (congestion avoidance);
- The fast retransmission (fast retransmit);
- Fast recovery (fast recovery).
The sender maintains a congestion window (cwnd) State variable of . Its size depends on the degree of network congestion , And it's changing dynamically . The sender makes its sending window less than or equal to the congestion window .
The slow start algorithm idea is :
- The sending window is set first cwnd = 1, Send the first segment , After receiving a message segment, confirm ,cwnd Add 1
- Every transmission round , Congestion window cwnd Double it ( That is, it increases exponentially )
- When cwnd Greater than ssthresh At threshold , Use congestion avoidance algorithm 1( Add more )
- Suppose it grows to a certain value ( Such as 24), The network timed out , At this time will be ssthresh The value becomes half the value ( Such as 12)( Multiplication is reduced ), Let's get back to cwnd Set up 1, Re adopt the slow start algorithm , Repeat the above steps .
The idea of congestion avoidance algorithm is : Let the congestion window cwnd Linear slow growth
The algorithm idea of fast retransmission is :
- The receiver is required to send repeated Confirmation immediately after receiving a sequential message segment , Instead of waiting for data to be sent before piggyback confirmation
- The sender only needs to receive three repeated confirmations in a row , It should immediately retransmit the message segment that has not been received by the other party , Instead of waiting for the set retransmission timer to expire
The idea of fast recovery algorithm is :
- When the sender receives three consecutive repeated acknowledgments , Is executed “ Multiplication is reduced ” Algorithm , Set the slow start threshold ssthresh halve
- Then start slowly without execution , It's a new threshold ssthresh Start congestion avoidance algorithm ( Add more )
边栏推荐
- System design learning (III) design Amazon's sales rank by category feature
- View UI Plus 发布 1.3.1 版本,增强 TypeScript 使用体验
- ArrayList的自动扩容机制实现原理
- 1.C语言矩阵加减法
- C语言入门指南
- Wei Pai: the product is applauded, but why is the sales volume still frustrated
- Mortal immortal cultivation pointer-1
- Floating point comparison, CMP, tabulation ideas
- Aurora system model of learning database
- View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
猜你喜欢
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
5.函数递归练习
2.初识C语言(2)
Database operation of tyut Taiyuan University of technology 2022 database
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
Application architecture of large live broadcast platform
Leetcode.3 无重复字符的最长子串——超过100%的解法
Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
12 excel charts and arrays
C语言入门指南
随机推荐
4.二分查找
8.C语言——位操作符与位移操作符
Rich Shenzhen people and renting Shenzhen people
Wei Pai: the product is applauded, but why is the sales volume still frustrated
7.数组、指针和数组的关系
Redis的两种持久化机制RDB和AOF的原理和优缺点
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
4.分支语句和循环语句
【毕业季·进击的技术er】再见了,我的学生时代
3.C语言用代数余子式计算行列式
Common method signatures and meanings of Iterable, collection and list
IPv6 experiment
Abstract classes and interfaces
View UI plus released version 1.3.1 to enhance the experience of typescript
C language Getting Started Guide
Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
4. Binary search
Tyut outline of 2022 database examination of Taiyuan University of Technology
【九阳神功】2018复旦大学应用统计真题+解析