diff --git a/libethernet/ethernet.c b/libethernet/ethernet.c index 67aeeb7d5315618598cbfeb87af51af32ff22946..3be205fd013b21cdc2a6cf8560de339a845f4f4d 100644 --- a/libethernet/ethernet.c +++ b/libethernet/ethernet.c @@ -38,6 +38,23 @@ #include "ethernet.h" + +#if defined(IOPSYS_BROADCOM) +extern const struct eth_ops bcm_eth_ops; +#elif defined(IOPSYS_TEST) +extern const struct eth_ops test_eth_ops; +#elif defined(IOPSYS_ECONET) +extern const struct eth_ops econet_gen_eth_ops; +extern const struct eth_ops econet_nas_wan_eth_ops; +extern const struct eth_ops econet_ae_wan_eth_ops; +#elif defined(IPQ95XX) +extern const struct eth_ops ipq95xx_eth_ops; +#elif defined(IOPSYS_LINUX) +extern const struct eth_ops linux_eth_ops; +#else +extern const struct eth_ops ethsw_ops; +#endif + const struct eth_ops *eth_ops[] = { #if defined(IOPSYS_BROADCOM) &bcm_eth_ops, diff --git a/libethernet/ethernet.h b/libethernet/ethernet.h index 96303a5055486151c38aec6877ed126425cf4c37..36efa789fb3faad7ad08e8612970f052d92e6a26 100644 --- a/libethernet/ethernet.h +++ b/libethernet/ethernet.h @@ -26,6 +26,7 @@ #include <stdbool.h> #include <linux/types.h> + #ifdef __cplusplus extern "C" { #endif @@ -35,23 +36,6 @@ extern "C" { #define libethernet_info(...) pr_info("libethernet: " __VA_ARGS__) #define libethernet_dbg(...) pr_debug("libethernet: " __VA_ARGS__) -#if defined(IOPSYS_BROADCOM) -extern const struct eth_ops bcm_eth_ops; -#elif defined(IOPSYS_TEST) -extern const struct eth_ops test_eth_ops; -#elif defined(IOPSYS_ECONET) -extern const struct eth_ops econet_gen_eth_ops; -extern const struct eth_ops econet_nas_wan_eth_ops; -extern const struct eth_ops econet_ae_wan_eth_ops; -#elif defined(IPQ95XX) -extern const struct eth_ops ipq95xx_eth_ops; -#elif defined(IOPSYS_LINUX) -extern const struct eth_ops linux_eth_ops; -#else -extern const struct eth_ops ethsw_ops; -#endif - - /* enum eth_duplex - duplex modes */ enum eth_duplex { AUTO_DUPLEX, diff --git a/libethernet/linux/linux_eth.c b/libethernet/linux/linux_eth.c index 918765d1bad94ad45a711de4325eff0fd8c1e1c3..a98970073283d16b307e3ccf5d748519b1c3c2f0 100644 --- a/libethernet/linux/linux_eth.c +++ b/libethernet/linux/linux_eth.c @@ -36,19 +36,6 @@ #include <unistd.h> #include <stdlib.h> #include <linux/ethtool.h> -#include <netlink/genl/genl.h> -#include <netlink/cache.h> -#include <netlink/utils.h> -#include <netlink/object.h> -#include <netlink/genl/genl.h> -#include <netlink/genl/ctrl.h> -#include <netlink/netlink.h> -#include <netlink/route/rtnl.h> -#include <netlink/route/link.h> -#include <netlink/genl/ctrl.h> -#include <netlink/netlink.h> -#include <netlink/route/rtnl.h> -#include <netlink/route/link.h> #include <linux/sockios.h> #include "easy.h" #include "if_utils.h" @@ -56,74 +43,78 @@ #include "linux_eth.h" -/************************************************************ - * Open a netlink socket connectivity with NETLINK_ROUTE - * to read the stats using designated API. - ************************************************************ - */ -static int if_openlnk(const char *ifname, struct nl_sock **s, - struct rtnl_link **l) -{ - struct rtnl_link *link; - struct nl_sock *sk; - int ret = 0; - - sk = nl_socket_alloc(); - if (sk == NULL) { - ret = -errno; - return ret; - } - - if (nl_connect(sk, NETLINK_ROUTE) < 0) { - syslog(LOG_ERR, "%s(%d) nl socket connection failed.", - __FUNCTION__, __LINE__); - ret = -1; - goto out; - } - - if (rtnl_link_get_kernel(sk, 0, ifname, &link) < 0) { - ret = -1; - goto out; - } - - *l = link; - *s = sk; - return 0; - -out: - nl_socket_free(sk); - return ret; -} - -/************************************************************ - * Close the socket and free the allocated nl objects. - ************************************************************ - */ - -static int if_closelnk(struct nl_sock *s, struct rtnl_link *l) -{ - rtnl_link_put(l); - nl_socket_free(s); - - return 0; -} +typedef enum { + tx_packets = 0, + tx_bytes, + rx_packets, + rx_bytes, + TxDrop, + TxCrcErr, + TxUnicast, + TxMulticast, + TxBroadcast, + TxCollision, + TxSingleCollision, + TxMultipleCollision, + TxDeferred, + TxLateCollision, + TxExcessiveCollistion, + TxPause, + TxPktSz64, + TxPktSz65To127, + TxPktSz128To255, + TxPktSz256To511, + TxPktSz512To1023, + Tx1024ToMax, + TxBytes, + RxDrop, + RxFiltering, + RxUnicast, + RxMulticast, + RxBroadcast, + RxAlignErr, + RxCrcErr, + RxUnderSizeErr, + RxFragErr, + RxOverSzErr, + RxJabberErr, + RxPause, + RxPktSz64, + RxPktSz65To127, + RxPktSz128To255, + RxPktSz256To511, + RxPktSz512To1023, + RxPktSz1024ToMax, + RxBytes, + RxCtrlDrop, + RxIngressDrop, + RxArlDrop, + STATS_END, +} idx; + +struct ethnic_stats { + uint32_t cmd; + uint32_t n_stats; + uint64_t data[STATS_END]; +} stat; + +static struct eth_stats ifstats; static int get_ifstats(const char *ifname, struct eth_stats *s) { - struct rtnl_link *link; - struct nl_sock *sk; - int ret = 0; + struct if_stats easy_ifstat; if (!s) return -1; - ret = if_openlnk(ifname, &sk, &link); - if (ret) - return -1; + memset(&easy_ifstat, 0, sizeof(struct if_stats)); + if (if_getstats(ifname, &easy_ifstat) < 0) { + return -1; + } - s->tx_bytes = rtnl_link_get_stat(link, RTNL_LINK_TX_BYTES); - s->tx_packets = rtnl_link_get_stat(link, RTNL_LINK_TX_PACKETS); - s->tx_errors = rtnl_link_get_stat(link, RTNL_LINK_TX_ERRORS); + s->tx_bytes = easy_ifstat.tx_bytes; + s->tx_packets = easy_ifstat.tx_packets; + s->tx_errors = easy_ifstat.tx_errors; /* Some counters set to zero, and will be populated cross referring * the rmon stats structure. */ @@ -131,17 +122,15 @@ static int get_ifstats(const char *ifname, struct eth_stats *s) s->tx_mcast_packets = 0; s->tx_bcast_packets = 0; s->tx_discard_packets = 0; - s->rx_bytes = rtnl_link_get_stat(link, RTNL_LINK_RX_BYTES); + s->rx_bytes = easy_ifstat.rx_bytes; s->rx_packets = 0; - s->rx_errors = rtnl_link_get_stat(link, RTNL_LINK_RX_ERRORS); + s->rx_errors = easy_ifstat.rx_errors; s->rx_ucast_packets = 0; - s->rx_mcast_packets = rtnl_link_get_stat(link, RTNL_LINK_MULTICAST); + s->rx_mcast_packets = 0; s->rx_bcast_packets = 0; s->rx_discard_packets = 0; s->rx_unknown_packets = 0; - if_closelnk(sk, link); - return 0; } @@ -187,7 +176,7 @@ int linux_eth_get_stats(const char *ifname, struct eth_stats *s) s->tx_packets = ifstats.tx_packets; s->tx_errors = ifstats.tx_errors; s->tx_ucast_packets = stat.data[TxUnicast]; - s->tx_mcast_packets = ifstats.tx_mcast_packets; + s->tx_mcast_packets = stat.data[TxMulticast]; s->tx_bcast_packets = stat.data[TxBroadcast]; s->tx_discard_packets = stat.data[TxDrop]; s->rx_bytes = ifstats.rx_bytes; diff --git a/libethernet/linux/linux_eth.h b/libethernet/linux/linux_eth.h index e625e3d193f439e31f0a7544963b1a38fd86f9e6..68befa703799c8fb3c136bcabc94a9b781559431 100644 --- a/libethernet/linux/linux_eth.h +++ b/libethernet/linux/linux_eth.h @@ -1,67 +1,10 @@ -#ifndef LINUX -#define LINUX +#ifndef LINUX_ETH_H +#define LINUX_ETH_H #ifdef __cplusplus extern "C" { #endif -typedef enum { - tx_packets = 0, - tx_bytes, - rx_packets, - rx_bytes, - TxDrop, - TxCrcErr, - TxUnicast, - TxMulticast, - TxBroadcast, - TxCollision, - TxSingleCollision, - TxMultipleCollision, - TxDeferred, - TxLateCollision, - TxExcessiveCollistion, - TxPause, - TxPktSz64, - TxPktSz65To127, - TxPktSz128To255, - TxPktSz256To511, - TxPktSz512To1023, - Tx1024ToMax, - TxBytes, - RxDrop, - RxFiltering, - RxUnicast, - RxMulticast, - RxBroadcast, - RxAlignErr, - RxCrcErr, - RxUnderSizeErr, - RxFragErr, - RxOverSzErr, - RxJabberErr, - RxPause, - RxPktSz64, - RxPktSz65To127, - RxPktSz128To255, - RxPktSz256To511, - RxPktSz512To1023, - RxPktSz1024ToMax, - RxBytes, - RxCtrlDrop, - RxIngressDrop, - RxArlDrop, - STATS_END, -} idx; - -static struct ethnic_stats { - __u32 cmd; - __u32 n_stats; - __u64 data[STATS_END]; -} stat; - -static struct eth_stats ifstats; - int linux_eth_get_stats(const char *ifname, struct eth_stats *s); int linux_eth_get_rmon_stats(const char *ifname, struct eth_rmon_stats *rmon); @@ -69,4 +12,4 @@ int linux_eth_get_rmon_stats(const char *ifname, struct eth_rmon_stats *rmon); } #endif -#endif /* LINUX */ +#endif /* LINUX_ETH_H */