blob: feae439419aa89790573fa5e6bddea4169d43f6e [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"
Vadim Kochan4612d042015-03-03 18:41:18 +020026#include "names.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000027#include "tc_util.h"
Vadim Kochan4612d042015-03-03 18:41:18 +020028#include "tc_common.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000029
Andreas Henriksson5e3bb532008-08-22 16:54:12 +020030#ifndef LIBDIR
Christoph J. Thompson5c434a92012-03-01 17:46:26 +010031#define LIBDIR "/usr/lib"
Rafael Almeidab514b352008-06-01 21:33:44 -030032#endif
33
Vadim Kochan4612d042015-03-03 18:41:18 +020034static struct db_names *cls_names = NULL;
35
36#define NAMES_DB "/etc/iproute2/cls_names"
37
38int cls_names_init(char *path)
39{
40 cls_names = db_names_alloc(path ?: NAMES_DB);
41 if (!cls_names) {
42 fprintf(stderr, "Error while opening class names file\n");
43 return -1;
44 }
45
46 return 0;
47}
48
49void cls_names_uninit(void)
50{
51 db_names_free(cls_names);
52}
53
Stephen Hemmingeraa27f882007-06-20 15:27:22 -070054const char *get_tc_lib(void)
55{
56 const char *lib_dir;
57
58 lib_dir = getenv("TC_LIB_DIR");
59 if (!lib_dir)
Andreas Henriksson5e3bb532008-08-22 16:54:12 +020060 lib_dir = LIBDIR "/tc/";
Stephen Hemmingeraa27f882007-06-20 15:27:22 -070061
62 return lib_dir;
63}
64
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000065int get_qdisc_handle(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000066{
67 __u32 maj;
68 char *p;
69
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 return -1;
76 maj <<= 16;
77 if (*p != ':' && *p!=0)
78 return -1;
79ok:
80 *h = maj;
81 return 0;
82}
83
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000084int get_tc_classid(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000085{
86 __u32 maj, min;
87 char *p;
88
89 maj = TC_H_ROOT;
90 if (strcmp(str, "root") == 0)
91 goto ok;
92 maj = TC_H_UNSPEC;
93 if (strcmp(str, "none") == 0)
94 goto ok;
95 maj = strtoul(str, &p, 16);
96 if (p == str) {
97 maj = 0;
98 if (*p != ':')
99 return -1;
100 }
101 if (*p == ':') {
osdl.net!shemmingera8b303c2005-02-07 18:16:29 +0000102 if (maj >= (1<<16))
103 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000104 maj <<= 16;
105 str = p+1;
106 min = strtoul(str, &p, 16);
107 if (*p != 0)
108 return -1;
osdl.net!shemmingera8b303c2005-02-07 18:16:29 +0000109 if (min >= (1<<16))
110 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000111 maj |= min;
112 } else if (*p != 0)
113 return -1;
114
115ok:
116 *h = maj;
117 return 0;
118}
119
120int print_tc_classid(char *buf, int len, __u32 h)
121{
Vadim Kochan4612d042015-03-03 18:41:18 +0200122 char handle[40] = {};
123
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000124 if (h == TC_H_ROOT)
Vadim Kochan4612d042015-03-03 18:41:18 +0200125 sprintf(handle, "root");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000126 else if (h == TC_H_UNSPEC)
Vadim Kochan4612d042015-03-03 18:41:18 +0200127 snprintf(handle, len, "none");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000128 else if (TC_H_MAJ(h) == 0)
Vadim Kochan4612d042015-03-03 18:41:18 +0200129 snprintf(handle, len, ":%x", TC_H_MIN(h));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000130 else if (TC_H_MIN(h) == 0)
Vadim Kochan4612d042015-03-03 18:41:18 +0200131 snprintf(handle, len, "%x:", TC_H_MAJ(h) >> 16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000132 else
Vadim Kochan4612d042015-03-03 18:41:18 +0200133 snprintf(handle, len, "%x:%x", TC_H_MAJ(h) >> 16, TC_H_MIN(h));
134
135 if (use_names) {
136 char clname[IDNAME_MAX] = {};
137
138 if (id_to_name(cls_names, h, clname))
139 snprintf(buf, len, "%s#%s", clname, handle);
140 else
141 snprintf(buf, len, "%s", handle);
142 } else {
143 snprintf(buf, len, "%s", handle);
144 }
145
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000146 return 0;
147}
148
Vadim Kochan4612d042015-03-03 18:41:18 +0200149char *sprint_tc_classid(__u32 h, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000150{
151 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
152 strcpy(buf, "???");
153 return buf;
154}
155
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000156/* See http://physics.nist.gov/cuu/Units/binary.html */
157static const struct rate_suffix {
158 const char *name;
159 double scale;
160} suffixes[] = {
161 { "bit", 1. },
162 { "Kibit", 1024. },
163 { "kbit", 1000. },
164 { "mibit", 1024.*1024. },
165 { "mbit", 1000000. },
166 { "gibit", 1024.*1024.*1024. },
167 { "gbit", 1000000000. },
168 { "tibit", 1024.*1024.*1024.*1024. },
169 { "tbit", 1000000000000. },
170 { "Bps", 8. },
171 { "KiBps", 8.*1024. },
172 { "KBps", 8000. },
173 { "MiBps", 8.*1024*1024. },
174 { "MBps", 8000000. },
175 { "GiBps", 8.*1024.*1024.*1024. },
176 { "GBps", 8000000000. },
177 { "TiBps", 8.*1024.*1024.*1024.*1024. },
178 { "TBps", 8000000000000. },
179 { NULL }
180};
181
182
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000183int get_rate(unsigned *rate, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000184{
185 char *p;
186 double bps = strtod(str, &p);
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000187 const struct rate_suffix *s;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000188
189 if (p == str)
190 return -1;
191
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000192 for (s = suffixes; s->name; ++s) {
193 if (strcasecmp(s->name, p) == 0) {
Eric Dumazeta3038532013-06-03 05:51:33 +0000194 bps *= s->scale;
195 p += strlen(p);
196 break;
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000197 }
198 }
199
Eric Dumazeta3038532013-06-03 05:51:33 +0000200 if (*p)
201 return -1; /* unknown suffix */
202
203 bps /= 8; /* -> bytes per second */
204 *rate = bps;
205 /* detect if an overflow happened */
206 if (*rate != floor(bps))
207 return -1;
208 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000209}
210
Eric Dumazet8334bb32013-11-12 14:34:07 -0800211int get_rate64(__u64 *rate, const char *str)
212{
213 char *p;
214 double bps = strtod(str, &p);
215 const struct rate_suffix *s;
216
217 if (p == str)
218 return -1;
219
220 for (s = suffixes; s->name; ++s) {
221 if (strcasecmp(s->name, p) == 0) {
222 bps *= s->scale;
223 p += strlen(p);
224 break;
225 }
226 }
227
228 if (*p)
229 return -1; /* unknown suffix */
230
231 bps /= 8; /* -> bytes per second */
232 *rate = bps;
233 return 0;
234}
235
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700236void print_rate(char *buf, int len, __u64 rate)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000237{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000238 extern int use_iec;
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800239 unsigned long kilo = use_iec ? 1024 : 1000;
240 const char *str = use_iec ? "i" : "";
241 int i = 0;
242 static char *units[5] = {"", "K", "M", "G", "T"};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000243
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800244 rate <<= 3; /* bytes/sec -> bits/sec */
245
246 for (i = 0; i < ARRAY_SIZE(units); i++) {
247 if (rate < kilo)
248 break;
249 if (((rate % kilo) != 0) && rate < 1000*kilo)
250 break;
251 rate /= kilo;
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000252 }
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800253 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000254}
255
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700256char * sprint_rate(__u64 rate, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000257{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000258 print_rate(buf, SPRINT_BSIZE-1, rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000259 return buf;
260}
261
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100262int get_time(unsigned *time, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000263{
264 double t;
265 char *p;
266
267 t = strtod(str, &p);
268 if (p == str)
269 return -1;
270
271 if (*p) {
272 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
273 strcasecmp(p, "secs")==0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100274 t *= TIME_UNITS_PER_SEC;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000275 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
276 strcasecmp(p, "msecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100277 t *= TIME_UNITS_PER_SEC/1000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000278 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
279 strcasecmp(p, "usecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100280 t *= TIME_UNITS_PER_SEC/1000000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000281 else
282 return -1;
283 }
284
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100285 *time = t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000286 return 0;
287}
288
289
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100290void print_time(char *buf, int len, __u32 time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000291{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100292 double tmp = time;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000293
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100294 if (tmp >= TIME_UNITS_PER_SEC)
295 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
296 else if (tmp >= TIME_UNITS_PER_SEC/1000)
297 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298 else
Stephen Hemminger89151442007-03-14 10:14:07 -0700299 snprintf(buf, len, "%uus", time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000300}
301
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100302char * sprint_time(__u32 time, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000303{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100304 print_time(buf, SPRINT_BSIZE-1, time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000305 return buf;
306}
307
Patrick McHardybd29e352007-03-04 20:15:01 +0100308char * sprint_ticks(__u32 ticks, char *buf)
309{
310 return sprint_time(tc_core_tick2time(ticks), buf);
311}
312
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000313int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000314{
315 double sz;
316 char *p;
317
318 sz = strtod(str, &p);
319 if (p == str)
320 return -1;
321
322 if (*p) {
323 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
324 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000325 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
326 sz *= 1024*1024*1024;
327 else if (strcasecmp(p, "gbit") == 0)
328 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000329 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
330 sz *= 1024*1024;
331 else if (strcasecmp(p, "mbit") == 0)
332 sz *= 1024*1024/8;
333 else if (strcasecmp(p, "kbit") == 0)
334 sz *= 1024/8;
335 else if (strcasecmp(p, "b") != 0)
336 return -1;
337 }
338
339 *size = sz;
340 return 0;
341}
342
343int get_size_and_cell(unsigned *size, int *cell_log, char *str)
344{
345 char * slash = strchr(str, '/');
346
347 if (slash)
348 *slash = 0;
349
350 if (get_size(size, str))
351 return -1;
352
353 if (slash) {
354 int cell;
355 int i;
356
357 if (get_integer(&cell, slash+1, 0))
358 return -1;
359 *slash = '/';
360
361 for (i=0; i<32; i++) {
362 if ((1<<i) == cell) {
363 *cell_log = i;
364 return 0;
365 }
366 }
367 return -1;
368 }
369 return 0;
370}
371
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000372void print_size(char *buf, int len, __u32 sz)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000373{
374 double tmp = sz;
375
376 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
377 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
378 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
379 snprintf(buf, len, "%gKb", rint(tmp/1024));
380 else
381 snprintf(buf, len, "%ub", sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000382}
383
384char * sprint_size(__u32 size, char *buf)
385{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000386 print_size(buf, SPRINT_BSIZE-1, size);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000387 return buf;
388}
389
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000390void print_qdisc_handle(char *buf, int len, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000391{
392 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000393}
394
395char * sprint_qdisc_handle(__u32 h, char *buf)
396{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000397 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000398 return buf;
399}
400
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000401char * action_n2a(int action, char *buf, int len)
402{
403 switch (action) {
404 case -1:
405 return "continue";
406 break;
407 case TC_ACT_OK:
408 return "pass";
409 break;
410 case TC_ACT_SHOT:
411 return "drop";
412 break;
413 case TC_ACT_RECLASSIFY:
414 return "reclassify";
415 case TC_ACT_PIPE:
416 return "pipe";
417 case TC_ACT_STOLEN:
418 return "stolen";
419 default:
420 snprintf(buf, len, "%d", action);
421 return buf;
422 }
423}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000424
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000425int action_a2n(char *arg, int *result)
426{
427 int res;
428
429 if (matches(arg, "continue") == 0)
430 res = -1;
431 else if (matches(arg, "drop") == 0)
432 res = TC_ACT_SHOT;
433 else if (matches(arg, "shot") == 0)
434 res = TC_ACT_SHOT;
435 else if (matches(arg, "pass") == 0)
436 res = TC_ACT_OK;
437 else if (strcmp(arg, "ok") == 0)
438 res = TC_ACT_OK;
439 else if (matches(arg, "reclassify") == 0)
440 res = TC_ACT_RECLASSIFY;
441 else {
442 char dummy;
443 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
444 return -1;
445 }
446 *result = res;
447 return 0;
448}
449
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300450int get_linklayer(unsigned *val, const char *arg)
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200451{
452 int res;
453
454 if (matches(arg, "ethernet") == 0)
455 res = LINKLAYER_ETHERNET;
456 else if (matches(arg, "atm") == 0)
457 res = LINKLAYER_ATM;
458 else if (matches(arg, "adsl") == 0)
459 res = LINKLAYER_ATM;
460 else
461 return -1; /* Indicate error */
462
463 *val = res;
464 return 0;
465}
466
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300467void print_linklayer(char *buf, int len, unsigned linklayer)
468{
469 switch (linklayer) {
470 case LINKLAYER_UNSPEC:
471 snprintf(buf, len, "%s", "unspec");
472 return;
473 case LINKLAYER_ETHERNET:
474 snprintf(buf, len, "%s", "ethernet");
475 return;
476 case LINKLAYER_ATM:
477 snprintf(buf, len, "%s", "atm");
478 return;
479 default:
480 snprintf(buf, len, "%s", "unknown");
481 return;
482 }
483}
484
485char *sprint_linklayer(unsigned linklayer, char *buf)
486{
487 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
488 return buf;
489}
490
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000491void print_tm(FILE * f, const struct tcf_t *tm)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000492{
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000493 int hz = get_user_hz();
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000494 if (tm->install != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000495 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000496 if (tm->lastuse != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000497 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000498 if (tm->expires != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000499 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000500}
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000501
502void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
503{
504 SPRINT_BUF(b1);
14!tgraf7d69fd92005-01-18 22:11:58 +0000505 struct rtattr *tbs[TCA_STATS_MAX + 1];
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000506
14!tgraf7d69fd92005-01-18 22:11:58 +0000507 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000508
509 if (tbs[TCA_STATS_BASIC]) {
510 struct gnet_stats_basic bs = {0};
511 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
512 fprintf(fp, "%sSent %llu bytes %u pkt",
net[shemminger]!shemmingerb9062432005-01-17 23:28:16 +0000513 prefix, (unsigned long long) bs.bytes, bs.packets);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000514 }
515
516 if (tbs[TCA_STATS_QUEUE]) {
517 struct gnet_stats_queue q = {0};
518 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
519 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
520 q.drops, q.overlimits, q.requeues);
521 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800522
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700523 if (tbs[TCA_STATS_RATE_EST64]) {
524 struct gnet_stats_rate_est64 re = {0};
525
526 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
527 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
528 sizeof(re)));
529 fprintf(fp, "\n%srate %s %llupps ",
530 prefix, sprint_rate(re.bps, b1), re.pps);
531 } else if (tbs[TCA_STATS_RATE_EST]) {
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000532 struct gnet_stats_rate_est re = {0};
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700533
534 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
535 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000536 fprintf(fp, "\n%srate %s %upps ",
537 prefix, sprint_rate(re.bps, b1), re.pps);
538 }
539
540 if (tbs[TCA_STATS_QUEUE]) {
541 struct gnet_stats_queue q = {0};
542 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
543 if (!tbs[TCA_STATS_RATE_EST])
544 fprintf(fp, "\n%s", prefix);
545 fprintf(fp, "backlog %s %up requeues %u ",
546 sprint_size(q.backlog, b1), q.qlen, q.requeues);
547 }
548
549 if (xstats)
550 *xstats = tbs[TCA_STATS_APP] ? : NULL;
551}
552
553void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
554{
555 SPRINT_BUF(b1);
556
557 if (tb[TCA_STATS2]) {
558 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
559 if (xstats && NULL == *xstats)
560 goto compat_xstats;
561 return;
562 }
563 /* backward compatibility */
564 if (tb[TCA_STATS]) {
565 struct tc_stats st;
566
567 /* handle case where kernel returns more/less than we know about */
568 memset(&st, 0, sizeof(st));
569 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
570
571 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800572 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000573 st.overlimits);
574
575 if (st.bps || st.pps || st.qlen || st.backlog) {
576 fprintf(fp, "\n%s", prefix);
577 if (st.bps || st.pps) {
578 fprintf(fp, "rate ");
579 if (st.bps)
580 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
581 if (st.pps)
582 fprintf(fp, "%upps ", st.pps);
583 }
584 if (st.qlen || st.backlog) {
585 fprintf(fp, "backlog ");
586 if (st.backlog)
587 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
588 if (st.qlen)
589 fprintf(fp, "%up ", st.qlen);
590 }
591 }
592 }
593
594compat_xstats:
595 if (tb[TCA_XSTATS] && xstats)
596 *xstats = tb[TCA_XSTATS];
597}
598