altqstat.c revision 1.6 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 $ */
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/time.h>
31 #include <sys/fcntl.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <errno.h>
39 #include <err.h>
40 #ifndef NO_CURSES
41 #include <curses.h>
42 #endif
43
44 #include "quip_client.h"
45 #include "altqstat.h"
46
47 #define DEV_PATH "/dev/altq"
48
49 int qdiscfd = -1;
50 int show_config = 0;
51 double ival = 5.0;
52 int no_server = 0;
53 char *interface = NULL;
54 char *qdisc_name = NULL;
55
56 stat_loop_t *stat_loop;
57
58 static void sig_handler(int);
59 static void alrm_handler(int);
60 static void usage(void);
61
62 static void
63 sig_handler(int sig)
64 {
65 char buf[8192];
66
67 snprintf(buf, sizeof buf, "Exiting on signal %d\n", sig);
68 write(STDERR_FILENO, buf, strlen(buf));
69
70 #ifndef NO_CURSES
71 /* XXX signal race */
72 if (qdisc_name != NULL && strcmp(qdisc_name, "wfq") == 0)
73 endwin(); /* wfqstat uses curses */
74 #endif
75 _exit(0);
76 }
77
78 static void
79 alrm_handler(int sig)
80 {
81 /* nothing */
82 }
83
84 static void
85 usage(void)
86 {
87 fprintf(stderr, "usage: altqstat [-enrs] [-c count] [-w wait] [-i interface|-I input_interface]\n");
88 exit(1);
89 }
90
91 int
92 main (int argc, char **argv)
93 {
94 int ch, raw_mode = 0;
95 int qtype, interval;
96 int count = 0;
97 struct itimerval it;
98 char device[64], qname[64], input[32];
99
100 while ((ch = getopt(argc, argv, "I:c:ei:nrsw:")) != -1) {
101 switch (ch) {
102 case 'I':
103 snprintf(input, sizeof(input), "_%s", optarg);
104 interface = input;
105 break;
106 case 'c':
107 count = atoi(optarg);
108 break;
109 case 'e':
110 quip_echo = 1;
111 break;
112 case 'i':
113 interface = optarg;
114 break;
115 case 'n':
116 no_server = 1;
117 break;
118 case 'r':
119 raw_mode = 1;
120 quip_echo = 1;
121 break;
122 case 's':
123 show_config = 1;
124 break;
125 case 'w':
126 ival = strtod(optarg, NULL);
127 break;
128 default:
129 usage();
130 break;
131 }
132 }
133
134 signal(SIGINT, sig_handler);
135 signal(SIGTERM, sig_handler);
136 signal(SIGPIPE, sig_handler);
137 signal(SIGALRM, alrm_handler);
138
139 if (no_server == 0) {
140 if (quip_openserver() < 0 && interface == NULL)
141 errx(1, "you have to specify interface!");
142 }
143
144 if (raw_mode == 1) {
145 quip_rawmode();
146 quip_closeserver();
147 exit(0);
148 }
149
150 if (show_config) {
151 if (no_server)
152 errx(1, "no server (-n) can't be set for show config (-s)!");
153 quip_printconfig();
154 quip_closeserver();
155 exit(0);
156 }
157
158 interface = quip_selectinterface(interface);
159 if (interface == NULL)
160 errx(1, "no interface found!");
161
162 qtype = ifname2qdisc(interface, qname);
163 if (qtype == 0)
164 errx(1, "altq is not attached on %s!", interface);
165
166 qdisc_name = qname;
167
168 stat_loop = qdisc2stat_loop(qdisc_name);
169 if (stat_loop == NULL)
170 errx(1, "qdisc %s is not supported!", qdisc_name);
171
172 printf("%s: %s on interface %s\n",
173 argv[0], qdisc_name, interface);
174
175 snprintf(device, sizeof(device), "%s/%s", DEV_PATH, qdisc_name);
176 if ((qdiscfd = open(device, O_RDONLY)) < 0)
177 err(1, "can't open %s", device);
178
179 interval = (int)(ival * 1000.0);
180 it.it_interval.tv_sec = interval / 1000;
181 it.it_interval.tv_usec = interval % 1000 * 1000;
182 it.it_value = it.it_interval;
183 setitimer(ITIMER_REAL, &it, NULL);
184
185 (*stat_loop)(qdiscfd, interface, count, (int)ival);
186
187 exit(0);
188 }
189
190 /* calculate interval in sec */
191 double
192 calc_interval(struct timeval *cur_time, struct timeval *last_time)
193 {
194 double sec;
195
196 sec = (double)(cur_time->tv_sec - last_time->tv_sec) +
197 (double)(cur_time->tv_usec - last_time->tv_usec) / 1000000;
198 return (sec);
199 }
200
201
202 /* calculate rate in bps */
203 double
204 calc_rate(u_int64_t new_bytes, u_int64_t last_bytes, double interval)
205 {
206 double rate;
207
208 rate = (double)(new_bytes - last_bytes) * 8 / interval;
209 return (rate);
210 }
211
212 /* calculate packets in second */
213 double
214 calc_pps(u_int64_t new_pkts, u_int64_t last_pkts, double interval)
215 {
216 double pps;
217
218 pps = (double)(new_pkts - last_pkts) / interval;
219 return (pps);
220 }
221
222 #define R2S_BUFS 8
223 #define RATESTR_MAX 16
224 char *
225 rate2str(double rate)
226 {
227 char *buf;
228 static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
229 static int idx = 0;
230
231 buf = r2sbuf[idx++];
232 if (idx == R2S_BUFS)
233 idx = 0;
234
235 if (rate == 0.0)
236 snprintf(buf, RATESTR_MAX, "0");
237 else if (rate >= 1000000.0)
238 snprintf(buf, RATESTR_MAX, "%.2fM", rate / 1000000.0);
239 else
240 snprintf(buf, RATESTR_MAX, "%.2fk", rate / 1000.0);
241 return (buf);
242 }
243