2 * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTIFSTAT_ERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/sysctl.h>
33 #include <net/if_mib.h>
41 #include <net/altq/altq.h>
42 #include <net/altq/altq_cbq.h>
43 #include <net/altq/altq_priq.h>
44 #include <net/altq/altq_hfsc.h>
45 #include <net/altq/altq_fairq.h>
47 #include <net/pf/pfvar.h>
53 static SLIST_HEAD(, qcol
) qcols
;
54 static SLIST_HEAD(, if_stat
) curlist
;
55 static int pf_fd
= -1;
57 static int TopSection1
;
58 static int TopSection2
;
59 static int TopSection3
;
62 SLIST_ENTRY(qcol
) link
;
67 typedef struct qcol qcol_t
;
71 struct priq_classstats priq
;
72 struct hfsc_classstats hfsc
;
73 struct fairq_classstats fairq
;
77 SLIST_ENTRY(queue_stats
) link
;
79 struct pfioc_qstats pq
;
81 union class_stats ostats
;
82 union class_stats nstats
;
85 typedef struct queue_stats queue_stats_t
;
88 SLIST_ENTRY(if_stat
) link
;
89 SLIST_HEAD(, queue_stats
) queues
;
90 char if_name
[IF_NAMESIZE
];
91 struct ifmibdata if_mib
;
93 struct timeval tv_lastchanged
;
94 u_long if_in_curtraffic
;
95 u_long if_out_curtraffic
;
96 u_long if_in_traffic_peak
;
97 u_long if_out_traffic_peak
;
98 u_int if_row
; /* Index into ifmib sysctl */
99 u_int row
; /* display row (relative) */
103 typedef struct if_stat if_stat_t
;
105 extern u_int curscale
;
107 static void load_altqs(void);
108 static void print_altq(if_stat_t
*p
, queue_stats_t
*q
);
109 static void right_align_string(if_stat_t
*);
110 static void getifmibdata(const int, struct ifmibdata
*);
111 static void sort_interface_list(void);
112 static u_int
getifnum(void);
114 #define IFSTAT_ERR(n, s) do { \
130 pf_fd
= open("/dev/pf", O_RDONLY
);
132 n
= getifnum(); /* NOTE: can return < 0 */
134 SLIST_INIT(&curlist
);
136 for (i
= 0; i
< n
; i
++) {
137 p
= (if_stat_t
*)calloc(1, sizeof(if_stat_t
));
139 IFSTAT_ERR(1, "out of memory");
140 SLIST_INSERT_HEAD(&curlist
, p
, link
);
141 SLIST_INIT(&p
->queues
);
143 getifmibdata(p
->if_row
, &p
->if_mib
);
144 right_align_string(p
);
147 * Initially, we only display interfaces that have
148 * received some traffic.
150 if (p
->if_mib
.ifmd_data
.ifi_ibytes
!= 0)
155 sort_interface_list();
157 return (subwin(stdscr
, LINES
-1-5, 0, 5, 0));
161 closealtqs(WINDOW
*w
)
163 if_stat_t
*node
= NULL
;
166 while (!SLIST_EMPTY(&curlist
)) {
167 node
= SLIST_FIRST(&curlist
);
168 SLIST_REMOVE_HEAD(&curlist
, link
);
169 while ((q
= SLIST_FIRST(&node
->queues
)) != NULL
) {
170 SLIST_REMOVE_HEAD(&node
->queues
, link
);
175 while (!SLIST_EMPTY(&qcols
)) {
176 qcol_t
*qc
= SLIST_FIRST(&qcols
);
177 SLIST_REMOVE_HEAD(&qcols
, link
);
200 wmove(wnd
, TOPLINE
, 0);
211 mvprintw(TopSection1
, 0, " PACKETS");
212 mvprintw(TopSection2
, 0, " BYTES");
213 mvprintw(TopSection3
, 0, " DROPS/QLEN");
214 SLIST_FOREACH(qc
, &qcols
, link
) {
215 mvprintw(TopSection1
, TOPQSTART
+ TOPQWIDTH
* qc
->col
,
217 mvprintw(TopSection2
, TOPQSTART
+ TOPQWIDTH
* qc
->col
,
219 mvprintw(TopSection3
, TOPQSTART
+ TOPQWIDTH
* qc
->col
,
223 SLIST_FOREACH(p
, &curlist
, link
) {
226 mvprintw(TopSection1
+ p
->row
, 0, "%s", p
->if_name
);
227 mvprintw(TopSection2
+ p
->row
, 0, "%s", p
->if_name
);
228 mvprintw(TopSection3
+ p
->row
, 0, "%s", p
->if_name
);
229 SLIST_FOREACH(q
, &p
->queues
, link
) {
238 TopSection1
= TOPLINE
;
246 struct if_stat
*ifp
= NULL
;
247 struct timeval tv
, new_tv
, old_tv
;
248 double elapsed
= 0.0;
249 u_int new_inb
, new_outb
, old_inb
, old_outb
= 0;
250 u_int we_need_to_sort_interface_list
= 0;
252 SLIST_FOREACH(ifp
, &curlist
, link
) {
254 * Grab a copy of the old input/output values before we
255 * call getifmibdata().
257 old_inb
= ifp
->if_mib
.ifmd_data
.ifi_ibytes
;
258 old_outb
= ifp
->if_mib
.ifmd_data
.ifi_obytes
;
259 ifp
->tv_lastchanged
= ifp
->if_mib
.ifmd_data
.ifi_lastchange
;
261 if (gettimeofday(&new_tv
, NULL
) != 0)
262 IFSTAT_ERR(2, "error getting time of day");
263 (void)getifmibdata(ifp
->if_row
, &ifp
->if_mib
);
266 new_inb
= ifp
->if_mib
.ifmd_data
.ifi_ibytes
;
267 new_outb
= ifp
->if_mib
.ifmd_data
.ifi_obytes
;
269 /* Display interface if it's received some traffic. */
270 if (new_inb
> 0 && old_inb
== 0) {
272 we_need_to_sort_interface_list
++;
276 * The rest is pretty trivial. Calculate the new values
277 * for our current traffic rates, and while we're there,
278 * see if we have new peak rates.
281 timersub(&new_tv
, &old_tv
, &tv
);
282 elapsed
= tv
.tv_sec
+ (tv
.tv_usec
* 1e-6);
284 ifp
->if_in_curtraffic
= new_inb
- old_inb
;
285 ifp
->if_out_curtraffic
= new_outb
- old_outb
;
288 * Rather than divide by the time specified on the comm-
289 * and line, we divide by ``elapsed'' as this is likely
290 * to be more accurate.
292 ifp
->if_in_curtraffic
/= elapsed
;
293 ifp
->if_out_curtraffic
/= elapsed
;
295 if (ifp
->if_in_curtraffic
> ifp
->if_in_traffic_peak
)
296 ifp
->if_in_traffic_peak
= ifp
->if_in_curtraffic
;
298 if (ifp
->if_out_curtraffic
> ifp
->if_out_traffic_peak
)
299 ifp
->if_out_traffic_peak
= ifp
->if_out_curtraffic
;
301 ifp
->tv
.tv_sec
= new_tv
.tv_sec
;
302 ifp
->tv
.tv_usec
= new_tv
.tv_usec
;
308 if (we_need_to_sort_interface_list
)
309 sort_interface_list();
315 * We want to right justify our interface names against the first column
316 * (first sixteen or so characters), so we need to do some alignment.
319 right_align_string(if_stat_t
*ifp
)
321 int str_len
= 0, pad_len
= 0;
322 char *newstr
= NULL
, *ptr
= NULL
;
324 if (ifp
== NULL
|| ifp
->if_mib
.ifmd_name
== NULL
)
327 /* string length + '\0' */
328 str_len
= strlen(ifp
->if_mib
.ifmd_name
)+1;
329 pad_len
= IF_NAMESIZE
-(str_len
);
331 newstr
= ifp
->if_name
;
332 ptr
= newstr
+ pad_len
;
333 (void)memset((void *)newstr
, (int)' ', IF_NAMESIZE
);
334 (void)strncpy(ptr
, (const char *)&ifp
->if_mib
.ifmd_name
,
342 * This function iterates through our list of interfaces, identifying
343 * those that are to be displayed (ifp->display = 1). For each interf-
344 * rface that we're displaying, we generate an appropriate position for
345 * it on the screen (ifp->row).
347 * This function is called any time a change is made to an interface's
351 sort_interface_list(void)
357 SLIST_FOREACH(ifp
, &curlist
, link
) {
361 TopSection2
= TopSection1
+ y
+ 4;
362 TopSection3
= TopSection2
+ y
+ 4;
371 static int name
[] = { CTL_NET
,
377 datalen
= sizeof(data
);
378 if (sysctl(name
, 5, (void *)&data
, (size_t *)&datalen
, NULL
,
380 IFSTAT_ERR(1, "sysctl error");
385 getifmibdata(int row
, struct ifmibdata
*data
)
388 static int name
[] = { CTL_NET
,
394 datalen
= sizeof(*data
);
397 if ((sysctl(name
, 6, (void *)data
, (size_t *)&datalen
, NULL
,
398 (size_t)0) != 0) && (errno
!= ENOENT
))
399 IFSTAT_ERR(2, "sysctl error getting interface data");
405 struct pfioc_altq pa
;
406 struct pfioc_qstats pq
;
413 bzero(&pa
, sizeof(pa
));
414 bzero(&pq
, sizeof(pq
));
416 if (ioctl(pf_fd
, DIOCGETALTQS
, &pa
))
419 for (i
= 0; i
< n
; ++i
) {
421 if (ioctl(pf_fd
, DIOCGETALTQ
, &pa
))
423 if (pa
.altq
.qid
<= 0)
426 SLIST_FOREACH(p
, &curlist
, link
) {
427 if (strcmp(pa
.altq
.ifname
, p
->if_mib
.ifmd_name
) == 0)
432 SLIST_FOREACH(q
, &p
->queues
, link
) {
433 if (strcmp(pa
.altq
.qname
, q
->pa
.altq
.qname
) == 0)
437 q
= calloc(1, sizeof(*q
));
439 SLIST_INSERT_HEAD(&p
->queues
, q
, link
);
441 q
->pa
.ticket
= pa
.ticket
;
443 q
->ostats
= q
->nstats
;
445 q
->pq
.ticket
= q
->pa
.ticket
;
446 q
->pq
.buf
= &q
->nstats
;
447 q
->pq
.nbytes
= sizeof(q
->nstats
);
448 if (ioctl(pf_fd
, DIOCGETQSTATS
, &q
->pq
) < 0) {
449 SLIST_REMOVE(&p
->queues
, q
, queue_stats
, link
);
452 SLIST_FOREACH(qc
, &qcols
, link
) {
453 if (strcmp(q
->pa
.altq
.qname
, qc
->qname
) == 0)
457 qc
= calloc(1, sizeof(*qc
));
458 qc
->qname
= strdup(q
->pa
.altq
.qname
);
460 SLIST_INSERT_HEAD(&qcols
, qc
, link
);
468 print_altq(if_stat_t
*p
, queue_stats_t
*q
)
473 uint64_t drop_bytes __unused
;
476 switch(q
->pa
.altq
.scheduler
) {
478 xmit_pkts
= q
->nstats
.cbq
.xmit_cnt
.packets
;
479 xmit_bytes
= q
->nstats
.cbq
.xmit_cnt
.bytes
;
480 drop_pkts
= q
->nstats
.cbq
.drop_cnt
.packets
;
481 drop_bytes
= q
->nstats
.cbq
.drop_cnt
.bytes
;
482 xmit_pkts
-= q
->ostats
.cbq
.xmit_cnt
.packets
;
483 xmit_bytes
-= q
->ostats
.cbq
.xmit_cnt
.bytes
;
484 drop_pkts
-= q
->ostats
.cbq
.drop_cnt
.packets
;
485 drop_bytes
-= q
->ostats
.cbq
.drop_cnt
.bytes
;
489 xmit_pkts
= q
->nstats
.priq
.xmitcnt
.packets
;
490 xmit_bytes
= q
->nstats
.priq
.xmitcnt
.bytes
;
491 drop_pkts
= q
->nstats
.priq
.dropcnt
.packets
;
492 drop_bytes
= q
->nstats
.priq
.dropcnt
.bytes
;
493 xmit_pkts
-= q
->ostats
.priq
.xmitcnt
.packets
;
494 xmit_bytes
-= q
->ostats
.priq
.xmitcnt
.bytes
;
495 drop_pkts
-= q
->ostats
.priq
.dropcnt
.packets
;
496 drop_bytes
-= q
->ostats
.priq
.dropcnt
.bytes
;
497 qlen
= q
->nstats
.priq
.qlength
;
500 xmit_pkts
= q
->nstats
.hfsc
.xmit_cnt
.packets
;
501 xmit_bytes
= q
->nstats
.hfsc
.xmit_cnt
.bytes
;
502 drop_pkts
= q
->nstats
.hfsc
.drop_cnt
.packets
;
503 drop_bytes
= q
->nstats
.hfsc
.drop_cnt
.bytes
;
504 xmit_pkts
-= q
->ostats
.hfsc
.xmit_cnt
.packets
;
505 xmit_bytes
-= q
->ostats
.hfsc
.xmit_cnt
.bytes
;
506 drop_pkts
-= q
->ostats
.hfsc
.drop_cnt
.packets
;
507 drop_bytes
-= q
->ostats
.hfsc
.drop_cnt
.bytes
;
508 qlen
= q
->nstats
.hfsc
.qlength
;
511 xmit_pkts
= q
->nstats
.fairq
.xmit_cnt
.packets
;
512 xmit_bytes
= q
->nstats
.fairq
.xmit_cnt
.bytes
;
513 drop_pkts
= q
->nstats
.fairq
.drop_cnt
.packets
;
514 drop_bytes
= q
->nstats
.fairq
.drop_cnt
.bytes
;
515 xmit_pkts
-= q
->ostats
.fairq
.xmit_cnt
.packets
;
516 xmit_bytes
-= q
->ostats
.fairq
.xmit_cnt
.bytes
;
517 drop_pkts
-= q
->ostats
.fairq
.drop_cnt
.packets
;
518 drop_bytes
-= q
->ostats
.fairq
.drop_cnt
.bytes
;
519 qlen
= q
->nstats
.fairq
.qlength
;
530 mvprintw(TopSection1
+ p
->row
,
531 TOPQSTART
+ q
->qc
->col
* TOPQWIDTH
- 1,
534 mvprintw(TopSection1
+ p
->row
,
535 TOPQSTART
+ q
->qc
->col
* TOPQWIDTH
- 1,
536 "%10jd", (intmax_t)xmit_pkts
);
539 mvprintw(TopSection2
+ p
->row
,
540 TOPQSTART
+ q
->qc
->col
* TOPQWIDTH
- 1,
543 mvprintw(TopSection2
+ p
->row
,
544 TOPQSTART
+ q
->qc
->col
* TOPQWIDTH
- 1,
545 "%10jd", (intmax_t)xmit_bytes
);
547 mvprintw(TopSection3
+ p
->row
,
548 TOPQSTART
+ q
->qc
->col
* TOPQWIDTH
- 1,
549 "%10jd", (intmax_t)drop_pkts
);
551 mvprintw(TopSection3
+ p
->row
,
552 TOPQSTART
+ q
->qc
->col
* TOPQWIDTH
- 1,
553 "%9jdQ", (intmax_t)qlen
);
555 mvprintw(TopSection3
+ p
->row
,
556 TOPQSTART
+ q
->qc
->col
* TOPQWIDTH
- 1,
561 cmdaltqs(const char *cmd
, char *args
)
565 retval
= ifcmd(cmd
, args
);
566 /* ifcmd() returns 1 on success */