xref: /onnv-gate/usr/src/cmd/fm/fmd/common/fmd_ctl.c (revision 1193:e784a8fa27da)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1193Smws 
230Sstevel@tonic-gate /*
24*1193Smws  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * FMD Control Event Subsystem
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * This file provides a simple and extensible subsystem for the processing of
340Sstevel@tonic-gate  * synchronous control events that can be received from the event transport
350Sstevel@tonic-gate  * and used to control the behavior of the fault manager itself.  At present
360Sstevel@tonic-gate  * this feature is used for the implementation of simulation controls such as
370Sstevel@tonic-gate  * advancing the simulated clock using events sent by the fminject utility.
38*1193Smws  * Control events are assigned a class of the form "resource.fm.fmd.*" and
390Sstevel@tonic-gate  * are assigned a callback function defined in the _fmd_ctls[] table below.
400Sstevel@tonic-gate  * As control events are received by the event transport, they are assigned a
410Sstevel@tonic-gate  * special event type (ev_type = FMD_EVT_CTL) and the ev_data member is used
420Sstevel@tonic-gate  * to refer to a fmd_ctl_t data structure, managed by the functions below.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * Control events are implemented so that they are synchronous with respect to
450Sstevel@tonic-gate  * the rest of the fault manager event stream, which is usually asynchronous
460Sstevel@tonic-gate  * (that is, the transport dispatch thread and the module receive threads all
470Sstevel@tonic-gate  * execute in parallel).  Synchronous processing is required for control events
480Sstevel@tonic-gate  * so that they can affect global state (e.g. the simulated clock) and ensure
490Sstevel@tonic-gate  * that the results of any state changes are seen by *all* subsequent events.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * To achieve synchronization, the event itself implements a thread barrier:
520Sstevel@tonic-gate  * the fmd_ctl_t maintains a reference count that mirrors the fmd_event_t
530Sstevel@tonic-gate  * reference count (which for ctls counts the number of modules the event
540Sstevel@tonic-gate  * was dispatched to).  As each module receive thread dequeues the event, it
550Sstevel@tonic-gate  * calls fmd_event_rele() to discard the event, which calls fmd_ctl_rele().
560Sstevel@tonic-gate  * fmd_ctl_rele() decrements the ctl's reference count but blocks there waiting
570Sstevel@tonic-gate  * for *all* other references to be released.  When all threads have reached
580Sstevel@tonic-gate  * the barrier, the final caller of fmd_ctl_rele() executes the control event
590Sstevel@tonic-gate  * callback function and then wakes everyone else up.  The transport dispatch
600Sstevel@tonic-gate  * thread, blocked in fmd_modhash_dispatch(), is typically this final caller.
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <strings.h>
640Sstevel@tonic-gate #include <limits.h>
650Sstevel@tonic-gate #include <signal.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <fmd_protocol.h>
680Sstevel@tonic-gate #include <fmd_alloc.h>
690Sstevel@tonic-gate #include <fmd_error.h>
700Sstevel@tonic-gate #include <fmd_subr.h>
710Sstevel@tonic-gate #include <fmd_time.h>
720Sstevel@tonic-gate #include <fmd_module.h>
730Sstevel@tonic-gate #include <fmd_thread.h>
740Sstevel@tonic-gate #include <fmd_ctl.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #include <fmd.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static void
fmd_ctl_addhrt(nvlist_t * nvl)790Sstevel@tonic-gate fmd_ctl_addhrt(nvlist_t *nvl)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	int64_t delta = 0;
820Sstevel@tonic-gate 
83*1193Smws 	(void) nvlist_lookup_int64(nvl, FMD_CTL_ADDHRT_DELTA, &delta);
840Sstevel@tonic-gate 	fmd_time_addhrtime(delta);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * If the non-adjustable clock has reached the apocalypse, fmd(1M)
880Sstevel@tonic-gate 	 * should exit gracefully: queue a SIGTERM for the main thread.
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 	if (fmd_time_gethrtime() == INT64_MAX)
910Sstevel@tonic-gate 		(void) pthread_kill(fmd.d_rmod->mod_thread->thr_tid, SIGTERM);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static void
fmd_ctl_inval(nvlist_t * nvl)950Sstevel@tonic-gate fmd_ctl_inval(nvlist_t *nvl)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	char *class = "<unknown>";
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
1000Sstevel@tonic-gate 	fmd_error(EFMD_CTL_INVAL, "ignoring invalid control event %s\n", class);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
103*1193Smws /*ARGSUSED*/
104*1193Smws static void
fmd_ctl_pause(nvlist_t * nvl)105*1193Smws fmd_ctl_pause(nvlist_t *nvl)
106*1193Smws {
107*1193Smws 	fmd_dprintf(FMD_DBG_DISP, "unpausing modules from ctl barrier\n");
108*1193Smws }
109*1193Smws 
1100Sstevel@tonic-gate static const fmd_ctl_desc_t _fmd_ctls[] = {
111*1193Smws 	{ FMD_CTL_ADDHRT, FMD_CTL_ADDHRT_VERS1, fmd_ctl_addhrt },
1120Sstevel@tonic-gate 	{ NULL, UINT_MAX, fmd_ctl_inval }
1130Sstevel@tonic-gate };
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate fmd_ctl_t *
fmd_ctl_init(nvlist_t * nvl)1160Sstevel@tonic-gate fmd_ctl_init(nvlist_t *nvl)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	fmd_ctl_t *cp = fmd_alloc(sizeof (fmd_ctl_t), FMD_SLEEP);
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	const fmd_ctl_desc_t *dp;
1210Sstevel@tonic-gate 	uint8_t vers;
1220Sstevel@tonic-gate 	char *class;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	(void) pthread_mutex_init(&cp->ctl_lock, NULL);
1250Sstevel@tonic-gate 	(void) pthread_cond_init(&cp->ctl_cv, NULL);
1260Sstevel@tonic-gate 
127*1193Smws 	cp->ctl_nvl = nvl;
128*1193Smws 	cp->ctl_refs = 0;
129*1193Smws 
130*1193Smws 	if (nvl == NULL) {
131*1193Smws 		cp->ctl_func = fmd_ctl_pause;
132*1193Smws 		return (cp);
133*1193Smws 	}
134*1193Smws 
1350Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||
1360Sstevel@tonic-gate 	    nvlist_lookup_uint8(nvl, FM_VERSION, &vers) != 0)
1370Sstevel@tonic-gate 		fmd_panic("ctl_init called with bad nvlist %p", (void *)nvl);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	for (dp = _fmd_ctls; dp->cde_class != NULL; dp++) {
1400Sstevel@tonic-gate 		if (strcmp(class, dp->cde_class) == 0)
1410Sstevel@tonic-gate 			break;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	cp->ctl_func = vers > dp->cde_vers ? &fmd_ctl_inval : dp->cde_func;
1450Sstevel@tonic-gate 	return (cp);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate void
fmd_ctl_fini(fmd_ctl_t * cp)1490Sstevel@tonic-gate fmd_ctl_fini(fmd_ctl_t *cp)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	fmd_free(cp, sizeof (fmd_ctl_t));
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * Increment the ref count on the fmd_ctl_t to correspond to a reference to the
1560Sstevel@tonic-gate  * fmd_event_t.  This count is used to implement a barrier in fmd_ctl_rele().
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate void
fmd_ctl_hold(fmd_ctl_t * cp)1590Sstevel@tonic-gate fmd_ctl_hold(fmd_ctl_t *cp)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	(void) pthread_mutex_lock(&cp->ctl_lock);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	cp->ctl_refs++;
1640Sstevel@tonic-gate 	ASSERT(cp->ctl_refs != 0);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&cp->ctl_lock);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * Decrement the reference count on the fmd_ctl_t.  If this rele() is the last
1710Sstevel@tonic-gate  * one, then execute the callback function and release all the other callers.
1720Sstevel@tonic-gate  * Otherwise enter a loop waiting on ctl_cv for other threads to call rele().
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate void
fmd_ctl_rele(fmd_ctl_t * cp)1750Sstevel@tonic-gate fmd_ctl_rele(fmd_ctl_t *cp)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	(void) pthread_mutex_lock(&cp->ctl_lock);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	ASSERT(cp->ctl_refs != 0);
1800Sstevel@tonic-gate 	cp->ctl_refs--;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (cp->ctl_refs == 0) {
1830Sstevel@tonic-gate 		cp->ctl_func(cp->ctl_nvl);
1840Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&cp->ctl_cv);
1850Sstevel@tonic-gate 	} else {
1860Sstevel@tonic-gate 		while (cp->ctl_refs != 0)
1870Sstevel@tonic-gate 			(void) pthread_cond_wait(&cp->ctl_cv, &cp->ctl_lock);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&cp->ctl_lock);
1910Sstevel@tonic-gate }
192