]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/core/request_sock.c
tcp: Add timewait recycling bits to ipv6 connect code.
[mv-sheeva.git] / net / core / request_sock.c
1 /*
2  * NET          Generic infrastructure for Network protocols.
3  *
4  * Authors:     Arnaldo Carvalho de Melo <acme@conectiva.com.br>
5  *
6  *              From code originally in include/net/tcp.h
7  *
8  *              This program is free software; you can redistribute it and/or
9  *              modify it under the terms of the GNU General Public License
10  *              as published by the Free Software Foundation; either version
11  *              2 of the License, or (at your option) any later version.
12  */
13
14 #include <linux/module.h>
15 #include <linux/random.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/vmalloc.h>
19
20 #include <net/request_sock.h>
21
22 /*
23  * Maximum number of SYN_RECV sockets in queue per LISTEN socket.
24  * One SYN_RECV socket costs about 80bytes on a 32bit machine.
25  * It would be better to replace it with a global counter for all sockets
26  * but then some measure against one socket starving all other sockets
27  * would be needed.
28  *
29  * It was 128 by default. Experiments with real servers show, that
30  * it is absolutely not enough even at 100conn/sec. 256 cures most
31  * of problems. This value is adjusted to 128 for very small machines
32  * (<=32Mb of memory) and to 1024 on normal or better ones (>=256Mb).
33  * Note : Dont forget somaxconn that may limit backlog too.
34  */
35 int sysctl_max_syn_backlog = 256;
36 EXPORT_SYMBOL(sysctl_max_syn_backlog);
37
38 int reqsk_queue_alloc(struct request_sock_queue *queue,
39                       unsigned int nr_table_entries)
40 {
41         size_t lopt_size = sizeof(struct listen_sock);
42         struct listen_sock *lopt;
43
44         nr_table_entries = min_t(u32, nr_table_entries, sysctl_max_syn_backlog);
45         nr_table_entries = max_t(u32, nr_table_entries, 8);
46         nr_table_entries = roundup_pow_of_two(nr_table_entries + 1);
47         lopt_size += nr_table_entries * sizeof(struct request_sock *);
48         if (lopt_size > PAGE_SIZE)
49                 lopt = __vmalloc(lopt_size,
50                         GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
51                         PAGE_KERNEL);
52         else
53                 lopt = kzalloc(lopt_size, GFP_KERNEL);
54         if (lopt == NULL)
55                 return -ENOMEM;
56
57         for (lopt->max_qlen_log = 3;
58              (1 << lopt->max_qlen_log) < nr_table_entries;
59              lopt->max_qlen_log++);
60
61         get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
62         rwlock_init(&queue->syn_wait_lock);
63         queue->rskq_accept_head = NULL;
64         lopt->nr_table_entries = nr_table_entries;
65
66         write_lock_bh(&queue->syn_wait_lock);
67         queue->listen_opt = lopt;
68         write_unlock_bh(&queue->syn_wait_lock);
69
70         return 0;
71 }
72
73 void __reqsk_queue_destroy(struct request_sock_queue *queue)
74 {
75         struct listen_sock *lopt;
76         size_t lopt_size;
77
78         /*
79          * this is an error recovery path only
80          * no locking needed and the lopt is not NULL
81          */
82
83         lopt = queue->listen_opt;
84         lopt_size = sizeof(struct listen_sock) +
85                 lopt->nr_table_entries * sizeof(struct request_sock *);
86
87         if (lopt_size > PAGE_SIZE)
88                 vfree(lopt);
89         else
90                 kfree(lopt);
91 }
92
93 static inline struct listen_sock *reqsk_queue_yank_listen_sk(
94                 struct request_sock_queue *queue)
95 {
96         struct listen_sock *lopt;
97
98         write_lock_bh(&queue->syn_wait_lock);
99         lopt = queue->listen_opt;
100         queue->listen_opt = NULL;
101         write_unlock_bh(&queue->syn_wait_lock);
102
103         return lopt;
104 }
105
106 void reqsk_queue_destroy(struct request_sock_queue *queue)
107 {
108         /* make all the listen_opt local to us */
109         struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
110         size_t lopt_size = sizeof(struct listen_sock) +
111                 lopt->nr_table_entries * sizeof(struct request_sock *);
112
113         if (lopt->qlen != 0) {
114                 unsigned int i;
115
116                 for (i = 0; i < lopt->nr_table_entries; i++) {
117                         struct request_sock *req;
118
119                         while ((req = lopt->syn_table[i]) != NULL) {
120                                 lopt->syn_table[i] = req->dl_next;
121                                 lopt->qlen--;
122                                 reqsk_free(req);
123                         }
124                 }
125         }
126
127         WARN_ON(lopt->qlen != 0);
128         if (lopt_size > PAGE_SIZE)
129                 vfree(lopt);
130         else
131                 kfree(lopt);
132 }
133