request_sock.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. EXPORT_SYMBOL(sysctl_max_syn_backlog);
  33. int reqsk_queue_alloc(struct request_sock_queue *queue,
  34. const int nr_table_entries)
  35. {
  36. const int lopt_size = sizeof(struct listen_sock) +
  37. nr_table_entries * sizeof(struct request_sock *);
  38. struct listen_sock *lopt = kmalloc(lopt_size, GFP_KERNEL);
  39. if (lopt == NULL)
  40. return -ENOMEM;
  41. memset(lopt, 0, lopt_size);
  42. for (lopt->max_qlen_log = 6;
  43. (1 << lopt->max_qlen_log) < sysctl_max_syn_backlog;
  44. lopt->max_qlen_log++);
  45. get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
  46. rwlock_init(&queue->syn_wait_lock);
  47. queue->rskq_accept_head = queue->rskq_accept_head = NULL;
  48. write_lock_bh(&queue->syn_wait_lock);
  49. queue->listen_opt = lopt;
  50. write_unlock_bh(&queue->syn_wait_lock);
  51. return 0;
  52. }
  53. EXPORT_SYMBOL(reqsk_queue_alloc);