xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_event.c (revision 7f7322febbcfe774b7270abc3b191c094bfcc517)
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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/fm/protocol.h>
31 #include <limits.h>
32 
33 #include <fmd_alloc.h>
34 #include <fmd_subr.h>
35 #include <fmd_event.h>
36 #include <fmd_string.h>
37 #include <fmd_module.h>
38 #include <fmd_case.h>
39 #include <fmd_log.h>
40 #include <fmd_time.h>
41 #include <fmd_ctl.h>
42 
43 #include <fmd.h>
44 
45 static void
46 fmd_event_nvwrap(fmd_event_impl_t *ep)
47 {
48 	(void) nvlist_remove_all(ep->ev_nvl, FMD_EVN_TTL);
49 	(void) nvlist_remove_all(ep->ev_nvl, FMD_EVN_TOD);
50 
51 	(void) nvlist_add_uint8(ep->ev_nvl,
52 	    FMD_EVN_TTL, ep->ev_ttl);
53 	(void) nvlist_add_uint64_array(ep->ev_nvl,
54 	    FMD_EVN_TOD, (uint64_t *)&ep->ev_time, 2);
55 }
56 
57 static void
58 fmd_event_nvunwrap(fmd_event_impl_t *ep, const fmd_timeval_t *tp)
59 {
60 	uint64_t *tod;
61 	uint_t n;
62 
63 	if (nvlist_lookup_uint8(ep->ev_nvl, FMD_EVN_TTL, &ep->ev_ttl) != 0) {
64 		ep->ev_flags |= FMD_EVF_LOCAL;
65 		ep->ev_ttl = (uint8_t)fmd.d_xprt_ttl;
66 	}
67 
68 	if (tp != NULL)
69 		ep->ev_time = *tp;
70 	else if (nvlist_lookup_uint64_array(ep->ev_nvl,
71 	    FMD_EVN_TOD, &tod, &n) == 0 && n >= 2)
72 		ep->ev_time = *(const fmd_timeval_t *)tod;
73 	else
74 		fmd_time_sync(&ep->ev_time, &ep->ev_hrt, 1);
75 }
76 
77 fmd_event_t *
78 fmd_event_recreate(uint_t type, const fmd_timeval_t *tp,
79     nvlist_t *nvl, void *data, fmd_log_t *lp, off64_t off, size_t len)
80 {
81 	fmd_event_impl_t *ep = fmd_alloc(sizeof (fmd_event_impl_t), FMD_SLEEP);
82 
83 	fmd_timeval_t tod;
84 	hrtime_t hr0;
85 
86 	(void) pthread_mutex_init(&ep->ev_lock, NULL);
87 	ep->ev_refs = 0;
88 	ASSERT(type < FMD_EVT_NTYPES);
89 	ep->ev_type = (uint8_t)type;
90 	ep->ev_state = FMD_EVS_RECEIVED;
91 	ep->ev_flags = FMD_EVF_REPLAY;
92 	ep->ev_nvl = nvl;
93 	ep->ev_data = data;
94 	ep->ev_log = lp;
95 	ep->ev_off = off;
96 	ep->ev_len = len;
97 
98 	fmd_event_nvunwrap(ep, tp);
99 
100 	/*
101 	 * If we're not restoring from a log, the event is marked volatile.  If
102 	 * we are restoring from a log, then hold the log pointer and increment
103 	 * the pending count.  If we're using a log but no offset and data len
104 	 * are specified, it's a checkpoint event: don't replay or set pending.
105 	 */
106 	if (lp == NULL)
107 		ep->ev_flags |= FMD_EVF_VOLATILE;
108 	else if (off != 0 && len != 0)
109 		fmd_log_hold_pending(lp);
110 	else {
111 		ep->ev_flags &= ~FMD_EVF_REPLAY;
112 		fmd_log_hold(lp);
113 	}
114 
115 	/*
116 	 * Sample a (TOD, hrtime) pair from the current system clocks and then
117 	 * compute ev_hrt by taking the delta between this TOD and ev_time.
118 	 */
119 	fmd_time_sync(&tod, &hr0, 1);
120 	fmd_time_tod2hrt(hr0, &tod, &ep->ev_time, &ep->ev_hrt);
121 
122 	fmd_event_nvwrap(ep);
123 	return ((fmd_event_t *)ep);
124 }
125 
126 fmd_event_t *
127 fmd_event_create(uint_t type, hrtime_t hrt, nvlist_t *nvl, void *data)
128 {
129 	fmd_event_impl_t *ep = fmd_alloc(sizeof (fmd_event_impl_t), FMD_SLEEP);
130 
131 	fmd_timeval_t tod;
132 	hrtime_t hr0;
133 	const char *p;
134 	uint64_t ena;
135 
136 	(void) pthread_mutex_init(&ep->ev_lock, NULL);
137 	ep->ev_refs = 0;
138 	ASSERT(type < FMD_EVT_NTYPES);
139 	ep->ev_type = (uint8_t)type;
140 	ep->ev_state = FMD_EVS_RECEIVED;
141 	ep->ev_flags = FMD_EVF_VOLATILE | FMD_EVF_REPLAY | FMD_EVF_LOCAL;
142 	ep->ev_ttl = (uint8_t)fmd.d_xprt_ttl;
143 	ep->ev_nvl = nvl;
144 	ep->ev_data = data;
145 	ep->ev_log = NULL;
146 	ep->ev_off = 0;
147 	ep->ev_len = 0;
148 
149 	/*
150 	 * Sample TOD and then set ev_time to the earlier TOD corresponding to
151 	 * the input hrtime value.  This needs to be improved later: hrestime
152 	 * should be sampled by the transport and passed as an input parameter.
153 	 */
154 	fmd_time_sync(&tod, &hr0, 1);
155 
156 	if (hrt == FMD_HRT_NOW)
157 		hrt = hr0; /* use hrtime sampled by fmd_time_sync() */
158 
159 	/*
160 	 * If this is an FMA protocol event of class "ereport.*" that contains
161 	 * valid ENA, we can compute a more precise bound on the event time.
162 	 */
163 	if (type == FMD_EVT_PROTOCOL && (p = strchr(data, '.')) != NULL &&
164 	    strncmp(data, FM_EREPORT_CLASS, (size_t)(p - (char *)data)) == 0 &&
165 	    nvlist_lookup_uint64(nvl, FM_EREPORT_ENA, &ena) == 0 &&
166 	    fmd.d_clockops == &fmd_timeops_native)
167 		hrt = fmd_time_ena2hrt(hrt, ena);
168 
169 	fmd_time_hrt2tod(hr0, &tod, hrt, &ep->ev_time);
170 	ep->ev_hrt = hrt;
171 
172 	fmd_event_nvwrap(ep);
173 	return ((fmd_event_t *)ep);
174 }
175 
176 void
177 fmd_event_destroy(fmd_event_t *e)
178 {
179 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
180 
181 	ASSERT(MUTEX_HELD(&ep->ev_lock));
182 	ASSERT(ep->ev_refs == 0);
183 
184 	/*
185 	 * If the current state is RECEIVED (i.e. no module has accepted the
186 	 * event) and the event was logged, then change the state to DISCARDED.
187 	 */
188 	if (ep->ev_state == FMD_EVS_RECEIVED)
189 		ep->ev_state = FMD_EVS_DISCARDED;
190 
191 	/*
192 	 * If the current state is DISCARDED, ACCEPTED, or DIAGNOSED and the
193 	 * event has not yet been commited, then attempt to commit it now.
194 	 */
195 	if (ep->ev_state != FMD_EVS_RECEIVED && (ep->ev_flags & (
196 	    FMD_EVF_VOLATILE | FMD_EVF_REPLAY)) == FMD_EVF_REPLAY)
197 		fmd_log_commit(ep->ev_log, e);
198 
199 	if (ep->ev_log != NULL) {
200 		if (ep->ev_flags & FMD_EVF_REPLAY)
201 			fmd_log_decommit(ep->ev_log, e);
202 		fmd_log_rele(ep->ev_log);
203 	}
204 
205 	/*
206 	 * Perform any event type-specific cleanup activities, and then free
207 	 * the name-value pair list and underlying event data structure.
208 	 */
209 	switch (ep->ev_type) {
210 	case FMD_EVT_TIMEOUT:
211 		fmd_free(ep->ev_data, sizeof (fmd_modtimer_t));
212 		break;
213 	case FMD_EVT_CLOSE:
214 	case FMD_EVT_PUBLISH:
215 		fmd_case_rele(ep->ev_data);
216 		break;
217 	case FMD_EVT_CTL:
218 		fmd_ctl_fini(ep->ev_data);
219 		break;
220 	}
221 
222 	if (ep->ev_nvl != NULL)
223 		nvlist_free(ep->ev_nvl);
224 
225 	fmd_free(ep, sizeof (fmd_event_impl_t));
226 }
227 
228 void
229 fmd_event_hold(fmd_event_t *e)
230 {
231 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
232 
233 	(void) pthread_mutex_lock(&ep->ev_lock);
234 	ep->ev_refs++;
235 	ASSERT(ep->ev_refs != 0);
236 	(void) pthread_mutex_unlock(&ep->ev_lock);
237 
238 	if (ep->ev_type == FMD_EVT_CTL)
239 		fmd_ctl_hold(ep->ev_data);
240 }
241 
242 void
243 fmd_event_rele(fmd_event_t *e)
244 {
245 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
246 
247 	if (ep->ev_type == FMD_EVT_CTL)
248 		fmd_ctl_rele(ep->ev_data);
249 
250 	(void) pthread_mutex_lock(&ep->ev_lock);
251 	ASSERT(ep->ev_refs != 0);
252 
253 	if (--ep->ev_refs == 0)
254 		fmd_event_destroy(e);
255 	else
256 		(void) pthread_mutex_unlock(&ep->ev_lock);
257 }
258 
259 /*
260  * Transition event from its current state to the specified state.  The states
261  * for events are defined in fmd_event.h and work according to the diagram:
262  *
263  *  -------------     -------------     State      Description
264  * ( RECEIVED =1 )-->( ACCEPTED =2 )    ---------- ---------------------------
265  *  -----+-------\    ------+------     DISCARDED  No active references in fmd
266  *       |        \         |           RECEIVED   Active refs in fmd, no case
267  *  -----v-------  \  ------v------     ACCEPTED   Active refs, case assigned
268  * ( DISCARDED=0 )  v( DIAGNOSED=3 )    DIAGNOSED  Active refs, case solved
269  *  -------------     -------------
270  *
271  * Since events are reference counted on behalf of multiple subscribers, any
272  * attempt to transition an event to an "earlier" or "equal" state (as defined
273  * by the numeric state values shown in the diagram) is silently ignored.
274  * An event begins life in the RECEIVED state, so the RECEIVED -> DISCARDED
275  * transition is handled by fmd_event_destroy() when no references remain.
276  */
277 void
278 fmd_event_transition(fmd_event_t *e, uint_t state)
279 {
280 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
281 
282 	(void) pthread_mutex_lock(&ep->ev_lock);
283 
284 	TRACE((FMD_DBG_EVT, "event %p transition %u -> %u",
285 	    (void *)ep, ep->ev_state, state));
286 
287 	if (state <= ep->ev_state) {
288 		(void) pthread_mutex_unlock(&ep->ev_lock);
289 		return; /* no state change necessary */
290 	}
291 
292 	if (ep->ev_state < FMD_EVS_RECEIVED || ep->ev_state > FMD_EVS_DIAGNOSED)
293 		fmd_panic("illegal transition %u -> %u\n", ep->ev_state, state);
294 
295 	ep->ev_state = state;
296 	(void) pthread_mutex_unlock(&ep->ev_lock);
297 }
298 
299 /*
300  * If the specified event is DISCARDED, ACCEPTED, OR DIAGNOSED and it has been
301  * written to a log but is still marked for replay, attempt to commit it to the
302  * log so that it will not be replayed.  If fmd_log_commit() is successful, it
303  * will clear the FMD_EVF_REPLAY flag on the event for us.
304  */
305 void
306 fmd_event_commit(fmd_event_t *e)
307 {
308 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
309 
310 	(void) pthread_mutex_lock(&ep->ev_lock);
311 
312 	if (ep->ev_state != FMD_EVS_RECEIVED && (ep->ev_flags & (
313 	    FMD_EVF_VOLATILE | FMD_EVF_REPLAY)) == FMD_EVF_REPLAY)
314 		fmd_log_commit(ep->ev_log, e);
315 
316 	(void) pthread_mutex_unlock(&ep->ev_lock);
317 }
318 
319 /*
320  * Compute the delta between events in nanoseconds.  To account for very old
321  * events which are replayed, we must handle the case where ev_hrt is negative.
322  * We convert the hrtime_t's to unsigned 64-bit integers and then handle the
323  * case where 'old' is greater than 'new' (i.e. high-res time has wrapped).
324  */
325 hrtime_t
326 fmd_event_delta(fmd_event_t *e1, fmd_event_t *e2)
327 {
328 	uint64_t old = ((fmd_event_impl_t *)e1)->ev_hrt;
329 	uint64_t new = ((fmd_event_impl_t *)e2)->ev_hrt;
330 
331 	return (new >= old ? new - old : (UINT64_MAX - old) + new + 1);
332 }
333 
334 hrtime_t
335 fmd_event_hrtime(fmd_event_t *ep)
336 {
337 	return (((fmd_event_impl_t *)ep)->ev_hrt);
338 }
339 
340 int
341 fmd_event_match(fmd_event_t *e, uint_t type, const void *data)
342 {
343 	fmd_event_impl_t *ep = (fmd_event_impl_t *)e;
344 
345 	if (type == FMD_EVT_PROTOCOL)
346 		return (ep->ev_type == type && fmd_strmatch(ep->ev_data, data));
347 	else
348 		return (ep->ev_type == type && ep->ev_data == data);
349 }
350 
351 int
352 fmd_event_equal(fmd_event_t *e1, fmd_event_t *e2)
353 {
354 	fmd_event_impl_t *ep1 = (fmd_event_impl_t *)e1;
355 	fmd_event_impl_t *ep2 = (fmd_event_impl_t *)e2;
356 
357 	return (ep1->ev_log != NULL &&
358 	    ep1->ev_log == ep2->ev_log && ep1->ev_off == ep2->ev_off);
359 }
360