Linux TCP数据包接收处理

在接收流程一节中可以看到数据包在读取到用户空间前,都要经过tcp_v4_do_rcv处理,从而在receive queue中排队。

在该函数中,我们只分析当连接已经建立后的数据包处理流程,也即tcp_rcv_established函数。

 

tcp_rcv_established函数的工作原理是把数据包的处理分为2类:fast path和slow path,其含义显而易见。这样分类

的目的当然是加快数据包的处理,因为在正常情况下,数据包是按顺序到达的,网络状况也是稳定的,这时可以按照fast path

直接把数据包存放到receive queue了。而在其他的情况下则需要走slow path流程了。

 

在协议栈中,是用头部预测来实现的,每个tcp sock有个pred_flags成员,它就是判别的依据。

可以看出头部预测依赖的是头部长度字段和通告窗口。也就是说标志位除了ACK和PSH外,如果其他的存在的话,就不能用

fast path处理,其揭示的含义如下:

1 Either the data transaction is taking place in only one direction (which means that we are the receiver

and not transmitting any data) or in the case where we are sending out data also, the window advertised

 from the other end is constant. The latter means that we have not transmitted any data from our side for

quite some time but are receiving data from the other end. The receive window advertised by the other end is constant.

 

2. Other than PSH|ACK flags in the TCP header, no other flag is set (ACK is set for each TCP segment). 

This means that if any other flag is set such as URG, FIN, SYN, ECN, RST, and CWR, we
know that something important is there to be attended and we need to move into the SLOW path.

 

3. The header length has unchanged. If the TCP header length remains unchanged,
we have not added/reduced any TCP option and we can safely assume that
there is nothing important to be attended, if the above two conditions are TRUE.

 

fast path工作的条件

 

1 没有乱序数据包

2 接收窗口不为0

3 还有接收缓存空间

4 没有紧急数据

 

反之,则进入slow path处理;另外当连接新建立时处于slow path。

 

从fast path进入slow path的触发条件(进入slow path 后pred_flags清除为0):

1 在tcp_data_queue中接收到乱序数据包

2 在tcp_prune_queue中用完缓存并且开始丢弃数据包

3 在tcp_urgent_check中遇到紧急指针

4 在tcp_select_window中发送的通告窗口下降到0.

 

从slow_path进入fast_path的触发条件:

1 When we have read past an urgent byte in tcp_recvmsg() . Wehave gotten an urgent byte and we remain

  in the slow path mode until we receive the urgent byte because it is handled in the slow path in
  tcp_rcv_established() .

2 当在tcp_data_queue中乱序队列由于gap被填充而处理完毕时,运行tcp_fast_path_check。

3 tcp_ack_update_window()中更新了通告窗口。

 

fast path处理流程

A 判断能否进入fast path

TCP_HP_BITS的作用就是排除flag中的PSH标志位。只有在头部预测满足并且数据包以正确的顺序(该数据包的第一个序号就是下个要接收

的序号)到达时才进入fast path。

该代码段是依据时戳选项来检查PAWS(Protect Against Wrapped Sequence numbers)。

 

如果发送来的仅是一个TCP头的话(没有捎带数据或者接收端检测到有乱序数据这些情况时都会发送一个纯粹的ACK包)

主要的工作如下:

1 保存对方的最近时戳 tcp_store_ts_recent。通过前面的if判断可以看出tcp总是回显2次时戳回显直接最先到达的数据包的时戳,

  rcv_wup只在发送数据(这时回显时戳)时重置为rcv_nxt,所以接收到前一次回显后第一个数据包后,rcv_nxt增加了,但是

  rcv_wup没有更新,所以后面的数据包处理时不会调用该函数来保存时戳。

2 ACK处理。这个函数非常复杂,包含了拥塞控制机制,确认处理等等。

3 检查是否有数据待发送 tcp_data_snd_check。

 

如果该数据包中包含了数据的话

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值