1/*	$NetBSD: qdisc_rio.c,v 1.5 2006/10/12 19:59:13 peter Exp $	*/
2/*	$KAME: qdisc_rio.c,v 1.7 2004/01/22 09:31:24 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_red.h>
37#include <altq/altq_rio.h>
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <string.h>
43#include <signal.h>
44#include <errno.h>
45#include <err.h>
46
47#include "altqstat.h"
48
49static int avg_scale = 4096;	/* default fixed-point scale */
50
51void
52rio_stat_loop(int fd, const char *ifname, int count, int interval)
53{
54	struct rio_stats rio_stats;
55	struct timeval cur_time, last_time;
56	u_int64_t last_bytes[3];
57	double sec;
58	int cnt = count;
59	sigset_t		omask;
60
61	memset(&last_bytes, 0, sizeof last_bytes);	/* XXX gcc */
62	bzero(&rio_stats, sizeof(rio_stats));
63	strlcpy(rio_stats.iface.rio_ifname, ifname,
64		sizeof(rio_stats.iface.rio_ifname));
65
66	gettimeofday(&last_time, NULL);
67	last_time.tv_sec -= interval;
68
69	for (;;) {
70		if (ioctl(fd, RIO_GETSTATS, &rio_stats) < 0)
71			err(1, "ioctl RIO_GETSTATS");
72
73		gettimeofday(&cur_time, NULL);
74		sec = calc_interval(&cur_time, &last_time);
75
76		printf("weight:%d q_limit:%d\n",
77		       rio_stats.weight, rio_stats.q_limit);
78
79		printf("\t\t\tLOW DP\t\tMEDIUM DP\t\tHIGH DP\n");
80
81		printf("thresh (prob):\t\t[%d,%d](1/%d)\t[%d,%d](1/%d)\t\t[%d,%d](1/%d)\n",
82		       rio_stats.q_params[0].th_min,
83		       rio_stats.q_params[0].th_max,
84		       rio_stats.q_params[0].inv_pmax,
85		       rio_stats.q_params[1].th_min,
86		       rio_stats.q_params[1].th_max,
87		       rio_stats.q_params[1].inv_pmax,
88		       rio_stats.q_params[2].th_min,
89		       rio_stats.q_params[2].th_max,
90		       rio_stats.q_params[2].inv_pmax);
91		printf("qlen (avg):\t\t%d (%.2f)\t%d (%.2f)\t\t%d (%.2f)\n",
92		       rio_stats.q_len[0],
93		       ((double)rio_stats.q_stats[0].q_avg)/(double)avg_scale,
94		       rio_stats.q_len[1],
95		       ((double)rio_stats.q_stats[1].q_avg)/(double)avg_scale,
96		       rio_stats.q_len[2],
97		       ((double)rio_stats.q_stats[2].q_avg)/(double)avg_scale);
98		printf("xmit (drop) pkts:\t%llu (%llu)\t\t%llu (%llu)\t\t\t%llu (%llu)\n",
99		       (ull)rio_stats.q_stats[0].xmit_cnt.packets,
100		       (ull)rio_stats.q_stats[0].drop_cnt.packets,
101		       (ull)rio_stats.q_stats[1].xmit_cnt.packets,
102		       (ull)rio_stats.q_stats[1].drop_cnt.packets,
103		       (ull)rio_stats.q_stats[2].xmit_cnt.packets,
104		       (ull)rio_stats.q_stats[2].drop_cnt.packets);
105		printf("(forced:early):\t\t(%u:%u)\t\t(%u:%u)\t\t\t(%u:%u)\n",
106		       rio_stats.q_stats[0].drop_forced,
107		       rio_stats.q_stats[0].drop_unforced,
108		       rio_stats.q_stats[1].drop_forced,
109		       rio_stats.q_stats[1].drop_unforced,
110		       rio_stats.q_stats[2].drop_forced,
111		       rio_stats.q_stats[2].drop_unforced);
112		if (rio_stats.q_stats[0].marked_packets != 0
113		    || rio_stats.q_stats[1].marked_packets != 0
114		    || rio_stats.q_stats[2].marked_packets != 0)
115			printf("marked:\t\t\t%u\t\t%u\t\t\t%u\n",
116			       rio_stats.q_stats[0].marked_packets,
117			       rio_stats.q_stats[1].marked_packets,
118			       rio_stats.q_stats[2].marked_packets);
119		printf("throughput:\t\t%sbps\t%sbps\t\t%sbps\n\n",
120		       rate2str(calc_rate(rio_stats.q_stats[0].xmit_cnt.bytes,
121					  last_bytes[0], sec)),
122		       rate2str(calc_rate(rio_stats.q_stats[1].xmit_cnt.bytes,
123					  last_bytes[1], sec)),
124		       rate2str(calc_rate(rio_stats.q_stats[2].xmit_cnt.bytes,
125					  last_bytes[2], sec)));
126
127		last_bytes[0] = rio_stats.q_stats[0].xmit_cnt.bytes;
128		last_bytes[1] = rio_stats.q_stats[1].xmit_cnt.bytes;
129		last_bytes[2] = rio_stats.q_stats[2].xmit_cnt.bytes;
130		last_time = cur_time;
131
132		if (count != 0 && --cnt == 0)
133			break;
134
135		/* wait for alarm signal */
136		if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
137			sigsuspend(&omask);
138	}
139}
140
141int
142print_riostats(struct redstats *rp)
143{
144	int dp;
145
146	for (dp = 0; dp < RIO_NDROPPREC; dp++)
147		printf("     RIO[%d] q_avg:%.2f xmit:%llu (forced: %u early:%u marked:%u)\n",
148		       dp,
149		       ((double)rp[dp].q_avg)/(double)avg_scale,
150		       (ull)rp[dp].xmit_cnt.packets,
151		       rp[dp].drop_forced,
152		       rp[dp].drop_unforced,
153		       rp[dp].marked_packets);
154	return 0;
155}
156