blob: f1fca0a8b59803df97a1b106c1c930f70e105bfd [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>
Natanael Copadd9cc0e2014-05-27 07:40:10 +000019#include <sys/param.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000020#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <string.h>
23#include <math.h>
24
25#include "utils.h"
26#include "tc_util.h"
27
Andreas Henriksson5e3bb532008-08-22 16:54:12 +020028#ifndef LIBDIR
Christoph J. Thompson5c434a92012-03-01 17:46:26 +010029#define LIBDIR "/usr/lib"
Rafael Almeidab514b352008-06-01 21:33:44 -030030#endif
31
Stephen Hemmingeraa27f882007-06-20 15:27:22 -070032const char *get_tc_lib(void)
33{
34 const char *lib_dir;
35
36 lib_dir = getenv("TC_LIB_DIR");
37 if (!lib_dir)
Andreas Henriksson5e3bb532008-08-22 16:54:12 +020038 lib_dir = LIBDIR "/tc/";
Stephen Hemmingeraa27f882007-06-20 15:27:22 -070039
40 return lib_dir;
41}
42
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000043int get_qdisc_handle(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000044{
45 __u32 maj;
46 char *p;
47
48 maj = TC_H_UNSPEC;
49 if (strcmp(str, "none") == 0)
50 goto ok;
51 maj = strtoul(str, &p, 16);
52 if (p == str)
53 return -1;
54 maj <<= 16;
55 if (*p != ':' && *p!=0)
56 return -1;
57ok:
58 *h = maj;
59 return 0;
60}
61
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000062int get_tc_classid(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000063{
64 __u32 maj, min;
65 char *p;
66
67 maj = TC_H_ROOT;
68 if (strcmp(str, "root") == 0)
69 goto ok;
70 maj = TC_H_UNSPEC;
71 if (strcmp(str, "none") == 0)
72 goto ok;
73 maj = strtoul(str, &p, 16);
74 if (p == str) {
75 maj = 0;
76 if (*p != ':')
77 return -1;
78 }
79 if (*p == ':') {
osdl.net!shemmingera8b303c2005-02-07 18:16:29 +000080 if (maj >= (1<<16))
81 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000082 maj <<= 16;
83 str = p+1;
84 min = strtoul(str, &p, 16);
85 if (*p != 0)
86 return -1;
osdl.net!shemmingera8b303c2005-02-07 18:16:29 +000087 if (min >= (1<<16))
88 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000089 maj |= min;
90 } else if (*p != 0)
91 return -1;
92
93ok:
94 *h = maj;
95 return 0;
96}
97
98int print_tc_classid(char *buf, int len, __u32 h)
99{
100 if (h == TC_H_ROOT)
101 sprintf(buf, "root");
102 else if (h == TC_H_UNSPEC)
103 snprintf(buf, len, "none");
104 else if (TC_H_MAJ(h) == 0)
105 snprintf(buf, len, ":%x", TC_H_MIN(h));
106 else if (TC_H_MIN(h) == 0)
107 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
108 else
109 snprintf(buf, len, "%x:%x", TC_H_MAJ(h)>>16, TC_H_MIN(h));
110 return 0;
111}
112
113char * sprint_tc_classid(__u32 h, char *buf)
114{
115 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
116 strcpy(buf, "???");
117 return buf;
118}
119
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000120/* See http://physics.nist.gov/cuu/Units/binary.html */
121static const struct rate_suffix {
122 const char *name;
123 double scale;
124} suffixes[] = {
125 { "bit", 1. },
126 { "Kibit", 1024. },
127 { "kbit", 1000. },
128 { "mibit", 1024.*1024. },
129 { "mbit", 1000000. },
130 { "gibit", 1024.*1024.*1024. },
131 { "gbit", 1000000000. },
132 { "tibit", 1024.*1024.*1024.*1024. },
133 { "tbit", 1000000000000. },
134 { "Bps", 8. },
135 { "KiBps", 8.*1024. },
136 { "KBps", 8000. },
137 { "MiBps", 8.*1024*1024. },
138 { "MBps", 8000000. },
139 { "GiBps", 8.*1024.*1024.*1024. },
140 { "GBps", 8000000000. },
141 { "TiBps", 8.*1024.*1024.*1024.*1024. },
142 { "TBps", 8000000000000. },
143 { NULL }
144};
145
146
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000147int get_rate(unsigned *rate, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000148{
149 char *p;
150 double bps = strtod(str, &p);
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000151 const struct rate_suffix *s;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000152
153 if (p == str)
154 return -1;
155
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000156 for (s = suffixes; s->name; ++s) {
157 if (strcasecmp(s->name, p) == 0) {
Eric Dumazeta3038532013-06-03 05:51:33 +0000158 bps *= s->scale;
159 p += strlen(p);
160 break;
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000161 }
162 }
163
Eric Dumazeta3038532013-06-03 05:51:33 +0000164 if (*p)
165 return -1; /* unknown suffix */
166
167 bps /= 8; /* -> bytes per second */
168 *rate = bps;
169 /* detect if an overflow happened */
170 if (*rate != floor(bps))
171 return -1;
172 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000173}
174
Eric Dumazet8334bb32013-11-12 14:34:07 -0800175int get_rate64(__u64 *rate, const char *str)
176{
177 char *p;
178 double bps = strtod(str, &p);
179 const struct rate_suffix *s;
180
181 if (p == str)
182 return -1;
183
184 for (s = suffixes; s->name; ++s) {
185 if (strcasecmp(s->name, p) == 0) {
186 bps *= s->scale;
187 p += strlen(p);
188 break;
189 }
190 }
191
192 if (*p)
193 return -1; /* unknown suffix */
194
195 bps /= 8; /* -> bytes per second */
196 *rate = bps;
197 return 0;
198}
199
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700200void print_rate(char *buf, int len, __u64 rate)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000201{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000202 extern int use_iec;
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800203 unsigned long kilo = use_iec ? 1024 : 1000;
204 const char *str = use_iec ? "i" : "";
205 int i = 0;
206 static char *units[5] = {"", "K", "M", "G", "T"};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000207
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800208 rate <<= 3; /* bytes/sec -> bits/sec */
209
210 for (i = 0; i < ARRAY_SIZE(units); i++) {
211 if (rate < kilo)
212 break;
213 if (((rate % kilo) != 0) && rate < 1000*kilo)
214 break;
215 rate /= kilo;
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000216 }
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800217 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000218}
219
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700220char * sprint_rate(__u64 rate, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000221{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000222 print_rate(buf, SPRINT_BSIZE-1, rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000223 return buf;
224}
225
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100226int get_time(unsigned *time, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000227{
228 double t;
229 char *p;
230
231 t = strtod(str, &p);
232 if (p == str)
233 return -1;
234
235 if (*p) {
236 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
237 strcasecmp(p, "secs")==0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100238 t *= TIME_UNITS_PER_SEC;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000239 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
240 strcasecmp(p, "msecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100241 t *= TIME_UNITS_PER_SEC/1000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000242 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
243 strcasecmp(p, "usecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100244 t *= TIME_UNITS_PER_SEC/1000000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000245 else
246 return -1;
247 }
248
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100249 *time = t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000250 return 0;
251}
252
253
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100254void print_time(char *buf, int len, __u32 time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000255{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100256 double tmp = time;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000257
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100258 if (tmp >= TIME_UNITS_PER_SEC)
259 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
260 else if (tmp >= TIME_UNITS_PER_SEC/1000)
261 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000262 else
Stephen Hemminger89151442007-03-14 10:14:07 -0700263 snprintf(buf, len, "%uus", time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000264}
265
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100266char * sprint_time(__u32 time, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000267{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100268 print_time(buf, SPRINT_BSIZE-1, time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000269 return buf;
270}
271
Patrick McHardybd29e352007-03-04 20:15:01 +0100272char * sprint_ticks(__u32 ticks, char *buf)
273{
274 return sprint_time(tc_core_tick2time(ticks), buf);
275}
276
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000277int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000278{
279 double sz;
280 char *p;
281
282 sz = strtod(str, &p);
283 if (p == str)
284 return -1;
285
286 if (*p) {
287 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
288 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000289 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
290 sz *= 1024*1024*1024;
291 else if (strcasecmp(p, "gbit") == 0)
292 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000293 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
294 sz *= 1024*1024;
295 else if (strcasecmp(p, "mbit") == 0)
296 sz *= 1024*1024/8;
297 else if (strcasecmp(p, "kbit") == 0)
298 sz *= 1024/8;
299 else if (strcasecmp(p, "b") != 0)
300 return -1;
301 }
302
303 *size = sz;
304 return 0;
305}
306
307int get_size_and_cell(unsigned *size, int *cell_log, char *str)
308{
309 char * slash = strchr(str, '/');
310
311 if (slash)
312 *slash = 0;
313
314 if (get_size(size, str))
315 return -1;
316
317 if (slash) {
318 int cell;
319 int i;
320
321 if (get_integer(&cell, slash+1, 0))
322 return -1;
323 *slash = '/';
324
325 for (i=0; i<32; i++) {
326 if ((1<<i) == cell) {
327 *cell_log = i;
328 return 0;
329 }
330 }
331 return -1;
332 }
333 return 0;
334}
335
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000336void print_size(char *buf, int len, __u32 sz)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000337{
338 double tmp = sz;
339
340 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
341 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
342 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
343 snprintf(buf, len, "%gKb", rint(tmp/1024));
344 else
345 snprintf(buf, len, "%ub", sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000346}
347
348char * sprint_size(__u32 size, char *buf)
349{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000350 print_size(buf, SPRINT_BSIZE-1, size);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000351 return buf;
352}
353
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000354void print_qdisc_handle(char *buf, int len, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000355{
356 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000357}
358
359char * sprint_qdisc_handle(__u32 h, char *buf)
360{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000361 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000362 return buf;
363}
364
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000365char * action_n2a(int action, char *buf, int len)
366{
367 switch (action) {
368 case -1:
369 return "continue";
370 break;
371 case TC_ACT_OK:
372 return "pass";
373 break;
374 case TC_ACT_SHOT:
375 return "drop";
376 break;
377 case TC_ACT_RECLASSIFY:
378 return "reclassify";
379 case TC_ACT_PIPE:
380 return "pipe";
381 case TC_ACT_STOLEN:
382 return "stolen";
383 default:
384 snprintf(buf, len, "%d", action);
385 return buf;
386 }
387}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000388
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000389int action_a2n(char *arg, int *result)
390{
391 int res;
392
393 if (matches(arg, "continue") == 0)
394 res = -1;
395 else if (matches(arg, "drop") == 0)
396 res = TC_ACT_SHOT;
397 else if (matches(arg, "shot") == 0)
398 res = TC_ACT_SHOT;
399 else if (matches(arg, "pass") == 0)
400 res = TC_ACT_OK;
401 else if (strcmp(arg, "ok") == 0)
402 res = TC_ACT_OK;
403 else if (matches(arg, "reclassify") == 0)
404 res = TC_ACT_RECLASSIFY;
405 else {
406 char dummy;
407 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
408 return -1;
409 }
410 *result = res;
411 return 0;
412}
413
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300414int get_linklayer(unsigned *val, const char *arg)
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200415{
416 int res;
417
418 if (matches(arg, "ethernet") == 0)
419 res = LINKLAYER_ETHERNET;
420 else if (matches(arg, "atm") == 0)
421 res = LINKLAYER_ATM;
422 else if (matches(arg, "adsl") == 0)
423 res = LINKLAYER_ATM;
424 else
425 return -1; /* Indicate error */
426
427 *val = res;
428 return 0;
429}
430
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300431void print_linklayer(char *buf, int len, unsigned linklayer)
432{
433 switch (linklayer) {
434 case LINKLAYER_UNSPEC:
435 snprintf(buf, len, "%s", "unspec");
436 return;
437 case LINKLAYER_ETHERNET:
438 snprintf(buf, len, "%s", "ethernet");
439 return;
440 case LINKLAYER_ATM:
441 snprintf(buf, len, "%s", "atm");
442 return;
443 default:
444 snprintf(buf, len, "%s", "unknown");
445 return;
446 }
447}
448
449char *sprint_linklayer(unsigned linklayer, char *buf)
450{
451 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
452 return buf;
453}
454
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000455void print_tm(FILE * f, const struct tcf_t *tm)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000456{
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000457 int hz = get_user_hz();
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000458 if (tm->install != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000459 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000460 if (tm->lastuse != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000461 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000462 if (tm->expires != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000463 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000464}
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000465
466void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
467{
468 SPRINT_BUF(b1);
14!tgraf7d69fd92005-01-18 22:11:58 +0000469 struct rtattr *tbs[TCA_STATS_MAX + 1];
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000470
14!tgraf7d69fd92005-01-18 22:11:58 +0000471 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000472
473 if (tbs[TCA_STATS_BASIC]) {
474 struct gnet_stats_basic bs = {0};
475 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
476 fprintf(fp, "%sSent %llu bytes %u pkt",
net[shemminger]!shemmingerb9062432005-01-17 23:28:16 +0000477 prefix, (unsigned long long) bs.bytes, bs.packets);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000478 }
479
480 if (tbs[TCA_STATS_QUEUE]) {
481 struct gnet_stats_queue q = {0};
482 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
483 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
484 q.drops, q.overlimits, q.requeues);
485 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800486
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700487 if (tbs[TCA_STATS_RATE_EST64]) {
488 struct gnet_stats_rate_est64 re = {0};
489
490 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
491 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
492 sizeof(re)));
493 fprintf(fp, "\n%srate %s %llupps ",
494 prefix, sprint_rate(re.bps, b1), re.pps);
495 } else if (tbs[TCA_STATS_RATE_EST]) {
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000496 struct gnet_stats_rate_est re = {0};
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700497
498 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
499 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000500 fprintf(fp, "\n%srate %s %upps ",
501 prefix, sprint_rate(re.bps, b1), re.pps);
502 }
503
504 if (tbs[TCA_STATS_QUEUE]) {
505 struct gnet_stats_queue q = {0};
506 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
507 if (!tbs[TCA_STATS_RATE_EST])
508 fprintf(fp, "\n%s", prefix);
509 fprintf(fp, "backlog %s %up requeues %u ",
510 sprint_size(q.backlog, b1), q.qlen, q.requeues);
511 }
512
513 if (xstats)
514 *xstats = tbs[TCA_STATS_APP] ? : NULL;
515}
516
517void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
518{
519 SPRINT_BUF(b1);
520
521 if (tb[TCA_STATS2]) {
522 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
523 if (xstats && NULL == *xstats)
524 goto compat_xstats;
525 return;
526 }
527 /* backward compatibility */
528 if (tb[TCA_STATS]) {
529 struct tc_stats st;
530
531 /* handle case where kernel returns more/less than we know about */
532 memset(&st, 0, sizeof(st));
533 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
534
535 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800536 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000537 st.overlimits);
538
539 if (st.bps || st.pps || st.qlen || st.backlog) {
540 fprintf(fp, "\n%s", prefix);
541 if (st.bps || st.pps) {
542 fprintf(fp, "rate ");
543 if (st.bps)
544 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
545 if (st.pps)
546 fprintf(fp, "%upps ", st.pps);
547 }
548 if (st.qlen || st.backlog) {
549 fprintf(fp, "backlog ");
550 if (st.backlog)
551 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
552 if (st.qlen)
553 fprintf(fp, "%up ", st.qlen);
554 }
555 }
556 }
557
558compat_xstats:
559 if (tb[TCA_XSTATS] && xstats)
560 *xstats = tb[TCA_XSTATS];
561}
562