Home | History | Annotate | Line # | Download | only in altqstat
      1  1.9   andvar /*	$NetBSD: altqstat.c,v 1.9 2021/07/24 21:31:39 andvar Exp $	*/
      2  1.6    peter /*	$KAME: altqstat.c,v 1.8 2002/10/27 03:19:35 kjc Exp $	*/
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (C) 1999-2000
      5  1.1  thorpej  *	Sony Computer Science Laboratories, Inc.  All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  *
     16  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     20  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  thorpej  * SUCH DAMAGE.
     27  1.1  thorpej  */
     28  1.1  thorpej 
     29  1.1  thorpej #include <sys/param.h>
     30  1.1  thorpej #include <sys/time.h>
     31  1.1  thorpej #include <sys/fcntl.h>
     32  1.1  thorpej 
     33  1.1  thorpej #include <stdio.h>
     34  1.1  thorpej #include <stdlib.h>
     35  1.1  thorpej #include <unistd.h>
     36  1.1  thorpej #include <string.h>
     37  1.1  thorpej #include <signal.h>
     38  1.1  thorpej #include <errno.h>
     39  1.1  thorpej #include <err.h>
     40  1.1  thorpej #ifndef NO_CURSES
     41  1.1  thorpej #include <curses.h>
     42  1.1  thorpej #endif
     43  1.1  thorpej 
     44  1.1  thorpej #include "quip_client.h"
     45  1.1  thorpej #include "altqstat.h"
     46  1.1  thorpej 
     47  1.1  thorpej #define DEV_PATH	"/dev/altq"
     48  1.1  thorpej 
     49  1.1  thorpej int qdiscfd = -1;
     50  1.1  thorpej int show_config = 0;
     51  1.6    peter double ival = 5.0;
     52  1.1  thorpej int no_server = 0;
     53  1.1  thorpej char *interface = NULL;
     54  1.1  thorpej char *qdisc_name = NULL;
     55  1.1  thorpej 
     56  1.1  thorpej stat_loop_t *stat_loop;
     57  1.1  thorpej 
     58  1.8    joerg __dead static void sig_handler(int);
     59  1.6    peter static void alrm_handler(int);
     60  1.8    joerg __dead static void usage(void);
     61  1.1  thorpej 
     62  1.1  thorpej static void
     63  1.1  thorpej sig_handler(int sig)
     64  1.1  thorpej {
     65  1.5   itojun 	char buf[8192];
     66  1.5   itojun 
     67  1.5   itojun 	snprintf(buf, sizeof buf, "Exiting on signal %d\n", sig);
     68  1.5   itojun 	write(STDERR_FILENO, buf, strlen(buf));
     69  1.1  thorpej 
     70  1.1  thorpej #ifndef NO_CURSES
     71  1.5   itojun 	/* XXX signal race */
     72  1.1  thorpej 	if (qdisc_name != NULL && strcmp(qdisc_name, "wfq") == 0)
     73  1.1  thorpej 		endwin();	/* wfqstat uses curses */
     74  1.1  thorpej #endif
     75  1.5   itojun 	_exit(0);
     76  1.1  thorpej }
     77  1.1  thorpej 
     78  1.6    peter static void
     79  1.6    peter alrm_handler(int sig)
     80  1.6    peter {
     81  1.6    peter 	/* nothing */
     82  1.6    peter }
     83  1.6    peter 
     84  1.1  thorpej static void
     85  1.1  thorpej usage(void)
     86  1.1  thorpej {
     87  1.1  thorpej 	fprintf(stderr, "usage: altqstat [-enrs] [-c count] [-w wait] [-i interface|-I input_interface]\n");
     88  1.1  thorpej 	exit(1);
     89  1.1  thorpej }
     90  1.1  thorpej 
     91  1.1  thorpej int
     92  1.1  thorpej main (int argc, char **argv)
     93  1.1  thorpej {
     94  1.1  thorpej 	int ch, raw_mode = 0;
     95  1.6    peter 	int qtype, interval;
     96  1.1  thorpej 	int count = 0;
     97  1.6    peter 	struct itimerval it;
     98  1.1  thorpej 	char device[64], qname[64], input[32];
     99  1.1  thorpej 
    100  1.1  thorpej 	while ((ch = getopt(argc, argv, "I:c:ei:nrsw:")) != -1) {
    101  1.1  thorpej 		switch (ch) {
    102  1.1  thorpej 		case 'I':
    103  1.2   itojun 			snprintf(input, sizeof(input), "_%s", optarg);
    104  1.1  thorpej 			interface = input;
    105  1.1  thorpej 			break;
    106  1.1  thorpej 		case 'c':
    107  1.1  thorpej 			count = atoi(optarg);
    108  1.7    peter 			if (count < 1)
    109  1.7    peter 				errx(1, "Please supply a count value bigger than 0.");
    110  1.1  thorpej 			break;
    111  1.1  thorpej 		case 'e':
    112  1.1  thorpej 			quip_echo = 1;
    113  1.1  thorpej 			break;
    114  1.1  thorpej 		case 'i':
    115  1.1  thorpej 			interface = optarg;
    116  1.1  thorpej 			break;
    117  1.1  thorpej 		case 'n':
    118  1.1  thorpej 			no_server = 1;
    119  1.1  thorpej 			break;
    120  1.1  thorpej 		case 'r':
    121  1.1  thorpej 			raw_mode = 1;
    122  1.1  thorpej 			quip_echo = 1;
    123  1.1  thorpej 			break;
    124  1.1  thorpej 		case 's':
    125  1.1  thorpej 			show_config = 1;
    126  1.1  thorpej 			break;
    127  1.1  thorpej 		case 'w':
    128  1.6    peter 			ival = strtod(optarg, NULL);
    129  1.1  thorpej 			break;
    130  1.1  thorpej 		default:
    131  1.1  thorpej 			usage();
    132  1.1  thorpej 			break;
    133  1.1  thorpej 		}
    134  1.1  thorpej 	}
    135  1.1  thorpej 
    136  1.1  thorpej 	signal(SIGINT, sig_handler);
    137  1.1  thorpej 	signal(SIGTERM, sig_handler);
    138  1.1  thorpej 	signal(SIGPIPE, sig_handler);
    139  1.6    peter 	signal(SIGALRM, alrm_handler);
    140  1.1  thorpej 
    141  1.1  thorpej 	if (no_server == 0) {
    142  1.1  thorpej 		if (quip_openserver() < 0 && interface == NULL)
    143  1.1  thorpej 			errx(1, "you have to specify interface!");
    144  1.1  thorpej 	}
    145  1.1  thorpej 
    146  1.1  thorpej 	if (raw_mode == 1) {
    147  1.1  thorpej 		quip_rawmode();
    148  1.1  thorpej 		quip_closeserver();
    149  1.1  thorpej 		exit(0);
    150  1.1  thorpej 	}
    151  1.1  thorpej 
    152  1.1  thorpej 	if (show_config) {
    153  1.1  thorpej 		if (no_server)
    154  1.1  thorpej 			errx(1, "no server (-n) can't be set for show config (-s)!");
    155  1.1  thorpej 		quip_printconfig();
    156  1.1  thorpej 		quip_closeserver();
    157  1.1  thorpej 		exit(0);
    158  1.1  thorpej 	}
    159  1.1  thorpej 
    160  1.1  thorpej 	interface = quip_selectinterface(interface);
    161  1.1  thorpej 	if (interface == NULL)
    162  1.1  thorpej 		errx(1, "no interface found!");
    163  1.1  thorpej 
    164  1.1  thorpej 	qtype = ifname2qdisc(interface, qname);
    165  1.1  thorpej 	if (qtype == 0)
    166  1.1  thorpej 		errx(1, "altq is not attached on %s!", interface);
    167  1.1  thorpej 
    168  1.1  thorpej 	qdisc_name = qname;
    169  1.1  thorpej 
    170  1.1  thorpej 	stat_loop = qdisc2stat_loop(qdisc_name);
    171  1.1  thorpej 	if (stat_loop == NULL)
    172  1.1  thorpej 		errx(1, "qdisc %s is not supported!", qdisc_name);
    173  1.1  thorpej 
    174  1.1  thorpej 	printf("%s: %s on interface %s\n",
    175  1.1  thorpej 	       argv[0], qdisc_name, interface);
    176  1.1  thorpej 
    177  1.2   itojun 	snprintf(device, sizeof(device), "%s/%s", DEV_PATH, qdisc_name);
    178  1.1  thorpej 	if ((qdiscfd = open(device, O_RDONLY)) < 0)
    179  1.1  thorpej 		err(1, "can't open %s", device);
    180  1.1  thorpej 
    181  1.6    peter 	interval = (int)(ival * 1000.0);
    182  1.6    peter 	it.it_interval.tv_sec = interval / 1000;
    183  1.6    peter 	it.it_interval.tv_usec = interval % 1000 * 1000;
    184  1.6    peter 	it.it_value = it.it_interval;
    185  1.6    peter 	setitimer(ITIMER_REAL, &it, NULL);
    186  1.6    peter 
    187  1.6    peter 	(*stat_loop)(qdiscfd, interface, count, (int)ival);
    188  1.1  thorpej 
    189  1.1  thorpej 	exit(0);
    190  1.1  thorpej }
    191  1.1  thorpej 
    192  1.1  thorpej /* calculate interval in sec */
    193  1.1  thorpej double
    194  1.1  thorpej calc_interval(struct timeval *cur_time, struct timeval *last_time)
    195  1.1  thorpej {
    196  1.1  thorpej 	double sec;
    197  1.1  thorpej 
    198  1.1  thorpej 	sec = (double)(cur_time->tv_sec - last_time->tv_sec) +
    199  1.1  thorpej 	    (double)(cur_time->tv_usec - last_time->tv_usec) / 1000000;
    200  1.1  thorpej 	return (sec);
    201  1.1  thorpej }
    202  1.1  thorpej 
    203  1.1  thorpej 
    204  1.1  thorpej /* calculate rate in bps */
    205  1.1  thorpej double
    206  1.1  thorpej calc_rate(u_int64_t new_bytes, u_int64_t last_bytes, double interval)
    207  1.1  thorpej {
    208  1.1  thorpej 	double rate;
    209  1.1  thorpej 
    210  1.1  thorpej 	rate = (double)(new_bytes - last_bytes) * 8 / interval;
    211  1.1  thorpej 	return (rate);
    212  1.1  thorpej }
    213  1.1  thorpej 
    214  1.1  thorpej /* calculate packets in second */
    215  1.1  thorpej double
    216  1.1  thorpej calc_pps(u_int64_t new_pkts, u_int64_t last_pkts, double interval)
    217  1.1  thorpej {
    218  1.1  thorpej 	double pps;
    219  1.1  thorpej 
    220  1.1  thorpej 	pps = (double)(new_pkts - last_pkts) / interval;
    221  1.1  thorpej 	return (pps);
    222  1.1  thorpej }
    223  1.1  thorpej 
    224  1.1  thorpej #define	R2S_BUFS	8
    225  1.2   itojun #define	RATESTR_MAX	16
    226  1.1  thorpej char *
    227  1.1  thorpej rate2str(double rate)
    228  1.1  thorpej {
    229  1.1  thorpej 	char *buf;
    230  1.9   andvar 	static char r2sbuf[R2S_BUFS][RATESTR_MAX];  /* ring buffer */
    231  1.1  thorpej 	static int idx = 0;
    232  1.1  thorpej 
    233  1.1  thorpej 	buf = r2sbuf[idx++];
    234  1.1  thorpej 	if (idx == R2S_BUFS)
    235  1.1  thorpej 		idx = 0;
    236  1.1  thorpej 
    237  1.1  thorpej 	if (rate == 0.0)
    238  1.2   itojun 		snprintf(buf, RATESTR_MAX, "0");
    239  1.1  thorpej 	else if (rate >= 1000000.0)
    240  1.2   itojun 		snprintf(buf, RATESTR_MAX, "%.2fM", rate / 1000000.0);
    241  1.1  thorpej 	else
    242  1.6    peter 		snprintf(buf, RATESTR_MAX, "%.2fk", rate / 1000.0);
    243  1.1  thorpej 	return (buf);
    244  1.1  thorpej }
    245