Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #ifndef _SNMP_H |
| 15 | #define _SNMP_H |
| 16 | |
| 17 | #include <linux/cache.h> |
| 18 | #include <linux/snmp.h> |
Herbert Xu | d647b36 | 2007-12-20 04:13:21 -0800 | [diff] [blame] | 19 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Alexey Dobriyan | 5833929 | 2010-01-22 10:17:26 +0000 | [diff] [blame] | 30 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 45 | * We use unsigned longs for most mibs but u64 for ipstats. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | */ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 47 | #include <linux/u64_stats_sync.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* IPstats */ |
| 50 | #define IPSTATS_MIB_MAX __IPSTATS_MIB_MAX |
| 51 | struct ipstats_mib { |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 52 | /* mibs[] must be first field of struct ipstats_mib */ |
| 53 | u64 mibs[IPSTATS_MIB_MAX]; |
| 54 | struct u64_stats_sync syncp; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 55 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* ICMP */ |
Shan Wei | a9527a3 | 2010-12-01 18:04:43 +0000 | [diff] [blame] | 58 | #define ICMP_MIB_MAX __ICMP_MIB_MAX |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct icmp_mib { |
| 60 | unsigned long mibs[ICMP_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 61 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
David L Stevens | 96793b4 | 2007-09-17 09:57:33 -0700 | [diff] [blame] | 63 | #define ICMPMSG_MIB_MAX __ICMPMSG_MIB_MAX |
| 64 | struct icmpmsg_mib { |
Eric Dumazet | acb32ba | 2011-11-08 13:04:43 +0000 | [diff] [blame] | 65 | atomic_long_t mibs[ICMPMSG_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 66 | }; |
David L Stevens | 96793b4 | 2007-09-17 09:57:33 -0700 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | /* ICMP6 (IPv6-ICMP) */ |
| 69 | #define ICMP6_MIB_MAX __ICMP6_MIB_MAX |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 70 | /* per network ns counters */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | struct icmpv6_mib { |
| 72 | unsigned long mibs[ICMP6_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 73 | }; |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 74 | /* per device counters, (shared on all cpus) */ |
| 75 | struct icmpv6_mib_device { |
| 76 | atomic_long_t mibs[ICMP6_MIB_MAX]; |
| 77 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 79 | #define ICMP6MSG_MIB_MAX __ICMP6MSG_MIB_MAX |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 80 | /* per network ns counters */ |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 81 | struct icmpv6msg_mib { |
Eric Dumazet | 2a24444 | 2011-11-13 01:24:04 +0000 | [diff] [blame] | 82 | atomic_long_t mibs[ICMP6MSG_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 83 | }; |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 84 | /* per device counters, (shared on all cpus) */ |
| 85 | struct icmpv6msg_mib_device { |
| 86 | atomic_long_t mibs[ICMP6MSG_MIB_MAX]; |
| 87 | }; |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 88 | |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* TCP */ |
| 91 | #define TCP_MIB_MAX __TCP_MIB_MAX |
| 92 | struct tcp_mib { |
| 93 | unsigned long mibs[TCP_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 94 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* UDP */ |
| 97 | #define UDP_MIB_MAX __UDP_MIB_MAX |
| 98 | struct udp_mib { |
| 99 | unsigned long mibs[UDP_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 100 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /* Linux */ |
| 103 | #define LINUX_MIB_MAX __LINUX_MIB_MAX |
| 104 | struct linux_mib { |
| 105 | unsigned long mibs[LINUX_MIB_MAX]; |
| 106 | }; |
| 107 | |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 108 | /* Linux Xfrm */ |
| 109 | #define LINUX_MIB_XFRMMAX __LINUX_MIB_XFRMMAX |
| 110 | struct linux_xfrm_mib { |
| 111 | unsigned long mibs[LINUX_MIB_XFRMMAX]; |
| 112 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | #define DEFINE_SNMP_STAT(type, name) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 115 | __typeof__(type) __percpu *name |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 116 | #define DEFINE_SNMP_STAT_ATOMIC(type, name) \ |
| 117 | __typeof__(type) *name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | #define DECLARE_SNMP_STAT(type, name) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 119 | extern __typeof__(type) __percpu *name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 121 | #define __SNMP_INC_STATS(mib, field) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 122 | __this_cpu_inc(mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 123 | |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 124 | #define SNMP_INC_STATS_ATOMIC_LONG(mib, field) \ |
| 125 | atomic_long_inc(&mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 126 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 127 | #define SNMP_INC_STATS(mib, field) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 128 | this_cpu_inc(mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 129 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 130 | #define SNMP_DEC_STATS(mib, field) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 131 | this_cpu_dec(mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 132 | |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 133 | #define __SNMP_ADD_STATS(mib, field, addend) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 134 | __this_cpu_add(mib->mibs[field], addend) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 135 | |
Tom Herbert | aa2ea05 | 2010-04-22 07:00:24 +0000 | [diff] [blame] | 136 | #define SNMP_ADD_STATS(mib, field, addend) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 137 | this_cpu_add(mib->mibs[field], addend) |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 138 | #define SNMP_UPD_PO_STATS(mib, basefield, addend) \ |
| 139 | do { \ |
Sabrina Dubroca | 54003f1 | 2014-09-17 23:23:12 +0200 | [diff] [blame] | 140 | __typeof__((mib->mibs) + 0) ptr = mib->mibs; \ |
Eric Dumazet | d25398d | 2012-08-04 20:26:13 +0000 | [diff] [blame] | 141 | this_cpu_inc(ptr[basefield##PKTS]); \ |
| 142 | this_cpu_add(ptr[basefield##OCTETS], addend); \ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 143 | } while (0) |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 144 | #define __SNMP_UPD_PO_STATS(mib, basefield, addend) \ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 145 | do { \ |
Sabrina Dubroca | 54003f1 | 2014-09-17 23:23:12 +0200 | [diff] [blame] | 146 | __typeof__((mib->mibs) + 0) ptr = mib->mibs; \ |
Eric Dumazet | d25398d | 2012-08-04 20:26:13 +0000 | [diff] [blame] | 147 | __this_cpu_inc(ptr[basefield##PKTS]); \ |
| 148 | __this_cpu_add(ptr[basefield##OCTETS], addend); \ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 149 | } while (0) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 150 | |
| 151 | |
| 152 | #if BITS_PER_LONG==32 |
| 153 | |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 154 | #define __SNMP_ADD_STATS64(mib, field, addend) \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 155 | do { \ |
Christoph Lameter | 903ceff | 2014-08-17 12:30:35 -0500 | [diff] [blame] | 156 | __typeof__(*mib) *ptr = raw_cpu_ptr(mib); \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 157 | u64_stats_update_begin(&ptr->syncp); \ |
| 158 | ptr->mibs[field] += addend; \ |
| 159 | u64_stats_update_end(&ptr->syncp); \ |
| 160 | } while (0) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 161 | |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 162 | #define SNMP_ADD_STATS64(mib, field, addend) \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 163 | do { \ |
Eric Dumazet | ba7863f | 2016-04-28 06:33:24 -0700 | [diff] [blame] | 164 | local_bh_disable(); \ |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 165 | __SNMP_ADD_STATS64(mib, field, addend); \ |
Eric Dumazet | ba7863f | 2016-04-28 06:33:24 -0700 | [diff] [blame] | 166 | local_bh_enable(); \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 167 | } while (0) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 168 | |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 169 | #define __SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 170 | #define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1) |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 171 | #define __SNMP_UPD_PO_STATS64(mib, basefield, addend) \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 172 | do { \ |
Christoph Lameter | 903ceff | 2014-08-17 12:30:35 -0500 | [diff] [blame] | 173 | __typeof__(*mib) *ptr; \ |
| 174 | ptr = raw_cpu_ptr((mib)); \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 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) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 180 | #define SNMP_UPD_PO_STATS64(mib, basefield, addend) \ |
| 181 | do { \ |
Eric Dumazet | ba7863f | 2016-04-28 06:33:24 -0700 | [diff] [blame] | 182 | local_bh_disable(); \ |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 183 | __SNMP_UPD_PO_STATS64(mib, basefield, addend); \ |
Eric Dumazet | ba7863f | 2016-04-28 06:33:24 -0700 | [diff] [blame] | 184 | local_bh_enable(); \ |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 185 | } while (0) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 186 | #else |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 187 | #define __SNMP_INC_STATS64(mib, field) __SNMP_INC_STATS(mib, field) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 188 | #define SNMP_INC_STATS64(mib, field) SNMP_INC_STATS(mib, field) |
| 189 | #define SNMP_DEC_STATS64(mib, field) SNMP_DEC_STATS(mib, field) |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 190 | #define __SNMP_ADD_STATS64(mib, field, addend) __SNMP_ADD_STATS(mib, field, addend) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 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) |
Eric Dumazet | 13415e4 | 2016-04-27 16:44:43 -0700 | [diff] [blame] | 193 | #define __SNMP_UPD_PO_STATS64(mib, basefield, addend) __SNMP_UPD_PO_STATS(mib, basefield, addend) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 194 | #endif |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | #endif |