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