blob: 2f979dfaad5649a94e73adf4f3cf1c492d288283 [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
Christoph J. Thompson5c434a92012-03-01 17:46:26 +010028#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 for (s = suffixes; s->name; ++s) {
156 if (strcasecmp(s->name, p) == 0) {
Eric Dumazeta3038532013-06-03 05:51:33 +0000157 bps *= s->scale;
158 p += strlen(p);
159 break;
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000160 }
161 }
162
Eric Dumazeta3038532013-06-03 05:51:33 +0000163 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;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000172}
173
Eric Dumazet8334bb32013-11-12 14:34:07 -0800174int get_rate64(__u64 *rate, const char *str)
175{
176 char *p;
177 double bps = strtod(str, &p);
178 const struct rate_suffix *s;
179
180 if (p == str)
181 return -1;
182
183 for (s = suffixes; s->name; ++s) {
184 if (strcasecmp(s->name, p) == 0) {
185 bps *= s->scale;
186 p += strlen(p);
187 break;
188 }
189 }
190
191 if (*p)
192 return -1; /* unknown suffix */
193
194 bps /= 8; /* -> bytes per second */
195 *rate = bps;
196 return 0;
197}
198
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700199void print_rate(char *buf, int len, __u64 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) {
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700205 if (tmp >= 1000.0*1024.0*1024.0*1024.0)
206 snprintf(buf, len, "%.0fGibit", tmp/(1024.0*1024.0*1024.0));
207 else if (tmp >= 1000.0*1024.0*1024.0)
Andreas Henrikssonf526af92012-03-09 17:09:19 +0100208 snprintf(buf, len, "%.0fMibit", tmp/(1024.0*1024.0));
osdl.net!shemmingerabf1d0b2004-10-19 19:51:41 +0000209 else if (tmp >= 1000.0*1024)
210 snprintf(buf, len, "%.0fKibit", tmp/1024);
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000211 else
osdl.net!shemmingerabf1d0b2004-10-19 19:51:41 +0000212 snprintf(buf, len, "%.0fbit", tmp);
213 } else {
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700214 if (tmp >= 1000.0*1000000000.0)
215 snprintf(buf, len, "%.0fGbit", tmp/1000000000.0);
216 else if (tmp >= 1000.0*1000000.0)
osdl.net!shemmingerabf1d0b2004-10-19 19:51:41 +0000217 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
218 else if (tmp >= 1000.0 * 1000.0)
219 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000220 else
osdl.net!shemmingerabf1d0b2004-10-19 19:51:41 +0000221 snprintf(buf, len, "%.0fbit", tmp);
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000222 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000223}
224
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700225char * sprint_rate(__u64 rate, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000226{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000227 print_rate(buf, SPRINT_BSIZE-1, rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000228 return buf;
229}
230
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100231int get_time(unsigned *time, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000232{
233 double t;
234 char *p;
235
236 t = strtod(str, &p);
237 if (p == str)
238 return -1;
239
240 if (*p) {
241 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
242 strcasecmp(p, "secs")==0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100243 t *= TIME_UNITS_PER_SEC;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000244 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
245 strcasecmp(p, "msecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100246 t *= TIME_UNITS_PER_SEC/1000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000247 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
248 strcasecmp(p, "usecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100249 t *= TIME_UNITS_PER_SEC/1000000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000250 else
251 return -1;
252 }
253
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100254 *time = t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000255 return 0;
256}
257
258
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100259void print_time(char *buf, int len, __u32 time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000260{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100261 double tmp = time;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000262
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100263 if (tmp >= TIME_UNITS_PER_SEC)
264 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
265 else if (tmp >= TIME_UNITS_PER_SEC/1000)
266 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000267 else
Stephen Hemminger89151442007-03-14 10:14:07 -0700268 snprintf(buf, len, "%uus", time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000269}
270
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100271char * sprint_time(__u32 time, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000272{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100273 print_time(buf, SPRINT_BSIZE-1, time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000274 return buf;
275}
276
Patrick McHardybd29e352007-03-04 20:15:01 +0100277char * sprint_ticks(__u32 ticks, char *buf)
278{
279 return sprint_time(tc_core_tick2time(ticks), buf);
280}
281
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000282int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000283{
284 double sz;
285 char *p;
286
287 sz = strtod(str, &p);
288 if (p == str)
289 return -1;
290
291 if (*p) {
292 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
293 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000294 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
295 sz *= 1024*1024*1024;
296 else if (strcasecmp(p, "gbit") == 0)
297 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
299 sz *= 1024*1024;
300 else if (strcasecmp(p, "mbit") == 0)
301 sz *= 1024*1024/8;
302 else if (strcasecmp(p, "kbit") == 0)
303 sz *= 1024/8;
304 else if (strcasecmp(p, "b") != 0)
305 return -1;
306 }
307
308 *size = sz;
309 return 0;
310}
311
312int get_size_and_cell(unsigned *size, int *cell_log, char *str)
313{
314 char * slash = strchr(str, '/');
315
316 if (slash)
317 *slash = 0;
318
319 if (get_size(size, str))
320 return -1;
321
322 if (slash) {
323 int cell;
324 int i;
325
326 if (get_integer(&cell, slash+1, 0))
327 return -1;
328 *slash = '/';
329
330 for (i=0; i<32; i++) {
331 if ((1<<i) == cell) {
332 *cell_log = i;
333 return 0;
334 }
335 }
336 return -1;
337 }
338 return 0;
339}
340
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000341void print_size(char *buf, int len, __u32 sz)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000342{
343 double tmp = sz;
344
345 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
346 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
347 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
348 snprintf(buf, len, "%gKb", rint(tmp/1024));
349 else
350 snprintf(buf, len, "%ub", sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000351}
352
353char * sprint_size(__u32 size, char *buf)
354{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000355 print_size(buf, SPRINT_BSIZE-1, size);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000356 return buf;
357}
358
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000359void print_qdisc_handle(char *buf, int len, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000360{
361 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000362}
363
364char * sprint_qdisc_handle(__u32 h, char *buf)
365{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000366 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000367 return buf;
368}
369
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000370char * action_n2a(int action, char *buf, int len)
371{
372 switch (action) {
373 case -1:
374 return "continue";
375 break;
376 case TC_ACT_OK:
377 return "pass";
378 break;
379 case TC_ACT_SHOT:
380 return "drop";
381 break;
382 case TC_ACT_RECLASSIFY:
383 return "reclassify";
384 case TC_ACT_PIPE:
385 return "pipe";
386 case TC_ACT_STOLEN:
387 return "stolen";
388 default:
389 snprintf(buf, len, "%d", action);
390 return buf;
391 }
392}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000393
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000394int action_a2n(char *arg, int *result)
395{
396 int res;
397
398 if (matches(arg, "continue") == 0)
399 res = -1;
400 else if (matches(arg, "drop") == 0)
401 res = TC_ACT_SHOT;
402 else if (matches(arg, "shot") == 0)
403 res = TC_ACT_SHOT;
404 else if (matches(arg, "pass") == 0)
405 res = TC_ACT_OK;
406 else if (strcmp(arg, "ok") == 0)
407 res = TC_ACT_OK;
408 else if (matches(arg, "reclassify") == 0)
409 res = TC_ACT_RECLASSIFY;
410 else {
411 char dummy;
412 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
413 return -1;
414 }
415 *result = res;
416 return 0;
417}
418
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300419int get_linklayer(unsigned *val, const char *arg)
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200420{
421 int res;
422
423 if (matches(arg, "ethernet") == 0)
424 res = LINKLAYER_ETHERNET;
425 else if (matches(arg, "atm") == 0)
426 res = LINKLAYER_ATM;
427 else if (matches(arg, "adsl") == 0)
428 res = LINKLAYER_ATM;
429 else
430 return -1; /* Indicate error */
431
432 *val = res;
433 return 0;
434}
435
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300436void print_linklayer(char *buf, int len, unsigned linklayer)
437{
438 switch (linklayer) {
439 case LINKLAYER_UNSPEC:
440 snprintf(buf, len, "%s", "unspec");
441 return;
442 case LINKLAYER_ETHERNET:
443 snprintf(buf, len, "%s", "ethernet");
444 return;
445 case LINKLAYER_ATM:
446 snprintf(buf, len, "%s", "atm");
447 return;
448 default:
449 snprintf(buf, len, "%s", "unknown");
450 return;
451 }
452}
453
454char *sprint_linklayer(unsigned linklayer, char *buf)
455{
456 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
457 return buf;
458}
459
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000460void print_tm(FILE * f, const struct tcf_t *tm)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000461{
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000462 int hz = get_user_hz();
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000463 if (tm->install != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000464 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000465 if (tm->lastuse != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000466 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000467 if (tm->expires != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000468 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000469}
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000470
471void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
472{
473 SPRINT_BUF(b1);
14!tgraf7d69fd92005-01-18 22:11:58 +0000474 struct rtattr *tbs[TCA_STATS_MAX + 1];
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000475
14!tgraf7d69fd92005-01-18 22:11:58 +0000476 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000477
478 if (tbs[TCA_STATS_BASIC]) {
479 struct gnet_stats_basic bs = {0};
480 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
481 fprintf(fp, "%sSent %llu bytes %u pkt",
net[shemminger]!shemmingerb9062432005-01-17 23:28:16 +0000482 prefix, (unsigned long long) bs.bytes, bs.packets);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000483 }
484
485 if (tbs[TCA_STATS_QUEUE]) {
486 struct gnet_stats_queue q = {0};
487 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
488 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
489 q.drops, q.overlimits, q.requeues);
490 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800491
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700492 if (tbs[TCA_STATS_RATE_EST64]) {
493 struct gnet_stats_rate_est64 re = {0};
494
495 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
496 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
497 sizeof(re)));
498 fprintf(fp, "\n%srate %s %llupps ",
499 prefix, sprint_rate(re.bps, b1), re.pps);
500 } else if (tbs[TCA_STATS_RATE_EST]) {
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000501 struct gnet_stats_rate_est re = {0};
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700502
503 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
504 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000505 fprintf(fp, "\n%srate %s %upps ",
506 prefix, sprint_rate(re.bps, b1), re.pps);
507 }
508
509 if (tbs[TCA_STATS_QUEUE]) {
510 struct gnet_stats_queue q = {0};
511 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
512 if (!tbs[TCA_STATS_RATE_EST])
513 fprintf(fp, "\n%s", prefix);
514 fprintf(fp, "backlog %s %up requeues %u ",
515 sprint_size(q.backlog, b1), q.qlen, q.requeues);
516 }
517
518 if (xstats)
519 *xstats = tbs[TCA_STATS_APP] ? : NULL;
520}
521
522void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
523{
524 SPRINT_BUF(b1);
525
526 if (tb[TCA_STATS2]) {
527 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
528 if (xstats && NULL == *xstats)
529 goto compat_xstats;
530 return;
531 }
532 /* backward compatibility */
533 if (tb[TCA_STATS]) {
534 struct tc_stats st;
535
536 /* handle case where kernel returns more/less than we know about */
537 memset(&st, 0, sizeof(st));
538 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
539
540 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800541 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000542 st.overlimits);
543
544 if (st.bps || st.pps || st.qlen || st.backlog) {
545 fprintf(fp, "\n%s", prefix);
546 if (st.bps || st.pps) {
547 fprintf(fp, "rate ");
548 if (st.bps)
549 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
550 if (st.pps)
551 fprintf(fp, "%upps ", st.pps);
552 }
553 if (st.qlen || st.backlog) {
554 fprintf(fp, "backlog ");
555 if (st.backlog)
556 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
557 if (st.qlen)
558 fprintf(fp, "%up ", st.qlen);
559 }
560 }
561 }
562
563compat_xstats:
564 if (tb[TCA_XSTATS] && xstats)
565 *xstats = tb[TCA_XSTATS];
566}
567