关于IPv4报文中总长度字段包含首部长度的疑问
嘿,这个问题问到点子上了!其实IPv4这么设计,背后是几个非常实际的考量,咱们来唠唠:
链路层处理更高效:不管是交换机、路由器还是网卡,在处理IPv4报文的时候,链路层需要知道整个报文的完整大小来做校验或者转发。总长度直接给出了这个数值,设备不用先读取IHL(首部长度)再加上数据长度来计算,少了一步运算——在早期设备算力有限的年代,这可是实打实的性能优化。
分片重组更方便:当报文因为链路MTU限制需要分片时,每个分片的总长度字段能直接告诉接收方这个分片的整体大小。接收方重组报文的时候,不用再去计算每个分片的首部加数据长度,直接靠总长度就能快速确定每个分片的边界,重组效率高很多。而且每个分片都是独立的,总长度包含首部的话,分片自己就能携带完整的长度信息,不用依赖其他分片。
历史设计的务实性:IPv4是几十年前的设计了,当时的主机和路由器性能远不如现在,协议设计更倾向于“直接给结果”而非“让接收方计算”。把总长度设为包含首部的数值,能最大程度降低设备的处理复杂度,减少出错概率。
你提到看了RFC791但没找到原因,其实RFC更多是定义字段的功能和规范,不会把所有设计动机都写进去。不过从实际使用场景倒推,这些就是最核心的原因啦。
Total Length: 16 bits
Total Length is the length of the datagram, measured in octets,
including internet header and data. This field allows the length of
a datagram to be up to 65,535 octets. Such long datagrams are
impractical for most hosts and networks. All hosts must be prepared
to accept datagrams of up to 576 octets (whether they arrive whole
or in fragments). It is recommended that hosts only send datagrams
larger than 576 octets if they have assurance that the destination
is prepared to accept the larger datagrams.The number 576 is selected to allow a reasonable sized data block to
be transmitted in addition to the required header information. For
example, this size allows a data block of 512 octets plus 64 header
octets to fit in a datagram. The maximal internet header is 60
octets, and a typical internet header is 20 octets, allowing a
margin for headers of higher level protocols.
备注:内容来源于stack exchange,提问作者joe joe




