sm_pfq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* $Id: sm_pfq.c,v 1.4 2005/10/21 14:58:46 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 <err.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #ifdef HAS_PFVAR_H
  44. #include <net/pfvar.h>
  45. #include <altq/altq.h>
  46. #include <altq/altq_cbq.h>
  47. #include <altq/altq_priq.h>
  48. #include <altq/altq_hfsc.h>
  49. #endif
  50. #include <errno.h>
  51. #include <string.h>
  52. #include <fcntl.h>
  53. #include "error.h"
  54. #include "symon.h"
  55. #include "xmalloc.h"
  56. #ifndef HAS_PFVAR_H
  57. void
  58. privinit_pfq()
  59. {
  60. fatal("pf support not available");
  61. }
  62. void
  63. init_pfq(struct stream *st)
  64. {
  65. fatal("pf support not available");
  66. }
  67. void
  68. gets_pfq()
  69. {
  70. fatal("pf support not available");
  71. }
  72. int
  73. get_pfq(char *b, int l, struct stream *st)
  74. {
  75. fatal("pf support not available");
  76. return 0;
  77. }
  78. #else
  79. union class_stats {
  80. class_stats_t cbq;
  81. struct priq_classstats priq;
  82. struct hfsc_classstats hfsc;
  83. };
  84. /*
  85. * We do not use the data structures from altq/altq_{cbq|hfsc|priq}.h as they
  86. * are overly complex. For now we only grab the interesting stuff.
  87. */
  88. struct altq_stats {
  89. char qname[PF_QNAME_SIZE + IFNAMSIZ + 1];
  90. u_int64_t sent_bytes;
  91. u_int64_t sent_packets;
  92. u_int64_t drop_bytes;
  93. u_int64_t drop_packets;
  94. };
  95. static struct altq_stats *pfq_stats = NULL;
  96. static int pfq_cur = 0;
  97. static int pfq_max = 0;
  98. int pfq_dev = -1;
  99. void
  100. privinit_pfq()
  101. {
  102. if ((pfq_dev = open("/dev/pf", O_RDONLY)) == -1) {
  103. warning("pfq: could not open \"/dev/pf\", %.200s", strerror(errno));
  104. }
  105. }
  106. void
  107. init_pfq(struct stream *st)
  108. {
  109. if (pfq_dev == -1) {
  110. privinit_pfq();
  111. }
  112. info("started module pfq(%.200s)", st->arg);
  113. }
  114. void
  115. gets_pfq()
  116. {
  117. struct pfioc_altq qs;
  118. struct pfioc_qstats stats;
  119. union class_stats q;
  120. unsigned int nqs;
  121. unsigned int i;
  122. bzero(&qs, sizeof(qs));
  123. bzero(&stats, sizeof(stats));
  124. bzero(&q, sizeof(q));
  125. if (ioctl(pfq_dev, DIOCGETALTQS, &qs)) {
  126. fatal("pfq: DIOCGETALTQS failed");
  127. }
  128. nqs = qs.nr;
  129. /* Allocate memory for info for the nqs queues */
  130. if (nqs > pfq_max) {
  131. if (pfq_stats) {
  132. xfree(pfq_stats);
  133. }
  134. pfq_max = 2 * nqs;
  135. if (pfq_max > SYMON_MAX_DOBJECTS) {
  136. fatal("%s:%d: dynamic object limit (%d) exceeded for pf queue structures",
  137. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  138. }
  139. pfq_stats = xmalloc(pfq_max * sizeof(struct altq_stats));
  140. }
  141. pfq_cur = 0;
  142. /* Loop through the queues, copy info */
  143. for (i = 0; i < nqs; i++) {
  144. qs.nr = i;
  145. if (ioctl(pfq_dev, DIOCGETALTQ, &qs)) {
  146. fatal("pfq: DIOCGETALTQ failed");
  147. }
  148. /* only process the non-empty queues */
  149. if (qs.altq.qid > 0) {
  150. stats.nr = qs.nr;
  151. stats.ticket = qs.ticket;
  152. stats.buf = &q;
  153. stats.nbytes = sizeof(q);
  154. if (ioctl(pfq_dev, DIOCGETQSTATS, &stats)) {
  155. fatal("pfq: DIOCGETQSTATS failed");
  156. }
  157. /* We're now ready to copy the data we want. */
  158. snprintf(pfq_stats[pfq_cur].qname, sizeof(pfq_stats[0].qname),
  159. "%s/%s", qs.altq.ifname, qs.altq.qname);
  160. switch (qs.altq.scheduler) {
  161. case ALTQT_CBQ:
  162. pfq_stats[pfq_cur].sent_bytes = q.cbq.xmit_cnt.bytes;
  163. pfq_stats[pfq_cur].sent_packets = q.cbq.xmit_cnt.packets;
  164. pfq_stats[pfq_cur].drop_bytes = q.cbq.drop_cnt.bytes;
  165. pfq_stats[pfq_cur].drop_packets = q.cbq.drop_cnt.packets;
  166. break;
  167. case ALTQT_PRIQ:
  168. pfq_stats[pfq_cur].sent_bytes = q.priq.xmitcnt.bytes;
  169. pfq_stats[pfq_cur].sent_packets = q.priq.xmitcnt.packets;
  170. pfq_stats[pfq_cur].drop_bytes = q.priq.dropcnt.bytes;
  171. pfq_stats[pfq_cur].drop_packets = q.priq.dropcnt.packets;
  172. break;
  173. case ALTQT_HFSC:
  174. pfq_stats[pfq_cur].sent_bytes = q.hfsc.xmit_cnt.bytes;
  175. pfq_stats[pfq_cur].sent_packets = q.hfsc.xmit_cnt.packets;
  176. pfq_stats[pfq_cur].drop_bytes = q.hfsc.drop_cnt.bytes;
  177. pfq_stats[pfq_cur].drop_packets = q.hfsc.drop_cnt.packets;
  178. break;
  179. default:
  180. warning("pfq: unknown altq scheduler type encountered");
  181. break;
  182. }
  183. pfq_cur++;
  184. }
  185. }
  186. }
  187. int
  188. get_pfq(char *symon_buf, int maxlen, struct stream *st)
  189. {
  190. unsigned int i;
  191. for (i = 0; i < pfq_cur; i++) {
  192. if (strncmp(pfq_stats[i].qname, st->arg, sizeof(pfq_stats[0].qname)) == 0) {
  193. return snpack(symon_buf, maxlen, st->arg, MT_PFQ,
  194. pfq_stats[i].sent_bytes,
  195. pfq_stats[i].sent_packets,
  196. pfq_stats[i].drop_bytes,
  197. pfq_stats[i].drop_packets
  198. );
  199. }
  200. }
  201. return 0;
  202. }
  203. #endif