blob: dc2b70faec01274af674250093ac9537f3b9f898 [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
Vadim Kochan46679bb2015-04-20 08:33:32 +0300131int print_tc_classid(char *buf, int blen, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000132{
Vadim Kochan46679bb2015-04-20 08:33:32 +0300133 SPRINT_BUF(handle) = {};
134 int hlen = SPRINT_BSIZE - 1;
Vadim Kochan4612d042015-03-03 18:41:18 +0200135
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000136 if (h == TC_H_ROOT)
Vadim Kochan4612d042015-03-03 18:41:18 +0200137 sprintf(handle, "root");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000138 else if (h == TC_H_UNSPEC)
Vadim Kochan46679bb2015-04-20 08:33:32 +0300139 snprintf(handle, hlen, "none");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000140 else if (TC_H_MAJ(h) == 0)
Vadim Kochan46679bb2015-04-20 08:33:32 +0300141 snprintf(handle, hlen, ":%x", TC_H_MIN(h));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000142 else if (TC_H_MIN(h) == 0)
Vadim Kochan46679bb2015-04-20 08:33:32 +0300143 snprintf(handle, hlen, "%x:", TC_H_MAJ(h) >> 16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000144 else
Vadim Kochan46679bb2015-04-20 08:33:32 +0300145 snprintf(handle, hlen, "%x:%x", TC_H_MAJ(h) >> 16, TC_H_MIN(h));
Vadim Kochan4612d042015-03-03 18:41:18 +0200146
147 if (use_names) {
148 char clname[IDNAME_MAX] = {};
149
150 if (id_to_name(cls_names, h, clname))
Vadim Kochan46679bb2015-04-20 08:33:32 +0300151 snprintf(buf, blen, "%s#%s", clname, handle);
Vadim Kochan4612d042015-03-03 18:41:18 +0200152 else
Vadim Kochan46679bb2015-04-20 08:33:32 +0300153 snprintf(buf, blen, "%s", handle);
Vadim Kochan4612d042015-03-03 18:41:18 +0200154 } else {
Vadim Kochan46679bb2015-04-20 08:33:32 +0300155 snprintf(buf, blen, "%s", handle);
Vadim Kochan4612d042015-03-03 18:41:18 +0200156 }
157
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000158 return 0;
159}
160
Vadim Kochan4612d042015-03-03 18:41:18 +0200161char *sprint_tc_classid(__u32 h, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000162{
163 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
164 strcpy(buf, "???");
165 return buf;
166}
167
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000168/* See http://physics.nist.gov/cuu/Units/binary.html */
169static const struct rate_suffix {
170 const char *name;
171 double scale;
172} suffixes[] = {
173 { "bit", 1. },
174 { "Kibit", 1024. },
175 { "kbit", 1000. },
176 { "mibit", 1024.*1024. },
177 { "mbit", 1000000. },
178 { "gibit", 1024.*1024.*1024. },
179 { "gbit", 1000000000. },
180 { "tibit", 1024.*1024.*1024.*1024. },
181 { "tbit", 1000000000000. },
182 { "Bps", 8. },
183 { "KiBps", 8.*1024. },
184 { "KBps", 8000. },
185 { "MiBps", 8.*1024*1024. },
186 { "MBps", 8000000. },
187 { "GiBps", 8.*1024.*1024.*1024. },
188 { "GBps", 8000000000. },
189 { "TiBps", 8.*1024.*1024.*1024.*1024. },
190 { "TBps", 8000000000000. },
191 { NULL }
192};
193
194
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000195int get_rate(unsigned *rate, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000196{
197 char *p;
198 double bps = strtod(str, &p);
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000199 const struct rate_suffix *s;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000200
201 if (p == str)
202 return -1;
203
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000204 for (s = suffixes; s->name; ++s) {
205 if (strcasecmp(s->name, p) == 0) {
Eric Dumazeta3038532013-06-03 05:51:33 +0000206 bps *= s->scale;
207 p += strlen(p);
208 break;
osdl.net!shemminger26ab0b12004-08-13 23:23:46 +0000209 }
210 }
211
Eric Dumazeta3038532013-06-03 05:51:33 +0000212 if (*p)
213 return -1; /* unknown suffix */
214
215 bps /= 8; /* -> bytes per second */
216 *rate = bps;
217 /* detect if an overflow happened */
218 if (*rate != floor(bps))
219 return -1;
220 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000221}
222
Eric Dumazet8334bb32013-11-12 14:34:07 -0800223int get_rate64(__u64 *rate, const char *str)
224{
225 char *p;
226 double bps = strtod(str, &p);
227 const struct rate_suffix *s;
228
229 if (p == str)
230 return -1;
231
232 for (s = suffixes; s->name; ++s) {
233 if (strcasecmp(s->name, p) == 0) {
234 bps *= s->scale;
235 p += strlen(p);
236 break;
237 }
238 }
239
240 if (*p)
241 return -1; /* unknown suffix */
242
243 bps /= 8; /* -> bytes per second */
244 *rate = bps;
245 return 0;
246}
247
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700248void print_rate(char *buf, int len, __u64 rate)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000249{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000250 extern int use_iec;
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800251 unsigned long kilo = use_iec ? 1024 : 1000;
252 const char *str = use_iec ? "i" : "";
253 int i = 0;
254 static char *units[5] = {"", "K", "M", "G", "T"};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000255
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800256 rate <<= 3; /* bytes/sec -> bits/sec */
257
258 for (i = 0; i < ARRAY_SIZE(units); i++) {
259 if (rate < kilo)
260 break;
261 if (((rate % kilo) != 0) && rate < 1000*kilo)
262 break;
263 rate /= kilo;
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000264 }
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800265 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000266}
267
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700268char * sprint_rate(__u64 rate, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000269{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000270 print_rate(buf, SPRINT_BSIZE-1, rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000271 return buf;
272}
273
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100274int get_time(unsigned *time, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000275{
276 double t;
277 char *p;
278
279 t = strtod(str, &p);
280 if (p == str)
281 return -1;
282
283 if (*p) {
284 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
285 strcasecmp(p, "secs")==0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100286 t *= TIME_UNITS_PER_SEC;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000287 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
288 strcasecmp(p, "msecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100289 t *= TIME_UNITS_PER_SEC/1000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000290 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
291 strcasecmp(p, "usecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100292 t *= TIME_UNITS_PER_SEC/1000000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000293 else
294 return -1;
295 }
296
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100297 *time = t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298 return 0;
299}
300
301
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100302void print_time(char *buf, int len, __u32 time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000303{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100304 double tmp = time;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000305
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100306 if (tmp >= TIME_UNITS_PER_SEC)
307 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
308 else if (tmp >= TIME_UNITS_PER_SEC/1000)
309 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000310 else
Stephen Hemminger89151442007-03-14 10:14:07 -0700311 snprintf(buf, len, "%uus", time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000312}
313
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100314char * sprint_time(__u32 time, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000315{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100316 print_time(buf, SPRINT_BSIZE-1, time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000317 return buf;
318}
319
Patrick McHardybd29e352007-03-04 20:15:01 +0100320char * sprint_ticks(__u32 ticks, char *buf)
321{
322 return sprint_time(tc_core_tick2time(ticks), buf);
323}
324
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000325int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000326{
327 double sz;
328 char *p;
329
330 sz = strtod(str, &p);
331 if (p == str)
332 return -1;
333
334 if (*p) {
335 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
336 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000337 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
338 sz *= 1024*1024*1024;
339 else if (strcasecmp(p, "gbit") == 0)
340 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000341 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
342 sz *= 1024*1024;
343 else if (strcasecmp(p, "mbit") == 0)
344 sz *= 1024*1024/8;
345 else if (strcasecmp(p, "kbit") == 0)
346 sz *= 1024/8;
347 else if (strcasecmp(p, "b") != 0)
348 return -1;
349 }
350
351 *size = sz;
352 return 0;
353}
354
355int get_size_and_cell(unsigned *size, int *cell_log, char *str)
356{
357 char * slash = strchr(str, '/');
358
359 if (slash)
360 *slash = 0;
361
362 if (get_size(size, str))
363 return -1;
364
365 if (slash) {
366 int cell;
367 int i;
368
369 if (get_integer(&cell, slash+1, 0))
370 return -1;
371 *slash = '/';
372
373 for (i=0; i<32; i++) {
374 if ((1<<i) == cell) {
375 *cell_log = i;
376 return 0;
377 }
378 }
379 return -1;
380 }
381 return 0;
382}
383
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000384void print_size(char *buf, int len, __u32 sz)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000385{
386 double tmp = sz;
387
388 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
389 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
390 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
391 snprintf(buf, len, "%gKb", rint(tmp/1024));
392 else
393 snprintf(buf, len, "%ub", sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000394}
395
396char * sprint_size(__u32 size, char *buf)
397{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000398 print_size(buf, SPRINT_BSIZE-1, size);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000399 return buf;
400}
401
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000402void print_qdisc_handle(char *buf, int len, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000403{
404 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000405}
406
407char * sprint_qdisc_handle(__u32 h, char *buf)
408{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000409 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000410 return buf;
411}
412
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000413char * action_n2a(int action, char *buf, int len)
414{
415 switch (action) {
416 case -1:
417 return "continue";
418 break;
419 case TC_ACT_OK:
420 return "pass";
421 break;
422 case TC_ACT_SHOT:
423 return "drop";
424 break;
425 case TC_ACT_RECLASSIFY:
426 return "reclassify";
427 case TC_ACT_PIPE:
428 return "pipe";
429 case TC_ACT_STOLEN:
430 return "stolen";
431 default:
432 snprintf(buf, len, "%d", action);
433 return buf;
434 }
435}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000436
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000437int action_a2n(char *arg, int *result)
438{
439 int res;
440
441 if (matches(arg, "continue") == 0)
442 res = -1;
443 else if (matches(arg, "drop") == 0)
444 res = TC_ACT_SHOT;
445 else if (matches(arg, "shot") == 0)
446 res = TC_ACT_SHOT;
447 else if (matches(arg, "pass") == 0)
448 res = TC_ACT_OK;
449 else if (strcmp(arg, "ok") == 0)
450 res = TC_ACT_OK;
451 else if (matches(arg, "reclassify") == 0)
452 res = TC_ACT_RECLASSIFY;
453 else {
454 char dummy;
455 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
456 return -1;
457 }
458 *result = res;
459 return 0;
460}
461
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300462int get_linklayer(unsigned *val, const char *arg)
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200463{
464 int res;
465
466 if (matches(arg, "ethernet") == 0)
467 res = LINKLAYER_ETHERNET;
468 else if (matches(arg, "atm") == 0)
469 res = LINKLAYER_ATM;
470 else if (matches(arg, "adsl") == 0)
471 res = LINKLAYER_ATM;
472 else
473 return -1; /* Indicate error */
474
475 *val = res;
476 return 0;
477}
478
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300479void print_linklayer(char *buf, int len, unsigned linklayer)
480{
481 switch (linklayer) {
482 case LINKLAYER_UNSPEC:
483 snprintf(buf, len, "%s", "unspec");
484 return;
485 case LINKLAYER_ETHERNET:
486 snprintf(buf, len, "%s", "ethernet");
487 return;
488 case LINKLAYER_ATM:
489 snprintf(buf, len, "%s", "atm");
490 return;
491 default:
492 snprintf(buf, len, "%s", "unknown");
493 return;
494 }
495}
496
497char *sprint_linklayer(unsigned linklayer, char *buf)
498{
499 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
500 return buf;
501}
502
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000503void print_tm(FILE * f, const struct tcf_t *tm)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000504{
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000505 int hz = get_user_hz();
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000506 if (tm->install != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000507 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000508 if (tm->lastuse != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000509 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000510 if (tm->expires != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000511 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000512}
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000513
514void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
515{
516 SPRINT_BUF(b1);
14!tgraf7d69fd92005-01-18 22:11:58 +0000517 struct rtattr *tbs[TCA_STATS_MAX + 1];
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000518
14!tgraf7d69fd92005-01-18 22:11:58 +0000519 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000520
521 if (tbs[TCA_STATS_BASIC]) {
522 struct gnet_stats_basic bs = {0};
523 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
524 fprintf(fp, "%sSent %llu bytes %u pkt",
net[shemminger]!shemmingerb9062432005-01-17 23:28:16 +0000525 prefix, (unsigned long long) bs.bytes, bs.packets);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000526 }
527
528 if (tbs[TCA_STATS_QUEUE]) {
529 struct gnet_stats_queue q = {0};
530 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
531 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
532 q.drops, q.overlimits, q.requeues);
533 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800534
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700535 if (tbs[TCA_STATS_RATE_EST64]) {
536 struct gnet_stats_rate_est64 re = {0};
537
538 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
539 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
540 sizeof(re)));
541 fprintf(fp, "\n%srate %s %llupps ",
542 prefix, sprint_rate(re.bps, b1), re.pps);
543 } else if (tbs[TCA_STATS_RATE_EST]) {
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000544 struct gnet_stats_rate_est re = {0};
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700545
546 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
547 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000548 fprintf(fp, "\n%srate %s %upps ",
549 prefix, sprint_rate(re.bps, b1), re.pps);
550 }
551
552 if (tbs[TCA_STATS_QUEUE]) {
553 struct gnet_stats_queue q = {0};
554 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
555 if (!tbs[TCA_STATS_RATE_EST])
556 fprintf(fp, "\n%s", prefix);
557 fprintf(fp, "backlog %s %up requeues %u ",
558 sprint_size(q.backlog, b1), q.qlen, q.requeues);
559 }
560
561 if (xstats)
562 *xstats = tbs[TCA_STATS_APP] ? : NULL;
563}
564
565void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
566{
567 SPRINT_BUF(b1);
568
569 if (tb[TCA_STATS2]) {
570 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
571 if (xstats && NULL == *xstats)
572 goto compat_xstats;
573 return;
574 }
575 /* backward compatibility */
576 if (tb[TCA_STATS]) {
577 struct tc_stats st;
578
579 /* handle case where kernel returns more/less than we know about */
580 memset(&st, 0, sizeof(st));
581 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
582
583 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800584 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000585 st.overlimits);
586
587 if (st.bps || st.pps || st.qlen || st.backlog) {
588 fprintf(fp, "\n%s", prefix);
589 if (st.bps || st.pps) {
590 fprintf(fp, "rate ");
591 if (st.bps)
592 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
593 if (st.pps)
594 fprintf(fp, "%upps ", st.pps);
595 }
596 if (st.qlen || st.backlog) {
597 fprintf(fp, "backlog ");
598 if (st.backlog)
599 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
600 if (st.qlen)
601 fprintf(fp, "%up ", st.qlen);
602 }
603 }
604 }
605
606compat_xstats:
607 if (tb[TCA_XSTATS] && xstats)
608 *xstats = tb[TCA_XSTATS];
609}
610