request_sock.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include <linux/module.h>
  14. #include <linux/random.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/vmalloc.h>
  18. #include <net/request_sock.h>
  19. /*
  20. * Maximum number of SYN_RECV sockets in queue per LISTEN socket.
  21. * One SYN_RECV socket costs about 80bytes on a 32bit machine.
  22. * It would be better to replace it with a global counter for all sockets
  23. * but then some measure against one socket starving all other sockets
  24. * would be needed.
  25. *
  26. * It was 128 by default. Experiments with real servers show, that
  27. * it is absolutely not enough even at 100conn/sec. 256 cures most
  28. * of problems. This value is adjusted to 128 for very small machines
  29. * (<=32Mb of memory) and to 1024 on normal or better ones (>=256Mb).
  30. * Note : Dont forget somaxconn that may limit backlog too.
  31. */
  32. int sysctl_max_syn_backlog = 256;
  33. EXPORT_SYMBOL(sysctl_max_syn_backlog);
  34. int reqsk_queue_alloc(struct request_sock_queue *queue,
  35. unsigned int nr_table_entries)
  36. {
  37. size_t lopt_size = sizeof(struct listen_sock);
  38. struct listen_sock *lopt;
  39. nr_table_entries = min_t(u32, nr_table_entries, sysctl_max_syn_backlog);
  40. nr_table_entries = max_t(u32, nr_table_entries, 8);
  41. nr_table_entries = roundup_pow_of_two(nr_table_entries + 1);
  42. lopt_size += nr_table_entries * sizeof(struct request_sock *);
  43. if (lopt_size > PAGE_SIZE)
  44. lopt = vzalloc(lopt_size);
  45. else
  46. lopt = kzalloc(lopt_size, GFP_KERNEL);
  47. if (lopt == NULL)
  48. return -ENOMEM;
  49. for (lopt->max_qlen_log = 3;
  50. (1 << lopt->max_qlen_log) < nr_table_entries;
  51. lopt->max_qlen_log++);
  52. get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
  53. rwlock_init(&queue->syn_wait_lock);
  54. queue->rskq_accept_head = NULL;
  55. lopt->nr_table_entries = nr_table_entries;
  56. write_lock_bh(&queue->syn_wait_lock);
  57. queue->listen_opt = lopt;
  58. write_unlock_bh(&queue->syn_wait_lock);
  59. return 0;
  60. }
  61. void __reqsk_queue_destroy(struct request_sock_queue *queue)
  62. {
  63. struct listen_sock *lopt;
  64. size_t lopt_size;
  65. /*
  66. * this is an error recovery path only
  67. * no locking needed and the lopt is not NULL
  68. */
  69. lopt = queue->listen_opt;
  70. lopt_size = sizeof(struct listen_sock) +
  71. lopt->nr_table_entries * sizeof(struct request_sock *);
  72. if (lopt_size > PAGE_SIZE)
  73. vfree(lopt);
  74. else
  75. kfree(lopt);
  76. }
  77. static inline struct listen_sock *reqsk_queue_yank_listen_sk(
  78. struct request_sock_queue *queue)
  79. {
  80. struct listen_sock *lopt;
  81. write_lock_bh(&queue->syn_wait_lock);
  82. lopt = queue->listen_opt;
  83. queue->listen_opt = NULL;
  84. write_unlock_bh(&queue->syn_wait_lock);
  85. return lopt;
  86. }
  87. void reqsk_queue_destroy(struct request_sock_queue *queue)
  88. {
  89. /* make all the listen_opt local to us */
  90. struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
  91. size_t lopt_size = sizeof(struct listen_sock) +
  92. lopt->nr_table_entries * sizeof(struct request_sock *);
  93. if (lopt->qlen != 0) {
  94. unsigned int i;
  95. for (i = 0; i < lopt->nr_table_entries; i++) {
  96. struct request_sock *req;
  97. while ((req = lopt->syn_table[i]) != NULL) {
  98. lopt->syn_table[i] = req->dl_next;
  99. lopt->qlen--;
  100. reqsk_free(req);
  101. }
  102. }
  103. }
  104. WARN_ON(lopt->qlen != 0);
  105. if (lopt_size > PAGE_SIZE)
  106. vfree(lopt);
  107. else
  108. kfree(lopt);
  109. }