xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_ctl.c (revision d9638e547d8811f2c689977f8dd2a353938b61fd)
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  */
22*d9638e54Smws 
237c478bd9Sstevel@tonic-gate /*
24*d9638e54Smws  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * FMD Control Event Subsystem
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * This file provides a simple and extensible subsystem for the processing of
347c478bd9Sstevel@tonic-gate  * synchronous control events that can be received from the event transport
357c478bd9Sstevel@tonic-gate  * and used to control the behavior of the fault manager itself.  At present
367c478bd9Sstevel@tonic-gate  * this feature is used for the implementation of simulation controls such as
377c478bd9Sstevel@tonic-gate  * advancing the simulated clock using events sent by the fminject utility.
38*d9638e54Smws  * Control events are assigned a class of the form "resource.fm.fmd.*" and
397c478bd9Sstevel@tonic-gate  * are assigned a callback function defined in the _fmd_ctls[] table below.
407c478bd9Sstevel@tonic-gate  * As control events are received by the event transport, they are assigned a
417c478bd9Sstevel@tonic-gate  * special event type (ev_type = FMD_EVT_CTL) and the ev_data member is used
427c478bd9Sstevel@tonic-gate  * to refer to a fmd_ctl_t data structure, managed by the functions below.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * Control events are implemented so that they are synchronous with respect to
457c478bd9Sstevel@tonic-gate  * the rest of the fault manager event stream, which is usually asynchronous
467c478bd9Sstevel@tonic-gate  * (that is, the transport dispatch thread and the module receive threads all
477c478bd9Sstevel@tonic-gate  * execute in parallel).  Synchronous processing is required for control events
487c478bd9Sstevel@tonic-gate  * so that they can affect global state (e.g. the simulated clock) and ensure
497c478bd9Sstevel@tonic-gate  * that the results of any state changes are seen by *all* subsequent events.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * To achieve synchronization, the event itself implements a thread barrier:
527c478bd9Sstevel@tonic-gate  * the fmd_ctl_t maintains a reference count that mirrors the fmd_event_t
537c478bd9Sstevel@tonic-gate  * reference count (which for ctls counts the number of modules the event
547c478bd9Sstevel@tonic-gate  * was dispatched to).  As each module receive thread dequeues the event, it
557c478bd9Sstevel@tonic-gate  * calls fmd_event_rele() to discard the event, which calls fmd_ctl_rele().
567c478bd9Sstevel@tonic-gate  * fmd_ctl_rele() decrements the ctl's reference count but blocks there waiting
577c478bd9Sstevel@tonic-gate  * for *all* other references to be released.  When all threads have reached
587c478bd9Sstevel@tonic-gate  * the barrier, the final caller of fmd_ctl_rele() executes the control event
597c478bd9Sstevel@tonic-gate  * callback function and then wakes everyone else up.  The transport dispatch
607c478bd9Sstevel@tonic-gate  * thread, blocked in fmd_modhash_dispatch(), is typically this final caller.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <strings.h>
647c478bd9Sstevel@tonic-gate #include <limits.h>
657c478bd9Sstevel@tonic-gate #include <signal.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <fmd_protocol.h>
687c478bd9Sstevel@tonic-gate #include <fmd_alloc.h>
697c478bd9Sstevel@tonic-gate #include <fmd_error.h>
707c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
717c478bd9Sstevel@tonic-gate #include <fmd_time.h>
727c478bd9Sstevel@tonic-gate #include <fmd_module.h>
737c478bd9Sstevel@tonic-gate #include <fmd_thread.h>
747c478bd9Sstevel@tonic-gate #include <fmd_ctl.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <fmd.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static void
fmd_ctl_addhrt(nvlist_t * nvl)797c478bd9Sstevel@tonic-gate fmd_ctl_addhrt(nvlist_t *nvl)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	int64_t delta = 0;
827c478bd9Sstevel@tonic-gate 
83*d9638e54Smws 	(void) nvlist_lookup_int64(nvl, FMD_CTL_ADDHRT_DELTA, &delta);
847c478bd9Sstevel@tonic-gate 	fmd_time_addhrtime(delta);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * If the non-adjustable clock has reached the apocalypse, fmd(1M)
887c478bd9Sstevel@tonic-gate 	 * should exit gracefully: queue a SIGTERM for the main thread.
897c478bd9Sstevel@tonic-gate 	 */
907c478bd9Sstevel@tonic-gate 	if (fmd_time_gethrtime() == INT64_MAX)
917c478bd9Sstevel@tonic-gate 		(void) pthread_kill(fmd.d_rmod->mod_thread->thr_tid, SIGTERM);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static void
fmd_ctl_inval(nvlist_t * nvl)957c478bd9Sstevel@tonic-gate fmd_ctl_inval(nvlist_t *nvl)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	char *class = "<unknown>";
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
1007c478bd9Sstevel@tonic-gate 	fmd_error(EFMD_CTL_INVAL, "ignoring invalid control event %s\n", class);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
103*d9638e54Smws /*ARGSUSED*/
104*d9638e54Smws static void
fmd_ctl_pause(nvlist_t * nvl)105*d9638e54Smws fmd_ctl_pause(nvlist_t *nvl)
106*d9638e54Smws {
107*d9638e54Smws 	fmd_dprintf(FMD_DBG_DISP, "unpausing modules from ctl barrier\n");
108*d9638e54Smws }
109*d9638e54Smws 
1107c478bd9Sstevel@tonic-gate static const fmd_ctl_desc_t _fmd_ctls[] = {
111*d9638e54Smws 	{ FMD_CTL_ADDHRT, FMD_CTL_ADDHRT_VERS1, fmd_ctl_addhrt },
1127c478bd9Sstevel@tonic-gate 	{ NULL, UINT_MAX, fmd_ctl_inval }
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate fmd_ctl_t *
fmd_ctl_init(nvlist_t * nvl)1167c478bd9Sstevel@tonic-gate fmd_ctl_init(nvlist_t *nvl)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	fmd_ctl_t *cp = fmd_alloc(sizeof (fmd_ctl_t), FMD_SLEEP);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	const fmd_ctl_desc_t *dp;
1217c478bd9Sstevel@tonic-gate 	uint8_t vers;
1227c478bd9Sstevel@tonic-gate 	char *class;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&cp->ctl_lock, NULL);
1257c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&cp->ctl_cv, NULL);
1267c478bd9Sstevel@tonic-gate 
127*d9638e54Smws 	cp->ctl_nvl = nvl;
128*d9638e54Smws 	cp->ctl_refs = 0;
129*d9638e54Smws 
130*d9638e54Smws 	if (nvl == NULL) {
131*d9638e54Smws 		cp->ctl_func = fmd_ctl_pause;
132*d9638e54Smws 		return (cp);
133*d9638e54Smws 	}
134*d9638e54Smws 
1357c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||
1367c478bd9Sstevel@tonic-gate 	    nvlist_lookup_uint8(nvl, FM_VERSION, &vers) != 0)
1377c478bd9Sstevel@tonic-gate 		fmd_panic("ctl_init called with bad nvlist %p", (void *)nvl);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	for (dp = _fmd_ctls; dp->cde_class != NULL; dp++) {
1407c478bd9Sstevel@tonic-gate 		if (strcmp(class, dp->cde_class) == 0)
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	cp->ctl_func = vers > dp->cde_vers ? &fmd_ctl_inval : dp->cde_func;
1457c478bd9Sstevel@tonic-gate 	return (cp);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate void
fmd_ctl_fini(fmd_ctl_t * cp)1497c478bd9Sstevel@tonic-gate fmd_ctl_fini(fmd_ctl_t *cp)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	fmd_free(cp, sizeof (fmd_ctl_t));
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Increment the ref count on the fmd_ctl_t to correspond to a reference to the
1567c478bd9Sstevel@tonic-gate  * fmd_event_t.  This count is used to implement a barrier in fmd_ctl_rele().
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate void
fmd_ctl_hold(fmd_ctl_t * cp)1597c478bd9Sstevel@tonic-gate fmd_ctl_hold(fmd_ctl_t *cp)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&cp->ctl_lock);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	cp->ctl_refs++;
1647c478bd9Sstevel@tonic-gate 	ASSERT(cp->ctl_refs != 0);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&cp->ctl_lock);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Decrement the reference count on the fmd_ctl_t.  If this rele() is the last
1717c478bd9Sstevel@tonic-gate  * one, then execute the callback function and release all the other callers.
1727c478bd9Sstevel@tonic-gate  * Otherwise enter a loop waiting on ctl_cv for other threads to call rele().
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate void
fmd_ctl_rele(fmd_ctl_t * cp)1757c478bd9Sstevel@tonic-gate fmd_ctl_rele(fmd_ctl_t *cp)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&cp->ctl_lock);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	ASSERT(cp->ctl_refs != 0);
1807c478bd9Sstevel@tonic-gate 	cp->ctl_refs--;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (cp->ctl_refs == 0) {
1837c478bd9Sstevel@tonic-gate 		cp->ctl_func(cp->ctl_nvl);
1847c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&cp->ctl_cv);
1857c478bd9Sstevel@tonic-gate 	} else {
1867c478bd9Sstevel@tonic-gate 		while (cp->ctl_refs != 0)
1877c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&cp->ctl_cv, &cp->ctl_lock);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&cp->ctl_lock);
1917c478bd9Sstevel@tonic-gate }
192