xref: /freebsd/sbin/etherswitchcfg/etherswitchcfg.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /*-
2  * Copyright (c) 2011-2012 Stefan Bethke.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <ctype.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 #include <net/if.h>
44 #include <net/if_media.h>
45 #include <dev/etherswitch/etherswitch.h>
46 
47 int	get_media_subtype(int, const char *);
48 int	get_media_mode(int, const char *);
49 int	get_media_options(int, const char *);
50 int	lookup_media_word(struct ifmedia_description *, const char *);
51 void    print_media_word(int, int);
52 void    print_media_word_ifconfig(int);
53 
54 /* some constants */
55 #define IEEE802DOT1Q_VID_MAX	4094
56 #define IFMEDIAREQ_NULISTENTRIES	256
57 
58 enum cmdmode {
59 	MODE_NONE = 0,
60 	MODE_PORT,
61 	MODE_CONFIG,
62 	MODE_VLANGROUP,
63 	MODE_REGISTER,
64 	MODE_PHYREG
65 };
66 
67 struct cfg {
68 	int					fd;
69 	int					verbose;
70 	int					mediatypes;
71 	const char			*controlfile;
72 	etherswitch_conf_t	conf;
73 	etherswitch_info_t	info;
74 	enum cmdmode		mode;
75 	int					unit;
76 };
77 
78 struct cmds {
79 	enum cmdmode	mode;
80 	const char		*name;
81 	int				args;
82 	void 			(*f)(struct cfg *, char *argv[]);
83 };
84 static struct cmds cmds[];
85 
86 /* Must match the ETHERSWITCH_PORT_LED_* enum order */
87 static const char *ledstyles[] = { "default", "on", "off", "blink", NULL };
88 
89 /*
90  * Print a value a la the %b format of the kernel's printf.
91  * Stolen from ifconfig.c.
92  */
93 static void
94 printb(const char *s, unsigned v, const char *bits)
95 {
96 	int i, any = 0;
97 	char c;
98 
99 	if (bits && *bits == 8)
100 		printf("%s=%o", s, v);
101 	else
102 		printf("%s=%x", s, v);
103 	bits++;
104 	if (bits) {
105 		putchar('<');
106 		while ((i = *bits++) != '\0') {
107 			if (v & (1 << (i-1))) {
108 				if (any)
109 					putchar(',');
110 				any = 1;
111 				for (; (c = *bits) > 32; bits++)
112 					putchar(c);
113 			} else
114 				for (; *bits > 32; bits++)
115 					;
116 		}
117 		putchar('>');
118 	}
119 }
120 
121 static int
122 read_register(struct cfg *cfg, int r)
123 {
124 	struct etherswitch_reg er;
125 
126 	er.reg = r;
127 	if (ioctl(cfg->fd, IOETHERSWITCHGETREG, &er) != 0)
128 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETREG)");
129 	return (er.val);
130 }
131 
132 static void
133 write_register(struct cfg *cfg, int r, int v)
134 {
135 	struct etherswitch_reg er;
136 
137 	er.reg = r;
138 	er.val = v;
139 	if (ioctl(cfg->fd, IOETHERSWITCHSETREG, &er) != 0)
140 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETREG)");
141 }
142 
143 static int
144 read_phyregister(struct cfg *cfg, int phy, int reg)
145 {
146 	struct etherswitch_phyreg er;
147 
148 	er.phy = phy;
149 	er.reg = reg;
150 	if (ioctl(cfg->fd, IOETHERSWITCHGETPHYREG, &er) != 0)
151 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETPHYREG)");
152 	return (er.val);
153 }
154 
155 static void
156 write_phyregister(struct cfg *cfg, int phy, int reg, int val)
157 {
158 	struct etherswitch_phyreg er;
159 
160 	er.phy = phy;
161 	er.reg = reg;
162 	er.val = val;
163 	if (ioctl(cfg->fd, IOETHERSWITCHSETPHYREG, &er) != 0)
164 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETPHYREG)");
165 }
166 
167 static void
168 set_port_vid(struct cfg *cfg, char *argv[])
169 {
170 	int v;
171 	etherswitch_port_t p;
172 
173 	v = strtol(argv[1], NULL, 0);
174 	if (v < 0 || v > IEEE802DOT1Q_VID_MAX)
175 		errx(EX_USAGE, "pvid must be between 0 and %d",
176 		    IEEE802DOT1Q_VID_MAX);
177 	bzero(&p, sizeof(p));
178 	p.es_port = cfg->unit;
179 	if (ioctl(cfg->fd, IOETHERSWITCHGETPORT, &p) != 0)
180 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETPORT)");
181 	p.es_pvid = v;
182 	if (ioctl(cfg->fd, IOETHERSWITCHSETPORT, &p) != 0)
183 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETPORT)");
184 }
185 
186 static void
187 set_port_flag(struct cfg *cfg, char *argv[])
188 {
189 	char *flag;
190 	int n;
191 	uint32_t f;
192 	etherswitch_port_t p;
193 
194 	n = 0;
195 	f = 0;
196 	flag = argv[0];
197 	if (strcmp(flag, "none") != 0) {
198 		if (*flag == '-') {
199 			n++;
200 			flag++;
201 		}
202 		if (strcasecmp(flag, "striptag") == 0)
203 			f = ETHERSWITCH_PORT_STRIPTAG;
204 		else if (strcasecmp(flag, "addtag") == 0)
205 			f = ETHERSWITCH_PORT_ADDTAG;
206 		else if (strcasecmp(flag, "firstlock") == 0)
207 			f = ETHERSWITCH_PORT_FIRSTLOCK;
208 		else if (strcasecmp(flag, "dropuntagged") == 0)
209 			f = ETHERSWITCH_PORT_DROPUNTAGGED;
210 		else if (strcasecmp(flag, "doubletag") == 0)
211 			f = ETHERSWITCH_PORT_DOUBLE_TAG;
212 		else if (strcasecmp(flag, "ingress") == 0)
213 			f = ETHERSWITCH_PORT_INGRESS;
214 	}
215 	bzero(&p, sizeof(p));
216 	p.es_port = cfg->unit;
217 	if (ioctl(cfg->fd, IOETHERSWITCHGETPORT, &p) != 0)
218 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETPORT)");
219 	if (n)
220 		p.es_flags &= ~f;
221 	else
222 		p.es_flags |= f;
223 	if (ioctl(cfg->fd, IOETHERSWITCHSETPORT, &p) != 0)
224 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETPORT)");
225 }
226 
227 static void
228 set_port_media(struct cfg *cfg, char *argv[])
229 {
230 	etherswitch_port_t p;
231 	int ifm_ulist[IFMEDIAREQ_NULISTENTRIES];
232 	int subtype;
233 
234 	bzero(&p, sizeof(p));
235 	p.es_port = cfg->unit;
236 	p.es_ifmr.ifm_ulist = ifm_ulist;
237 	p.es_ifmr.ifm_count = IFMEDIAREQ_NULISTENTRIES;
238 	if (ioctl(cfg->fd, IOETHERSWITCHGETPORT, &p) != 0)
239 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETPORT)");
240 	if (p.es_ifmr.ifm_count == 0)
241 		return;
242 	subtype = get_media_subtype(IFM_TYPE(ifm_ulist[0]), argv[1]);
243 	p.es_ifr.ifr_media = (p.es_ifmr.ifm_current & IFM_IMASK) |
244 	        IFM_TYPE(ifm_ulist[0]) | subtype;
245 	if (ioctl(cfg->fd, IOETHERSWITCHSETPORT, &p) != 0)
246 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETPORT)");
247 }
248 
249 static void
250 set_port_mediaopt(struct cfg *cfg, char *argv[])
251 {
252 	etherswitch_port_t p;
253 	int ifm_ulist[IFMEDIAREQ_NULISTENTRIES];
254 	int options;
255 
256 	bzero(&p, sizeof(p));
257 	p.es_port = cfg->unit;
258 	p.es_ifmr.ifm_ulist = ifm_ulist;
259 	p.es_ifmr.ifm_count = IFMEDIAREQ_NULISTENTRIES;
260 	if (ioctl(cfg->fd, IOETHERSWITCHGETPORT, &p) != 0)
261 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETPORT)");
262 	options = get_media_options(IFM_TYPE(ifm_ulist[0]), argv[1]);
263 	if (options == -1)
264 		errx(EX_USAGE, "invalid media options \"%s\"", argv[1]);
265 	if (options & IFM_HDX) {
266 		p.es_ifr.ifr_media &= ~IFM_FDX;
267 		options &= ~IFM_HDX;
268 	}
269 	p.es_ifr.ifr_media |= options;
270 	if (ioctl(cfg->fd, IOETHERSWITCHSETPORT, &p) != 0)
271 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETPORT)");
272 }
273 
274 static void
275 set_port_led(struct cfg *cfg, char *argv[])
276 {
277 	etherswitch_port_t p;
278 	int led;
279 	int i;
280 
281 	bzero(&p, sizeof(p));
282 	p.es_port = cfg->unit;
283 	if (ioctl(cfg->fd, IOETHERSWITCHGETPORT, &p) != 0)
284 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETPORT)");
285 
286 	led = strtol(argv[1], NULL, 0);
287 	if (led < 1 || led > p.es_nleds)
288 		errx(EX_USAGE, "invalid led number %s; must be between 1 and %d",
289 			argv[1], p.es_nleds);
290 
291 	led--;
292 
293 	for (i=0; ledstyles[i] != NULL; i++) {
294 		if (strcmp(argv[2], ledstyles[i]) == 0) {
295 			p.es_led[led] = i;
296 			break;
297 		}
298 	}
299 	if (ledstyles[i] == NULL)
300 		errx(EX_USAGE, "invalid led style \"%s\"", argv[2]);
301 
302 	if (ioctl(cfg->fd, IOETHERSWITCHSETPORT, &p) != 0)
303 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETPORT)");
304 }
305 
306 static void
307 set_vlangroup_vid(struct cfg *cfg, char *argv[])
308 {
309 	int v;
310 	etherswitch_vlangroup_t vg;
311 
312 	v = strtol(argv[1], NULL, 0);
313 	if (v < 0 || v > IEEE802DOT1Q_VID_MAX)
314 		errx(EX_USAGE, "vlan must be between 0 and %d", IEEE802DOT1Q_VID_MAX);
315 	vg.es_vlangroup = cfg->unit;
316 	if (ioctl(cfg->fd, IOETHERSWITCHGETVLANGROUP, &vg) != 0)
317 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETVLANGROUP)");
318 	vg.es_vid = v;
319 	if (ioctl(cfg->fd, IOETHERSWITCHSETVLANGROUP, &vg) != 0)
320 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETVLANGROUP)");
321 }
322 
323 static void
324 set_vlangroup_members(struct cfg *cfg, char *argv[])
325 {
326 	etherswitch_vlangroup_t vg;
327 	int member, untagged;
328 	char *c, *d;
329 	int v;
330 
331 	member = untagged = 0;
332 	if (strcmp(argv[1], "none") != 0) {
333 		for (c=argv[1]; *c; c=d) {
334 			v = strtol(c, &d, 0);
335 			if (d == c)
336 				break;
337 			if (v < 0 || v >= cfg->info.es_nports)
338 				errx(EX_USAGE, "Member port must be between 0 and %d", cfg->info.es_nports-1);
339 			if (d[0] == ',' || d[0] == '\0' ||
340 				((d[0] == 't' || d[0] == 'T') && (d[1] == ',' || d[1] == '\0'))) {
341 				if (d[0] == 't' || d[0] == 'T') {
342 					untagged &= ~ETHERSWITCH_PORTMASK(v);
343 					d++;
344 				} else
345 					untagged |= ETHERSWITCH_PORTMASK(v);
346 				member |= ETHERSWITCH_PORTMASK(v);
347 				d++;
348 			} else
349 				errx(EX_USAGE, "Invalid members specification \"%s\"", d);
350 		}
351 	}
352 	vg.es_vlangroup = cfg->unit;
353 	if (ioctl(cfg->fd, IOETHERSWITCHGETVLANGROUP, &vg) != 0)
354 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETVLANGROUP)");
355 	vg.es_member_ports = member;
356 	vg.es_untagged_ports = untagged;
357 	if (ioctl(cfg->fd, IOETHERSWITCHSETVLANGROUP, &vg) != 0)
358 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETVLANGROUP)");
359 }
360 
361 static int
362 set_register(struct cfg *cfg, char *arg)
363 {
364 	int a, v;
365 	char *c;
366 
367 	a = strtol(arg, &c, 0);
368 	if (c==arg)
369 		return (1);
370 	if (*c == '=') {
371 		v = strtoul(c+1, NULL, 0);
372 		write_register(cfg, a, v);
373 	}
374 	printf("\treg 0x%04x=0x%08x\n", a, read_register(cfg, a));
375 	return (0);
376 }
377 
378 static int
379 set_phyregister(struct cfg *cfg, char *arg)
380 {
381 	int phy, reg, val;
382 	char *c, *d;
383 
384 	phy = strtol(arg, &c, 0);
385 	if (c==arg)
386 		return (1);
387 	if (*c != '.')
388 		return (1);
389 	d = c+1;
390 	reg = strtol(d, &c, 0);
391 	if (d == c)
392 		return (1);
393 	if (*c == '=') {
394 		val = strtoul(c+1, NULL, 0);
395 		write_phyregister(cfg, phy, reg, val);
396 	}
397 	printf("\treg %d.0x%02x=0x%04x\n", phy, reg, read_phyregister(cfg, phy, reg));
398 	return (0);
399 }
400 
401 static void
402 set_vlan_mode(struct cfg *cfg, char *argv[])
403 {
404 	etherswitch_conf_t conf;
405 
406 	bzero(&conf, sizeof(conf));
407 	conf.cmd = ETHERSWITCH_CONF_VLAN_MODE;
408 	if (strcasecmp(argv[1], "isl") == 0)
409 		conf.vlan_mode = ETHERSWITCH_VLAN_ISL;
410 	else if (strcasecmp(argv[1], "port") == 0)
411 		conf.vlan_mode = ETHERSWITCH_VLAN_PORT;
412 	else if (strcasecmp(argv[1], "dot1q") == 0)
413 		conf.vlan_mode = ETHERSWITCH_VLAN_DOT1Q;
414 	else if (strcasecmp(argv[1], "dot1q4k") == 0)
415 		conf.vlan_mode = ETHERSWITCH_VLAN_DOT1Q_4K;
416 	else if (strcasecmp(argv[1], "qinq") == 0)
417 		conf.vlan_mode = ETHERSWITCH_VLAN_DOUBLE_TAG;
418 	else
419 		conf.vlan_mode = 0;
420 	if (ioctl(cfg->fd, IOETHERSWITCHSETCONF, &conf) != 0)
421 		err(EX_OSERR, "ioctl(IOETHERSWITCHSETCONF)");
422 }
423 
424 static void
425 print_config(struct cfg *cfg)
426 {
427 	const char *c;
428 
429 	/* Get the device name. */
430 	c = strrchr(cfg->controlfile, '/');
431 	if (c != NULL)
432 		c = c + 1;
433 	else
434 		c = cfg->controlfile;
435 
436 	/* Print VLAN mode. */
437 	if (cfg->conf.cmd & ETHERSWITCH_CONF_VLAN_MODE) {
438 		printf("%s: VLAN mode: ", c);
439 		switch (cfg->conf.vlan_mode) {
440 		case ETHERSWITCH_VLAN_ISL:
441 			printf("ISL\n");
442 			break;
443 		case ETHERSWITCH_VLAN_PORT:
444 			printf("PORT\n");
445 			break;
446 		case ETHERSWITCH_VLAN_DOT1Q:
447 			printf("DOT1Q\n");
448 			break;
449 		case ETHERSWITCH_VLAN_DOT1Q_4K:
450 			printf("DOT1Q4K\n");
451 			break;
452 		case ETHERSWITCH_VLAN_DOUBLE_TAG:
453 			printf("QinQ\n");
454 			break;
455 		default:
456 			printf("none\n");
457 		}
458 	}
459 }
460 
461 static void
462 print_port(struct cfg *cfg, int port)
463 {
464 	etherswitch_port_t p;
465 	int ifm_ulist[IFMEDIAREQ_NULISTENTRIES];
466 	int i;
467 
468 	bzero(&p, sizeof(p));
469 	p.es_port = port;
470 	p.es_ifmr.ifm_ulist = ifm_ulist;
471 	p.es_ifmr.ifm_count = IFMEDIAREQ_NULISTENTRIES;
472 	if (ioctl(cfg->fd, IOETHERSWITCHGETPORT, &p) != 0)
473 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETPORT)");
474 	printf("port%d:\n", port);
475 	if (cfg->conf.vlan_mode == ETHERSWITCH_VLAN_DOT1Q)
476 		printf("\tpvid: %d\n", p.es_pvid);
477 	printb("\tflags", p.es_flags, ETHERSWITCH_PORT_FLAGS_BITS);
478 	printf("\n");
479 	if (p.es_nleds) {
480 		printf("\tled: ");
481 		for (i = 0; i < p.es_nleds; i++) {
482 			printf("%d:%s%s", i+1, ledstyles[p.es_led[i]], (i==p.es_nleds-1)?"":" ");
483 		}
484 		printf("\n");
485 	}
486 	printf("\tmedia: ");
487 	print_media_word(p.es_ifmr.ifm_current, 1);
488 	if (p.es_ifmr.ifm_active != p.es_ifmr.ifm_current) {
489 		putchar(' ');
490 		putchar('(');
491 		print_media_word(p.es_ifmr.ifm_active, 0);
492 		putchar(')');
493 	}
494 	putchar('\n');
495 	printf("\tstatus: %s\n", (p.es_ifmr.ifm_status & IFM_ACTIVE) != 0 ? "active" : "no carrier");
496 	if (cfg->mediatypes) {
497 		printf("\tsupported media:\n");
498 		if (p.es_ifmr.ifm_count > IFMEDIAREQ_NULISTENTRIES)
499 			p.es_ifmr.ifm_count = IFMEDIAREQ_NULISTENTRIES;
500 		for (i=0; i<p.es_ifmr.ifm_count; i++) {
501 			printf("\t\tmedia ");
502 			print_media_word(ifm_ulist[i], 0);
503 			putchar('\n');
504 		}
505 	}
506 }
507 
508 static void
509 print_vlangroup(struct cfg *cfg, int vlangroup)
510 {
511 	etherswitch_vlangroup_t vg;
512 	int i, comma;
513 
514 	vg.es_vlangroup = vlangroup;
515 	if (ioctl(cfg->fd, IOETHERSWITCHGETVLANGROUP, &vg) != 0)
516 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETVLANGROUP)");
517 	if ((vg.es_vid & ETHERSWITCH_VID_VALID) == 0)
518 		return;
519 	vg.es_vid &= ETHERSWITCH_VID_MASK;
520 	printf("vlangroup%d:\n", vlangroup);
521 	if (cfg->conf.vlan_mode == ETHERSWITCH_VLAN_PORT)
522 		printf("\tport: %d\n", vg.es_vid);
523 	else
524 		printf("\tvlan: %d\n", vg.es_vid);
525 	printf("\tmembers ");
526 	comma = 0;
527 	if (vg.es_member_ports != 0)
528 		for (i=0; i<cfg->info.es_nports; i++) {
529 			if ((vg.es_member_ports & ETHERSWITCH_PORTMASK(i)) != 0) {
530 				if (comma)
531 					printf(",");
532 				printf("%d", i);
533 				if ((vg.es_untagged_ports & ETHERSWITCH_PORTMASK(i)) == 0)
534 					printf("t");
535 				comma = 1;
536 			}
537 		}
538 	else
539 		printf("none");
540 	printf("\n");
541 }
542 
543 static void
544 print_info(struct cfg *cfg)
545 {
546 	const char *c;
547 	int i;
548 
549 	c = strrchr(cfg->controlfile, '/');
550 	if (c != NULL)
551 		c = c + 1;
552 	else
553 		c = cfg->controlfile;
554 	if (cfg->verbose) {
555 		printf("%s: %s with %d ports and %d VLAN groups\n", c,
556 		    cfg->info.es_name, cfg->info.es_nports,
557 		    cfg->info.es_nvlangroups);
558 		printf("%s: ", c);
559 		printb("VLAN capabilities",  cfg->info.es_vlan_caps,
560 		    ETHERSWITCH_VLAN_CAPS_BITS);
561 		printf("\n");
562 	}
563 	print_config(cfg);
564 	for (i=0; i<cfg->info.es_nports; i++) {
565 		print_port(cfg, i);
566 	}
567 	for (i=0; i<cfg->info.es_nvlangroups; i++) {
568 		print_vlangroup(cfg, i);
569 	}
570 }
571 
572 static void
573 usage(struct cfg *cfg __unused, char *argv[] __unused)
574 {
575 	fprintf(stderr, "usage: etherswitchctl\n");
576 	fprintf(stderr, "\tetherswitchcfg [-f control file] info\n");
577 	fprintf(stderr, "\tetherswitchcfg [-f control file] config "
578 	    "command parameter\n");
579 	fprintf(stderr, "\t\tconfig commands: vlan_mode\n");
580 	fprintf(stderr, "\tetherswitchcfg [-f control file] phy "
581 	    "phy.register[=value]\n");
582 	fprintf(stderr, "\tetherswitchcfg [-f control file] portX "
583 	    "[flags] command parameter\n");
584 	fprintf(stderr, "\t\tport commands: pvid, media, mediaopt, led\n");
585 	fprintf(stderr, "\tetherswitchcfg [-f control file] reg "
586 	    "register[=value]\n");
587 	fprintf(stderr, "\tetherswitchcfg [-f control file] vlangroupX "
588 	    "command parameter\n");
589 	fprintf(stderr, "\t\tvlangroup commands: vlan, members\n");
590 	exit(EX_USAGE);
591 }
592 
593 static void
594 newmode(struct cfg *cfg, enum cmdmode mode)
595 {
596 	if (mode == cfg->mode)
597 		return;
598 	switch (cfg->mode) {
599 	case MODE_NONE:
600 		break;
601 	case MODE_CONFIG:
602 		/*
603 		 * Read the updated the configuration (it can be different
604 		 * from the last time we read it).
605 		 */
606 		if (ioctl(cfg->fd, IOETHERSWITCHGETCONF, &cfg->conf) != 0)
607 			err(EX_OSERR, "ioctl(IOETHERSWITCHGETCONF)");
608 		print_config(cfg);
609 		break;
610 	case MODE_PORT:
611 		print_port(cfg, cfg->unit);
612 		break;
613 	case MODE_VLANGROUP:
614 		print_vlangroup(cfg, cfg->unit);
615 		break;
616 	case MODE_REGISTER:
617 	case MODE_PHYREG:
618 		break;
619 	}
620 	cfg->mode = mode;
621 }
622 
623 int
624 main(int argc, char *argv[])
625 {
626 	int ch;
627 	struct cfg cfg;
628 	int i;
629 
630 	bzero(&cfg, sizeof(cfg));
631 	cfg.controlfile = "/dev/etherswitch0";
632 	while ((ch = getopt(argc, argv, "f:mv?")) != -1)
633 		switch(ch) {
634 		case 'f':
635 			cfg.controlfile = optarg;
636 			break;
637 		case 'm':
638 			cfg.mediatypes++;
639 			break;
640 		case 'v':
641 			cfg.verbose++;
642 			break;
643 		case '?':
644 			/* FALLTHROUGH */
645 		default:
646 			usage(&cfg, argv);
647 		}
648 	argc -= optind;
649 	argv += optind;
650 	cfg.fd = open(cfg.controlfile, O_RDONLY);
651 	if (cfg.fd < 0)
652 		err(EX_UNAVAILABLE, "Can't open control file: %s", cfg.controlfile);
653 	if (ioctl(cfg.fd, IOETHERSWITCHGETINFO, &cfg.info) != 0)
654 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETINFO)");
655 	if (ioctl(cfg.fd, IOETHERSWITCHGETCONF, &cfg.conf) != 0)
656 		err(EX_OSERR, "ioctl(IOETHERSWITCHGETCONF)");
657 	if (argc == 0) {
658 		print_info(&cfg);
659 		return (0);
660 	}
661 	cfg.mode = MODE_NONE;
662 	while (argc > 0) {
663 		switch(cfg.mode) {
664 		case MODE_NONE:
665 			if (strcmp(argv[0], "info") == 0) {
666 				print_info(&cfg);
667 			} else if (sscanf(argv[0], "port%d", &cfg.unit) == 1) {
668 				if (cfg.unit < 0 || cfg.unit >= cfg.info.es_nports)
669 					errx(EX_USAGE, "port unit must be between 0 and %d", cfg.info.es_nports - 1);
670 				newmode(&cfg, MODE_PORT);
671 			} else if (sscanf(argv[0], "vlangroup%d", &cfg.unit) == 1) {
672 				if (cfg.unit < 0 || cfg.unit >= cfg.info.es_nvlangroups)
673 					errx(EX_USAGE,
674 					    "vlangroup unit must be between 0 and %d",
675 					    cfg.info.es_nvlangroups - 1);
676 				newmode(&cfg, MODE_VLANGROUP);
677 			} else if (strcmp(argv[0], "config") == 0) {
678 				newmode(&cfg, MODE_CONFIG);
679 			} else if (strcmp(argv[0], "phy") == 0) {
680 				newmode(&cfg, MODE_PHYREG);
681 			} else if (strcmp(argv[0], "reg") == 0) {
682 				newmode(&cfg, MODE_REGISTER);
683 			} else if (strcmp(argv[0], "help") == 0) {
684 				usage(&cfg, argv);
685 			} else {
686 				errx(EX_USAGE, "Unknown command \"%s\"", argv[0]);
687 			}
688 			break;
689 		case MODE_PORT:
690 		case MODE_CONFIG:
691 		case MODE_VLANGROUP:
692 			for(i=0; cmds[i].name != NULL; i++) {
693 				if (cfg.mode == cmds[i].mode && strcmp(argv[0], cmds[i].name) == 0) {
694 					if (argc < (cmds[i].args + 1)) {
695 						printf("%s needs %d argument%s\n", cmds[i].name, cmds[i].args, (cmds[i].args==1)?"":",");
696 						break;
697 					}
698 					(cmds[i].f)(&cfg, argv);
699 					argc -= cmds[i].args;
700 					argv += cmds[i].args;
701 					break;
702 				}
703 			}
704 			if (cmds[i].name == NULL) {
705 				newmode(&cfg, MODE_NONE);
706 				continue;
707 			}
708 			break;
709 		case MODE_REGISTER:
710 			if (set_register(&cfg, argv[0]) != 0) {
711 				newmode(&cfg, MODE_NONE);
712 				continue;
713 			}
714 			break;
715 		case MODE_PHYREG:
716 			if (set_phyregister(&cfg, argv[0]) != 0) {
717 				newmode(&cfg, MODE_NONE);
718 				continue;
719 			}
720 			break;
721 		}
722 		argc--;
723 		argv++;
724 	}
725 	/* switch back to command mode to print configuration for last command */
726 	newmode(&cfg, MODE_NONE);
727 	close(cfg.fd);
728 	return (0);
729 }
730 
731 static struct cmds cmds[] = {
732 	{ MODE_PORT, "pvid", 1, set_port_vid },
733 	{ MODE_PORT, "media", 1, set_port_media },
734 	{ MODE_PORT, "mediaopt", 1, set_port_mediaopt },
735 	{ MODE_PORT, "led", 2, set_port_led },
736 	{ MODE_PORT, "addtag", 0, set_port_flag },
737 	{ MODE_PORT, "-addtag", 0, set_port_flag },
738 	{ MODE_PORT, "ingress", 0, set_port_flag },
739 	{ MODE_PORT, "-ingress", 0, set_port_flag },
740 	{ MODE_PORT, "striptag", 0, set_port_flag },
741 	{ MODE_PORT, "-striptag", 0, set_port_flag },
742 	{ MODE_PORT, "doubletag", 0, set_port_flag },
743 	{ MODE_PORT, "-doubletag", 0, set_port_flag },
744 	{ MODE_PORT, "firstlock", 0, set_port_flag },
745 	{ MODE_PORT, "-firstlock", 0, set_port_flag },
746 	{ MODE_PORT, "dropuntagged", 0, set_port_flag },
747 	{ MODE_PORT, "-dropuntagged", 0, set_port_flag },
748 	{ MODE_CONFIG, "vlan_mode", 1, set_vlan_mode },
749 	{ MODE_VLANGROUP, "vlan", 1, set_vlangroup_vid },
750 	{ MODE_VLANGROUP, "members", 1, set_vlangroup_members },
751 	{ 0, NULL, 0, NULL }
752 };
753