Linux两种方法来处理传入TCP数据段:快速路径(Fast Path)和慢速路径(Slow Path)

来源:互联网 发布:淘宝美工入门教程 编辑:程序博客网 时间:2024/06/15 16:33

        在Linux中,有两种方法来处理传入TCP数据段:快速路径(Fast Path)和慢速路径(Slow Path)。使用快速路径只进行最少的处理,如处理数据段、发生ACK、存储时间戳等。使用慢速路径可以处理乱序数据段、PAWS、socket内存管理和紧急数据等。Linux通过预测标志来区分这两种处理模式,预测标志存储在tp->pred_flags,生成这个标志的函数是__tcp_fast_path_on和tcp_fast_path_on,TCP会直接使用这两个函数来生成预测标记,也可以调用tcp_fast_path_check来完成这一任务:

[cpp] view plain copy
  1. 613 static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)  
  2. 614 {     
  3. 615     tp->pred_flags = htonl((tp->tcp_header_len << 26) |  
  4. 616                    ntohl(TCP_FLAG_ACK) |  
  5. 617                    snd_wnd);  
  6. 618 }     
  7. 619           
  8. 620 static inline void tcp_fast_path_on(struct tcp_sock *tp)  
  9. 621 {     
  10. 622     __tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);  
  11. 623 }     
  12. 624           
  13. 625 static inline void tcp_fast_path_check(struct sock *sk)  
  14. 626 {     
  15. 627     struct tcp_sock *tp = tcp_sk(sk);  
  16. 628           
  17. 629     if (skb_queue_empty(&tp->out_of_order_queue) &&  
  18. 630         tp->rcv_wnd &&  
  19. 631         atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&  
  20. 632         !tp->urg_data)  
  21. 633         tcp_fast_path_on(tp);  
  22. 634 }     
        613-617:__tcp_fast_path_on函数做的工作实际上是在构建TCP首部的第4个字节,即首部长度、标记位、窗口(格式见1.2节)。其中tp->tcp_header_len是首部长度的字节数,TCP首部中记录首部长度的数值位于第4个字节的高4bit,即第28-31bit,而且这个数值乘以4才是首部长度的字节数。故tp->tcp_header_len需要左移28位,再右移2位(除以4),即左移26位。
        620-622:tcp_fast_path_on封装了__tcp_fast_path_on函数,它在设置预测标记时会考虑窗口扩大因子的影响

        625-633:tcp_fast_path_check会先检查条件是否满足,如果满足再设置预测标记。条件是:

(1)没有乱序数据(629)

(2)接收窗口不为0(630)

(3)接收缓存未耗尽(631)

(4)没有紧急数据(632)

        TCP直接调用__tcp_fast_path_on的时机是connect系统调用即将结束时:

[cpp] view plain copy
  1. 5291 void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)  
  2. 5292 {  
  3. ...  
  4. 5320     if (!tp->rx_opt.snd_wscale)  
  5. 5321         __tcp_fast_path_on(tp, tp->snd_wnd);  
  6. 5322     else             
  7. 5323         tp->pred_flags = 0;  
  8. ...  
        TCP直接调用tcp_fast_path_on的时机是收到三次握手中的ACK报文时:
[cpp] view plain copy
  1. 5600 int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,  
  2. 5601               const struct tcphdr *th, unsigned int len)  
  3. 5602 {  
  4. ...  
  5. 5678         int acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |  
  6. 5679                           FLAG_UPDATE_TS_RECENT) > 0;  
  7. 5680   
  8. 5681         switch (sk->sk_state) {  
  9. 5682         case TCP_SYN_RECV:  
  10. 5683             if (acceptable) {  
  11. ...  
  12. 5745                 tcp_fast_path_on(tp);  
  13. 5746             } else {  
  14. 5747                 return 1;  
  15. 5748             }  
  16. 5749             break;  
  17. ...  
        TCP直接调用tcp_fast_path_check的时机有3处:

(1)当读过紧急数据时;紧急数据是由慢速路径处理,需要保持在慢速路径模式直到收完紧急数据,然后就可以开启快速路径模式了。

[cpp] view plain copy
  1. 1545 int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,  
  2. 1546         size_t len, int nonblock, int flags, int *addr_len)  
  3. 1547 {  
  4. ...  
  5. 1874         if (tp->urg_data && after(tp->copied_seq, tp->urg_seq)) {  
  6. 1875             tp->urg_data = 0;  
  7. 1876             tcp_fast_path_check(sk);  
  8. 1877         }  
  9. ...  
(2)当发生方收到ACK并调用tcp_ack_update_window更新窗口时;通告窗口发生了变化,则必须更新预测标记,以免后续的输入报文因为窗口不符而进入慢速路径。
[cpp] view plain copy
  1. 3218 static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,  
  2. 3219                  u32 ack_seq)  
  3. 3220 {  
  4. 3221     struct tcp_sock *tp = tcp_sk(sk);  
  5. 3222     int flag = 0;  
  6. 3223     u32 nwin = ntohs(tcp_hdr(skb)->window);  
  7. 3224   
  8. 3225     if (likely(!tcp_hdr(skb)->syn))  
  9. 3226         nwin <<= tp->rx_opt.snd_wscale;  
  10. 3227   
  11. 3228     if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {  
  12. 3229         flag |= FLAG_WIN_UPDATE;  
  13. 3230         tcp_update_wl(tp, ack_seq);  
  14. 3231   
  15. 3232         if (tp->snd_wnd != nwin) {  //窗口有变化  
  16. 3233             tp->snd_wnd = nwin;  
  17. 3234   
  18. 3235             /* Note, it is the only place, where 
  19. 3236              * fast path is recovered for sending TCP. 
  20. 3237              */  
  21. 3238             tp->pred_flags = 0;  
  22. 3239             tcp_fast_path_check(sk);  
  23. ...  
(3)当tcp_data_queue将数据放入接收队列时;这时可用的接收缓存大小发生变化,tcp_fast_path_check会检查这个缓存的变化是否允许开启快速路径模式。
[cpp] view plain copy
  1. 4300 static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)  
  2. 4301 {  
  3. ...  
  4. 4321     if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {  
  5. 4322         if (tcp_receive_window(tp) == 0)  
  6. 4323             goto out_of_window;  
  7. 4324   
  8. ...  
  9. 4371         tcp_fast_path_check(sk);  
  10. ...  
        设置了预测标记后,使用它是在处理TCP数据段的唯一入口函数——tcp_rcv_established:
[cpp] view plain copy
  1. 5076 int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,  
  2. 5077             const struct tcphdr *th, unsigned int len)  
  3. 5078 {                 
  4. 5079     struct tcp_sock *tp = tcp_sk(sk);  
  5. ...  
  6. 5109     if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&  //预测标记匹配  
  7. 5110         TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&  //包的序列号恰好是本端期望接收的  
  8. 5111         !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {  //确认号没有超出本端最新发送的数据的序列号  
  9. 5112         int tcp_header_len = tp->tcp_header_len;  
  10. 5113 //进入快速处理路径  
  11. 5114         /* Timestamp header prediction: tcp_header_len 
  12. 5115          * is automatically equal to th->doff*4 due to pred_flags 
  13. 5116          * match. 
  14. 5117          */  
  15. 5118  
  16. 5119         /* Check timestamp */  
  17. 5120         if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {//TCP头可能有时间戳选项  
  18. 5121             /* No? Slow path! */  
  19. 5122             if (!tcp_parse_aligned_timestamp(tp, th))//解析时间戳选项失败  
  20. 5123                 goto slow_path;  
  21. 5124  
  22. 5125             /* If PAWS failed, check it more carefully in slow path */  
  23. 5126             if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)//此包发送的时间比上个包早,可能是旧包,需仔细检查  
  24. 5127                 goto slow_path;  
  25. 5128  
  26. 5129             /* DO NOT update ts_recent here, if checksum fails 
  27. 5130              * and timestamp was corrupted part, it will result 
  28. 5131              * in a hung connection since we will drop all 
  29. 5132              * future packets due to the PAWS test. 
  30. 5133              */  
  31. 5134         }  
  32. 5135  
  33. 5136         if (len <= tcp_header_len) {  
  34. 5137             /* Bulk data transfer: sender */  
  35. 5138             if (len == tcp_header_len) {//包中无数据  
  36. 5139                 /* Predicted packet is in window by definition. 
  37. 5140                  * seq == rcv_nxt and rcv_wup <= rcv_nxt. 
  38. 5141                  * Hence, check seq<=rcv_wup reduces to: 
  39. 5142                  */  
  40. 5143                 if (tcp_header_len ==  
  41. 5144                     (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&//有时间戳选项  
  42. 5145                     tp->rcv_nxt == tp->rcv_wup)//期望接收的序列号与最后一次发送数据但时一致  
  43. 5146                     tcp_store_ts_recent(tp);//更新时间戳  
  44. 5147  
  45. 5148                 /* We know that such packets are checksummed 
  46. 5149                  * on entry. 
  47. 5150                  */  
  48. 5151                 tcp_ack(sk, skb, 0);//处理ACK标记  
  49. 5152                 __kfree_skb(skb);  
  50. 5153                 tcp_data_snd_check(sk);//发送发送缓存中尚未发送的包  
  51. 5154                 return 0;  
  52. 5155             } else { /* Header too small */  
  53. 5156                 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);  
  54. 5157                 goto discard;  
  55. 5158             }  
  56. 5159         } else {//包中有数据  
  57. 5160             int eaten = 0;  
  58. 5161             int copied_early = 0;  
  59. 5162             bool fragstolen = false;  
  60. 5163  
  61. 5164             if (tp->copied_seq == tp->rcv_nxt &&//接收缓存中没有数据  
  62. 5165                 len - tcp_header_len <= tp->ucopy.len) {//当前包中的数据能够放入用户缓存中;tp->ucopy.len > 0意味着有进程等待收包  
  63. 5166 #ifdef CONFIG_NET_DMA  
  64. 5167                 if (tp->ucopy.task == current &&  
  65. 5168                     sock_owned_by_user(sk) &&  
  66. 5169                     tcp_dma_try_early_copy(sk, skb, tcp_header_len)) {  
  67. 5170                     copied_early = 1;  
  68. 5171                     eaten = 1;  
  69. 5172                 }  
  70. 5173 #endif  
  71. 5174                 if (tp->ucopy.task == current &&//当前是在进程上下文中运行  
  72. 5175                     sock_owned_by_user(sk) && !copied_early) {  
  73. 5176                     __set_current_state(TASK_RUNNING);  
  74. 5177  
  75. 5178                     if (!tcp_copy_to_iovec(sk, skb, tcp_header_len))//copy数据到用户缓存中,不必交付接收缓存  
  76. 5179                         eaten = 1;  
  77. 5180                 }  
  78. 5181                 if (eaten) {//copy成功  
  79. 5182                     /* Predicted packet is in window by definition. 
  80. 5183                      * seq == rcv_nxt and rcv_wup <= rcv_nxt. 
  81. 5184                      * Hence, check seq<=rcv_wup reduces to: 
  82. 5185                      */  
  83. 5186                     if (tcp_header_len ==  
  84. 5187                         (sizeof(struct tcphdr) +  
  85. 5188                          TCPOLEN_TSTAMP_ALIGNED) &&  
  86. 5189                         tp->rcv_nxt == tp->rcv_wup)  
  87. 5190                         tcp_store_ts_recent(tp);  
  88. 5191  
  89. 5192                     tcp_rcv_rtt_measure_ts(sk, skb);  
  90. 5193  
  91. 5194                     __skb_pull(skb, tcp_header_len);  
  92. 5195                     tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;  
  93. 5196                     NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITSTOUSER);  
  94. 5197                 }  
  95. 5198                 if (copied_early)  
  96. 5199                     tcp_cleanup_rbuf(sk, skb->len);  
  97. 5200             }  
  98. 5201             if (!eaten) {//没有被提前copy进用户缓存  
  99. 5202                 if (tcp_checksum_complete_user(sk, skb)) //检验和校验失败  
  100. 5203                     goto csum_error;  
  101. 5204  
  102. 5205                 if ((int)skb->truesize > sk->sk_forward_alloc) //skb的大小超过了发送队列中可以直接使用的空间大小(即已经分配了但尚未使用的空间)  
  103. 5206                     goto step5;  
  104. 5207  
  105. 5208                 /* Predicted packet is in window by definition. 
  106. 5209                  * seq == rcv_nxt and rcv_wup <= rcv_nxt. 
  107. 5210                  * Hence, check seq<=rcv_wup reduces to: 
  108. 5211                  */  
  109. 5212                 if (tcp_header_len ==  
  110. 5213                     (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&  
  111. 5214                     tp->rcv_nxt == tp->rcv_wup)  
  112. 5215                     tcp_store_ts_recent(tp);  
  113. 5216  
  114. 5217                 tcp_rcv_rtt_measure_ts(sk, skb);  
  115. 5218  
  116. 5219                 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITS);  
  117. 5220  
  118. 5221                 /* Bulk data transfer: receiver */  
  119. 5222                 eaten = tcp_queue_rcv(sk, skb, tcp_header_len,  
  120. 5223                               &fragstolen);//将当前包放入接收队列中;此包并非乱序包,可以直接放入接收队列  
  121. 5224             }  
  122. 5225  
  123. 5226             tcp_event_data_recv(sk, skb);  
  124. 5227  
  125. 5228             if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {  
  126. 5229                 /* Well, only one small jumplet in fast path... */  
  127. 5230                 tcp_ack(sk, skb, FLAG_DATA);  
  128. 5231                 tcp_data_snd_check(sk);  
  129. 5232                 if (!inet_csk_ack_scheduled(sk))  
  130. 5233                     goto no_ack;  
  131. 5234             }  
  132. 5235  
  133. 5236             if (!copied_early || tp->rcv_nxt != tp->rcv_wup)  
  134. 5237                 __tcp_ack_snd_check(sk, 0);  
  135. 5238 no_ack:  
  136. 5239 #ifdef CONFIG_NET_DMA  
  137. 5240             if (copied_early)  
  138. 5241                 __skb_queue_tail(&sk->sk_async_wait_queue, skb);  
  139. 5242             else  
  140. 5243 #endif  
  141. 5244             if (eaten)  
  142. 5245                 kfree_skb_partial(skb, fragstolen);  
  143. 5246             sk->sk_data_ready(sk, 0);//调用sock_def_readable函数通知进程有数据可读  
  144. 5247             return 0;  
  145. 5248         }  
  146. 5249     }  
  147. 5250  
  148. 5251 slow_path://慢速路径  
  149. ...  

        5109:这样是检查输入的TCP报文是否与预测标志匹配:

[cpp] view plain copy
  1.  64 union tcp_word_hdr {   
  2.  65     struct tcphdr hdr;  
  3.  66     __be32        words[5];  
  4.  67 };  
  5.  68   
  6.  69 #define tcp_flag_word(tp) ( ((union tcp_word_hdr *)(tp))->words [3])  
  7.  70   
  8.  71 enum {   
  9.  72     TCP_FLAG_CWR = __constant_cpu_to_be32(0x00800000),  
  10.  73     TCP_FLAG_ECE = __constant_cpu_to_be32(0x00400000),  
  11.  74     TCP_FLAG_URG = __constant_cpu_to_be32(0x00200000),  
  12.  75     TCP_FLAG_ACK = __constant_cpu_to_be32(0x00100000),  
  13.  76     TCP_FLAG_PSH = __constant_cpu_to_be32(0x00080000),  
  14.  77     TCP_FLAG_RST = __constant_cpu_to_be32(0x00040000),  
  15.  78     TCP_FLAG_SYN = __constant_cpu_to_be32(0x00020000),  
  16.  79     TCP_FLAG_FIN = __constant_cpu_to_be32(0x00010000),  
  17.  80     TCP_RESERVED_BITS = __constant_cpu_to_be32(0x0F000000),  
  18.  81     TCP_DATA_OFFSET = __constant_cpu_to_be32(0xF0000000)  
  19.  82 };  
        而TCP_HP_BITS的定义为:
[cpp] view plain copy
  1. 122 #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))  
        可以看出,5109行的匹配方法是将TCP首部的第4个字节去掉TCP_FLAG_PSH标记后再与预测标记对比,只有首部长度和窗口大小与预测标记一致且标记位仅有TCP_FLAG_ACK的包才能通过。除了预测标记匹配通过外,当前包序列号和确认号还需要满足5110和5111行的要求才能进入快速处理路径。

  5120-5127:如果报文中携带时间戳选项,则需要解析时间戳然后检查是否有序列号回绕(PAWS)的问题。如果时间戳选项解析失败或PAWS检查失败,则需要进入慢速路径仔细检查

  5136-5157是对没有数据部分的报文的处理。

  5143-5144:TCP首部中有时间戳选项

  5145:这个条件满足意味着当前包是对最新一次发送的数据包的回应

  5146:这时需要更新时间戳信息

  5151-5152:调用tcp_ack处理完ACK标记后,这个没有数据的包就已经完成了使命,可以释放了

  5153:收到ACK后可能确认了一部分旧的数据,也可能更新了通告窗口,这时需要调用tcp_data_snd_check函数试着发送一下发送队列中的数据

  至此,快速处理路径中对无数据的包的处理完毕,下面是对有数据的包的处理。

  5164-5199:如果进程使用prequeue收包,则需要试图将数据直接copy到用户缓存;如果copy成功,则需要更新时间戳、设置tp->rcv_nxt等

  5201-5223:如果没有数据被直接copy到用户空间,则在进行了检验和校验、接收空间检查、时间戳保存等工作后,调用tcp_queue_rcv函数将skb放入接收队列中:

[cpp] view plain copy
  1. 4244 static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen,  
  2. 4245           bool *fragstolen)  
  3. 4246 {  
  4. 4247     int eaten;  
  5. 4248     struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue); //获取接收队列最尾部的skb  
  6. 4249   
  7. 4250     __skb_pull(skb, hdrlen);  
  8. 4251     eaten = (tail &&     //接收队列不为空  
  9. 4252          tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0; //试着将当前skb整合到从队列尾取得的skb中  
  10. 4253     tcp_sk(sk)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;  
  11. 4254     if (!eaten) {        //整合不成功  
  12. 4255         __skb_queue_tail(&sk->sk_receive_queue, skb);  
  13. 4256         skb_set_owner_r(skb, sk);        
  14. 4257     }  
  15. 4258     return eaten;          
  16. 4259 }  
  可见无论skb是否整合成功,tcp_queue_rcv函数最终会将skb的数据部分放入到sk->sk_receive_queue中。
  处理完skb的数据部分后,tcp_rcv_established函数会调用tcp_event_data_recv函数更新拥塞控制信息(包括拥塞窗口),然后处理ACK标记位:

  5228:满足这个判断条件意味着要么ACK会确认新的数据(TCP_SKB_CB(skb)->ack_seq > tp->snd_una),要么是一个旧的ACK(TCP_SKB_CB(skb)->ack_seq < tp->snd_una),这两种情况都需要调用tcp_ack进行处理,然后再试图发送一下发送队列中的数据。

  5236-5237:如果没有数据通过DMA直接copy给进程,或接收了新的数据,则发送ACK(立即发送或使用延迟ACK机制)。换个角度考虑,不发送ACK的条件是:有数据通过DMA直接copy给进程,且没有接收新的数据。也就是说,skb中的数据长度为0。但这样的话skb是走不到5159这个分支中的啊。我实在想不出使这个条件为假的情况。

  5240-5240:如果有数据通过DMA传输,则将skb放入sk_async_wait_queue中,以防DMA传输失败。

  5246:最后,调用sk->sk_data_ready指向的函数通知进程有数据可以接收。

  至此,快速路径处理完成,不满足快速处理条件的skb会被送入慢速处理路径。在那里,skb会接受更多更严格的检查与处理。


TCP报文段进入慢速路径处理的条件有:

(1)收到乱序数据或乱序队列非空

(2)收到紧急指针

(3)接收缓存耗尽

(4)收到0窗口通告

(5)收到的报文中含有除了PUSH和ACK之外的标记,如SYN、FIN、RST

(6)报文的时间戳选项解析失败

(7)报文的PAWS检查失败

  慢速路径处理的代码如下:

[cpp] view plain copy
  1. 5076 int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,  
  2. 5077             const struct tcphdr *th, unsigned int len)  
  3. 5078 {  
  4. 5079     struct tcp_sock *tp = tcp_sk(sk);  
  5. ...  
  6. 5251 slow_path:  
  7. 5252     if (len < (th->doff << 2) || tcp_checksum_complete_user(sk, skb)) //报文长度非法或检验和错误  
  8. 5253         goto csum_error;  
  9. 5254   
  10. 5255     if (!th->ack && !th->rst)  
  11. 5256         goto discard;  
  12. 5257   
  13. 5258     /* 
  14. 5259      *  Standard slow path. 
  15. 5260      */  
  16. 5261   
  17. 5262     if (!tcp_validate_incoming(sk, skb, th, 1)) //其它合法性检查  
  18. 5263         return 0;  
  19. 5264   
  20. 5265 step5:  
  21. 5266     if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0) //ACK非法,则丢弃之  
  22. 5267         goto discard;  
  23. 5268   
  24. 5269     tcp_rcv_rtt_measure_ts(sk, skb); //更新RTT估计量  
  25. 5270   
  26. 5271     /* Process urgent data. */  
  27. 5272     tcp_urg(sk, skb, th); //紧急指针  
  28. 5273   
  29. 5274     /* step 7: process the segment text */  
  30. 5275     tcp_data_queue(sk, skb); //处理数据,包括乱序数据  
  31. 5276   
  32. 5277     tcp_data_snd_check(sk); //试图发送队列中的数据  
  33. 5278     tcp_ack_snd_check(sk); //发送ACK  
  34. 5279     return 0;  
  35. ...  
  tcp_validate_incoming函数用于检查时间戳、序列号等字段:
[cpp] view plain copy
  1. 4985 static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,  
  2. 4986                   const struct tcphdr *th, int syn_inerr)  
  3. 4987 {  
  4. 4988     struct tcp_sock *tp = tcp_sk(sk);  
  5. 4989   
  6. 4990     /* RFC1323: H1. Apply PAWS check first. */  
  7. 4991     if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp && //解析选项成功并且时间戳选项开启  
  8. 4992         tcp_paws_discard(sk, skb)) { //检查序列号回绕  
  9. 4993         if (!th->rst) {  
  10. 4994             NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);  
  11. 4995             tcp_send_dupack(sk, skb); //立即发送重复ACK  
  12. 4996             goto discard;  //丢弃之  
  13. 4997         }  
  14. 4998         /* Reset is accepted even if it did not pass PAWS. */  
  15. 4999     }  
  16. 5000   
  17. 5001     /* Step 1: check sequence number */  
  18. 5002     if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {  
  19. 5003         /* RFC793, page 37: "In all states except SYN-SENT, all reset 
  20. 5004          * (RST) segments are validated by checking their SEQ-fields." 
  21. 5005          * And page 69: "If an incoming segment is not acceptable, 
  22. 5006          * an acknowledgment should be sent in reply (unless the RST 
  23. 5007          * bit is set, if so drop the segment and return)". 
  24. 5008          */  
  25. 5009         if (!th->rst) {  
  26. 5010             if (th->syn)  
  27. 5011                 goto syn_challenge;  
  28. 5012             tcp_send_dupack(sk, skb);  
  29. 5013         }  
  30. 5014         goto discard;  
  31. 5015     }  
  32. 5016   
  33. 5017     /* Step 2: check RST bit */  
  34. 5018     if (th->rst) {  
  35. 5019         /* RFC 5961 3.2 : 
  36. 5020          * If sequence number exactly matches RCV.NXT, then 
  37. 5021          *     RESET the connection 
  38. 5022          * else 
  39. 5023          *     Send a challenge ACK 
  40. 5024          */  
  41. 5025         if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt)  
  42. 5026             tcp_reset(sk);  //处理RST标记,设置TCP状态机,释放部分资源  
  43. 5027         else  
  44. 5028             tcp_send_challenge_ack(sk); //发送ACK挑战,以应对Blind In-Window Attack  
  45. 5029         goto discard;  
  46. 5030     }  
  47. 5031   
  48. 5032     /* step 3: check security and precedence [ignored] */  
  49. 5033   
  50. 5034     /* step 4: Check for a SYN 
  51. 5035      * RFC 5691 4.2 : Send a challenge ack 
  52. 5036      */  
  53. 5037     if (th->syn) {  
  54. 5038 syn_challenge:  
  55. 5039         if (syn_inerr)  
  56. 5040             TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);  
  57. 5041         NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);  
  58. 5042         tcp_send_challenge_ack(sk);  
  59. 5043         goto discard;  
  60. 5044     }  
  61. 5045   
  62. 5046     return true;  
  63. 5047   
  64. 5048 discard:  
  65. 5049     __kfree_skb(skb);  
  66. 5050     return false;  
  67. 5051 }  

  4991-4996:如果有时间戳选项的话检查PAWS,不通过则认为是旧包,丢弃之,并立即发送ACK;不过RST包即使PAWS检查不过也是可以接受的,并不丢弃。

  5002-5014:tcp_sequence函数用来确保由数据的序列号落入接收窗口之内,否则丢弃之;但如果SYN包的序列号非法则需要发送ACK挑战(原因见RFC5961)。

  5018-5046:按照RFC5961的要求处理SYN报文和RST报文,以防止Blind In-Window Attack。

  Blind In-Window Attack简介:这个攻击的基本原理是攻击者通过发送伪造的RST包或SYN包导致TCP通信两端中的一个认为包非法而发送RST,从而异常结束连接。而这个攻击能够奏效的前提是所伪造的包的序列号必须在窗口范围内,要保证这一点攻击者必须发送许多攻击包进行猜测,因此得名。防御这种攻击的基本方法是,对于RST包,仔细检查其序列号,只有其序列号真正等于tp->rcv_nxt时才发送RST(攻击者能够伪造出符合这一特征的RST包的概率是很低的);而对于建立状态下SYN包,只是发送ACK,不发RST。

  慢速处理路径中ACK的处理、拥塞控制信息的更新以及紧急指针在后续章节中详细讨论。

  数据接收处理在tcp_data_queue函数中进行:

[cpp] view plain copy
  1. 4300 static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)  
  2. 4301 {  
  3. 4302     const struct tcphdr *th = tcp_hdr(skb);  
  4. 4303     struct tcp_sock *tp = tcp_sk(sk);  
  5. 4304     int eaten = -1;  
  6. 4305     bool fragstolen = false;  
  7. 4306  
  8. 4307     if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq)//没有数据  
  9. 4308         goto drop;  
  10. 4309  
  11. 4310     skb_dst_drop(skb);  
  12. 4311     __skb_pull(skb, th->doff * 4); //skb->data指向数据段  
  13. 4312  
  14. 4313     TCP_ECN_accept_cwr(tp, skb);     
  15. 4314  
  16. 4315     tp->rx_opt.dsack = 0;  
  17. 4316  
  18. 4317     /*  Queue data for delivery to the user. 
  19. 4318      *  Packets in sequence go to the receive queue. 
  20. 4319      *  Out of sequence packets to the out_of_order_queue. 
  21. 4320      */  
  22. 4321     if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {//正序包  
  23. 4322         if (tcp_receive_window(tp) == 0) //接收窗口无法容纳新数据  
  24. 4323             goto out_of_window;  
  25. 4324  
  26. 4325         /* Ok. In sequence. In window. */  
  27. 4326         if (tp->ucopy.task == current && //处于进程上下文中;这时应该是进程调用release_sock函数然后进入了tcp_v4_do_rcv  
  28. 4327             tp->copied_seq == tp->rcv_nxt && tp->ucopy.len &&//接收队列中没有数据且用户缓存长度不为0  
  29. 4328             sock_owned_by_user(sk) && !tp->urg_data) {//进程正在系统调用中访问socket且没有收到紧急指针  
  30. 4329             int chunk = min_t(unsigned int, skb->len,  
  31. 4330                       tp->ucopy.len);  
  32. 4331  
  33. 4332             __set_current_state(TASK_RUNNING);  
  34. 4333  
  35. 4334             local_bh_enable();//必须开启软中断,因为从内核向用户态缓存copy数据可能会睡眠,在禁用软中断的条件下是不允许的  
  36. 4335             if (!skb_copy_datagram_iovec(skb, 0, tp->ucopy.iov, chunk)) {//将skb中的数据copy到用户缓存中  
  37. 4336                 tp->ucopy.len -= chunk;  
  38. 4337                 tp->copied_seq += chunk;  
  39. 4338                 eaten = (chunk == skb->len);  
  40. 4339                 tcp_rcv_space_adjust(sk);  
  41. 4340             }  
  42. 4341             local_bh_disable();  
  43. 4342         }  
  44. 4343  
  45. 4344         if (eaten <= 0) {//skb中的数据没有被全部copy完毕  
  46. 4345 queue_and_out:  
  47. 4346             if (eaten < 0 &&  
  48. 4347                 tcp_try_rmem_schedule(sk, skb, skb->truesize))//检查接收缓存空间是否能容纳skb  
  49. 4348                 goto drop;  
  50. 4349  
  51. 4350             eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);//将skb放入接收队列中;如果是与队列尾部的skb合并则eaten为1  
  52. 4351         }  
  53. 4352         tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;  
  54. 4353         if (skb->len)  
  55. 4354             tcp_event_data_recv(sk, skb);  
  56. 4355         if (th->fin)//FIN包  
  57. 4356             tcp_fin(sk);//处理FIN  
  58. 4357  
  59. 4358         if (!skb_queue_empty(&tp->out_of_order_queue)) {//乱序队列非空  
  60. 4359             tcp_ofo_queue(sk);//整理乱序队列,查看是否能将乱序队列中的包放入接收队列  
  61. 4360  
  62. 4361             /* RFC2581. 4.2. SHOULD send immediate ACK, when 
  63. 4362              * gap in queue is filled. 
  64. 4363              */  
  65. 4364             if (skb_queue_empty(&tp->out_of_order_queue))  
  66. 4365                 inet_csk(sk)->icsk_ack.pingpong = 0;  
  67. 4366         }  
  68. 4367  
  69. 4368         if (tp->rx_opt.num_sacks)  
  70. 4369             tcp_sack_remove(tp);  
  71. 4370  
  72. 4371         tcp_fast_path_check(sk);  //检查是否可以开启快速处理路径  
  73. 4372  
  74. 4373         if (eaten > 0)//将skb与队列尾部的skb合并  
  75. 4374             kfree_skb_partial(skb, fragstolen);  
  76. 4375         if (!sock_flag(sk, SOCK_DEAD))//将整个skb放入接收队列中  
  77. 4376             sk->sk_data_ready(sk, 0);//通知进程接收数据  
  78. 4377         return;  
  79. 4378     }  
  80. 4379 //非正序包  
  81. 4380     if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {//旧包  
  82. 4381         /* A retransmit, 2nd most common case.  Force an immediate ack. */  
  83. 4382         NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);  
  84. 4383         tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);  
  85. 4384  
  86. 4385 out_of_window:  
  87. 4386         tcp_enter_quickack_mode(sk);  
  88. 4387         inet_csk_schedule_ack(sk);//标识此socket正在等待发送ACK,如果以后有数据要发送的话会尽快发送,以便将携带的ACK尽快发送到对端  
  89. 4388 drop:  
  90. 4389         __kfree_skb(skb);  
  91. 4390         return;  
  92. 4391     }  
  93. 4392  
  94. 4393     /* Out of window. F.e. zero window probe. */  
  95. 4394     if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))  
  96. 4395         goto out_of_window;  
  97. 4396  
  98. 4397     tcp_enter_quickack_mode(sk);  
  99. 4398  
  100. 4399     if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {//部分是旧数据,部分是新数据  
  101. 4400         /* Partial packet, seq < rcv_next < end_seq */  
  102. 4401         SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",  
  103. 4402                tp->rcv_nxt, TCP_SKB_CB(skb)->seq,  
  104. 4403                TCP_SKB_CB(skb)->end_seq);  
  105. 4404  
  106. 4405         tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);  
  107. 4406  
  108. 4407         /* If window is closed, drop tail of packet. But after 
  109. 4408          * remembering D-SACK for its head made in previous line. 
  110. 4409          */  
  111. 4410         if (!tcp_receive_window(tp))  
  112. 4411             goto out_of_window;  
  113. 4412         goto queue_and_out;//将skb放入接收队列中  
  114. 4413     }  
  115. 4414  
  116. 4415     tcp_data_queue_ofo(sk, skb);//处理乱序包  
  117. 4416 }  
  乱序数据的重组由tcp_ofo_queue函数完成:
[cpp] view plain copy
  1. 4023 static void tcp_ofo_queue(struct sock *sk)  
  2. 4024 {  
  3. 4025     struct tcp_sock *tp = tcp_sk(sk);  
  4. 4026     __u32 dsack_high = tp->rcv_nxt;  
  5. 4027     struct sk_buff *skb;  
  6. 4028  
  7. 4029     while ((skb = skb_peek(&tp->out_of_order_queue)) != NULL) {//取乱序队列首包  
  8. 4030         if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))//空洞仍然没有填满  
  9. 4031             break;  
  10. 4032  
  11. 4033         if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {//包中有数据被放入接收缓存了  
  12. 4034             __u32 dsack = dsack_high;        
  13. 4035             if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))//包中的数据已经全部被放入接收缓存了  
  14. 4036                 dsack_high = TCP_SKB_CB(skb)->end_seq;  
  15. 4037             tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);  
  16. 4038         }  
  17. 4039  
  18. 4040         if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {//包中的数据已经全部被放入接收缓存了  
  19. 4041             SOCK_DEBUG(sk, "ofo packet was already received\n");  
  20. 4042             __skb_unlink(skb, &tp->out_of_order_queue);  
  21. 4043             __kfree_skb(skb);  
  22. 4044             continue;  
  23. 4045         }  
  24. 4046         SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n",  
  25. 4047                tp->rcv_nxt, TCP_SKB_CB(skb)->seq,  
  26. 4048                TCP_SKB_CB(skb)->end_seq);  
  27. 4049         //空洞被填满,包可以放入接收队列中  
  28. 4050         __skb_unlink(skb, &tp->out_of_order_queue);  
  29. 4051         __skb_queue_tail(&sk->sk_receive_queue, skb);  
  30. 4052         tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;  
  31. 4053         if (tcp_hdr(skb)->fin)  
  32. 4054             tcp_fin(sk);  
  33. 4055     }  
  34. 4056 }  
  tcp_data_queue_ofo负责将乱序包按照seq顺序放入乱序队列中,并设置SACK选项信息:
[cpp] view plain copy
  1. 4121 static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)  
  2. 4122 {  
  3. 4123     struct tcp_sock *tp = tcp_sk(sk);  
  4. 4124     struct sk_buff *skb1;  
  5. 4125     u32 seq, end_seq;      
  6. 4126  
  7. 4127     TCP_ECN_check_ce(tp, skb);  
  8. 4128  
  9. 4129     if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) {//检查接收缓存空间是否能容纳skb  
  10. 4130         NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFODROP);  
  11. 4131         __kfree_skb(skb);  
  12. 4132         return;  
  13. 4133     }  
  14. 4134  
  15. 4135     /* Disable header prediction. */  
  16. 4136     tp->pred_flags = 0;  
  17. 4137     inet_csk_schedule_ack(sk);//标识此socket正在等待发送ACK,如果以后有数据要发送的话会尽快发送,以便将携带的ACK尽快发送到对端  
  18. 4138  
  19. 4139     NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);  
  20. 4140     SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n",  
  21. 4141            tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);  
  22. 4142  
  23. 4143     skb1 = skb_peek_tail(&tp->out_of_order_queue);  
  24. 4144     if (!skb1) {//乱序队列中没有数据  
  25. 4145         /* Initial out of order segment, build 1 SACK. */  
  26. 4146         if (tcp_is_sack(tp)) {  
  27. 4147             tp->rx_opt.num_sacks = 1;  
  28. 4148             tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq;  
  29. 4149             tp->selective_acks[0].end_seq =  
  30. 4150                         TCP_SKB_CB(skb)->end_seq;  
  31. 4151         }  
  32. 4152         __skb_queue_head(&tp->out_of_order_queue, skb);//将skb放入乱序队列中  
  33. 4153         goto end;  
  34. 4154     }  
  35. 4155  
  36. 4156     seq = TCP_SKB_CB(skb)->seq;  
  37. 4157     end_seq = TCP_SKB_CB(skb)->end_seq;  
  38. 4158  
  39. 4159     if (seq == TCP_SKB_CB(skb1)->end_seq) {//当前skb与队列中end_seq最大的数据无缝拼接  
  40. 4160         bool fragstolen;  
  41. 4161  
  42. 4162         if (!tcp_try_coalesce(sk, skb1, skb, &fragstolen)) {//看能否将skb合并到skb1中  
  43. 4163             __skb_queue_after(&tp->out_of_order_queue, skb1, skb);//如果不能则将skb房子skb1的后面  
  44. 4164         } else {  
  45. 4165             kfree_skb_partial(skb, fragstolen);  
  46. 4166             skb = NULL;  
  47. 4167         }  
  48. 4168  
  49. 4169         if (!tp->rx_opt.num_sacks ||  
  50. 4170             tp->selective_acks[0].end_seq != seq)  
  51. 4171             goto add_sack;  
  52. 4172  
  53. 4173         /* Common case: data arrive in order after hole. */  
  54. 4174         tp->selective_acks[0].end_seq = end_seq;  
  55. 4175         goto end;  
  56. 4176     }  
  57. 4177  
  58. 4178     /* Find place to insert this segment. */  
  59. 4179     while (1) {//找到合适的地方插入skb  
  60. 4180         if (!after(TCP_SKB_CB(skb1)->seq, seq))//seq1 <= seq  
  61. 4181             break;//找到第一个序列号小于等于skb的包  
  62. 4182         if (skb_queue_is_first(&tp->out_of_order_queue, skb1)) {  
  63. 4183             skb1 = NULL;  
  64. 4184             break;//skb的序列号最小  
  65. 4185         }  
  66. 4186         skb1 = skb_queue_prev(&tp->out_of_order_queue, skb1);  
  67. 4187     }  
  68. 4188  
  69. 4189     /* Do skb overlap to previous one? */  
  70. 4190     if (skb1 && before(seq, TCP_SKB_CB(skb1)->end_seq)) {//seq >= seq1 && seq < end_seq1  
  71. 4191         if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {//end_seq <= end_seq1  
  72. 4192             /* All the bits are present. Drop. */    //skb的数据完全被包含在skb1中  
  73. 4193             NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE);  
  74. 4194             __kfree_skb(skb);  
  75. 4195             skb = NULL;  
  76. 4196             tcp_dsack_set(sk, seq, end_seq);  
  77. 4197             goto add_sack;  
  78. 4198         }  
  79. 4199         if (after(seq, TCP_SKB_CB(skb1)->seq)) {//seq > seq1 && end_seq > end_seq1  
  80. 4200             /* Partial overlap. */  
  81. 4201             tcp_dsack_set(sk, seq,  
  82. 4202                       TCP_SKB_CB(skb1)->end_seq);  
  83. 4203         } else { //seq == seq1 && end_seq > end_seq1  
  84. 4204             if (skb_queue_is_first(&tp->out_of_order_queue,  
  85. 4205                            skb1))  
  86. 4206                 skb1 = NULL;  
  87. 4207             else  
  88. 4208                 skb1 = skb_queue_prev(  
  89. 4209                     &tp->out_of_order_queue,  
  90. 4210                     skb1);  
  91. 4211         }  
  92. 4212     }  
  93. 4213     if (!skb1)  
  94. 4214         __skb_queue_head(&tp->out_of_order_queue, skb);  
  95. 4215     else  
  96. 4216         __skb_queue_after(&tp->out_of_order_queue, skb1, skb);  
  97. 4217  
  98. 4218     /* And clean segments covered by new one as whole. */  
  99. 4219     while (!skb_queue_is_last(&tp->out_of_order_queue, skb)) {  
  100. 4220         skb1 = skb_queue_next(&tp->out_of_order_queue, skb);  
  101. 4221  
  102. 4222         if (!after(end_seq, TCP_SKB_CB(skb1)->seq))//end_seq <= seq1;skb与其后续skb没有重叠数据  
  103. 4223             break;  
  104. 4224         if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {//end_seq > seq1 && end_seq < end_seq1;skb与其后续skb有部分重叠数据但并不完全覆盖后续skb的数据  
  105. 4225             tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,  
  106. 4226                      end_seq);  
  107. 4227             break;  
  108. 4228         }//skb完全包含了其后续skb的数据,需要释放被完全重叠的skb  
  109. 4229         __skb_unlink(skb1, &tp->out_of_order_queue);  
  110. 4230         tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,  
  111. 4231                  TCP_SKB_CB(skb1)->end_seq);  
  112. 4232         NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE);  
  113. 4233         __kfree_skb(skb1);  
  114. 4234     }  
  115. 4235  
  116. 4236 add_sack:  
  117. 4237     if (tcp_is_sack(tp))  
  118. 4238         tcp_sack_new_ofo_skb(sk, seq, end_seq);  
  119. 4239 end:  
  120. 4240     if (skb)  
  121. 4241         skb_set_owner_r(skb, sk);  
  122. 4242   

  4129:如果TCP接收缓存的空间紧张,乱序队列中的数据是可以被删除的(反正也没确认,不删白不删):

[cpp] view plain copy
  1. 4061 static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,  
  2. 4062                  unsigned int size)  
  3. 4063 {  
  4. 4064     if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || //接收缓存分配超量  
  5. 4065         !sk_rmem_schedule(sk, skb, size)) { //全局缓存或接收缓存空间达到上限  
  6. 4066   
  7. 4067         if (tcp_prune_queue(sk) < 0) //释放一部分缓存,必要时会清空乱序队列  
  8. 4068             return -1;  
  9. 4069   
  10. 4070         if (!sk_rmem_schedule(sk, skb, size)) { //经过释放后仍然超限  
  11. 4071             if (!tcp_prune_ofo_queue(sk)) //清除乱序队列中所有的包  
  12. 4072                 return -1;  
  13. 4073   
  14. 4074             if (!sk_rmem_schedule(sk, skb, size))  
  15. 4075                 return -1;  
  16. 4076         }  
  17. 4077     }  
  18. 4078     return 0;  
  19. 4079 }  

  数据包放入接收队列进程后,进程就可以使用系统调用将包中的数据copy到用户缓存中,最终完成TCP的数据接收。

  在慢速处理路径的最后部分,调用tcp_data_snd_check函数发送数据并检查接收缓存空间:

[cpp] view plain copy
  1. 4719 static void tcp_new_space(struct sock *sk)  
  2. 4720 {  
  3. 4721     struct tcp_sock *tp = tcp_sk(sk);  
  4. 4722   
  5. 4723     if (tcp_should_expand_sndbuf(sk)) {  //应该扩展发送缓存空间  
  6. 4724         int sndmem = SKB_TRUESIZE(max_t(u32,  
  7. 4725                         tp->rx_opt.mss_clamp,  
  8. 4726                         tp->mss_cache) +  
  9. 4727                       MAX_TCP_HEADER);  
  10. 4728         int demanded = max_t(unsigned int, tp->snd_cwnd,  
  11. 4729                      tp->reordering + 1);  
  12. 4730         sndmem *= 2 * demanded;  
  13. 4731         if (sndmem > sk->sk_sndbuf)  
  14. 4732             sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);  
  15. 4733         tp->snd_cwnd_stamp = tcp_time_stamp;  
  16. 4734     }  
  17. 4735   
  18. 4736     sk->sk_write_space(sk); //调用sock_def_write_space函数,如果有可用发送空间则通知(唤醒)进程  
  19. 4737 }  
  20. 4738       
  21. 4739 static void tcp_check_space(struct sock *sk)   
  22. 4740 {  
  23. 4741     if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) { //发送缓存空间曾经收缩过  
  24. 4742         sock_reset_flag(sk, SOCK_QUEUE_SHRUNK);  
  25. 4743         if (sk->sk_socket &&  
  26. 4744             test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) //发生过发送缓存耗尽事件,这意味着可能有进程在等待可用缓存空间  
  27. 4745             tcp_new_space(sk);  
  28. 4746     }  
  29. 4747 }  
  30. 4748       
  31. 4749 static inline void tcp_data_snd_check(struct sock *sk)  
  32. 4750 {     
  33. 4751     tcp_push_pending_frames(sk); //调用tcp_write_xmit发送数据  
  34. 4752     tcp_check_space(sk); //检查发送缓存空间  
  35. 4753 }  
  以上简要分析了TCP在慢速路径处理模式下的工作。下节我们着重关注一下TCP发送和接收ACK的细节。
0 0
原创粉丝点击