xref: /illumos-gate/usr/src/lib/libslp/clib/slp_da_cache.c (revision 48d1bcbbf436904298ce3191590b67834ac47dea)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This module contains a cache used to optimized scope and DA
297c478bd9Sstevel@tonic-gate  * discovery. Entries live for a short duration only (about 10 seconds),
307c478bd9Sstevel@tonic-gate  * although their lifetime can be advanced somewhat by frequent use.
317c478bd9Sstevel@tonic-gate  * The intent is that the canonical source for DAs will always be slpd,
327c478bd9Sstevel@tonic-gate  * so the short lifetime of cache entries is designed to force clients
337c478bd9Sstevel@tonic-gate  * to consult slpd frequently so as to pick up the latest DA state
347c478bd9Sstevel@tonic-gate  * quickly.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * The cache is managed by a thread which monitors calls into the cache.
377c478bd9Sstevel@tonic-gate  * If the cache has been unused for a certain amount of time, the thread
387c478bd9Sstevel@tonic-gate  * frees the cache and exits.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * The cache is keyed on the queries sent to slpd to access slpd's DA
417c478bd9Sstevel@tonic-gate  * table. Associated with each query is a reply (in the format of an
427c478bd9Sstevel@tonic-gate  * on-the-wire SLP SRVRPLY message).
437c478bd9Sstevel@tonic-gate  * The cache is accessed by the following two functions:
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * slp_find_das_cached:		searches the cache
467c478bd9Sstevel@tonic-gate  * slp_put_das_cached:		adds a reply to the cache
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * All parameters added to the cache are copied in first, and all results
497c478bd9Sstevel@tonic-gate  * read from the cache are copied out, so all memory must be freed by
507c478bd9Sstevel@tonic-gate  * the caller.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <stdio.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <thread.h>
567c478bd9Sstevel@tonic-gate #include <synch.h>
577c478bd9Sstevel@tonic-gate #include <syslog.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate #include <sys/types.h>
607c478bd9Sstevel@tonic-gate #include <time.h>
617c478bd9Sstevel@tonic-gate #include <errno.h>
627c478bd9Sstevel@tonic-gate #include <slp-internal.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* These constants control the behaviour of the cache */
657c478bd9Sstevel@tonic-gate #define	MAX_LIFETIME	25	/* max lifetime, in seconds */
667c478bd9Sstevel@tonic-gate #define	ADVANCE_PER_USE	5	/* seconds lifetime is extended on each use */
677c478bd9Sstevel@tonic-gate #define	INIT_LIFETIME	10	/* cache entries start with this lifetime */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /* Management thread components */
707c478bd9Sstevel@tonic-gate #define	IDLE_TIMEOUT	30	/* thread will exit after this idle time */
717c478bd9Sstevel@tonic-gate static int cache_thr_running;
727c478bd9Sstevel@tonic-gate static mutex_t start_lock = DEFAULTMUTEX;
737c478bd9Sstevel@tonic-gate static int cache_called;
747c478bd9Sstevel@tonic-gate static cond_t cache_called_cond;
757c478bd9Sstevel@tonic-gate static mutex_t cache_called_lock = DEFAULTMUTEX;
767c478bd9Sstevel@tonic-gate static SLPError start_cache_thr();
77*48d1bcbbSToomas Soome static void *cache_thr(void *);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* The cache and cache synchronization */
807c478bd9Sstevel@tonic-gate static void *da_cache;
817c478bd9Sstevel@tonic-gate static mutex_t cache_lock = DEFAULTMUTEX;
827c478bd9Sstevel@tonic-gate struct cache_entry {
837c478bd9Sstevel@tonic-gate 	const char *query;
847c478bd9Sstevel@tonic-gate 	const char *reply;
857c478bd9Sstevel@tonic-gate 	unsigned int reply_len;
867c478bd9Sstevel@tonic-gate 	time_t max_life;
877c478bd9Sstevel@tonic-gate 	time_t expires;
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate typedef struct cache_entry cache_entry_t;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /* cache management and searching */
927c478bd9Sstevel@tonic-gate static int compare_entries(const void *, const void *);
93*48d1bcbbSToomas Soome static void free_cache_entry(void *, VISIT, int, void *);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Searches the cache for the reply to 'query'. Returns the reply if
977c478bd9Sstevel@tonic-gate  * found, otherwise NULL.
987c478bd9Sstevel@tonic-gate  * The caller must free the result.
997c478bd9Sstevel@tonic-gate  */
slp_find_das_cached(const char * query)1007c478bd9Sstevel@tonic-gate char *slp_find_das_cached(const char *query) {
1017c478bd9Sstevel@tonic-gate 	cache_entry_t ce[1], **ans;
1027c478bd9Sstevel@tonic-gate 	char *reply = NULL;
1037c478bd9Sstevel@tonic-gate 	time_t now;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (!cache_thr_running) {
1067c478bd9Sstevel@tonic-gate 		if (start_cache_thr() != SLP_OK) {
1077c478bd9Sstevel@tonic-gate 			return (NULL);
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1127c478bd9Sstevel@tonic-gate 	ce->query = query;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	ans = slp_tfind(ce, &da_cache, compare_entries);
1157c478bd9Sstevel@tonic-gate 	if (ans) {
1167c478bd9Sstevel@tonic-gate 		now = time(NULL);
1177c478bd9Sstevel@tonic-gate 		if ((*ans)->expires < now || (*ans)->max_life < now) {
1187c478bd9Sstevel@tonic-gate 			goto done;
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		/* copy out the reply */
1227c478bd9Sstevel@tonic-gate 		if (!(reply = malloc((*ans)->reply_len))) {
1237c478bd9Sstevel@tonic-gate 			slp_err(LOG_CRIT, 0, "slp_find_das_cached",
1247c478bd9Sstevel@tonic-gate 						"out of memory");
1257c478bd9Sstevel@tonic-gate 			goto done;
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 		(void) memcpy(reply, (*ans)->reply, (*ans)->reply_len);
1287c478bd9Sstevel@tonic-gate 		(*ans)->expires += ADVANCE_PER_USE;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/* notify cache thread of call */
1327c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_called_lock);
1337c478bd9Sstevel@tonic-gate 	cache_called = 1;
1347c478bd9Sstevel@tonic-gate 	(void) cond_signal(&cache_called_cond);
1357c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_called_lock);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate done:
1387c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1397c478bd9Sstevel@tonic-gate 	return (reply);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Adds 'reply' to the cache under the index 'query'. Both parameters
1447c478bd9Sstevel@tonic-gate  * are copied in first, so the caller may free them after the call.
1457c478bd9Sstevel@tonic-gate  * 'len' is the length of 'reply' in bytes.
1467c478bd9Sstevel@tonic-gate  */
slp_put_das_cached(const char * query,const char * reply,unsigned int len)1477c478bd9Sstevel@tonic-gate void slp_put_das_cached(const char *query, const char *reply,
1487c478bd9Sstevel@tonic-gate 			unsigned int len) {
1497c478bd9Sstevel@tonic-gate 	cache_entry_t *ce, **ce2;
1507c478bd9Sstevel@tonic-gate 	time_t now;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (!cache_thr_running) {
1537c478bd9Sstevel@tonic-gate 		if (start_cache_thr() != SLP_OK) {
1547c478bd9Sstevel@tonic-gate 			return;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* create the cache entry for this reply */
1597c478bd9Sstevel@tonic-gate 	if (!(ce = malloc(sizeof (*ce)))) {
1607c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_put_das_cached", "out of memory");
1617c478bd9Sstevel@tonic-gate 		return;
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (!(ce->query = strdup(query))) {
1657c478bd9Sstevel@tonic-gate 		free(ce);
1667c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_put_das_cached", "out of memory");
1677c478bd9Sstevel@tonic-gate 		return;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (!(ce->reply = malloc(len))) {
1717c478bd9Sstevel@tonic-gate 		free((void *) (ce->query));
1727c478bd9Sstevel@tonic-gate 		free(ce);
1737c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "slp_put_das_cached", "out of memory");
1747c478bd9Sstevel@tonic-gate 		return;
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 	(void) memcpy((void *) (ce->reply), reply, len);
1777c478bd9Sstevel@tonic-gate 	ce->reply_len = len;
1787c478bd9Sstevel@tonic-gate 	now = time(NULL);
1797c478bd9Sstevel@tonic-gate 	ce->max_life = now + MAX_LIFETIME;
1807c478bd9Sstevel@tonic-gate 	ce->expires = now + INIT_LIFETIME;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* write to the cache */
1837c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_lock);
1847c478bd9Sstevel@tonic-gate 	ce2 = slp_tsearch((void *) ce, &da_cache, compare_entries);
1857c478bd9Sstevel@tonic-gate 	if (ce != *ce2) {
1867c478bd9Sstevel@tonic-gate 		/* overwrite existing entry */
1877c478bd9Sstevel@tonic-gate 		free((void *) ((*ce2)->query));
1887c478bd9Sstevel@tonic-gate 		free((void *) ((*ce2)->reply));
1897c478bd9Sstevel@tonic-gate 		free(*ce2);
1907c478bd9Sstevel@tonic-gate 		*ce2 = ce;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&cache_lock);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
compare_entries(const void * x1,const void * x2)1967c478bd9Sstevel@tonic-gate static int compare_entries(const void *x1, const void *x2) {
1977c478bd9Sstevel@tonic-gate 	cache_entry_t *e1 = (cache_entry_t *)x1;
1987c478bd9Sstevel@tonic-gate 	cache_entry_t *e2 = (cache_entry_t *)x2;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	return (strcasecmp(e1->query, e2->query));
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
203*48d1bcbbSToomas Soome static void
free_cache_entry(void * node,VISIT order,int arg __unused,void * arg1 __unused)204*48d1bcbbSToomas Soome free_cache_entry(void *node, VISIT order, int arg __unused, void *arg1 __unused)
205*48d1bcbbSToomas Soome {
2067c478bd9Sstevel@tonic-gate 	if (order == endorder || order == leaf) {
2077c478bd9Sstevel@tonic-gate 		cache_entry_t *ce = *(cache_entry_t **)node;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		free((void *) (ce->query));
2107c478bd9Sstevel@tonic-gate 		free((void *) (ce->reply));
2117c478bd9Sstevel@tonic-gate 		free(ce);
2127c478bd9Sstevel@tonic-gate 		free(node);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
start_cache_thr()2167c478bd9Sstevel@tonic-gate static SLPError start_cache_thr() {
2177c478bd9Sstevel@tonic-gate 	int terr;
2187c478bd9Sstevel@tonic-gate 	SLPError err = SLP_OK;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&start_lock);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (cache_thr_running) {
2237c478bd9Sstevel@tonic-gate 		goto start_done;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	(void) cond_init(&cache_called_cond, 0, NULL);
2277c478bd9Sstevel@tonic-gate 
228*48d1bcbbSToomas Soome 	if ((terr = thr_create(0, 0, cache_thr, NULL, 0, NULL)) != 0) {
2297c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "start_cache_thr",
2307c478bd9Sstevel@tonic-gate 			"could not start thread: %s", strerror(terr));
2317c478bd9Sstevel@tonic-gate 		err = SLP_INTERNAL_SYSTEM_ERROR;
2327c478bd9Sstevel@tonic-gate 		goto start_done;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	cache_thr_running = 1;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate start_done:
2377c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&start_lock);
2387c478bd9Sstevel@tonic-gate 	return (err);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
241*48d1bcbbSToomas Soome static void *
cache_thr(void * arg __unused)242*48d1bcbbSToomas Soome cache_thr(void *arg __unused)
243*48d1bcbbSToomas Soome {
2447c478bd9Sstevel@tonic-gate 	timestruc_t timeout;
2457c478bd9Sstevel@tonic-gate 	timeout.tv_nsec = 0;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&cache_called_lock);
2487c478bd9Sstevel@tonic-gate 	cache_called = 0;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	while (cache_called == 0) {
2517c478bd9Sstevel@tonic-gate 		int err;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 		timeout.tv_sec = IDLE_TIMEOUT;
2547c478bd9Sstevel@tonic-gate 		err = cond_reltimedwait(&cache_called_cond,
2557c478bd9Sstevel@tonic-gate 		    &cache_called_lock, &timeout);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		if (err == ETIME) {
2587c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&cache_lock);
2597c478bd9Sstevel@tonic-gate 			/* free cache */
2607c478bd9Sstevel@tonic-gate 			if (da_cache) {
261*48d1bcbbSToomas Soome 				slp_twalk(da_cache, free_cache_entry, 0, NULL);
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 			da_cache = NULL;
2647c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&cache_lock);
2657c478bd9Sstevel@tonic-gate 			cache_thr_running = 0;
2667c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&cache_called_lock);
2677c478bd9Sstevel@tonic-gate 			thr_exit(NULL);
2687c478bd9Sstevel@tonic-gate 		} else {
2697c478bd9Sstevel@tonic-gate 			cache_called = 0;
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 	}
272*48d1bcbbSToomas Soome 	return (NULL);
2737c478bd9Sstevel@tonic-gate }
274