]>
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 void print_rate(char *buf
, int len
, __u64 rate
)
176 double tmp
= (double)rate
*8;
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)
183 snprintf(buf
, len
, "%.0fMibit", tmp
/(1024.0*1024.0));
184 else if (tmp
>= 1000.0*1024)
185 snprintf(buf
, len
, "%.0fKibit", tmp
/1024);
187 snprintf(buf
, len
, "%.0fbit", tmp
);
189 if (tmp
>= 1000.0*1000000000.0)
190 snprintf(buf
, len
, "%.0fGbit", tmp
/1000000000.0);
191 else if (tmp
>= 1000.0*1000000.0)
192 snprintf(buf
, len
, "%.0fMbit", tmp
/1000000.0);
193 else if (tmp
>= 1000.0 * 1000.0)
194 snprintf(buf
, len
, "%.0fKbit", tmp
/1000.0);
196 snprintf(buf
, len
, "%.0fbit", tmp
);
200 char * sprint_rate(__u64 rate
, char *buf
)
202 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
206 int get_time(unsigned *time
, const char *str
)
216 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec")==0 ||
217 strcasecmp(p
, "secs")==0)
218 t
*= TIME_UNITS_PER_SEC
;
219 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec")==0 ||
220 strcasecmp(p
, "msecs") == 0)
221 t
*= TIME_UNITS_PER_SEC
/1000;
222 else if (strcasecmp(p
, "us") == 0 || strcasecmp(p
, "usec")==0 ||
223 strcasecmp(p
, "usecs") == 0)
224 t
*= TIME_UNITS_PER_SEC
/1000000;
234 void print_time(char *buf
, int len
, __u32 time
)
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));
243 snprintf(buf
, len
, "%uus", time
);
246 char * sprint_time(__u32 time
, char *buf
)
248 print_time(buf
, SPRINT_BSIZE
-1, time
);
252 char * sprint_ticks(__u32 ticks
, char *buf
)
254 return sprint_time(tc_core_tick2time(ticks
), buf
);
257 int get_size(unsigned *size
, const char *str
)
262 sz
= strtod(str
, &p
);
267 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k")==0)
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;
273 else if (strcasecmp(p
, "mb") == 0 || strcasecmp(p
, "m")==0)
275 else if (strcasecmp(p
, "mbit") == 0)
277 else if (strcasecmp(p
, "kbit") == 0)
279 else if (strcasecmp(p
, "b") != 0)
287 int get_size_and_cell(unsigned *size
, int *cell_log
, char *str
)
289 char * slash
= strchr(str
, '/');
294 if (get_size(size
, str
))
301 if (get_integer(&cell
, slash
+1, 0))
305 for (i
=0; i
<32; i
++) {
306 if ((1<<i
) == cell
) {
316 void print_size(char *buf
, int len
, __u32 sz
)
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));
325 snprintf(buf
, len
, "%ub", sz
);
328 char * sprint_size(__u32 size
, char *buf
)
330 print_size(buf
, SPRINT_BSIZE
-1, size
);
334 void print_qdisc_handle(char *buf
, int len
, __u32 h
)
336 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
339 char * sprint_qdisc_handle(__u32 h
, char *buf
)
341 print_qdisc_handle(buf
, SPRINT_BSIZE
-1, h
);
345 char * action_n2a(int action
, char *buf
, int len
)
357 case TC_ACT_RECLASSIFY
:
364 snprintf(buf
, len
, "%d", action
);
369 int action_a2n(char *arg
, int *result
)
373 if (matches(arg
, "continue") == 0)
375 else if (matches(arg
, "drop") == 0)
377 else if (matches(arg
, "shot") == 0)
379 else if (matches(arg
, "pass") == 0)
381 else if (strcmp(arg
, "ok") == 0)
383 else if (matches(arg
, "reclassify") == 0)
384 res
= TC_ACT_RECLASSIFY
;
387 if (sscanf(arg
, "%d%c", &res
, &dummy
) != 1)
394 int get_linklayer(unsigned *val
, const char *arg
)
398 if (matches(arg
, "ethernet") == 0)
399 res
= LINKLAYER_ETHERNET
;
400 else if (matches(arg
, "atm") == 0)
402 else if (matches(arg
, "adsl") == 0)
405 return -1; /* Indicate error */
411 void print_linklayer(char *buf
, int len
, unsigned linklayer
)
414 case LINKLAYER_UNSPEC
:
415 snprintf(buf
, len
, "%s", "unspec");
417 case LINKLAYER_ETHERNET
:
418 snprintf(buf
, len
, "%s", "ethernet");
421 snprintf(buf
, len
, "%s", "atm");
424 snprintf(buf
, len
, "%s", "unknown");
429 char *sprint_linklayer(unsigned linklayer
, char *buf
)
431 print_linklayer(buf
, SPRINT_BSIZE
-1, linklayer
);
435 void print_tm(FILE * f
, const struct tcf_t
*tm
)
437 int hz
= get_user_hz();
438 if (tm
->install
!= 0)
439 fprintf(f
, " installed %u sec", (unsigned)(tm
->install
/hz
));
440 if (tm
->lastuse
!= 0)
441 fprintf(f
, " used %u sec", (unsigned)(tm
->lastuse
/hz
));
442 if (tm
->expires
!= 0)
443 fprintf(f
, " expires %u sec", (unsigned)(tm
->expires
/hz
));
446 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
449 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1];
451 parse_rtattr_nested(tbs
, TCA_STATS_MAX
, rta
);
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",
457 prefix
, (unsigned long long) bs
.bytes
, bs
.packets
);
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
);
467 if (tbs
[TCA_STATS_RATE_EST64
]) {
468 struct gnet_stats_rate_est64 re
= {0};
470 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST64
]),
471 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST64
]),
473 fprintf(fp
, "\n%srate %s %llupps ",
474 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
475 } else if (tbs
[TCA_STATS_RATE_EST
]) {
476 struct gnet_stats_rate_est re
= {0};
478 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST
]),
479 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST
]), sizeof(re
)));
480 fprintf(fp
, "\n%srate %s %upps ",
481 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
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
);
494 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
497 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
501 if (tb
[TCA_STATS2
]) {
502 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
503 if (xstats
&& NULL
== *xstats
)
507 /* backward compatibility */
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
)));
515 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
516 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
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 ");
524 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
526 fprintf(fp
, "%upps ", st
.pps
);
528 if (st
.qlen
|| st
.backlog
) {
529 fprintf(fp
, "backlog ");
531 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
533 fprintf(fp
, "%up ", st
.qlen
);
539 if (tb
[TCA_XSTATS
] && xstats
)
540 *xstats
= tb
[TCA_XSTATS
];