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 */
29struct 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/*
40 * We use unsigned longs for most mibs but u64 for ipstats.
41 */
42#include <linux/u64_stats_sync.h>
43
44/* IPstats */
45#define IPSTATS_MIB_MAX __IPSTATS_MIB_MAX
46struct ipstats_mib {
47 /* mibs[] must be first field of struct ipstats_mib */
48 u64 mibs[IPSTATS_MIB_MAX];
49 struct u64_stats_sync syncp;
50};
51
52/* ICMP */
53#define ICMP_MIB_MAX __ICMP_MIB_MAX
54struct icmp_mib {
55 unsigned long mibs[ICMP_MIB_MAX];
56};
57
58#define ICMPMSG_MIB_MAX __ICMPMSG_MIB_MAX
59struct icmpmsg_mib {
60 atomic_long_t mibs[ICMPMSG_MIB_MAX];
61};
62
63/* ICMP6 (IPv6-ICMP) */
64#define ICMP6_MIB_MAX __ICMP6_MIB_MAX
65/* per network ns counters */
66struct icmpv6_mib {
67 unsigned long mibs[ICMP6_MIB_MAX];
68};
69/* per device counters, (shared on all cpus) */
70struct icmpv6_mib_device {
71 atomic_long_t mibs[ICMP6_MIB_MAX];
72};
73
74#define ICMP6MSG_MIB_MAX __ICMP6MSG_MIB_MAX
75/* per network ns counters */
76struct icmpv6msg_mib {
77 atomic_long_t mibs[ICMP6MSG_MIB_MAX];
78};
79/* per device counters, (shared on all cpus) */
80struct icmpv6msg_mib_device {
81 atomic_long_t mibs[ICMP6MSG_MIB_MAX];
82};
83
84
85/* TCP */
86#define TCP_MIB_MAX __TCP_MIB_MAX
87struct tcp_mib {
88 unsigned long mibs[TCP_MIB_MAX];
89};
90
91/* UDP */
92#define UDP_MIB_MAX __UDP_MIB_MAX
93struct udp_mib {
94 unsigned long mibs[UDP_MIB_MAX];
95};
96
97/* Linux */
98#define LINUX_MIB_MAX __LINUX_MIB_MAX
99struct linux_mib {
100 unsigned long mibs[LINUX_MIB_MAX];
101};
102
103/* Linux Xfrm */
104#define LINUX_MIB_XFRMMAX __LINUX_MIB_XFRMMAX
105struct linux_xfrm_mib {
106 unsigned long mibs[LINUX_MIB_XFRMMAX];
107};
108
109/* Linux TLS */
110#define LINUX_MIB_TLSMAX __LINUX_MIB_TLSMAX
111struct linux_tls_mib {
112 unsigned long mibs[LINUX_MIB_TLSMAX];
113};
114
115#define DEFINE_SNMP_STAT(type, name) \
116 __typeof__(type) __percpu *name
117#define DEFINE_SNMP_STAT_ATOMIC(type, name) \
118 __typeof__(type) *name
119#define DECLARE_SNMP_STAT(type, name) \
120 extern __typeof__(type) __percpu *name
121
122#define __SNMP_INC_STATS(mib, field) \
123 __this_cpu_inc(mib->mibs[field])
124
125#define SNMP_INC_STATS_ATOMIC_LONG(mib, field) \
126 atomic_long_inc(&mib->mibs[field])
127
128#define SNMP_INC_STATS(mib, field) \
129 this_cpu_inc(mib->mibs[field])
130
131#define SNMP_DEC_STATS(mib, field) \
132 this_cpu_dec(mib->mibs[field])
133
134#define __SNMP_ADD_STATS(mib, field, addend) \
135 __this_cpu_add(mib->mibs[field], addend)
136
137#define SNMP_ADD_STATS(mib, field, addend) \
138 this_cpu_add(mib->mibs[field], addend)
139#define SNMP_UPD_PO_STATS(mib, basefield, addend) \
140 do { \
141 __typeof__((mib->mibs) + 0) ptr = mib->mibs; \
142 this_cpu_inc(ptr[basefield##PKTS]); \
143 this_cpu_add(ptr[basefield##OCTETS], addend); \
144 } while (0)
145#define __SNMP_UPD_PO_STATS(mib, basefield, addend) \
146 do { \
147 __typeof__((mib->mibs) + 0) ptr = mib->mibs; \
148 __this_cpu_inc(ptr[basefield##PKTS]); \
149 __this_cpu_add(ptr[basefield##OCTETS], addend); \
150 } while (0)
151
152
153#if BITS_PER_LONG==32
154
155#define __SNMP_ADD_STATS64(mib, field, addend) \
156 do { \
157 TYPEOF_UNQUAL(*mib) *ptr = raw_cpu_ptr(mib); \
158 u64_stats_update_begin(&ptr->syncp); \
159 ptr->mibs[field] += addend; \
160 u64_stats_update_end(&ptr->syncp); \
161 } while (0)
162
163#define SNMP_ADD_STATS64(mib, field, addend) \
164 do { \
165 local_bh_disable(); \
166 __SNMP_ADD_STATS64(mib, field, addend); \
167 local_bh_enable(); \
168 } while (0)
169
170#define __SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
171#define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
172#define __SNMP_UPD_PO_STATS64(mib, basefield, addend) \
173 do { \
174 TYPEOF_UNQUAL(*mib) *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