TCP (one)

Source: Internet
Author: User

Transmission Control Protocol TCP
Features: 1, connection-oriented Transport layer protocol
2, each TCP can have only two endpoints. Point-to-point
3,TCP are reliable, error-free, non-repetitive, sequential arrivals.
4, full duplex, allows the process of communication both sides to send and receive data at all times.
5, for Byte stream. No structure byte stream.

TCP connections
Both ends of each TCP connection are sockets.
Format of sockets: IP Address: Port number

Ideal transmission characteristics: The transmission channel does not produce errors, regardless of the sender at a multi-block speed to send data, the receiver is always time to deal with.

Stop waiting for protocol

To stop waiting is to stop sending after each packet is sent, waiting for the other party to acknowledge receipt of the confirmation before sending the next packet.

There was a mistake
A as long as more than a period of time still do not receive confirmation, it is believed that the packet just sent was lost. The previously sent groupings are therefore re-transmitted. This is the time-out retransmission. Set a timeout timer every time you send it out. If you receive an acknowledgement before the timeout, the timer is revoked.
Note: 1,a after sending a group, you must temporarily keep a copy of the group that you sent, and only clear the copy if you receive confirmation.
2, grouping and confirmation are numbered.
3, the time-out timer setting should be longer than the average round trip time for packet transfers.


3 Confirmation of loss and confirmation of lateness
1, discard duplicate packet M2, do not deliver to upper layer
2, send confirmation to a. Can't think it's been sent, not sent,


Channel utilization
A the time required to send the packet td,b to send a confirmation packet time ta. A after td+rtt+ta, you can send the next packet again. RTT is round-trip time.

Channel Utilization =td/td+rtt+ta

In order to improve the transmission efficiency, the sender can not apply the stop waiting protocol, but the pipeline transmission. With pipelining, a continuous ARQ protocol and a sliding window protocol are used.


Continuous ARQ protocol

Each sender receives a confirmation that the sending window slides forward a grouped position. The receiver generally uses the cumulative confirmation method, does not need to confirm each, after receiving several groupings, sends the confirmation to the last group that arrives in order.

Pros: Lost without retransmission
Disadvantage: You cannot reflect to the sender the information that the receiver has confirmed to be correctly received by all groups.


TCP Message Header

TCP is a byte stream, but the transmitted unit is a segment of the packet. A TCP message segment is divided into the header and the data two parts. The full functionality of TCP is reflected in the header fields.

The first 20 bytes of the TCP header are fixed. The following 4N bytes are incremented as needed, with a minimum TCP header of 20 bytes.

Header fields:
1, source port and destination port, each two bytes.

2, serial number, 4 bytes, range [0,2^32-1]. Each byte in a stream of bytes transmitted in a TCP connection is numbered sequentially. For example, a segment sequence number is 300, the data bit is 100 bytes, then the last byte ordinal is 400. The data sequence for the next message starts at 401 and ends at 2^32-1, starting from 0.

3, Confirmation number, 4 bytes, expected to receive the other side of the next message segment of the first data byte ordinal. For example, send the number 100-200 data, then expect to receive the serial number should be 201, receive confirmation number is N, indicating that all the data so far N-1 received normally.

4, data offset, 4 bits, indicates how far from the beginning of the TCP message segment the data at the beginning of the TCP segment is. The first ministerial degree of TCP message segment is actually pointed out. The data offset is in the 32-bit word, which is 4 bytes. The 4-bit binary has a maximum of 15, so the offset is up to 60 bytes. The maximum length of the TCP header.

5, reserved, 6-bit, all 0

6, emergency, urg=1 indicates that the emergency pointer is valid, high-speed system This message has emergency data and is transmitted as soon as possible.

7, confirm the ACK, when ack=1, the confirmation number is valid, the ack=0 confirmation number is invalid. After the connection is established, the ACK is set to 1.

8, push PSH, two application process to interact, at one end of the application process want to type a command immediately after receiving the response.

9, reset rst,rst=1, serious error in connection, release connection, reestablish connection.

10, Synchronous SYN, when the connection is established with synchronous serial number, when the syn=1 sometimes ack=0, indicating that is a request message segment. If the other party agrees to establish a connection, syn=1,ack=1 should be made in the Response message section. So syn=1 means that this is a connection request or acceptance message.

11, terminate fin, release connection. Fin=1 indicates that the message segment has been sent without requiring a free connection.

An integer between 12, window, 2 bytes, window value, [0,2^16-1]. The window value is the basis on which the sender sets its sending window as the receiver.
The confirmation number is 100 and the window is 1000, which represents the receive cache space for 1000 bytes of data starting from 101.
The Window field indicates that the amount of data that is now allowed to be sent is dynamically changing.

13, test and, 2 bytes. The inspection scope includes the header and the data. At the time of the test, a 12-byte pseudo-header is prepended to the TCP message segment.

14, emergency pointer, 2 bytes, urg=1 meaningful. Indicates the number of emergency data bytes in this section. Indicates the position of the end of the emergency data in the message segment. A window of 0 can also send emergency data.

15, option, variable length, maximum 40 bytes.

Maximum segment length MSS, is the maximum length of the data field in each TCP message segment.

Implementation of TCP reliable transmission

Sliding window in bytes

Suppose a receives a confirmation segment from B, where the window is 20 bytes and the confirmation number is 31. Indicates that B expects to receive the next ordinal number of 31, and the data of the sequence number 30 has been received.


A Send window, in the absence of confirmation of B, a can continuously send the data in the window, all the data that has been sent, must be temporarily retained before receiving confirmation, in order to time-out retransmission.
The serial number in the sending window indicates the number that is allowed to be sent, the larger the window, the more data can be sent by the sender before the confirmation is received, which may result in higher transmission efficiency. However, the receiving party must be in time to process the received data.
The section after the Send window indicates that it has been sent and has been confirmed and does not need to be preserved. The previous section of the Send window front indicates that no send is allowed, and the receiver does not reserve temporary cache space for this part of the data.

After sending the window there are two kinds of changes along the window, not moving (no new confirmation received), forward (receive new confirmation). The trailing edge cannot be moved backwards.

The front of the sending window may move forward and motionless, without receiving confirmation that the window does not change. A new confirmation is received, but the window of the other party's notification shrinks, making the front of the sending window just not moving.

The front of the sending window may also shrink backwards. The window that occurs on the other side of the notification shrinks, but the TCP standard is strongly not recommended.

Describe the status of the Send window



P3-p1=a's Send Window
p2-p1= number of bytes sent but not yet acknowledged
p3-p2= the number of bytes allowed to be sent but not yet sent

B received a sequence number of 32 and 33, the data did not arrive sequentially, because the serial number 31 is not received, B can only be confirmed in order to receive the highest sequence number of data received, so B sent the confirmation report section of the confirmation number is still 31, rather than 32 or 33.

Suppose B receives the sequence number 31 and delivers 31-33 of the data to the host, and then B deletes the data. The Receive window moves forward 3 sequence numbers, while a confirmation is sent to a, the window value is still 20, but the confirmation number is 34. Indicates that the data received up to 33 have been collected, and that 37,38,40 data has been received. But none of these arrives in order, only the receive window can be temporarily present. A after receiving B confirmation, the sending window can be slid forward 3 serial number, P2, the available window increases. can transmit range 42-53


A continue to send 42-53 data, P2 and P3 coincident, window serial number runs out, not received confirmation, the available window is 0. In the absence of confirmation, a after a period of time, a retransmission of this data. Until confirmation is received.



Send Cache
Purpose: 1, the sending application transmits data to the sender TCP ready to send. 2,TCP data that has been sent out but not yet received.
The Send window is usually just part of the send cache, and the confirmed data is deleted in the Send cache. The send cache and the Send window are coincident along the edge.

Receive cache
Usage: 1, the data that arrives sequentially, but is not yet read by the receiving application. 2, data not arriving in order.

Attention
1, at the same time, A's send window is not as large as the receive window of the sum B.
2, for non-sequential arrival of data, the TCP standard is not explicitly specified.
3,TCP requires the receiver to have a cumulative acknowledgement function to reduce the transfer overhead.

Selection of timeout retransmission time

TCP uses adaptive method to record the time of a message segment and the corresponding confirmation time. The two time difference is the round-trip RTT of the message segment. A weighted average round trip time of RTT Rtts. Each subsequent measurement of a new RTT sample is recalculated as follows Rtts
New Rtts= (1-a) * (Old Rtts) +a* (new RTT sample)

0<=a<1, if a is close to 0, indicates that the new Rtts and the old Rtts values change little. If close to 1, the effect is larger.

The timeout retransmission time RTO should be slightly larger than the weighted average round trip time Rtts.
Rto=rtts+4*rttd

The RTTD is a weighted average of the deviations of the RTT. is related to the difference between the Rtts and the new RTT sample.

At the first measurement, the RTTD value is half of the measured RTT sample value, and in subsequent measurements, the formula is used
rttd= (1-b) * (Old RTTD) +b*| rtts-New RTT Sample |

Here B is a factor less than 1, the recommended value is 1/4.


Select Confirm Sack

When a block of bytes is not continuously sent, it should only be re-transmitted missing. The range of byte blocks can be marked.

If you want to use Select Acknowledgement, then when establishing a TCP connection, the option to allow sack is added to the TCP header options, both parties agree.

TCP Traffic Control

Flow control with sliding window

The main function lets the sender send the rate not too fast, lets the other party time receives.

A sends data to B. At the link, B tells a, my receive window rwnd=400. Therefore, the sender's sending window cannot exceed the value of the receiving window given by the receiving party. The window unit of TCP is a byte, not a message segment.


TCP For each connection has a continuous timer, as long as the TCP connection receives the other side of the 0 window notification, the start of the continuous timer, if the duration of the timer set time expires, send a 0 window to explore the text segment, if it is still a zero window, then reset the continuous timer, If it were not 0, it would break the deadlock.

The time of sending a paper by TCP
1,TCP maintains a variable equal to MSS, the maximum message segment length. As long as the data stored in the cache reaches MSS bytes, a TCP message is sent out.
2, the application process by the sender indicates the request sending the message segment, TCP supported push operation.
3, the sender of a timer expires, the current cache is loaded into the message segment sent, but not more than MSS.

Nagle algorithm widely used by TCP

Sending process to send the data sent to the TCP send cache, the sender first sent the first data bytes, the subsequent arrival of the cache. When the first acknowledgment is sent, all the data in the cache is sent, and the subsequent caches are made. The next message is sent only if the previous acknowledgement is received. It also stipulates that when the data reached has reached half the size of the sending window or the maximum length of the message, a message segment is sent immediately.

Solving confused window syndrome:
Let the receiver wait for a period of time so that either the receiving cache has enough space to hold the longest segment of the packet, or wait until the receiving cache has half the free space.


TCP (one)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.