]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ethtool-util: introduce ethtool_get_link_info()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 17 Jun 2019 06:31:20 +0000 (15:31 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 19 Jun 2019 14:15:19 +0000 (23:15 +0900)
Will be used in later commits.

src/shared/ethtool-util.c
src/shared/ethtool-util.h

index 3fba238ac4cde622975639327feea054e15ef303..b2a81e4215cd1c5f0a914a86dfed4521ee6e538d 100644 (file)
@@ -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
index cbbe04179939f9bd0debc03f2085f80aa6ed6294..03976c6ee3521417547b2e327d3646f6733b3437 100644 (file)
@@ -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);