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