altqstat.c revision 1.6 1 1.6 peter /* $NetBSD: altqstat.c,v 1.6 2006/10/12 19:59:13 peter 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.2 itojun static void sig_handler(int);
59 1.6 peter static void alrm_handler(int);
60 1.1 thorpej 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.1 thorpej break;
109 1.1 thorpej case 'e':
110 1.1 thorpej quip_echo = 1;
111 1.1 thorpej break;
112 1.1 thorpej case 'i':
113 1.1 thorpej interface = optarg;
114 1.1 thorpej break;
115 1.1 thorpej case 'n':
116 1.1 thorpej no_server = 1;
117 1.1 thorpej break;
118 1.1 thorpej case 'r':
119 1.1 thorpej raw_mode = 1;
120 1.1 thorpej quip_echo = 1;
121 1.1 thorpej break;
122 1.1 thorpej case 's':
123 1.1 thorpej show_config = 1;
124 1.1 thorpej break;
125 1.1 thorpej case 'w':
126 1.6 peter ival = strtod(optarg, NULL);
127 1.1 thorpej break;
128 1.1 thorpej default:
129 1.1 thorpej usage();
130 1.1 thorpej break;
131 1.1 thorpej }
132 1.1 thorpej }
133 1.1 thorpej
134 1.1 thorpej signal(SIGINT, sig_handler);
135 1.1 thorpej signal(SIGTERM, sig_handler);
136 1.1 thorpej signal(SIGPIPE, sig_handler);
137 1.6 peter signal(SIGALRM, alrm_handler);
138 1.1 thorpej
139 1.1 thorpej if (no_server == 0) {
140 1.1 thorpej if (quip_openserver() < 0 && interface == NULL)
141 1.1 thorpej errx(1, "you have to specify interface!");
142 1.1 thorpej }
143 1.1 thorpej
144 1.1 thorpej if (raw_mode == 1) {
145 1.1 thorpej quip_rawmode();
146 1.1 thorpej quip_closeserver();
147 1.1 thorpej exit(0);
148 1.1 thorpej }
149 1.1 thorpej
150 1.1 thorpej if (show_config) {
151 1.1 thorpej if (no_server)
152 1.1 thorpej errx(1, "no server (-n) can't be set for show config (-s)!");
153 1.1 thorpej quip_printconfig();
154 1.1 thorpej quip_closeserver();
155 1.1 thorpej exit(0);
156 1.1 thorpej }
157 1.1 thorpej
158 1.1 thorpej interface = quip_selectinterface(interface);
159 1.1 thorpej if (interface == NULL)
160 1.1 thorpej errx(1, "no interface found!");
161 1.1 thorpej
162 1.1 thorpej qtype = ifname2qdisc(interface, qname);
163 1.1 thorpej if (qtype == 0)
164 1.1 thorpej errx(1, "altq is not attached on %s!", interface);
165 1.1 thorpej
166 1.1 thorpej qdisc_name = qname;
167 1.1 thorpej
168 1.1 thorpej stat_loop = qdisc2stat_loop(qdisc_name);
169 1.1 thorpej if (stat_loop == NULL)
170 1.1 thorpej errx(1, "qdisc %s is not supported!", qdisc_name);
171 1.1 thorpej
172 1.1 thorpej printf("%s: %s on interface %s\n",
173 1.1 thorpej argv[0], qdisc_name, interface);
174 1.1 thorpej
175 1.2 itojun snprintf(device, sizeof(device), "%s/%s", DEV_PATH, qdisc_name);
176 1.1 thorpej if ((qdiscfd = open(device, O_RDONLY)) < 0)
177 1.1 thorpej err(1, "can't open %s", device);
178 1.1 thorpej
179 1.6 peter interval = (int)(ival * 1000.0);
180 1.6 peter it.it_interval.tv_sec = interval / 1000;
181 1.6 peter it.it_interval.tv_usec = interval % 1000 * 1000;
182 1.6 peter it.it_value = it.it_interval;
183 1.6 peter setitimer(ITIMER_REAL, &it, NULL);
184 1.6 peter
185 1.6 peter (*stat_loop)(qdiscfd, interface, count, (int)ival);
186 1.1 thorpej
187 1.1 thorpej exit(0);
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej /* calculate interval in sec */
191 1.1 thorpej double
192 1.1 thorpej calc_interval(struct timeval *cur_time, struct timeval *last_time)
193 1.1 thorpej {
194 1.1 thorpej double sec;
195 1.1 thorpej
196 1.1 thorpej sec = (double)(cur_time->tv_sec - last_time->tv_sec) +
197 1.1 thorpej (double)(cur_time->tv_usec - last_time->tv_usec) / 1000000;
198 1.1 thorpej return (sec);
199 1.1 thorpej }
200 1.1 thorpej
201 1.1 thorpej
202 1.1 thorpej /* calculate rate in bps */
203 1.1 thorpej double
204 1.1 thorpej calc_rate(u_int64_t new_bytes, u_int64_t last_bytes, double interval)
205 1.1 thorpej {
206 1.1 thorpej double rate;
207 1.1 thorpej
208 1.1 thorpej rate = (double)(new_bytes - last_bytes) * 8 / interval;
209 1.1 thorpej return (rate);
210 1.1 thorpej }
211 1.1 thorpej
212 1.1 thorpej /* calculate packets in second */
213 1.1 thorpej double
214 1.1 thorpej calc_pps(u_int64_t new_pkts, u_int64_t last_pkts, double interval)
215 1.1 thorpej {
216 1.1 thorpej double pps;
217 1.1 thorpej
218 1.1 thorpej pps = (double)(new_pkts - last_pkts) / interval;
219 1.1 thorpej return (pps);
220 1.1 thorpej }
221 1.1 thorpej
222 1.1 thorpej #define R2S_BUFS 8
223 1.2 itojun #define RATESTR_MAX 16
224 1.1 thorpej char *
225 1.1 thorpej rate2str(double rate)
226 1.1 thorpej {
227 1.1 thorpej char *buf;
228 1.2 itojun static char r2sbuf[R2S_BUFS][RATESTR_MAX]; /* ring bufer */
229 1.1 thorpej static int idx = 0;
230 1.1 thorpej
231 1.1 thorpej buf = r2sbuf[idx++];
232 1.1 thorpej if (idx == R2S_BUFS)
233 1.1 thorpej idx = 0;
234 1.1 thorpej
235 1.1 thorpej if (rate == 0.0)
236 1.2 itojun snprintf(buf, RATESTR_MAX, "0");
237 1.1 thorpej else if (rate >= 1000000.0)
238 1.2 itojun snprintf(buf, RATESTR_MAX, "%.2fM", rate / 1000000.0);
239 1.1 thorpej else
240 1.6 peter snprintf(buf, RATESTR_MAX, "%.2fk", rate / 1000.0);
241 1.1 thorpej return (buf);
242 1.1 thorpej }
243