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 { |
| 70 | unsigned long 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 |
| 75 | struct icmpv6_mib { |
| 76 | unsigned long mibs[ICMP6_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 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 |
| 80 | struct icmpv6msg_mib { |
| 81 | unsigned long mibs[ICMP6MSG_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 82 | }; |
David L Stevens | 14878f7 | 2007-09-16 16:52:35 -0700 | [diff] [blame] | 83 | |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /* TCP */ |
| 86 | #define TCP_MIB_MAX __TCP_MIB_MAX |
| 87 | struct tcp_mib { |
| 88 | unsigned long mibs[TCP_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 89 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | /* UDP */ |
| 92 | #define UDP_MIB_MAX __UDP_MIB_MAX |
| 93 | struct udp_mib { |
| 94 | unsigned long mibs[UDP_MIB_MAX]; |
Eric Dumazet | ec733b15 | 2010-03-18 20:36:06 +0000 | [diff] [blame] | 95 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* Linux */ |
| 98 | #define LINUX_MIB_MAX __LINUX_MIB_MAX |
| 99 | struct linux_mib { |
| 100 | unsigned long mibs[LINUX_MIB_MAX]; |
| 101 | }; |
| 102 | |
Masahide NAKAMURA | 558f82e | 2007-12-20 20:42:57 -0800 | [diff] [blame] | 103 | /* Linux Xfrm */ |
| 104 | #define LINUX_MIB_XFRMMAX __LINUX_MIB_XFRMMAX |
| 105 | struct linux_xfrm_mib { |
| 106 | unsigned long mibs[LINUX_MIB_XFRMMAX]; |
| 107 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * FIXME: On x86 and some other CPUs the split into user and softirq parts |
| 111 | * is not needed because addl $1,memory is atomic against interrupts (but |
| 112 | * atomic_inc would be overkill because of the lock cycles). Wants new |
| 113 | * nonlocked_atomic_inc() primitives -AK |
| 114 | */ |
| 115 | #define DEFINE_SNMP_STAT(type, name) \ |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 116 | __typeof__(type) __percpu *name[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | #define DECLARE_SNMP_STAT(type, name) \ |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 118 | extern __typeof__(type) __percpu *name[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | #define SNMP_STAT_BHPTR(name) (name[0]) |
| 121 | #define SNMP_STAT_USRPTR(name) (name[1]) |
| 122 | |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 123 | #define SNMP_INC_STATS_BH(mib, field) \ |
| 124 | __this_cpu_inc(mib[0]->mibs[field]) |
| 125 | #define SNMP_INC_STATS_USER(mib, field) \ |
| 126 | this_cpu_inc(mib[1]->mibs[field]) |
| 127 | #define SNMP_INC_STATS(mib, field) \ |
| 128 | this_cpu_inc(mib[!in_softirq()]->mibs[field]) |
| 129 | #define SNMP_DEC_STATS(mib, field) \ |
| 130 | this_cpu_dec(mib[!in_softirq()]->mibs[field]) |
| 131 | #define SNMP_ADD_STATS_BH(mib, field, addend) \ |
| 132 | __this_cpu_add(mib[0]->mibs[field], addend) |
| 133 | #define SNMP_ADD_STATS_USER(mib, field, addend) \ |
| 134 | this_cpu_add(mib[1]->mibs[field], addend) |
Tom Herbert | aa2ea05 | 2010-04-22 07:00:24 +0000 | [diff] [blame] | 135 | #define SNMP_ADD_STATS(mib, field, addend) \ |
Eric Dumazet | 8f1c14b | 2010-06-23 00:32:03 +0000 | [diff] [blame] | 136 | this_cpu_add(mib[!in_softirq()]->mibs[field], addend) |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 137 | /* |
| 138 | * Use "__typeof__(*mib[0]) *ptr" instead of "__typeof__(mib[0]) ptr" |
| 139 | * to make @ptr a non-percpu pointer. |
| 140 | */ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 141 | #define SNMP_UPD_PO_STATS(mib, basefield, addend) \ |
| 142 | do { \ |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 143 | __typeof__(*mib[0]) *ptr; \ |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 144 | preempt_disable(); \ |
| 145 | ptr = this_cpu_ptr((mib)[!in_softirq()]); \ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 146 | ptr->mibs[basefield##PKTS]++; \ |
| 147 | ptr->mibs[basefield##OCTETS] += addend;\ |
Christoph Lameter | 4eb41d1 | 2009-10-03 19:48:22 +0900 | [diff] [blame] | 148 | preempt_enable(); \ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 149 | } while (0) |
| 150 | #define SNMP_UPD_PO_STATS_BH(mib, basefield, addend) \ |
| 151 | do { \ |
Tejun Heo | 7d720c3 | 2010-02-16 15:20:26 +0000 | [diff] [blame] | 152 | __typeof__(*mib[0]) *ptr = \ |
Eric Dumazet | 20246a8 | 2011-03-21 18:12:54 -0700 | [diff] [blame] | 153 | __this_cpu_ptr((mib)[0]); \ |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 154 | ptr->mibs[basefield##PKTS]++; \ |
| 155 | ptr->mibs[basefield##OCTETS] += addend;\ |
| 156 | } while (0) |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 157 | |
| 158 | |
| 159 | #if BITS_PER_LONG==32 |
| 160 | |
| 161 | #define SNMP_ADD_STATS64_BH(mib, field, addend) \ |
| 162 | do { \ |
| 163 | __typeof__(*mib[0]) *ptr = __this_cpu_ptr((mib)[0]); \ |
| 164 | u64_stats_update_begin(&ptr->syncp); \ |
| 165 | ptr->mibs[field] += addend; \ |
| 166 | u64_stats_update_end(&ptr->syncp); \ |
| 167 | } while (0) |
| 168 | #define SNMP_ADD_STATS64_USER(mib, field, addend) \ |
| 169 | do { \ |
| 170 | __typeof__(*mib[0]) *ptr; \ |
| 171 | preempt_disable(); \ |
| 172 | ptr = __this_cpu_ptr((mib)[1]); \ |
| 173 | u64_stats_update_begin(&ptr->syncp); \ |
| 174 | ptr->mibs[field] += addend; \ |
| 175 | u64_stats_update_end(&ptr->syncp); \ |
| 176 | preempt_enable(); \ |
| 177 | } while (0) |
| 178 | #define SNMP_ADD_STATS64(mib, field, addend) \ |
| 179 | do { \ |
| 180 | __typeof__(*mib[0]) *ptr; \ |
| 181 | preempt_disable(); \ |
| 182 | ptr = __this_cpu_ptr((mib)[!in_softirq()]); \ |
| 183 | u64_stats_update_begin(&ptr->syncp); \ |
| 184 | ptr->mibs[field] += addend; \ |
| 185 | u64_stats_update_end(&ptr->syncp); \ |
| 186 | preempt_enable(); \ |
| 187 | } while (0) |
| 188 | #define SNMP_INC_STATS64_BH(mib, field) SNMP_ADD_STATS64_BH(mib, field, 1) |
| 189 | #define SNMP_INC_STATS64_USER(mib, field) SNMP_ADD_STATS64_USER(mib, field, 1) |
| 190 | #define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1) |
| 191 | #define SNMP_UPD_PO_STATS64(mib, basefield, addend) \ |
| 192 | do { \ |
| 193 | __typeof__(*mib[0]) *ptr; \ |
| 194 | preempt_disable(); \ |
| 195 | ptr = __this_cpu_ptr((mib)[!in_softirq()]); \ |
| 196 | u64_stats_update_begin(&ptr->syncp); \ |
| 197 | ptr->mibs[basefield##PKTS]++; \ |
| 198 | ptr->mibs[basefield##OCTETS] += addend; \ |
| 199 | u64_stats_update_end(&ptr->syncp); \ |
| 200 | preempt_enable(); \ |
| 201 | } while (0) |
| 202 | #define SNMP_UPD_PO_STATS64_BH(mib, basefield, addend) \ |
| 203 | do { \ |
| 204 | __typeof__(*mib[0]) *ptr; \ |
Eric Dumazet | 20246a8 | 2011-03-21 18:12:54 -0700 | [diff] [blame] | 205 | ptr = __this_cpu_ptr((mib)[0]); \ |
Eric Dumazet | 4ce3c18 | 2010-06-30 13:31:19 -0700 | [diff] [blame] | 206 | u64_stats_update_begin(&ptr->syncp); \ |
| 207 | ptr->mibs[basefield##PKTS]++; \ |
| 208 | ptr->mibs[basefield##OCTETS] += addend; \ |
| 209 | u64_stats_update_end(&ptr->syncp); \ |
| 210 | } while (0) |
| 211 | #else |
| 212 | #define SNMP_INC_STATS64_BH(mib, field) SNMP_INC_STATS_BH(mib, field) |
| 213 | #define SNMP_INC_STATS64_USER(mib, field) SNMP_INC_STATS_USER(mib, field) |
| 214 | #define SNMP_INC_STATS64(mib, field) SNMP_INC_STATS(mib, field) |
| 215 | #define SNMP_DEC_STATS64(mib, field) SNMP_DEC_STATS(mib, field) |
| 216 | #define SNMP_ADD_STATS64_BH(mib, field, addend) SNMP_ADD_STATS_BH(mib, field, addend) |
| 217 | #define SNMP_ADD_STATS64_USER(mib, field, addend) SNMP_ADD_STATS_USER(mib, field, addend) |
| 218 | #define SNMP_ADD_STATS64(mib, field, addend) SNMP_ADD_STATS(mib, field, addend) |
| 219 | #define SNMP_UPD_PO_STATS64(mib, basefield, addend) SNMP_UPD_PO_STATS(mib, basefield, addend) |
| 220 | #define SNMP_UPD_PO_STATS64_BH(mib, basefield, addend) SNMP_UPD_PO_STATS_BH(mib, basefield, addend) |
| 221 | #endif |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | #endif |