aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetr Machata <petrm@nvidia.com>2022-04-22 10:30:51 +0200
committerDavid Ahern <dsahern@kernel.org>2022-04-27 20:12:42 -0600
commit5520cf1603bacdc9fdafa119e8bf4a35ad57fefc (patch)
tree94874e23ac2277362d7a05c867230548bccb8912
parenta463d6b19107373748f6f930170b4c106bee0c42 (diff)
downloadiproute2-5520cf1603bacdc9fdafa119e8bf4a35ad57fefc.tar.gz
ip: Publish functions for stats formatting
Formatting struct rtnl_link_stats64 will be useful outside of iplink.c as well. Extract from __print_link_stats() a new function, print_stats64(), make it non-static and publish in the header file. Additionally, publish the helper size_columns(), which will be useful for formatting the new struct rtnl_hw_stats64. Signed-off-by: Petr Machata <petrm@nvidia.com> Reviewed-by: Ido Schimmel <idosch@nvidia.com> Signed-off-by: David Ahern <dsahern@kernel.org>
-rw-r--r--ip/ip_common.h3
-rw-r--r--ip/ipaddress.c33
2 files changed, 25 insertions, 11 deletions
diff --git a/ip/ip_common.h b/ip/ip_common.h
index ea04c8ff3..51a7edc71 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -171,4 +171,7 @@ void print_rta_ifidx(FILE *fp, __u32 ifidx, const char *prefix);
void __print_rta_gateway(FILE *fp, unsigned char family, const char *gateway);
void print_rta_gateway(FILE *fp, unsigned char family,
const struct rtattr *rta);
+void size_columns(unsigned int cols[], unsigned int n, ...);
+void print_stats64(FILE *fp, struct rtnl_link_stats64 *s,
+ const struct rtattr *carrier_changes, const char *what);
#endif /* _IP_COMMON_H_ */
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index a80996efd..17341d287 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -546,7 +546,7 @@ static void print_vfinfo(FILE *fp, struct ifinfomsg *ifi, struct rtattr *vfinfo)
print_vf_stats64(fp, vf[IFLA_VF_STATS]);
}
-static void size_columns(unsigned int cols[], unsigned int n, ...)
+void size_columns(unsigned int cols[], unsigned int n, ...)
{
unsigned int i, len;
uint64_t val, powi;
@@ -680,10 +680,10 @@ static void print_vf_stats64(FILE *fp, struct rtattr *vfstats)
}
}
-static void __print_link_stats(FILE *fp, struct rtattr *tb[])
+void print_stats64(FILE *fp, struct rtnl_link_stats64 *s,
+ const struct rtattr *carrier_changes,
+ const char *what)
{
- const struct rtattr *carrier_changes = tb[IFLA_CARRIER_CHANGES];
- struct rtnl_link_stats64 _s, *s = &_s;
unsigned int cols[] = {
strlen("*X errors:"),
strlen("packets"),
@@ -693,14 +693,10 @@ static void __print_link_stats(FILE *fp, struct rtattr *tb[])
strlen("overrun"),
strlen("compressed"),
};
- int ret;
-
- ret = get_rtnl_link_stats_rta(s, tb);
- if (ret < 0)
- return;
if (is_json_context()) {
- open_json_object((ret == sizeof(*s)) ? "stats64" : "stats");
+ if (what)
+ open_json_object(what);
/* RX stats */
open_json_object("rx");
@@ -771,7 +767,8 @@ static void __print_link_stats(FILE *fp, struct rtattr *tb[])
}
close_json_object();
- close_json_object();
+ if (what)
+ close_json_object();
} else {
size_columns(cols, ARRAY_SIZE(cols),
s->rx_bytes, s->rx_packets, s->rx_errors,
@@ -870,6 +867,20 @@ static void __print_link_stats(FILE *fp, struct rtattr *tb[])
}
}
+static void __print_link_stats(FILE *fp, struct rtattr *tb[])
+{
+ const struct rtattr *carrier_changes = tb[IFLA_CARRIER_CHANGES];
+ struct rtnl_link_stats64 _s, *s = &_s;
+ int ret;
+
+ ret = get_rtnl_link_stats_rta(s, tb);
+ if (ret < 0)
+ return;
+
+ print_stats64(fp, s, carrier_changes,
+ (ret == sizeof(*s)) ? "stats64" : "stats");
+}
+
static void print_link_stats(FILE *fp, struct nlmsghdr *n)
{
struct ifinfomsg *ifi = NLMSG_DATA(n);