xref: /titanic_50/usr/src/uts/sun4v/io/vgen_stats.c (revision 2dd2efa5a06a9befe46075cf41e16f57533c9f98)
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 2007 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 
106 	/* Rx stats */
107 	kstat_named_init(&ldckp->rx_allocb_fail,	"rx_allocb_fail",
108 	    KSTAT_DATA_ULONG);
109 	kstat_named_init(&ldckp->rx_vio_allocb_fail,	"rx_vio_allocb_fail",
110 	    KSTAT_DATA_ULONG);
111 	kstat_named_init(&ldckp->rx_lost_pkts,		"rx_lost_pkts",
112 	    KSTAT_DATA_ULONG);
113 
114 	/* Interrupt stats */
115 	kstat_named_init(&ldckp->callbacks,		"callbacks",
116 	    KSTAT_DATA_ULONG);
117 	kstat_named_init(&ldckp->dring_data_acks,	"dring_data_acks",
118 	    KSTAT_DATA_ULONG);
119 	kstat_named_init(&ldckp->dring_stopped_acks,	"dring_stopped_acks",
120 	    KSTAT_DATA_ULONG);
121 	kstat_named_init(&ldckp->dring_data_msgs,	"dring_data_msgs",
122 	    KSTAT_DATA_ULONG);
123 
124 	ksp->ks_update = vgen_kstat_update;
125 	ksp->ks_private = (void *)statsp;
126 	kstat_install(ksp);
127 	return (ksp);
128 }
129 
130 /*
131  * Destroy kstats.
132  */
133 void
134 vgen_destroy_kstats(kstat_t *ksp)
135 {
136 	if (ksp != NULL)
137 		kstat_delete(ksp);
138 }
139 
140 /*
141  * Update the kstats.
142  */
143 int
144 vgen_kstat_update(kstat_t *ksp, int rw)
145 {
146 	vgen_stats_t *statsp;
147 	vgen_kstats_t *ldckp;
148 
149 	statsp = (vgen_stats_t *)ksp->ks_private;
150 	ldckp = (vgen_kstats_t *)ksp->ks_data;
151 
152 	if (rw == KSTAT_READ) {
153 		ldckp->ipackets.value.ul	= (uint32_t)statsp->ipackets;
154 		ldckp->ipackets64.value.ull	= statsp->ipackets;
155 		ldckp->ierrors.value.ul		= statsp->ierrors;
156 		ldckp->opackets.value.ul	= (uint32_t)statsp->opackets;
157 		ldckp->opackets64.value.ull	= statsp->opackets;
158 		ldckp->oerrors.value.ul		= statsp->oerrors;
159 
160 		/*
161 		 * MIB II kstat variables
162 		 */
163 		ldckp->rbytes.value.ul		= (uint32_t)statsp->rbytes;
164 		ldckp->rbytes64.value.ull	= statsp->rbytes;
165 		ldckp->obytes.value.ul		= (uint32_t)statsp->obytes;
166 		ldckp->obytes64.value.ull	= statsp->obytes;
167 		ldckp->multircv.value.ul	= statsp->multircv;
168 		ldckp->multixmt.value.ul	= statsp->multixmt;
169 		ldckp->brdcstrcv.value.ul	= statsp->brdcstrcv;
170 		ldckp->brdcstxmt.value.ul	= statsp->brdcstxmt;
171 		ldckp->norcvbuf.value.ul	= statsp->norcvbuf;
172 		ldckp->noxmtbuf.value.ul	= statsp->noxmtbuf;
173 
174 		ldckp->tx_no_desc.value.ul	= statsp->tx_no_desc;
175 
176 		ldckp->rx_allocb_fail.value.ul	= statsp->rx_allocb_fail;
177 		ldckp->rx_vio_allocb_fail.value.ul = statsp->rx_vio_allocb_fail;
178 		ldckp->rx_lost_pkts.value.ul	= statsp->rx_lost_pkts;
179 
180 		ldckp->callbacks.value.ul	= statsp->callbacks;
181 		ldckp->dring_data_acks.value.ul	= statsp->dring_data_acks;
182 		ldckp->dring_stopped_acks.value.ul = statsp->dring_stopped_acks;
183 		ldckp->dring_data_msgs.value.ul	= statsp->dring_data_msgs;
184 	} else {
185 		statsp->ipackets	= ldckp->ipackets64.value.ull;
186 		statsp->ierrors		= ldckp->ierrors.value.ul;
187 		statsp->opackets	= ldckp->opackets64.value.ull;
188 		statsp->oerrors		= ldckp->oerrors.value.ul;
189 
190 		/*
191 		 * MIB II kstat variables
192 		 */
193 		statsp->rbytes		= ldckp->rbytes64.value.ull;
194 		statsp->obytes		= ldckp->obytes64.value.ull;
195 		statsp->multircv	= ldckp->multircv.value.ul;
196 		statsp->multixmt	= ldckp->multixmt.value.ul;
197 		statsp->brdcstrcv	= ldckp->brdcstrcv.value.ul;
198 		statsp->brdcstxmt	= ldckp->brdcstxmt.value.ul;
199 		statsp->norcvbuf	= ldckp->norcvbuf.value.ul;
200 		statsp->noxmtbuf	= ldckp->noxmtbuf.value.ul;
201 
202 		statsp->tx_no_desc	= ldckp->tx_no_desc.value.ul;
203 
204 		statsp->rx_allocb_fail	= ldckp->rx_allocb_fail.value.ul;
205 		statsp->rx_vio_allocb_fail = ldckp->rx_vio_allocb_fail.value.ul;
206 		statsp->rx_lost_pkts	= ldckp->rx_lost_pkts.value.ul;
207 
208 		statsp->callbacks	= ldckp->callbacks.value.ul;
209 		statsp->dring_data_acks	= ldckp->dring_data_acks.value.ul;
210 		statsp->dring_stopped_acks = ldckp->dring_stopped_acks.value.ul;
211 		statsp->dring_data_msgs	= ldckp->dring_data_msgs.value.ul;
212 	}
213 
214 	return (0);
215 }
216