]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_util.c
2 * tc_util.c Misc TC utility functions.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
28 #define LIBDIR "/usr/lib"
31 const char *get_tc_lib(void)
35 lib_dir
= getenv("TC_LIB_DIR");
37 lib_dir
= LIBDIR
"/tc/";
42 int get_qdisc_handle(__u32
*h
, const char *str
)
48 if (strcmp(str
, "none") == 0)
50 maj
= strtoul(str
, &p
, 16);
54 if (*p
!= ':' && *p
!=0)
61 int get_tc_classid(__u32
*h
, const char *str
)
67 if (strcmp(str
, "root") == 0)
70 if (strcmp(str
, "none") == 0)
72 maj
= strtoul(str
, &p
, 16);
83 min
= strtoul(str
, &p
, 16);
97 int print_tc_classid(char *buf
, int len
, __u32 h
)
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);
108 snprintf(buf
, len
, "%x:%x", TC_H_MAJ(h
)>>16, TC_H_MIN(h
));
112 char * sprint_tc_classid(__u32 h
, char *buf
)
114 if (print_tc_classid(buf
, SPRINT_BSIZE
-1, h
))
119 /* See http://physics.nist.gov/cuu/Units/binary.html */
120 static const struct rate_suffix
{
127 { "mibit", 1024.*1024. },
128 { "mbit", 1000000. },
129 { "gibit", 1024.*1024.*1024. },
130 { "gbit", 1000000000. },
131 { "tibit", 1024.*1024.*1024.*1024. },
132 { "tbit", 1000000000000. },
134 { "KiBps", 8.*1024. },
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. },
146 int get_rate(unsigned *rate
, const char *str
)
149 double bps
= strtod(str
, &p
);
150 const struct rate_suffix
*s
;
155 for (s
= suffixes
; s
->name
; ++s
) {
156 if (strcasecmp(s
->name
, p
) == 0) {
164 return -1; /* unknown suffix */
166 bps
/= 8; /* -> bytes per second */
168 /* detect if an overflow happened */
169 if (*rate
!= floor(bps
))
174 int get_rate64(__u64
*rate
, const char *str
)
177 double bps
= strtod(str
, &p
);
178 const struct rate_suffix
*s
;
183 for (s
= suffixes
; s
->name
; ++s
) {
184 if (strcasecmp(s
->name
, p
) == 0) {
192 return -1; /* unknown suffix */
194 bps
/= 8; /* -> bytes per second */
199 void print_rate(char *buf
, int len
, __u64 rate
)
202 unsigned long kilo
= use_iec
? 1024 : 1000;
203 const char *str
= use_iec
? "i" : "";
205 static char *units
[5] = {"", "K", "M", "G", "T"};
207 rate
<<= 3; /* bytes/sec -> bits/sec */
209 for (i
= 0; i
< ARRAY_SIZE(units
); i
++) {
212 if (((rate
% kilo
) != 0) && rate
< 1000*kilo
)
216 snprintf(buf
, len
, "%.0f%s%sbit", (double)rate
, units
[i
], str
);
219 char * sprint_rate(__u64 rate
, char *buf
)
221 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
225 int get_time(unsigned *time
, const char *str
)
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;
253 void print_time(char *buf
, int len
, __u32 time
)
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));
262 snprintf(buf
, len
, "%uus", time
);
265 char * sprint_time(__u32 time
, char *buf
)
267 print_time(buf
, SPRINT_BSIZE
-1, time
);
271 char * sprint_ticks(__u32 ticks
, char *buf
)
273 return sprint_time(tc_core_tick2time(ticks
), buf
);
276 int get_size(unsigned *size
, const char *str
)
281 sz
= strtod(str
, &p
);
286 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k")==0)
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)
294 else if (strcasecmp(p
, "mbit") == 0)
296 else if (strcasecmp(p
, "kbit") == 0)
298 else if (strcasecmp(p
, "b") != 0)
306 int get_size_and_cell(unsigned *size
, int *cell_log
, char *str
)
308 char * slash
= strchr(str
, '/');
313 if (get_size(size
, str
))
320 if (get_integer(&cell
, slash
+1, 0))
324 for (i
=0; i
<32; i
++) {
325 if ((1<<i
) == cell
) {
335 void print_size(char *buf
, int len
, __u32 sz
)
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));
344 snprintf(buf
, len
, "%ub", sz
);
347 char * sprint_size(__u32 size
, char *buf
)
349 print_size(buf
, SPRINT_BSIZE
-1, size
);
353 void print_qdisc_handle(char *buf
, int len
, __u32 h
)
355 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
358 char * sprint_qdisc_handle(__u32 h
, char *buf
)
360 print_qdisc_handle(buf
, SPRINT_BSIZE
-1, h
);
364 char * action_n2a(int action
, char *buf
, int len
)
376 case TC_ACT_RECLASSIFY
:
383 snprintf(buf
, len
, "%d", action
);
388 int action_a2n(char *arg
, int *result
)
392 if (matches(arg
, "continue") == 0)
394 else if (matches(arg
, "drop") == 0)
396 else if (matches(arg
, "shot") == 0)
398 else if (matches(arg
, "pass") == 0)
400 else if (strcmp(arg
, "ok") == 0)
402 else if (matches(arg
, "reclassify") == 0)
403 res
= TC_ACT_RECLASSIFY
;
406 if (sscanf(arg
, "%d%c", &res
, &dummy
) != 1)
413 int get_linklayer(unsigned *val
, const char *arg
)
417 if (matches(arg
, "ethernet") == 0)
418 res
= LINKLAYER_ETHERNET
;
419 else if (matches(arg
, "atm") == 0)
421 else if (matches(arg
, "adsl") == 0)
424 return -1; /* Indicate error */
430 void print_linklayer(char *buf
, int len
, unsigned linklayer
)
433 case LINKLAYER_UNSPEC
:
434 snprintf(buf
, len
, "%s", "unspec");
436 case LINKLAYER_ETHERNET
:
437 snprintf(buf
, len
, "%s", "ethernet");
440 snprintf(buf
, len
, "%s", "atm");
443 snprintf(buf
, len
, "%s", "unknown");
448 char *sprint_linklayer(unsigned linklayer
, char *buf
)
450 print_linklayer(buf
, SPRINT_BSIZE
-1, linklayer
);
454 void print_tm(FILE * f
, const struct tcf_t
*tm
)
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
));
465 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
468 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1];
470 parse_rtattr_nested(tbs
, TCA_STATS_MAX
, rta
);
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
);
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
);
486 if (tbs
[TCA_STATS_RATE_EST64
]) {
487 struct gnet_stats_rate_est64 re
= {0};
489 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST64
]),
490 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST64
]),
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};
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
);
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
);
513 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
516 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
520 if (tb
[TCA_STATS2
]) {
521 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
522 if (xstats
&& NULL
== *xstats
)
526 /* backward compatibility */
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
)));
534 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
535 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
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 ");
543 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
545 fprintf(fp
, "%upps ", st
.pps
);
547 if (st
.qlen
|| st
.backlog
) {
548 fprintf(fp
, "backlog ");
550 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
552 fprintf(fp
, "%up ", st
.qlen
);
558 if (tb
[TCA_XSTATS
] && xstats
)
559 *xstats
= tb
[TCA_XSTATS
];