blob: 9b570a6a33c5d8c52d777e160742dc31ec350c16 [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 *
Peter Pan(潘卫平)99b53bd2011-12-05 21:39:41 +000029 * The minimum value of it is 128. Experiments with real servers show that
David S. Millere52c1f12005-06-18 22:49:40 -070030 * it is absolutely not enough even at 100conn/sec. 256 cures most
Peter Pan(潘卫平)99b53bd2011-12-05 21:39:41 +000031 * of problems.
32 * This value is adjusted to 128 for low memory machines,
33 * and it will increase in proportion to the memory of machine.
Eric Dumazet72a3eff2006-11-16 02:30:37 -080034 * Note : Dont forget somaxconn that may limit backlog too.
David S. Millere52c1f12005-06-18 22:49:40 -070035 */
36int sysctl_max_syn_backlog = 256;
David S. Miller493f3772010-12-02 12:14:29 -080037EXPORT_SYMBOL(sysctl_max_syn_backlog);
David S. Millere52c1f12005-06-18 22:49:40 -070038
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070039int reqsk_queue_alloc(struct request_sock_queue *queue,
Eric Dumazet72a3eff2006-11-16 02:30:37 -080040 unsigned int nr_table_entries)
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070041{
Eric Dumazet72a3eff2006-11-16 02:30:37 -080042 size_t lopt_size = sizeof(struct listen_sock);
43 struct listen_sock *lopt;
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -070044
Eric Dumazet72a3eff2006-11-16 02:30:37 -080045 nr_table_entries = min_t(u32, nr_table_entries, sysctl_max_syn_backlog);
46 nr_table_entries = max_t(u32, nr_table_entries, 8);
47 nr_table_entries = roundup_pow_of_two(nr_table_entries + 1);
48 lopt_size += nr_table_entries * sizeof(struct request_sock *);
49 if (lopt_size > PAGE_SIZE)
Eric Dumazet7a1c8e52010-11-20 07:46:35 +000050 lopt = vzalloc(lopt_size);
Eric Dumazet72a3eff2006-11-16 02:30:37 -080051 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
Pavel Emelyanovdab6ba32007-11-15 02:57:06 -080072void __reqsk_queue_destroy(struct request_sock_queue *queue)
73{
74 struct listen_sock *lopt;
75 size_t lopt_size;
76
77 /*
78 * this is an error recovery path only
79 * no locking needed and the lopt is not NULL
80 */
81
82 lopt = queue->listen_opt;
83 lopt_size = sizeof(struct listen_sock) +
84 lopt->nr_table_entries * sizeof(struct request_sock *);
85
86 if (lopt_size > PAGE_SIZE)
87 vfree(lopt);
88 else
89 kfree(lopt);
90}
91
Pavel Emelyanovdab6ba32007-11-15 02:57:06 -080092static inline struct listen_sock *reqsk_queue_yank_listen_sk(
93 struct request_sock_queue *queue)
94{
95 struct listen_sock *lopt;
96
97 write_lock_bh(&queue->syn_wait_lock);
98 lopt = queue->listen_opt;
99 queue->listen_opt = NULL;
100 write_unlock_bh(&queue->syn_wait_lock);
101
102 return lopt;
103}
104
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -0700105void reqsk_queue_destroy(struct request_sock_queue *queue)
106{
107 /* make all the listen_opt local to us */
108 struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800109 size_t lopt_size = sizeof(struct listen_sock) +
110 lopt->nr_table_entries * sizeof(struct request_sock *);
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -0700111
112 if (lopt->qlen != 0) {
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800113 unsigned int i;
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -0700114
115 for (i = 0; i < lopt->nr_table_entries; i++) {
116 struct request_sock *req;
117
118 while ((req = lopt->syn_table[i]) != NULL) {
119 lopt->syn_table[i] = req->dl_next;
120 lopt->qlen--;
121 reqsk_free(req);
122 }
123 }
124 }
125
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700126 WARN_ON(lopt->qlen != 0);
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800127 if (lopt_size > PAGE_SIZE)
128 vfree(lopt);
129 else
130 kfree(lopt);
Arnaldo Carvalho de Melo83e36092005-08-09 19:33:31 -0700131}
132