diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index 273defaf5..0e556d351 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -56,6 +56,7 @@ #include "nm-enum-types.h" #include "nm-netlink-monitor.h" #include "nm-dbus-manager.h" +#include "nm-platform.h" #include "nm-device-ethernet-glue.h" @@ -110,9 +111,6 @@ enum { }; -static gboolean supports_mii_carrier_detect (NMDeviceEthernet *dev); -static gboolean supports_ethtool_carrier_detect (NMDeviceEthernet *dev); - static GQuark nm_ethernet_error_quark (void) { @@ -453,12 +451,15 @@ update_initial_hw_address (NMDevice *dev) static guint32 get_generic_capabilities (NMDevice *dev) { - NMDeviceEthernet *self = NM_DEVICE_ETHERNET (dev); - - if (supports_ethtool_carrier_detect (self) || supports_mii_carrier_detect (self)) - return NM_DEVICE_CAP_CARRIER_DETECT; - - return NM_DEVICE_CAP_NONE; + if (nm_platform_link_supports_carrier_detect (nm_device_get_ifindex (dev))) + return NM_DEVICE_CAP_CARRIER_DETECT; + else { + nm_log_info (LOGD_HW, + "(%s): driver '%s' does not support carrier detection.", + nm_device_get_iface (dev), + nm_device_get_driver (dev)); + return NM_DEVICE_CAP_NONE; + } } static gboolean @@ -1469,111 +1470,3 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass) dbus_g_error_domain_register (NM_ETHERNET_ERROR, NULL, NM_TYPE_ETHERNET_ERROR); } - - -/**************************************/ -/* Ethtool capability detection */ -/**************************************/ - -static gboolean -supports_ethtool_carrier_detect (NMDeviceEthernet *self) -{ - int fd; - struct ifreq ifr; - gboolean supports_ethtool = FALSE; - struct ethtool_cmd edata; - - g_return_val_if_fail (self != NULL, FALSE); - - fd = socket (PF_INET, SOCK_DGRAM, 0); - if (fd < 0) { - nm_log_err (LOGD_HW, "couldn't open control socket."); - return FALSE; - } - - memset (&ifr, 0, sizeof (struct ifreq)); - strncpy (ifr.ifr_name, nm_device_get_iface (NM_DEVICE (self)), IFNAMSIZ); - - edata.cmd = ETHTOOL_GLINK; - ifr.ifr_data = (char *) &edata; - - errno = 0; - if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) { - nm_log_dbg (LOGD_HW | LOGD_ETHER, "SIOCETHTOOL failed: %d", errno); - goto out; - } - - supports_ethtool = TRUE; - -out: - close (fd); - nm_log_dbg (LOGD_HW | LOGD_ETHER, "ethtool %s supported", - supports_ethtool ? "is" : "not"); - return supports_ethtool; -} - - -/**************************************/ -/* MII capability detection */ -/**************************************/ -#define _LINUX_IF_H -#include -#undef _LINUX_IF_H - -static int -mdio_read (NMDeviceEthernet *self, int fd, struct ifreq *ifr, int location) -{ - struct mii_ioctl_data *mii; - int val = -1; - - g_return_val_if_fail (fd >= 0, -1); - g_return_val_if_fail (ifr != NULL, -1); - - mii = (struct mii_ioctl_data *) &ifr->ifr_ifru; - mii->reg_num = location; - - errno = 0; - if (ioctl (fd, SIOCGMIIREG, ifr) == 0) { - nm_log_dbg (LOGD_HW | LOGD_ETHER, "SIOCGMIIREG result 0x%X", mii->val_out); - val = mii->val_out; - } else { - nm_log_dbg (LOGD_HW | LOGD_ETHER, "SIOCGMIIREG failed: %d", errno); - } - - return val; -} - -static gboolean -supports_mii_carrier_detect (NMDeviceEthernet *self) -{ - int fd, bmsr; - struct ifreq ifr; - gboolean supports_mii = FALSE; - - g_return_val_if_fail (self != NULL, FALSE); - - fd = socket (PF_INET, SOCK_DGRAM, 0); - if (fd < 0) { - nm_log_err (LOGD_HW, "couldn't open control socket."); - return FALSE; - } - - memset (&ifr, 0, sizeof (struct ifreq)); - strncpy (ifr.ifr_name, nm_device_get_iface (NM_DEVICE (self)), IFNAMSIZ); - - errno = 0; - if (ioctl (fd, SIOCGMIIPHY, &ifr) < 0) { - nm_log_dbg (LOGD_HW | LOGD_ETHER, "SIOCGMIIPHY failed: %d", errno); - goto out; - } - - /* If we can read the BMSR register, we assume that the card supports MII link detection */ - bmsr = mdio_read (self, fd, &ifr, MII_BMSR); - supports_mii = (bmsr != -1) ? TRUE : FALSE; - nm_log_dbg (LOGD_HW | LOGD_ETHER, "MII %s supported", - supports_mii ? "is" : "not"); - -out: - close (fd); - return supports_mii; -} diff --git a/src/devices/nm-device-generic.c b/src/devices/nm-device-generic.c index 1b15932d6..bb6fc6ce1 100644 --- a/src/devices/nm-device-generic.c +++ b/src/devices/nm-device-generic.c @@ -58,6 +58,15 @@ nm_device_generic_error_quark (void) /**************************************************************/ +static guint32 +get_generic_capabilities (NMDevice *dev) +{ + if (nm_platform_link_supports_carrier_detect (nm_device_get_ifindex (dev))) + return NM_DEVICE_CAP_CARRIER_DETECT; + else + return NM_DEVICE_CAP_NONE; +} + static gboolean is_available (NMDevice *device) { @@ -190,6 +199,7 @@ nm_device_generic_class_init (NMDeviceGenericClass *klass) object_class->get_property = get_property; object_class->set_property = set_property; + parent_class->get_generic_capabilities = get_generic_capabilities; parent_class->is_available = is_available; parent_class->check_connection_compatible = check_connection_compatible; diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index c789fe90b..ed4adab09 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -569,10 +569,6 @@ constructed (GObject *object) nm_netlink_monitor_request_status (netlink_monitor); } } else { - nm_log_info (LOGD_HW, - "(%s): driver '%s' does not support carrier detection.", - nm_device_get_iface (dev), - nm_device_get_driver (dev)); /* Fake online link when carrier detection is not available. */ priv->carrier = TRUE; } diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index d38b3628c..b63ef1342 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -1058,20 +1059,70 @@ ethtool_get_stringset_index (const char *ifname, int stringset_id, const char *s } static gboolean -link_supports_carrier_detect (NMPlatform *platform, int ifindex) +supports_ethtool_carrier_detect (const char *ifname) { - const char *name = nm_platform_link_get_name (ifindex); struct ethtool_cmd edata = { .cmd = ETHTOOL_GLINK }; /* We ignore the result. If the ETHTOOL_GLINK call succeeded, then we * assume the device supports carrier-detect, otherwise we assume it * doesn't. - * - * We don't use ETHTOOL_GLINK for carrier detect itself, so this can - * be regarded as a hack. Instead, kernel should be able to report - * carrier detection capability via netlink. */ - return name && ethtool_get (name, &edata); + return ethtool_get (ifname, &edata); +} + +static gboolean +supports_mii_carrier_detect (const char *ifname) +{ + int fd; + struct ifreq ifr; + struct mii_ioctl_data *mii; + gboolean supports_mii = FALSE; + + fd = socket (PF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + nm_log_err (LOGD_PLATFORM, "couldn't open control socket."); + return FALSE; + } + + memset (&ifr, 0, sizeof (struct ifreq)); + strncpy (ifr.ifr_name, ifname, IFNAMSIZ); + + errno = 0; + if (ioctl (fd, SIOCGMIIPHY, &ifr) < 0) { + nm_log_dbg (LOGD_PLATFORM, "SIOCGMIIPHY failed: %d", errno); + goto out; + } + + /* If we can read the BMSR register, we assume that the card supports MII link detection */ + mii = (struct mii_ioctl_data *) &ifr.ifr_ifru; + mii->reg_num = MII_BMSR; + + if (ioctl (fd, SIOCGMIIREG, &ifr) == 0) { + nm_log_dbg (LOGD_PLATFORM, "SIOCGMIIREG result 0x%X", mii->val_out); + supports_mii = TRUE; + } else { + nm_log_dbg (LOGD_PLATFORM, "SIOCGMIIREG failed: %d", errno); + } + + out: + close (fd); + nm_log_dbg (LOGD_PLATFORM, "MII %s supported", supports_mii ? "is" : "not"); + return supports_mii; +} + +static gboolean +link_supports_carrier_detect (NMPlatform *platform, int ifindex) +{ + const char *name = nm_platform_link_get_name (ifindex); + + if (!name) + return FALSE; + + /* We use netlink for the actual carrier detection, but netlink can't tell + * us whether the device actually supports carrier detection in the first + * place. We assume any device that does implements one of these two APIs. + */ + return supports_ethtool_carrier_detect (name) || supports_mii_carrier_detect (name); } static gboolean