sm_pfq.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* $Id: sm_pfq.c,v 1.1 2005/03/20 16:17:22 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. * Non re-entrant code: gets_pfq messes with the globals without locking.
  36. */
  37. #include "conf.h"
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <sys/ioctl.h>
  41. #include <net/if.h>
  42. #include <err.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #ifdef HAS_PFVAR_H
  46. #include <net/pfvar.h>
  47. #include <altq/altq.h>
  48. #include <altq/altq_cbq.h>
  49. #include <altq/altq_priq.h>
  50. #include <altq/altq_hfsc.h>
  51. #endif
  52. #include <errno.h>
  53. #include <string.h>
  54. #include <fcntl.h>
  55. #include "error.h"
  56. #include "symon.h"
  57. #include "xmalloc.h"
  58. #ifndef HAS_PFVAR_H
  59. void
  60. privinit_pfq()
  61. {
  62. fatal("pf support not available");
  63. }
  64. void
  65. init_pfq(char *s)
  66. {
  67. fatal("pf support not available");
  68. }
  69. void
  70. gets_pfq()
  71. {
  72. fatal("pf support not available");
  73. }
  74. int
  75. get_pfq(char *b, int l, char *s)
  76. {
  77. fatal("pf support not available");
  78. return 0;
  79. }
  80. #else
  81. union class_stats {
  82. class_stats_t cbq;
  83. struct priq_classstats priq;
  84. struct hfsc_classstats hfsc;
  85. };
  86. /*
  87. * We don't reuse the data structures from altq/altq_{cbq|hfsc|priq}.h as they
  88. * are overly complex. For now we only grab the interesting stuff.
  89. */
  90. struct altq_stats {
  91. char qname[PF_QNAME_SIZE];
  92. u_int64_t sent_bytes;
  93. u_int64_t sent_packets;
  94. u_int64_t drop_bytes;
  95. u_int64_t drop_packets;
  96. };
  97. static struct altq_stats *pfq_stats = NULL;
  98. static int pfq_numstats = 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(char *s)
  109. {
  110. if (pfq_dev == -1) {
  111. privinit_pfq();
  112. }
  113. info("started module pfq(%.200s)",s);
  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_numstats) {
  132. if (pfq_stats) {
  133. xfree(pfq_stats);
  134. }
  135. pfq_stats = xmalloc(nqs * sizeof(struct altq_stats));
  136. pfq_numstats = nqs;
  137. }
  138. /* Loop through the queues, copy info */
  139. for (i = 0; i < nqs; i++) {
  140. qs.nr = i;
  141. if (ioctl(pfq_dev, DIOCGETALTQ, &qs)) {
  142. fatal("pfq: DIOCGETALTQ failed");
  143. }
  144. /* only process the non-empty queues */
  145. if (qs.altq.qid > 0) {
  146. stats.nr = qs.nr;
  147. stats.ticket = qs.ticket;
  148. stats.buf = &q;
  149. stats.nbytes = sizeof(q);
  150. if (ioctl(pfq_dev, DIOCGETQSTATS, &stats)) {
  151. fatal("pfq: DIOCGETQSTATS failed");
  152. }
  153. /* We're now ready to copy the data we want. */
  154. strncpy(pfq_stats[i].qname, qs.altq.qname, PF_QNAME_SIZE);
  155. switch (qs.altq.scheduler) {
  156. case ALTQT_CBQ:
  157. pfq_stats[i].sent_bytes = q.cbq.xmit_cnt.bytes;
  158. pfq_stats[i].sent_packets = q.cbq.xmit_cnt.packets;
  159. pfq_stats[i].drop_bytes = q.cbq.drop_cnt.bytes;
  160. pfq_stats[i].drop_packets = q.cbq.drop_cnt.packets;
  161. break;
  162. case ALTQT_PRIQ:
  163. pfq_stats[i].sent_bytes = q.priq.xmitcnt.bytes;
  164. pfq_stats[i].sent_packets = q.priq.xmitcnt.packets;
  165. pfq_stats[i].drop_bytes = q.priq.dropcnt.bytes;
  166. pfq_stats[i].drop_packets = q.priq.dropcnt.packets;
  167. break;
  168. case ALTQT_HFSC:
  169. pfq_stats[i].sent_bytes = q.hfsc.xmit_cnt.bytes;
  170. pfq_stats[i].sent_packets = q.hfsc.xmit_cnt.packets;
  171. pfq_stats[i].drop_bytes = q.hfsc.drop_cnt.bytes;
  172. pfq_stats[i].drop_packets = q.hfsc.drop_cnt.packets;
  173. break;
  174. default:
  175. warning("pfq: unknown altq scheduler type encountered");
  176. break;
  177. }
  178. } else {
  179. /* Make sure the lookup in get_pfq fails immediately whenever
  180. * there's no queue in the slot. We do this by nul'ing the name */
  181. pfq_stats[i].qname[0] = '\0';
  182. }
  183. }
  184. }
  185. int
  186. get_pfq(char *symon_buf, int maxlen, char *queue)
  187. {
  188. unsigned int i;
  189. for (i = 0; i < pfq_numstats; i++) {
  190. if (strncmp(pfq_stats[i].qname, queue, PF_QNAME_SIZE) == 0) {
  191. return snpack(symon_buf, maxlen, queue, MT_PFQ,
  192. pfq_stats[i].sent_bytes,
  193. pfq_stats[i].sent_packets,
  194. pfq_stats[i].drop_bytes,
  195. pfq_stats[i].drop_packets
  196. );
  197. }
  198. }
  199. return 0;
  200. }
  201. #endif