timer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. /*
  29. timer.c is where we implement TM timers. It has been designed
  30. for high performance using some techniques of which timer users
  31. need to be aware.
  32. One technique is "fixed-timer-length". We maintain separate
  33. timer lists, all of them include elements of the same time
  34. to fire. That allows *appending* new events to the list as
  35. opposed to inserting them by time, which is costly due to
  36. searching time spent in a mutex. The performance benefit is
  37. noticeable. The limitation is you need a new timer list for
  38. each new timer length.
  39. Another technique is the timer process slices off expired elements
  40. from the list in a mutex, but executes the timer after the mutex
  41. is left. That saves time greatly as whichever process wants to
  42. add/remove a timer, it does not have to wait until the current
  43. list is processed. However, be aware the timers may hit in a delayed
  44. manner; you have no guarantee in your process that after resetting a timer,
  45. it will no more hit. It might have been removed by timer process,
  46. and is waiting to be executed. The following example shows it:
  47. PROCESS1 TIMER PROCESS
  48. 0. timer hits, it is removed from queue and
  49. about to be executed
  50. 1. process1 decides to
  51. reset the timer
  52. 2. timer is executed now
  53. 3. if the process1 naively
  54. thinks the timer could not
  55. have been executed after
  56. resetting the timer, it is
  57. WRONG -- it was (step 2.)
  58. So be careful when writing the timer handlers. Currently defined timers
  59. don't hurt if they hit delayed, I hope at least. Retransmission timer
  60. may results in a useless retransmission -- not too bad. FR timer not too
  61. bad either as timer processing uses a REPLY mutex making it safe to other
  62. processing affecting transaction state. Wait timer not bad either -- processes
  63. putting a transaction on wait don't do anything with it anymore.
  64. Example when it does not hurt:
  65. P1 TIMER
  66. 0. RETR timer removed from list and
  67. scheduled for execution
  68. 1. 200/BYE received->
  69. reset RETR, put_on_wait
  70. 2. RETR timer executed -- too late but it does
  71. not hurt
  72. 3. WAIT handler executed
  73. The rule of thumb is don't touch data you put under a timer. Create data,
  74. put them under a timer, and let them live until they are safely destroyed from
  75. wait/delete timer. The only safe place to manipulate the data is
  76. from timer process in which delayed timers cannot hit (all timers are
  77. processed sequentially).
  78. A "bad example" -- rewriting content of retransmission buffer
  79. in an unprotected way is bad because a delayed retransmission timer might
  80. hit. Thats why our reply retransmission procedure is enclosed in
  81. a REPLY_LOCK.
  82. */
  83. /*
  84. * History:
  85. * --------
  86. * 2003-06-27 timers are not unlinked if timerlist is 0 (andrei)
  87. * 2004-02-13 t->is_invite, t->local, t->noisy_ctimer replaced;
  88. * timer_link.payload removed (bogdan)
  89. * 2005-10-03 almost completely rewritten to use the new timers (andrei)
  90. * 2005-12-12 on final response marked the rb as removed to avoid deleting
  91. * it from the timer handle; timer_allow_del() (andrei)
  92. * 2006-08-11 final_response_handler dns failover support for timeout-ed
  93. * invites (andrei)
  94. * 2006-09-28 removed the 480 on fr_inv_timeout reply: on timeout always
  95. * return a 408
  96. * set the corresponding "faked" failure route sip_msg->msg_flags
  97. * on timeout or if the branch received a reply (andrei)
  98. * 2007-03-15 TMCB_ONSEND callbacks support (andrei)
  99. * 2007-05-29 delete on transaction ref_count==0 : removed the delete timer
  100. * (andrei)
  101. * 2007-06-01 support for different retransmissions intervals per transaction;
  102. * added maximum inv. and non-inv. transaction life time (andrei)
  103. */
  104. #include "defs.h"
  105. #include "config.h"
  106. #include "h_table.h"
  107. #include "timer.h"
  108. #include "../../dprint.h"
  109. #include "lock.h"
  110. #include "t_stats.h"
  111. #include "../../hash_func.h"
  112. #include "../../dprint.h"
  113. #include "../../config.h"
  114. #include "../../parser/parser_f.h"
  115. #include "../../ut.h"
  116. #include "../../timer_ticks.h"
  117. #include "../../compiler_opt.h"
  118. #include "t_funcs.h"
  119. #include "t_reply.h"
  120. #include "t_cancel.h"
  121. #include "t_hooks.h"
  122. #ifdef USE_DNS_FAILOVER
  123. #include "t_fwd.h" /* t_send_branch */
  124. #include "../../cfg_core.h" /* cfg_get(core, core_cfg, use_dns_failover) */
  125. #endif
  126. #ifdef USE_DST_BLACKLIST
  127. #include "../../dst_blacklist.h"
  128. #endif
  129. struct msgid_var user_fr_timeout;
  130. struct msgid_var user_fr_inv_timeout;
  131. #ifdef TM_DIFF_RT_TIMEOUT
  132. struct msgid_var user_rt_t1_timeout;
  133. struct msgid_var user_rt_t2_timeout;
  134. #endif
  135. struct msgid_var user_inv_max_lifetime;
  136. struct msgid_var user_noninv_max_lifetime;
  137. /* internal use, val should be unsigned or positive
  138. * <= instead of < to get read of gcc warning when
  139. * sizeof(cell_member)==sizeof(val) (Note that this limits
  140. * maximum value to max. type -1) */
  141. #define SIZE_FIT_CHECK(cell_member, val, cfg_name) \
  142. if (MAX_UVAR_VALUE(((struct cell*)0)->cell_member) <= (val)){ \
  143. ERR("tm_init_timers: " cfg_name " too big: %lu (%lu ticks) " \
  144. "- max %lu (%lu ticks) \n", TICKS_TO_MS((unsigned long)(val)),\
  145. (unsigned long)(val), \
  146. TICKS_TO_MS(MAX_UVAR_VALUE(((struct cell*)0)->cell_member)), \
  147. MAX_UVAR_VALUE(((struct cell*)0)->cell_member)); \
  148. goto error; \
  149. }
  150. /* fix timer values to ticks */
  151. int tm_init_timers()
  152. {
  153. default_tm_cfg.fr_timeout=MS_TO_TICKS(default_tm_cfg.fr_timeout);
  154. default_tm_cfg.fr_inv_timeout=MS_TO_TICKS(default_tm_cfg.fr_inv_timeout);
  155. default_tm_cfg.wait_timeout=MS_TO_TICKS(default_tm_cfg.wait_timeout);
  156. default_tm_cfg.delete_timeout=MS_TO_TICKS(default_tm_cfg.delete_timeout);
  157. default_tm_cfg.rt_t1_timeout=MS_TO_TICKS(default_tm_cfg.rt_t1_timeout);
  158. default_tm_cfg.rt_t2_timeout=MS_TO_TICKS(default_tm_cfg.rt_t2_timeout);
  159. default_tm_cfg.tm_max_inv_lifetime=MS_TO_TICKS(default_tm_cfg.tm_max_inv_lifetime);
  160. default_tm_cfg.tm_max_noninv_lifetime=MS_TO_TICKS(default_tm_cfg.tm_max_noninv_lifetime);
  161. /* fix 0 values to 1 tick (minimum possible wait time ) */
  162. if (default_tm_cfg.fr_timeout==0) default_tm_cfg.fr_timeout=1;
  163. if (default_tm_cfg.fr_inv_timeout==0) default_tm_cfg.fr_inv_timeout=1;
  164. if (default_tm_cfg.wait_timeout==0) default_tm_cfg.wait_timeout=1;
  165. if (default_tm_cfg.delete_timeout==0) default_tm_cfg.delete_timeout=1;
  166. if (default_tm_cfg.rt_t2_timeout==0) default_tm_cfg.rt_t2_timeout=1;
  167. if (default_tm_cfg.rt_t1_timeout==0) default_tm_cfg.rt_t1_timeout=1;
  168. if (default_tm_cfg.tm_max_inv_lifetime==0) default_tm_cfg.tm_max_inv_lifetime=1;
  169. if (default_tm_cfg.tm_max_noninv_lifetime==0) default_tm_cfg.tm_max_noninv_lifetime=1;
  170. /* size fit checks */
  171. SIZE_FIT_CHECK(fr_timeout, default_tm_cfg.fr_timeout, "fr_timer");
  172. SIZE_FIT_CHECK(fr_inv_timeout, default_tm_cfg.fr_inv_timeout, "fr_inv_timer");
  173. #ifdef TM_DIFF_RT_TIMEOUT
  174. SIZE_FIT_CHECK(rt_t1_timeout, default_tm_cfg.rt_t1_timeout, "retr_timer1");
  175. SIZE_FIT_CHECK(rt_t2_timeout, default_tm_cfg.rt_t2_timeout, "retr_timer2");
  176. #endif
  177. SIZE_FIT_CHECK(end_of_life, default_tm_cfg.tm_max_inv_lifetime, "max_inv_lifetime");
  178. SIZE_FIT_CHECK(end_of_life, default_tm_cfg.tm_max_noninv_lifetime, "max_noninv_lifetime");
  179. memset(&user_fr_timeout, 0, sizeof(user_fr_timeout));
  180. memset(&user_fr_inv_timeout, 0, sizeof(user_fr_inv_timeout));
  181. #ifdef TM_DIFF_RT_TIMEOUT
  182. memset(&user_rt_t1_timeout, 0, sizeof(user_rt_t1_timeout));
  183. memset(&user_rt_t2_timeout, 0, sizeof(user_rt_t2_timeout));
  184. #endif
  185. memset(&user_inv_max_lifetime, 0, sizeof(user_inv_max_lifetime));
  186. memset(&user_noninv_max_lifetime, 0, sizeof(user_noninv_max_lifetime));
  187. DBG("tm: tm_init_timers: fr=%d fr_inv=%d wait=%d delete=%d t1=%d t2=%d"
  188. " max_inv_lifetime=%d max_noninv_lifetime=%d\n",
  189. default_tm_cfg.fr_timeout, default_tm_cfg.fr_inv_timeout,
  190. default_tm_cfg.wait_timeout, default_tm_cfg.delete_timeout,
  191. default_tm_cfg.rt_t1_timeout, default_tm_cfg.rt_t2_timeout,
  192. default_tm_cfg.tm_max_inv_lifetime, default_tm_cfg.tm_max_noninv_lifetime);
  193. return 0;
  194. error:
  195. return -1;
  196. }
  197. /* internal macro for timer_fixup()
  198. * performs size fit check if the timer name matches
  199. */
  200. #define IF_IS_TIMER_NAME(cell_member, cfg_name) \
  201. if ((name->len == sizeof(cfg_name)-1) && \
  202. (memcmp(name->s, cfg_name, sizeof(cfg_name)-1)==0)) { \
  203. SIZE_FIT_CHECK(cell_member, t, cfg_name); \
  204. }
  205. /* fixup function for the timer values
  206. * (called by the configuration framework)
  207. */
  208. int timer_fixup(void *handle, str *gname, str *name, void **val)
  209. {
  210. ticks_t t;
  211. t = MS_TO_TICKS((unsigned int)(long)(*val));
  212. /* fix 0 values to 1 tick (minimum possible wait time ) */
  213. if (t == 0) t = 1;
  214. /* size fix checks */
  215. IF_IS_TIMER_NAME(fr_timeout, "fr_timer")
  216. else IF_IS_TIMER_NAME(fr_inv_timeout, "fr_inv_timer")
  217. #ifdef TM_DIFF_RT_TIMEOUT
  218. else IF_IS_TIMER_NAME(rt_t1_timeout, "retr_timer1")
  219. else IF_IS_TIMER_NAME(rt_t2_timeout, "retr_timer2")
  220. #endif
  221. else IF_IS_TIMER_NAME(end_of_life, "max_inv_lifetime")
  222. else IF_IS_TIMER_NAME(end_of_life, "max_noninv_lifetime")
  223. *val = (void *)(long)t;
  224. return 0;
  225. error:
  226. return -1;
  227. }
  228. /******************** handlers ***************************/
  229. #ifndef TM_DEL_UNREF
  230. /* returns number of ticks before retrying the del, or 0 if the del.
  231. * was succesfull */
  232. inline static ticks_t delete_cell( struct cell *p_cell, int unlock )
  233. {
  234. /* there may still be FR/RETR timers, which have been reset
  235. (i.e., time_out==TIMER_DELETED) but are stilled linked to
  236. timer lists and must be removed from there before the
  237. structures are released
  238. */
  239. unlink_timers( p_cell );
  240. /* still in use ... don't delete */
  241. if ( IS_REFFED_UNSAFE(p_cell) ) {
  242. if (unlock) UNLOCK_HASH(p_cell->hash_index);
  243. DBG("DEBUG: delete_cell %p: can't delete -- still reffed (%d)\n",
  244. p_cell, p_cell->ref_count);
  245. /* delay the delete */
  246. /* TODO: change refcnts and delete on refcnt==0 */
  247. return cfg_get(tm, tm_cfg, delete_timeout);
  248. } else {
  249. if (unlock) UNLOCK_HASH(p_cell->hash_index);
  250. #ifdef EXTRA_DEBUG
  251. DBG("DEBUG: delete transaction %p\n", p_cell );
  252. #endif
  253. free_cell( p_cell );
  254. return 0;
  255. }
  256. }
  257. #endif /* TM_DEL_UNREF */
  258. /* generate a fake reply
  259. * it assumes the REPLY_LOCK is already held and returns unlocked */
  260. static void fake_reply(struct cell *t, int branch, int code )
  261. {
  262. branch_bm_t cancel_bitmap;
  263. short do_cancel_branch;
  264. enum rps reply_status;
  265. do_cancel_branch = is_invite(t) && prepare_cancel_branch(t, branch, 0);
  266. /* mark branch as canceled */
  267. t->uac[branch].request.flags|=F_RB_CANCELED;
  268. if ( is_local(t) ) {
  269. reply_status=local_reply( t, FAKED_REPLY, branch,
  270. code, &cancel_bitmap );
  271. } else {
  272. /* rely reply, but don't put on wait, we still need t
  273. * to send the cancels */
  274. reply_status=relay_reply( t, FAKED_REPLY, branch, code,
  275. &cancel_bitmap, 0 );
  276. }
  277. /* now when out-of-lock do the cancel I/O */
  278. if (do_cancel_branch) cancel_branch(t, branch, 0);
  279. /* it's cleaned up on error; if no error occurred and transaction
  280. completed regularly, I have to clean-up myself
  281. */
  282. if (reply_status == RPS_COMPLETED)
  283. put_on_wait(t);
  284. }
  285. /* return (ticks_t)-1 on error/disable and 0 on success */
  286. inline static ticks_t retransmission_handler( struct retr_buf *r_buf )
  287. {
  288. #ifdef EXTRA_DEBUG
  289. if (r_buf->my_T->flags & T_IN_AGONY) {
  290. LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
  291. " called from RETR timer (flags %x)\n",
  292. r_buf->my_T, r_buf->my_T->flags );
  293. abort();
  294. }
  295. #endif
  296. if ( r_buf->activ_type==TYPE_LOCAL_CANCEL
  297. || r_buf->activ_type==TYPE_REQUEST ) {
  298. #ifdef EXTRA_DEBUG
  299. DBG("DEBUG: retransmission_handler : "
  300. "request resending (t=%p, %.9s ... )\n",
  301. r_buf->my_T, r_buf->buffer);
  302. #endif
  303. if (SEND_BUFFER( r_buf )==-1) {
  304. /* disable retr. timers => return -1 */
  305. fake_reply(r_buf->my_T, r_buf->branch, 503 );
  306. return (ticks_t)-1;
  307. }
  308. #ifdef TMCB_ONSEND
  309. if (unlikely(has_tran_tmcbs(r_buf->my_T, TMCB_REQUEST_SENT)))
  310. run_onsend_callbacks(TMCB_REQUEST_SENT, r_buf,
  311. 0, 0, TMCB_RETR_F);
  312. #endif
  313. } else {
  314. #ifdef EXTRA_DEBUG
  315. DBG("DEBUG: retransmission_handler : "
  316. "reply resending (t=%p, %.9s ... )\n",
  317. r_buf->my_T, r_buf->buffer);
  318. #endif
  319. t_retransmit_reply(r_buf->my_T);
  320. }
  321. return 0;
  322. }
  323. inline static void final_response_handler( struct retr_buf* r_buf,
  324. struct cell* t)
  325. {
  326. int silent;
  327. #ifdef USE_DNS_FAILOVER
  328. /*int i;
  329. int added_branches;
  330. */
  331. int branch_ret;
  332. int prev_branch;
  333. ticks_t now;
  334. #endif
  335. # ifdef EXTRA_DEBUG
  336. if (t->flags & T_IN_AGONY)
  337. {
  338. LOG( L_ERR, "ERROR: transaction %p scheduled for deletion and"
  339. " called from FR timer (flags %x)\n", t, t->flags);
  340. abort();
  341. }
  342. # endif
  343. /* FR for local cancels.... */
  344. if (r_buf->activ_type==TYPE_LOCAL_CANCEL)
  345. {
  346. #ifdef TIMER_DEBUG
  347. DBG("DEBUG: final_response_handler: stop retr for Local Cancel\n");
  348. #endif
  349. return;
  350. }
  351. /* FR for replies (negative INVITE replies) */
  352. if (r_buf->activ_type>0) {
  353. # ifdef EXTRA_DEBUG
  354. if (t->uas.request->REQ_METHOD!=METHOD_INVITE
  355. || t->uas.status < 200 ) {
  356. LOG(L_CRIT, "BUG: final_response_handler: unknown type reply"
  357. " buffer\n");
  358. abort();
  359. }
  360. # endif
  361. put_on_wait( t );
  362. return;
  363. };
  364. /* lock reply processing to determine how to proceed reliably */
  365. LOCK_REPLIES( t );
  366. /* now it can be only a request retransmission buffer;
  367. try if you can simply discard the local transaction
  368. state without compellingly removing it from the
  369. world */
  370. silent=
  371. /* don't go silent if disallowed globally ... */
  372. cfg_get(tm, tm_cfg, noisy_ctimer)==0
  373. /* ... or for this particular transaction */
  374. && has_noisy_ctimer(t) == 0
  375. /* not for UACs */
  376. && !is_local(t)
  377. /* invites only */
  378. && is_invite(t)
  379. /* parallel forking does not allow silent state discarding */
  380. && t->nr_of_outgoings==1
  381. /* on_negativ reply handler not installed -- serial forking
  382. * could occur otherwise */
  383. && t->on_negative==0
  384. /* the same for FAILURE callbacks */
  385. && !has_tran_tmcbs( t, TMCB_ON_FAILURE_RO|TMCB_ON_FAILURE)
  386. /* something received -- we will not be silent on error */
  387. && t->uac[r_buf->branch].last_received==0;
  388. if (silent) {
  389. UNLOCK_REPLIES(t);
  390. #ifdef EXTRA_DEBUG
  391. DBG("DEBUG: final_response_handler: transaction silently dropped (%p)"
  392. ", branch %d, last_received %d\n",t, r_buf->branch,
  393. t->uac[r_buf->branch].last_received);
  394. #endif
  395. put_on_wait( t );
  396. return;
  397. }
  398. #ifdef EXTRA_DEBUG
  399. DBG("DEBUG: final_response_handler:stop retr. and send CANCEL (%p)\n", t);
  400. #endif
  401. if ((r_buf->branch < MAX_BRANCHES) && /* r_buf->branch is always >=0 */
  402. (t->uac[r_buf->branch].last_received==0) &&
  403. (t->uac[r_buf->branch].request.buffer!=NULL) /* not a blind UAC */
  404. ){
  405. /* no reply received */
  406. #ifdef USE_DST_BLACKLIST
  407. if (cfg_get(core, core_cfg, use_dst_blacklist)
  408. && r_buf->my_T
  409. && r_buf->my_T->uas.request
  410. && (r_buf->my_T->uas.request->REQ_METHOD & cfg_get(tm, tm_cfg, tm_blst_methods_add))
  411. )
  412. dst_blacklist_add( BLST_ERR_TIMEOUT, &r_buf->dst,
  413. r_buf->my_T->uas.request);
  414. #endif
  415. #ifdef USE_DNS_FAILOVER
  416. /* if this is an invite, the destination resolves to more ips, and
  417. * it still hasn't passed more than fr_inv_timeout since we
  418. * started, add another branch/uac */
  419. if (cfg_get(core, core_cfg, use_dns_failover)){
  420. now=get_ticks_raw();
  421. if ((s_ticks_t)(t->end_of_life-now)>0){
  422. branch_ret=add_uac_dns_fallback(t, t->uas.request,
  423. &t->uac[r_buf->branch], 0);
  424. prev_branch=-1;
  425. while((branch_ret>=0) &&(branch_ret!=prev_branch)){
  426. prev_branch=branch_ret;
  427. branch_ret=t_send_branch(t, branch_ret, t->uas.request ,
  428. 0, 0);
  429. }
  430. }
  431. }
  432. #endif
  433. }
  434. fake_reply(t, r_buf->branch, 408);
  435. }
  436. /* handles retransmissions and fr timers */
  437. /* the following assumption are made (to avoid deleting/re-adding the timer):
  438. * retr_buf->retr_interval < ( 1<<((sizeof(ticks_t)*8-1) )
  439. * if retr_buf->retr_interval==0 => timer disabled
  440. * ==(ticks_t) -1 => retr. disabled (fr working)
  441. * retr_buf->retr_interval & (1 <<(sizeof(ticks_t)*8-1) => retr. & fr reset
  442. * (we never reset only retr, it's either reset both of them or retr
  443. * disabled & reset fr). In this case the fr_origin will contain the
  444. * "time" of the reset and next retr should occur at
  445. * fr->origin+retr_interval (we also assume that we'll never reset retr
  446. * to a lower value then the current one)
  447. */
  448. ticks_t retr_buf_handler(ticks_t ticks, struct timer_ln* tl, void *p)
  449. {
  450. struct retr_buf* rbuf ;
  451. ticks_t fr_remainder;
  452. ticks_t retr_remainder;
  453. ticks_t retr_interval;
  454. ticks_t new_retr_interval;
  455. struct cell *t;
  456. rbuf=(struct retr_buf*)
  457. ((void*)tl-(void*)(&((struct retr_buf*)0)->timer));
  458. membar_depends(); /* to be on the safe side */
  459. t=rbuf->my_T;
  460. #ifdef TIMER_DEBUG
  461. DBG("tm: timer retr_buf_handler @%d (%p -> %p -> %p)\n",
  462. ticks, tl, rbuf, t);
  463. #endif
  464. if (unlikely(rbuf->flags & F_RB_DEL_TIMER)){
  465. /* timer marked for deletion */
  466. rbuf->t_active=0; /* mark it as removed */
  467. /* a membar is not really needed, in the very unlikely case that
  468. * another process will see old t_active's value and will try to
  469. * delete the timer again, but since timer_del it's safe in this cases
  470. * it will be a no-op */
  471. return 0;
  472. }
  473. /* overflow safe check (should work ok for fr_intervals < max ticks_t/2) */
  474. if ((s_ticks_t)(rbuf->fr_expire-ticks)<=0){
  475. /* final response */
  476. rbuf->t_active=0; /* mark the timer as removed
  477. (both timers disabled)
  478. a little race risk, but
  479. nothing bad would happen */
  480. rbuf->flags|=F_RB_TIMEOUT;
  481. /* WARNING: the next line depends on taking care not to start the
  482. * wait timer before finishing with t (if this is not
  483. * guaranteed then comment the timer_allow_del() line) */
  484. timer_allow_del(); /* [optional] allow timer_dels, since we're done
  485. and there is no race risk */
  486. final_response_handler(rbuf, t);
  487. return 0;
  488. }else{
  489. /* 4 possible states running (t1), t2, paused, disabled */
  490. if ((s_ticks_t)(rbuf->retr_expire-ticks)<=0){
  491. if (rbuf->flags & F_RB_RETR_DISABLED)
  492. goto disabled;
  493. /* retr_interval= min (2*ri, rt_t2) , *p==2*ri*/
  494. /* no branch version:
  495. #idef CC_SIGNED_RIGHT_SHIFT
  496. ri= rt_t2+((2*ri-rt_t2) &
  497. ((signed)(2*ri-rt_t2)>>(sizeof(ticks_t)*8-1));
  498. #else
  499. ri=rt_t2+((2*ri-rt_t2)& -(2*ri<rt_t2));
  500. #endif
  501. */
  502. /* get the current interval from timer param. */
  503. if ((rbuf->flags & F_RB_T2) ||
  504. (((ticks_t)(unsigned long)p)>RT_T2_TIMEOUT(rbuf))){
  505. retr_interval=RT_T2_TIMEOUT(rbuf);
  506. new_retr_interval=RT_T2_TIMEOUT(rbuf);
  507. }else{
  508. retr_interval=(ticks_t)(unsigned long)p;
  509. new_retr_interval=retr_interval<<1;
  510. }
  511. #ifdef TIMER_DEBUG
  512. DBG("tm: timer: retr: new interval %d (max %d)\n",
  513. retr_interval, RT_T2_TIMEOUT(rbuf));
  514. #endif
  515. /* we could race with the reply_received code, but the
  516. * worst thing that can happen is to delay a reset_to_t2
  517. * for crt_interval and send an extra retr.*/
  518. rbuf->retr_expire=ticks+retr_interval;
  519. /* set new interval to -1 on error, or retr_int. on success */
  520. retr_remainder=retransmission_handler(rbuf) | retr_interval;
  521. /* store the next retr. interval inside the timer struct,
  522. * in the data member */
  523. tl->data=(void*)(unsigned long)(new_retr_interval);
  524. }else{
  525. retr_remainder= rbuf->retr_expire-ticks;
  526. DBG("tm: timer: retr: nothing to do, expire in %d\n",
  527. retr_remainder);
  528. }
  529. }
  530. /* skip: */
  531. /* return minimum of the next retransmission handler and the
  532. * final response (side benefit: it properly cancels timer if ret==0 and
  533. * sleeps for fr_remainder if retr. is canceled [==(ticks_t)-1]) */
  534. fr_remainder=rbuf->fr_expire-ticks; /* to be more precise use
  535. get_ticks_raw() instead of ticks
  536. (but make sure that
  537. crt. ticks < fr_expire */
  538. #ifdef TIMER_DEBUG
  539. DBG("tm: timer retr_buf_handler @%d (%p ->%p->%p) exiting min (%d, %d)\n",
  540. ticks, tl, rbuf, t, retr_remainder, fr_remainder);
  541. #endif
  542. #ifdef EXTRA_DEBUG
  543. if (retr_remainder==0 || fr_remainder==0){
  544. BUG("tm: timer retr_buf_handler: 0 remainder => disabling timer!: "
  545. "retr_remainder=%d, fr_remainder=%d\n", retr_remainder,
  546. fr_remainder);
  547. }
  548. #endif
  549. if (retr_remainder<fr_remainder)
  550. return retr_remainder;
  551. else{
  552. /* hack to switch to the slow timer */
  553. #ifdef TM_FAST_RETR_TIMER
  554. tl->flags&=~F_TIMER_FAST;
  555. #endif
  556. return fr_remainder;
  557. }
  558. disabled:
  559. return rbuf->fr_expire-ticks;
  560. }
  561. ticks_t wait_handler(ticks_t ti, struct timer_ln *wait_tl, void* data)
  562. {
  563. struct cell *p_cell;
  564. ticks_t ret;
  565. p_cell=(struct cell*)data;
  566. #ifdef TIMER_DEBUG
  567. DBG("DEBUG: WAIT timer hit @%d for %p (timer_lm %p)\n",
  568. ti, p_cell, wait_tl);
  569. #endif
  570. #ifdef TM_DEL_UNREF
  571. /* stop cancel timers if any running */
  572. if ( is_invite(p_cell) ) cleanup_localcancel_timers( p_cell );
  573. /* remove the cell from the hash table */
  574. LOCK_HASH( p_cell->hash_index );
  575. remove_from_hash_table_unsafe( p_cell );
  576. UNLOCK_HASH( p_cell->hash_index );
  577. p_cell->flags |= T_IN_AGONY;
  578. UNREF_FREE(p_cell);
  579. ret=0;
  580. #else /* TM_DEL_UNREF */
  581. if (p_cell->flags & T_IN_AGONY){
  582. /* delayed delete */
  583. /* we call delete now without any locking on hash/ref_count;
  584. we can do that because delete_handler is only entered after
  585. the delete timer was installed from wait_handler, which
  586. removed transaction from hash table and did not destroy it
  587. because some processes were using it; that means that the
  588. processes currently using the transaction can unref and no
  589. new processes can ref -- we can wait until ref_count is
  590. zero safely without locking
  591. */
  592. ret=delete_cell( p_cell, 0 /* don't unlock on return */ );
  593. }else {
  594. /* stop cancel timers if any running */
  595. if ( is_invite(p_cell) ) cleanup_localcancel_timers( p_cell );
  596. /* remove the cell from the hash table */
  597. LOCK_HASH( p_cell->hash_index );
  598. remove_from_hash_table_unsafe( p_cell );
  599. p_cell->flags |= T_IN_AGONY;
  600. /* delete (returns with UNLOCK-ed_HASH) */
  601. ret=delete_cell( p_cell, 1 /* unlock on return */ );
  602. }
  603. #endif /* TM_DEL_UNREF */
  604. return ret;
  605. }