sm_pfq.c 6.4 KB

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