sm_pfq.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2005 J. Martin Petersen
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * Get current altq statistics from pf and return them in symon_buf as
  32. * sent_bytes : sent_packets : drop_bytes : drop_packets
  33. */
  34. #include "conf.h"
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <sys/ioctl.h>
  38. #include <net/if.h>
  39. #include <err.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #ifdef HAS_PFVAR_H
  43. #include <net/pfvar.h>
  44. #include <altq/altq.h>
  45. #include <altq/altq_cbq.h>
  46. #include <altq/altq_priq.h>
  47. #include <altq/altq_hfsc.h>
  48. #endif
  49. #include <errno.h>
  50. #include <string.h>
  51. #include <fcntl.h>
  52. #include "error.h"
  53. #include "symon.h"
  54. #include "xmalloc.h"
  55. #ifndef HAS_PFVAR_H
  56. void
  57. privinit_pfq()
  58. {
  59. fatal("pf support not available");
  60. }
  61. void
  62. init_pfq(struct stream *st)
  63. {
  64. fatal("pf support not available");
  65. }
  66. void
  67. gets_pfq()
  68. {
  69. fatal("pf support not available");
  70. }
  71. int
  72. get_pfq(char *b, int l, struct stream *st)
  73. {
  74. fatal("pf support not available");
  75. return 0;
  76. }
  77. #else
  78. union class_stats {
  79. class_stats_t cbq;
  80. struct priq_classstats priq;
  81. struct hfsc_classstats hfsc;
  82. };
  83. /*
  84. * We do not use the data structures from altq/altq_{cbq|hfsc|priq}.h as they
  85. * are overly complex. For now we only grab the interesting stuff.
  86. */
  87. struct altq_stats {
  88. char qname[PF_QNAME_SIZE + IFNAMSIZ + 1];
  89. u_int64_t sent_bytes;
  90. u_int64_t sent_packets;
  91. u_int64_t drop_bytes;
  92. u_int64_t drop_packets;
  93. };
  94. static struct altq_stats *pfq_stats = NULL;
  95. static int pfq_cur = 0;
  96. static int pfq_max = 0;
  97. int pfq_dev = -1;
  98. void
  99. privinit_pfq()
  100. {
  101. if ((pfq_dev = open("/dev/pf", O_RDONLY)) == -1) {
  102. warning("pfq: could not open \"/dev/pf\", %.200s", strerror(errno));
  103. }
  104. }
  105. void
  106. init_pfq(struct stream *st)
  107. {
  108. if (pfq_dev == -1) {
  109. privinit_pfq();
  110. }
  111. info("started module pfq(%.200s)", st->arg);
  112. }
  113. void
  114. gets_pfq()
  115. {
  116. struct pfioc_altq qs;
  117. struct pfioc_qstats stats;
  118. union class_stats q;
  119. unsigned int nqs;
  120. unsigned int i;
  121. bzero(&qs, sizeof(qs));
  122. bzero(&stats, sizeof(stats));
  123. bzero(&q, sizeof(q));
  124. if (ioctl(pfq_dev, DIOCGETALTQS, &qs)) {
  125. fatal("pfq: DIOCGETALTQS failed");
  126. }
  127. nqs = qs.nr;
  128. /* Allocate memory for info for the nqs queues */
  129. if (nqs > pfq_max) {
  130. if (pfq_stats) {
  131. xfree(pfq_stats);
  132. }
  133. pfq_max = 2 * nqs;
  134. if (pfq_max > SYMON_MAX_DOBJECTS) {
  135. fatal("%s:%d: dynamic object limit (%d) exceeded for pf queue structures",
  136. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  137. }
  138. pfq_stats = xmalloc(pfq_max * sizeof(struct altq_stats));
  139. }
  140. pfq_cur = 0;
  141. /* Loop through the queues, copy info */
  142. for (i = 0; i < nqs; i++) {
  143. qs.nr = i;
  144. if (ioctl(pfq_dev, DIOCGETALTQ, &qs)) {
  145. fatal("pfq: DIOCGETALTQ failed");
  146. }
  147. /* only process the non-empty queues */
  148. if (qs.altq.qid > 0) {
  149. stats.nr = qs.nr;
  150. stats.ticket = qs.ticket;
  151. stats.buf = &q;
  152. stats.nbytes = sizeof(q);
  153. if (ioctl(pfq_dev, DIOCGETQSTATS, &stats)) {
  154. fatal("pfq: DIOCGETQSTATS failed");
  155. }
  156. /* We're now ready to copy the data we want. */
  157. snprintf(pfq_stats[pfq_cur].qname, sizeof(pfq_stats[0].qname),
  158. "%s/%s", qs.altq.ifname, qs.altq.qname);
  159. switch (qs.altq.scheduler) {
  160. case ALTQT_CBQ:
  161. pfq_stats[pfq_cur].sent_bytes = q.cbq.xmit_cnt.bytes;
  162. pfq_stats[pfq_cur].sent_packets = q.cbq.xmit_cnt.packets;
  163. pfq_stats[pfq_cur].drop_bytes = q.cbq.drop_cnt.bytes;
  164. pfq_stats[pfq_cur].drop_packets = q.cbq.drop_cnt.packets;
  165. break;
  166. case ALTQT_PRIQ:
  167. pfq_stats[pfq_cur].sent_bytes = q.priq.xmitcnt.bytes;
  168. pfq_stats[pfq_cur].sent_packets = q.priq.xmitcnt.packets;
  169. pfq_stats[pfq_cur].drop_bytes = q.priq.dropcnt.bytes;
  170. pfq_stats[pfq_cur].drop_packets = q.priq.dropcnt.packets;
  171. break;
  172. case ALTQT_HFSC:
  173. pfq_stats[pfq_cur].sent_bytes = q.hfsc.xmit_cnt.bytes;
  174. pfq_stats[pfq_cur].sent_packets = q.hfsc.xmit_cnt.packets;
  175. pfq_stats[pfq_cur].drop_bytes = q.hfsc.drop_cnt.bytes;
  176. pfq_stats[pfq_cur].drop_packets = q.hfsc.drop_cnt.packets;
  177. break;
  178. default:
  179. warning("pfq: unknown altq scheduler type encountered");
  180. break;
  181. }
  182. pfq_cur++;
  183. }
  184. }
  185. }
  186. int
  187. get_pfq(char *symon_buf, int maxlen, struct stream *st)
  188. {
  189. unsigned int i;
  190. for (i = 0; i < pfq_cur; i++) {
  191. if (strncmp(pfq_stats[i].qname, st->arg, sizeof(pfq_stats[0].qname)) == 0) {
  192. return snpack(symon_buf, maxlen, st->arg, MT_PFQ,
  193. pfq_stats[i].sent_bytes,
  194. pfq_stats[i].sent_packets,
  195. pfq_stats[i].drop_bytes,
  196. pfq_stats[i].drop_packets
  197. );
  198. }
  199. }
  200. return 0;
  201. }
  202. #endif