snmp.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 all unsigned longs. Linux will soon be so reliable that even
  45. * these will rapidly get too small 8-). Seriously consider the IpInReceives
  46. * count on the 20Gb/s + networks people expect in a few years time!
  47. */
  48. /* IPstats */
  49. #define IPSTATS_MIB_MAX __IPSTATS_MIB_MAX
  50. struct ipstats_mib {
  51. unsigned long mibs[IPSTATS_MIB_MAX];
  52. };
  53. /* ICMP */
  54. #define ICMP_MIB_DUMMY __ICMP_MIB_MAX
  55. #define ICMP_MIB_MAX (__ICMP_MIB_MAX + 1)
  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. unsigned long mibs[ICMPMSG_MIB_MAX];
  62. };
  63. /* ICMP6 (IPv6-ICMP) */
  64. #define ICMP6_MIB_MAX __ICMP6_MIB_MAX
  65. struct icmpv6_mib {
  66. unsigned long mibs[ICMP6_MIB_MAX];
  67. };
  68. #define ICMP6MSG_MIB_MAX __ICMP6MSG_MIB_MAX
  69. struct icmpv6msg_mib {
  70. unsigned long mibs[ICMP6MSG_MIB_MAX];
  71. };
  72. /* TCP */
  73. #define TCP_MIB_MAX __TCP_MIB_MAX
  74. struct tcp_mib {
  75. unsigned long mibs[TCP_MIB_MAX];
  76. };
  77. /* UDP */
  78. #define UDP_MIB_MAX __UDP_MIB_MAX
  79. struct udp_mib {
  80. unsigned long mibs[UDP_MIB_MAX];
  81. };
  82. /* Linux */
  83. #define LINUX_MIB_MAX __LINUX_MIB_MAX
  84. struct linux_mib {
  85. unsigned long mibs[LINUX_MIB_MAX];
  86. };
  87. /* Linux Xfrm */
  88. #define LINUX_MIB_XFRMMAX __LINUX_MIB_XFRMMAX
  89. struct linux_xfrm_mib {
  90. unsigned long mibs[LINUX_MIB_XFRMMAX];
  91. };
  92. /*
  93. * FIXME: On x86 and some other CPUs the split into user and softirq parts
  94. * is not needed because addl $1,memory is atomic against interrupts (but
  95. * atomic_inc would be overkill because of the lock cycles). Wants new
  96. * nonlocked_atomic_inc() primitives -AK
  97. */
  98. #define DEFINE_SNMP_STAT(type, name) \
  99. __typeof__(type) __percpu *name[2]
  100. #define DECLARE_SNMP_STAT(type, name) \
  101. extern __typeof__(type) __percpu *name[2]
  102. #define SNMP_STAT_BHPTR(name) (name[0])
  103. #define SNMP_STAT_USRPTR(name) (name[1])
  104. #define SNMP_INC_STATS_BH(mib, field) \
  105. __this_cpu_inc(mib[0]->mibs[field])
  106. #define SNMP_INC_STATS_USER(mib, field) \
  107. this_cpu_inc(mib[1]->mibs[field])
  108. #define SNMP_INC_STATS(mib, field) \
  109. this_cpu_inc(mib[!in_softirq()]->mibs[field])
  110. #define SNMP_DEC_STATS(mib, field) \
  111. this_cpu_dec(mib[!in_softirq()]->mibs[field])
  112. #define SNMP_ADD_STATS_BH(mib, field, addend) \
  113. __this_cpu_add(mib[0]->mibs[field], addend)
  114. #define SNMP_ADD_STATS_USER(mib, field, addend) \
  115. this_cpu_add(mib[1]->mibs[field], addend)
  116. #define SNMP_ADD_STATS(mib, field, addend) \
  117. this_cpu_add(mib[!in_softirq()]->mibs[field], addend)
  118. /*
  119. * Use "__typeof__(*mib[0]) *ptr" instead of "__typeof__(mib[0]) ptr"
  120. * to make @ptr a non-percpu pointer.
  121. */
  122. #define SNMP_UPD_PO_STATS(mib, basefield, addend) \
  123. do { \
  124. __typeof__(*mib[0]) *ptr; \
  125. preempt_disable(); \
  126. ptr = this_cpu_ptr((mib)[!in_softirq()]); \
  127. ptr->mibs[basefield##PKTS]++; \
  128. ptr->mibs[basefield##OCTETS] += addend;\
  129. preempt_enable(); \
  130. } while (0)
  131. #define SNMP_UPD_PO_STATS_BH(mib, basefield, addend) \
  132. do { \
  133. __typeof__(*mib[0]) *ptr = \
  134. __this_cpu_ptr((mib)[!in_softirq()]); \
  135. ptr->mibs[basefield##PKTS]++; \
  136. ptr->mibs[basefield##OCTETS] += addend;\
  137. } while (0)
  138. #endif