2 * Copyright (c) 2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
31 * npe statistics class.
33 #include <sys/param.h>
34 #include <sys/sysctl.h>
45 #define AFTER(prev) ((prev)+1)
47 static const struct fmt npestats[] = {
49 { 7, "align", "align", "alignment errors" },
50 #define S_FCS AFTER(S_ALIGN)
51 { 7, "fcs", "fcs", "FCS errors" },
52 #define S_MACRX AFTER(S_FCS)
53 { 7, "macrx", "macrx", "internal MAC rx errors" },
54 #define S_RXORN AFTER(S_MACRX)
55 { 6, "overrun", "overrun", "rx overrun discards" },
56 #define S_LEARN AFTER(S_RXORN)
57 { 5, "learn", "learn", "rx learned entry discards" },
58 #define S_LARGE AFTER(S_LEARN)
59 { 5, "large", "large", "rx large frame discards" },
60 #define S_STP AFTER(S_LARGE)
61 { 5, "stp", "stp", "rx STP blocked discards" },
62 #define S_RX_VLAN_TYPE AFTER(S_STP)
63 { 5, "rx_vlan_type", "rx_vlant", "rx VLAN type filter discards" },
64 #define S_RX_VLAN_ID AFTER(S_RX_VLAN_TYPE)
65 { 5, "rx_vlan_id", "rx_vlani", "rx VLAN Id filter discards" },
66 #define S_BADSRC AFTER(S_RX_VLAN_ID)
67 { 5, "badsrc", "badsrc", "rx invalid source discards" },
68 #define S_BLACKLIST AFTER(S_BADSRC)
69 { 5, "blacklist", "blacklist", "rx black list discards" },
70 #define S_WHITELIST AFTER(S_BLACKLIST)
71 { 5, "whitelist", "whitelist", "rx white list discards" },
72 #define S_UNDERFLOW AFTER(S_WHITELIST)
73 { 5, "underflow", "underflow", "rx underflow entry discards" },
74 #define S_COLL_SINGLE AFTER(S_UNDERFLOW)
75 { 5, "collision1", "collision1", "single collision frames" },
76 #define S_COLL_MULTI AFTER(S_COLL_SINGLE)
77 { 5, "collisionM", "collisionM", "multiple collision frames" },
78 #define S_DEFERRED AFTER(S_COLL_MULTI)
79 { 5, "deferred", "deferred", "deferred transmissions" },
80 #define S_LATE AFTER(S_DEFERRED)
81 { 5, "late", "late", "late collisions" },
82 #define S_EXCESSIVE AFTER(S_LATE)
83 { 5, "excessive", "excessive", "excessive collisions" },
84 #define S_MACTX AFTER(S_EXCESSIVE)
85 { 7, "mactx", "mactx", "internal MAC tx errors" },
86 #define S_CARRIER AFTER(S_MACTX)
87 { 7, "carrier", "carrier", "carrier sense errors" },
88 #define S_TOOBIG AFTER(S_CARRIER)
89 { 7, "toobig", "toobig", "tx large frame discards" },
90 #define S_TX_VLAN_ID AFTER(S_TOOBIG)
91 { 7, "tx_vlan_id", "tx_vlani", "tx VLAN Id filter discards" },
93 #define S_LAST S_TX_VLAN_ID
96 * Stat block returned by NPE with NPE_GETSTATS msg.
99 uint32_t dot3StatsAlignmentErrors;
100 uint32_t dot3StatsFCSErrors;
101 uint32_t dot3StatsInternalMacReceiveErrors;
102 uint32_t RxOverrunDiscards;
103 uint32_t RxLearnedEntryDiscards;
104 uint32_t RxLargeFramesDiscards;
105 uint32_t RxSTPBlockedDiscards;
106 uint32_t RxVLANTypeFilterDiscards;
107 uint32_t RxVLANIdFilterDiscards;
108 uint32_t RxInvalidSourceDiscards;
109 uint32_t RxBlackListDiscards;
110 uint32_t RxWhiteListDiscards;
111 uint32_t RxUnderflowEntryDiscards;
112 uint32_t dot3StatsSingleCollisionFrames;
113 uint32_t dot3StatsMultipleCollisionFrames;
114 uint32_t dot3StatsDeferredTransmissions;
115 uint32_t dot3StatsLateCollisions;
116 uint32_t dot3StatsExcessiveCollisions;
117 uint32_t dot3StatsInternalMacTransmitErrors;
118 uint32_t dot3StatsCarrierSenseErrors;
119 uint32_t TxLargeFrameDiscards;
120 uint32_t TxVLANIdFilterDiscards;
123 struct npestatfoo_p {
124 struct npestatfoo base;
128 struct npestats total;
132 npe_setifname(struct npestatfoo *wf0, const char *ifname)
134 struct npestatfoo_p *wf = (struct npestatfoo_p *) wf0;
137 snprintf(wf->oid, sizeof(wf->oid), "dev.npe.%s.stats", ifname+3);
139 if (sysctlnametomib(wf->oid, wf->mib, &len) < 0)
140 err(1, "sysctlnametomib: %s", wf->oid);
144 npe_collect(struct npestatfoo_p *wf, struct npestats *stats)
146 size_t len = sizeof(struct npestats);
147 if (sysctl(wf->mib, 4, stats, &len, NULL, 0) < 0)
148 err(1, "sysctl: %s", wf->oid);
152 npe_collect_cur(struct bsdstat *sf)
154 struct npestatfoo_p *wf = (struct npestatfoo_p *) sf;
156 npe_collect(wf, &wf->cur);
160 npe_collect_tot(struct bsdstat *sf)
162 struct npestatfoo_p *wf = (struct npestatfoo_p *) sf;
164 npe_collect(wf, &wf->total);
168 npe_update_tot(struct bsdstat *sf)
170 struct npestatfoo_p *wf = (struct npestatfoo_p *) sf;
176 npe_get_curstat(struct bsdstat *sf, int s, char b[], size_t bs)
178 struct npestatfoo_p *wf = (struct npestatfoo_p *) sf;
180 snprintf(b, bs, "%u", wf->cur.x - wf->total.x); return 1
183 case S_ALIGN: STAT(dot3StatsAlignmentErrors);
184 case S_FCS: STAT(dot3StatsFCSErrors);
185 case S_MACRX: STAT(dot3StatsInternalMacReceiveErrors);
186 case S_RXORN: STAT(RxOverrunDiscards);
187 case S_LEARN: STAT(RxLearnedEntryDiscards);
188 case S_LARGE: STAT(RxLargeFramesDiscards);
189 case S_STP: STAT(RxSTPBlockedDiscards);
190 case S_RX_VLAN_TYPE: STAT(RxVLANTypeFilterDiscards);
191 case S_RX_VLAN_ID: STAT(RxVLANIdFilterDiscards);
192 case S_BADSRC: STAT(RxInvalidSourceDiscards);
193 case S_BLACKLIST: STAT(RxBlackListDiscards);
194 case S_WHITELIST: STAT(RxWhiteListDiscards);
195 case S_UNDERFLOW: STAT(RxUnderflowEntryDiscards);
196 case S_COLL_SINGLE: STAT(dot3StatsSingleCollisionFrames);
197 case S_COLL_MULTI: STAT(dot3StatsMultipleCollisionFrames);
198 case S_DEFERRED: STAT(dot3StatsDeferredTransmissions);
199 case S_LATE: STAT(dot3StatsLateCollisions);
200 case S_EXCESSIVE: STAT(dot3StatsExcessiveCollisions);
201 case S_MACTX: STAT(dot3StatsInternalMacTransmitErrors);
202 case S_CARRIER: STAT(dot3StatsCarrierSenseErrors);
203 case S_TOOBIG: STAT(TxLargeFrameDiscards);
204 case S_TX_VLAN_ID: STAT(TxVLANIdFilterDiscards);
212 npe_get_totstat(struct bsdstat *sf, int s, char b[], size_t bs)
214 struct npestatfoo_p *wf = (struct npestatfoo_p *) sf;
216 snprintf(b, bs, "%u", wf->total.x); return 1
219 case S_ALIGN: STAT(dot3StatsAlignmentErrors);
220 case S_FCS: STAT(dot3StatsFCSErrors);
221 case S_MACRX: STAT(dot3StatsInternalMacReceiveErrors);
222 case S_RXORN: STAT(RxOverrunDiscards);
223 case S_LEARN: STAT(RxLearnedEntryDiscards);
224 case S_LARGE: STAT(RxLargeFramesDiscards);
225 case S_STP: STAT(RxSTPBlockedDiscards);
226 case S_RX_VLAN_TYPE: STAT(RxVLANTypeFilterDiscards);
227 case S_RX_VLAN_ID: STAT(RxVLANIdFilterDiscards);
228 case S_BADSRC: STAT(RxInvalidSourceDiscards);
229 case S_BLACKLIST: STAT(RxBlackListDiscards);
230 case S_WHITELIST: STAT(RxWhiteListDiscards);
231 case S_UNDERFLOW: STAT(RxUnderflowEntryDiscards);
232 case S_COLL_SINGLE: STAT(dot3StatsSingleCollisionFrames);
233 case S_COLL_MULTI: STAT(dot3StatsMultipleCollisionFrames);
234 case S_DEFERRED: STAT(dot3StatsDeferredTransmissions);
235 case S_LATE: STAT(dot3StatsLateCollisions);
236 case S_EXCESSIVE: STAT(dot3StatsExcessiveCollisions);
237 case S_MACTX: STAT(dot3StatsInternalMacTransmitErrors);
238 case S_CARRIER: STAT(dot3StatsCarrierSenseErrors);
239 case S_TOOBIG: STAT(TxLargeFrameDiscards);
240 case S_TX_VLAN_ID: STAT(TxVLANIdFilterDiscards);
247 BSDSTAT_DEFINE_BOUNCE(npestatfoo)
250 npestats_new(const char *ifname, const char *fmtstring)
252 struct npestatfoo_p *wf;
254 wf = calloc(1, sizeof(struct npestatfoo_p));
256 bsdstat_init(&wf->base.base, "npestats", npestats,
258 /* override base methods */
259 wf->base.base.collect_cur = npe_collect_cur;
260 wf->base.base.collect_tot = npe_collect_tot;
261 wf->base.base.get_curstat = npe_get_curstat;
262 wf->base.base.get_totstat = npe_get_totstat;
263 wf->base.base.update_tot = npe_update_tot;
265 /* setup bounce functions for public methods */
266 BSDSTAT_BOUNCE(wf, npestatfoo);
268 /* setup our public methods */
269 wf->base.setifname = npe_setifname;
271 npe_setifname(&wf->base, ifname);
272 wf->base.setfmt(&wf->base, fmtstring);