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