blob: bae4885f43e3d6f2fce6eb92574aab502062afe2 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * tc_util.c Misc TC utility functions.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include <math.h>
23
24#include "utils.h"
25#include "tc_util.h"
26
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000027int get_qdisc_handle(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000028{
29 __u32 maj;
30 char *p;
31
32 maj = TC_H_UNSPEC;
33 if (strcmp(str, "none") == 0)
34 goto ok;
35 maj = strtoul(str, &p, 16);
36 if (p == str)
37 return -1;
38 maj <<= 16;
39 if (*p != ':' && *p!=0)
40 return -1;
41ok:
42 *h = maj;
43 return 0;
44}
45
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000046int get_tc_classid(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000047{
48 __u32 maj, min;
49 char *p;
50
51 maj = TC_H_ROOT;
52 if (strcmp(str, "root") == 0)
53 goto ok;
54 maj = TC_H_UNSPEC;
55 if (strcmp(str, "none") == 0)
56 goto ok;
57 maj = strtoul(str, &p, 16);
58 if (p == str) {
59 maj = 0;
60 if (*p != ':')
61 return -1;
62 }
63 if (*p == ':') {
64 maj <<= 16;
65 str = p+1;
66 min = strtoul(str, &p, 16);
67 if (*p != 0)
68 return -1;
69 maj |= min;
70 } else if (*p != 0)
71 return -1;
72
73ok:
74 *h = maj;
75 return 0;
76}
77
78int print_tc_classid(char *buf, int len, __u32 h)
79{
80 if (h == TC_H_ROOT)
81 sprintf(buf, "root");
82 else if (h == TC_H_UNSPEC)
83 snprintf(buf, len, "none");
84 else if (TC_H_MAJ(h) == 0)
85 snprintf(buf, len, ":%x", TC_H_MIN(h));
86 else if (TC_H_MIN(h) == 0)
87 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
88 else
89 snprintf(buf, len, "%x:%x", TC_H_MAJ(h)>>16, TC_H_MIN(h));
90 return 0;
91}
92
93char * sprint_tc_classid(__u32 h, char *buf)
94{
95 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
96 strcpy(buf, "???");
97 return buf;
98}
99
100
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000101int get_rate(unsigned *rate, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000102{
103 char *p;
104 double bps = strtod(str, &p);
105
106 if (p == str)
107 return -1;
108
109 if (*p) {
110 if (strcasecmp(p, "kbps") == 0)
111 bps *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000112 else if (strcasecmp(p, "gbps") == 0)
113 bps *= 1024*1024*1024;
114 else if (strcasecmp(p, "gbit") == 0)
115 bps *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000116 else if (strcasecmp(p, "mbps") == 0)
117 bps *= 1024*1024;
118 else if (strcasecmp(p, "mbit") == 0)
119 bps *= 1024*1024/8;
120 else if (strcasecmp(p, "kbit") == 0)
121 bps *= 1024/8;
122 else if (strcasecmp(p, "bps") != 0)
123 return -1;
124 } else
125 bps /= 8;
126
127 *rate = bps;
128 return 0;
129}
130
131int get_rate_and_cell(unsigned *rate, int *cell_log, char *str)
132{
133 char * slash = strchr(str, '/');
134
135 if (slash)
136 *slash = 0;
137
138 if (get_rate(rate, str))
139 return -1;
140
141 if (slash) {
142 int cell;
143 int i;
144
145 if (get_integer(&cell, slash+1, 0))
146 return -1;
147 *slash = '/';
148
149 for (i=0; i<32; i++) {
150 if ((1<<i) == cell) {
151 *cell_log = i;
152 return 0;
153 }
154 }
155 return -1;
156 }
157 return 0;
158}
159
160
161int print_rate(char *buf, int len, __u32 rate)
162{
163 double tmp = (double)rate*8;
164
165 if (tmp >= 1024*1023 && fabs(1024*1024*rint(tmp/(1024*1024)) - tmp) < 1024)
166 snprintf(buf, len, "%gMbit", rint(tmp/(1024*1024)));
167 else if (tmp >= 1024-16 && fabs(1024*rint(tmp/1024) - tmp) < 16)
168 snprintf(buf, len, "%gKbit", rint(tmp/1024));
169 else
170 snprintf(buf, len, "%ubps", rate);
171 return 0;
172}
173
174char * sprint_rate(__u32 rate, char *buf)
175{
176 if (print_rate(buf, SPRINT_BSIZE-1, rate))
177 strcpy(buf, "???");
178 return buf;
179}
180
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000181int get_usecs(unsigned *usecs, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000182{
183 double t;
184 char *p;
185
186 t = strtod(str, &p);
187 if (p == str)
188 return -1;
189
190 if (*p) {
191 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
192 strcasecmp(p, "secs")==0)
193 t *= 1000000;
194 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
195 strcasecmp(p, "msecs") == 0)
196 t *= 1000;
197 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
198 strcasecmp(p, "usecs") == 0)
199 t *= 1;
200 else
201 return -1;
202 }
203
204 *usecs = t;
205 return 0;
206}
207
208
209int print_usecs(char *buf, int len, __u32 usec)
210{
211 double tmp = usec;
212
213 if (tmp >= 1000000)
214 snprintf(buf, len, "%.1fs", tmp/1000000);
215 else if (tmp >= 1000)
216 snprintf(buf, len, "%.1fms", tmp/1000);
217 else
218 snprintf(buf, len, "%uus", usec);
219 return 0;
220}
221
222char * sprint_usecs(__u32 usecs, char *buf)
223{
224 if (print_usecs(buf, SPRINT_BSIZE-1, usecs))
225 strcpy(buf, "???");
226 return buf;
227}
228
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000229int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000230{
231 double sz;
232 char *p;
233
234 sz = strtod(str, &p);
235 if (p == str)
236 return -1;
237
238 if (*p) {
239 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
240 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000241 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
242 sz *= 1024*1024*1024;
243 else if (strcasecmp(p, "gbit") == 0)
244 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000245 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
246 sz *= 1024*1024;
247 else if (strcasecmp(p, "mbit") == 0)
248 sz *= 1024*1024/8;
249 else if (strcasecmp(p, "kbit") == 0)
250 sz *= 1024/8;
251 else if (strcasecmp(p, "b") != 0)
252 return -1;
253 }
254
255 *size = sz;
256 return 0;
257}
258
259int get_size_and_cell(unsigned *size, int *cell_log, char *str)
260{
261 char * slash = strchr(str, '/');
262
263 if (slash)
264 *slash = 0;
265
266 if (get_size(size, str))
267 return -1;
268
269 if (slash) {
270 int cell;
271 int i;
272
273 if (get_integer(&cell, slash+1, 0))
274 return -1;
275 *slash = '/';
276
277 for (i=0; i<32; i++) {
278 if ((1<<i) == cell) {
279 *cell_log = i;
280 return 0;
281 }
282 }
283 return -1;
284 }
285 return 0;
286}
287
288int print_size(char *buf, int len, __u32 sz)
289{
290 double tmp = sz;
291
292 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
293 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
294 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
295 snprintf(buf, len, "%gKb", rint(tmp/1024));
296 else
297 snprintf(buf, len, "%ub", sz);
298 return 0;
299}
300
301char * sprint_size(__u32 size, char *buf)
302{
303 if (print_size(buf, SPRINT_BSIZE-1, size))
304 strcpy(buf, "???");
305 return buf;
306}
307
308int print_qdisc_handle(char *buf, int len, __u32 h)
309{
310 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
311 return 0;
312}
313
314char * sprint_qdisc_handle(__u32 h, char *buf)
315{
316 if (print_qdisc_handle(buf, SPRINT_BSIZE-1, h))
317 strcpy(buf, "???");
318 return buf;
319}
320
321