xref: /linux/include/net/snmp.h (revision 8bc7c5e525584903ea83332e18a2118ed3b1985e)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *
4  *		SNMP MIB entries for the IP subsystem.
5  *
6  *		Alan Cox <gw4pts@gw4pts.ampr.org>
7  *
8  *		We don't chose to implement SNMP in the kernel (this would
9  *		be silly as SNMP is a pain in the backside in places). We do
10  *		however need to collect the MIB statistics and export them
11  *		out of /proc (eventually)
12  */
13 
14 #ifndef _SNMP_H
15 #define _SNMP_H
16 
17 #include <linux/cache.h>
18 #include <linux/snmp.h>
19 #include <linux/smp.h>
20 
21 /*
22  * Mibs are stored in array of unsigned long.
23  */
24 /*
25  * struct snmp_mib{}
26  *  - list of entries for particular API (such as /proc/net/snmp)
27  *  - name of entries.
28  */
29 struct snmp_mib {
30 	const char *name;
31 	int entry;
32 };
33 
34 #define SNMP_MIB_ITEM(_name,_entry)	{	\
35 	.name = _name,				\
36 	.entry = _entry,			\
37 }
38 
39 #define SNMP_MIB_SENTINEL {	\
40 	.name = NULL,		\
41 	.entry = 0,		\
42 }
43 
44 /*
45  * We use unsigned longs for most mibs but u64 for ipstats.
46  */
47 #include <linux/u64_stats_sync.h>
48 
49 /* IPstats */
50 #define IPSTATS_MIB_MAX	__IPSTATS_MIB_MAX
51 struct ipstats_mib {
52 	/* mibs[] must be first field of struct ipstats_mib */
53 	u64		mibs[IPSTATS_MIB_MAX];
54 	struct u64_stats_sync syncp;
55 };
56 
57 /* ICMP */
58 #define ICMP_MIB_MAX	__ICMP_MIB_MAX
59 struct icmp_mib {
60 	unsigned long	mibs[ICMP_MIB_MAX];
61 };
62 
63 #define ICMPMSG_MIB_MAX	__ICMPMSG_MIB_MAX
64 struct icmpmsg_mib {
65 	atomic_long_t	mibs[ICMPMSG_MIB_MAX];
66 };
67 
68 /* ICMP6 (IPv6-ICMP) */
69 #define ICMP6_MIB_MAX	__ICMP6_MIB_MAX
70 /* per network ns counters */
71 struct icmpv6_mib {
72 	unsigned long	mibs[ICMP6_MIB_MAX];
73 };
74 /* per device counters, (shared on all cpus) */
75 struct icmpv6_mib_device {
76 	atomic_long_t	mibs[ICMP6_MIB_MAX];
77 };
78 
79 #define ICMP6MSG_MIB_MAX  __ICMP6MSG_MIB_MAX
80 /* per network ns counters */
81 struct icmpv6msg_mib {
82 	atomic_long_t	mibs[ICMP6MSG_MIB_MAX];
83 };
84 /* per device counters, (shared on all cpus) */
85 struct icmpv6msg_mib_device {
86 	atomic_long_t	mibs[ICMP6MSG_MIB_MAX];
87 };
88 
89 
90 /* TCP */
91 #define TCP_MIB_MAX	__TCP_MIB_MAX
92 struct tcp_mib {
93 	unsigned long	mibs[TCP_MIB_MAX];
94 };
95 
96 /* UDP */
97 #define UDP_MIB_MAX	__UDP_MIB_MAX
98 struct udp_mib {
99 	unsigned long	mibs[UDP_MIB_MAX];
100 };
101 
102 /* Linux */
103 #define LINUX_MIB_MAX	__LINUX_MIB_MAX
104 struct linux_mib {
105 	unsigned long	mibs[LINUX_MIB_MAX];
106 };
107 
108 /* Linux Xfrm */
109 #define LINUX_MIB_XFRMMAX	__LINUX_MIB_XFRMMAX
110 struct linux_xfrm_mib {
111 	unsigned long	mibs[LINUX_MIB_XFRMMAX];
112 };
113 
114 /* Linux TLS */
115 #define LINUX_MIB_TLSMAX	__LINUX_MIB_TLSMAX
116 struct linux_tls_mib {
117 	unsigned long	mibs[LINUX_MIB_TLSMAX];
118 };
119 
120 #define DEFINE_SNMP_STAT(type, name)	\
121 	__typeof__(type) __percpu *name
122 #define DEFINE_SNMP_STAT_ATOMIC(type, name)	\
123 	__typeof__(type) *name
124 #define DECLARE_SNMP_STAT(type, name)	\
125 	extern __typeof__(type) __percpu *name
126 
127 #define __SNMP_INC_STATS(mib, field)	\
128 			__this_cpu_inc(mib->mibs[field])
129 
130 #define SNMP_INC_STATS_ATOMIC_LONG(mib, field)	\
131 			atomic_long_inc(&mib->mibs[field])
132 
133 #define SNMP_INC_STATS(mib, field)	\
134 			this_cpu_inc(mib->mibs[field])
135 
136 #define SNMP_DEC_STATS(mib, field)	\
137 			this_cpu_dec(mib->mibs[field])
138 
139 #define __SNMP_ADD_STATS(mib, field, addend)	\
140 			__this_cpu_add(mib->mibs[field], addend)
141 
142 #define SNMP_ADD_STATS(mib, field, addend)	\
143 			this_cpu_add(mib->mibs[field], addend)
144 #define SNMP_UPD_PO_STATS(mib, basefield, addend)	\
145 	do { \
146 		__typeof__((mib->mibs) + 0) ptr = mib->mibs;	\
147 		this_cpu_inc(ptr[basefield##PKTS]);		\
148 		this_cpu_add(ptr[basefield##OCTETS], addend);	\
149 	} while (0)
150 #define __SNMP_UPD_PO_STATS(mib, basefield, addend)	\
151 	do { \
152 		__typeof__((mib->mibs) + 0) ptr = mib->mibs;	\
153 		__this_cpu_inc(ptr[basefield##PKTS]);		\
154 		__this_cpu_add(ptr[basefield##OCTETS], addend);	\
155 	} while (0)
156 
157 
158 #if BITS_PER_LONG==32
159 
160 #define __SNMP_ADD_STATS64(mib, field, addend) 				\
161 	do {								\
162 		__typeof__(*mib) *ptr = raw_cpu_ptr(mib);		\
163 		u64_stats_update_begin(&ptr->syncp);			\
164 		ptr->mibs[field] += addend;				\
165 		u64_stats_update_end(&ptr->syncp);			\
166 	} while (0)
167 
168 #define SNMP_ADD_STATS64(mib, field, addend) 				\
169 	do {								\
170 		local_bh_disable();					\
171 		__SNMP_ADD_STATS64(mib, field, addend);			\
172 		local_bh_enable();				\
173 	} while (0)
174 
175 #define __SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
176 #define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
177 #define __SNMP_UPD_PO_STATS64(mib, basefield, addend)			\
178 	do {								\
179 		__typeof__(*mib) *ptr;				\
180 		ptr = raw_cpu_ptr((mib));				\
181 		u64_stats_update_begin(&ptr->syncp);			\
182 		ptr->mibs[basefield##PKTS]++;				\
183 		ptr->mibs[basefield##OCTETS] += addend;			\
184 		u64_stats_update_end(&ptr->syncp);			\
185 	} while (0)
186 #define SNMP_UPD_PO_STATS64(mib, basefield, addend)			\
187 	do {								\
188 		local_bh_disable();					\
189 		__SNMP_UPD_PO_STATS64(mib, basefield, addend);		\
190 		local_bh_enable();				\
191 	} while (0)
192 #else
193 #define __SNMP_INC_STATS64(mib, field)		__SNMP_INC_STATS(mib, field)
194 #define SNMP_INC_STATS64(mib, field)		SNMP_INC_STATS(mib, field)
195 #define SNMP_DEC_STATS64(mib, field)		SNMP_DEC_STATS(mib, field)
196 #define __SNMP_ADD_STATS64(mib, field, addend)	__SNMP_ADD_STATS(mib, field, addend)
197 #define SNMP_ADD_STATS64(mib, field, addend)	SNMP_ADD_STATS(mib, field, addend)
198 #define SNMP_UPD_PO_STATS64(mib, basefield, addend) SNMP_UPD_PO_STATS(mib, basefield, addend)
199 #define __SNMP_UPD_PO_STATS64(mib, basefield, addend) __SNMP_UPD_PO_STATS(mib, basefield, addend)
200 #endif
201 
202 #endif
203