blob: 876f1d01f607c4706608859dd9691bfcbb7b4836 [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
osdl.org!shemminger4703e082004-06-09 21:29:27 +0000100/*
osdl.org!shemminger72fc2042004-06-09 22:03:03 +0000101 * NB: rates are scaled differently depending on bits or bytes.
102 * if bits are requested then k == 1000
103 * for bytes k = 1024
osdl.org!shemminger4703e082004-06-09 21:29:27 +0000104 */
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000105int get_rate(unsigned *rate, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106{
107 char *p;
108 double bps = strtod(str, &p);
109
110 if (p == str)
111 return -1;
112
osdl.org!shemminger72fc2042004-06-09 22:03:03 +0000113 if (*p == 0 || strcasecmp(p, "bit") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000114 bps /= 8;
osdl.org!shemminger72fc2042004-06-09 22:03:03 +0000115 else if (strcasecmp(p, "kbit") == 0)
116 bps = (bps * 1000.) / 8;
117 else if (strcasecmp(p, "mbit") == 0)
118 bps = (bps * 1000000.)/8;
119 else if (strcasecmp(p, "gbit") == 0)
120 bps = (bps * 1000000000.)/8;
121 else if (strcasecmp(p, "kibit") == 0)
122 bps *= 1024 / 8;
123 else if (strcasecmp(p, "mibit") == 0)
124 bps *= 1024*1024/8;
125 else if (strcasecmp(p, "gibit") == 0)
126 bps *= 1024*1024*1024/8;
127 else if (strcasecmp(p, "kbps") == 0)
128 bps *= 1024;
129 else if (strcasecmp(p, "mbps") == 0)
130 bps *= 1024*1024;
131 else if (strcasecmp(p, "gbps") == 0)
132 bps *= 1024*1024*1024;
133 else if (strcasecmp(p, "bps") != 0)
134 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000135
136 *rate = bps;
137 return 0;
138}
139
140int get_rate_and_cell(unsigned *rate, int *cell_log, char *str)
141{
142 char * slash = strchr(str, '/');
143
144 if (slash)
145 *slash = 0;
146
147 if (get_rate(rate, str))
148 return -1;
149
150 if (slash) {
151 int cell;
152 int i;
153
154 if (get_integer(&cell, slash+1, 0))
155 return -1;
156 *slash = '/';
157
158 for (i=0; i<32; i++) {
159 if ((1<<i) == cell) {
160 *cell_log = i;
161 return 0;
162 }
163 }
164 return -1;
165 }
166 return 0;
167}
168
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000169void print_rate(char *buf, int len, __u32 rate)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000170{
171 double tmp = (double)rate*8;
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000172 extern int use_iec;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000173
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000174 if (use_iec) {
175 if (tmp >= 1024*1023 &&
176 fabs(1024*1024*rint(tmp/(1024*1024)) - tmp) < 1024)
177 snprintf(buf, len, "%gMibps", rint(tmp/(1024*1024)));
178 else if (tmp >= 1024-16 && fabs(1024*rint(tmp/1024) - tmp) < 16)
179 snprintf(buf, len, "%gKibps", rint(tmp/1024));
180 else
181 snprintf(buf, len, "%ubps", rate);
182
183 } else {
184 if (tmp >= 999999 &&
185 fabs(1000000.*rint(tmp/1000000.) - tmp) < 1000)
186 snprintf(buf, len, "%gMbit", rint(tmp/1000000.));
187 else if (tmp >= 990 && fabs(1000.*rint(tmp/1000.) - tmp) < 10)
188 snprintf(buf, len, "%gKbit", rint(tmp/1000.));
189 else
190 snprintf(buf, len, "%ubit", rate);
191 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000192}
193
194char * sprint_rate(__u32 rate, char *buf)
195{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000196 print_rate(buf, SPRINT_BSIZE-1, rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000197 return buf;
198}
199
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000200int get_usecs(unsigned *usecs, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000201{
202 double t;
203 char *p;
204
205 t = strtod(str, &p);
206 if (p == str)
207 return -1;
208
209 if (*p) {
210 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
211 strcasecmp(p, "secs")==0)
212 t *= 1000000;
213 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
214 strcasecmp(p, "msecs") == 0)
215 t *= 1000;
216 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
217 strcasecmp(p, "usecs") == 0)
218 t *= 1;
219 else
220 return -1;
221 }
222
223 *usecs = t;
224 return 0;
225}
226
227
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000228void print_usecs(char *buf, int len, __u32 usec)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000229{
230 double tmp = usec;
231
232 if (tmp >= 1000000)
233 snprintf(buf, len, "%.1fs", tmp/1000000);
234 else if (tmp >= 1000)
235 snprintf(buf, len, "%.1fms", tmp/1000);
236 else
237 snprintf(buf, len, "%uus", usec);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000238}
239
240char * sprint_usecs(__u32 usecs, char *buf)
241{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000242 print_usecs(buf, SPRINT_BSIZE-1, usecs);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000243 return buf;
244}
245
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000246int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000247{
248 double sz;
249 char *p;
250
251 sz = strtod(str, &p);
252 if (p == str)
253 return -1;
254
255 if (*p) {
256 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
257 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000258 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
259 sz *= 1024*1024*1024;
260 else if (strcasecmp(p, "gbit") == 0)
261 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000262 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
263 sz *= 1024*1024;
264 else if (strcasecmp(p, "mbit") == 0)
265 sz *= 1024*1024/8;
266 else if (strcasecmp(p, "kbit") == 0)
267 sz *= 1024/8;
268 else if (strcasecmp(p, "b") != 0)
269 return -1;
270 }
271
272 *size = sz;
273 return 0;
274}
275
276int get_size_and_cell(unsigned *size, int *cell_log, char *str)
277{
278 char * slash = strchr(str, '/');
279
280 if (slash)
281 *slash = 0;
282
283 if (get_size(size, str))
284 return -1;
285
286 if (slash) {
287 int cell;
288 int i;
289
290 if (get_integer(&cell, slash+1, 0))
291 return -1;
292 *slash = '/';
293
294 for (i=0; i<32; i++) {
295 if ((1<<i) == cell) {
296 *cell_log = i;
297 return 0;
298 }
299 }
300 return -1;
301 }
302 return 0;
303}
304
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000305void print_size(char *buf, int len, __u32 sz)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000306{
307 double tmp = sz;
308
309 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
310 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
311 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
312 snprintf(buf, len, "%gKb", rint(tmp/1024));
313 else
314 snprintf(buf, len, "%ub", sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000315}
316
317char * sprint_size(__u32 size, char *buf)
318{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000319 print_size(buf, SPRINT_BSIZE-1, size);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000320 return buf;
321}
322
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000323static double percent_scale = (double)(1ull << 32) / 100.;
324
325int get_percent(__u32 *percent, const char *str)
326{
327 char *p;
328 double per = strtod(str, &p);
329
330 if (per > 100.)
331 return -1;
332 if (*p && strcmp(p, "%"))
333 return -1;
334
335 *percent = per * percent_scale;
336 return 0;
337}
338
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000339void print_percent(char *buf, int len, __u32 per)
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000340{
341 snprintf(buf, len, "%g%%", (double) per / percent_scale);
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000342}
343
344char * sprint_percent(__u32 per, char *buf)
345{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000346 print_percent(buf, SPRINT_BSIZE-1, per);
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000347 return buf;
348}
349
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000350void print_qdisc_handle(char *buf, int len, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000351{
352 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000353}
354
355char * sprint_qdisc_handle(__u32 h, char *buf)
356{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000357 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000358 return buf;
359}
360
361