TCP 窗口缩放指数rcv_wscale(发送 SYN 或 SYN-ACK 时的 fe)是在 Linux 内核中基于函数 tcp_select_initial_window() 中的套接字接收缓冲区计算的:
/* If no clamp set the clamp to the max possible scaled window */
if (*window_clamp == 0)
(*window_clamp) = (65535 << 14);
space = min(*window_clamp, space);
/* Quantize space offering to a multiple of mss if possible. */
if (space > mss)
space = (space / mss) * mss;
//...
(*rcv_wscale) = 0;
if (wscale_ok) {
/* Set window scaling on max possible window
* See RFC1323 for an explanation of the limit to 14
*/
space = max_t(u32, space, sysctl_tcp_rmem[2]);
space = max_t(u32, space, sysctl_rmem_max);
space = min_t(u32, space, *window_clamp);
while (space > 65535 && (*rcv_wscale) < 14) {
space >>= 1;
(*rcv_wscale)++;
}
}
这里的space取自基于sk_rcvbuf的tcp_full_space()。
知道您可以通过更改接收缓冲区的大小来影响此计算:
int buflen = 12345;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof int) < 0)
perror("setsockopt():");
//...
这可以为您提供零缩放(WS=0 或 wscale 0)。
附:请记住,在服务器端它应该在侦听套接字上完成,因为在 TCP 握手之后你不能影响它。