Merge branch 'akpm' (patches from Andrew)
[cascardo/linux.git] / net / ipv4 / tcp.c
index ffbb218..3251fe7 100644 (file)
@@ -380,14 +380,14 @@ void tcp_init_sock(struct sock *sk)
        struct inet_connection_sock *icsk = inet_csk(sk);
        struct tcp_sock *tp = tcp_sk(sk);
 
-       __skb_queue_head_init(&tp->out_of_order_queue);
+       tp->out_of_order_queue = RB_ROOT;
        tcp_init_xmit_timers(sk);
        tcp_prequeue_init(tp);
        INIT_LIST_HEAD(&tp->tsq_node);
 
        icsk->icsk_rto = TCP_TIMEOUT_INIT;
        tp->mdev_us = jiffies_to_usecs(TCP_TIMEOUT_INIT);
-       tp->rtt_min[0].rtt = ~0U;
+       minmax_reset(&tp->rtt_min, tcp_time_stamp, ~0U);
 
        /* So many TCP implementations out there (incorrectly) count the
         * initial SYN frame in their delayed-ACK and congestion control
@@ -396,6 +396,9 @@ void tcp_init_sock(struct sock *sk)
         */
        tp->snd_cwnd = TCP_INIT_CWND;
 
+       /* There's a bubble in the pipe until at least the first ACK. */
+       tp->app_limited = ~0U;
+
        /* See draft-stevens-tcpca-spec-01 for discussion of the
         * initialization of these values.
         */
@@ -421,8 +424,6 @@ void tcp_init_sock(struct sock *sk)
        sk->sk_rcvbuf = sysctl_tcp_rmem[1];
 
        local_bh_disable();
-       if (mem_cgroup_sockets_enabled)
-               sock_update_memcg(sk);
        sk_sockets_allocated_inc(sk);
        local_bh_enable();
 }
@@ -688,8 +689,7 @@ static int tcp_splice_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
        int ret;
 
        ret = skb_splice_bits(skb, skb->sk, offset, tss->pipe,
-                             min(rd_desc->count, len), tss->flags,
-                             skb_socket_splice);
+                             min(rd_desc->count, len), tss->flags);
        if (ret > 0)
                rd_desc->count -= ret;
        return ret;
@@ -1014,23 +1014,40 @@ int tcp_sendpage(struct sock *sk, struct page *page, int offset,
                                        flags);
 
        lock_sock(sk);
+
+       tcp_rate_check_app_limited(sk);  /* is sending application-limited? */
+
        res = do_tcp_sendpages(sk, page, offset, size, flags);
        release_sock(sk);
        return res;
 }
 EXPORT_SYMBOL(tcp_sendpage);
 
-static inline int select_size(const struct sock *sk, bool sg)
+/* Do not bother using a page frag for very small frames.
+ * But use this heuristic only for the first skb in write queue.
+ *
+ * Having no payload in skb->head allows better SACK shifting
+ * in tcp_shift_skb_data(), reducing sack/rack overhead, because
+ * write queue has less skbs.
+ * Each skb can hold up to MAX_SKB_FRAGS * 32Kbytes, or ~0.5 MB.
+ * This also speeds up tso_fragment(), since it wont fallback
+ * to tcp_fragment().
+ */
+static int linear_payload_sz(bool first_skb)
+{
+       if (first_skb)
+               return SKB_WITH_OVERHEAD(2048 - MAX_TCP_HEADER);
+       return 0;
+}
+
+static int select_size(const struct sock *sk, bool sg, bool first_skb)
 {
        const struct tcp_sock *tp = tcp_sk(sk);
        int tmp = tp->mss_cache;
 
        if (sg) {
                if (sk_can_gso(sk)) {
-                       /* Small frames wont use a full page:
-                        * Payload will immediately follow tcp header.
-                        */
-                       tmp = SKB_WITH_OVERHEAD(2048 - MAX_TCP_HEADER);
+                       tmp = linear_payload_sz(first_skb);
                } else {
                        int pgbreak = SKB_MAX_HEAD(MAX_TCP_HEADER);
 
@@ -1101,6 +1118,8 @@ int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 
        timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
 
+       tcp_rate_check_app_limited(sk);  /* is sending application-limited? */
+
        /* Wait for a connection to finish. One exception is TCP Fast Open
         * (passive side) where data is allowed to be sent before a connection
         * is fully established.
@@ -1161,6 +1180,8 @@ restart:
                }
 
                if (copy <= 0 || !tcp_skb_can_collapse_to(skb)) {
+                       bool first_skb;
+
 new_segment:
                        /* Allocate new segment. If the interface is SG,
                         * allocate skb fitting to single page.
@@ -1172,10 +1193,11 @@ new_segment:
                                process_backlog = false;
                                goto restart;
                        }
+                       first_skb = skb_queue_empty(&sk->sk_write_queue);
                        skb = sk_stream_alloc_skb(sk,
-                                                 select_size(sk, sg),
+                                                 select_size(sk, sg, first_skb),
                                                  sk->sk_allocation,
-                                                 skb_queue_empty(&sk->sk_write_queue));
+                                                 first_skb);
                        if (!skb)
                                goto wait_for_memory;
 
@@ -1570,6 +1592,12 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
 }
 EXPORT_SYMBOL(tcp_read_sock);
 
+int tcp_peek_len(struct socket *sock)
+{
+       return tcp_inq(sock->sk);
+}
+EXPORT_SYMBOL(tcp_peek_len);
+
 /*
  *     This routine copies from a sock struct into the user buffer.
  *
@@ -2237,7 +2265,7 @@ int tcp_disconnect(struct sock *sk, int flags)
        tcp_clear_xmit_timers(sk);
        __skb_queue_purge(&sk->sk_receive_queue);
        tcp_write_queue_purge(sk);
-       __skb_queue_purge(&tp->out_of_order_queue);
+       skb_rbtree_purge(&tp->out_of_order_queue);
 
        inet->inet_dport = 0;
 
@@ -2681,7 +2709,7 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
 {
        const struct tcp_sock *tp = tcp_sk(sk); /* iff sk_type == SOCK_STREAM */
        const struct inet_connection_sock *icsk = inet_csk(sk);
-       u32 now = tcp_time_stamp;
+       u32 now = tcp_time_stamp, intv;
        unsigned int start;
        int notsent_bytes;
        u64 rate64;
@@ -2771,6 +2799,15 @@ void tcp_get_info(struct sock *sk, struct tcp_info *info)
        info->tcpi_min_rtt = tcp_min_rtt(tp);
        info->tcpi_data_segs_in = tp->data_segs_in;
        info->tcpi_data_segs_out = tp->data_segs_out;
+
+       info->tcpi_delivery_rate_app_limited = tp->rate_app_limited ? 1 : 0;
+       rate = READ_ONCE(tp->rate_delivered);
+       intv = READ_ONCE(tp->rate_interval_us);
+       if (rate && intv) {
+               rate64 = (u64)rate * tp->mss_cache * USEC_PER_SEC;
+               do_div(rate64, intv);
+               put_unaligned(rate64, &info->tcpi_delivery_rate);
+       }
 }
 EXPORT_SYMBOL_GPL(tcp_get_info);
 
@@ -3092,23 +3129,6 @@ struct tcp_md5sig_pool *tcp_get_md5sig_pool(void)
 }
 EXPORT_SYMBOL(tcp_get_md5sig_pool);
 
-int tcp_md5_hash_header(struct tcp_md5sig_pool *hp,
-                       const struct tcphdr *th)
-{
-       struct scatterlist sg;
-       struct tcphdr hdr;
-
-       /* We are not allowed to change tcphdr, make a local copy */
-       memcpy(&hdr, th, sizeof(hdr));
-       hdr.check = 0;
-
-       /* options aren't included in the hash */
-       sg_init_one(&sg, &hdr, sizeof(hdr));
-       ahash_request_set_crypt(hp->md5_req, &sg, NULL, sizeof(hdr));
-       return crypto_ahash_update(hp->md5_req);
-}
-EXPORT_SYMBOL(tcp_md5_hash_header);
-
 int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
                          const struct sk_buff *skb, unsigned int header_len)
 {
@@ -3255,11 +3275,12 @@ static void __init tcp_init_mem(void)
 
 void __init tcp_init(void)
 {
-       unsigned long limit;
        int max_rshare, max_wshare, cnt;
+       unsigned long limit;
        unsigned int i;
 
-       sock_skb_cb_check_size(sizeof(struct tcp_skb_cb));
+       BUILD_BUG_ON(sizeof(struct tcp_skb_cb) >
+                    FIELD_SIZEOF(struct sk_buff, cb));
 
        percpu_counter_init(&tcp_sockets_allocated, 0, GFP_KERNEL);
        percpu_counter_init(&tcp_orphan_count, 0, GFP_KERNEL);