123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #include "conf.h"
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <net/if.h>
- #include <err.h>
- #include <stdio.h>
- #include <stdlib.h>
- #ifdef HAS_PFVAR_H
- #include <net/pfvar.h>
- #include <altq/altq.h>
- #include <altq/altq_cbq.h>
- #include <altq/altq_priq.h>
- #include <altq/altq_hfsc.h>
- #endif
- #include <errno.h>
- #include <string.h>
- #include <fcntl.h>
- #include "error.h"
- #include "symon.h"
- #include "xmalloc.h"
- #ifndef HAS_PFVAR_H
- void
- privinit_pfq()
- {
- fatal("pf support not available");
- }
- void
- init_pfq(struct stream *st)
- {
- fatal("pf support not available");
- }
- void
- gets_pfq()
- {
- fatal("pf support not available");
- }
- int
- get_pfq(char *b, int l, struct stream *st)
- {
- fatal("pf support not available");
- return 0;
- }
- #else
- union class_stats {
- class_stats_t cbq;
- struct priq_classstats priq;
- struct hfsc_classstats hfsc;
- };
- struct altq_stats {
- char qname[PF_QNAME_SIZE + IFNAMSIZ + 1];
- u_int64_t sent_bytes;
- u_int64_t sent_packets;
- u_int64_t drop_bytes;
- u_int64_t drop_packets;
- };
- static struct altq_stats *pfq_stats = NULL;
- static int pfq_cur = 0;
- static int pfq_max = 0;
- int pfq_dev = -1;
- void
- privinit_pfq()
- {
- if ((pfq_dev = open("/dev/pf", O_RDONLY)) == -1) {
- warning("pfq: could not open \"/dev/pf\", %.200s", strerror(errno));
- }
- }
- void
- init_pfq(struct stream *st)
- {
- if (pfq_dev == -1) {
- privinit_pfq();
- }
- info("started module pfq(%.200s)", st->arg);
- }
- void
- gets_pfq()
- {
- struct pfioc_altq qs;
- struct pfioc_qstats stats;
- union class_stats q;
- unsigned int nqs;
- unsigned int i;
- bzero(&qs, sizeof(qs));
- bzero(&stats, sizeof(stats));
- bzero(&q, sizeof(q));
- if (ioctl(pfq_dev, DIOCGETALTQS, &qs)) {
- fatal("pfq: DIOCGETALTQS failed");
- }
- nqs = qs.nr;
-
- if (nqs > pfq_max) {
- if (pfq_stats) {
- xfree(pfq_stats);
- }
- pfq_max = 2 * nqs;
- if (pfq_max > SYMON_MAX_DOBJECTS) {
- fatal("%s:%d: dynamic object limit (%d) exceeded for pf queue structures",
- __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
- }
- pfq_stats = xmalloc(pfq_max * sizeof(struct altq_stats));
- }
- pfq_cur = 0;
-
- for (i = 0; i < nqs; i++) {
- qs.nr = i;
- if (ioctl(pfq_dev, DIOCGETALTQ, &qs)) {
- fatal("pfq: DIOCGETALTQ failed");
- }
-
- if (qs.altq.qid > 0) {
- stats.nr = qs.nr;
- stats.ticket = qs.ticket;
- stats.buf = &q;
- stats.nbytes = sizeof(q);
- if (ioctl(pfq_dev, DIOCGETQSTATS, &stats)) {
- fatal("pfq: DIOCGETQSTATS failed");
- }
-
- snprintf(pfq_stats[pfq_cur].qname, sizeof(pfq_stats[0].qname),
- "%s/%s", qs.altq.ifname, qs.altq.qname);
- switch (qs.altq.scheduler) {
- case ALTQT_CBQ:
- pfq_stats[pfq_cur].sent_bytes = q.cbq.xmit_cnt.bytes;
- pfq_stats[pfq_cur].sent_packets = q.cbq.xmit_cnt.packets;
- pfq_stats[pfq_cur].drop_bytes = q.cbq.drop_cnt.bytes;
- pfq_stats[pfq_cur].drop_packets = q.cbq.drop_cnt.packets;
- break;
- case ALTQT_PRIQ:
- pfq_stats[pfq_cur].sent_bytes = q.priq.xmitcnt.bytes;
- pfq_stats[pfq_cur].sent_packets = q.priq.xmitcnt.packets;
- pfq_stats[pfq_cur].drop_bytes = q.priq.dropcnt.bytes;
- pfq_stats[pfq_cur].drop_packets = q.priq.dropcnt.packets;
- break;
- case ALTQT_HFSC:
- pfq_stats[pfq_cur].sent_bytes = q.hfsc.xmit_cnt.bytes;
- pfq_stats[pfq_cur].sent_packets = q.hfsc.xmit_cnt.packets;
- pfq_stats[pfq_cur].drop_bytes = q.hfsc.drop_cnt.bytes;
- pfq_stats[pfq_cur].drop_packets = q.hfsc.drop_cnt.packets;
- break;
- default:
- warning("pfq: unknown altq scheduler type encountered");
- break;
- }
- pfq_cur++;
- }
- }
- }
- int
- get_pfq(char *symon_buf, int maxlen, struct stream *st)
- {
- unsigned int i;
- for (i = 0; i < pfq_cur; i++) {
- if (strncmp(pfq_stats[i].qname, st->arg, sizeof(pfq_stats[0].qname)) == 0) {
- return snpack(symon_buf, maxlen, st->arg, MT_PFQ,
- pfq_stats[i].sent_bytes,
- pfq_stats[i].sent_packets,
- pfq_stats[i].drop_bytes,
- pfq_stats[i].drop_packets
- );
- }
- }
- return 0;
- }
- #endif
|