Skip to content
Snippets Groups Projects
tcp_output.c 94 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Advance write_seq and place onto the write_queue. */
    	tp->write_seq = TCP_SKB_CB(skb)->end_seq;
    	skb_header_release(skb);
    
    	tcp_add_write_queue_tail(sk, skb);
    
    	sk->sk_wmem_queued += skb->truesize;
    	sk_mem_charge(sk, skb->truesize);
    
    /* Initialize TSO segments for a packet. */
    
    static void tcp_set_skb_tso_segs(const struct sock *sk, struct sk_buff *skb,
    
    				 unsigned int mss_now)
    
    	struct skb_shared_info *shinfo = skb_shinfo(skb);
    
    
    	/* Make sure we own this skb before messing gso_size/gso_segs */
    	WARN_ON_ONCE(skb_cloned(skb));
    
    
    	if (skb->len <= mss_now || skb->ip_summed == CHECKSUM_NONE) {
    
    		/* Avoid the costly divide in the normal
    		 * non-TSO case.
    		 */
    
    		shinfo->gso_segs = 1;
    		shinfo->gso_size = 0;
    		shinfo->gso_type = 0;
    
    		shinfo->gso_segs = DIV_ROUND_UP(skb->len, mss_now);
    		shinfo->gso_size = mss_now;
    		shinfo->gso_type = sk->sk_gso_type;
    
    /* When a modification to fackets out becomes necessary, we need to check
    
     * skb is counted to fackets_out or not.
    
    static void tcp_adjust_fackets_out(struct sock *sk, const struct sk_buff *skb,
    
    	if (!tp->sacked_out || tcp_is_reno(tp))
    
    	if (after(tcp_highest_sack_seq(tp), TCP_SKB_CB(skb)->seq))
    
    		tp->fackets_out -= decr;
    }
    
    
    /* Pcount in the middle of the write queue got changed, we need to do various
     * tweaks to fix counters
     */
    
    static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int decr)
    
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	tp->packets_out -= decr;
    
    	if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
    		tp->sacked_out -= decr;
    	if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
    		tp->retrans_out -= decr;
    	if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST)
    		tp->lost_out -= decr;
    
    	/* Reno case is special. Sigh... */
    	if (tcp_is_reno(tp) && decr > 0)
    		tp->sacked_out -= min_t(u32, tp->sacked_out, decr);
    
    	tcp_adjust_fackets_out(sk, skb, decr);
    
    	if (tp->lost_skb_hint &&
    	    before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) &&
    
    	    (tcp_is_fack(tp) || (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)))
    
    		tp->lost_cnt_hint -= decr;
    
    	tcp_verify_left_out(tp);
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /* Function to create two new TCP segments.  Shrinks the given segment
     * to the specified size and appends a new segment with the rest of the
    
     * packet to the list.  This won't be called frequently, I hope.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * Remember, these are still headerless SKBs at this point.
     */
    
    int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len,
    		 unsigned int mss_now)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct sk_buff *buff;
    
    	int nsize, old_factor;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (WARN_ON(len > skb->len))
    		return -EINVAL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	nsize = skb_headlen(skb) - len;
    	if (nsize < 0)
    		nsize = 0;
    
    
    	if (skb_unclone(skb, GFP_ATOMIC))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -ENOMEM;
    
    	/* Get a new skb... force flag on. */
    	buff = sk_stream_alloc_skb(sk, nsize, GFP_ATOMIC);
    	if (buff == NULL)
    		return -ENOMEM; /* We'll just try again later. */
    
    	sk->sk_wmem_queued += buff->truesize;
    	sk_mem_charge(sk, buff->truesize);
    
    	nlen = skb->len - len - nsize;
    	buff->truesize += nlen;
    	skb->truesize -= nlen;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Correct the sequence numbers. */
    	TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
    	TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
    	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
    
    	/* PSH and FIN should only be set in the second packet. */
    
    	flags = TCP_SKB_CB(skb)->tcp_flags;
    	TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH);
    	TCP_SKB_CB(buff)->tcp_flags = flags;
    
    	TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (!skb_shinfo(skb)->nr_frags && skb->ip_summed != CHECKSUM_PARTIAL) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		/* Copy and checksum data tail into the new buffer. */
    
    		buff->csum = csum_partial_copy_nocheck(skb->data + len,
    						       skb_put(buff, nsize),
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    						       nsize, 0);
    
    		skb_trim(skb, len);
    
    		skb->csum = csum_block_sub(skb->csum, buff->csum, len);
    	} else {
    
    		skb->ip_summed = CHECKSUM_PARTIAL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		skb_split(skb, buff, len);
    	}
    
    	buff->ip_summed = skb->ip_summed;
    
    	/* Looks stupid, but our code really uses when of
    	 * skbs, which it never sent before. --ANK
    	 */
    	TCP_SKB_CB(buff)->when = TCP_SKB_CB(skb)->when;
    
    	buff->tstamp = skb->tstamp;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	old_factor = tcp_skb_pcount(skb);
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/* Fix up tso_factor for both original and new SKB.  */
    
    	tcp_set_skb_tso_segs(sk, skb, mss_now);
    	tcp_set_skb_tso_segs(sk, buff, mss_now);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	/* If this packet has been sent out already, we must
    	 * adjust the various packet counters.
    	 */
    
    	if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) {
    
    		int diff = old_factor - tcp_skb_pcount(skb) -
    			tcp_skb_pcount(buff);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		if (diff)
    			tcp_adjust_pcount(sk, skb, diff);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	/* Link BUFF into the send queue. */
    
    	tcp_insert_write_queue_after(skb, buff, sk);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	return 0;
    }
    
    /* This is similar to __pskb_pull_head() (it will go to core/skbuff.c
     * eventually). The difference is that pulled data not copied, but
     * immediately discarded.
     */
    
    static void __pskb_trim_head(struct sk_buff *skb, int len)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct skb_shared_info *shinfo;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int i, k, eat;
    
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	eat = min_t(int, len, skb_headlen(skb));
    	if (eat) {
    		__skb_pull(skb, eat);
    		len -= eat;
    		if (!len)
    			return;
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	eat = len;
    	k = 0;
    
    	shinfo = skb_shinfo(skb);
    	for (i = 0; i < shinfo->nr_frags; i++) {
    		int size = skb_frag_size(&shinfo->frags[i]);
    
    
    		if (size <= eat) {
    
    			skb_frag_unref(skb, i);
    
    			eat -= size;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		} else {
    
    			shinfo->frags[k] = shinfo->frags[i];
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			if (eat) {
    
    				shinfo->frags[k].page_offset += eat;
    				skb_frag_size_sub(&shinfo->frags[k], eat);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				eat = 0;
    			}
    			k++;
    		}
    	}
    
    	shinfo->nr_frags = k;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	skb_reset_tail_pointer(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	skb->data_len -= len;
    	skb->len = skb->data_len;
    }
    
    
    /* Remove acked data from a packet in the transmit queue. */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
    {
    
    	if (skb_unclone(skb, GFP_ATOMIC))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -ENOMEM;
    
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	__pskb_trim_head(skb, len);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	TCP_SKB_CB(skb)->seq += len;
    
    	skb->ip_summed = CHECKSUM_PARTIAL;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	skb->truesize	     -= len;
    	sk->sk_wmem_queued   -= len;
    
    	sk_mem_uncharge(sk, len);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	sock_set_flag(sk, SOCK_QUEUE_SHRUNK);
    
    
    	/* Any change of skb->len requires recalculation of tso factor. */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (tcp_skb_pcount(skb) > 1)
    
    		tcp_set_skb_tso_segs(sk, skb, tcp_skb_mss(skb));
    
    /* Calculate MSS not accounting any TCP options.  */
    static inline int __tcp_mtu_to_mss(struct sock *sk, int pmtu)
    
    John Heffner's avatar
    John Heffner committed
    {
    
    	const struct tcp_sock *tp = tcp_sk(sk);
    	const struct inet_connection_sock *icsk = inet_csk(sk);
    
    John Heffner's avatar
    John Heffner committed
    	int mss_now;
    
    	/* Calculate base mss without TCP options:
    	   It is MMS_S - sizeof(tcphdr) of rfc1122
    	 */
    	mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr);
    
    
    	/* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */
    	if (icsk->icsk_af_ops->net_frag_header_len) {
    		const struct dst_entry *dst = __sk_dst_get(sk);
    
    		if (dst && dst_allfrag(dst))
    			mss_now -= icsk->icsk_af_ops->net_frag_header_len;
    	}
    
    
    John Heffner's avatar
    John Heffner committed
    	/* Clamp it (mss_clamp does not include tcp options) */
    	if (mss_now > tp->rx_opt.mss_clamp)
    		mss_now = tp->rx_opt.mss_clamp;
    
    	/* Now subtract optional transport overhead */
    	mss_now -= icsk->icsk_ext_hdr_len;
    
    	/* Then reserve room for full set of TCP options and 8 bytes of data */
    	if (mss_now < 48)
    		mss_now = 48;
    	return mss_now;
    }
    
    
    /* Calculate MSS. Not accounting for SACKs here.  */
    int tcp_mtu_to_mss(struct sock *sk, int pmtu)
    {
    	/* Subtract TCP options size, not including SACKs */
    	return __tcp_mtu_to_mss(sk, pmtu) -
    	       (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr));
    }
    
    
    John Heffner's avatar
    John Heffner committed
    /* Inverse of above */
    
    int tcp_mss_to_mtu(struct sock *sk, int mss)
    
    John Heffner's avatar
    John Heffner committed
    {
    
    	const struct tcp_sock *tp = tcp_sk(sk);
    	const struct inet_connection_sock *icsk = inet_csk(sk);
    
    John Heffner's avatar
    John Heffner committed
    	int mtu;
    
    	mtu = mss +
    	      tp->tcp_header_len +
    	      icsk->icsk_ext_hdr_len +
    	      icsk->icsk_af_ops->net_header_len;
    
    
    	/* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */
    	if (icsk->icsk_af_ops->net_frag_header_len) {
    		const struct dst_entry *dst = __sk_dst_get(sk);
    
    		if (dst && dst_allfrag(dst))
    			mtu += icsk->icsk_af_ops->net_frag_header_len;
    	}
    
    John Heffner's avatar
    John Heffner committed
    	return mtu;
    }
    
    
    /* MTU probing init per socket */
    
    John Heffner's avatar
    John Heffner committed
    void tcp_mtup_init(struct sock *sk)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct inet_connection_sock *icsk = inet_csk(sk);
    
    	icsk->icsk_mtup.enabled = sysctl_tcp_mtu_probing > 1;
    	icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) +
    
    			       icsk->icsk_af_ops->net_header_len;
    
    John Heffner's avatar
    John Heffner committed
    	icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, sysctl_tcp_base_mss);
    	icsk->icsk_mtup.probe_size = 0;
    }
    
    EXPORT_SYMBOL(tcp_mtup_init);
    
    John Heffner's avatar
    John Heffner committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /* This function synchronize snd mss to current pmtu/exthdr set.
    
       tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
       for TCP options, but includes only bare TCP header.
    
       tp->rx_opt.mss_clamp is mss negotiated at connection setup.
    
    Stephen Hemminger's avatar
    Stephen Hemminger committed
       It is minimum of user_mss and mss received with SYN.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
       It also does not include TCP options.
    
    
       inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
       tp->mss_cache is current effective sending mss, including
       all tcp options except for SACKs. It is evaluated,
       taking into account current pmtu, but never exceeds
       tp->rx_opt.mss_clamp.
    
       NOTE1. rfc1122 clearly states that advertised MSS
       DOES NOT include either tcp or ip options.
    
    
       NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
       are READ ONLY outside this function.		--ANK (980731)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	struct inet_connection_sock *icsk = inet_csk(sk);
    
    John Heffner's avatar
    John Heffner committed
    	int mss_now;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    John Heffner's avatar
    John Heffner committed
    	if (icsk->icsk_mtup.search_high > pmtu)
    		icsk->icsk_mtup.search_high = pmtu;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    John Heffner's avatar
    John Heffner committed
    	mss_now = tcp_mtu_to_mss(sk, pmtu);
    
    	mss_now = tcp_bound_to_half_wnd(tp, mss_now);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* And store cached results */
    
    	icsk->icsk_pmtu_cookie = pmtu;
    
    John Heffner's avatar
    John Heffner committed
    	if (icsk->icsk_mtup.enabled)
    		mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low));
    
    	tp->mss_cache = mss_now;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	return mss_now;
    }
    
    EXPORT_SYMBOL(tcp_sync_mss);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    /* Compute the current effective MSS, taking SACKs and IP options,
     * and even PMTU discovery events into account.
     */
    
    unsigned int tcp_current_mss(struct sock *sk)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	const struct tcp_sock *tp = tcp_sk(sk);
    	const struct dst_entry *dst = __sk_dst_get(sk);
    
    	unsigned int header_len;
    
    Adam Langley's avatar
    Adam Langley committed
    	struct tcp_out_options opts;
    	struct tcp_md5sig_key *md5;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (dst) {
    		u32 mtu = dst_mtu(dst);
    
    		if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			mss_now = tcp_sync_mss(sk, mtu);
    	}
    
    
    Adam Langley's avatar
    Adam Langley committed
    	header_len = tcp_established_options(sk, NULL, &opts, &md5) +
    		     sizeof(struct tcphdr);
    	/* The mss_cache is sized based on tp->tcp_header_len, which assumes
    	 * some common options. If this is an odd packet (because we have SACK
    	 * blocks etc) then our calculated header_len will be different, and
    	 * we have to adjust mss_now correspondingly */
    	if (header_len != tp->tcp_header_len) {
    		int delta = (int) header_len - tp->tcp_header_len;
    		mss_now -= delta;
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return mss_now;
    }
    
    
    /* Congestion window validation. (RFC2861) */
    
    static void tcp_cwnd_validate(struct sock *sk)
    
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	if (tp->packets_out >= tp->snd_cwnd) {
    
    		/* Network is feed fully. */
    		tp->snd_cwnd_used = 0;
    		tp->snd_cwnd_stamp = tcp_time_stamp;
    	} else {
    		/* Network starves. */
    		if (tp->packets_out > tp->snd_cwnd_used)
    			tp->snd_cwnd_used = tp->packets_out;
    
    
    		if (sysctl_tcp_slow_start_after_idle &&
    		    (s32)(tcp_time_stamp - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto)
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    /* Minshall's variant of the Nagle send check. */
    static bool tcp_minshall_check(const struct tcp_sock *tp)
    {
    	return after(tp->snd_sml, tp->snd_una) &&
    		!after(tp->snd_sml, tp->snd_nxt);
    }
    
    /* Update snd_sml if this skb is under mss
     * Note that a TSO packet might end with a sub-mss segment
     * The test is really :
     * if ((skb->len % mss) != 0)
     *        tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
     * But we can avoid doing the divide again given we already have
     *  skb_pcount = skb->len / mss_now
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss_now,
    				const struct sk_buff *skb)
    {
    	if (skb->len < tcp_skb_pcount(skb) * mss_now)
    		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
    }
    
    /* Return false, if packet can be sent now without violation Nagle's rules:
     * 1. It is full sized. (provided by caller in %partial bool)
     * 2. Or it contains FIN. (already checked by caller)
     * 3. Or TCP_CORK is not set, and TCP_NODELAY is set.
     * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
     *    With Minshall's modification: all sent small packets are ACKed.
     */
    static bool tcp_nagle_check(bool partial, const struct tcp_sock *tp,
    			    unsigned int mss_now, int nonagle)
    {
    	return partial &&
    		((nonagle & TCP_NAGLE_CORK) ||
    		 (!nonagle && tp->packets_out && tcp_minshall_check(tp)));
    }
    /* Returns the portion of skb which can be sent right away */
    static unsigned int tcp_mss_split_point(const struct sock *sk,
    					const struct sk_buff *skb,
    					unsigned int mss_now,
    					unsigned int max_segs,
    					int nonagle)
    
    	const struct tcp_sock *tp = tcp_sk(sk);
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	u32 partial, needed, window, max_len;
    
    	window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
    
    	max_len = mss_now * max_segs;
    
    	if (likely(max_len <= window && skb != tcp_write_queue_tail(sk)))
    		return max_len;
    
    	if (max_len <= needed)
    		return max_len;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	partial = needed % mss_now;
    	/* If last segment is not a full MSS, check if Nagle rules allow us
    	 * to include this last segment in this skb.
    	 * Otherwise, we'll split the skb at last MSS boundary
    	 */
    	if (tcp_nagle_check(partial != 0, tp, mss_now, nonagle))
    		return needed - partial;
    
    	return needed;
    
    }
    
    /* Can at least one segment of SKB be sent right now, according to the
     * congestion window rules?  If so, return how many segments are allowed.
     */
    
    static inline unsigned int tcp_cwnd_test(const struct tcp_sock *tp,
    					 const struct sk_buff *skb)
    
    {
    	u32 in_flight, cwnd;
    
    	/* Don't be strict about the congestion window for the final FIN.  */
    
    	if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) &&
    	    tcp_skb_pcount(skb) == 1)
    
    		return 1;
    
    	in_flight = tcp_packets_in_flight(tp);
    	cwnd = tp->snd_cwnd;
    	if (in_flight < cwnd)
    		return (cwnd - in_flight);
    
    	return 0;
    }
    
    
    /* Initialize TSO state of a skb.
    
     * This must be invoked the first time we consider transmitting
    
     * SKB onto the wire.
     */
    
    static int tcp_init_tso_segs(const struct sock *sk, struct sk_buff *skb,
    
    			     unsigned int mss_now)
    
    {
    	int tso_segs = tcp_skb_pcount(skb);
    
    
    	if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) {
    
    		tcp_set_skb_tso_segs(sk, skb, mss_now);
    
    		tso_segs = tcp_skb_pcount(skb);
    	}
    	return tso_segs;
    }
    
    
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    /* Return true if the Nagle test allows this packet to be
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb,
    				  unsigned int cur_mss, int nonagle)
    
    {
    	/* Nagle rule does not apply to frames, which sit in the middle of the
    	 * write_queue (they have no chances to get new data).
    	 *
    	 * This is implemented in the callers, where they modify the 'nonagle'
    	 * argument based upon the location of SKB in the send queue.
    	 */
    	if (nonagle & TCP_NAGLE_PUSH)
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return true;
    
    Yuchung Cheng's avatar
    Yuchung Cheng committed
    	/* Don't use the nagle rule for urgent data (or for the final FIN). */
    	if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return true;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	if (!tcp_nagle_check(skb->len < cur_mss, tp, cur_mss, nonagle))
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return true;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	return false;
    
    }
    
    /* Does at least the first segment of SKB fit into the send window? */
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static bool tcp_snd_wnd_test(const struct tcp_sock *tp,
    			     const struct sk_buff *skb,
    			     unsigned int cur_mss)
    
    {
    	u32 end_seq = TCP_SKB_CB(skb)->end_seq;
    
    	if (skb->len > cur_mss)
    		end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
    
    
    	return !after(end_seq, tcp_wnd_end(tp));
    
    /* This checks if the data bearing packet SKB (usually tcp_send_head(sk))
    
     * should be put on the wire right now.  If so, it returns the number of
     * packets allowed by the congestion window.
     */
    
    static unsigned int tcp_snd_test(const struct sock *sk, struct sk_buff *skb,
    
    				 unsigned int cur_mss, int nonagle)
    {
    
    	const struct tcp_sock *tp = tcp_sk(sk);
    
    	unsigned int cwnd_quota;
    
    
    	tcp_init_tso_segs(sk, skb, cur_mss);
    
    
    	if (!tcp_nagle_test(tp, skb, cur_mss, nonagle))
    		return 0;
    
    	cwnd_quota = tcp_cwnd_test(tp, skb);
    
    	if (cwnd_quota && !tcp_snd_wnd_test(tp, skb, cur_mss))
    
    		cwnd_quota = 0;
    
    	return cwnd_quota;
    }
    
    
    /* Test if sending is allowed right now. */
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    bool tcp_may_send_now(struct sock *sk)
    
    	const struct tcp_sock *tp = tcp_sk(sk);
    
    	struct sk_buff *skb = tcp_send_head(sk);
    
    	return skb &&
    
    		tcp_snd_test(sk, skb, tcp_current_mss(sk),
    
    			     (tcp_skb_is_last(sk, skb) ?
    
    			      tp->nonagle : TCP_NAGLE_PUSH));
    
    }
    
    /* Trim TSO SKB to LEN bytes, put the remaining data into a new packet
     * which is put after SKB on the list.  It is very much like
     * tcp_fragment() except that it may make several kinds of assumptions
     * in order to speed up the splitting operation.  In particular, we
     * know that all the data is in scatter-gather pages, and that the
     * packet has never been sent out before (and thus is not cloned).
     */
    
    static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len,
    
    			unsigned int mss_now, gfp_t gfp)
    
    {
    	struct sk_buff *buff;
    	int nlen = skb->len - len;
    
    
    	/* All of a TSO frame must be composed of paged data.  */
    
    	if (skb->len != skb->data_len)
    		return tcp_fragment(sk, skb, len, mss_now);
    
    	buff = sk_stream_alloc_skb(sk, 0, gfp);
    
    	if (unlikely(buff == NULL))
    		return -ENOMEM;
    
    
    	sk->sk_wmem_queued += buff->truesize;
    	sk_mem_charge(sk, buff->truesize);
    
    	buff->truesize += nlen;
    
    	skb->truesize -= nlen;
    
    	/* Correct the sequence numbers. */
    	TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
    	TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
    	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
    
    	/* PSH and FIN should only be set in the second packet. */
    
    	flags = TCP_SKB_CB(skb)->tcp_flags;
    	TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH);
    	TCP_SKB_CB(buff)->tcp_flags = flags;
    
    
    	/* This packet was never sent out yet, so no SACK bits. */
    	TCP_SKB_CB(buff)->sacked = 0;
    
    
    	buff->ip_summed = skb->ip_summed = CHECKSUM_PARTIAL;
    
    	skb_split(skb, buff, len);
    
    	/* Fix up tso_factor for both original and new SKB.  */
    
    	tcp_set_skb_tso_segs(sk, skb, mss_now);
    	tcp_set_skb_tso_segs(sk, buff, mss_now);
    
    
    	/* Link BUFF into the send queue. */
    	skb_header_release(buff);
    
    	tcp_insert_write_queue_after(skb, buff, sk);
    
    
    	return 0;
    }
    
    /* Try to defer sending, if possible, in order to minimize the amount
     * of TSO splitting we do.  View it as a kind of TSO Nagle test.
     *
     * This algorithm is from John Heffner.
     */
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb)
    
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	const struct inet_connection_sock *icsk = inet_csk(sk);
    
    	u32 send_win, cong_win, limit, in_flight;
    
    	int win_divisor;
    
    	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
    
    		goto send_now;
    
    	if (icsk->icsk_ca_state != TCP_CA_Open)
    
    		goto send_now;
    
    	/* Defer for less than two clock ticks. */
    
    Ilpo Järvinen's avatar
    Ilpo Järvinen committed
    	if (tp->tso_deferred &&
    
    	    (((u32)jiffies << 1) >> 1) - (tp->tso_deferred >> 1) > 1)
    
    		goto send_now;
    
    	in_flight = tcp_packets_in_flight(tp);
    
    
    	BUG_ON(tcp_skb_pcount(skb) <= 1 || (tp->snd_cwnd <= in_flight));
    
    	send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
    
    
    	/* From in_flight test above, we know that cwnd > in_flight.  */
    	cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache;
    
    	limit = min(send_win, cong_win);
    
    
    	/* If a full-sized TSO skb can be sent, do it. */
    
    	if (limit >= min_t(unsigned int, sk->sk_gso_max_size,
    
    			   tp->xmit_size_goal_segs * tp->mss_cache))
    
    		goto send_now;
    
    	/* Middle in queue won't get any more data, full sendable already? */
    	if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len))
    		goto send_now;
    
    
    	win_divisor = ACCESS_ONCE(sysctl_tcp_tso_win_divisor);
    	if (win_divisor) {
    
    		u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache);
    
    		/* If at least some fraction of a window is available,
    		 * just use it.
    		 */
    
    		chunk /= win_divisor;
    
    		if (limit >= chunk)
    
    			goto send_now;
    
    	} else {
    		/* Different approach, try not to defer past a single
    		 * ACK.  Receiver should ACK every other full sized
    		 * frame, so if we have space for more than 3 frames
    		 * then send now.
    		 */
    
    		if (limit > tcp_max_tso_deferred_mss(tp) * tp->mss_cache)
    
    			goto send_now;
    
    	/* Ok, it looks like it is advisable to defer.
    	 * Do not rearm the timer if already set to not break TCP ACK clocking.
    	 */
    	if (!tp->tso_deferred)
    		tp->tso_deferred = 1 | (jiffies << 1);
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	return true;
    
    
    send_now:
    	tp->tso_deferred = 0;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    	return false;
    
    John Heffner's avatar
    John Heffner committed
    /* Create a new MTU probe if we are ready.
    
     * MTU probe is regularly attempting to increase the path MTU by
     * deliberately sending larger packets.  This discovers routing
     * changes resulting in larger path MTUs.
     *
    
    John Heffner's avatar
    John Heffner committed
     * Returns 0 if we should wait to probe (no cwnd available),
     *         1 if a probe was sent,
    
    John Heffner's avatar
    John Heffner committed
    static int tcp_mtu_probe(struct sock *sk)
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    	struct inet_connection_sock *icsk = inet_csk(sk);
    	struct sk_buff *skb, *nskb, *next;
    	int len;
    	int probe_size;
    
    John Heffner's avatar
    John Heffner committed
    	int copy;
    	int mss_now;
    
    	/* Not currently probing/verifying,
    	 * not in recovery,
    	 * have enough cwnd, and
    	 * not SACKing (the variable headers throw things off) */
    	if (!icsk->icsk_mtup.enabled ||
    	    icsk->icsk_mtup.probe_size ||
    	    inet_csk(sk)->icsk_ca_state != TCP_CA_Open ||
    	    tp->snd_cwnd < 11 ||
    
    	    tp->rx_opt.num_sacks || tp->rx_opt.dsack)
    
    John Heffner's avatar
    John Heffner committed
    		return -1;
    
    	/* Very simple search strategy: just double the MSS. */
    
    	mss_now = tcp_current_mss(sk);
    
    	probe_size = 2 * tp->mss_cache;
    
    	size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache;
    
    John Heffner's avatar
    John Heffner committed
    	if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high)) {
    		/* TODO: set timer for probe_converge_event */
    		return -1;
    	}
    
    	/* Have enough data in the send queue to probe? */
    
    	if (tp->write_seq - tp->snd_nxt < size_needed)
    
    John Heffner's avatar
    John Heffner committed
    		return -1;
    
    
    	if (tp->snd_wnd < size_needed)
    		return -1;
    
    	if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp)))
    
    John Heffner's avatar
    John Heffner committed
    
    
    	/* Do we need to wait to drain cwnd? With none in flight, don't stall */
    	if (tcp_packets_in_flight(tp) + 2 > tp->snd_cwnd) {
    		if (!tcp_packets_in_flight(tp))
    
    John Heffner's avatar
    John Heffner committed
    			return -1;
    		else
    			return 0;
    	}
    
    	/* We're allowed to probe.  Build it now. */
    	if ((nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC)) == NULL)
    		return -1;
    
    	sk->sk_wmem_queued += nskb->truesize;
    	sk_mem_charge(sk, nskb->truesize);
    
    John Heffner's avatar
    John Heffner committed
    
    
    	skb = tcp_send_head(sk);
    
    John Heffner's avatar
    John Heffner committed
    
    	TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq;
    	TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size;
    
    	TCP_SKB_CB(nskb)->tcp_flags = TCPHDR_ACK;
    
    John Heffner's avatar
    John Heffner committed
    	TCP_SKB_CB(nskb)->sacked = 0;
    	nskb->csum = 0;
    
    	nskb->ip_summed = skb->ip_summed;
    
    John Heffner's avatar
    John Heffner committed
    
    
    	tcp_insert_write_queue_before(nskb, skb, sk);
    
    
    John Heffner's avatar
    John Heffner committed
    	len = 0;
    
    	tcp_for_write_queue_from_safe(skb, next, sk) {
    
    John Heffner's avatar
    John Heffner committed
    		copy = min_t(int, skb->len, probe_size - len);
    		if (nskb->ip_summed)
    			skb_copy_bits(skb, 0, skb_put(nskb, copy), copy);
    		else
    			nskb->csum = skb_copy_and_csum_bits(skb, 0,
    
    							    skb_put(nskb, copy),
    							    copy, nskb->csum);
    
    John Heffner's avatar
    John Heffner committed
    
    		if (skb->len <= copy) {
    			/* We've eaten all the data from this skb.
    			 * Throw it away. */
    
    			TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
    
    			tcp_unlink_write_queue(skb, sk);
    
    			sk_wmem_free_skb(sk, skb);
    
    John Heffner's avatar
    John Heffner committed
    		} else {
    
    			TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags &
    
    Changli Gao's avatar
    Changli Gao committed
    						   ~(TCPHDR_FIN|TCPHDR_PSH);
    
    John Heffner's avatar
    John Heffner committed
    			if (!skb_shinfo(skb)->nr_frags) {
    				skb_pull(skb, copy);
    
    				if (skb->ip_summed != CHECKSUM_PARTIAL)
    
    					skb->csum = csum_partial(skb->data,
    								 skb->len, 0);
    
    John Heffner's avatar
    John Heffner committed
    			} else {
    				__pskb_trim_head(skb, copy);
    				tcp_set_skb_tso_segs(sk, skb, mss_now);
    			}
    			TCP_SKB_CB(skb)->seq += copy;
    		}
    
    		len += copy;
    
    John Heffner's avatar
    John Heffner committed
    	}
    	tcp_init_tso_segs(sk, nskb, nskb->len);
    
    	/* We're ready to send.  If this fails, the probe will
    	 * be resegmented into mss-sized pieces by tcp_write_xmit(). */
    	TCP_SKB_CB(nskb)->when = tcp_time_stamp;
    	if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) {
    		/* Decrement cwnd here because we are sending
    
    		 * effectively two packets. */
    
    John Heffner's avatar
    John Heffner committed
    		tp->snd_cwnd--;
    
    		tcp_event_new_data_sent(sk, nskb);
    
    John Heffner's avatar
    John Heffner committed
    
    		icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len);
    
    		tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq;
    		tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /* This routine writes packets to the network.  It advances the
     * send_head.  This happens as incoming acks open up the remote
     * window for us.
     *
    
     * LARGESEND note: !tcp_urg_mode is overkill, only frames between
     * snd_up-64k-mss .. snd_up cannot be large. However, taking into
     * account rare use of URG, this is not a big flaw.
     *
    
     * Send at most one packet when push_one > 0. Temporarily ignore
     * cwnd limit to force at most one packet out when push_one == 2.
    
    
    Eric Dumazet's avatar
    Eric Dumazet committed
     * Returns true, if no segments are in flight and we have queued segments,
     * but cannot send anything now because of SWS or another problem.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
    			   int push_one, gfp_t gfp)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct tcp_sock *tp = tcp_sk(sk);
    
    	struct sk_buff *skb;
    
    	unsigned int tso_segs, sent_pkts;
    	int cwnd_quota;
    
    John Heffner's avatar
    John Heffner committed
    	int result;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    John Heffner's avatar
    John Heffner committed
    
    
    	if (!push_one) {
    		/* Do MTU probing. */
    		result = tcp_mtu_probe(sk);
    		if (!result) {
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    			return false;
    
    		} else if (result > 0) {
    			sent_pkts = 1;
    		}
    
    	while ((skb = tcp_send_head(sk))) {
    
    		tso_segs = tcp_init_tso_segs(sk, skb, mss_now);
    
    		BUG_ON(!tso_segs);
    
    		if (unlikely(tp->repair) && tp->repair_queue == TCP_SEND_QUEUE)
    			goto repair; /* Skip network transmission */
    
    
    		cwnd_quota = tcp_cwnd_test(tp, skb);
    
    		if (!cwnd_quota) {
    			if (push_one == 2)
    				/* Force out a loss probe pkt. */
    				cwnd_quota = 1;
    			else
    				break;
    		}
    
    
    		if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now)))
    			break;
    
    
    		if (tso_segs == 1) {
    			if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
    						     (tcp_skb_is_last(sk, skb) ?
    						      nonagle : TCP_NAGLE_PUSH))))
    				break;
    		} else {
    
    			if (!push_one && tcp_tso_should_defer(sk, skb))
    
    		/* TCP Small Queues :
    		 * Control number of packets in qdisc/devices to two packets / or ~1 ms.
    		 * This allows for :
    		 *  - better RTT estimation and ACK scheduling
    		 *  - faster recovery
    		 *  - high rates
    
    		 * Alas, some drivers / subsystems require a fair amount
    		 * of queued bytes to ensure line rate.
    		 * One example is wifi aggregation (802.11 AMPDU)
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		 */
    
    		limit = max_t(unsigned int, sysctl_tcp_limit_output_bytes,
    			      sk->sk_pacing_rate >> 10);
    
    
    		if (atomic_read(&sk->sk_wmem_alloc) > limit) {
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    			set_bit(TSQ_THROTTLED, &tp->tsq_flags);
    
    			/* It is possible TX completion already happened
    			 * before we set TSQ_THROTTLED, so we must
    			 * test again the condition.
    			 * We abuse smp_mb__after_clear_bit() because
    			 * there is no smp_mb__after_set_bit() yet
    			 */
    			smp_mb__after_clear_bit();
    			if (atomic_read(&sk->sk_wmem_alloc) > limit)
    				break;
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		}
    
    		if (tso_segs > 1 && !tcp_urg_mode(tp))
    
    			limit = tcp_mss_split_point(sk, skb, mss_now,
    
    						    min_t(unsigned int,
    							  cwnd_quota,
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    							  sk->sk_gso_max_segs),
    						    nonagle);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		    unlikely(tso_fragment(sk, skb, limit, mss_now, gfp)))
    
    		TCP_SKB_CB(skb)->when = tcp_time_stamp;
    
    		if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp)))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		/* Advance the send_head.  This one is sent out.
    		 * This call will increment packets_out.
    		 */
    
    		tcp_event_new_data_sent(sk, skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		tcp_minshall_update(tp, mss_now, skb);
    
    		sent_pkts += tcp_skb_pcount(skb);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    		if (tcp_in_cwnd_reduction(sk))
    			tp->prr_out += sent_pkts;
    
    
    		/* Send one loss probe per tail loss episode. */
    		if (push_one != 2)
    			tcp_schedule_loss_probe(sk);
    
    Eric Dumazet's avatar
    Eric Dumazet committed
    		return false;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	return (push_one == 2) || (!tp->packets_out && tcp_send_head(sk));
    }
    
    bool tcp_schedule_loss_probe(struct sock *sk)
    {
    	struct inet_connection_sock *icsk = inet_csk(sk);
    	struct tcp_sock *tp = tcp_sk(sk);
    	u32 timeout, tlp_time_stamp, rto_time_stamp;
    
    	u32 rtt = usecs_to_jiffies(tp->srtt_us >> 3);
    
    
    	if (WARN_ON(icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS))
    		return false;
    	/* No consecutive loss probes. */
    	if (WARN_ON(icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)) {
    		tcp_rearm_rto(sk);
    		return false;
    	}
    	/* Don't do any loss probe on a Fast Open connection before 3WHS
    	 * finishes.
    	 */