xref: /titanic_51/usr/src/lib/libdladm/common/libdladm.c (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
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
5*ba2e4443Sseb  * Common Development and Distribution License (the "License").
6*ba2e4443Sseb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ba2e4443Sseb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <stropts.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
377c478bd9Sstevel@tonic-gate #include <libdlpi.h>
387c478bd9Sstevel@tonic-gate #include <libdladm.h>
397c478bd9Sstevel@tonic-gate #include <sys/dld.h>
407c478bd9Sstevel@tonic-gate #include <net/if.h>
417c478bd9Sstevel@tonic-gate 
42aae21359Skrgopi typedef struct dladm_dev {
43aae21359Skrgopi 	char			dd_name[IFNAMSIZ];
44aae21359Skrgopi 	struct dladm_dev	*dd_next;
45aae21359Skrgopi } dladm_dev_t;
46aae21359Skrgopi 
47aae21359Skrgopi typedef struct dladm_walk {
48aae21359Skrgopi 	dladm_dev_t		*dw_dev_list;
49aae21359Skrgopi } dladm_walk_t;
50aae21359Skrgopi 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Issue an ioctl to the specified file descriptor attached to the
537c478bd9Sstevel@tonic-gate  * DLD control driver interface.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate static int
56210db224Sericheng i_dladm_ioctl(int fd, int ic_cmd, void *ic_dp, int ic_len)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	struct strioctl	iocb;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	iocb.ic_cmd = ic_cmd;
617c478bd9Sstevel@tonic-gate 	iocb.ic_timout = 0;
627c478bd9Sstevel@tonic-gate 	iocb.ic_len = ic_len;
63210db224Sericheng 	iocb.ic_dp = (char *)ic_dp;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	return (ioctl(fd, I_STR, &iocb));
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Return the attributes of the specified datalink from the DLD driver.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static int
727c478bd9Sstevel@tonic-gate i_dladm_info(int fd, const char *name, dladm_attr_t *dap)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	dld_ioc_attr_t	dia;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (strlen(name) >= IFNAMSIZ) {
777c478bd9Sstevel@tonic-gate 		errno = EINVAL;
787c478bd9Sstevel@tonic-gate 		return (-1);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	(void) strlcpy(dia.dia_name, name, IFNAMSIZ);
827c478bd9Sstevel@tonic-gate 
83210db224Sericheng 	if (i_dladm_ioctl(fd, DLDIOCATTR, &dia, sizeof (dia)) < 0)
847c478bd9Sstevel@tonic-gate 		return (-1);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	(void) strlcpy(dap->da_dev, dia.dia_dev, MAXNAMELEN);
87210db224Sericheng 	dap->da_max_sdu = dia.dia_max_sdu;
887c478bd9Sstevel@tonic-gate 	dap->da_vid = dia.dia_vid;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (0);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Adds a datalink to the array corresponding to arg.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate static void
977c478bd9Sstevel@tonic-gate i_dladm_nt_net_add(void *arg, char *name)
987c478bd9Sstevel@tonic-gate {
99aae21359Skrgopi 	dladm_walk_t	*dwp = arg;
100aae21359Skrgopi 	dladm_dev_t	*ddp = dwp->dw_dev_list;
101aae21359Skrgopi 	dladm_dev_t	**lastp = &dwp->dw_dev_list;
1027c478bd9Sstevel@tonic-gate 
103aae21359Skrgopi 	while (ddp) {
104aae21359Skrgopi 		/*
105aae21359Skrgopi 		 * Skip duplicates.
106aae21359Skrgopi 		 */
107aae21359Skrgopi 		if (strcmp(ddp->dd_name, name) == 0)
1087c478bd9Sstevel@tonic-gate 			return;
109aae21359Skrgopi 
110aae21359Skrgopi 		lastp = &ddp->dd_next;
111aae21359Skrgopi 		ddp = ddp->dd_next;
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
114aae21359Skrgopi 	if ((ddp = malloc(sizeof (*ddp))) == NULL)
115aae21359Skrgopi 		return;
116aae21359Skrgopi 
117aae21359Skrgopi 	(void) strlcpy(ddp->dd_name, name, IFNAMSIZ);
118aae21359Skrgopi 	ddp->dd_next = NULL;
119aae21359Skrgopi 	*lastp = ddp;
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Walker callback invoked for each DDI_NT_NET node.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate static int
1267c478bd9Sstevel@tonic-gate i_dladm_nt_net_walk(di_node_t node, di_minor_t minor, void *arg)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	dl_info_ack_t	dlia;
1297c478bd9Sstevel@tonic-gate 	char		name[IFNAMSIZ];
1307c478bd9Sstevel@tonic-gate 	int		fd;
1317c478bd9Sstevel@tonic-gate 	char		*provider;
1327c478bd9Sstevel@tonic-gate 	uint_t		ppa;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	provider = di_minor_name(minor);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if ((fd = dlpi_open(provider)) < 0)
1377c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (dlpi_info(fd, -1, &dlia, NULL, NULL, NULL, NULL, NULL, NULL) < 0) {
1407c478bd9Sstevel@tonic-gate 		(void) dlpi_close(fd);
1417c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (dlia.dl_provider_style == DL_STYLE1) {
1457c478bd9Sstevel@tonic-gate 		i_dladm_nt_net_add(arg, provider);
1467c478bd9Sstevel@tonic-gate 		(void) dlpi_close(fd);
1477c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	ppa = di_instance(node);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (dlpi_attach(fd, -1, ppa) < 0) {
1537c478bd9Sstevel@tonic-gate 		(void) dlpi_close(fd);
1547c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	(void) snprintf(name, IFNAMSIZ - 1, "%s%d", provider, ppa);
1577c478bd9Sstevel@tonic-gate 	i_dladm_nt_net_add(arg, name);
1587c478bd9Sstevel@tonic-gate 	(void) dlpi_close(fd);
1597c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Invoke the specified callback function for each active DDI_NT_NET
1647c478bd9Sstevel@tonic-gate  * node.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate int
1677c478bd9Sstevel@tonic-gate dladm_walk(void (*fn)(void *, const char *), void *arg)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	di_node_t	root;
170aae21359Skrgopi 	dladm_walk_t	dw;
171aae21359Skrgopi 	dladm_dev_t	*ddp, *last_ddp;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
1747c478bd9Sstevel@tonic-gate 		errno = EFAULT;
1757c478bd9Sstevel@tonic-gate 		return (-1);
1767c478bd9Sstevel@tonic-gate 	}
177aae21359Skrgopi 	dw.dw_dev_list = NULL;
1787c478bd9Sstevel@tonic-gate 
179aae21359Skrgopi 	(void) di_walk_minor(root, DDI_NT_NET, DI_CHECK_ALIAS, &dw,
1807c478bd9Sstevel@tonic-gate 	    i_dladm_nt_net_walk);
181aae21359Skrgopi 
1827c478bd9Sstevel@tonic-gate 	di_fini(root);
1837c478bd9Sstevel@tonic-gate 
184aae21359Skrgopi 	ddp = dw.dw_dev_list;
185aae21359Skrgopi 	while (ddp) {
186aae21359Skrgopi 		fn(arg, ddp->dd_name);
1876e61f8d9Skrgopi 		(void) dladm_walk_vlan(fn, arg, ddp->dd_name);
188aae21359Skrgopi 		last_ddp = ddp;
189aae21359Skrgopi 		ddp = ddp->dd_next;
190aae21359Skrgopi 		free(last_ddp);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
193aae21359Skrgopi 	return (0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
197210db224Sericheng  * Invoke the specified callback function for each vlan managed by dld
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate int
200aae21359Skrgopi dladm_walk_vlan(void (*fn)(void *, const char *), void *arg, const char *name)
2017c478bd9Sstevel@tonic-gate {
202aae21359Skrgopi 	int		fd, bufsize, i;
203aae21359Skrgopi 	int		nvlan = 4094;
204210db224Sericheng 	dld_ioc_vlan_t	*iocp = NULL;
205210db224Sericheng 	dld_vlan_info_t	*dvip;
2067c478bd9Sstevel@tonic-gate 
207210db224Sericheng 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
2087c478bd9Sstevel@tonic-gate 		return (-1);
209210db224Sericheng 
210aae21359Skrgopi 	bufsize = sizeof (dld_ioc_vlan_t) + nvlan * sizeof (dld_vlan_info_t);
211210db224Sericheng 
212aae21359Skrgopi 	if ((iocp = (dld_ioc_vlan_t *)calloc(1, bufsize)) == NULL)
213aae21359Skrgopi 		return (-1);
214210db224Sericheng 
215aae21359Skrgopi 	(void) strlcpy((char *)iocp->div_name, name, IFNAMSIZ);
216aae21359Skrgopi 	if (i_dladm_ioctl(fd, DLDIOCVLAN, iocp, bufsize) == 0) {
217210db224Sericheng 		dvip = (dld_vlan_info_t *)(iocp + 1);
218aae21359Skrgopi 		for (i = 0; i < iocp->div_count; i++)
219210db224Sericheng 			(*fn)(arg, dvip[i].dvi_name);
2207c478bd9Sstevel@tonic-gate 	}
221aae21359Skrgopi 	/*
222aae21359Skrgopi 	 * Note: Callers of dladm_walk_vlan() ignore the return
223aae21359Skrgopi 	 * value of this routine. So ignoring ioctl failure case
224aae21359Skrgopi 	 * and just returning 0.
225aae21359Skrgopi 	 */
226210db224Sericheng 	free(iocp);
2277c478bd9Sstevel@tonic-gate 	(void) close(fd);
2287c478bd9Sstevel@tonic-gate 	return (0);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  * Returns the current attributes of the specified datalink.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate int
2367c478bd9Sstevel@tonic-gate dladm_info(const char *name, dladm_attr_t *dap)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int		fd;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
2417c478bd9Sstevel@tonic-gate 		return (-1);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (i_dladm_info(fd, name, dap) < 0)
2447c478bd9Sstevel@tonic-gate 		goto failed;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	(void) close(fd);
2477c478bd9Sstevel@tonic-gate 	return (0);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate failed:
2507c478bd9Sstevel@tonic-gate 	(void) close(fd);
2517c478bd9Sstevel@tonic-gate 	return (-1);
2527c478bd9Sstevel@tonic-gate }
253