1/*	$NetBSD: qdisc_hfsc.c,v 1.5 2006/10/12 19:59:13 peter Exp $	*/
2/*	$KAME: qdisc_hfsc.c,v 1.8 2003/07/10 12:09:38 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_hfsc.h>
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <string.h>
42#include <signal.h>
43#include <math.h>
44#include <errno.h>
45#include <err.h>
46
47#include "quip_client.h"
48#include "altqstat.h"
49
50#define NCLASSES	64
51
52void
53hfsc_stat_loop(int fd, const char *ifname, int count, int interval)
54{
55	struct hfsc_classstats	stats1[NCLASSES], stats2[NCLASSES];
56	char			clnames[NCLASSES][128];
57	struct hfsc_class_stats	get_stats;
58	struct hfsc_classstats	*sp, *lp, *new, *last, *tmp;
59	struct timeval		cur_time, last_time;
60	int			i;
61	double			sec;
62	int			cnt = count;
63	sigset_t		omask;
64	u_int64_t		stattime;
65	u_int32_t		machclk_freq;
66
67	strlcpy(get_stats.iface.hfsc_ifname, ifname,
68		sizeof(get_stats.iface.hfsc_ifname));
69	new = &stats1[0];
70	last = &stats2[0];
71
72	/* invalidate class ids */
73	for (i = 0; i < NCLASSES; i++) {
74		last[i].class_id = 999999; /* XXX */
75		last[i].class_handle = HFSC_NULLCLASS_HANDLE;
76	}
77
78	for (;;) {
79		get_stats.nskip = 0;
80		get_stats.nclasses = NCLASSES;
81		get_stats.stats = new;
82
83		if (ioctl(fd, HFSC_GETSTATS, &get_stats) < 0)
84			err(1, "ioctl HFSC_GETSTATS");
85
86		gettimeofday(&cur_time, NULL);
87		sec = calc_interval(&cur_time, &last_time);
88
89		stattime = get_stats.cur_time;
90		machclk_freq = get_stats.machclk_freq;
91		printf("\ncur_time:%#llx %u classes %u packets in the tree\n",
92		    (ull)stattime,
93		    get_stats.hif_classes, get_stats.hif_packets);
94
95		for (i=0; i<get_stats.nclasses; i++) {
96			sp = &new[i];
97			lp = &last[i];
98
99			if (sp->class_id != lp->class_id) {
100				quip_chandle2name(ifname, sp->class_handle,
101				    clnames[i], sizeof(clnames[0]));
102			}
103
104			printf("[%2d %s] handle:%#x [rt %s %ums %s][ls %s %ums %s]",
105			       sp->class_id, clnames[i], sp->class_handle,
106			       rate2str((double)sp->rsc.m1), sp->rsc.d,
107			       rate2str((double)sp->rsc.m2),
108			       rate2str((double)sp->fsc.m1), sp->fsc.d,
109			       rate2str((double)sp->fsc.m2));
110			if (sp->usc.m1 != 0 || sp->usc.m2 != 0)
111				printf("[ul %s %ums %s]\n",
112				       rate2str((double)sp->usc.m1), sp->usc.d,
113				       rate2str((double)sp->usc.m2));
114			else
115				 printf("\n");
116			if (lp->class_handle != HFSC_NULLCLASS_HANDLE) {
117				printf("  measured: %sbps [rt:%s ls:%s] qlen:%2d period:%u\n",
118				    rate2str(calc_rate(sp->total, lp->total, sec)),
119				    rate2str(calc_rate(sp->cumul, lp->cumul, sec)),
120				    rate2str(calc_rate(sp->total - sp->cumul,
121					lp->total - lp->cumul, sec)),
122				    sp->qlength, sp->period);
123			}
124			printf("     packets:%llu (%llu bytes) drops:%llu (%llu bytes) \n",
125			       (ull)sp->xmit_cnt.packets,
126			       (ull)sp->xmit_cnt.bytes,
127			       (ull)sp->drop_cnt.packets,
128			       (ull)sp->drop_cnt.bytes);
129			printf("     cumul:%#llx total:%#llx\n",
130			       (ull)sp->cumul, (ull)sp->total);
131			printf("     e:%.2fus d:%.2fus f:%.2fus vt:%#llx\n",
132			    sp->e == 0 ? 0 : ((double)sp->e - (double)stattime)
133			    		     / machclk_freq * 1000000,
134			    sp->d == 0 ? 0 : ((double)sp->d - (double)stattime)
135			    		     / machclk_freq * 1000000,
136			    sp->f == 0 ? 0 : ((double)sp->f - (double)stattime)
137			    		     / machclk_freq * 1000000,
138			    (ull)sp->vt);
139			printf("     vtperiod:%u parentperiod:%u nactive:%d\n",
140			    sp->vtperiod, sp->parentperiod, sp->nactive);
141			printf("     initvt:%#llx cvtmax:%#llx vtoff:%#llx\n",
142			    (ull)sp->initvt, (ull)sp->cvtmax, (ull)sp->vtoff);
143
144			printf("     myf:%#llx cfmin:%#llx cvtmin:%#llx",
145			    (ull)sp->myf, (ull)sp->cfmin, (ull)sp->cvtmin);
146			printf(" myfadj:%#llx vtadj:%#llx\n",
147			    (ull)sp->myfadj, (ull)sp->vtadj);
148			if (sp->qtype == Q_RED)
149				print_redstats(sp->red);
150			else if (sp->qtype == Q_RIO)
151				print_riostats(sp->red);
152		}
153
154		/* swap the buffer pointers */
155		tmp = last;
156		last = new;
157		new = tmp;
158
159		last_time = cur_time;
160
161		if (count != 0 && --cnt == 0)
162			break;
163
164		/* wait for alarm signal */
165		if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
166			sigsuspend(&omask);
167	}
168}
169