Skip to content
Snippets Groups Projects
tcp_input.c 163 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		 * rather simple to implement as we could count fack_count
    		 * during the walk and do tp->fackets_out - fack_count).
    		 */
    		if (after(received_upto, ack_seq)) {
    
    			TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
    			tp->retrans_out -= tcp_skb_pcount(skb);
    
    
    			tcp_skb_mark_lost_uncond_verify(tp, skb);
    
    			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT);
    
    			if (before(ack_seq, new_low_seq))
    
    			cnt += tcp_skb_pcount(skb);
    
    
    	if (tp->retrans_out)
    		tp->lost_retrans_low = new_low_seq;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
    			    struct tcp_sack_block_wire *sp, int num_sacks,
    			    u32 prior_snd_una)
    
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
    	u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	bool dup_sack = false;
    
    
    	if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		dup_sack = true;
    
    		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
    
    		u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
    		u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
    
    
    		if (!after(end_seq_0, end_seq_1) &&
    		    !before(start_seq_0, start_seq_1)) {
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    			dup_sack = true;
    
    			NET_INC_STATS_BH(sock_net(sk),
    					LINUX_MIB_TCPDSACKOFORECV);
    
    		}
    	}
    
    	/* D-SACK for already forgotten data... Do dumb counting. */
    
    	if (dup_sack && tp->undo_marker && tp->undo_retrans &&
    
    	    !after(end_seq_0, prior_snd_una) &&
    	    after(end_seq_0, tp->undo_marker))
    		tp->undo_retrans--;
    
    	return dup_sack;
    }
    
    
    struct tcp_sacktag_state {
    	int reord;
    	int fack_count;
    	int flag;
    };
    
    
    /* Check if skb is fully within the SACK block. In presence of GSO skbs,
     * the incoming SACK may not exactly match but we can find smaller MSS
     * aligned portion of it that matches. Therefore we might need to fragment
     * which may fail and creates some hassle (caller must handle error case
     * returns).
    
     *
     * FIXME: this could be merged to shift decision code
    
    static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    				  u32 start_seq, u32 end_seq)
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	int err;
    	bool in_sack;
    
    
    	in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
    		  !before(end_seq, TCP_SKB_CB(skb)->end_seq);
    
    	if (tcp_skb_pcount(skb) > 1 && !in_sack &&
    	    after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
    
    		mss = tcp_skb_mss(skb);
    
    		in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
    
    
    			pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
    
    			if (pkt_len < mss)
    				pkt_len = mss;
    		} else {
    
    			pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
    
    			if (pkt_len < mss)
    				return -EINVAL;
    		}
    
    		/* Round if necessary so that SACKs cover only full MSSes
    		 * and/or the remaining small portion (if present)
    		 */
    		if (pkt_len > mss) {
    			unsigned int new_len = (pkt_len / mss) * mss;
    			if (!in_sack && new_len < pkt_len) {
    				new_len += mss;
    				if (new_len > skb->len)
    					return 0;
    			}
    			pkt_len = new_len;
    		}
    		err = tcp_fragment(sk, skb, pkt_len, mss);
    
    /* Mark the given newly-SACKed range as such, adjusting counters and hints. */
    static u8 tcp_sacktag_one(struct sock *sk,
    			  struct tcp_sacktag_state *state, u8 sacked,
    			  u32 start_seq, u32 end_seq,
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    			  bool dup_sack, int pcount)
    
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	int fack_count = state->fack_count;
    
    
    	/* Account D-SACK for retransmitted packet. */
    	if (dup_sack && (sacked & TCPCB_RETRANS)) {
    
    		if (tp->undo_marker && tp->undo_retrans &&
    
    		    after(end_seq, tp->undo_marker))
    
    			tp->undo_retrans--;
    
    		if (sacked & TCPCB_SACKED_ACKED)
    
    			state->reord = min(fack_count, state->reord);
    
    	}
    
    	/* Nothing to do; acked frame is about to be dropped (was ACKed). */
    
    	if (!after(end_seq, tp->snd_una))
    
    
    	if (!(sacked & TCPCB_SACKED_ACKED)) {
    		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.
    			 */
    			if (sacked & TCPCB_LOST) {
    
    				sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
    
    				tp->lost_out -= pcount;
    				tp->retrans_out -= pcount;
    
    			}
    		} else {
    			if (!(sacked & TCPCB_RETRANS)) {
    				/* New sack for not retransmitted frame,
    				 * which was in hole. It is reordering.
    				 */
    
    					   tcp_highest_sack_seq(tp)))
    
    					state->reord = min(fack_count,
    							   state->reord);
    
    			}
    
    			if (sacked & TCPCB_LOST) {
    
    		sacked |= TCPCB_SACKED_ACKED;
    		state->flag |= FLAG_DATA_SACKED;
    
    		tp->sacked_out += pcount;
    
    
    		/* Lost marker hint past SACKed? Tweak RFC3517 cnt */
    		if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) &&
    
    		    before(start_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;
    	}
    
    	/* 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.
    	 */
    
    	if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
    		sacked &= ~TCPCB_SACKED_RETRANS;
    
    		tp->retrans_out -= pcount;
    
    /* Shift newly-SACKed bytes from this skb to the immediately previous
     * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
     */
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
    			    struct tcp_sacktag_state *state,
    			    unsigned int pcount, int shifted, int mss,
    			    bool dup_sack)
    
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
    
    	u32 start_seq = TCP_SKB_CB(skb)->seq;	/* start of newly-SACKed */
    	u32 end_seq = start_seq + shifted;	/* end of newly-SACKed */
    
    	/* Adjust counters and hints for the newly sacked sequence
    	 * range but discard the return value since prev is already
    	 * marked. We must tag the range first because the seq
    	 * advancement below implicitly advances
    	 * tcp_highest_sack_seq() when skb is highest_sack.
    	 */
    	tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
    			start_seq, end_seq, dup_sack, pcount);
    
    	if (skb == tp->lost_skb_hint)
    
    	TCP_SKB_CB(prev)->end_seq += shifted;
    	TCP_SKB_CB(skb)->seq += shifted;
    
    	skb_shinfo(prev)->gso_segs += pcount;
    	BUG_ON(skb_shinfo(skb)->gso_segs < pcount);
    	skb_shinfo(skb)->gso_segs -= pcount;
    
    	/* When we're adding to gso_segs == 1, gso_size will be zero,
    	 * in theory this shouldn't be necessary but as long as DSACK
    	 * code can come after this skb later on it's better to keep
    	 * setting gso_size to something.
    	 */
    	if (!skb_shinfo(prev)->gso_size) {
    		skb_shinfo(prev)->gso_size = mss;
    
    		skb_shinfo(prev)->gso_type = sk->sk_gso_type;
    
    	}
    
    	/* CHECKME: To clear or not to clear? Mimics normal skb currently */
    	if (skb_shinfo(skb)->gso_segs <= 1) {
    		skb_shinfo(skb)->gso_size = 0;
    
    		skb_shinfo(skb)->gso_type = 0;
    
    	}
    
    	/* Difference in this won't matter, both ACKed by the same cumul. ACK */
    	TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
    
    	if (skb->len > 0) {
    		BUG_ON(!tcp_skb_pcount(skb));
    
    		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED);
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return false;
    
    	if (skb == tp->retransmit_skb_hint)
    		tp->retransmit_skb_hint = prev;
    	if (skb == tp->scoreboard_skb_hint)
    		tp->scoreboard_skb_hint = prev;
    	if (skb == tp->lost_skb_hint) {
    		tp->lost_skb_hint = prev;
    		tp->lost_cnt_hint -= tcp_skb_pcount(prev);
    	}
    
    
    	TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(prev)->tcp_flags;
    
    	if (skb == tcp_highest_sack(sk))
    		tcp_advance_highest_sack(sk, skb);
    
    	tcp_unlink_write_queue(skb, sk);
    	sk_wmem_free_skb(sk, skb);
    
    
    	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED);
    
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	return true;
    
    }
    
    /* I wish gso_size would have a bit more sane initialization than
     * something-or-zero which complicates things
     */
    
    static int tcp_skb_seglen(const struct sk_buff *skb)
    
    	return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
    
    }
    
    /* Shifting pages past head area doesn't work */
    
    static int skb_can_shift(const struct sk_buff *skb)
    
    {
    	return !skb_headlen(skb) && skb_is_nonlinear(skb);
    }
    
    /* Try collapsing SACK blocks spanning across multiple skbs to a single
     * skb.
     */
    static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
    
    					  struct tcp_sacktag_state *state,
    
    					  u32 start_seq, u32 end_seq,
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    					  bool dup_sack)
    
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct sk_buff *prev;
    	int mss;
    	int pcount = 0;
    	int len;
    	int in_sack;
    
    	if (!sk_can_gso(sk))
    		goto fallback;
    
    	/* Normally R but no L won't result in plain S */
    	if (!dup_sack &&
    
    	    (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
    
    		goto fallback;
    	if (!skb_can_shift(skb))
    		goto fallback;
    	/* This frame is about to be dropped (was ACKed). */
    	if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
    		goto fallback;
    
    	/* Can only happen with delayed DSACK + discard craziness */
    	if (unlikely(skb == tcp_write_queue_head(sk)))
    		goto fallback;
    	prev = tcp_write_queue_prev(sk, skb);
    
    	if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
    		goto fallback;
    
    	in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
    		  !before(end_seq, TCP_SKB_CB(skb)->end_seq);
    
    	if (in_sack) {
    		len = skb->len;
    		pcount = tcp_skb_pcount(skb);
    
    
    		/* TODO: Fix DSACKs to not fragment already SACKed and we can
    		 * drop this restriction as unnecessary
    		 */
    
    		if (mss != tcp_skb_seglen(prev))
    
    			goto fallback;
    	} else {
    		if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
    			goto noop;
    		/* CHECKME: This is non-MSS split case only?, this will
    		 * cause skipped skbs due to advancing loop btw, original
    		 * has that feature too
    		 */
    		if (tcp_skb_pcount(skb) <= 1)
    			goto noop;
    
    		in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
    		if (!in_sack) {
    			/* TODO: head merge to next could be attempted here
    			 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
    			 * though it might not be worth of the additional hassle
    			 *
    			 * ...we can probably just fallback to what was done
    			 * previously. We could try merging non-SACKed ones
    			 * as well but it probably isn't going to buy off
    			 * because later SACKs might again split them, and
    			 * it would make skb timestamp tracking considerably
    			 * harder problem.
    			 */
    			goto fallback;
    		}
    
    		len = end_seq - TCP_SKB_CB(skb)->seq;
    		BUG_ON(len < 0);
    		BUG_ON(len > skb->len);
    
    		/* MSS boundaries should be honoured or else pcount will
    		 * severely break even though it makes things bit trickier.
    		 * Optimize common case to avoid most of the divides
    		 */
    		mss = tcp_skb_mss(skb);
    
    		/* TODO: Fix DSACKs to not fragment already SACKed and we can
    		 * drop this restriction as unnecessary
    		 */
    
    		if (mss != tcp_skb_seglen(prev))
    
    			goto fallback;
    
    		if (len == mss) {
    			pcount = 1;
    		} else if (len < mss) {
    			goto noop;
    		} else {
    			pcount = len / mss;
    			len = pcount * mss;
    		}
    	}
    
    
    	/* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
    	if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
    		goto fallback;
    
    
    	if (!skb_shift(prev, skb, len))
    		goto fallback;
    
    	if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
    
    		goto out;
    
    	/* Hole filled allows collapsing with the next as well, this is very
    	 * useful when hole on every nth skb pattern happens
    	 */
    	if (prev == tcp_write_queue_tail(sk))
    		goto out;
    	skb = tcp_write_queue_next(sk, prev);
    
    
    	if (!skb_can_shift(skb) ||
    	    (skb == tcp_send_head(sk)) ||
    	    ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
    
    		goto out;
    
    	len = skb->len;
    	if (skb_shift(prev, skb, len)) {
    		pcount += tcp_skb_pcount(skb);
    
    		tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
    
    	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
    
    static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
    					struct tcp_sack_block *next_dup,
    
    					struct tcp_sacktag_state *state,
    
    					u32 start_seq, u32 end_seq,
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    					bool dup_sack_in)
    
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct sk_buff *tmp;
    
    
    	tcp_for_write_queue_from(skb, sk) {
    		int in_sack = 0;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		bool dup_sack = dup_sack_in;
    
    
    		if (skb == tcp_send_head(sk))
    			break;
    
    		/* queue is in-order => we can short-circuit the walk early */
    		if (!before(TCP_SKB_CB(skb)->seq, end_seq))
    			break;
    
    		if ((next_dup != NULL) &&
    		    before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
    			in_sack = tcp_match_skb_to_sack(sk, skb,
    							next_dup->start_seq,
    							next_dup->end_seq);
    			if (in_sack > 0)
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    				dup_sack = true;
    
    		/* skb reference here is a bit tricky to get right, since
    		 * shifting can eat and free both this skb and the next,
    		 * so not even _safe variant of the loop is enough.
    		 */
    		if (in_sack <= 0) {
    
    			tmp = tcp_shift_skb_data(sk, skb, state,
    						 start_seq, end_seq, dup_sack);
    
    			if (tmp != NULL) {
    				if (tmp != skb) {
    					skb = tmp;
    					continue;
    				}
    
    				in_sack = 0;
    			} else {
    				in_sack = tcp_match_skb_to_sack(sk, skb,
    								start_seq,
    								end_seq);
    			}
    		}
    
    
    			TCP_SKB_CB(skb)->sacked =
    				tcp_sacktag_one(sk,
    						state,
    						TCP_SKB_CB(skb)->sacked,
    						TCP_SKB_CB(skb)->seq,
    						TCP_SKB_CB(skb)->end_seq,
    						dup_sack,
    						tcp_skb_pcount(skb));
    
    			if (!before(TCP_SKB_CB(skb)->seq,
    				    tcp_highest_sack_seq(tp)))
    				tcp_advance_highest_sack(sk, skb);
    		}
    
    
    		state->fack_count += tcp_skb_pcount(skb);
    
    	}
    	return skb;
    }
    
    /* Avoid all extra work that is being done by sacktag while walking in
     * a normal way
     */
    static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
    
    					struct tcp_sacktag_state *state,
    					u32 skip_to_seq)
    
    {
    	tcp_for_write_queue_from(skb, sk) {
    		if (skb == tcp_send_head(sk))
    			break;
    
    
    		if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
    
    		state->fack_count += tcp_skb_pcount(skb);
    
    	}
    	return skb;
    }
    
    static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
    						struct sock *sk,
    						struct tcp_sack_block *next_dup,
    
    						struct tcp_sacktag_state *state,
    						u32 skip_to_seq)
    
    {
    	if (next_dup == NULL)
    		return skb;
    
    	if (before(next_dup->start_seq, skip_to_seq)) {
    
    		skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
    		skb = tcp_sacktag_walk(skb, sk, NULL, state,
    				       next_dup->start_seq, next_dup->end_seq,
    				       1);
    
    static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
    
    {
    	return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    static int
    
    tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
    
    			u32 prior_snd_una)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	const unsigned char *ptr = (skb_transport_header(ack_skb) +
    				    TCP_SKB_CB(ack_skb)->sacked);
    
    	struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
    
    	struct tcp_sack_block sp[TCP_NUM_SACKS];
    
    	struct tcp_sack_block *cache;
    
    	struct tcp_sacktag_state state;
    
    	int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	bool found_dup_sack = false;
    
    	int first_sack_index;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	state.flag = 0;
    	state.reord = tp->packets_out;
    
    
    		if (WARN_ON(tp->fackets_out))
    			tp->fackets_out = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
    
    					 num_sacks, prior_snd_una);
    	if (found_dup_sack)
    
    		state.flag |= FLAG_DSACKING_ACK;
    
    
    	/* Eliminate too old ACKs, but take into
    	 * account more or less fresh ones, they can
    	 * contain valid SACK info.
    	 */
    	if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
    		return 0;
    
    
    	used_sacks = 0;
    	first_sack_index = 0;
    	for (i = 0; i < num_sacks; i++) {
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		bool dup_sack = !i && found_dup_sack;
    
    		sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
    		sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
    
    
    		if (!tcp_is_sackblock_valid(tp, dup_sack,
    					    sp[used_sacks].start_seq,
    					    sp[used_sacks].end_seq)) {
    
    			if (dup_sack) {
    				if (!tp->undo_marker)
    
    					mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
    
    					mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
    
    			} else {
    				/* Don't count olds caused by ACK reordering */
    				if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
    				    !after(sp[used_sacks].end_seq, tp->snd_una))
    					continue;
    
    				mib_idx = LINUX_MIB_TCPSACKDISCARD;
    
    			NET_INC_STATS_BH(sock_net(sk), mib_idx);
    
    			if (i == 0)
    				first_sack_index = -1;
    			continue;
    		}
    
    		/* Ignore very old stuff early */
    		if (!after(sp[used_sacks].end_seq, prior_snd_una))
    			continue;
    
    		used_sacks++;
    	}
    
    
    	/* order SACK blocks to allow in order walk of the retrans queue */
    	for (i = used_sacks - 1; i > 0; i--) {
    
    		for (j = 0; j < i; j++) {
    			if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
    
    				swap(sp[j], sp[j + 1]);
    
    				/* Track where the first SACK block goes to */
    				if (j == first_sack_index)
    
    					first_sack_index = j + 1;
    
    	skb = tcp_write_queue_head(sk);
    
    	i = 0;
    
    	if (!tp->sacked_out) {
    		/* It's already past, so skip checking against it */
    		cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
    	} else {
    		cache = tp->recv_sack_cache;
    		/* Skip empty blocks in at head of the cache */
    		while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
    		       !cache->end_seq)
    			cache++;
    
    		u32 start_seq = sp[i].start_seq;
    		u32 end_seq = sp[i].end_seq;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		bool dup_sack = (found_dup_sack && (i == first_sack_index));
    
    		struct tcp_sack_block *next_dup = NULL;
    
    		if (found_dup_sack && ((i + 1) == first_sack_index))
    			next_dup = &sp[i + 1];
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		/* Skip too early cached blocks */
    		while (tcp_sack_cache_ok(tp, cache) &&
    		       !before(start_seq, cache->end_seq))
    			cache++;
    
    		/* Can skip some work by looking recv_sack_cache? */
    		if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
    		    after(end_seq, cache->start_seq)) {
    
    			/* Head todo? */
    			if (before(start_seq, cache->start_seq)) {
    
    				skb = tcp_sacktag_skip(skb, sk, &state,
    						       start_seq);
    
    				skb = tcp_sacktag_walk(skb, sk, next_dup,
    
    						       start_seq,
    						       cache->start_seq,
    
    			/* Rest of the block already fully processed? */
    
    			if (!after(end_seq, cache->end_seq))
    
    			skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
    
    			/* ...tail remains todo... */
    
    			if (tcp_highest_sack_seq(tp) == cache->end_seq) {
    
    				/* ...but better entrypoint exists! */
    
    				skb = tcp_highest_sack(sk);
    				if (skb == NULL)
    					break;
    
    				state.fack_count = tp->fackets_out;
    
    			skb = tcp_sacktag_skip(skb, sk, &state, cache->end_seq);
    
    			/* Check overlap against next cached too (past this one already) */
    			cache++;
    			continue;
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		if (!before(start_seq, tcp_highest_sack_seq(tp))) {
    			skb = tcp_highest_sack(sk);
    			if (skb == NULL)
    				break;
    
    			state.fack_count = tp->fackets_out;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    
    		skb = tcp_sacktag_skip(skb, sk, &state, start_seq);
    
    		skb = tcp_sacktag_walk(skb, sk, next_dup, &state,
    				       start_seq, end_seq, dup_sack);
    
    	/* Clear the head of the cache sack blocks so we can skip it next time */
    	for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
    		tp->recv_sack_cache[i].start_seq = 0;
    		tp->recv_sack_cache[i].end_seq = 0;
    	}
    	for (j = 0; j < used_sacks; j++)
    		tp->recv_sack_cache[i++] = sp[j];
    
    
    	tcp_mark_lost_retrans(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if ((state.reord < tp->fackets_out) &&
    
    Yuchung Cheng's avatar
    Yuchung Cheng committed
    	    ((inet_csk(sk)->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker))
    
    		tcp_update_reordering(sk, tp->fackets_out - state.reord, 0);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #if FASTRETRANS_DEBUG > 0
    
    	WARN_ON((int)tp->sacked_out < 0);
    	WARN_ON((int)tp->lost_out < 0);
    	WARN_ON((int)tp->retrans_out < 0);
    	WARN_ON((int)tcp_packets_in_flight(tp) < 0);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    
    /* Limits sacked_out so that sum with lost_out isn't ever larger than
    
    Eric Dumazet's avatar
    Eric Dumazet committed
     * packets_out. Returns false if sacked_out adjustement wasn't necessary.
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
    
    {
    	u32 holes;
    
    	holes = max(tp->lost_out, 1U);
    	holes = min(holes, tp->packets_out);
    
    	if ((tp->sacked_out + holes) > tp->packets_out) {
    		tp->sacked_out = tp->packets_out - holes;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return true;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	return false;
    
    }
    
    /* If we receive more dupacks than we expected counting segments
     * in assumption of absent reordering, interpret this as reordering.
     * The only another reason could be bug in receiver TCP.
     */
    static void tcp_check_reno_reordering(struct sock *sk, const int addend)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	if (tcp_limit_reno_sacked(tp))
    		tcp_update_reordering(sk, tp->packets_out + addend, 0);
    
    }
    
    /* Emulate SACKs for SACKless connection: account for a new dupack. */
    
    static void tcp_add_reno_sack(struct sock *sk)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	tp->sacked_out++;
    	tcp_check_reno_reordering(sk, 0);
    
    }
    
    /* Account for ACK, ACKing some data in Reno Recovery phase. */
    
    static void tcp_remove_reno_sacks(struct sock *sk, int acked)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	if (acked > 0) {
    		/* One ACK acked hole. The rest eat duplicate ACKs. */
    
    		if (acked - 1 >= tp->sacked_out)
    
    			tp->sacked_out -= acked - 1;
    
    	}
    	tcp_check_reno_reordering(sk, acked);
    
    }
    
    static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
    {
    	tp->sacked_out = 0;
    }
    
    
    static void tcp_clear_retrans_partial(struct tcp_sock *tp)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	tp->retrans_out = 0;
    	tp->lost_out = 0;
    
    	tp->undo_marker = 0;
    	tp->undo_retrans = 0;
    }
    
    
    void tcp_clear_retrans(struct tcp_sock *tp)
    {
    	tcp_clear_retrans_partial(tp);
    
    	tp->fackets_out = 0;
    	tp->sacked_out = 0;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /* Enter Loss state. If "how" is not zero, forget all SACK information
     * and reset tags completely, otherwise preserve SACKs. If receiver
     * dropped its ofo queue, we will know this due to reneging detection.
     */
    void tcp_enter_loss(struct sock *sk, int how)
    {
    
    	const struct inet_connection_sock *icsk = inet_csk(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct sk_buff *skb;
    
    	/* Reduce ssthresh if it has not yet been made inside this window. */
    
    	if (icsk->icsk_ca_state <= TCP_CA_Disorder || tp->snd_una == tp->high_seq ||
    	    (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
    		tp->prior_ssthresh = tcp_current_ssthresh(sk);
    		tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
    		tcp_ca_event(sk, CA_EVENT_LOSS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	tp->snd_cwnd	   = 1;
    	tp->snd_cwnd_cnt   = 0;
    	tp->snd_cwnd_stamp = tcp_time_stamp;
    
    
    	tcp_clear_retrans_partial(tp);
    
    	if (tcp_is_reno(tp))
    		tcp_reset_reno_sack(tp);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (!how) {
    		/* Push undo marker, if it was plain RTO and nothing
    		 * was retransmitted. */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		tp->undo_marker = tp->snd_una;
    
    		tp->sacked_out = 0;
    		tp->fackets_out = 0;
    
    	tcp_clear_all_retrans_hints(tp);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	tcp_for_write_queue(skb, sk) {
    		if (skb == tcp_send_head(sk))
    			break;
    
    		if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			tp->undo_marker = 0;
    		TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED;
    		if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) || how) {
    			TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
    			TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
    			tp->lost_out += tcp_skb_pcount(skb);
    
    			tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	tp->reordering = min_t(unsigned int, tp->reordering,
    
    			       sysctl_tcp_reordering);
    
    	tcp_set_ca_state(sk, TCP_CA_Loss);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	tp->high_seq = tp->snd_nxt;
    	TCP_ECN_queue_cwr(tp);
    }
    
    
    /* If ACK arrived pointing to a remembered SACK, it means that our
     * remembered SACKs do not reflect real state of receiver i.e.
     * receiver _host_ is heavily congested (or buggy).
     *
     * Do processing similar to RTO timeout.
     */
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static bool tcp_check_sack_reneging(struct sock *sk, int flag)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	if (flag & FLAG_SACK_RENEGING) {
    
    		struct inet_connection_sock *icsk = inet_csk(sk);
    
    		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    		tcp_enter_loss(sk, 1);
    
    		tcp_retransmit_skb(sk, tcp_write_queue_head(sk));
    
    		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return true;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	return false;
    
    static inline int tcp_fackets_out(const struct tcp_sock *tp)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out;
    
    /* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
     * counter when SACK is enabled (without SACK, sacked_out is used for
     * that purpose).
     *
     * Instead, with FACK TCP uses fackets_out that includes both SACKed
     * segments up to the highest received SACK block so far and holes in
     * between them.
     *
     * With reordering, holes may still be in flight, so RFC3517 recovery
     * uses pure sacked_out (total number of SACKed segments) even though
     * it violates the RFC that uses duplicate ACKs, often these are equal
     * but when e.g. out-of-window ACKs or packet duplication occurs,
     * they differ. Since neither occurs due to loss, TCP should really
     * ignore them.
     */
    
    static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
    
    {
    	return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1;
    }
    
    
    static bool tcp_pause_early_retransmit(struct sock *sk, int flag)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	unsigned long delay;
    
    	/* Delay early retransmit and entering fast recovery for
    	 * max(RTT/4, 2msec) unless ack has ECE mark, no RTT samples
    	 * available, or RTO is scheduled to fire first.
    	 */
    
    	if (sysctl_tcp_early_retrans < 2 || sysctl_tcp_early_retrans > 3 ||
    	    (flag & FLAG_ECE) || !tp->srtt)
    
    		return false;
    
    	delay = max_t(unsigned long, (tp->srtt >> 5), msecs_to_jiffies(2));
    	if (!time_after(inet_csk(sk)->icsk_timeout, (jiffies + delay)))
    		return false;
    
    
    	inet_csk_reset_xmit_timer(sk, ICSK_TIME_EARLY_RETRANS, delay,
    				  TCP_RTO_MAX);
    
    static inline int tcp_skb_timedout(const struct sock *sk,
    				   const struct sk_buff *skb)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	return tcp_time_stamp - TCP_SKB_CB(skb)->when > inet_csk(sk)->icsk_rto;
    
    static inline int tcp_head_timedout(const struct sock *sk)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	const struct tcp_sock *tp = tcp_sk(sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return tp->packets_out &&
    
    	       tcp_skb_timedout(sk, tcp_write_queue_head(sk));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /* Linux NewReno/SACK/FACK/ECN state machine.
     * --------------------------------------
     *
     * "Open"	Normal state, no dubious events, fast path.
     * "Disorder"   In all the respects it is "Open",
     *		but requires a bit more attention. It is entered when
     *		we see some SACKs or dupacks. It is split of "Open"
     *		mainly to move some processing from fast path to slow one.
     * "CWR"	CWND was reduced due to some Congestion Notification event.
     *		It can be ECN, ICMP source quench, local device congestion.
     * "Recovery"	CWND was reduced, we are fast-retransmitting.
     * "Loss"	CWND was reduced due to RTO timeout or SACK reneging.
     *
     * tcp_fastretrans_alert() is entered:
     * - each incoming ACK, if state is not "Open"
     * - when arrived ACK is unusual, namely:
     *	* SACK
     *	* Duplicate ACK.
     *	* ECN ECE.
     *
     * Counting packets in flight is pretty simple.
     *
     *	in_flight = packets_out - left_out + retrans_out
     *
     *	packets_out is SND.NXT-SND.UNA counted in packets.
     *
     *	retrans_out is number of retransmitted segments.
     *
     *	left_out is number of segments left network, but not ACKed yet.
     *