request_sock.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * The minimum value of it is 128. Experiments with real servers show that
  27. * it is absolutely not enough even at 100conn/sec. 256 cures most
  28. * of problems.
  29. * This value is adjusted to 128 for low memory machines,
  30. * and it will increase in proportion to the memory of machine.
  31. * Note : Dont forget somaxconn that may limit backlog too.
  32. */
  33. int sysctl_max_syn_backlog = 256;
  34. EXPORT_SYMBOL(sysctl_max_syn_backlog);
  35. int reqsk_queue_alloc(struct request_sock_queue *queue,
  36. unsigned int nr_table_entries)
  37. {
  38. size_t lopt_size = sizeof(struct listen_sock);
  39. struct listen_sock *lopt;
  40. nr_table_entries = min_t(u32, nr_table_entries, sysctl_max_syn_backlog);
  41. nr_table_entries = max_t(u32, nr_table_entries, 8);
  42. nr_table_entries = roundup_pow_of_two(nr_table_entries + 1);
  43. lopt_size += nr_table_entries * sizeof(struct request_sock *);
  44. if (lopt_size > PAGE_SIZE)
  45. lopt = vzalloc(lopt_size);
  46. else
  47. lopt = kzalloc(lopt_size, GFP_KERNEL);
  48. if (lopt == NULL)
  49. return -ENOMEM;
  50. for (lopt->max_qlen_log = 3;
  51. (1 << lopt->max_qlen_log) < nr_table_entries;
  52. lopt->max_qlen_log++);
  53. get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
  54. rwlock_init(&queue->syn_wait_lock);
  55. queue->rskq_accept_head = NULL;
  56. lopt->nr_table_entries = nr_table_entries;
  57. write_lock_bh(&queue->syn_wait_lock);
  58. queue->listen_opt = lopt;
  59. write_unlock_bh(&queue->syn_wait_lock);
  60. return 0;
  61. }
  62. void __reqsk_queue_destroy(struct request_sock_queue *queue)
  63. {
  64. struct listen_sock *lopt;
  65. size_t lopt_size;
  66. /*
  67. * this is an error recovery path only
  68. * no locking needed and the lopt is not NULL
  69. */
  70. lopt = queue->listen_opt;
  71. lopt_size = sizeof(struct listen_sock) +
  72. lopt->nr_table_entries * sizeof(struct request_sock *);
  73. if (lopt_size > PAGE_SIZE)
  74. vfree(lopt);
  75. else
  76. kfree(lopt);
  77. }
  78. static inline struct listen_sock *reqsk_queue_yank_listen_sk(
  79. struct request_sock_queue *queue)
  80. {
  81. struct listen_sock *lopt;
  82. write_lock_bh(&queue->syn_wait_lock);
  83. lopt = queue->listen_opt;
  84. queue->listen_opt = NULL;
  85. write_unlock_bh(&queue->syn_wait_lock);
  86. return lopt;
  87. }
  88. void reqsk_queue_destroy(struct request_sock_queue *queue)
  89. {
  90. /* make all the listen_opt local to us */
  91. struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
  92. size_t lopt_size = sizeof(struct listen_sock) +
  93. lopt->nr_table_entries * sizeof(struct request_sock *);
  94. if (lopt->qlen != 0) {
  95. unsigned int i;
  96. for (i = 0; i < lopt->nr_table_entries; i++) {
  97. struct request_sock *req;
  98. while ((req = lopt->syn_table[i]) != NULL) {
  99. lopt->syn_table[i] = req->dl_next;
  100. lopt->qlen--;
  101. reqsk_free(req);
  102. }
  103. }
  104. }
  105. WARN_ON(lopt->qlen != 0);
  106. if (lopt_size > PAGE_SIZE)
  107. vfree(lopt);
  108. else
  109. kfree(lopt);
  110. }