blob: fe2c7eb53e6bb0d4d4deecc84ef8357130b78e66 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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
Andreas Henriksson5e3bb532008-08-22 16:54:12 +020027#ifndef LIBDIR
28#define LIBDIR "/usr/lib/"
Rafael Almeidab514b352008-06-01 21:33:44 -030029#endif
30
Stephen Hemmingeraa27f882007-06-20 15:27:22 -070031const char *get_tc_lib(void)
32{
33 const char *lib_dir;
34
35 lib_dir = getenv("TC_LIB_DIR");
36 if (!lib_dir)
Andreas Henriksson5e3bb532008-08-22 16:54:12 +020037 lib_dir = LIBDIR "/tc/";
Stephen Hemmingeraa27f882007-06-20 15:27:22 -070038
39 return lib_dir;
40}
41
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000042int get_qdisc_handle(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000043{
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
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000061int get_tc_classid(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000062{
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 == ':') {
osdl.net!shemmingera8b303c2005-02-07 18:16:29 +000079 if (maj >= (1<<16))
80 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000081 maj <<= 16;
82 str = p+1;
83 min = strtoul(str, &p, 16);
84 if (*p != 0)
85 return -1;
osdl.net!shemmingera8b303c2005-02-07 18:16:29 +000086 if (min >= (1<<16))
87 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000088 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
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000119/* 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
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000146int get_rate(unsigned *rate, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000147{
148 char *p;
149 double bps = strtod(str, &p);
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000150 const struct rate_suffix *s;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000151
152 if (p == str)
153 return -1;
154
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000155 if (*p == '\0') {
156 *rate = bps / 8.; /* assume bytes/sec */
157 return 0;
158 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000159
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000160 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;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000168}
169
170int get_rate_and_cell(unsigned *rate, int *cell_log, char *str)
171{
172 char * slash = strchr(str, '/');
173
174 if (slash)
175 *slash = 0;
176
177 if (get_rate(rate, str))
178 return -1;
179
180 if (slash) {
181 int cell;
182 int i;
183
184 if (get_integer(&cell, slash+1, 0))
185 return -1;
186 *slash = '/';
187
188 for (i=0; i<32; i++) {
189 if ((1<<i) == cell) {
190 *cell_log = i;
191 return 0;
192 }
193 }
194 return -1;
195 }
196 return 0;
197}
198
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000199void print_rate(char *buf, int len, __u32 rate)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000200{
201 double tmp = (double)rate*8;
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000202 extern int use_iec;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000203
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000204 if (use_iec) {
osdl.net!shemmingerabf1d0b2004-10-19 19:51:41 +0000205 if (tmp >= 1000.0*1024.0*1024.0)
206 snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0);
207 else if (tmp >= 1000.0*1024)
208 snprintf(buf, len, "%.0fKibit", tmp/1024);
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000209 else
osdl.net!shemmingerabf1d0b2004-10-19 19:51:41 +0000210 snprintf(buf, len, "%.0fbit", tmp);
211 } else {
212 if (tmp >= 1000.0*1000000.0)
213 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
214 else if (tmp >= 1000.0 * 1000.0)
215 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000216 else
osdl.net!shemmingerabf1d0b2004-10-19 19:51:41 +0000217 snprintf(buf, len, "%.0fbit", tmp);
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000218 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000219}
220
221char * sprint_rate(__u32 rate, char *buf)
222{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000223 print_rate(buf, SPRINT_BSIZE-1, rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000224 return buf;
225}
226
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100227int get_time(unsigned *time, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000228{
229 double t;
230 char *p;
231
232 t = strtod(str, &p);
233 if (p == str)
234 return -1;
235
236 if (*p) {
237 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
238 strcasecmp(p, "secs")==0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100239 t *= TIME_UNITS_PER_SEC;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000240 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
241 strcasecmp(p, "msecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100242 t *= TIME_UNITS_PER_SEC/1000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000243 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
244 strcasecmp(p, "usecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100245 t *= TIME_UNITS_PER_SEC/1000000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000246 else
247 return -1;
248 }
249
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100250 *time = t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000251 return 0;
252}
253
254
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100255void print_time(char *buf, int len, __u32 time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000256{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100257 double tmp = time;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000258
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100259 if (tmp >= TIME_UNITS_PER_SEC)
260 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
261 else if (tmp >= TIME_UNITS_PER_SEC/1000)
262 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000263 else
Stephen Hemminger89151442007-03-14 10:14:07 -0700264 snprintf(buf, len, "%uus", time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000265}
266
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100267char * sprint_time(__u32 time, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000268{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100269 print_time(buf, SPRINT_BSIZE-1, time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000270 return buf;
271}
272
Patrick McHardybd29e352007-03-04 20:15:01 +0100273char * sprint_ticks(__u32 ticks, char *buf)
274{
275 return sprint_time(tc_core_tick2time(ticks), buf);
276}
277
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000278int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000279{
280 double sz;
281 char *p;
282
283 sz = strtod(str, &p);
284 if (p == str)
285 return -1;
286
287 if (*p) {
288 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
289 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000290 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
291 sz *= 1024*1024*1024;
292 else if (strcasecmp(p, "gbit") == 0)
293 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000294 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
295 sz *= 1024*1024;
296 else if (strcasecmp(p, "mbit") == 0)
297 sz *= 1024*1024/8;
298 else if (strcasecmp(p, "kbit") == 0)
299 sz *= 1024/8;
300 else if (strcasecmp(p, "b") != 0)
301 return -1;
302 }
303
304 *size = sz;
305 return 0;
306}
307
308int get_size_and_cell(unsigned *size, int *cell_log, char *str)
309{
310 char * slash = strchr(str, '/');
311
312 if (slash)
313 *slash = 0;
314
315 if (get_size(size, str))
316 return -1;
317
318 if (slash) {
319 int cell;
320 int i;
321
322 if (get_integer(&cell, slash+1, 0))
323 return -1;
324 *slash = '/';
325
326 for (i=0; i<32; i++) {
327 if ((1<<i) == cell) {
328 *cell_log = i;
329 return 0;
330 }
331 }
332 return -1;
333 }
334 return 0;
335}
336
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000337void print_size(char *buf, int len, __u32 sz)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000338{
339 double tmp = sz;
340
341 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
342 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
343 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
344 snprintf(buf, len, "%gKb", rint(tmp/1024));
345 else
346 snprintf(buf, len, "%ub", sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000347}
348
349char * sprint_size(__u32 size, char *buf)
350{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000351 print_size(buf, SPRINT_BSIZE-1, size);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000352 return buf;
353}
354
osdl.net!shemminger25f86902004-08-24 18:54:49 +0000355static const double max_percent_value = 0xffffffff;
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000356
357int get_percent(__u32 *percent, const char *str)
358{
359 char *p;
osdl.net!shemminger25f86902004-08-24 18:54:49 +0000360 double per = strtod(str, &p) / 100.;
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000361
osdl.net!shemminger25f86902004-08-24 18:54:49 +0000362 if (per > 1. || per < 0)
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000363 return -1;
364 if (*p && strcmp(p, "%"))
365 return -1;
366
osdl.net!shemminger25f86902004-08-24 18:54:49 +0000367 *percent = (unsigned) rint(per * max_percent_value);
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000368 return 0;
369}
370
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000371void print_percent(char *buf, int len, __u32 per)
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000372{
osdl.net!shemminger25f86902004-08-24 18:54:49 +0000373 snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value);
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000374}
375
376char * sprint_percent(__u32 per, char *buf)
377{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000378 print_percent(buf, SPRINT_BSIZE-1, per);
osdl.net!shemminger63e989f2004-06-25 20:59:28 +0000379 return buf;
380}
381
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000382void print_qdisc_handle(char *buf, int len, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000383{
384 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000385}
386
387char * sprint_qdisc_handle(__u32 h, char *buf)
388{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000389 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000390 return buf;
391}
392
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000393char * action_n2a(int action, char *buf, int len)
394{
395 switch (action) {
396 case -1:
397 return "continue";
398 break;
399 case TC_ACT_OK:
400 return "pass";
401 break;
402 case TC_ACT_SHOT:
403 return "drop";
404 break;
405 case TC_ACT_RECLASSIFY:
406 return "reclassify";
407 case TC_ACT_PIPE:
408 return "pipe";
409 case TC_ACT_STOLEN:
410 return "stolen";
411 default:
412 snprintf(buf, len, "%d", action);
413 return buf;
414 }
415}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000416
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000417int action_a2n(char *arg, int *result)
418{
419 int res;
420
421 if (matches(arg, "continue") == 0)
422 res = -1;
423 else if (matches(arg, "drop") == 0)
424 res = TC_ACT_SHOT;
425 else if (matches(arg, "shot") == 0)
426 res = TC_ACT_SHOT;
427 else if (matches(arg, "pass") == 0)
428 res = TC_ACT_OK;
429 else if (strcmp(arg, "ok") == 0)
430 res = TC_ACT_OK;
431 else if (matches(arg, "reclassify") == 0)
432 res = TC_ACT_RECLASSIFY;
433 else {
434 char dummy;
435 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
436 return -1;
437 }
438 *result = res;
439 return 0;
440}
441
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300442int get_linklayer(unsigned *val, const char *arg)
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200443{
444 int res;
445
446 if (matches(arg, "ethernet") == 0)
447 res = LINKLAYER_ETHERNET;
448 else if (matches(arg, "atm") == 0)
449 res = LINKLAYER_ATM;
450 else if (matches(arg, "adsl") == 0)
451 res = LINKLAYER_ATM;
452 else
453 return -1; /* Indicate error */
454
455 *val = res;
456 return 0;
457}
458
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300459void print_linklayer(char *buf, int len, unsigned linklayer)
460{
461 switch (linklayer) {
462 case LINKLAYER_UNSPEC:
463 snprintf(buf, len, "%s", "unspec");
464 return;
465 case LINKLAYER_ETHERNET:
466 snprintf(buf, len, "%s", "ethernet");
467 return;
468 case LINKLAYER_ATM:
469 snprintf(buf, len, "%s", "atm");
470 return;
471 default:
472 snprintf(buf, len, "%s", "unknown");
473 return;
474 }
475}
476
477char *sprint_linklayer(unsigned linklayer, char *buf)
478{
479 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
480 return buf;
481}
482
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000483void print_tm(FILE * f, const struct tcf_t *tm)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000484{
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000485 int hz = get_user_hz();
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000486 if (tm->install != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000487 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000488 if (tm->lastuse != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000489 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000490 if (tm->expires != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000491 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000492}
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000493
494void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
495{
496 SPRINT_BUF(b1);
14!tgraf7d69fd92005-01-18 22:11:58 +0000497 struct rtattr *tbs[TCA_STATS_MAX + 1];
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000498
14!tgraf7d69fd92005-01-18 22:11:58 +0000499 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000500
501 if (tbs[TCA_STATS_BASIC]) {
502 struct gnet_stats_basic bs = {0};
503 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
504 fprintf(fp, "%sSent %llu bytes %u pkt",
net[shemminger]!shemmingerb9062432005-01-17 23:28:16 +0000505 prefix, (unsigned long long) bs.bytes, bs.packets);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000506 }
507
508 if (tbs[TCA_STATS_QUEUE]) {
509 struct gnet_stats_queue q = {0};
510 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
511 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
512 q.drops, q.overlimits, q.requeues);
513 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800514
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000515 if (tbs[TCA_STATS_RATE_EST]) {
516 struct gnet_stats_rate_est re = {0};
517 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
518 fprintf(fp, "\n%srate %s %upps ",
519 prefix, sprint_rate(re.bps, b1), re.pps);
520 }
521
522 if (tbs[TCA_STATS_QUEUE]) {
523 struct gnet_stats_queue q = {0};
524 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
525 if (!tbs[TCA_STATS_RATE_EST])
526 fprintf(fp, "\n%s", prefix);
527 fprintf(fp, "backlog %s %up requeues %u ",
528 sprint_size(q.backlog, b1), q.qlen, q.requeues);
529 }
530
531 if (xstats)
532 *xstats = tbs[TCA_STATS_APP] ? : NULL;
533}
534
535void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
536{
537 SPRINT_BUF(b1);
538
539 if (tb[TCA_STATS2]) {
540 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
541 if (xstats && NULL == *xstats)
542 goto compat_xstats;
543 return;
544 }
545 /* backward compatibility */
546 if (tb[TCA_STATS]) {
547 struct tc_stats st;
548
549 /* handle case where kernel returns more/less than we know about */
550 memset(&st, 0, sizeof(st));
551 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
552
553 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800554 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000555 st.overlimits);
556
557 if (st.bps || st.pps || st.qlen || st.backlog) {
558 fprintf(fp, "\n%s", prefix);
559 if (st.bps || st.pps) {
560 fprintf(fp, "rate ");
561 if (st.bps)
562 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
563 if (st.pps)
564 fprintf(fp, "%upps ", st.pps);
565 }
566 if (st.qlen || st.backlog) {
567 fprintf(fp, "backlog ");
568 if (st.backlog)
569 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
570 if (st.qlen)
571 fprintf(fp, "%up ", st.qlen);
572 }
573 }
574 }
575
576compat_xstats:
577 if (tb[TCA_XSTATS] && xstats)
578 *xstats = tb[TCA_XSTATS];
579}
580