Arnaldo Carvalho de Melo | 0e87506 | 2005-06-18 22:47:59 -0700 | [diff] [blame] | 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> |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 18 | #include <linux/vmalloc.h> |
Arnaldo Carvalho de Melo | 0e87506 | 2005-06-18 22:47:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include <net/request_sock.h> |
| 21 | |
David S. Miller | e52c1f1 | 2005-06-18 22:49:40 -0700 | [diff] [blame] | 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). |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 33 | * Note : Dont forget somaxconn that may limit backlog too. |
David S. Miller | e52c1f1 | 2005-06-18 22:49:40 -0700 | [diff] [blame] | 34 | */ |
| 35 | int sysctl_max_syn_backlog = 256; |
David S. Miller | 493f377 | 2010-12-02 12:14:29 -0800 | [diff] [blame] | 36 | EXPORT_SYMBOL(sysctl_max_syn_backlog); |
David S. Miller | e52c1f1 | 2005-06-18 22:49:40 -0700 | [diff] [blame] | 37 | |
Arnaldo Carvalho de Melo | 0e87506 | 2005-06-18 22:47:59 -0700 | [diff] [blame] | 38 | int reqsk_queue_alloc(struct request_sock_queue *queue, |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 39 | unsigned int nr_table_entries) |
Arnaldo Carvalho de Melo | 0e87506 | 2005-06-18 22:47:59 -0700 | [diff] [blame] | 40 | { |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 41 | size_t lopt_size = sizeof(struct listen_sock); |
| 42 | struct listen_sock *lopt; |
Arnaldo Carvalho de Melo | 0e87506 | 2005-06-18 22:47:59 -0700 | [diff] [blame] | 43 | |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 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) |
Eric Dumazet | 7a1c8e5 | 2010-11-20 07:46:35 +0000 | [diff] [blame] | 49 | lopt = vzalloc(lopt_size); |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 50 | else |
| 51 | lopt = kzalloc(lopt_size, GFP_KERNEL); |
Arnaldo Carvalho de Melo | 0e87506 | 2005-06-18 22:47:59 -0700 | [diff] [blame] | 52 | if (lopt == NULL) |
| 53 | return -ENOMEM; |
| 54 | |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 55 | for (lopt->max_qlen_log = 3; |
| 56 | (1 << lopt->max_qlen_log) < nr_table_entries; |
Arnaldo Carvalho de Melo | 0e87506 | 2005-06-18 22:47:59 -0700 | [diff] [blame] | 57 | lopt->max_qlen_log++); |
| 58 | |
| 59 | get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd)); |
| 60 | rwlock_init(&queue->syn_wait_lock); |
Norbert Kiesel | 3eb4801 | 2006-03-26 17:39:55 -0800 | [diff] [blame] | 61 | queue->rskq_accept_head = NULL; |
Arnaldo Carvalho de Melo | 83e3609 | 2005-08-09 19:33:31 -0700 | [diff] [blame] | 62 | lopt->nr_table_entries = nr_table_entries; |
Arnaldo Carvalho de Melo | 0e87506 | 2005-06-18 22:47:59 -0700 | [diff] [blame] | 63 | |
| 64 | write_lock_bh(&queue->syn_wait_lock); |
| 65 | queue->listen_opt = lopt; |
| 66 | write_unlock_bh(&queue->syn_wait_lock); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Pavel Emelyanov | dab6ba3 | 2007-11-15 02:57:06 -0800 | [diff] [blame] | 71 | void __reqsk_queue_destroy(struct request_sock_queue *queue) |
| 72 | { |
| 73 | struct listen_sock *lopt; |
| 74 | size_t lopt_size; |
| 75 | |
| 76 | /* |
| 77 | * this is an error recovery path only |
| 78 | * no locking needed and the lopt is not NULL |
| 79 | */ |
| 80 | |
| 81 | lopt = queue->listen_opt; |
| 82 | lopt_size = sizeof(struct listen_sock) + |
| 83 | lopt->nr_table_entries * sizeof(struct request_sock *); |
| 84 | |
| 85 | if (lopt_size > PAGE_SIZE) |
| 86 | vfree(lopt); |
| 87 | else |
| 88 | kfree(lopt); |
| 89 | } |
| 90 | |
Pavel Emelyanov | dab6ba3 | 2007-11-15 02:57:06 -0800 | [diff] [blame] | 91 | static inline struct listen_sock *reqsk_queue_yank_listen_sk( |
| 92 | struct request_sock_queue *queue) |
| 93 | { |
| 94 | struct listen_sock *lopt; |
| 95 | |
| 96 | write_lock_bh(&queue->syn_wait_lock); |
| 97 | lopt = queue->listen_opt; |
| 98 | queue->listen_opt = NULL; |
| 99 | write_unlock_bh(&queue->syn_wait_lock); |
| 100 | |
| 101 | return lopt; |
| 102 | } |
| 103 | |
Arnaldo Carvalho de Melo | 83e3609 | 2005-08-09 19:33:31 -0700 | [diff] [blame] | 104 | void reqsk_queue_destroy(struct request_sock_queue *queue) |
| 105 | { |
| 106 | /* make all the listen_opt local to us */ |
| 107 | struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue); |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 108 | size_t lopt_size = sizeof(struct listen_sock) + |
| 109 | lopt->nr_table_entries * sizeof(struct request_sock *); |
Arnaldo Carvalho de Melo | 83e3609 | 2005-08-09 19:33:31 -0700 | [diff] [blame] | 110 | |
| 111 | if (lopt->qlen != 0) { |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 112 | unsigned int i; |
Arnaldo Carvalho de Melo | 83e3609 | 2005-08-09 19:33:31 -0700 | [diff] [blame] | 113 | |
| 114 | for (i = 0; i < lopt->nr_table_entries; i++) { |
| 115 | struct request_sock *req; |
| 116 | |
| 117 | while ((req = lopt->syn_table[i]) != NULL) { |
| 118 | lopt->syn_table[i] = req->dl_next; |
| 119 | lopt->qlen--; |
| 120 | reqsk_free(req); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 125 | WARN_ON(lopt->qlen != 0); |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 126 | if (lopt_size > PAGE_SIZE) |
| 127 | vfree(lopt); |
| 128 | else |
| 129 | kfree(lopt); |
Arnaldo Carvalho de Melo | 83e3609 | 2005-08-09 19:33:31 -0700 | [diff] [blame] | 130 | } |
| 131 | |