blob: 45aed75cb571220da14d2924ec9674ac06f4d99b [file] [log] [blame]
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -07001/*
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>
Eric Dumazet72a3eff2006-11-16 02:30:37 -080018#include <linux/vmalloc.h>
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070019
20#include <net/request_sock.h>
21
David S. Millere52c1f12005-06-18 22:49:40 -070022/*
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).
Eric Dumazet72a3eff2006-11-16 02:30:37 -080033 * Note : Dont forget somaxconn that may limit backlog too.
David S. Millere52c1f12005-06-18 22:49:40 -070034 */
35int sysctl_max_syn_backlog = 256;
David S. Millere52c1f12005-06-18 22:49:40 -070036
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070037int reqsk_queue_alloc(struct request_sock_queue *queue,
Eric Dumazet72a3eff2006-11-16 02:30:37 -080038 unsigned int nr_table_entries)
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070039{
Eric Dumazet72a3eff2006-11-16 02:30:37 -080040 size_t lopt_size = sizeof(struct listen_sock);
41 struct listen_sock *lopt;
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070042
Eric Dumazet72a3eff2006-11-16 02:30:37 -080043 nr_table_entries = min_t(u32, nr_table_entries, sysctl_max_syn_backlog);
44 nr_table_entries = max_t(u32, nr_table_entries, 8);
45 nr_table_entries = roundup_pow_of_two(nr_table_entries + 1);
46 lopt_size += nr_table_entries * sizeof(struct request_sock *);
47 if (lopt_size > PAGE_SIZE)
48 lopt = __vmalloc(lopt_size,
49 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
50 PAGE_KERNEL);
51 else
52 lopt = kzalloc(lopt_size, GFP_KERNEL);
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070053 if (lopt == NULL)
54 return -ENOMEM;
55
Eric Dumazet72a3eff2006-11-16 02:30:37 -080056 for (lopt->max_qlen_log = 3;
57 (1 << lopt->max_qlen_log) < nr_table_entries;
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070058 lopt->max_qlen_log++);
59
60 get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
61 rwlock_init(&queue->syn_wait_lock);
Norbert Kiesel3eb48012006-03-26 17:39:55 -080062 queue->rskq_accept_head = NULL;
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -070063 lopt->nr_table_entries = nr_table_entries;
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070064
65 write_lock_bh(&queue->syn_wait_lock);
66 queue->listen_opt = lopt;
67 write_unlock_bh(&queue->syn_wait_lock);
68
69 return 0;
70}
71
72EXPORT_SYMBOL(reqsk_queue_alloc);
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -070073
Pavel Emelyanovdab6ba32007-11-15 02:57:06 -080074void __reqsk_queue_destroy(struct request_sock_queue *queue)
75{
76 struct listen_sock *lopt;
77 size_t lopt_size;
78
79 /*
80 * this is an error recovery path only
81 * no locking needed and the lopt is not NULL
82 */
83
84 lopt = queue->listen_opt;
85 lopt_size = sizeof(struct listen_sock) +
86 lopt->nr_table_entries * sizeof(struct request_sock *);
87
88 if (lopt_size > PAGE_SIZE)
89 vfree(lopt);
90 else
91 kfree(lopt);
92}
93
94EXPORT_SYMBOL(__reqsk_queue_destroy);
95
96static inline struct listen_sock *reqsk_queue_yank_listen_sk(
97 struct request_sock_queue *queue)
98{
99 struct listen_sock *lopt;
100
101 write_lock_bh(&queue->syn_wait_lock);
102 lopt = queue->listen_opt;
103 queue->listen_opt = NULL;
104 write_unlock_bh(&queue->syn_wait_lock);
105
106 return lopt;
107}
108
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -0700109void reqsk_queue_destroy(struct request_sock_queue *queue)
110{
111 /* make all the listen_opt local to us */
112 struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800113 size_t lopt_size = sizeof(struct listen_sock) +
114 lopt->nr_table_entries * sizeof(struct request_sock *);
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -0700115
116 if (lopt->qlen != 0) {
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800117 unsigned int i;
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -0700118
119 for (i = 0; i < lopt->nr_table_entries; i++) {
120 struct request_sock *req;
121
122 while ((req = lopt->syn_table[i]) != NULL) {
123 lopt->syn_table[i] = req->dl_next;
124 lopt->qlen--;
125 reqsk_free(req);
126 }
127 }
128 }
129
130 BUG_TRAP(lopt->qlen == 0);
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800131 if (lopt_size > PAGE_SIZE)
132 vfree(lopt);
133 else
134 kfree(lopt);
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -0700135}
136
137EXPORT_SYMBOL(reqsk_queue_destroy);