MSS:The TCP Maximum Segment Size Option(wireshark抓包查看)
MSS相关概念:
MSS全称是 Maximum Segment Size,表示TCP握手时通信双方协商的最大报文段长度。
MSS计算方式: THE TCP MAXIMUM SEGMENT SIZE IS THE IP MAXIMUM DATAGRAM SIZE MINUS FORTY.(RFC879)
即MSS = MTU - IP HEADER(20字节) - TCP HEADER(20字节)
RFC6691关于MSS的说明:
When calculating the value to put in the TCP MSS option, the MTU value SHOULD be decreased by only the size of the fixed IP and TCP headers and SHOULD NOT be decreased to account for any possible IP or TCP options; conversely, the sender MUST reduce the TCP data length to account for any IP or TCP options that it is including in the packets that it sends. The rest of this document just expounds on that statement, and the goal is to avoid IP-level fragmentation of TCP packets.
The size of the fixed TCP header is 20 bytes [RFC793], the size of the fixed IPv4 header is 20 bytes [RFC791], and the size of the fixed IPv6 header is 40 bytes [RFC2460].
简单来说,数据接收方声明MSS值时,只需要考虑MTU减去固定IP头(-20)减去固定TCP头(-20);反过来,数据发送方必须再减去数据包中所包含的IP或TCP可选信息(Options)。
MSS声明而非MSS协商:
This Maximum Segment Size (MSS) announcement (often mistakenly called a negotiation) is sent from the data receiver to the data sender and says "I can accept TCP segments up to size X". The size (X) may be larger or smaller than the default. The MSS can be used completely independently in each direction of data flow. The result may be quite different maximum sizes in the two directions.
MSS声明(通常被错误称为协商),即数据接收方告诉数据发送方本端能接受的最大消息段长度。
MSS值可以在数据流的两个方向上不同,即A<---tcp--->B,A和B可以向对方声明各自的MSS,且双方MSS在传输过程中不同。
MSS设置过大或者过小
MSS值过小:数据传输效率降低
MSS值过大:导致数据包可以封装很大,那么在IP传输中分片的可能性就会增大,接受方在处理分片包所 消耗的资源和处理时间都会增大,如果分片在传输中还发生了重传,那么其网络开销也会增大。因此合理的 MSS是至关重要的。MSS的合理值应为保证数据包不分片的最大值。
对于以太网MSS可以达到1460byte = MTU(1500) - IP头(20) - TCP头(20) (即:TCP一次发送的应用数据大于MSS,会分段成多个TCP包发送出去)
Whireshark抓包
- TCP握手时,通信双方发送各自的MSS字段,均为1460;
- TCP连接建立好后,发送数据最大长度(1448)=MSS(1460)-可选TCP信息(Options 12) octets