blob: 1d3153df9554e171eb6cf5820d85069115486abf [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>
Vadim Kochan8b90a992015-03-25 05:14:37 +020024#include <errno.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000025
26#include "utils.h"
Vadim Kochan4612d042015-03-03 18:41:18 +020027#include "names.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000028#include "tc_util.h"
Vadim Kochan4612d042015-03-03 18:41:18 +020029#include "tc_common.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030
Andreas Henriksson5e3bb532008-08-22 16:54:12 +020031#ifndef LIBDIR
Christoph J. Thompson5c434a92012-03-01 17:46:26 +010032#define LIBDIR "/usr/lib"
Rafael Almeidab514b352008-06-01 21:33:44 -030033#endif
34
Vadim Kochan4612d042015-03-03 18:41:18 +020035static struct db_names *cls_names = NULL;
36
Vadim Kochan8b90a992015-03-25 05:14:37 +020037#define NAMES_DB "/etc/iproute2/tc_cls"
Vadim Kochan4612d042015-03-03 18:41:18 +020038
39int cls_names_init(char *path)
40{
Vadim Kochan8b90a992015-03-25 05:14:37 +020041 int ret;
42
43 cls_names = db_names_alloc();
44 if (!cls_names)
Vadim Kochan4612d042015-03-03 18:41:18 +020045 return -1;
Vadim Kochan8b90a992015-03-25 05:14:37 +020046
47 ret = db_names_load(cls_names, path ?: NAMES_DB);
48 if (ret == -ENOENT && path) {
49 fprintf(stderr, "Can't open class names file: %s\n", path);
50 return -1;
51 }
52 if (ret) {
53 db_names_free(cls_names);
54 cls_names = NULL;
Vadim Kochan4612d042015-03-03 18:41:18 +020055 }
56
57 return 0;
58}
59
60void cls_names_uninit(void)
61{
62 db_names_free(cls_names);
63}
64
Stephen Hemmingeraa27f882007-06-20 15:27:22 -070065const char *get_tc_lib(void)
66{
67 const char *lib_dir;
68
69 lib_dir = getenv("TC_LIB_DIR");
70 if (!lib_dir)
Andreas Henriksson5e3bb532008-08-22 16:54:12 +020071 lib_dir = LIBDIR "/tc/";
Stephen Hemmingeraa27f882007-06-20 15:27:22 -070072
73 return lib_dir;
74}
75
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000076int get_qdisc_handle(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000077{
78 __u32 maj;
79 char *p;
80
81 maj = TC_H_UNSPEC;
82 if (strcmp(str, "none") == 0)
83 goto ok;
84 maj = strtoul(str, &p, 16);
85 if (p == str)
86 return -1;
87 maj <<= 16;
88 if (*p != ':' && *p!=0)
89 return -1;
90ok:
91 *h = maj;
92 return 0;
93}
94
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +000095int get_tc_classid(__u32 *h, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000096{
97 __u32 maj, min;
98 char *p;
99
100 maj = TC_H_ROOT;
101 if (strcmp(str, "root") == 0)
102 goto ok;
103 maj = TC_H_UNSPEC;
104 if (strcmp(str, "none") == 0)
105 goto ok;
106 maj = strtoul(str, &p, 16);
107 if (p == str) {
108 maj = 0;
109 if (*p != ':')
110 return -1;
111 }
112 if (*p == ':') {
osdl.net!shemmingera8b303c2005-02-07 18:16:29 +0000113 if (maj >= (1<<16))
114 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000115 maj <<= 16;
116 str = p+1;
117 min = strtoul(str, &p, 16);
118 if (*p != 0)
119 return -1;
osdl.net!shemmingera8b303c2005-02-07 18:16:29 +0000120 if (min >= (1<<16))
121 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000122 maj |= min;
123 } else if (*p != 0)
124 return -1;
125
126ok:
127 *h = maj;
128 return 0;
129}
130
131int print_tc_classid(char *buf, int len, __u32 h)
132{
Vadim Kochan4612d042015-03-03 18:41:18 +0200133 char handle[40] = {};
134
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000135 if (h == TC_H_ROOT)
Vadim Kochan4612d042015-03-03 18:41:18 +0200136 sprintf(handle, "root");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000137 else if (h == TC_H_UNSPEC)
Vadim Kochan4612d042015-03-03 18:41:18 +0200138 snprintf(handle, len, "none");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000139 else if (TC_H_MAJ(h) == 0)
Vadim Kochan4612d042015-03-03 18:41:18 +0200140 snprintf(handle, len, ":%x", TC_H_MIN(h));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000141 else if (TC_H_MIN(h) == 0)
Vadim Kochan4612d042015-03-03 18:41:18 +0200142 snprintf(handle, len, "%x:", TC_H_MAJ(h) >> 16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000143 else
Vadim Kochan4612d042015-03-03 18:41:18 +0200144 snprintf(handle, len, "%x:%x", TC_H_MAJ(h) >> 16, TC_H_MIN(h));
145
146 if (use_names) {
147 char clname[IDNAME_MAX] = {};
148
149 if (id_to_name(cls_names, h, clname))
150 snprintf(buf, len, "%s#%s", clname, handle);
151 else
152 snprintf(buf, len, "%s", handle);
153 } else {
154 snprintf(buf, len, "%s", handle);
155 }
156
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000157 return 0;
158}
159
Vadim Kochan4612d042015-03-03 18:41:18 +0200160char *sprint_tc_classid(__u32 h, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000161{
162 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
163 strcpy(buf, "???");
164 return buf;
165}
166
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000167/* See http://physics.nist.gov/cuu/Units/binary.html */
168static const struct rate_suffix {
169 const char *name;
170 double scale;
171} suffixes[] = {
172 { "bit", 1. },
173 { "Kibit", 1024. },
174 { "kbit", 1000. },
175 { "mibit", 1024.*1024. },
176 { "mbit", 1000000. },
177 { "gibit", 1024.*1024.*1024. },
178 { "gbit", 1000000000. },
179 { "tibit", 1024.*1024.*1024.*1024. },
180 { "tbit", 1000000000000. },
181 { "Bps", 8. },
182 { "KiBps", 8.*1024. },
183 { "KBps", 8000. },
184 { "MiBps", 8.*1024*1024. },
185 { "MBps", 8000000. },
186 { "GiBps", 8.*1024.*1024.*1024. },
187 { "GBps", 8000000000. },
188 { "TiBps", 8.*1024.*1024.*1024.*1024. },
189 { "TBps", 8000000000000. },
190 { NULL }
191};
192
193
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000194int get_rate(unsigned *rate, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000195{
196 char *p;
197 double bps = strtod(str, &p);
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000198 const struct rate_suffix *s;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000199
200 if (p == str)
201 return -1;
202
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000203 for (s = suffixes; s->name; ++s) {
204 if (strcasecmp(s->name, p) == 0) {
Eric Dumazeta3038532013-06-03 05:51:33 +0000205 bps *= s->scale;
206 p += strlen(p);
207 break;
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000208 }
209 }
210
Eric Dumazeta3038532013-06-03 05:51:33 +0000211 if (*p)
212 return -1; /* unknown suffix */
213
214 bps /= 8; /* -> bytes per second */
215 *rate = bps;
216 /* detect if an overflow happened */
217 if (*rate != floor(bps))
218 return -1;
219 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000220}
221
Eric Dumazet8334bb32013-11-12 14:34:07 -0800222int get_rate64(__u64 *rate, const char *str)
223{
224 char *p;
225 double bps = strtod(str, &p);
226 const struct rate_suffix *s;
227
228 if (p == str)
229 return -1;
230
231 for (s = suffixes; s->name; ++s) {
232 if (strcasecmp(s->name, p) == 0) {
233 bps *= s->scale;
234 p += strlen(p);
235 break;
236 }
237 }
238
239 if (*p)
240 return -1; /* unknown suffix */
241
242 bps /= 8; /* -> bytes per second */
243 *rate = bps;
244 return 0;
245}
246
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700247void print_rate(char *buf, int len, __u64 rate)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000248{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000249 extern int use_iec;
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800250 unsigned long kilo = use_iec ? 1024 : 1000;
251 const char *str = use_iec ? "i" : "";
252 int i = 0;
253 static char *units[5] = {"", "K", "M", "G", "T"};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000254
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800255 rate <<= 3; /* bytes/sec -> bits/sec */
256
257 for (i = 0; i < ARRAY_SIZE(units); i++) {
258 if (rate < kilo)
259 break;
260 if (((rate % kilo) != 0) && rate < 1000*kilo)
261 break;
262 rate /= kilo;
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000263 }
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800264 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000265}
266
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700267char * sprint_rate(__u64 rate, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000268{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000269 print_rate(buf, SPRINT_BSIZE-1, rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000270 return buf;
271}
272
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100273int get_time(unsigned *time, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000274{
275 double t;
276 char *p;
277
278 t = strtod(str, &p);
279 if (p == str)
280 return -1;
281
282 if (*p) {
283 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
284 strcasecmp(p, "secs")==0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100285 t *= TIME_UNITS_PER_SEC;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000286 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
287 strcasecmp(p, "msecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100288 t *= TIME_UNITS_PER_SEC/1000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000289 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
290 strcasecmp(p, "usecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100291 t *= TIME_UNITS_PER_SEC/1000000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000292 else
293 return -1;
294 }
295
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100296 *time = t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000297 return 0;
298}
299
300
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100301void print_time(char *buf, int len, __u32 time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000302{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100303 double tmp = time;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000304
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100305 if (tmp >= TIME_UNITS_PER_SEC)
306 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
307 else if (tmp >= TIME_UNITS_PER_SEC/1000)
308 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000309 else
Stephen Hemminger89151442007-03-14 10:14:07 -0700310 snprintf(buf, len, "%uus", time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000311}
312
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100313char * sprint_time(__u32 time, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000314{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100315 print_time(buf, SPRINT_BSIZE-1, time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000316 return buf;
317}
318
Patrick McHardybd29e352007-03-04 20:15:01 +0100319char * sprint_ticks(__u32 ticks, char *buf)
320{
321 return sprint_time(tc_core_tick2time(ticks), buf);
322}
323
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000324int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000325{
326 double sz;
327 char *p;
328
329 sz = strtod(str, &p);
330 if (p == str)
331 return -1;
332
333 if (*p) {
334 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
335 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000336 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
337 sz *= 1024*1024*1024;
338 else if (strcasecmp(p, "gbit") == 0)
339 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000340 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
341 sz *= 1024*1024;
342 else if (strcasecmp(p, "mbit") == 0)
343 sz *= 1024*1024/8;
344 else if (strcasecmp(p, "kbit") == 0)
345 sz *= 1024/8;
346 else if (strcasecmp(p, "b") != 0)
347 return -1;
348 }
349
350 *size = sz;
351 return 0;
352}
353
354int get_size_and_cell(unsigned *size, int *cell_log, char *str)
355{
356 char * slash = strchr(str, '/');
357
358 if (slash)
359 *slash = 0;
360
361 if (get_size(size, str))
362 return -1;
363
364 if (slash) {
365 int cell;
366 int i;
367
368 if (get_integer(&cell, slash+1, 0))
369 return -1;
370 *slash = '/';
371
372 for (i=0; i<32; i++) {
373 if ((1<<i) == cell) {
374 *cell_log = i;
375 return 0;
376 }
377 }
378 return -1;
379 }
380 return 0;
381}
382
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000383void print_size(char *buf, int len, __u32 sz)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000384{
385 double tmp = sz;
386
387 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
388 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
389 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
390 snprintf(buf, len, "%gKb", rint(tmp/1024));
391 else
392 snprintf(buf, len, "%ub", sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000393}
394
395char * sprint_size(__u32 size, char *buf)
396{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000397 print_size(buf, SPRINT_BSIZE-1, size);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000398 return buf;
399}
400
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000401void print_qdisc_handle(char *buf, int len, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000402{
403 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000404}
405
406char * sprint_qdisc_handle(__u32 h, char *buf)
407{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000408 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000409 return buf;
410}
411
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000412char * action_n2a(int action, char *buf, int len)
413{
414 switch (action) {
415 case -1:
416 return "continue";
417 break;
418 case TC_ACT_OK:
419 return "pass";
420 break;
421 case TC_ACT_SHOT:
422 return "drop";
423 break;
424 case TC_ACT_RECLASSIFY:
425 return "reclassify";
426 case TC_ACT_PIPE:
427 return "pipe";
428 case TC_ACT_STOLEN:
429 return "stolen";
430 default:
431 snprintf(buf, len, "%d", action);
432 return buf;
433 }
434}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000435
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000436int action_a2n(char *arg, int *result)
437{
438 int res;
439
440 if (matches(arg, "continue") == 0)
441 res = -1;
442 else if (matches(arg, "drop") == 0)
443 res = TC_ACT_SHOT;
444 else if (matches(arg, "shot") == 0)
445 res = TC_ACT_SHOT;
446 else if (matches(arg, "pass") == 0)
447 res = TC_ACT_OK;
448 else if (strcmp(arg, "ok") == 0)
449 res = TC_ACT_OK;
450 else if (matches(arg, "reclassify") == 0)
451 res = TC_ACT_RECLASSIFY;
452 else {
453 char dummy;
454 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
455 return -1;
456 }
457 *result = res;
458 return 0;
459}
460
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300461int get_linklayer(unsigned *val, const char *arg)
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200462{
463 int res;
464
465 if (matches(arg, "ethernet") == 0)
466 res = LINKLAYER_ETHERNET;
467 else if (matches(arg, "atm") == 0)
468 res = LINKLAYER_ATM;
469 else if (matches(arg, "adsl") == 0)
470 res = LINKLAYER_ATM;
471 else
472 return -1; /* Indicate error */
473
474 *val = res;
475 return 0;
476}
477
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300478void print_linklayer(char *buf, int len, unsigned linklayer)
479{
480 switch (linklayer) {
481 case LINKLAYER_UNSPEC:
482 snprintf(buf, len, "%s", "unspec");
483 return;
484 case LINKLAYER_ETHERNET:
485 snprintf(buf, len, "%s", "ethernet");
486 return;
487 case LINKLAYER_ATM:
488 snprintf(buf, len, "%s", "atm");
489 return;
490 default:
491 snprintf(buf, len, "%s", "unknown");
492 return;
493 }
494}
495
496char *sprint_linklayer(unsigned linklayer, char *buf)
497{
498 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
499 return buf;
500}
501
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000502void print_tm(FILE * f, const struct tcf_t *tm)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000503{
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000504 int hz = get_user_hz();
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000505 if (tm->install != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000506 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000507 if (tm->lastuse != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000508 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000509 if (tm->expires != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000510 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000511}
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000512
513void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
514{
515 SPRINT_BUF(b1);
14!tgraf7d69fd92005-01-18 22:11:58 +0000516 struct rtattr *tbs[TCA_STATS_MAX + 1];
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000517
14!tgraf7d69fd92005-01-18 22:11:58 +0000518 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000519
520 if (tbs[TCA_STATS_BASIC]) {
521 struct gnet_stats_basic bs = {0};
522 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
523 fprintf(fp, "%sSent %llu bytes %u pkt",
net[shemminger]!shemmingerb9062432005-01-17 23:28:16 +0000524 prefix, (unsigned long long) bs.bytes, bs.packets);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000525 }
526
527 if (tbs[TCA_STATS_QUEUE]) {
528 struct gnet_stats_queue q = {0};
529 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
530 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
531 q.drops, q.overlimits, q.requeues);
532 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800533
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700534 if (tbs[TCA_STATS_RATE_EST64]) {
535 struct gnet_stats_rate_est64 re = {0};
536
537 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
538 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
539 sizeof(re)));
540 fprintf(fp, "\n%srate %s %llupps ",
541 prefix, sprint_rate(re.bps, b1), re.pps);
542 } else if (tbs[TCA_STATS_RATE_EST]) {
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000543 struct gnet_stats_rate_est re = {0};
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700544
545 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
546 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000547 fprintf(fp, "\n%srate %s %upps ",
548 prefix, sprint_rate(re.bps, b1), re.pps);
549 }
550
551 if (tbs[TCA_STATS_QUEUE]) {
552 struct gnet_stats_queue q = {0};
553 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
554 if (!tbs[TCA_STATS_RATE_EST])
555 fprintf(fp, "\n%s", prefix);
556 fprintf(fp, "backlog %s %up requeues %u ",
557 sprint_size(q.backlog, b1), q.qlen, q.requeues);
558 }
559
560 if (xstats)
561 *xstats = tbs[TCA_STATS_APP] ? : NULL;
562}
563
564void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
565{
566 SPRINT_BUF(b1);
567
568 if (tb[TCA_STATS2]) {
569 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
570 if (xstats && NULL == *xstats)
571 goto compat_xstats;
572 return;
573 }
574 /* backward compatibility */
575 if (tb[TCA_STATS]) {
576 struct tc_stats st;
577
578 /* handle case where kernel returns more/less than we know about */
579 memset(&st, 0, sizeof(st));
580 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
581
582 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800583 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000584 st.overlimits);
585
586 if (st.bps || st.pps || st.qlen || st.backlog) {
587 fprintf(fp, "\n%s", prefix);
588 if (st.bps || st.pps) {
589 fprintf(fp, "rate ");
590 if (st.bps)
591 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
592 if (st.pps)
593 fprintf(fp, "%upps ", st.pps);
594 }
595 if (st.qlen || st.backlog) {
596 fprintf(fp, "backlog ");
597 if (st.backlog)
598 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
599 if (st.qlen)
600 fprintf(fp, "%up ", st.qlen);
601 }
602 }
603 }
604
605compat_xstats:
606 if (tb[TCA_XSTATS] && xstats)
607 *xstats = tb[TCA_XSTATS];
608}
609