3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1993 The Regents of the University of Michigan.
10 * cache.c - local caching support for LDAP
17 #include <ac/stdlib.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
27 static int cache_hash LDAP_P(( BerElement *ber ));
28 static LDAPMessage *msg_dup LDAP_P(( LDAPMessage *msg ));
29 static int request_cmp LDAP_P(( BerElement *req1, BerElement *req2 ));
30 static int chain_contains_dn LDAP_P(( LDAPMessage *msg, LDAP_CONST char *dn ));
31 static ber_len_t msg_size LDAP_P(( LDAPMessage *msg ));
32 static void check_cache_memused LDAP_P(( LDAPCache *lc ));
33 static void uncache_entry_or_req LDAP_P(( LDAP *ld, LDAP_CONST char *dn, ber_int_t msgid ));
38 ldap_enable_cache( LDAP *ld, long timeout, ber_len_t maxmem )
41 assert( LDAP_VALID( ld ) );
44 if ( ld->ld_cache == NULL ) {
45 if (( ld->ld_cache = (LDAPCache *)LDAP_MALLOC( sizeof( LDAPCache )))
47 ld->ld_errno = LDAP_NO_MEMORY;
50 (void) memset( ld->ld_cache, '\0', sizeof( LDAPCache ));
51 ld->ld_cache->lc_memused = sizeof( LDAPCache );
54 ld->ld_cache->lc_timeout = timeout;
55 ld->ld_cache->lc_maxmem = maxmem;
56 check_cache_memused( ld->ld_cache );
57 ld->ld_cache->lc_enabled = 1;
66 ldap_disable_cache( LDAP *ld )
69 assert( LDAP_VALID( ld ) );
72 if ( ld->ld_cache != NULL ) {
73 ld->ld_cache->lc_enabled = 0;
81 ldap_set_cache_options( LDAP *ld, unsigned long opts )
84 assert( LDAP_VALID( ld ) );
87 if ( ld->ld_cache != NULL ) {
88 ld->ld_cache->lc_options = opts;
95 ldap_destroy_cache( LDAP *ld )
98 assert( LDAP_VALID( ld ) );
101 if ( ld->ld_cache != NULL ) {
102 ldap_flush_cache( ld );
103 LDAP_FREE( (char *)ld->ld_cache );
111 ldap_flush_cache( LDAP *ld )
115 LDAPMessage *m, *next;
117 assert( ld != NULL );
118 assert( LDAP_VALID( ld ) );
121 LDAP_LOG (( "cache", LDAP_LEVEL_ENTRY, "ldap_flush_cache\n" ));
123 Debug( LDAP_DEBUG_TRACE, "ldap_flush_cache\n", 0, 0, 0 );
126 if ( ld->ld_cache != NULL ) {
127 /* delete all requests in the queue */
128 for ( m = ld->ld_cache->lc_requests; m != NULL; m = next ) {
132 ld->ld_cache->lc_requests = NULL;
134 /* delete all messages in the cache */
135 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
136 for ( m = ld->ld_cache->lc_buckets[ i ];
137 m != NULL; m = next ) {
141 ld->ld_cache->lc_buckets[ i ] = NULL;
143 ld->ld_cache->lc_memused = sizeof( LDAPCache );
150 ldap_uncache_request( LDAP *ld, int msgid )
152 assert( ld != NULL );
153 assert( LDAP_VALID( ld ) );
157 LDAP_LOG (( "cache", LDAP_LEVEL_ARGS,
158 "ldap_uncache_request %d ld_cache %lx\n",
159 msgid, (long) ld->ld_cache ));
161 Debug( LDAP_DEBUG_TRACE, "ldap_uncache_request %d ld_cache %lx\n",
162 msgid, (long) ld->ld_cache, 0 );
165 uncache_entry_or_req( ld, NULL, msgid );
171 ldap_uncache_entry( LDAP *ld, LDAP_CONST char *dn )
173 assert( ld != NULL );
174 assert( LDAP_VALID( ld ) );
175 assert( dn != NULL );
179 LDAP_LOG (( "cache", LDAP_LEVEL_ARGS,
180 "ldap_uncache_entry %s ld_cache %lx\n",
181 dn, (long) ld->ld_cache ));
183 Debug( LDAP_DEBUG_TRACE, "ldap_uncache_entry %s ld_cache %lx\n",
184 dn, (long) ld->ld_cache, 0 );
187 uncache_entry_or_req( ld, dn, 0 );
195 uncache_entry_or_req( LDAP *ld,
196 const char *dn, /* if non-NULL, uncache entry */
197 ber_int_t msgid ) /* request to uncache (if dn == NULL) */
200 LDAPMessage *m, *prev, *next;
203 LDAP_LOG (( "cache", LDAP_LEVEL_ARGS,
204 "uncache_entry_or_req dn %s msgid %ld ld_cache %lx\n",
205 dn, (long) msgid, (long) ld->ld_cache ));
207 Debug( LDAP_DEBUG_TRACE,
208 "ldap_uncache_entry_or_req dn %s msgid %ld ld_cache %lx\n",
209 dn, (long) msgid, (long) ld->ld_cache );
212 if ( ld->ld_cache == NULL ) {
216 /* first check the request queue */
218 for ( m = ld->ld_cache->lc_requests; m != NULL; m = next ) {
220 if (( dn != NULL && chain_contains_dn( m, dn )) ||
221 ( dn == NULL && m->lm_msgid == msgid )) {
222 if ( prev == NULL ) {
223 ld->ld_cache->lc_requests = next;
225 prev->lm_next = next;
227 ld->ld_cache->lc_memused -= msg_size( m );
234 /* now check the rest of the cache */
235 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
237 for ( m = ld->ld_cache->lc_buckets[ i ]; m != NULL;
240 if (( dn != NULL && chain_contains_dn( m, dn )) ||
241 ( dn == NULL && m->lm_msgid == msgid )) {
242 if ( prev == NULL ) {
243 ld->ld_cache->lc_buckets[ i ] = next;
245 prev->lm_next = next;
247 ld->ld_cache->lc_memused -= msg_size( m );
259 ldap_add_request_to_cache( LDAP *ld, ber_tag_t msgtype, BerElement *request )
265 assert( ld != NULL );
266 assert( LDAP_VALID( ld ) );
269 LDAP_LOG (( "cache", LDAP_LEVEL_ENTRY, "ldap_add_request_to_cache\n" ));
271 Debug( LDAP_DEBUG_TRACE, "ldap_add_request_to_cache\n", 0, 0, 0 );
274 ld->ld_errno = LDAP_SUCCESS;
275 if ( ld->ld_cache == NULL ||
276 ( ld->ld_cache->lc_enabled == 0 )) {
280 if (( new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
282 if (( new->lm_ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
283 LDAP_FREE( (char *)new );
286 len = request->ber_ptr - request->ber_buf;
287 if (( new->lm_ber->ber_buf = (char *) ber_memalloc( (size_t)len ))
289 ber_free( new->lm_ber, 0 );
290 LDAP_FREE( (char *)new );
291 ld->ld_errno = LDAP_NO_MEMORY;
294 AC_MEMCPY( new->lm_ber->ber_buf, request->ber_buf,
296 new->lm_ber->ber_ptr = new->lm_ber->ber_buf;
297 new->lm_ber->ber_end = new->lm_ber->ber_buf + len;
298 new->lm_msgid = ld->ld_msgid;
299 new->lm_msgtype = msgtype;;
300 new->lm_next = ld->ld_cache->lc_requests;
301 ld->ld_cache->lc_requests = new;
303 ld->ld_errno = LDAP_NO_MEMORY;
310 ldap_add_result_to_cache( LDAP *ld, LDAPMessage *result )
313 LDAPMessage *m, **mp, *req, *new, *prev;
316 assert( ld != NULL );
317 assert( LDAP_VALID( ld ) );
318 assert( result != NULL );
321 LDAP_LOG (( "cache", LDAP_LEVEL_ARGS,
322 "ldap_add_result_to_cache: id %ld, type %ld\n",
323 (long) result->lm_msgid, (long) result->lm_msgtype ));
325 Debug( LDAP_DEBUG_TRACE, "ldap_add_result_to_cache: id %ld, type %ld\n",
326 (long) result->lm_msgid, (long) result->lm_msgtype, 0 );
329 if ( ld->ld_cache == NULL ||
330 ( ld->ld_cache->lc_enabled == 0 )) {
332 LDAP_LOG (( "cache", LDAP_LEVEL_DETAIL1,
333 "ldap_add_result_to_cache: cache disabled\n" ));
335 Debug( LDAP_DEBUG_TRACE, "artc: cache disabled\n", 0, 0, 0 );
340 if ( result->lm_msgtype != LDAP_RES_SEARCH_ENTRY &&
341 result->lm_msgtype != LDAP_RES_SEARCH_REFERENCE &&
342 result->lm_msgtype != LDAP_RES_SEARCH_RESULT &&
343 result->lm_msgtype != LDAP_RES_COMPARE ) {
345 * only cache search and compare operations
348 LDAP_LOG (( "cache", LDAP_LEVEL_DETAIL1,
349 "ldap_add_result_to_cache: "
350 "only caching search & compare operations\n" ));
352 Debug( LDAP_DEBUG_TRACE,
353 "artc: only caching search & compare operations\n", 0, 0, 0 );
359 * if corresponding request is in the lc_requests list, add this
360 * result to it. if this result completes the results for the
361 * request, add the request/result chain to the cache proper.
364 for ( m = ld->ld_cache->lc_requests; m != NULL; m = m->lm_next ) {
365 if ( m->lm_msgid == result->lm_msgid ) {
371 if ( m != NULL ) { /* found request; add to end of chain */
373 for ( ; m->lm_chain != NULL; m = m->lm_chain )
375 if (( new = msg_dup( result )) != NULL ) {
376 new->lm_chain = NULL;
379 LDAP_LOG (( "cache", LDAP_LEVEL_RESULTS,
380 "ldap_add_result_to_cache: "
381 "result added to cache request chain\n" ));
383 Debug( LDAP_DEBUG_TRACE,
384 "artc: result added to cache request chain\n",
388 if ( result->lm_msgtype == LDAP_RES_SEARCH_RESULT ||
389 result->lm_msgtype == LDAP_RES_COMPARE ) {
391 * this result completes the chain of results
392 * add to cache proper if appropriate
394 keep = 0; /* pessimistic */
395 err = ldap_result2error( ld, result, 0 );
396 if ( err == LDAP_SUCCESS ||
397 ( result->lm_msgtype == LDAP_RES_COMPARE &&
398 ( err == LDAP_COMPARE_FALSE ||
399 err == LDAP_COMPARE_TRUE ||
400 err == LDAP_NO_SUCH_ATTRIBUTE ))) {
404 if ( ld->ld_cache->lc_options == 0 ) {
405 if ( err == LDAP_SIZELIMIT_EXCEEDED ) {
408 } else if (( ld->ld_cache->lc_options &
409 LDAP_CACHE_OPT_CACHEALLERRS ) != 0 ) {
413 if ( prev == NULL ) {
414 ld->ld_cache->lc_requests = req->lm_next;
416 prev->lm_next = req->lm_next;
421 LDAP_LOG (( "cache", LDAP_LEVEL_RESULTS,
422 "ldap_add_result_to_cache: "
423 "not caching result with error\n" ));
425 Debug( LDAP_DEBUG_TRACE,
426 "artc: not caching result with error %d\n",
431 mp = &ld->ld_cache->lc_buckets[
432 cache_hash( req->lm_ber ) ];
435 req->lm_time = (long) time( NULL );
436 ld->ld_cache->lc_memused += msg_size( req );
437 check_cache_memused( ld->ld_cache );
439 LDAP_LOG (( "cache", LDAP_LEVEL_RESULTS,
440 "ldap_add_result_to_cache: "
441 "cached result with error\n" ));
443 Debug( LDAP_DEBUG_TRACE,
444 "artc: cached result with error %d\n",
451 LDAP_LOG (( "cache", LDAP_LEVEL_RESULTS,
452 "ldap_add_result_to_cache: "
453 "msgid not in request list\n" ));
455 Debug( LDAP_DEBUG_TRACE, "artc: msgid not in request list\n",
464 * look in the cache for this request
465 * return 0 if found, -1 if not
466 * if found, the corresponding result messages are added to the incoming
467 * queue with the correct (new) msgid so that subsequent ldap_result calls
471 ldap_check_cache( LDAP *ld, ber_tag_t msgtype, BerElement *request )
474 LDAPMessage *m, *new, *prev, *next;
479 assert( ld != NULL );
480 assert( LDAP_VALID( ld ) );
481 assert( request != NULL );
484 LDAP_LOG (( "cache", LDAP_LEVEL_ENTRY, "ldap_check_cache\n" ));
486 Debug( LDAP_DEBUG_TRACE, "ldap_check_cache\n", 0, 0, 0 );
489 if ( ld->ld_cache == NULL ||
490 ( ld->ld_cache->lc_enabled == 0 )) {
494 memset( &reqber, '\0', sizeof(reqber) );
495 reqber.ber_valid = LBER_VALID_BERELEMENT;
496 reqber.ber_buf = reqber.ber_ptr = request->ber_buf;
497 reqber.ber_end = request->ber_ptr;
498 reqber.ber_debug = ber_int_debug;
500 c_time = time( NULL );
503 hash = cache_hash( &reqber );
504 for ( m = ld->ld_cache->lc_buckets[ hash ]; m != NULL; m = next ) {
506 LDAP_LOG (( "cache", LDAP_LEVEL_DETAIL1,
507 "ldap_check_cache: examining id %ld, type %ld\n",
508 (long) m->lm_msgid, (long) m->lm_msgtype ));
510 Debug( LDAP_DEBUG_TRACE,"cc: examining id %ld,type %ld\n",
511 (long) m->lm_msgid, (long) m->lm_msgtype, 0 );
513 if ( difftime(c_time, m->lm_time) > ld->ld_cache->lc_timeout ) {
514 /* delete expired message */
516 if ( prev == NULL ) {
517 ld->ld_cache->lc_buckets[ hash ] = next;
519 prev->lm_next = next;
522 LDAP_LOG (( "cache", LDAP_LEVEL_DETAIL1,
523 "ldap_check_cache: expired id %ld\n",
524 (long) m->lm_msgid ));
526 Debug( LDAP_DEBUG_TRACE, "cc: expired id %d\n",
529 ld->ld_cache->lc_memused -= msg_size( m );
532 if ( m->lm_msgtype == msgtype &&
533 request_cmp( m->lm_ber, &reqber ) == 0 ) {
546 * add duplicates of responses to incoming queue
549 for ( m = m->lm_chain; m != NULL; m = m->lm_chain ) {
550 if (( new = msg_dup( m )) == NULL ) {
554 new->lm_msgid = ld->ld_msgid;
555 new->lm_chain = NULL;
557 new->lm_next = ld->ld_responses;
558 ld->ld_responses = new;
561 prev->lm_chain = new;
565 LDAP_LOG (( "cache", LDAP_LEVEL_DETAIL1,
566 "ldap_check_cache: added type %ld\n",
567 (long) m->lm_msgtype ));
569 Debug( LDAP_DEBUG_TRACE, "cc: added type %ld\n",
570 (long) new->lm_msgtype, 0, 0 );
575 LDAP_LOG (( "cache", LDAP_LEVEL_RESULTS,
576 "ldap_check_cache: result returned from cache\n" ));
578 Debug( LDAP_DEBUG_TRACE, "cc: result returned from cache\n", 0, 0, 0 );
589 cache_hash( BerElement *ber )
595 * just take the length of the packet and mod with # of buckets
598 if ( ber_skip_tag( &bercpy, &len ) == LBER_ERROR
599 || ber_scanf( &bercpy, "x" ) == LBER_ERROR ) {
600 len = 0; /* punt: just return zero */
602 len = bercpy.ber_end - bercpy.ber_ptr;
606 LDAP_LOG (( "cache", LDAP_LEVEL_RESULTS,
607 "cache_hash: len is %ld, returning %ld\n",
608 len, len % LDAP_CACHE_BUCKETS ));
610 Debug( LDAP_DEBUG_TRACE, "cache_hash: len is %ld, returning %ld\n",
611 len, len % LDAP_CACHE_BUCKETS, 0 );
613 return( (int) ( len % LDAP_CACHE_BUCKETS ));
618 msg_dup( LDAPMessage *msg )
623 if (( new = (LDAPMessage *)LDAP_MALLOC( sizeof(LDAPMessage))) != NULL ) {
624 *new = *msg; /* struct copy */
625 if (( new->lm_ber = ber_dup( msg->lm_ber )) == NULL ) {
626 LDAP_FREE( (char *)new );
629 len = msg->lm_ber->ber_end - msg->lm_ber->ber_buf;
630 if (( new->lm_ber->ber_buf = (char *) ber_memalloc(
631 (size_t)len )) == NULL ) {
632 ber_free( new->lm_ber, 0 );
633 LDAP_FREE( (char *)new );
636 AC_MEMCPY( new->lm_ber->ber_buf, msg->lm_ber->ber_buf,
638 new->lm_ber->ber_ptr = new->lm_ber->ber_buf +
639 ( msg->lm_ber->ber_ptr - msg->lm_ber->ber_buf );
640 new->lm_ber->ber_end = new->lm_ber->ber_buf + len;
648 request_cmp( BerElement *req1, BerElement *req2 )
653 r1 = *req1; /* struct copies */
657 * skip the enclosing tags (sequence markers) and the msg ids
659 if ( ber_skip_tag( &r1, &len ) == LBER_ERROR || ber_scanf( &r1, "x" )
663 if ( ber_skip_tag( &r2, &len ) == LBER_ERROR || ber_scanf( &r2, "x" )
669 * check remaining length and bytes if necessary
671 if (( len = r1.ber_end - r1.ber_ptr ) !=
672 (ber_len_t) (r2.ber_end - r2.ber_ptr) )
674 return( -1 ); /* different lengths */
676 return( memcmp( r1.ber_ptr, r2.ber_ptr, (size_t)len ));
681 chain_contains_dn( LDAPMessage *msg, const char *dn )
691 * first check the base or dn of the request
693 ber = *msg->lm_ber; /* struct copy */
694 if ( ber_scanf( &ber, "{i{a" /*}}*/, &msgid, &s ) != LBER_ERROR ) {
695 rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
702 if ( msg->lm_msgtype == LDAP_REQ_COMPARE ) {
707 * now check the dn of each search result
710 for ( m = msg->lm_chain; m != NULL && rc == 0 ; m = m->lm_chain ) {
711 if ( m->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
714 ber = *m->lm_ber; /* struct copy */
715 if ( ber_scanf( &ber, "{a" /*}*/, &s ) != LBER_ERROR ) {
716 rc = ( strcasecmp( dn, s ) == 0 ) ? 1 : 0;
726 msg_size( LDAPMessage *msg )
732 for ( m = msg; m != NULL; m = m->lm_chain ) {
733 size += ( sizeof( LDAPMessage ) + m->lm_ber->ber_end -
734 m->lm_ber->ber_buf );
741 #define THRESHOLD_FACTOR 3 / 4
742 #define SIZE_FACTOR 2 / 3
745 check_cache_memused( LDAPCache *lc )
748 * this routine is called to check if the cache is too big (lc_maxmem >
749 * minimum cache size and lc_memused > lc_maxmem). If too big, it reduces
750 * the cache size to < SIZE_FACTOR * lc_maxmem. The algorithm is as follows:
751 * remove_threshold = lc_timeout seconds;
753 * remove everything older than remove_threshold seconds;
754 * remove_threshold = remove_threshold * THRESHOLD_FACTOR;
755 * } while ( cache size is > SIZE_FACTOR * lc_maxmem )
758 unsigned long remove_threshold;
760 LDAPMessage *m, *prev, *next;
763 LDAP_LOG (( "cache", LDAP_LEVEL_DETAIL1,
764 "check_cache_memused: %ld bytes in use (%ld max)\n",
765 lc->lc_memused, lc->lc_maxmem ));
767 Debug( LDAP_DEBUG_TRACE, "check_cache_memused: %ld bytes in use (%ld max)\n",
768 lc->lc_memused, lc->lc_maxmem, 0 );
771 if ( (unsigned) lc->lc_maxmem <= sizeof( LDAPCache )
772 || lc->lc_memused <= lc->lc_maxmem * SIZE_FACTOR ) {
776 remove_threshold = lc->lc_timeout;
777 while ( lc->lc_memused > lc->lc_maxmem * SIZE_FACTOR ) {
778 c_time = time( NULL );
779 for ( i = 0; i < LDAP_CACHE_BUCKETS; ++i ) {
781 for ( m = lc->lc_buckets[ i ]; m != NULL;
784 if ( difftime(c_time, m->lm_time) > remove_threshold) {
785 if ( prev == NULL ) {
786 lc->lc_buckets[ i ] = next;
788 prev->lm_next = next;
790 lc->lc_memused -= msg_size( m );
792 LDAP_LOG (( "cache", LDAP_LEVEL_DETAIL1,
793 "check_cache_memused: removed %ld\n",
796 Debug( LDAP_DEBUG_TRACE,
806 remove_threshold *= THRESHOLD_FACTOR;
810 LDAP_LOG (( "cache", LDAP_LEVEL_DETAIL1,
811 "check_cache_memused: reduced usage to %ld bytes\n",
814 Debug( LDAP_DEBUG_TRACE, "ccm: reduced usage to %ld bytes\n",
815 lc->lc_memused, 0, 0 );
819 #endif /* !NO_CACHE */