tcp: accept socket after TCP_DEFER_ACCEPT period
[safe/jmp/linux-2.6] / net / ipv4 / inet_connection_sock.c
index 045e799..4351ca2 100644 (file)
@@ -30,20 +30,22 @@ EXPORT_SYMBOL(inet_csk_timer_bug_msg);
 #endif
 
 /*
- * This array holds the first and last local port number.
+ * This struct holds the first and last local port number.
  */
-int sysctl_local_port_range[2] = { 32768, 61000 };
-DEFINE_SEQLOCK(sysctl_port_range_lock);
+struct local_ports sysctl_local_ports __read_mostly = {
+       .lock = SEQLOCK_UNLOCKED,
+       .range = { 32768, 61000 },
+};
 
 void inet_get_local_port_range(int *low, int *high)
 {
        unsigned seq;
        do {
-               seq = read_seqbegin(&sysctl_port_range_lock);
+               seq = read_seqbegin(&sysctl_local_ports.lock);
 
-               *low = sysctl_local_port_range[0];
-               *high = sysctl_local_port_range[1];
-       } while (read_seqretry(&sysctl_port_range_lock, seq));
+               *low = sysctl_local_ports.range[0];
+               *high = sysctl_local_ports.range[1];
+       } while (read_seqretry(&sysctl_local_ports.lock, seq));
 }
 EXPORT_SYMBOL(inet_get_local_port_range);
 
@@ -91,23 +93,40 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
        struct inet_bind_hashbucket *head;
        struct hlist_node *node;
        struct inet_bind_bucket *tb;
-       int ret;
+       int ret, attempts = 5;
        struct net *net = sock_net(sk);
+       int smallest_size = -1, smallest_rover;
 
        local_bh_disable();
        if (!snum) {
                int remaining, rover, low, high;
 
+again:
                inet_get_local_port_range(&low, &high);
                remaining = (high - low) + 1;
-               rover = net_random() % remaining + low;
+               smallest_rover = rover = net_random() % remaining + low;
 
+               smallest_size = -1;
                do {
-                       head = &hashinfo->bhash[inet_bhashfn(rover, hashinfo->bhash_size)];
+                       head = &hashinfo->bhash[inet_bhashfn(net, rover,
+                                       hashinfo->bhash_size)];
                        spin_lock(&head->lock);
                        inet_bind_bucket_for_each(tb, node, &head->chain)
-                               if (tb->ib_net == net && tb->port == rover)
+                               if (ib_net(tb) == net && tb->port == rover) {
+                                       if (tb->fastreuse > 0 &&
+                                           sk->sk_reuse &&
+                                           sk->sk_state != TCP_LISTEN &&
+                                           (tb->num_owners < smallest_size || smallest_size == -1)) {
+                                               smallest_size = tb->num_owners;
+                                               smallest_rover = rover;
+                                               if (atomic_read(&hashinfo->bsockets) > (high - low) + 1) {
+                                                       spin_unlock(&head->lock);
+                                                       snum = smallest_rover;
+                                                       goto have_snum;
+                                               }
+                                       }
                                        goto next;
+                               }
                        break;
                next:
                        spin_unlock(&head->lock);
@@ -122,18 +141,24 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
                 * the top level, not from the 'break;' statement.
                 */
                ret = 1;
-               if (remaining <= 0)
+               if (remaining <= 0) {
+                       if (smallest_size != -1) {
+                               snum = smallest_rover;
+                               goto have_snum;
+                       }
                        goto fail;
-
+               }
                /* OK, here is the one we will use.  HEAD is
                 * non-NULL and we hold it's mutex.
                 */
                snum = rover;
        } else {
-               head = &hashinfo->bhash[inet_bhashfn(snum, hashinfo->bhash_size)];
+have_snum:
+               head = &hashinfo->bhash[inet_bhashfn(net, snum,
+                               hashinfo->bhash_size)];
                spin_lock(&head->lock);
                inet_bind_bucket_for_each(tb, node, &head->chain)
-                       if (tb->ib_net == net && tb->port == snum)
+                       if (ib_net(tb) == net && tb->port == snum)
                                goto tb_found;
        }
        tb = NULL;
@@ -141,12 +166,19 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
 tb_found:
        if (!hlist_empty(&tb->owners)) {
                if (tb->fastreuse > 0 &&
-                   sk->sk_reuse && sk->sk_state != TCP_LISTEN) {
+                   sk->sk_reuse && sk->sk_state != TCP_LISTEN &&
+                   smallest_size == -1) {
                        goto success;
                } else {
                        ret = 1;
-                       if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb))
+                       if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb)) {
+                               if (sk->sk_reuse && sk->sk_state != TCP_LISTEN &&
+                                   smallest_size != -1 && --attempts >= 0) {
+                                       spin_unlock(&head->lock);
+                                       goto again;
+                               }
                                goto fail_unlock;
+                       }
                }
        }
 tb_not_found:
@@ -165,7 +197,7 @@ tb_not_found:
 success:
        if (!inet_csk(sk)->icsk_bind_hash)
                inet_bind_hash(sk, tb, snum);
-       BUG_TRAP(inet_csk(sk)->icsk_bind_hash == tb);
+       WARN_ON(inet_csk(sk)->icsk_bind_hash != tb);
        ret = 0;
 
 fail_unlock:
@@ -258,7 +290,7 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
        }
 
        newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk);
-       BUG_TRAP(newsk->sk_state != TCP_SYN_RECV);
+       WARN_ON(newsk->sk_state == TCP_SYN_RECV);
 out:
        release_sock(sk);
        return newsk;
@@ -319,7 +351,7 @@ void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
 
 EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
 
-struct dst_entryinet_csk_route_req(struct sock *sk,
+struct dst_entry *inet_csk_route_req(struct sock *sk,
                                     const struct request_sock *req)
 {
        struct rtable *rt;
@@ -333,21 +365,24 @@ struct dst_entry* inet_csk_route_req(struct sock *sk,
                                        .saddr = ireq->loc_addr,
                                        .tos = RT_CONN_FLAGS(sk) } },
                            .proto = sk->sk_protocol,
+                           .flags = inet_sk_flowi_flags(sk),
                            .uli_u = { .ports =
                                       { .sport = inet_sk(sk)->sport,
                                         .dport = ireq->rmt_port } } };
+       struct net *net = sock_net(sk);
 
        security_req_classify_flow(req, &fl);
-       if (ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 0)) {
-               IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
-               return NULL;
-       }
-       if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) {
-               ip_rt_put(rt);
-               IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
-               return NULL;
-       }
+       if (ip_route_output_flow(net, &rt, &fl, sk, 0))
+               goto no_route;
+       if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway)
+               goto route_err;
        return &rt->u.dst;
+
+route_err:
+       ip_rt_put(rt);
+no_route:
+       IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
+       return NULL;
 }
 
 EXPORT_SYMBOL_GPL(inet_csk_route_req);
@@ -383,7 +418,7 @@ struct request_sock *inet_csk_search_req(const struct sock *sk,
                    ireq->rmt_addr == raddr &&
                    ireq->loc_addr == laddr &&
                    AF_INET_FAMILY(req->rsk_ops->family)) {
-                       BUG_TRAP(!req->sk);
+                       WARN_ON(req->sk);
                        *prevp = prev;
                        break;
                }
@@ -466,9 +501,9 @@ void inet_csk_reqsk_queue_prune(struct sock *parent,
                reqp=&lopt->syn_table[i];
                while ((req = *reqp) != NULL) {
                        if (time_after_eq(now, req->expires)) {
-                               if ((req->retrans < (inet_rsk(req)->acked ? max_retries : thresh)) &&
-                                   (inet_rsk(req)->acked ||
-                                    !req->rsk_ops->rtx_syn_ack(parent, req))) {
+                               if ((req->retrans < thresh ||
+                                    (inet_rsk(req)->acked && req->retrans < max_retries))
+                                   && !req->rsk_ops->rtx_syn_ack(parent, req)) {
                                        unsigned long timeo;
 
                                        if (req->retrans++ == 0)
@@ -512,6 +547,8 @@ struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
                newicsk->icsk_bind_hash = NULL;
 
                inet_sk(newsk)->dport = inet_rsk(req)->rmt_port;
+               inet_sk(newsk)->num = ntohs(inet_rsk(req)->loc_port);
+               inet_sk(newsk)->sport = inet_rsk(req)->loc_port;
                newsk->sk_write_space = sk_stream_write_space;
 
                newicsk->icsk_retransmits = 0;
@@ -536,14 +573,14 @@ EXPORT_SYMBOL_GPL(inet_csk_clone);
  */
 void inet_csk_destroy_sock(struct sock *sk)
 {
-       BUG_TRAP(sk->sk_state == TCP_CLOSE);
-       BUG_TRAP(sock_flag(sk, SOCK_DEAD));
+       WARN_ON(sk->sk_state != TCP_CLOSE);
+       WARN_ON(!sock_flag(sk, SOCK_DEAD));
 
        /* It cannot be in hash table! */
-       BUG_TRAP(sk_unhashed(sk));
+       WARN_ON(!sk_unhashed(sk));
 
        /* If it has not 0 inet_sk(sk)->num, it must be bound */
-       BUG_TRAP(!inet_sk(sk)->num || inet_csk(sk)->icsk_bind_hash);
+       WARN_ON(inet_sk(sk)->num && !inet_csk(sk)->icsk_bind_hash);
 
        sk->sk_prot->destroy(sk);
 
@@ -553,7 +590,7 @@ void inet_csk_destroy_sock(struct sock *sk)
 
        sk_refcnt_debug_release(sk);
 
-       atomic_dec(sk->sk_prot->orphan_count);
+       percpu_counter_dec(sk->sk_prot->orphan_count);
        sock_put(sk);
 }
 
@@ -626,14 +663,14 @@ void inet_csk_listen_stop(struct sock *sk)
 
                local_bh_disable();
                bh_lock_sock(child);
-               BUG_TRAP(!sock_owned_by_user(child));
+               WARN_ON(sock_owned_by_user(child));
                sock_hold(child);
 
                sk->sk_prot->disconnect(child, O_NONBLOCK);
 
                sock_orphan(child);
 
-               atomic_inc(sk->sk_prot->orphan_count);
+               percpu_counter_inc(sk->sk_prot->orphan_count);
 
                inet_csk_destroy_sock(child);
 
@@ -644,7 +681,7 @@ void inet_csk_listen_stop(struct sock *sk)
                sk_acceptq_removed(sk);
                __reqsk_free(req);
        }
-       BUG_TRAP(!sk->sk_ack_backlog);
+       WARN_ON(sk->sk_ack_backlog);
 }
 
 EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
@@ -677,7 +714,7 @@ int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
 EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
 
 int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
-                              char __user *optval, int optlen)
+                              char __user *optval, unsigned int optlen)
 {
        const struct inet_connection_sock *icsk = inet_csk(sk);