io_wait.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005 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 io wait common stuff used by tcp_main.c & tcp_read.c
  20. * (see io_wait.h)
  21. */
  22. /*
  23. * History:
  24. * --------
  25. * 2005-06-15 created by andrei
  26. * 2005-06-26 added kqueue (andrei)
  27. * 2005-07-04 added /dev/poll (andrei)
  28. */
  29. /*!
  30. * \file
  31. * \brief SIP-router core ::
  32. * \ingroup core
  33. * Module: \ref core
  34. */
  35. #ifndef NO_IO_WAIT
  36. #ifdef HAVE_EPOLL
  37. #include <unistd.h> /* close() */
  38. #endif
  39. #ifdef HAVE_DEVPOLL
  40. #include <sys/types.h> /* open */
  41. #include <sys/stat.h>
  42. #include <fcntl.h>
  43. #include <unistd.h> /* close, ioctl */
  44. #endif
  45. #include <stdlib.h> /* strtol() */
  46. #include "io_wait.h"
  47. #include "ut.h" /* get_sys_ver() */
  48. #include "mem/mem.h"
  49. #ifndef local_malloc
  50. #define local_malloc pkg_malloc
  51. #endif
  52. #ifndef local_free
  53. #define local_free pkg_free
  54. #endif
  55. char* poll_support="poll"
  56. #ifdef HAVE_EPOLL
  57. ", epoll_lt, epoll_et"
  58. #endif
  59. #ifdef HAVE_SIGIO_RT
  60. ", sigio_rt"
  61. #endif
  62. #ifdef HAVE_SELECT
  63. ", select"
  64. #endif
  65. #ifdef HAVE_KQUEUE
  66. ", kqueue"
  67. #endif
  68. #ifdef HAVE_DEVPOLL
  69. ", /dev/poll"
  70. #endif
  71. ;
  72. char* poll_method_str[POLL_END]={ "none", "poll", "epoll_lt", "epoll_et",
  73. "sigio_rt", "select", "kqueue", "/dev/poll"
  74. };
  75. int _os_ver=0; /* os version number */
  76. #ifdef HAVE_SIGIO_RT
  77. static int _sigio_init=0;
  78. static int _sigio_crt_rtsig;
  79. static sigset_t _sigio_rtsig_used;
  80. #endif
  81. #ifdef HAVE_SIGIO_RT
  82. /* sigio specific init
  83. * returns -1 on error, 0 on success */
  84. static int init_sigio(io_wait_h* h, int rsig)
  85. {
  86. int r;
  87. int n;
  88. int signo;
  89. int start_sig;
  90. sigset_t oldset;
  91. if (!_sigio_init){
  92. _sigio_init=1;
  93. _sigio_crt_rtsig=SIGRTMIN;
  94. sigemptyset(&_sigio_rtsig_used);
  95. }
  96. h->signo=0;
  97. if (rsig==0){
  98. start_sig=_sigio_crt_rtsig;
  99. n=SIGRTMAX-SIGRTMIN;
  100. }else{
  101. if ((rsig < SIGRTMIN) || (rsig >SIGRTMAX)){
  102. LM_CRIT("real time signal %d out of range [%d, %d]\n",
  103. rsig, SIGRTMIN, SIGRTMAX);
  104. goto error;
  105. }
  106. start_sig=rsig;
  107. n=0;
  108. }
  109. sigemptyset(&h->sset);
  110. sigemptyset(&oldset);
  111. retry1:
  112. /* get current block mask */
  113. if (sigprocmask(SIG_BLOCK, &h->sset, &oldset )==-1){
  114. if (errno==EINTR) goto retry1;
  115. LM_ERR("1st sigprocmask failed: %s [%d]\n", strerror(errno), errno);
  116. /* try to continue */
  117. }
  118. for (r=start_sig; r<=(n+start_sig); r++){
  119. signo=(r>SIGRTMAX)?r-SIGRTMAX+SIGRTMIN:r;
  120. if (! sigismember(&_sigio_rtsig_used, signo) &&
  121. ! sigismember(&oldset, signo)){
  122. sigaddset(&_sigio_rtsig_used, signo);
  123. h->signo=signo;
  124. _sigio_crt_rtsig=(signo<SIGRTMAX)?signo+1:SIGRTMIN;
  125. break;
  126. }
  127. }
  128. if (h->signo==0){
  129. LM_CRIT("%s\n", rsig?"could not assign requested real-time signal":
  130. "out of real-time signals");
  131. goto error;
  132. }
  133. DBG("init_sigio: trying signal %d... \n", h->signo);
  134. if (sigaddset(&h->sset, h->signo)==-1){
  135. LM_ERR("sigaddset failed for %d: %s [%d]\n",
  136. h->signo, strerror(errno), errno);
  137. goto error;
  138. }
  139. if (sigaddset(&h->sset, SIGIO)==-1){
  140. LM_ERR("sigaddset failed for %d: %s [%d]\n",
  141. SIGIO, strerror(errno), errno);
  142. goto error;
  143. }
  144. retry:
  145. if (sigprocmask(SIG_BLOCK, &h->sset, 0)==-1){
  146. if (errno==EINTR) goto retry;
  147. LM_ERR("sigprocmask failed: %s [%d]\n",
  148. strerror(errno), errno);
  149. goto error;
  150. }
  151. return 0;
  152. error:
  153. h->signo=0;
  154. sigemptyset(&h->sset);
  155. return -1;
  156. }
  157. /* sigio specific destroy */
  158. static void destroy_sigio(io_wait_h* h)
  159. {
  160. if (h->signo){
  161. sigprocmask(SIG_UNBLOCK, &h->sset, 0);
  162. sigemptyset(&h->sset);
  163. sigdelset(&_sigio_rtsig_used, h->signo);
  164. h->signo=0;
  165. }
  166. }
  167. #endif
  168. #ifdef HAVE_EPOLL
  169. /* epoll specific init
  170. * returns -1 on error, 0 on success */
  171. static int init_epoll(io_wait_h* h)
  172. {
  173. again:
  174. h->epfd=epoll_create(h->max_fd_no);
  175. if (h->epfd==-1){
  176. if (errno==EINTR) goto again;
  177. LM_ERR("epoll_create: %s [%d]\n", strerror(errno), errno);
  178. return -1;
  179. }
  180. return 0;
  181. }
  182. static void destroy_epoll(io_wait_h* h)
  183. {
  184. if (h->epfd!=-1){
  185. close(h->epfd);
  186. h->epfd=-1;
  187. }
  188. }
  189. #endif
  190. #ifdef HAVE_KQUEUE
  191. /* kqueue specific init
  192. * returns -1 on error, 0 on success */
  193. static int init_kqueue(io_wait_h* h)
  194. {
  195. again:
  196. h->kq_fd=kqueue();
  197. if (h->kq_fd==-1){
  198. if (errno==EINTR) goto again;
  199. LM_ERR("kqueue: %s [%d]\n", strerror(errno), errno);
  200. return -1;
  201. }
  202. return 0;
  203. }
  204. static void destroy_kqueue(io_wait_h* h)
  205. {
  206. if (h->kq_fd!=-1){
  207. close(h->kq_fd);
  208. h->kq_fd=-1;
  209. }
  210. }
  211. #endif
  212. #ifdef HAVE_DEVPOLL
  213. /* /dev/poll specific init
  214. * returns -1 on error, 0 on success */
  215. static int init_devpoll(io_wait_h* h)
  216. {
  217. again:
  218. h->dpoll_fd=open("/dev/poll", O_RDWR);
  219. if (h->dpoll_fd==-1){
  220. if (errno==EINTR) goto again;
  221. LM_ERR("open: %s [%d]\n", strerror(errno), errno);
  222. return -1;
  223. }
  224. return 0;
  225. }
  226. static void destroy_devpoll(io_wait_h* h)
  227. {
  228. if (h->dpoll_fd!=-1){
  229. close(h->dpoll_fd);
  230. h->dpoll_fd=-1;
  231. }
  232. }
  233. #endif
  234. #ifdef HAVE_SELECT
  235. static int init_select(io_wait_h* h)
  236. {
  237. FD_ZERO(&h->master_rset);
  238. FD_ZERO(&h->master_wset);
  239. return 0;
  240. }
  241. #endif
  242. /*
  243. * returns 0 on success, and an error message on error
  244. */
  245. char* check_poll_method(enum poll_types poll_method)
  246. {
  247. char* ret;
  248. ret=0;
  249. if (_os_ver==0)
  250. _os_ver=get_sys_version(0,0,0);
  251. switch(poll_method){
  252. case POLL_NONE:
  253. break;
  254. case POLL_POLL:
  255. /* always supported */
  256. break;
  257. case POLL_SELECT:
  258. /* should be always supported */
  259. #ifndef HAVE_SELECT
  260. ret="select not supported, try re-compiling with -DHAVE_SELECT";
  261. #endif
  262. break;
  263. case POLL_EPOLL_LT:
  264. case POLL_EPOLL_ET:
  265. #ifndef HAVE_EPOLL
  266. ret="epoll not supported, try re-compiling with -DHAVE_EPOLL";
  267. #else
  268. /* only on 2.6 + */
  269. if (_os_ver<0x020542) /* if ver < 2.5.66 */
  270. ret="epoll not supported on kernels < 2.6";
  271. #endif
  272. break;
  273. case POLL_SIGIO_RT:
  274. #ifndef HAVE_SIGIO_RT
  275. ret="sigio_rt not supported, try re-compiling with"
  276. " -DHAVE_SIGIO_RT";
  277. #else
  278. /* only on 2.2 + ?? */
  279. if (_os_ver<0x020200) /* if ver < 2.2.0 */
  280. ret="epoll not supported on kernels < 2.2 (?)";
  281. #endif
  282. break;
  283. case POLL_KQUEUE:
  284. #ifndef HAVE_KQUEUE
  285. ret="kqueue not supported, try re-compiling with -DHAVE_KQUEUE";
  286. #else
  287. /* only in FreeBSD 4.1, NETBSD 2.0, OpenBSD 2.9, Darwin, DragonFly */
  288. #ifdef __OS_freebsd
  289. /* all DragonFly versions have kqueque */
  290. #ifndef __OS_dragonfly
  291. if (_os_ver<0x0401) /* if ver < 4.1 */
  292. ret="kqueue not supported on FreeBSD < 4.1";
  293. #endif /* __OS_dragonfly */
  294. #elif defined (__OS_netbsd)
  295. if (_os_ver<0x020000) /* if ver < 2.0 */
  296. ret="kqueue not supported on NetBSD < 2.0";
  297. #elif defined (__OS_openbsd)
  298. if (_os_ver<0x0209) /* if ver < 2.9 ? */
  299. ret="kqueue not supported on OpenBSD < 2.9 (?)";
  300. #endif /* assume that the rest support kqueue ifdef HAVE_KQUEUE */
  301. #endif
  302. break;
  303. case POLL_DEVPOLL:
  304. #ifndef HAVE_DEVPOLL
  305. ret="/dev/poll not supported, try re-compiling with"
  306. " -DHAVE_DEVPOLL";
  307. #else
  308. /* only in Solaris >= 7.0 (?) */
  309. #ifdef __OS_solaris
  310. if (_os_ver<0x0507) /* ver < 5.7 */
  311. ret="/dev/poll not supported on Solaris < 7.0 (SunOS 5.7)";
  312. #endif
  313. #endif
  314. break;
  315. default:
  316. ret="unknown not supported method";
  317. }
  318. return ret;
  319. }
  320. enum poll_types choose_poll_method()
  321. {
  322. enum poll_types poll_method;
  323. if (_os_ver==0)
  324. _os_ver=get_sys_version(0,0,0);
  325. poll_method=0;
  326. #ifdef HAVE_EPOLL
  327. if (_os_ver>=0x020542) /* if ver >= 2.5.66 */
  328. poll_method=POLL_EPOLL_LT; /* or POLL_EPOLL_ET */
  329. #endif
  330. #ifdef HAVE_KQUEUE
  331. if (poll_method==0)
  332. /* only in FreeBSD 4.1, NETBSD 2.0, OpenBSD 2.9, Darwin, DragonFly */
  333. #ifdef __OS_freebsd
  334. /* all DragonFly versions have kqueque */
  335. #ifndef __OS_dragonfly
  336. if (_os_ver>=0x0401) /* if ver >= 4.1 */
  337. #endif /**__OS_dragonfly */
  338. #elif defined (__OS_netbsd)
  339. if (_os_ver>=0x020000) /* if ver >= 2.0 */
  340. #elif defined (__OS_openbsd)
  341. if (_os_ver>=0x0209) /* if ver >= 2.9 (?) */
  342. #endif /* assume that the rest support kqueue ifdef HAVE_KQUEUE */
  343. poll_method=POLL_KQUEUE;
  344. #endif
  345. #ifdef HAVE_DEVPOLL
  346. #ifdef __OS_solaris
  347. if (poll_method==0)
  348. /* only in Solaris >= 7.0 (?) */
  349. if (_os_ver>=0x0507) /* if ver >=SunOS 5.7 */
  350. poll_method=POLL_DEVPOLL;
  351. #endif
  352. #endif
  353. #ifdef HAVE_SIGIO_RT
  354. if (poll_method==0)
  355. if (_os_ver>=0x020200) /* if ver >= 2.2.0 */
  356. poll_method=POLL_SIGIO_RT;
  357. #endif
  358. if (poll_method==0) poll_method=POLL_POLL;
  359. return poll_method;
  360. }
  361. char* poll_method_name(enum poll_types poll_method)
  362. {
  363. if ((poll_method>=POLL_NONE) && (poll_method<POLL_END))
  364. return poll_method_str[poll_method];
  365. else
  366. return "invalid poll method";
  367. }
  368. /* converts a string into a poll_method
  369. * returns POLL_NONE (0) on error, else the corresponding poll type */
  370. enum poll_types get_poll_type(char* s)
  371. {
  372. int r;
  373. int l;
  374. l=strlen(s);
  375. for (r=POLL_END-1; r>POLL_NONE; r--)
  376. if ((strlen(poll_method_str[r])==l) &&
  377. (strncasecmp(poll_method_str[r], s, l)==0))
  378. break;
  379. return r;
  380. }
  381. /* initializes the static vars/arrays
  382. * params: h - pointer to the io_wait_h that will be initialized
  383. * max_fd - maximum allowed fd number
  384. * poll_m - poll method (0 for automatic best fit)
  385. */
  386. int init_io_wait(io_wait_h* h, int max_fd, enum poll_types poll_method)
  387. {
  388. char * poll_err;
  389. if (_os_ver==0) _os_ver=get_sys_version(0,0,0);
  390. memset(h, 0, sizeof(*h));
  391. h->max_fd_no=max_fd;
  392. #ifdef HAVE_EPOLL
  393. h->epfd=-1;
  394. #endif
  395. #ifdef HAVE_KQUEUE
  396. h->kq_fd=-1;
  397. #endif
  398. #ifdef HAVE_DEVPOLL
  399. h->dpoll_fd=-1;
  400. #endif
  401. poll_err=check_poll_method(poll_method);
  402. /* set an appropiate poll method */
  403. if (poll_err || (poll_method==0)){
  404. poll_method=choose_poll_method();
  405. if (poll_err){
  406. LM_ERR("%s, using %s instead\n",
  407. poll_err, poll_method_str[poll_method]);
  408. }else{
  409. LM_INFO("using %s as the io watch method (auto detected)\n",
  410. poll_method_str[poll_method]);
  411. }
  412. }
  413. h->poll_method=poll_method;
  414. /* common stuff, everybody has fd_hash */
  415. h->fd_hash=local_malloc(sizeof(*(h->fd_hash))*h->max_fd_no);
  416. if (h->fd_hash==0){
  417. LM_CRIT("could not alloc fd hashtable (%ld bytes)\n",
  418. (long)sizeof(*(h->fd_hash))*h->max_fd_no );
  419. goto error;
  420. }
  421. memset((void*)h->fd_hash, 0, sizeof(*(h->fd_hash))*h->max_fd_no);
  422. switch(poll_method){
  423. case POLL_POLL:
  424. #ifdef HAVE_SELECT
  425. case POLL_SELECT:
  426. #endif
  427. #ifdef HAVE_SIGIO_RT
  428. case POLL_SIGIO_RT:
  429. #endif
  430. #ifdef HAVE_DEVPOLL
  431. case POLL_DEVPOLL:
  432. #endif
  433. h->fd_array=local_malloc(sizeof(*(h->fd_array))*h->max_fd_no);
  434. if (h->fd_array==0){
  435. LM_CRIT("could not alloc fd array (%ld bytes)\n",
  436. (long)sizeof(*(h->fd_hash))*h->max_fd_no);
  437. goto error;
  438. }
  439. memset((void*)h->fd_array, 0, sizeof(*(h->fd_array))*h->max_fd_no);
  440. #ifdef HAVE_SIGIO_RT
  441. if ((poll_method==POLL_SIGIO_RT) && (init_sigio(h, 0)<0)){
  442. LM_CRIT("sigio init failed\n");
  443. goto error;
  444. }
  445. #endif
  446. #ifdef HAVE_DEVPOLL
  447. if ((poll_method==POLL_DEVPOLL) && (init_devpoll(h)<0)){
  448. LM_CRIT("/dev/poll init failed\n");
  449. goto error;
  450. }
  451. #endif
  452. #ifdef HAVE_SELECT
  453. if ((poll_method==POLL_SELECT) && (init_select(h)<0)){
  454. LM_CRIT("select init failed\n");
  455. goto error;
  456. }
  457. #endif
  458. break;
  459. #ifdef HAVE_EPOLL
  460. case POLL_EPOLL_LT:
  461. case POLL_EPOLL_ET:
  462. h->ep_array=local_malloc(sizeof(*(h->ep_array))*h->max_fd_no);
  463. if (h->ep_array==0){
  464. LM_CRIT("could not alloc epoll array\n");
  465. goto error;
  466. }
  467. memset((void*)h->ep_array, 0, sizeof(*(h->ep_array))*h->max_fd_no);
  468. if (init_epoll(h)<0){
  469. LM_CRIT("epoll init failed\n");
  470. goto error;
  471. }
  472. break;
  473. #endif
  474. #ifdef HAVE_KQUEUE
  475. case POLL_KQUEUE:
  476. h->kq_changes_size=KQ_CHANGES_ARRAY_SIZE;
  477. /* kevent returns different events for read & write
  478. => to get all the possible events in one call we
  479. need twice the number of added fds + space
  480. for possible changelist errors.
  481. OTOH if memory is to be saved at all costs, one can
  482. decrease the array size.
  483. */
  484. h->kq_array_size=2 * h->max_fd_no + h->kq_changes_size;
  485. h->kq_array=local_malloc(sizeof(*(h->kq_array))*h->kq_array_size);
  486. if (h->kq_array==0){
  487. LM_CRIT("could not alloc kqueue event array\n");
  488. goto error;
  489. }
  490. h->kq_changes=local_malloc(sizeof(*(h->kq_changes))*
  491. h->kq_changes_size);
  492. if (h->kq_changes==0){
  493. LM_CRIT("could not alloc kqueue changes array\n");
  494. goto error;
  495. }
  496. h->kq_nchanges=0;
  497. memset((void*)h->kq_array, 0,
  498. sizeof(*(h->kq_array))*h->kq_array_size);
  499. memset((void*)h->kq_changes, 0,
  500. sizeof(*(h->kq_changes))* h->kq_changes_size);
  501. if (init_kqueue(h)<0){
  502. LM_CRIT("kqueue init failed\n");
  503. goto error;
  504. }
  505. break;
  506. #endif
  507. default:
  508. LM_CRIT("unknown/unsupported poll method %s (%d)\n",
  509. poll_method_str[poll_method], poll_method);
  510. goto error;
  511. }
  512. return 0;
  513. error:
  514. return -1;
  515. }
  516. /* destroys everything init_io_wait allocated */
  517. void destroy_io_wait(io_wait_h* h)
  518. {
  519. switch(h->poll_method){
  520. #ifdef HAVE_EPOLL
  521. case POLL_EPOLL_LT:
  522. case POLL_EPOLL_ET:
  523. destroy_epoll(h);
  524. if (h->ep_array){
  525. local_free(h->ep_array);
  526. h->ep_array=0;
  527. }
  528. break;
  529. #endif
  530. #ifdef HAVE_KQUEUE
  531. case POLL_KQUEUE:
  532. destroy_kqueue(h);
  533. if (h->kq_array){
  534. local_free(h->kq_array);
  535. h->kq_array=0;
  536. }
  537. if (h->kq_changes){
  538. local_free(h->kq_changes);
  539. h->kq_changes=0;
  540. }
  541. break;
  542. #endif
  543. #ifdef HAVE_SIGIO_RT
  544. case POLL_SIGIO_RT:
  545. destroy_sigio(h);
  546. break;
  547. #endif
  548. #ifdef HAVE_DEVPOLL
  549. case POLL_DEVPOLL:
  550. destroy_devpoll(h);
  551. break;
  552. #endif
  553. default: /*do nothing*/
  554. ;
  555. }
  556. if (h->fd_array){
  557. local_free(h->fd_array);
  558. h->fd_array=0;
  559. }
  560. if (h->fd_hash){
  561. local_free(h->fd_hash);
  562. h->fd_hash=0;
  563. }
  564. }
  565. #endif /*ifndef NO_IO_WAIT */