1 /*        $NetBSD: if_stats.h,v 1.6 2024/07/01 13:13:37 riastradh Exp $         */
2 
3 /*-
4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _NET_IF_STATS_H_
33 #define _NET_IF_STATS_H_
34 
35 #include <net/net_stats.h>
36 
37 /*
38  * Interface statistics.  All values are unsigned 64-bit.
39  */
40 typedef enum {
41           if_ipackets                   = 0,      /* packets received on interface */
42           if_ierrors                    = 1,      /* input errors on interface */
43           if_opackets                   = 2,      /* packets sent on interface */
44           if_oerrors                    = 3,      /* output errors on interface */
45           if_collisions                 = 4,      /* collisions on csma interfaces */
46           if_ibytes           = 5,      /* total number of octets received */
47           if_obytes           = 6,      /* total number of octets sent */
48           if_imcasts                    = 7,      /* packets received via multicast */
49           if_omcasts                    = 8,      /* packets sent via multicast */
50           if_iqdrops                    = 9,      /* dropped on input, this interface */
51           if_noproto                    = 10,     /* destined for unsupported protocol */
52 
53           IF_NSTATS           = 11
54 } if_stat_t;
55 
56 #ifdef _KERNEL
57 
58 #include <sys/sdt.h>
59 
60 SDT_PROBE_DECLARE(sdt, net, interface, stat);
61 
62 #define   IF_STAT_GETREF(ifp) _NET_STAT_GETREF((ifp)->if_stats)
63 #define   IF_STAT_PUTREF(ifp) _NET_STAT_PUTREF((ifp)->if_stats)
64 
65 static inline void
if_statinc(ifnet_t * ifp,if_stat_t x)66 if_statinc(ifnet_t *ifp, if_stat_t x)
67 {
68           SDT_PROBE3(sdt, net, interface, stat,  ifp, x, +1);
69           _NET_STATINC((ifp)->if_stats, x);
70 }
71 
72 static inline void
if_statinc_ref(ifnet_t * ifp,net_stat_ref_t nsr,if_stat_t x)73 if_statinc_ref(ifnet_t *ifp, net_stat_ref_t nsr, if_stat_t x)
74 {
75           SDT_PROBE3(sdt, net, interface, stat,  ifp, x, +1);
76           _NET_STATINC_REF(nsr, x);
77 }
78 
79 static inline void
if_statdec(ifnet_t * ifp,if_stat_t x)80 if_statdec(ifnet_t *ifp, if_stat_t x)
81 {
82           SDT_PROBE3(sdt, net, interface, stat,  ifp, x, -1);
83           _NET_STATDEC((ifp)->if_stats, x);
84 }
85 
86 static inline void
if_statdec_ref(ifnet_t * ifp,net_stat_ref_t nsr,if_stat_t x)87 if_statdec_ref(ifnet_t *ifp, net_stat_ref_t nsr, if_stat_t x)
88 {
89           SDT_PROBE3(sdt, net, interface, stat,  ifp, x, -1);
90           _NET_STATDEC_REF(nsr, x);
91 }
92 
93 static inline void
if_statadd(ifnet_t * ifp,if_stat_t x,uint64_t v)94 if_statadd(ifnet_t *ifp, if_stat_t x, uint64_t v)
95 {
96           SDT_PROBE3(sdt, net, interface, stat,  ifp, x, v);
97           _NET_STATADD((ifp)->if_stats, x, v);
98 }
99 
100 static inline void
if_statadd_ref(ifnet_t * ifp,net_stat_ref_t nsr,if_stat_t x,uint64_t v)101 if_statadd_ref(ifnet_t *ifp, net_stat_ref_t nsr, if_stat_t x, uint64_t v)
102 {
103           SDT_PROBE3(sdt, net, interface, stat,  ifp, x, v);
104           _NET_STATADD_REF(nsr, x, v);
105 }
106 
107 static inline void
if_statadd2(ifnet_t * ifp,if_stat_t x1,uint64_t v1,if_stat_t x2,uint64_t v2)108 if_statadd2(ifnet_t *ifp, if_stat_t x1, uint64_t v1, if_stat_t x2, uint64_t v2)
109 {
110           net_stat_ref_t _nsr_ = IF_STAT_GETREF(ifp);
111           SDT_PROBE3(sdt, net, interface, stat,  ifp, x1, v1);
112           SDT_PROBE3(sdt, net, interface, stat,  ifp, x2, v2);
113           _NET_STATADD_REF(_nsr_, x1, v1);
114           _NET_STATADD_REF(_nsr_, x2, v2);
115           IF_STAT_PUTREF(ifp);
116 }
117 
118 static inline void
if_statsub(ifnet_t * ifp,if_stat_t x,uint64_t v)119 if_statsub(ifnet_t *ifp, if_stat_t x, uint64_t v)
120 {
121           SDT_PROBE3(sdt, net, interface, stat,  ifp, x, -v);
122           _NET_STATSUB((ifp)->if_stats, x, v);
123 }
124 
125 static inline void
if_statsub_ref(ifnet_t * ifp,net_stat_ref_t nsr,if_stat_t x,uint64_t v)126 if_statsub_ref(ifnet_t *ifp, net_stat_ref_t nsr, if_stat_t x, uint64_t v)
127 {
128           SDT_PROBE3(sdt, net, interface, stat,  ifp, x, -v);
129           _NET_STATSUB_REF(nsr, x, v);
130 }
131 
132 void      if_stats_init(ifnet_t *);
133 void      if_stats_fini(ifnet_t *);
134 void      if_stats_to_if_data(ifnet_t *, struct if_data *, bool);
135 
136 #endif /* _KERNEL */
137 
138 #endif /* !_NET_IF_STATS_H_ */
139