xref: /illumos-gate/usr/src/uts/sun4v/io/vgen_stats.c (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/errno.h>
31 #include <sys/param.h>
32 #include <sys/stream.h>
33 #include <sys/strsubr.h>
34 #include <sys/kmem.h>
35 #include <sys/ksynch.h>
36 #include <sys/stat.h>
37 #include <sys/vgen_stats.h>
38 
39 /*
40  * A set of common kstat statistics related functions that are
41  * used by both vnet and vsw drivers to maintain the statistics specific
42  * LDCs.
43  */
44 
45 /*
46  * Setup kstats for the LDC statistics.
47  * NOTE: the synchronization for the statistics is the
48  * responsibility of the caller.
49  */
50 kstat_t *
51 vgen_setup_kstats(char *ks_mod, int instance,
52     char *ks_name, vgen_stats_t *statsp)
53 {
54 	kstat_t *ksp;
55 	vgen_kstats_t *ldckp;
56 	size_t size;
57 
58 	size = sizeof (vgen_kstats_t) / sizeof (kstat_named_t);
59 	ksp = kstat_create(ks_mod, instance, ks_name, "net", KSTAT_TYPE_NAMED,
60 	    size, 0);
61 	if (ksp == NULL) {
62 		return (NULL);
63 	}
64 
65 	ldckp = (vgen_kstats_t *)ksp->ks_data;
66 	kstat_named_init(&ldckp->ipackets,		"ipackets",
67 	    KSTAT_DATA_ULONG);
68 	kstat_named_init(&ldckp->ipackets64,		"ipackets64",
69 	    KSTAT_DATA_ULONGLONG);
70 	kstat_named_init(&ldckp->ierrors,		"ierrors",
71 	    KSTAT_DATA_ULONG);
72 	kstat_named_init(&ldckp->opackets,		"opackets",
73 	    KSTAT_DATA_ULONG);
74 	kstat_named_init(&ldckp->opackets64,		"opackets64",
75 	    KSTAT_DATA_ULONGLONG);
76 	kstat_named_init(&ldckp->oerrors,		"oerrors",
77 	    KSTAT_DATA_ULONG);
78 
79 
80 	/* MIB II kstat variables */
81 	kstat_named_init(&ldckp->rbytes,		"rbytes",
82 	    KSTAT_DATA_ULONG);
83 	kstat_named_init(&ldckp->rbytes64,		"rbytes64",
84 	    KSTAT_DATA_ULONGLONG);
85 	kstat_named_init(&ldckp->obytes,		"obytes",
86 	    KSTAT_DATA_ULONG);
87 	kstat_named_init(&ldckp->obytes64,		"obytes64",
88 	    KSTAT_DATA_ULONGLONG);
89 	kstat_named_init(&ldckp->multircv,		"multircv",
90 	    KSTAT_DATA_ULONG);
91 	kstat_named_init(&ldckp->multixmt,		"multixmt",
92 	    KSTAT_DATA_ULONG);
93 	kstat_named_init(&ldckp->brdcstrcv,		"brdcstrcv",
94 	    KSTAT_DATA_ULONG);
95 	kstat_named_init(&ldckp->brdcstxmt,		"brdcstxmt",
96 	    KSTAT_DATA_ULONG);
97 	kstat_named_init(&ldckp->norcvbuf,		"norcvbuf",
98 	    KSTAT_DATA_ULONG);
99 	kstat_named_init(&ldckp->noxmtbuf,		"noxmtbuf",
100 	    KSTAT_DATA_ULONG);
101 
102 	/* Tx stats */
103 	kstat_named_init(&ldckp->tx_no_desc,		"tx_no_desc",
104 	    KSTAT_DATA_ULONG);
105 	kstat_named_init(&ldckp->tx_qfull,		"tx_qfull",
106 	    KSTAT_DATA_ULONG);
107 	kstat_named_init(&ldckp->tx_pri_fail,		"tx_pri_fail",
108 	    KSTAT_DATA_ULONG);
109 	kstat_named_init(&ldckp->tx_pri_packets,	"tx_pri_packets",
110 	    KSTAT_DATA_ULONGLONG);
111 	kstat_named_init(&ldckp->tx_pri_bytes,		"tx_pri_bytes",
112 	    KSTAT_DATA_ULONGLONG);
113 
114 	/* Rx stats */
115 	kstat_named_init(&ldckp->rx_allocb_fail,	"rx_allocb_fail",
116 	    KSTAT_DATA_ULONG);
117 	kstat_named_init(&ldckp->rx_vio_allocb_fail,	"rx_vio_allocb_fail",
118 	    KSTAT_DATA_ULONG);
119 	kstat_named_init(&ldckp->rx_lost_pkts,		"rx_lost_pkts",
120 	    KSTAT_DATA_ULONG);
121 	kstat_named_init(&ldckp->rx_pri_fail,		"rx_pri_fail",
122 	    KSTAT_DATA_ULONG);
123 	kstat_named_init(&ldckp->rx_pri_packets,	"rx_pri_packets",
124 	    KSTAT_DATA_ULONGLONG);
125 	kstat_named_init(&ldckp->rx_pri_bytes,		"rx_pri_bytes",
126 	    KSTAT_DATA_ULONGLONG);
127 
128 	/* Interrupt stats */
129 	kstat_named_init(&ldckp->callbacks,		"callbacks",
130 	    KSTAT_DATA_ULONG);
131 	kstat_named_init(&ldckp->dring_data_acks,	"dring_data_acks",
132 	    KSTAT_DATA_ULONG);
133 	kstat_named_init(&ldckp->dring_stopped_acks,	"dring_stopped_acks",
134 	    KSTAT_DATA_ULONG);
135 	kstat_named_init(&ldckp->dring_data_msgs,	"dring_data_msgs",
136 	    KSTAT_DATA_ULONG);
137 
138 	ksp->ks_update = vgen_kstat_update;
139 	ksp->ks_private = (void *)statsp;
140 	kstat_install(ksp);
141 	return (ksp);
142 }
143 
144 /*
145  * Destroy kstats.
146  */
147 void
148 vgen_destroy_kstats(kstat_t *ksp)
149 {
150 	if (ksp != NULL)
151 		kstat_delete(ksp);
152 }
153 
154 /*
155  * Update the kstats.
156  */
157 int
158 vgen_kstat_update(kstat_t *ksp, int rw)
159 {
160 	vgen_stats_t *statsp;
161 	vgen_kstats_t *ldckp;
162 
163 	statsp = (vgen_stats_t *)ksp->ks_private;
164 	ldckp = (vgen_kstats_t *)ksp->ks_data;
165 
166 	if (rw == KSTAT_READ) {
167 		ldckp->ipackets.value.ul	= (uint32_t)statsp->ipackets +
168 		    (uint32_t)statsp->rx_pri_packets;
169 		ldckp->ipackets64.value.ull	= statsp->ipackets +
170 		    statsp->rx_pri_packets;
171 		ldckp->ierrors.value.ul		= statsp->ierrors;
172 		ldckp->opackets.value.ul	= (uint32_t)statsp->opackets +
173 		    (uint32_t)statsp->tx_pri_packets;
174 		ldckp->opackets64.value.ull	= statsp->opackets +
175 		    statsp->tx_pri_packets;
176 		ldckp->oerrors.value.ul		= statsp->oerrors;
177 
178 		/*
179 		 * MIB II kstat variables
180 		 */
181 		ldckp->rbytes.value.ul		= (uint32_t)statsp->rbytes +
182 		    (uint32_t)statsp->rx_pri_bytes;
183 		ldckp->rbytes64.value.ull	= statsp->rbytes +
184 		    statsp->rx_pri_bytes;
185 		ldckp->obytes.value.ul		= (uint32_t)statsp->obytes +
186 		    (uint32_t)statsp->tx_pri_bytes;
187 		ldckp->obytes64.value.ull	= statsp->obytes +
188 		    statsp->tx_pri_bytes;
189 		ldckp->multircv.value.ul	= statsp->multircv;
190 		ldckp->multixmt.value.ul	= statsp->multixmt;
191 		ldckp->brdcstrcv.value.ul	= statsp->brdcstrcv;
192 		ldckp->brdcstxmt.value.ul	= statsp->brdcstxmt;
193 		ldckp->norcvbuf.value.ul	= statsp->norcvbuf;
194 		ldckp->noxmtbuf.value.ul	= statsp->noxmtbuf;
195 
196 		ldckp->tx_no_desc.value.ul	= statsp->tx_no_desc;
197 		ldckp->tx_qfull.value.ul	= statsp->tx_qfull;
198 		ldckp->tx_pri_fail.value.ul	= statsp->tx_pri_fail;
199 		ldckp->tx_pri_packets.value.ull	= statsp->tx_pri_packets;
200 		ldckp->tx_pri_bytes.value.ull	= statsp->tx_pri_bytes;
201 
202 		ldckp->rx_allocb_fail.value.ul	= statsp->rx_allocb_fail;
203 		ldckp->rx_vio_allocb_fail.value.ul = statsp->rx_vio_allocb_fail;
204 		ldckp->rx_lost_pkts.value.ul	= statsp->rx_lost_pkts;
205 		ldckp->rx_pri_fail.value.ul	= statsp->rx_pri_fail;
206 		ldckp->rx_pri_packets.value.ull	= statsp->rx_pri_packets;
207 		ldckp->rx_pri_bytes.value.ull	= statsp->rx_pri_bytes;
208 
209 		ldckp->callbacks.value.ul	= statsp->callbacks;
210 		ldckp->dring_data_acks.value.ul	= statsp->dring_data_acks;
211 		ldckp->dring_stopped_acks.value.ul = statsp->dring_stopped_acks;
212 		ldckp->dring_data_msgs.value.ul	= statsp->dring_data_msgs;
213 	} else {
214 		statsp->ipackets	= ldckp->ipackets64.value.ull;
215 		statsp->ierrors		= ldckp->ierrors.value.ul;
216 		statsp->opackets	= ldckp->opackets64.value.ull;
217 		statsp->oerrors		= ldckp->oerrors.value.ul;
218 
219 		/*
220 		 * MIB II kstat variables
221 		 */
222 		statsp->rbytes		= ldckp->rbytes64.value.ull;
223 		statsp->obytes		= ldckp->obytes64.value.ull;
224 		statsp->multircv	= ldckp->multircv.value.ul;
225 		statsp->multixmt	= ldckp->multixmt.value.ul;
226 		statsp->brdcstrcv	= ldckp->brdcstrcv.value.ul;
227 		statsp->brdcstxmt	= ldckp->brdcstxmt.value.ul;
228 		statsp->norcvbuf	= ldckp->norcvbuf.value.ul;
229 		statsp->noxmtbuf	= ldckp->noxmtbuf.value.ul;
230 
231 		statsp->tx_no_desc	= ldckp->tx_no_desc.value.ul;
232 		statsp->tx_qfull	= ldckp->tx_qfull.value.ul;
233 		statsp->tx_pri_fail	= ldckp->tx_pri_fail.value.ul;
234 		statsp->tx_pri_packets	= ldckp->tx_pri_packets.value.ull;
235 		statsp->tx_pri_bytes	= ldckp->tx_pri_bytes.value.ull;
236 
237 		statsp->rx_allocb_fail	= ldckp->rx_allocb_fail.value.ul;
238 		statsp->rx_vio_allocb_fail = ldckp->rx_vio_allocb_fail.value.ul;
239 		statsp->rx_lost_pkts	= ldckp->rx_lost_pkts.value.ul;
240 		statsp->rx_pri_fail	= ldckp->rx_pri_fail.value.ul;
241 		statsp->rx_pri_packets	= ldckp->rx_pri_packets.value.ull;
242 		statsp->rx_pri_bytes	= ldckp->rx_pri_bytes.value.ull;
243 
244 		statsp->callbacks	= ldckp->callbacks.value.ul;
245 		statsp->dring_data_acks	= ldckp->dring_data_acks.value.ul;
246 		statsp->dring_stopped_acks = ldckp->dring_stopped_acks.value.ul;
247 		statsp->dring_data_msgs	= ldckp->dring_data_msgs.value.ul;
248 	}
249 
250 	return (0);
251 }
252