altqstat.c revision 1.5 1 1.5 itojun /* $NetBSD: altqstat.c,v 1.5 2002/03/05 04:11:52 itojun Exp $ */
2 1.5 itojun /* $KAME: altqstat.c,v 1.7 2001/11/19 09:14:22 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.1 thorpej int interval = 5;
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.2 itojun static void sig_handler(int);
59 1.1 thorpej static void usage(void);
60 1.1 thorpej
61 1.1 thorpej static void
62 1.1 thorpej sig_handler(int sig)
63 1.1 thorpej {
64 1.5 itojun char buf[8192];
65 1.5 itojun
66 1.5 itojun snprintf(buf, sizeof buf, "Exiting on signal %d\n", sig);
67 1.5 itojun write(STDERR_FILENO, buf, strlen(buf));
68 1.1 thorpej
69 1.1 thorpej #ifndef NO_CURSES
70 1.5 itojun /* XXX signal race */
71 1.1 thorpej if (qdisc_name != NULL && strcmp(qdisc_name, "wfq") == 0)
72 1.1 thorpej endwin(); /* wfqstat uses curses */
73 1.1 thorpej #endif
74 1.5 itojun _exit(0);
75 1.1 thorpej }
76 1.1 thorpej
77 1.1 thorpej static void
78 1.1 thorpej usage(void)
79 1.1 thorpej {
80 1.1 thorpej fprintf(stderr, "usage: altqstat [-enrs] [-c count] [-w wait] [-i interface|-I input_interface]\n");
81 1.1 thorpej exit(1);
82 1.1 thorpej }
83 1.1 thorpej
84 1.1 thorpej int
85 1.1 thorpej main (int argc, char **argv)
86 1.1 thorpej {
87 1.1 thorpej int ch, raw_mode = 0;
88 1.1 thorpej int qtype;
89 1.1 thorpej int count = 0;
90 1.1 thorpej char device[64], qname[64], input[32];
91 1.1 thorpej
92 1.1 thorpej while ((ch = getopt(argc, argv, "I:c:ei:nrsw:")) != -1) {
93 1.1 thorpej switch (ch) {
94 1.1 thorpej case 'I':
95 1.2 itojun snprintf(input, sizeof(input), "_%s", optarg);
96 1.1 thorpej interface = input;
97 1.1 thorpej break;
98 1.1 thorpej case 'c':
99 1.1 thorpej count = atoi(optarg);
100 1.1 thorpej break;
101 1.1 thorpej case 'e':
102 1.1 thorpej quip_echo = 1;
103 1.1 thorpej break;
104 1.1 thorpej case 'i':
105 1.1 thorpej interface = optarg;
106 1.1 thorpej break;
107 1.1 thorpej case 'n':
108 1.1 thorpej no_server = 1;
109 1.1 thorpej break;
110 1.1 thorpej case 'r':
111 1.1 thorpej raw_mode = 1;
112 1.1 thorpej quip_echo = 1;
113 1.1 thorpej break;
114 1.1 thorpej case 's':
115 1.1 thorpej show_config = 1;
116 1.1 thorpej break;
117 1.1 thorpej case 'w':
118 1.1 thorpej interval = atoi(optarg);
119 1.1 thorpej break;
120 1.1 thorpej default:
121 1.1 thorpej usage();
122 1.1 thorpej break;
123 1.1 thorpej }
124 1.1 thorpej }
125 1.1 thorpej
126 1.1 thorpej signal(SIGINT, sig_handler);
127 1.1 thorpej signal(SIGTERM, sig_handler);
128 1.1 thorpej signal(SIGPIPE, sig_handler);
129 1.1 thorpej
130 1.1 thorpej if (no_server == 0) {
131 1.1 thorpej if (quip_openserver() < 0 && interface == NULL)
132 1.1 thorpej errx(1, "you have to specify interface!");
133 1.1 thorpej }
134 1.1 thorpej
135 1.1 thorpej if (raw_mode == 1) {
136 1.1 thorpej quip_rawmode();
137 1.1 thorpej quip_closeserver();
138 1.1 thorpej exit(0);
139 1.1 thorpej }
140 1.1 thorpej
141 1.1 thorpej if (show_config) {
142 1.1 thorpej if (no_server)
143 1.1 thorpej errx(1, "no server (-n) can't be set for show config (-s)!");
144 1.1 thorpej quip_printconfig();
145 1.1 thorpej quip_closeserver();
146 1.1 thorpej exit(0);
147 1.1 thorpej }
148 1.1 thorpej
149 1.1 thorpej interface = quip_selectinterface(interface);
150 1.1 thorpej if (interface == NULL)
151 1.1 thorpej errx(1, "no interface found!");
152 1.1 thorpej
153 1.1 thorpej qtype = ifname2qdisc(interface, qname);
154 1.1 thorpej if (qtype == 0)
155 1.1 thorpej errx(1, "altq is not attached on %s!", interface);
156 1.1 thorpej
157 1.1 thorpej qdisc_name = qname;
158 1.1 thorpej
159 1.1 thorpej stat_loop = qdisc2stat_loop(qdisc_name);
160 1.1 thorpej if (stat_loop == NULL)
161 1.1 thorpej errx(1, "qdisc %s is not supported!", qdisc_name);
162 1.1 thorpej
163 1.1 thorpej printf("%s: %s on interface %s\n",
164 1.1 thorpej argv[0], qdisc_name, interface);
165 1.1 thorpej
166 1.2 itojun snprintf(device, sizeof(device), "%s/%s", DEV_PATH, qdisc_name);
167 1.1 thorpej if ((qdiscfd = open(device, O_RDONLY)) < 0)
168 1.1 thorpej err(1, "can't open %s", device);
169 1.1 thorpej
170 1.1 thorpej (*stat_loop)(qdiscfd, interface, count, interval);
171 1.1 thorpej /* never returns */
172 1.1 thorpej
173 1.1 thorpej exit(0);
174 1.1 thorpej }
175 1.1 thorpej
176 1.1 thorpej /* calculate interval in sec */
177 1.1 thorpej double
178 1.1 thorpej calc_interval(struct timeval *cur_time, struct timeval *last_time)
179 1.1 thorpej {
180 1.1 thorpej double sec;
181 1.1 thorpej
182 1.1 thorpej sec = (double)(cur_time->tv_sec - last_time->tv_sec) +
183 1.1 thorpej (double)(cur_time->tv_usec - last_time->tv_usec) / 1000000;
184 1.1 thorpej return (sec);
185 1.1 thorpej }
186 1.1 thorpej
187 1.1 thorpej
188 1.1 thorpej /* calculate rate in bps */
189 1.1 thorpej double
190 1.1 thorpej calc_rate(u_int64_t new_bytes, u_int64_t last_bytes, double interval)
191 1.1 thorpej {
192 1.1 thorpej double rate;
193 1.1 thorpej
194 1.1 thorpej rate = (double)(new_bytes - last_bytes) * 8 / interval;
195 1.1 thorpej return (rate);
196 1.1 thorpej }
197 1.1 thorpej
198 1.1 thorpej /* calculate packets in second */
199 1.1 thorpej double
200 1.1 thorpej calc_pps(u_int64_t new_pkts, u_int64_t last_pkts, double interval)
201 1.1 thorpej {
202 1.1 thorpej double pps;
203 1.1 thorpej
204 1.1 thorpej pps = (double)(new_pkts - last_pkts) / interval;
205 1.1 thorpej return (pps);
206 1.1 thorpej }
207 1.1 thorpej
208 1.1 thorpej #define R2S_BUFS 8
209 1.2 itojun #define RATESTR_MAX 16
210 1.1 thorpej char *
211 1.1 thorpej rate2str(double rate)
212 1.1 thorpej {
213 1.1 thorpej char *buf;
214 1.2 itojun static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
215 1.1 thorpej static int idx = 0;
216 1.1 thorpej
217 1.1 thorpej buf = r2sbuf[idx++];
218 1.1 thorpej if (idx == R2S_BUFS)
219 1.1 thorpej idx = 0;
220 1.1 thorpej
221 1.1 thorpej if (rate == 0.0)
222 1.2 itojun snprintf(buf, RATESTR_MAX, "0");
223 1.1 thorpej else if (rate >= 1000000.0)
224 1.2 itojun snprintf(buf, RATESTR_MAX, "%.2fM", rate / 1000000.0);
225 1.1 thorpej else
226 1.2 itojun snprintf(buf, RATESTR_MAX, "%.2fK", rate / 1000.0);
227 1.1 thorpej return (buf);
228 1.1 thorpej }
229