xref: /onnv-gate/usr/src/uts/common/io/dls/dls_stat.c (revision 1184:1c788f55a808)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Data-Link Services Module
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/atomic.h>
360Sstevel@tonic-gate #include <sys/kstat.h>
370Sstevel@tonic-gate #include <sys/vlan.h>
380Sstevel@tonic-gate #include <sys/mac.h>
390Sstevel@tonic-gate #include <sys/ctype.h>
400Sstevel@tonic-gate #include <sys/dls.h>
410Sstevel@tonic-gate #include <sys/dls_impl.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate typedef struct i_dls_stat_info_s {
440Sstevel@tonic-gate 	enum mac_stat	dsi_stat;
450Sstevel@tonic-gate 	char		*dsi_name;
460Sstevel@tonic-gate 	uint_t		dsi_type;
470Sstevel@tonic-gate } i_mac_stat_info_t;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static i_mac_stat_info_t	i_dls_si[] = {
500Sstevel@tonic-gate 	{ MAC_STAT_IFSPEED, "ifspeed", KSTAT_DATA_UINT64 },
510Sstevel@tonic-gate 	{ MAC_STAT_MULTIRCV, "multircv", KSTAT_DATA_UINT32 },
520Sstevel@tonic-gate 	{ MAC_STAT_BRDCSTRCV, "brdcstrcv", KSTAT_DATA_UINT32 },
530Sstevel@tonic-gate 	{ MAC_STAT_MULTIXMT, "multixmt", KSTAT_DATA_UINT32 },
540Sstevel@tonic-gate 	{ MAC_STAT_BRDCSTXMT, "brdcstxmt", KSTAT_DATA_UINT32 },
550Sstevel@tonic-gate 	{ MAC_STAT_NORCVBUF, "norcvbuf", KSTAT_DATA_UINT32 },
560Sstevel@tonic-gate 	{ MAC_STAT_IERRORS, "ierrors", KSTAT_DATA_UINT32 },
570Sstevel@tonic-gate 	{ MAC_STAT_NOXMTBUF, "noxmtbuf", KSTAT_DATA_UINT32 },
580Sstevel@tonic-gate 	{ MAC_STAT_OERRORS, "oerrors", KSTAT_DATA_UINT32 },
590Sstevel@tonic-gate 	{ MAC_STAT_COLLISIONS, "collisions", KSTAT_DATA_UINT32 },
600Sstevel@tonic-gate 	{ MAC_STAT_RBYTES, "rbytes", KSTAT_DATA_UINT32 },
610Sstevel@tonic-gate 	{ MAC_STAT_IPACKETS, "ipackets", KSTAT_DATA_UINT32 },
620Sstevel@tonic-gate 	{ MAC_STAT_OBYTES, "obytes", KSTAT_DATA_UINT32 },
630Sstevel@tonic-gate 	{ MAC_STAT_OPACKETS, "opackets", KSTAT_DATA_UINT32 },
640Sstevel@tonic-gate 	{ MAC_STAT_RBYTES, "rbytes64", KSTAT_DATA_UINT64 },
650Sstevel@tonic-gate 	{ MAC_STAT_IPACKETS, "ipackets64", KSTAT_DATA_UINT64 },
660Sstevel@tonic-gate 	{ MAC_STAT_OBYTES, "obytes64", KSTAT_DATA_UINT64 },
670Sstevel@tonic-gate 	{ MAC_STAT_OPACKETS, "opackets64", KSTAT_DATA_UINT64 }
680Sstevel@tonic-gate };
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define	STAT_INFO_COUNT	(sizeof (i_dls_si) / sizeof (i_dls_si[0]))
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Private functions.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static int
770Sstevel@tonic-gate i_dls_stat_update(kstat_t *ksp, int rw)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	dls_vlan_t	*dvp = ksp->ks_private;
800Sstevel@tonic-gate 	dls_link_t	*dlp = dvp->dv_dlp;
810Sstevel@tonic-gate 	kstat_named_t	*knp;
820Sstevel@tonic-gate 	uint_t		i;
830Sstevel@tonic-gate 	uint64_t	val;
840Sstevel@tonic-gate 	int		err;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (rw != KSTAT_READ)
870Sstevel@tonic-gate 		return (EACCES);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if ((err = dls_mac_hold(dlp)) != 0)
900Sstevel@tonic-gate 		return (err);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	knp = (kstat_named_t *)ksp->ks_data;
930Sstevel@tonic-gate 	for (i = 0; i < STAT_INFO_COUNT; i++) {
940Sstevel@tonic-gate 		if (!(dlp->dl_mip->mi_stat[i_dls_si[i].dsi_stat]))
950Sstevel@tonic-gate 			continue;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 		val = mac_stat_get(dlp->dl_mh, i_dls_si[i].dsi_stat);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 		switch (i_dls_si[i].dsi_type) {
1000Sstevel@tonic-gate 		case KSTAT_DATA_UINT64:
1010Sstevel@tonic-gate 			knp->value.ui64 = val;
1020Sstevel@tonic-gate 			break;
1030Sstevel@tonic-gate 		case KSTAT_DATA_UINT32:
1040Sstevel@tonic-gate 			knp->value.ui32 = (uint32_t)val;
1050Sstevel@tonic-gate 			break;
1060Sstevel@tonic-gate 		default:
1070Sstevel@tonic-gate 			ASSERT(B_FALSE);
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		knp++;
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	knp->value.ui32 = dlp->dl_unknowns;
1140Sstevel@tonic-gate 	dls_mac_rele(dlp);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	return (0);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Exported functions.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate void
124*1184Skrgopi dls_mac_stat_create(dls_vlan_t *dvp)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	dls_link_t		*dlp = dvp->dv_dlp;
1270Sstevel@tonic-gate 	char			module[IFNAMSIZ];
1280Sstevel@tonic-gate 	uint_t			instance;
1290Sstevel@tonic-gate 	kstat_t			*ksp;
1300Sstevel@tonic-gate 	kstat_named_t		*knp;
1310Sstevel@tonic-gate 	uint_t			i;
1320Sstevel@tonic-gate 	uint_t			count;
1330Sstevel@tonic-gate 	int			err;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (dls_mac_hold(dlp) != 0)
1360Sstevel@tonic-gate 		return;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	count = 0;
1390Sstevel@tonic-gate 	for (i = 0; i < STAT_INFO_COUNT; i++) {
1400Sstevel@tonic-gate 		if (dlp->dl_mip->mi_stat[i_dls_si[i].dsi_stat])
1410Sstevel@tonic-gate 			count++;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	err = ddi_parse(dvp->dv_name, module, &instance);
1450Sstevel@tonic-gate 	ASSERT(err == DDI_SUCCESS);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if ((ksp = kstat_create(module, instance, NULL, "net",
1480Sstevel@tonic-gate 	    KSTAT_TYPE_NAMED, count + 1, 0)) == NULL)
1490Sstevel@tonic-gate 		goto done;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	ksp->ks_update = i_dls_stat_update;
1520Sstevel@tonic-gate 	ksp->ks_private = (void *)dvp;
1530Sstevel@tonic-gate 	dvp->dv_ksp = ksp;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	knp = (kstat_named_t *)ksp->ks_data;
1560Sstevel@tonic-gate 	for (i = 0; i < STAT_INFO_COUNT; i++) {
1570Sstevel@tonic-gate 		if (!(dlp->dl_mip->mi_stat[i_dls_si[i].dsi_stat]))
1580Sstevel@tonic-gate 			continue;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		kstat_named_init(knp, i_dls_si[i].dsi_name,
1610Sstevel@tonic-gate 		    i_dls_si[i].dsi_type);
1620Sstevel@tonic-gate 		knp++;
1630Sstevel@tonic-gate 		--count;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 	ASSERT(count == 0);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	kstat_named_init(knp, "unknowns", KSTAT_DATA_UINT32);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	kstat_install(ksp);
1700Sstevel@tonic-gate done:
1710Sstevel@tonic-gate 	dls_mac_rele(dlp);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate void
175*1184Skrgopi dls_mac_stat_destroy(dls_vlan_t *dvp)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	kstat_delete(dvp->dv_ksp);
1780Sstevel@tonic-gate 	dvp->dv_ksp = NULL;
1790Sstevel@tonic-gate }
180