xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_module.h (revision b5d3ab78446c645a1150b57b7a58b535229ee742)
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 #ifndef	_FMD_MODULE_H
28 #define	_FMD_MODULE_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <fm/diagcode.h>
34 #include <pthread.h>
35 #include <setjmp.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 #include <fmd_conf.h>
42 #include <fmd_list.h>
43 #include <fmd_serd.h>
44 #include <fmd_buf.h>
45 #include <fmd_api.h>
46 #include <fmd_eventq.h>
47 #include <fmd_topo.h>
48 
49 struct fmd_module;			/* see below */
50 struct fmd_thread;			/* see <fmd_thread.h> */
51 struct fmd_idspace;			/* see <fmd_idspace.h> */
52 struct fmd_ustat;			/* see <fmd_ustat.h> */
53 struct fmd_ustat_snap;			/* see <fmd_ustat.h> */
54 
55 typedef struct fmd_modops {
56 	int (*mop_init)(struct fmd_module *);
57 	int (*mop_fini)(struct fmd_module *);
58 	void (*mop_dispatch)(struct fmd_module *, struct fmd_event *);
59 	int (*mop_transport)(struct fmd_module *,
60 	    fmd_xprt_t *, struct fmd_event *);
61 } fmd_modops_t;
62 
63 typedef struct fmd_modhash {
64 	pthread_rwlock_t mh_lock;	/* r/w lock to protect hash */
65 	struct fmd_module **mh_hash;	/* hash bucket array */
66 	uint_t mh_hashlen;		/* size of hash bucket array */
67 	uint_t mh_nelems;		/* number of modules in hash */
68 } fmd_modhash_t;
69 
70 /*
71  * Statistics maintained by fmd itself on behalf of all modules for fmstat(1M).
72  * NOTE: FMD_TYPE_STRING statistics should not be used here.  If they are
73  * required in the future, the FMD_ADM_MODDSTAT service routine must change.
74  */
75 typedef struct fmd_modstat {
76 	fmd_eventqstat_t ms_evqstat;	/* stats for main module event queue */
77 	fmd_stat_t ms_loadtime;		/* hrtime at which module was loaded */
78 	fmd_stat_t ms_snaptime;		/* hrtime of recent stats snapshot */
79 	fmd_stat_t ms_accepted;		/* total events accepted by module */
80 	fmd_stat_t ms_debugdrop;	/* dropped debug messages */
81 	fmd_stat_t ms_memtotal;		/* total space allocated by module */
82 	fmd_stat_t ms_memlimit;		/* limit on space allocated by module */
83 	fmd_stat_t ms_buftotal;		/* total space consumed by buffers */
84 	fmd_stat_t ms_buflimit;		/* limit on space consumed by buffers */
85 	fmd_stat_t ms_thrtotal;		/* total number of auxiliary threads */
86 	fmd_stat_t ms_thrlimit;		/* limit on auxiliary threads */
87 	fmd_stat_t ms_caseopen;		/* cases currently open */
88 	fmd_stat_t ms_casesolved;	/* total cases solved by module */
89 	fmd_stat_t ms_caseclosed;	/* total cases closed by module */
90 	fmd_stat_t ms_ckpt_save;	/* save checkpoints for module */
91 	fmd_stat_t ms_ckpt_restore;	/* restore checkpoints for module */
92 	fmd_stat_t ms_ckpt_zeroed;	/* checkpoint was zeroed at startup */
93 	fmd_stat_t ms_ckpt_cnt;		/* number of checkpoints taken */
94 	fmd_stat_t ms_ckpt_time;	/* total checkpoint time */
95 	fmd_stat_t ms_xprtopen;		/* total number of open transports */
96 	fmd_stat_t ms_xprtlimit;	/* limit on number of open transports */
97 	fmd_stat_t ms_xprtqlimit;	/* limit on transport eventq length */
98 } fmd_modstat_t;
99 
100 typedef struct fmd_module {
101 	fmd_list_t mod_list;		/* linked list next/prev pointers */
102 	pthread_mutex_t mod_lock;	/* lock for mod_cv/owner/flags/refs */
103 	pthread_cond_t mod_cv;		/* condition variable for waiters */
104 	pthread_t mod_owner;		/* tid of thread that set MOD_LOCK */
105 	uint_t mod_refs;		/* module reference count */
106 	uint_t mod_flags;		/* miscellaneous flags (see below) */
107 	uint64_t mod_gen;		/* module checkpoint generation */
108 	int mod_error;			/* error return from module thread */
109 	jmp_buf mod_jmpbuf;		/* setjmp data for fmd_module_enter() */
110 	fmd_modhash_t *mod_hash;	/* containing namespace (ro) */
111 	struct fmd_module *mod_next;	/* next module in fmd_modhash chain */
112 	char *mod_name;			/* basename of module (ro) */
113 	char *mod_path;			/* full pathname of module file (ro) */
114 	char *mod_ckpt;			/* pathname of checkpoint dir (ro) */
115 	nvlist_t *mod_fmri;		/* fmri for this module */
116 	const fmd_modops_t *mod_ops;	/* module class ops vector (ro) */
117 	void *mod_data;			/* data private to module ops vector */
118 	fmd_hdl_info_t *mod_info;	/* module info registered with handle */
119 	void *mod_spec;			/* fmd_hdl_get/setspecific data value */
120 	int mod_argc;			/* size of mod_argv formals array */
121 	fmd_conf_formal_t *mod_argv; 	/* array of conf file formals */
122 	fmd_conf_t *mod_conf;		/* configuration properties (ro) */
123 	struct fm_dc_handle **mod_dictv; /* libdiagcode dictionaries */
124 	int mod_dictc;			/* size of mod_dictv array */
125 	size_t mod_codelen;		/* libdiagcode maximum string length */
126 	struct fmd_eventq *mod_queue;	/* eventq associated with module (ro) */
127 	struct fmd_ustat *mod_ustat;	/* collection of custom statistics */
128 	pthread_mutex_t mod_stats_lock;	/* lock protecting mod_stats data */
129 	fmd_modstat_t *mod_stats;	/* fmd built-in per-module statistics */
130 	struct fmd_thread *mod_thread;	/* thread associated with module (ro) */
131 	struct fmd_idspace *mod_threads;  /* idspace for alternate thread ids */
132 	struct fmd_idspace *mod_timerids; /* idspace for timer identifiers */
133 	fmd_list_t mod_cases;		/* list of cases owned by this module */
134 	fmd_buf_hash_t mod_bufs;	/* hash of bufs owned by this module */
135 	fmd_serd_hash_t mod_serds;	/* hash of serd engs owned by module */
136 	fmd_list_t mod_transports;	/* list of transports owned by module */
137 	fmd_list_t mod_topolist;	/* list of held topo handles */
138 	fmd_topo_t *mod_topo_current;	/* current libtopo snapshot */
139 	char *mod_vers;			/* a copy of module version string */
140 } fmd_module_t;
141 
142 #define	FMD_MOD_INIT	0x001		/* mod_ops->mop_init() has completed */
143 #define	FMD_MOD_FINI	0x002		/* mod_ops->mop_fini() has completed */
144 #define	FMD_MOD_QUIT	0x004		/* module has been requested to quit */
145 #define	FMD_MOD_FAIL	0x008		/* unrecoverable error has occurred */
146 #define	FMD_MOD_LOCK	0x010		/* lock bit for fmd_module_lock() */
147 #define	FMD_MOD_BUSY	0x020		/* module is busy executing a call */
148 #define	FMD_MOD_MDIRTY	0x040		/* module meta state needs checkpoint */
149 #define	FMD_MOD_CDIRTY	0x080		/* module case state needs checkpoint */
150 #define	FMD_MOD_STSUB	0x100		/* stats subscriber is waiting */
151 #define	FMD_MOD_STPUB	0x200		/* stats publisher is waiting */
152 
153 typedef struct fmd_modtimer {
154 	fmd_module_t *mt_mod;		/* module that installed this timer */
155 	void *mt_arg;			/* module private timer argument */
156 	id_t mt_id;			/* timer ID (or -1 if still pending) */
157 } fmd_modtimer_t;
158 
159 typedef struct fmd_modtopo {
160 	fmd_list_t mt_link;		/* link on module topo list */
161 	fmd_topo_t *mt_topo;		/* topo handle */
162 } fmd_modtopo_t;
163 
164 extern const fmd_modops_t fmd_bltin_ops; /* see fmd/common/fmd_builtin.c */
165 extern const fmd_modops_t fmd_rtld_ops;	/* see fmd/common/fmd_rtld.c */
166 extern const fmd_modops_t fmd_proc_ops;	/* see fmd/common/fmd_proc.c */
167 
168 extern fmd_module_t *fmd_module_create(const char *, const fmd_modops_t *);
169 extern void fmd_module_unload(fmd_module_t *);
170 extern void fmd_module_destroy(fmd_module_t *);
171 
172 extern void fmd_module_dispatch(fmd_module_t *, fmd_event_t *);
173 extern int fmd_module_transport(fmd_module_t *, fmd_xprt_t *, fmd_event_t *);
174 extern void fmd_module_timeout(fmd_modtimer_t *, id_t, hrtime_t);
175 extern void fmd_module_gc(fmd_module_t *);
176 extern void fmd_module_trygc(fmd_module_t *);
177 
178 extern int fmd_module_contains(fmd_module_t *, fmd_event_t *);
179 extern void fmd_module_setdirty(fmd_module_t *);
180 extern void fmd_module_setcdirty(fmd_module_t *);
181 extern void fmd_module_clrdirty(fmd_module_t *);
182 extern void fmd_module_commit(fmd_module_t *);
183 
184 extern void fmd_module_lock(fmd_module_t *);
185 extern void fmd_module_unlock(fmd_module_t *);
186 extern int fmd_module_trylock(fmd_module_t *);
187 extern int fmd_module_locked(fmd_module_t *);
188 
189 extern void fmd_module_unregister(fmd_module_t *);
190 extern int fmd_module_enter(fmd_module_t *, void (*)(fmd_hdl_t *));
191 extern void fmd_module_exit(fmd_module_t *);
192 extern void fmd_module_abort(fmd_module_t *, int) __NORETURN;
193 
194 extern void fmd_module_hold(fmd_module_t *);
195 extern void fmd_module_rele(fmd_module_t *);
196 
197 extern int fmd_module_dc_opendict(fmd_module_t *, const char *);
198 extern int fmd_module_dc_key2code(fmd_module_t *,
199     char *const [], char *, size_t);
200 
201 extern fmd_modhash_t *fmd_modhash_create(void);
202 extern void fmd_modhash_destroy(fmd_modhash_t *);
203 
204 extern fmd_module_t *fmd_modhash_load(fmd_modhash_t *,
205     const char *, const fmd_modops_t *);
206 
207 extern void fmd_modhash_loadall(fmd_modhash_t *,
208     const fmd_conf_path_t *, const fmd_modops_t *, const char *);
209 
210 extern fmd_module_t *fmd_modhash_lookup(fmd_modhash_t *, const char *);
211 extern int fmd_modhash_unload(fmd_modhash_t *, const char *);
212 
213 extern void fmd_modhash_apply(fmd_modhash_t *, void (*)(fmd_module_t *));
214 extern void fmd_modhash_tryapply(fmd_modhash_t *, void (*)(fmd_module_t *));
215 extern void fmd_modhash_dispatch(fmd_modhash_t *, fmd_event_t *);
216 
217 extern void fmd_modstat_publish(fmd_module_t *);
218 extern int fmd_modstat_snapshot(fmd_module_t *, struct fmd_ustat_snap *);
219 
220 extern struct topo_hdl *fmd_module_topo_hold(fmd_module_t *);
221 extern int fmd_module_topo_rele(fmd_module_t *, struct topo_hdl *);
222 
223 #ifdef	__cplusplus
224 }
225 #endif
226 
227 #endif	/* _FMD_MODULE_H */
228