From 647bce24ca25375b9b6ef46bc2fbf514aae32927 Mon Sep 17 00:00:00 2001
From: Beniamino Galvani <bgalvani@redhat.com>
Date: Thu, 21 Apr 2016 11:31:23 +0200
Subject: [PATCH] platform: add logs to debug link type matching

---
 src/platform/nm-linux-platform.c | 24 ++++++++++++++++++++----
 src/platform/wifi/wifi-utils.c   |  7 ++++++-
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c
index 1ce4818..ea8c710 100644
--- a/src/platform/nm-linux-platform.c
+++ b/src/platform/nm-linux-platform.c
@@ -631,6 +631,8 @@ _linktype_get_type (NMPlatform *platform,
 
 	_assert_netns_current (platform);
 
+	_LOGT ("link-type: get type for %s ifindex %d arptype %u", ifname ?: "(null)", ifindex, arptype);
+
 	if (completed_from_cache) {
 		const NMPObject *obj;
 
@@ -652,6 +654,7 @@ _linktype_get_type (NMPlatform *platform,
 		        || !g_strcmp0 (kind, obj->link.kind))) {
 			nm_assert (obj->link.kind == g_intern_string (obj->link.kind));
 			*out_kind = obj->link.kind;
+			_LOGT ("link-type: %u (from cache lookup)", (guint) obj->link.type);
 			return obj->link.type;
 		}
 	}
@@ -660,8 +663,10 @@ _linktype_get_type (NMPlatform *platform,
 
 	if (kind) {
 		for (i = 0; i < G_N_ELEMENTS (linktypes); i++) {
-			if (g_strcmp0 (kind, linktypes[i].rtnl_type) == 0)
+			if (g_strcmp0 (kind, linktypes[i].rtnl_type) == 0) {
+				_LOGT ("link-type: %u (from kind attribute '%s')", (guint) linktypes[i].nm_type, kind);
 				return linktypes[i].nm_type;
+			}
 		}
 
 		if (!strcmp (kind, "tun")) {
@@ -697,6 +702,8 @@ _linktype_get_type (NMPlatform *platform,
 		gs_free char *anycast_mask = NULL;
 		gs_free char *devtype = NULL;
 
+		_LOGT ("link-type: trying ethtool");
+
 		/* Fallback OVS detection for kernel <= 3.16 */
 		if (nmp_utils_ethtool_get_driver_info (ifname, &driver, NULL, NULL)) {
 			if (!g_strcmp0 (driver, "openvswitch"))
@@ -711,12 +718,15 @@ _linktype_get_type (NMPlatform *platform,
 			}
 		}
 
+		_LOGT ("link-type: trying sysfs");
+
 		sysfs_path = g_strdup_printf ("/sys/class/net/%s", ifname);
 		anycast_mask = g_strdup_printf ("%s/anycast_mask", sysfs_path);
 		if (g_file_test (anycast_mask, G_FILE_TEST_EXISTS))
 			return NM_LINK_TYPE_OLPC_MESH;
 
 		devtype = _linktype_read_devtype (sysfs_path);
+		_LOGT ("link-type: devtype '%s'", devtype ?: "(null)");
 		for (i = 0; devtype && i < G_N_ELEMENTS (linktypes); i++) {
 			if (g_strcmp0 (devtype, linktypes[i].devtype) == 0) {
 				if (linktypes[i].nm_type == NM_LINK_TYPE_BNEP) {
@@ -731,8 +741,10 @@ _linktype_get_type (NMPlatform *platform,
 		}
 
 		/* Fallback for drivers that don't call SET_NETDEV_DEVTYPE() */
-		if (wifi_utils_is_wifi (ifname, sysfs_path))
+		if (wifi_utils_is_wifi (ifname, sysfs_path)) {
+			_LOGT ("link-type: wifi type found");
 			return NM_LINK_TYPE_WIFI;
+		}
 
 		if (arptype == ARPHRD_ETHER) {
 			/* Standard wired ethernet interfaces don't report an rtnl_link_type, so
@@ -740,12 +752,16 @@ _linktype_get_type (NMPlatform *platform,
 			 * prevent future virtual network drivers from being treated as Ethernet
 			 * when they should be Generic instead.
 			 */
-			if (!kind && !devtype)
+			if (!kind && !devtype) {
+				_LOGT ("link-type: ethernet (no kind or devtype)");
 				return NM_LINK_TYPE_ETHERNET;
+			}
 			/* The USB gadget interfaces behave and look like ordinary ethernet devices
 			 * aside from the DEVTYPE. */
-			if (!g_strcmp0 (devtype, "gadget"))
+			if (!g_strcmp0 (devtype, "gadget")) {
+				_LOGT ("link-type: ethernet (gadget)");
 				return NM_LINK_TYPE_ETHERNET;
+			}
 		}
 	}
 
diff --git a/src/platform/wifi/wifi-utils.c b/src/platform/wifi/wifi-utils.c
index 38e5a05..d67cb65 100644
--- a/src/platform/wifi/wifi-utils.c
+++ b/src/platform/wifi/wifi-utils.c
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "nm-logging.h"
 #include "wifi-utils.h"
 #include "wifi-utils-private.h"
 #include "wifi-utils-nl80211.h"
@@ -166,13 +167,17 @@ wifi_utils_is_wifi (const char *iface, const char *sysfs_path)
 {
 	char phy80211_path[255];
 	struct stat s;
+	int r;
 
 	g_return_val_if_fail (iface != NULL, FALSE);
 
 	if (sysfs_path) {
 		/* Check for nl80211 sysfs paths */
 		g_snprintf (phy80211_path, sizeof (phy80211_path), "%s/phy80211", sysfs_path);
-		if ((stat (phy80211_path, &s) == 0 && (s.st_mode & S_IFDIR)))
+		r = stat (phy80211_path, &s);
+		nm_log_trace (LOGD_WIFI, "link-type: '%s' phy80211 %d, mode %x",
+		              iface, r, (guint) (r == 0 ? s.st_mode : 0));
+		if (r == 0 && (s.st_mode & S_IFDIR))
 			return TRUE;
 	}
 
-- 
2.5.5

