tunnel: Split statistic getting and printing

This is first step to move tunnel code to use rtnl dump interface
instead of /proc/net/dev read.

Make tnl_print_stats() to accept @struct rtnl_link_stats64 parameter,
introduce tnl_get_stats() that will parse line from /proc/net/dev into
@struct rtnl_link_stats64.

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Serhey Popovych 2018-02-07 08:30:53 +02:00 committed by David Ahern
parent 9a7bd5442b
commit affb361785
4 changed files with 51 additions and 25 deletions

View File

@ -390,8 +390,12 @@ static int do_tunnels_list(struct ip6_tnl_parm2 *p)
if (!ip6_tnl_parm_match(p, &p1))
continue;
print_tunnel(&p1);
if (show_stats)
tnl_print_stats(ptr);
if (show_stats) {
struct rtnl_link_stats64 s;
if (!tnl_get_stats(ptr, &s))
tnl_print_stats(&s);
}
printf("\n");
}
err = 0;

View File

@ -425,8 +425,12 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
(p->i_key && p1.i_key != p->i_key))
continue;
print_tunnel(&p1);
if (show_stats)
tnl_print_stats(ptr);
if (show_stats) {
struct rtnl_link_stats64 s;
if (!tnl_get_stats(ptr, &s))
tnl_print_stats(&s);
}
printf("\n");
}
err = 0;

View File

@ -307,30 +307,45 @@ void tnl_print_endpoint(const char *name, const struct rtattr *rta, int family)
}
}
/* tnl_print_stats - print tunnel statistics
*
* @buf - tunnel interface's line in /proc/net/dev,
* starting past the interface name and following colon
*/
void tnl_print_stats(const char *buf)
int tnl_get_stats(const char *buf, struct rtnl_link_stats64 *s)
{
unsigned long rx_bytes, rx_packets, rx_errs, rx_drops,
rx_fifo, rx_frame,
/* rx */
__u64 *rx_bytes = &s->rx_bytes;
__u64 *rx_packets = &s->rx_packets;
__u64 *rx_errs = &s->rx_errors;
__u64 *rx_drops = &s->rx_dropped;
__u64 *rx_fifo = &s->rx_fifo_errors;
__u64 *rx_frame = &s->rx_frame_errors;
__u64 *rx_multi = &s->multicast;
/* tx */
__u64 *tx_bytes = &s->tx_bytes;
__u64 *tx_packets = &s->tx_packets;
__u64 *tx_errs = &s->tx_errors;
__u64 *tx_drops = &s->tx_dropped;
__u64 *tx_fifo = &s->tx_fifo_errors;
__u64 *tx_carrier = &s->tx_carrier_errors;
__u64 *tx_colls = &s->collisions;
if (sscanf(buf,
"%llu%llu%llu%llu%llu%llu%llu%*d%llu%llu%llu%llu%llu%llu%llu",
rx_bytes, rx_packets, rx_errs, rx_drops,
rx_fifo, rx_frame, rx_multi,
tx_bytes, tx_packets, tx_errs, tx_drops,
tx_fifo, tx_colls, tx_carrier, rx_multi;
tx_fifo, tx_colls, tx_carrier) != 14)
return -1;
if (sscanf(buf, "%lu%lu%lu%lu%lu%lu%lu%*d%lu%lu%lu%lu%lu%lu%lu",
&rx_bytes, &rx_packets, &rx_errs, &rx_drops,
&rx_fifo, &rx_frame, &rx_multi,
&tx_bytes, &tx_packets, &tx_errs, &tx_drops,
&tx_fifo, &tx_colls, &tx_carrier) != 14)
return;
return 0;
}
void tnl_print_stats(const struct rtnl_link_stats64 *s)
{
printf("%s", _SL_);
printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_);
printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-8ld%s",
rx_packets, rx_bytes, rx_errs, rx_frame, rx_fifo, rx_multi, _SL_);
printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-8lld%s",
s->rx_packets, s->rx_bytes, s->rx_errors, s->rx_frame_errors,
s->rx_fifo_errors, s->multicast, _SL_);
printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_);
printf(" %-10ld %-12ld %-6ld %-8ld %-8ld %-6ld",
tx_packets, tx_bytes, tx_errs, tx_colls, tx_carrier, tx_drops);
printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-6lld",
s->tx_packets, s->tx_bytes, s->tx_errors, s->collisions,
s->tx_carrier_errors, s->tx_dropped);
}

View File

@ -24,6 +24,7 @@
#include <linux/types.h>
struct rtattr;
struct rtnl_link_stats64;
const char *tnl_strproto(__u8 proto);
@ -39,6 +40,8 @@ void tnl_print_encap(struct rtattr *tb[],
int encap_sport, int encap_dport);
void tnl_print_endpoint(const char *name,
const struct rtattr *rta, int family);
void tnl_print_stats(const char *buf);
void tnl_print_stats(const struct rtnl_link_stats64 *s);
int tnl_get_stats(const char *buf, struct rtnl_link_stats64 *s);
#endif