1 /* 2 * NET Generic infrastructure for Network protocols. 3 * 4 * Authors: Arnaldo Carvalho de Melo <[email protected]> 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 37 int reqsk_queue_alloc(struct request_sock_queue *queue, 38 unsigned int nr_table_entries) 39 { 40 size_t lopt_size = sizeof(struct listen_sock); 41 struct listen_sock *lopt; 42 43 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 = vzalloc(lopt_size); 49 else 50 lopt = kzalloc(lopt_size, GFP_KERNEL); 51 if (lopt == NULL) 52 return -ENOMEM; 53 54 for (lopt->max_qlen_log = 3; 55 (1 << lopt->max_qlen_log) < nr_table_entries; 56 lopt->max_qlen_log++); 57 58 get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd)); 59 rwlock_init(&queue->syn_wait_lock); 60 queue->rskq_accept_head = NULL; 61 lopt->nr_table_entries = nr_table_entries; 62 63 write_lock_bh(&queue->syn_wait_lock); 64 queue->listen_opt = lopt; 65 write_unlock_bh(&queue->syn_wait_lock); 66 67 return 0; 68 } 69 70 void __reqsk_queue_destroy(struct request_sock_queue *queue) 71 { 72 struct listen_sock *lopt; 73 size_t lopt_size; 74 75 /* 76 * this is an error recovery path only 77 * no locking needed and the lopt is not NULL 78 */ 79 80 lopt = queue->listen_opt; 81 lopt_size = sizeof(struct listen_sock) + 82 lopt->nr_table_entries * sizeof(struct request_sock *); 83 84 if (lopt_size > PAGE_SIZE) 85 vfree(lopt); 86 else 87 kfree(lopt); 88 } 89 90 static inline struct listen_sock *reqsk_queue_yank_listen_sk( 91 struct request_sock_queue *queue) 92 { 93 struct listen_sock *lopt; 94 95 write_lock_bh(&queue->syn_wait_lock); 96 lopt = queue->listen_opt; 97 queue->listen_opt = NULL; 98 write_unlock_bh(&queue->syn_wait_lock); 99 100 return lopt; 101 } 102 103 void reqsk_queue_destroy(struct request_sock_queue *queue) 104 { 105 /* make all the listen_opt local to us */ 106 struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue); 107 size_t lopt_size = sizeof(struct listen_sock) + 108 lopt->nr_table_entries * sizeof(struct request_sock *); 109 110 if (lopt->qlen != 0) { 111 unsigned int i; 112 113 for (i = 0; i < lopt->nr_table_entries; i++) { 114 struct request_sock *req; 115 116 while ((req = lopt->syn_table[i]) != NULL) { 117 lopt->syn_table[i] = req->dl_next; 118 lopt->qlen--; 119 reqsk_free(req); 120 } 121 } 122 } 123 124 WARN_ON(lopt->qlen != 0); 125 if (lopt_size > PAGE_SIZE) 126 vfree(lopt); 127 else 128 kfree(lopt); 129 } 130 131