From: Yu Watanabe Date: Mon, 17 Jun 2019 06:31:20 +0000 (+0900) Subject: ethtool-util: introduce ethtool_get_link_info() X-Git-Tag: v243-rc1~263^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=33a8695fdcdd1c85878287a46bb7b77184f5db8b;p=thirdparty%2Fsystemd.git ethtool-util: introduce ethtool_get_link_info() Will be used in later commits. --- diff --git a/src/shared/ethtool-util.c b/src/shared/ethtool-util.c index 3fba238ac4c..b2a81e4215c 100644 --- a/src/shared/ethtool-util.c +++ b/src/shared/ethtool-util.c @@ -160,6 +160,44 @@ int ethtool_get_driver(int *fd, const char *ifname, char **ret) { return 0; } +int ethtool_get_link_info(int *fd, const char *ifname, + int *ret_autonegotiation, size_t *ret_speed, + Duplex *ret_duplex, NetDevPort *ret_port) { + struct ethtool_cmd ecmd = { + .cmd = ETHTOOL_GSET, + }; + struct ifreq ifr = { + .ifr_data = (void*) &ecmd, + }; + int r; + + if (*fd < 0) { + r = ethtool_connect_or_warn(fd, false); + if (r < 0) + return r; + } + + strscpy(ifr.ifr_name, IFNAMSIZ, ifname); + + r = ioctl(*fd, SIOCETHTOOL, &ifr); + if (r < 0) + return -errno; + + if (ret_autonegotiation) + *ret_autonegotiation = ecmd.autoneg; + + if (ret_speed) + *ret_speed = ethtool_cmd_speed(&ecmd) * 1000 * 1000; + + if (ret_duplex) + *ret_duplex = ecmd.duplex; + + if (ret_port) + *ret_port = ecmd.port; + + return 0; +} + int ethtool_set_speed(int *fd, const char *ifname, unsigned speed, Duplex duplex) { struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET diff --git a/src/shared/ethtool-util.h b/src/shared/ethtool-util.h index cbbe0417993..03976c6ee35 100644 --- a/src/shared/ethtool-util.h +++ b/src/shared/ethtool-util.h @@ -80,6 +80,9 @@ typedef struct netdev_channels { } netdev_channels; int ethtool_get_driver(int *fd, const char *ifname, char **ret); +int ethtool_get_link_info(int *fd, const char *ifname, + int *ret_autonegotiation, size_t *ret_speed, + Duplex *ret_duplex, NetDevPort *ret_port); int ethtool_set_speed(int *fd, const char *ifname, unsigned speed, Duplex duplex); int ethtool_set_wol(int *fd, const char *ifname, WakeOnLan wol); int ethtool_set_features(int *fd, const char *ifname, int *features);