xref: /openbmc/linux/include/net/snmp.h (revision 20e2fc42)
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 #define DEFINE_SNMP_STAT(type, name)	\
115 	__typeof__(type) __percpu *name
116 #define DEFINE_SNMP_STAT_ATOMIC(type, name)	\
117 	__typeof__(type) *name
118 #define DECLARE_SNMP_STAT(type, name)	\
119 	extern __typeof__(type) __percpu *name
120 
121 #define __SNMP_INC_STATS(mib, field)	\
122 			__this_cpu_inc(mib->mibs[field])
123 
124 #define SNMP_INC_STATS_ATOMIC_LONG(mib, field)	\
125 			atomic_long_inc(&mib->mibs[field])
126 
127 #define SNMP_INC_STATS(mib, field)	\
128 			this_cpu_inc(mib->mibs[field])
129 
130 #define SNMP_DEC_STATS(mib, field)	\
131 			this_cpu_dec(mib->mibs[field])
132 
133 #define __SNMP_ADD_STATS(mib, field, addend)	\
134 			__this_cpu_add(mib->mibs[field], addend)
135 
136 #define SNMP_ADD_STATS(mib, field, addend)	\
137 			this_cpu_add(mib->mibs[field], addend)
138 #define SNMP_UPD_PO_STATS(mib, basefield, addend)	\
139 	do { \
140 		__typeof__((mib->mibs) + 0) ptr = mib->mibs;	\
141 		this_cpu_inc(ptr[basefield##PKTS]);		\
142 		this_cpu_add(ptr[basefield##OCTETS], addend);	\
143 	} while (0)
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 
151 
152 #if BITS_PER_LONG==32
153 
154 #define __SNMP_ADD_STATS64(mib, field, addend) 				\
155 	do {								\
156 		__typeof__(*mib) *ptr = raw_cpu_ptr(mib);		\
157 		u64_stats_update_begin(&ptr->syncp);			\
158 		ptr->mibs[field] += addend;				\
159 		u64_stats_update_end(&ptr->syncp);			\
160 	} while (0)
161 
162 #define SNMP_ADD_STATS64(mib, field, addend) 				\
163 	do {								\
164 		local_bh_disable();					\
165 		__SNMP_ADD_STATS64(mib, field, addend);			\
166 		local_bh_enable();				\
167 	} while (0)
168 
169 #define __SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
170 #define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
171 #define __SNMP_UPD_PO_STATS64(mib, basefield, addend)			\
172 	do {								\
173 		__typeof__(*mib) *ptr;				\
174 		ptr = raw_cpu_ptr((mib));				\
175 		u64_stats_update_begin(&ptr->syncp);			\
176 		ptr->mibs[basefield##PKTS]++;				\
177 		ptr->mibs[basefield##OCTETS] += addend;			\
178 		u64_stats_update_end(&ptr->syncp);			\
179 	} while (0)
180 #define SNMP_UPD_PO_STATS64(mib, basefield, addend)			\
181 	do {								\
182 		local_bh_disable();					\
183 		__SNMP_UPD_PO_STATS64(mib, basefield, addend);		\
184 		local_bh_enable();				\
185 	} while (0)
186 #else
187 #define __SNMP_INC_STATS64(mib, field)		__SNMP_INC_STATS(mib, field)
188 #define SNMP_INC_STATS64(mib, field)		SNMP_INC_STATS(mib, field)
189 #define SNMP_DEC_STATS64(mib, field)		SNMP_DEC_STATS(mib, field)
190 #define __SNMP_ADD_STATS64(mib, field, addend)	__SNMP_ADD_STATS(mib, field, addend)
191 #define SNMP_ADD_STATS64(mib, field, addend)	SNMP_ADD_STATS(mib, field, addend)
192 #define SNMP_UPD_PO_STATS64(mib, basefield, addend) SNMP_UPD_PO_STATS(mib, basefield, addend)
193 #define __SNMP_UPD_PO_STATS64(mib, basefield, addend) __SNMP_UPD_PO_STATS(mib, basefield, addend)
194 #endif
195 
196 #endif
197