]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_util.c
Merge branch 'master' 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 <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 if (*p == '\0') {
156 *rate = bps / 8.; /* assume bits/sec */
157 return 0;
158 }
159
160 for (s = suffixes; s->name; ++s) {
161 if (strcasecmp(s->name, p) == 0) {
162 *rate = (bps * s->scale) / 8.;
163 return 0;
164 }
165 }
166
167 return -1;
168 }
169
170 void print_rate(char *buf, int len, __u32 rate)
171 {
172 double tmp = (double)rate*8;
173 extern int use_iec;
174
175 if (use_iec) {
176 if (tmp >= 1000.0*1024.0*1024.0)
177 snprintf(buf, len, "%.0fMibit", tmp/(1024.0*1024.0));
178 else if (tmp >= 1000.0*1024)
179 snprintf(buf, len, "%.0fKibit", tmp/1024);
180 else
181 snprintf(buf, len, "%.0fbit", tmp);
182 } else {
183 if (tmp >= 1000.0*1000000.0)
184 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
185 else if (tmp >= 1000.0 * 1000.0)
186 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
187 else
188 snprintf(buf, len, "%.0fbit", tmp);
189 }
190 }
191
192 char * sprint_rate(__u32 rate, char *buf)
193 {
194 print_rate(buf, SPRINT_BSIZE-1, rate);
195 return buf;
196 }
197
198 int get_time(unsigned *time, const char *str)
199 {
200 double t;
201 char *p;
202
203 t = strtod(str, &p);
204 if (p == str)
205 return -1;
206
207 if (*p) {
208 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
209 strcasecmp(p, "secs")==0)
210 t *= TIME_UNITS_PER_SEC;
211 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
212 strcasecmp(p, "msecs") == 0)
213 t *= TIME_UNITS_PER_SEC/1000;
214 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
215 strcasecmp(p, "usecs") == 0)
216 t *= TIME_UNITS_PER_SEC/1000000;
217 else
218 return -1;
219 }
220
221 *time = t;
222 return 0;
223 }
224
225
226 void print_time(char *buf, int len, __u32 time)
227 {
228 double tmp = time;
229
230 if (tmp >= TIME_UNITS_PER_SEC)
231 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
232 else if (tmp >= TIME_UNITS_PER_SEC/1000)
233 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
234 else
235 snprintf(buf, len, "%uus", time);
236 }
237
238 char * sprint_time(__u32 time, char *buf)
239 {
240 print_time(buf, SPRINT_BSIZE-1, time);
241 return buf;
242 }
243
244 char * sprint_ticks(__u32 ticks, char *buf)
245 {
246 return sprint_time(tc_core_tick2time(ticks), buf);
247 }
248
249 int get_size(unsigned *size, const char *str)
250 {
251 double sz;
252 char *p;
253
254 sz = strtod(str, &p);
255 if (p == str)
256 return -1;
257
258 if (*p) {
259 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
260 sz *= 1024;
261 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
262 sz *= 1024*1024*1024;
263 else if (strcasecmp(p, "gbit") == 0)
264 sz *= 1024*1024*1024/8;
265 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
266 sz *= 1024*1024;
267 else if (strcasecmp(p, "mbit") == 0)
268 sz *= 1024*1024/8;
269 else if (strcasecmp(p, "kbit") == 0)
270 sz *= 1024/8;
271 else if (strcasecmp(p, "b") != 0)
272 return -1;
273 }
274
275 *size = sz;
276 return 0;
277 }
278
279 int get_size_and_cell(unsigned *size, int *cell_log, char *str)
280 {
281 char * slash = strchr(str, '/');
282
283 if (slash)
284 *slash = 0;
285
286 if (get_size(size, str))
287 return -1;
288
289 if (slash) {
290 int cell;
291 int i;
292
293 if (get_integer(&cell, slash+1, 0))
294 return -1;
295 *slash = '/';
296
297 for (i=0; i<32; i++) {
298 if ((1<<i) == cell) {
299 *cell_log = i;
300 return 0;
301 }
302 }
303 return -1;
304 }
305 return 0;
306 }
307
308 void print_size(char *buf, int len, __u32 sz)
309 {
310 double tmp = sz;
311
312 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
313 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
314 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
315 snprintf(buf, len, "%gKb", rint(tmp/1024));
316 else
317 snprintf(buf, len, "%ub", sz);
318 }
319
320 char * sprint_size(__u32 size, char *buf)
321 {
322 print_size(buf, SPRINT_BSIZE-1, size);
323 return buf;
324 }
325
326 void print_qdisc_handle(char *buf, int len, __u32 h)
327 {
328 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
329 }
330
331 char * sprint_qdisc_handle(__u32 h, char *buf)
332 {
333 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
334 return buf;
335 }
336
337 char * action_n2a(int action, char *buf, int len)
338 {
339 switch (action) {
340 case -1:
341 return "continue";
342 break;
343 case TC_ACT_OK:
344 return "pass";
345 break;
346 case TC_ACT_SHOT:
347 return "drop";
348 break;
349 case TC_ACT_RECLASSIFY:
350 return "reclassify";
351 case TC_ACT_PIPE:
352 return "pipe";
353 case TC_ACT_STOLEN:
354 return "stolen";
355 default:
356 snprintf(buf, len, "%d", action);
357 return buf;
358 }
359 }
360
361 int action_a2n(char *arg, int *result)
362 {
363 int res;
364
365 if (matches(arg, "continue") == 0)
366 res = -1;
367 else if (matches(arg, "drop") == 0)
368 res = TC_ACT_SHOT;
369 else if (matches(arg, "shot") == 0)
370 res = TC_ACT_SHOT;
371 else if (matches(arg, "pass") == 0)
372 res = TC_ACT_OK;
373 else if (strcmp(arg, "ok") == 0)
374 res = TC_ACT_OK;
375 else if (matches(arg, "reclassify") == 0)
376 res = TC_ACT_RECLASSIFY;
377 else {
378 char dummy;
379 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
380 return -1;
381 }
382 *result = res;
383 return 0;
384 }
385
386 int get_linklayer(unsigned *val, const char *arg)
387 {
388 int res;
389
390 if (matches(arg, "ethernet") == 0)
391 res = LINKLAYER_ETHERNET;
392 else if (matches(arg, "atm") == 0)
393 res = LINKLAYER_ATM;
394 else if (matches(arg, "adsl") == 0)
395 res = LINKLAYER_ATM;
396 else
397 return -1; /* Indicate error */
398
399 *val = res;
400 return 0;
401 }
402
403 void print_linklayer(char *buf, int len, unsigned linklayer)
404 {
405 switch (linklayer) {
406 case LINKLAYER_UNSPEC:
407 snprintf(buf, len, "%s", "unspec");
408 return;
409 case LINKLAYER_ETHERNET:
410 snprintf(buf, len, "%s", "ethernet");
411 return;
412 case LINKLAYER_ATM:
413 snprintf(buf, len, "%s", "atm");
414 return;
415 default:
416 snprintf(buf, len, "%s", "unknown");
417 return;
418 }
419 }
420
421 char *sprint_linklayer(unsigned linklayer, char *buf)
422 {
423 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
424 return buf;
425 }
426
427 void print_tm(FILE * f, const struct tcf_t *tm)
428 {
429 int hz = get_user_hz();
430 if (tm->install != 0)
431 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
432 if (tm->lastuse != 0)
433 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
434 if (tm->expires != 0)
435 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
436 }
437
438 void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
439 {
440 SPRINT_BUF(b1);
441 struct rtattr *tbs[TCA_STATS_MAX + 1];
442
443 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
444
445 if (tbs[TCA_STATS_BASIC]) {
446 struct gnet_stats_basic bs = {0};
447 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
448 fprintf(fp, "%sSent %llu bytes %u pkt",
449 prefix, (unsigned long long) bs.bytes, bs.packets);
450 }
451
452 if (tbs[TCA_STATS_QUEUE]) {
453 struct gnet_stats_queue q = {0};
454 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
455 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
456 q.drops, q.overlimits, q.requeues);
457 }
458
459 if (tbs[TCA_STATS_RATE_EST]) {
460 struct gnet_stats_rate_est re = {0};
461 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
462 fprintf(fp, "\n%srate %s %upps ",
463 prefix, sprint_rate(re.bps, b1), re.pps);
464 }
465
466 if (tbs[TCA_STATS_QUEUE]) {
467 struct gnet_stats_queue q = {0};
468 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
469 if (!tbs[TCA_STATS_RATE_EST])
470 fprintf(fp, "\n%s", prefix);
471 fprintf(fp, "backlog %s %up requeues %u ",
472 sprint_size(q.backlog, b1), q.qlen, q.requeues);
473 }
474
475 if (xstats)
476 *xstats = tbs[TCA_STATS_APP] ? : NULL;
477 }
478
479 void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
480 {
481 SPRINT_BUF(b1);
482
483 if (tb[TCA_STATS2]) {
484 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
485 if (xstats && NULL == *xstats)
486 goto compat_xstats;
487 return;
488 }
489 /* backward compatibility */
490 if (tb[TCA_STATS]) {
491 struct tc_stats st;
492
493 /* handle case where kernel returns more/less than we know about */
494 memset(&st, 0, sizeof(st));
495 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
496
497 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
498 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
499 st.overlimits);
500
501 if (st.bps || st.pps || st.qlen || st.backlog) {
502 fprintf(fp, "\n%s", prefix);
503 if (st.bps || st.pps) {
504 fprintf(fp, "rate ");
505 if (st.bps)
506 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
507 if (st.pps)
508 fprintf(fp, "%upps ", st.pps);
509 }
510 if (st.qlen || st.backlog) {
511 fprintf(fp, "backlog ");
512 if (st.backlog)
513 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
514 if (st.qlen)
515 fprintf(fp, "%up ", st.qlen);
516 }
517 }
518 }
519
520 compat_xstats:
521 if (tb[TCA_XSTATS] && xstats)
522 *xstats = tb[TCA_XSTATS];
523 }
524