tcp_options.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /* cfg_group_tcp description (for the config framework)*/
  47. static cfg_def_t tcp_cfg_def[] = {
  48. /* name , type |input type| chg type, min, max, fixup, proc. cbk
  49. description */
  50. { "connect_timeout", CFG_VAR_INT | CFG_ATOMIC, -1,
  51. TICKS_TO_S(MAX_TCP_CON_LIFETIME), fix_connect_to, 0,
  52. "used only in non-async mode, in seconds"},
  53. { "send_timeout", CFG_VAR_INT | CFG_ATOMIC, -1,
  54. MAX_TCP_CON_LIFETIME, fix_send_to, 0,
  55. "in seconds"},
  56. { "connection_lifetime", CFG_VAR_INT | CFG_ATOMIC, -1,
  57. MAX_TCP_CON_LIFETIME, fix_con_lt, 0,
  58. "connection lifetime (in seconds)"},
  59. { "max_connections", CFG_VAR_INT | CFG_ATOMIC, 0, (1U<<31)-1,
  60. fix_max_conns, 0,
  61. "maximum connection number, soft limit"},
  62. { "no_connect", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  63. "if set only accept new connections, never actively open new ones"},
  64. { "fd_cache", CFG_VAR_INT | CFG_READONLY, 0, 1, 0, 0,
  65. "file descriptor cache for tcp_send"},
  66. /* tcp async options */
  67. { "async", CFG_VAR_INT | CFG_READONLY, 0, 1, 0, 0,
  68. "async mode for writes and connects"},
  69. { "connect_wait", CFG_VAR_INT | CFG_READONLY, 0, 1, 0, 0,
  70. "parallel simultaneous connects to the same dst. (0) or one connect"},
  71. { "conn_wq_max", CFG_VAR_INT | CFG_ATOMIC, 0, 1024*1024, 0, 0,
  72. "maximum bytes queued for write per connection (depends on async)"},
  73. { "wq_max", CFG_VAR_INT | CFG_ATOMIC, 0, 1<<30, 0, 0,
  74. "maximum bytes queued for write allowed globally (depends on async)"},
  75. /* see also send_timeout above */
  76. /* tcp socket options */
  77. { "defer_accept", CFG_VAR_INT | CFG_READONLY, 0, 3600, 0, 0,
  78. "0/1 on linux, seconds on freebsd (see docs)"},
  79. { "delayed_ack", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  80. "initial ack will be delayed and sent with the first data segment"},
  81. { "syncnt", CFG_VAR_INT | CFG_ATOMIC, 0, 1024, 0, 0,
  82. "number of syn retransmissions before aborting a connect (0=not set)"},
  83. { "linger2", CFG_VAR_INT | CFG_ATOMIC, 0, 3600, 0, 0,
  84. "lifetime of orphaned sockets in FIN_WAIT2 state in s (0=not set)"},
  85. { "keepalive", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  86. "enables/disables keepalives for tcp"},
  87. { "keepidle", CFG_VAR_INT | CFG_ATOMIC, 0, 24*3600, 0, 0,
  88. "time before sending a keepalive if the connection is idle (linux)"},
  89. { "keepintvl", CFG_VAR_INT | CFG_ATOMIC, 0, 24*3600, 0, 0,
  90. "time interval between keepalive probes on failure (linux)"},
  91. { "keepcnt", CFG_VAR_INT | CFG_ATOMIC, 0, 1<<10, 0, 0,
  92. "number of failed keepalives before dropping the connection (linux)"},
  93. /* other options */
  94. { "crlf_ping", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  95. "enable responding to CRLF SIP-level keepalives "},
  96. { "accept_aliases", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  97. "turn on/off tcp aliases (see tcp_accept_aliases) "},
  98. { "alias_flags", CFG_VAR_INT | CFG_ATOMIC, 0, 2, 0, 0,
  99. "flags used for adding new aliases (FORCE_ADD:1 , REPLACE:2) "},
  100. { "new_conn_alias_flags", CFG_VAR_INT | CFG_ATOMIC, 0, 2, 0, 0,
  101. "flags for the def. aliases for a new conn. (FORCE_ADD:1, REPLACE:2 "},
  102. { "accept_no_cl", CFG_VAR_INT | CFG_ATOMIC, 0, 1, 0, 0,
  103. "accept TCP messges without Content-Lenght "},
  104. /* internal and/or "fixed" versions of some vars
  105. (not supposed to be writeable, read will provide only debugging value*/
  106. { "rd_buf_size", CFG_VAR_INT | CFG_ATOMIC, 512, 16777216, 0, 0,
  107. "internal read buffer size (should be > max. expected datagram)"},
  108. { "wq_blk_size", CFG_VAR_INT | CFG_ATOMIC, 1, 65535, 0, 0,
  109. "internal async write block size (debugging use only for now)"},
  110. {0, 0, 0, 0, 0, 0, 0}
  111. };
  112. void* tcp_cfg; /* tcp config handle */
  113. /* set defaults */
  114. void init_tcp_options()
  115. {
  116. tcp_default_cfg.connect_timeout_s=DEFAULT_TCP_CONNECT_TIMEOUT;
  117. tcp_default_cfg.send_timeout=S_TO_TICKS(DEFAULT_TCP_SEND_TIMEOUT);
  118. tcp_default_cfg.con_lifetime=S_TO_TICKS(DEFAULT_TCP_CONNECTION_LIFETIME_S);
  119. #ifdef USE_TCP
  120. tcp_default_cfg.max_connections=tcp_max_connections;
  121. #else /*USE_TCP*/
  122. tcp_default_cfg.max_connections=0;
  123. #endif /*USE_TCP*/
  124. #ifdef TCP_ASYNC
  125. tcp_default_cfg.async=1;
  126. tcp_default_cfg.tcpconn_wq_max=32*1024; /* 32 k */
  127. tcp_default_cfg.tcp_wq_max=10*1024*1024; /* 10 MB */
  128. #ifdef TCP_CONNECT_WAIT
  129. tcp_default_cfg.tcp_connect_wait=1;
  130. #endif /* TCP_CONNECT_WAIT */
  131. #endif /* TCP_ASYNC */
  132. #ifdef TCP_FD_CACHE
  133. tcp_default_cfg.fd_cache=1;
  134. #endif
  135. #ifdef HAVE_SO_KEEPALIVE
  136. tcp_default_cfg.keepalive=1;
  137. #endif
  138. /*
  139. #if defined HAVE_TCP_DEFER_ACCEPT || defined HAVE_TCP_ACCEPT_FILTER
  140. tcp_default_cfg.defer_accept=1;
  141. #endif
  142. */
  143. #ifdef HAVE_TCP_QUICKACK
  144. tcp_default_cfg.delayed_ack=1;
  145. #endif
  146. tcp_default_cfg.crlf_ping=1;
  147. tcp_default_cfg.accept_aliases=0; /* don't accept aliases by default */
  148. /* flags used for adding new aliases */
  149. tcp_default_cfg.alias_flags=TCP_ALIAS_FORCE_ADD;
  150. /* flags used for adding the default aliases of a new tcp connection */
  151. tcp_default_cfg.new_conn_alias_flags=TCP_ALIAS_REPLACE;
  152. tcp_default_cfg.rd_buf_size=DEFAULT_TCP_BUF_SIZE;
  153. tcp_default_cfg.wq_blk_size=DEFAULT_TCP_WBUF_SIZE;
  154. }
  155. #define W_OPT_NC(option) \
  156. if (tcp_default_cfg.option){\
  157. WARN("tcp_options: tcp_" #option \
  158. " cannot be enabled (recompile needed)\n"); \
  159. tcp_default_cfg.option=0; \
  160. }
  161. #define W_OPT_NS(option) \
  162. if (tcp_default_cfg.option){\
  163. WARN("tcp_options: tcp_" #option \
  164. " cannot be enabled (no OS support)\n"); \
  165. tcp_default_cfg.option=0; \
  166. }
  167. /* if *to<0 to=default_val, else if to>max_val to=max_val */
  168. static void fix_timeout(char* name, int* to, int default_val, unsigned max_val)
  169. {
  170. if (*to < 0) *to=default_val;
  171. else if ((unsigned)*to > max_val){
  172. WARN("%s: timeout too big (%u), the maximum value is %u\n",
  173. name, *to, max_val);
  174. *to=max_val;
  175. }
  176. }
  177. static int fix_connect_to(void* cfg_h, str* gname, str* name, void** val)
  178. {
  179. int v;
  180. v=(int)(long)*val;
  181. fix_timeout("tcp_connect_timeout", &v, DEFAULT_TCP_CONNECT_TIMEOUT,
  182. TICKS_TO_S(MAX_TCP_CON_LIFETIME));
  183. *val=(void*)(long)v;
  184. return 0;
  185. }
  186. static int fix_send_to(void* cfg_h, str* gname, str* name, void** val)
  187. {
  188. int v;
  189. v=S_TO_TICKS((int)(long)*val);
  190. fix_timeout("tcp_send_timeout", &v, S_TO_TICKS(DEFAULT_TCP_SEND_TIMEOUT),
  191. MAX_TCP_CON_LIFETIME);
  192. *val=(void*)(long)v;
  193. return 0;
  194. }
  195. static int fix_con_lt(void* cfg_h, str* gname, str* name, void** val)
  196. {
  197. int v;
  198. v=S_TO_TICKS((int)(long)*val);
  199. fix_timeout("tcp_connection_lifetime", &v,
  200. MAX_TCP_CON_LIFETIME, MAX_TCP_CON_LIFETIME);
  201. *val=(void*)(long)v;
  202. return 0;
  203. }
  204. static int fix_max_conns(void* cfg_h, str* gname, str* name, void** val)
  205. {
  206. int v;
  207. v=(int)(long)*val;
  208. #ifdef USE_TCP
  209. if (v>tcp_max_connections){
  210. INFO("cannot override hard tcp_max_connections limit, please"
  211. " restart and increase tcp_max_connections in the cfg.\n");
  212. v=tcp_max_connections;
  213. }
  214. #else /* USE_TCP */
  215. if (v){
  216. ERR("TCP support disabled at compile-time, tcp_max_connection is"
  217. " hardwired to 0.\n");
  218. v=0;
  219. }
  220. #endif /*USE_TCP */
  221. *val=(void*)(long)v;
  222. return 0;
  223. }
  224. /** fix *val according to the cfg entry "name".
  225. * (*val must be integer)
  226. * 1. check if *val is between name min..max and if not change it to
  227. * the corresp. value
  228. * 2. call fixup callback if defined in the cfg
  229. * @return 0 on success
  230. */
  231. static int tcp_cfg_def_fix(char* name, int* val)
  232. {
  233. cfg_def_t* c;
  234. str s;
  235. for (c=&tcp_cfg_def[0]; c->name; c++){
  236. if (strcmp(name, c->name)==0){
  237. /* found */
  238. if ((c->type & CFG_VAR_INT) && (c->min || c->max)){
  239. if (*val < c->min) *val=c->min;
  240. else if (*val > c->max) *val=c->max;
  241. if (c->on_change_cb){
  242. s.s=c->name;
  243. s.len=strlen(s.s);
  244. return c->on_change_cb(&tcp_default_cfg, NULL, &s, (void*)val);
  245. }
  246. }
  247. return 0;
  248. }
  249. }
  250. WARN("tcp config option \"%s\" not found\n", name);
  251. return -1; /* not found */
  252. }
  253. /* checks & warns if some tcp_option cannot be enabled */
  254. void tcp_options_check()
  255. {
  256. #ifndef TCP_FD_CACHE
  257. W_OPT_NC(defer_accept);
  258. #endif
  259. #ifndef TCP_ASYNC
  260. W_OPT_NC(async);
  261. W_OPT_NC(tcpconn_wq_max);
  262. W_OPT_NC(tcp_wq_max);
  263. #endif /* TCP_ASYNC */
  264. #ifndef TCP_CONNECT_WAIT
  265. W_OPT_NC(tcp_connect_wait);
  266. #endif /* TCP_CONNECT_WAIT */
  267. if (tcp_default_cfg.tcp_connect_wait && !tcp_default_cfg.async){
  268. tcp_default_cfg.tcp_connect_wait=0;
  269. }
  270. #if ! defined HAVE_TCP_DEFER_ACCEPT && ! defined HAVE_TCP_ACCEPT_FILTER
  271. W_OPT_NS(defer_accept);
  272. #endif
  273. #ifndef HAVE_TCP_SYNCNT
  274. W_OPT_NS(syncnt);
  275. #endif
  276. #ifndef HAVE_TCP_LINGER2
  277. W_OPT_NS(linger2);
  278. #endif
  279. #ifndef HAVE_TCP_KEEPINTVL
  280. W_OPT_NS(keepintvl);
  281. #endif
  282. #ifndef HAVE_TCP_KEEPIDLE
  283. W_OPT_NS(keepidle);
  284. #endif
  285. #ifndef HAVE_TCP_KEEPCNT
  286. W_OPT_NS(keepcnt);
  287. #endif
  288. if (tcp_default_cfg.keepintvl || tcp_default_cfg.keepidle ||
  289. tcp_default_cfg.keepcnt){
  290. tcp_default_cfg.keepalive=1; /* force on */
  291. }
  292. #ifndef HAVE_SO_KEEPALIVE
  293. W_OPT_NS(keepalive);
  294. #endif
  295. #ifndef HAVE_TCP_QUICKACK
  296. W_OPT_NS(delayed_ack);
  297. #endif
  298. /* fix various timeouts */
  299. fix_timeout("tcp_connect_timeout", &tcp_default_cfg.connect_timeout_s,
  300. DEFAULT_TCP_CONNECT_TIMEOUT,
  301. TICKS_TO_S(MAX_TCP_CON_LIFETIME));
  302. fix_timeout("tcp_send_timeout", &tcp_default_cfg.send_timeout,
  303. S_TO_TICKS(DEFAULT_TCP_SEND_TIMEOUT),
  304. MAX_TCP_CON_LIFETIME);
  305. fix_timeout("tcp_connection_lifetime", &tcp_default_cfg.con_lifetime,
  306. MAX_TCP_CON_LIFETIME, MAX_TCP_CON_LIFETIME);
  307. #ifdef USE_TCP
  308. tcp_default_cfg.max_connections=tcp_max_connections;
  309. #else /* USE_TCP */
  310. tcp_default_cfg.max_connections=0;
  311. #endif /* USE_TCP */
  312. tcp_cfg_def_fix("rd_buf_size", (int*)&tcp_default_cfg.rd_buf_size);
  313. tcp_cfg_def_fix("wq_blk_size", (int*)&tcp_default_cfg.wq_blk_size);
  314. }
  315. void tcp_options_get(struct cfg_group_tcp* t)
  316. {
  317. *t=*(struct cfg_group_tcp*)tcp_cfg;
  318. }
  319. /** register tcp config into the configuration framework.
  320. * @return 0 on succes, -1 on error*/
  321. int tcp_register_cfg()
  322. {
  323. if (cfg_declare("tcp", tcp_cfg_def, &tcp_default_cfg, cfg_sizeof(tcp),
  324. &tcp_cfg))
  325. return -1;
  326. if (tcp_cfg==0){
  327. BUG("null tcp cfg");
  328. return -1;
  329. }
  330. return 0;
  331. }