17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <fcntl.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <stropts.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <libdevinfo.h> 387c478bd9Sstevel@tonic-gate #include <libdlpi.h> 397c478bd9Sstevel@tonic-gate #include <libdladm.h> 407c478bd9Sstevel@tonic-gate #include <sys/dld.h> 417c478bd9Sstevel@tonic-gate #include <net/if.h> 427c478bd9Sstevel@tonic-gate 43*aae21359Skrgopi typedef struct dladm_dev { 44*aae21359Skrgopi char dd_name[IFNAMSIZ]; 45*aae21359Skrgopi struct dladm_dev *dd_next; 46*aae21359Skrgopi } dladm_dev_t; 47*aae21359Skrgopi 48*aae21359Skrgopi typedef struct dladm_walk { 49*aae21359Skrgopi dladm_dev_t *dw_dev_list; 50*aae21359Skrgopi } dladm_walk_t; 51*aae21359Skrgopi 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * Issue an ioctl to the specified file descriptor attached to the 547c478bd9Sstevel@tonic-gate * DLD control driver interface. 557c478bd9Sstevel@tonic-gate */ 567c478bd9Sstevel@tonic-gate static int 57210db224Sericheng i_dladm_ioctl(int fd, int ic_cmd, void *ic_dp, int ic_len) 587c478bd9Sstevel@tonic-gate { 597c478bd9Sstevel@tonic-gate struct strioctl iocb; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate iocb.ic_cmd = ic_cmd; 627c478bd9Sstevel@tonic-gate iocb.ic_timout = 0; 637c478bd9Sstevel@tonic-gate iocb.ic_len = ic_len; 64210db224Sericheng iocb.ic_dp = (char *)ic_dp; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate return (ioctl(fd, I_STR, &iocb)); 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * Return the attributes of the specified datalink from the DLD driver. 717c478bd9Sstevel@tonic-gate */ 727c478bd9Sstevel@tonic-gate static int 737c478bd9Sstevel@tonic-gate i_dladm_info(int fd, const char *name, dladm_attr_t *dap) 747c478bd9Sstevel@tonic-gate { 757c478bd9Sstevel@tonic-gate dld_ioc_attr_t dia; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate if (strlen(name) >= IFNAMSIZ) { 787c478bd9Sstevel@tonic-gate errno = EINVAL; 797c478bd9Sstevel@tonic-gate return (-1); 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate (void) strlcpy(dia.dia_name, name, IFNAMSIZ); 837c478bd9Sstevel@tonic-gate 84210db224Sericheng if (i_dladm_ioctl(fd, DLDIOCATTR, &dia, sizeof (dia)) < 0) 857c478bd9Sstevel@tonic-gate return (-1); 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate (void) strlcpy(dap->da_dev, dia.dia_dev, MAXNAMELEN); 88210db224Sericheng dap->da_max_sdu = dia.dia_max_sdu; 897c478bd9Sstevel@tonic-gate dap->da_port = dia.dia_port; 907c478bd9Sstevel@tonic-gate dap->da_vid = dia.dia_vid; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate return (0); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * Adds a datalink to the array corresponding to arg. 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate static void 997c478bd9Sstevel@tonic-gate i_dladm_nt_net_add(void *arg, char *name) 1007c478bd9Sstevel@tonic-gate { 101*aae21359Skrgopi dladm_walk_t *dwp = arg; 102*aae21359Skrgopi dladm_dev_t *ddp = dwp->dw_dev_list; 103*aae21359Skrgopi dladm_dev_t **lastp = &dwp->dw_dev_list; 1047c478bd9Sstevel@tonic-gate 105*aae21359Skrgopi while (ddp) { 106*aae21359Skrgopi /* 107*aae21359Skrgopi * Skip duplicates. 108*aae21359Skrgopi */ 109*aae21359Skrgopi if (strcmp(ddp->dd_name, name) == 0) 1107c478bd9Sstevel@tonic-gate return; 111*aae21359Skrgopi 112*aae21359Skrgopi lastp = &ddp->dd_next; 113*aae21359Skrgopi ddp = ddp->dd_next; 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 116*aae21359Skrgopi if ((ddp = malloc(sizeof (*ddp))) == NULL) 117*aae21359Skrgopi return; 118*aae21359Skrgopi 119*aae21359Skrgopi (void) strlcpy(ddp->dd_name, name, IFNAMSIZ); 120*aae21359Skrgopi ddp->dd_next = NULL; 121*aae21359Skrgopi *lastp = ddp; 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * Walker callback invoked for each DDI_NT_NET node. 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate static int 1287c478bd9Sstevel@tonic-gate i_dladm_nt_net_walk(di_node_t node, di_minor_t minor, void *arg) 1297c478bd9Sstevel@tonic-gate { 1307c478bd9Sstevel@tonic-gate dl_info_ack_t dlia; 1317c478bd9Sstevel@tonic-gate char name[IFNAMSIZ]; 1327c478bd9Sstevel@tonic-gate int fd; 1337c478bd9Sstevel@tonic-gate char *provider; 1347c478bd9Sstevel@tonic-gate uint_t ppa; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate provider = di_minor_name(minor); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate if ((fd = dlpi_open(provider)) < 0) 1397c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate if (dlpi_info(fd, -1, &dlia, NULL, NULL, NULL, NULL, NULL, NULL) < 0) { 1427c478bd9Sstevel@tonic-gate (void) dlpi_close(fd); 1437c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate if (dlia.dl_provider_style == DL_STYLE1) { 1477c478bd9Sstevel@tonic-gate i_dladm_nt_net_add(arg, provider); 1487c478bd9Sstevel@tonic-gate (void) dlpi_close(fd); 1497c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate ppa = di_instance(node); 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate if (dlpi_attach(fd, -1, ppa) < 0) { 1557c478bd9Sstevel@tonic-gate (void) dlpi_close(fd); 1567c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate (void) snprintf(name, IFNAMSIZ - 1, "%s%d", provider, ppa); 1597c478bd9Sstevel@tonic-gate i_dladm_nt_net_add(arg, name); 1607c478bd9Sstevel@tonic-gate (void) dlpi_close(fd); 1617c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * Invoke the specified callback function for each active DDI_NT_NET 1667c478bd9Sstevel@tonic-gate * node. 1677c478bd9Sstevel@tonic-gate */ 1687c478bd9Sstevel@tonic-gate int 1697c478bd9Sstevel@tonic-gate dladm_walk(void (*fn)(void *, const char *), void *arg) 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate di_node_t root; 172*aae21359Skrgopi dladm_walk_t dw; 173*aae21359Skrgopi dladm_dev_t *ddp, *last_ddp; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) { 1767c478bd9Sstevel@tonic-gate errno = EFAULT; 1777c478bd9Sstevel@tonic-gate return (-1); 1787c478bd9Sstevel@tonic-gate } 179*aae21359Skrgopi dw.dw_dev_list = NULL; 1807c478bd9Sstevel@tonic-gate 181*aae21359Skrgopi (void) di_walk_minor(root, DDI_NT_NET, DI_CHECK_ALIAS, &dw, 1827c478bd9Sstevel@tonic-gate i_dladm_nt_net_walk); 183*aae21359Skrgopi 1847c478bd9Sstevel@tonic-gate di_fini(root); 1857c478bd9Sstevel@tonic-gate 186*aae21359Skrgopi ddp = dw.dw_dev_list; 187*aae21359Skrgopi while (ddp) { 188*aae21359Skrgopi fn(arg, ddp->dd_name); 189*aae21359Skrgopi dladm_walk_vlan(fn, arg, ddp->dd_name); 190*aae21359Skrgopi last_ddp = ddp; 191*aae21359Skrgopi ddp = ddp->dd_next; 192*aae21359Skrgopi free(last_ddp); 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 195*aae21359Skrgopi return (0); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* 199210db224Sericheng * Invoke the specified callback function for each vlan managed by dld 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate int 202*aae21359Skrgopi dladm_walk_vlan(void (*fn)(void *, const char *), void *arg, const char *name) 2037c478bd9Sstevel@tonic-gate { 204*aae21359Skrgopi int fd, bufsize, i; 205*aae21359Skrgopi int nvlan = 4094; 206210db224Sericheng dld_ioc_vlan_t *iocp = NULL; 207210db224Sericheng dld_vlan_info_t *dvip; 2087c478bd9Sstevel@tonic-gate 209210db224Sericheng if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) 2107c478bd9Sstevel@tonic-gate return (-1); 211210db224Sericheng 212*aae21359Skrgopi bufsize = sizeof (dld_ioc_vlan_t) + nvlan * sizeof (dld_vlan_info_t); 213210db224Sericheng 214*aae21359Skrgopi if ((iocp = (dld_ioc_vlan_t *)calloc(1, bufsize)) == NULL) 215*aae21359Skrgopi return (-1); 216210db224Sericheng 217*aae21359Skrgopi if (strncmp(name, "aggr", 4) == 0) { 218*aae21359Skrgopi (void) strlcpy((char *)iocp->div_name, "aggr0", IFNAMSIZ); 219*aae21359Skrgopi iocp->div_port = atoi(strpbrk(name, "0123456789")); 220*aae21359Skrgopi } else { 221*aae21359Skrgopi (void) strlcpy((char *)iocp->div_name, name, IFNAMSIZ); 222*aae21359Skrgopi iocp->div_port = 0; 223210db224Sericheng } 224*aae21359Skrgopi if (i_dladm_ioctl(fd, DLDIOCVLAN, iocp, bufsize) == 0) { 225210db224Sericheng dvip = (dld_vlan_info_t *)(iocp + 1); 226*aae21359Skrgopi for (i = 0; i < iocp->div_count; i++) 227210db224Sericheng (*fn)(arg, dvip[i].dvi_name); 2287c478bd9Sstevel@tonic-gate } 229*aae21359Skrgopi /* 230*aae21359Skrgopi * Note: Callers of dladm_walk_vlan() ignore the return 231*aae21359Skrgopi * value of this routine. So ignoring ioctl failure case 232*aae21359Skrgopi * and just returning 0. 233*aae21359Skrgopi */ 234210db224Sericheng free(iocp); 2357c478bd9Sstevel@tonic-gate (void) close(fd); 2367c478bd9Sstevel@tonic-gate return (0); 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate /* 2417c478bd9Sstevel@tonic-gate * Returns the current attributes of the specified datalink. 2427c478bd9Sstevel@tonic-gate */ 2437c478bd9Sstevel@tonic-gate int 2447c478bd9Sstevel@tonic-gate dladm_info(const char *name, dladm_attr_t *dap) 2457c478bd9Sstevel@tonic-gate { 2467c478bd9Sstevel@tonic-gate int fd; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) 2497c478bd9Sstevel@tonic-gate return (-1); 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if (i_dladm_info(fd, name, dap) < 0) 2527c478bd9Sstevel@tonic-gate goto failed; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate (void) close(fd); 2557c478bd9Sstevel@tonic-gate return (0); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate failed: 2587c478bd9Sstevel@tonic-gate (void) close(fd); 2597c478bd9Sstevel@tonic-gate return (-1); 2607c478bd9Sstevel@tonic-gate } 261