diff options
Diffstat (limited to 'net/ipv4/tcp_input.c')
| -rw-r--r-- | net/ipv4/tcp_input.c | 3120 | 
1 files changed, 1530 insertions, 1590 deletions
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 6d8ab1c4efc..40639c288dc 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -61,6 +61,8 @@   *		Pasi Sarolahti:		F-RTO for dealing with spurious RTOs   */ +#define pr_fmt(fmt) "TCP: " fmt +  #include <linux/mm.h>  #include <linux/slab.h>  #include <linux/module.h> @@ -79,24 +81,23 @@ int sysctl_tcp_sack __read_mostly = 1;  int sysctl_tcp_fack __read_mostly = 1;  int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH;  EXPORT_SYMBOL(sysctl_tcp_reordering); -int sysctl_tcp_ecn __read_mostly = 2; -EXPORT_SYMBOL(sysctl_tcp_ecn);  int sysctl_tcp_dsack __read_mostly = 1;  int sysctl_tcp_app_win __read_mostly = 31; -int sysctl_tcp_adv_win_scale __read_mostly = 2; +int sysctl_tcp_adv_win_scale __read_mostly = 1;  EXPORT_SYMBOL(sysctl_tcp_adv_win_scale); +/* rfc5961 challenge ack rate limiting */ +int sysctl_tcp_challenge_ack_limit = 100; +  int sysctl_tcp_stdurg __read_mostly;  int sysctl_tcp_rfc1337 __read_mostly;  int sysctl_tcp_max_orphans __read_mostly = NR_FILE;  int sysctl_tcp_frto __read_mostly = 2; -int sysctl_tcp_frto_response __read_mostly; -int sysctl_tcp_nometrics_save __read_mostly;  int sysctl_tcp_thin_dupack __read_mostly;  int sysctl_tcp_moderate_rcvbuf __read_mostly = 1; -int sysctl_tcp_abc __read_mostly; +int sysctl_tcp_early_retrans __read_mostly = 3;  #define FLAG_DATA		0x01 /* Incoming frame contained data.		*/  #define FLAG_WIN_UPDATE		0x02 /* Incoming ACK was a window update.	*/ @@ -105,19 +106,17 @@ int sysctl_tcp_abc __read_mostly;  #define FLAG_SYN_ACKED		0x10 /* This ACK acknowledged SYN.		*/  #define FLAG_DATA_SACKED	0x20 /* New SACK.				*/  #define FLAG_ECE		0x40 /* ECE in this ACK				*/ -#define FLAG_DATA_LOST		0x80 /* SACK detected data lossage.		*/  #define FLAG_SLOWPATH		0x100 /* Do not skip RFC checks for window update.*/ -#define FLAG_ONLY_ORIG_SACKED	0x200 /* SACKs only non-rexmit sent before RTO */ +#define FLAG_ORIG_SACK_ACKED	0x200 /* Never retransmitted data are (s)acked	*/  #define FLAG_SND_UNA_ADVANCED	0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */  #define FLAG_DSACKING_ACK	0x800 /* SACK blocks contained D-SACK info */ -#define FLAG_NONHEAD_RETRANS_ACKED	0x1000 /* Non-head rexmitted data was ACKed */  #define FLAG_SACK_RENEGING	0x2000 /* snd_una advanced to a sacked seq */ +#define FLAG_UPDATE_TS_RECENT	0x4000 /* tcp_replace_ts_recent() */  #define FLAG_ACKED		(FLAG_DATA_ACKED|FLAG_SYN_ACKED)  #define FLAG_NOT_DUP		(FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)  #define FLAG_CA_ALERT		(FLAG_DATA_SACKED|FLAG_ECE)  #define FLAG_FORWARD_PROGRESS	(FLAG_ACKED|FLAG_DATA_SACKED) -#define FLAG_ANY_PROGRESS	(FLAG_FORWARD_PROGRESS|FLAG_SND_UNA_ADVANCED)  #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)  #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH)) @@ -174,7 +173,7 @@ static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)  static void tcp_incr_quickack(struct sock *sk)  {  	struct inet_connection_sock *icsk = inet_csk(sk); -	unsigned quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss); +	unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);  	if (quickacks == 0)  		quickacks = 2; @@ -194,9 +193,10 @@ static void tcp_enter_quickack_mode(struct sock *sk)   * and the session is not interactive.   */ -static inline int tcp_in_quickack_mode(const struct sock *sk) +static inline bool tcp_in_quickack_mode(const struct sock *sk)  {  	const struct inet_connection_sock *icsk = inet_csk(sk); +  	return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong;  } @@ -206,7 +206,7 @@ static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp)  		tp->ecn_flags |= TCP_ECN_QUEUE_CWR;  } -static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, struct sk_buff *skb) +static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)  {  	if (tcp_hdr(skb)->cwr)  		tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR; @@ -217,36 +217,49 @@ static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)  	tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;  } -static inline void TCP_ECN_check_ce(struct tcp_sock *tp, struct sk_buff *skb) +static inline void TCP_ECN_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)  { -	if (tp->ecn_flags & TCP_ECN_OK) { -		if (INET_ECN_is_ce(TCP_SKB_CB(skb)->flags)) -			tp->ecn_flags |= TCP_ECN_DEMAND_CWR; +	if (!(tp->ecn_flags & TCP_ECN_OK)) +		return; + +	switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) { +	case INET_ECN_NOT_ECT:  		/* Funny extension: if ECT is not set on a segment, -		 * it is surely retransmit. It is not in ECN RFC, -		 * but Linux follows this rule. */ -		else if (INET_ECN_is_not_ect((TCP_SKB_CB(skb)->flags))) +		 * and we already seen ECT on a previous segment, +		 * it is probably a retransmit. +		 */ +		if (tp->ecn_flags & TCP_ECN_SEEN) +			tcp_enter_quickack_mode((struct sock *)tp); +		break; +	case INET_ECN_CE: +		if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) { +			/* Better not delay acks, sender can have a very low cwnd */  			tcp_enter_quickack_mode((struct sock *)tp); +			tp->ecn_flags |= TCP_ECN_DEMAND_CWR; +		} +		/* fallinto */ +	default: +		tp->ecn_flags |= TCP_ECN_SEEN;  	}  } -static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, struct tcphdr *th) +static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)  {  	if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))  		tp->ecn_flags &= ~TCP_ECN_OK;  } -static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, struct tcphdr *th) +static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)  {  	if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))  		tp->ecn_flags &= ~TCP_ECN_OK;  } -static inline int TCP_ECN_rcv_ecn_echo(struct tcp_sock *tp, struct tcphdr *th) +static bool TCP_ECN_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)  {  	if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK)) -		return 1; -	return 0; +		return true; +	return false;  }  /* Buffer size and advertised window tuning. @@ -254,16 +267,33 @@ static inline int TCP_ECN_rcv_ecn_echo(struct tcp_sock *tp, struct tcphdr *th)   * 1. Tuning sk->sk_sndbuf, when connection enters established state.   */ -static void tcp_fixup_sndbuf(struct sock *sk) +static void tcp_sndbuf_expand(struct sock *sk)  { -	int sndmem = tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER + 16 + -		     sizeof(struct sk_buff); +	const struct tcp_sock *tp = tcp_sk(sk); +	int sndmem, per_mss; +	u32 nr_segs; -	if (sk->sk_sndbuf < 3 * sndmem) { -		sk->sk_sndbuf = 3 * sndmem; -		if (sk->sk_sndbuf > sysctl_tcp_wmem[2]) -			sk->sk_sndbuf = sysctl_tcp_wmem[2]; -	} +	/* Worst case is non GSO/TSO : each frame consumes one skb +	 * and skb->head is kmalloced using power of two area of memory +	 */ +	per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) + +		  MAX_TCP_HEADER + +		  SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); + +	per_mss = roundup_pow_of_two(per_mss) + +		  SKB_DATA_ALIGN(sizeof(struct sk_buff)); + +	nr_segs = max_t(u32, TCP_INIT_CWND, tp->snd_cwnd); +	nr_segs = max_t(u32, nr_segs, tp->reordering + 1); + +	/* Fast Recovery (RFC 5681 3.2) : +	 * Cubic needs 1.7 factor, rounded to 2 to include +	 * extra cushion (application might react slowly to POLLOUT) +	 */ +	sndmem = 2 * nr_segs * per_mss; + +	if (sk->sk_sndbuf < sndmem) +		sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);  }  /* 2. Tuning advertised window (window_clamp, rcv_ssthresh) @@ -309,14 +339,14 @@ static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)  	return 0;  } -static void tcp_grow_window(struct sock *sk, struct sk_buff *skb) +static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)  {  	struct tcp_sock *tp = tcp_sk(sk);  	/* Check #1 */  	if (tp->rcv_ssthresh < tp->window_clamp &&  	    (int)tp->rcv_ssthresh < tcp_space(sk) && -	    !tcp_memory_pressure) { +	    !sk_under_memory_pressure(sk)) {  		int incr;  		/* Check #2. Increase window, if skb with such overhead @@ -328,6 +358,7 @@ static void tcp_grow_window(struct sock *sk, struct sk_buff *skb)  			incr = __tcp_grow_window(sk, skb);  		if (incr) { +			incr = max_t(int, incr, 2 * skb->len);  			tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,  					       tp->window_clamp);  			inet_csk(sk)->icsk_ack.quick |= 1; @@ -336,26 +367,28 @@ static void tcp_grow_window(struct sock *sk, struct sk_buff *skb)  }  /* 3. Tuning rcvbuf, when connection enters established state. */ -  static void tcp_fixup_rcvbuf(struct sock *sk)  { -	struct tcp_sock *tp = tcp_sk(sk); -	int rcvmem = tp->advmss + MAX_TCP_HEADER + 16 + sizeof(struct sk_buff); +	u32 mss = tcp_sk(sk)->advmss; +	int rcvmem; + +	rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) * +		 tcp_default_init_rwnd(mss); -	/* Try to select rcvbuf so that 4 mss-sized segments -	 * will fit to window and corresponding skbs will fit to our rcvbuf. -	 * (was 3; 4 is minimum to allow fast retransmit to work.) +	/* Dynamic Right Sizing (DRS) has 2 to 3 RTT latency +	 * Allow enough cushion so that sender is not limited by our window  	 */ -	while (tcp_win_from_space(rcvmem) < tp->advmss) -		rcvmem += 128; -	if (sk->sk_rcvbuf < 4 * rcvmem) -		sk->sk_rcvbuf = min(4 * rcvmem, sysctl_tcp_rmem[2]); +	if (sysctl_tcp_moderate_rcvbuf) +		rcvmem <<= 2; + +	if (sk->sk_rcvbuf < rcvmem) +		sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);  }  /* 4. Try to fixup all. It is made immediately after connection enters   *    established state.   */ -static void tcp_init_buffer_space(struct sock *sk) +void tcp_init_buffer_space(struct sock *sk)  {  	struct tcp_sock *tp = tcp_sk(sk);  	int maxwin; @@ -363,9 +396,11 @@ static void tcp_init_buffer_space(struct sock *sk)  	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))  		tcp_fixup_rcvbuf(sk);  	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) -		tcp_fixup_sndbuf(sk); +		tcp_sndbuf_expand(sk);  	tp->rcvq_space.space = tp->rcv_wnd; +	tp->rcvq_space.time = tcp_time_stamp; +	tp->rcvq_space.seq = tp->copied_seq;  	maxwin = tcp_full_space(sk); @@ -398,8 +433,8 @@ static void tcp_clamp_window(struct sock *sk)  	if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&  	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) && -	    !tcp_memory_pressure && -	    atomic_long_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) { +	    !sk_under_memory_pressure(sk) && +	    sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {  		sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),  				    sysctl_tcp_rmem[2]);  	} @@ -416,7 +451,7 @@ static void tcp_clamp_window(struct sock *sk)   */  void tcp_initialize_rcv_mss(struct sock *sk)  { -	struct tcp_sock *tp = tcp_sk(sk); +	const struct tcp_sock *tp = tcp_sk(sk);  	unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);  	hint = min(hint, tp->rcv_wnd / 2); @@ -460,8 +495,11 @@ static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)  		if (!win_dep) {  			m -= (new_sample >> 3);  			new_sample += m; -		} else if (m < new_sample) -			new_sample = m << 3; +		} else { +			m <<= 3; +			if (m < new_sample) +				new_sample = m; +		}  	} else {  		/* No previous measure. */  		new_sample = m << 3; @@ -477,7 +515,7 @@ static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)  		goto new_measure;  	if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))  		return; -	tcp_rcv_rtt_update(tp, jiffies - tp->rcv_rtt_est.time, 1); +	tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rcv_rtt_est.time, 1);  new_measure:  	tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd; @@ -502,49 +540,62 @@ void tcp_rcv_space_adjust(struct sock *sk)  {  	struct tcp_sock *tp = tcp_sk(sk);  	int time; -	int space; - -	if (tp->rcvq_space.time == 0) -		goto new_measure; +	int copied;  	time = tcp_time_stamp - tp->rcvq_space.time;  	if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)  		return; -	space = 2 * (tp->copied_seq - tp->rcvq_space.seq); +	/* Number of bytes copied to user in last RTT */ +	copied = tp->copied_seq - tp->rcvq_space.seq; +	if (copied <= tp->rcvq_space.space) +		goto new_measure; + +	/* A bit of theory : +	 * copied = bytes received in previous RTT, our base window +	 * To cope with packet losses, we need a 2x factor +	 * To cope with slow start, and sender growing its cwin by 100 % +	 * every RTT, we need a 4x factor, because the ACK we are sending +	 * now is for the next RTT, not the current one : +	 * <prev RTT . ><current RTT .. ><next RTT .... > +	 */ + +	if (sysctl_tcp_moderate_rcvbuf && +	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { +		int rcvwin, rcvmem, rcvbuf; -	space = max(tp->rcvq_space.space, space); +		/* minimal window to cope with packet losses, assuming +		 * steady state. Add some cushion because of small variations. +		 */ +		rcvwin = (copied << 1) + 16 * tp->advmss; -	if (tp->rcvq_space.space != space) { -		int rcvmem; +		/* If rate increased by 25%, +		 *	assume slow start, rcvwin = 3 * copied +		 * If rate increased by 50%, +		 *	assume sender can use 2x growth, rcvwin = 4 * copied +		 */ +		if (copied >= +		    tp->rcvq_space.space + (tp->rcvq_space.space >> 2)) { +			if (copied >= +			    tp->rcvq_space.space + (tp->rcvq_space.space >> 1)) +				rcvwin <<= 1; +			else +				rcvwin += (rcvwin >> 1); +		} -		tp->rcvq_space.space = space; +		rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER); +		while (tcp_win_from_space(rcvmem) < tp->advmss) +			rcvmem += 128; -		if (sysctl_tcp_moderate_rcvbuf && -		    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { -			int new_clamp = space; +		rcvbuf = min(rcvwin / tp->advmss * rcvmem, sysctl_tcp_rmem[2]); +		if (rcvbuf > sk->sk_rcvbuf) { +			sk->sk_rcvbuf = rcvbuf; -			/* Receive space grows, normalize in order to -			 * take into account packet headers and sk_buff -			 * structure overhead. -			 */ -			space /= tp->advmss; -			if (!space) -				space = 1; -			rcvmem = (tp->advmss + MAX_TCP_HEADER + -				  16 + sizeof(struct sk_buff)); -			while (tcp_win_from_space(rcvmem) < tp->advmss) -				rcvmem += 128; -			space *= rcvmem; -			space = min(space, sysctl_tcp_rmem[2]); -			if (space > sk->sk_rcvbuf) { -				sk->sk_rcvbuf = space; - -				/* Make the window clamp follow along.  */ -				tp->window_clamp = new_clamp; -			} +			/* Make the window clamp follow along.  */ +			tp->window_clamp = rcvwin;  		}  	} +	tp->rcvq_space.space = copied;  new_measure:  	tp->rcvq_space.seq = tp->copied_seq; @@ -616,10 +667,11 @@ static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)   * To save cycles in the RFC 1323 implementation it was better to break   * it up into three procedures. -- erics   */ -static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt) +static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)  {  	struct tcp_sock *tp = tcp_sk(sk); -	long m = mrtt; /* RTT */ +	long m = mrtt_us; /* RTT */ +	u32 srtt = tp->srtt_us;  	/*	The following amusing code comes from Jacobson's  	 *	article in SIGCOMM '88.  Note that rtt and mdev @@ -637,14 +689,12 @@ static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt)  	 * does not matter how to _calculate_ it. Seems, it was trap  	 * that VJ failed to avoid. 8)  	 */ -	if (m == 0) -		m = 1; -	if (tp->srtt != 0) { -		m -= (tp->srtt >> 3);	/* m is now error in rtt est */ -		tp->srtt += m;		/* rtt = 7/8 rtt + 1/8 new */ +	if (srtt != 0) { +		m -= (srtt >> 3);	/* m is now error in rtt est */ +		srtt += m;		/* rtt = 7/8 rtt + 1/8 new */  		if (m < 0) {  			m = -m;		/* m is now abs(error) */ -			m -= (tp->mdev >> 2);   /* similar update on mdev */ +			m -= (tp->mdev_us >> 2);   /* similar update on mdev */  			/* This is similar to one of Eifel findings.  			 * Eifel blocks mdev updates when rtt decreases.  			 * This solution is a bit different: we use finer gain @@ -656,33 +706,62 @@ static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt)  			if (m > 0)  				m >>= 3;  		} else { -			m -= (tp->mdev >> 2);   /* similar update on mdev */ +			m -= (tp->mdev_us >> 2);   /* similar update on mdev */  		} -		tp->mdev += m;	    	/* mdev = 3/4 mdev + 1/4 new */ -		if (tp->mdev > tp->mdev_max) { -			tp->mdev_max = tp->mdev; -			if (tp->mdev_max > tp->rttvar) -				tp->rttvar = tp->mdev_max; +		tp->mdev_us += m;		/* mdev = 3/4 mdev + 1/4 new */ +		if (tp->mdev_us > tp->mdev_max_us) { +			tp->mdev_max_us = tp->mdev_us; +			if (tp->mdev_max_us > tp->rttvar_us) +				tp->rttvar_us = tp->mdev_max_us;  		}  		if (after(tp->snd_una, tp->rtt_seq)) { -			if (tp->mdev_max < tp->rttvar) -				tp->rttvar -= (tp->rttvar - tp->mdev_max) >> 2; +			if (tp->mdev_max_us < tp->rttvar_us) +				tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2;  			tp->rtt_seq = tp->snd_nxt; -			tp->mdev_max = tcp_rto_min(sk); +			tp->mdev_max_us = tcp_rto_min_us(sk);  		}  	} else {  		/* no previous measure. */ -		tp->srtt = m << 3;	/* take the measured time to be rtt */ -		tp->mdev = m << 1;	/* make sure rto = 3*rtt */ -		tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk)); +		srtt = m << 3;		/* take the measured time to be rtt */ +		tp->mdev_us = m << 1;	/* make sure rto = 3*rtt */ +		tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk)); +		tp->mdev_max_us = tp->rttvar_us;  		tp->rtt_seq = tp->snd_nxt;  	} +	tp->srtt_us = max(1U, srtt); +} + +/* Set the sk_pacing_rate to allow proper sizing of TSO packets. + * Note: TCP stack does not yet implement pacing. + * FQ packet scheduler can be used to implement cheap but effective + * TCP pacing, to smooth the burst on large writes when packets + * in flight is significantly lower than cwnd (or rwin) + */ +static void tcp_update_pacing_rate(struct sock *sk) +{ +	const struct tcp_sock *tp = tcp_sk(sk); +	u64 rate; + +	/* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */ +	rate = (u64)tp->mss_cache * 2 * (USEC_PER_SEC << 3); + +	rate *= max(tp->snd_cwnd, tp->packets_out); + +	if (likely(tp->srtt_us)) +		do_div(rate, tp->srtt_us); + +	/* ACCESS_ONCE() is needed because sch_fq fetches sk_pacing_rate +	 * without any lock. We want to make sure compiler wont store +	 * intermediate values in this location. +	 */ +	ACCESS_ONCE(sk->sk_pacing_rate) = min_t(u64, rate, +						sk->sk_max_pacing_rate);  }  /* Calculate rto without backoff.  This is the second half of Van Jacobson's   * routine referred to above.   */ -static inline void tcp_set_rto(struct sock *sk) +static void tcp_set_rto(struct sock *sk)  {  	const struct tcp_sock *tp = tcp_sk(sk);  	/* Old crap is replaced with new one. 8) @@ -709,228 +788,31 @@ static inline void tcp_set_rto(struct sock *sk)  	tcp_bound_rto(sk);  } -/* Save metrics learned by this TCP session. -   This function is called only, when TCP finishes successfully -   i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE. - */ -void tcp_update_metrics(struct sock *sk) -{ -	struct tcp_sock *tp = tcp_sk(sk); -	struct dst_entry *dst = __sk_dst_get(sk); - -	if (sysctl_tcp_nometrics_save) -		return; - -	dst_confirm(dst); - -	if (dst && (dst->flags & DST_HOST)) { -		const struct inet_connection_sock *icsk = inet_csk(sk); -		int m; -		unsigned long rtt; - -		if (icsk->icsk_backoff || !tp->srtt) { -			/* This session failed to estimate rtt. Why? -			 * Probably, no packets returned in time. -			 * Reset our results. -			 */ -			if (!(dst_metric_locked(dst, RTAX_RTT))) -				dst->metrics[RTAX_RTT - 1] = 0; -			return; -		} - -		rtt = dst_metric_rtt(dst, RTAX_RTT); -		m = rtt - tp->srtt; - -		/* If newly calculated rtt larger than stored one, -		 * store new one. Otherwise, use EWMA. Remember, -		 * rtt overestimation is always better than underestimation. -		 */ -		if (!(dst_metric_locked(dst, RTAX_RTT))) { -			if (m <= 0) -				set_dst_metric_rtt(dst, RTAX_RTT, tp->srtt); -			else -				set_dst_metric_rtt(dst, RTAX_RTT, rtt - (m >> 3)); -		} - -		if (!(dst_metric_locked(dst, RTAX_RTTVAR))) { -			unsigned long var; -			if (m < 0) -				m = -m; - -			/* Scale deviation to rttvar fixed point */ -			m >>= 1; -			if (m < tp->mdev) -				m = tp->mdev; - -			var = dst_metric_rtt(dst, RTAX_RTTVAR); -			if (m >= var) -				var = m; -			else -				var -= (var - m) >> 2; - -			set_dst_metric_rtt(dst, RTAX_RTTVAR, var); -		} - -		if (tcp_in_initial_slowstart(tp)) { -			/* Slow start still did not finish. */ -			if (dst_metric(dst, RTAX_SSTHRESH) && -			    !dst_metric_locked(dst, RTAX_SSTHRESH) && -			    (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH)) -				dst->metrics[RTAX_SSTHRESH-1] = tp->snd_cwnd >> 1; -			if (!dst_metric_locked(dst, RTAX_CWND) && -			    tp->snd_cwnd > dst_metric(dst, RTAX_CWND)) -				dst->metrics[RTAX_CWND - 1] = tp->snd_cwnd; -		} else if (tp->snd_cwnd > tp->snd_ssthresh && -			   icsk->icsk_ca_state == TCP_CA_Open) { -			/* Cong. avoidance phase, cwnd is reliable. */ -			if (!dst_metric_locked(dst, RTAX_SSTHRESH)) -				dst->metrics[RTAX_SSTHRESH-1] = -					max(tp->snd_cwnd >> 1, tp->snd_ssthresh); -			if (!dst_metric_locked(dst, RTAX_CWND)) -				dst->metrics[RTAX_CWND-1] = (dst_metric(dst, RTAX_CWND) + tp->snd_cwnd) >> 1; -		} else { -			/* Else slow start did not finish, cwnd is non-sense, -			   ssthresh may be also invalid. -			 */ -			if (!dst_metric_locked(dst, RTAX_CWND)) -				dst->metrics[RTAX_CWND-1] = (dst_metric(dst, RTAX_CWND) + tp->snd_ssthresh) >> 1; -			if (dst_metric(dst, RTAX_SSTHRESH) && -			    !dst_metric_locked(dst, RTAX_SSTHRESH) && -			    tp->snd_ssthresh > dst_metric(dst, RTAX_SSTHRESH)) -				dst->metrics[RTAX_SSTHRESH-1] = tp->snd_ssthresh; -		} - -		if (!dst_metric_locked(dst, RTAX_REORDERING)) { -			if (dst_metric(dst, RTAX_REORDERING) < tp->reordering && -			    tp->reordering != sysctl_tcp_reordering) -				dst->metrics[RTAX_REORDERING-1] = tp->reordering; -		} -	} -} - -__u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst) +__u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)  {  	__u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);  	if (!cwnd) -		cwnd = rfc3390_bytes_to_packets(tp->mss_cache); +		cwnd = TCP_INIT_CWND;  	return min_t(__u32, cwnd, tp->snd_cwnd_clamp);  } -/* Set slow start threshold and cwnd not falling to slow start */ -void tcp_enter_cwr(struct sock *sk, const int set_ssthresh) -{ -	struct tcp_sock *tp = tcp_sk(sk); -	const struct inet_connection_sock *icsk = inet_csk(sk); - -	tp->prior_ssthresh = 0; -	tp->bytes_acked = 0; -	if (icsk->icsk_ca_state < TCP_CA_CWR) { -		tp->undo_marker = 0; -		if (set_ssthresh) -			tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk); -		tp->snd_cwnd = min(tp->snd_cwnd, -				   tcp_packets_in_flight(tp) + 1U); -		tp->snd_cwnd_cnt = 0; -		tp->high_seq = tp->snd_nxt; -		tp->snd_cwnd_stamp = tcp_time_stamp; -		TCP_ECN_queue_cwr(tp); - -		tcp_set_ca_state(sk, TCP_CA_CWR); -	} -} -  /*   * Packet counting of FACK is based on in-order assumptions, therefore TCP   * disables it when reordering is detected   */ -static void tcp_disable_fack(struct tcp_sock *tp) +void tcp_disable_fack(struct tcp_sock *tp)  {  	/* RFC3517 uses different metric in lost marker => reset on change */  	if (tcp_is_fack(tp))  		tp->lost_skb_hint = NULL; -	tp->rx_opt.sack_ok &= ~2; +	tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED;  }  /* Take a notice that peer is sending D-SACKs */  static void tcp_dsack_seen(struct tcp_sock *tp)  { -	tp->rx_opt.sack_ok |= 4; -} - -/* Initialize metrics on socket. */ - -static void tcp_init_metrics(struct sock *sk) -{ -	struct tcp_sock *tp = tcp_sk(sk); -	struct dst_entry *dst = __sk_dst_get(sk); - -	if (dst == NULL) -		goto reset; - -	dst_confirm(dst); - -	if (dst_metric_locked(dst, RTAX_CWND)) -		tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND); -	if (dst_metric(dst, RTAX_SSTHRESH)) { -		tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH); -		if (tp->snd_ssthresh > tp->snd_cwnd_clamp) -			tp->snd_ssthresh = tp->snd_cwnd_clamp; -	} -	if (dst_metric(dst, RTAX_REORDERING) && -	    tp->reordering != dst_metric(dst, RTAX_REORDERING)) { -		tcp_disable_fack(tp); -		tp->reordering = dst_metric(dst, RTAX_REORDERING); -	} - -	if (dst_metric(dst, RTAX_RTT) == 0) -		goto reset; - -	if (!tp->srtt && dst_metric_rtt(dst, RTAX_RTT) < (TCP_TIMEOUT_INIT << 3)) -		goto reset; - -	/* Initial rtt is determined from SYN,SYN-ACK. -	 * The segment is small and rtt may appear much -	 * less than real one. Use per-dst memory -	 * to make it more realistic. -	 * -	 * A bit of theory. RTT is time passed after "normal" sized packet -	 * is sent until it is ACKed. In normal circumstances sending small -	 * packets force peer to delay ACKs and calculation is correct too. -	 * The algorithm is adaptive and, provided we follow specs, it -	 * NEVER underestimate RTT. BUT! If peer tries to make some clever -	 * tricks sort of "quick acks" for time long enough to decrease RTT -	 * to low value, and then abruptly stops to do it and starts to delay -	 * ACKs, wait for troubles. -	 */ -	if (dst_metric_rtt(dst, RTAX_RTT) > tp->srtt) { -		tp->srtt = dst_metric_rtt(dst, RTAX_RTT); -		tp->rtt_seq = tp->snd_nxt; -	} -	if (dst_metric_rtt(dst, RTAX_RTTVAR) > tp->mdev) { -		tp->mdev = dst_metric_rtt(dst, RTAX_RTTVAR); -		tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk)); -	} -	tcp_set_rto(sk); -	if (inet_csk(sk)->icsk_rto < TCP_TIMEOUT_INIT && !tp->rx_opt.saw_tstamp) -		goto reset; - -cwnd: -	tp->snd_cwnd = tcp_init_cwnd(tp, dst); -	tp->snd_cwnd_stamp = tcp_time_stamp; -	return; - -reset: -	/* Play conservative. If timestamps are not -	 * supported, TCP will fail to recalculate correct -	 * rtt, if initial rto is too small. FORGET ALL AND RESET! -	 */ -	if (!tp->rx_opt.saw_tstamp && tp->srtt) { -		tp->srtt = 0; -		tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_INIT; -		inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT; -	} -	goto cwnd; +	tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;  }  static void tcp_update_reordering(struct sock *sk, const int metric, @@ -954,15 +836,18 @@ static void tcp_update_reordering(struct sock *sk, const int metric,  		NET_INC_STATS_BH(sock_net(sk), mib_idx);  #if FASTRETRANS_DEBUG > 1 -		printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n", -		       tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state, -		       tp->reordering, -		       tp->fackets_out, -		       tp->sacked_out, -		       tp->undo_marker ? tp->undo_retrans : 0); +		pr_debug("Disorder%d %d %u f%u s%u rr%d\n", +			 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state, +			 tp->reordering, +			 tp->fackets_out, +			 tp->sacked_out, +			 tp->undo_marker ? tp->undo_retrans : 0);  #endif  		tcp_disable_fack(tp);  	} + +	if (metric > 0) +		tcp_disable_early_retrans(tp);  }  /* This must be called before lost_out is incremented */ @@ -1020,13 +905,11 @@ static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp,   * 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: + * 3. Loss detection event of two flavors:   *	A. Scoreboard estimator decided the packet is lost.   *	   A'. Reno "three dupacks" marks head of queue lost. - *	   A''. Its FACK modfication, head until snd.fack is lost. - *	B. SACK arrives sacking data transmitted after never retransmitted - *	   hole was sent out. - *	C. SACK arrives sacking SND.NXT at the moment, when the + *	   A''. Its FACK modification, head until snd.fack is lost. + *	B. 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.   * @@ -1095,36 +978,36 @@ static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp,   * the exact amount is rather hard to quantify. However, tp->max_window can   * be used as an exaggerated estimate.   */ -static int tcp_is_sackblock_valid(struct tcp_sock *tp, int is_dsack, -				  u32 start_seq, u32 end_seq) +static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack, +				   u32 start_seq, u32 end_seq)  {  	/* Too far in future, or reversed (interpretation is ambiguous) */  	if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq)) -		return 0; +		return false;  	/* Nasty start_seq wrap-around check (see comments above) */  	if (!before(start_seq, tp->snd_nxt)) -		return 0; +		return false;  	/* In outstanding window? ...This is valid exit for D-SACKs too.  	 * start_seq == snd_una is non-sensical (see comments above)  	 */  	if (after(start_seq, tp->snd_una)) -		return 1; +		return true;  	if (!is_dsack || !tp->undo_marker) -		return 0; +		return false;  	/* ...Then it's D-SACK, and must reside below snd_una completely */ -	if (!after(end_seq, tp->snd_una)) -		return 0; +	if (after(end_seq, tp->snd_una)) +		return false;  	if (!before(start_seq, tp->undo_marker)) -		return 1; +		return true;  	/* Too old */  	if (!after(end_seq, tp->undo_marker)) -		return 0; +		return false;  	/* Undo_marker boundary crossing (overestimates a lot). Known already:  	 *   start_seq < undo_marker and end_seq >= undo_marker. @@ -1133,7 +1016,7 @@ static int tcp_is_sackblock_valid(struct tcp_sock *tp, int is_dsack,  }  /* Check for lost retransmit. This superb idea is borrowed from "ratehalving". - * Event "C". Later note: FACK people cheated me again 8), we have to account + * Event "B". Later note: FACK people cheated me again 8), we have to account   * for reordering! Ugly, but should help.   *   * Search retransmitted skbs from write_queue that were sent when snd_nxt was @@ -1196,17 +1079,17 @@ static void tcp_mark_lost_retrans(struct sock *sk)  		tp->lost_retrans_low = new_low_seq;  } -static int tcp_check_dsack(struct sock *sk, struct sk_buff *ack_skb, -			   struct tcp_sack_block_wire *sp, int num_sacks, -			   u32 prior_snd_una) +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); -	int dup_sack = 0; +	bool dup_sack = false;  	if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) { -		dup_sack = 1; +		dup_sack = true;  		tcp_dsack_seen(tp);  		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV);  	} else if (num_sacks > 1) { @@ -1215,7 +1098,7 @@ static int tcp_check_dsack(struct sock *sk, struct sk_buff *ack_skb,  		if (!after(end_seq_0, end_seq_1) &&  		    !before(start_seq_0, start_seq_1)) { -			dup_sack = 1; +			dup_sack = true;  			tcp_dsack_seen(tp);  			NET_INC_STATS_BH(sock_net(sk),  					LINUX_MIB_TCPDSACKOFORECV); @@ -1223,7 +1106,7 @@ static int tcp_check_dsack(struct sock *sk, struct sk_buff *ack_skb,  	}  	/* D-SACK for already forgotten data... Do dumb counting. */ -	if (dup_sack && +	if (dup_sack && tp->undo_marker && tp->undo_retrans > 0 &&  	    !after(end_seq_0, prior_snd_una) &&  	    after(end_seq_0, tp->undo_marker))  		tp->undo_retrans--; @@ -1232,9 +1115,10 @@ static int tcp_check_dsack(struct sock *sk, struct sk_buff *ack_skb,  }  struct tcp_sacktag_state { -	int reord; -	int fack_count; -	int flag; +	int	reord; +	int	fack_count; +	long	rtt_us; /* RTT measured by SACKing never-retransmitted data */ +	int	flag;  };  /* Check if skb is fully within the SACK block. In presence of GSO skbs, @@ -1246,9 +1130,10 @@ struct tcp_sacktag_state {   * FIXME: this could be merged to shift decision code   */  static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb, -				 u32 start_seq, u32 end_seq) +				  u32 start_seq, u32 end_seq)  { -	int in_sack, err; +	int err; +	bool in_sack;  	unsigned int pkt_len;  	unsigned int mss; @@ -1277,12 +1162,12 @@ static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,  			unsigned int new_len = (pkt_len / mss) * mss;  			if (!in_sack && new_len < pkt_len) {  				new_len += mss; -				if (new_len > skb->len) +				if (new_len >= skb->len)  					return 0;  			}  			pkt_len = new_len;  		} -		err = tcp_fragment(sk, skb, pkt_len, mss); +		err = tcp_fragment(sk, skb, pkt_len, mss, GFP_ATOMIC);  		if (err < 0)  			return err;  	} @@ -1290,24 +1175,27 @@ static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,  	return in_sack;  } -static u8 tcp_sacktag_one(struct sk_buff *skb, struct sock *sk, -			  struct tcp_sacktag_state *state, -			  int dup_sack, int pcount) +/* 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, +			  int dup_sack, int pcount, +			  const struct skb_mstamp *xmit_time)  {  	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. */  	if (dup_sack && (sacked & TCPCB_RETRANS)) { -		if (after(TCP_SKB_CB(skb)->end_seq, tp->undo_marker)) +		if (tp->undo_marker && tp->undo_retrans > 0 && +		    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(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) +	if (!after(end_seq, tp->snd_una))  		return sacked;  	if (!(sacked & TCPCB_SACKED_ACKED)) { @@ -1326,14 +1214,20 @@ static u8 tcp_sacktag_one(struct sk_buff *skb, struct sock *sk,  				/* New sack for not retransmitted frame,  				 * which was in hole. It is reordering.  				 */ -				if (before(TCP_SKB_CB(skb)->seq, +				if (before(start_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; +				if (!after(end_seq, tp->high_seq)) +					state->flag |= FLAG_ORIG_SACK_ACKED; +				/* Pick the earliest sequence sacked for RTT */ +				if (state->rtt_us < 0) { +					struct skb_mstamp now; + +					skb_mstamp_get(&now); +					state->rtt_us = skb_mstamp_us_delta(&now, +								xmit_time); +				}  			}  			if (sacked & TCPCB_LOST) { @@ -1350,8 +1244,7 @@ static u8 tcp_sacktag_one(struct sk_buff *skb, struct sock *sk,  		/* Lost marker hint past SACKed? Tweak RFC3517 cnt */  		if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) && -		    before(TCP_SKB_CB(skb)->seq, -			   TCP_SKB_CB(tp->lost_skb_hint)->seq)) +		    before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))  			tp->lost_cnt_hint += pcount;  		if (fack_count > tp->fackets_out) @@ -1370,19 +1263,32 @@ static u8 tcp_sacktag_one(struct sk_buff *skb, struct sock *sk,  	return sacked;  } -static int tcp_shifted_skb(struct sock *sk, struct sk_buff *skb, -			   struct tcp_sacktag_state *state, -			   unsigned int pcount, int shifted, int mss, -			   int dup_sack) +/* Shift newly-SACKed bytes from this skb to the immediately previous + * already-SACKed sk_buff. Mark the newly-SACKed bytes as such. + */ +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 */  	BUG_ON(!pcount); -	/* Tweak before seqno plays */ -	if (!tcp_is_fack(tp) && tcp_is_sack(tp) && tp->lost_skb_hint && -	    !before(TCP_SKB_CB(tp->lost_skb_hint)->seq, TCP_SKB_CB(skb)->seq)) +	/* 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, +			&skb->skb_mstamp); + +	if (skb == tp->lost_skb_hint)  		tp->lost_cnt_hint += pcount;  	TCP_SKB_CB(prev)->end_seq += shifted; @@ -1408,30 +1314,28 @@ static int tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,  		skb_shinfo(skb)->gso_type = 0;  	} -	/* We discard results */ -	tcp_sacktag_one(skb, sk, state, dup_sack, pcount); -  	/* 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); -		return 0; +		return false;  	}  	/* Whole SKB was eaten :-) */  	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)->flags |= TCP_SKB_CB(prev)->flags; +	TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags; +	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) +		TCP_SKB_CB(prev)->end_seq++; +  	if (skb == tcp_highest_sack(sk))  		tcp_advance_highest_sack(sk, skb); @@ -1440,19 +1344,19 @@ static int tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,  	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED); -	return 1; +	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(struct sk_buff *skb) +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(struct sk_buff *skb) +static int skb_can_shift(const struct sk_buff *skb)  {  	return !skb_headlen(skb) && skb_is_nonlinear(skb);  } @@ -1463,7 +1367,7 @@ static int skb_can_shift(struct sk_buff *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, -					  int dup_sack) +					  bool dup_sack)  {  	struct tcp_sock *tp = tcp_sk(sk);  	struct sk_buff *prev; @@ -1558,6 +1462,10 @@ static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,  		}  	} +	/* 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)) @@ -1598,14 +1506,14 @@ 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, -					int dup_sack_in) +					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; -		int dup_sack = dup_sack_in; +		bool dup_sack = dup_sack_in;  		if (skb == tcp_send_head(sk))  			break; @@ -1620,7 +1528,7 @@ static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,  							next_dup->start_seq,  							next_dup->end_seq);  			if (in_sack > 0) -				dup_sack = 1; +				dup_sack = true;  		}  		/* skb reference here is a bit tricky to get right, since @@ -1648,10 +1556,15 @@ static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,  			break;  		if (in_sack) { -			TCP_SKB_CB(skb)->sacked = tcp_sacktag_one(skb, sk, -								  state, -								  dup_sack, -								  tcp_skb_pcount(skb)); +			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), +						&skb->skb_mstamp);  			if (!before(TCP_SKB_CB(skb)->seq,  				    tcp_highest_sack_seq(tp))) @@ -1701,19 +1614,18 @@ static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,  	return skb;  } -static int tcp_sack_cache_ok(struct tcp_sock *tp, struct tcp_sack_block *cache) +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);  }  static int -tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb, -			u32 prior_snd_una) +tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb, +			u32 prior_snd_una, long *sack_rtt_us)  { -	const struct inet_connection_sock *icsk = inet_csk(sk);  	struct tcp_sock *tp = tcp_sk(sk); -	unsigned char *ptr = (skb_transport_header(ack_skb) + -			      TCP_SKB_CB(ack_skb)->sacked); +	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; @@ -1721,12 +1633,13 @@ tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb,  	struct sk_buff *skb;  	int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);  	int used_sacks; -	int found_dup_sack = 0; +	bool found_dup_sack = false;  	int i, j;  	int first_sack_index;  	state.flag = 0;  	state.reord = tp->packets_out; +	state.rtt_us = -1L;  	if (!tp->sacked_out) {  		if (WARN_ON(tp->fackets_out)) @@ -1752,7 +1665,7 @@ tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb,  	used_sacks = 0;  	first_sack_index = 0;  	for (i = 0; i < num_sacks; i++) { -		int dup_sack = !i && found_dup_sack; +		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); @@ -1819,16 +1732,12 @@ tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb,  	while (i < used_sacks) {  		u32 start_seq = sp[i].start_seq;  		u32 end_seq = sp[i].end_seq; -		int dup_sack = (found_dup_sack && (i == first_sack_index)); +		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]; -		/* Event "B" in the comment above. */ -		if (after(end_seq, tp->high_seq)) -			state.flag |= FLAG_DATA_LOST; -  		/* Skip too early cached blocks */  		while (tcp_sack_cache_ok(tp, cache) &&  		       !before(start_seq, cache->end_seq)) @@ -1887,12 +1796,6 @@ walk:  				       start_seq, end_seq, dup_sack);  advance_sp: -		/* 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; -  		i++;  	} @@ -1909,8 +1812,7 @@ advance_sp:  	tcp_verify_left_out(tp);  	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))) +	    ((inet_csk(sk)->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker))  		tcp_update_reordering(sk, tp->fackets_out - state.reord, 0);  out: @@ -1921,13 +1823,14 @@ out:  	WARN_ON((int)tp->retrans_out < 0);  	WARN_ON((int)tcp_packets_in_flight(tp) < 0);  #endif +	*sack_rtt_us = state.rtt_us;  	return state.flag;  }  /* Limits sacked_out so that sum with lost_out isn't ever larger than - * packets_out. Returns zero if sacked_out adjustement wasn't necessary. + * packets_out. Returns false if sacked_out adjustement wasn't necessary.   */ -static int tcp_limit_reno_sacked(struct tcp_sock *tp) +static bool tcp_limit_reno_sacked(struct tcp_sock *tp)  {  	u32 holes; @@ -1936,9 +1839,9 @@ static int tcp_limit_reno_sacked(struct tcp_sock *tp)  	if ((tp->sacked_out + holes) > tp->packets_out) {  		tp->sacked_out = tp->packets_out - holes; -		return 1; +		return true;  	} -	return 0; +	return false;  }  /* If we receive more dupacks than we expected counting segments @@ -1984,205 +1887,13 @@ 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) - */ -int tcp_use_frto(struct sock *sk) -{ -	const struct tcp_sock *tp = tcp_sk(sk); -	const struct inet_connection_sock *icsk = inet_csk(sk); -	struct sk_buff *skb; - -	if (!sysctl_tcp_frto) -		return 0; - -	/* MTU probe and F-RTO won't really play nicely along currently */ -	if (icsk->icsk_mtup.probe_size) -		return 0; - -	if (tcp_is_sackfrto(tp)) -		return 1; - -	/* Avoid expensive walking of rexmit queue if possible */ -	if (tp->retrans_out > 1) -		return 0; - -	skb = tcp_write_queue_head(sk); -	if (tcp_skb_is_last(sk, skb)) -		return 1; -	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) -			return 0; -		/* Short-circuit when first non-SACKed skb has been checked */ -		if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) -			break; -	} -	return 1; -} - -/* 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." - */ -void tcp_enter_frto(struct sock *sk) -{ -	const struct inet_connection_sock *icsk = inet_csk(sk); -	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); -	} - -	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); -	} -	tcp_verify_left_out(tp); - -	/* 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; -	tp->frto_counter = 1; -} - -/* 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) -{ -	struct tcp_sock *tp = tcp_sk(sk); -	struct sk_buff *skb; - -	tp->lost_out = 0; -	tp->retrans_out = 0; -	if (tcp_is_reno(tp)) -		tcp_reset_reno_sack(tp); - -	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; -		} - -		/* 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; -		} -	} -	tcp_verify_left_out(tp); - -	tp->snd_cwnd = tcp_packets_in_flight(tp) + allowed_segments; -	tp->snd_cwnd_cnt = 0; -	tp->snd_cwnd_stamp = tcp_time_stamp; -	tp->frto_counter = 0; -	tp->bytes_acked = 0; - -	tp->reordering = min_t(unsigned int, tp->reordering, -			       sysctl_tcp_reordering); -	tcp_set_ca_state(sk, TCP_CA_Loss); -	tp->high_seq = tp->snd_nxt; -	TCP_ECN_queue_cwr(tp); - -	tcp_clear_all_retrans_hints(tp); -} -  static void tcp_clear_retrans_partial(struct tcp_sock *tp)  {  	tp->retrans_out = 0;  	tp->lost_out = 0;  	tp->undo_marker = 0; -	tp->undo_retrans = 0; +	tp->undo_retrans = -1;  }  void tcp_clear_retrans(struct tcp_sock *tp) @@ -2202,10 +1913,13 @@ void tcp_enter_loss(struct sock *sk, int how)  	const struct inet_connection_sock *icsk = inet_csk(sk);  	struct tcp_sock *tp = tcp_sk(sk);  	struct sk_buff *skb; +	bool new_recovery = false;  	/* 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 || +	if (icsk->icsk_ca_state <= TCP_CA_Disorder || +	    !after(tp->high_seq, tp->snd_una) ||  	    (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) { +		new_recovery = true;  		tp->prior_ssthresh = tcp_current_ssthresh(sk);  		tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);  		tcp_ca_event(sk, CA_EVENT_LOSS); @@ -2214,17 +1928,13 @@ void tcp_enter_loss(struct sock *sk, int how)  	tp->snd_cwnd_cnt   = 0;  	tp->snd_cwnd_stamp = tcp_time_stamp; -	tp->bytes_acked = 0;  	tcp_clear_retrans_partial(tp);  	if (tcp_is_reno(tp))  		tcp_reset_reno_sack(tp); -	if (!how) { -		/* Push undo marker, if it was plain RTO and nothing -		 * was retransmitted. */ -		tp->undo_marker = tp->snd_una; -	} else { +	tp->undo_marker = tp->snd_una; +	if (how) {  		tp->sacked_out = 0;  		tp->fackets_out = 0;  	} @@ -2234,8 +1944,9 @@ void tcp_enter_loss(struct sock *sk, int how)  		if (skb == tcp_send_head(sk))  			break; -		if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) +		if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)  			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; @@ -2246,13 +1957,24 @@ void tcp_enter_loss(struct sock *sk, int how)  	}  	tcp_verify_left_out(tp); -	tp->reordering = min_t(unsigned int, tp->reordering, -			       sysctl_tcp_reordering); +	/* Timeout in disordered state after receiving substantial DUPACKs +	 * suggests that the degree of reordering is over-estimated. +	 */ +	if (icsk->icsk_ca_state <= TCP_CA_Disorder && +	    tp->sacked_out >= sysctl_tcp_reordering) +		tp->reordering = min_t(unsigned int, tp->reordering, +				       sysctl_tcp_reordering);  	tcp_set_ca_state(sk, TCP_CA_Loss);  	tp->high_seq = tp->snd_nxt;  	TCP_ECN_queue_cwr(tp); -	/* Abort F-RTO algorithm if one is in progress */ -	tp->frto_counter = 0; + +	/* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous +	 * loss recovery is underway except recurring timeout(s) on +	 * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing +	 */ +	tp->frto = sysctl_tcp_frto && +		   (new_recovery || icsk->icsk_retransmits) && +		   !inet_csk(sk)->icsk_mtup.probe_size;  }  /* If ACK arrived pointing to a remembered SACK, it means that our @@ -2261,7 +1983,7 @@ void tcp_enter_loss(struct sock *sk, int how)   *   * Do processing similar to RTO timeout.   */ -static int tcp_check_sack_reneging(struct sock *sk, int flag) +static bool tcp_check_sack_reneging(struct sock *sk, int flag)  {  	if (flag & FLAG_SACK_RENEGING) {  		struct inet_connection_sock *icsk = inet_csk(sk); @@ -2272,12 +1994,12 @@ static int tcp_check_sack_reneging(struct sock *sk, int flag)  		tcp_retransmit_skb(sk, tcp_write_queue_head(sk));  		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,  					  icsk->icsk_rto, TCP_RTO_MAX); -		return 1; +		return true;  	} -	return 0; +	return false;  } -static inline int tcp_fackets_out(struct tcp_sock *tp) +static inline int tcp_fackets_out(const struct tcp_sock *tp)  {  	return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out;  } @@ -2297,22 +2019,33 @@ static inline int tcp_fackets_out(struct tcp_sock *tp)   * they differ. Since neither occurs due to loss, TCP should really   * ignore them.   */ -static inline int tcp_dupack_heuristics(struct tcp_sock *tp) +static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)  {  	return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1;  } -static inline int tcp_skb_timedout(struct sock *sk, struct sk_buff *skb) -{ -	return tcp_time_stamp - TCP_SKB_CB(skb)->when > inet_csk(sk)->icsk_rto; -} - -static inline int tcp_head_timedout(struct sock *sk) +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_us) +		return false; + +	delay = max(usecs_to_jiffies(tp->srtt_us >> 5), +		    msecs_to_jiffies(2)); -	return tp->packets_out && -	       tcp_skb_timedout(sk, tcp_write_queue_head(sk)); +	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); +	return true;  }  /* Linux NewReno/SACK/FACK/ECN state machine. @@ -2408,28 +2141,18 @@ static inline int tcp_head_timedout(struct sock *sk)   * Main question: may we further continue forward transmission   * with the same cwnd?   */ -static int tcp_time_to_recover(struct sock *sk) +static bool tcp_time_to_recover(struct sock *sk, int flag)  {  	struct tcp_sock *tp = tcp_sk(sk);  	__u32 packets_out; -	/* Do not perform any recovery during F-RTO algorithm */ -	if (tp->frto_counter) -		return 0; -  	/* Trick#1: The loss is proven. */  	if (tp->lost_out) -		return 1; +		return true;  	/* Not-A-Trick#2 : Classic rule... */  	if (tcp_dupack_heuristics(tp) > tp->reordering) -		return 1; - -	/* Trick#3 : when we use RFC2988 timer restart, fast -	 * retransmit can be triggered by timeout of queue head. -	 */ -	if (tcp_is_fack(tp) && tcp_head_timedout(sk)) -		return 1; +		return true;  	/* Trick#4: It is still not OK... But will it be useful to delay  	 * recovery more? @@ -2441,7 +2164,7 @@ static int tcp_time_to_recover(struct sock *sk)  		/* We have nothing to send. This connection is limited  		 * either by receiver window or by application.  		 */ -		return 1; +		return true;  	}  	/* If a thin stream is detected, retransmit after first @@ -2452,51 +2175,26 @@ static int tcp_time_to_recover(struct sock *sk)  	if ((tp->thin_dupack || sysctl_tcp_thin_dupack) &&  	    tcp_stream_is_thin(tp) && tcp_dupack_heuristics(tp) > 1 &&  	    tcp_is_sack(tp) && !tcp_send_head(sk)) -		return 1; - -	return 0; -} - -/* New heuristics: it is possible only after we switched to restart timer - * each time when something is ACKed. Hence, we can detect timed out packets - * during fast retransmit without falling to slow start. - * - * Usefulness of this as is very questionable, since we should know which of - * the segments is the next to timeout which is relatively expensive to find - * in general case unless we add some data structure just for that. The - * current approach certainly won't find the right one too often and when it - * finally does find _something_ it usually marks large part of the window - * right away (because a retransmission with a larger timestamp blocks the - * loop from advancing). -ij - */ -static void tcp_timeout_skbs(struct sock *sk) -{ -	struct tcp_sock *tp = tcp_sk(sk); -	struct sk_buff *skb; - -	if (!tcp_is_fack(tp) || !tcp_head_timedout(sk)) -		return; - -	skb = tp->scoreboard_skb_hint; -	if (tp->scoreboard_skb_hint == NULL) -		skb = tcp_write_queue_head(sk); - -	tcp_for_write_queue_from(skb, sk) { -		if (skb == tcp_send_head(sk)) -			break; -		if (!tcp_skb_timedout(sk, skb)) -			break; - -		tcp_skb_mark_lost(tp, skb); -	} +		return true; -	tp->scoreboard_skb_hint = skb; +	/* Trick#6: TCP early retransmit, per RFC5827.  To avoid spurious +	 * retransmissions due to small network reorderings, we implement +	 * Mitigation A.3 in the RFC and delay the retransmission for a short +	 * interval if appropriate. +	 */ +	if (tp->do_early_retrans && !tp->retrans_out && tp->sacked_out && +	    (tp->packets_out >= (tp->sacked_out + 1) && tp->packets_out < 4) && +	    !tcp_may_send_now(sk)) +		return !tcp_pause_early_retransmit(sk, flag); -	tcp_verify_left_out(tp); +	return false;  } -/* Mark head of queue up as lost. With RFC3517 SACK, the packets is - * is against sacked "cnt", otherwise it's against facked "cnt" +/* Detect loss in event "A" above by marking head of queue up as lost. + * For FACK or non-SACK(Reno) senders, the first "packets" number of segments + * are considered lost. For RFC3517 SACK, a segment is considered lost if it + * has at least tp->reordering SACKed seqments above it; "packets" refers to + * the maximum SACKed segments to pass before reaching this limit.   */  static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)  { @@ -2505,6 +2203,8 @@ static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)  	int cnt, oldcnt;  	int err;  	unsigned int mss; +	/* Use SACK to deduce losses of new sequences sent during recovery */ +	const u32 loss_high = tcp_is_sack(tp) ?  tp->snd_nxt : tp->high_seq;  	WARN_ON(packets > tp->packets_out);  	if (tp->lost_skb_hint) { @@ -2526,7 +2226,7 @@ static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)  		tp->lost_skb_hint = skb;  		tp->lost_cnt_hint = cnt; -		if (after(TCP_SKB_CB(skb)->end_seq, tp->high_seq)) +		if (after(TCP_SKB_CB(skb)->end_seq, loss_high))  			break;  		oldcnt = cnt; @@ -2536,11 +2236,13 @@ static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)  		if (cnt > packets) {  			if ((tcp_is_sack(tp) && !tcp_is_fack(tp)) || +			    (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||  			    (oldcnt >= packets))  				break;  			mss = skb_shinfo(skb)->gso_size; -			err = tcp_fragment(sk, skb, (packets - oldcnt) * mss, mss); +			err = tcp_fragment(sk, skb, (packets - oldcnt) * mss, +					   mss, GFP_ATOMIC);  			if (err < 0)  				break;  			cnt = packets; @@ -2574,8 +2276,6 @@ static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)  		else if (fast_rexmit)  			tcp_mark_head_lost(sk, 1, 1);  	} - -	tcp_timeout_skbs(sk);  }  /* CWND moderation, preventing bursts due to too big ACKs @@ -2588,39 +2288,10 @@ static inline void tcp_moderate_cwnd(struct tcp_sock *tp)  	tp->snd_cwnd_stamp = tcp_time_stamp;  } -/* Lower bound on congestion window is slow start threshold - * unless congestion avoidance choice decides to overide it. - */ -static inline u32 tcp_cwnd_min(const struct sock *sk) -{ -	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; - -	return ca_ops->min_cwnd ? ca_ops->min_cwnd(sk) : tcp_sk(sk)->snd_ssthresh; -} - -/* Decrease cwnd each second ack. */ -static void tcp_cwnd_down(struct sock *sk, int flag) -{ -	struct tcp_sock *tp = tcp_sk(sk); -	int decr = tp->snd_cwnd_cnt + 1; - -	if ((flag & (FLAG_ANY_PROGRESS | FLAG_DSACKING_ACK)) || -	    (tcp_is_reno(tp) && !(flag & FLAG_NOT_DUP))) { -		tp->snd_cwnd_cnt = decr & 1; -		decr >>= 1; - -		if (decr && tp->snd_cwnd > tcp_cwnd_min(sk)) -			tp->snd_cwnd -= decr; - -		tp->snd_cwnd = min(tp->snd_cwnd, tcp_packets_in_flight(tp) + 1); -		tp->snd_cwnd_stamp = tcp_time_stamp; -	} -} -  /* Nothing was retransmitted or returned timestamp is less   * than timestamp of the first retransmission.   */ -static inline int tcp_packet_delayed(struct tcp_sock *tp) +static inline bool tcp_packet_delayed(const struct tcp_sock *tp)  {  	return !tp->retrans_stamp ||  		(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && @@ -2636,22 +2307,22 @@ static void DBGUNDO(struct sock *sk, const char *msg)  	struct inet_sock *inet = inet_sk(sk);  	if (sk->sk_family == AF_INET) { -		printk(KERN_DEBUG "Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n", -		       msg, -		       &inet->inet_daddr, ntohs(inet->inet_dport), -		       tp->snd_cwnd, tcp_left_out(tp), -		       tp->snd_ssthresh, tp->prior_ssthresh, -		       tp->packets_out); -	} -#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) +		pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n", +			 msg, +			 &inet->inet_daddr, ntohs(inet->inet_dport), +			 tp->snd_cwnd, tcp_left_out(tp), +			 tp->snd_ssthresh, tp->prior_ssthresh, +			 tp->packets_out); +	} +#if IS_ENABLED(CONFIG_IPV6)  	else if (sk->sk_family == AF_INET6) {  		struct ipv6_pinfo *np = inet6_sk(sk); -		printk(KERN_DEBUG "Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n", -		       msg, -		       &np->daddr, ntohs(inet->inet_dport), -		       tp->snd_cwnd, tcp_left_out(tp), -		       tp->snd_ssthresh, tp->prior_ssthresh, -		       tp->packets_out); +		pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n", +			 msg, +			 &np->daddr, ntohs(inet->inet_dport), +			 tp->snd_cwnd, tcp_left_out(tp), +			 tp->snd_ssthresh, tp->prior_ssthresh, +			 tp->packets_out);  	}  #endif  } @@ -2659,10 +2330,22 @@ static void DBGUNDO(struct sock *sk, const char *msg)  #define DBGUNDO(x...) do { } while (0)  #endif -static void tcp_undo_cwr(struct sock *sk, const int undo) +static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss)  {  	struct tcp_sock *tp = tcp_sk(sk); +	if (unmark_loss) { +		struct sk_buff *skb; + +		tcp_for_write_queue(skb, sk) { +			if (skb == tcp_send_head(sk)) +				break; +			TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST; +		} +		tp->lost_out = 0; +		tcp_clear_all_retrans_hints(tp); +	} +  	if (tp->prior_ssthresh) {  		const struct inet_connection_sock *icsk = inet_csk(sk); @@ -2671,24 +2354,24 @@ static void tcp_undo_cwr(struct sock *sk, const int undo)  		else  			tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh << 1); -		if (undo && tp->prior_ssthresh > tp->snd_ssthresh) { +		if (tp->prior_ssthresh > tp->snd_ssthresh) {  			tp->snd_ssthresh = tp->prior_ssthresh;  			TCP_ECN_withdraw_cwr(tp);  		}  	} else {  		tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh);  	} -	tcp_moderate_cwnd(tp);  	tp->snd_cwnd_stamp = tcp_time_stamp; +	tp->undo_marker = 0;  } -static inline int tcp_may_undo(struct tcp_sock *tp) +static inline bool tcp_may_undo(const struct tcp_sock *tp)  {  	return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));  }  /* People celebrate: "We love our President!" */ -static int tcp_try_undo_recovery(struct sock *sk) +static bool tcp_try_undo_recovery(struct sock *sk)  {  	struct tcp_sock *tp = tcp_sk(sk); @@ -2699,37 +2382,37 @@ static int tcp_try_undo_recovery(struct sock *sk)  		 * or our original transmission succeeded.  		 */  		DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans"); -		tcp_undo_cwr(sk, 1); +		tcp_undo_cwnd_reduction(sk, false);  		if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)  			mib_idx = LINUX_MIB_TCPLOSSUNDO;  		else  			mib_idx = LINUX_MIB_TCPFULLUNDO;  		NET_INC_STATS_BH(sock_net(sk), mib_idx); -		tp->undo_marker = 0;  	}  	if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {  		/* Hold old state until something *above* high_seq  		 * is ACKed. For Reno it is MUST to prevent false  		 * fast retransmits (RFC2582). SACK TCP is safe. */  		tcp_moderate_cwnd(tp); -		return 1; +		return true;  	}  	tcp_set_ca_state(sk, TCP_CA_Open); -	return 0; +	return false;  }  /* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */ -static void tcp_try_undo_dsack(struct sock *sk) +static bool tcp_try_undo_dsack(struct sock *sk)  {  	struct tcp_sock *tp = tcp_sk(sk);  	if (tp->undo_marker && !tp->undo_retrans) {  		DBGUNDO(sk, "D-SACK"); -		tcp_undo_cwr(sk, 1); -		tp->undo_marker = 0; +		tcp_undo_cwnd_reduction(sk, false);  		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKUNDO); +		return true;  	} +	return false;  }  /* We can clear retrans_stamp when there are no retransmissions in the @@ -2746,85 +2429,115 @@ static void tcp_try_undo_dsack(struct sock *sk)   * that successive retransmissions of a segment must not advance   * retrans_stamp under any conditions.   */ -static int tcp_any_retrans_done(struct sock *sk) +static bool tcp_any_retrans_done(const struct sock *sk)  { -	struct tcp_sock *tp = tcp_sk(sk); +	const struct tcp_sock *tp = tcp_sk(sk);  	struct sk_buff *skb;  	if (tp->retrans_out) -		return 1; +		return true;  	skb = tcp_write_queue_head(sk);  	if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS)) -		return 1; +		return true; -	return 0; +	return false;  } -/* Undo during fast recovery after partial ACK. */ - -static int tcp_try_undo_partial(struct sock *sk, int acked) +/* Undo during loss recovery after partial ACK or using F-RTO. */ +static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)  {  	struct tcp_sock *tp = tcp_sk(sk); -	/* Partial ACK arrived. Force Hoe's retransmit. */ -	int failed = tcp_is_reno(tp) || (tcp_fackets_out(tp) > tp->reordering); -	if (tcp_may_undo(tp)) { -		/* Plain luck! Hole if filled with delayed -		 * packet, rather than with a retransmit. -		 */ -		if (!tcp_any_retrans_done(sk)) -			tp->retrans_stamp = 0; +	if (frto_undo || tcp_may_undo(tp)) { +		tcp_undo_cwnd_reduction(sk, true); -		tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1); +		DBGUNDO(sk, "partial loss"); +		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSSUNDO); +		if (frto_undo) +			NET_INC_STATS_BH(sock_net(sk), +					 LINUX_MIB_TCPSPURIOUSRTOS); +		inet_csk(sk)->icsk_retransmits = 0; +		if (frto_undo || tcp_is_sack(tp)) +			tcp_set_ca_state(sk, TCP_CA_Open); +		return true; +	} +	return false; +} -		DBGUNDO(sk, "Hoe"); -		tcp_undo_cwr(sk, 0); -		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO); +/* The cwnd reduction in CWR and Recovery use the PRR algorithm + * https://datatracker.ietf.org/doc/draft-ietf-tcpm-proportional-rate-reduction/ + * It computes the number of packets to send (sndcnt) based on packets newly + * delivered: + *   1) If the packets in flight is larger than ssthresh, PRR spreads the + *	cwnd reductions across a full RTT. + *   2) If packets in flight is lower than ssthresh (such as due to excess + *	losses and/or application stalls), do not perform any further cwnd + *	reductions, but instead slow start up to ssthresh. + */ +static void tcp_init_cwnd_reduction(struct sock *sk, const bool set_ssthresh) +{ +	struct tcp_sock *tp = tcp_sk(sk); -		/* So... Do not make Hoe's retransmit yet. -		 * If the first packet was delayed, the rest -		 * ones are most probably delayed as well. -		 */ -		failed = 0; -	} -	return failed; +	tp->high_seq = tp->snd_nxt; +	tp->tlp_high_seq = 0; +	tp->snd_cwnd_cnt = 0; +	tp->prior_cwnd = tp->snd_cwnd; +	tp->prr_delivered = 0; +	tp->prr_out = 0; +	if (set_ssthresh) +		tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk); +	TCP_ECN_queue_cwr(tp);  } -/* Undo during loss recovery after partial ACK. */ -static int tcp_try_undo_loss(struct sock *sk) +static void tcp_cwnd_reduction(struct sock *sk, const int prior_unsacked, +			       int fast_rexmit)  {  	struct tcp_sock *tp = tcp_sk(sk); +	int sndcnt = 0; +	int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp); +	int newly_acked_sacked = prior_unsacked - +				 (tp->packets_out - tp->sacked_out); + +	tp->prr_delivered += newly_acked_sacked; +	if (tcp_packets_in_flight(tp) > tp->snd_ssthresh) { +		u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered + +			       tp->prior_cwnd - 1; +		sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out; +	} else { +		sndcnt = min_t(int, delta, +			       max_t(int, tp->prr_delivered - tp->prr_out, +				     newly_acked_sacked) + 1); +	} -	if (tcp_may_undo(tp)) { -		struct sk_buff *skb; -		tcp_for_write_queue(skb, sk) { -			if (skb == tcp_send_head(sk)) -				break; -			TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST; -		} +	sndcnt = max(sndcnt, (fast_rexmit ? 1 : 0)); +	tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt; +} -		tcp_clear_all_retrans_hints(tp); +static inline void tcp_end_cwnd_reduction(struct sock *sk) +{ +	struct tcp_sock *tp = tcp_sk(sk); -		DBGUNDO(sk, "partial loss"); -		tp->lost_out = 0; -		tcp_undo_cwr(sk, 1); -		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSSUNDO); -		inet_csk(sk)->icsk_retransmits = 0; -		tp->undo_marker = 0; -		if (tcp_is_sack(tp)) -			tcp_set_ca_state(sk, TCP_CA_Open); -		return 1; +	/* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */ +	if (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR || +	    (tp->undo_marker && tp->snd_ssthresh < TCP_INFINITE_SSTHRESH)) { +		tp->snd_cwnd = tp->snd_ssthresh; +		tp->snd_cwnd_stamp = tcp_time_stamp;  	} -	return 0; +	tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);  } -static inline void tcp_complete_cwr(struct sock *sk) +/* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */ +void tcp_enter_cwr(struct sock *sk, const int set_ssthresh)  {  	struct tcp_sock *tp = tcp_sk(sk); -	tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh); -	tp->snd_cwnd_stamp = tcp_time_stamp; -	tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR); + +	tp->prior_ssthresh = 0; +	if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) { +		tp->undo_marker = 0; +		tcp_init_cwnd_reduction(sk, set_ssthresh); +		tcp_set_ca_state(sk, TCP_CA_CWR); +	}  }  static void tcp_try_keep_open(struct sock *sk) @@ -2832,7 +2545,7 @@ static void tcp_try_keep_open(struct sock *sk)  	struct tcp_sock *tp = tcp_sk(sk);  	int state = TCP_CA_Open; -	if (tcp_left_out(tp) || tcp_any_retrans_done(sk) || tp->undo_marker) +	if (tcp_left_out(tp) || tcp_any_retrans_done(sk))  		state = TCP_CA_Disorder;  	if (inet_csk(sk)->icsk_ca_state != state) { @@ -2841,13 +2554,13 @@ static void tcp_try_keep_open(struct sock *sk)  	}  } -static void tcp_try_to_open(struct sock *sk, int flag) +static void tcp_try_to_open(struct sock *sk, int flag, const int prior_unsacked)  {  	struct tcp_sock *tp = tcp_sk(sk);  	tcp_verify_left_out(tp); -	if (!tp->frto_counter && !tcp_any_retrans_done(sk)) +	if (!tcp_any_retrans_done(sk))  		tp->retrans_stamp = 0;  	if (flag & FLAG_ECE) @@ -2855,9 +2568,8 @@ static void tcp_try_to_open(struct sock *sk, int flag)  	if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {  		tcp_try_keep_open(sk); -		tcp_moderate_cwnd(tp);  	} else { -		tcp_cwnd_down(sk, flag); +		tcp_cwnd_reduction(sk, prior_unsacked, 0);  	}  } @@ -2939,6 +2651,115 @@ void tcp_simple_retransmit(struct sock *sk)  }  EXPORT_SYMBOL(tcp_simple_retransmit); +static void tcp_enter_recovery(struct sock *sk, bool ece_ack) +{ +	struct tcp_sock *tp = tcp_sk(sk); +	int mib_idx; + +	if (tcp_is_reno(tp)) +		mib_idx = LINUX_MIB_TCPRENORECOVERY; +	else +		mib_idx = LINUX_MIB_TCPSACKRECOVERY; + +	NET_INC_STATS_BH(sock_net(sk), mib_idx); + +	tp->prior_ssthresh = 0; +	tp->undo_marker = tp->snd_una; +	tp->undo_retrans = tp->retrans_out ? : -1; + +	if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) { +		if (!ece_ack) +			tp->prior_ssthresh = tcp_current_ssthresh(sk); +		tcp_init_cwnd_reduction(sk, true); +	} +	tcp_set_ca_state(sk, TCP_CA_Recovery); +} + +/* Process an ACK in CA_Loss state. Move to CA_Open if lost data are + * recovered or spurious. Otherwise retransmits more on partial ACKs. + */ +static void tcp_process_loss(struct sock *sk, int flag, bool is_dupack) +{ +	struct inet_connection_sock *icsk = inet_csk(sk); +	struct tcp_sock *tp = tcp_sk(sk); +	bool recovered = !before(tp->snd_una, tp->high_seq); + +	if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */ +		/* Step 3.b. A timeout is spurious if not all data are +		 * lost, i.e., never-retransmitted data are (s)acked. +		 */ +		if (tcp_try_undo_loss(sk, flag & FLAG_ORIG_SACK_ACKED)) +			return; + +		if (after(tp->snd_nxt, tp->high_seq) && +		    (flag & FLAG_DATA_SACKED || is_dupack)) { +			tp->frto = 0; /* Loss was real: 2nd part of step 3.a */ +		} else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) { +			tp->high_seq = tp->snd_nxt; +			__tcp_push_pending_frames(sk, tcp_current_mss(sk), +						  TCP_NAGLE_OFF); +			if (after(tp->snd_nxt, tp->high_seq)) +				return; /* Step 2.b */ +			tp->frto = 0; +		} +	} + +	if (recovered) { +		/* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */ +		icsk->icsk_retransmits = 0; +		tcp_try_undo_recovery(sk); +		return; +	} +	if (flag & FLAG_DATA_ACKED) +		icsk->icsk_retransmits = 0; +	if (tcp_is_reno(tp)) { +		/* A Reno DUPACK means new data in F-RTO step 2.b above are +		 * delivered. Lower inflight to clock out (re)tranmissions. +		 */ +		if (after(tp->snd_nxt, tp->high_seq) && is_dupack) +			tcp_add_reno_sack(sk); +		else if (flag & FLAG_SND_UNA_ADVANCED) +			tcp_reset_reno_sack(tp); +	} +	if (tcp_try_undo_loss(sk, false)) +		return; +	tcp_xmit_retransmit_queue(sk); +} + +/* Undo during fast recovery after partial ACK. */ +static bool tcp_try_undo_partial(struct sock *sk, const int acked, +				 const int prior_unsacked) +{ +	struct tcp_sock *tp = tcp_sk(sk); + +	if (tp->undo_marker && tcp_packet_delayed(tp)) { +		/* Plain luck! Hole if filled with delayed +		 * packet, rather than with a retransmit. +		 */ +		tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1); + +		/* We are getting evidence that the reordering degree is higher +		 * than we realized. If there are no retransmits out then we +		 * can undo. Otherwise we clock out new packets but do not +		 * mark more packets lost or retransmit more. +		 */ +		if (tp->retrans_out) { +			tcp_cwnd_reduction(sk, prior_unsacked, 0); +			return true; +		} + +		if (!tcp_any_retrans_done(sk)) +			tp->retrans_stamp = 0; + +		DBGUNDO(sk, "partial recovery"); +		tcp_undo_cwnd_reduction(sk, true); +		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO); +		tcp_try_keep_open(sk); +		return true; +	} +	return false; +} +  /* Process an event, which can update packets-in-flight not trivially.   * Main goal of this function is to calculate new estimate for left_out,   * taking into account both packets sitting in receiver's buffer and @@ -2950,14 +2771,15 @@ EXPORT_SYMBOL(tcp_simple_retransmit);   * It does _not_ decide what to send, it is made in function   * tcp_xmit_retransmit_queue().   */ -static void tcp_fastretrans_alert(struct sock *sk, int pkts_acked, int flag) +static void tcp_fastretrans_alert(struct sock *sk, const int acked, +				  const int prior_unsacked, +				  bool is_dupack, int flag)  {  	struct inet_connection_sock *icsk = inet_csk(sk);  	struct tcp_sock *tp = tcp_sk(sk); -	int is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP)); -	int do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) && +	bool do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&  				    (tcp_fackets_out(tp) > tp->reordering)); -	int fast_rexmit = 0, mib_idx; +	int fast_rexmit = 0;  	if (WARN_ON(!tp->packets_out && tp->sacked_out))  		tp->sacked_out = 0; @@ -2973,47 +2795,21 @@ static void tcp_fastretrans_alert(struct sock *sk, int pkts_acked, int flag)  	if (tcp_check_sack_reneging(sk, flag))  		return; -	/* C. Process data loss notification, provided it is valid. */ -	if (tcp_is_fack(tp) && (flag & FLAG_DATA_LOST) && -	    before(tp->snd_una, tp->high_seq) && -	    icsk->icsk_ca_state != TCP_CA_Open && -	    tp->fackets_out > tp->reordering) { -		tcp_mark_head_lost(sk, tp->fackets_out - tp->reordering, 0); -		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSS); -	} - -	/* D. Check consistency of the current state. */ +	/* C. Check consistency of the current state. */  	tcp_verify_left_out(tp); -	/* E. Check state exit conditions. State can be terminated +	/* D. Check state exit conditions. State can be terminated  	 *    when high_seq is ACKed. */  	if (icsk->icsk_ca_state == TCP_CA_Open) {  		WARN_ON(tp->retrans_out != 0);  		tp->retrans_stamp = 0;  	} else if (!before(tp->snd_una, tp->high_seq)) {  		switch (icsk->icsk_ca_state) { -		case TCP_CA_Loss: -			icsk->icsk_retransmits = 0; -			if (tcp_try_undo_recovery(sk)) -				return; -			break; -  		case TCP_CA_CWR:  			/* CWR is to be held something *above* high_seq  			 * is ACKed for CWR bit to reach receiver. */  			if (tp->snd_una != tp->high_seq) { -				tcp_complete_cwr(sk); -				tcp_set_ca_state(sk, TCP_CA_Open); -			} -			break; - -		case TCP_CA_Disorder: -			tcp_try_undo_dsack(sk); -			if (!tp->undo_marker || -			    /* For SACK case do not Open to allow to undo -			     * catching for all duplicate ACKs. */ -			    tcp_is_reno(tp) || tp->snd_una != tp->high_seq) { -				tp->undo_marker = 0; +				tcp_end_cwnd_reduction(sk);  				tcp_set_ca_state(sk, TCP_CA_Open);  			}  			break; @@ -3023,33 +2819,34 @@ static void tcp_fastretrans_alert(struct sock *sk, int pkts_acked, int flag)  				tcp_reset_reno_sack(tp);  			if (tcp_try_undo_recovery(sk))  				return; -			tcp_complete_cwr(sk); +			tcp_end_cwnd_reduction(sk);  			break;  		}  	} -	/* F. Process state. */ +	/* E. Process state. */  	switch (icsk->icsk_ca_state) {  	case TCP_CA_Recovery:  		if (!(flag & FLAG_SND_UNA_ADVANCED)) {  			if (tcp_is_reno(tp) && is_dupack)  				tcp_add_reno_sack(sk); -		} else -			do_lost = tcp_try_undo_partial(sk, pkts_acked); -		break; -	case TCP_CA_Loss: -		if (flag & FLAG_DATA_ACKED) -			icsk->icsk_retransmits = 0; -		if (tcp_is_reno(tp) && flag & FLAG_SND_UNA_ADVANCED) -			tcp_reset_reno_sack(tp); -		if (!tcp_try_undo_loss(sk)) { -			tcp_moderate_cwnd(tp); -			tcp_xmit_retransmit_queue(sk); +		} else { +			if (tcp_try_undo_partial(sk, acked, prior_unsacked)) +				return; +			/* Partial ACK arrived. Force fast retransmit. */ +			do_lost = tcp_is_reno(tp) || +				  tcp_fackets_out(tp) > tp->reordering; +		} +		if (tcp_try_undo_dsack(sk)) { +			tcp_try_keep_open(sk);  			return;  		} +		break; +	case TCP_CA_Loss: +		tcp_process_loss(sk, flag, is_dupack);  		if (icsk->icsk_ca_state != TCP_CA_Open)  			return; -		/* Loss is undone; fall through to processing in Open state. */ +		/* Fall through to processing in Open state. */  	default:  		if (tcp_is_reno(tp)) {  			if (flag & FLAG_SND_UNA_ADVANCED) @@ -3058,11 +2855,11 @@ static void tcp_fastretrans_alert(struct sock *sk, int pkts_acked, int flag)  				tcp_add_reno_sack(sk);  		} -		if (icsk->icsk_ca_state == TCP_CA_Disorder) +		if (icsk->icsk_ca_state <= TCP_CA_Disorder)  			tcp_try_undo_dsack(sk); -		if (!tcp_time_to_recover(sk)) { -			tcp_try_to_open(sk, flag); +		if (!tcp_time_to_recover(sk, flag)) { +			tcp_try_to_open(sk, flag, prior_unsacked);  			return;  		} @@ -3078,120 +2875,130 @@ static void tcp_fastretrans_alert(struct sock *sk, int pkts_acked, int flag)  		}  		/* Otherwise enter Recovery state */ - -		if (tcp_is_reno(tp)) -			mib_idx = LINUX_MIB_TCPRENORECOVERY; -		else -			mib_idx = LINUX_MIB_TCPSACKRECOVERY; - -		NET_INC_STATS_BH(sock_net(sk), mib_idx); - -		tp->high_seq = tp->snd_nxt; -		tp->prior_ssthresh = 0; -		tp->undo_marker = tp->snd_una; -		tp->undo_retrans = tp->retrans_out; - -		if (icsk->icsk_ca_state < TCP_CA_CWR) { -			if (!(flag & FLAG_ECE)) -				tp->prior_ssthresh = tcp_current_ssthresh(sk); -			tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk); -			TCP_ECN_queue_cwr(tp); -		} - -		tp->bytes_acked = 0; -		tp->snd_cwnd_cnt = 0; -		tcp_set_ca_state(sk, TCP_CA_Recovery); +		tcp_enter_recovery(sk, (flag & FLAG_ECE));  		fast_rexmit = 1;  	} -	if (do_lost || (tcp_is_fack(tp) && tcp_head_timedout(sk))) +	if (do_lost)  		tcp_update_scoreboard(sk, fast_rexmit); -	tcp_cwnd_down(sk, flag); +	tcp_cwnd_reduction(sk, prior_unsacked, fast_rexmit);  	tcp_xmit_retransmit_queue(sk);  } -static void tcp_valid_rtt_meas(struct sock *sk, u32 seq_rtt) +static inline bool tcp_ack_update_rtt(struct sock *sk, const int flag, +				      long seq_rtt_us, long sack_rtt_us)  { -	tcp_rtt_estimator(sk, seq_rtt); -	tcp_set_rto(sk); -	inet_csk(sk)->icsk_backoff = 0; -} +	const struct tcp_sock *tp = tcp_sk(sk); + +	/* Prefer RTT measured from ACK's timing to TS-ECR. This is because +	 * broken middle-boxes or peers may corrupt TS-ECR fields. But +	 * Karn's algorithm forbids taking RTT if some retransmitted data +	 * is acked (RFC6298). +	 */ +	if (flag & FLAG_RETRANS_DATA_ACKED) +		seq_rtt_us = -1L; + +	if (seq_rtt_us < 0) +		seq_rtt_us = sack_rtt_us; -/* Read draft-ietf-tcplw-high-performance before mucking - * with this code. (Supersedes RFC1323) - */ -static void tcp_ack_saw_tstamp(struct sock *sk, int flag) -{  	/* RTTM Rule: A TSecr value received in a segment is used to  	 * update the averaged RTT measurement only if the segment  	 * acknowledges some new data, i.e., only if it advances the  	 * left edge of the send window. -	 *  	 * See draft-ietf-tcplw-high-performance-00, section 3.3. -	 * 1998/04/10 Andrey V. Savochkin <saw@msu.ru> -	 * -	 * Changed: reset backoff as soon as we see the first valid sample. -	 * If we do not, we get strongly overestimated rto. With timestamps -	 * samples are accepted even from very old segments: f.e., when rtt=1 -	 * increases to 8, we retransmit 5 times and after 8 seconds delayed -	 * answer arrives rto becomes 120 seconds! If at least one of segments -	 * in window is lost... Voila.	 			--ANK (010210)  	 */ -	struct tcp_sock *tp = tcp_sk(sk); - -	tcp_valid_rtt_meas(sk, tcp_time_stamp - tp->rx_opt.rcv_tsecr); -} +	if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && +	    flag & FLAG_ACKED) +		seq_rtt_us = jiffies_to_usecs(tcp_time_stamp - tp->rx_opt.rcv_tsecr); -static void tcp_ack_no_tstamp(struct sock *sk, u32 seq_rtt, int flag) -{ -	/* We don't have a timestamp. Can only use -	 * packets that are not retransmitted to determine -	 * rtt estimates. Also, we must not reset the -	 * backoff for rto until we get a non-retransmitted -	 * packet. This allows us to deal with a situation -	 * where the network delay has increased suddenly. -	 * I.e. Karn's algorithm. (SIGCOMM '87, p5.) -	 */ +	if (seq_rtt_us < 0) +		return false; -	if (flag & FLAG_RETRANS_DATA_ACKED) -		return; +	tcp_rtt_estimator(sk, seq_rtt_us); +	tcp_set_rto(sk); -	tcp_valid_rtt_meas(sk, seq_rtt); +	/* RFC6298: only reset backoff on valid RTT measurement. */ +	inet_csk(sk)->icsk_backoff = 0; +	return true;  } -static inline void tcp_ack_update_rtt(struct sock *sk, const int flag, -				      const s32 seq_rtt) +/* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */ +static void tcp_synack_rtt_meas(struct sock *sk, const u32 synack_stamp)  { -	const struct tcp_sock *tp = tcp_sk(sk); -	/* Note that peer MAY send zero echo. In this case it is ignored. (rfc1323) */ -	if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) -		tcp_ack_saw_tstamp(sk, flag); -	else if (seq_rtt >= 0) -		tcp_ack_no_tstamp(sk, seq_rtt, flag); +	struct tcp_sock *tp = tcp_sk(sk); +	long seq_rtt_us = -1L; + +	if (synack_stamp && !tp->total_retrans) +		seq_rtt_us = jiffies_to_usecs(tcp_time_stamp - synack_stamp); + +	/* If the ACK acks both the SYNACK and the (Fast Open'd) data packets +	 * sent in SYN_RECV, SYNACK RTT is the smooth RTT computed in tcp_ack() +	 */ +	if (!tp->srtt_us) +		tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, seq_rtt_us, -1L);  } -static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) +static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)  {  	const struct inet_connection_sock *icsk = inet_csk(sk); -	icsk->icsk_ca_ops->cong_avoid(sk, ack, in_flight); + +	icsk->icsk_ca_ops->cong_avoid(sk, ack, acked);  	tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp;  }  /* Restart timer after forward progress on connection.   * RFC2988 recommends to restart timer to now+rto.   */ -static void tcp_rearm_rto(struct sock *sk) +void tcp_rearm_rto(struct sock *sk)  { +	const struct inet_connection_sock *icsk = inet_csk(sk);  	struct tcp_sock *tp = tcp_sk(sk); +	/* If the retrans timer is currently being used by Fast Open +	 * for SYN-ACK retrans purpose, stay put. +	 */ +	if (tp->fastopen_rsk) +		return; +  	if (!tp->packets_out) {  		inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);  	} else { -		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, -					  inet_csk(sk)->icsk_rto, TCP_RTO_MAX); +		u32 rto = inet_csk(sk)->icsk_rto; +		/* Offset the time elapsed after installing regular RTO */ +		if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || +		    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { +			struct sk_buff *skb = tcp_write_queue_head(sk); +			const u32 rto_time_stamp = TCP_SKB_CB(skb)->when + rto; +			s32 delta = (s32)(rto_time_stamp - tcp_time_stamp); +			/* delta may not be positive if the socket is locked +			 * when the retrans timer fires and is rescheduled. +			 */ +			if (delta > 0) +				rto = delta; +		} +		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto, +					  TCP_RTO_MAX);  	}  } +/* This function is called when the delayed ER timer fires. TCP enters + * fast recovery and performs fast-retransmit. + */ +void tcp_resume_early_retransmit(struct sock *sk) +{ +	struct tcp_sock *tp = tcp_sk(sk); + +	tcp_rearm_rto(sk); + +	/* Stop if ER is disabled after the delayed ER timer is scheduled */ +	if (!tp->do_early_retrans) +		return; + +	tcp_enter_recovery(sk, false); +	tcp_update_scoreboard(sk, 1); +	tcp_xmit_retransmit_queue(sk); +} +  /* If we get here, the whole TSO packet has not been acked. */  static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)  { @@ -3218,25 +3025,27 @@ static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)   * arrived at the other end.   */  static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets, -			       u32 prior_snd_una) +			       u32 prior_snd_una, long sack_rtt_us)  { -	struct tcp_sock *tp = tcp_sk(sk);  	const struct inet_connection_sock *icsk = inet_csk(sk); +	struct skb_mstamp first_ackt, last_ackt, now; +	struct tcp_sock *tp = tcp_sk(sk); +	u32 prior_sacked = tp->sacked_out; +	u32 reord = tp->packets_out; +	bool fully_acked = true; +	long ca_seq_rtt_us = -1L; +	long seq_rtt_us = -1L;  	struct sk_buff *skb; -	u32 now = tcp_time_stamp; -	int fully_acked = 1; -	int flag = 0;  	u32 pkts_acked = 0; -	u32 reord = tp->packets_out; -	u32 prior_sacked = tp->sacked_out; -	s32 seq_rtt = -1; -	s32 ca_seq_rtt = -1; -	ktime_t last_ackt = net_invalid_timestamp(); +	bool rtt_update; +	int flag = 0; + +	first_ackt.v64 = 0;  	while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) {  		struct tcp_skb_cb *scb = TCP_SKB_CB(skb); -		u32 acked_pcount;  		u8 sacked = scb->sacked; +		u32 acked_pcount;  		/* Determine how many packets and what bytes were acked, tso and else */  		if (after(scb->end_seq, tp->snd_una)) { @@ -3248,7 +3057,7 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,  			if (!acked_pcount)  				break; -			fully_acked = 0; +			fully_acked = false;  		} else {  			acked_pcount = tcp_skb_pcount(skb);  		} @@ -3257,18 +3066,16 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,  			if (sacked & TCPCB_SACKED_RETRANS)  				tp->retrans_out -= acked_pcount;  			flag |= FLAG_RETRANS_DATA_ACKED; -			ca_seq_rtt = -1; -			seq_rtt = -1; -			if ((flag & FLAG_DATA_ACKED) || (acked_pcount > 1)) -				flag |= FLAG_NONHEAD_RETRANS_ACKED;  		} else { -			ca_seq_rtt = now - scb->when; -			last_ackt = skb->tstamp; -			if (seq_rtt < 0) { -				seq_rtt = ca_seq_rtt; -			} +			last_ackt = skb->skb_mstamp; +			WARN_ON_ONCE(last_ackt.v64 == 0); +			if (!first_ackt.v64) +				first_ackt = last_ackt; +  			if (!(sacked & TCPCB_SACKED_ACKED))  				reord = min(pkts_acked, reord); +			if (!after(scb->end_seq, tp->high_seq)) +				flag |= FLAG_ORIG_SACK_ACKED;  		}  		if (sacked & TCPCB_SACKED_ACKED) @@ -3286,7 +3093,7 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,  		 * connection startup slow start one packet too  		 * quickly.  This is severely frowned upon behavior.  		 */ -		if (!(scb->flags & TCPHDR_SYN)) { +		if (!(scb->tcp_flags & TCPHDR_SYN)) {  			flag |= FLAG_DATA_ACKED;  		} else {  			flag |= FLAG_SYN_ACKED; @@ -3298,7 +3105,6 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,  		tcp_unlink_write_queue(skb, sk);  		sk_wmem_free_skb(sk, skb); -		tp->scoreboard_skb_hint = NULL;  		if (skb == tp->retransmit_skb_hint)  			tp->retransmit_skb_hint = NULL;  		if (skb == tp->lost_skb_hint) @@ -3311,18 +3117,24 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,  	if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))  		flag |= FLAG_SACK_RENEGING; +	skb_mstamp_get(&now); +	if (first_ackt.v64) { +		seq_rtt_us = skb_mstamp_us_delta(&now, &first_ackt); +		ca_seq_rtt_us = skb_mstamp_us_delta(&now, &last_ackt); +	} + +	rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us); +  	if (flag & FLAG_ACKED) {  		const struct tcp_congestion_ops *ca_ops  			= inet_csk(sk)->icsk_ca_ops; +		tcp_rearm_rto(sk);  		if (unlikely(icsk->icsk_mtup.probe_size &&  			     !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {  			tcp_mtup_probe_success(sk);  		} -		tcp_ack_update_rtt(sk, flag, seq_rtt); -		tcp_rearm_rto(sk); -  		if (tcp_is_reno(tp)) {  			tcp_remove_reno_sacks(sk, pkts_acked);  		} else { @@ -3339,23 +3151,16 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,  		tp->fackets_out -= min(pkts_acked, tp->fackets_out); -		if (ca_ops->pkts_acked) { -			s32 rtt_us = -1; - -			/* Is the ACK triggering packet unambiguous? */ -			if (!(flag & FLAG_RETRANS_DATA_ACKED)) { -				/* High resolution needed and available? */ -				if (ca_ops->flags & TCP_CONG_RTT_STAMP && -				    !ktime_equal(last_ackt, -						 net_invalid_timestamp())) -					rtt_us = ktime_us_delta(ktime_get_real(), -								last_ackt); -				else if (ca_seq_rtt > 0) -					rtt_us = jiffies_to_usecs(ca_seq_rtt); -			} +		if (ca_ops->pkts_acked) +			ca_ops->pkts_acked(sk, pkts_acked, ca_seq_rtt_us); -			ca_ops->pkts_acked(sk, pkts_acked, rtt_us); -		} +	} else if (skb && rtt_update && sack_rtt_us >= 0 && +		   sack_rtt_us > skb_mstamp_us_delta(&now, &skb->skb_mstamp)) { +		/* Do not re-arm RTO if the sack RTT is measured from data sent +		 * after when the head was last (re)transmitted. Otherwise the +		 * timeout may continue to extend in loss recovery. +		 */ +		tcp_rearm_rto(sk);  	}  #if FASTRETRANS_DEBUG > 0 @@ -3365,18 +3170,18 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,  	if (!tp->packets_out && tcp_is_sack(tp)) {  		icsk = inet_csk(sk);  		if (tp->lost_out) { -			printk(KERN_DEBUG "Leak l=%u %d\n", -			       tp->lost_out, icsk->icsk_ca_state); +			pr_debug("Leak l=%u %d\n", +				 tp->lost_out, icsk->icsk_ca_state);  			tp->lost_out = 0;  		}  		if (tp->sacked_out) { -			printk(KERN_DEBUG "Leak s=%u %d\n", -			       tp->sacked_out, icsk->icsk_ca_state); +			pr_debug("Leak s=%u %d\n", +				 tp->sacked_out, icsk->icsk_ca_state);  			tp->sacked_out = 0;  		}  		if (tp->retrans_out) { -			printk(KERN_DEBUG "Leak r=%u %d\n", -			       tp->retrans_out, icsk->icsk_ca_state); +			pr_debug("Leak r=%u %d\n", +				 tp->retrans_out, icsk->icsk_ca_state);  			tp->retrans_out = 0;  		}  	} @@ -3404,23 +3209,34 @@ static void tcp_ack_probe(struct sock *sk)  	}  } -static inline int tcp_ack_is_dubious(const struct sock *sk, const int flag) +static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)  {  	return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||  		inet_csk(sk)->icsk_ca_state != TCP_CA_Open;  } -static inline int tcp_may_raise_cwnd(const struct sock *sk, const int flag) +/* Decide wheather to run the increase function of congestion control. */ +static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)  { -	const struct tcp_sock *tp = tcp_sk(sk); -	return (!(flag & FLAG_ECE) || tp->snd_cwnd < tp->snd_ssthresh) && -		!((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_Recovery | TCPF_CA_CWR)); +	if (tcp_in_cwnd_reduction(sk)) +		return false; + +	/* If reordering is high then always grow cwnd whenever data is +	 * delivered regardless of its ordering. Otherwise stay conservative +	 * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/ +	 * new SACK or ECE mark may first advance cwnd here and later reduce +	 * cwnd in tcp_fastretrans_alert() based on more states. +	 */ +	if (tcp_sk(sk)->reordering > sysctl_tcp_reordering) +		return flag & FLAG_FORWARD_PROGRESS; + +	return flag & FLAG_DATA_ACKED;  }  /* Check that window update is acceptable.   * The function assumes that snd_una<=ack<=snd_next.   */ -static inline int tcp_may_update_window(const struct tcp_sock *tp, +static inline bool tcp_may_update_window(const struct tcp_sock *tp,  					const u32 ack, const u32 ack_seq,  					const u32 nwin)  { @@ -3434,7 +3250,7 @@ static inline int tcp_may_update_window(const struct tcp_sock *tp,   * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2   * and in FreeBSD. NetBSD's one is even worse.) is wrong.   */ -static int tcp_ack_update_window(struct sock *sk, struct sk_buff *skb, u32 ack, +static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,  				 u32 ack_seq)  {  	struct tcp_sock *tp = tcp_sk(sk); @@ -3469,164 +3285,103 @@ static int tcp_ack_update_window(struct sock *sk, struct sk_buff *skb, u32 ack,  	return flag;  } -/* A very conservative spurious RTO response algorithm: reduce cwnd and - * continue in congestion avoidance. - */ -static void tcp_conservative_spur_to_response(struct tcp_sock *tp) +/* RFC 5961 7 [ACK Throttling] */ +static void tcp_send_challenge_ack(struct sock *sk)  { -	tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh); -	tp->snd_cwnd_cnt = 0; -	tp->bytes_acked = 0; -	TCP_ECN_queue_cwr(tp); -	tcp_moderate_cwnd(tp); +	/* unprotected vars, we dont care of overwrites */ +	static u32 challenge_timestamp; +	static unsigned int challenge_count; +	u32 now = jiffies / HZ; + +	if (now != challenge_timestamp) { +		challenge_timestamp = now; +		challenge_count = 0; +	} +	if (++challenge_count <= sysctl_tcp_challenge_ack_limit) { +		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK); +		tcp_send_ack(sk); +	}  } -/* A conservative spurious RTO response algorithm: reduce cwnd using - * rate halving and continue in congestion avoidance. - */ -static void tcp_ratehalving_spur_to_response(struct sock *sk) +static void tcp_store_ts_recent(struct tcp_sock *tp)  { -	tcp_enter_cwr(sk, 0); +	tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval; +	tp->rx_opt.ts_recent_stamp = get_seconds();  } -static void tcp_undo_spur_to_response(struct sock *sk, int flag) +static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)  { -	if (flag & FLAG_ECE) -		tcp_ratehalving_spur_to_response(sk); -	else -		tcp_undo_cwr(sk, 1); +	if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) { +		/* PAWS bug workaround wrt. ACK frames, the PAWS discard +		 * extra check below makes sure this can only happen +		 * for pure ACK frames.  -DaveM +		 * +		 * Not only, also it occurs for expired timestamps. +		 */ + +		if (tcp_paws_check(&tp->rx_opt, 0)) +			tcp_store_ts_recent(tp); +	}  } -/* F-RTO spurious RTO detection algorithm (RFC4138) - * - * F-RTO affects during two new ACKs following RTO (well, almost, see inline - * comments). State (ACK number) is kept in frto_counter. When ACK advances - * window (but not to or beyond highest sequence sent before RTO): - *   On First ACK,  send two new segments out. - *   On Second ACK, RTO was likely spurious. Do spurious response (response - *                  algorithm is not part of the F-RTO detection algorithm - *                  given in RFC4138 but can be selected separately). - * Otherwise (basically on duplicate ACK), RTO was (likely) caused by a loss - * and TCP falls back to conventional RTO recovery. F-RTO allows overriding - * of Nagle, this is done using frto_counter states 2 and 3, when a new data - * segment of any size sent during F-RTO, state 2 is upgraded to 3. - * - * Rationale: if the RTO was spurious, new ACKs should arrive from the - * original window even after we transmit two new data segments. - * - * SACK version: - *   on first step, wait until first cumulative ACK arrives, then move to - *   the second step. In second step, the next ACK decides. - * - * F-RTO is implemented (mainly) in four functions: - *   - tcp_use_frto() is used to determine if TCP is can use F-RTO - *   - tcp_enter_frto() prepares TCP state on RTO if F-RTO is used, it is - *     called when tcp_use_frto() showed green light - *   - tcp_process_frto() handles incoming ACKs during F-RTO algorithm - *   - tcp_enter_frto_loss() is called if there is not enough evidence - *     to prove that the RTO is indeed spurious. It transfers the control - *     from F-RTO to the conventional RTO recovery +/* This routine deals with acks during a TLP episode. + * Ref: loss detection algorithm in draft-dukkipati-tcpm-tcp-loss-probe.   */ -static int tcp_process_frto(struct sock *sk, int flag) +static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)  {  	struct tcp_sock *tp = tcp_sk(sk); +	bool is_tlp_dupack = (ack == tp->tlp_high_seq) && +			     !(flag & (FLAG_SND_UNA_ADVANCED | +				       FLAG_NOT_DUP | FLAG_DATA_SACKED)); -	tcp_verify_left_out(tp); - -	/* Duplicate the behavior from Loss state (fastretrans_alert) */ -	if (flag & FLAG_DATA_ACKED) -		inet_csk(sk)->icsk_retransmits = 0; - -	if ((flag & FLAG_NONHEAD_RETRANS_ACKED) || -	    ((tp->frto_counter >= 2) && (flag & FLAG_RETRANS_DATA_ACKED))) -		tp->undo_marker = 0; - -	if (!before(tp->snd_una, tp->frto_highmark)) { -		tcp_enter_frto_loss(sk, (tp->frto_counter == 1 ? 2 : 3), flag); -		return 1; -	} - -	if (!tcp_is_sackfrto(tp)) { -		/* RFC4138 shortcoming in step 2; should also have case c): -		 * ACK isn't duplicate nor advances window, e.g., opposite dir -		 * data, winupdate -		 */ -		if (!(flag & FLAG_ANY_PROGRESS) && (flag & FLAG_NOT_DUP)) -			return 1; - -		if (!(flag & FLAG_DATA_ACKED)) { -			tcp_enter_frto_loss(sk, (tp->frto_counter == 1 ? 0 : 3), -					    flag); -			return 1; -		} -	} else { -		if (!(flag & FLAG_DATA_ACKED) && (tp->frto_counter == 1)) { -			/* Prevent sending of new data. */ -			tp->snd_cwnd = min(tp->snd_cwnd, -					   tcp_packets_in_flight(tp)); -			return 1; -		} - -		if ((tp->frto_counter >= 2) && -		    (!(flag & FLAG_FORWARD_PROGRESS) || -		     ((flag & FLAG_DATA_SACKED) && -		      !(flag & FLAG_ONLY_ORIG_SACKED)))) { -			/* RFC4138 shortcoming (see comment above) */ -			if (!(flag & FLAG_FORWARD_PROGRESS) && -			    (flag & FLAG_NOT_DUP)) -				return 1; - -			tcp_enter_frto_loss(sk, 3, flag); -			return 1; -		} +	/* Mark the end of TLP episode on receiving TLP dupack or when +	 * ack is after tlp_high_seq. +	 */ +	if (is_tlp_dupack) { +		tp->tlp_high_seq = 0; +		return;  	} -	if (tp->frto_counter == 1) { -		/* tcp_may_send_now needs to see updated state */ -		tp->snd_cwnd = tcp_packets_in_flight(tp) + 2; -		tp->frto_counter = 2; - -		if (!tcp_may_send_now(sk)) -			tcp_enter_frto_loss(sk, 2, flag); - -		return 1; -	} else { -		switch (sysctl_tcp_frto_response) { -		case 2: -			tcp_undo_spur_to_response(sk, flag); -			break; -		case 1: -			tcp_conservative_spur_to_response(tp); -			break; -		default: -			tcp_ratehalving_spur_to_response(sk); -			break; +	if (after(ack, tp->tlp_high_seq)) { +		tp->tlp_high_seq = 0; +		/* Don't reduce cwnd if DSACK arrives for TLP retrans. */ +		if (!(flag & FLAG_DSACKING_ACK)) { +			tcp_init_cwnd_reduction(sk, true); +			tcp_set_ca_state(sk, TCP_CA_CWR); +			tcp_end_cwnd_reduction(sk); +			tcp_try_keep_open(sk); +			NET_INC_STATS_BH(sock_net(sk), +					 LINUX_MIB_TCPLOSSPROBERECOVERY);  		} -		tp->frto_counter = 0; -		tp->undo_marker = 0; -		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSPURIOUSRTOS);  	} -	return 0;  }  /* This routine deals with incoming acks, but not outgoing ones. */ -static int tcp_ack(struct sock *sk, struct sk_buff *skb, int flag) +static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)  {  	struct inet_connection_sock *icsk = inet_csk(sk);  	struct tcp_sock *tp = tcp_sk(sk);  	u32 prior_snd_una = tp->snd_una;  	u32 ack_seq = TCP_SKB_CB(skb)->seq;  	u32 ack = TCP_SKB_CB(skb)->ack_seq; -	u32 prior_in_flight; +	bool is_dupack = false;  	u32 prior_fackets; -	int prior_packets; -	int frto_cwnd = 0; +	int prior_packets = tp->packets_out; +	const int prior_unsacked = tp->packets_out - tp->sacked_out; +	int acked = 0; /* Number of packets newly acked */ +	long sack_rtt_us = -1L;  	/* If the ack is older than previous acks  	 * then we can probably ignore it.  	 */ -	if (before(ack, prior_snd_una)) +	if (before(ack, prior_snd_una)) { +		/* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */ +		if (before(ack, prior_snd_una - tp->max_window)) { +			tcp_send_challenge_ack(sk); +			return -1; +		}  		goto old_ack; +	}  	/* If the ack includes data we haven't sent yet, discard  	 * this segment (RFC793 Section 3.9). @@ -3634,20 +3389,20 @@ static int tcp_ack(struct sock *sk, struct sk_buff *skb, int flag)  	if (after(ack, tp->snd_nxt))  		goto invalid_ack; +	if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || +	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) +		tcp_rearm_rto(sk); +  	if (after(ack, prior_snd_una))  		flag |= FLAG_SND_UNA_ADVANCED; -	if (sysctl_tcp_abc) { -		if (icsk->icsk_ca_state < TCP_CA_CWR) -			tp->bytes_acked += ack - prior_snd_una; -		else if (icsk->icsk_ca_state == TCP_CA_Loss) -			/* we assume just one segment left network */ -			tp->bytes_acked += min(ack - prior_snd_una, -					       tp->mss_cache); -	} -  	prior_fackets = tp->fackets_out; -	prior_in_flight = tcp_packets_in_flight(tp); + +	/* ts_recent update must be made after we are sure that the packet +	 * is in window. +	 */ +	if (flag & FLAG_UPDATE_TS_RECENT) +		tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);  	if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {  		/* Window is constant, pure forward advance. @@ -3670,7 +3425,8 @@ static int tcp_ack(struct sock *sk, struct sk_buff *skb, int flag)  		flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);  		if (TCP_SKB_CB(skb)->sacked) -			flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una); +			flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una, +							&sack_rtt_us);  		if (TCP_ECN_rcv_ecn_echo(tp, tcp_hdr(skb)))  			flag |= FLAG_ECE; @@ -3684,43 +3440,52 @@ static int tcp_ack(struct sock *sk, struct sk_buff *skb, int flag)  	sk->sk_err_soft = 0;  	icsk->icsk_probes_out = 0;  	tp->rcv_tstamp = tcp_time_stamp; -	prior_packets = tp->packets_out;  	if (!prior_packets)  		goto no_queue;  	/* See if we can take anything off of the retransmit queue. */ -	flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una); +	acked = tp->packets_out; +	flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una, +				    sack_rtt_us); +	acked -= tp->packets_out; -	if (tp->frto_counter) -		frto_cwnd = tcp_process_frto(sk, flag); -	/* Guarantee sacktag reordering detection against wrap-arounds */ -	if (before(tp->frto_highmark, tp->snd_una)) -		tp->frto_highmark = 0; +	/* Advance cwnd if state allows */ +	if (tcp_may_raise_cwnd(sk, flag)) +		tcp_cong_avoid(sk, ack, acked);  	if (tcp_ack_is_dubious(sk, flag)) { -		/* Advance CWND, if state allows this. */ -		if ((flag & FLAG_DATA_ACKED) && !frto_cwnd && -		    tcp_may_raise_cwnd(sk, flag)) -			tcp_cong_avoid(sk, ack, prior_in_flight); -		tcp_fastretrans_alert(sk, prior_packets - tp->packets_out, -				      flag); -	} else { -		if ((flag & FLAG_DATA_ACKED) && !frto_cwnd) -			tcp_cong_avoid(sk, ack, prior_in_flight); +		is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP)); +		tcp_fastretrans_alert(sk, acked, prior_unsacked, +				      is_dupack, flag);  	} +	if (tp->tlp_high_seq) +		tcp_process_tlp_ack(sk, ack, flag); -	if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP)) -		dst_confirm(__sk_dst_get(sk)); +	if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP)) { +		struct dst_entry *dst = __sk_dst_get(sk); +		if (dst) +			dst_confirm(dst); +	} +	if (icsk->icsk_pending == ICSK_TIME_RETRANS) +		tcp_schedule_loss_probe(sk); +	tcp_update_pacing_rate(sk);  	return 1;  no_queue: +	/* If data was DSACKed, see if we can undo a cwnd reduction. */ +	if (flag & FLAG_DSACKING_ACK) +		tcp_fastretrans_alert(sk, acked, prior_unsacked, +				      is_dupack, flag);  	/* If this ack opens up a zero window, clear backoff.  It was  	 * being used to time the probes, and is probably far higher than  	 * it needs to be for normal retransmission.  	 */  	if (tcp_send_head(sk))  		tcp_ack_probe(sk); + +	if (tp->tlp_high_seq) +		tcp_process_tlp_ack(sk, ack, flag);  	return 1;  invalid_ack: @@ -3728,10 +3493,14 @@ invalid_ack:  	return -1;  old_ack: +	/* If data was SACKed, tag it and see if we should send more data. +	 * If data was DSACKed, see if we can undo a cwnd reduction. +	 */  	if (TCP_SKB_CB(skb)->sacked) { -		tcp_sacktag_write_queue(sk, skb, prior_snd_una); -		if (icsk->icsk_ca_state == TCP_CA_Open) -			tcp_try_keep_open(sk); +		flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una, +						&sack_rtt_us); +		tcp_fastretrans_alert(sk, acked, prior_unsacked, +				      is_dupack, flag);  	}  	SOCK_DEBUG(sk, "Ack %u before %u:%u\n", ack, tp->snd_una, tp->snd_nxt); @@ -3742,14 +3511,15 @@ old_ack:   * But, this can also be called on packets in the established flow when   * the fast version below fails.   */ -void tcp_parse_options(struct sk_buff *skb, struct tcp_options_received *opt_rx, -		       u8 **hvpp, int estab) +void tcp_parse_options(const struct sk_buff *skb, +		       struct tcp_options_received *opt_rx, int estab, +		       struct tcp_fastopen_cookie *foc)  { -	unsigned char *ptr; -	struct tcphdr *th = tcp_hdr(skb); +	const unsigned char *ptr; +	const struct tcphdr *th = tcp_hdr(skb);  	int length = (th->doff * 4) - sizeof(struct tcphdr); -	ptr = (unsigned char *)(th + 1); +	ptr = (const unsigned char *)(th + 1);  	opt_rx->saw_tstamp = 0;  	while (length > 0) { @@ -3786,10 +3556,9 @@ void tcp_parse_options(struct sk_buff *skb, struct tcp_options_received *opt_rx,  					__u8 snd_wscale = *(__u8 *)ptr;  					opt_rx->wscale_ok = 1;  					if (snd_wscale > 14) { -						if (net_ratelimit()) -							printk(KERN_INFO "tcp_parse_options: Illegal window " -							       "scaling value %d >14 received.\n", -							       snd_wscale); +						net_info_ratelimited("%s: Illegal window scaling value %d >14 received\n", +								     __func__, +								     snd_wscale);  						snd_wscale = 14;  					}  					opt_rx->snd_wscale = snd_wscale; @@ -3807,7 +3576,7 @@ void tcp_parse_options(struct sk_buff *skb, struct tcp_options_received *opt_rx,  			case TCPOPT_SACK_PERM:  				if (opsize == TCPOLEN_SACK_PERM && th->syn &&  				    !estab && sysctl_tcp_sack) { -					opt_rx->sack_ok = 1; +					opt_rx->sack_ok = TCP_SACK_SEEN;  					tcp_sack_reset(opt_rx);  				}  				break; @@ -3827,32 +3596,24 @@ void tcp_parse_options(struct sk_buff *skb, struct tcp_options_received *opt_rx,  				 */  				break;  #endif -			case TCPOPT_COOKIE: -				/* This option is variable length. +			case TCPOPT_EXP: +				/* Fast Open option shares code 254 using a +				 * 16 bits magic number. It's valid only in +				 * SYN or SYN-ACK with an even size.  				 */ -				switch (opsize) { -				case TCPOLEN_COOKIE_BASE: -					/* not yet implemented */ -					break; -				case TCPOLEN_COOKIE_PAIR: -					/* not yet implemented */ -					break; -				case TCPOLEN_COOKIE_MIN+0: -				case TCPOLEN_COOKIE_MIN+2: -				case TCPOLEN_COOKIE_MIN+4: -				case TCPOLEN_COOKIE_MIN+6: -				case TCPOLEN_COOKIE_MAX: -					/* 16-bit multiple */ -					opt_rx->cookie_plus = opsize; -					*hvpp = ptr; -					break; -				default: -					/* ignore option */ +				if (opsize < TCPOLEN_EXP_FASTOPEN_BASE || +				    get_unaligned_be16(ptr) != TCPOPT_FASTOPEN_MAGIC || +				    foc == NULL || !th->syn || (opsize & 1))  					break; -				} +				foc->len = opsize - TCPOLEN_EXP_FASTOPEN_BASE; +				if (foc->len >= TCP_FASTOPEN_COOKIE_MIN && +				    foc->len <= TCP_FASTOPEN_COOKIE_MAX) +					memcpy(foc->val, ptr + 2, foc->len); +				else if (foc->len != 0) +					foc->len = -1;  				break; -			} +			}  			ptr += opsize-2;  			length -= opsize;  		} @@ -3860,9 +3621,9 @@ void tcp_parse_options(struct sk_buff *skb, struct tcp_options_received *opt_rx,  }  EXPORT_SYMBOL(tcp_parse_options); -static int tcp_parse_aligned_timestamp(struct tcp_sock *tp, struct tcphdr *th) +static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)  { -	__be32 *ptr = (__be32 *)(th + 1); +	const __be32 *ptr = (const __be32 *)(th + 1);  	if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)  			  | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) { @@ -3870,41 +3631,48 @@ static int tcp_parse_aligned_timestamp(struct tcp_sock *tp, struct tcphdr *th)  		++ptr;  		tp->rx_opt.rcv_tsval = ntohl(*ptr);  		++ptr; -		tp->rx_opt.rcv_tsecr = ntohl(*ptr); -		return 1; +		if (*ptr) +			tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset; +		else +			tp->rx_opt.rcv_tsecr = 0; +		return true;  	} -	return 0; +	return false;  }  /* Fast parse options. This hopes to only see timestamps.   * If it is wrong it falls back on tcp_parse_options().   */ -static int tcp_fast_parse_options(struct sk_buff *skb, struct tcphdr *th, -				  struct tcp_sock *tp, u8 **hvpp) +static bool tcp_fast_parse_options(const struct sk_buff *skb, +				   const struct tcphdr *th, struct tcp_sock *tp)  {  	/* In the spirit of fast parsing, compare doff directly to constant  	 * values.  Because equality is used, short doff can be ignored here.  	 */  	if (th->doff == (sizeof(*th) / 4)) {  		tp->rx_opt.saw_tstamp = 0; -		return 0; +		return false;  	} else if (tp->rx_opt.tstamp_ok &&  		   th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {  		if (tcp_parse_aligned_timestamp(tp, th)) -			return 1; +			return true;  	} -	tcp_parse_options(skb, &tp->rx_opt, hvpp, 1); -	return 1; + +	tcp_parse_options(skb, &tp->rx_opt, 1, NULL); +	if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) +		tp->rx_opt.rcv_tsecr -= tp->tsoffset; + +	return true;  }  #ifdef CONFIG_TCP_MD5SIG  /*   * Parse MD5 Signature option   */ -u8 *tcp_parse_md5sig_option(struct tcphdr *th) +const u8 *tcp_parse_md5sig_option(const struct tcphdr *th)  { -	int length = (th->doff << 2) - sizeof (*th); -	u8 *ptr = (u8*)(th + 1); +	int length = (th->doff << 2) - sizeof(*th); +	const u8 *ptr = (const u8 *)(th + 1);  	/* If the TCP option is too short, we can short cut */  	if (length < TCPOLEN_MD5SIG) @@ -3914,7 +3682,7 @@ u8 *tcp_parse_md5sig_option(struct tcphdr *th)  		int opcode = *ptr++;  		int opsize; -		switch(opcode) { +		switch (opcode) {  		case TCPOPT_EOL:  			return NULL;  		case TCPOPT_NOP: @@ -3935,27 +3703,6 @@ u8 *tcp_parse_md5sig_option(struct tcphdr *th)  EXPORT_SYMBOL(tcp_parse_md5sig_option);  #endif -static inline void tcp_store_ts_recent(struct tcp_sock *tp) -{ -	tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval; -	tp->rx_opt.ts_recent_stamp = get_seconds(); -} - -static inline void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq) -{ -	if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) { -		/* PAWS bug workaround wrt. ACK frames, the PAWS discard -		 * extra check below makes sure this can only happen -		 * for pure ACK frames.  -DaveM -		 * -		 * Not only, also it occurs for expired timestamps. -		 */ - -		if (tcp_paws_check(&tp->rx_opt, 0)) -			tcp_store_ts_recent(tp); -	} -} -  /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM   *   * It is not fatal. If this ACK does _not_ change critical state (seqs, window) @@ -3981,8 +3728,8 @@ static inline void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)  static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)  { -	struct tcp_sock *tp = tcp_sk(sk); -	struct tcphdr *th = tcp_hdr(skb); +	const struct tcp_sock *tp = tcp_sk(sk); +	const struct tcphdr *th = tcp_hdr(skb);  	u32 seq = TCP_SKB_CB(skb)->seq;  	u32 ack = TCP_SKB_CB(skb)->ack_seq; @@ -3999,7 +3746,7 @@ static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)  		(s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <= (inet_csk(sk)->icsk_rto * 1024) / HZ);  } -static inline int tcp_paws_discard(const struct sock *sk, +static inline bool tcp_paws_discard(const struct sock *sk,  				   const struct sk_buff *skb)  {  	const struct tcp_sock *tp = tcp_sk(sk); @@ -4021,14 +3768,14 @@ static inline int tcp_paws_discard(const struct sock *sk,   * (borrowed from freebsd)   */ -static inline int tcp_sequence(struct tcp_sock *tp, u32 seq, u32 end_seq) +static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)  {  	return	!before(end_seq, tp->rcv_wup) &&  		!after(seq, tp->rcv_nxt + tcp_receive_window(tp));  }  /* When we get a reset we do this. */ -static void tcp_reset(struct sock *sk) +void tcp_reset(struct sock *sk)  {  	/* We want the right error as BSD sees it (and indeed as we do). */  	switch (sk->sk_state) { @@ -4066,9 +3813,10 @@ static void tcp_reset(struct sock *sk)   *   *	If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.   */ -static void tcp_fin(struct sk_buff *skb, struct sock *sk, struct tcphdr *th) +static void tcp_fin(struct sock *sk)  {  	struct tcp_sock *tp = tcp_sk(sk); +	const struct dst_entry *dst;  	inet_csk_schedule_ack(sk); @@ -4080,7 +3828,9 @@ static void tcp_fin(struct sk_buff *skb, struct sock *sk, struct tcphdr *th)  	case TCP_ESTABLISHED:  		/* Move to CLOSE_WAIT */  		tcp_set_state(sk, TCP_CLOSE_WAIT); -		inet_csk(sk)->icsk_ack.pingpong = 1; +		dst = __sk_dst_get(sk); +		if (!dst || !dst_metric(dst, RTAX_QUICKACK)) +			inet_csk(sk)->icsk_ack.pingpong = 1;  		break;  	case TCP_CLOSE_WAIT: @@ -4110,7 +3860,7 @@ static void tcp_fin(struct sk_buff *skb, struct sock *sk, struct tcphdr *th)  		/* Only TCP_LISTEN and TCP_CLOSE are left, in these  		 * cases we should never reach this piece of code.  		 */ -		printk(KERN_ERR "%s: Impossible, sk->sk_state=%d\n", +		pr_err("%s: Impossible, sk->sk_state=%d\n",  		       __func__, sk->sk_state);  		break;  	} @@ -4135,7 +3885,7 @@ static void tcp_fin(struct sk_buff *skb, struct sock *sk, struct tcphdr *th)  	}  } -static inline int tcp_sack_extend(struct tcp_sack_block *sp, u32 seq, +static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,  				  u32 end_seq)  {  	if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) { @@ -4143,9 +3893,9 @@ static inline int tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,  			sp->start_seq = seq;  		if (after(end_seq, sp->end_seq))  			sp->end_seq = end_seq; -		return 1; +		return true;  	} -	return 0; +	return false;  }  static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq) @@ -4178,7 +3928,7 @@ static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)  		tcp_sack_extend(tp->duplicate_sack, seq, end_seq);  } -static void tcp_send_dupack(struct sock *sk, struct sk_buff *skb) +static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)  {  	struct tcp_sock *tp = tcp_sk(sk); @@ -4292,7 +4042,7 @@ static void tcp_sack_remove(struct tcp_sock *tp)  			WARN_ON(before(tp->rcv_nxt, sp->end_seq));  			/* Zap this SACK, by moving forward any other SACKS. */ -			for (i=this_sack+1; i < num_sacks; i++) +			for (i = this_sack+1; i < num_sacks; i++)  				tp->selective_acks[i-1] = tp->selective_acks[i];  			num_sacks--;  			continue; @@ -4337,37 +4087,261 @@ static void tcp_ofo_queue(struct sock *sk)  		__skb_queue_tail(&sk->sk_receive_queue, skb);  		tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;  		if (tcp_hdr(skb)->fin) -			tcp_fin(skb, sk, tcp_hdr(skb)); +			tcp_fin(sk);  	}  } -static int tcp_prune_ofo_queue(struct sock *sk); +static bool tcp_prune_ofo_queue(struct sock *sk);  static int tcp_prune_queue(struct sock *sk); -static inline int tcp_try_rmem_schedule(struct sock *sk, unsigned int size) +static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb, +				 unsigned int size)  {  	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || -	    !sk_rmem_schedule(sk, size)) { +	    !sk_rmem_schedule(sk, skb, size)) {  		if (tcp_prune_queue(sk) < 0)  			return -1; -		if (!sk_rmem_schedule(sk, size)) { +		if (!sk_rmem_schedule(sk, skb, size)) {  			if (!tcp_prune_ofo_queue(sk))  				return -1; -			if (!sk_rmem_schedule(sk, size)) +			if (!sk_rmem_schedule(sk, skb, size))  				return -1;  		}  	}  	return 0;  } +/** + * tcp_try_coalesce - try to merge skb to prior one + * @sk: socket + * @to: prior buffer + * @from: buffer to add in queue + * @fragstolen: pointer to boolean + * + * Before queueing skb @from after @to, try to merge them + * to reduce overall memory use and queue lengths, if cost is small. + * Packets in ofo or receive queues can stay a long time. + * Better try to coalesce them right now to avoid future collapses. + * Returns true if caller should free @from instead of queueing it + */ +static bool tcp_try_coalesce(struct sock *sk, +			     struct sk_buff *to, +			     struct sk_buff *from, +			     bool *fragstolen) +{ +	int delta; + +	*fragstolen = false; + +	if (tcp_hdr(from)->fin) +		return false; + +	/* Its possible this segment overlaps with prior segment in queue */ +	if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq) +		return false; + +	if (!skb_try_coalesce(to, from, fragstolen, &delta)) +		return false; + +	atomic_add(delta, &sk->sk_rmem_alloc); +	sk_mem_charge(sk, delta); +	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE); +	TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq; +	TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq; +	return true; +} + +static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb) +{ +	struct tcp_sock *tp = tcp_sk(sk); +	struct sk_buff *skb1; +	u32 seq, end_seq; + +	TCP_ECN_check_ce(tp, skb); + +	if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) { +		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFODROP); +		__kfree_skb(skb); +		return; +	} + +	/* Disable header prediction. */ +	tp->pred_flags = 0; +	inet_csk_schedule_ack(sk); + +	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOQUEUE); +	SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n", +		   tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); + +	skb1 = skb_peek_tail(&tp->out_of_order_queue); +	if (!skb1) { +		/* Initial out of order segment, build 1 SACK. */ +		if (tcp_is_sack(tp)) { +			tp->rx_opt.num_sacks = 1; +			tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq; +			tp->selective_acks[0].end_seq = +						TCP_SKB_CB(skb)->end_seq; +		} +		__skb_queue_head(&tp->out_of_order_queue, skb); +		goto end; +	} + +	seq = TCP_SKB_CB(skb)->seq; +	end_seq = TCP_SKB_CB(skb)->end_seq; + +	if (seq == TCP_SKB_CB(skb1)->end_seq) { +		bool fragstolen; + +		if (!tcp_try_coalesce(sk, skb1, skb, &fragstolen)) { +			__skb_queue_after(&tp->out_of_order_queue, skb1, skb); +		} else { +			tcp_grow_window(sk, skb); +			kfree_skb_partial(skb, fragstolen); +			skb = NULL; +		} + +		if (!tp->rx_opt.num_sacks || +		    tp->selective_acks[0].end_seq != seq) +			goto add_sack; + +		/* Common case: data arrive in order after hole. */ +		tp->selective_acks[0].end_seq = end_seq; +		goto end; +	} + +	/* Find place to insert this segment. */ +	while (1) { +		if (!after(TCP_SKB_CB(skb1)->seq, seq)) +			break; +		if (skb_queue_is_first(&tp->out_of_order_queue, skb1)) { +			skb1 = NULL; +			break; +		} +		skb1 = skb_queue_prev(&tp->out_of_order_queue, skb1); +	} + +	/* Do skb overlap to previous one? */ +	if (skb1 && before(seq, TCP_SKB_CB(skb1)->end_seq)) { +		if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) { +			/* All the bits are present. Drop. */ +			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE); +			__kfree_skb(skb); +			skb = NULL; +			tcp_dsack_set(sk, seq, end_seq); +			goto add_sack; +		} +		if (after(seq, TCP_SKB_CB(skb1)->seq)) { +			/* Partial overlap. */ +			tcp_dsack_set(sk, seq, +				      TCP_SKB_CB(skb1)->end_seq); +		} else { +			if (skb_queue_is_first(&tp->out_of_order_queue, +					       skb1)) +				skb1 = NULL; +			else +				skb1 = skb_queue_prev( +					&tp->out_of_order_queue, +					skb1); +		} +	} +	if (!skb1) +		__skb_queue_head(&tp->out_of_order_queue, skb); +	else +		__skb_queue_after(&tp->out_of_order_queue, skb1, skb); + +	/* And clean segments covered by new one as whole. */ +	while (!skb_queue_is_last(&tp->out_of_order_queue, skb)) { +		skb1 = skb_queue_next(&tp->out_of_order_queue, skb); + +		if (!after(end_seq, TCP_SKB_CB(skb1)->seq)) +			break; +		if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) { +			tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq, +					 end_seq); +			break; +		} +		__skb_unlink(skb1, &tp->out_of_order_queue); +		tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq, +				 TCP_SKB_CB(skb1)->end_seq); +		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE); +		__kfree_skb(skb1); +	} + +add_sack: +	if (tcp_is_sack(tp)) +		tcp_sack_new_ofo_skb(sk, seq, end_seq); +end: +	if (skb) { +		tcp_grow_window(sk, skb); +		skb_set_owner_r(skb, sk); +	} +} + +static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen, +		  bool *fragstolen) +{ +	int eaten; +	struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue); + +	__skb_pull(skb, hdrlen); +	eaten = (tail && +		 tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0; +	tcp_sk(sk)->rcv_nxt = TCP_SKB_CB(skb)->end_seq; +	if (!eaten) { +		__skb_queue_tail(&sk->sk_receive_queue, skb); +		skb_set_owner_r(skb, sk); +	} +	return eaten; +} + +int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size) +{ +	struct sk_buff *skb = NULL; +	struct tcphdr *th; +	bool fragstolen; + +	if (size == 0) +		return 0; + +	skb = alloc_skb(size + sizeof(*th), sk->sk_allocation); +	if (!skb) +		goto err; + +	if (tcp_try_rmem_schedule(sk, skb, size + sizeof(*th))) +		goto err_free; + +	th = (struct tcphdr *)skb_put(skb, sizeof(*th)); +	skb_reset_transport_header(skb); +	memset(th, 0, sizeof(*th)); + +	if (memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size)) +		goto err_free; + +	TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt; +	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size; +	TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1; + +	if (tcp_queue_rcv(sk, skb, sizeof(*th), &fragstolen)) { +		WARN_ON_ONCE(fragstolen); /* should not happen */ +		__kfree_skb(skb); +	} +	return size; + +err_free: +	kfree_skb(skb); +err: +	return -ENOMEM; +} +  static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)  { -	struct tcphdr *th = tcp_hdr(skb); +	const struct tcphdr *th = tcp_hdr(skb);  	struct tcp_sock *tp = tcp_sk(sk);  	int eaten = -1; +	bool fragstolen = false;  	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq)  		goto drop; @@ -4400,7 +4374,7 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)  			if (!skb_copy_datagram_iovec(skb, 0, tp->ucopy.iov, chunk)) {  				tp->ucopy.len -= chunk;  				tp->copied_seq += chunk; -				eaten = (chunk == skb->len && !th->fin); +				eaten = (chunk == skb->len);  				tcp_rcv_space_adjust(sk);  			}  			local_bh_disable(); @@ -4409,17 +4383,16 @@ static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)  		if (eaten <= 0) {  queue_and_out:  			if (eaten < 0 && -			    tcp_try_rmem_schedule(sk, skb->truesize)) +			    tcp_try_rmem_schedule(sk, skb, skb->truesize))  				goto drop; -			skb_set_owner_r(skb, sk); -			__skb_queue_tail(&sk->sk_receive_queue, skb); +			eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);  		}  		tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;  		if (skb->len)  			tcp_event_data_recv(sk, skb);  		if (th->fin) -			tcp_fin(skb, sk, th); +			tcp_fin(sk);  		if (!skb_queue_empty(&tp->out_of_order_queue)) {  			tcp_ofo_queue(sk); @@ -4437,9 +4410,9 @@ queue_and_out:  		tcp_fast_path_check(sk);  		if (eaten > 0) -			__kfree_skb(skb); -		else if (!sock_flag(sk, SOCK_DEAD)) -			sk->sk_data_ready(sk, 0); +			kfree_skb_partial(skb, fragstolen); +		if (!sock_flag(sk, SOCK_DEAD)) +			sk->sk_data_ready(sk);  		return;  	} @@ -4478,105 +4451,7 @@ drop:  		goto queue_and_out;  	} -	TCP_ECN_check_ce(tp, skb); - -	if (tcp_try_rmem_schedule(sk, skb->truesize)) -		goto drop; - -	/* Disable header prediction. */ -	tp->pred_flags = 0; -	inet_csk_schedule_ack(sk); - -	SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n", -		   tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); - -	skb_set_owner_r(skb, sk); - -	if (!skb_peek(&tp->out_of_order_queue)) { -		/* Initial out of order segment, build 1 SACK. */ -		if (tcp_is_sack(tp)) { -			tp->rx_opt.num_sacks = 1; -			tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq; -			tp->selective_acks[0].end_seq = -						TCP_SKB_CB(skb)->end_seq; -		} -		__skb_queue_head(&tp->out_of_order_queue, skb); -	} else { -		struct sk_buff *skb1 = skb_peek_tail(&tp->out_of_order_queue); -		u32 seq = TCP_SKB_CB(skb)->seq; -		u32 end_seq = TCP_SKB_CB(skb)->end_seq; - -		if (seq == TCP_SKB_CB(skb1)->end_seq) { -			__skb_queue_after(&tp->out_of_order_queue, skb1, skb); - -			if (!tp->rx_opt.num_sacks || -			    tp->selective_acks[0].end_seq != seq) -				goto add_sack; - -			/* Common case: data arrive in order after hole. */ -			tp->selective_acks[0].end_seq = end_seq; -			return; -		} - -		/* Find place to insert this segment. */ -		while (1) { -			if (!after(TCP_SKB_CB(skb1)->seq, seq)) -				break; -			if (skb_queue_is_first(&tp->out_of_order_queue, skb1)) { -				skb1 = NULL; -				break; -			} -			skb1 = skb_queue_prev(&tp->out_of_order_queue, skb1); -		} - -		/* Do skb overlap to previous one? */ -		if (skb1 && before(seq, TCP_SKB_CB(skb1)->end_seq)) { -			if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) { -				/* All the bits are present. Drop. */ -				__kfree_skb(skb); -				tcp_dsack_set(sk, seq, end_seq); -				goto add_sack; -			} -			if (after(seq, TCP_SKB_CB(skb1)->seq)) { -				/* Partial overlap. */ -				tcp_dsack_set(sk, seq, -					      TCP_SKB_CB(skb1)->end_seq); -			} else { -				if (skb_queue_is_first(&tp->out_of_order_queue, -						       skb1)) -					skb1 = NULL; -				else -					skb1 = skb_queue_prev( -						&tp->out_of_order_queue, -						skb1); -			} -		} -		if (!skb1) -			__skb_queue_head(&tp->out_of_order_queue, skb); -		else -			__skb_queue_after(&tp->out_of_order_queue, skb1, skb); - -		/* And clean segments covered by new one as whole. */ -		while (!skb_queue_is_last(&tp->out_of_order_queue, skb)) { -			skb1 = skb_queue_next(&tp->out_of_order_queue, skb); - -			if (!after(end_seq, TCP_SKB_CB(skb1)->seq)) -				break; -			if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) { -				tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq, -						 end_seq); -				break; -			} -			__skb_unlink(skb1, &tp->out_of_order_queue); -			tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq, -					 TCP_SKB_CB(skb1)->end_seq); -			__kfree_skb(skb1); -		} - -add_sack: -		if (tcp_is_sack(tp)) -			tcp_sack_new_ofo_skb(sk, seq, end_seq); -	} +	tcp_data_queue_ofo(sk, skb);  }  static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb, @@ -4755,10 +4630,10 @@ static void tcp_collapse_ofo_queue(struct sock *sk)   * Purge the out-of-order queue.   * Return true if queue was pruned.   */ -static int tcp_prune_ofo_queue(struct sock *sk) +static bool tcp_prune_ofo_queue(struct sock *sk)  {  	struct tcp_sock *tp = tcp_sk(sk); -	int res = 0; +	bool res = false;  	if (!skb_queue_empty(&tp->out_of_order_queue)) {  		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_OFOPRUNED); @@ -4772,7 +4647,7 @@ static int tcp_prune_ofo_queue(struct sock *sk)  		if (tp->rx_opt.sack_ok)  			tcp_sack_reset(&tp->rx_opt);  		sk_mem_reclaim(sk); -		res = 1; +		res = true;  	}  	return res;  } @@ -4794,7 +4669,7 @@ static int tcp_prune_queue(struct sock *sk)  	if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)  		tcp_clamp_window(sk); -	else if (tcp_memory_pressure) +	else if (sk_under_memory_pressure(sk))  		tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);  	tcp_collapse_ofo_queue(sk); @@ -4827,51 +4702,29 @@ static int tcp_prune_queue(struct sock *sk)  	return -1;  } -/* RFC2861, slow part. Adjust cwnd, after it was not full during one rto. - * As additional protections, we do not touch cwnd in retransmission phases, - * and if application hit its sndbuf limit recently. - */ -void tcp_cwnd_application_limited(struct sock *sk) +static bool tcp_should_expand_sndbuf(const struct sock *sk)  { -	struct tcp_sock *tp = tcp_sk(sk); - -	if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open && -	    sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { -		/* Limited by application or receiver window. */ -		u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk)); -		u32 win_used = max(tp->snd_cwnd_used, init_win); -		if (win_used < tp->snd_cwnd) { -			tp->snd_ssthresh = tcp_current_ssthresh(sk); -			tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1; -		} -		tp->snd_cwnd_used = 0; -	} -	tp->snd_cwnd_stamp = tcp_time_stamp; -} - -static int tcp_should_expand_sndbuf(struct sock *sk) -{ -	struct tcp_sock *tp = tcp_sk(sk); +	const struct tcp_sock *tp = tcp_sk(sk);  	/* If the user specified a specific send buffer setting, do  	 * not modify it.  	 */  	if (sk->sk_userlocks & SOCK_SNDBUF_LOCK) -		return 0; +		return false;  	/* If we are under global TCP memory pressure, do not expand.  */ -	if (tcp_memory_pressure) -		return 0; +	if (sk_under_memory_pressure(sk)) +		return false;  	/* If we are under soft global TCP memory pressure, do not expand.  */ -	if (atomic_long_read(&tcp_memory_allocated) >= sysctl_tcp_mem[0]) -		return 0; +	if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0)) +		return false;  	/* If we filled the congestion window, do not expand.  */  	if (tp->packets_out >= tp->snd_cwnd) -		return 0; +		return false; -	return 1; +	return true;  }  /* When incoming ACK allowed to free some skb from write_queue, @@ -4885,13 +4738,7 @@ static void tcp_new_space(struct sock *sk)  	struct tcp_sock *tp = tcp_sk(sk);  	if (tcp_should_expand_sndbuf(sk)) { -		int sndmem = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) + -			MAX_TCP_HEADER + 16 + sizeof(struct sk_buff); -		int demanded = max_t(unsigned int, tp->snd_cwnd, -				     tp->reordering + 1); -		sndmem *= 2 * demanded; -		if (sndmem > sk->sk_sndbuf) -			sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]); +		tcp_sndbuf_expand(sk);  		tp->snd_cwnd_stamp = tcp_time_stamp;  	} @@ -4958,7 +4805,7 @@ static inline void tcp_ack_snd_check(struct sock *sk)   *	either form (or just set the sysctl tcp_stdurg).   */ -static void tcp_check_urg(struct sock *sk, struct tcphdr *th) +static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)  {  	struct tcp_sock *tp = tcp_sk(sk);  	u32 ptr = ntohs(th->urg_ptr); @@ -5024,7 +4871,7 @@ static void tcp_check_urg(struct sock *sk, struct tcphdr *th)  }  /* This is the 'fast' part of urgent handling. */ -static void tcp_urg(struct sock *sk, struct sk_buff *skb, struct tcphdr *th) +static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)  {  	struct tcp_sock *tp = tcp_sk(sk); @@ -5044,7 +4891,7 @@ static void tcp_urg(struct sock *sk, struct sk_buff *skb, struct tcphdr *th)  				BUG();  			tp->urg_data = TCP_URG_VALID | tmp;  			if (!sock_flag(sk, SOCK_DEAD)) -				sk->sk_data_ready(sk, 0); +				sk->sk_data_ready(sk);  		}  	}  } @@ -5087,7 +4934,7 @@ static __sum16 __tcp_checksum_complete_user(struct sock *sk,  	return result;  } -static inline int tcp_checksum_complete_user(struct sock *sk, +static inline bool tcp_checksum_complete_user(struct sock *sk,  					     struct sk_buff *skb)  {  	return !skb_csum_unnecessary(skb) && @@ -5095,19 +4942,19 @@ static inline int tcp_checksum_complete_user(struct sock *sk,  }  #ifdef CONFIG_NET_DMA -static int tcp_dma_try_early_copy(struct sock *sk, struct sk_buff *skb, +static bool tcp_dma_try_early_copy(struct sock *sk, struct sk_buff *skb,  				  int hlen)  {  	struct tcp_sock *tp = tcp_sk(sk);  	int chunk = skb->len - hlen;  	int dma_cookie; -	int copied_early = 0; +	bool copied_early = false;  	if (tp->ucopy.wakeup) -		return 0; +		return false;  	if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list) -		tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY); +		tp->ucopy.dma_chan = net_dma_find_channel();  	if (tp->ucopy.dma_chan && skb_csum_unnecessary(skb)) { @@ -5120,7 +4967,7 @@ static int tcp_dma_try_early_copy(struct sock *sk, struct sk_buff *skb,  			goto out;  		tp->ucopy.dma_cookie = dma_cookie; -		copied_early = 1; +		copied_early = true;  		tp->ucopy.len -= chunk;  		tp->copied_seq += chunk; @@ -5130,11 +4977,11 @@ static int tcp_dma_try_early_copy(struct sock *sk, struct sk_buff *skb,  		    (tcp_flag_word(tcp_hdr(skb)) & TCP_FLAG_PSH) ||  		    (atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1))) {  			tp->ucopy.wakeup = 1; -			sk->sk_data_ready(sk, 0); +			sk->sk_data_ready(sk);  		}  	} else if (chunk > 0) {  		tp->ucopy.wakeup = 1; -		sk->sk_data_ready(sk, 0); +		sk->sk_data_ready(sk);  	}  out:  	return copied_early; @@ -5144,15 +4991,13 @@ out:  /* Does PAWS and seqno based validation of an incoming segment, flags will   * play significant role here.   */ -static int tcp_validate_incoming(struct sock *sk, struct sk_buff *skb, -			      struct tcphdr *th, int syn_inerr) +static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb, +				  const struct tcphdr *th, int syn_inerr)  { -	u8 *hash_location;  	struct tcp_sock *tp = tcp_sk(sk);  	/* RFC1323: H1. Apply PAWS check first. */ -	if (tcp_fast_parse_options(skb, th, tp, &hash_location) && -	    tp->rx_opt.saw_tstamp && +	if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp &&  	    tcp_paws_discard(sk, skb)) {  		if (!th->rst) {  			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED); @@ -5170,38 +5015,48 @@ static int tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,  		 * an acknowledgment should be sent in reply (unless the RST  		 * bit is set, if so drop the segment and return)".  		 */ -		if (!th->rst) +		if (!th->rst) { +			if (th->syn) +				goto syn_challenge;  			tcp_send_dupack(sk, skb); +		}  		goto discard;  	}  	/* Step 2: check RST bit */  	if (th->rst) { -		tcp_reset(sk); +		/* RFC 5961 3.2 : +		 * If sequence number exactly matches RCV.NXT, then +		 *     RESET the connection +		 * else +		 *     Send a challenge ACK +		 */ +		if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) +			tcp_reset(sk); +		else +			tcp_send_challenge_ack(sk);  		goto discard;  	} -	/* ts_recent update must be made after we are sure that the packet -	 * is in window. -	 */ -	tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq); -  	/* step 3: check security and precedence [ignored] */ -	/* step 4: Check for a SYN in window. */ -	if (th->syn && !before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { +	/* step 4: Check for a SYN +	 * RFC 5691 4.2 : Send a challenge ack +	 */ +	if (th->syn) { +syn_challenge:  		if (syn_inerr)  			TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS); -		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONSYN); -		tcp_reset(sk); -		return -1; +		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE); +		tcp_send_challenge_ack(sk); +		goto discard;  	} -	return 1; +	return true;  discard:  	__kfree_skb(skb); -	return 0; +	return false;  }  /* @@ -5227,12 +5082,13 @@ discard:   *	the rest is checked inline. Fast processing is turned on in   *	tcp_data_queue when everything is OK.   */ -int tcp_rcv_established(struct sock *sk, struct sk_buff *skb, -			struct tcphdr *th, unsigned len) +void tcp_rcv_established(struct sock *sk, struct sk_buff *skb, +			 const struct tcphdr *th, unsigned int len)  {  	struct tcp_sock *tp = tcp_sk(sk); -	int res; +	if (unlikely(sk->sk_rx_dst == NULL)) +		inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);  	/*  	 *	Header prediction.  	 *	The code loosely follows the one in the famous @@ -5304,7 +5160,7 @@ int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,  				tcp_ack(sk, skb, 0);  				__kfree_skb(skb);  				tcp_data_snd_check(sk); -				return 0; +				return;  			} else { /* Header too small */  				TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);  				goto discard; @@ -5312,11 +5168,14 @@ int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,  		} else {  			int eaten = 0;  			int copied_early = 0; +			bool fragstolen = false;  			if (tp->copied_seq == tp->rcv_nxt &&  			    len - tcp_header_len <= tp->ucopy.len) {  #ifdef CONFIG_NET_DMA -				if (tcp_dma_try_early_copy(sk, skb, tcp_header_len)) { +				if (tp->ucopy.task == current && +				    sock_owned_by_user(sk) && +				    tcp_dma_try_early_copy(sk, skb, tcp_header_len)) {  					copied_early = 1;  					eaten = 1;  				} @@ -5352,6 +5211,9 @@ int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,  				if (tcp_checksum_complete_user(sk, skb))  					goto csum_error; +				if ((int)skb->truesize > sk->sk_forward_alloc) +					goto step5; +  				/* Predicted packet is in window by definition.  				 * seq == rcv_nxt and rcv_wup <= rcv_nxt.  				 * Hence, check seq<=rcv_wup reduces to: @@ -5363,16 +5225,11 @@ int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,  				tcp_rcv_rtt_measure_ts(sk, skb); -				if ((int)skb->truesize > sk->sk_forward_alloc) -					goto step5; -  				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITS);  				/* Bulk data transfer: receiver */ -				__skb_pull(skb, tcp_header_len); -				__skb_queue_tail(&sk->sk_receive_queue, skb); -				skb_set_owner_r(skb, sk); -				tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; +				eaten = tcp_queue_rcv(sk, skb, tcp_header_len, +						      &fragstolen);  			}  			tcp_event_data_recv(sk, skb); @@ -5394,10 +5251,9 @@ no_ack:  			else  #endif  			if (eaten) -				__kfree_skb(skb); -			else -				sk->sk_data_ready(sk, 0); -			return 0; +				kfree_skb_partial(skb, fragstolen); +			sk->sk_data_ready(sk); +			return;  		}  	} @@ -5405,16 +5261,18 @@ slow_path:  	if (len < (th->doff << 2) || tcp_checksum_complete_user(sk, skb))  		goto csum_error; +	if (!th->ack && !th->rst) +		goto discard; +  	/*  	 *	Standard slow path.  	 */ -	res = tcp_validate_incoming(sk, skb, th, 1); -	if (res <= 0) -		return -res; +	if (!tcp_validate_incoming(sk, skb, th, 1)) +		return;  step5: -	if (th->ack && tcp_ack(sk, skb, FLAG_SLOWPATH) < 0) +	if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)  		goto discard;  	tcp_rcv_rtt_measure_ts(sk, skb); @@ -5427,27 +5285,113 @@ step5:  	tcp_data_snd_check(sk);  	tcp_ack_snd_check(sk); -	return 0; +	return;  csum_error: +	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_CSUMERRORS);  	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);  discard:  	__kfree_skb(skb); -	return 0;  }  EXPORT_SYMBOL(tcp_rcv_established); +void tcp_finish_connect(struct sock *sk, struct sk_buff *skb) +{ +	struct tcp_sock *tp = tcp_sk(sk); +	struct inet_connection_sock *icsk = inet_csk(sk); + +	tcp_set_state(sk, TCP_ESTABLISHED); + +	if (skb != NULL) { +		icsk->icsk_af_ops->sk_rx_dst_set(sk, skb); +		security_inet_conn_established(sk, skb); +	} + +	/* Make sure socket is routed, for correct metrics.  */ +	icsk->icsk_af_ops->rebuild_header(sk); + +	tcp_init_metrics(sk); + +	tcp_init_congestion_control(sk); + +	/* Prevent spurious tcp_cwnd_restart() on first data +	 * packet. +	 */ +	tp->lsndtime = tcp_time_stamp; + +	tcp_init_buffer_space(sk); + +	if (sock_flag(sk, SOCK_KEEPOPEN)) +		inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp)); + +	if (!tp->rx_opt.snd_wscale) +		__tcp_fast_path_on(tp, tp->snd_wnd); +	else +		tp->pred_flags = 0; + +	if (!sock_flag(sk, SOCK_DEAD)) { +		sk->sk_state_change(sk); +		sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT); +	} +} + +static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack, +				    struct tcp_fastopen_cookie *cookie) +{ +	struct tcp_sock *tp = tcp_sk(sk); +	struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL; +	u16 mss = tp->rx_opt.mss_clamp; +	bool syn_drop; + +	if (mss == tp->rx_opt.user_mss) { +		struct tcp_options_received opt; + +		/* Get original SYNACK MSS value if user MSS sets mss_clamp */ +		tcp_clear_options(&opt); +		opt.user_mss = opt.mss_clamp = 0; +		tcp_parse_options(synack, &opt, 0, NULL); +		mss = opt.mss_clamp; +	} + +	if (!tp->syn_fastopen)  /* Ignore an unsolicited cookie */ +		cookie->len = -1; + +	/* The SYN-ACK neither has cookie nor acknowledges the data. Presumably +	 * the remote receives only the retransmitted (regular) SYNs: either +	 * the original SYN-data or the corresponding SYN-ACK is lost. +	 */ +	syn_drop = (cookie->len <= 0 && data && tp->total_retrans); + +	tcp_fastopen_cache_set(sk, mss, cookie, syn_drop); + +	if (data) { /* Retransmit unacked data in SYN */ +		tcp_for_write_queue_from(data, sk) { +			if (data == tcp_send_head(sk) || +			    __tcp_retransmit_skb(sk, data)) +				break; +		} +		tcp_rearm_rto(sk); +		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVEFAIL); +		return true; +	} +	tp->syn_data_acked = tp->syn_data; +	if (tp->syn_data_acked) +		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE); +	return false; +} +  static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, -					 struct tcphdr *th, unsigned len) +					 const struct tcphdr *th, unsigned int len)  { -	u8 *hash_location;  	struct inet_connection_sock *icsk = inet_csk(sk);  	struct tcp_sock *tp = tcp_sk(sk); -	struct tcp_cookie_values *cvp = tp->cookie_values; +	struct tcp_fastopen_cookie foc = { .len = -1 };  	int saved_clamp = tp->rx_opt.mss_clamp; -	tcp_parse_options(skb, &tp->rx_opt, &hash_location, 0); +	tcp_parse_options(skb, &tp->rx_opt, 0, &foc); +	if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) +		tp->rx_opt.rcv_tsecr -= tp->tsoffset;  	if (th->ack) {  		/* rfc793: @@ -5457,11 +5401,9 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,  		 *	  If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send  		 *        a reset (unless the RST bit is set, if so drop  		 *        the segment and return)" -		 * -		 *  We do not send data with SYN, so that RFC-correct -		 *  test reduces to:  		 */ -		if (TCP_SKB_CB(skb)->ack_seq != tp->snd_nxt) +		if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) || +		    after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))  			goto reset_and_undo;  		if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && @@ -5503,7 +5445,7 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,  		TCP_ECN_rcv_synack(tp, th); -		tp->snd_wl1 = TCP_SKB_CB(skb)->seq; +		tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);  		tcp_ack(sk, skb, FLAG_SLOWPATH);  		/* Ok.. it's good. Set up sequence numbers and @@ -5516,7 +5458,6 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,  		 * never scaled.  		 */  		tp->snd_wnd = ntohs(th->window); -		tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);  		if (!tp->rx_opt.wscale_ok) {  			tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0; @@ -5545,61 +5486,13 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,  		 * is initialized. */  		tp->copied_seq = tp->rcv_nxt; -		if (cvp != NULL && -		    cvp->cookie_pair_size > 0 && -		    tp->rx_opt.cookie_plus > 0) { -			int cookie_size = tp->rx_opt.cookie_plus -					- TCPOLEN_COOKIE_BASE; -			int cookie_pair_size = cookie_size -					     + cvp->cookie_desired; - -			/* A cookie extension option was sent and returned. -			 * Note that each incoming SYNACK replaces the -			 * Responder cookie.  The initial exchange is most -			 * fragile, as protection against spoofing relies -			 * entirely upon the sequence and timestamp (above). -			 * This replacement strategy allows the correct pair to -			 * pass through, while any others will be filtered via -			 * Responder verification later. -			 */ -			if (sizeof(cvp->cookie_pair) >= cookie_pair_size) { -				memcpy(&cvp->cookie_pair[cvp->cookie_desired], -				       hash_location, cookie_size); -				cvp->cookie_pair_size = cookie_pair_size; -			} -		} -  		smp_mb(); -		tcp_set_state(sk, TCP_ESTABLISHED); -		security_inet_conn_established(sk, skb); - -		/* Make sure socket is routed, for correct metrics.  */ -		icsk->icsk_af_ops->rebuild_header(sk); - -		tcp_init_metrics(sk); - -		tcp_init_congestion_control(sk); - -		/* Prevent spurious tcp_cwnd_restart() on first data -		 * packet. -		 */ -		tp->lsndtime = tcp_time_stamp; - -		tcp_init_buffer_space(sk); +		tcp_finish_connect(sk, skb); -		if (sock_flag(sk, SOCK_KEEPOPEN)) -			inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp)); - -		if (!tp->rx_opt.snd_wscale) -			__tcp_fast_path_on(tp, tp->snd_wnd); -		else -			tp->pred_flags = 0; - -		if (!sock_flag(sk, SOCK_DEAD)) { -			sk->sk_state_change(sk); -			sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT); -		} +		if ((tp->syn_fastopen || tp->syn_data) && +		    tcp_rcv_fastopen_synack(sk, skb, &foc)) +			return -1;  		if (sk->sk_write_pending ||  		    icsk->icsk_accept_queue.rskq_defer_accept || @@ -5613,8 +5506,6 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,  			 */  			inet_csk_schedule_ack(sk);  			icsk->icsk_ack.lrcvtime = tcp_time_stamp; -			icsk->icsk_ack.ato	 = TCP_ATO_MIN; -			tcp_incr_quickack(sk);  			tcp_enter_quickack_mode(sk);  			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,  						  TCP_DELACK_MAX, TCP_RTO_MAX); @@ -5680,7 +5571,9 @@ discard:  		tcp_send_synack(sk);  #if 0  		/* Note, we could accept data and URG from this segment. -		 * There are no obstacles to make this. +		 * There are no obstacles to make this (except that we must +		 * either change tcp_recvmsg() to prevent it from returning data +		 * before 3WHS completes per RFC793, or employ TCP Fast Open).  		 *  		 * However, if we ignore data in ACKless segments sometimes,  		 * we have no reasons to accept it sometimes. @@ -5716,12 +5609,14 @@ reset_and_undo:   */  int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, -			  struct tcphdr *th, unsigned len) +			  const struct tcphdr *th, unsigned int len)  {  	struct tcp_sock *tp = tcp_sk(sk);  	struct inet_connection_sock *icsk = inet_csk(sk); +	struct request_sock *req;  	int queued = 0; -	int res; +	bool acceptable; +	u32 synack_stamp;  	tp->rx_opt.saw_tstamp = 0; @@ -5737,6 +5632,8 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,  			goto discard;  		if (th->syn) { +			if (th->fin) +				goto discard;  			if (icsk->icsk_af_ops->conn_request(sk, skb) < 0)  				return 1; @@ -5774,124 +5671,167 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,  		return 0;  	} -	res = tcp_validate_incoming(sk, skb, th, 0); -	if (res <= 0) -		return -res; +	req = tp->fastopen_rsk; +	if (req != NULL) { +		WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV && +		    sk->sk_state != TCP_FIN_WAIT1); + +		if (tcp_check_req(sk, skb, req, NULL, true) == NULL) +			goto discard; +	} + +	if (!th->ack && !th->rst) +		goto discard; + +	if (!tcp_validate_incoming(sk, skb, th, 0)) +		return 0;  	/* step 5: check the ACK field */ -	if (th->ack) { -		int acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH) > 0; - -		switch (sk->sk_state) { -		case TCP_SYN_RECV: -			if (acceptable) { -				tp->copied_seq = tp->rcv_nxt; -				smp_mb(); -				tcp_set_state(sk, TCP_ESTABLISHED); -				sk->sk_state_change(sk); - -				/* Note, that this wakeup is only for marginal -				 * crossed SYN case. Passively open sockets -				 * are not waked up, because sk->sk_sleep == -				 * NULL and sk->sk_socket == NULL. -				 */ -				if (sk->sk_socket) -					sk_wake_async(sk, -						      SOCK_WAKE_IO, POLL_OUT); - -				tp->snd_una = TCP_SKB_CB(skb)->ack_seq; -				tp->snd_wnd = ntohs(th->window) << -					      tp->rx_opt.snd_wscale; -				tcp_init_wl(tp, TCP_SKB_CB(skb)->seq); - -				/* tcp_ack considers this ACK as duplicate -				 * and does not calculate rtt. -				 * Force it here. -				 */ -				tcp_ack_update_rtt(sk, 0, 0); +	acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH | +				      FLAG_UPDATE_TS_RECENT) > 0; -				if (tp->rx_opt.tstamp_ok) -					tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; +	switch (sk->sk_state) { +	case TCP_SYN_RECV: +		if (!acceptable) +			return 1; -				/* Make sure socket is routed, for -				 * correct metrics. -				 */ -				icsk->icsk_af_ops->rebuild_header(sk); +		/* Once we leave TCP_SYN_RECV, we no longer need req +		 * so release it. +		 */ +		if (req) { +			synack_stamp = tcp_rsk(req)->snt_synack; +			tp->total_retrans = req->num_retrans; +			reqsk_fastopen_remove(sk, req, false); +		} else { +			synack_stamp = tp->lsndtime; +			/* Make sure socket is routed, for correct metrics. */ +			icsk->icsk_af_ops->rebuild_header(sk); +			tcp_init_congestion_control(sk); + +			tcp_mtup_init(sk); +			tp->copied_seq = tp->rcv_nxt; +			tcp_init_buffer_space(sk); +		} +		smp_mb(); +		tcp_set_state(sk, TCP_ESTABLISHED); +		sk->sk_state_change(sk); -				tcp_init_metrics(sk); +		/* Note, that this wakeup is only for marginal crossed SYN case. +		 * Passively open sockets are not waked up, because +		 * sk->sk_sleep == NULL and sk->sk_socket == NULL. +		 */ +		if (sk->sk_socket) +			sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT); -				tcp_init_congestion_control(sk); +		tp->snd_una = TCP_SKB_CB(skb)->ack_seq; +		tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale; +		tcp_init_wl(tp, TCP_SKB_CB(skb)->seq); +		tcp_synack_rtt_meas(sk, synack_stamp); -				/* Prevent spurious tcp_cwnd_restart() on -				 * first data packet. -				 */ -				tp->lsndtime = tcp_time_stamp; +		if (tp->rx_opt.tstamp_ok) +			tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; -				tcp_mtup_init(sk); -				tcp_initialize_rcv_mss(sk); -				tcp_init_buffer_space(sk); -				tcp_fast_path_on(tp); -			} else { +		if (req) { +			/* Re-arm the timer because data may have been sent out. +			 * This is similar to the regular data transmission case +			 * when new data has just been ack'ed. +			 * +			 * (TFO) - we could try to be more aggressive and +			 * retransmitting any data sooner based on when they +			 * are sent out. +			 */ +			tcp_rearm_rto(sk); +		} else +			tcp_init_metrics(sk); + +		tcp_update_pacing_rate(sk); + +		/* Prevent spurious tcp_cwnd_restart() on first data packet */ +		tp->lsndtime = tcp_time_stamp; + +		tcp_initialize_rcv_mss(sk); +		tcp_fast_path_on(tp); +		break; + +	case TCP_FIN_WAIT1: { +		struct dst_entry *dst; +		int tmo; + +		/* If we enter the TCP_FIN_WAIT1 state and we are a +		 * Fast Open socket and this is the first acceptable +		 * ACK we have received, this would have acknowledged +		 * our SYNACK so stop the SYNACK timer. +		 */ +		if (req != NULL) { +			/* Return RST if ack_seq is invalid. +			 * Note that RFC793 only says to generate a +			 * DUPACK for it but for TCP Fast Open it seems +			 * better to treat this case like TCP_SYN_RECV +			 * above. +			 */ +			if (!acceptable)  				return 1; -			} +			/* We no longer need the request sock. */ +			reqsk_fastopen_remove(sk, req, false); +			tcp_rearm_rto(sk); +		} +		if (tp->snd_una != tp->write_seq)  			break; -		case TCP_FIN_WAIT1: -			if (tp->snd_una == tp->write_seq) { -				tcp_set_state(sk, TCP_FIN_WAIT2); -				sk->sk_shutdown |= SEND_SHUTDOWN; -				dst_confirm(__sk_dst_get(sk)); - -				if (!sock_flag(sk, SOCK_DEAD)) -					/* Wake up lingering close() */ -					sk->sk_state_change(sk); -				else { -					int tmo; - -					if (tp->linger2 < 0 || -					    (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && -					     after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) { -						tcp_done(sk); -						NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA); -						return 1; -					} +		tcp_set_state(sk, TCP_FIN_WAIT2); +		sk->sk_shutdown |= SEND_SHUTDOWN; -					tmo = tcp_fin_time(sk); -					if (tmo > TCP_TIMEWAIT_LEN) { -						inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN); -					} else if (th->fin || sock_owned_by_user(sk)) { -						/* Bad case. We could lose such FIN otherwise. -						 * It is not a big problem, but it looks confusing -						 * and not so rare event. We still can lose it now, -						 * if it spins in bh_lock_sock(), but it is really -						 * marginal case. -						 */ -						inet_csk_reset_keepalive_timer(sk, tmo); -					} else { -						tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); -						goto discard; -					} -				} -			} -			break; +		dst = __sk_dst_get(sk); +		if (dst) +			dst_confirm(dst); -		case TCP_CLOSING: -			if (tp->snd_una == tp->write_seq) { -				tcp_time_wait(sk, TCP_TIME_WAIT, 0); -				goto discard; -			} +		if (!sock_flag(sk, SOCK_DEAD)) { +			/* Wake up lingering close() */ +			sk->sk_state_change(sk);  			break; +		} -		case TCP_LAST_ACK: -			if (tp->snd_una == tp->write_seq) { -				tcp_update_metrics(sk); -				tcp_done(sk); -				goto discard; -			} -			break; +		if (tp->linger2 < 0 || +		    (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && +		     after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) { +			tcp_done(sk); +			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA); +			return 1;  		} -	} else -		goto discard; + +		tmo = tcp_fin_time(sk); +		if (tmo > TCP_TIMEWAIT_LEN) { +			inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN); +		} else if (th->fin || sock_owned_by_user(sk)) { +			/* Bad case. We could lose such FIN otherwise. +			 * It is not a big problem, but it looks confusing +			 * and not so rare event. We still can lose it now, +			 * if it spins in bh_lock_sock(), but it is really +			 * marginal case. +			 */ +			inet_csk_reset_keepalive_timer(sk, tmo); +		} else { +			tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); +			goto discard; +		} +		break; +	} + +	case TCP_CLOSING: +		if (tp->snd_una == tp->write_seq) { +			tcp_time_wait(sk, TCP_TIME_WAIT, 0); +			goto discard; +		} +		break; + +	case TCP_LAST_ACK: +		if (tp->snd_una == tp->write_seq) { +			tcp_update_metrics(sk); +			tcp_done(sk); +			goto discard; +		} +		break; +	}  	/* step 6: check the URG bit */  	tcp_urg(sk, skb, th);  | 
