snmp.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 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. /*
  49. * The rule for padding:
  50. * Best is power of two because then the right structure can be found by a
  51. * simple shift. The structure should be always cache line aligned.
  52. * gcc needs n=alignto(cachelinesize, popcnt(sizeof(bla_mib))) shift/add
  53. * instructions to emulate multiply in case it is not power-of-two.
  54. * Currently n is always <=3 for all sizes so simple cache line alignment
  55. * is enough.
  56. *
  57. * The best solution would be a global CPU local area , especially on 64
  58. * and 128byte cacheline machine it makes a *lot* of sense -AK
  59. */
  60. #define __SNMP_MIB_ALIGN__ ____cacheline_aligned
  61. /* IPstats */
  62. #define IPSTATS_MIB_MAX __IPSTATS_MIB_MAX
  63. struct ipstats_mib {
  64. unsigned long mibs[IPSTATS_MIB_MAX];
  65. } __SNMP_MIB_ALIGN__;
  66. /* ICMP */
  67. #define ICMP_MIB_DUMMY __ICMP_MIB_MAX
  68. #define ICMP_MIB_MAX (__ICMP_MIB_MAX + 1)
  69. struct icmp_mib {
  70. unsigned long mibs[ICMP_MIB_MAX];
  71. } __SNMP_MIB_ALIGN__;
  72. #define ICMPMSG_MIB_MAX __ICMPMSG_MIB_MAX
  73. struct icmpmsg_mib {
  74. unsigned long mibs[ICMPMSG_MIB_MAX];
  75. } __SNMP_MIB_ALIGN__;
  76. /* ICMP6 (IPv6-ICMP) */
  77. #define ICMP6_MIB_MAX __ICMP6_MIB_MAX
  78. struct icmpv6_mib {
  79. unsigned long mibs[ICMP6_MIB_MAX];
  80. } __SNMP_MIB_ALIGN__;
  81. #define ICMP6MSG_MIB_MAX __ICMP6MSG_MIB_MAX
  82. struct icmpv6msg_mib {
  83. unsigned long mibs[ICMP6MSG_MIB_MAX];
  84. } __SNMP_MIB_ALIGN__;
  85. /* TCP */
  86. #define TCP_MIB_MAX __TCP_MIB_MAX
  87. struct tcp_mib {
  88. unsigned long mibs[TCP_MIB_MAX];
  89. } __SNMP_MIB_ALIGN__;
  90. /* UDP */
  91. #define UDP_MIB_MAX __UDP_MIB_MAX
  92. struct udp_mib {
  93. unsigned long mibs[UDP_MIB_MAX];
  94. } __SNMP_MIB_ALIGN__;
  95. /* Linux */
  96. #define LINUX_MIB_MAX __LINUX_MIB_MAX
  97. struct linux_mib {
  98. unsigned long mibs[LINUX_MIB_MAX];
  99. };
  100. /* Linux Xfrm */
  101. #define LINUX_MIB_XFRMMAX __LINUX_MIB_XFRMMAX
  102. struct linux_xfrm_mib {
  103. unsigned long mibs[LINUX_MIB_XFRMMAX];
  104. };
  105. /*
  106. * FIXME: On x86 and some other CPUs the split into user and softirq parts
  107. * is not needed because addl $1,memory is atomic against interrupts (but
  108. * atomic_inc would be overkill because of the lock cycles). Wants new
  109. * nonlocked_atomic_inc() primitives -AK
  110. */
  111. #define DEFINE_SNMP_STAT(type, name) \
  112. __typeof__(type) *name[2]
  113. #define DECLARE_SNMP_STAT(type, name) \
  114. extern __typeof__(type) *name[2]
  115. #define SNMP_STAT_BHPTR(name) (name[0])
  116. #define SNMP_STAT_USRPTR(name) (name[1])
  117. #define SNMP_INC_STATS_BH(mib, field) \
  118. (per_cpu_ptr(mib[0], raw_smp_processor_id())->mibs[field]++)
  119. #define SNMP_INC_STATS_USER(mib, field) \
  120. do { \
  121. per_cpu_ptr(mib[1], get_cpu())->mibs[field]++; \
  122. put_cpu(); \
  123. } while (0)
  124. #define SNMP_INC_STATS(mib, field) \
  125. do { \
  126. per_cpu_ptr(mib[!in_softirq()], get_cpu())->mibs[field]++; \
  127. put_cpu(); \
  128. } while (0)
  129. #define SNMP_DEC_STATS(mib, field) \
  130. do { \
  131. per_cpu_ptr(mib[!in_softirq()], get_cpu())->mibs[field]--; \
  132. put_cpu(); \
  133. } while (0)
  134. #define SNMP_ADD_STATS(mib, field, addend) \
  135. do { \
  136. per_cpu_ptr(mib[!in_softirq()], get_cpu())->mibs[field] += addend; \
  137. put_cpu(); \
  138. } while (0)
  139. #define SNMP_ADD_STATS_BH(mib, field, addend) \
  140. (per_cpu_ptr(mib[0], raw_smp_processor_id())->mibs[field] += addend)
  141. #define SNMP_ADD_STATS_USER(mib, field, addend) \
  142. do { \
  143. per_cpu_ptr(mib[1], get_cpu())->mibs[field] += addend; \
  144. put_cpu(); \
  145. } while (0)
  146. #define SNMP_UPD_PO_STATS(mib, basefield, addend) \
  147. do { \
  148. __typeof__(mib[0]) ptr = per_cpu_ptr(mib[!in_softirq()], get_cpu());\
  149. ptr->mibs[basefield##PKTS]++; \
  150. ptr->mibs[basefield##OCTETS] += addend;\
  151. put_cpu(); \
  152. } while (0)
  153. #define SNMP_UPD_PO_STATS_BH(mib, basefield, addend) \
  154. do { \
  155. __typeof__(mib[0]) ptr = per_cpu_ptr(mib[!in_softirq()], raw_smp_processor_id());\
  156. ptr->mibs[basefield##PKTS]++; \
  157. ptr->mibs[basefield##OCTETS] += addend;\
  158. } while (0)
  159. #endif