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