xref: /titanic_44/usr/src/lib/fm/libfmd_snmp/common/module.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <fm/fmd_adm.h>
30 #include <fm/fmd_snmp.h>
31 #include <net-snmp/net-snmp-config.h>
32 #include <net-snmp/net-snmp-includes.h>
33 #include <net-snmp/agent/net-snmp-agent-includes.h>
34 #include <pthread.h>
35 #include <stddef.h>
36 #include <errno.h>
37 #include <libuutil.h>
38 #include "sunFM_impl.h"
39 #include "module.h"
40 
41 static uu_avl_pool_t	*mod_name_avl_pool;
42 static uu_avl_pool_t	*mod_index_avl_pool;
43 static uu_avl_t		*mod_name_avl;
44 static uu_avl_t		*mod_index_avl;
45 
46 #define	VALID_AVL_STATE	(mod_name_avl_pool != NULL &&		\
47 	mod_index_avl_pool != NULL && mod_name_avl != NULL &&	\
48 	mod_index_avl != NULL)
49 
50 #define	UPDATE_WAIT_MILLIS	10	/* poll interval in milliseconds */
51 
52 /*
53  * Update types.  Single-index and all are mutually exclusive.
54  */
55 #define	UCT_INDEX	0x1
56 #define	UCT_ALL		0x2
57 #define	UCT_FLAGS	0x3
58 
59 #define	MODULE_DATA_VALID(d)	((d)->d_valid == valid_stamp)
60 
61 /*
62  * Locking rules are straightforward.  There is only one updater thread
63  * for each table, and requests for update that are received while
64  * another update is in progress are ignored.  The single per-table lock
65  * protects all the data for the table, the valid_stamp and max_index
66  * tags for new data, and - importantly - all the hidden static data
67  * used by the Net-SNMP library.  The result return callbacks are always
68  * called in the master agent thread; holding the table lock is
69  * therefore sufficient since only one table's callback can be run at
70  * any one time.  Finer-grained locking is possible here but
71  * substantially more difficult because nearly all Net-SNMP functions
72  * are unsafe.
73  *
74  * In practice this is more than adequate, since the purpose of
75  * threading out the update is to prevent excessively time-consuming
76  * data collection from bottlenecking the entire agent, not to improve
77  * result throughput (SNMP is not intended to be used for applications
78  * requiring high throughput anyway).  If the agent itself ever becomes
79  * multithreaded, locking requirements become limited to our local
80  * per-table data (the tree, max_index, and valid_stamp), and the
81  * implementation could be revisited for better performance.
82  */
83 
84 static ulong_t		max_index;
85 static int		valid_stamp;
86 static pthread_mutex_t	update_lock;
87 static pthread_cond_t	update_cv;
88 static volatile enum { US_QUIET, US_NEEDED, US_INPROGRESS } update_status;
89 
90 static Netsnmp_Node_Handler	sunFmModuleTable_handler;
91 
92 static sunFmModule_data_t *
93 key_build(const char *name, const ulong_t index)
94 {
95 	static sunFmModule_data_t	key;
96 
97 	key.d_index = index;
98 	if (name)
99 		(void) strlcpy(key.d_ami_name, name, sizeof (key.d_ami_name));
100 	else
101 		key.d_ami_name[0] = '\0';
102 
103 	return (&key);
104 }
105 
106 /*
107  * If name is the name of a module we have previously seen and indexed, return
108  * data for it.  Otherwise, return NULL.  Note that the module may not be
109  * valid; that is, it may have been removed from the fault manager since its
110  * information was last updated.
111  */
112 static sunFmModule_data_t *
113 module_lookup_name(const char *name)
114 {
115 	sunFmModule_data_t	*key;
116 
117 	key = key_build(name, 0);
118 	return (uu_avl_find(mod_name_avl, key, NULL, NULL));
119 }
120 
121 /*
122  * If index corresponds to a module we have previously seen and indexed, return
123  * data for it.  Otherwise, return NULL.  Note that the module may not be
124  * valid; that is, it may have been removed from the fault manager since its
125  * information was last updated.
126  */
127 static sunFmModule_data_t *
128 module_lookup_index_exact(const ulong_t index)
129 {
130 	sunFmModule_data_t	*key;
131 
132 	key = key_build(NULL, index);
133 	return (uu_avl_find(mod_index_avl, key, NULL, NULL));
134 }
135 
136 /*
137  * If index corresponds to a valid (that is, extant as of latest information
138  * from the fault manager) fmd module, return the data for that module.
139  * Otherwise, return the data for the valid module whose index is as close as
140  * possible to index but not lower.  This preserves the lexicographical
141  * ordering required for GETNEXT processing.
142  */
143 static sunFmModule_data_t *
144 module_lookup_index_nextvalid(const ulong_t index)
145 {
146 	sunFmModule_data_t	*key, *data;
147 	uu_avl_index_t		idx;
148 
149 	key = key_build(NULL, index);
150 
151 	if ((data = uu_avl_find(mod_index_avl, key, NULL, &idx)) != NULL &&
152 	    MODULE_DATA_VALID(data))
153 		return (data);
154 
155 	data = uu_avl_nearest_next(mod_index_avl, idx);
156 
157 	while (data != NULL && !MODULE_DATA_VALID(data))
158 		data = uu_avl_next(mod_index_avl, data);
159 
160 	return (data);
161 }
162 
163 /*
164  * Possible update the contents of a single module within the cache.  This
165  * is our callback from fmd_module_iter.
166  */
167 static int
168 modinfo_update_one(const fmd_adm_modinfo_t *modinfo, void *arg)
169 {
170 	const sunFmModule_update_ctx_t *update_ctx =
171 	    (sunFmModule_update_ctx_t *)arg;
172 	sunFmModule_data_t *data = module_lookup_name(modinfo->ami_name);
173 
174 	/*
175 	 * An fmd module we haven't seen before.  We're obligated to index
176 	 * it and link it into our cache so that we can find it, but we're
177 	 * not obligated to fill it in completely unless we're doing a
178 	 * thorough update or this is the module we were asked for.  This
179 	 * avoids unnecessary iteration and memory manipulation for data
180 	 * we're not going to return for this request.
181 	 */
182 	if (data == NULL) {
183 		uu_avl_index_t idx;
184 
185 		DEBUGMSGTL((MODNAME_STR, "found new fmd module %s\n",
186 		    modinfo->ami_name));
187 		if ((data = SNMP_MALLOC_TYPEDEF(sunFmModule_data_t)) == NULL) {
188 			snmp_log(LOG_ERR, MODNAME_STR ": Out of memory for "
189 			    "new module data at %s:%d\n", __FILE__, __LINE__);
190 			return (1);
191 		}
192 		/*
193 		 * We allocate indices sequentially and never reuse them.
194 		 * This ensures we can always return valid GETNEXT responses
195 		 * without having to reindex, and it provides the user a
196 		 * more consistent view of the fault manager.
197 		 */
198 		data->d_index = ++max_index;
199 		DEBUGMSGTL((MODNAME_STR, "index %lu is %s@%p\n", data->d_index,
200 		    modinfo->ami_name, data));
201 
202 		(void) strlcpy(data->d_ami_name, modinfo->ami_name,
203 		    sizeof (data->d_ami_name));
204 
205 		uu_avl_node_init(data, &data->d_name_avl, mod_name_avl_pool);
206 		(void) uu_avl_find(mod_name_avl, data, NULL, &idx);
207 		uu_avl_insert(mod_name_avl, data, idx);
208 
209 		uu_avl_node_init(data, &data->d_index_avl, mod_index_avl_pool);
210 		(void) uu_avl_find(mod_index_avl, data, NULL, &idx);
211 		uu_avl_insert(mod_index_avl, data, idx);
212 
213 		DEBUGMSGTL((MODNAME_STR, "completed new module %lu/%s@%p\n",
214 		    data->d_index, data->d_ami_name, data));
215 	}
216 
217 	data->d_valid = valid_stamp;
218 
219 	DEBUGMSGTL((MODNAME_STR, "timestamp updated for %lu/%s@%p: %lu\n",
220 	    data->d_index, data->d_ami_name, data, data->d_valid));
221 
222 	if ((update_ctx->uc_type & UCT_ALL) ||
223 	    update_ctx->uc_index == data->d_index) {
224 		(void) strlcpy(data->d_ami_vers, modinfo->ami_vers,
225 		    sizeof (data->d_ami_vers));
226 		(void) strlcpy(data->d_ami_desc, modinfo->ami_desc,
227 		    sizeof (data->d_ami_desc));
228 		data->d_ami_flags = modinfo->ami_flags;
229 	}
230 
231 	return (!(update_ctx->uc_type & UCT_ALL) &&
232 	    update_ctx->uc_index == data->d_index);
233 }
234 
235 /*
236  * Update some or all module data from fmd.  If thorough is set, all modules
237  * will be indexed and their data cached.  Otherwise, updates will stop once
238  * the module matching index has been updated.
239  *
240  * Returns appropriate SNMP error codes.
241  */
242 static int
243 modinfo_update(sunFmModule_update_ctx_t *update_ctx)
244 {
245 	fmd_adm_t *adm;
246 
247 	ASSERT(update_ctx != NULL);
248 	ASSERT((update_ctx->uc_type & (UCT_INDEX|UCT_ALL)) !=
249 	    (UCT_INDEX|UCT_ALL));
250 	ASSERT((update_ctx->uc_type & ~UCT_FLAGS) == 0);
251 	ASSERT(VALID_AVL_STATE);
252 
253 	if ((adm = fmd_adm_open(update_ctx->uc_host, update_ctx->uc_prog,
254 	    update_ctx->uc_version)) == NULL) {
255 		snmp_log(LOG_ERR, MODNAME_STR ": Communication with fmd "
256 		    "failed: %s\n", strerror(errno));
257 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
258 	}
259 
260 	++valid_stamp;
261 	if (fmd_adm_module_iter(adm, modinfo_update_one, update_ctx) != 0) {
262 		snmp_log(LOG_ERR, MODNAME_STR ": fmd module information update "
263 		    "failed: %s\n", fmd_adm_errmsg(adm));
264 		fmd_adm_close(adm);
265 		return (SNMP_ERR_RESOURCEUNAVAILABLE);
266 	}
267 
268 	DEBUGMSGTL((MODNAME_STR, "module iteration completed\n"));
269 
270 	fmd_adm_close(adm);
271 	return (SNMP_ERR_NOERROR);
272 }
273 
274 /*ARGSUSED*/
275 static void
276 update_thread(void *arg)
277 {
278 	/*
279 	 * The current modinfo_update implementation offers minimal savings
280 	 * for the use of index-only updates; therefore we always do a full
281 	 * update.  If it becomes advantageous to limit updates to a single
282 	 * index, the contexts can be queued by the handler instead.
283 	 */
284 	sunFmModule_update_ctx_t	uc;
285 
286 	uc.uc_host = NULL;
287 	uc.uc_prog = FMD_ADM_PROGRAM;
288 	uc.uc_version = FMD_ADM_VERSION;
289 
290 	uc.uc_index = 0;
291 	uc.uc_type = UCT_ALL;
292 
293 	for (;;) {
294 		(void) pthread_mutex_lock(&update_lock);
295 		update_status = US_QUIET;
296 		while (update_status == US_QUIET)
297 			(void) pthread_cond_wait(&update_cv, &update_lock);
298 		update_status = US_INPROGRESS;
299 		(void) pthread_mutex_unlock(&update_lock);
300 		(void) modinfo_update(&uc);
301 	}
302 }
303 
304 static void
305 request_update(void)
306 {
307 	(void) pthread_mutex_lock(&update_lock);
308 	if (update_status != US_QUIET) {
309 		(void) pthread_mutex_unlock(&update_lock);
310 		return;
311 	}
312 	update_status = US_NEEDED;
313 	(void) pthread_cond_signal(&update_cv);
314 	(void) pthread_mutex_unlock(&update_lock);
315 }
316 
317 /*ARGSUSED*/
318 static int
319 module_compare_name(const void *l, const void *r, void *private)
320 {
321 	sunFmModule_data_t	*l_data = (sunFmModule_data_t *)l;
322 	sunFmModule_data_t	*r_data = (sunFmModule_data_t *)r;
323 
324 	ASSERT(l_data != NULL && r_data != NULL);
325 
326 	return (strcmp(l_data->d_ami_name, r_data->d_ami_name));
327 }
328 
329 /*ARGSUSED*/
330 static int
331 module_compare_index(const void *l, const void *r, void *private)
332 {
333 	sunFmModule_data_t	*l_data = (sunFmModule_data_t *)l;
334 	sunFmModule_data_t	*r_data = (sunFmModule_data_t *)r;
335 
336 	ASSERT(l_data != NULL && r_data != NULL);
337 
338 	return (l_data->d_index < r_data->d_index ? -1 :
339 	    l_data->d_index > r_data->d_index ? 1 : 0);
340 }
341 
342 int
343 sunFmModuleTable_init(void)
344 {
345 	static oid sunFmModuleTable_oid[] = { SUNFMMODULETABLE_OID };
346 	netsnmp_table_registration_info *table_info;
347 	netsnmp_handler_registration *handler;
348 	int err;
349 
350 	if ((err = pthread_mutex_init(&update_lock, NULL)) != 0) {
351 		snmp_log(LOG_ERR, MODNAME_STR ": mutex_init failure: %s\n",
352 		    strerror(err));
353 		return (MIB_REGISTRATION_FAILED);
354 	}
355 	if ((err = pthread_cond_init(&update_cv, NULL)) != 0) {
356 		snmp_log(LOG_ERR, MODNAME_STR ": cond_init failure: %s\n",
357 		    strerror(err));
358 		return (MIB_REGISTRATION_FAILED);
359 	}
360 
361 	if ((err = pthread_create(NULL, NULL, (void *(*)(void *))update_thread,
362 	    NULL)) != 0) {
363 		snmp_log(LOG_ERR, MODNAME_STR ": error creating update "
364 		    "thread: %s\n", strerror(err));
365 		return (MIB_REGISTRATION_FAILED);
366 	}
367 
368 	if ((table_info =
369 	    SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info)) == NULL)
370 		return (MIB_REGISTRATION_FAILED);
371 
372 	if ((handler = netsnmp_create_handler_registration("sunFmModuleTable",
373 	    sunFmModuleTable_handler, sunFmModuleTable_oid,
374 	    OID_LENGTH(sunFmModuleTable_oid), HANDLER_CAN_RONLY)) == NULL) {
375 		SNMP_FREE(table_info);
376 		return (MIB_REGISTRATION_FAILED);
377 	}
378 
379 	/*
380 	 * The Net-SNMP template uses add_indexes here, but that
381 	 * function is unsafe because it does not check for failure.
382 	 */
383 	if (netsnmp_table_helper_add_index(table_info, ASN_UNSIGNED) == NULL) {
384 		SNMP_FREE(table_info);
385 		SNMP_FREE(handler);
386 		return (MIB_REGISTRATION_FAILED);
387 	}
388 
389 	table_info->min_column = SUNFMMODULE_COLMIN;
390 	table_info->max_column = SUNFMMODULE_COLMAX;
391 
392 	if ((mod_name_avl_pool = uu_avl_pool_create("mod_name",
393 	    sizeof (sunFmModule_data_t),
394 	    offsetof(sunFmModule_data_t, d_name_avl), module_compare_name,
395 	    UU_AVL_DEBUG)) == NULL) {
396 		snmp_free_varbind(table_info->indexes);
397 		SNMP_FREE(table_info);
398 		SNMP_FREE(handler);
399 	}
400 
401 	if ((mod_name_avl = uu_avl_create(mod_name_avl_pool, NULL,
402 	    UU_AVL_DEBUG)) == NULL) {
403 		snmp_log(LOG_ERR, MODNAME_STR ": mod_name_avl creation "
404 		    "failed: %s\n", uu_strerror(uu_error()));
405 		snmp_free_varbind(table_info->indexes);
406 		SNMP_FREE(table_info);
407 		SNMP_FREE(handler);
408 		uu_avl_pool_destroy(mod_name_avl_pool);
409 		return (MIB_REGISTRATION_FAILED);
410 	}
411 
412 	if ((mod_index_avl_pool = uu_avl_pool_create("mod_index",
413 	    sizeof (sunFmModule_data_t),
414 	    offsetof(sunFmModule_data_t, d_index_avl),
415 	    module_compare_index, UU_AVL_DEBUG)) == NULL) {
416 		snmp_free_varbind(table_info->indexes);
417 		SNMP_FREE(table_info);
418 		SNMP_FREE(handler);
419 		uu_avl_destroy(mod_name_avl);
420 		uu_avl_pool_destroy(mod_name_avl_pool);
421 	}
422 
423 	if ((mod_index_avl = uu_avl_create(mod_index_avl_pool, NULL,
424 	    UU_AVL_DEBUG)) == NULL) {
425 		snmp_log(LOG_ERR, MODNAME_STR ": mod_index_avl creation "
426 		    "failed: %s\n", uu_strerror(uu_error()));
427 		snmp_free_varbind(table_info->indexes);
428 		SNMP_FREE(table_info);
429 		SNMP_FREE(handler);
430 		uu_avl_destroy(mod_name_avl);
431 		uu_avl_pool_destroy(mod_name_avl_pool);
432 		uu_avl_pool_destroy(mod_index_avl_pool);
433 		return (MIB_REGISTRATION_FAILED);
434 	}
435 
436 	if ((err = netsnmp_register_table(handler, table_info)) !=
437 	    MIB_REGISTERED_OK) {
438 		snmp_free_varbind(table_info->indexes);
439 		SNMP_FREE(table_info);
440 		SNMP_FREE(handler);
441 		uu_avl_destroy(mod_name_avl);
442 		uu_avl_pool_destroy(mod_name_avl_pool);
443 		uu_avl_destroy(mod_index_avl);
444 		uu_avl_pool_destroy(mod_index_avl_pool);
445 		return (err);
446 	}
447 
448 	return (MIB_REGISTERED_OK);
449 }
450 
451 /*
452  * These two functions form the core of GET/GETNEXT/GETBULK handling (the
453  * only kind we do).  They perform two functions:
454  *
455  * - First, frob the request to set all the index variables to correspond
456  *   to the value that's going to be returned.  For GET, this is a nop;
457  *   for GETNEXT/GETBULK it always requires some work.
458  * - Second, find and return the fmd module information corresponding to
459  *   the (possibly updated) indices.
460  *
461  * These should be as fast as possible; they run in the agent thread.
462  */
463 static sunFmModule_data_t *
464 sunFmModuleTable_nextmod(netsnmp_handler_registration *reginfo,
465     netsnmp_table_request_info *table_info)
466 {
467 	sunFmModule_data_t	*data;
468 	netsnmp_variable_list	*var;
469 	ulong_t index;
470 
471 	/*
472 	 * If we have no index, we must make one.
473 	 */
474 	if (table_info->number_indexes < 1) {
475 		oid tmpoid[MAX_OID_LEN];
476 		index = 1;
477 
478 		DEBUGMSGTL((MODNAME_STR, "nextmod: no indexes given\n"));
479 		var = SNMP_MALLOC_TYPEDEF(netsnmp_variable_list);
480 		snmp_set_var_typed_value(var, ASN_UNSIGNED, (uchar_t *)&index,
481 		    sizeof (index));
482 		(void) memcpy(tmpoid, reginfo->rootoid,
483 		    reginfo->rootoid_len * sizeof (oid));
484 		tmpoid[reginfo->rootoid_len] = 1;	/* Entry is .1 */
485 		tmpoid[reginfo->rootoid_len + 1] = table_info->colnum;
486 		if (build_oid(&var->name, &var->name_length, tmpoid,
487 		    reginfo->rootoid_len + 2, var) != SNMPERR_SUCCESS) {
488 			snmp_free_varbind(var);
489 			return (NULL);
490 		}
491 		DEBUGMSGTL((MODNAME_STR, "nextmod: built fake index:\n"));
492 		DEBUGMSGVAR((MODNAME_STR, var));
493 		DEBUGMSG((MODNAME_STR, "\n"));
494 	} else {
495 		var = snmp_clone_varbind(table_info->indexes);
496 		index = *var->val.integer;
497 		DEBUGMSGTL((MODNAME_STR, "nextmod: received index:\n"));
498 		DEBUGMSGVAR((MODNAME_STR, var));
499 		DEBUGMSG((MODNAME_STR, "\n"));
500 		index++;
501 	}
502 
503 	snmp_free_varbind(table_info->indexes);
504 	table_info->indexes = NULL;
505 	table_info->number_indexes = 0;
506 
507 	if ((data = module_lookup_index_nextvalid(index)) == NULL) {
508 		DEBUGMSGTL((MODNAME_STR, "nextmod: exact match not found for "
509 		    "index %lu; trying next column\n", index));
510 		if (table_info->colnum >=
511 		    netsnmp_find_table_registration_info(reginfo)->max_column) {
512 			snmp_free_varbind(var);
513 			DEBUGMSGTL((MODNAME_STR, "nextmod: out of columns\n"));
514 			return (NULL);
515 		}
516 		table_info->colnum++;
517 		index = 1;
518 
519 		data = module_lookup_index_nextvalid(index);
520 	}
521 
522 	if (data == NULL) {
523 		DEBUGMSGTL((MODNAME_STR, "nextmod: exact match not found for "
524 		    "index %lu; stopping\n", index));
525 		snmp_free_varbind(var);
526 		return (NULL);
527 	}
528 
529 	*var->val.integer = data->d_index;
530 	table_info->indexes = var;
531 	table_info->number_indexes = 1;
532 
533 	DEBUGMSGTL((MODNAME_STR, "matching data is %lu/%s@%p\n", data->d_index,
534 	    data->d_ami_name, data));
535 
536 	return (data);
537 }
538 
539 /*ARGSUSED*/
540 static sunFmModule_data_t *
541 sunFmModuleTable_mod(netsnmp_handler_registration *reginfo,
542     netsnmp_table_request_info *table_info)
543 {
544 	ASSERT(table_info->number_indexes == 1);
545 
546 	return (module_lookup_index_exact(table_info->index_oid[0]));
547 }
548 
549 /*ARGSUSED*/
550 static void
551 sunFmModuleTable_return(unsigned int reg, void *arg)
552 {
553 	netsnmp_delegated_cache		*cache = (netsnmp_delegated_cache *)arg;
554 	netsnmp_request_info		*request;
555 	netsnmp_agent_request_info	*reqinfo;
556 	netsnmp_handler_registration	*reginfo;
557 	netsnmp_table_request_info	*table_info;
558 	sunFmModule_data_t		*data;
559 	ulong_t				modstate;
560 
561 	ASSERT(netsnmp_handler_check_cache(cache) != NULL);
562 
563 	(void) pthread_mutex_lock(&update_lock);
564 	if (update_status != US_QUIET) {
565 		struct timeval			tv;
566 
567 		tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
568 		tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
569 
570 		(void) snmp_alarm_register_hr(tv, 0, sunFmModuleTable_return,
571 		    cache);
572 		(void) pthread_mutex_unlock(&update_lock);
573 		return;
574 	}
575 
576 	request = cache->requests;
577 	reqinfo = cache->reqinfo;
578 	reginfo = cache->reginfo;
579 
580 	table_info = netsnmp_extract_table_info(request);
581 	request->delegated = 0;
582 
583 	ASSERT(table_info->colnum >= SUNFMMODULE_COLMIN);
584 	ASSERT(table_info->colnum <= SUNFMMODULE_COLMAX);
585 
586 	/*
587 	 * table_info->colnum contains the column number requested.
588 	 * table_info->indexes contains a linked list of snmp variable
589 	 * bindings for the indexes of the table.  Values in the list
590 	 * have been set corresponding to the indexes of the
591 	 * request.  We have other guarantees as well:
592 	 *
593 	 * - The column number is always within range.
594 	 * - If we have no index data, table_info->index_oid_len is 0.
595 	 * - We will never receive requests outside our table nor
596 	 *   those with the first subid anything other than 1 (Entry)
597 	 *   nor those without a column number.  This is true even
598 	 *   for GETNEXT requests.
599 	 */
600 
601 	switch (reqinfo->mode) {
602 	case MODE_GET:
603 		if ((data = sunFmModuleTable_mod(reginfo, table_info)) ==
604 		    NULL) {
605 			netsnmp_free_delegated_cache(cache);
606 			(void) pthread_mutex_unlock(&update_lock);
607 			return;
608 		}
609 		break;
610 	case MODE_GETNEXT:
611 	case MODE_GETBULK:
612 		if ((data = sunFmModuleTable_nextmod(reginfo, table_info)) ==
613 		    NULL) {
614 			netsnmp_free_delegated_cache(cache);
615 			(void) pthread_mutex_unlock(&update_lock);
616 			return;
617 		}
618 		break;
619 	default:
620 		snmp_log(LOG_ERR, MODNAME_STR ": Unsupported request "
621 		    "mode %d\n", reqinfo->mode);
622 		netsnmp_free_delegated_cache(cache);
623 		(void) pthread_mutex_unlock(&update_lock);
624 		return;
625 	}
626 
627 	switch (table_info->colnum) {
628 	case SUNFMMODULE_COL_NAME:
629 		netsnmp_table_build_result(reginfo, request, table_info,
630 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_name,
631 		    strlen(data->d_ami_name));
632 		break;
633 	case SUNFMMODULE_COL_VERSION:
634 		netsnmp_table_build_result(reginfo, request, table_info,
635 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_vers,
636 		    strlen(data->d_ami_vers));
637 		break;
638 	case SUNFMMODULE_COL_STATUS:
639 		modstate = (data->d_ami_flags & FMD_ADM_MOD_FAILED) ?
640 		    SUNFMMODULE_STATE_FAILED : SUNFMMODULE_STATE_ACTIVE;
641 		netsnmp_table_build_result(reginfo, request, table_info,
642 		    ASN_INTEGER, (uchar_t *)&modstate,
643 		    sizeof (modstate));
644 		break;
645 	case SUNFMMODULE_COL_DESCRIPTION:
646 		netsnmp_table_build_result(reginfo, request, table_info,
647 		    ASN_OCTET_STR, (uchar_t *)data->d_ami_desc,
648 		    strlen(data->d_ami_desc));
649 		break;
650 	default:
651 		break;
652 	}
653 	netsnmp_free_delegated_cache(cache);
654 	(void) pthread_mutex_unlock(&update_lock);
655 }
656 
657 static int
658 sunFmModuleTable_handler(netsnmp_mib_handler *handler,
659     netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo,
660     netsnmp_request_info *requests)
661 {
662 	netsnmp_request_info		*request;
663 	struct timeval			tv;
664 
665 	tv.tv_sec = UPDATE_WAIT_MILLIS / 1000;
666 	tv.tv_usec = (UPDATE_WAIT_MILLIS % 1000) * 1000;
667 
668 	request_update();
669 
670 	for (request = requests; request; request = request->next) {
671 		if (request->processed != 0)
672 			continue;
673 
674 		if (netsnmp_extract_table_info(request) == NULL)
675 			continue;
676 
677 		request->delegated = 1;
678 		(void) snmp_alarm_register_hr(tv, 0, sunFmModuleTable_return,
679 		    (void *) netsnmp_create_delegated_cache(handler, reginfo,
680 		    reqinfo, request, NULL));
681 	}
682 
683 	return (SNMP_ERR_NOERROR);
684 }
685