]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip: Publish functions for stats formatting
authorPetr Machata <petrm@nvidia.com>
Fri, 22 Apr 2022 08:30:51 +0000 (10:30 +0200)
committerDavid Ahern <dsahern@kernel.org>
Thu, 28 Apr 2022 02:12:42 +0000 (20:12 -0600)
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>
ip/ip_common.h
ip/ipaddress.c

index ea04c8ff3dea919c197b31f353be42f5793b0129..51a7edc71e150013f022f2a31111088c981badef 100644 (file)
@@ -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_ */
index a80996efdc28753da3cc80e7a90e39941a67b926..17341d287629fbd6c9fcb3f6a9a89635fc9dc3b1 100644 (file)
@@ -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);