在ACK的确认过程中,需要做的非常重要的一件事就是将已经确认的数据从发送队列(以及重传队列)中删除,这是通过tcp_clean_rtx_queue()完成的。此外由于TCP在实现过程中,发送队列和重传队列都是sk_write_queue,所以这两个队列是一并处理的。这篇笔记就来看看发送队列的清除操作是如何实现的。
1. tcp_clean_rtx_queue()
/* Remove acknowledged frames from the retransmission queue. If our packet
* is before the ack sequence we can discard it as it's confirmed to have
* arrived at the other end.
*/
static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets)
{
struct tcp_sock *tp = tcp_sk(sk);
const struct inet_connection_sock *icsk = inet_csk(sk);
struct sk_buff *skb;
u32 now = tcp_time_stamp;
//如果为1,表示skb中所有序号都被确认了,否则只是部分确认
int fully_acked = 1;
int flag = 0;
//累计本次清除操作清除的TSO段数
u32 pkts_acked = 0;
u32 reord = tp->packets_out;
s32 seq_rtt = -1;
s32 ca_seq_rtt = -1;
ktime_t last_ackt = net_invalid_timestamp();
//遍历发送队列中所有已发送的数据段
while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) {
struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
//记录当前skb能够被确认的最大序号(的下一个序号)
u32 end_seq;
//记录当前skb中能够被ACK的TSO段数
u32 acked_pcount;
//sacked是当前skb的记分牌,它记录了skb的重传、SACK、丢失信息
u8 sacked = scb->sacked;
//如果当前skb的右边界大于等于snd_una(该变量已经更新),
//说明当前skb最多只能被确认一部分
if (after(scb->end_seq, tp->snd_una)) {
//如果当前skb只有一个TSO段,或者这一个段的左边界也大于等于snd_una,
//说明当前skb的所有数据都不能被确认,进而队列中后续skb也不用判断了,结束遍历
if (tcp_skb_pcount(skb) == 1 || !after(tp-