1/*	$NetBSD: if_stats.h,v 1.3 2021/06/29 21:19:58 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 */
40typedef 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#define	IF_STAT_GETREF(ifp)	_NET_STAT_GETREF((ifp)->if_stats)
59#define	IF_STAT_PUTREF(ifp)	_NET_STAT_PUTREF((ifp)->if_stats)
60
61static inline void
62if_statinc(ifnet_t *ifp, if_stat_t x)
63{
64	_NET_STATINC((ifp)->if_stats, x);
65}
66
67static inline void
68if_statinc_ref(net_stat_ref_t nsr, if_stat_t x)
69{
70	_NET_STATINC_REF(nsr, x);
71}
72
73static inline void
74if_statdec(ifnet_t *ifp, if_stat_t x)
75{
76	_NET_STATDEC((ifp)->if_stats, x);
77}
78
79static inline void
80if_statdec_ref(net_stat_ref_t nsr, if_stat_t x)
81{
82	_NET_STATDEC_REF(nsr, x);
83}
84
85static inline void
86if_statadd(ifnet_t *ifp, if_stat_t x, uint64_t v)
87{
88	_NET_STATADD((ifp)->if_stats, x, v);
89}
90
91static inline void
92if_statadd_ref(net_stat_ref_t nsr, if_stat_t x, uint64_t v)
93{
94	_NET_STATADD_REF(nsr, x, v);
95}
96
97static inline void
98if_statadd2(ifnet_t *ifp, if_stat_t x1, uint64_t v1, if_stat_t x2, uint64_t v2)
99{
100	net_stat_ref_t _nsr_ = IF_STAT_GETREF(ifp);
101	_NET_STATADD_REF(_nsr_, x1, v1);
102	_NET_STATADD_REF(_nsr_, x2, v2);
103	IF_STAT_PUTREF(ifp);
104}
105
106static inline void
107if_statsub(ifnet_t *ifp, if_stat_t x, uint64_t v)
108{
109	_NET_STATSUB((ifp)->if_stats, x, v);
110}
111
112static inline void
113if_statsub_ref(net_stat_ref_t nsr, if_stat_t x, uint64_t v)
114{
115	_NET_STATSUB_REF(nsr, x, v);
116}
117
118void	if_stats_init(ifnet_t *);
119void	if_stats_fini(ifnet_t *);
120void	if_stats_to_if_data(ifnet_t *, struct if_data *, bool);
121
122#endif /* _KERNEL */
123
124#endif /* !_NET_IF_STATS_H_ */
125