Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * SNMP MIB entries for the IP subsystem. |
| 4 | * |
| 5 | * Alan Cox <gw4pts@gw4pts.ampr.org> |
| 6 | * |
| 7 | * We don't chose to implement SNMP in the kernel (this would |
| 8 | * be silly as SNMP is a pain in the backside in places). We do |
| 9 | * however need to collect the MIB statistics and export them |
| 10 | * out of /proc (eventually) |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #ifndef _SNMP_H |
| 20 | #define _SNMP_H |
| 21 | |
| 22 | #include <linux/cache.h> |
| 23 | #include <linux/snmp.h> |
Herbert Xu | d647b36 | 2007-12-20 04:13:21 -0800 | [diff] [blame] | 24 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Mibs are stored in array of unsigned long. |
| 28 | */ |
| 29 | /* |
| 30 | * struct snmp_mib{} |
| 31 | * - list of entries for particular API (such as /proc/net/snmp) |
| 32 | * - name of entries. |
| 33 | */ |
| 34 | struct snmp_mib { |
Alexey Dobriyan | 5833929 | 2010-01-22 10:17:26 +0000 | [diff] [blame] | 35 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | int entry; |
| 37 | }; |
| 38 | |
| 39 | #define SNMP_MIB_ITEM(_name,_entry) { \ |
| 40 | .name = _name, \ |
| 41 | .entry = _entry, \ |
| 42 | } |
| 43 | |
| 44 | #define SNMP_MIB_SENTINEL { \ |
| 45 | .name = NULL, \ |
| 46 | .entry = 0, \ |
| 47 | } |
| 48 | |
| 49 | /* |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 50 | * We use unsigned longs for most mibs but u64 for ipstats. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | */ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 52 | #include <linux/u64_stats_sync.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* IPstats */ |
| 55 | #define IPSTATS_MIB_MAX __IPSTATS_MIB_MAX |
| 56 | struct ipstats_mib { |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 57 | /* mibs[] must be first field of struct ipstats_mib */ |
| 58 | u64 mibs[IPSTATS_MIB_MAX]; |
| 59 | struct u64_stats_sync syncp; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 60 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* ICMP */ |
Shan Wei | a9527a3 | 2010-12-01 18:04:43 +0000 | [diff] [blame] | 63 | #define ICMP_MIB_MAX __ICMP_MIB_MAX |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | struct icmp_mib { |
| 65 | unsigned long mibs[ICMP_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 66 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
David L Stevens | 96793b4 | 2007-09-17 09:57:33 -0700 | [diff] [blame] | 68 | #define ICMPMSG_MIB_MAX __ICMPMSG_MIB_MAX |
| 69 | struct icmpmsg_mib { |
Eric Dumazet | acb32ba | 2011-11-08 13:04:43 +0000 | [diff] [blame] | 70 | atomic_long_t mibs[ICMPMSG_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 71 | }; |
David L Stevens | 96793b4 | 2007-09-17 09:57:33 -0700 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* ICMP6 (IPv6-ICMP) */ |
| 74 | #define ICMP6_MIB_MAX __ICMP6_MIB_MAX |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 75 | /* per network ns counters */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | struct icmpv6_mib { |
| 77 | unsigned long mibs[ICMP6_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 78 | }; |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 79 | /* per device counters, (shared on all cpus) */ |
| 80 | struct icmpv6_mib_device { |
| 81 | atomic_long_t mibs[ICMP6_MIB_MAX]; |
| 82 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 84 | #define ICMP6MSG_MIB_MAX __ICMP6MSG_MIB_MAX |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 85 | /* per network ns counters */ |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 86 | struct icmpv6msg_mib { |
Eric Dumazet | 2a24444 | 2011-11-13 01:24:04 +0000 | [diff] [blame] | 87 | atomic_long_t mibs[ICMP6MSG_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 88 | }; |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 89 | /* per device counters, (shared on all cpus) */ |
| 90 | struct icmpv6msg_mib_device { |
| 91 | atomic_long_t mibs[ICMP6MSG_MIB_MAX]; |
| 92 | }; |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 93 | |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /* TCP */ |
| 96 | #define TCP_MIB_MAX __TCP_MIB_MAX |
| 97 | struct tcp_mib { |
| 98 | unsigned long mibs[TCP_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 99 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | /* UDP */ |
| 102 | #define UDP_MIB_MAX __UDP_MIB_MAX |
| 103 | struct udp_mib { |
| 104 | unsigned long mibs[UDP_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 105 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* Linux */ |
| 108 | #define LINUX_MIB_MAX __LINUX_MIB_MAX |
| 109 | struct linux_mib { |
| 110 | unsigned long mibs[LINUX_MIB_MAX]; |
| 111 | }; |
| 112 | |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 113 | /* Linux Xfrm */ |
| 114 | #define LINUX_MIB_XFRMMAX __LINUX_MIB_XFRMMAX |
| 115 | struct linux_xfrm_mib { |
| 116 | unsigned long mibs[LINUX_MIB_XFRMMAX]; |
| 117 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | #define DEFINE_SNMP_STAT(type, name) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 120 | __typeof__(type) __percpu *name |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 121 | #define DEFINE_SNMP_STAT_ATOMIC(type, name) \ |
| 122 | __typeof__(type) *name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | #define DECLARE_SNMP_STAT(type, name) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 124 | extern __typeof__(type) __percpu *name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 126 | #define SNMP_INC_STATS_BH(mib, field) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 127 | __this_cpu_inc(mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 128 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 129 | #define SNMP_INC_STATS_USER(mib, field) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 130 | this_cpu_inc(mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 131 | |
Eric Dumazet | be281e5 | 2011-05-19 01:14:23 +0000 | [diff] [blame] | 132 | #define SNMP_INC_STATS_ATOMIC_LONG(mib, field) \ |
| 133 | atomic_long_inc(&mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 134 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 135 | #define SNMP_INC_STATS(mib, field) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 136 | this_cpu_inc(mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 137 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 138 | #define SNMP_DEC_STATS(mib, field) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 139 | this_cpu_dec(mib->mibs[field]) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 140 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 141 | #define SNMP_ADD_STATS_BH(mib, field, addend) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 142 | __this_cpu_add(mib->mibs[field], addend) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 143 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 144 | #define SNMP_ADD_STATS_USER(mib, field, addend) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 145 | this_cpu_add(mib->mibs[field], addend) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 146 | |
Tom Herbert | aa2ea05 | 2010-04-22 07:00:24 +0000 | [diff] [blame] | 147 | #define SNMP_ADD_STATS(mib, field, addend) \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 148 | this_cpu_add(mib->mibs[field], addend) |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 149 | /* |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 150 | * Use "__typeof__(*mib) *ptr" instead of "__typeof__(mib) ptr" |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 151 | * to make @ptr a non-percpu pointer. |
| 152 | */ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 153 | #define SNMP_UPD_PO_STATS(mib, basefield, addend) \ |
| 154 | do { \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 155 | __typeof__(*mib->mibs) *ptr = mib->mibs; \ |
Eric Dumazet | d25398d | 2012-08-04 20:26:13 +0000 | [diff] [blame] | 156 | this_cpu_inc(ptr[basefield##PKTS]); \ |
| 157 | this_cpu_add(ptr[basefield##OCTETS], addend); \ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 158 | } while (0) |
| 159 | #define SNMP_UPD_PO_STATS_BH(mib, basefield, addend) \ |
| 160 | do { \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 161 | __typeof__(*mib->mibs) *ptr = mib->mibs; \ |
Eric Dumazet | d25398d | 2012-08-04 20:26:13 +0000 | [diff] [blame] | 162 | __this_cpu_inc(ptr[basefield##PKTS]); \ |
| 163 | __this_cpu_add(ptr[basefield##OCTETS], addend); \ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 164 | } while (0) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 165 | |
| 166 | |
| 167 | #if BITS_PER_LONG==32 |
| 168 | |
| 169 | #define SNMP_ADD_STATS64_BH(mib, field, addend) \ |
| 170 | do { \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 171 | __typeof__(*mib) *ptr = __this_cpu_ptr(mib); \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 172 | u64_stats_update_begin(&ptr->syncp); \ |
| 173 | ptr->mibs[field] += addend; \ |
| 174 | u64_stats_update_end(&ptr->syncp); \ |
| 175 | } while (0) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 176 | |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 177 | #define SNMP_ADD_STATS64_USER(mib, field, addend) \ |
| 178 | do { \ |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 179 | local_bh_disable(); \ |
| 180 | SNMP_ADD_STATS64_BH(mib, field, addend); \ |
| 181 | local_bh_enable(); \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 182 | } while (0) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 183 | |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 184 | #define SNMP_ADD_STATS64(mib, field, addend) \ |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 185 | SNMP_ADD_STATS64_USER(mib, field, addend) |
| 186 | |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 187 | #define SNMP_INC_STATS64_BH(mib, field) SNMP_ADD_STATS64_BH(mib, field, 1) |
| 188 | #define SNMP_INC_STATS64_USER(mib, field) SNMP_ADD_STATS64_USER(mib, field, 1) |
| 189 | #define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 190 | #define SNMP_UPD_PO_STATS64_BH(mib, basefield, addend) \ |
| 191 | do { \ |
WANG Cong | 698365f | 2014-05-05 15:55:55 -0700 | [diff] [blame] | 192 | __typeof__(*mib) *ptr; \ |
| 193 | ptr = __this_cpu_ptr(mib); \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 194 | u64_stats_update_begin(&ptr->syncp); \ |
| 195 | ptr->mibs[basefield##PKTS]++; \ |
| 196 | ptr->mibs[basefield##OCTETS] += addend; \ |
| 197 | u64_stats_update_end(&ptr->syncp); \ |
| 198 | } while (0) |
Eric Dumazet | 8f0ea0f | 2011-06-10 19:45:51 +0000 | [diff] [blame] | 199 | #define SNMP_UPD_PO_STATS64(mib, basefield, addend) \ |
| 200 | do { \ |
| 201 | local_bh_disable(); \ |
| 202 | SNMP_UPD_PO_STATS64_BH(mib, basefield, addend); \ |
| 203 | local_bh_enable(); \ |
| 204 | } while (0) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 205 | #else |
| 206 | #define SNMP_INC_STATS64_BH(mib, field) SNMP_INC_STATS_BH(mib, field) |
| 207 | #define SNMP_INC_STATS64_USER(mib, field) SNMP_INC_STATS_USER(mib, field) |
| 208 | #define SNMP_INC_STATS64(mib, field) SNMP_INC_STATS(mib, field) |
| 209 | #define SNMP_DEC_STATS64(mib, field) SNMP_DEC_STATS(mib, field) |
| 210 | #define SNMP_ADD_STATS64_BH(mib, field, addend) SNMP_ADD_STATS_BH(mib, field, addend) |
| 211 | #define SNMP_ADD_STATS64_USER(mib, field, addend) SNMP_ADD_STATS_USER(mib, field, addend) |
| 212 | #define SNMP_ADD_STATS64(mib, field, addend) SNMP_ADD_STATS(mib, field, addend) |
| 213 | #define SNMP_UPD_PO_STATS64(mib, basefield, addend) SNMP_UPD_PO_STATS(mib, basefield, addend) |
| 214 | #define SNMP_UPD_PO_STATS64_BH(mib, basefield, addend) SNMP_UPD_PO_STATS_BH(mib, basefield, addend) |
| 215 | #endif |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | #endif |