summaryrefslogtreecommitdiff
path: root/usr/src/lib/libslp/clib/slp_da_cache.c
blob: cd3acef489cc16175a32ee1e55be049e9137c015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This module contains a cache used to optimized scope and DA
 * discovery. Entries live for a short duration only (about 10 seconds),
 * although their lifetime can be advanced somewhat by frequent use.
 * The intent is that the canonical source for DAs will always be slpd,
 * so the short lifetime of cache entries is designed to force clients
 * to consult slpd frequently so as to pick up the latest DA state
 * quickly.
 *
 * The cache is managed by a thread which monitors calls into the cache.
 * If the cache has been unused for a certain amount of time, the thread
 * frees the cache and exits.
 *
 * The cache is keyed on the queries sent to slpd to access slpd's DA
 * table. Associated with each query is a reply (in the format of an
 * on-the-wire SLP SRVRPLY message).
 * The cache is accessed by the following two functions:
 *
 * slp_find_das_cached:		searches the cache
 * slp_put_das_cached:		adds a reply to the cache
 *
 * All parameters added to the cache are copied in first, and all results
 * read from the cache are copied out, so all memory must be freed by
 * the caller.
 */

#include <stdio.h>
#include <stdlib.h>
#include <thread.h>
#include <synch.h>
#include <syslog.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <errno.h>
#include <slp-internal.h>

/* These constants control the behaviour of the cache */
#define	MAX_LIFETIME	25	/* max lifetime, in seconds */
#define	ADVANCE_PER_USE	5	/* seconds lifetime is extended on each use */
#define	INIT_LIFETIME	10	/* cache entries start with this lifetime */

/* Management thread components */
#define	IDLE_TIMEOUT	30	/* thread will exit after this idle time */
static int cache_thr_running;
static mutex_t start_lock = DEFAULTMUTEX;
static int cache_called;
static cond_t cache_called_cond;
static mutex_t cache_called_lock = DEFAULTMUTEX;
static SLPError start_cache_thr();
static void cache_thr();

/* The cache and cache synchronization */
static void *da_cache;
static mutex_t cache_lock = DEFAULTMUTEX;
struct cache_entry {
	const char *query;
	const char *reply;
	unsigned int reply_len;
	time_t max_life;
	time_t expires;
};
typedef struct cache_entry cache_entry_t;

/* cache management and searching */
static int compare_entries(const void *, const void *);
static void free_cache_entry(void *, VISIT);

/*
 * Searches the cache for the reply to 'query'. Returns the reply if
 * found, otherwise NULL.
 * The caller must free the result.
 */
char *slp_find_das_cached(const char *query) {
	cache_entry_t ce[1], **ans;
	char *reply = NULL;
	time_t now;

	if (!cache_thr_running) {
		if (start_cache_thr() != SLP_OK) {
			return (NULL);
		}
	}

	(void) mutex_lock(&cache_lock);
	ce->query = query;

	ans = slp_tfind(ce, &da_cache, compare_entries);
	if (ans) {
		now = time(NULL);
		if ((*ans)->expires < now || (*ans)->max_life < now) {
			goto done;
		}

		/* copy out the reply */
		if (!(reply = malloc((*ans)->reply_len))) {
			slp_err(LOG_CRIT, 0, "slp_find_das_cached",
						"out of memory");
			goto done;
		}
		(void) memcpy(reply, (*ans)->reply, (*ans)->reply_len);
		(*ans)->expires += ADVANCE_PER_USE;
	}

	/* notify cache thread of call */
	(void) mutex_lock(&cache_called_lock);
	cache_called = 1;
	(void) cond_signal(&cache_called_cond);
	(void) mutex_unlock(&cache_called_lock);

done:
	(void) mutex_unlock(&cache_lock);
	return (reply);
}

/*
 * Adds 'reply' to the cache under the index 'query'. Both parameters
 * are copied in first, so the caller may free them after the call.
 * 'len' is the length of 'reply' in bytes.
 */
void slp_put_das_cached(const char *query, const char *reply,
			unsigned int len) {
	cache_entry_t *ce, **ce2;
	time_t now;

	if (!cache_thr_running) {
		if (start_cache_thr() != SLP_OK) {
			return;
		}
	}

	/* create the cache entry for this reply */
	if (!(ce = malloc(sizeof (*ce)))) {
		slp_err(LOG_CRIT, 0, "slp_put_das_cached", "out of memory");
		return;
	}

	if (!(ce->query = strdup(query))) {
		free(ce);
		slp_err(LOG_CRIT, 0, "slp_put_das_cached", "out of memory");
		return;
	}

	if (!(ce->reply = malloc(len))) {
		free((void *) (ce->query));
		free(ce);
		slp_err(LOG_CRIT, 0, "slp_put_das_cached", "out of memory");
		return;
	}
	(void) memcpy((void *) (ce->reply), reply, len);
	ce->reply_len = len;
	now = time(NULL);
	ce->max_life = now + MAX_LIFETIME;
	ce->expires = now + INIT_LIFETIME;

	/* write to the cache */
	(void) mutex_lock(&cache_lock);
	ce2 = slp_tsearch((void *) ce, &da_cache, compare_entries);
	if (ce != *ce2) {
		/* overwrite existing entry */
		free((void *) ((*ce2)->query));
		free((void *) ((*ce2)->reply));
		free(*ce2);
		*ce2 = ce;
	}

	(void) mutex_unlock(&cache_lock);
}

static int compare_entries(const void *x1, const void *x2) {
	cache_entry_t *e1 = (cache_entry_t *)x1;
	cache_entry_t *e2 = (cache_entry_t *)x2;

	return (strcasecmp(e1->query, e2->query));
}

static void free_cache_entry(void *node, VISIT order) {
	if (order == endorder || order == leaf) {
		cache_entry_t *ce = *(cache_entry_t **)node;

		free((void *) (ce->query));
		free((void *) (ce->reply));
		free(ce);
		free(node);
	}
}

static SLPError start_cache_thr() {
	int terr;
	SLPError err = SLP_OK;

	(void) mutex_lock(&start_lock);

	if (cache_thr_running) {
		goto start_done;
	}

	(void) cond_init(&cache_called_cond, 0, NULL);

	if ((terr = thr_create(
		0, NULL, (void *(*)(void *)) cache_thr,
		NULL, 0, NULL)) != 0) {
		slp_err(LOG_CRIT, 0, "start_cache_thr",
			"could not start thread: %s", strerror(terr));
		err = SLP_INTERNAL_SYSTEM_ERROR;
		goto start_done;
	}
	cache_thr_running = 1;

start_done:
	(void) mutex_unlock(&start_lock);
	return (err);
}

static void cache_thr() {
	timestruc_t timeout;
	timeout.tv_nsec = 0;

	(void) mutex_lock(&cache_called_lock);
	cache_called = 0;

	while (cache_called == 0) {
		int err;

		timeout.tv_sec = IDLE_TIMEOUT;
		err = cond_reltimedwait(&cache_called_cond,
					&cache_called_lock, &timeout);

		if (err == ETIME) {
			(void) mutex_lock(&cache_lock);
			/* free cache */
			if (da_cache) {
				slp_twalk(da_cache,
			(void (*)(void *, VISIT, int, void *))free_cache_entry,
						0, NULL);
			}
			da_cache = NULL;
			(void) mutex_unlock(&cache_lock);
			cache_thr_running = 0;
			(void) mutex_unlock(&cache_called_lock);
			thr_exit(NULL);
		} else {
			cache_called = 0;
		}
	}
}