IPv4 Internet Protocol version 4 协议解析

协议头

|00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|
|  Version  |    IHL    |      DSCP       | ECN |                   Total Length                |
|              Identification                   |  Flags |          Fragment Offset             |
|   Time To Live        |      Protocol         |                   Header Checksum             |
|                                            Source IP Address                                  |
|                                         Destination IP Address                                |
|                                           Options(if IHL > 5)                                 |
|                                              Data                                             |

Version 版本号 4bits

数值是4表示IPv4

IHL Internet Head Length IPv4协议头长度 4bits

这个数值表示有几个32位的数据,也就是有几个4字节,如果没有可选项,那么值是5,表示4*5=20个字节。从上图也可以看出正好5排。所以可选项是否存在也是根据IHL>5判断。

TOS Type of Service 服务类型 8bits

用来控制数据传输优先级,又分为DSCP和ECN

  • DSCP Differentiated Services Code Point 差分服务器编码点 6bits

  • ECN Explicit Congestion Notification 显示拥塞通知 2bits

TOS RFC791

实际上一开始IP协议中定义的不是这个格式,具体信息如下

  Bits 0-2:  Precedence.
  Bit    3:  0 = Normal Delay,      1 = Low Delay.
  Bits   4:  0 = Normal Throughput, 1 = High Throughput.
  Bits   5:  0 = Normal Relibility, 1 = High Relibility.
  Bit  6-7:  Reserved for Future Use.

     0     1     2     3     4     5     6     7
  +-----+-----+-----+-----+-----+-----+-----+-----+
  |                 |     |     |     |     |     |
  |   PRECEDENCE    |  D  |  T  |  R  |  0  |  0  |
  |                 |     |     |     |     |     |
  +-----+-----+-----+-----+-----+-----+-----+-----+

    Precedence

      111 - Network Control
      110 - Internetwork Control
      101 - CRITIC/ECP
      100 - Flash Override
      011 - Flash
      010 - Immediate
      001 - Priority
      000 - Routine

DSCP RFC1349

增加一位,表示消耗

                1000   --   minimize delay
                0100   --   maximize throughput
                0010   --   maximize reliability
                0001   --   minimize monetary cost
                0000   --   normal service

DSCP RFC2474

后来标志位不够用,又重新做了定义,前面6个字节就变成了现在的样子

    0   1   2   3   4   5   6   7
  +---+---+---+---+---+---+---+---+
  |         DSCP          |  CU   |
  +---+---+---+---+---+---+---+---+

    DSCP: differentiated services codepoint
    CU:   currently unused

类别选择代码 Class Selector Codepoints CS

由于DSCP与上面的PRECEDENCE有兼容性问题,并且可读性比较差,因为DSCP有6bits,那么就有0-63个级别,不方便判断具体的等级。所以DSCP又做了区分,前3bits定义与PRECEDENCE定义一样,在这里叫做类别选择码。定义了CS0-CS7一共8个优先级。

CS类别

对于CS又分了4个类别

类别 缩写 解释 中文解释 对应数值
Default Forwarding DF which is typically best-effort traffic 默认值,尽量转发 000 000
Expedited Forwarding EF dedicated to low-loss, low-latency traffic 加速转发,要求低丢包,低延时 101 110
Assured Forwarding AF gives assurance of delivery under prescribed conditions 保证转发,保证在规定条件下传输 xxx xx0
Class Selector CS which maintain backward compatibility with the IP precedence field 向后兼容 xxx 000
Assured Forwarding RCF2597

对于保证转发,又做了细分,前3bits做了4个级别,后面紧跟的2bits表示丢弃等级,可以表示3个丢弃等级

注意:这里用了5位,实际上有6位,最后一位没有意义

丢弃等级 Class 1(CS1) Class 2(CS2) Class 3(CS3) Class 4(CS4)
Low Drop Prec AF11 001010 AF21 010010 AF31 011010 AF41 100010
Medium Drop Prec AF12 001100 AF22 010100 AF32 011100 AF42 100100
High Drop Prec AF13 001110 AF23 010110 AF33 011110 AF43 100110

Explicit Congestion Notification ECN 显式拥塞通知 RFC3168

在TCP传输中,需要进行拥塞控制,实际上可以在发生拥塞之前,提前通知对方,减少数据包发送,这样就可以避免触发拥塞控制,提高网络质量。
ECT-ECN-Capable Transport 支持ECN的协议

    ECT   CE         [Obsolete] RFC 2481 names for the ECN bits.
     0     0         Not-ECT,非ECT
     0     1         ECT(1),支持ECN
     1     0         ECT(0),支持ECN
     1     1         CE,遇到拥塞

01-已经发送ECN-Echo

10-还未发送ECN-Echo

Total Length 总长度 16bits

这个IP数据包总长度,包括Head,表示多少个bytes。因为是16bits,所以IPv4数据包最大是65535bytes。但是由于MTU大小一般是1500,包括Ethernet II协议也规定数据长度为1500,所以如果数据超过这个长度就需要分片了。

Identification 标识 16bits

说分片,分片就来了。这个是用来针对同一组传输数据,由于受限于Total Length,采取分片传输后,区分属于哪组分片的标识。Identification相同,并且四元组也形同,就可以认定为同一分片。

Flags 标志 3bits

标识分片状态,控制分片

  • bit 0:保留,设为0
  • bit 1:是否可分片 0-可分片;1-不可分片,这个标志是DF(Don't Fragment),所以设置是反过来的。
  • bit 2:是否有更多分片 0-没有更多分片,表示为最后一个分片数据包;1-还有更多分片数据包

Fragment Offset 分片偏移 13bits

第一个分片是0,这个数值表示数据处于原数据的位置偏移,偏移了多少个8bytes。最大数据是2^13 - 1=8191个8bytes,所以最大是8191*8=65528bytes,是小于IPv4最大数据长度65535的,所以是可以分片的,并且就算加上head也是合法的。
这里有一个需要注意,由于表示是偏移几个8bytes,所以分片数据必须是8bytes倍数。

Time To Live TTL 生存时间 8bits

一个数值,每经过一个路由器会减1,数值为0时,数据包丢掉。这是为了避免有数据包找不到目的地址,在网络中不断的空转,这样的包越来越多,如果不丢掉,就会耗尽网络资源。

Protocol 协议 8bits

表示当前数据包的数据部分保存的是什么协议的内容,列几个常见的

数值 协议
1 ICMP
4 IP in IP 一种隧道协议把IP数据包在另一个IP中
6 TCP
17 UDP

详情可以参考 https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml

Header checksum 头部校验码 CRC 16bits

用于校验头部数据是否正确

Source address 源IPv4地址 32bits

Destination address 目的IPv4地址 32bits

Options 可选项

这个不常用,最后数据必须填充为32bits的倍数

Data 数据

https://www.firewall.cx/networking-topics/protocols/protocols-ip/164-protocols-ip-header.html
https://www.rfc-editor.org/rfc/rfc791.html
https://www.rfc-editor.org/rfc/rfc1349.html
https://www.rfc-editor.org/rfc/rfc2474.html
https://www.rfc-editor.org/rfc/rfc2597.html
https://www.rfc-editor.org/rfc/rfc2481.html
https://www.rfc-editor.org/rfc/inline-errata/rfc3168.html
https://en.wikipedia.org/wiki/Differentiated_services
https://support.huawei.com/enterprise/zh/doc/EDOC1100247677/d1ffff39#fig_dc_fd_qos_000501

posted @ 2023-04-13 15:40  秋来叶黄  阅读(217)  评论(0编辑  收藏  举报