13871Syz147064 /* 23871Syz147064 * CDDL HEADER START 33871Syz147064 * 43871Syz147064 * The contents of this file are subject to the terms of the 53871Syz147064 * Common Development and Distribution License (the "License"). 63871Syz147064 * You may not use this file except in compliance with the License. 73871Syz147064 * 83871Syz147064 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 93871Syz147064 * or http://www.opensolaris.org/os/licensing. 103871Syz147064 * See the License for the specific language governing permissions 113871Syz147064 * and limitations under the License. 123871Syz147064 * 133871Syz147064 * When distributing Covered Code, include this CDDL HEADER in each 143871Syz147064 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 153871Syz147064 * If applicable, add the following below this CDDL HEADER, with the 163871Syz147064 * fields enclosed by brackets "[]" replaced with your own identifying 173871Syz147064 * information: Portions Copyright [yyyy] [name of copyright owner] 183871Syz147064 * 193871Syz147064 * CDDL HEADER END 203871Syz147064 */ 213871Syz147064 /* 225895Syz147064 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 233871Syz147064 * Use is subject to license terms. 243871Syz147064 */ 253871Syz147064 263871Syz147064 #pragma ident "%Z%%M% %I% %E% SMI" 273871Syz147064 283871Syz147064 #include <stdio.h> 293871Syz147064 #include <sys/types.h> 303871Syz147064 #include <sys/stat.h> 313871Syz147064 #include <string.h> 323871Syz147064 #include <fcntl.h> 333871Syz147064 #include <unistd.h> 343871Syz147064 #include <stropts.h> 353871Syz147064 #include <stdlib.h> 363871Syz147064 #include <errno.h> 375895Syz147064 #include <assert.h> 383871Syz147064 #include <strings.h> 393871Syz147064 #include <libintl.h> 403871Syz147064 #include <net/if_types.h> 413871Syz147064 #include <net/if_dl.h> 425895Syz147064 #include <libdllink.h> 435895Syz147064 #include <libdlvlan.h> 443871Syz147064 #include <libdlaggr.h> 453871Syz147064 #include <libdladm_impl.h> 463871Syz147064 473871Syz147064 /* 483871Syz147064 * Link Aggregation Administration Library. 493871Syz147064 * 503871Syz147064 * This library is used by administration tools such as dladm(1M) to 513871Syz147064 * configure link aggregations. 523871Syz147064 */ 533871Syz147064 545895Syz147064 #define DLADM_AGGR_DEV "/devices/pseudo/aggr@0:" AGGR_DEVNAME_CTL 553871Syz147064 563871Syz147064 /* Limits on buffer size for LAIOC_INFO request */ 573871Syz147064 #define MIN_INFO_SIZE (4*1024) 583871Syz147064 #define MAX_INFO_SIZE (128*1024) 593871Syz147064 605895Syz147064 static uchar_t zero_mac[] = {0, 0, 0, 0, 0, 0}; 615895Syz147064 #define VALID_PORT_MAC(mac) \ 625895Syz147064 (((mac) != NULL) && (bcmp(zero_mac, (mac), ETHERADDRL) != 0) && \ 635895Syz147064 (!(mac)[0] & 0x01)) 643871Syz147064 655895Syz147064 #define PORT_DELIMITER '.' 663871Syz147064 675895Syz147064 #define WRITE_PORT(portstr, portid, size) { \ 685895Syz147064 char pstr[LINKID_STR_WIDTH + 2]; \ 695895Syz147064 (void) snprintf(pstr, LINKID_STR_WIDTH + 2, "%d%c", \ 705895Syz147064 (portid), PORT_DELIMITER); \ 715895Syz147064 (void) strlcat((portstr), pstr, (size)); \ 725895Syz147064 } 733871Syz147064 745895Syz147064 #define READ_PORT(portstr, portid, status) { \ 755895Syz147064 errno = 0; \ 765895Syz147064 (status) = DLADM_STATUS_OK; \ 775895Syz147064 (portid) = (int)strtol((portstr), &(portstr), 10); \ 785895Syz147064 if (errno != 0 || *(portstr) != PORT_DELIMITER) { \ 795895Syz147064 (status) = DLADM_STATUS_REPOSITORYINVAL; \ 805895Syz147064 } else { \ 815895Syz147064 /* Skip the delimiter. */ \ 825895Syz147064 (portstr)++; \ 835895Syz147064 } \ 845895Syz147064 } 853871Syz147064 863871Syz147064 typedef struct dladm_aggr_modify_attr { 873871Syz147064 uint32_t ld_policy; 883871Syz147064 boolean_t ld_mac_fixed; 893871Syz147064 uchar_t ld_mac[ETHERADDRL]; 903871Syz147064 aggr_lacp_mode_t ld_lacp_mode; 913871Syz147064 aggr_lacp_timer_t ld_lacp_timer; 923871Syz147064 } dladm_aggr_modify_attr_t; 933871Syz147064 943871Syz147064 typedef struct policy_s { 953871Syz147064 char *pol_name; 963871Syz147064 uint32_t policy; 973871Syz147064 } policy_t; 983871Syz147064 993871Syz147064 static policy_t policies[] = { 1003871Syz147064 {"L2", AGGR_POLICY_L2}, 1013871Syz147064 {"L3", AGGR_POLICY_L3}, 1023871Syz147064 {"L4", AGGR_POLICY_L4}}; 1033871Syz147064 1043871Syz147064 #define NPOLICIES (sizeof (policies) / sizeof (policy_t)) 1053871Syz147064 1063871Syz147064 typedef struct dladm_aggr_lacpmode_s { 1073871Syz147064 char *mode_str; 1083871Syz147064 aggr_lacp_mode_t mode_id; 1093871Syz147064 } dladm_aggr_lacpmode_t; 1103871Syz147064 1113871Syz147064 static dladm_aggr_lacpmode_t lacp_modes[] = { 1123871Syz147064 {"off", AGGR_LACP_OFF}, 1133871Syz147064 {"active", AGGR_LACP_ACTIVE}, 1143871Syz147064 {"passive", AGGR_LACP_PASSIVE}}; 1153871Syz147064 1163871Syz147064 #define NLACP_MODES (sizeof (lacp_modes) / sizeof (dladm_aggr_lacpmode_t)) 1173871Syz147064 1183871Syz147064 typedef struct dladm_aggr_lacptimer_s { 1193871Syz147064 char *lt_str; 1203871Syz147064 aggr_lacp_timer_t lt_id; 1213871Syz147064 } dladm_aggr_lacptimer_t; 1223871Syz147064 1233871Syz147064 static dladm_aggr_lacptimer_t lacp_timers[] = { 1243871Syz147064 {"short", AGGR_LACP_TIMER_SHORT}, 1253871Syz147064 {"long", AGGR_LACP_TIMER_LONG}}; 1263871Syz147064 1273871Syz147064 #define NLACP_TIMERS (sizeof (lacp_timers) / sizeof (dladm_aggr_lacptimer_t)) 1283871Syz147064 1293871Syz147064 typedef struct dladm_aggr_port_state { 1303871Syz147064 char *state_str; 1313871Syz147064 aggr_port_state_t state_id; 1323871Syz147064 } dladm_aggr_port_state_t; 1333871Syz147064 1343871Syz147064 static dladm_aggr_port_state_t port_states[] = { 1353871Syz147064 {"standby", AGGR_PORT_STATE_STANDBY }, 1363871Syz147064 {"attached", AGGR_PORT_STATE_ATTACHED } 1373871Syz147064 }; 1383871Syz147064 1393871Syz147064 #define NPORT_STATES \ 1403871Syz147064 (sizeof (port_states) / sizeof (dladm_aggr_port_state_t)) 1413871Syz147064 1425895Syz147064 static int 1435895Syz147064 i_dladm_aggr_strioctl(int cmd, void *ptr, int ilen) 1445895Syz147064 { 1455895Syz147064 int err, fd; 1463871Syz147064 1475895Syz147064 if ((fd = open(DLADM_AGGR_DEV, O_RDWR)) < 0) 1483871Syz147064 return (-1); 1493871Syz147064 1505895Syz147064 err = i_dladm_ioctl(fd, cmd, ptr, ilen); 1515895Syz147064 (void) close(fd); 1523871Syz147064 1535895Syz147064 return (err); 1543871Syz147064 } 1553871Syz147064 1563871Syz147064 /* 1575895Syz147064 * Caller must free attr.lg_ports. The ptr pointer is advanced while convert 1585895Syz147064 * the laioc_info_t to the dladm_aggr_grp_attr_t structure. 1593871Syz147064 */ 1605895Syz147064 static int 1615895Syz147064 i_dladm_aggr_iocp2grpattr(void **ptr, dladm_aggr_grp_attr_t *attrp) 1623871Syz147064 { 1635895Syz147064 laioc_info_group_t *grp; 1645895Syz147064 laioc_info_port_t *port; 1655895Syz147064 int i; 1665895Syz147064 void *where = (*ptr); 1675895Syz147064 1685895Syz147064 grp = (laioc_info_group_t *)where; 1695895Syz147064 1705895Syz147064 attrp->lg_linkid = grp->lg_linkid; 1715895Syz147064 attrp->lg_key = grp->lg_key; 1725895Syz147064 attrp->lg_nports = grp->lg_nports; 1735895Syz147064 attrp->lg_policy = grp->lg_policy; 1745895Syz147064 attrp->lg_lacp_mode = grp->lg_lacp_mode; 1755895Syz147064 attrp->lg_lacp_timer = grp->lg_lacp_timer; 1765895Syz147064 attrp->lg_force = grp->lg_force; 1775895Syz147064 1785895Syz147064 bcopy(grp->lg_mac, attrp->lg_mac, ETHERADDRL); 1795895Syz147064 attrp->lg_mac_fixed = grp->lg_mac_fixed; 1803871Syz147064 1815895Syz147064 if ((attrp->lg_ports = malloc(grp->lg_nports * 1825895Syz147064 sizeof (dladm_aggr_port_attr_t))) == NULL) { 1835895Syz147064 errno = ENOMEM; 1845895Syz147064 goto fail; 1855895Syz147064 } 1865895Syz147064 1875895Syz147064 where = (grp + 1); 1883871Syz147064 1895895Syz147064 /* 1905895Syz147064 * Go through each port that is part of the group. 1915895Syz147064 */ 1925895Syz147064 for (i = 0; i < grp->lg_nports; i++) { 1935895Syz147064 port = (laioc_info_port_t *)where; 1943871Syz147064 1955895Syz147064 attrp->lg_ports[i].lp_linkid = port->lp_linkid; 1965895Syz147064 bcopy(port->lp_mac, attrp->lg_ports[i].lp_mac, ETHERADDRL); 1975895Syz147064 attrp->lg_ports[i].lp_state = port->lp_state; 1985895Syz147064 attrp->lg_ports[i].lp_lacp_state = port->lp_lacp_state; 1995895Syz147064 2005895Syz147064 where = (port + 1); 2015895Syz147064 } 2025895Syz147064 *ptr = where; 2035895Syz147064 return (0); 2045895Syz147064 fail: 2055895Syz147064 return (-1); 2063871Syz147064 } 2073871Syz147064 2083871Syz147064 /* 2095895Syz147064 * Get active configuration of a specific aggregation. 2105895Syz147064 * Caller must free attrp->la_ports. 2113871Syz147064 */ 2125895Syz147064 static dladm_status_t 2135895Syz147064 i_dladm_aggr_info_active(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp) 2143871Syz147064 { 2153871Syz147064 laioc_info_t *ioc; 2165895Syz147064 int rc, bufsize; 2175895Syz147064 void *where; 2185895Syz147064 dladm_status_t status = DLADM_STATUS_OK; 2193871Syz147064 2203871Syz147064 bufsize = MIN_INFO_SIZE; 2213871Syz147064 ioc = (laioc_info_t *)calloc(1, bufsize); 2225895Syz147064 if (ioc == NULL) 2235895Syz147064 return (DLADM_STATUS_NOMEM); 2245895Syz147064 2255895Syz147064 ioc->li_group_linkid = linkid; 2263871Syz147064 2273871Syz147064 tryagain: 2285895Syz147064 rc = i_dladm_aggr_strioctl(LAIOC_INFO, ioc, bufsize); 2293871Syz147064 if (rc != 0) { 2303871Syz147064 if (errno == ENOSPC) { 2313871Syz147064 /* 2323871Syz147064 * The LAIOC_INFO call failed due to a short 2333871Syz147064 * buffer. Reallocate the buffer and try again. 2343871Syz147064 */ 2353871Syz147064 bufsize *= 2; 2363871Syz147064 if (bufsize <= MAX_INFO_SIZE) { 2373871Syz147064 ioc = (laioc_info_t *)realloc(ioc, bufsize); 2383871Syz147064 if (ioc != NULL) { 2393871Syz147064 bzero(ioc, sizeof (bufsize)); 2403871Syz147064 goto tryagain; 2413871Syz147064 } 2423871Syz147064 } 2433871Syz147064 } 2445895Syz147064 status = dladm_errno2status(errno); 2453871Syz147064 goto bail; 2463871Syz147064 } 2473871Syz147064 2483871Syz147064 /* 2493871Syz147064 * Go through each group returned by the aggregation driver. 2503871Syz147064 */ 2513871Syz147064 where = (char *)(ioc + 1); 2525895Syz147064 if (i_dladm_aggr_iocp2grpattr(&where, attrp) != 0) { 2535895Syz147064 status = dladm_errno2status(errno); 2545895Syz147064 goto bail; 2553871Syz147064 } 2563871Syz147064 2573871Syz147064 bail: 2583871Syz147064 free(ioc); 2593871Syz147064 return (status); 2603871Syz147064 } 2613871Syz147064 2623871Syz147064 /* 2635895Syz147064 * Get configuration information of a specific aggregation. 2645895Syz147064 * Caller must free attrp->la_ports. 2653871Syz147064 */ 2663871Syz147064 static dladm_status_t 2675895Syz147064 i_dladm_aggr_info_persist(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp) 2683871Syz147064 { 2695895Syz147064 dladm_conf_t conf; 2705895Syz147064 uint32_t nports, i; 2715895Syz147064 char *portstr, *next; 2725895Syz147064 dladm_status_t status; 2735895Syz147064 uint64_t u64; 2745895Syz147064 int size; 2755895Syz147064 char macstr[ETHERADDRL * 3]; 2765895Syz147064 2775895Syz147064 attrp->lg_linkid = linkid; 2785895Syz147064 if ((status = dladm_read_conf(linkid, &conf)) != DLADM_STATUS_OK) 2795895Syz147064 return (status); 2805895Syz147064 2815895Syz147064 status = dladm_get_conf_field(conf, FKEY, &u64, sizeof (u64)); 2825895Syz147064 if (status != DLADM_STATUS_OK) 2835895Syz147064 goto done; 2845895Syz147064 attrp->lg_key = (uint16_t)u64; 2855895Syz147064 2865895Syz147064 status = dladm_get_conf_field(conf, FPOLICY, &u64, sizeof (u64)); 2875895Syz147064 if (status != DLADM_STATUS_OK) 2885895Syz147064 goto done; 2895895Syz147064 attrp->lg_policy = (uint32_t)u64; 2905895Syz147064 2915895Syz147064 status = dladm_get_conf_field(conf, FFIXMACADDR, &attrp->lg_mac_fixed, 2925895Syz147064 sizeof (boolean_t)); 2935895Syz147064 if (status != DLADM_STATUS_OK) 2945895Syz147064 goto done; 2955895Syz147064 2965895Syz147064 if (attrp->lg_mac_fixed) { 2975895Syz147064 boolean_t fixed; 2983871Syz147064 2995895Syz147064 if ((status = dladm_get_conf_field(conf, FMACADDR, macstr, 3005895Syz147064 sizeof (macstr))) != DLADM_STATUS_OK) { 3015895Syz147064 goto done; 3025895Syz147064 } 3035895Syz147064 if (!dladm_aggr_str2macaddr(macstr, &fixed, attrp->lg_mac)) { 3045895Syz147064 status = DLADM_STATUS_REPOSITORYINVAL; 3055895Syz147064 goto done; 3065895Syz147064 } 3075895Syz147064 } 3085895Syz147064 3095895Syz147064 status = dladm_get_conf_field(conf, FFORCE, &attrp->lg_force, 3105895Syz147064 sizeof (boolean_t)); 3115895Syz147064 if (status != DLADM_STATUS_OK) 3125895Syz147064 goto done; 3135895Syz147064 3145895Syz147064 status = dladm_get_conf_field(conf, FLACPMODE, &u64, sizeof (u64)); 3155895Syz147064 if (status != DLADM_STATUS_OK) 3165895Syz147064 goto done; 3175895Syz147064 attrp->lg_lacp_mode = (aggr_lacp_mode_t)u64; 3185895Syz147064 3195895Syz147064 status = dladm_get_conf_field(conf, FLACPTIMER, &u64, sizeof (u64)); 3205895Syz147064 if (status != DLADM_STATUS_OK) 3215895Syz147064 goto done; 3225895Syz147064 attrp->lg_lacp_timer = (aggr_lacp_timer_t)u64; 3235895Syz147064 3245895Syz147064 status = dladm_get_conf_field(conf, FNPORTS, &u64, sizeof (u64)); 3255895Syz147064 if (status != DLADM_STATUS_OK) 3265895Syz147064 goto done; 3275895Syz147064 nports = (uint32_t)u64; 3285895Syz147064 attrp->lg_nports = nports; 3295895Syz147064 3305895Syz147064 size = nports * (LINKID_STR_WIDTH + 1) + 1; 3315895Syz147064 if ((portstr = calloc(1, size)) == NULL) { 3325895Syz147064 status = DLADM_STATUS_NOMEM; 3335895Syz147064 goto done; 3345895Syz147064 } 3355895Syz147064 3365895Syz147064 status = dladm_get_conf_field(conf, FPORTS, portstr, size); 3375895Syz147064 if (status != DLADM_STATUS_OK) { 3385895Syz147064 free(portstr); 3395895Syz147064 goto done; 3405895Syz147064 } 3415895Syz147064 3425895Syz147064 if ((attrp->lg_ports = malloc(nports * 3435895Syz147064 sizeof (dladm_aggr_port_attr_t))) == NULL) { 3445895Syz147064 free(portstr); 3453871Syz147064 status = DLADM_STATUS_NOMEM; 3463871Syz147064 goto done; 3473871Syz147064 } 3483871Syz147064 3495895Syz147064 for (next = portstr, i = 0; i < nports; i++) { 3505895Syz147064 READ_PORT(next, attrp->lg_ports[i].lp_linkid, status); 3515895Syz147064 if (status != DLADM_STATUS_OK) { 3525895Syz147064 free(portstr); 3535895Syz147064 free(attrp->lg_ports); 3545895Syz147064 goto done; 3555895Syz147064 } 3565895Syz147064 } 3575895Syz147064 free(portstr); 3585895Syz147064 3595895Syz147064 done: 3605895Syz147064 dladm_destroy_conf(conf); 3615895Syz147064 return (status); 3625895Syz147064 } 3635895Syz147064 3645895Syz147064 dladm_status_t 3655895Syz147064 dladm_aggr_info(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp, 3665895Syz147064 uint32_t flags) 3675895Syz147064 { 3685895Syz147064 assert(flags == DLADM_OPT_ACTIVE || flags == DLADM_OPT_PERSIST); 3695895Syz147064 if (flags == DLADM_OPT_ACTIVE) 3705895Syz147064 return (i_dladm_aggr_info_active(linkid, attrp)); 3715895Syz147064 else 3725895Syz147064 return (i_dladm_aggr_info_persist(linkid, attrp)); 3735895Syz147064 } 3743871Syz147064 3755895Syz147064 /* 3765895Syz147064 * Add or remove one or more ports to/from an existing link aggregation. 3775895Syz147064 */ 3785895Syz147064 static dladm_status_t 3795895Syz147064 i_dladm_aggr_add_rmv(datalink_id_t linkid, uint32_t nports, 3805895Syz147064 dladm_aggr_port_attr_db_t *ports, uint32_t flags, int cmd) 3815895Syz147064 { 3825895Syz147064 char *orig_portstr = NULL, *portstr = NULL; 383*6077Syz147064 laioc_add_rem_t *iocp = NULL; 3845895Syz147064 laioc_port_t *ioc_ports; 3855895Syz147064 uint32_t orig_nports, result_nports, len, i, j; 3865895Syz147064 dladm_conf_t conf; 3875895Syz147064 datalink_class_t class; 3885895Syz147064 dladm_status_t status = DLADM_STATUS_OK; 3895895Syz147064 int size; 3905895Syz147064 uint64_t u64; 3915895Syz147064 uint32_t media; 3925895Syz147064 3935895Syz147064 if (nports == 0) 3945895Syz147064 return (DLADM_STATUS_BADARG); 3955895Syz147064 3965895Syz147064 /* 3975895Syz147064 * Sanity check - aggregations can only be created over Ethernet 3985895Syz147064 * physical links. 3995895Syz147064 */ 4005895Syz147064 for (i = 0; i < nports; i++) { 4015895Syz147064 if ((dladm_datalink_id2info(ports[i].lp_linkid, NULL, 4025895Syz147064 &class, &media, NULL, 0) != DLADM_STATUS_OK) || 4035895Syz147064 (class != DATALINK_CLASS_PHYS) || (media != DL_ETHER)) { 4045895Syz147064 return (DLADM_STATUS_BADARG); 4053871Syz147064 } 4063871Syz147064 } 4073871Syz147064 4085895Syz147064 /* 4095895Syz147064 * First, update the persistent configuration if requested. We only 4105895Syz147064 * need to update the FPORTS and FNPORTS fields of this aggregation. 4115895Syz147064 * Note that FPORTS is a list of port linkids separated by 4125895Syz147064 * PORT_DELIMITER ('.'). 4135895Syz147064 */ 4145895Syz147064 if (flags & DLADM_OPT_PERSIST) { 4155895Syz147064 status = dladm_read_conf(linkid, &conf); 4165895Syz147064 if (status != DLADM_STATUS_OK) 4175895Syz147064 return (status); 4185895Syz147064 4195895Syz147064 /* 4205895Syz147064 * Get the original configuration of FNPORTS and FPORTS. 4215895Syz147064 */ 4225895Syz147064 status = dladm_get_conf_field(conf, FNPORTS, &u64, 4235895Syz147064 sizeof (u64)); 4245895Syz147064 if (status != DLADM_STATUS_OK) 4255895Syz147064 goto destroyconf; 4265895Syz147064 orig_nports = (uint32_t)u64; 4275895Syz147064 4285895Syz147064 /* 4295895Syz147064 * At least one port needs to be in the aggregation. 4305895Syz147064 */ 4315895Syz147064 if ((cmd == LAIOC_REMOVE) && (orig_nports <= nports)) { 4325895Syz147064 status = DLADM_STATUS_BADARG; 4335895Syz147064 goto destroyconf; 4345895Syz147064 } 4355895Syz147064 4365895Syz147064 size = orig_nports * (LINKID_STR_WIDTH + 1) + 1; 4375895Syz147064 if ((orig_portstr = calloc(1, size)) == NULL) { 4385895Syz147064 status = dladm_errno2status(errno); 4395895Syz147064 goto destroyconf; 4405895Syz147064 } 4415895Syz147064 4425895Syz147064 status = dladm_get_conf_field(conf, FPORTS, orig_portstr, size); 4435895Syz147064 if (status != DLADM_STATUS_OK) 4445895Syz147064 goto destroyconf; 4455895Syz147064 4465895Syz147064 result_nports = (cmd == LAIOC_ADD) ? orig_nports + nports : 4475895Syz147064 orig_nports; 4485895Syz147064 4495895Syz147064 size = result_nports * (LINKID_STR_WIDTH + 1) + 1; 4505895Syz147064 if ((portstr = calloc(1, size)) == NULL) { 4515895Syz147064 status = dladm_errno2status(errno); 4525895Syz147064 goto destroyconf; 4535895Syz147064 } 4545895Syz147064 4555895Syz147064 /* 4565895Syz147064 * get the new configuration and set to result_nports and 4575895Syz147064 * portstr. 4585895Syz147064 */ 4595895Syz147064 if (cmd == LAIOC_ADD) { 4605895Syz147064 (void) strlcpy(portstr, orig_portstr, size); 4615895Syz147064 for (i = 0; i < nports; i++) 4625895Syz147064 WRITE_PORT(portstr, ports[i].lp_linkid, size); 4635895Syz147064 } else { 4645895Syz147064 char *next; 4655895Syz147064 datalink_id_t portid; 4665895Syz147064 uint32_t remove = 0; 4675895Syz147064 4685895Syz147064 for (next = orig_portstr, j = 0; j < orig_nports; j++) { 4695895Syz147064 /* 4705895Syz147064 * Read the portids from the old configuration 4715895Syz147064 * one by one. 4725895Syz147064 */ 4735895Syz147064 READ_PORT(next, portid, status); 4745895Syz147064 if (status != DLADM_STATUS_OK) { 4755895Syz147064 free(portstr); 4765895Syz147064 goto destroyconf; 4775895Syz147064 } 4785895Syz147064 4795895Syz147064 /* 4805895Syz147064 * See whether this port is in the removal 4815895Syz147064 * list. If not, copy to the new config. 4825895Syz147064 */ 4835895Syz147064 for (i = 0; i < nports; i++) { 4845895Syz147064 if (ports[i].lp_linkid == portid) 4855895Syz147064 break; 4865895Syz147064 } 4875895Syz147064 if (i == nports) { 4885895Syz147064 WRITE_PORT(portstr, portid, size); 4895895Syz147064 } else { 4905895Syz147064 remove++; 4915895Syz147064 } 4925895Syz147064 } 4935895Syz147064 if (remove != nports) { 4945895Syz147064 status = DLADM_STATUS_LINKINVAL; 4955895Syz147064 free(portstr); 4965895Syz147064 goto destroyconf; 4975895Syz147064 } 4985895Syz147064 result_nports -= nports; 4995895Syz147064 } 5005895Syz147064 5015895Syz147064 u64 = result_nports; 5025895Syz147064 if ((status = dladm_set_conf_field(conf, FNPORTS, 5035895Syz147064 DLADM_TYPE_UINT64, &u64)) != DLADM_STATUS_OK) { 5045895Syz147064 free(portstr); 5055895Syz147064 goto destroyconf; 5065895Syz147064 } 5075895Syz147064 5085895Syz147064 status = dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR, 5095895Syz147064 portstr); 5105895Syz147064 free(portstr); 5115895Syz147064 if (status != DLADM_STATUS_OK) 5125895Syz147064 goto destroyconf; 5135895Syz147064 5145895Syz147064 /* 5155895Syz147064 * Write the new configuration to the persistent repository. 5165895Syz147064 */ 5175895Syz147064 status = dladm_write_conf(conf); 5185895Syz147064 5195895Syz147064 destroyconf: 5205895Syz147064 dladm_destroy_conf(conf); 5215895Syz147064 if (status != DLADM_STATUS_OK) { 5225895Syz147064 free(orig_portstr); 5235895Syz147064 return (status); 5245895Syz147064 } 5255895Syz147064 } 5265895Syz147064 5275895Syz147064 /* 5285895Syz147064 * If the caller only requested to update the persistent 5295895Syz147064 * configuration, we are done. 5305895Syz147064 */ 5315895Syz147064 if (!(flags & DLADM_OPT_ACTIVE)) 5325895Syz147064 goto done; 5335895Syz147064 5345895Syz147064 /* 5355895Syz147064 * Update the active configuration. 5365895Syz147064 */ 5375895Syz147064 len = sizeof (*iocp) + nports * sizeof (laioc_port_t); 5385895Syz147064 if ((iocp = malloc(len)) == NULL) { 5395895Syz147064 status = DLADM_STATUS_NOMEM; 5403871Syz147064 goto done; 5413871Syz147064 } 5423871Syz147064 5435895Syz147064 iocp->la_linkid = linkid; 5445895Syz147064 iocp->la_nports = nports; 5455895Syz147064 if (cmd == LAIOC_ADD) 5465895Syz147064 iocp->la_force = (flags & DLADM_OPT_FORCE); 5473871Syz147064 5485895Syz147064 ioc_ports = (laioc_port_t *)(iocp + 1); 5495895Syz147064 for (i = 0; i < nports; i++) 5505895Syz147064 ioc_ports[i].lp_linkid = ports[i].lp_linkid; 5515895Syz147064 5525895Syz147064 if (i_dladm_aggr_strioctl(cmd, iocp, len) < 0) 5535895Syz147064 status = dladm_errno2status(errno); 5543871Syz147064 5553871Syz147064 done: 5563871Syz147064 free(iocp); 5575895Syz147064 5585895Syz147064 /* 5595895Syz147064 * If the active configuration update fails, restore the old 5605895Syz147064 * persistent configuration if we've changed that. 5615895Syz147064 */ 5625895Syz147064 if ((status != DLADM_STATUS_OK) && (flags & DLADM_OPT_PERSIST)) { 5635895Syz147064 if (dladm_read_conf(linkid, &conf) == DLADM_STATUS_OK) { 5645895Syz147064 u64 = orig_nports; 5655895Syz147064 if ((dladm_set_conf_field(conf, FNPORTS, 5665895Syz147064 DLADM_TYPE_UINT64, &u64) == DLADM_STATUS_OK) && 5675895Syz147064 (dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR, 5685895Syz147064 orig_portstr) == DLADM_STATUS_OK)) { 5695895Syz147064 (void) dladm_write_conf(conf); 5705895Syz147064 } 5715895Syz147064 (void) dladm_destroy_conf(conf); 5725895Syz147064 } 5735895Syz147064 } 5745895Syz147064 free(orig_portstr); 5753871Syz147064 return (status); 5763871Syz147064 } 5773871Syz147064 5783871Syz147064 /* 5793871Syz147064 * Send a modify command to the link aggregation driver. 5803871Syz147064 */ 5813871Syz147064 static dladm_status_t 5825895Syz147064 i_dladm_aggr_modify_sys(datalink_id_t linkid, uint32_t mask, 5833871Syz147064 dladm_aggr_modify_attr_t *attr) 5843871Syz147064 { 5853871Syz147064 laioc_modify_t ioc; 5863871Syz147064 5875895Syz147064 ioc.lu_linkid = linkid; 5883871Syz147064 5893871Syz147064 ioc.lu_modify_mask = 0; 5903871Syz147064 if (mask & DLADM_AGGR_MODIFY_POLICY) 5913871Syz147064 ioc.lu_modify_mask |= LAIOC_MODIFY_POLICY; 5923871Syz147064 if (mask & DLADM_AGGR_MODIFY_MAC) 5933871Syz147064 ioc.lu_modify_mask |= LAIOC_MODIFY_MAC; 5943871Syz147064 if (mask & DLADM_AGGR_MODIFY_LACP_MODE) 5953871Syz147064 ioc.lu_modify_mask |= LAIOC_MODIFY_LACP_MODE; 5963871Syz147064 if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) 5973871Syz147064 ioc.lu_modify_mask |= LAIOC_MODIFY_LACP_TIMER; 5983871Syz147064 5993871Syz147064 ioc.lu_policy = attr->ld_policy; 6003871Syz147064 ioc.lu_mac_fixed = attr->ld_mac_fixed; 6013871Syz147064 bcopy(attr->ld_mac, ioc.lu_mac, ETHERADDRL); 6023871Syz147064 ioc.lu_lacp_mode = attr->ld_lacp_mode; 6033871Syz147064 ioc.lu_lacp_timer = attr->ld_lacp_timer; 6043871Syz147064 6055895Syz147064 if (i_dladm_aggr_strioctl(LAIOC_MODIFY, &ioc, sizeof (ioc)) < 0) { 6063871Syz147064 if (errno == EINVAL) 6075895Syz147064 return (DLADM_STATUS_MACADDRINVAL); 6083871Syz147064 else 6095895Syz147064 return (dladm_errno2status(errno)); 6105895Syz147064 } else { 6115895Syz147064 return (DLADM_STATUS_OK); 6123871Syz147064 } 6133871Syz147064 } 6143871Syz147064 6153871Syz147064 /* 6163871Syz147064 * Send a create command to the link aggregation driver. 6173871Syz147064 */ 6183871Syz147064 static dladm_status_t 6195895Syz147064 i_dladm_aggr_create_sys(datalink_id_t linkid, uint16_t key, uint32_t nports, 6205895Syz147064 dladm_aggr_port_attr_db_t *ports, uint32_t policy, 6215895Syz147064 boolean_t mac_addr_fixed, const uchar_t *mac_addr, 6225895Syz147064 aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer, boolean_t force) 6233871Syz147064 { 6243871Syz147064 int i, rc, len; 6255895Syz147064 laioc_create_t *iocp = NULL; 6265895Syz147064 laioc_port_t *ioc_ports; 6273871Syz147064 dladm_status_t status = DLADM_STATUS_OK; 6283871Syz147064 6295895Syz147064 len = sizeof (*iocp) + nports * sizeof (laioc_port_t); 6303871Syz147064 iocp = malloc(len); 6313871Syz147064 if (iocp == NULL) 6323871Syz147064 return (DLADM_STATUS_NOMEM); 6333871Syz147064 6345895Syz147064 iocp->lc_key = key; 6355895Syz147064 iocp->lc_linkid = linkid; 6365895Syz147064 iocp->lc_nports = nports; 6375895Syz147064 iocp->lc_policy = policy; 6385895Syz147064 iocp->lc_lacp_mode = lacp_mode; 6395895Syz147064 iocp->lc_lacp_timer = lacp_timer; 6405895Syz147064 ioc_ports = (laioc_port_t *)(iocp + 1); 6415895Syz147064 iocp->lc_force = force; 6423871Syz147064 6435895Syz147064 for (i = 0; i < nports; i++) 6445895Syz147064 ioc_ports[i].lp_linkid = ports[i].lp_linkid; 6455895Syz147064 6465895Syz147064 if (mac_addr_fixed && !VALID_PORT_MAC(mac_addr)) { 6475895Syz147064 status = DLADM_STATUS_MACADDRINVAL; 6485895Syz147064 goto done; 6493871Syz147064 } 6503871Syz147064 6515895Syz147064 bcopy(mac_addr, iocp->lc_mac, ETHERADDRL); 6525895Syz147064 iocp->lc_mac_fixed = mac_addr_fixed; 6533871Syz147064 6545895Syz147064 rc = i_dladm_aggr_strioctl(LAIOC_CREATE, iocp, len); 6555895Syz147064 if (rc < 0) 6565895Syz147064 status = dladm_errno2status(errno); 6573871Syz147064 6585895Syz147064 done: 6593871Syz147064 free(iocp); 6603871Syz147064 return (status); 6613871Syz147064 } 6623871Syz147064 6633871Syz147064 /* 6643871Syz147064 * Invoked to bring up a link aggregation group. 6653871Syz147064 */ 6665895Syz147064 static int 6675895Syz147064 i_dladm_aggr_up(datalink_id_t linkid, void *arg) 6683871Syz147064 { 6695895Syz147064 dladm_status_t *statusp = (dladm_status_t *)arg; 6705895Syz147064 dladm_aggr_grp_attr_t attr; 6715895Syz147064 dladm_aggr_port_attr_db_t *ports = NULL; 6725895Syz147064 uint16_t key = 0; 6735895Syz147064 int i, j; 6743871Syz147064 dladm_status_t status; 6753871Syz147064 6765895Syz147064 status = dladm_aggr_info(linkid, &attr, DLADM_OPT_PERSIST); 6775895Syz147064 if (status != DLADM_STATUS_OK) { 6785895Syz147064 *statusp = status; 6795895Syz147064 return (DLADM_WALK_CONTINUE); 6805895Syz147064 } 6813871Syz147064 6825895Syz147064 if (attr.lg_key <= AGGR_MAX_KEY) 6835895Syz147064 key = attr.lg_key; 6845895Syz147064 6855895Syz147064 ports = malloc(attr.lg_nports * sizeof (dladm_aggr_port_attr_db_t)); 6865895Syz147064 if (ports == NULL) { 6875895Syz147064 status = DLADM_STATUS_NOMEM; 6885895Syz147064 goto done; 6893871Syz147064 } 6903871Syz147064 6913871Syz147064 /* 6925895Syz147064 * Validate (and purge) each physical link associated with this 6935895Syz147064 * aggregation, if the specific hardware has been removed during 6945895Syz147064 * the system shutdown. 6953871Syz147064 */ 6965895Syz147064 for (i = 0, j = 0; i < attr.lg_nports; i++) { 6975895Syz147064 datalink_id_t portid = attr.lg_ports[i].lp_linkid; 6985895Syz147064 uint32_t flags; 6995895Syz147064 dladm_status_t s; 7003871Syz147064 7015895Syz147064 s = dladm_datalink_id2info(portid, &flags, NULL, NULL, NULL, 0); 7025895Syz147064 if (s != DLADM_STATUS_OK || !(flags & DLADM_OPT_ACTIVE)) 7035895Syz147064 continue; 7045895Syz147064 7055895Syz147064 ports[j++].lp_linkid = portid; 7065895Syz147064 } 7073871Syz147064 7085895Syz147064 if (j == 0) { 7095895Syz147064 /* 7105895Syz147064 * All of the physical links are removed. 7115895Syz147064 */ 7125895Syz147064 status = DLADM_STATUS_BADARG; 7135895Syz147064 goto done; 7143871Syz147064 } 7153871Syz147064 7165895Syz147064 /* 7175895Syz147064 * Create active aggregation. 7185895Syz147064 */ 7195895Syz147064 if ((status = i_dladm_aggr_create_sys(linkid, 7205895Syz147064 key, j, ports, attr.lg_policy, attr.lg_mac_fixed, 7215895Syz147064 (const uchar_t *)attr.lg_mac, attr.lg_lacp_mode, 7225895Syz147064 attr.lg_lacp_timer, attr.lg_force)) != DLADM_STATUS_OK) { 7235895Syz147064 goto done; 7245895Syz147064 } 7253871Syz147064 7265895Syz147064 if ((status = dladm_up_datalink_id(linkid)) != DLADM_STATUS_OK) { 7275895Syz147064 laioc_delete_t ioc; 7285895Syz147064 ioc.ld_linkid = linkid; 7295895Syz147064 (void) i_dladm_aggr_strioctl(LAIOC_DELETE, &ioc, sizeof (ioc)); 7305895Syz147064 goto done; 7315895Syz147064 } 7323871Syz147064 7333871Syz147064 /* 7345895Syz147064 * Reset the active linkprop of this specific link. 7353871Syz147064 */ 7365895Syz147064 (void) dladm_init_linkprop(linkid); 7373871Syz147064 7385895Syz147064 done: 7395895Syz147064 free(attr.lg_ports); 7405895Syz147064 free(ports); 7415895Syz147064 7425895Syz147064 *statusp = status; 7435895Syz147064 return (DLADM_WALK_CONTINUE); 7443871Syz147064 } 7453871Syz147064 7463871Syz147064 /* 7475895Syz147064 * Bring up one aggregation, or all persistent aggregations. In the latter 7485895Syz147064 * case, the walk may terminate early if bringup of an aggregation fails. 7493871Syz147064 */ 7505895Syz147064 dladm_status_t 7515895Syz147064 dladm_aggr_up(datalink_id_t linkid) 7523871Syz147064 { 7533871Syz147064 dladm_status_t status; 7543871Syz147064 7555895Syz147064 if (linkid == DATALINK_ALL_LINKID) { 7565895Syz147064 (void) dladm_walk_datalink_id(i_dladm_aggr_up, &status, 7575895Syz147064 DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE, 7585895Syz147064 DLADM_OPT_PERSIST); 7595895Syz147064 return (DLADM_STATUS_OK); 7605895Syz147064 } else { 7615895Syz147064 (void) i_dladm_aggr_up(linkid, &status); 7623871Syz147064 return (status); 7633871Syz147064 } 7643871Syz147064 } 7653871Syz147064 7663871Syz147064 /* 7673871Syz147064 * Given a policy string, return a policy mask. Returns B_TRUE on 7685895Syz147064 * success, or B_FALSE if an error occurred during parsing. 7693871Syz147064 */ 7703871Syz147064 boolean_t 7713871Syz147064 dladm_aggr_str2policy(const char *str, uint32_t *policy) 7723871Syz147064 { 7733871Syz147064 int i; 7743871Syz147064 policy_t *pol; 7753871Syz147064 char *token = NULL; 7763871Syz147064 char *lasts; 7773871Syz147064 7783871Syz147064 *policy = 0; 7793871Syz147064 7803871Syz147064 while ((token = strtok_r((token == NULL) ? (char *)str : NULL, ",", 7813871Syz147064 &lasts)) != NULL) { 7823871Syz147064 for (i = 0; i < NPOLICIES; i++) { 7833871Syz147064 pol = &policies[i]; 7843871Syz147064 if (strcasecmp(token, pol->pol_name) == 0) { 7853871Syz147064 *policy |= pol->policy; 7863871Syz147064 break; 7873871Syz147064 } 7883871Syz147064 } 7893871Syz147064 if (i == NPOLICIES) 7903871Syz147064 return (B_FALSE); 7913871Syz147064 } 7923871Syz147064 7933871Syz147064 return (B_TRUE); 7943871Syz147064 } 7953871Syz147064 7963871Syz147064 /* 7973871Syz147064 * Given a policy mask, returns a printable string, or NULL if the 7983871Syz147064 * policy mask is invalid. It is the responsibility of the caller to 7993871Syz147064 * free the returned string after use. 8003871Syz147064 */ 8013871Syz147064 char * 8023871Syz147064 dladm_aggr_policy2str(uint32_t policy, char *str) 8033871Syz147064 { 8043871Syz147064 int i, npolicies = 0; 8053871Syz147064 policy_t *pol; 8063871Syz147064 8075895Syz147064 if (str == NULL) 8085895Syz147064 return (NULL); 8095895Syz147064 8103871Syz147064 str[0] = '\0'; 8113871Syz147064 8123871Syz147064 for (i = 0; i < NPOLICIES; i++) { 8133871Syz147064 pol = &policies[i]; 8143871Syz147064 if ((policy & pol->policy) != 0) { 8153871Syz147064 npolicies++; 8163871Syz147064 if (npolicies > 1) 8175895Syz147064 (void) strlcat(str, ",", DLADM_STRSIZE); 8185895Syz147064 (void) strlcat(str, pol->pol_name, DLADM_STRSIZE); 8193871Syz147064 } 8203871Syz147064 } 8213871Syz147064 8223871Syz147064 return (str); 8233871Syz147064 } 8243871Syz147064 8253871Syz147064 /* 8263871Syz147064 * Given a MAC address string, return the MAC address in the mac_addr 8273871Syz147064 * array. If the MAC address was not explicitly specified, i.e. is 8283871Syz147064 * equal to 'auto', zero out mac-addr and set mac_fixed to B_TRUE. 8293871Syz147064 * Return B_FALSE if a syntax error was encountered, B_FALSE otherwise. 8303871Syz147064 */ 8313871Syz147064 boolean_t 8323871Syz147064 dladm_aggr_str2macaddr(const char *str, boolean_t *mac_fixed, uchar_t *mac_addr) 8333871Syz147064 { 8343871Syz147064 uchar_t *conv_str; 8353871Syz147064 int mac_len; 8363871Syz147064 8373871Syz147064 *mac_fixed = (strcmp(str, "auto") != 0); 8383871Syz147064 if (!*mac_fixed) { 8393871Syz147064 bzero(mac_addr, ETHERADDRL); 8403871Syz147064 return (B_TRUE); 8413871Syz147064 } 8423871Syz147064 8433871Syz147064 conv_str = _link_aton(str, &mac_len); 8443871Syz147064 if (conv_str == NULL) 8453871Syz147064 return (B_FALSE); 8463871Syz147064 8473871Syz147064 if (mac_len != ETHERADDRL) { 8483871Syz147064 free(conv_str); 8493871Syz147064 return (B_FALSE); 8503871Syz147064 } 8513871Syz147064 8523871Syz147064 if ((bcmp(zero_mac, conv_str, ETHERADDRL) == 0) || 8533871Syz147064 (conv_str[0] & 0x01)) { 8543871Syz147064 free(conv_str); 8553871Syz147064 return (B_FALSE); 8563871Syz147064 } 8573871Syz147064 8583871Syz147064 bcopy(conv_str, mac_addr, ETHERADDRL); 8593871Syz147064 free(conv_str); 8603871Syz147064 8613871Syz147064 return (B_TRUE); 8623871Syz147064 } 8633871Syz147064 8643871Syz147064 /* 8653871Syz147064 * Returns a string containing a printable representation of a MAC address. 8663871Syz147064 */ 8673871Syz147064 const char * 8685895Syz147064 dladm_aggr_macaddr2str(const unsigned char *mac, char *buf) 8693871Syz147064 { 8703871Syz147064 static char unknown_mac[] = {0, 0, 0, 0, 0, 0}; 8713871Syz147064 8723871Syz147064 if (buf == NULL) 8733871Syz147064 return (NULL); 8743871Syz147064 8753871Syz147064 if (bcmp(unknown_mac, mac, ETHERADDRL) == 0) 8765895Syz147064 (void) strlcpy(buf, "unknown", DLADM_STRSIZE); 8773871Syz147064 else 8783871Syz147064 return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER)); 8795895Syz147064 8805895Syz147064 return (buf); 8813871Syz147064 } 8823871Syz147064 8833871Syz147064 /* 8843871Syz147064 * Given a LACP mode string, find the corresponding LACP mode number. Returns 8853871Syz147064 * B_TRUE if a match was found, B_FALSE otherwise. 8863871Syz147064 */ 8873871Syz147064 boolean_t 8883871Syz147064 dladm_aggr_str2lacpmode(const char *str, aggr_lacp_mode_t *lacp_mode) 8893871Syz147064 { 8903871Syz147064 int i; 8913871Syz147064 dladm_aggr_lacpmode_t *mode; 8923871Syz147064 8933871Syz147064 for (i = 0; i < NLACP_MODES; i++) { 8943871Syz147064 mode = &lacp_modes[i]; 8953871Syz147064 if (strncasecmp(str, mode->mode_str, 8963871Syz147064 strlen(mode->mode_str)) == 0) { 8973871Syz147064 *lacp_mode = mode->mode_id; 8983871Syz147064 return (B_TRUE); 8993871Syz147064 } 9003871Syz147064 } 9013871Syz147064 9023871Syz147064 return (B_FALSE); 9033871Syz147064 } 9043871Syz147064 9053871Syz147064 /* 9063871Syz147064 * Given a LACP mode number, returns a printable string, or NULL if the 9073871Syz147064 * LACP mode number is invalid. 9083871Syz147064 */ 9093871Syz147064 const char * 9103871Syz147064 dladm_aggr_lacpmode2str(aggr_lacp_mode_t mode_id, char *buf) 9113871Syz147064 { 9123871Syz147064 int i; 9133871Syz147064 dladm_aggr_lacpmode_t *mode; 9143871Syz147064 9155895Syz147064 if (buf == NULL) 9165895Syz147064 return (NULL); 9175895Syz147064 9183871Syz147064 for (i = 0; i < NLACP_MODES; i++) { 9193871Syz147064 mode = &lacp_modes[i]; 9203871Syz147064 if (mode->mode_id == mode_id) { 9213871Syz147064 (void) snprintf(buf, DLADM_STRSIZE, "%s", 9223871Syz147064 mode->mode_str); 9233871Syz147064 return (buf); 9243871Syz147064 } 9253871Syz147064 } 9263871Syz147064 9273871Syz147064 (void) strlcpy(buf, "unknown", DLADM_STRSIZE); 9283871Syz147064 return (buf); 9293871Syz147064 } 9303871Syz147064 9313871Syz147064 /* 9323871Syz147064 * Given a LACP timer string, find the corresponding LACP timer number. Returns 9333871Syz147064 * B_TRUE if a match was found, B_FALSE otherwise. 9343871Syz147064 */ 9353871Syz147064 boolean_t 9363871Syz147064 dladm_aggr_str2lacptimer(const char *str, aggr_lacp_timer_t *lacp_timer) 9373871Syz147064 { 9383871Syz147064 int i; 9393871Syz147064 dladm_aggr_lacptimer_t *timer; 9403871Syz147064 9413871Syz147064 for (i = 0; i < NLACP_TIMERS; i++) { 9423871Syz147064 timer = &lacp_timers[i]; 9433871Syz147064 if (strncasecmp(str, timer->lt_str, 9443871Syz147064 strlen(timer->lt_str)) == 0) { 9453871Syz147064 *lacp_timer = timer->lt_id; 9463871Syz147064 return (B_TRUE); 9473871Syz147064 } 9483871Syz147064 } 9493871Syz147064 9503871Syz147064 return (B_FALSE); 9513871Syz147064 } 9523871Syz147064 9533871Syz147064 /* 9543871Syz147064 * Given a LACP timer, returns a printable string, or NULL if the 9553871Syz147064 * LACP timer number is invalid. 9563871Syz147064 */ 9573871Syz147064 const char * 9583871Syz147064 dladm_aggr_lacptimer2str(aggr_lacp_timer_t timer_id, char *buf) 9593871Syz147064 { 9603871Syz147064 int i; 9613871Syz147064 dladm_aggr_lacptimer_t *timer; 9623871Syz147064 9635895Syz147064 if (buf == NULL) 9645895Syz147064 return (NULL); 9655895Syz147064 9663871Syz147064 for (i = 0; i < NLACP_TIMERS; i++) { 9673871Syz147064 timer = &lacp_timers[i]; 9683871Syz147064 if (timer->lt_id == timer_id) { 9693871Syz147064 (void) snprintf(buf, DLADM_STRSIZE, "%s", 9703871Syz147064 timer->lt_str); 9713871Syz147064 return (buf); 9723871Syz147064 } 9733871Syz147064 } 9743871Syz147064 9753871Syz147064 (void) strlcpy(buf, "unknown", DLADM_STRSIZE); 9763871Syz147064 return (buf); 9773871Syz147064 } 9783871Syz147064 9793871Syz147064 const char * 9803871Syz147064 dladm_aggr_portstate2str(aggr_port_state_t state_id, char *buf) 9813871Syz147064 { 9823871Syz147064 int i; 9835895Syz147064 dladm_aggr_port_state_t *state; 9845895Syz147064 9855895Syz147064 if (buf == NULL) 9865895Syz147064 return (NULL); 9873871Syz147064 9883871Syz147064 for (i = 0; i < NPORT_STATES; i++) { 9893871Syz147064 state = &port_states[i]; 9903871Syz147064 if (state->state_id == state_id) { 9913871Syz147064 (void) snprintf(buf, DLADM_STRSIZE, "%s", 9923871Syz147064 state->state_str); 9933871Syz147064 return (buf); 9943871Syz147064 } 9953871Syz147064 } 9963871Syz147064 9973871Syz147064 (void) strlcpy(buf, "unknown", DLADM_STRSIZE); 9983871Syz147064 return (buf); 9993871Syz147064 } 10003871Syz147064 10015895Syz147064 static dladm_status_t 10025895Syz147064 dladm_aggr_persist_aggr_conf(const char *link, datalink_id_t linkid, 10035895Syz147064 uint16_t key, uint32_t nports, dladm_aggr_port_attr_db_t *ports, 10045895Syz147064 uint32_t policy, boolean_t mac_addr_fixed, const uchar_t *mac_addr, 10055895Syz147064 aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer, 10065895Syz147064 boolean_t force) 10073871Syz147064 { 10085895Syz147064 dladm_conf_t conf = DLADM_INVALID_CONF; 10095895Syz147064 char *portstr = NULL; 10105895Syz147064 char macstr[ETHERADDRL * 3]; 10115895Syz147064 dladm_status_t status; 10125895Syz147064 int i, size; 10135895Syz147064 uint64_t u64; 10143871Syz147064 10155895Syz147064 if ((status = dladm_create_conf(link, linkid, DATALINK_CLASS_AGGR, 10165895Syz147064 DL_ETHER, &conf)) != DLADM_STATUS_OK) { 10173871Syz147064 return (status); 10183871Syz147064 } 10193871Syz147064 10205895Syz147064 u64 = key; 10215895Syz147064 status = dladm_set_conf_field(conf, FKEY, DLADM_TYPE_UINT64, &u64); 10225895Syz147064 if (status != DLADM_STATUS_OK) 10235895Syz147064 goto done; 10243871Syz147064 10255895Syz147064 u64 = nports; 10265895Syz147064 status = dladm_set_conf_field(conf, FNPORTS, DLADM_TYPE_UINT64, &u64); 10275895Syz147064 if (status != DLADM_STATUS_OK) 10285895Syz147064 goto done; 10295895Syz147064 10305895Syz147064 size = nports * (LINKID_STR_WIDTH + 1) + 1; 10315895Syz147064 if ((portstr = calloc(1, size)) == NULL) { 10325895Syz147064 status = DLADM_STATUS_NOMEM; 10335895Syz147064 goto done; 10345895Syz147064 } 10353871Syz147064 10365895Syz147064 for (i = 0; i < nports; i++) 10375895Syz147064 WRITE_PORT(portstr, ports[i].lp_linkid, size); 10385895Syz147064 status = dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR, portstr); 10395895Syz147064 free(portstr); 10405895Syz147064 10415895Syz147064 if (status != DLADM_STATUS_OK) 10425895Syz147064 goto done; 10433871Syz147064 10445895Syz147064 u64 = policy; 10455895Syz147064 status = dladm_set_conf_field(conf, FPOLICY, DLADM_TYPE_UINT64, &u64); 10465895Syz147064 if (status != DLADM_STATUS_OK) 10475895Syz147064 goto done; 10485895Syz147064 10495895Syz147064 status = dladm_set_conf_field(conf, FFIXMACADDR, DLADM_TYPE_BOOLEAN, 10505895Syz147064 &mac_addr_fixed); 10515895Syz147064 if (status != DLADM_STATUS_OK) 10525895Syz147064 goto done; 10535895Syz147064 10545895Syz147064 if (mac_addr_fixed) { 10555895Syz147064 if (!VALID_PORT_MAC(mac_addr)) { 10565895Syz147064 status = DLADM_STATUS_MACADDRINVAL; 10573871Syz147064 goto done; 10583871Syz147064 } 10593871Syz147064 10605895Syz147064 (void) dladm_aggr_macaddr2str(mac_addr, macstr); 10615895Syz147064 status = dladm_set_conf_field(conf, FMACADDR, DLADM_TYPE_STR, 10625895Syz147064 macstr); 10635895Syz147064 if (status != DLADM_STATUS_OK) 10643871Syz147064 goto done; 10653871Syz147064 } 10663871Syz147064 10675895Syz147064 status = dladm_set_conf_field(conf, FFORCE, DLADM_TYPE_BOOLEAN, &force); 10685895Syz147064 if (status != DLADM_STATUS_OK) 10695895Syz147064 goto done; 10705895Syz147064 10715895Syz147064 u64 = lacp_mode; 10725895Syz147064 status = dladm_set_conf_field(conf, FLACPMODE, DLADM_TYPE_UINT64, &u64); 10735895Syz147064 if (status != DLADM_STATUS_OK) 10745895Syz147064 goto done; 10755895Syz147064 10765895Syz147064 u64 = lacp_timer; 10775895Syz147064 status = dladm_set_conf_field(conf, FLACPTIMER, DLADM_TYPE_UINT64, 10785895Syz147064 &u64); 10795895Syz147064 if (status != DLADM_STATUS_OK) 10805895Syz147064 goto done; 10815895Syz147064 10823871Syz147064 /* 10835895Syz147064 * Commit the link aggregation configuration. 10843871Syz147064 */ 10855895Syz147064 status = dladm_write_conf(conf); 10863871Syz147064 10873871Syz147064 done: 10885895Syz147064 dladm_destroy_conf(conf); 10893871Syz147064 return (status); 10903871Syz147064 } 10913871Syz147064 10923871Syz147064 /* 10933871Syz147064 * Create a new link aggregation group. Update the configuration 10943871Syz147064 * file and bring it up. 10953871Syz147064 */ 10963871Syz147064 dladm_status_t 10975895Syz147064 dladm_aggr_create(const char *name, uint16_t key, uint32_t nports, 10983871Syz147064 dladm_aggr_port_attr_db_t *ports, uint32_t policy, boolean_t mac_addr_fixed, 10995895Syz147064 const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode, 11005895Syz147064 aggr_lacp_timer_t lacp_timer, uint32_t flags) 11013871Syz147064 { 11025895Syz147064 datalink_id_t linkid = DATALINK_INVALID_LINKID; 11035895Syz147064 uint32_t media; 11045895Syz147064 uint32_t i; 11055895Syz147064 datalink_class_t class; 11063871Syz147064 dladm_status_t status; 11075895Syz147064 boolean_t force = (flags & DLADM_OPT_FORCE) ? B_TRUE : B_FALSE; 11083871Syz147064 11095895Syz147064 if (key != 0 && key > AGGR_MAX_KEY) 11103871Syz147064 return (DLADM_STATUS_KEYINVAL); 11113871Syz147064 11125895Syz147064 if (nports == 0) 11135895Syz147064 return (DLADM_STATUS_BADARG); 11145895Syz147064 11155895Syz147064 for (i = 0; i < nports; i++) { 11165895Syz147064 if ((dladm_datalink_id2info(ports[i].lp_linkid, NULL, 11175895Syz147064 &class, &media, NULL, 0) != DLADM_STATUS_OK) || 11185895Syz147064 (class != DATALINK_CLASS_PHYS) && (media != DL_ETHER)) { 11195895Syz147064 return (DLADM_STATUS_BADARG); 11205895Syz147064 } 11215895Syz147064 } 11225895Syz147064 11235895Syz147064 flags &= (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST); 11245895Syz147064 if ((status = dladm_create_datalink_id(name, DATALINK_CLASS_AGGR, 11255895Syz147064 DL_ETHER, flags, &linkid)) != DLADM_STATUS_OK) { 11265895Syz147064 goto fail; 11275895Syz147064 } 11285895Syz147064 11295895Syz147064 if ((flags & DLADM_OPT_PERSIST) && 11305895Syz147064 (status = dladm_aggr_persist_aggr_conf(name, linkid, key, nports, 11315895Syz147064 ports, policy, mac_addr_fixed, mac_addr, lacp_mode, lacp_timer, 11325895Syz147064 force)) != DLADM_STATUS_OK) { 11335895Syz147064 goto fail; 11345895Syz147064 } 11355895Syz147064 11365895Syz147064 if (!(flags & DLADM_OPT_ACTIVE)) 11375895Syz147064 return (DLADM_STATUS_OK); 11385895Syz147064 11395895Syz147064 status = i_dladm_aggr_create_sys(linkid, key, nports, ports, policy, 11405895Syz147064 mac_addr_fixed, mac_addr, lacp_mode, lacp_timer, force); 11413871Syz147064 11425895Syz147064 if (status != DLADM_STATUS_OK) { 11435895Syz147064 if (flags & DLADM_OPT_PERSIST) 11445895Syz147064 (void) dladm_remove_conf(linkid); 11455895Syz147064 goto fail; 11465895Syz147064 } 11475895Syz147064 11485895Syz147064 return (DLADM_STATUS_OK); 11495895Syz147064 11505895Syz147064 fail: 11515895Syz147064 if (linkid != DATALINK_INVALID_LINKID) 11525895Syz147064 (void) dladm_destroy_datalink_id(linkid, flags); 11535895Syz147064 11545895Syz147064 return (status); 11555895Syz147064 } 11565895Syz147064 11575895Syz147064 static dladm_status_t 11585895Syz147064 i_dladm_aggr_get_aggr_attr(dladm_conf_t conf, uint32_t mask, 11595895Syz147064 dladm_aggr_modify_attr_t *attrp) 11605895Syz147064 { 11615895Syz147064 dladm_status_t status = DLADM_STATUS_OK; 11625895Syz147064 char macstr[ETHERADDRL * 3]; 11635895Syz147064 uint64_t u64; 11645895Syz147064 11655895Syz147064 if (mask & DLADM_AGGR_MODIFY_POLICY) { 11665895Syz147064 status = dladm_get_conf_field(conf, FPOLICY, &u64, 11675895Syz147064 sizeof (u64)); 11685895Syz147064 if (status != DLADM_STATUS_OK) 11695895Syz147064 return (status); 11705895Syz147064 attrp->ld_policy = (uint32_t)u64; 11715895Syz147064 } 11725895Syz147064 11735895Syz147064 if (mask & DLADM_AGGR_MODIFY_MAC) { 11745895Syz147064 status = dladm_get_conf_field(conf, FFIXMACADDR, 11755895Syz147064 &attrp->ld_mac_fixed, sizeof (boolean_t)); 11763871Syz147064 if (status != DLADM_STATUS_OK) 11773871Syz147064 return (status); 11785895Syz147064 11795895Syz147064 if (attrp->ld_mac_fixed) { 11805895Syz147064 boolean_t fixed; 11815895Syz147064 11825895Syz147064 status = dladm_get_conf_field(conf, FMACADDR, 11835895Syz147064 macstr, sizeof (macstr)); 11845895Syz147064 if (status != DLADM_STATUS_OK) 11855895Syz147064 return (status); 11863871Syz147064 11875895Syz147064 if (!dladm_aggr_str2macaddr(macstr, &fixed, 11885895Syz147064 attrp->ld_mac)) { 11895895Syz147064 return (DLADM_STATUS_REPOSITORYINVAL); 11905895Syz147064 } 11915895Syz147064 } 11925895Syz147064 } 11933871Syz147064 11945895Syz147064 if (mask & DLADM_AGGR_MODIFY_LACP_MODE) { 11955895Syz147064 status = dladm_get_conf_field(conf, FLACPMODE, &u64, 11965895Syz147064 sizeof (u64)); 11975895Syz147064 if (status != DLADM_STATUS_OK) 11985895Syz147064 return (status); 11995895Syz147064 attrp->ld_lacp_mode = (aggr_lacp_mode_t)u64; 12005895Syz147064 } 12015895Syz147064 12025895Syz147064 if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) { 12035895Syz147064 status = dladm_get_conf_field(conf, FLACPTIMER, &u64, 12045895Syz147064 sizeof (u64)); 12055895Syz147064 if (status != DLADM_STATUS_OK) 12065895Syz147064 return (status); 12075895Syz147064 attrp->ld_lacp_timer = (aggr_lacp_timer_t)u64; 12083871Syz147064 } 12093871Syz147064 12105895Syz147064 return (status); 12115895Syz147064 } 12125895Syz147064 12135895Syz147064 static dladm_status_t 12145895Syz147064 i_dladm_aggr_set_aggr_attr(dladm_conf_t conf, uint32_t mask, 12155895Syz147064 dladm_aggr_modify_attr_t *attrp) 12165895Syz147064 { 12175895Syz147064 dladm_status_t status = DLADM_STATUS_OK; 12185895Syz147064 char macstr[ETHERADDRL * 3]; 12195895Syz147064 uint64_t u64; 12205895Syz147064 12215895Syz147064 if (mask & DLADM_AGGR_MODIFY_POLICY) { 12225895Syz147064 u64 = attrp->ld_policy; 12235895Syz147064 status = dladm_set_conf_field(conf, FPOLICY, DLADM_TYPE_UINT64, 12245895Syz147064 &u64); 12255895Syz147064 if (status != DLADM_STATUS_OK) 12265895Syz147064 return (status); 12275895Syz147064 } 12285895Syz147064 12295895Syz147064 if (mask & DLADM_AGGR_MODIFY_MAC) { 12305895Syz147064 status = dladm_set_conf_field(conf, FFIXMACADDR, 12315895Syz147064 DLADM_TYPE_BOOLEAN, &attrp->ld_mac_fixed); 12325895Syz147064 if (status != DLADM_STATUS_OK) 12335895Syz147064 return (status); 12343871Syz147064 12355895Syz147064 if (attrp->ld_mac_fixed) { 12365895Syz147064 (void) dladm_aggr_macaddr2str(attrp->ld_mac, macstr); 12375895Syz147064 status = dladm_set_conf_field(conf, FMACADDR, 12385895Syz147064 DLADM_TYPE_STR, macstr); 12395895Syz147064 if (status != DLADM_STATUS_OK) 12405895Syz147064 return (status); 12415895Syz147064 } 12425895Syz147064 } 12435895Syz147064 12445895Syz147064 if (mask & DLADM_AGGR_MODIFY_LACP_MODE) { 12455895Syz147064 u64 = attrp->ld_lacp_mode; 12465895Syz147064 status = dladm_set_conf_field(conf, FLACPMODE, 12475895Syz147064 DLADM_TYPE_UINT64, &u64); 12485895Syz147064 if (status != DLADM_STATUS_OK) 12495895Syz147064 return (status); 12505895Syz147064 } 12515895Syz147064 12525895Syz147064 if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) { 12535895Syz147064 u64 = attrp->ld_lacp_timer; 12545895Syz147064 status = dladm_set_conf_field(conf, FLACPTIMER, 12555895Syz147064 DLADM_TYPE_UINT64, &u64); 12565895Syz147064 if (status != DLADM_STATUS_OK) 12575895Syz147064 return (status); 12585895Syz147064 } 12593871Syz147064 12603871Syz147064 return (status); 12613871Syz147064 } 12623871Syz147064 12633871Syz147064 /* 12643871Syz147064 * Modify the parameters of an existing link aggregation group. Update 12653871Syz147064 * the configuration file and pass the changes to the kernel. 12663871Syz147064 */ 12673871Syz147064 dladm_status_t 12685895Syz147064 dladm_aggr_modify(datalink_id_t linkid, uint32_t modify_mask, uint32_t policy, 12695895Syz147064 boolean_t mac_fixed, const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode, 12705895Syz147064 aggr_lacp_timer_t lacp_timer, uint32_t flags) 12713871Syz147064 { 12723871Syz147064 dladm_aggr_modify_attr_t new_attr, old_attr; 12735895Syz147064 dladm_conf_t conf; 12743871Syz147064 dladm_status_t status; 12753871Syz147064 12765895Syz147064 new_attr.ld_policy = policy; 12775895Syz147064 new_attr.ld_mac_fixed = mac_fixed; 12785895Syz147064 new_attr.ld_lacp_mode = lacp_mode; 12795895Syz147064 new_attr.ld_lacp_timer = lacp_timer; 12805895Syz147064 bcopy(mac_addr, new_attr.ld_mac, ETHERADDRL); 12815895Syz147064 12825895Syz147064 if (flags & DLADM_OPT_PERSIST) { 12835895Syz147064 status = dladm_read_conf(linkid, &conf); 12845895Syz147064 if (status != DLADM_STATUS_OK) 12855895Syz147064 return (status); 12863871Syz147064 12875895Syz147064 if ((status = i_dladm_aggr_get_aggr_attr(conf, modify_mask, 12885895Syz147064 &old_attr)) != DLADM_STATUS_OK) { 12895895Syz147064 goto done; 12905895Syz147064 } 12913871Syz147064 12925895Syz147064 if ((status = i_dladm_aggr_set_aggr_attr(conf, modify_mask, 12935895Syz147064 &new_attr)) != DLADM_STATUS_OK) { 12945895Syz147064 goto done; 12955895Syz147064 } 12965895Syz147064 12975895Syz147064 status = dladm_write_conf(conf); 12985895Syz147064 12995895Syz147064 done: 13005895Syz147064 dladm_destroy_conf(conf); 13015895Syz147064 if (status != DLADM_STATUS_OK) 13025895Syz147064 return (status); 13033871Syz147064 } 13043871Syz147064 13055895Syz147064 if (!(flags & DLADM_OPT_ACTIVE)) 13065895Syz147064 return (DLADM_STATUS_OK); 13073871Syz147064 13085895Syz147064 status = i_dladm_aggr_modify_sys(linkid, modify_mask, &new_attr); 13095895Syz147064 if ((status != DLADM_STATUS_OK) && (flags & DLADM_OPT_PERSIST)) { 13105895Syz147064 if (dladm_read_conf(linkid, &conf) == DLADM_STATUS_OK) { 13115895Syz147064 if (i_dladm_aggr_set_aggr_attr(conf, modify_mask, 13125895Syz147064 &old_attr) == DLADM_STATUS_OK) { 13135895Syz147064 (void) dladm_write_conf(conf); 13145895Syz147064 } 13155895Syz147064 dladm_destroy_conf(conf); 13165895Syz147064 } 13173871Syz147064 } 13183871Syz147064 13193871Syz147064 return (status); 13203871Syz147064 } 13213871Syz147064 13225895Syz147064 typedef struct aggr_held_arg_s { 13235895Syz147064 datalink_id_t aggrid; 13245895Syz147064 boolean_t isheld; 13255895Syz147064 } aggr_held_arg_t; 13265895Syz147064 13275895Syz147064 static int 13285895Syz147064 i_dladm_aggr_is_held(datalink_id_t linkid, void *arg) 13295895Syz147064 { 13305895Syz147064 aggr_held_arg_t *aggr_held_arg = arg; 13315895Syz147064 dladm_vlan_attr_t dva; 13325895Syz147064 13335895Syz147064 if (dladm_vlan_info(linkid, &dva, DLADM_OPT_PERSIST) != DLADM_STATUS_OK) 13345895Syz147064 return (DLADM_WALK_CONTINUE); 13355895Syz147064 13365895Syz147064 if (dva.dv_linkid == aggr_held_arg->aggrid) { 13375895Syz147064 /* 13385895Syz147064 * This VLAN is created over the given aggregation. 13395895Syz147064 */ 13405895Syz147064 aggr_held_arg->isheld = B_TRUE; 13415895Syz147064 return (DLADM_WALK_TERMINATE); 13425895Syz147064 } 13435895Syz147064 return (DLADM_WALK_CONTINUE); 13445895Syz147064 } 13455895Syz147064 13463871Syz147064 /* 13475895Syz147064 * Delete a previously created link aggregation group. Either the name "aggr" 13485895Syz147064 * or the "key" is specified. 13493871Syz147064 */ 13503871Syz147064 dladm_status_t 13515895Syz147064 dladm_aggr_delete(datalink_id_t linkid, uint32_t flags) 13523871Syz147064 { 13535895Syz147064 laioc_delete_t ioc; 13545895Syz147064 datalink_class_t class; 13553871Syz147064 dladm_status_t status; 13563871Syz147064 13575895Syz147064 if ((dladm_datalink_id2info(linkid, NULL, &class, NULL, NULL, 0) != 13585895Syz147064 DLADM_STATUS_OK) || (class != DATALINK_CLASS_AGGR)) { 13595895Syz147064 return (DLADM_STATUS_BADARG); 13605895Syz147064 } 13613871Syz147064 13625895Syz147064 if (flags & DLADM_OPT_ACTIVE) { 13635895Syz147064 ioc.ld_linkid = linkid; 13645895Syz147064 if ((i_dladm_aggr_strioctl(LAIOC_DELETE, &ioc, 13655895Syz147064 sizeof (ioc)) < 0) && 13665895Syz147064 ((errno != ENOENT) || !(flags & DLADM_OPT_PERSIST))) { 13675895Syz147064 status = dladm_errno2status(errno); 13685895Syz147064 return (status); 13695895Syz147064 } 13703871Syz147064 13713871Syz147064 /* 13725895Syz147064 * Delete ACTIVE linkprop first. 13733871Syz147064 */ 13745895Syz147064 (void) dladm_set_linkprop(linkid, NULL, NULL, 0, 13755895Syz147064 DLADM_OPT_ACTIVE); 13765895Syz147064 (void) dladm_destroy_datalink_id(linkid, DLADM_OPT_ACTIVE); 13773871Syz147064 } 13783871Syz147064 13795895Syz147064 /* 13805895Syz147064 * If we reach here, it means that the active aggregation has already 13815895Syz147064 * been deleted, and there is no active VLANs holding this aggregation. 13825895Syz147064 * Now we see whether there is any persistent VLANs holding this 13835895Syz147064 * aggregation. If so, we fail the operation. 13845895Syz147064 */ 13855895Syz147064 if (flags & DLADM_OPT_PERSIST) { 13865895Syz147064 aggr_held_arg_t arg; 13875895Syz147064 13885895Syz147064 arg.aggrid = linkid; 13895895Syz147064 arg.isheld = B_FALSE; 13903871Syz147064 13915895Syz147064 (void) dladm_walk_datalink_id(i_dladm_aggr_is_held, 13925895Syz147064 &arg, DATALINK_CLASS_VLAN, DATALINK_ANY_MEDIATYPE, 13935895Syz147064 DLADM_OPT_PERSIST); 13945895Syz147064 if (arg.isheld) 13955895Syz147064 return (DLADM_STATUS_LINKBUSY); 13965895Syz147064 13975895Syz147064 (void) dladm_destroy_datalink_id(linkid, DLADM_OPT_PERSIST); 13985895Syz147064 (void) dladm_remove_conf(linkid); 13995895Syz147064 } 14005895Syz147064 14015895Syz147064 return (DLADM_STATUS_OK); 14023871Syz147064 } 14033871Syz147064 14043871Syz147064 /* 14053871Syz147064 * Add one or more ports to an existing link aggregation. 14063871Syz147064 */ 14073871Syz147064 dladm_status_t 14085895Syz147064 dladm_aggr_add(datalink_id_t linkid, uint32_t nports, 14095895Syz147064 dladm_aggr_port_attr_db_t *ports, uint32_t flags) 14103871Syz147064 { 14115895Syz147064 return (i_dladm_aggr_add_rmv(linkid, nports, ports, flags, LAIOC_ADD)); 14123871Syz147064 } 14133871Syz147064 14143871Syz147064 /* 14153871Syz147064 * Remove one or more ports from an existing link aggregation. 14163871Syz147064 */ 14173871Syz147064 dladm_status_t 14185895Syz147064 dladm_aggr_remove(datalink_id_t linkid, uint32_t nports, 14195895Syz147064 dladm_aggr_port_attr_db_t *ports, uint32_t flags) 14203871Syz147064 { 14215895Syz147064 return (i_dladm_aggr_add_rmv(linkid, nports, ports, flags, 14225895Syz147064 LAIOC_REMOVE)); 14235895Syz147064 } 14243871Syz147064 14255895Syz147064 typedef struct i_walk_key_state_s { 14265895Syz147064 uint16_t key; 14275895Syz147064 datalink_id_t linkid; 14285895Syz147064 boolean_t found; 14295895Syz147064 } i_walk_key_state_t; 14303871Syz147064 14315895Syz147064 static int 14325895Syz147064 i_dladm_walk_key2linkid(datalink_id_t linkid, void *arg) 14335895Syz147064 { 14345895Syz147064 dladm_conf_t conf; 14355895Syz147064 uint16_t key; 14365895Syz147064 dladm_status_t status; 14375895Syz147064 i_walk_key_state_t *statep = (i_walk_key_state_t *)arg; 14385895Syz147064 uint64_t u64; 14393871Syz147064 14405895Syz147064 if (dladm_read_conf(linkid, &conf) != 0) 14415895Syz147064 return (DLADM_WALK_CONTINUE); 14425895Syz147064 14435895Syz147064 status = dladm_get_conf_field(conf, FKEY, &u64, sizeof (u64)); 14445895Syz147064 key = (uint16_t)u64; 14455895Syz147064 dladm_destroy_conf(conf); 14465895Syz147064 14475895Syz147064 if ((status == DLADM_STATUS_OK) && (key == statep->key)) { 14485895Syz147064 statep->found = B_TRUE; 14495895Syz147064 statep->linkid = linkid; 14505895Syz147064 return (DLADM_WALK_TERMINATE); 14513871Syz147064 } 14523871Syz147064 14535895Syz147064 return (DLADM_WALK_CONTINUE); 14545895Syz147064 } 14555895Syz147064 14565895Syz147064 dladm_status_t 14575895Syz147064 dladm_key2linkid(uint16_t key, datalink_id_t *linkidp, uint32_t flags) 14585895Syz147064 { 14595895Syz147064 i_walk_key_state_t state; 14605895Syz147064 14615895Syz147064 if (key > AGGR_MAX_KEY) 14625895Syz147064 return (DLADM_STATUS_NOTFOUND); 14633871Syz147064 14645895Syz147064 state.found = B_FALSE; 14655895Syz147064 state.key = key; 14665895Syz147064 14675895Syz147064 (void) dladm_walk_datalink_id(i_dladm_walk_key2linkid, &state, 14685895Syz147064 DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE, flags); 14695895Syz147064 if (state.found == B_TRUE) { 14705895Syz147064 *linkidp = state.linkid; 14715895Syz147064 return (DLADM_STATUS_OK); 14725895Syz147064 } else { 14735895Syz147064 return (DLADM_STATUS_NOTFOUND); 14745895Syz147064 } 14753871Syz147064 } 1476