TCP的核心系列 — SACK和DSACK的实现(六)

时间:2024-01-17 16:30:56

上篇文章中我们主要说明如何skip到一个SACK块对应的开始段,如何walk这个SACK块包含的段,而没有涉及到

如何标志一个段的记分牌。37版本把给一个段打标志的内容独立出来,这就是tcp_sacktag_one()。

本文主要内容:tcp_sacktag_one(),给一个段打上标志。

Author:zhangskd @ csdn

标志一个包

tcp_sacktag_walk()用于遍历块中的数据包,最终会调用tcp_sacktag_one()来标志一个数据包的记分牌,

即TCP_SKB_CB(skb)->sacked。

记分牌有哪些标志呢?

#define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block, 标志S */

#define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted,标志R */

#define TCPCB_LOST 0x04 /* SKB is lot,标志L */

#define TCPCB_TAGBITS 0x07 /* All tag bits,标志位掩码 */

#define TCPCB_EVER_RETRANS 0x08 /* Ever retransmitted frame,曾经重传过 */

#define TCPCB_RETRANS (TCPCB_SACKED_RETRANS | TCPCB_EVER_RETRANS)

以上标志的说明如下:

We have three tag bits: SACKED(S)、RETRANS(R) and LOST(L).

Packets in queue with these bits set are counted in variables sacked_out、retrans_out and lost_out.

tag标志可能的6种情况:

Tag        InFlight        Description

0             1                   orig segment is in flight,正常情况

S             0                   nothing flies, orig reached receiver.

L              0                  nothing flies, orig lost by net.

R             2                  both orig and retransmit is in flight.

L|R          1                  orig is lost, retransmit is in flight.

S|R          1                  orig reached receiver, retrans is still in flight.

(L|S|R is logically valid, it could occur when L|R is sacked, but it is equivalent to plain S and code

short-curcuits it to S.

L|S is logically invalid, it would mean -1 packet in flight.

以上6种情况是由以下事件触发的:

These 6 states form finite state machine, controlled by the following events:

1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())

2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())

3. Loss detection event of one of three flavors:

A. Scoreboard estimator decided the packet is lost.

A'. Reno "three dupacks" marks head of queue lost.

A''. Its FACK modification, head until snd.fack is lost.

B. SACK arrives sacking data retransmitted after never retransmitted hole was sent out.

C. SACK arrives sacking SND.NXT at the moment, when the segment was retransmitted.

4. D-SACK added new rule: D-SACK changes any tag to S.

static u8 tcp_sacktag_one(struct sk_buff *skb, struct sock *sk,
struct tcp_sacktag_state *state, int dup_sack, int pcount)
{
struct tcp_sock *tp = tcp_sk(sk);
u8 sacked = TCP_SKB_CB(skb)->sacked;
int fack_count = state->fack_count; /* Account D-SACK for retransmitted packet.
* 如果此skb属于DSACK块,且skb被重传过。即此前tag为R、或者R|S。
*/
if (dup_sack && (sacked & TCPCB_RETRANS)) { /* 位于上次进入Recovery或Loss之后 */
if (after(TCP_SKB_CB(skb)->end_seq, tp->undo_marker))
tp->undo_retrans--; /* 如果减为0,那么说明之前重传都是不必要的,进行拥塞控制调整撤销 */ if (sacked & TCPCB_SACKED_ACKED) /* 如果这个包已经被SACK过,那么说明是乱序 */
state->reord = min(fack_count, state->reord); /* 更新乱序队列的起始点 */
} /* Nothing to do; acked frame is about to be dropped (was ACKed).
* 这个skb已经被正常确认了,不用再处理了,它即将被丢弃。
*/
if (! after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
return sacked; /* 如果skb还没有被SACK,那么进行处理 */
if (! (sacked & TCPCB_SACKED_ACKED)) { /* 有R标志,表示被重传过 */
if (sacked & TCPCB_SACKED_RETRANS) {
/* If the segment is not tagged as lost, we do not clear RETRANS, believing
* that retransmission is still in flight.
* 如果之前的标志是:R | L,那么好,现在收到包了,可以清除R和L。
* 如果之前的标志是:R,那么认为现在收到的是orig,重传包还在路上,所以不用干活:)
*/
if (sacked & TCPCB_LOST) {
sacked &= ~(TCPCB_LOST | TCPCB_SACKED_RETRANS); /* 取消L和R标志 */
tp->lost_out -= pcount; /* 更新LOST包个数 */
tp->retrans_out -= pcount; /* 更新RETRANS包个数 */
} } else { /* 没有R标志 */
if (! (sacked & TCPCB_RETRANS)) {
/* New sack for not retransmitted frame, which was in hole. It is reordering.
* 如果一个包落在highest_sack之前,它即没被SACK过,也不是重传的,那么
* 它肯定是乱序了,到现在才被SACK。
*/
if (before(TCP_SKB_CB(skb)->seq, tcp_highest_sack_seq(tp)))
state->reord = min(fack_count, state->reord); /* 记录乱序的起始位置 */ /* SACK enhanced F-RTO (RFC4138; Appendix B) */
if (! after(TCP_SKB_CB(skb)->end_seq, tp->frto_highmark))
state->flag |= FLAG_ONLY_ORIG_SACKED; /* SACKs only non-rexmit sent before RTO */
} /* 如果它有LOST标志,既然收到了,那么要撤销了 */
if (sacked & TCPCB_LOST) {
sacked &= ~TCPCB_LOST; /* 撤销LOST标志 */
tp->lost_out -= pcount; /* 更新LOST包个数 */
}
} sacked |= TCPCB_SACKED_ACKED; /* 给skb打上SACK标志,就是这里:) */
state->flag |= FLAG_DATA_SACKED;
tp->sacked_out += pcount; /* 更新SACK包个数 */
fack_count += pcount; /* fackets_out =sacked_out + lost_out,也跟着更新 */ /* 没有使用FACK时 */
if (! tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) &&
before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
tp->lost_cnt_hint += pcount; if (fack_count > tp->fackets_out)
tp->fackets_out = fack_count; /* 更新tp->fackets_out */
} /* D-SACK. We can detect redundant retransmission in S|R and plain R frames and clear it.
* undo_retrans is decreased above, L|R frames are accounted above as well.
* 如果skb被D-SACK,并且它的重传标志还未被清除,那么现在清除。
*/
if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
sacked &= ~TCPCB_SACKED_RETRANS; /* 清除重传标志 */
tp->retrans_out -= pcount; /* 更新重传包个数 */
} return sacked; /* 返回此skb的记分牌 */
}