MPTCP 源码分析(五) 接收端窗口值

简述:
     在TCP协议中影响数据发送的三个因素分别为:发送端窗口值、接收端窗口值和拥塞窗口值。
本文主要分析MPTCP中各个子路径对接收端窗口值rcv_wnd的处理。
 
接收端窗口值的初始化
     根据《MPTCP 源码分析(二) 建立子路径》中描述服务端在发送完SYN/ACK并接收到ACK的时候建立新的sock。
在内核实现中,针对连接请求分为两个步骤处理:
  1. SYN队列处理:当服务端收到SYN的时候,此连接请求request_sock将被存放于listening socket的SYN队列,服务端发送SYN/ACK并等待相应的ACK。
  2. accept队列处理:一旦等待的ACK收到,服务端将会创建新的socket,并将连接请求从listening socket的SYN队列移到其accept队列。
当服务端进入LINSTEN状态后,收到第一个SYN包后的处理流程如下:
MPTCP 源码分析(五) 接收端窗口值
详细的函数调用为:
tcp_v4_rcv 
               =》 tcp_v4_do_rcv
                     =》 tcp_rcv_state_process 
                         =》mptcp_conn_request
                              =》tcp_v4_conn_request
                                   =》tcp_conn_request
                                        =》tcp_openreq_init
在函数tcp_conn_request中对连接请求request_sock进行了分配内存。
"net/ipv4/tcp_input.c" line  of
req = inet_reqsk_alloc(rsk_ops);
if (!req)
goto drop;
在函数tcp_openreq_init中对request_sock进行了初始化操作。
 static inline void tcp_openreq_init(struct request_sock *req,
struct tcp_options_received *rx_opt,
struct sk_buff *skb)
{
struct inet_request_sock *ireq = inet_rsk(req); req->rcv_wnd = ; /* So that tcp_send_synack() knows! */
req->cookie_ts = ;
tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + ;
tcp_rsk(req)->snt_synack = ;
req->mss = rx_opt->mss_clamp;
req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : ;
ireq->tstamp_ok = rx_opt->tstamp_ok;
ireq->sack_ok = rx_opt->sack_ok;
ireq->snd_wscale = rx_opt->snd_wscale;
ireq->wscale_ok = rx_opt->wscale_ok;
ireq->acked = ;
ireq->ecn_ok = ;
ireq->mptcp_rqsk = ;
ireq->saw_mpc = ;
ireq->ir_rmt_port = tcp_hdr(skb)->source;
ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
}
第1232行对request_sock的rcv_wnd进行了初始化为0。
 
当服务端收到ACK的时候就会建立相应的socket。将会调用tcp_create_openreq_child函数实现,定义如下:
"include/net/tcp.h" line  of
struct sock *tcp_create_openreq_child(struct sock *sk,
struct request_sock *req,
struct sk_buff *skb);
对于rcv_wnd的处理具体如下:
"net/ipv4/tcp_minisocks.c" line  of
newtp->window_clamp = req->window_clamp;
newtp->rcv_ssthresh = req->rcv_wnd;
newtp->rcv_wnd = req->rcv_wnd;
newtp->rx_opt.wscale_ok = ireq->wscale_ok;
这个阶段为MPTCP的第一条子路径建立情况的三次握手,因此此时创建的socket的属性为master而非slave.
 
下面的情景为创建一条子路径的情况,当服务端收到第一个SYN包的函数调用情况如下:
MPTCP 源码分析(五) 接收端窗口值
函数mptcp_v4_join_request将会对连接请求request_sock进行内存分配并初始化。具体的调用如下:
mptcp_v4_join_request
                                   =》tcp_conn_request
                                        =》inet_reqsk_alloc
                                        =》tcp_openreq_init
当客户端的ACK到达后,内核会将此连接请求request_sock的rcv_wnd赋值给slave subsocket.
 
  
master sock 和 slave sock之间接收端窗口值的关系
     TCP在发包的时候会告诉对方自身的接收端窗口值。MPTCP的实现如下:
"net/mptcp/mptcp_output.c" line  of
u16 mptcp_select_window(struct sock *sk)
{
u16 new_win = tcp_select_window(sk);
struct tcp_sock *tp = tcp_sk(sk);
struct tcp_sock *meta_tp = mptcp_meta_tp(tp); meta_tp->rcv_wnd = tp->rcv_wnd;
meta_tp->rcv_wup = meta_tp->rcv_nxt; return new_win;
}
第994获得最新的窗口值并返回。第998行将slave sock的rcv_wnd赋值给master sock。
 
第994行的函数tcp_select_window的实现如下:
"net/ipv4/tcp_output.c" line  of
u16 tcp_select_window(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
/* The window must never shrink at the meta-level. At the subflow we
279 * have to allow this. Otherwise we may announce a window too large
280 * for the current meta-level sk_rcvbuf.
281 */
u32 cur_win = tcp_receive_window(mptcp(tp) ? tcp_sk(mptcp_meta_sk(sk)) : tp);
u32 new_win = tp->__select_window(sk);
对于第283行的__select_window()函数,MPTCP的内核实现如下:
 
"net/mptcp/mptcp_output.c" line  of
u32 __mptcp_select_window(struct sock *sk)
{
struct inet_connection_sock *icsk = inet_csk(sk);
struct tcp_sock *tp = tcp_sk(sk), *meta_tp = mptcp_meta_tp(tp);
struct sock *meta_sk = mptcp_meta_sk(sk);
int mss, free_space, full_space, window; /* MSS for the peer's data. Previous versions used mss_clamp
779 * here. I don't know if the value based on our guesses
780 * of peer's MSS is better for the performance. It's more correct
781 * but may be worse for the performance because of rcv_mss
782 * fluctuations. --SAW 1998/11/1
783 */
mss = icsk->icsk_ack.rcv_mss;
free_space = tcp_space(meta_sk);
full_space = min_t(int, meta_tp->window_clamp,
tcp_full_space(meta_sk)); if (mss > full_space)
mss = full_space; if (free_space < (full_space >> )) {
icsk->icsk_ack.quick = ; if (tcp_memory_pressure)
/* TODO this has to be adapted when we support different
797 * MSS's among the subflows.
798 */
meta_tp->rcv_ssthresh = min(meta_tp->rcv_ssthresh,
4U * meta_tp->advmss); if (free_space < mss)
return ;
} if (free_space > meta_tp->rcv_ssthresh)
free_space = meta_tp->rcv_ssthresh; /* Don't do rounding if we are using window scaling, since the
810 * scaled window will not line up with the MSS boundary anyway.
811 */
window = meta_tp->rcv_wnd;
if (tp->rx_opt.rcv_wscale) {
window = free_space; /* Advertise enough space so that it won't get scaled away.
817 * Import case: prevent zero window announcement if
818 * 1<<rcv_wscale > mss.
819 */
if (((window >> tp->rx_opt.rcv_wscale) << tp->
rx_opt.rcv_wscale) != window)
window = (((window >> tp->rx_opt.rcv_wscale) + )
<< tp->rx_opt.rcv_wscale);
} else {
/* Get the largest window that is a nice multiple of mss.
826 * Window clamp already applied above.
827 * If our current window offering is within 1 mss of the
828 * free space we just keep it. This prevents the divide
829 * and multiply from happening most of the time.
830 * We also don't do any window rounding when the free space
831 * is too small.
832 */
if (window <= free_space - mss || window > free_space)
window = (free_space / mss) * mss;
else if (mss == full_space &&
free_space > window + (full_space >> ))
window = free_space;
} return window;
}
影响window的计算的因素为:
  1. 收到的MSS( icsk->icsk_ack.rcv_mss)
  2. 套接字缓冲区总的空间(tcp_full_space)
  3. 套接字缓冲区的空闲空间(tcp_space)
  4. meta_tp->rcv_ssthresh  /* Current window clamp */
观察上面的代码可以知道MPTCP的实现和__tcp_select_window的区别是都是依据meta_tp,而非tp。这说明
master sock 和 其余slave sock使用相同的 rcv_wnd。
 
结论:
1.master sock 和 其余slave sock使用相同的接收缓冲区和 rcv_wnd。
上一篇:基于redis的分布式ID生成器


下一篇:linux系统时间修改及同步