diff options
author | Leo Ruan <tingquan.ruan@cn.bosch.com> | 2018-08-04 20:07:42 +0200 |
---|---|---|
committer | Marc Kleine-Budde <mkl@pengutronix.de> | 2018-08-08 12:28:57 +0200 |
commit | 9679a3f50a14b2c864889c6da56a7cdbaa7b1f9b (patch) | |
tree | 0a7336eb48f9abc089604304acac33d8836467b7 | |
parent | 6f13cefafe48f5d7a33ccbb45ce15b1417e1f4d6 (diff) | |
download | libsocketcan-9679a3f50a14b2c864889c6da56a7cdbaa7b1f9b.tar.gz libsocketcan-9679a3f50a14b2c864889c6da56a7cdbaa7b1f9b.tar.xz |
Add interface to retrieve link statistics
This commit adds an interface to get the statistics (64-bits) from
CAN interface. See more information from /usr/include/linux/if_link.h
where defines the struct rtnl_link_stats64.
Compare to the rtnl_link_stats, rtnl_link_stats64 was introduced
since linux kernel 2.6. After that the rtnl_link_stats is synchronous
with struct rtnl_link_stats64 by truncating each member from 64 bits
to 32 bits. Actually, the struct rtnl_link_stats is kept for
compatibility.
Signed-off-by: Leo Ruan <tingquan.ruan@cn.bosch.com>
Signed-off-by: Mark Jonas <mark.jonas@de.bosch.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
-rw-r--r-- | include/libsocketcan.h | 3 | ||||
-rw-r--r-- | src/libsocketcan.c | 38 |
2 files changed, 41 insertions, 0 deletions
diff --git a/include/libsocketcan.h b/include/libsocketcan.h index dc52053..1603a7b 100644 --- a/include/libsocketcan.h +++ b/include/libsocketcan.h @@ -28,6 +28,8 @@ #include <can_netlink.h> +struct rtnl_link_stats64; /* from <linux/if_link.h> */ + #ifdef __cplusplus extern "C" { #endif @@ -50,6 +52,7 @@ int can_get_clock(const char *name, struct can_clock *clock); int can_get_bittiming_const(const char *name, struct can_bittiming_const *btc); int can_get_berr_counter(const char *name, struct can_berr_counter *bc); int can_get_device_stats(const char *name, struct can_device_stats *cds); +int can_get_link_stats(const char *name, struct rtnl_link_stats64 *rls); #ifdef __cplusplus } diff --git a/src/libsocketcan.c b/src/libsocketcan.c index a44728d..a1a4717 100644 --- a/src/libsocketcan.c +++ b/src/libsocketcan.c @@ -30,6 +30,7 @@ #include <fcntl.h> #include <net/if.h> +#include <linux/if_link.h> #include <linux/rtnetlink.h> #include <linux/netlink.h> @@ -54,6 +55,7 @@ #define GET_BITTIMING_CONST 6 #define GET_BERR_COUNTER 7 #define GET_XSTATS 8 +#define GET_LINK_STATS 9 struct get_req { struct nlmsghdr n; @@ -408,6 +410,16 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res) else continue; + if (acquire == GET_LINK_STATS) { + if (!tb[IFLA_STATS64]) { + fprintf(stderr, "no link statistics (64-bit) found\n"); + } else { + memcpy(res, RTA_DATA(tb[IFLA_STATS64]), sizeof(struct rtnl_link_stats64)); + ret = 0; + } + continue; + } + if (tb[IFLA_LINKINFO]) parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); @@ -1157,3 +1169,29 @@ int can_get_device_stats(const char *name, struct can_device_stats *cds) { return get_link(name, GET_XSTATS, cds); } + +/** + * @ingroup extern + * can_get_link_statistics - get RX/TX statistics (64 bits version) + * + * @param name name of the can device. This is the netdev name, as ip link shows + * in your system. usually it contains prefix "can" and the number of the can + * line. e.g. "can0" + * @param rls pointer to the rtnl_link_stats64 struct which is from if_link.h. + * + * This one gets the current rtnl_link_stats64. Compares to the rtnl_link_stats, + * The rtnl_link_stats64 is introduced since linux kernel 2.6. After that the + * rtnl_link_stats is synchronous with struct rtnl_link_stats64 by truncating + * each member from 64 bits to 32 bits. actually, the struct rtnl_link_stats + * is kept for compatibility. + * + * Please see struct rtnl_link_stats64 (/usr/include/linux/if_link.h) for more + * information. + * + * @return 0 if success + * @return -1 if failed + */ +int can_get_link_stats(const char *name, struct rtnl_link_stats64 *rls) +{ + return get_link(name, GET_LINK_STATS, rls); +} |