Skip to content
Snippets Groups Projects
tcp_input.c 175 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		 * the available datastructures.
    		 *
    		 * Whether FACK should check here for tp->reordering segs
    		 * in-between one could argue for either way (it would be
    		 * 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);
    
    
    				/* SACK enhanced F-RTO (RFC4138; Appendix B) */
    
    				if (!after(end_seq, tp->frto_highmark))
    
    					state->flag |= FLAG_ONLY_ORIG_SACKED;
    
    			}
    
    			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
    {
    
    	const struct inet_connection_sock *icsk = inet_csk(sk);
    
    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);
    
    		/* SACK enhanced FRTO (RFC4138, Appendix B): Clearing correct
    		 * due to in-order walk
    		 */
    		if (after(end_seq, tp->frto_highmark))
    
    			state.flag &= ~FLAG_ONLY_ORIG_SACKED;
    
    	/* 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) &&
    
    	    ((icsk->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker) &&
    
    	    (!tp->frto_highmark || after(tp->snd_una, tp->frto_highmark)))
    
    		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 int tcp_is_sackfrto(const struct tcp_sock *tp)
    {
    	return (sysctl_tcp_frto == 0x2) && !tcp_is_reno(tp);
    }
    
    
    /* F-RTO can only be used if TCP has never retransmitted anything other than
     * head (SACK enhanced variant from Appendix B of RFC4138 is more robust here)
     */
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    bool tcp_use_frto(struct sock *sk)
    
    {
    	const struct tcp_sock *tp = tcp_sk(sk);
    
    	const struct inet_connection_sock *icsk = inet_csk(sk);
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return false;
    
    	/* MTU probe and F-RTO won't really play nicely along currently */
    	if (icsk->icsk_mtup.probe_size)
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return false;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return true;
    
    	/* Avoid expensive walking of rexmit queue if possible */
    	if (tp->retrans_out > 1)
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return false;
    
    	skb = tcp_write_queue_head(sk);
    
    	if (tcp_skb_is_last(sk, skb))
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return true;
    
    	skb = tcp_write_queue_next(sk, skb);	/* Skips head */
    	tcp_for_write_queue_from(skb, sk) {
    		if (skb == tcp_send_head(sk))
    			break;
    
    		if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    			return false;
    
    		/* Short-circuit when first non-SACKed skb has been checked */
    
    		if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	return true;
    
    /* RTO occurred, but do not yet enter Loss state. Instead, defer RTO
     * recovery a bit and use heuristics in tcp_process_frto() to detect if
    
     * the RTO was spurious. Only clear SACKED_RETRANS of the head here to
     * keep retrans_out counting accurate (with SACK F-RTO, other than head
     * may still have that bit set); TCPCB_LOST and remaining SACKED_RETRANS
     * bits are handled if the Loss state is really to be entered (in
     * tcp_enter_frto_loss).
    
     *
     * Do like tcp_enter_loss() would; when RTO expires the second time it
     * does:
     *  "Reduce ssthresh if it has not yet been made inside this window."
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    void tcp_enter_frto(struct sock *sk)
    {
    
    	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;
    
    
    	if ((!tp->frto_counter && icsk->icsk_ca_state <= TCP_CA_Disorder) ||
    
    	    tp->snd_una == tp->high_seq ||
    
    	    ((icsk->icsk_ca_state == TCP_CA_Loss || tp->frto_counter) &&
    	     !icsk->icsk_retransmits)) {
    
    		tp->prior_ssthresh = tcp_current_ssthresh(sk);
    
    		/* Our state is too optimistic in ssthresh() call because cwnd
    
    		 * is not reduced until tcp_enter_frto_loss() when previous F-RTO
    
    		 * recovery has not yet completed. Pattern would be this: RTO,
    		 * Cumulative ACK, RTO (2xRTO for the same segment does not end
    		 * up here twice).
    		 * RFC4138 should be more specific on what to do, even though
    		 * RTO is quite unlikely to occur after the first Cumulative ACK
    		 * due to back-off and complexity of triggering events ...
    		 */
    		if (tp->frto_counter) {
    			u32 stored_cwnd;
    			stored_cwnd = tp->snd_cwnd;
    			tp->snd_cwnd = 2;
    			tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
    			tp->snd_cwnd = stored_cwnd;
    		} else {
    			tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
    		}
    		/* ... in theory, cong.control module could do "any tricks" in
    		 * ssthresh(), which means that ca_state, lost bits and lost_out
    		 * counter would have to be faked before the call occurs. We
    		 * consider that too expensive, unlikely and hacky, so modules
    		 * using these in ssthresh() must deal these incompatibility
    		 * issues if they receives CA_EVENT_FRTO and frto_counter != 0
    		 */
    
    		tcp_ca_event(sk, CA_EVENT_FRTO);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	tp->undo_marker = tp->snd_una;
    	tp->undo_retrans = 0;
    
    
    	skb = tcp_write_queue_head(sk);
    
    	if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
    		tp->undo_marker = 0;
    
    	if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
    
    		TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
    
    		tp->retrans_out -= tcp_skb_pcount(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* Too bad if TCP was application limited */
    	tp->snd_cwnd = min(tp->snd_cwnd, tcp_packets_in_flight(tp) + 1);
    
    
    	/* Earlier loss recovery underway (see RFC4138; Appendix B).
    	 * The last condition is necessary at least in tp->frto_counter case.
    	 */
    
    	if (tcp_is_sackfrto(tp) && (tp->frto_counter ||
    
    	    ((1 << icsk->icsk_ca_state) & (TCPF_CA_Recovery|TCPF_CA_Loss))) &&
    	    after(tp->high_seq, tp->snd_una)) {
    		tp->frto_highmark = tp->high_seq;
    	} else {
    		tp->frto_highmark = tp->snd_nxt;
    	}
    
    	tcp_set_ca_state(sk, TCP_CA_Disorder);
    	tp->high_seq = tp->snd_nxt;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /* Enter Loss state after F-RTO was applied. Dupack arrived after RTO,
     * which indicates that we should follow the traditional RTO recovery,
     * i.e. mark everything lost and do go-back-N retransmission.
     */
    
    static void tcp_enter_frto_loss(struct sock *sk, int allowed_segments, int flag)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct sk_buff *skb;
    
    	tp->lost_out = 0;
    
    	tp->retrans_out = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	tcp_for_write_queue(skb, sk) {
    		if (skb == tcp_send_head(sk))
    			break;
    
    
    		TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
    
    		/*
    		 * Count the retransmission made on RTO correctly (only when
    		 * waiting for the first ACK and did not get it)...
    		 */
    
    		if ((tp->frto_counter == 1) && !(flag & FLAG_DATA_ACKED)) {
    
    			/* For some reason this R-bit might get cleared? */
    			if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
    				tp->retrans_out += tcp_skb_pcount(skb);
    
    			/* ...enter this if branch just for the first segment */
    			flag |= FLAG_DATA_ACKED;
    		} else {
    
    			if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
    				tp->undo_marker = 0;
    
    			TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		/* Marking forward transmissions that were made after RTO lost
    		 * can cause unnecessary retransmissions in some scenarios,
    		 * SACK blocks will mitigate that in some but not in all cases.
    		 * We used to not mark them but it was causing break-ups with
    		 * receivers that do only in-order receival.
    		 *
    		 * TODO: we could detect presence of such receiver and select
    		 * different behavior per flow.
    		 */
    		if (!(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;