conntable.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* Connection Table */
  42. #include "fe.h"
  43. Connection_Table *
  44. connection_table_new(int table_size)
  45. {
  46. Connection_Table *ct;
  47. int i = 0;
  48. ct= (Connection_Table*)slapi_ch_calloc( 1, sizeof(Connection_Table) );
  49. ct->size= table_size;
  50. ct->c = (Connection *)slapi_ch_calloc( 1, table_size * sizeof(Connection) );
  51. ct->fd = (struct POLL_STRUCT *)slapi_ch_calloc(1, table_size * sizeof(struct POLL_STRUCT));
  52. ct->table_mutex = PR_NewLock();
  53. /* We rely on the fact that we called calloc, which zeros the block, so we don't
  54. * init any structure element unless a zero value is troublesome later
  55. */
  56. for ( i = 0; i < table_size; i++ )
  57. {
  58. LBER_SOCKET invalid_socket;
  59. ber_len_t maxbersize = config_get_maxbersize();
  60. /* DBDB---move this out of here once everything works */
  61. ct->c[i].c_sb = ber_sockbuf_alloc();
  62. invalid_socket = SLAPD_INVALID_SOCKET;
  63. ber_sockbuf_set_option( ct->c[i].c_sb, LBER_SOCKBUF_OPT_DESC, &invalid_socket );
  64. ct->c[i].c_sd = SLAPD_INVALID_SOCKET;
  65. ber_sockbuf_set_option( ct->c[i].c_sb, LBER_SOCKBUF_OPT_NO_READ_AHEAD, LBER_OPT_ON );
  66. ber_sockbuf_set_option( ct->c[i].c_sb, LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE, &maxbersize );
  67. #ifndef _WIN32
  68. /* all connections start out invalid */
  69. ct->fd[i].fd = SLAPD_INVALID_SOCKET;
  70. #endif
  71. /* The connection table has a double linked list running through it.
  72. * This is used to find out which connections should be looked at
  73. * in the poll loop. Slot 0 in the table is always the head of
  74. * the linked list. Each slot has a c_next and c_prev which are
  75. * pointers back into the array of connection slots. */
  76. ct->c[i].c_next = NULL;
  77. ct->c[i].c_prev = NULL;
  78. ct->c[i].c_ci= i;
  79. ct->c[i].c_fdi= SLAPD_INVALID_SOCKET_INDEX;
  80. }
  81. return ct;
  82. }
  83. void connection_table_free(Connection_Table *ct)
  84. {
  85. int i;
  86. for (i = 0; i < ct->size; i++)
  87. {
  88. /* Free the contents of the connection structure */
  89. Connection *c= &(ct->c[i]);
  90. connection_done(c);
  91. }
  92. slapi_ch_free((void**)&ct->c);
  93. slapi_ch_free((void**)&ct->fd);
  94. PR_DestroyLock(ct->table_mutex);
  95. slapi_ch_free((void**)&ct);
  96. }
  97. #ifdef _WIN32
  98. /*
  99. * This function looks up connection by nspr fd. It is
  100. * slow because it iterrates through the entire connection
  101. * array. Currently, it is only used on NT in secure_read_function
  102. * to handle I/O timeout on SSL socket we should almost never
  103. * happen.
  104. */
  105. Connection*
  106. connection_table_get_connection_from_fd(Connection_Table *ct,PRFileDesc *prfd)
  107. {
  108. int i;
  109. for (i = 0; i < ct->size; i++)
  110. {
  111. if (ct->c[i].c_prfd == prfd)
  112. {
  113. return (&(ct->c[i]));
  114. }
  115. }
  116. return NULL;
  117. }
  118. #endif
  119. void
  120. connection_table_abandon_all_operations(Connection_Table *ct)
  121. {
  122. int i;
  123. for ( i = 0; i < ct->size; i++ )
  124. {
  125. if ( ct->c[i].c_mutex != NULL )
  126. {
  127. PR_Lock( ct->c[i].c_mutex );
  128. connection_abandon_operations( &ct->c[i] );
  129. PR_Unlock( ct->c[i].c_mutex );
  130. }
  131. }
  132. }
  133. /* Given a file descriptor for a socket, this function will return
  134. * a slot in the connection table to use.
  135. *
  136. * Note: this function is only called from the slapd_daemon (listener)
  137. * thread, which means it will never be called by two threads at
  138. * the same time.
  139. *
  140. * Returns a Connection on success
  141. * Returns NULL on failure
  142. */
  143. Connection *
  144. connection_table_get_connection(Connection_Table *ct, int sd)
  145. {
  146. Connection *c= NULL;
  147. int index, count;
  148. index = sd % ct->size;
  149. for( count = 0; count < ct->size; count++, index = (index + 1) % ct->size)
  150. {
  151. /* Do not use slot 0, slot 0 is head of the list of active connections */
  152. if ( index == 0 )
  153. {
  154. continue;
  155. }
  156. else if( ct->c[index].c_mutex == NULL )
  157. {
  158. break;
  159. }
  160. if( connection_is_free (&(ct->c[index])))
  161. {
  162. break;
  163. }
  164. }
  165. if ( count < ct->size )
  166. {
  167. /* Found an available Connection */
  168. c= &(ct->c[index]);
  169. PR_ASSERT(c->c_next==NULL);
  170. PR_ASSERT(c->c_prev==NULL);
  171. PR_ASSERT(c->c_extension==NULL);
  172. if ( c->c_mutex == NULL )
  173. {
  174. PR_Lock( ct->table_mutex );
  175. c->c_mutex = PR_NewLock();
  176. c->c_pdumutex = PR_NewLock();
  177. PR_Unlock( ct->table_mutex );
  178. if ( c->c_mutex == NULL || c->c_pdumutex == NULL )
  179. {
  180. c->c_mutex = NULL;
  181. c->c_pdumutex = NULL;
  182. LDAPDebug( LDAP_DEBUG_ANY,"PR_NewLock failed\n",0, 0, 0 );
  183. c= NULL;
  184. }
  185. }
  186. /* Let's make sure there's no cruft left on there from the last time this connection was used. */
  187. /* Note: no need to lock c->c_mutex because this function is only
  188. * called by one thread (the slapd_daemon thread), and if we got this
  189. * far then `c' is not being used by any operation threads, etc.
  190. */
  191. connection_cleanup(c);
  192. }
  193. else
  194. {
  195. /* couldn't find a Connection */
  196. LDAPDebug( LDAP_DEBUG_CONNS, "max open connections reached\n", 0, 0, 0);
  197. }
  198. return c;
  199. }
  200. /* active connection iteration functions */
  201. Connection*
  202. connection_table_get_first_active_connection (Connection_Table *ct)
  203. {
  204. return ct->c[0].c_next;
  205. }
  206. Connection*
  207. connection_table_get_next_active_connection (Connection_Table *ct, Connection *c)
  208. {
  209. return c->c_next;
  210. }
  211. int connection_table_iterate_active_connections(Connection_Table *ct, void* arg, Connection_Table_Iterate_Function f)
  212. {
  213. /* Locking in this area seems rather undeveloped, I think because typically only one
  214. * thread accesses the connection table (daemon thread). But now we allow other threads
  215. * to iterate over the table. So we use the "new mutex mutex" to lock the table.
  216. */
  217. Connection *current_conn = NULL;
  218. int ret = 0;
  219. PR_Lock(ct->table_mutex);
  220. current_conn = connection_table_get_first_active_connection (ct);
  221. while (current_conn) {
  222. ret = f(current_conn, arg);
  223. if (ret) {
  224. break;
  225. }
  226. current_conn = connection_table_get_next_active_connection (ct, current_conn);
  227. }
  228. PR_Unlock(ct->table_mutex);
  229. return ret;
  230. }
  231. #ifdef FOR_DEBUGGING
  232. static void
  233. connection_table_dump_active_connection (Connection *c)
  234. {
  235. slapi_log_error(SLAPI_LOG_FATAL, "connection", "conn=%p fd=%d refcnt=%d c_flags=%d\n"
  236. "mutex=%p next=%p prev=%p\n\n", c, c->c_sd, c->c_refcnt, c->c_flags,
  237. c->c_mutex, c->c_next, c->c_prev);
  238. }
  239. static void
  240. connection_table_dump_active_connections (Connection_Table *ct)
  241. {
  242. Connection* c;
  243. PR_Lock(ct->table_mutex);
  244. slapi_log_error(SLAPI_LOG_FATAL, "connection", "********** BEGIN DUMP ************\n");
  245. c = connection_table_get_first_active_connection (ct);
  246. while (c)
  247. {
  248. connection_table_dump_active_connection (c);
  249. c = connection_table_get_next_active_connection (ct, c);
  250. }
  251. slapi_log_error(SLAPI_LOG_FATAL, "connection", "********** END DUMP ************\n");
  252. PR_Unlock(ct->table_mutex);
  253. }
  254. #endif
  255. /*
  256. * There's a double linked list of active connections running through the array
  257. * of connections. This function removes a connection (by index) from that
  258. * list. This list is used to find the connections that should be used in the
  259. * poll call.
  260. */
  261. void
  262. connection_table_move_connection_out_of_active_list(Connection_Table *ct,Connection *c)
  263. {
  264. /* we always have previous element because list contains a dummy header */;
  265. PR_ASSERT (c->c_prev);
  266. #ifdef FOR_DEBUGGING
  267. slapi_log_error(SLAPI_LOG_FATAL, "connection", "Moving connection out of active list\n");
  268. connection_table_dump_active_connection (c);
  269. #endif
  270. /*
  271. * Note: the connection will NOT be moved off the active list if any other threads still hold
  272. * a reference to the connection (that is, its reference count must be 1 or less).
  273. */
  274. if(c->c_refcnt > 1) return;
  275. /* We need to lock here because we're modifying the linked list */
  276. PR_Lock(ct->table_mutex);
  277. c->c_prev->c_next = c->c_next;
  278. if (c->c_next)
  279. {
  280. c->c_next->c_prev = c->c_prev;
  281. }
  282. connection_release_nolock (c);
  283. connection_cleanup (c);
  284. PR_Unlock(ct->table_mutex);
  285. #ifdef FOR_DEBUGGING
  286. connection_table_dump_active_connections (ct);
  287. #endif
  288. }
  289. /*
  290. * There's a double linked list of active connections running through the array
  291. * of connections. This function adds a connection (by index) to the head of
  292. * that list. This list is used to find the connections that should be used in the
  293. * poll call.
  294. */
  295. void
  296. connection_table_move_connection_on_to_active_list(Connection_Table *ct,Connection *c)
  297. {
  298. PR_ASSERT(c->c_next==NULL);
  299. PR_ASSERT(c->c_prev==NULL);
  300. PR_Lock(ct->table_mutex);
  301. connection_acquire_nolock (c);
  302. #ifdef FOR_DEBUGGING
  303. slapi_log_error(SLAPI_LOG_FATAL, "connection", "Moving connection into active list\n");
  304. connection_table_dump_active_connection (c);
  305. #endif
  306. c->c_next = ct->c[0].c_next;
  307. if ( c->c_next != NULL )
  308. {
  309. c->c_next->c_prev = c;
  310. }
  311. c->c_prev = &(ct->c[0]);
  312. ct->c[0].c_next = c;
  313. PR_Unlock(ct->table_mutex);
  314. #ifdef FOR_DEBUGGING
  315. connection_table_dump_active_connections (ct);
  316. #endif
  317. }
  318. /*
  319. * Replace the following attributes within the entry 'e' with
  320. * information about the connection table:
  321. * connection // multivalued; one value for each active connection
  322. * currentconnections // single valued; an integer count
  323. * totalconnections // single valued; an integer count
  324. * dtablesize // single valued; an integer size
  325. * readwaiters // single valued; an integer count
  326. */
  327. void
  328. connection_table_as_entry(Connection_Table *ct, Slapi_Entry *e)
  329. {
  330. char buf[BUFSIZ];
  331. struct berval val;
  332. struct berval *vals[2];
  333. int i, nconns, nreadwaiters;
  334. struct tm utm;
  335. vals[0] = &val;
  336. vals[1] = NULL;
  337. attrlist_delete( &e->e_attrs, "connection");
  338. nconns = 0;
  339. nreadwaiters = 0;
  340. for ( i = 0; i < (ct!=NULL?ct->size:0); i++ )
  341. {
  342. PR_Lock( ct->table_mutex );
  343. if ( (ct->c[i].c_mutex == NULL) || (ct->c[i].c_mutex == (PRLock*)-1) )
  344. {
  345. PR_Unlock( ct->table_mutex );
  346. continue;
  347. }
  348. /* Can't take c_mutex if holding table_mutex; temporarily unlock */
  349. PR_Unlock( ct->table_mutex );
  350. PR_Lock( ct->c[i].c_mutex );
  351. if ( ct->c[i].c_sd != SLAPD_INVALID_SOCKET )
  352. {
  353. char buf2[20];
  354. int lendn = ct->c[i].c_dn ? strlen(ct->c[i].c_dn) : 6; /* "NULLDN" */
  355. char *bufptr = &buf[0];
  356. char *newbuf = NULL;
  357. nconns++;
  358. if ( ct->c[i].c_gettingber )
  359. {
  360. nreadwaiters++;
  361. }
  362. #ifdef _WIN32
  363. {
  364. struct tm *pt;
  365. pt = gmtime( &ct->c[i].c_starttime );
  366. memcpy(&utm, pt, sizeof(struct tm) );
  367. }
  368. #else
  369. gmtime_r( &ct->c[i].c_starttime, &utm );
  370. #endif
  371. strftime( buf2, sizeof(buf2), "%Y%m%d%H%M%SZ", &utm );
  372. if (lendn > (BUFSIZ - 46)) {
  373. /*
  374. * 46 = 4 for the "i" couter + 20 for buf2 +
  375. * 10 for c_opsinitiated + 10 for c_opscompleted +
  376. * 1 for c_gettingber + 1
  377. */
  378. newbuf = (char *) slapi_ch_malloc(lendn + 46);
  379. bufptr = newbuf;
  380. }
  381. sprintf( bufptr, "%d:%s:%d:%d:%s%s:%s", i,
  382. buf2,
  383. ct->c[i].c_opsinitiated,
  384. ct->c[i].c_opscompleted,
  385. ct->c[i].c_gettingber ? "r" : "",
  386. "",
  387. ct->c[i].c_dn ? ct->c[i].c_dn : "NULLDN" );
  388. val.bv_val = bufptr;
  389. val.bv_len = strlen( bufptr );
  390. attrlist_merge( &e->e_attrs, "connection", vals );
  391. if (newbuf) {
  392. slapi_ch_free_string(&newbuf);
  393. }
  394. }
  395. PR_Unlock( ct->c[i].c_mutex );
  396. }
  397. sprintf( buf, "%d", nconns );
  398. val.bv_val = buf;
  399. val.bv_len = strlen( buf );
  400. attrlist_replace( &e->e_attrs, "currentconnections", vals );
  401. PR_Lock( num_conns_mutex );
  402. sprintf( buf, "%d", num_conns );
  403. PR_Unlock( num_conns_mutex );
  404. val.bv_val = buf;
  405. val.bv_len = strlen( buf );
  406. attrlist_replace( &e->e_attrs, "totalconnections", vals );
  407. sprintf( buf, "%d", (ct!=NULL?ct->size:0) );
  408. val.bv_val = buf;
  409. val.bv_len = strlen( buf );
  410. attrlist_replace( &e->e_attrs, "dtablesize", vals );
  411. sprintf( buf, "%d", nreadwaiters );
  412. val.bv_val = buf;
  413. val.bv_len = strlen( buf );
  414. attrlist_replace( &e->e_attrs, "readwaiters", vals );
  415. }
  416. void
  417. connection_table_dump_activity_to_errors_log(Connection_Table *ct)
  418. {
  419. int i;
  420. for ( i = 0; i < ct->size; i++ )
  421. {
  422. Connection *c= &(ct->c[i]);
  423. if ( c->c_mutex != NULL )
  424. {
  425. /* Find the connection we are referring to */
  426. int j= c->c_fdi;
  427. PR_Lock( c->c_mutex );
  428. if ( c->c_sd != SLAPD_INVALID_SOCKET && c->c_prfd == ct->fd[j].fd )
  429. {
  430. int r = ct->fd[j].out_flags & SLAPD_POLL_FLAGS;
  431. if ( r )
  432. {
  433. LDAPDebug( LDAP_DEBUG_CONNS,"activity on %d%s\n", i, r ? "r" : "",0 );
  434. }
  435. }
  436. PR_Unlock( c->c_mutex );
  437. }
  438. }
  439. }
  440. #if 0
  441. void dump_op_list(FILE *file, Operation *op);
  442. void
  443. connection_table_dump(Connection_Table *ct)
  444. {
  445. FILE *file;
  446. file = fopen("/tmp/slapd.conn", "a+");
  447. if (file != NULL)
  448. {
  449. int i;
  450. fprintf(file, "=============pid=%d==================\n", getpid());
  451. for ( i = 0; i < ct->size; i++ )
  452. {
  453. if ( (ct->c[i].c_sd == SLAPD_INVALID_SOCKET) && (ct->c[i].c_connid == 0) )
  454. {
  455. continue;
  456. }
  457. fprintf(file, "c[%d].c_dn=0x%x\n", i, ct->c[i].c_dn);
  458. dump_op_list(file, ct->c[i].c_ops);
  459. fprintf(file, "c[%d].c_sb.sb_sd=%d\n", i, ct->c[i].c_sd);
  460. fprintf(file, "c[%d].c_connid=%d\n", i, ct->c[i].c_connid);
  461. fprintf(file, "c[%d].c_opsinitiated=%d\n", i, ct->c[i].c_opsinitiated);
  462. fprintf(file, "c[%d].c_opscompleted=%d\n", i, ct->c[i].c_opscompleted);
  463. }
  464. fclose(file);
  465. }
  466. }
  467. static const char *
  468. op_status2str( int o_status )
  469. {
  470. const char *s = "unknown";
  471. switch( o_status ) {
  472. case SLAPI_OP_STATUS_PROCESSING:
  473. s = "processing";
  474. break;
  475. case SLAPI_OP_STATUS_ABANDONED:
  476. s = "abandoned";
  477. break;
  478. case SLAPI_OP_STATUS_WILL_COMPLETE:
  479. s = "will_complete";
  480. break;
  481. case SLAPI_OP_STATUS_RESULT_SENT:
  482. s = "result_sent";
  483. break;
  484. }
  485. return s;
  486. }
  487. void
  488. dump_op_list(FILE *file, Operation *op)
  489. {
  490. Operation *tmp;
  491. for ( tmp = op; tmp != NULL; tmp = tmp->o_next )
  492. {
  493. fprintf(file,
  494. "(o_msgid=%d, o_tag=%d, o_sdn=0x%x, o_opid=%d, o_connid=%d, o_status=%s)\n",
  495. tmp->o_msgid, tmp->o_tag, slapi_sdn_get_dn(&tmp->o_sdn), tmp->o_connid,
  496. *tmp->o_tid, op_status2str( tmp->o_status ));
  497. }
  498. }
  499. #endif /* 0 */