1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 * SNMP MIB entries for the IP subsystem.
6 * Alan Cox <gw4pts@gw4pts.ampr.org>
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)
17 #include <linux/cache.h>
18 #include <linux/snmp.h>
19 #include <linux/smp.h>
22 * Mibs are stored in array of unsigned long.
26 * - list of entries for particular API (such as /proc/net/snmp)
34 #define SNMP_MIB_ITEM(_name,_entry) { \
39 #define SNMP_MIB_SENTINEL { \
45 * We use unsigned longs for most mibs but u64 for ipstats.
47 #include <linux/u64_stats_sync.h>
50 #define IPSTATS_MIB_MAX __IPSTATS_MIB_MAX
52 /* mibs[] must be first field of struct ipstats_mib */
53 u64 mibs[IPSTATS_MIB_MAX];
54 struct u64_stats_sync syncp;
58 #define ICMP_MIB_MAX __ICMP_MIB_MAX
60 unsigned long mibs[ICMP_MIB_MAX];
63 #define ICMPMSG_MIB_MAX __ICMPMSG_MIB_MAX
65 atomic_long_t mibs[ICMPMSG_MIB_MAX];
68 /* ICMP6 (IPv6-ICMP) */
69 #define ICMP6_MIB_MAX __ICMP6_MIB_MAX
70 /* per network ns counters */
72 unsigned long mibs[ICMP6_MIB_MAX];
74 /* per device counters, (shared on all cpus) */
75 struct icmpv6_mib_device {
76 atomic_long_t mibs[ICMP6_MIB_MAX];
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];
84 /* per device counters, (shared on all cpus) */
85 struct icmpv6msg_mib_device {
86 atomic_long_t mibs[ICMP6MSG_MIB_MAX];
91 #define TCP_MIB_MAX __TCP_MIB_MAX
93 unsigned long mibs[TCP_MIB_MAX];
97 #define UDP_MIB_MAX __UDP_MIB_MAX
99 unsigned long mibs[UDP_MIB_MAX];
103 #define LINUX_MIB_MAX __LINUX_MIB_MAX
105 unsigned long mibs[LINUX_MIB_MAX];
109 #define LINUX_MIB_XFRMMAX __LINUX_MIB_XFRMMAX
110 struct linux_xfrm_mib {
111 unsigned long mibs[LINUX_MIB_XFRMMAX];
115 #define LINUX_MIB_TLSMAX __LINUX_MIB_TLSMAX
116 struct linux_tls_mib {
117 unsigned long mibs[LINUX_MIB_TLSMAX];
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
127 #define __SNMP_INC_STATS(mib, field) \
128 __this_cpu_inc(mib->mibs[field])
130 #define SNMP_INC_STATS_ATOMIC_LONG(mib, field) \
131 atomic_long_inc(&mib->mibs[field])
133 #define SNMP_INC_STATS(mib, field) \
134 this_cpu_inc(mib->mibs[field])
136 #define SNMP_DEC_STATS(mib, field) \
137 this_cpu_dec(mib->mibs[field])
139 #define __SNMP_ADD_STATS(mib, field, addend) \
140 __this_cpu_add(mib->mibs[field], addend)
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) \
146 __typeof__((mib->mibs) + 0) ptr = mib->mibs; \
147 this_cpu_inc(ptr[basefield##PKTS]); \
148 this_cpu_add(ptr[basefield##OCTETS], addend); \
150 #define __SNMP_UPD_PO_STATS(mib, basefield, addend) \
152 __typeof__((mib->mibs) + 0) ptr = mib->mibs; \
153 __this_cpu_inc(ptr[basefield##PKTS]); \
154 __this_cpu_add(ptr[basefield##OCTETS], addend); \
158 #if BITS_PER_LONG==32
160 #define __SNMP_ADD_STATS64(mib, field, addend) \
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); \
168 #define SNMP_ADD_STATS64(mib, field, addend) \
170 local_bh_disable(); \
171 __SNMP_ADD_STATS64(mib, field, addend); \
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) \
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); \
186 #define SNMP_UPD_PO_STATS64(mib, basefield, addend) \
188 local_bh_disable(); \
189 __SNMP_UPD_PO_STATS64(mib, basefield, addend); \
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)