blob: aa6de2448bf2e3ef93e4d7f304a688f7f124ebab [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" : "";
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800253 static char *units[5] = {"", "K", "M", "G", "T"};
Daniel Borkmannad1fe0d2015-05-29 21:47:45 +0200254 int i;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000255
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800256 rate <<= 3; /* bytes/sec -> bits/sec */
257
Daniel Borkmannad1fe0d2015-05-29 21:47:45 +0200258 for (i = 0; i < ARRAY_SIZE(units) - 1; i++) {
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800259 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 }
Daniel Borkmannad1fe0d2015-05-29 21:47:45 +0200265
Eric Dumazet8cecdc22013-11-21 22:37:17 -0800266 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000267}
268
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700269char * sprint_rate(__u64 rate, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000270{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000271 print_rate(buf, SPRINT_BSIZE-1, rate);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000272 return buf;
273}
274
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100275int get_time(unsigned *time, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000276{
277 double t;
278 char *p;
279
280 t = strtod(str, &p);
281 if (p == str)
282 return -1;
283
284 if (*p) {
285 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
286 strcasecmp(p, "secs")==0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100287 t *= TIME_UNITS_PER_SEC;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000288 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
289 strcasecmp(p, "msecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100290 t *= TIME_UNITS_PER_SEC/1000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000291 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
292 strcasecmp(p, "usecs") == 0)
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100293 t *= TIME_UNITS_PER_SEC/1000000;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000294 else
295 return -1;
296 }
297
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100298 *time = t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000299 return 0;
300}
301
302
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100303void print_time(char *buf, int len, __u32 time)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000304{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100305 double tmp = time;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000306
Patrick McHardyf0bda7e2007-03-04 20:14:59 +0100307 if (tmp >= TIME_UNITS_PER_SEC)
308 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
309 else if (tmp >= TIME_UNITS_PER_SEC/1000)
310 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000311 else
Stephen Hemminger89151442007-03-14 10:14:07 -0700312 snprintf(buf, len, "%uus", time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000313}
314
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100315char * sprint_time(__u32 time, char *buf)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000316{
Patrick McHardy8f34caa2007-03-04 20:15:00 +0100317 print_time(buf, SPRINT_BSIZE-1, time);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000318 return buf;
319}
320
Patrick McHardybd29e352007-03-04 20:15:01 +0100321char * sprint_ticks(__u32 ticks, char *buf)
322{
323 return sprint_time(tc_core_tick2time(ticks), buf);
324}
325
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000326int get_size(unsigned *size, const char *str)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000327{
328 double sz;
329 char *p;
330
331 sz = strtod(str, &p);
332 if (p == str)
333 return -1;
334
335 if (*p) {
336 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
337 sz *= 1024;
osdl.org!shemmingerdbd90dc2004-06-02 20:22:08 +0000338 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
339 sz *= 1024*1024*1024;
340 else if (strcasecmp(p, "gbit") == 0)
341 sz *= 1024*1024*1024/8;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000342 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
343 sz *= 1024*1024;
344 else if (strcasecmp(p, "mbit") == 0)
345 sz *= 1024*1024/8;
346 else if (strcasecmp(p, "kbit") == 0)
347 sz *= 1024/8;
348 else if (strcasecmp(p, "b") != 0)
349 return -1;
350 }
351
352 *size = sz;
353 return 0;
354}
355
356int get_size_and_cell(unsigned *size, int *cell_log, char *str)
357{
358 char * slash = strchr(str, '/');
359
360 if (slash)
361 *slash = 0;
362
363 if (get_size(size, str))
364 return -1;
365
366 if (slash) {
367 int cell;
368 int i;
369
370 if (get_integer(&cell, slash+1, 0))
371 return -1;
372 *slash = '/';
373
374 for (i=0; i<32; i++) {
375 if ((1<<i) == cell) {
376 *cell_log = i;
377 return 0;
378 }
379 }
380 return -1;
381 }
382 return 0;
383}
384
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000385void print_size(char *buf, int len, __u32 sz)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000386{
387 double tmp = sz;
388
389 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
390 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
391 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
392 snprintf(buf, len, "%gKb", rint(tmp/1024));
393 else
394 snprintf(buf, len, "%ub", sz);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000395}
396
397char * sprint_size(__u32 size, char *buf)
398{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000399 print_size(buf, SPRINT_BSIZE-1, size);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000400 return buf;
401}
402
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000403void print_qdisc_handle(char *buf, int len, __u32 h)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000404{
405 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000406}
407
408char * sprint_qdisc_handle(__u32 h, char *buf)
409{
osdl.net!shemmingerd40b38b2004-06-28 20:42:59 +0000410 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000411 return buf;
412}
413
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000414char * action_n2a(int action, char *buf, int len)
415{
416 switch (action) {
417 case -1:
418 return "continue";
419 break;
420 case TC_ACT_OK:
421 return "pass";
422 break;
423 case TC_ACT_SHOT:
424 return "drop";
425 break;
426 case TC_ACT_RECLASSIFY:
427 return "reclassify";
428 case TC_ACT_PIPE:
429 return "pipe";
430 case TC_ACT_STOLEN:
431 return "stolen";
432 default:
433 snprintf(buf, len, "%d", action);
434 return buf;
435 }
436}
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000437
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000438int action_a2n(char *arg, int *result)
439{
440 int res;
441
442 if (matches(arg, "continue") == 0)
443 res = -1;
444 else if (matches(arg, "drop") == 0)
445 res = TC_ACT_SHOT;
446 else if (matches(arg, "shot") == 0)
447 res = TC_ACT_SHOT;
448 else if (matches(arg, "pass") == 0)
449 res = TC_ACT_OK;
450 else if (strcmp(arg, "ok") == 0)
451 res = TC_ACT_OK;
452 else if (matches(arg, "reclassify") == 0)
453 res = TC_ACT_RECLASSIFY;
454 else {
455 char dummy;
456 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
457 return -1;
458 }
459 *result = res;
460 return 0;
461}
462
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300463int get_linklayer(unsigned *val, const char *arg)
Jesper Dangaard Brouer292f29b2008-04-09 23:01:01 +0200464{
465 int res;
466
467 if (matches(arg, "ethernet") == 0)
468 res = LINKLAYER_ETHERNET;
469 else if (matches(arg, "atm") == 0)
470 res = LINKLAYER_ATM;
471 else if (matches(arg, "adsl") == 0)
472 res = LINKLAYER_ATM;
473 else
474 return -1; /* Indicate error */
475
476 *val = res;
477 return 0;
478}
479
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300480void print_linklayer(char *buf, int len, unsigned linklayer)
481{
482 switch (linklayer) {
483 case LINKLAYER_UNSPEC:
484 snprintf(buf, len, "%s", "unspec");
485 return;
486 case LINKLAYER_ETHERNET:
487 snprintf(buf, len, "%s", "ethernet");
488 return;
489 case LINKLAYER_ATM:
490 snprintf(buf, len, "%s", "atm");
491 return;
492 default:
493 snprintf(buf, len, "%s", "unknown");
494 return;
495 }
496}
497
498char *sprint_linklayer(unsigned linklayer, char *buf)
499{
500 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
501 return buf;
502}
503
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000504void print_tm(FILE * f, const struct tcf_t *tm)
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000505{
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000506 int hz = get_user_hz();
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000507 if (tm->install != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000508 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000509 if (tm->lastuse != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000510 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000511 if (tm->expires != 0)
osdl.net!shemminger63ae25d2005-03-10 19:04:17 +0000512 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000513}
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000514
515void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
516{
517 SPRINT_BUF(b1);
14!tgraf7d69fd92005-01-18 22:11:58 +0000518 struct rtattr *tbs[TCA_STATS_MAX + 1];
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000519
14!tgraf7d69fd92005-01-18 22:11:58 +0000520 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000521
522 if (tbs[TCA_STATS_BASIC]) {
523 struct gnet_stats_basic bs = {0};
524 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
525 fprintf(fp, "%sSent %llu bytes %u pkt",
net[shemminger]!shemmingerb9062432005-01-17 23:28:16 +0000526 prefix, (unsigned long long) bs.bytes, bs.packets);
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000527 }
528
529 if (tbs[TCA_STATS_QUEUE]) {
530 struct gnet_stats_queue q = {0};
531 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
532 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
533 q.drops, q.overlimits, q.requeues);
534 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800535
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700536 if (tbs[TCA_STATS_RATE_EST64]) {
537 struct gnet_stats_rate_est64 re = {0};
538
539 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
540 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
541 sizeof(re)));
542 fprintf(fp, "\n%srate %s %llupps ",
543 prefix, sprint_rate(re.bps, b1), re.pps);
544 } else if (tbs[TCA_STATS_RATE_EST]) {
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000545 struct gnet_stats_rate_est re = {0};
Eric Dumazet8f7574e2013-09-17 04:19:03 -0700546
547 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
548 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000549 fprintf(fp, "\n%srate %s %upps ",
550 prefix, sprint_rate(re.bps, b1), re.pps);
551 }
552
553 if (tbs[TCA_STATS_QUEUE]) {
554 struct gnet_stats_queue q = {0};
555 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
556 if (!tbs[TCA_STATS_RATE_EST])
557 fprintf(fp, "\n%s", prefix);
558 fprintf(fp, "backlog %s %up requeues %u ",
559 sprint_size(q.backlog, b1), q.qlen, q.requeues);
560 }
561
562 if (xstats)
563 *xstats = tbs[TCA_STATS_APP] ? : NULL;
564}
565
566void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
567{
568 SPRINT_BUF(b1);
569
570 if (tb[TCA_STATS2]) {
571 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
572 if (xstats && NULL == *xstats)
573 goto compat_xstats;
574 return;
575 }
576 /* backward compatibility */
577 if (tb[TCA_STATS]) {
578 struct tc_stats st;
579
580 /* handle case where kernel returns more/less than we know about */
581 memset(&st, 0, sizeof(st));
582 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
583
584 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800585 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000586 st.overlimits);
587
588 if (st.bps || st.pps || st.qlen || st.backlog) {
589 fprintf(fp, "\n%s", prefix);
590 if (st.bps || st.pps) {
591 fprintf(fp, "rate ");
592 if (st.bps)
593 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
594 if (st.pps)
595 fprintf(fp, "%upps ", st.pps);
596 }
597 if (st.qlen || st.backlog) {
598 fprintf(fp, "backlog ");
599 if (st.backlog)
600 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
601 if (st.qlen)
602 fprintf(fp, "%up ", st.qlen);
603 }
604 }
605 }
606
607compat_xstats:
608 if (tb[TCA_XSTATS] && xstats)
609 *xstats = tb[TCA_XSTATS];
610}
611