xref: /onnv-gate/usr/src/cmd/fm/fminject/common/inj_cmds.c (revision 10928:eb060666c73f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*10928SStephen.Hanson@Sun.COM  * Common Development and Distribution License (the "License").
6*10928SStephen.Hanson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211193Smws 
220Sstevel@tonic-gate /*
23*10928SStephen.Hanson@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * The "program" executed by the injector consists of a tree of commands.
290Sstevel@tonic-gate  * Routines in this file build and execute said command tree.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/fm/protocol.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <inj.h>
360Sstevel@tonic-gate #include <inj_event.h>
370Sstevel@tonic-gate #include <inj_lex.h>
380Sstevel@tonic-gate #include <inj_err.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * Command tree construction
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static inj_list_t inj_cmds;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate void
inj_cmds_add(inj_cmd_t * cmd)470Sstevel@tonic-gate inj_cmds_add(inj_cmd_t *cmd)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	inj_list_append(&inj_cmds, cmd);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate 
520Sstevel@tonic-gate inj_list_t *
inj_cmds_get(void)530Sstevel@tonic-gate inj_cmds_get(void)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	return (&inj_cmds);
560Sstevel@tonic-gate }
570Sstevel@tonic-gate 
580Sstevel@tonic-gate inj_randelem_t *
inj_rand_create(inj_defn_t * ev,uint_t prob)590Sstevel@tonic-gate inj_rand_create(inj_defn_t *ev, uint_t prob)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	inj_randelem_t *re = inj_zalloc(sizeof (inj_randelem_t));
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	re->re_event = ev;
640Sstevel@tonic-gate 	re->re_prob = prob;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	return (re);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate inj_randelem_t *
inj_rand_add(inj_randelem_t * list,inj_randelem_t * new)700Sstevel@tonic-gate inj_rand_add(inj_randelem_t *list, inj_randelem_t *new)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	new->re_next = list;
730Sstevel@tonic-gate 	return (new);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate inj_cmd_t *
inj_cmd_rand(inj_randelem_t * rlist)770Sstevel@tonic-gate inj_cmd_rand(inj_randelem_t *rlist)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	inj_randelem_t *r;
800Sstevel@tonic-gate 	inj_cmd_t *cmd;
810Sstevel@tonic-gate 	uint_t prob, tmpprob;
820Sstevel@tonic-gate 	int nelems, i;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	prob = 0;
850Sstevel@tonic-gate 	for (i = 0, r = rlist; r != NULL; r = r->re_next, i++)
860Sstevel@tonic-gate 		prob += r->re_prob;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (prob != 100) {
890Sstevel@tonic-gate 		yyerror("probabilities don't sum to 100\n");
900Sstevel@tonic-gate 		return (NULL);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	nelems = i;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	cmd = inj_zalloc(sizeof (inj_cmd_t));
960Sstevel@tonic-gate 	cmd->cmd_type = CMD_RANDOM;
970Sstevel@tonic-gate 	cmd->cmd_num = nelems;
980Sstevel@tonic-gate 	cmd->cmd_rand = inj_alloc(sizeof (inj_randelem_t *) * nelems);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	prob = 0;
1010Sstevel@tonic-gate 	for (r = rlist, i = 0; i < nelems; i++, r = r->re_next) {
1020Sstevel@tonic-gate 		tmpprob = r->re_prob;
1030Sstevel@tonic-gate 		r->re_prob = prob;
1040Sstevel@tonic-gate 		prob += tmpprob;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		cmd->cmd_rand[i] = r;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	return (cmd);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate inj_cmd_t *
inj_cmd_repeat(inj_cmd_t * repcmd,uint_t num)1130Sstevel@tonic-gate inj_cmd_repeat(inj_cmd_t *repcmd, uint_t num)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	cmd->cmd_type = CMD_REPEAT;
1180Sstevel@tonic-gate 	cmd->cmd_num = num;
1190Sstevel@tonic-gate 	cmd->cmd_subcmd = repcmd;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	return (cmd);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate inj_cmd_t *
inj_cmd_send(inj_defn_t * ev)1250Sstevel@tonic-gate inj_cmd_send(inj_defn_t *ev)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	cmd->cmd_type = CMD_SEND_EVENT;
1300Sstevel@tonic-gate 	cmd->cmd_event = ev;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	return (cmd);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate inj_cmd_t *
inj_cmd_sleep(uint_t secs)1360Sstevel@tonic-gate inj_cmd_sleep(uint_t secs)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	cmd->cmd_type = CMD_SLEEP;
1410Sstevel@tonic-gate 	cmd->cmd_num = secs;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	return (cmd);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate inj_cmd_t *
inj_cmd_addhrt(hrtime_t delta)1470Sstevel@tonic-gate inj_cmd_addhrt(hrtime_t delta)
1480Sstevel@tonic-gate {
1491193Smws 	const char *class = "resource.fm.fmd.clock.addhrtime";
1500Sstevel@tonic-gate 	inj_cmd_t *cmd = inj_zalloc(sizeof (inj_cmd_t));
1510Sstevel@tonic-gate 	inj_defn_t *ev = inj_zalloc(sizeof (inj_defn_t));
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	ev->defn_name = class;
1540Sstevel@tonic-gate 	ev->defn_lineno = yylineno;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if ((errno = nvlist_alloc(&ev->defn_nvl, NV_UNIQUE_NAME, 0)) != 0)
1570Sstevel@tonic-gate 		die("failed to allocate nvl for %s event", class);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if ((errno = nvlist_add_string(ev->defn_nvl, FM_CLASS, class)) != 0 ||
1600Sstevel@tonic-gate 	    (errno = nvlist_add_uint8(ev->defn_nvl, FM_VERSION, 1)) != 0 ||
1610Sstevel@tonic-gate 	    (errno = nvlist_add_int64(ev->defn_nvl, "delta", delta)) != 0)
1620Sstevel@tonic-gate 		die("failed to build nvl for %s event", class);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	cmd->cmd_type = CMD_SEND_EVENT;
1650Sstevel@tonic-gate 	cmd->cmd_event = ev;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	return (cmd);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate inj_cmd_t *
inj_cmd_endhrt(void)1710Sstevel@tonic-gate inj_cmd_endhrt(void)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	return (inj_cmd_addhrt(-1LL)); /* clock underflow causes end of time */
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static uint64_t
inj_ena(void)1770Sstevel@tonic-gate inj_ena(void)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	return (((gethrtime() & ENA_FMT1_TIME_MASK) <<
1800Sstevel@tonic-gate 	    ENA_FMT1_TIME_SHFT) | (FM_ENA_FMT1 & ENA_FORMAT_MASK));
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static void
cmd_run_send(const inj_mode_ops_t * mode,void * hdl,inj_defn_t * ev)1840Sstevel@tonic-gate cmd_run_send(const inj_mode_ops_t *mode, void *hdl, inj_defn_t *ev)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	if (!quiet) {
1870Sstevel@tonic-gate 		(void) printf("sending event %s ... ", ev->defn_name);
1880Sstevel@tonic-gate 		(void) fflush(stdout);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
191*10928SStephen.Hanson@Sun.COM 	if ((errno = nvlist_add_boolean_value(ev->defn_nvl, "__injected",
192*10928SStephen.Hanson@Sun.COM 	    1)) != 0)
193*10928SStephen.Hanson@Sun.COM 		warn("failed to add __injected to %s", ev->defn_name);
194*10928SStephen.Hanson@Sun.COM 
1950Sstevel@tonic-gate 	if (ev->defn_decl && (ev->defn_decl->decl_flags & DECL_F_AUTOENA) &&
1960Sstevel@tonic-gate 	    (errno = nvlist_add_uint64(ev->defn_nvl, "ena", inj_ena())) != 0)
1970Sstevel@tonic-gate 		warn("failed to add ena to %s", ev->defn_name);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (verbose) {
2000Sstevel@tonic-gate 		nvlist_print(stdout, ev->defn_nvl);
2010Sstevel@tonic-gate 		(void) printf("\n");
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	mode->mo_send(hdl, ev->defn_nvl);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (!quiet)
2070Sstevel@tonic-gate 		(void) printf("done\n");
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static void
cmd_run_random(const inj_mode_ops_t * mode,void * hdl,inj_cmd_t * cmd)2110Sstevel@tonic-gate cmd_run_random(const inj_mode_ops_t *mode, void *hdl, inj_cmd_t *cmd)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	uint_t num = lrand48() % 100;
2140Sstevel@tonic-gate 	int i;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	for (i = 1; i < cmd->cmd_num; i++) {
2170Sstevel@tonic-gate 		if (cmd->cmd_rand[i]->re_prob > num)
2180Sstevel@tonic-gate 			break;
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	cmd_run_send(mode, hdl, cmd->cmd_rand[i - 1]->re_event);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate static void
cmd_run(const inj_mode_ops_t * mode,void * hdl,inj_cmd_t * cmd)2250Sstevel@tonic-gate cmd_run(const inj_mode_ops_t *mode, void *hdl, inj_cmd_t *cmd)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	switch (cmd->cmd_type) {
2280Sstevel@tonic-gate 	case CMD_SEND_EVENT:
2290Sstevel@tonic-gate 		cmd_run_send(mode, hdl, cmd->cmd_event);
2300Sstevel@tonic-gate 		break;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	case CMD_SLEEP:
2330Sstevel@tonic-gate 		(void) printf("sleeping for %d sec%s ... ",
2340Sstevel@tonic-gate 		    cmd->cmd_num, cmd->cmd_num > 1 ? "s" : "");
2350Sstevel@tonic-gate 		(void) fflush(stdout);
2360Sstevel@tonic-gate 		(void) sleep(cmd->cmd_num);
2370Sstevel@tonic-gate 		(void) printf("done\n");
2380Sstevel@tonic-gate 		break;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	case CMD_RANDOM:
2410Sstevel@tonic-gate 		cmd_run_random(mode, hdl, cmd);
2420Sstevel@tonic-gate 		break;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	default:
2450Sstevel@tonic-gate 		warn("ignoring unknown command type: %d\n", cmd->cmd_type);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate void
inj_program_run(inj_list_t * prog,const inj_mode_ops_t * mode,void * mode_arg)2500Sstevel@tonic-gate inj_program_run(inj_list_t *prog, const inj_mode_ops_t *mode, void *mode_arg)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	void *hdl = mode->mo_open(mode_arg);
2530Sstevel@tonic-gate 	inj_cmd_t *cmd;
2540Sstevel@tonic-gate 	int i;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	for (cmd = inj_list_next(prog); cmd != NULL; cmd = inj_list_next(cmd)) {
2570Sstevel@tonic-gate 		if (cmd->cmd_type == CMD_REPEAT) {
2580Sstevel@tonic-gate 			for (i = 1; i <= cmd->cmd_num; i++) {
2590Sstevel@tonic-gate 				if (verbose) {
2600Sstevel@tonic-gate 					(void) printf("(repeat %d of %d)\n",
2610Sstevel@tonic-gate 					    i, cmd->cmd_num);
2620Sstevel@tonic-gate 				}
2630Sstevel@tonic-gate 				cmd_run(mode, hdl, cmd->cmd_subcmd);
2640Sstevel@tonic-gate 			}
2650Sstevel@tonic-gate 		} else
2660Sstevel@tonic-gate 			cmd_run(mode, hdl, cmd);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	mode->mo_close(hdl);
2700Sstevel@tonic-gate }
271