From: Leo Ruan <tingquan.ruan@xxxxxxxxxxxx> 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@xxxxxxxxxxxx> Signed-off-by: Mark Jonas <mark.jonas@xxxxxxxxxxxx> --- include/libsocketcan.h | 3 +++ src/libsocketcan.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) 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 04e6647..c4b95d9 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; @@ -404,6 +406,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]); @@ -1153,3 +1165,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); +} -- 2.7.4 -- To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html