tcp_options.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2007 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /*
  19. * tcp options
  20. *
  21. * History:
  22. * --------
  23. * 2007-11-28 created by andrei
  24. * 2009-03-05 use cfg framework (andrei)
  25. */
  26. /*!
  27. * \file
  28. * \brief SIP-router core ::
  29. * \ingroup core
  30. * Module: \ref core
  31. */
  32. #include "tcp_options.h"
  33. #include "dprint.h"
  34. #include "globals.h"
  35. #include "timer_ticks.h"
  36. #include "cfg/cfg.h"
  37. #include "tcp_init.h" /* DEFAULT* */
  38. /* default/initial values for tcp config options
  39. NOTE: all the options are initialized in init_tcp_options()
  40. depending on compile time defines */
  41. struct cfg_group_tcp tcp_default_cfg;
  42. static int fix_connect_to(void* cfg_h, str* gname, str* name, void** val);
  43. static int fix_send_to(void* cfg_h, str* gname, str* name, void** val);
  44. static int fix_con_lt(void* cfg_h, str* gname, str* name, void** val);
  45. static int fix_max_conns(void* cfg_h, str* gname, str* name, void** val);
  46. static int fix_max_tls_conns(void* cfg_h, str* gname, str* name, void** val);
  47. /* cfg_group_tcp description (for the config framework)*/
  48. static cfg_def_t tcp_cfg_def[] = {
  49. /* name , type |input type| chg type, min, max, fixup, proc. cbk
  50. description */
  51. { "connect_timeout", CFG_VAR_INT | CFG_ATOMIC, -1,
  52. TICKS_TO_S(MAX_TCP_CON_LIFETIME), fix_connect_to, 0,
  53. "used only in non-async mode, in seconds"},
  54. { "send_timeout", CFG_VAR_INT | CFG_ATOMIC, -1,
  55. MAX_TCP_CON_LIFETIME, fix_send_to, 0,
  56. "in seconds"},
  57. { "connection_lifetime", CFG_VAR_INT | CFG_ATOMIC, -1,
  58. MAX_TCP_CON_LIFETIME, fix_con_lt, 0,
  59. "connection lifetime (in seconds)"},
  60. { "max_connections", CFG_VAR_INT | CFG_ATOMIC, 0, (1U<<31)-1,
  61. fix_max_conns, 0,
  62. "maximum tcp connections number, soft limit"},
  63. { "max_tls_connections", CFG_VAR_INT | CFG_ATOMIC, 0, (1U<<31)-1,
  64. fix_max_tls_conns,0,
  65. "maximum tls connections number, soft limit"},
  66. { "no_connect", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  67. "if set only accept new connections, never actively open new ones"},
  68. { "fd_cache", CFG_VAR_INT | CFG_READONLY, 0, 1, 0, 0,
  69. "file descriptor cache for tcp_send"},
  70. /* tcp async options */
  71. { "async", CFG_VAR_INT | CFG_READONLY, 0, 1, 0, 0,
  72. "async mode for writes and connects"},
  73. { "connect_wait", CFG_VAR_INT | CFG_READONLY, 0, 1, 0, 0,
  74. "parallel simultaneous connects to the same dst. (0) or one connect"},
  75. { "conn_wq_max", CFG_VAR_INT | CFG_ATOMIC, 0, 1024*1024, 0, 0,
  76. "maximum bytes queued for write per connection (depends on async)"},
  77. { "wq_max", CFG_VAR_INT | CFG_ATOMIC, 0, 1<<30, 0, 0,
  78. "maximum bytes queued for write allowed globally (depends on async)"},
  79. /* see also send_timeout above */
  80. /* tcp socket options */
  81. { "defer_accept", CFG_VAR_INT | CFG_READONLY, 0, 3600, 0, 0,
  82. "0/1 on linux, seconds on freebsd (see docs)"},
  83. { "delayed_ack", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  84. "initial ack will be delayed and sent with the first data segment"},
  85. { "syncnt", CFG_VAR_INT | CFG_ATOMIC, 0, 1024, 0, 0,
  86. "number of syn retransmissions before aborting a connect (0=not set)"},
  87. { "linger2", CFG_VAR_INT | CFG_ATOMIC, 0, 3600, 0, 0,
  88. "lifetime of orphaned sockets in FIN_WAIT2 state in s (0=not set)"},
  89. { "keepalive", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  90. "enables/disables keepalives for tcp"},
  91. { "keepidle", CFG_VAR_INT | CFG_ATOMIC, 0, 24*3600, 0, 0,
  92. "time before sending a keepalive if the connection is idle (linux)"},
  93. { "keepintvl", CFG_VAR_INT | CFG_ATOMIC, 0, 24*3600, 0, 0,
  94. "time interval between keepalive probes on failure (linux)"},
  95. { "keepcnt", CFG_VAR_INT | CFG_ATOMIC, 0, 1<<10, 0, 0,
  96. "number of failed keepalives before dropping the connection (linux)"},
  97. /* other options */
  98. { "crlf_ping", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  99. "enable responding to CRLF SIP-level keepalives "},
  100. { "accept_aliases", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  101. "turn on/off tcp aliases (see tcp_accept_aliases) "},
  102. { "alias_flags", CFG_VAR_INT | CFG_ATOMIC, 0, 2, 0, 0,
  103. "flags used for adding new aliases (FORCE_ADD:1 , REPLACE:2) "},
  104. { "new_conn_alias_flags", CFG_VAR_INT | CFG_ATOMIC, 0, 2, 0, 0,
  105. "flags for the def. aliases for a new conn. (FORCE_ADD:1, REPLACE:2 "},
  106. { "accept_no_cl", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  107. "accept TCP messges without Content-Length "},
  108. /* internal and/or "fixed" versions of some vars
  109. (not supposed to be writeable, read will provide only debugging value*/
  110. { "rd_buf_size", CFG_VAR_INT | CFG_ATOMIC, 512, 16777216, 0, 0,
  111. "internal read buffer size (should be > max. expected datagram)"},
  112. { "wq_blk_size", CFG_VAR_INT | CFG_ATOMIC, 1, 65535, 0, 0,
  113. "internal async write block size (debugging use only for now)"},
  114. {0, 0, 0, 0, 0, 0, 0}
  115. };
  116. void* tcp_cfg; /* tcp config handle */
  117. /* set defaults */
  118. void init_tcp_options()
  119. {
  120. tcp_default_cfg.connect_timeout_s=DEFAULT_TCP_CONNECT_TIMEOUT;
  121. tcp_default_cfg.send_timeout=S_TO_TICKS(DEFAULT_TCP_SEND_TIMEOUT);
  122. tcp_default_cfg.con_lifetime=S_TO_TICKS(DEFAULT_TCP_CONNECTION_LIFETIME_S);
  123. #ifdef USE_TCP
  124. tcp_default_cfg.max_connections=tcp_max_connections;
  125. tcp_default_cfg.max_tls_connections=tls_max_connections;
  126. #else /*USE_TCP*/
  127. tcp_default_cfg.max_connections=0;
  128. tcp_default_cfg.max_tls_connections=0;
  129. #endif /*USE_TCP*/
  130. #ifdef TCP_ASYNC
  131. tcp_default_cfg.async=1;
  132. tcp_default_cfg.tcpconn_wq_max=32*1024; /* 32 k */
  133. tcp_default_cfg.tcp_wq_max=10*1024*1024; /* 10 MB */
  134. #ifdef TCP_CONNECT_WAIT
  135. tcp_default_cfg.tcp_connect_wait=1;
  136. #endif /* TCP_CONNECT_WAIT */
  137. #endif /* TCP_ASYNC */
  138. #ifdef TCP_FD_CACHE
  139. tcp_default_cfg.fd_cache=1;
  140. #endif
  141. #ifdef HAVE_SO_KEEPALIVE
  142. tcp_default_cfg.keepalive=1;
  143. #endif
  144. /*
  145. #if defined HAVE_TCP_DEFER_ACCEPT || defined HAVE_TCP_ACCEPT_FILTER
  146. tcp_default_cfg.defer_accept=1;
  147. #endif
  148. */
  149. #ifdef HAVE_TCP_QUICKACK
  150. tcp_default_cfg.delayed_ack=1;
  151. #endif
  152. tcp_default_cfg.crlf_ping=1;
  153. tcp_default_cfg.accept_aliases=0; /* don't accept aliases by default */
  154. /* flags used for adding new aliases */
  155. tcp_default_cfg.alias_flags=TCP_ALIAS_FORCE_ADD;
  156. /* flags used for adding the default aliases of a new tcp connection */
  157. tcp_default_cfg.new_conn_alias_flags=TCP_ALIAS_REPLACE;
  158. tcp_default_cfg.rd_buf_size=DEFAULT_TCP_BUF_SIZE;
  159. tcp_default_cfg.wq_blk_size=DEFAULT_TCP_WBUF_SIZE;
  160. }
  161. #define W_OPT_NC(option) \
  162. if (tcp_default_cfg.option){\
  163. WARN("tcp_options: tcp_" #option \
  164. " cannot be enabled (recompile needed)\n"); \
  165. tcp_default_cfg.option=0; \
  166. }
  167. #define W_OPT_NS(option) \
  168. if (tcp_default_cfg.option){\
  169. WARN("tcp_options: tcp_" #option \
  170. " cannot be enabled (no OS support)\n"); \
  171. tcp_default_cfg.option=0; \
  172. }
  173. /* if *to<0 to=default_val, else if to>max_val to=max_val */
  174. static void fix_timeout(char* name, int* to, int default_val, unsigned max_val)
  175. {
  176. if (*to < 0) *to=default_val;
  177. else if ((unsigned)*to > max_val){
  178. WARN("%s: timeout too big (%u), the maximum value is %u\n",
  179. name, *to, max_val);
  180. *to=max_val;
  181. }
  182. }
  183. static int fix_connect_to(void* cfg_h, str* gname, str* name, void** val)
  184. {
  185. int v;
  186. v=(int)(long)*val;
  187. fix_timeout("tcp_connect_timeout", &v, DEFAULT_TCP_CONNECT_TIMEOUT,
  188. TICKS_TO_S(MAX_TCP_CON_LIFETIME));
  189. *val=(void*)(long)v;
  190. return 0;
  191. }
  192. static int fix_send_to(void* cfg_h, str* gname, str* name, void** val)
  193. {
  194. int v;
  195. v=S_TO_TICKS((int)(long)*val);
  196. fix_timeout("tcp_send_timeout", &v, S_TO_TICKS(DEFAULT_TCP_SEND_TIMEOUT),
  197. MAX_TCP_CON_LIFETIME);
  198. *val=(void*)(long)v;
  199. return 0;
  200. }
  201. static int fix_con_lt(void* cfg_h, str* gname, str* name, void** val)
  202. {
  203. int v;
  204. v=S_TO_TICKS((int)(long)*val);
  205. fix_timeout("tcp_connection_lifetime", &v,
  206. MAX_TCP_CON_LIFETIME, MAX_TCP_CON_LIFETIME);
  207. *val=(void*)(long)v;
  208. return 0;
  209. }
  210. static int fix_max_conns(void* cfg_h, str* gname, str* name, void** val)
  211. {
  212. int v;
  213. v=(int)(long)*val;
  214. #ifdef USE_TCP
  215. if (v>tcp_max_connections){
  216. INFO("cannot override hard tcp_max_connections limit, please"
  217. " restart and increase tcp_max_connections in the cfg.\n");
  218. v=tcp_max_connections;
  219. }
  220. #else /* USE_TCP */
  221. if (v){
  222. ERR("TCP support disabled at compile-time, tcp_max_connection is"
  223. " hardwired to 0.\n");
  224. v=0;
  225. }
  226. #endif /*USE_TCP */
  227. *val=(void*)(long)v;
  228. return 0;
  229. }
  230. static int fix_max_tls_conns(void* cfg_h, str* gname, str* name, void** val)
  231. {
  232. int v;
  233. v=(int)(long)*val;
  234. #ifdef USE_TLS
  235. if (v>tls_max_connections){
  236. INFO("cannot override hard tls_max_connections limit, please"
  237. " restart and increase tls_max_connections in the cfg.\n");
  238. v=tls_max_connections;
  239. }
  240. #else /* USE_TLS */
  241. if (v){
  242. ERR("TLS support disabled at compile-time, tls_max_connection is"
  243. " hardwired to 0.\n");
  244. v=0;
  245. }
  246. #endif /*USE_TLS */
  247. *val=(void*)(long)v;
  248. return 0;
  249. }
  250. /** fix *val according to the cfg entry "name".
  251. * (*val must be integer)
  252. * 1. check if *val is between name min..max and if not change it to
  253. * the corresp. value
  254. * 2. call fixup callback if defined in the cfg
  255. * @return 0 on success
  256. */
  257. static int tcp_cfg_def_fix(char* name, int* val)
  258. {
  259. cfg_def_t* c;
  260. str s;
  261. for (c=&tcp_cfg_def[0]; c->name; c++){
  262. if (strcmp(name, c->name)==0){
  263. /* found */
  264. if ((c->type & CFG_VAR_INT) && (c->min || c->max)){
  265. if (*val < c->min) *val=c->min;
  266. else if (*val > c->max) *val=c->max;
  267. if (c->on_change_cb){
  268. s.s=c->name;
  269. s.len=strlen(s.s);
  270. return c->on_change_cb(&tcp_default_cfg, NULL, &s, (void*)val);
  271. }
  272. }
  273. return 0;
  274. }
  275. }
  276. WARN("tcp config option \"%s\" not found\n", name);
  277. return -1; /* not found */
  278. }
  279. /* checks & warns if some tcp_option cannot be enabled */
  280. void tcp_options_check()
  281. {
  282. #ifndef TCP_FD_CACHE
  283. W_OPT_NC(defer_accept);
  284. #endif
  285. #ifndef TCP_ASYNC
  286. W_OPT_NC(async);
  287. W_OPT_NC(tcpconn_wq_max);
  288. W_OPT_NC(tcp_wq_max);
  289. #endif /* TCP_ASYNC */
  290. #ifndef TCP_CONNECT_WAIT
  291. W_OPT_NC(tcp_connect_wait);
  292. #endif /* TCP_CONNECT_WAIT */
  293. if (tcp_default_cfg.tcp_connect_wait && !tcp_default_cfg.async){
  294. tcp_default_cfg.tcp_connect_wait=0;
  295. }
  296. #if ! defined HAVE_TCP_DEFER_ACCEPT && ! defined HAVE_TCP_ACCEPT_FILTER
  297. W_OPT_NS(defer_accept);
  298. #endif
  299. #ifndef HAVE_TCP_SYNCNT
  300. W_OPT_NS(syncnt);
  301. #endif
  302. #ifndef HAVE_TCP_LINGER2
  303. W_OPT_NS(linger2);
  304. #endif
  305. #ifndef HAVE_TCP_KEEPINTVL
  306. W_OPT_NS(keepintvl);
  307. #endif
  308. #ifndef HAVE_TCP_KEEPIDLE
  309. W_OPT_NS(keepidle);
  310. #endif
  311. #ifndef HAVE_TCP_KEEPCNT
  312. W_OPT_NS(keepcnt);
  313. #endif
  314. if (tcp_default_cfg.keepintvl || tcp_default_cfg.keepidle ||
  315. tcp_default_cfg.keepcnt){
  316. tcp_default_cfg.keepalive=1; /* force on */
  317. }
  318. #ifndef HAVE_SO_KEEPALIVE
  319. W_OPT_NS(keepalive);
  320. #endif
  321. #ifndef HAVE_TCP_QUICKACK
  322. W_OPT_NS(delayed_ack);
  323. #endif
  324. /* fix various timeouts */
  325. fix_timeout("tcp_connect_timeout", &tcp_default_cfg.connect_timeout_s,
  326. DEFAULT_TCP_CONNECT_TIMEOUT,
  327. TICKS_TO_S(MAX_TCP_CON_LIFETIME));
  328. fix_timeout("tcp_send_timeout", &tcp_default_cfg.send_timeout,
  329. S_TO_TICKS(DEFAULT_TCP_SEND_TIMEOUT),
  330. MAX_TCP_CON_LIFETIME);
  331. fix_timeout("tcp_connection_lifetime", &tcp_default_cfg.con_lifetime,
  332. MAX_TCP_CON_LIFETIME, MAX_TCP_CON_LIFETIME);
  333. #ifdef USE_TCP
  334. tcp_default_cfg.max_connections=tcp_max_connections;
  335. tcp_default_cfg.max_tls_connections=tls_max_connections;
  336. #else /* USE_TCP */
  337. tcp_default_cfg.max_connections=0;
  338. tcp_default_cfg.max_tls_connections=0;
  339. #endif /* USE_TCP */
  340. tcp_cfg_def_fix("rd_buf_size", (int*)&tcp_default_cfg.rd_buf_size);
  341. tcp_cfg_def_fix("wq_blk_size", (int*)&tcp_default_cfg.wq_blk_size);
  342. }
  343. void tcp_options_get(struct cfg_group_tcp* t)
  344. {
  345. *t=*(struct cfg_group_tcp*)tcp_cfg;
  346. }
  347. /** register tcp config into the configuration framework.
  348. * @return 0 on succes, -1 on error*/
  349. int tcp_register_cfg()
  350. {
  351. if (cfg_declare("tcp", tcp_cfg_def, &tcp_default_cfg, cfg_sizeof(tcp),
  352. &tcp_cfg))
  353. return -1;
  354. if (tcp_cfg==0){
  355. BUG("null tcp cfg");
  356. return -1;
  357. }
  358. return 0;
  359. }