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  * The "program" executed by the injector consists of a tree of commands.
31*7c478bd9Sstevel@tonic-gate  * Routines in this file build and execute said command tree.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
35*7c478bd9Sstevel@tonic-gate #include <unistd.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <inj.h>
38*7c478bd9Sstevel@tonic-gate #include <inj_event.h>
39*7c478bd9Sstevel@tonic-gate #include <inj_lex.h>
40*7c478bd9Sstevel@tonic-gate #include <inj_err.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Command tree construction
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static inj_list_t inj_cmds;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate void
49*7c478bd9Sstevel@tonic-gate inj_cmds_add(inj_cmd_t *cmd)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	inj_list_append(&inj_cmds, cmd);
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate inj_list_t *
55*7c478bd9Sstevel@tonic-gate inj_cmds_get(void)
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate 	return (&inj_cmds);
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate inj_randelem_t *
61*7c478bd9Sstevel@tonic-gate inj_rand_create(inj_defn_t *ev, uint_t prob)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	inj_randelem_t *re = inj_zalloc(sizeof (inj_randelem_t));
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	re->re_event = ev;
66*7c478bd9Sstevel@tonic-gate 	re->re_prob = prob;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	return (re);
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate inj_randelem_t *
72*7c478bd9Sstevel@tonic-gate inj_rand_add(inj_randelem_t *list, inj_randelem_t *new)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	new->re_next = list;
75*7c478bd9Sstevel@tonic-gate 	return (new);
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate inj_cmd_t *
79*7c478bd9Sstevel@tonic-gate inj_cmd_rand(inj_randelem_t *rlist)
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate 	inj_randelem_t *r;
82*7c478bd9Sstevel@tonic-gate 	inj_cmd_t *cmd;
83*7c478bd9Sstevel@tonic-gate 	uint_t prob, tmpprob;
84*7c478bd9Sstevel@tonic-gate 	int nelems, i;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	prob = 0;
87*7c478bd9Sstevel@tonic-gate 	for (i = 0, r = rlist; r != NULL; r = r->re_next, i++)
88*7c478bd9Sstevel@tonic-gate 		prob += r->re_prob;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (prob != 100) {
91*7c478bd9Sstevel@tonic-gate 		yyerror("probabilities don't sum to 100\n");
92*7c478bd9Sstevel@tonic-gate 		return (NULL);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	nelems = i;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	cmd = inj_zalloc(sizeof (inj_cmd_t));
98*7c478bd9Sstevel@tonic-gate 	cmd->cmd_type = CMD_RANDOM;
99*7c478bd9Sstevel@tonic-gate 	cmd->cmd_num = nelems;
100*7c478bd9Sstevel@tonic-gate 	cmd->cmd_rand = inj_alloc(sizeof (inj_randelem_t *) * nelems);
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	prob = 0;
103*7c478bd9Sstevel@tonic-gate 	for (r = rlist, i = 0; i < nelems; i++, r = r->re_next) {
104*7c478bd9Sstevel@tonic-gate 		tmpprob = r->re_prob;
105*7c478bd9Sstevel@tonic-gate 		r->re_prob = prob;
106*7c478bd9Sstevel@tonic-gate 		prob += tmpprob;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 		cmd->cmd_rand[i] = r;
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	return (cmd);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate inj_cmd_t *
115*7c478bd9Sstevel@tonic-gate inj_cmd_repeat(inj_cmd_t *repcmd, uint_t num)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	cmd->cmd_type = CMD_REPEAT;
120*7c478bd9Sstevel@tonic-gate 	cmd->cmd_num = num;
121*7c478bd9Sstevel@tonic-gate 	cmd->cmd_subcmd = repcmd;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	return (cmd);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate inj_cmd_t *
127*7c478bd9Sstevel@tonic-gate inj_cmd_send(inj_defn_t *ev)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	cmd->cmd_type = CMD_SEND_EVENT;
132*7c478bd9Sstevel@tonic-gate 	cmd->cmd_event = ev;
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	return (cmd);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate inj_cmd_t *
138*7c478bd9Sstevel@tonic-gate inj_cmd_sleep(uint_t secs)
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	cmd->cmd_type = CMD_SLEEP;
143*7c478bd9Sstevel@tonic-gate 	cmd->cmd_num = secs;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	return (cmd);
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate inj_cmd_t *
149*7c478bd9Sstevel@tonic-gate inj_cmd_addhrt(hrtime_t delta)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	const char *class = "resource.sunos.fmd.clock.addhrtime";
152*7c478bd9Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
153*7c478bd9Sstevel@tonic-gate 	inj_defn_t *ev = inj_zalloc(sizeof (inj_defn_t));
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	ev->defn_name = class;
156*7c478bd9Sstevel@tonic-gate 	ev->defn_lineno = yylineno;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if ((errno = nvlist_alloc(&ev->defn_nvl, NV_UNIQUE_NAME, 0)) != 0)
159*7c478bd9Sstevel@tonic-gate 		die("failed to allocate nvl for %s event", class);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if ((errno = nvlist_add_string(ev->defn_nvl, FM_CLASS, class)) != 0 ||
162*7c478bd9Sstevel@tonic-gate 	    (errno = nvlist_add_uint8(ev->defn_nvl, FM_VERSION, 1)) != 0 ||
163*7c478bd9Sstevel@tonic-gate 	    (errno = nvlist_add_int64(ev->defn_nvl, "delta", delta)) != 0)
164*7c478bd9Sstevel@tonic-gate 		die("failed to build nvl for %s event", class);
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	cmd->cmd_type = CMD_SEND_EVENT;
167*7c478bd9Sstevel@tonic-gate 	cmd->cmd_event = ev;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	return (cmd);
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate inj_cmd_t *
173*7c478bd9Sstevel@tonic-gate inj_cmd_endhrt(void)
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	return (inj_cmd_addhrt(-1LL)); /* clock underflow causes end of time */
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate static uint64_t
179*7c478bd9Sstevel@tonic-gate inj_ena(void)
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	return (((gethrtime() & ENA_FMT1_TIME_MASK) <<
182*7c478bd9Sstevel@tonic-gate 	    ENA_FMT1_TIME_SHFT) | (FM_ENA_FMT1 & ENA_FORMAT_MASK));
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate static void
186*7c478bd9Sstevel@tonic-gate cmd_run_send(const inj_mode_ops_t *mode, void *hdl, inj_defn_t *ev)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	if (!quiet) {
189*7c478bd9Sstevel@tonic-gate 		(void) printf("sending event %s ... ", ev->defn_name);
190*7c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	if (ev->defn_decl && (ev->defn_decl->decl_flags & DECL_F_AUTOENA) &&
194*7c478bd9Sstevel@tonic-gate 	    (errno = nvlist_add_uint64(ev->defn_nvl, "ena", inj_ena())) != 0)
195*7c478bd9Sstevel@tonic-gate 		warn("failed to add ena to %s", ev->defn_name);
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	if (verbose) {
198*7c478bd9Sstevel@tonic-gate 		nvlist_print(stdout, ev->defn_nvl);
199*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	mode->mo_send(hdl, ev->defn_nvl);
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	if (!quiet)
205*7c478bd9Sstevel@tonic-gate 		(void) printf("done\n");
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate static void
209*7c478bd9Sstevel@tonic-gate cmd_run_random(const inj_mode_ops_t *mode, void *hdl, inj_cmd_t *cmd)
210*7c478bd9Sstevel@tonic-gate {
211*7c478bd9Sstevel@tonic-gate 	uint_t num = lrand48() % 100;
212*7c478bd9Sstevel@tonic-gate 	int i;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < cmd->cmd_num; i++) {
215*7c478bd9Sstevel@tonic-gate 		if (cmd->cmd_rand[i]->re_prob > num)
216*7c478bd9Sstevel@tonic-gate 			break;
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	cmd_run_send(mode, hdl, cmd->cmd_rand[i - 1]->re_event);
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate static void
223*7c478bd9Sstevel@tonic-gate cmd_run(const inj_mode_ops_t *mode, void *hdl, inj_cmd_t *cmd)
224*7c478bd9Sstevel@tonic-gate {
225*7c478bd9Sstevel@tonic-gate 	switch (cmd->cmd_type) {
226*7c478bd9Sstevel@tonic-gate 	case CMD_SEND_EVENT:
227*7c478bd9Sstevel@tonic-gate 		cmd_run_send(mode, hdl, cmd->cmd_event);
228*7c478bd9Sstevel@tonic-gate 		break;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	case CMD_SLEEP:
231*7c478bd9Sstevel@tonic-gate 		(void) printf("sleeping for %d sec%s ... ",
232*7c478bd9Sstevel@tonic-gate 		    cmd->cmd_num, cmd->cmd_num > 1 ? "s" : "");
233*7c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
234*7c478bd9Sstevel@tonic-gate 		(void) sleep(cmd->cmd_num);
235*7c478bd9Sstevel@tonic-gate 		(void) printf("done\n");
236*7c478bd9Sstevel@tonic-gate 		break;
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	case CMD_RANDOM:
239*7c478bd9Sstevel@tonic-gate 		cmd_run_random(mode, hdl, cmd);
240*7c478bd9Sstevel@tonic-gate 		break;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	default:
243*7c478bd9Sstevel@tonic-gate 		warn("ignoring unknown command type: %d\n", cmd->cmd_type);
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate void
248*7c478bd9Sstevel@tonic-gate inj_program_run(inj_list_t *prog, const inj_mode_ops_t *mode, void *mode_arg)
249*7c478bd9Sstevel@tonic-gate {
250*7c478bd9Sstevel@tonic-gate 	void *hdl = mode->mo_open(mode_arg);
251*7c478bd9Sstevel@tonic-gate 	inj_cmd_t *cmd;
252*7c478bd9Sstevel@tonic-gate 	int i;
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	for (cmd = inj_list_next(prog); cmd != NULL; cmd = inj_list_next(cmd)) {
255*7c478bd9Sstevel@tonic-gate 		if (cmd->cmd_type == CMD_REPEAT) {
256*7c478bd9Sstevel@tonic-gate 			for (i = 1; i <= cmd->cmd_num; i++) {
257*7c478bd9Sstevel@tonic-gate 				if (verbose) {
258*7c478bd9Sstevel@tonic-gate 					(void) printf("(repeat %d of %d)\n",
259*7c478bd9Sstevel@tonic-gate 					    i, cmd->cmd_num);
260*7c478bd9Sstevel@tonic-gate 				}
261*7c478bd9Sstevel@tonic-gate 				cmd_run(mode, hdl, cmd->cmd_subcmd);
262*7c478bd9Sstevel@tonic-gate 			}
263*7c478bd9Sstevel@tonic-gate 		} else
264*7c478bd9Sstevel@tonic-gate 			cmd_run(mode, hdl, cmd);
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	mode->mo_close(hdl);
268*7c478bd9Sstevel@tonic-gate }
269