Blame SOURCES/0012-config-slaves-order-rh1452585.patch

fc9aca
From 5231db02204ce3c45e2415eedbd915fa56445401 Mon Sep 17 00:00:00 2001
fc9aca
From: Beniamino Galvani <bgalvani@redhat.com>
fc9aca
Date: Mon, 15 May 2017 17:17:26 +0200
fc9aca
Subject: [PATCH] core: add configuration flag to choose slaves activation
fc9aca
 order
fc9aca
fc9aca
Commits 39d0559d9a7a ("platform: sort links by name instead of
fc9aca
ifindex") and 529a0a1a7f19 ("manager: sort slaves to be autoconnected
fc9aca
by device name") changed the order of activation of slaves. Introduce
fc9aca
a system-wide configuration property to preserve the old behavior.
fc9aca
fc9aca
https://bugzilla.redhat.com/show_bug.cgi?id=1452585
fc9aca
(cherry picked from commit 31656a066bfb3edc106f5efd5d2be46396824930)
fc9aca
(cherry picked from commit 3fefef8594ef2690c56fd44a6ce4836decaaee56)
fc9aca
---
fc9aca
 man/NetworkManager.conf.xml       | 13 +++++++++++++
fc9aca
 src/nm-config.h                   |  1 +
fc9aca
 src/nm-manager.c                  | 26 ++++++++++++++++++++------
fc9aca
 src/platform/nm-platform.c        | 26 +++++++++++++++-----------
fc9aca
 src/platform/nm-platform.h        |  2 +-
fc9aca
 src/platform/tests/test-common.c  |  2 +-
fc9aca
 src/platform/tests/test-general.c |  2 +-
fc9aca
 7 files changed, 52 insertions(+), 20 deletions(-)
fc9aca
fc9aca
diff --git a/man/NetworkManager.conf.xml b/man/NetworkManager.conf.xml
fc9aca
index 2a8ba76..5e76c0a 100644
fc9aca
--- a/man/NetworkManager.conf.xml
fc9aca
+++ b/man/NetworkManager.conf.xml
fc9aca
@@ -409,6 +409,19 @@ no-auto-default=*
fc9aca
         </listitem>
fc9aca
       </varlistentry>
fc9aca
 
fc9aca
+      <varlistentry>
fc9aca
+        <term><varname>slaves-order</varname></term>
fc9aca
+        <listitem>
fc9aca
+          <para>
fc9aca
+            This key specifies in which order slave connections are
fc9aca
+            auto-activated on boot or when the master activates
fc9aca
+            them. Allowed values are <literal>name</literal> (order
fc9aca
+            connection by interface name, the default), or
fc9aca
+            <literal>index</literal> (order slaves by their kernel
fc9aca
+            index).
fc9aca
+          </para>
fc9aca
+        </listitem>
fc9aca
+      </varlistentry>
fc9aca
     </variablelist>
fc9aca
   </refsect1>
fc9aca
 
fc9aca
diff --git a/src/nm-config.h b/src/nm-config.h
fc9aca
index 283d6a1..ae695fc 100644
fc9aca
--- a/src/nm-config.h
fc9aca
+++ b/src/nm-config.h
fc9aca
@@ -63,6 +63,7 @@
fc9aca
 #define NM_CONFIG_KEYFILE_KEY_MAIN_DHCP                     "dhcp"
fc9aca
 #define NM_CONFIG_KEYFILE_KEY_MAIN_DEBUG                    "debug"
fc9aca
 #define NM_CONFIG_KEYFILE_KEY_MAIN_HOSTNAME_MODE            "hostname-mode"
fc9aca
+#define NM_CONFIG_KEYFILE_KEY_MAIN_SLAVES_ORDER             "slaves-order"
fc9aca
 #define NM_CONFIG_KEYFILE_KEY_LOGGING_BACKEND               "backend"
fc9aca
 #define NM_CONFIG_KEYFILE_KEY_CONFIG_ENABLE                 "enable"
fc9aca
 #define NM_CONFIG_KEYFILE_KEY_ATOMIC_SECTION_WAS            ".was"
fc9aca
diff --git a/src/nm-manager.c b/src/nm-manager.c
fc9aca
index a740219..3d94ce9 100644
fc9aca
--- a/src/nm-manager.c
fc9aca
+++ b/src/nm-manager.c
fc9aca
@@ -2459,10 +2459,14 @@ platform_query_devices (NMManager *self)
fc9aca
 	NMPlatformLink *links;
fc9aca
 	int i;
fc9aca
 	gboolean guess_assume;
fc9aca
+	const char *order;
fc9aca
 
fc9aca
 	guess_assume = nm_config_get_first_start (nm_config_get ());
fc9aca
-
fc9aca
-	links_array = nm_platform_link_get_all (NM_PLATFORM_GET);
fc9aca
+	order = nm_config_data_get_value_cached (NM_CONFIG_GET_DATA,
fc9aca
+	                                         NM_CONFIG_KEYFILE_GROUP_MAIN,
fc9aca
+	                                         NM_CONFIG_KEYFILE_KEY_MAIN_SLAVES_ORDER,
fc9aca
+	                                         NM_CONFIG_GET_VALUE_STRIP);
fc9aca
+	links_array = nm_platform_link_get_all (NM_PLATFORM_GET, !nm_streq0 (order, "index"));
fc9aca
 	links = (NMPlatformLink *) links_array->data;
fc9aca
 	for (i = 0; i < links_array->len; i++) {
fc9aca
 		gs_free NMConfigDeviceStateData *dev_state = NULL;
fc9aca
@@ -3016,7 +3020,7 @@ out:
fc9aca
 }
fc9aca
 
fc9aca
 static gint
fc9aca
-compare_slaves (gconstpointer a, gconstpointer b, gpointer _unused)
fc9aca
+compare_slaves (gconstpointer a, gconstpointer b, gpointer sort_by_name)
fc9aca
 {
fc9aca
 	const SlaveConnectionInfo *a_info = a;
fc9aca
 	const SlaveConnectionInfo *b_info = b;
fc9aca
@@ -3027,8 +3031,12 @@ compare_slaves (gconstpointer a, gconstpointer b, gpointer _unused)
fc9aca
 	if (!b_info->device)
fc9aca
 		return -1;
fc9aca
 
fc9aca
-	return g_strcmp0 (nm_device_get_iface (a_info->device),
fc9aca
-	                  nm_device_get_iface (b_info->device));
fc9aca
+	if (GPOINTER_TO_INT (sort_by_name)) {
fc9aca
+		return g_strcmp0 (nm_device_get_iface (a_info->device),
fc9aca
+		                  nm_device_get_iface (b_info->device));
fc9aca
+	}
fc9aca
+
fc9aca
+	return nm_device_get_ifindex (a_info->device) - nm_device_get_ifindex (b_info->device);
fc9aca
 }
fc9aca
 
fc9aca
 static void
fc9aca
@@ -3042,11 +3050,17 @@ autoconnect_slaves (NMManager *self,
fc9aca
 	if (should_connect_slaves (NM_CONNECTION (master_connection), master_device)) {
fc9aca
 		gs_free SlaveConnectionInfo *slaves = NULL;
fc9aca
 		guint i, n_slaves = 0;
fc9aca
+		const char *value;
fc9aca
 
fc9aca
 		slaves = find_slaves (self, master_connection, master_device, &n_slaves);
fc9aca
 		if (n_slaves > 1) {
fc9aca
+			value = nm_config_data_get_value_cached (NM_CONFIG_GET_DATA,
fc9aca
+			                                         NM_CONFIG_KEYFILE_GROUP_MAIN,
fc9aca
+			                                         NM_CONFIG_KEYFILE_KEY_MAIN_SLAVES_ORDER,
fc9aca
+			                                         NM_CONFIG_GET_VALUE_STRIP);
fc9aca
 			g_qsort_with_data (slaves, n_slaves, sizeof (slaves[0]),
fc9aca
-			                   compare_slaves, NULL);
fc9aca
+			                   compare_slaves,
fc9aca
+			                   GINT_TO_POINTER (!nm_streq0 (value, "index")));
fc9aca
 		}
fc9aca
 
fc9aca
 		for (i = 0; i < n_slaves; i++) {
fc9aca
diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c
fc9aca
index 767187d..a244ff3 100644
fc9aca
--- a/src/platform/nm-platform.c
fc9aca
+++ b/src/platform/nm-platform.c
fc9aca
@@ -437,7 +437,8 @@ nm_platform_sysctl_get_int_checked (NMPlatform *self, const char *pathid, int di
fc9aca
 
fc9aca
 static int
fc9aca
 _link_get_all_presort (gconstpointer  p_a,
fc9aca
-                       gconstpointer  p_b)
fc9aca
+                       gconstpointer  p_b,
fc9aca
+                       gpointer       sort_by_name)
fc9aca
 {
fc9aca
 	const NMPlatformLink *a = p_a;
fc9aca
 	const NMPlatformLink *b = p_b;
fc9aca
@@ -448,13 +449,16 @@ _link_get_all_presort (gconstpointer  p_a,
fc9aca
 	if (b->ifindex == 1)
fc9aca
 		return 1;
fc9aca
 
fc9aca
-	/* Initialized links first */
fc9aca
-	if (a->initialized > b->initialized)
fc9aca
-		return -1;
fc9aca
-	if (a->initialized < b->initialized)
fc9aca
-		return 1;
fc9aca
+	if (GPOINTER_TO_INT (sort_by_name)) {
fc9aca
+		/* Initialized links first */
fc9aca
+		if (a->initialized > b->initialized)
fc9aca
+			return -1;
fc9aca
+		if (a->initialized < b->initialized)
fc9aca
+			return 1;
fc9aca
 
fc9aca
-	return strcmp (a->name, b->name);
fc9aca
+		return strcmp (a->name, b->name);
fc9aca
+	} else
fc9aca
+		return a->ifindex - b->ifindex;
fc9aca
 }
fc9aca
 
fc9aca
 /**
fc9aca
@@ -465,7 +469,7 @@ _link_get_all_presort (gconstpointer  p_a,
fc9aca
  * owned by the caller and should be freed with g_array_unref().
fc9aca
  */
fc9aca
 GArray *
fc9aca
-nm_platform_link_get_all (NMPlatform *self)
fc9aca
+nm_platform_link_get_all (NMPlatform *self, gboolean sort_by_name)
fc9aca
 {
fc9aca
 	GArray *links, *result;
fc9aca
 	guint i, j, nresult;
fc9aca
@@ -479,9 +483,9 @@ nm_platform_link_get_all (NMPlatform *self)
fc9aca
 	if (!links || links->len == 0)
fc9aca
 		return links;
fc9aca
 
fc9aca
-	/* first sort the links by their ifindex. Below we will sort further by moving
fc9aca
-	 * children/slaves to the end. */
fc9aca
-	g_array_sort (links, _link_get_all_presort);
fc9aca
+	/* first sort the links by their ifindex or name. Below we will sort
fc9aca
+	 * further by moving children/slaves to the end. */
fc9aca
+	g_array_sort_with_data (links, _link_get_all_presort, GINT_TO_POINTER (sort_by_name));
fc9aca
 
fc9aca
 	unseen = g_hash_table_new (g_direct_hash, g_direct_equal);
fc9aca
 	for (i = 0; i < links->len; i++) {
fc9aca
diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h
fc9aca
index 43be17f..1b8fa13 100644
fc9aca
--- a/src/platform/nm-platform.h
fc9aca
+++ b/src/platform/nm-platform.h
fc9aca
@@ -763,7 +763,7 @@ const NMPlatformLink *nm_platform_link_get (NMPlatform *self, int ifindex);
fc9aca
 const NMPlatformLink *nm_platform_link_get_by_ifname (NMPlatform *self, const char *ifname);
fc9aca
 const NMPlatformLink *nm_platform_link_get_by_address (NMPlatform *self, gconstpointer address, size_t length);
fc9aca
 
fc9aca
-GArray *nm_platform_link_get_all (NMPlatform *self);
fc9aca
+GArray *nm_platform_link_get_all (NMPlatform *self, gboolean sort_by_name);
fc9aca
 NMPlatformError nm_platform_link_dummy_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link);
fc9aca
 NMPlatformError nm_platform_link_bridge_add (NMPlatform *self, const char *name, const void *address, size_t address_len, const NMPlatformLink **out_link);
fc9aca
 NMPlatformError nm_platform_link_bond_add (NMPlatform *self, const char *name, const NMPlatformLink **out_link);
fc9aca
diff --git a/src/platform/tests/test-common.c b/src/platform/tests/test-common.c
fc9aca
index 04db862..a9d0694 100644
fc9aca
--- a/src/platform/tests/test-common.c
fc9aca
+++ b/src/platform/tests/test-common.c
fc9aca
@@ -185,7 +185,7 @@ link_callback (NMPlatform *platform, int obj_type_i, int ifindex, NMPlatformLink
fc9aca
 
fc9aca
 	/* Check the data */
fc9aca
 	g_assert (received->ifindex > 0);
fc9aca
-	links = nm_platform_link_get_all (NM_PLATFORM_GET);
fc9aca
+	links = nm_platform_link_get_all (NM_PLATFORM_GET, TRUE);
fc9aca
 	for (i = 0; i < links->len; i++) {
fc9aca
 		cached = &g_array_index (links, NMPlatformLink, i);
fc9aca
 		if (cached->ifindex == received->ifindex) {
fc9aca
diff --git a/src/platform/tests/test-general.c b/src/platform/tests/test-general.c
fc9aca
index 2ccfac7..e772662 100644
fc9aca
--- a/src/platform/tests/test-general.c
fc9aca
+++ b/src/platform/tests/test-general.c
fc9aca
@@ -48,7 +48,7 @@ test_link_get_all (void)
fc9aca
 
fc9aca
 	platform = nm_linux_platform_new (TRUE, NM_PLATFORM_NETNS_SUPPORT_DEFAULT);
fc9aca
 
fc9aca
-	links = nm_platform_link_get_all (platform);
fc9aca
+	links = nm_platform_link_get_all (platform, TRUE);
fc9aca
 }
fc9aca
 
fc9aca
 /*****************************************************************************/
fc9aca
-- 
fc9aca
2.9.3
fc9aca