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 +struct rtnl_link_stats64; /* from */ + #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 #include +#include #include #include @@ -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); +}