summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ipdadm/ipdadm.c
blob: 61020ecd4c09181a616a3cc858323f4825828bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2012 Joyent, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <values.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stropts.h>
#include <zone.h>
#include <libgen.h>
#include <assert.h>

#include <libipd.h>

static char *g_pname;
static char g_zonename[ZONENAME_MAX];
static zoneid_t	g_zid;

#define	E_SUCCESS	0
#define	E_ERROR		1
#define	E_USAGE		2

typedef int (*idc_cmd_func_t)(int, char *[]);
typedef struct ipdadm_cmd {
	const char	*idc_name;	/* subcommand name */
	idc_cmd_func_t	idc_func;	/* subcommand function */
	const char	*idc_usage;	/* subcommand help */
} ipdadm_cmd_t;

static int ipdadm_list(int, char *[]);
static int ipdadm_info(int, char *[]);
static int ipdadm_corrupt(int, char *[]);
static int ipdadm_delay(int, char *[]);
static int ipdadm_drop(int, char *[]);
static int ipdadm_remove(int, char *[]);

#define	IPDADM_NCMDS	6
static ipdadm_cmd_t ipdadm_cmds[] = {
	{ "list", ipdadm_list, "list [-v]" },
	{ "info", ipdadm_info, "info" },
	{ "corrupt", ipdadm_corrupt, "corrupt <percentage>" },
	{ "delay", ipdadm_delay, "delay <microseconds>" },
	{ "drop", ipdadm_drop, "drop <percentage>" },
	{ "remove", ipdadm_remove, "remove [corrupt|delay|drop]" }
};

static int
usage(FILE *fp)
{
	int ii;
	ipdadm_cmd_t *cmd;

	(void) fprintf(fp, "Usage: %s [-z zonename] subcommand "
	    "[subcommand opts]\n\n", g_pname);
	(void) fprintf(fp, "Subcommands:\n");
	for (ii = 0; ii < IPDADM_NCMDS; ii++) {
		cmd = &ipdadm_cmds[ii];
		(void) fprintf(fp, "\t%s\n", cmd->idc_usage);
	}

	return (E_USAGE);
}

static void
ipdadm_list_one(zoneid_t z, const ipd_config_t *icp, void *arg)
{
	char zonename[ZONENAME_MAX];
	int opt_v = (int)(intptr_t)arg;

	if (getzonenamebyid(z, zonename, sizeof (zonename)) < 0)
		(void) printf("%ld", z);
	else
		(void) printf("%s", zonename);

	if (!opt_v) {
		(void) printf("\n");
		return;
	}

	(void) printf("\t%u\t%u\t%u\n", icp->ic_corrupt, icp->ic_drop,
	    icp->ic_delay);
}

static int
ipdadm_list(int argc, char *argv[])
{
	int opt_v = 0;
	int fd, rval;
	ipd_stathdl_t hdl;

	if (argc > 1)
		return (usage(stderr));

	if (argc == 1) {
		if (strcmp(argv[0], "-v") == 0)
			++opt_v;
		else
			return (usage(stderr));
	}

	fd = ipd_open(NULL);
	rval = ipd_status_read(fd, &hdl);
	(void) ipd_close(fd);

	if (rval != 0) {
		(void) fprintf(stderr, "%s: failed to get list info: %s\n",
		    g_pname, ipd_errmsg);
		return (E_ERROR);
	}

	ipd_status_foreach_zone(hdl, ipdadm_list_one, (void *)(intptr_t)opt_v);
	ipd_status_free(hdl);

	return (E_SUCCESS);
}

/*ARGSUSED*/
static int
ipdadm_info(int argc, char *argv[])
{
	int rval, fd;
	ipd_stathdl_t hdl;
	ipd_config_t *icp;

	if (argc != 0)
		return (usage(stderr));

	fd = ipd_open(NULL);
	rval = ipd_status_read(fd, &hdl);
	(void) ipd_close(fd);
	if (rval != 0) {
		(void) fprintf(stderr, "%s: failed to get info: %s\n",
		    g_pname, ipd_errmsg);
		return (E_ERROR);
	}

	if (ipd_status_get_config(hdl, g_zid, &icp) != 0) {
		if (ipd_errno == EIPD_ZC_NOENT) {
			(void) printf("zone %s does not exist or has no "
			    "ipd actions enabled\n", g_zonename);
			return (E_SUCCESS);
		}
		(void) fprintf(stderr, "%s: failed to get info: %s\n",
		    g_pname, ipd_errmsg);
		return (E_ERROR);
	}

	(void) printf("ipd information for zone %s:\n",
	    g_zonename);
	(void) printf("\tcorrupt:\t%u%% chance of packet corruption\n",
	    icp->ic_corrupt);
	(void) printf("\tdrop:\t\t%u%% chance of packet drop\n",
	    icp->ic_drop);
	(void) printf("\tdelay:\t\t%u microsecond delay per packet\n",
	    icp->ic_delay);

	ipd_status_free(hdl);

	return (E_SUCCESS);
}

static long
ipdadm_parse_long(const char *str, const char *name, long min, long max)
{
	long val;
	char *end;

	errno = 0;
	val = strtol(str, &end, 10);
	if (errno != 0) {
		(void) fprintf(stderr, "%s: invalid value for %s: %s\n",
		    g_pname, name, str);
		exit(E_ERROR);
	}

	/*
	 * We want to make sure that we got the whole string. If not that's an
	 * error. e.g. 23.42 should not be valid.
	 */
	if (*end != '\0') {
		(void) fprintf(stderr, "%s: %s value must be an integer\n",
		    g_pname, name);
		exit(E_ERROR);
	}

	if (val < min || val > max) {
		(void) fprintf(stderr, "%s: %s value must be between %ld and "
		    "%ld inclusive\n", g_pname, name, min, max);
		exit(E_ERROR);
	}

	return (val);
}

static int
ipdadm_corrupt(int argc, char *argv[])
{
	int rval, fd;
	long val;
	ipd_config_t ic;

	if (argc != 1) {
		(void) fprintf(stderr, "%s: corrupt <percentage>\n",
		    g_pname);
		return (usage(stderr));
	}

	val = ipdadm_parse_long(argv[0], "corrupt", 0, 100);
	bzero(&ic, sizeof (ic));
	ic.ic_mask = IPDM_CORRUPT;
	ic.ic_corrupt = val;

	fd = ipd_open(NULL);
	rval = ipd_ctl(fd, g_zid, &ic);
	(void) ipd_close(fd);

	if (rval != 0) {
		(void) fprintf(stderr, "%s: failed to change corrupt "
		    "value: %s\n", g_pname, ipd_errmsg);
		return (E_ERROR);
	}

	return (E_SUCCESS);
}

static int
ipdadm_delay(int argc, char *argv[])
{
	long val;
	int fd, rval;
	ipd_config_t ic;

	if (argc != 1) {
		(void) fprintf(stderr, "%s: delay <microseconds>\n",
		    g_pname);
		return (usage(stderr));
	}

	val = ipdadm_parse_long(argv[0], "delay", 0, MAXLONG);
	bzero(&ic, sizeof (ic));
	ic.ic_mask = IPDM_DELAY;
	ic.ic_delay = val;

	fd = ipd_open(NULL);
	rval = ipd_ctl(fd, g_zid, &ic);
	(void) ipd_close(fd);

	if (rval != 0) {
		(void) fprintf(stderr, "%s: failed to change delay value: %s\n",
		    g_pname, ipd_errmsg);
		return (E_ERROR);
	}

	return (E_SUCCESS);
}

static int
ipdadm_drop(int argc, char *argv[])
{
	long val;
	int fd, rval;
	ipd_config_t ic;

	if (argc != 1) {
		(void) fprintf(stderr, "%s: drop <percentage>\n",
		    g_pname);
		return (usage(stderr));
	}

	val = ipdadm_parse_long(argv[0], "drop", 0, 100);
	bzero(&ic, sizeof (ic));
	ic.ic_mask = IPDM_DROP;
	ic.ic_drop = val;

	fd = ipd_open(NULL);
	rval = ipd_ctl(fd, g_zid, &ic);
	(void) ipd_close(fd);

	if (rval != 0) {
		(void) fprintf(stderr, "%s: failed to change drop value: %s\n",
		    g_pname, ipd_errmsg);
		return (E_ERROR);
	}

	return (E_SUCCESS);
}

static int
ipdadm_remove_valid(const char *str)
{
	if (strcmp(str, "corrupt") == 0) {
		return (IPDM_CORRUPT);
	} else if (strcmp(str, "drop") == 0) {
		return (IPDM_DROP);
	} else if (strcmp(str, "delay") == 0) {
		return (IPDM_DELAY);
	}

	return (0);
}

static int
ipdadm_remove(int argc, char *argv[])
{
	ipd_config_t ic;
	char *cur, *res;
	int rval, fd;

	if (argc < 1) {
		(void) fprintf(stderr, "%s: remove <arguments>\n",
		    g_pname);
		return (usage(stderr));
	}

	if (argc > 1) {
		(void) fprintf(stderr, "%s: remove's arguments must be "
		    "comma seperated\n", g_pname);
		return (E_ERROR);
	}

	bzero(&ic, sizeof (ic));

	cur = argv[0];
	while ((res = strchr(cur, ',')) != NULL) {
		*res = '\0';
		if ((rval = ipdadm_remove_valid(cur)) == 0) {
			(void) fprintf(stderr, "%s: unknown remove "
			    "argument: %s\n", g_pname, cur);
			return (E_ERROR);
		}
		ic.ic_mask |= rval;
		cur = res + 1;
	}

	if ((rval = ipdadm_remove_valid(cur)) == 0) {
		(void) fprintf(stderr, "%s: unknown remove argument: %s\n",
		    g_pname, cur);
		return (E_ERROR);
	}
	ic.ic_mask |= rval;

	fd = ipd_open(NULL);
	rval = ipd_ctl(fd, g_zid, &ic);
	(void) ipd_close(fd);
	if (rval == -1) {
		(void) fprintf(stderr, "%s: failed to remove instances: %s\n",
		    g_pname, ipd_errmsg);
		return (E_ERROR);
	}

	return (E_SUCCESS);
}


int
main(int argc, char *argv[])
{
	int ii;
	ipdadm_cmd_t *cmd;

	g_pname = basename(argv[0]);

	if (argc < 2)
		return (usage(stderr));
	argc--;
	argv++;

	g_zid = getzoneid();
	if (strcmp("-z", argv[0]) == 0) {
		argc--;
		argv++;
		if (argc < 1) {
			(void) fprintf(stderr, "%s: -z requires an argument\n",
			    g_pname);
			return (usage(stderr));
		}

		if (g_zid != GLOBAL_ZONEID) {
			(void) fprintf(stderr, "%s: -z option only permitted "
			    "in global zone\n", g_pname);
			return (usage(stderr));
		}

		g_zid = getzoneidbyname(argv[0]);
		if (g_zid == -1) {
			(void) fprintf(stderr, "%s: %s: invalid zone\n",
			    g_pname, argv[0]);
			return (E_ERROR);
		}
		argc--;
		argv++;
	}

	if (getzonenamebyid(g_zid, g_zonename, sizeof (g_zonename)) < 0) {
		(void) fprintf(stderr, "%s: failed to get zonename: %s\n",
		    g_pname, strerror(errno));
		return (E_ERROR);
	}

	if (argc < 1)
		return (usage(stderr));

	for (ii = 0; ii < IPDADM_NCMDS; ii++) {
		cmd = &ipdadm_cmds[ii];
		if (strcmp(argv[0], cmd->idc_name) == 0) {
			argv++;
			argc--;
			assert(cmd->idc_func != NULL);
			return (cmd->idc_func(argc, argv));
		}
	}

	(void) fprintf(stderr, "%s: %s: unknown command\n", g_pname, argv[0]);
	return (usage(stderr));
}