xref: /titanic_44/usr/src/cmd/fm/fmd/common/fmd_ctl.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 /*
30  * FMD Control Event Subsystem
31  *
32  * This file provides a simple and extensible subsystem for the processing of
33  * synchronous control events that can be received from the event transport
34  * and used to control the behavior of the fault manager itself.  At present
35  * this feature is used for the implementation of simulation controls such as
36  * advancing the simulated clock using events sent by the fminject utility.
37  * Control events are assigned a class of the form "resource.sunos.fmd.*" and
38  * are assigned a callback function defined in the _fmd_ctls[] table below.
39  * As control events are received by the event transport, they are assigned a
40  * special event type (ev_type = FMD_EVT_CTL) and the ev_data member is used
41  * to refer to a fmd_ctl_t data structure, managed by the functions below.
42  *
43  * Control events are implemented so that they are synchronous with respect to
44  * the rest of the fault manager event stream, which is usually asynchronous
45  * (that is, the transport dispatch thread and the module receive threads all
46  * execute in parallel).  Synchronous processing is required for control events
47  * so that they can affect global state (e.g. the simulated clock) and ensure
48  * that the results of any state changes are seen by *all* subsequent events.
49  *
50  * To achieve synchronization, the event itself implements a thread barrier:
51  * the fmd_ctl_t maintains a reference count that mirrors the fmd_event_t
52  * reference count (which for ctls counts the number of modules the event
53  * was dispatched to).  As each module receive thread dequeues the event, it
54  * calls fmd_event_rele() to discard the event, which calls fmd_ctl_rele().
55  * fmd_ctl_rele() decrements the ctl's reference count but blocks there waiting
56  * for *all* other references to be released.  When all threads have reached
57  * the barrier, the final caller of fmd_ctl_rele() executes the control event
58  * callback function and then wakes everyone else up.  The transport dispatch
59  * thread, blocked in fmd_modhash_dispatch(), is typically this final caller.
60  */
61 
62 #include <strings.h>
63 #include <limits.h>
64 #include <signal.h>
65 
66 #include <fmd_protocol.h>
67 #include <fmd_alloc.h>
68 #include <fmd_error.h>
69 #include <fmd_subr.h>
70 #include <fmd_time.h>
71 #include <fmd_module.h>
72 #include <fmd_thread.h>
73 #include <fmd_ctl.h>
74 
75 #include <fmd.h>
76 
77 static void
78 fmd_ctl_addhrt(nvlist_t *nvl)
79 {
80 	int64_t delta = 0;
81 
82 	(void) nvlist_lookup_int64(nvl, FMD_RSRC_ADDHRT_DELTA, &delta);
83 	fmd_time_addhrtime(delta);
84 
85 	/*
86 	 * If the non-adjustable clock has reached the apocalypse, fmd(1M)
87 	 * should exit gracefully: queue a SIGTERM for the main thread.
88 	 */
89 	if (fmd_time_gethrtime() == INT64_MAX)
90 		(void) pthread_kill(fmd.d_rmod->mod_thread->thr_tid, SIGTERM);
91 }
92 
93 static void
94 fmd_ctl_inval(nvlist_t *nvl)
95 {
96 	char *class = "<unknown>";
97 
98 	(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
99 	fmd_error(EFMD_CTL_INVAL, "ignoring invalid control event %s\n", class);
100 }
101 
102 static const fmd_ctl_desc_t _fmd_ctls[] = {
103 	{ FMD_RSRC_ADDHRT, FMD_RSRC_ADDHRT_VERS1, fmd_ctl_addhrt },
104 	{ NULL, UINT_MAX, fmd_ctl_inval }
105 };
106 
107 fmd_ctl_t *
108 fmd_ctl_init(nvlist_t *nvl)
109 {
110 	fmd_ctl_t *cp = fmd_alloc(sizeof (fmd_ctl_t), FMD_SLEEP);
111 
112 	const fmd_ctl_desc_t *dp;
113 	uint8_t vers;
114 	char *class;
115 
116 	(void) pthread_mutex_init(&cp->ctl_lock, NULL);
117 	(void) pthread_cond_init(&cp->ctl_cv, NULL);
118 
119 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) != 0 ||
120 	    nvlist_lookup_uint8(nvl, FM_VERSION, &vers) != 0)
121 		fmd_panic("ctl_init called with bad nvlist %p", (void *)nvl);
122 
123 	for (dp = _fmd_ctls; dp->cde_class != NULL; dp++) {
124 		if (strcmp(class, dp->cde_class) == 0)
125 			break;
126 	}
127 
128 	cp->ctl_func = vers > dp->cde_vers ? &fmd_ctl_inval : dp->cde_func;
129 	cp->ctl_nvl = nvl;
130 	cp->ctl_refs = 0;
131 
132 	return (cp);
133 }
134 
135 void
136 fmd_ctl_fini(fmd_ctl_t *cp)
137 {
138 	fmd_free(cp, sizeof (fmd_ctl_t));
139 }
140 
141 /*
142  * Increment the ref count on the fmd_ctl_t to correspond to a reference to the
143  * fmd_event_t.  This count is used to implement a barrier in fmd_ctl_rele().
144  */
145 void
146 fmd_ctl_hold(fmd_ctl_t *cp)
147 {
148 	(void) pthread_mutex_lock(&cp->ctl_lock);
149 
150 	cp->ctl_refs++;
151 	ASSERT(cp->ctl_refs != 0);
152 
153 	(void) pthread_mutex_unlock(&cp->ctl_lock);
154 }
155 
156 /*
157  * Decrement the reference count on the fmd_ctl_t.  If this rele() is the last
158  * one, then execute the callback function and release all the other callers.
159  * Otherwise enter a loop waiting on ctl_cv for other threads to call rele().
160  */
161 void
162 fmd_ctl_rele(fmd_ctl_t *cp)
163 {
164 	(void) pthread_mutex_lock(&cp->ctl_lock);
165 
166 	ASSERT(cp->ctl_refs != 0);
167 	cp->ctl_refs--;
168 
169 	if (cp->ctl_refs == 0) {
170 		cp->ctl_func(cp->ctl_nvl);
171 		(void) pthread_cond_broadcast(&cp->ctl_cv);
172 	} else {
173 		while (cp->ctl_refs != 0)
174 			(void) pthread_cond_wait(&cp->ctl_cv, &cp->ctl_lock);
175 	}
176 
177 	(void) pthread_mutex_unlock(&cp->ctl_lock);
178 }
179