tcp options

TCP Options

 
Current, TCP is used is shown below Option
----

Kind

(Type)

Length

Name

Reference

Description Use &

0

1

EOL

RFC 793

The end of the list of options

1

1

NOP

RFC 793

No operation (complement for bit stuffing)

2

4

MSS

RFC 793

Segment maximum length

3

3

WSOPT

RFC 1323

Window expansion coefficient (Window Scaling Factor)

4

2

SACK-Premitted

RFC 2018

Show support SACK

5

variable

SACK

RFC 2018

SACK Block (received scrambled data)

8

10

TSPOT

RFC 1323

Timestamps

19

18

TCP-MD5

RFC 2385

MD5 authentication

28

4

TUE

RFC 5482

User Timeout (after removal of the connection over a certain idle time)

29

variable

TCP-IP

RFC 5925

Certification (optional algorithms)

253/254

variable

Experimental

RFC 4727

Reserved for scientific experiments

 
Option for the general format of TLV structure, as follows:

Kind / Type(1 Byte)

Length(1 Byte)

Value


1. EOL, and NOP Option (Kind 0, Kind 1) only 1 Byte, no Length, and Value fields;

2. NOP for padded to a multiple of the length of the TCP Header 32bit (due to the Header Length field to 32bit units, so TCP Header length must be a multiple of 32bit);

3. SACK-Premitted Option accounted 2 Byte, Value field is not;

4. Option begin with the rest of the 1 Byte "Kind", Option type specified; the total length specified Option Length (including Kind and Length)

5. For receive a "can not understand" the Option, TCP will ignore off does not affect the other contents of the TCP Segment;

 


 ① .Maximum Segment Size (MSS) Option

 
一般情况下,通信双方在建立连接时,SYN Segment中会携带MSS Option,MSS指明本端可以接受的最大长度的TCP Segment(Payload,不含TCP Header),也就是说,对端发送数据的长度不应该大于MSS(单位Byte)。
————
1.    首先要明确一点,MSS并非和对端协商的值,而是对对端发送数据长度的“限制”,表明在整个TCP连接期间,都不会接收长度大于MSS的TCP Segment。

2.    如果收到的SYN中没有MSS,将使用默认值536。MSS Option的Value字段长度固定为16bit,所以MSS最大值为65535(单位Byte)。因此,网络中所有设备都被要求,必须能够处理大小小于576Byte的数据包(IP Header + TCP Header + Default MSS 最小值为 576 Byte)

3.    IPv4网络中,MSS的典型取值为1460 ,1460Byte + 20Byte IP Header + 20Byte TCP Header = 1500Byte = 以太网典型MTU;

4.    IPv6网络中,典型MSS取值为1440;另外,如果MSS=65535,表示MSS = PMTU - 60

 

 

② .Selective Acknowledgment (SACK) Options

 
     在标准的TCP实现中,使用的是累加式的ACK,例如“ACK Num = n”代表对序列号n以前的Bytes进行确认。但是,显然,这样将无法对不连续的Segment进行确认。此外,当出现不连续Segment时,还会导致TCP的接收队列出现一个“坑”,不将这个坑填上,坑后的数据就无法交付给应用程序。

      为解决上述问题,TCP定义了SACK Option,可以使TCP接收者将这个“坑”的位置通告给发送者,让其对这一段数据进行重传。


     注意:若要使用SACK特性,必须在建立连接时,在SYN Segment中附加上SACK-Permitted Option,以此告知对方自己支持SACK。

SACK-Permitted Option格式如下所示:

Kind = 4

Length = 2


SACK Option通过“Left Edge ~ Right Edge”,指定了一个或多个范围的Seq Num,称为SACK Block,指明了处于“坑”后面(或坑之间)的、已成功接收的Bytes。SACK Option格式如下所示
————

Kind

(5)

Length

(可变)

Left Edge of 1st Block

(32bit)

Right Edge of 1st Block

(32bit)

……

……

Left Edge of nst Block

(n≤4)

Right Edge of nst Block

(n≤4)


另外,由于TCP Header最长为60 Byte,因此SACK Option中最多只能包含4个SACK Block

Example(终端A - 终端B)
————
1.    终端A收到了TCP数据流中的Seq Num为0 ~ 1452、2905~4096的字节,但缺少了1453~2904;

2.    终端A向B发送ACK Segment,其中ACK Num=1453、SACK Option=2905~4097,表明它已经收到了数据流中的Seq Num为2905 ~ 4096的字节,但没有还没收到1453~2904;

3.    终端B收到这个SACK后,重传包含Byte 1453~2904的TCP Segment;

4.    终端A向B发送ACK Segment,其中ACK Num=4097,表明它已经收到Seq Num 4097之前的所有字节;

5.    之后,数据通信恢复正常。




③ .Window Scale (WSCALE or WSopt) Option

TCP Header的Window Size字段长度为16bit,因而正常情况下,Window Advertisement最大只能是65535 Bytes;

Window Scale Option用于将TCP Header的Window Size字段从16bit扩展至最多30bit,格式如下所示:

Kind

(3)

Length

(3)

shift.cnt

(范围0~14)


————
1.    shift.cnt的取值范围为0~14,表示将Window Advertisement的值扩展至“WindowSize × 2s

2.    WSopt只能出现在SYN Segment或SYN+ACK Segment中,因此shift.cnt在三次握手之后就会固定下来。

3.    另外,WSopt是双向独立的,因此连接的两个方向可以有不同的Shift.cnt。但是,WSopt必须双向同时启用,也就是说,如果SYN中不带有WSopt,SYN+ACK中也不能出现WSopt;同样,如果SYN+ACK中不带有WSopt,那么发起SYN的一端就会当作自己也不曾发送过WSopt。

4.    shift.cnt根据接收Buffer的大小,由TCP自动选取。接收Buffer由系统或程序设定。

 

 

 ④ .Timestamps Option and PAWS(Protection against Wrapped Sequence Numbers,防止序列号回绕)

 
启用Timestamp Option后,每个TCP Segment中都会带有Timestamp Option,其中包含了两个32bit的Timestamp(TSval和TSecr),具体格式如下所示:

Kind

(8)

Length

(10)

Timestamp Value(TSval)

Timestamp Echo Reply(TSecr)


————
1.    TSval指明了发送端在发送TCP Segment时的Timestamp;接收端在对该TCP Segment做ACK时,将TSval值回显在TSecr字段中。

注意:由于TCP连接是双向的,接收端在ACK中回显TSecr时,也会把自己当前的Timestamp放入TSval字段。

2.    Timestamp是一个随时间单调递增的值,由于TCP接收端只需要在ACK中将TSval简单地回显,因此通信双方并不需要进行时间同步等操作。

3.    通过Timestamp Option,发送端再也不需要在内存中保存发送Segment的时间了,只需要将其放入TSval,然后接收端将其回显在ACK Segment即可。当发送端收到ACK Segment后,取出TSscr,和当前时间做算术差,即可完成一次RTT的测量。

4.    若非通过Timestamp Option来计算RTT,大部分TCP实现只会以“每个Window采样一次”的频率来测算RTT。因此通过Timestamp Option,可以实现更密集的RTT采样,使RTT的测算更精确。

另外,Timestamp Option还有PAWS(Protection Against Wrapped Sequence Numbers,防止序列号回绕)功能,详见以下示例
————
1.    假设TCP Window Size为1GB(使用Window Scale),发送者每发送一个Window的数据Timestamp值加100,数据的发送情况如下所示:

时间点

发送数据量

Seq Num

Timestamp

接收

1

0G:1G

0G:1G

0~100

OK

2

1G:2G

1G:2G

100~200

其中某些Segment丢包后重传

3

2G:3G

2G:3G

200~300

<p align="left" style="box-sizin
发布了140 篇原创文章 · 获赞 28 · 访问量 18万+

Guess you like

Origin blog.csdn.net/qq_16097611/article/details/79063149