]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_util.c
Update headers to 3.13-rc2
[mirror_iproute2.git] / tc / tc_util.c
CommitLineData
aba5acdf
SH
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
5e3bb534 27#ifndef LIBDIR
5c434a9e 28#define LIBDIR "/usr/lib"
b514b358
RA
29#endif
30
aa27f88c
SH
31const char *get_tc_lib(void)
32{
33 const char *lib_dir;
34
35 lib_dir = getenv("TC_LIB_DIR");
36 if (!lib_dir)
5e3bb534 37 lib_dir = LIBDIR "/tc/";
aa27f88c
SH
38
39 return lib_dir;
40}
41
dbd90dc2 42int get_qdisc_handle(__u32 *h, const char *str)
aba5acdf
SH
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;
56ok:
57 *h = maj;
58 return 0;
59}
60
dbd90dc2 61int get_tc_classid(__u32 *h, const char *str)
aba5acdf
SH
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 == ':') {
a8b303cc
SH
79 if (maj >= (1<<16))
80 return -1;
aba5acdf
SH
81 maj <<= 16;
82 str = p+1;
83 min = strtoul(str, &p, 16);
84 if (*p != 0)
85 return -1;
a8b303cc
SH
86 if (min >= (1<<16))
87 return -1;
aba5acdf
SH
88 maj |= min;
89 } else if (*p != 0)
90 return -1;
91
92ok:
93 *h = maj;
94 return 0;
95}
96
97int 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
112char * 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
26ab0b10
SH
119/* See http://physics.nist.gov/cuu/Units/binary.html */
120static 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
dbd90dc2 146int get_rate(unsigned *rate, const char *str)
aba5acdf
SH
147{
148 char *p;
149 double bps = strtod(str, &p);
26ab0b10 150 const struct rate_suffix *s;
aba5acdf
SH
151
152 if (p == str)
153 return -1;
154
26ab0b10
SH
155 for (s = suffixes; s->name; ++s) {
156 if (strcasecmp(s->name, p) == 0) {
a303853e
ED
157 bps *= s->scale;
158 p += strlen(p);
159 break;
26ab0b10
SH
160 }
161 }
162
a303853e
ED
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;
aba5acdf
SH
172}
173
8f7574ed 174void print_rate(char *buf, int len, __u64 rate)
aba5acdf
SH
175{
176 double tmp = (double)rate*8;
d40b38b4
SH
177 extern int use_iec;
178
179 if (use_iec) {
8f7574ed
ED
180 if (tmp >= 1000.0*1024.0*1024.0*1024.0)
181 snprintf(buf, len, "%.0fGibit", tmp/(1024.0*1024.0*1024.0));
182 else if (tmp >= 1000.0*1024.0*1024.0)
f526af99 183 snprintf(buf, len, "%.0fMibit", tmp/(1024.0*1024.0));
abf1d0b0
SH
184 else if (tmp >= 1000.0*1024)
185 snprintf(buf, len, "%.0fKibit", tmp/1024);
d40b38b4 186 else
abf1d0b0
SH
187 snprintf(buf, len, "%.0fbit", tmp);
188 } else {
8f7574ed
ED
189 if (tmp >= 1000.0*1000000000.0)
190 snprintf(buf, len, "%.0fGbit", tmp/1000000000.0);
191 else if (tmp >= 1000.0*1000000.0)
abf1d0b0
SH
192 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
193 else if (tmp >= 1000.0 * 1000.0)
194 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
d40b38b4 195 else
abf1d0b0 196 snprintf(buf, len, "%.0fbit", tmp);
d40b38b4 197 }
aba5acdf
SH
198}
199
8f7574ed 200char * sprint_rate(__u64 rate, char *buf)
aba5acdf 201{
d40b38b4 202 print_rate(buf, SPRINT_BSIZE-1, rate);
aba5acdf
SH
203 return buf;
204}
205
8f34caaf 206int get_time(unsigned *time, const char *str)
aba5acdf
SH
207{
208 double t;
209 char *p;
210
211 t = strtod(str, &p);
212 if (p == str)
213 return -1;
214
215 if (*p) {
216 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
217 strcasecmp(p, "secs")==0)
f0bda7e5 218 t *= TIME_UNITS_PER_SEC;
aba5acdf
SH
219 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
220 strcasecmp(p, "msecs") == 0)
f0bda7e5 221 t *= TIME_UNITS_PER_SEC/1000;
aba5acdf
SH
222 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
223 strcasecmp(p, "usecs") == 0)
f0bda7e5 224 t *= TIME_UNITS_PER_SEC/1000000;
aba5acdf
SH
225 else
226 return -1;
227 }
228
8f34caaf 229 *time = t;
aba5acdf
SH
230 return 0;
231}
232
233
8f34caaf 234void print_time(char *buf, int len, __u32 time)
aba5acdf 235{
8f34caaf 236 double tmp = time;
aba5acdf 237
f0bda7e5
PM
238 if (tmp >= TIME_UNITS_PER_SEC)
239 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
240 else if (tmp >= TIME_UNITS_PER_SEC/1000)
241 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
aba5acdf 242 else
89151447 243 snprintf(buf, len, "%uus", time);
aba5acdf
SH
244}
245
8f34caaf 246char * sprint_time(__u32 time, char *buf)
aba5acdf 247{
8f34caaf 248 print_time(buf, SPRINT_BSIZE-1, time);
aba5acdf
SH
249 return buf;
250}
251
bd29e35d
PM
252char * sprint_ticks(__u32 ticks, char *buf)
253{
254 return sprint_time(tc_core_tick2time(ticks), buf);
255}
256
dbd90dc2 257int get_size(unsigned *size, const char *str)
aba5acdf
SH
258{
259 double sz;
260 char *p;
261
262 sz = strtod(str, &p);
263 if (p == str)
264 return -1;
265
266 if (*p) {
267 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
268 sz *= 1024;
dbd90dc2
SH
269 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
270 sz *= 1024*1024*1024;
271 else if (strcasecmp(p, "gbit") == 0)
272 sz *= 1024*1024*1024/8;
aba5acdf
SH
273 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
274 sz *= 1024*1024;
275 else if (strcasecmp(p, "mbit") == 0)
276 sz *= 1024*1024/8;
277 else if (strcasecmp(p, "kbit") == 0)
278 sz *= 1024/8;
279 else if (strcasecmp(p, "b") != 0)
280 return -1;
281 }
282
283 *size = sz;
284 return 0;
285}
286
287int get_size_and_cell(unsigned *size, int *cell_log, char *str)
288{
289 char * slash = strchr(str, '/');
290
291 if (slash)
292 *slash = 0;
293
294 if (get_size(size, str))
295 return -1;
296
297 if (slash) {
298 int cell;
299 int i;
300
301 if (get_integer(&cell, slash+1, 0))
302 return -1;
303 *slash = '/';
304
305 for (i=0; i<32; i++) {
306 if ((1<<i) == cell) {
307 *cell_log = i;
308 return 0;
309 }
310 }
311 return -1;
312 }
313 return 0;
314}
315
d40b38b4 316void print_size(char *buf, int len, __u32 sz)
aba5acdf
SH
317{
318 double tmp = sz;
319
320 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
321 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
322 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
323 snprintf(buf, len, "%gKb", rint(tmp/1024));
324 else
325 snprintf(buf, len, "%ub", sz);
aba5acdf
SH
326}
327
328char * sprint_size(__u32 size, char *buf)
329{
d40b38b4 330 print_size(buf, SPRINT_BSIZE-1, size);
aba5acdf
SH
331 return buf;
332}
333
d40b38b4 334void print_qdisc_handle(char *buf, int len, __u32 h)
aba5acdf
SH
335{
336 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
aba5acdf
SH
337}
338
339char * sprint_qdisc_handle(__u32 h, char *buf)
340{
d40b38b4 341 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
aba5acdf
SH
342 return buf;
343}
344
2373fde9
SH
345char * action_n2a(int action, char *buf, int len)
346{
347 switch (action) {
348 case -1:
349 return "continue";
350 break;
351 case TC_ACT_OK:
352 return "pass";
353 break;
354 case TC_ACT_SHOT:
355 return "drop";
356 break;
357 case TC_ACT_RECLASSIFY:
358 return "reclassify";
359 case TC_ACT_PIPE:
360 return "pipe";
361 case TC_ACT_STOLEN:
362 return "stolen";
363 default:
364 snprintf(buf, len, "%d", action);
365 return buf;
366 }
367}
aba5acdf 368
2373fde9
SH
369int action_a2n(char *arg, int *result)
370{
371 int res;
372
373 if (matches(arg, "continue") == 0)
374 res = -1;
375 else if (matches(arg, "drop") == 0)
376 res = TC_ACT_SHOT;
377 else if (matches(arg, "shot") == 0)
378 res = TC_ACT_SHOT;
379 else if (matches(arg, "pass") == 0)
380 res = TC_ACT_OK;
381 else if (strcmp(arg, "ok") == 0)
382 res = TC_ACT_OK;
383 else if (matches(arg, "reclassify") == 0)
384 res = TC_ACT_RECLASSIFY;
385 else {
386 char dummy;
387 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
388 return -1;
389 }
390 *result = res;
391 return 0;
392}
393
839c8456 394int get_linklayer(unsigned *val, const char *arg)
292f29b4
JDB
395{
396 int res;
397
398 if (matches(arg, "ethernet") == 0)
399 res = LINKLAYER_ETHERNET;
400 else if (matches(arg, "atm") == 0)
401 res = LINKLAYER_ATM;
402 else if (matches(arg, "adsl") == 0)
403 res = LINKLAYER_ATM;
404 else
405 return -1; /* Indicate error */
406
407 *val = res;
408 return 0;
409}
410
839c8456
JK
411void print_linklayer(char *buf, int len, unsigned linklayer)
412{
413 switch (linklayer) {
414 case LINKLAYER_UNSPEC:
415 snprintf(buf, len, "%s", "unspec");
416 return;
417 case LINKLAYER_ETHERNET:
418 snprintf(buf, len, "%s", "ethernet");
419 return;
420 case LINKLAYER_ATM:
421 snprintf(buf, len, "%s", "atm");
422 return;
423 default:
424 snprintf(buf, len, "%s", "unknown");
425 return;
426 }
427}
428
429char *sprint_linklayer(unsigned linklayer, char *buf)
430{
431 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
432 return buf;
433}
434
6dc9f016 435void print_tm(FILE * f, const struct tcf_t *tm)
2373fde9 436{
5e8bc631 437 int hz = get_user_hz();
2373fde9 438 if (tm->install != 0)
63ae25d0 439 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
2373fde9 440 if (tm->lastuse != 0)
63ae25d0 441 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
2373fde9 442 if (tm->expires != 0)
63ae25d0 443 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
2373fde9 444}
e5879dc6 445
446void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
447{
448 SPRINT_BUF(b1);
7d69fd97 449 struct rtattr *tbs[TCA_STATS_MAX + 1];
e5879dc6 450
7d69fd97 451 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
e5879dc6 452
453 if (tbs[TCA_STATS_BASIC]) {
454 struct gnet_stats_basic bs = {0};
455 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
456 fprintf(fp, "%sSent %llu bytes %u pkt",
b906243b 457 prefix, (unsigned long long) bs.bytes, bs.packets);
e5879dc6 458 }
459
460 if (tbs[TCA_STATS_QUEUE]) {
461 struct gnet_stats_queue q = {0};
462 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
463 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
464 q.drops, q.overlimits, q.requeues);
465 }
ae665a52 466
8f7574ed
ED
467 if (tbs[TCA_STATS_RATE_EST64]) {
468 struct gnet_stats_rate_est64 re = {0};
469
470 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
471 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
472 sizeof(re)));
473 fprintf(fp, "\n%srate %s %llupps ",
474 prefix, sprint_rate(re.bps, b1), re.pps);
475 } else if (tbs[TCA_STATS_RATE_EST]) {
e5879dc6 476 struct gnet_stats_rate_est re = {0};
8f7574ed
ED
477
478 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
479 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
e5879dc6 480 fprintf(fp, "\n%srate %s %upps ",
481 prefix, sprint_rate(re.bps, b1), re.pps);
482 }
483
484 if (tbs[TCA_STATS_QUEUE]) {
485 struct gnet_stats_queue q = {0};
486 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
487 if (!tbs[TCA_STATS_RATE_EST])
488 fprintf(fp, "\n%s", prefix);
489 fprintf(fp, "backlog %s %up requeues %u ",
490 sprint_size(q.backlog, b1), q.qlen, q.requeues);
491 }
492
493 if (xstats)
494 *xstats = tbs[TCA_STATS_APP] ? : NULL;
495}
496
497void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
498{
499 SPRINT_BUF(b1);
500
501 if (tb[TCA_STATS2]) {
502 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
503 if (xstats && NULL == *xstats)
504 goto compat_xstats;
505 return;
506 }
507 /* backward compatibility */
508 if (tb[TCA_STATS]) {
509 struct tc_stats st;
510
511 /* handle case where kernel returns more/less than we know about */
512 memset(&st, 0, sizeof(st));
513 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
514
515 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
ae665a52 516 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
e5879dc6 517 st.overlimits);
518
519 if (st.bps || st.pps || st.qlen || st.backlog) {
520 fprintf(fp, "\n%s", prefix);
521 if (st.bps || st.pps) {
522 fprintf(fp, "rate ");
523 if (st.bps)
524 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
525 if (st.pps)
526 fprintf(fp, "%upps ", st.pps);
527 }
528 if (st.qlen || st.backlog) {
529 fprintf(fp, "backlog ");
530 if (st.backlog)
531 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
532 if (st.qlen)
533 fprintf(fp, "%up ", st.qlen);
534 }
535 }
536 }
537
538compat_xstats:
539 if (tb[TCA_XSTATS] && xstats)
540 *xstats = tb[TCA_XSTATS];
541}
542