snmp.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_DUMMY __ICMP_MIB_MAX
  56. #define ICMP_MIB_MAX (__ICMP_MIB_MAX + 1)
  57. struct icmp_mib {
  58. unsigned long mibs[ICMP_MIB_MAX];
  59. };
  60. #define ICMPMSG_MIB_MAX __ICMPMSG_MIB_MAX
  61. struct icmpmsg_mib {
  62. unsigned long mibs[ICMPMSG_MIB_MAX];
  63. };
  64. /* ICMP6 (IPv6-ICMP) */
  65. #define ICMP6_MIB_MAX __ICMP6_MIB_MAX
  66. struct icmpv6_mib {
  67. unsigned long mibs[ICMP6_MIB_MAX];
  68. };
  69. #define ICMP6MSG_MIB_MAX __ICMP6MSG_MIB_MAX
  70. struct icmpv6msg_mib {
  71. unsigned long 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. /*
  94. * FIXME: On x86 and some other CPUs the split into user and softirq parts
  95. * is not needed because addl $1,memory is atomic against interrupts (but
  96. * atomic_inc would be overkill because of the lock cycles). Wants new
  97. * nonlocked_atomic_inc() primitives -AK
  98. */
  99. #define DEFINE_SNMP_STAT(type, name) \
  100. __typeof__(type) __percpu *name[2]
  101. #define DECLARE_SNMP_STAT(type, name) \
  102. extern __typeof__(type) __percpu *name[2]
  103. #define SNMP_STAT_BHPTR(name) (name[0])
  104. #define SNMP_STAT_USRPTR(name) (name[1])
  105. #define SNMP_INC_STATS_BH(mib, field) \
  106. __this_cpu_inc(mib[0]->mibs[field])
  107. #define SNMP_INC_STATS_USER(mib, field) \
  108. this_cpu_inc(mib[1]->mibs[field])
  109. #define SNMP_INC_STATS(mib, field) \
  110. this_cpu_inc(mib[!in_softirq()]->mibs[field])
  111. #define SNMP_DEC_STATS(mib, field) \
  112. this_cpu_dec(mib[!in_softirq()]->mibs[field])
  113. #define SNMP_ADD_STATS_BH(mib, field, addend) \
  114. __this_cpu_add(mib[0]->mibs[field], addend)
  115. #define SNMP_ADD_STATS_USER(mib, field, addend) \
  116. this_cpu_add(mib[1]->mibs[field], addend)
  117. #define SNMP_ADD_STATS(mib, field, addend) \
  118. this_cpu_add(mib[!in_softirq()]->mibs[field], addend)
  119. /*
  120. * Use "__typeof__(*mib[0]) *ptr" instead of "__typeof__(mib[0]) ptr"
  121. * to make @ptr a non-percpu pointer.
  122. */
  123. #define SNMP_UPD_PO_STATS(mib, basefield, addend) \
  124. do { \
  125. __typeof__(*mib[0]) *ptr; \
  126. preempt_disable(); \
  127. ptr = this_cpu_ptr((mib)[!in_softirq()]); \
  128. ptr->mibs[basefield##PKTS]++; \
  129. ptr->mibs[basefield##OCTETS] += addend;\
  130. preempt_enable(); \
  131. } while (0)
  132. #define SNMP_UPD_PO_STATS_BH(mib, basefield, addend) \
  133. do { \
  134. __typeof__(*mib[0]) *ptr = \
  135. __this_cpu_ptr((mib)[!in_softirq()]); \
  136. ptr->mibs[basefield##PKTS]++; \
  137. ptr->mibs[basefield##OCTETS] += addend;\
  138. } while (0)
  139. #if BITS_PER_LONG==32
  140. #define SNMP_ADD_STATS64_BH(mib, field, addend) \
  141. do { \
  142. __typeof__(*mib[0]) *ptr = __this_cpu_ptr((mib)[0]); \
  143. u64_stats_update_begin(&ptr->syncp); \
  144. ptr->mibs[field] += addend; \
  145. u64_stats_update_end(&ptr->syncp); \
  146. } while (0)
  147. #define SNMP_ADD_STATS64_USER(mib, field, addend) \
  148. do { \
  149. __typeof__(*mib[0]) *ptr; \
  150. preempt_disable(); \
  151. ptr = __this_cpu_ptr((mib)[1]); \
  152. u64_stats_update_begin(&ptr->syncp); \
  153. ptr->mibs[field] += addend; \
  154. u64_stats_update_end(&ptr->syncp); \
  155. preempt_enable(); \
  156. } while (0)
  157. #define SNMP_ADD_STATS64(mib, field, addend) \
  158. do { \
  159. __typeof__(*mib[0]) *ptr; \
  160. preempt_disable(); \
  161. ptr = __this_cpu_ptr((mib)[!in_softirq()]); \
  162. u64_stats_update_begin(&ptr->syncp); \
  163. ptr->mibs[field] += addend; \
  164. u64_stats_update_end(&ptr->syncp); \
  165. preempt_enable(); \
  166. } while (0)
  167. #define SNMP_INC_STATS64_BH(mib, field) SNMP_ADD_STATS64_BH(mib, field, 1)
  168. #define SNMP_INC_STATS64_USER(mib, field) SNMP_ADD_STATS64_USER(mib, field, 1)
  169. #define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
  170. #define SNMP_UPD_PO_STATS64(mib, basefield, addend) \
  171. do { \
  172. __typeof__(*mib[0]) *ptr; \
  173. preempt_disable(); \
  174. ptr = __this_cpu_ptr((mib)[!in_softirq()]); \
  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. preempt_enable(); \
  180. } while (0)
  181. #define SNMP_UPD_PO_STATS64_BH(mib, basefield, addend) \
  182. do { \
  183. __typeof__(*mib[0]) *ptr; \
  184. ptr = __this_cpu_ptr((mib)[!in_softirq()]); \
  185. u64_stats_update_begin(&ptr->syncp); \
  186. ptr->mibs[basefield##PKTS]++; \
  187. ptr->mibs[basefield##OCTETS] += addend; \
  188. u64_stats_update_end(&ptr->syncp); \
  189. } while (0)
  190. #else
  191. #define SNMP_INC_STATS64_BH(mib, field) SNMP_INC_STATS_BH(mib, field)
  192. #define SNMP_INC_STATS64_USER(mib, field) SNMP_INC_STATS_USER(mib, field)
  193. #define SNMP_INC_STATS64(mib, field) SNMP_INC_STATS(mib, field)
  194. #define SNMP_DEC_STATS64(mib, field) SNMP_DEC_STATS(mib, field)
  195. #define SNMP_ADD_STATS64_BH(mib, field, addend) SNMP_ADD_STATS_BH(mib, field, addend)
  196. #define SNMP_ADD_STATS64_USER(mib, field, addend) SNMP_ADD_STATS_USER(mib, field, addend)
  197. #define SNMP_ADD_STATS64(mib, field, addend) SNMP_ADD_STATS(mib, field, addend)
  198. #define SNMP_UPD_PO_STATS64(mib, basefield, addend) SNMP_UPD_PO_STATS(mib, basefield, addend)
  199. #define SNMP_UPD_PO_STATS64_BH(mib, basefield, addend) SNMP_UPD_PO_STATS_BH(mib, basefield, addend)
  200. #endif
  201. #endif