blob: 468a67836e2f1fcf744b845784312175fab63668 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#ifndef _SNMP_H
15#define _SNMP_H
16
17#include <linux/cache.h>
18#include <linux/snmp.h>
Herbert Xud647b362007-12-20 04:13:21 -080019#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
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 {
Alexey Dobriyan58339292010-01-22 10:17:26 +000030 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 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/*
Eric Dumazet4ce3c182010-06-30 13:31:19 -070045 * We use unsigned longs for most mibs but u64 for ipstats.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Eric Dumazet4ce3c182010-06-30 13:31:19 -070047#include <linux/u64_stats_sync.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* IPstats */
50#define IPSTATS_MIB_MAX __IPSTATS_MIB_MAX
51struct ipstats_mib {
Eric Dumazet4ce3c182010-06-30 13:31:19 -070052 /* mibs[] must be first field of struct ipstats_mib */
53 u64 mibs[IPSTATS_MIB_MAX];
54 struct u64_stats_sync syncp;
Eric Dumazetec733b152010-03-18 20:36:06 +000055};
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* ICMP */
Shan Weia9527a32010-12-01 18:04:43 +000058#define ICMP_MIB_MAX __ICMP_MIB_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct icmp_mib {
60 unsigned long mibs[ICMP_MIB_MAX];
Eric Dumazetec733b152010-03-18 20:36:06 +000061};
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
David L Stevens96793b42007-09-17 09:57:33 -070063#define ICMPMSG_MIB_MAX __ICMPMSG_MIB_MAX
64struct icmpmsg_mib {
Eric Dumazetacb32ba2011-11-08 13:04:43 +000065 atomic_long_t mibs[ICMPMSG_MIB_MAX];
Eric Dumazetec733b152010-03-18 20:36:06 +000066};
David L Stevens96793b42007-09-17 09:57:33 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* ICMP6 (IPv6-ICMP) */
69#define ICMP6_MIB_MAX __ICMP6_MIB_MAX
Eric Dumazetbe281e52011-05-19 01:14:23 +000070/* per network ns counters */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct icmpv6_mib {
72 unsigned long mibs[ICMP6_MIB_MAX];
Eric Dumazetec733b152010-03-18 20:36:06 +000073};
Eric Dumazetbe281e52011-05-19 01:14:23 +000074/* per device counters, (shared on all cpus) */
75struct icmpv6_mib_device {
76 atomic_long_t mibs[ICMP6_MIB_MAX];
77};
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David L Stevens14878f72007-09-16 16:52:35 -070079#define ICMP6MSG_MIB_MAX __ICMP6MSG_MIB_MAX
Eric Dumazetbe281e52011-05-19 01:14:23 +000080/* per network ns counters */
David L Stevens14878f72007-09-16 16:52:35 -070081struct icmpv6msg_mib {
Eric Dumazet2a244442011-11-13 01:24:04 +000082 atomic_long_t mibs[ICMP6MSG_MIB_MAX];
Eric Dumazetec733b152010-03-18 20:36:06 +000083};
Eric Dumazetbe281e52011-05-19 01:14:23 +000084/* per device counters, (shared on all cpus) */
85struct icmpv6msg_mib_device {
86 atomic_long_t mibs[ICMP6MSG_MIB_MAX];
87};
David L Stevens14878f72007-09-16 16:52:35 -070088
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* TCP */
91#define TCP_MIB_MAX __TCP_MIB_MAX
92struct tcp_mib {
93 unsigned long mibs[TCP_MIB_MAX];
Eric Dumazetec733b152010-03-18 20:36:06 +000094};
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* UDP */
97#define UDP_MIB_MAX __UDP_MIB_MAX
98struct udp_mib {
99 unsigned long mibs[UDP_MIB_MAX];
Eric Dumazetec733b152010-03-18 20:36:06 +0000100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* Linux */
103#define LINUX_MIB_MAX __LINUX_MIB_MAX
104struct linux_mib {
105 unsigned long mibs[LINUX_MIB_MAX];
106};
107
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -0800108/* Linux Xfrm */
109#define LINUX_MIB_XFRMMAX __LINUX_MIB_XFRMMAX
110struct linux_xfrm_mib {
111 unsigned long mibs[LINUX_MIB_XFRMMAX];
112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jakub Kicinskid26b6982019-10-04 16:19:24 -0700114/* Linux TLS */
115#define LINUX_MIB_TLSMAX __LINUX_MIB_TLSMAX
116struct linux_tls_mib {
117 unsigned long mibs[LINUX_MIB_TLSMAX];
118};
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define DEFINE_SNMP_STAT(type, name) \
WANG Cong698365f2014-05-05 15:55:55 -0700121 __typeof__(type) __percpu *name
Eric Dumazetbe281e52011-05-19 01:14:23 +0000122#define DEFINE_SNMP_STAT_ATOMIC(type, name) \
123 __typeof__(type) *name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define DECLARE_SNMP_STAT(type, name) \
WANG Cong698365f2014-05-05 15:55:55 -0700125 extern __typeof__(type) __percpu *name
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Eric Dumazet13415e42016-04-27 16:44:43 -0700127#define __SNMP_INC_STATS(mib, field) \
WANG Cong698365f2014-05-05 15:55:55 -0700128 __this_cpu_inc(mib->mibs[field])
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000129
Eric Dumazetbe281e52011-05-19 01:14:23 +0000130#define SNMP_INC_STATS_ATOMIC_LONG(mib, field) \
131 atomic_long_inc(&mib->mibs[field])
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000132
Christoph Lameter4eb41d12009-10-03 19:48:22 +0900133#define SNMP_INC_STATS(mib, field) \
WANG Cong698365f2014-05-05 15:55:55 -0700134 this_cpu_inc(mib->mibs[field])
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000135
Christoph Lameter4eb41d12009-10-03 19:48:22 +0900136#define SNMP_DEC_STATS(mib, field) \
WANG Cong698365f2014-05-05 15:55:55 -0700137 this_cpu_dec(mib->mibs[field])
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000138
Eric Dumazet13415e42016-04-27 16:44:43 -0700139#define __SNMP_ADD_STATS(mib, field, addend) \
WANG Cong698365f2014-05-05 15:55:55 -0700140 __this_cpu_add(mib->mibs[field], addend)
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000141
Tom Herbertaa2ea052010-04-22 07:00:24 +0000142#define SNMP_ADD_STATS(mib, field, addend) \
WANG Cong698365f2014-05-05 15:55:55 -0700143 this_cpu_add(mib->mibs[field], addend)
Neil Hormanedf391f2009-04-27 02:45:02 -0700144#define SNMP_UPD_PO_STATS(mib, basefield, addend) \
145 do { \
Sabrina Dubroca54003f12014-09-17 23:23:12 +0200146 __typeof__((mib->mibs) + 0) ptr = mib->mibs; \
Eric Dumazetd25398d2012-08-04 20:26:13 +0000147 this_cpu_inc(ptr[basefield##PKTS]); \
148 this_cpu_add(ptr[basefield##OCTETS], addend); \
Neil Hormanedf391f2009-04-27 02:45:02 -0700149 } while (0)
Eric Dumazet13415e42016-04-27 16:44:43 -0700150#define __SNMP_UPD_PO_STATS(mib, basefield, addend) \
Neil Hormanedf391f2009-04-27 02:45:02 -0700151 do { \
Sabrina Dubroca54003f12014-09-17 23:23:12 +0200152 __typeof__((mib->mibs) + 0) ptr = mib->mibs; \
Eric Dumazetd25398d2012-08-04 20:26:13 +0000153 __this_cpu_inc(ptr[basefield##PKTS]); \
154 __this_cpu_add(ptr[basefield##OCTETS], addend); \
Neil Hormanedf391f2009-04-27 02:45:02 -0700155 } while (0)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700156
157
158#if BITS_PER_LONG==32
159
Eric Dumazet13415e42016-04-27 16:44:43 -0700160#define __SNMP_ADD_STATS64(mib, field, addend) \
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700161 do { \
Christoph Lameter903ceff2014-08-17 12:30:35 -0500162 __typeof__(*mib) *ptr = raw_cpu_ptr(mib); \
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700163 u64_stats_update_begin(&ptr->syncp); \
164 ptr->mibs[field] += addend; \
165 u64_stats_update_end(&ptr->syncp); \
166 } while (0)
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000167
Eric Dumazet13415e42016-04-27 16:44:43 -0700168#define SNMP_ADD_STATS64(mib, field, addend) \
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700169 do { \
Eric Dumazetba7863f2016-04-28 06:33:24 -0700170 local_bh_disable(); \
Eric Dumazet13415e42016-04-27 16:44:43 -0700171 __SNMP_ADD_STATS64(mib, field, addend); \
Eric Dumazetba7863f2016-04-28 06:33:24 -0700172 local_bh_enable(); \
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700173 } while (0)
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000174
Eric Dumazet13415e42016-04-27 16:44:43 -0700175#define __SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700176#define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
Eric Dumazet13415e42016-04-27 16:44:43 -0700177#define __SNMP_UPD_PO_STATS64(mib, basefield, addend) \
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700178 do { \
Christoph Lameter903ceff2014-08-17 12:30:35 -0500179 __typeof__(*mib) *ptr; \
180 ptr = raw_cpu_ptr((mib)); \
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700181 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)
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000186#define SNMP_UPD_PO_STATS64(mib, basefield, addend) \
187 do { \
Eric Dumazetba7863f2016-04-28 06:33:24 -0700188 local_bh_disable(); \
Eric Dumazet13415e42016-04-27 16:44:43 -0700189 __SNMP_UPD_PO_STATS64(mib, basefield, addend); \
Eric Dumazetba7863f2016-04-28 06:33:24 -0700190 local_bh_enable(); \
Eric Dumazet8f0ea0f2011-06-10 19:45:51 +0000191 } while (0)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700192#else
Eric Dumazet13415e42016-04-27 16:44:43 -0700193#define __SNMP_INC_STATS64(mib, field) __SNMP_INC_STATS(mib, field)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700194#define SNMP_INC_STATS64(mib, field) SNMP_INC_STATS(mib, field)
195#define SNMP_DEC_STATS64(mib, field) SNMP_DEC_STATS(mib, field)
Eric Dumazet13415e42016-04-27 16:44:43 -0700196#define __SNMP_ADD_STATS64(mib, field, addend) __SNMP_ADD_STATS(mib, field, addend)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700197#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)
Eric Dumazet13415e42016-04-27 16:44:43 -0700199#define __SNMP_UPD_PO_STATS64(mib, basefield, addend) __SNMP_UPD_PO_STATS(mib, basefield, addend)
Eric Dumazet4ce3c182010-06-30 13:31:19 -0700200#endif
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#endif