简体   繁体   English

TCP数据包分片中的数据包顺序

[英]packet order in TCP packet fragmentation

In TCP/IP, we have MSS and MTU when sending and receiving packets. 在TCP / IP中,在发送和接收数据包时,我们具有MSS和MTU。

  1. MTU is an IP layer concept, which is determined by the underlying hardware. MTU是IP层概念,由底层硬件确定。 It shows the maximum data size that an IP layer packet can contain during one transmission. 它显示了一次传输期间IP层数据包可以包含的最大数据大小。
  2. MSS is a TCP layer concept, which is limited by the MTU, showing that the TCP data stream will be fragmented into MSS-size packets. MSS是TCP层的概念,受MTU的限制,表明TCP数据流将被分成MSS大小的数据包。

Our protocol lies on top of TCP, and each protocol will define its own packet. 我们的协议位于TCP之上,并且每个协议都将定义自己的数据包。 One example is MySQL, which defines its packet size up to 2^24-1, that is around 16M. 一个示例是MySQL,它定义的数据包大小最大为2 ^ 24-1,大约为16M。 When the big enough protocol packet comes to TCP, it will be fragmented according to MSS. 当足够大的协议数据包到达TCP时,它将根据MSS进行分段。

Assume that a client needs to send DATA1 and DATA2 to server. 假设客户端需要将DATA1和DATA2发送到服务器。 DATA2 size is bigger than MSS, and DATA2 will be fragmented into DATA2_1, DATA2_2. DATA2的大小大于MSS,并且DATA2将分为DATA2_1,DATA2_2。 As the packets will be handled by the IP layer, so the time that each packet arrives at server might not be the same as that when the client sends them. 由于数据包将由IP层处理,因此每个数据包到达服务器的时间可能与客户端发送数据包的时间不同。

So I think the sequence of packets' arriving might be the following: 因此,我认为数据包到达的顺序可能如下:

  1. DATA1 DATA2_1, DATA2_2 DATA1 DATA2_1,DATA2_2
  2. DATA1, DATA2_1, DATA2_2 DATA1,DATA2_1,DATA2_2
  3. DATA1, DATA2_2, DATA2_1 DATA1,DATA2_2,DATA2_1

In the first case, the server receives DATA1 and DATA2_1 in one tcp packet and then another packet contains DATA2_2 arrives. 在第一种情况下,服务器在一个tcp数据包中接收DATA1和DATA2_1,然后另一个包含DATA2_2的数据包到达。

In the second case, the server receives DATA1, DATA2_1 and DATA2_2 in three packets. 在第二种情况下,服务器在三个数据包中接收DATA1,DATA2_1和DATA2_2。

In the third case, the server first receives DATA2_2 and then DATA2_1. 在第三种情况下,服务器首先接收DATA2_2,然后接收DATA2_1。

My question: 我的问题:

Is the third case possible? 第三种情况可能吗?

If yes, it disobeys that TCP is a stream protocol, and stream protocol should be ordered. 如果是,则它不服从TCP是流协议,应订购流协议。 And even this does not break the stream rule, how to handle this scenario? 甚至这并没有违反流规则,如何处理这种情况?

If no, how TCP makes the disordered packets into its original order? 如果否,TCP如何使无序数据包恢复其原始顺序?

It is possible to receive that sequence over the network, however the TCP implementation will hide that detail from your application and only feed the data to you in stream order. 可以通过网络接收该序列,但是TCP实现将在您的应用程序中隐藏该细节,并且仅以流顺序将数据提供给您。 (In fact since fragmentation happens at the IP layer it won't even be shown to the TCP layer until the second part has arrived also) (实际上,由于分段发生在IP层,因此直到第二部分也到达TCP层时才显示)

The fact that received packets have to be held in a buffer even after receiving them in some cases like this is why you will see people referring to UDP as better for lower latency applications: you can receive datagrams out of order with UDP and it's up to your application to figure out how to deal with that possibility. 即使在这样的情况下,即使在接收到数据包之后也必须将其保存在缓冲区中,这就是为什么您会看到人们将UDP称为低延迟应用程序更好的原因:您可以使用UDP乱序接收数据报,而这取决于您的应用程序以弄清楚如何处理这种可能性。

Is the third case possible. 第三种情况是可能的。

Yes, of course. 当然是。

If so, it disobeys that TCP is a stream protocol ... 如果是这样,它就违背TCP是一种流协议...

No it doesn't. 不,不是。

Your cases concern arrival of IP packets into a host. 您的案例涉及IP数据包到达主机。 TCP being a stream protocol is about delivery of data into an application. TCP是一种流协议,它涉及将数据传送到应用程序中。

The packet fragments get reassembled in the correct order by the IP layer, and the packets get reassembled into segments in the correct order by TCP, and the now correctly ordered data stream is delivered to the application. IP层以正确的顺序将数据包片段重组,而TCP则以正确的顺序将数据包重组为段,然后将正确排序的数据流传递给应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM