Beispiel #1
0
/*ARGSUSED*/
void
dlpi_walk(dlpi_walkfunc_t *fn, void *arg, uint_t flags)
{
	struct i_dlpi_walklink_arg warg;
	struct dirent *d;
	DIR *dp;
	dladm_handle_t handle;

	warg.fn = fn;
	warg.arg = arg;

	if (flags & DLPI_DEVIPNET) {
		if ((dp = opendir("/dev/ipnet")) == NULL)
			return;

		while ((d = readdir(dp)) != NULL) {
			if (d->d_name[0] == '.')
				continue;

			if (warg.fn(d->d_name, warg.arg))
				break;
		}

		(void) closedir(dp);
	} else {
		/*
		 * Rather than have libdlpi take the libdladm handle,
		 * open the handle here.
		 */
		if (dladm_open(&handle) != DLADM_STATUS_OK)
			return;

		(void) dladm_walk(i_dlpi_walk_link, handle, &warg,
		    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
		    DLADM_OPT_ACTIVE);

		dladm_close(handle);
	}
}
Beispiel #2
0
/* ARGSUSED */
static void
link_stats(const char *link, uint32_t interval)
{
	dladm_attr_t		dlattr;
	boolean_t		legacy;
	show_link_state_t	state;

	if (link != NULL && get_if_info(link, &dlattr, &legacy) < 0) {
		(void) fprintf(stderr, gettext("%s: invalid device '%s'\n"),
		    progname, link);
		exit(1);
	}
	bzero(&state, sizeof (state));

	/*
	 * If an interval is specified, continuously show the stats
	 * only for the first MAC port.
	 */
	state.ls_firstonly = (interval != 0);

	for (;;) {
		(void) printf("\t\tipackets  rbytes	 ierrors ");
		(void) printf("opackets	 obytes	     oerrors\n");

		state.ls_donefirst = B_FALSE;
		if (link == NULL)
			(void) dladm_walk(show_link_stats, &state);
		else
			show_link_stats(&state, link);

		if (interval == 0)
			break;

		(void) sleep(interval);
	}
}
Beispiel #3
0
static void
do_show_link(int argc, char *argv[])
{
	char		*name = NULL;
	int		option;
	boolean_t	s_arg = B_FALSE;
	boolean_t	i_arg = B_FALSE;
	uint32_t	interval = 0;
	show_link_state_t state;
	char		*endp = NULL;

	state.ls_stats = B_FALSE;
	state.ls_parseable = B_FALSE;

	opterr = 0;
	while ((option = getopt_long(argc, argv, ":psi:",
	    longopts, NULL)) != -1) {
		switch (option) {
		case 'p':
			state.ls_parseable = B_TRUE;
			break;
		case 's':
			if (s_arg) {
				(void) fprintf(stderr, gettext(
				    "%s: the option -s cannot be specified "
				    "more than once\n"), progname);
				usage();
			}

			s_arg = B_TRUE;
			break;
		case 'i':
			if (i_arg) {
				(void) fprintf(stderr, gettext(
				    "%s: the option -i cannot be specified "
				    "more than once\n"), progname);
				usage();
			}

			i_arg = B_TRUE;

			errno = 0;
			interval = (int)strtol(optarg, &endp, 10);
			if (errno != 0 || interval == 0 || *endp != '\0') {
				(void) fprintf(stderr,
				    gettext("%s: invalid interval value"
				    " '%d'\n"),
				    progname, interval);
				exit(1);
			}
			break;
		case ':':
			(void) fprintf(stderr,
			    gettext("%s: option requires a value '-%c'\n"),
			    progname, optopt);
			exit(1);
			/*NOTREACHED*/
		case '?':
		default:
			(void) fprintf(stderr,
			    gettext("%s: unrecognized option '-%c'\n"),
			    progname, optopt);
			exit(1);
		}
	}

	if (i_arg && !s_arg) {
		(void) fprintf(stderr, gettext("%s: the option -i "
		    "can be used only with -s\n"), progname);
		usage();
	}


	/* get link name (optional last argument) */
	if (optind == (argc-1))
		name = argv[optind];
	else if (optind != argc)
		usage();

	if (s_arg) {
		link_stats(name, interval);
		return;
	}

	if (name == NULL) {
		(void) dladm_walk(show_link, &state);
	} else {
		show_link(&state, name);
	}
}