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 2008 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_topo.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 case FMD_EVT_TOPO: 221 fmd_topo_rele(ep->ev_data); 222 break; 223 } 224 225 if (ep->ev_nvl != NULL) 226 nvlist_free(ep->ev_nvl); 227 228 fmd_free(ep, sizeof (fmd_event_impl_t)); 229 } 230 231 void 232 fmd_event_hold(fmd_event_t *e) 233 { 234 fmd_event_impl_t *ep = (fmd_event_impl_t *)e; 235 236 (void) pthread_mutex_lock(&ep->ev_lock); 237 ep->ev_refs++; 238 ASSERT(ep->ev_refs != 0); 239 (void) pthread_mutex_unlock(&ep->ev_lock); 240 241 if (ep->ev_type == FMD_EVT_CTL) 242 fmd_ctl_hold(ep->ev_data); 243 } 244 245 void 246 fmd_event_rele(fmd_event_t *e) 247 { 248 fmd_event_impl_t *ep = (fmd_event_impl_t *)e; 249 250 if (ep->ev_type == FMD_EVT_CTL) 251 fmd_ctl_rele(ep->ev_data); 252 253 (void) pthread_mutex_lock(&ep->ev_lock); 254 ASSERT(ep->ev_refs != 0); 255 256 if (--ep->ev_refs == 0) 257 fmd_event_destroy(e); 258 else 259 (void) pthread_mutex_unlock(&ep->ev_lock); 260 } 261 262 /* 263 * Transition event from its current state to the specified state. The states 264 * for events are defined in fmd_event.h and work according to the diagram: 265 * 266 * ------------- ------------- State Description 267 * ( RECEIVED =1 )-->( ACCEPTED =2 ) ---------- --------------------------- 268 * -----+-------\ ------+------ DISCARDED No active references in fmd 269 * | \ | RECEIVED Active refs in fmd, no case 270 * -----v------- \ ------v------ ACCEPTED Active refs, case assigned 271 * ( DISCARDED=0 ) v( DIAGNOSED=3 ) DIAGNOSED Active refs, case solved 272 * ------------- ------------- 273 * 274 * Since events are reference counted on behalf of multiple subscribers, any 275 * attempt to transition an event to an "earlier" or "equal" state (as defined 276 * by the numeric state values shown in the diagram) is silently ignored. 277 * An event begins life in the RECEIVED state, so the RECEIVED -> DISCARDED 278 * transition is handled by fmd_event_destroy() when no references remain. 279 */ 280 void 281 fmd_event_transition(fmd_event_t *e, uint_t state) 282 { 283 fmd_event_impl_t *ep = (fmd_event_impl_t *)e; 284 285 (void) pthread_mutex_lock(&ep->ev_lock); 286 287 TRACE((FMD_DBG_EVT, "event %p transition %u -> %u", 288 (void *)ep, ep->ev_state, state)); 289 290 if (state <= ep->ev_state) { 291 (void) pthread_mutex_unlock(&ep->ev_lock); 292 return; /* no state change necessary */ 293 } 294 295 if (ep->ev_state < FMD_EVS_RECEIVED || ep->ev_state > FMD_EVS_DIAGNOSED) 296 fmd_panic("illegal transition %u -> %u\n", ep->ev_state, state); 297 298 ep->ev_state = state; 299 (void) pthread_mutex_unlock(&ep->ev_lock); 300 } 301 302 /* 303 * If the specified event is DISCARDED, ACCEPTED, OR DIAGNOSED and it has been 304 * written to a log but is still marked for replay, attempt to commit it to the 305 * log so that it will not be replayed. If fmd_log_commit() is successful, it 306 * will clear the FMD_EVF_REPLAY flag on the event for us. 307 */ 308 void 309 fmd_event_commit(fmd_event_t *e) 310 { 311 fmd_event_impl_t *ep = (fmd_event_impl_t *)e; 312 313 (void) pthread_mutex_lock(&ep->ev_lock); 314 315 if (ep->ev_state != FMD_EVS_RECEIVED && (ep->ev_flags & ( 316 FMD_EVF_VOLATILE | FMD_EVF_REPLAY)) == FMD_EVF_REPLAY) 317 fmd_log_commit(ep->ev_log, e); 318 319 (void) pthread_mutex_unlock(&ep->ev_lock); 320 } 321 322 /* 323 * Compute the delta between events in nanoseconds. To account for very old 324 * events which are replayed, we must handle the case where ev_hrt is negative. 325 * We convert the hrtime_t's to unsigned 64-bit integers and then handle the 326 * case where 'old' is greater than 'new' (i.e. high-res time has wrapped). 327 */ 328 hrtime_t 329 fmd_event_delta(fmd_event_t *e1, fmd_event_t *e2) 330 { 331 uint64_t old = ((fmd_event_impl_t *)e1)->ev_hrt; 332 uint64_t new = ((fmd_event_impl_t *)e2)->ev_hrt; 333 334 return (new >= old ? new - old : (UINT64_MAX - old) + new + 1); 335 } 336 337 hrtime_t 338 fmd_event_hrtime(fmd_event_t *ep) 339 { 340 return (((fmd_event_impl_t *)ep)->ev_hrt); 341 } 342 343 int 344 fmd_event_match(fmd_event_t *e, uint_t type, const void *data) 345 { 346 fmd_event_impl_t *ep = (fmd_event_impl_t *)e; 347 348 if (ep->ev_type != type) 349 return (0); 350 351 if (type == FMD_EVT_PROTOCOL) 352 return (fmd_strmatch(ep->ev_data, data)); 353 else if (type == FMD_EVT_TIMEOUT) 354 return ((id_t)data == ((fmd_modtimer_t *)ep->ev_data)->mt_id); 355 else 356 return (ep->ev_data == data); 357 } 358 359 int 360 fmd_event_equal(fmd_event_t *e1, fmd_event_t *e2) 361 { 362 fmd_event_impl_t *ep1 = (fmd_event_impl_t *)e1; 363 fmd_event_impl_t *ep2 = (fmd_event_impl_t *)e2; 364 365 return (ep1->ev_log != NULL && 366 ep1->ev_log == ep2->ev_log && ep1->ev_off == ep2->ev_off); 367 } 368