Home | History | Annotate | Line # | Download | only in ifconfig
lagg.c revision 1.6
      1 /*	$NetBSD: lagg.c,v 1.6 2024/04/04 08:01:55 yamaguchi Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2021 Internet Initiative Japan Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #if !defined(lint)
     31 __RCSID("$NetBSD: lagg.c,v 1.6 2024/04/04 08:01:55 yamaguchi Exp $");
     32 #endif /* !defined(lint) */
     33 
     34 #include <sys/param.h>
     35 
     36 #include <sys/ioctl.h>
     37 
     38 #include <net/if.h>
     39 #include <net/if_ether.h>
     40 #include <net/if_lagg.h>
     41 
     42 #include <ctype.h>
     43 #include <err.h>
     44 #include <errno.h>
     45 #include <string.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <util.h>
     49 
     50 #include "env.h"
     51 #include "extern.h"
     52 #include "util.h"
     53 
     54 static status_func_t	 status;
     55 static usage_func_t	 usage;
     56 static cmdloop_branch_t	 branch;
     57 
     58 static void	lagg_constructor(void) __attribute__((constructor));
     59 static void	lagg_status(prop_dictionary_t, prop_dictionary_t);
     60 static void	lagg_usage(prop_dictionary_t);
     61 
     62 static int	setlaggproto(prop_dictionary_t, prop_dictionary_t);
     63 static int	setlaggport(prop_dictionary_t, prop_dictionary_t);
     64 static int	setlagglacp(prop_dictionary_t, prop_dictionary_t);
     65 static int	setlagglacpmaxports(prop_dictionary_t, prop_dictionary_t);
     66 static int	setlaggfail(prop_dictionary_t, prop_dictionary_t);
     67 static void	lagg_status_proto(lagg_proto, struct laggreqproto *);
     68 static void	lagg_status_port(lagg_proto, struct laggreqport *);
     69 
     70 #ifdef LAGG_DEBUG
     71 static const bool	 lagg_debug = true;
     72 #else
     73 static const bool	 lagg_debug = false;
     74 #endif
     75 
     76 #define LAGG_RETRY_MAX 10
     77 
     78 static const char	*laggprotostr[LAGG_PROTO_MAX] = {
     79 	[LAGG_PROTO_NONE] = "none",
     80 	[LAGG_PROTO_LACP] = "lacp",
     81 	[LAGG_PROTO_FAILOVER] = "failover",
     82 	[LAGG_PROTO_LOADBALANCE] = "loadbalance",
     83 };
     84 
     85 enum laggportcmd {
     86 	LAGGPORT_NOCMD = 1,
     87 	LAGGPORT_ADD,
     88 	LAGGPORT_DEL,
     89 };
     90 
     91 enum lagglacpcmd {
     92 	LAGGLACP_ADD = 1,
     93 	LAGGLACP_DEL,
     94 };
     95 
     96 enum lagglacpopt {
     97 	LAGGLACPOPT_DUMPDU = 1,
     98 	LAGGLACPOPT_STOPDU,
     99 	LAGGLACPOPT_OPTIMISTIC,
    100 	LAGGLACPOPT_MULTILS,
    101 };
    102 
    103 enum laggfailopt {
    104 	LAGGFAILOPT_RXALL = 1,
    105 };
    106 
    107 struct pbranch	 laggport_root;
    108 struct pbranch	 lagglacp_root;
    109 struct pbranch	 laggfail_root;
    110 
    111 struct pstr	 laggproto = PSTR_INITIALIZER(&laggproto, "lagg-protocol",
    112 		    setlaggproto, "laggproto", &command_root.pb_parser);
    113 struct piface	 laggaddport = PIFACE_INITIALIZER(&laggaddport,
    114 		    "lagg-port-interface", setlaggport, "laggport",
    115 		    &laggport_root.pb_parser);
    116 struct piface	 laggdelport = PIFACE_INITIALIZER(&laggdelport,
    117 		    "lagg-port-interface", setlaggport, "laggport",
    118 		    &command_root.pb_parser);
    119 struct pinteger	 laggportoptpri =  PINTEGER_INITIALIZER1(&laggportoptpri,
    120 		    "lagg-port-priority", 0, UINT16_MAX, 10,
    121 		    setlaggport, "laggportpri", &laggport_root.pb_parser);
    122 struct pinteger	 lagglacpmaxports = PINTEGER_INITIALIZER1(&lagglacpmaxports,
    123 		    "lagg-lacp-maxports", 1, UINT32_MAX, 10,
    124 		    setlagglacpmaxports, "lacpmaxports",
    125 		    &lagglacp_root.pb_parser);
    126 struct pinteger	 laggportpri_num = PINTEGER_INITIALIZER1(&laggportpri_num,
    127 		    "lagg-port-priority", 0, UINT16_MAX, 10,
    128 		    setlaggport, "laggportpri", &command_root.pb_parser);
    129 struct piface	 laggportpri_if = PIFACE_INITIALIZER(&laggportpri_if,
    130 		    "lagg-port-interface", NULL, "laggport",
    131 		    &laggportpri_num.pi_parser);
    132 
    133 static const struct kwinst	 lagglacpkw[] = {
    134 	  {.k_word = "dumpdu", .k_key = "lacpdumpdu",
    135 	   .k_type = KW_T_INT, .k_int = LAGGLACPOPT_DUMPDU,
    136 	   .k_exec = setlagglacp}
    137 	, {.k_word = "-dumpdu", .k_key = "lacpdumpdu",
    138 	   .k_type = KW_T_INT, .k_int = -LAGGLACPOPT_DUMPDU,
    139 	   .k_exec = setlagglacp}
    140 	, {.k_word = "stopdu", .k_key = "lacpstopdu",
    141 	   .k_type = KW_T_INT, .k_int = LAGGLACPOPT_STOPDU,
    142 	   .k_exec = setlagglacp}
    143 	, {.k_word = "-stopdu", .k_key = "lacpstopdu",
    144 	   .k_type = KW_T_INT, .k_int = -LAGGLACPOPT_STOPDU,
    145 	   .k_exec = setlagglacp}
    146 	, {.k_word = "optimistic", .k_key = "lacpoptimistic",
    147 	   .k_type = KW_T_INT, .k_int = LAGGLACPOPT_OPTIMISTIC,
    148 	   .k_exec = setlagglacp}
    149 	, {.k_word = "-optimistic", .k_key = "lacpoptimistic",
    150 	   .k_type = KW_T_INT, .k_int = -LAGGLACPOPT_OPTIMISTIC,
    151 	   .k_exec = setlagglacp}
    152 	, {.k_word = "maxports", .k_nextparser = &lagglacpmaxports.pi_parser}
    153 	, {.k_word = "-maxports", .k_key = "lacpmaxports",
    154 	   .k_type = KW_T_INT, .k_int = 0, .k_exec = setlagglacpmaxports}
    155 	, {.k_word = "multi-linkspeed", .k_key = "lacpmultils",
    156 	   .k_type = KW_T_INT, .k_int = LAGGLACPOPT_MULTILS,
    157 	   .k_exec = setlagglacp}
    158 	, {.k_word = "-multi-linkspeed", .k_key = "lacpmultils",
    159 	   .k_type = KW_T_INT, .k_int = -LAGGLACPOPT_MULTILS,
    160 	   .k_exec = setlagglacp}
    161 };
    162 struct pkw	 lagglacp = PKW_INITIALIZER(&lagglacp, "lagg-lacp-option",
    163 		    NULL, NULL, lagglacpkw, __arraycount(lagglacpkw),
    164 		    &lagglacp_root.pb_parser);
    165 
    166 static const struct kwinst	 laggfailkw[] = {
    167 	  {.k_word = "rx-all", .k_key = "failrxall",
    168 	   .k_type = KW_T_INT, .k_int = LAGGFAILOPT_RXALL}
    169 	, {.k_word = "-rx-all", .k_key = "failrxall",
    170 	   .k_type = KW_T_INT, .k_int = -LAGGFAILOPT_RXALL}
    171 };
    172 struct pkw	 laggfail = PKW_INITIALIZER(&laggfail, "lagg-failover-option",
    173 		    setlaggfail, NULL, laggfailkw, __arraycount(laggfailkw),
    174 		    &laggfail_root.pb_parser);
    175 
    176 static const struct kwinst	 laggkw[] = {
    177 	  {.k_word = "laggproto", .k_nextparser = &laggproto.ps_parser}
    178 	, {.k_word = "-laggproto", .k_key = "laggproto", .k_type = KW_T_STR,
    179 	   .k_str = "none", .k_exec = setlaggproto}
    180 	, {.k_word = "laggport", .k_key = "laggportcmd", .k_type = KW_T_INT,
    181 	   .k_int = LAGGPORT_ADD, .k_nextparser = &laggaddport.pif_parser}
    182 	, {.k_word = "-laggport", .k_key = "laggportcmd", .k_type = KW_T_INT,
    183 	   .k_int = LAGGPORT_DEL, .k_nextparser = &laggdelport.pif_parser}
    184 	, {.k_word = "lagglacp", .k_nextparser = &lagglacp.pk_parser}
    185 	, {.k_word = "laggportpri", .k_nextparser = &laggportpri_if.pif_parser}
    186 	, {.k_word = "laggfailover", .k_nextparser = &laggfail.pk_parser}
    187 };
    188 struct pkw	 lagg = PKW_INITIALIZER(&lagg, "lagg", NULL, NULL,
    189 		    laggkw, __arraycount(laggkw), NULL);
    190 
    191 static const struct kwinst	 laggportkw[] = {
    192 	  {.k_word = "pri", .k_nextparser = &laggportoptpri.pi_parser}
    193 };
    194 struct pkw	laggportopt = PKW_INITIALIZER(&laggportopt, "lagg-port-option",
    195 		    NULL, NULL, laggportkw, __arraycount(laggportkw), NULL);
    196 
    197 struct branch	 laggport_brs[] = {
    198 	  {.b_nextparser = &laggportopt.pk_parser}
    199 	, {.b_nextparser = &command_root.pb_parser}
    200 };
    201 struct branch	 lagglacp_brs[] = {
    202 	  {.b_nextparser = &lagglacp.pk_parser}
    203 	, {.b_nextparser = &command_root.pb_parser}
    204 };
    205 struct branch	 laggfail_brs[] = {
    206 	  {.b_nextparser = &laggfail.pk_parser}
    207 	, {.b_nextparser = &command_root.pb_parser}
    208 };
    209 
    210 static void
    211 lagg_constructor(void)
    212 {
    213 	struct pbranch _laggport_root = PBRANCH_INITIALIZER(&laggport_root,
    214 	    "laggport-root", laggport_brs, __arraycount(laggport_brs), true);
    215 	struct pbranch _lagglacp_root = PBRANCH_INITIALIZER(&lagglacp_root,
    216 	    "lagglacp-root", lagglacp_brs, __arraycount(lagglacp_brs), true);
    217 	struct pbranch _laggfail_root = PBRANCH_INITIALIZER(&laggfail_root,
    218 	    "laggfail-root", laggfail_brs, __arraycount(laggfail_brs), true);
    219 
    220 	laggport_root = _laggport_root;
    221 	lagglacp_root = _lagglacp_root;
    222 	laggfail_root = _laggfail_root;
    223 
    224 	cmdloop_branch_init(&branch, &lagg.pk_parser);
    225 	status_func_init(&status, lagg_status);
    226 	usage_func_init(&usage, lagg_usage);
    227 
    228 	register_cmdloop_branch(&branch);
    229 	register_status(&status);
    230 	register_usage(&usage);
    231 }
    232 
    233 static int
    234 is_laggif(prop_dictionary_t env)
    235 {
    236 	const char *ifname;
    237 	size_t i, len;
    238 
    239 	if ((ifname = getifname(env)) == NULL)
    240 		return 0;
    241 
    242 	if (strncmp(ifname, "lagg", 4) != 0)
    243 		return 0;
    244 
    245 	len = strlen(ifname);
    246 	for (i = 4; i < len; i++) {
    247 		if (!isdigit((unsigned char)ifname[i]))
    248 			return 0;
    249 	}
    250 
    251 	return 1;
    252 }
    253 
    254 static struct lagg_req *
    255 getlagg(prop_dictionary_t env)
    256 {
    257 	struct lagg_req *req = NULL, *p;
    258 	size_t nports, bufsiz;
    259 	int i;
    260 
    261 	if (!is_laggif(env)) {
    262 		if (lagg_debug)
    263 			warnx("valid only with lagg(4) interfaces");
    264 		goto done;
    265 	}
    266 
    267 	for (i = 0, nports = 0; i < LAGG_RETRY_MAX; i++) {
    268 		bufsiz = sizeof(*req);
    269 		bufsiz += sizeof(req->lrq_reqports[0]) * nports;
    270 		p = realloc(req, bufsiz);
    271 		if (p == NULL)
    272 			break;
    273 
    274 		req = p;
    275 		memset(req, 0, bufsiz);
    276 		req->lrq_nports = nports;
    277 		if (indirect_ioctl(env, SIOCGLAGG, req) == 0)
    278 			goto done;
    279 
    280 		if (errno != ENOBUFS)
    281 			break;
    282 		nports = req->lrq_nports;
    283 	}
    284 
    285 	if (req != NULL) {
    286 		free(req);
    287 		req = NULL;
    288 	}
    289 
    290 done:
    291 	return req;
    292 }
    293 
    294 static void
    295 freelagg(struct lagg_req *req)
    296 {
    297 
    298 	free(req);
    299 }
    300 
    301 static void
    302 lagg_status(prop_dictionary_t env, prop_dictionary_t oenv)
    303 {
    304 	struct lagg_req *req;
    305 	struct laggreqport *port;
    306 	const char *proto;
    307 	char str[256];
    308 	size_t i;
    309 
    310 	req = getlagg(env);
    311 	if (req == NULL)
    312 		return;
    313 
    314 	if (req->lrq_proto >= LAGG_PROTO_MAX ||
    315 	    (proto = laggprotostr[req->lrq_proto]) == NULL) {
    316 		proto = "unknown";
    317 	}
    318 
    319 	printf("\tlaggproto %s", proto);
    320 	if (vflag)
    321 		lagg_status_proto(req->lrq_proto, &req->lrq_reqproto);
    322 	putchar('\n');
    323 
    324 	if (req->lrq_nports > 0) {
    325 		printf("\tlaggport:\n");
    326 		for (i = 0; i < req->lrq_nports; i++) {
    327 			port = &req->lrq_reqports[i];
    328 			snprintb(str, sizeof(str),
    329 			    LAGG_PORT_BITS, port->rp_flags);
    330 
    331 			printf("\t\t%.*s pri=%u flags=%s",
    332 			    IFNAMSIZ, port->rp_portname,
    333 			    (unsigned int)port->rp_prio,
    334 			    str);
    335 			if (vflag)
    336 				lagg_status_port(req->lrq_proto, port);
    337 			putchar('\n');
    338 		}
    339 	}
    340 
    341 	freelagg(req);
    342 }
    343 
    344 static int
    345 setlaggproto(prop_dictionary_t env, prop_dictionary_t oenv)
    346 {
    347 	prop_object_t obj;
    348 	struct lagg_req req;
    349 	const char *proto;
    350 	size_t i, proto_len;
    351 
    352 	memset(&req, 0, sizeof(req));
    353 
    354 	obj = prop_dictionary_get(env, "laggproto");
    355 	if (obj == NULL) {
    356 		errno = ENOENT;
    357 		return -1;
    358 	}
    359 
    360 	switch (prop_object_type(obj)) {
    361 	case PROP_TYPE_DATA:
    362 		proto = prop_data_value(obj);
    363 		proto_len = prop_data_size(obj);
    364 		break;
    365 	case PROP_TYPE_STRING:
    366 		proto = prop_string_value(obj);
    367 		proto_len = prop_string_size(obj);
    368 		break;
    369 	default:
    370 		errno = EFAULT;
    371 		return -1;
    372 	}
    373 
    374 	for (i = 0; i < LAGG_PROTO_MAX; i++) {
    375 		if (strncmp(proto, laggprotostr[i], proto_len) == 0)
    376 			break;
    377 	}
    378 
    379 	if (i >= LAGG_PROTO_MAX) {
    380 		errno = EPROTONOSUPPORT;
    381 		return -1;
    382 	}
    383 
    384 	req.lrq_ioctl = LAGGIOC_SETPROTO;
    385 	req.lrq_proto = i;
    386 
    387 	if (indirect_ioctl(env, SIOCSLAGG, &req) == -1)
    388 		return -1;
    389 
    390 	return 0;
    391 }
    392 
    393 static int
    394 setlaggport(prop_dictionary_t env, prop_dictionary_t oenv __unused)
    395 {
    396 	struct {
    397 		struct lagg_req req;
    398 		struct laggreqport port[1];
    399 	} _req;
    400 	struct lagg_req *req;
    401 	struct laggreqport *rp;
    402 	const char *ifname;
    403 	enum lagg_ioctl ioc;
    404 	int64_t lpcmd, pri;
    405 
    406 	if (!prop_dictionary_get_string(env, "laggport", &ifname)) {
    407 		if (lagg_debug)
    408 			warnx("%s.%d", __func__, __LINE__);
    409 		errno = ENOENT;
    410 		return -1;
    411 	}
    412 
    413 	memset(&_req, 0, sizeof(_req));
    414 	req = (struct lagg_req *)&_req;
    415 	rp = &req->lrq_reqports[0];
    416 
    417 	strlcpy(rp->rp_portname, ifname, sizeof(rp->rp_portname));
    418 	ioc = LAGGIOC_NOCMD;
    419 
    420 	if (prop_dictionary_get_int64(env, "laggportcmd", &lpcmd)) {
    421 		if (lpcmd == LAGGPORT_ADD) {
    422 			ioc = LAGGIOC_ADDPORT;
    423 		} else {
    424 			ioc = LAGGIOC_DELPORT;
    425 		}
    426 	}
    427 
    428 	if (prop_dictionary_get_int64(env, "laggportpri", &pri)) {
    429 		ioc = LAGGIOC_SETPORTPRI;
    430 		rp->rp_prio = (uint32_t)pri;
    431 	}
    432 
    433 	if (ioc != LAGGIOC_NOCMD) {
    434 		req->lrq_ioctl = ioc;
    435 		if (indirect_ioctl(env, SIOCSLAGG, req) == -1) {
    436 			if (lagg_debug) {
    437 				warn("cmd=%d", ioc);
    438 			}
    439 			return -1;
    440 		}
    441 	}
    442 
    443 	return 0;
    444 }
    445 
    446 static int
    447 setlagglacp(prop_dictionary_t env, prop_dictionary_t oenv __unused)
    448 {
    449 	struct lagg_req req_add, req_del;
    450 	struct laggreq_lacp *add_lacp, *del_lacp;
    451 	int64_t v;
    452 
    453 	memset(&req_add, 0, sizeof(req_add));
    454 	memset(&req_del, 0, sizeof(req_del));
    455 
    456 	req_add.lrq_proto = req_del.lrq_proto = LAGG_PROTO_LACP;
    457 	req_add.lrq_ioctl = req_del.lrq_ioctl = LAGGIOC_SETPROTOOPT;
    458 	add_lacp = &req_add.lrq_reqproto.rp_lacp;
    459 	del_lacp = &req_del.lrq_reqproto.rp_lacp;
    460 
    461 	add_lacp->command = LAGGIOC_LACPSETFLAGS;
    462 	del_lacp->command = LAGGIOC_LACPCLRFLAGS;
    463 
    464 	if (prop_dictionary_get_int64(env, "lacpdumpdu", &v)) {
    465 		if (v == LAGGLACPOPT_DUMPDU) {
    466 			add_lacp->flags |= LAGGREQLACP_DUMPDU;
    467 		} else {
    468 			del_lacp->flags |= LAGGREQLACP_DUMPDU;
    469 		}
    470 	}
    471 
    472 	if (prop_dictionary_get_int64(env, "lacpstopdu", &v)) {
    473 		if (v == LAGGLACPOPT_STOPDU) {
    474 			add_lacp->flags |= LAGGREQLACP_STOPDU;
    475 		} else {
    476 			del_lacp->flags |= LAGGREQLACP_STOPDU;
    477 		}
    478 	}
    479 
    480 	if (prop_dictionary_get_int64(env, "lacpoptimistic", &v)) {
    481 		if (v == LAGGLACPOPT_OPTIMISTIC) {
    482 			add_lacp->flags |= LAGGREQLACP_OPTIMISTIC;
    483 		} else {
    484 			del_lacp->flags |= LAGGREQLACP_OPTIMISTIC;
    485 		}
    486 	}
    487 
    488 	if (prop_dictionary_get_int64(env, "lacpmultils", &v)) {
    489 		if (v == LAGGLACPOPT_MULTILS) {
    490 			add_lacp->flags |= LAGGREQLACP_MULTILS;
    491 		} else {
    492 			del_lacp->flags |= LAGGREQLACP_MULTILS;
    493 		}
    494 	}
    495 
    496 	if (del_lacp->flags != 0) {
    497 		if (indirect_ioctl(env, SIOCSLAGG, &req_del) == -1) {
    498 			if (lagg_debug) {
    499 				warn("cmd=%d, pcmd=%d",
    500 				    req_del.lrq_ioctl,
    501 				    del_lacp->command);
    502 			}
    503 			return -1;
    504 		}
    505 	}
    506 
    507 	if (add_lacp->flags != 0) {
    508 		if (indirect_ioctl(env, SIOCSLAGG, &req_add) == -1) {
    509 			if (lagg_debug) {
    510 				warn("cmd=%d, pcmd=%d",
    511 				    req_add.lrq_ioctl,
    512 				    add_lacp->command);
    513 			}
    514 			return -1;
    515 		}
    516 	}
    517 
    518 	return 0;
    519 }
    520 
    521 static int
    522 setlagglacpmaxports(prop_dictionary_t env,
    523     prop_dictionary_t oenv __unused)
    524 {
    525 	struct lagg_req req;
    526 	struct laggreq_lacp *lrq_lacp;
    527 	int64_t v;
    528 
    529 	memset(&req, 0, sizeof(req));
    530 	req.lrq_proto = LAGG_PROTO_LACP;
    531 	req.lrq_ioctl = LAGGIOC_SETPROTOOPT;
    532 	lrq_lacp = &req.lrq_reqproto.rp_lacp;
    533 
    534 	if (!prop_dictionary_get_int64(env, "lacpmaxports", &v)) {
    535 		if (lagg_debug)
    536 			warnx("%s.%d", __func__, __LINE__);
    537 		errno = ENOENT;
    538 		return -1;
    539 	}
    540 
    541 	if (v <= 0) {
    542 		lrq_lacp->command = LAGGIOC_LACPCLRMAXPORTS;
    543 	} else if (v > 0){
    544 		lrq_lacp->command = LAGGIOC_LACPSETMAXPORTS;
    545 		lrq_lacp->maxports = (size_t)v;
    546 	}
    547 
    548 	if (indirect_ioctl(env, SIOCSLAGG, &req) == -1) {
    549 		err(EXIT_FAILURE, "SIOCSLAGGPROTO");
    550 	}
    551 
    552 	return 0;
    553 }
    554 
    555 static int
    556 setlaggfail(prop_dictionary_t env,
    557     prop_dictionary_t oenv __unused)
    558 {
    559 	struct lagg_req req_add, req_del;
    560 	struct laggreq_fail *add_fail, *del_fail;
    561 	int64_t v;
    562 
    563 	memset(&req_add, 0, sizeof(req_add));
    564 	memset(&req_del, 0, sizeof(req_del));
    565 
    566 	req_add.lrq_proto = req_del.lrq_proto = LAGG_PROTO_FAILOVER;
    567 	req_add.lrq_ioctl = req_del.lrq_ioctl = LAGGIOC_SETPROTOOPT;
    568 	add_fail = &req_add.lrq_reqproto.rp_fail;
    569 	del_fail = &req_del.lrq_reqproto.rp_fail;
    570 
    571 	add_fail->command = LAGGIOC_FAILSETFLAGS;
    572 	del_fail->command = LAGGIOC_FAILCLRFLAGS;
    573 
    574 	if (prop_dictionary_get_int64(env, "failrxall", &v)) {
    575 		if (v == LAGGFAILOPT_RXALL) {
    576 			add_fail->flags |= LAGGREQFAIL_RXALL;
    577 		} else {
    578 			del_fail->flags |= LAGGREQFAIL_RXALL;
    579 		}
    580 	}
    581 
    582 	if (del_fail->flags != 0) {
    583 		if (indirect_ioctl(env, SIOCSLAGG, &req_del) == -1) {
    584 			if (lagg_debug) {
    585 				warn("cmd=%d, pcmd=%d",
    586 				    req_del.lrq_ioctl,
    587 				    del_fail->command);
    588 			}
    589 			return -1;
    590 		}
    591 	}
    592 
    593 	if (add_fail->flags != 0) {
    594 		if (indirect_ioctl(env, SIOCSLAGG, &req_add) == -1) {
    595 			if (lagg_debug) {
    596 				warn("cmd=%d, pcmd=%d",
    597 				    req_add.lrq_ioctl,
    598 				    add_fail->command);
    599 			}
    600 			return -1;
    601 		}
    602 	}
    603 
    604 	return 0;
    605 }
    606 
    607 static void
    608 lagg_usage(prop_dictionary_t env __unused)
    609 {
    610 
    611 	fprintf(stderr, "\t[ laggproto p ]\n");
    612 	fprintf(stderr, "\t[ laggport i [ pri n ] ] "
    613 	    "[ -laggport i ]\n");
    614 	fprintf(stderr, "\t[ laggportpri i [ pri n]]\n");
    615 	fprintf(stderr, "\t[ lagglacp [ dumpdu | -dumpdu ] "
    616 	    "[ stopdu | -stopdu ]\n"
    617 	    "\t\t[ maxports n | -maxports ] [ optimistic | -optimistic ] ]\n");
    618 	fprintf(stderr, "\t[ laggfailover] [ rx-all | -rx-all ]\n");
    619 }
    620 static void
    621 lacp_format_id(char *buf, size_t len,
    622     uint16_t system_prio, uint8_t *system_mac, uint16_t system_key)
    623 {
    624 
    625 	snprintf(buf, len, "[%04X,%02X-%02X-%02X-%02X-%02X-%02X,"
    626 	    "%04X]",
    627 	    system_prio,
    628 	    (unsigned int)system_mac[0],(unsigned int)system_mac[1],
    629 	    (unsigned int)system_mac[2],(unsigned int)system_mac[3],
    630 	    (unsigned int)system_mac[4],(unsigned int)system_mac[5],
    631 	    system_key);
    632 }
    633 
    634 static void
    635 lagg_status_proto(lagg_proto pr, struct laggreqproto *req)
    636 {
    637 	struct laggreq_lacp *lacp;
    638 	char str[256];
    639 
    640 	switch (pr) {
    641 	case LAGG_PROTO_LACP:
    642 		lacp = &req->rp_lacp;
    643 
    644 		printf("\n");
    645 		snprintb(str, sizeof(str), LAGGREQLACP_BITS,
    646 		    lacp->flags);
    647 		printf("\t\tmax ports=%zu, flags=%s\n",
    648 		    lacp->maxports, str);
    649 
    650 		lacp_format_id(str, sizeof(str), lacp->actor_prio,
    651 		    lacp->actor_mac, lacp->actor_key);
    652 		printf("\t\tactor=%s\n", str);
    653 
    654 		lacp_format_id(str, sizeof(str), lacp->partner_prio,
    655 		    lacp->partner_mac, lacp->partner_key);
    656 		printf("\t\tpartner=%s", str);
    657 		break;
    658 	default:
    659 		break;
    660 	}
    661 }
    662 
    663 static void
    664 lagg_status_port(lagg_proto pr, struct laggreqport *req)
    665 {
    666 	struct laggreq_lacpport *lacp;
    667 	char str[256];
    668 
    669 	switch (pr) {
    670 	case LAGG_PROTO_LACP:
    671 		lacp = &req->rp_lacpport;
    672 
    673 		putchar('\n');
    674 
    675 		snprintb(str, sizeof(str), LACP_STATE_BITS, lacp->actor_state);
    676 		printf("\t\t\tactor: state=%s\n",str);
    677 
    678 		lacp_format_id(str, sizeof(str), lacp->partner_prio,
    679 		    lacp->partner_mac, lacp->partner_key);
    680 		printf("\t\t\tpartner=%s\n", str);
    681 		snprintb(str, sizeof(str), LACP_STATE_BITS,
    682 		    lacp->partner_state);
    683 		printf("\t\t\tpartner: port=%04X prio=%04X state=%s",
    684 		    lacp->partner_portno, lacp->partner_portprio, str);
    685 		break;
    686 	default:
    687 		break;
    688 	}
    689 }
    690