tcp_tunning.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. Kamailio TCP Tunning/monitoring for lots of open connections
  2. ==============================================================
  3. 0. Introduction
  4. ----------------
  5. This document describes very briefly various settings that should improve
  6. Kamailio TCP performance for sites handling a lot of TCP traffic (> 1000 open
  7. connections or very high connection/disconnection rates).
  8. For now it deals only with Linux specific optimizations.
  9. 1. Useful Linux kernel settings
  10. -------------------------------
  11. 1.1 Connection rate/pending connections: by default the connection rate is
  12. too small
  13. net.core.somaxconn - limit of the listen() backlog, default 128
  14. net.ipv4.tcp_max_syn_backlog - default 1024 or 128
  15. net.ipv4.tcp_timestamps - default on., should be on (along with
  16. tcp_tw_recycle and timestamp supporting
  17. peers allows for fast connections rates)
  18. 1.2 Connection in close_wait
  19. Connection should stay as little as possible
  20. in close_wait to quickly free the fd/resources for new connections attempts
  21. WARNING: this could break normal TCP use, use it only if you know what you are
  22. doing
  23. net.ipv4.tcp_max_tw_buckets - maximum number of timewait sockets
  24. (the default seems to be ok)
  25. net.ipv4.tcp_tw_recycle - enables fast time wait sockets recycling (default
  26. off), should be enabled if you have lots of short
  27. lived connections
  28. WARNING: see the above warning
  29. net.ipv4.tcp_tw_reuse - allows reusing of time-wait sockets (default off)
  30. WARNING: see above
  31. net.ipv4.tcp_syncookies - default off, in this case it's probably better to
  32. keep it off
  33. 1.3 Port range
  34. net.ipv4.ip_local_port_range - should be increased (e.g. 4096-65534)
  35. 1.4 Open file descriptors
  36. fs.file-max - maximum number of fds that will be allocated
  37. (you probably need to increase it, default
  38. depends on installed memory)
  39. 1.5 Buffers size
  40. When dealing with large data, check the values for network sockets buffers
  41. to be sufficient, among them:
  42. net.core.rmem_max
  43. net.core.wmem_max
  44. net.core.rmem_default
  45. net.core.wmem_default
  46. net.ipv4.tcp_mem
  47. net.ipv4.tcp_rmem
  48. net.ipv4.tcp_wmem
  49. Note: sysctl -w net.ipv4.route.flush=1 - ensure that immediately subsequent
  50. connections use the values
  51. 1.6 Other sysctl that might affect tcp connection rate or the maximum number
  52. of open connections
  53. fs.epoll.max_user_instances - maximum number of devices - per user (2.6.27.8+)
  54. fs.epoll.max_user_watches - maximum number of "watched" fds - per user
  55. (2.6.27.8+)
  56. net.ipv4.tcp_max_orphans - might be worth a look if things go wrong
  57. net.core.netdev_max_backlog - maximum device backlog
  58. Related applications
  59. --------------------
  60. ifconfig <dev> txqueuelen <val> - set device transmission queue len
  61. iptables - remove the ip_conntrack module (it limits the maximum tcp
  62. connections, adds extra overhead (slow)). It's probably better to remove
  63. all the iptables modules.
  64. 2. Monitoring (values to watch for)
  65. -----------------------------------
  66. 2.1 File descriptors
  67. fs.dentry-state - format: nr. dentries, nr. unused, age_limit, want_pages
  68. fs.file-nr - format: allocated, unused, max (==fs.file-max)
  69. fs.inode-state - format: nr. allocated, nr. free, preshrink
  70. 2.2 TCP
  71. /proc/net/netstat - the TW, TWRecycled, TWKilled, PAWPassive, PAWActive,
  72. PASWEstab fields
  73. ( cat /proc/net/netstat |cut -d" " -f12-17 ;
  74. cat /proc/net/sockstat)
  75. /proc/net/sockstat
  76. 3. Kamailio settings
  77. ----------------------
  78. - Don't forget to increase tcp_max_connections and the amount of shared memory
  79. - You should increase the number of Kamailio "tcp_children" processes (-N no)
  80. As a rule of thumb, (maximum simultaneous connections)/2000 should be ok
  81. - You might have to decrease TCP_BUF_SIZE to a smaller value (e.g 8K)
  82. - You might want to increase PKG_MEM_POOL_SIZE (for large queues)
  83. - You might need to increase the maximum open fds limit before starting Kamailio
  84. (e.g. ulimit -n 1000000)