1193323Sed/*	$NetBSD: qdisc_fifoq.c,v 1.4 2006/10/12 19:59:13 peter Exp $	*/
2193323Sed/*	$KAME: qdisc_fifoq.c,v 1.6 2002/11/08 06:36:18 kjc Exp $	*/
3193323Sed/*
4193323Sed * Copyright (C) 1999-2000
5193323Sed *	Sony Computer Science Laboratories, Inc.  All rights reserved.
6193323Sed *
7193323Sed * Redistribution and use in source and binary forms, with or without
8193323Sed * modification, are permitted provided that the following conditions
9193323Sed * are met:
10193323Sed * 1. Redistributions of source code must retain the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer.
12193323Sed * 2. Redistributions in binary form must reproduce the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer in the
14193323Sed *    documentation and/or other materials provided with the distribution.
15193323Sed *
16193323Sed * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26193323Sed * SUCH DAMAGE.
27198090Srdivacky */
28193323Sed
29193323Sed#include <sys/param.h>
30193323Sed#include <sys/ioctl.h>
31193323Sed#include <sys/time.h>
32198090Srdivacky#include <sys/socket.h>
33193323Sed#include <net/if.h>
34193323Sed#include <netinet/in.h>
35193323Sed#include <altq/altq.h>
36193323Sed#include <altq/altq_fifoq.h>
37193323Sed
38193323Sed#include <stdio.h>
39193323Sed#include <stdlib.h>
40193323Sed#include <unistd.h>
41193323Sed#include <string.h>
42193323Sed#include <signal.h>
43193323Sed#include <errno.h>
44193323Sed#include <err.h>
45193323Sed
46193323Sed#include "altqstat.h"
47193323Sed
48193323Sedvoid
49202878Srdivackyfifoq_stat_loop(int fd, const char *ifname, int count, int interval)
50202878Srdivacky{
51202878Srdivacky	struct fifoq_getstats get_stats;
52202878Srdivacky	struct timeval cur_time, last_time;
53202878Srdivacky	u_int64_t last_bytes;
54193323Sed	double sec;
55193323Sed	int cnt = count;
56193323Sed	sigset_t		omask;
57193323Sed
58193323Sed	strlcpy(get_stats.iface.fifoq_ifname, ifname,
59193323Sed		sizeof(get_stats.iface.fifoq_ifname));
60198892Srdivacky
61193323Sed	gettimeofday(&last_time, NULL);
62193323Sed	last_time.tv_sec -= interval;
63193323Sed	last_bytes = 0;
64193323Sed
65193323Sed	for (;;) {
66193323Sed		if (ioctl(fd, FIFOQ_GETSTATS, &get_stats) < 0)
67193323Sed			err(1, "ioctl FIFOQ_GETSTATS");
68193323Sed
69193323Sed		gettimeofday(&cur_time, NULL);
70193323Sed		sec = calc_interval(&cur_time, &last_time);
71193323Sed
72193323Sed		printf(" q_len:%d q_limit:%d period:%u\n",
73193323Sed		       get_stats.q_len, get_stats.q_limit, get_stats.period);
74193323Sed		printf(" xmit:%llu pkts (%llu bytes) drop:%llu pkts (%llu bytes)\n",
75193323Sed		       (ull)get_stats.xmit_cnt.packets,
76193323Sed		       (ull)get_stats.xmit_cnt.bytes,
77193323Sed		       (ull)get_stats.drop_cnt.packets,
78193323Sed		       (ull)get_stats.drop_cnt.bytes);
79193323Sed		printf(" throughput: %sbps\n",
80193323Sed		       rate2str(calc_rate(get_stats.xmit_cnt.bytes,
81193323Sed					  last_bytes, sec)));
82193323Sed
83193323Sed		last_bytes = get_stats.xmit_cnt.bytes;
84193323Sed		last_time = cur_time;
85193323Sed
86193323Sed		if (count != 0 && --cnt == 0)
87193323Sed			break;
88193323Sed
89193323Sed		/* wait for alarm signal */
90193323Sed		if (sigprocmask(SIG_BLOCK, NULL, &omask) == 0)
91193323Sed			sigsuspend(&omask);
92193323Sed	}
93193323Sed}
94193323Sed