snmp.h 5.1 KB

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