2004-04-16 04:56:59 +08:00
|
|
|
/*
|
|
|
|
* tc_util.c Misc TC utility functions.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
2014-05-27 15:40:10 +08:00
|
|
|
#include <sys/param.h>
|
2004-04-16 04:56:59 +08:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2015-03-25 11:14:37 +08:00
|
|
|
#include <errno.h>
|
2004-04-16 04:56:59 +08:00
|
|
|
|
|
|
|
#include "utils.h"
|
2015-03-04 00:41:18 +08:00
|
|
|
#include "names.h"
|
2004-04-16 04:56:59 +08:00
|
|
|
#include "tc_util.h"
|
2015-03-04 00:41:18 +08:00
|
|
|
#include "tc_common.h"
|
2004-04-16 04:56:59 +08:00
|
|
|
|
2008-08-22 22:54:12 +08:00
|
|
|
#ifndef LIBDIR
|
2012-03-02 00:46:26 +08:00
|
|
|
#define LIBDIR "/usr/lib"
|
2008-06-02 08:33:44 +08:00
|
|
|
#endif
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
static struct db_names *cls_names;
|
2015-03-04 00:41:18 +08:00
|
|
|
|
2015-03-25 11:14:37 +08:00
|
|
|
#define NAMES_DB "/etc/iproute2/tc_cls"
|
2015-03-04 00:41:18 +08:00
|
|
|
|
|
|
|
int cls_names_init(char *path)
|
|
|
|
{
|
2015-03-25 11:14:37 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
cls_names = db_names_alloc();
|
|
|
|
if (!cls_names)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = db_names_load(cls_names, path ?: NAMES_DB);
|
|
|
|
if (ret == -ENOENT && path) {
|
|
|
|
fprintf(stderr, "Can't open class names file: %s\n", path);
|
2015-03-04 00:41:18 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-03-25 11:14:37 +08:00
|
|
|
if (ret) {
|
|
|
|
db_names_free(cls_names);
|
|
|
|
cls_names = NULL;
|
|
|
|
}
|
2015-03-04 00:41:18 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cls_names_uninit(void)
|
|
|
|
{
|
|
|
|
db_names_free(cls_names);
|
|
|
|
}
|
|
|
|
|
2007-06-21 06:27:22 +08:00
|
|
|
const char *get_tc_lib(void)
|
|
|
|
{
|
|
|
|
const char *lib_dir;
|
|
|
|
|
|
|
|
lib_dir = getenv("TC_LIB_DIR");
|
|
|
|
if (!lib_dir)
|
2008-08-22 22:54:12 +08:00
|
|
|
lib_dir = LIBDIR "/tc/";
|
2007-06-21 06:27:22 +08:00
|
|
|
|
|
|
|
return lib_dir;
|
|
|
|
}
|
|
|
|
|
2004-06-03 04:22:08 +08:00
|
|
|
int get_qdisc_handle(__u32 *h, const char *str)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
|
|
|
__u32 maj;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
maj = TC_H_UNSPEC;
|
|
|
|
if (strcmp(str, "none") == 0)
|
|
|
|
goto ok;
|
|
|
|
maj = strtoul(str, &p, 16);
|
2016-09-16 16:30:00 +08:00
|
|
|
if (p == str || maj >= (1 << 16))
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
maj <<= 16;
|
2016-03-22 02:48:36 +08:00
|
|
|
if (*p != ':' && *p != 0)
|
2004-04-16 04:56:59 +08:00
|
|
|
return -1;
|
|
|
|
ok:
|
|
|
|
*h = maj;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-03 04:22:08 +08:00
|
|
|
int get_tc_classid(__u32 *h, const char *str)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
|
|
|
__u32 maj, min;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
maj = TC_H_ROOT;
|
|
|
|
if (strcmp(str, "root") == 0)
|
|
|
|
goto ok;
|
|
|
|
maj = TC_H_UNSPEC;
|
|
|
|
if (strcmp(str, "none") == 0)
|
|
|
|
goto ok;
|
|
|
|
maj = strtoul(str, &p, 16);
|
|
|
|
if (p == str) {
|
|
|
|
maj = 0;
|
|
|
|
if (*p != ':')
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (*p == ':') {
|
2005-02-08 02:16:29 +08:00
|
|
|
if (maj >= (1<<16))
|
|
|
|
return -1;
|
2004-04-16 04:56:59 +08:00
|
|
|
maj <<= 16;
|
|
|
|
str = p+1;
|
|
|
|
min = strtoul(str, &p, 16);
|
|
|
|
if (*p != 0)
|
|
|
|
return -1;
|
2005-02-08 02:16:29 +08:00
|
|
|
if (min >= (1<<16))
|
|
|
|
return -1;
|
2004-04-16 04:56:59 +08:00
|
|
|
maj |= min;
|
|
|
|
} else if (*p != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ok:
|
|
|
|
*h = maj;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-20 13:33:32 +08:00
|
|
|
int print_tc_classid(char *buf, int blen, __u32 h)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2015-04-20 13:33:32 +08:00
|
|
|
SPRINT_BUF(handle) = {};
|
|
|
|
int hlen = SPRINT_BSIZE - 1;
|
2015-03-04 00:41:18 +08:00
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
if (h == TC_H_ROOT)
|
2015-03-04 00:41:18 +08:00
|
|
|
sprintf(handle, "root");
|
2004-04-16 04:56:59 +08:00
|
|
|
else if (h == TC_H_UNSPEC)
|
2015-04-20 13:33:32 +08:00
|
|
|
snprintf(handle, hlen, "none");
|
2004-04-16 04:56:59 +08:00
|
|
|
else if (TC_H_MAJ(h) == 0)
|
2015-04-20 13:33:32 +08:00
|
|
|
snprintf(handle, hlen, ":%x", TC_H_MIN(h));
|
2004-04-16 04:56:59 +08:00
|
|
|
else if (TC_H_MIN(h) == 0)
|
2015-04-20 13:33:32 +08:00
|
|
|
snprintf(handle, hlen, "%x:", TC_H_MAJ(h) >> 16);
|
2004-04-16 04:56:59 +08:00
|
|
|
else
|
2015-04-20 13:33:32 +08:00
|
|
|
snprintf(handle, hlen, "%x:%x", TC_H_MAJ(h) >> 16, TC_H_MIN(h));
|
2015-03-04 00:41:18 +08:00
|
|
|
|
|
|
|
if (use_names) {
|
|
|
|
char clname[IDNAME_MAX] = {};
|
|
|
|
|
|
|
|
if (id_to_name(cls_names, h, clname))
|
2015-04-20 13:33:32 +08:00
|
|
|
snprintf(buf, blen, "%s#%s", clname, handle);
|
2015-03-04 00:41:18 +08:00
|
|
|
else
|
2015-04-20 13:33:32 +08:00
|
|
|
snprintf(buf, blen, "%s", handle);
|
2015-03-04 00:41:18 +08:00
|
|
|
} else {
|
2015-04-20 13:33:32 +08:00
|
|
|
snprintf(buf, blen, "%s", handle);
|
2015-03-04 00:41:18 +08:00
|
|
|
}
|
|
|
|
|
2004-04-16 04:56:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-04 00:41:18 +08:00
|
|
|
char *sprint_tc_classid(__u32 h, char *buf)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
|
|
|
if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
|
|
|
|
strcpy(buf, "???");
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2004-08-14 07:23:46 +08:00
|
|
|
/* See http://physics.nist.gov/cuu/Units/binary.html */
|
|
|
|
static const struct rate_suffix {
|
|
|
|
const char *name;
|
|
|
|
double scale;
|
|
|
|
} suffixes[] = {
|
|
|
|
{ "bit", 1. },
|
|
|
|
{ "Kibit", 1024. },
|
|
|
|
{ "kbit", 1000. },
|
|
|
|
{ "mibit", 1024.*1024. },
|
|
|
|
{ "mbit", 1000000. },
|
|
|
|
{ "gibit", 1024.*1024.*1024. },
|
|
|
|
{ "gbit", 1000000000. },
|
|
|
|
{ "tibit", 1024.*1024.*1024.*1024. },
|
|
|
|
{ "tbit", 1000000000000. },
|
|
|
|
{ "Bps", 8. },
|
|
|
|
{ "KiBps", 8.*1024. },
|
|
|
|
{ "KBps", 8000. },
|
|
|
|
{ "MiBps", 8.*1024*1024. },
|
|
|
|
{ "MBps", 8000000. },
|
|
|
|
{ "GiBps", 8.*1024.*1024.*1024. },
|
|
|
|
{ "GBps", 8000000000. },
|
|
|
|
{ "TiBps", 8.*1024.*1024.*1024.*1024. },
|
|
|
|
{ "TBps", 8000000000000. },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2019-06-29 00:03:45 +08:00
|
|
|
/* Parse a percent e.g: '30%'
|
|
|
|
* return: 0 = ok, -1 = error, 1 = out of range
|
|
|
|
*/
|
|
|
|
int parse_percent(double *val, const char *str)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
*val = strtod(str, &p) / 100.;
|
2019-07-13 17:44:07 +08:00
|
|
|
if (*val > 1.0 || *val < 0.0)
|
2019-06-29 00:03:45 +08:00
|
|
|
return 1;
|
|
|
|
if (*p && strcmp(p, "%"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-07 02:49:47 +08:00
|
|
|
static int parse_percent_rate(char *rate, size_t len,
|
|
|
|
const char *str, const char *dev)
|
2017-11-21 10:20:47 +08:00
|
|
|
{
|
|
|
|
long dev_mbit;
|
|
|
|
int ret;
|
2019-02-07 23:29:54 +08:00
|
|
|
double perc, rate_bit;
|
2019-02-07 02:41:58 +08:00
|
|
|
char *str_perc = NULL;
|
2017-11-21 10:20:47 +08:00
|
|
|
|
|
|
|
if (!dev[0]) {
|
|
|
|
fprintf(stderr, "No device specified; specify device to rate limit by percentage\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_prop(dev, "speed", &dev_mbit))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = sscanf(str, "%m[0-9.%]", &str_perc);
|
|
|
|
if (ret != 1)
|
|
|
|
goto malf;
|
|
|
|
|
2019-07-13 17:44:07 +08:00
|
|
|
ret = parse_percent(&perc, str_perc);
|
|
|
|
if (ret == 1) {
|
|
|
|
fprintf(stderr, "Invalid rate specified; should be between [0,100]%% but is %s\n", str);
|
|
|
|
goto err;
|
|
|
|
} else if (ret == -1) {
|
2017-11-21 10:20:47 +08:00
|
|
|
goto malf;
|
2019-07-13 17:44:07 +08:00
|
|
|
}
|
2017-11-21 10:20:47 +08:00
|
|
|
|
|
|
|
free(str_perc);
|
|
|
|
|
2019-02-07 23:29:54 +08:00
|
|
|
rate_bit = perc * dev_mbit * 1000 * 1000;
|
2017-11-21 10:20:47 +08:00
|
|
|
|
2019-02-07 23:29:54 +08:00
|
|
|
ret = snprintf(rate, len, "%lf", rate_bit);
|
2019-02-07 02:49:47 +08:00
|
|
|
if (ret <= 0 || ret >= len) {
|
2017-11-21 10:20:47 +08:00
|
|
|
fprintf(stderr, "Unable to parse calculated rate\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
malf:
|
|
|
|
fprintf(stderr, "Specified rate value could not be read or is malformed\n");
|
2019-07-13 17:44:07 +08:00
|
|
|
err:
|
|
|
|
free(str_perc);
|
2017-11-21 10:20:47 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_percent_rate(unsigned int *rate, const char *str, const char *dev)
|
|
|
|
{
|
|
|
|
char r_str[20];
|
|
|
|
|
2019-02-07 02:49:47 +08:00
|
|
|
if (parse_percent_rate(r_str, sizeof(r_str), str, dev))
|
2017-11-21 10:20:47 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return get_rate(rate, r_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_percent_rate64(__u64 *rate, const char *str, const char *dev)
|
|
|
|
{
|
|
|
|
char r_str[20];
|
|
|
|
|
2019-02-07 02:49:47 +08:00
|
|
|
if (parse_percent_rate(r_str, sizeof(r_str), str, dev))
|
2017-11-21 10:20:47 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return get_rate64(rate, r_str);
|
|
|
|
}
|
2004-08-14 07:23:46 +08:00
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
int get_rate(unsigned int *rate, const char *str)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
double bps = strtod(str, &p);
|
2004-08-14 07:23:46 +08:00
|
|
|
const struct rate_suffix *s;
|
2004-04-16 04:56:59 +08:00
|
|
|
|
|
|
|
if (p == str)
|
|
|
|
return -1;
|
|
|
|
|
2004-08-14 07:23:46 +08:00
|
|
|
for (s = suffixes; s->name; ++s) {
|
|
|
|
if (strcasecmp(s->name, p) == 0) {
|
get_rate: detect 32bit overflows
On Mon, 2013-06-03 at 16:36 +0100, Ben Hutchings wrote:
> Oops, I read this as being strtol() currently, not strtod(). Currently
> '1.5gbit' will work, but this change will break that. So I think you
> need to keep bps as a double.
Arg
> Then here I think the check should be *rate != floor(bps), i.e. accept
> rounding down of a non-integer number of bytes but any other change is
> assumed to be overflow.
Thanks Ben, here is v4 then ;)
[PATCH v4] get_rate: detect 32bit overflows
Current rate limit is 34.359.738.360 bit per second, and
unfortunately 40Gbps links are above it.
overflows in get_rate() are currently not detected, and some
users are confused. Let's detect this and complain.
Note that some qdisc are ready to get extended range, but this will
need additional attributes and new iproute2
With help from Ben Hutchings
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Ben Hutchings <bhutchings@solarflare.com>
2013-06-03 13:51:33 +08:00
|
|
|
bps *= s->scale;
|
|
|
|
p += strlen(p);
|
|
|
|
break;
|
2004-08-14 07:23:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
get_rate: detect 32bit overflows
On Mon, 2013-06-03 at 16:36 +0100, Ben Hutchings wrote:
> Oops, I read this as being strtol() currently, not strtod(). Currently
> '1.5gbit' will work, but this change will break that. So I think you
> need to keep bps as a double.
Arg
> Then here I think the check should be *rate != floor(bps), i.e. accept
> rounding down of a non-integer number of bytes but any other change is
> assumed to be overflow.
Thanks Ben, here is v4 then ;)
[PATCH v4] get_rate: detect 32bit overflows
Current rate limit is 34.359.738.360 bit per second, and
unfortunately 40Gbps links are above it.
overflows in get_rate() are currently not detected, and some
users are confused. Let's detect this and complain.
Note that some qdisc are ready to get extended range, but this will
need additional attributes and new iproute2
With help from Ben Hutchings
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Ben Hutchings <bhutchings@solarflare.com>
2013-06-03 13:51:33 +08:00
|
|
|
if (*p)
|
|
|
|
return -1; /* unknown suffix */
|
|
|
|
|
|
|
|
bps /= 8; /* -> bytes per second */
|
|
|
|
*rate = bps;
|
|
|
|
/* detect if an overflow happened */
|
|
|
|
if (*rate != floor(bps))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
|
|
|
|
2013-11-13 06:34:07 +08:00
|
|
|
int get_rate64(__u64 *rate, const char *str)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
double bps = strtod(str, &p);
|
|
|
|
const struct rate_suffix *s;
|
|
|
|
|
|
|
|
if (p == str)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (s = suffixes; s->name; ++s) {
|
|
|
|
if (strcasecmp(s->name, p) == 0) {
|
|
|
|
bps *= s->scale;
|
|
|
|
p += strlen(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p)
|
|
|
|
return -1; /* unknown suffix */
|
|
|
|
|
|
|
|
bps /= 8; /* -> bytes per second */
|
|
|
|
*rate = bps;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-17 19:19:03 +08:00
|
|
|
void print_rate(char *buf, int len, __u64 rate)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2004-06-29 04:42:59 +08:00
|
|
|
extern int use_iec;
|
2013-11-22 14:37:17 +08:00
|
|
|
unsigned long kilo = use_iec ? 1024 : 1000;
|
|
|
|
const char *str = use_iec ? "i" : "";
|
|
|
|
static char *units[5] = {"", "K", "M", "G", "T"};
|
2015-05-30 03:47:45 +08:00
|
|
|
int i;
|
2004-06-29 04:42:59 +08:00
|
|
|
|
2013-11-22 14:37:17 +08:00
|
|
|
rate <<= 3; /* bytes/sec -> bits/sec */
|
|
|
|
|
2015-05-30 03:47:45 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(units) - 1; i++) {
|
2013-11-22 14:37:17 +08:00
|
|
|
if (rate < kilo)
|
|
|
|
break;
|
|
|
|
if (((rate % kilo) != 0) && rate < 1000*kilo)
|
|
|
|
break;
|
|
|
|
rate /= kilo;
|
2004-06-29 04:42:59 +08:00
|
|
|
}
|
2015-05-30 03:47:45 +08:00
|
|
|
|
2013-11-22 14:37:17 +08:00
|
|
|
snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
|
2004-04-16 04:56:59 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
char *sprint_rate(__u64 rate, char *buf)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2004-06-29 04:42:59 +08:00
|
|
|
print_rate(buf, SPRINT_BSIZE-1, rate);
|
2004-04-16 04:56:59 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
char *sprint_ticks(__u32 ticks, char *buf)
|
2007-03-05 03:15:01 +08:00
|
|
|
{
|
|
|
|
return sprint_time(tc_core_tick2time(ticks), buf);
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
int get_size(unsigned int *size, const char *str)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
|
|
|
double sz;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
sz = strtod(str, &p);
|
|
|
|
if (p == str)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (*p) {
|
2016-03-22 02:48:36 +08:00
|
|
|
if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0)
|
2004-04-16 04:56:59 +08:00
|
|
|
sz *= 1024;
|
2016-03-22 02:48:36 +08:00
|
|
|
else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0)
|
2004-06-03 04:22:08 +08:00
|
|
|
sz *= 1024*1024*1024;
|
|
|
|
else if (strcasecmp(p, "gbit") == 0)
|
|
|
|
sz *= 1024*1024*1024/8;
|
2016-03-22 02:48:36 +08:00
|
|
|
else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0)
|
2004-04-16 04:56:59 +08:00
|
|
|
sz *= 1024*1024;
|
|
|
|
else if (strcasecmp(p, "mbit") == 0)
|
|
|
|
sz *= 1024*1024/8;
|
|
|
|
else if (strcasecmp(p, "kbit") == 0)
|
|
|
|
sz *= 1024/8;
|
|
|
|
else if (strcasecmp(p, "b") != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size = sz;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
int get_size_and_cell(unsigned int *size, int *cell_log, char *str)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2016-03-22 02:48:36 +08:00
|
|
|
char *slash = strchr(str, '/');
|
2004-04-16 04:56:59 +08:00
|
|
|
|
|
|
|
if (slash)
|
|
|
|
*slash = 0;
|
|
|
|
|
|
|
|
if (get_size(size, str))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (slash) {
|
|
|
|
int cell;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (get_integer(&cell, slash+1, 0))
|
|
|
|
return -1;
|
|
|
|
*slash = '/';
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
for (i = 0; i < 32; i++) {
|
2004-04-16 04:56:59 +08:00
|
|
|
if ((1<<i) == cell) {
|
|
|
|
*cell_log = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-21 08:39:34 +08:00
|
|
|
void print_devname(enum output_type type, int ifindex)
|
|
|
|
{
|
|
|
|
const char *ifname = ll_index_to_name(ifindex);
|
|
|
|
|
|
|
|
if (!is_json_context())
|
|
|
|
printf("dev ");
|
|
|
|
|
|
|
|
print_color_string(type, COLOR_IFNAME,
|
|
|
|
"dev", "%s ", ifname);
|
|
|
|
}
|
|
|
|
|
2018-11-16 06:36:36 +08:00
|
|
|
static void print_size(char *buf, int len, __u32 sz)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
|
|
|
double tmp = sz;
|
|
|
|
|
|
|
|
if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
|
|
|
|
snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
|
|
|
|
else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
|
|
|
|
snprintf(buf, len, "%gKb", rint(tmp/1024));
|
|
|
|
else
|
|
|
|
snprintf(buf, len, "%ub", sz);
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
char *sprint_size(__u32 size, char *buf)
|
2004-04-16 04:56:59 +08:00
|
|
|
{
|
2004-06-29 04:42:59 +08:00
|
|
|
print_size(buf, SPRINT_BSIZE-1, size);
|
2004-04-16 04:56:59 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-05-17 01:29:36 +08:00
|
|
|
static const char *action_n2a(int action)
|
2004-08-14 07:54:55 +08:00
|
|
|
{
|
2016-07-23 19:28:10 +08:00
|
|
|
static char buf[64];
|
|
|
|
|
2017-05-17 01:29:37 +08:00
|
|
|
if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
|
|
|
|
return "goto";
|
2017-10-22 22:48:10 +08:00
|
|
|
if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
|
|
|
|
return "jump";
|
2004-08-14 07:54:55 +08:00
|
|
|
switch (action) {
|
2016-07-23 19:28:10 +08:00
|
|
|
case TC_ACT_UNSPEC:
|
2004-08-14 07:54:55 +08:00
|
|
|
return "continue";
|
|
|
|
case TC_ACT_OK:
|
|
|
|
return "pass";
|
|
|
|
case TC_ACT_SHOT:
|
|
|
|
return "drop";
|
|
|
|
case TC_ACT_RECLASSIFY:
|
|
|
|
return "reclassify";
|
|
|
|
case TC_ACT_PIPE:
|
|
|
|
return "pipe";
|
|
|
|
case TC_ACT_STOLEN:
|
|
|
|
return "stolen";
|
2017-06-05 22:39:45 +08:00
|
|
|
case TC_ACT_TRAP:
|
|
|
|
return "trap";
|
2004-08-14 07:54:55 +08:00
|
|
|
default:
|
2016-07-23 19:28:10 +08:00
|
|
|
snprintf(buf, 64, "%d", action);
|
2004-08-14 07:54:55 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
2004-04-16 04:56:59 +08:00
|
|
|
|
2016-07-23 19:28:08 +08:00
|
|
|
/* Convert action branch name into numeric format.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* @arg - string to parse
|
|
|
|
* @result - pointer to output variable
|
|
|
|
* @allow_num - whether @arg may be in numeric format already
|
|
|
|
*
|
|
|
|
* In error case, returns -1 and does not touch @result. Otherwise returns 0.
|
|
|
|
*/
|
2018-01-12 19:20:21 +08:00
|
|
|
int action_a2n(char *arg, int *result, bool allow_num)
|
2004-08-14 07:54:55 +08:00
|
|
|
{
|
2016-07-23 19:28:08 +08:00
|
|
|
int n;
|
|
|
|
char dummy;
|
|
|
|
struct {
|
|
|
|
const char *a;
|
|
|
|
int n;
|
|
|
|
} a2n[] = {
|
|
|
|
{"continue", TC_ACT_UNSPEC},
|
|
|
|
{"drop", TC_ACT_SHOT},
|
|
|
|
{"shot", TC_ACT_SHOT},
|
|
|
|
{"pass", TC_ACT_OK},
|
|
|
|
{"ok", TC_ACT_OK},
|
|
|
|
{"reclassify", TC_ACT_RECLASSIFY},
|
|
|
|
{"pipe", TC_ACT_PIPE},
|
2017-05-17 01:29:37 +08:00
|
|
|
{"goto", TC_ACT_GOTO_CHAIN},
|
2017-10-22 22:48:10 +08:00
|
|
|
{"jump", TC_ACT_JUMP},
|
2017-06-05 22:39:45 +08:00
|
|
|
{"trap", TC_ACT_TRAP},
|
2016-07-23 19:28:08 +08:00
|
|
|
{ NULL },
|
|
|
|
}, *iter;
|
|
|
|
|
|
|
|
for (iter = a2n; iter->a; iter++) {
|
|
|
|
if (matches(arg, iter->a) != 0)
|
|
|
|
continue;
|
2018-01-12 19:20:21 +08:00
|
|
|
n = iter->n;
|
|
|
|
goto out_ok;
|
2004-08-14 07:54:55 +08:00
|
|
|
}
|
2016-07-23 19:28:08 +08:00
|
|
|
if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1)
|
|
|
|
return -1;
|
|
|
|
|
2018-01-12 19:20:21 +08:00
|
|
|
out_ok:
|
|
|
|
if (result)
|
|
|
|
*result = n;
|
2004-08-14 07:54:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-15 20:10:51 +08:00
|
|
|
static int __parse_action_control(int *argc_p, char ***argv_p, int *result_p,
|
|
|
|
bool allow_num, bool ignore_a2n_miss)
|
2017-05-17 01:29:36 +08:00
|
|
|
{
|
|
|
|
int argc = *argc_p;
|
|
|
|
char **argv = *argv_p;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (!argc)
|
|
|
|
return -1;
|
|
|
|
if (action_a2n(*argv, &result, allow_num) == -1) {
|
2017-06-15 20:10:51 +08:00
|
|
|
if (!ignore_a2n_miss)
|
|
|
|
fprintf(stderr, "Bad action type %s\n", *argv);
|
2017-05-17 01:29:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2017-05-17 01:29:37 +08:00
|
|
|
if (result == TC_ACT_GOTO_CHAIN) {
|
|
|
|
__u32 chain_index;
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
if (matches(*argv, "chain") != 0) {
|
|
|
|
fprintf(stderr, "\"chain index\" expected\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARG();
|
|
|
|
if (get_u32(&chain_index, *argv, 10) ||
|
|
|
|
chain_index > TC_ACT_EXT_VAL_MASK) {
|
|
|
|
fprintf(stderr, "Illegal \"chain index\"\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
result |= chain_index;
|
|
|
|
}
|
2017-10-22 22:48:10 +08:00
|
|
|
if (result == TC_ACT_JUMP) {
|
|
|
|
__u32 jump_cnt = 0;
|
|
|
|
|
|
|
|
NEXT_ARG();
|
|
|
|
if (get_u32(&jump_cnt, *argv, 10) ||
|
|
|
|
jump_cnt > TC_ACT_EXT_VAL_MASK) {
|
|
|
|
fprintf(stderr, "Invalid \"jump count\" (%s)\n", *argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
result |= jump_cnt;
|
|
|
|
}
|
2018-03-03 02:36:16 +08:00
|
|
|
NEXT_ARG_FWD();
|
2017-05-17 01:29:36 +08:00
|
|
|
*argc_p = argc;
|
|
|
|
*argv_p = argv;
|
|
|
|
*result_p = result;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-15 20:10:51 +08:00
|
|
|
/* Parse action control including possible options.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* @argc_p - pointer to argc to parse
|
|
|
|
* @argv_p - pointer to argv to parse
|
|
|
|
* @result_p - pointer to output variable
|
|
|
|
* @allow_num - whether action may be in numeric format already
|
|
|
|
*
|
|
|
|
* In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
|
|
|
|
*/
|
|
|
|
int parse_action_control(int *argc_p, char ***argv_p,
|
|
|
|
int *result_p, bool allow_num)
|
|
|
|
{
|
|
|
|
return __parse_action_control(argc_p, argv_p, result_p,
|
|
|
|
allow_num, false);
|
|
|
|
}
|
|
|
|
|
2017-05-17 01:29:36 +08:00
|
|
|
/* Parse action control including possible options.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* @argc_p - pointer to argc to parse
|
|
|
|
* @argv_p - pointer to argv to parse
|
|
|
|
* @result_p - pointer to output variable
|
|
|
|
* @allow_num - whether action may be in numeric format already
|
|
|
|
* @default_result - set as a result in case of parsing error
|
|
|
|
*
|
|
|
|
* In case there is an error during parsing, the default result is used.
|
|
|
|
*/
|
|
|
|
void parse_action_control_dflt(int *argc_p, char ***argv_p,
|
|
|
|
int *result_p, bool allow_num,
|
|
|
|
int default_result)
|
|
|
|
{
|
2017-06-15 20:10:51 +08:00
|
|
|
if (__parse_action_control(argc_p, argv_p, result_p, allow_num, true))
|
2017-05-17 01:29:36 +08:00
|
|
|
*result_p = default_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p,
|
|
|
|
int *result1_p, int *result2_p,
|
|
|
|
bool allow_num)
|
|
|
|
{
|
|
|
|
int argc = *argc_p;
|
|
|
|
char **argv = *argv_p;
|
2017-11-15 22:01:31 +08:00
|
|
|
int result1 = -1, result2;
|
2017-05-17 01:29:36 +08:00
|
|
|
int *result_p = &result1;
|
|
|
|
int ok = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (argc > 0) {
|
|
|
|
switch (ok) {
|
|
|
|
case 1:
|
|
|
|
if (strcmp(*argv, "/") != 0)
|
|
|
|
goto out;
|
|
|
|
result_p = &result2;
|
|
|
|
NEXT_ARG();
|
|
|
|
/* fall-through */
|
|
|
|
case 0: /* fall-through */
|
|
|
|
case 2:
|
|
|
|
ret = parse_action_control(&argc, &argv,
|
|
|
|
result_p, allow_num);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ok++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
*result1_p = result1;
|
|
|
|
if (ok == 2)
|
|
|
|
*result2_p = result2;
|
|
|
|
*argc_p = argc;
|
|
|
|
*argv_p = argv;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse action control with slash including possible options.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* @argc_p - pointer to argc to parse
|
|
|
|
* @argv_p - pointer to argv to parse
|
|
|
|
* @result1_p - pointer to the first (before slash) output variable
|
|
|
|
* @result2_p - pointer to the second (after slash) output variable
|
|
|
|
* @allow_num - whether action may be in numeric format already
|
|
|
|
*
|
|
|
|
* In error case, returns -1 and does not touch @result*. Otherwise returns 0.
|
|
|
|
*/
|
|
|
|
int parse_action_control_slash(int *argc_p, char ***argv_p,
|
|
|
|
int *result1_p, int *result2_p, bool allow_num)
|
|
|
|
{
|
2018-03-03 02:36:16 +08:00
|
|
|
int result1, result2, argc = *argc_p;
|
2017-05-17 01:29:36 +08:00
|
|
|
char **argv = *argv_p;
|
|
|
|
char *p = strchr(*argv, '/');
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return parse_action_control_slash_spaces(argc_p, argv_p,
|
|
|
|
result1_p, result2_p,
|
|
|
|
allow_num);
|
|
|
|
*p = 0;
|
|
|
|
if (action_a2n(*argv, &result1, allow_num)) {
|
2017-11-15 22:01:30 +08:00
|
|
|
*p = '/';
|
2017-05-17 01:29:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '/';
|
|
|
|
if (action_a2n(p + 1, &result2, allow_num))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*result1_p = result1;
|
|
|
|
*result2_p = result2;
|
2018-03-03 02:36:16 +08:00
|
|
|
NEXT_ARG_FWD();
|
|
|
|
*argc_p = argc;
|
|
|
|
*argv_p = argv;
|
2017-05-17 01:29:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_action_control(FILE *f, const char *prefix,
|
|
|
|
int action, const char *suffix)
|
|
|
|
{
|
2017-11-25 22:48:32 +08:00
|
|
|
print_string(PRINT_FP, NULL, "%s", prefix);
|
|
|
|
open_json_object("control_action");
|
|
|
|
print_string(PRINT_ANY, "type", "%s", action_n2a(action));
|
2017-05-17 01:29:37 +08:00
|
|
|
if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
|
2017-11-25 22:48:32 +08:00
|
|
|
print_uint(PRINT_ANY, "chain", " chain %u",
|
|
|
|
action & TC_ACT_EXT_VAL_MASK);
|
2017-10-22 22:48:10 +08:00
|
|
|
if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
|
2017-11-25 22:48:32 +08:00
|
|
|
print_uint(PRINT_ANY, "jump", " %u",
|
|
|
|
action & TC_ACT_EXT_VAL_MASK);
|
|
|
|
close_json_object();
|
|
|
|
print_string(PRINT_FP, NULL, "%s", suffix);
|
2017-05-17 01:29:36 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
int get_linklayer(unsigned int *val, const char *arg)
|
2008-04-10 05:01:01 +08:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (matches(arg, "ethernet") == 0)
|
|
|
|
res = LINKLAYER_ETHERNET;
|
|
|
|
else if (matches(arg, "atm") == 0)
|
|
|
|
res = LINKLAYER_ATM;
|
|
|
|
else if (matches(arg, "adsl") == 0)
|
|
|
|
res = LINKLAYER_ATM;
|
|
|
|
else
|
|
|
|
return -1; /* Indicate error */
|
|
|
|
|
|
|
|
*val = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-16 06:36:36 +08:00
|
|
|
static void print_linklayer(char *buf, int len, unsigned int linklayer)
|
2008-07-25 21:19:09 +08:00
|
|
|
{
|
|
|
|
switch (linklayer) {
|
|
|
|
case LINKLAYER_UNSPEC:
|
|
|
|
snprintf(buf, len, "%s", "unspec");
|
|
|
|
return;
|
|
|
|
case LINKLAYER_ETHERNET:
|
|
|
|
snprintf(buf, len, "%s", "ethernet");
|
|
|
|
return;
|
|
|
|
case LINKLAYER_ATM:
|
|
|
|
snprintf(buf, len, "%s", "atm");
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
snprintf(buf, len, "%s", "unknown");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
char *sprint_linklayer(unsigned int linklayer, char *buf)
|
2008-07-25 21:19:09 +08:00
|
|
|
{
|
|
|
|
print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:48:36 +08:00
|
|
|
void print_tm(FILE *f, const struct tcf_t *tm)
|
2004-08-14 07:54:55 +08:00
|
|
|
{
|
2005-03-15 02:44:54 +08:00
|
|
|
int hz = get_user_hz();
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2017-11-25 22:48:32 +08:00
|
|
|
if (tm->install != 0) {
|
|
|
|
print_uint(PRINT_JSON, "installed", NULL, tm->install);
|
|
|
|
print_uint(PRINT_FP, NULL, " installed %u sec",
|
|
|
|
(unsigned int)(tm->install/hz));
|
|
|
|
}
|
|
|
|
if (tm->lastuse != 0) {
|
|
|
|
print_uint(PRINT_JSON, "last_used", NULL, tm->lastuse);
|
|
|
|
print_uint(PRINT_FP, NULL, " used %u sec",
|
|
|
|
(unsigned int)(tm->lastuse/hz));
|
|
|
|
}
|
|
|
|
if (tm->expires != 0) {
|
|
|
|
print_uint(PRINT_JSON, "expires", NULL, tm->expires);
|
|
|
|
print_uint(PRINT_FP, NULL, " expires %u sec",
|
|
|
|
(unsigned int)(tm->expires/hz));
|
|
|
|
}
|
2004-08-14 07:54:55 +08:00
|
|
|
}
|
2004-12-08 07:52:52 +08:00
|
|
|
|
2018-10-02 15:27:18 +08:00
|
|
|
static void print_tcstats_basic_hw(struct rtattr **tbs, char *prefix)
|
|
|
|
{
|
|
|
|
struct gnet_stats_basic bs_hw;
|
|
|
|
|
|
|
|
if (!tbs[TCA_STATS_BASIC_HW])
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(&bs_hw, RTA_DATA(tbs[TCA_STATS_BASIC_HW]),
|
|
|
|
MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC_HW]), sizeof(bs_hw)));
|
|
|
|
|
|
|
|
if (bs_hw.bytes == 0 && bs_hw.packets == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (tbs[TCA_STATS_BASIC]) {
|
|
|
|
struct gnet_stats_basic bs;
|
|
|
|
|
|
|
|
memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]),
|
|
|
|
MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]),
|
|
|
|
sizeof(bs)));
|
|
|
|
|
|
|
|
if (bs.bytes >= bs_hw.bytes && bs.packets >= bs_hw.packets) {
|
|
|
|
print_string(PRINT_FP, NULL, "%s", _SL_);
|
|
|
|
print_string(PRINT_FP, NULL, "%s", prefix);
|
|
|
|
print_lluint(PRINT_ANY, "sw_bytes",
|
|
|
|
"Sent software %llu bytes",
|
|
|
|
bs.bytes - bs_hw.bytes);
|
|
|
|
print_uint(PRINT_ANY, "sw_packets", " %u pkt",
|
|
|
|
bs.packets - bs_hw.packets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print_string(PRINT_FP, NULL, "%s", _SL_);
|
|
|
|
print_string(PRINT_FP, NULL, "%s", prefix);
|
|
|
|
print_lluint(PRINT_ANY, "hw_bytes", "Sent hardware %llu bytes",
|
|
|
|
bs_hw.bytes);
|
|
|
|
print_uint(PRINT_ANY, "hw_packets", " %u pkt", bs_hw.packets);
|
|
|
|
}
|
|
|
|
|
2004-12-08 07:52:52 +08:00
|
|
|
void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
|
|
|
|
{
|
|
|
|
SPRINT_BUF(b1);
|
2005-01-19 06:11:58 +08:00
|
|
|
struct rtattr *tbs[TCA_STATS_MAX + 1];
|
2004-12-08 07:52:52 +08:00
|
|
|
|
2005-01-19 06:11:58 +08:00
|
|
|
parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
|
2004-12-08 07:52:52 +08:00
|
|
|
|
|
|
|
if (tbs[TCA_STATS_BASIC]) {
|
|
|
|
struct gnet_stats_basic bs = {0};
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-12-08 07:52:52 +08:00
|
|
|
memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
|
2017-11-25 22:48:26 +08:00
|
|
|
print_string(PRINT_FP, NULL, "%s", prefix);
|
|
|
|
print_lluint(PRINT_ANY, "bytes", "Sent %llu bytes", bs.bytes);
|
|
|
|
print_uint(PRINT_ANY, "packets", " %u pkt", bs.packets);
|
2004-12-08 07:52:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tbs[TCA_STATS_QUEUE]) {
|
|
|
|
struct gnet_stats_queue q = {0};
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-12-08 07:52:52 +08:00
|
|
|
memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
|
2017-11-25 22:48:26 +08:00
|
|
|
print_uint(PRINT_ANY, "drops", " (dropped %u", q.drops);
|
|
|
|
print_uint(PRINT_ANY, "overlimits", ", overlimits %u",
|
|
|
|
q.overlimits);
|
|
|
|
print_uint(PRINT_ANY, "requeues", " requeues %u) ", q.requeues);
|
2004-12-08 07:52:52 +08:00
|
|
|
}
|
2006-12-06 02:10:22 +08:00
|
|
|
|
2018-10-02 15:27:18 +08:00
|
|
|
if (tbs[TCA_STATS_BASIC_HW])
|
|
|
|
print_tcstats_basic_hw(tbs, prefix);
|
|
|
|
|
2013-09-17 19:19:03 +08:00
|
|
|
if (tbs[TCA_STATS_RATE_EST64]) {
|
|
|
|
struct gnet_stats_rate_est64 re = {0};
|
|
|
|
|
|
|
|
memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
|
|
|
|
MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
|
|
|
|
sizeof(re)));
|
2017-11-25 22:48:26 +08:00
|
|
|
print_string(PRINT_FP, NULL, "\n%s", prefix);
|
|
|
|
print_lluint(PRINT_JSON, "rate", NULL, re.bps);
|
|
|
|
print_string(PRINT_FP, NULL, "rate %s",
|
|
|
|
sprint_rate(re.bps, b1));
|
|
|
|
print_lluint(PRINT_ANY, "pps", " %llupps", re.pps);
|
2013-09-17 19:19:03 +08:00
|
|
|
} else if (tbs[TCA_STATS_RATE_EST]) {
|
2004-12-08 07:52:52 +08:00
|
|
|
struct gnet_stats_rate_est re = {0};
|
2013-09-17 19:19:03 +08:00
|
|
|
|
|
|
|
memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
|
|
|
|
MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
|
2017-11-25 22:48:26 +08:00
|
|
|
print_string(PRINT_FP, NULL, "\n%s", prefix);
|
|
|
|
print_uint(PRINT_JSON, "rate", NULL, re.bps);
|
|
|
|
print_string(PRINT_FP, NULL, "rate %s",
|
|
|
|
sprint_rate(re.bps, b1));
|
|
|
|
print_uint(PRINT_ANY, "pps", " %upps", re.pps);
|
2004-12-08 07:52:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tbs[TCA_STATS_QUEUE]) {
|
|
|
|
struct gnet_stats_queue q = {0};
|
2016-03-22 02:48:36 +08:00
|
|
|
|
2004-12-08 07:52:52 +08:00
|
|
|
memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
|
|
|
|
if (!tbs[TCA_STATS_RATE_EST])
|
2018-11-30 21:57:02 +08:00
|
|
|
print_string(PRINT_FP, NULL, "\n", "");
|
2017-11-25 22:48:26 +08:00
|
|
|
print_uint(PRINT_JSON, "backlog", NULL, q.backlog);
|
2018-11-30 21:57:02 +08:00
|
|
|
print_string(PRINT_FP, NULL, "%s", prefix);
|
2017-11-25 22:48:26 +08:00
|
|
|
print_string(PRINT_FP, NULL, "backlog %s",
|
|
|
|
sprint_size(q.backlog, b1));
|
|
|
|
print_uint(PRINT_ANY, "qlen", " %up", q.qlen);
|
2018-01-27 17:19:04 +08:00
|
|
|
print_uint(PRINT_FP, NULL, " requeues %u", q.requeues);
|
2004-12-08 07:52:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (xstats)
|
|
|
|
*xstats = tbs[TCA_STATS_APP] ? : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
|
|
|
|
{
|
|
|
|
SPRINT_BUF(b1);
|
|
|
|
|
|
|
|
if (tb[TCA_STATS2]) {
|
|
|
|
print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
|
2019-01-25 04:30:14 +08:00
|
|
|
if (xstats && !*xstats)
|
2004-12-08 07:52:52 +08:00
|
|
|
goto compat_xstats;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* backward compatibility */
|
|
|
|
if (tb[TCA_STATS]) {
|
2016-07-18 22:48:42 +08:00
|
|
|
struct tc_stats st = {};
|
2004-12-08 07:52:52 +08:00
|
|
|
|
|
|
|
/* handle case where kernel returns more/less than we know about */
|
|
|
|
memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
|
|
|
|
|
|
|
|
fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
|
2006-12-06 02:10:22 +08:00
|
|
|
prefix, (unsigned long long)st.bytes, st.packets, st.drops,
|
2004-12-08 07:52:52 +08:00
|
|
|
st.overlimits);
|
|
|
|
|
|
|
|
if (st.bps || st.pps || st.qlen || st.backlog) {
|
|
|
|
fprintf(fp, "\n%s", prefix);
|
|
|
|
if (st.bps || st.pps) {
|
|
|
|
fprintf(fp, "rate ");
|
|
|
|
if (st.bps)
|
|
|
|
fprintf(fp, "%s ", sprint_rate(st.bps, b1));
|
|
|
|
if (st.pps)
|
|
|
|
fprintf(fp, "%upps ", st.pps);
|
|
|
|
}
|
|
|
|
if (st.qlen || st.backlog) {
|
|
|
|
fprintf(fp, "backlog ");
|
|
|
|
if (st.backlog)
|
|
|
|
fprintf(fp, "%s ", sprint_size(st.backlog, b1));
|
|
|
|
if (st.qlen)
|
|
|
|
fprintf(fp, "%up ", st.qlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
compat_xstats:
|
|
|
|
if (tb[TCA_XSTATS] && xstats)
|
|
|
|
*xstats = tb[TCA_XSTATS];
|
|
|
|
}
|
2019-07-11 16:14:26 +08:00
|
|
|
|
2019-11-14 20:44:37 +08:00
|
|
|
static void print_masked_type(__u32 type_max,
|
|
|
|
__u32 (*rta_getattr_type)(const struct rtattr *),
|
|
|
|
const char *name, struct rtattr *attr,
|
|
|
|
struct rtattr *mask_attr)
|
|
|
|
{
|
|
|
|
SPRINT_BUF(namefrm);
|
|
|
|
__u32 value, mask;
|
|
|
|
SPRINT_BUF(out);
|
|
|
|
size_t done;
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
value = rta_getattr_type(attr);
|
|
|
|
mask = mask_attr ? rta_getattr_type(mask_attr) : type_max;
|
|
|
|
|
|
|
|
if (is_json_context()) {
|
|
|
|
sprintf(namefrm, "\n %s %%u", name);
|
|
|
|
print_hu(PRINT_ANY, name, namefrm,
|
|
|
|
rta_getattr_type(attr));
|
|
|
|
if (mask != type_max) {
|
|
|
|
char mask_name[SPRINT_BSIZE-6];
|
|
|
|
|
|
|
|
sprintf(mask_name, "%s_mask", name);
|
|
|
|
print_string(PRINT_FP, NULL, "%s ", _SL_);
|
|
|
|
sprintf(namefrm, "%s %%u", mask_name);
|
|
|
|
print_hu(PRINT_ANY, mask_name, namefrm, mask);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
done = sprintf(out, "%u", value);
|
|
|
|
if (mask != type_max)
|
|
|
|
sprintf(out + done, "/0x%x", mask);
|
|
|
|
|
|
|
|
print_string(PRINT_FP, NULL, "%s ", _SL_);
|
|
|
|
sprintf(namefrm, "%s %%s", name);
|
|
|
|
print_string(PRINT_ANY, name, namefrm, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 16:14:26 +08:00
|
|
|
void print_masked_u32(const char *name, struct rtattr *attr,
|
|
|
|
struct rtattr *mask_attr)
|
|
|
|
{
|
|
|
|
__u32 value, mask;
|
|
|
|
SPRINT_BUF(namefrm);
|
|
|
|
SPRINT_BUF(out);
|
|
|
|
size_t done;
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
value = rta_getattr_u32(attr);
|
|
|
|
mask = mask_attr ? rta_getattr_u32(mask_attr) : UINT32_MAX;
|
|
|
|
|
|
|
|
done = sprintf(out, "%u", value);
|
|
|
|
if (mask != UINT32_MAX)
|
|
|
|
sprintf(out + done, "/0x%x", mask);
|
|
|
|
|
|
|
|
sprintf(namefrm, " %s %%s", name);
|
|
|
|
print_string(PRINT_ANY, name, namefrm, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_masked_u16(const char *name, struct rtattr *attr,
|
|
|
|
struct rtattr *mask_attr)
|
|
|
|
{
|
|
|
|
__u16 value, mask;
|
|
|
|
SPRINT_BUF(namefrm);
|
|
|
|
SPRINT_BUF(out);
|
|
|
|
size_t done;
|
|
|
|
|
|
|
|
if (!attr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
value = rta_getattr_u16(attr);
|
|
|
|
mask = mask_attr ? rta_getattr_u16(mask_attr) : UINT16_MAX;
|
|
|
|
|
|
|
|
done = sprintf(out, "%u", value);
|
|
|
|
if (mask != UINT16_MAX)
|
|
|
|
sprintf(out + done, "/0x%x", mask);
|
|
|
|
|
|
|
|
sprintf(namefrm, " %s %%s", name);
|
|
|
|
print_string(PRINT_ANY, name, namefrm, out);
|
|
|
|
}
|
2019-11-14 20:44:37 +08:00
|
|
|
|
|
|
|
static __u32 __rta_getattr_u8_u32(const struct rtattr *attr)
|
|
|
|
{
|
|
|
|
return rta_getattr_u8(attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_masked_u8(const char *name, struct rtattr *attr,
|
|
|
|
struct rtattr *mask_attr)
|
|
|
|
{
|
|
|
|
print_masked_type(UINT8_MAX, __rta_getattr_u8_u32, name, attr,
|
|
|
|
mask_attr);
|
|
|
|
}
|