snmp.h 5.2 KB

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