xref: /netbsd-src/sys/net/net_stats.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: net_stats.h,v 1.5 2020/01/29 03:04:55 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 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_NET_STATS_H_
33 #define _NET_NET_STATS_H_
34 
35 #ifdef _KERNEL
36 #include <sys/percpu.h>
37 
38 typedef void *net_stat_ref_t;
39 
40 #define	_NET_STAT_GETREF(stat)	((net_stat_ref_t)percpu_getref((stat)))
41 #define	_NET_STAT_PUTREF(stat)	percpu_putref((stat))
42 
43 #define	_NET_STATINC_REF(r, x)						\
44 do {									\
45 	uint64_t *_stat_ = (r);						\
46 	_stat_[x]++;							\
47 } while (/*CONSTCOND*/0)
48 
49 #define	_NET_STATINC(stat, x)						\
50 do {									\
51 	net_stat_ref_t _nsr_ = _NET_STAT_GETREF(stat);			\
52 	_NET_STATINC_REF(_nsr_, x);					\
53 	_NET_STAT_PUTREF(stat);						\
54 } while (/*CONSTCOND*/0)
55 
56 #define	_NET_STATDEC_REF(r, x)						\
57 do {									\
58 	uint64_t *_stat_ = (r);						\
59 	_stat_[x]--;							\
60 } while (/*CONSTCOND*/0)
61 
62 #define	_NET_STATDEC(stat, x)						\
63 do {									\
64 	net_stat_ref_t _nsr_ = _NET_STAT_GETREF(stat);			\
65 	_NET_STATDEC_REF(_nsr_, x);					\
66 	_NET_STAT_PUTREF(stat);						\
67 } while (/*CONSTCOND*/0)
68 
69 #define	_NET_STATADD_REF(r, x, v)					\
70 do {									\
71 	uint64_t *_stat_ = (r);						\
72 	_stat_[x] += (v);						\
73 } while (/*CONSTCOND*/0)
74 
75 #define	_NET_STATADD(stat, x, v)					\
76 do {									\
77 	net_stat_ref_t _nsr_ = _NET_STAT_GETREF(stat);			\
78 	_NET_STATADD_REF(_nsr_, x, v);					\
79 	_NET_STAT_PUTREF(stat);						\
80 } while (/*CONSTCOND*/0)
81 
82 #define	_NET_STATSUB_REF(r, x, v)					\
83 do {									\
84 	uint64_t *_stat_ = (r);						\
85 	_stat_[x] -= (v);						\
86 } while (/*CONSTCOND*/0)
87 
88 #define	_NET_STATSUB(stat, x, v)					\
89 do {									\
90 	net_stat_ref_t _nsr_ = _NET_STAT_GETREF(stat);			\
91 	_NET_STATSUB_REF(_nsr_, x, v);					\
92 	_NET_STAT_PUTREF(stat);						\
93 } while (/*CONSTCOND*/0)
94 
95 __BEGIN_DECLS
96 struct lwp;
97 struct sysctlnode;
98 
99 int	netstat_sysctl(percpu_t *, u_int,
100 		       const int *, u_int, void *,
101 		       size_t *, const void *, size_t,
102 		       const int *, struct lwp *, const struct sysctlnode *);
103 
104 #define	NETSTAT_SYSCTL(stat, nctrs)					\
105 	netstat_sysctl((stat), (nctrs), name, namelen, oldp, oldlenp,	\
106 		       newp, newlen, oname, l, rnode)
107 __END_DECLS
108 #endif /* _KERNEL */
109 
110 #endif /* !_NET_NET_STATS_H_ */
111