]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_util.c
Add TC_LIB_DIR environment variable.
[mirror_iproute2.git] / tc / tc_util.c
1 /*
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
27 const char *get_tc_lib(void)
28 {
29 const char *lib_dir;
30
31 lib_dir = getenv("TC_LIB_DIR");
32 if (!lib_dir)
33 lib_dir = "/usr/lib/tc";
34
35 return lib_dir;
36 }
37
38 int get_qdisc_handle(__u32 *h, const char *str)
39 {
40 __u32 maj;
41 char *p;
42
43 maj = TC_H_UNSPEC;
44 if (strcmp(str, "none") == 0)
45 goto ok;
46 maj = strtoul(str, &p, 16);
47 if (p == str)
48 return -1;
49 maj <<= 16;
50 if (*p != ':' && *p!=0)
51 return -1;
52 ok:
53 *h = maj;
54 return 0;
55 }
56
57 int get_tc_classid(__u32 *h, const char *str)
58 {
59 __u32 maj, min;
60 char *p;
61
62 maj = TC_H_ROOT;
63 if (strcmp(str, "root") == 0)
64 goto ok;
65 maj = TC_H_UNSPEC;
66 if (strcmp(str, "none") == 0)
67 goto ok;
68 maj = strtoul(str, &p, 16);
69 if (p == str) {
70 maj = 0;
71 if (*p != ':')
72 return -1;
73 }
74 if (*p == ':') {
75 if (maj >= (1<<16))
76 return -1;
77 maj <<= 16;
78 str = p+1;
79 min = strtoul(str, &p, 16);
80 if (*p != 0)
81 return -1;
82 if (min >= (1<<16))
83 return -1;
84 maj |= min;
85 } else if (*p != 0)
86 return -1;
87
88 ok:
89 *h = maj;
90 return 0;
91 }
92
93 int print_tc_classid(char *buf, int len, __u32 h)
94 {
95 if (h == TC_H_ROOT)
96 sprintf(buf, "root");
97 else if (h == TC_H_UNSPEC)
98 snprintf(buf, len, "none");
99 else if (TC_H_MAJ(h) == 0)
100 snprintf(buf, len, ":%x", TC_H_MIN(h));
101 else if (TC_H_MIN(h) == 0)
102 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
103 else
104 snprintf(buf, len, "%x:%x", TC_H_MAJ(h)>>16, TC_H_MIN(h));
105 return 0;
106 }
107
108 char * sprint_tc_classid(__u32 h, char *buf)
109 {
110 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
111 strcpy(buf, "???");
112 return buf;
113 }
114
115 /* See http://physics.nist.gov/cuu/Units/binary.html */
116 static const struct rate_suffix {
117 const char *name;
118 double scale;
119 } suffixes[] = {
120 { "bit", 1. },
121 { "Kibit", 1024. },
122 { "kbit", 1000. },
123 { "mibit", 1024.*1024. },
124 { "mbit", 1000000. },
125 { "gibit", 1024.*1024.*1024. },
126 { "gbit", 1000000000. },
127 { "tibit", 1024.*1024.*1024.*1024. },
128 { "tbit", 1000000000000. },
129 { "Bps", 8. },
130 { "KiBps", 8.*1024. },
131 { "KBps", 8000. },
132 { "MiBps", 8.*1024*1024. },
133 { "MBps", 8000000. },
134 { "GiBps", 8.*1024.*1024.*1024. },
135 { "GBps", 8000000000. },
136 { "TiBps", 8.*1024.*1024.*1024.*1024. },
137 { "TBps", 8000000000000. },
138 { NULL }
139 };
140
141
142 int get_rate(unsigned *rate, const char *str)
143 {
144 char *p;
145 double bps = strtod(str, &p);
146 const struct rate_suffix *s;
147
148 if (p == str)
149 return -1;
150
151 if (*p == '\0') {
152 *rate = bps / 8.; /* assume bytes/sec */
153 return 0;
154 }
155
156 for (s = suffixes; s->name; ++s) {
157 if (strcasecmp(s->name, p) == 0) {
158 *rate = (bps * s->scale) / 8.;
159 return 0;
160 }
161 }
162
163 return -1;
164 }
165
166 int get_rate_and_cell(unsigned *rate, int *cell_log, char *str)
167 {
168 char * slash = strchr(str, '/');
169
170 if (slash)
171 *slash = 0;
172
173 if (get_rate(rate, str))
174 return -1;
175
176 if (slash) {
177 int cell;
178 int i;
179
180 if (get_integer(&cell, slash+1, 0))
181 return -1;
182 *slash = '/';
183
184 for (i=0; i<32; i++) {
185 if ((1<<i) == cell) {
186 *cell_log = i;
187 return 0;
188 }
189 }
190 return -1;
191 }
192 return 0;
193 }
194
195 void print_rate(char *buf, int len, __u32 rate)
196 {
197 double tmp = (double)rate*8;
198 extern int use_iec;
199
200 if (use_iec) {
201 if (tmp >= 1000.0*1024.0*1024.0)
202 snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0);
203 else if (tmp >= 1000.0*1024)
204 snprintf(buf, len, "%.0fKibit", tmp/1024);
205 else
206 snprintf(buf, len, "%.0fbit", tmp);
207 } else {
208 if (tmp >= 1000.0*1000000.0)
209 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
210 else if (tmp >= 1000.0 * 1000.0)
211 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
212 else
213 snprintf(buf, len, "%.0fbit", tmp);
214 }
215 }
216
217 char * sprint_rate(__u32 rate, char *buf)
218 {
219 print_rate(buf, SPRINT_BSIZE-1, rate);
220 return buf;
221 }
222
223 int get_time(unsigned *time, const char *str)
224 {
225 double t;
226 char *p;
227
228 t = strtod(str, &p);
229 if (p == str)
230 return -1;
231
232 if (*p) {
233 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
234 strcasecmp(p, "secs")==0)
235 t *= TIME_UNITS_PER_SEC;
236 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
237 strcasecmp(p, "msecs") == 0)
238 t *= TIME_UNITS_PER_SEC/1000;
239 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
240 strcasecmp(p, "usecs") == 0)
241 t *= TIME_UNITS_PER_SEC/1000000;
242 else
243 return -1;
244 }
245
246 *time = t;
247 return 0;
248 }
249
250
251 void print_time(char *buf, int len, __u32 time)
252 {
253 double tmp = time;
254
255 if (tmp >= TIME_UNITS_PER_SEC)
256 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
257 else if (tmp >= TIME_UNITS_PER_SEC/1000)
258 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
259 else
260 snprintf(buf, len, "%uus", time);
261 }
262
263 char * sprint_time(__u32 time, char *buf)
264 {
265 print_time(buf, SPRINT_BSIZE-1, time);
266 return buf;
267 }
268
269 char * sprint_ticks(__u32 ticks, char *buf)
270 {
271 return sprint_time(tc_core_tick2time(ticks), buf);
272 }
273
274 int get_size(unsigned *size, const char *str)
275 {
276 double sz;
277 char *p;
278
279 sz = strtod(str, &p);
280 if (p == str)
281 return -1;
282
283 if (*p) {
284 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
285 sz *= 1024;
286 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
287 sz *= 1024*1024*1024;
288 else if (strcasecmp(p, "gbit") == 0)
289 sz *= 1024*1024*1024/8;
290 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
291 sz *= 1024*1024;
292 else if (strcasecmp(p, "mbit") == 0)
293 sz *= 1024*1024/8;
294 else if (strcasecmp(p, "kbit") == 0)
295 sz *= 1024/8;
296 else if (strcasecmp(p, "b") != 0)
297 return -1;
298 }
299
300 *size = sz;
301 return 0;
302 }
303
304 int get_size_and_cell(unsigned *size, int *cell_log, char *str)
305 {
306 char * slash = strchr(str, '/');
307
308 if (slash)
309 *slash = 0;
310
311 if (get_size(size, str))
312 return -1;
313
314 if (slash) {
315 int cell;
316 int i;
317
318 if (get_integer(&cell, slash+1, 0))
319 return -1;
320 *slash = '/';
321
322 for (i=0; i<32; i++) {
323 if ((1<<i) == cell) {
324 *cell_log = i;
325 return 0;
326 }
327 }
328 return -1;
329 }
330 return 0;
331 }
332
333 void print_size(char *buf, int len, __u32 sz)
334 {
335 double tmp = sz;
336
337 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
338 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
339 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
340 snprintf(buf, len, "%gKb", rint(tmp/1024));
341 else
342 snprintf(buf, len, "%ub", sz);
343 }
344
345 char * sprint_size(__u32 size, char *buf)
346 {
347 print_size(buf, SPRINT_BSIZE-1, size);
348 return buf;
349 }
350
351 static const double max_percent_value = 0xffffffff;
352
353 int get_percent(__u32 *percent, const char *str)
354 {
355 char *p;
356 double per = strtod(str, &p) / 100.;
357
358 if (per > 1. || per < 0)
359 return -1;
360 if (*p && strcmp(p, "%"))
361 return -1;
362
363 *percent = (unsigned) rint(per * max_percent_value);
364 return 0;
365 }
366
367 void print_percent(char *buf, int len, __u32 per)
368 {
369 snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value);
370 }
371
372 char * sprint_percent(__u32 per, char *buf)
373 {
374 print_percent(buf, SPRINT_BSIZE-1, per);
375 return buf;
376 }
377
378 void print_qdisc_handle(char *buf, int len, __u32 h)
379 {
380 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
381 }
382
383 char * sprint_qdisc_handle(__u32 h, char *buf)
384 {
385 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
386 return buf;
387 }
388
389 char * action_n2a(int action, char *buf, int len)
390 {
391 switch (action) {
392 case -1:
393 return "continue";
394 break;
395 case TC_ACT_OK:
396 return "pass";
397 break;
398 case TC_ACT_SHOT:
399 return "drop";
400 break;
401 case TC_ACT_RECLASSIFY:
402 return "reclassify";
403 case TC_ACT_PIPE:
404 return "pipe";
405 case TC_ACT_STOLEN:
406 return "stolen";
407 default:
408 snprintf(buf, len, "%d", action);
409 return buf;
410 }
411 }
412
413 int action_a2n(char *arg, int *result)
414 {
415 int res;
416
417 if (matches(arg, "continue") == 0)
418 res = -1;
419 else if (matches(arg, "drop") == 0)
420 res = TC_ACT_SHOT;
421 else if (matches(arg, "shot") == 0)
422 res = TC_ACT_SHOT;
423 else if (matches(arg, "pass") == 0)
424 res = TC_ACT_OK;
425 else if (strcmp(arg, "ok") == 0)
426 res = TC_ACT_OK;
427 else if (matches(arg, "reclassify") == 0)
428 res = TC_ACT_RECLASSIFY;
429 else {
430 char dummy;
431 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
432 return -1;
433 }
434 *result = res;
435 return 0;
436 }
437
438 void print_tm(FILE * f, const struct tcf_t *tm)
439 {
440 int hz = get_user_hz();
441 if (tm->install != 0)
442 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
443 if (tm->lastuse != 0)
444 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
445 if (tm->expires != 0)
446 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
447 }
448
449 void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
450 {
451 SPRINT_BUF(b1);
452 struct rtattr *tbs[TCA_STATS_MAX + 1];
453
454 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
455
456 if (tbs[TCA_STATS_BASIC]) {
457 struct gnet_stats_basic bs = {0};
458 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
459 fprintf(fp, "%sSent %llu bytes %u pkt",
460 prefix, (unsigned long long) bs.bytes, bs.packets);
461 }
462
463 if (tbs[TCA_STATS_QUEUE]) {
464 struct gnet_stats_queue q = {0};
465 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
466 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
467 q.drops, q.overlimits, q.requeues);
468 }
469
470 if (tbs[TCA_STATS_RATE_EST]) {
471 struct gnet_stats_rate_est re = {0};
472 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
473 fprintf(fp, "\n%srate %s %upps ",
474 prefix, sprint_rate(re.bps, b1), re.pps);
475 }
476
477 if (tbs[TCA_STATS_QUEUE]) {
478 struct gnet_stats_queue q = {0};
479 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
480 if (!tbs[TCA_STATS_RATE_EST])
481 fprintf(fp, "\n%s", prefix);
482 fprintf(fp, "backlog %s %up requeues %u ",
483 sprint_size(q.backlog, b1), q.qlen, q.requeues);
484 }
485
486 if (xstats)
487 *xstats = tbs[TCA_STATS_APP] ? : NULL;
488 }
489
490 void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
491 {
492 SPRINT_BUF(b1);
493
494 if (tb[TCA_STATS2]) {
495 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
496 if (xstats && NULL == *xstats)
497 goto compat_xstats;
498 return;
499 }
500 /* backward compatibility */
501 if (tb[TCA_STATS]) {
502 struct tc_stats st;
503
504 /* handle case where kernel returns more/less than we know about */
505 memset(&st, 0, sizeof(st));
506 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
507
508 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
509 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
510 st.overlimits);
511
512 if (st.bps || st.pps || st.qlen || st.backlog) {
513 fprintf(fp, "\n%s", prefix);
514 if (st.bps || st.pps) {
515 fprintf(fp, "rate ");
516 if (st.bps)
517 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
518 if (st.pps)
519 fprintf(fp, "%upps ", st.pps);
520 }
521 if (st.qlen || st.backlog) {
522 fprintf(fp, "backlog ");
523 if (st.backlog)
524 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
525 if (st.qlen)
526 fprintf(fp, "%up ", st.qlen);
527 }
528 }
529 }
530
531 compat_xstats:
532 if (tb[TCA_XSTATS] && xstats)
533 *xstats = tb[TCA_XSTATS];
534 }
535