[Interpretation] TCP stick package unpacking

A, TCP stick package, illustrated unpacking

Suppose the client send two packets D1 and D2 to the server, the server since the time to read the number of bytes is uncertain, so that there may be the following four cases:

     1) is read twice to the server, two separate data packets, respectively D1 and D2, and not stick package unpacking

     2) the server receives a packet of the two, D1 and D2 are bonded together, referred to as  TCP stick package

     3) the server is read twice to the packet, the first time a complete read packet D1 and D2 parts of the package, the second reading to the remaining package contents D2, which is called  TCP unpacking

     4) the server is read twice to the packet, the first read part to the package D1_1 - D1, the second reading to the remaining portions of the package contents D1 D1_2 D2 and the full package.

     In particular, note that, if accepted TCP sliding window is very small, and the data packet D1 and D2 is relatively large, is likely to occur fifth case, a service end points several times to the D1 and D2 package fully accept, during on many occasions unpacking.

 

Second, stick package, reasons occurred unpacking

      Causes There are three types: sliding window, MSS / MTU restrictions, Nagle Algorithm

1, sliding window

      TCP flow control, primarily using a sliding window protocol , the size of the acceptance window is a sliding window of data terminal used, the size of the buffer used to tell the receiving end of the transmitting side, the transmitting side in order to control the size of the transmission data, so as to achieve the flow rate

Control purposes. The window size is our first several data transmission. Sequentially given frame numbers for all the data transmission side during transmission is always maintains a transmission window, only the frame falls within the allowed transmission window to be transmitted;

While the receiver maintains a receive window, only the frame falls within the reception window only allowed to receive. Thus: By adjusting the size of transmitting window and the side window of the receiver, the flow control can be achieved.

  Now look at how to create a sliding window is stick package, unpacking?

Stick package: Suppose the sender 256 bytes each represents a complete message, the recipient is not in time because the data processing, the 256 bytes of data are cached to the SO_RCVBUF (reception buffer region).

   If SO_RCVBUF recipient of a number of packets in the cache, then for the recipient concerned, this is the stick package.

Unpacking: Consider another case, the receiver is assumed only the window 128, means that the transmission side can also send a maximum of 128 bytes,

           Since the sender and the data size is 256 bytes, and therefore only 128 bytes before sending, the receiver until after the ACK, in order to transmit the remaining bytes. This has resulted in unpacking.

2, MSS and MTU fragmentation

MSS: Maximum Segement Size is the abbreviation, TCP represents the maximum length of packet data portion, the TCP protocol is five in the OSI network model, to limit the maximum data transfer can be sent to the primary layer.

MTU: Maximum Transmission Unit Maxitum Transmission Unit is an abbreviation of a five-layer OSI network model, limits the maximum data link layer (datalink layer) may be transmitted to the primary.

When data to be transmitted is larger than the MSS or MTU, data is split into multiple packets for transmission. Since the MSS is calculated from the MTU, so when the data transmission to meet the MSS, necessarily satisfied MTU.

To better understand, we first introduce the application process to send data via TCP five-layer network model:

For the application layer, only concerned with the transmission data DATA, the data is written to the transmission buffer SO_SNDBUF socket in the kernel, i.e., to return the data in the operating system will be taken out SO_SNDBUF transmitted.

    Before DATA-transport layer will add TCP Header, constituting a full TCP packet.

    When the data arrives at the network layer (network layer), the network will add a layer on the basis of IP Header TCP packet on, that is, its own network address added to the message.

    When the data link layer, and will be added Datalink Header CRC.

    Upon reaching the physical layer, will SMAC (Source Machine, the MAC address of the sender of data), DMAC (Destination Machine, data recipient MAC address) is added and the Type field.

    Can be found before sending data, each layer will add some content on the basis of the upper layer, the lower diagram shows the MSS, MTU role in this process.

      Ethernet transmission of data MTU is limited aspect, each Ethernet frame has a maximum or minimum size can not exceed 64bytes 1518bytes. Plane to the Ethernet frame header (DMAC destination MAC address 48bit = 6Bytes

+ SMAC source MAC address field 48bit = 6Bytes + Type 2bytes) 14Bytes CRC check and the trailer portion 4Bytes (this section we sometimes call it also FCS), the local carrier then the remaining upper layer protocol also

Data Domain is the maximum you can only have 1500Bytes, we'll call it the value MTU .

  Since MTU restrictions can send a maximum of 1500 bytes, whereas TCP protocol when sending DATA, will add additional TCP Header and Ip Header, and therefore the plane to two parts, one is the TCP protocol

The maximum size of the actual application data to be transmitted, that is MSS.

      MSS longitudinal length = MTU - IP Header - TCP Header

TCP Header length is 20 bytes, the IPv4 in the IP Header length is 20 bytes, the IPV6 IP Header length is 40 bytes, thus: in IPV4, the MSS may reach Ethernet 1460byte; in IPV6, the Ethernet

MSS can reach 1440byte.

  Note that the maximum length of time that can be sent DATA MSS representation, rather than the true length of the DATA. When the sender transmits data, when the data is larger than a SO_SNDBUF the MSS, the operating system will enter data

行拆分,使得每一部分都小于MSS,这就是拆包,然后每一部分都加上TCP Header,构成多个完整的TCP报文进行发送,当然经过网络层和数据链路层的时候,还会分别

加上相应的内容。

需要注意:  默认情况下,与外部通信的网卡的MTU大小是1500个字节。而本地回环地址的MTU大小为65535,这是因为本地测试时数据不需要走网卡,所以不受到1500

               的限制。

3、 Nagle算法

       TCP/IP协议中,无论发送多少数据,总是要在数据(DATA)前面加上协议头(TCP Header+IP Header),同时,对方接收到数据,也需要发送ACK表示确认。

即使从键盘输入的一个字符,占用一个字节,可能在传输上造成41字节的包,其中包括1字节的有用信息和40字节的首部数据。这种情况转变成了4000%的消耗,这样的

情况对于重负载的网络来是无法接受的。

为了尽可能的利用网络带宽,TCP总是希望尽可能的发送足够大的数据。(一个连接会设置MSS参数,因此,TCP/IP希望每次都能够以MSS尺寸的数据块来发送数据)。

Nagle算法就是为了尽可能发送大块数据,避免网络中充斥着许多小数据块。

Nagle算法的基本定义是任意时刻,最多只能有一个未被确认的小段。 所谓“小段”,指的是小于MSS尺寸的数据块,所谓“未被确认”,是指一个数据块发送出去后,没有

收到对方发送的ACK确认该数据已收到。

Nagle算法的规则:

      1)如果SO_SNDBUF(发送缓冲区)中的数据长度达到MSS,则允许发送;

      2)如果该SO_SNDBUF中含有FIN,表示请求关闭连接,则先将SO_SNDBUF中的剩余数据发送,再关闭;

      3)设置了TCP_NODELAY=true选项,则允许发送。TCP_NODELAY是取消TCP的确认延迟机制,相当于禁用了Nagle 算法。

      4)未设置TCP_CORK选项时,若所有发出去的小数据包(包长度小于MSS)均被确认,则允许发送;

      5)上述条件都未满足,但发生了超时(一般为200ms),则立即发送。

 

Guess you like

Origin www.cnblogs.com/651434092qq/p/11067528.html
Recommended