blob: 79ebd75fbe4dec5c57ddd34c4501c5858b04d8fb [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>
18
19#include <net/request_sock.h>
20
David S. Millere52c1f12005-06-18 22:49:40 -070021/*
22 * Maximum number of SYN_RECV sockets in queue per LISTEN socket.
23 * One SYN_RECV socket costs about 80bytes on a 32bit machine.
24 * It would be better to replace it with a global counter for all sockets
25 * but then some measure against one socket starving all other sockets
26 * would be needed.
27 *
28 * It was 128 by default. Experiments with real servers show, that
29 * it is absolutely not enough even at 100conn/sec. 256 cures most
30 * of problems. This value is adjusted to 128 for very small machines
31 * (<=32Mb of memory) and to 1024 on normal or better ones (>=256Mb).
32 * Further increasing requires to change hash table size.
33 */
34int sysctl_max_syn_backlog = 256;
David S. Millere52c1f12005-06-18 22:49:40 -070035
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070036int reqsk_queue_alloc(struct request_sock_queue *queue,
37 const int nr_table_entries)
38{
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -070039 const int lopt_size = sizeof(struct listen_sock) +
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070040 nr_table_entries * sizeof(struct request_sock *);
Andrew Morton77d04bd2006-04-07 14:52:59 -070041 struct listen_sock *lopt = kzalloc(lopt_size, GFP_KERNEL);
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070042
43 if (lopt == NULL)
44 return -ENOMEM;
45
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070046 for (lopt->max_qlen_log = 6;
47 (1 << lopt->max_qlen_log) < sysctl_max_syn_backlog;
48 lopt->max_qlen_log++);
49
50 get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
51 rwlock_init(&queue->syn_wait_lock);
Norbert Kiesel3eb48012006-03-26 17:39:55 -080052 queue->rskq_accept_head = NULL;
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -070053 lopt->nr_table_entries = nr_table_entries;
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070054
55 write_lock_bh(&queue->syn_wait_lock);
56 queue->listen_opt = lopt;
57 write_unlock_bh(&queue->syn_wait_lock);
58
59 return 0;
60}
61
62EXPORT_SYMBOL(reqsk_queue_alloc);
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -070063
64void reqsk_queue_destroy(struct request_sock_queue *queue)
65{
66 /* make all the listen_opt local to us */
67 struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
68
69 if (lopt->qlen != 0) {
70 int i;
71
72 for (i = 0; i < lopt->nr_table_entries; i++) {
73 struct request_sock *req;
74
75 while ((req = lopt->syn_table[i]) != NULL) {
76 lopt->syn_table[i] = req->dl_next;
77 lopt->qlen--;
78 reqsk_free(req);
79 }
80 }
81 }
82
83 BUG_TRAP(lopt->qlen == 0);
84 kfree(lopt);
85}
86
87EXPORT_SYMBOL(reqsk_queue_destroy);