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