xref: /titanic_50/usr/src/uts/common/io/mac/mac_stat.c (revision dfb96a4f56fb431b915bc67e5d9d5c8d4f4f6679)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * MAC Services Module
30  */
31 
32 #include <sys/types.h>
33 #include <sys/sysmacros.h>
34 #include <sys/stream.h>
35 #include <sys/kstat.h>
36 #include <sys/mac.h>
37 #include <sys/mac_impl.h>
38 
39 #define	MAC_KSTAT_NAME	"mac"
40 #define	MAC_KSTAT_CLASS	"net"
41 
42 static mac_stat_info_t	i_mac_si[] = {
43 	{ MAC_STAT_IFSPEED,	"ifspeed",	KSTAT_DATA_UINT64,	0 },
44 	{ MAC_STAT_MULTIRCV,	"multircv",	KSTAT_DATA_UINT32,	0 },
45 	{ MAC_STAT_BRDCSTRCV,	"brdcstrcv",	KSTAT_DATA_UINT32,	0 },
46 	{ MAC_STAT_MULTIXMT,	"multixmt",	KSTAT_DATA_UINT32,	0 },
47 	{ MAC_STAT_BRDCSTXMT,	"brdcstxmt",	KSTAT_DATA_UINT32,	0 },
48 	{ MAC_STAT_NORCVBUF,	"norcvbuf",	KSTAT_DATA_UINT32,	0 },
49 	{ MAC_STAT_IERRORS,	"ierrors",	KSTAT_DATA_UINT32,	0 },
50 	{ MAC_STAT_UNKNOWNS,	"unknowns",	KSTAT_DATA_UINT32,	0 },
51 	{ MAC_STAT_NOXMTBUF,	"noxmtbuf",	KSTAT_DATA_UINT32,	0 },
52 	{ MAC_STAT_OERRORS,	"oerrors",	KSTAT_DATA_UINT32,	0 },
53 	{ MAC_STAT_COLLISIONS,	"collisions",	KSTAT_DATA_UINT32,	0 },
54 	{ MAC_STAT_UNDERFLOWS,	"uflo",		KSTAT_DATA_UINT32,	0 },
55 	{ MAC_STAT_OVERFLOWS,	"oflo",		KSTAT_DATA_UINT32,	0 },
56 	{ MAC_STAT_RBYTES,	"rbytes",	KSTAT_DATA_UINT32,	0 },
57 	{ MAC_STAT_IPACKETS,	"ipackets",	KSTAT_DATA_UINT32,	0 },
58 	{ MAC_STAT_OBYTES,	"obytes",	KSTAT_DATA_UINT32,	0 },
59 	{ MAC_STAT_OPACKETS,	"opackets",	KSTAT_DATA_UINT32,	0 },
60 	{ MAC_STAT_RBYTES,	"rbytes64",	KSTAT_DATA_UINT64,	0 },
61 	{ MAC_STAT_IPACKETS,	"ipackets64",	KSTAT_DATA_UINT64,	0 },
62 	{ MAC_STAT_OBYTES,	"obytes64",	KSTAT_DATA_UINT64,	0 },
63 	{ MAC_STAT_OPACKETS,	"opackets64",	KSTAT_DATA_UINT64,	0 }
64 };
65 
66 #define	MAC_NKSTAT \
67 	(sizeof (i_mac_si) / sizeof (mac_stat_info_t))
68 
69 static mac_stat_info_t	i_mac_mod_si[] = {
70 	{ MAC_STAT_LINK_STATE,	"link_state",	KSTAT_DATA_UINT32,
71 	    (uint64_t)LINK_STATE_UNKNOWN },
72 	{ MAC_STAT_LINK_UP,	"link_up",	KSTAT_DATA_UINT32,	0 },
73 	{ MAC_STAT_PROMISC,	"promisc",	KSTAT_DATA_UINT32,	0 }
74 };
75 
76 #define	MAC_MOD_NKSTAT \
77 	(sizeof (i_mac_mod_si) / sizeof (mac_stat_info_t))
78 
79 #define	MAC_MOD_KSTAT_OFFSET	0
80 #define	MAC_KSTAT_OFFSET	MAC_MOD_KSTAT_OFFSET + MAC_MOD_NKSTAT
81 #define	MAC_TYPE_KSTAT_OFFSET	MAC_KSTAT_OFFSET + MAC_NKSTAT
82 
83 /*
84  * Private functions.
85  */
86 
87 static int
88 i_mac_stat_update(kstat_t *ksp, int rw)
89 {
90 	mac_impl_t	*mip = ksp->ks_private;
91 	kstat_named_t	*knp = ksp->ks_data;
92 	uint_t		i;
93 	uint64_t	val;
94 	mac_stat_info_t	*msi;
95 	uint_t		msi_index;
96 
97 	if (rw != KSTAT_READ)
98 		return (EACCES);
99 
100 	for (i = 0; i < mip->mi_kstat_count; i++, msi_index++) {
101 		if (i == MAC_MOD_KSTAT_OFFSET) {
102 			msi_index = 0;
103 			msi = i_mac_mod_si;
104 		} else if (i == MAC_KSTAT_OFFSET) {
105 			msi_index = 0;
106 			msi = i_mac_si;
107 		} else if (i == MAC_TYPE_KSTAT_OFFSET) {
108 			msi_index = 0;
109 			msi = mip->mi_type->mt_stats;
110 		}
111 
112 		val = mac_stat_get((mac_handle_t)mip, msi[msi_index].msi_stat);
113 		switch (msi[msi_index].msi_type) {
114 		case KSTAT_DATA_UINT64:
115 			knp->value.ui64 = val;
116 			break;
117 		case KSTAT_DATA_UINT32:
118 			knp->value.ui32 = (uint32_t)val;
119 			break;
120 		default:
121 			ASSERT(B_FALSE);
122 			break;
123 		}
124 
125 		knp++;
126 	}
127 
128 	return (0);
129 }
130 
131 static void
132 i_mac_kstat_init(kstat_named_t *knp, mac_stat_info_t *si, uint_t count)
133 {
134 	int i;
135 	for (i = 0; i < count; i++) {
136 		kstat_named_init(knp, si[i].msi_name, si[i].msi_type);
137 		knp++;
138 	}
139 }
140 
141 /*
142  * Exported functions.
143  */
144 
145 /*
146  * Create the "mac" kstat.  The "mac" kstat is comprised of three kinds of
147  * statistics: statistics maintained by the mac module itself, generic mac
148  * statistics maintained by the driver, and MAC-type specific statistics
149  * also maintained by the driver.
150  */
151 void
152 mac_stat_create(mac_impl_t *mip)
153 {
154 	kstat_t		*ksp;
155 	kstat_named_t	*knp;
156 	uint_t		count;
157 
158 	count = MAC_MOD_NKSTAT + MAC_NKSTAT + mip->mi_type->mt_statcount;
159 	ksp = kstat_create(mip->mi_drvname, mip->mi_instance, MAC_KSTAT_NAME,
160 	    MAC_KSTAT_CLASS, KSTAT_TYPE_NAMED, count, 0);
161 	if (ksp == NULL)
162 		return;
163 
164 	ksp->ks_update = i_mac_stat_update;
165 	ksp->ks_private = mip;
166 	mip->mi_ksp = ksp;
167 	mip->mi_kstat_count = count;
168 
169 	knp = (kstat_named_t *)ksp->ks_data;
170 	i_mac_kstat_init(knp, i_mac_mod_si, MAC_MOD_NKSTAT);
171 	knp += MAC_MOD_NKSTAT;
172 	i_mac_kstat_init(knp, i_mac_si, MAC_NKSTAT);
173 	if (mip->mi_type->mt_statcount > 0) {
174 		knp += MAC_NKSTAT;
175 		i_mac_kstat_init(knp, mip->mi_type->mt_stats,
176 		    mip->mi_type->mt_statcount);
177 	}
178 
179 	kstat_install(ksp);
180 }
181 
182 /*ARGSUSED*/
183 void
184 mac_stat_destroy(mac_impl_t *mip)
185 {
186 	if (mip->mi_ksp != NULL) {
187 		kstat_delete(mip->mi_ksp);
188 		mip->mi_ksp = NULL;
189 		mip->mi_kstat_count = 0;
190 	}
191 }
192 
193 uint64_t
194 mac_stat_default(mac_impl_t *mip, uint_t stat)
195 {
196 	uint_t	stat_index;
197 
198 	if (IS_MAC_STAT(stat)) {
199 		stat_index = stat - MAC_STAT_MIN;
200 		return (i_mac_si[stat_index].msi_default);
201 	}
202 	ASSERT(IS_MACTYPE_STAT(stat));
203 	stat_index = stat - MACTYPE_STAT_MIN;
204 	return (mip->mi_type->mt_stats[stat_index].msi_default);
205 }
206