xref: /netbsd-src/usr.sbin/altq/altqstat/qdisc_rio.c (revision ba31dc0ca8176faff9f4d91a768d3d97468abbd5)
1*ba31dc0cSpeter /*	$NetBSD: qdisc_rio.c,v 1.6 2006/10/28 11:43:02 peter Exp $	*/
2dd191f37Speter /*	$KAME: qdisc_rio.c,v 1.7 2004/01/22 09:31:24 kjc Exp $	*/
38726857eSthorpej /*
48726857eSthorpej  * Copyright (C) 1999-2000
58726857eSthorpej  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
68726857eSthorpej  *
78726857eSthorpej  * Redistribution and use in source and binary forms, with or without
88726857eSthorpej  * modification, are permitted provided that the following conditions
98726857eSthorpej  * are met:
108726857eSthorpej  * 1. Redistributions of source code must retain the above copyright
118726857eSthorpej  *    notice, this list of conditions and the following disclaimer.
128726857eSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
138726857eSthorpej  *    notice, this list of conditions and the following disclaimer in the
148726857eSthorpej  *    documentation and/or other materials provided with the distribution.
158726857eSthorpej  *
168726857eSthorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
178726857eSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188726857eSthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198726857eSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
208726857eSthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218726857eSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228726857eSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238726857eSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248726857eSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258726857eSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268726857eSthorpej  * SUCH DAMAGE.
278726857eSthorpej  */
288726857eSthorpej 
298726857eSthorpej #include <sys/param.h>
308726857eSthorpej #include <sys/ioctl.h>
318726857eSthorpej #include <sys/time.h>
328726857eSthorpej #include <sys/socket.h>
338726857eSthorpej #include <net/if.h>
348726857eSthorpej #include <netinet/in.h>
358726857eSthorpej #include <altq/altq.h>
368726857eSthorpej #include <altq/altq_red.h>
378726857eSthorpej #include <altq/altq_rio.h>
388726857eSthorpej 
398726857eSthorpej #include <stdio.h>
408726857eSthorpej #include <stdlib.h>
418726857eSthorpej #include <unistd.h>
428726857eSthorpej #include <string.h>
43dd191f37Speter #include <signal.h>
448726857eSthorpej #include <errno.h>
458726857eSthorpej #include <err.h>
468726857eSthorpej 
478726857eSthorpej #include "altqstat.h"
488726857eSthorpej 
498726857eSthorpej static int avg_scale = 4096;	/* default fixed-point scale */
508726857eSthorpej 
518726857eSthorpej void
rio_stat_loop(int fd,const char * ifname,int count,int interval)528726857eSthorpej rio_stat_loop(int fd, const char *ifname, int count, int interval)
538726857eSthorpej {
548726857eSthorpej 	struct rio_stats rio_stats;
558726857eSthorpej 	struct timeval cur_time, last_time;
568726857eSthorpej 	u_int64_t last_bytes[3];
578726857eSthorpej 	double sec;
588726857eSthorpej 	int cnt = count;
59dd191f37Speter 	sigset_t		omask;
608726857eSthorpej 
61084c0528Smrg 	memset(&last_bytes, 0, sizeof last_bytes);	/* XXX gcc */
628726857eSthorpej 	bzero(&rio_stats, sizeof(rio_stats));
638b8734d2Sitojun 	strlcpy(rio_stats.iface.rio_ifname, ifname,
648b8734d2Sitojun 		sizeof(rio_stats.iface.rio_ifname));
658726857eSthorpej 
668726857eSthorpej 	gettimeofday(&last_time, NULL);
678726857eSthorpej 	last_time.tv_sec -= interval;
688726857eSthorpej 
69*ba31dc0cSpeter 	for (;;) {
708726857eSthorpej 		if (ioctl(fd, RIO_GETSTATS, &rio_stats) < 0)
718726857eSthorpej 			err(1, "ioctl RIO_GETSTATS");
728726857eSthorpej 
738726857eSthorpej 		gettimeofday(&cur_time, NULL);
748726857eSthorpej 		sec = calc_interval(&cur_time, &last_time);
758726857eSthorpej 
768726857eSthorpej 		printf("weight:%d q_limit:%d\n",
778726857eSthorpej 		       rio_stats.weight, rio_stats.q_limit);
788726857eSthorpej 
798726857eSthorpej 		printf("\t\t\tLOW DP\t\tMEDIUM DP\t\tHIGH DP\n");
808726857eSthorpej 
81dd191f37Speter 		printf("thresh (prob):\t\t[%d,%d](1/%d)\t[%d,%d](1/%d)\t\t[%d,%d](1/%d)\n",
828726857eSthorpej 		       rio_stats.q_params[0].th_min,
838726857eSthorpej 		       rio_stats.q_params[0].th_max,
848726857eSthorpej 		       rio_stats.q_params[0].inv_pmax,
858726857eSthorpej 		       rio_stats.q_params[1].th_min,
868726857eSthorpej 		       rio_stats.q_params[1].th_max,
878726857eSthorpej 		       rio_stats.q_params[1].inv_pmax,
888726857eSthorpej 		       rio_stats.q_params[2].th_min,
898726857eSthorpej 		       rio_stats.q_params[2].th_max,
908726857eSthorpej 		       rio_stats.q_params[2].inv_pmax);
918726857eSthorpej 		printf("qlen (avg):\t\t%d (%.2f)\t%d (%.2f)\t\t%d (%.2f)\n",
928726857eSthorpej 		       rio_stats.q_len[0],
938726857eSthorpej 		       ((double)rio_stats.q_stats[0].q_avg)/(double)avg_scale,
948726857eSthorpej 		       rio_stats.q_len[1],
958726857eSthorpej 		       ((double)rio_stats.q_stats[1].q_avg)/(double)avg_scale,
968726857eSthorpej 		       rio_stats.q_len[2],
978726857eSthorpej 		       ((double)rio_stats.q_stats[2].q_avg)/(double)avg_scale);
988726857eSthorpej 		printf("xmit (drop) pkts:\t%llu (%llu)\t\t%llu (%llu)\t\t\t%llu (%llu)\n",
998726857eSthorpej 		       (ull)rio_stats.q_stats[0].xmit_cnt.packets,
1008726857eSthorpej 		       (ull)rio_stats.q_stats[0].drop_cnt.packets,
1018726857eSthorpej 		       (ull)rio_stats.q_stats[1].xmit_cnt.packets,
1028726857eSthorpej 		       (ull)rio_stats.q_stats[1].drop_cnt.packets,
1038726857eSthorpej 		       (ull)rio_stats.q_stats[2].xmit_cnt.packets,
1048726857eSthorpej 		       (ull)rio_stats.q_stats[2].drop_cnt.packets);
1058726857eSthorpej 		printf("(forced:early):\t\t(%u:%u)\t\t(%u:%u)\t\t\t(%u:%u)\n",
1068726857eSthorpej 		       rio_stats.q_stats[0].drop_forced,
1078726857eSthorpej 		       rio_stats.q_stats[0].drop_unforced,
1088726857eSthorpej 		       rio_stats.q_stats[1].drop_forced,
1098726857eSthorpej 		       rio_stats.q_stats[1].drop_unforced,
1108726857eSthorpej 		       rio_stats.q_stats[2].drop_forced,
1118726857eSthorpej 		       rio_stats.q_stats[2].drop_unforced);
1128726857eSthorpej 		if (rio_stats.q_stats[0].marked_packets != 0
1138726857eSthorpej 		    || rio_stats.q_stats[1].marked_packets != 0
1148726857eSthorpej 		    || rio_stats.q_stats[2].marked_packets != 0)
1158726857eSthorpej 			printf("marked:\t\t\t%u\t\t%u\t\t\t%u\n",
1168726857eSthorpej 			       rio_stats.q_stats[0].marked_packets,
1178726857eSthorpej 			       rio_stats.q_stats[1].marked_packets,
1188726857eSthorpej 			       rio_stats.q_stats[2].marked_packets);
1198726857eSthorpej 		printf("throughput:\t\t%sbps\t%sbps\t\t%sbps\n\n",
1208726857eSthorpej 		       rate2str(calc_rate(rio_stats.q_stats[0].xmit_cnt.bytes,
1218726857eSthorpej 					  last_bytes[0], sec)),
1228726857eSthorpej 		       rate2str(calc_rate(rio_stats.q_stats[1].xmit_cnt.bytes,
1238726857eSthorpej 					  last_bytes[1], sec)),
1248726857eSthorpej 		       rate2str(calc_rate(rio_stats.q_stats[2].xmit_cnt.bytes,
1258726857eSthorpej 					  last_bytes[2], sec)));
1268726857eSthorpej 
1278726857eSthorpej 		last_bytes[0] = rio_stats.q_stats[0].xmit_cnt.bytes;
1288726857eSthorpej 		last_bytes[1] = rio_stats.q_stats[1].xmit_cnt.bytes;
1298b8734d2Sitojun 		last_bytes[2] = rio_stats.q_stats[2].xmit_cnt.bytes;
1308726857eSthorpej 		last_time = cur_time;
131dd191f37Speter 
132*ba31dc0cSpeter 		if (count != 0 && --cnt == 0)
133*ba31dc0cSpeter 			break;
134*ba31dc0cSpeter 
135dd191f37Speter 		/* wait for alarm signal */
136dd191f37Speter 		if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
137dd191f37Speter 			sigsuspend(&omask);
1388726857eSthorpej 	}
1398726857eSthorpej }
1408726857eSthorpej 
1418726857eSthorpej int
print_riostats(struct redstats * rp)1428726857eSthorpej print_riostats(struct redstats *rp)
1438726857eSthorpej {
1448726857eSthorpej 	int dp;
1458726857eSthorpej 
1468726857eSthorpej 	for (dp = 0; dp < RIO_NDROPPREC; dp++)
1478726857eSthorpej 		printf("     RIO[%d] q_avg:%.2f xmit:%llu (forced: %u early:%u marked:%u)\n",
1488726857eSthorpej 		       dp,
1498726857eSthorpej 		       ((double)rp[dp].q_avg)/(double)avg_scale,
1508726857eSthorpej 		       (ull)rp[dp].xmit_cnt.packets,
1518726857eSthorpej 		       rp[dp].drop_forced,
1528726857eSthorpej 		       rp[dp].drop_unforced,
1538726857eSthorpej 		       rp[dp].marked_packets);
1548726857eSthorpej 	return 0;
1558726857eSthorpej }
156