request_sock.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <net/request_sock.h>
  18. /*
  19. * Maximum number of SYN_RECV sockets in queue per LISTEN socket.
  20. * One SYN_RECV socket costs about 80bytes on a 32bit machine.
  21. * It would be better to replace it with a global counter for all sockets
  22. * but then some measure against one socket starving all other sockets
  23. * would be needed.
  24. *
  25. * It was 128 by default. Experiments with real servers show, that
  26. * it is absolutely not enough even at 100conn/sec. 256 cures most
  27. * of problems. This value is adjusted to 128 for very small machines
  28. * (<=32Mb of memory) and to 1024 on normal or better ones (>=256Mb).
  29. * Further increasing requires to change hash table size.
  30. */
  31. int sysctl_max_syn_backlog = 256;
  32. int reqsk_queue_alloc(struct request_sock_queue *queue,
  33. const int nr_table_entries)
  34. {
  35. const int lopt_size = sizeof(struct listen_sock) +
  36. nr_table_entries * sizeof(struct request_sock *);
  37. struct listen_sock *lopt = kzalloc(lopt_size, GFP_KERNEL);
  38. if (lopt == NULL)
  39. return -ENOMEM;
  40. for (lopt->max_qlen_log = 6;
  41. (1 << lopt->max_qlen_log) < sysctl_max_syn_backlog;
  42. lopt->max_qlen_log++);
  43. get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
  44. rwlock_init(&queue->syn_wait_lock);
  45. queue->rskq_accept_head = NULL;
  46. lopt->nr_table_entries = nr_table_entries;
  47. write_lock_bh(&queue->syn_wait_lock);
  48. queue->listen_opt = lopt;
  49. write_unlock_bh(&queue->syn_wait_lock);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(reqsk_queue_alloc);
  53. void reqsk_queue_destroy(struct request_sock_queue *queue)
  54. {
  55. /* make all the listen_opt local to us */
  56. struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
  57. if (lopt->qlen != 0) {
  58. int i;
  59. for (i = 0; i < lopt->nr_table_entries; i++) {
  60. struct request_sock *req;
  61. while ((req = lopt->syn_table[i]) != NULL) {
  62. lopt->syn_table[i] = req->dl_next;
  63. lopt->qlen--;
  64. reqsk_free(req);
  65. }
  66. }
  67. }
  68. BUG_TRAP(lopt->qlen == 0);
  69. kfree(lopt);
  70. }
  71. EXPORT_SYMBOL(reqsk_queue_destroy);