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