xref: /netbsd/usr.sbin/altq/altqstat/qdisc_cbq.c (revision bf9ec67e)
1 /*	$NetBSD: qdisc_cbq.c,v 1.5 2002/03/05 04:11:52 itojun Exp $	*/
2 /*	$KAME: qdisc_cbq.c,v 1.5 2001/11/07 04:56:08 kjc Exp $	*/
3 /*
4  * Copyright (C) 1999-2000
5  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY SONY CSL 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 SONY CSL 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 INTERRUPTION)
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <sys/time.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35 #include <altq/altq.h>
36 #include <altq/altq_cbq.h>
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <math.h>
43 #include <errno.h>
44 #include <err.h>
45 
46 #include "quip_client.h"
47 #include "altqstat.h"
48 
49 #define NCLASSES	64
50 
51 #ifndef RM_FILTER_GAIN
52 #define	RM_FILTER_GAIN	5	/* log2 of gain, e.g., 5 => 31/32 */
53 #endif
54 #ifndef RM_POWER
55 #define RM_POWER	(1 << RM_FILTER_GAIN)
56 #endif
57 
58 void
59 cbq_stat_loop(int fd, const char *ifname, int count, int interval)
60 {
61 	class_stats_t stats1[NCLASSES], stats2[NCLASSES];
62 	char clnames[NCLASSES][128];
63 	u_long clhandles[NCLASSES];
64 	struct cbq_getstats	get_stats;
65 	class_stats_t		*sp, *lp, *new, *last, *tmp;
66 	struct timeval		cur_time, last_time;
67 	int			i;
68 	double			flow_bps, sec;
69 	int cnt = count;
70 
71 	strlcpy(get_stats.iface.cbq_ifacename, ifname,
72 		sizeof(get_stats.iface.cbq_ifacename));
73 	new = &stats1[0];
74 	last = &stats2[0];
75 
76 	for (i = 0; i < NCLASSES; i++)
77 	    clhandles[i] = NULL_CLASS_HANDLE;
78 
79 	while (count == 0 || cnt-- > 0) {
80 		get_stats.nclasses = NCLASSES;
81 		get_stats.stats = new;
82 		if (ioctl(fd, CBQ_GETSTATS, &get_stats) < 0)
83 			err(1, "ioctl CBQ_GETSTATS");
84 
85 		gettimeofday(&cur_time, NULL);
86 		sec = calc_interval(&cur_time, &last_time);
87 
88 		for (i=0; i<get_stats.nclasses; i++) {
89 			sp = &new[i];
90 			lp = &last[i];
91 
92 			if (sp->handle != clhandles[i]) {
93 				quip_chandle2name(ifname, sp->handle,
94 						  clnames[i], sizeof(clnames[0]));
95 				clhandles[i] = sp->handle;
96 				continue;
97 			}
98 
99 			switch (sp->handle) {
100 			case ROOT_CLASS_HANDLE:
101 				printf("Root Class for Interface %s: %s\n",
102 				       ifname, clnames[i]);
103 				break;
104 			case DEFAULT_CLASS_HANDLE:
105 				printf("Default Class for Interface %s: %s\n",
106 				       ifname, clnames[i]);
107 				break;
108 			case CTL_CLASS_HANDLE:
109 				printf("Ctl Class for Interface %s: %s\n",
110 				       ifname, clnames[i]);
111 				break;
112 			default:
113 				printf("Class %d on Interface %s: %s\n",
114 				       sp->handle, ifname, clnames[i]);
115 				break;
116 			}
117 
118 			flow_bps = 8.0 / (double)sp->ns_per_byte
119 			    * 1000*1000*1000;
120 
121 			printf("\tpriority: %d depth: %d",
122 			       sp->priority, sp->depth);
123 			printf(" offtime: %d [us] wrr_allot: %d bytes\n",
124 			       sp->offtime, sp->wrr_allot);
125 			printf("\tnsPerByte: %d", sp->ns_per_byte);
126 			printf("\t(%sbps),", rate2str(flow_bps));
127 			printf("\tMeasured: %s [bps]\n",
128 			       rate2str(calc_rate(sp->xmit_cnt.bytes,
129 						  lp->xmit_cnt.bytes, sec)));
130 			printf("\tpkts: %llu,\tbytes: %llu\n",
131 			       (ull)sp->xmit_cnt.packets,
132 			       (ull)sp->xmit_cnt.bytes);
133 			printf("\tovers: %u,\toveractions: %u\n",
134 			       sp->over, sp->overactions);
135 			printf("\tborrows: %u,\tdelays: %u\n",
136 			       sp->borrows, sp->delays);
137 			printf("\tdrops: %llu,\tdrop_bytes: %llu\n",
138 			       (ull)sp->drop_cnt.packets,
139 			       (ull)sp->drop_cnt.bytes);
140 			if (sp->qtype == Q_RED)
141 				print_redstats(sp->red);
142 			else if (sp->qtype == Q_RIO)
143 				print_riostats(sp->red);
144 
145 			printf("\tQCount: %d,\t(qmax: %d)\n",
146 			       sp->qcnt, sp->qmax);
147 			printf("\tAvgIdle: %d [us],\t(maxidle: %d minidle: %d [us])\n",
148 			       sp->avgidle >> RM_FILTER_GAIN,
149 			       sp->maxidle >> RM_FILTER_GAIN,
150 			       sp->minidle / RM_POWER);
151 		}
152 
153 		/* swap the buffer pointers */
154 		tmp = last;
155 		last = new;
156 		new = tmp;
157 
158 		last_time = cur_time;
159 		sleep(interval);
160 	}
161 }
162