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