sm_pfq.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* $Id: sm_pfq.c,v 1.5 2007/07/09 12:32:16 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. #ifdef HAS_ALTQ_BASIC_STATS
  83. struct priq_basic_class_stats priq;
  84. struct hfsc_basic_class_stats hfsc;
  85. #else
  86. struct priq_classstats priq;
  87. struct hfsc_classstats hfsc;
  88. #endif
  89. };
  90. /*
  91. * We do not use the data structures from altq/altq_{cbq|hfsc|priq}.h as they
  92. * are overly complex. For now we only grab the interesting stuff.
  93. */
  94. struct altq_stats {
  95. char qname[PF_QNAME_SIZE + IFNAMSIZ + 1];
  96. u_int64_t sent_bytes;
  97. u_int64_t sent_packets;
  98. u_int64_t drop_bytes;
  99. u_int64_t drop_packets;
  100. };
  101. static struct altq_stats *pfq_stats = NULL;
  102. static int pfq_cur = 0;
  103. static int pfq_max = 0;
  104. int pfq_dev = -1;
  105. void
  106. privinit_pfq()
  107. {
  108. if ((pfq_dev = open("/dev/pf", O_RDONLY)) == -1) {
  109. warning("pfq: could not open \"/dev/pf\", %.200s", strerror(errno));
  110. }
  111. }
  112. void
  113. init_pfq(struct stream *st)
  114. {
  115. if (pfq_dev == -1) {
  116. privinit_pfq();
  117. }
  118. info("started module pfq(%.200s)", st->arg);
  119. }
  120. void
  121. gets_pfq()
  122. {
  123. struct pfioc_altq qs;
  124. struct pfioc_qstats stats;
  125. union class_stats q;
  126. unsigned int nqs;
  127. unsigned int i;
  128. bzero(&qs, sizeof(qs));
  129. bzero(&stats, sizeof(stats));
  130. bzero(&q, sizeof(q));
  131. if (ioctl(pfq_dev, DIOCGETALTQS, &qs)) {
  132. fatal("pfq: DIOCGETALTQS failed");
  133. }
  134. nqs = qs.nr;
  135. /* Allocate memory for info for the nqs queues */
  136. if (nqs > pfq_max) {
  137. if (pfq_stats) {
  138. xfree(pfq_stats);
  139. }
  140. pfq_max = 2 * nqs;
  141. if (pfq_max > SYMON_MAX_DOBJECTS) {
  142. fatal("%s:%d: dynamic object limit (%d) exceeded for pf queue structures",
  143. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  144. }
  145. pfq_stats = xmalloc(pfq_max * sizeof(struct altq_stats));
  146. }
  147. pfq_cur = 0;
  148. /* Loop through the queues, copy info */
  149. for (i = 0; i < nqs; i++) {
  150. qs.nr = i;
  151. if (ioctl(pfq_dev, DIOCGETALTQ, &qs)) {
  152. fatal("pfq: DIOCGETALTQ failed");
  153. }
  154. /* only process the non-empty queues */
  155. if (qs.altq.qid > 0) {
  156. stats.nr = qs.nr;
  157. stats.ticket = qs.ticket;
  158. stats.buf = &q;
  159. stats.nbytes = sizeof(q);
  160. if (ioctl(pfq_dev, DIOCGETQSTATS, &stats)) {
  161. fatal("pfq: DIOCGETQSTATS failed");
  162. }
  163. /* We're now ready to copy the data we want. */
  164. snprintf(pfq_stats[pfq_cur].qname, sizeof(pfq_stats[0].qname),
  165. "%s/%s", qs.altq.ifname, qs.altq.qname);
  166. switch (qs.altq.scheduler) {
  167. case ALTQT_CBQ:
  168. pfq_stats[pfq_cur].sent_bytes = q.cbq.xmit_cnt.bytes;
  169. pfq_stats[pfq_cur].sent_packets = q.cbq.xmit_cnt.packets;
  170. pfq_stats[pfq_cur].drop_bytes = q.cbq.drop_cnt.bytes;
  171. pfq_stats[pfq_cur].drop_packets = q.cbq.drop_cnt.packets;
  172. break;
  173. case ALTQT_PRIQ:
  174. pfq_stats[pfq_cur].sent_bytes = q.priq.xmitcnt.bytes;
  175. pfq_stats[pfq_cur].sent_packets = q.priq.xmitcnt.packets;
  176. pfq_stats[pfq_cur].drop_bytes = q.priq.dropcnt.bytes;
  177. pfq_stats[pfq_cur].drop_packets = q.priq.dropcnt.packets;
  178. break;
  179. case ALTQT_HFSC:
  180. pfq_stats[pfq_cur].sent_bytes = q.hfsc.xmit_cnt.bytes;
  181. pfq_stats[pfq_cur].sent_packets = q.hfsc.xmit_cnt.packets;
  182. pfq_stats[pfq_cur].drop_bytes = q.hfsc.drop_cnt.bytes;
  183. pfq_stats[pfq_cur].drop_packets = q.hfsc.drop_cnt.packets;
  184. break;
  185. default:
  186. warning("pfq: unknown altq scheduler type encountered");
  187. break;
  188. }
  189. pfq_cur++;
  190. }
  191. }
  192. }
  193. int
  194. get_pfq(char *symon_buf, int maxlen, struct stream *st)
  195. {
  196. unsigned int i;
  197. for (i = 0; i < pfq_cur; i++) {
  198. if (strncmp(pfq_stats[i].qname, st->arg, sizeof(pfq_stats[0].qname)) == 0) {
  199. return snpack(symon_buf, maxlen, st->arg, MT_PFQ,
  200. pfq_stats[i].sent_bytes,
  201. pfq_stats[i].sent_packets,
  202. pfq_stats[i].drop_bytes,
  203. pfq_stats[i].drop_packets
  204. );
  205. }
  206. }
  207. return 0;
  208. }
  209. #endif