1 /* $NetBSD: altqstat.c,v 1.6 2006/10/12 19:59:13 peter Exp $ */
2 /* $KAME: altqstat.c,v 1.8 2002/10/27 03:19:35 kjc Exp $ */
4 * Copyright (C) 1999-2000
5 * Sony Computer Science Laboratories, Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
31 #include <sys/fcntl.h>
44 #include "quip_client.h"
47 #define DEV_PATH "/dev/altq"
53 char *interface
= NULL
;
54 char *qdisc_name
= NULL
;
56 stat_loop_t
*stat_loop
;
58 static void sig_handler(int);
59 static void alrm_handler(int);
60 static void usage(void);
67 snprintf(buf
, sizeof buf
, "Exiting on signal %d\n", sig
);
68 write(STDERR_FILENO
, buf
, strlen(buf
));
72 if (qdisc_name
!= NULL
&& strcmp(qdisc_name
, "wfq") == 0)
73 endwin(); /* wfqstat uses curses */
87 fprintf(stderr
, "usage: altqstat [-enrs] [-c count] [-w wait] [-i interface|-I input_interface]\n");
92 main (int argc
, char **argv
)
98 char device
[64], qname
[64], input
[32];
100 while ((ch
= getopt(argc
, argv
, "I:c:ei:nrsw:")) != -1) {
103 snprintf(input
, sizeof(input
), "_%s", optarg
);
107 count
= atoi(optarg
);
109 errx(1, "Please supply a count value bigger than 0.");
128 ival
= strtod(optarg
, NULL
);
136 signal(SIGINT
, sig_handler
);
137 signal(SIGTERM
, sig_handler
);
138 signal(SIGPIPE
, sig_handler
);
139 signal(SIGALRM
, alrm_handler
);
141 if (no_server
== 0) {
142 if (quip_openserver() < 0 && interface
== NULL
)
143 errx(1, "you have to specify interface!");
154 errx(1, "no server (-n) can't be set for show config (-s)!");
160 interface
= quip_selectinterface(interface
);
161 if (interface
== NULL
)
162 errx(1, "no interface found!");
164 qtype
= ifname2qdisc(interface
, qname
);
166 errx(1, "altq is not attached on %s!", interface
);
170 stat_loop
= qdisc2stat_loop(qdisc_name
);
171 if (stat_loop
== NULL
)
172 errx(1, "qdisc %s is not supported!", qdisc_name
);
174 printf("%s: %s on interface %s\n",
175 argv
[0], qdisc_name
, interface
);
177 snprintf(device
, sizeof(device
), "%s/%s", DEV_PATH
, qdisc_name
);
178 if ((qdiscfd
= open(device
, O_RDONLY
)) < 0)
179 err(1, "can't open %s", device
);
181 interval
= (int)(ival
* 1000.0);
182 it
.it_interval
.tv_sec
= interval
/ 1000;
183 it
.it_interval
.tv_usec
= interval
% 1000 * 1000;
184 it
.it_value
= it
.it_interval
;
185 setitimer(ITIMER_REAL
, &it
, NULL
);
187 (*stat_loop
)(qdiscfd
, interface
, count
, (int)ival
);
192 /* calculate interval in sec */
194 calc_interval(struct timeval
*cur_time
, struct timeval
*last_time
)
198 sec
= (double)(cur_time
->tv_sec
- last_time
->tv_sec
) +
199 (double)(cur_time
->tv_usec
- last_time
->tv_usec
) / 1000000;
204 /* calculate rate in bps */
206 calc_rate(u_int64_t new_bytes
, u_int64_t last_bytes
, double interval
)
210 rate
= (double)(new_bytes
- last_bytes
) * 8 / interval
;
214 /* calculate packets in second */
216 calc_pps(u_int64_t new_pkts
, u_int64_t last_pkts
, double interval
)
220 pps
= (double)(new_pkts
- last_pkts
) / interval
;
225 #define RATESTR_MAX 16
227 rate2str(double rate
)
230 static char r2sbuf
[R2S_BUFS
][RATESTR_MAX
]; /* ring bufer */
238 snprintf(buf
, RATESTR_MAX
, "0");
239 else if (rate
>= 1000000.0)
240 snprintf(buf
, RATESTR_MAX
, "%.2fM", rate
/ 1000000.0);
242 snprintf(buf
, RATESTR_MAX
, "%.2fk", rate
/ 1000.0);