settings: create default wired connection from NMDeviceEthernet
authorDan Williams <dcbw@redhat.com>
Mon, 8 Sep 2014 21:11:51 +0000 (16:11 -0500)
committerDan Williams <dcbw@redhat.com>
Thu, 11 Sep 2014 17:47:07 +0000 (12:47 -0500)
Instead of creating it in NMSettings, where we must use
NM_IS_DEVICE_ETHERNET() (not NM_DEVICE_TYPE_ETHERNET because various generic
devices masquerade as NM_DEVICE_TYPE_ETHERNET too), push knowledge
of which device types create default wired connections into the device
types themselves.  This solves a problem with testcases where
libNetworkManager.a (which testcases link to) requires the symbol
nm_type_device_ethernet().

12 files changed:
.gitignore
po/POTFILES.in
src/Makefile.am
src/devices/nm-device-ethernet-utils.c [moved from src/settings/nm-settings-utils.c with 66% similarity]
src/devices/nm-device-ethernet-utils.h [moved from src/settings/nm-settings-utils.h with 78% similarity]
src/devices/nm-device-ethernet.c
src/devices/nm-device.c
src/devices/nm-device.h
src/settings/nm-settings.c
src/settings/tests/Makefile.am [deleted file]
src/tests/Makefile.am
src/tests/test-wired-defname.c [moved from src/settings/tests/test-wired-defname.c with 57% similarity]

index 41c0d90..57325cc 100644 (file)
@@ -235,7 +235,6 @@ valgrind-*.log
 /src/settings/plugins/ifnet/tests/check_ifnet
 /src/settings/plugins/ifupdown/tests/test-ifupdown
 /src/settings/plugins/keyfile/tests/test-keyfile
-/src/settings/tests/test-wired-defname
 /src/supplicant-manager/tests/test-supplicant-config
 /src/tests/config/test-config
 /src/tests/test-dcb
@@ -244,5 +243,6 @@ valgrind-*.log
 /src/tests/test-ip4-config
 /src/tests/test-ip6-config
 /src/tests/test-resolvconf-capture
+/src/tests/test-wired-defname
 
 /vapi/*.vapi
index a7e75c4..b6be03f 100644 (file)
@@ -114,6 +114,7 @@ src/devices/bluetooth/nm-device-bt.c
 src/devices/nm-device-bond.c
 src/devices/nm-device-bridge.c
 src/devices/nm-device-ethernet.c
+src/devices/nm-device-ethernet-utils.c
 src/devices/nm-device-infiniband.c
 src/devices/nm-device-vlan.c
 src/devices/team/nm-device-team.c
@@ -125,4 +126,3 @@ src/nm-manager.c
 src/nm-sleep-monitor-systemd.c
 src/settings/plugins/ibft/plugin.c
 src/settings/plugins/ifcfg-rh/reader.c
-src/settings/nm-settings-utils.c
index ae4f267..494bc8e 100644 (file)
@@ -30,7 +30,6 @@ SUBDIRS += \
        dnsmasq-manager/tests \
        platform \
        rdisc \
-       settings/tests \
        supplicant-manager/tests \
        tests
 endif
@@ -71,6 +70,8 @@ nm_sources = \
        devices/nm-device-bridge.h \
        devices/nm-device-ethernet.c \
        devices/nm-device-ethernet.h \
+       devices/nm-device-ethernet-utils.c \
+       devices/nm-device-ethernet-utils.h \
        devices/nm-device-factory.c \
        devices/nm-device-factory.h \
        devices/nm-device-generic.c \
@@ -157,8 +158,6 @@ nm_sources = \
        settings/nm-settings-connection.h \
        settings/nm-settings-error.c \
        settings/nm-settings-error.h \
-       settings/nm-settings-utils.c \
-       settings/nm-settings-utils.h \
        settings/nm-settings.c \
        settings/nm-settings.h \
        settings/nm-system-config-interface.c \
similarity index 66%
rename from src/settings/nm-settings-utils.c
rename to src/devices/nm-device-ethernet-utils.c
index d0e21fb..631dd34 100644 (file)
 #include <glib/gi18n.h>
 
 #include <nm-connection.h>
-#include "nm-settings-utils.h"
+#include "nm-device-ethernet-utils.h"
 
 char *
-nm_settings_utils_get_default_wired_name (GHashTable *connections)
+nm_device_ethernet_utils_get_default_wired_name (const GSList *connections)
 {
-       GHashTableIter iter;
-       NMConnection *connection = NULL;
-       GSList *names = NULL, *niter;
+       const GSList *iter;
        char *cname = NULL;
        int i = 0;
 
-       /* Build up a list of all existing connection names for dupe checking */
-       g_hash_table_iter_init (&iter, connections);
-       while (g_hash_table_iter_next (&iter, NULL, (gpointer) &connection)) {
-               const char *id;
-
-               id = nm_connection_get_id (connection);
-               g_assert (id);
-               names = g_slist_append (names, (gpointer) id);
-       }
-
        /* Find the next available unique connection name */
        while (!cname && (i++ < 10000)) {
                char *temp;
                gboolean found = FALSE;
 
                temp = g_strdup_printf (_("Wired connection %d"), i);
-               for (niter = names; niter; niter = g_slist_next (niter)) {
-                       if (g_strcmp0 (niter->data, temp) == 0) {
+               for (iter = connections; iter; iter = iter->next) {
+                       if (g_strcmp0 (nm_connection_get_id (NM_CONNECTION (iter->data)), temp) == 0) {
                                found = TRUE;
                                g_free (temp);
                                break;
@@ -59,7 +47,6 @@ nm_settings_utils_get_default_wired_name (GHashTable *connections)
                if (found == FALSE)
                        cname = temp;
        }
-       g_slist_free (names);
 
        return cname;
 }
similarity index 78%
rename from src/settings/nm-settings-utils.h
rename to src/devices/nm-device-ethernet-utils.h
index 4ab8d44..5848f7e 100644 (file)
  * (C) Copyright 2011 Red Hat, Inc.
  */
 
-#ifndef __NETWORKMANAGER_SETTINGS_UTILS_H__
-#define __NETWORKMANAGER_SETTINGS_UTILS_H__
+#ifndef __NETWORKMANAGER_DEVICE_ETHERNET_UTILS_H__
+#define __NETWORKMANAGER_DEVICE_ETHERNET_UTILS_H__
 
 #include <glib.h>
 
-char *nm_settings_utils_get_default_wired_name (GHashTable *connections);
+char *nm_device_ethernet_utils_get_default_wired_name (const GSList *connections);
 
-#endif  /* NM_SETTINGS_UTILS_H */
+#endif  /* NETWORKMANAGER_DEVICE_ETHERNET_UTILS_H */
index d657c07..4415932 100644 (file)
@@ -55,6 +55,9 @@
 #include "nm-platform.h"
 #include "nm-dcb.h"
 #include "nm-settings-connection.h"
+#include "nm-config.h"
+#include "nm-device-ethernet-utils.h"
+#include "nm-connection-provider.h"
 
 #include "nm-device-ethernet-glue.h"
 
@@ -1473,6 +1476,51 @@ complete_connection (NMDevice *device,
        return TRUE;
 }
 
+static NMConnection *
+new_default_connection (NMDevice *self)
+{
+       NMConnection *connection;
+       const GSList *connections;
+       NMSetting *setting;
+       const char *hw_address;
+       char *defname, *uuid;
+       GByteArray *mac;
+
+       if (!nm_config_get_ethernet_can_auto_default (nm_config_get (), self))
+               return NULL;
+
+       hw_address = nm_device_get_hw_address (self);
+       if (!hw_address)
+               return NULL;
+
+       connection = nm_simple_connection_new ();
+       setting = nm_setting_connection_new ();
+       nm_connection_add_setting (connection, setting);
+
+       connections = nm_connection_provider_get_connections (nm_connection_provider_get ());
+       defname = nm_device_ethernet_utils_get_default_wired_name (connections);
+       uuid = nm_utils_uuid_generate ();
+       g_object_set (setting,
+                     NM_SETTING_CONNECTION_ID, defname,
+                     NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME,
+                     NM_SETTING_CONNECTION_AUTOCONNECT, TRUE,
+                     NM_SETTING_CONNECTION_UUID, uuid,
+                     NM_SETTING_CONNECTION_TIMESTAMP, (guint64) time (NULL),
+                     NULL);
+       g_free (uuid);
+       g_free (defname);
+
+       /* Lock the connection to the device */
+       setting = nm_setting_wired_new ();
+       nm_connection_add_setting (connection, setting);
+
+       mac = nm_utils_hwaddr_atoba (hw_address, ETH_ALEN);
+       g_object_set (setting, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL);
+       g_byte_array_unref (mac);
+
+       return connection;
+}
+
 static gboolean
 spec_match_list (NMDevice *device, const GSList *specs)
 {
@@ -1679,6 +1727,7 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass)
        parent_class->update_initial_hw_address = update_initial_hw_address;
        parent_class->check_connection_compatible = check_connection_compatible;
        parent_class->complete_connection = complete_connection;
+       parent_class->new_default_connection = new_default_connection;
 
        parent_class->act_stage1_prepare = act_stage1_prepare;
        parent_class->act_stage2_config = act_stage2_config;
index 8bcdadf..1afb5d1 100644 (file)
@@ -1215,6 +1215,14 @@ nm_device_owns_iface (NMDevice *self, const char *iface)
        return FALSE;
 }
 
+NMConnection *
+nm_device_new_default_connection (NMDevice *self)
+{
+       if (NM_DEVICE_GET_CLASS (self)->new_default_connection)
+               return NM_DEVICE_GET_CLASS (self)->new_default_connection (self);
+       return NULL;
+}
+
 static void
 slave_state_changed (NMDevice *slave,
                      NMDeviceState slave_new_state,
index 3ac3fd6..28a2ff0 100644 (file)
@@ -209,6 +209,8 @@ typedef struct {
        gboolean        (* component_added) (NMDevice *self, GObject *component);
 
        gboolean        (* owns_iface) (NMDevice *self, const char *iface);
+
+       NMConnection *  (* new_default_connection) (NMDevice *self);
 } NMDeviceClass;
 
 
@@ -362,6 +364,8 @@ gboolean nm_device_notify_component_added (NMDevice *device, GObject *component)
 
 gboolean nm_device_owns_iface (NMDevice *device, const char *iface);
 
+NMConnection *nm_device_new_default_connection (NMDevice *self);
+
 G_END_DECLS
 
 /* For testing only */
index 2957a21..0ad85e6 100644 (file)
@@ -67,7 +67,6 @@
 #include "nm-session-monitor.h"
 #include "plugins/keyfile/plugin.h"
 #include "nm-agent-manager.h"
-#include "nm-settings-utils.h"
 #include "nm-connection-provider.h"
 #include "nm-config.h"
 #include "NetworkManagerUtils.h"
@@ -1585,55 +1584,22 @@ default_wired_clear_tag (NMSettings *self,
 void
 nm_settings_device_added (NMSettings *self, NMDevice *device)
 {
-       NMSettingsPrivate *priv = NM_SETTINGS_GET_PRIVATE (self);
        NMConnection *connection;
        NMSettingsConnection *added;
-       NMSetting *setting;
        GError *error = NULL;
-       const char *hw_address;
-       char *defname, *uuid;
-       GByteArray *mac;
-
-       if (!NM_IS_DEVICE_ETHERNET (device))
-               return;
 
        /* If the device isn't managed or it already has a default wired connection,
         * ignore it.
         */
        if (   !nm_device_get_managed (device)
            || g_object_get_data (G_OBJECT (device), DEFAULT_WIRED_CONNECTION_TAG)
-           || have_connection_for_device (self, device)
-           || !nm_config_get_ethernet_can_auto_default (priv->config, device))
+           || have_connection_for_device (self, device))
                return;
 
-       hw_address = nm_device_get_hw_address (device);
-       if (!hw_address)
+       connection = nm_device_new_default_connection (device);
+       if (!connection)
                return;
 
-       connection = nm_simple_connection_new ();
-       setting = nm_setting_connection_new ();
-       nm_connection_add_setting (connection, setting);
-
-       defname = nm_settings_utils_get_default_wired_name (priv->connections);
-       uuid = nm_utils_uuid_generate ();
-       g_object_set (setting,
-                     NM_SETTING_CONNECTION_ID, defname,
-                     NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME,
-                     NM_SETTING_CONNECTION_AUTOCONNECT, TRUE,
-                     NM_SETTING_CONNECTION_UUID, uuid,
-                     NM_SETTING_CONNECTION_TIMESTAMP, (guint64) time (NULL),
-                     NULL);
-       g_free (uuid);
-       g_free (defname);
-
-       /* Lock the connection to the device */
-       setting = nm_setting_wired_new ();
-       nm_connection_add_setting (connection, setting);
-
-       mac = nm_utils_hwaddr_atoba (hw_address, ETH_ALEN);
-       g_object_set (setting, NM_SETTING_WIRED_MAC_ADDRESS, mac, NULL);
-       g_byte_array_unref (mac);
-
        /* Add the connection */
        added = nm_settings_add_connection (self, connection, FALSE, &error);
        g_object_unref (connection);
diff --git a/src/settings/tests/Makefile.am b/src/settings/tests/Makefile.am
deleted file mode 100644 (file)
index 28accaa..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-AM_CPPFLAGS = \
-       -I$(top_srcdir)/include \
-       -I$(top_srcdir)/libnm-core \
-       -I$(top_builddir)/libnm-core \
-       -I$(top_srcdir)/src/settings \
-       -DG_LOG_DOMAIN=\""NetworkManager"\" \
-       -DNETWORKMANAGER_COMPILATION \
-       -DNM_VERSION_MAX_ALLOWED=NM_VERSION_NEXT_STABLE \
-       $(GLIB_CFLAGS)
-
-noinst_PROGRAMS = \
-       test-wired-defname
-
-####### wired defname test #######
-
-test_wired_defname_SOURCES = \
-       test-wired-defname.c
-
-test_wired_defname_LDADD = \
-       $(top_builddir)/src/libNetworkManager.la
-
-###########################################
-
-TESTS = test-wired-defname
index aca9b3c..a5824c9 100644 (file)
@@ -6,6 +6,7 @@ AM_CPPFLAGS = \
        -I$(top_builddir)/libnm-core \
        -I$(top_srcdir)/src/platform \
        -I$(top_srcdir)/src/dhcp-manager \
+       -I$(top_srcdir)/src/devices \
        -I$(top_srcdir)/src \
        -I$(top_builddir)/src \
        -DG_LOG_DOMAIN=\""NetworkManager"\" \
@@ -20,7 +21,8 @@ noinst_PROGRAMS = \
        test-ip4-config \
        test-ip6-config \
        test-dcb \
-       test-resolvconf-capture
+       test-resolvconf-capture \
+       test-wired-defname
 
 ####### ip4 config test #######
 
@@ -70,11 +72,26 @@ test_general_with_expect_SOURCES = \
 test_general_with_expect_LDADD = \
        $(top_builddir)/src/libNetworkManager.la
 
+####### wired defname test #######
+
+test_wired_defname_SOURCES = \
+       test-wired-defname.c
+
+test_wired_defname_LDADD = \
+       $(top_builddir)/src/libNetworkManager.la
+
 ####### secret agent interface test #######
 
 EXTRA_DIST = test-secret-agent.py
 
 ###########################################
 
-TESTS = test-ip4-config test-ip6-config test-dcb test-resolvconf-capture test-general test-general-with-expect
+TESTS = \
+       test-ip4-config \
+       test-ip6-config \
+       test-dcb \
+       test-resolvconf-capture \
+       test-general \
+       test-general-with-expect \
+       test-wired-defname
 
similarity index 57%
rename from src/settings/tests/test-wired-defname.c
rename to src/tests/test-wired-defname.c
index f8944fc..968971d 100644 (file)
@@ -23,7 +23,7 @@
 
 #include <nm-simple-connection.h>
 #include <nm-setting-connection.h>
-#include "nm-settings-utils.h"
+#include "nm-device-ethernet-utils.h"
 
 static NMConnection *
 _new_connection (const char *id)
@@ -43,15 +43,10 @@ _new_connection (const char *id)
 static void
 test_defname_no_connections (void)
 {
-       GHashTable *hash;
        char *name;
 
-       hash = g_hash_table_new (g_direct_hash, g_direct_equal);
-
-       name = nm_settings_utils_get_default_wired_name (hash);
+       name = nm_device_ethernet_utils_get_default_wired_name (NULL);
        g_assert_cmpstr (name, ==, "Wired connection 1");
-
-       g_hash_table_destroy (hash);
 }
 
 /*******************************************/
@@ -59,19 +54,17 @@ test_defname_no_connections (void)
 static void
 test_defname_no_conflict (void)
 {
-       GHashTable *hash;
+       GSList *list = NULL;
        char *name;
 
-       hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_object_unref);
-
-       g_hash_table_insert (hash, "a", _new_connection ("asdfasdfasdfadf"));
-       g_hash_table_insert (hash, "b", _new_connection ("work wifi"));
-       g_hash_table_insert (hash, "c", _new_connection ("random gsm connection"));
+       list = g_slist_append (list, _new_connection ("asdfasdfasdfadf"));
+       list = g_slist_append (list, _new_connection ("work wifi"));
+       list = g_slist_append (list, _new_connection ("random gsm connection"));
 
-       name = nm_settings_utils_get_default_wired_name (hash);
+       name = nm_device_ethernet_utils_get_default_wired_name (list);
        g_assert_cmpstr (name, ==, "Wired connection 1");
 
-       g_hash_table_destroy (hash);
+       g_slist_free_full (list, g_object_unref);
 }
 
 /*******************************************/
@@ -79,19 +72,17 @@ test_defname_no_conflict (void)
 static void
 test_defname_conflict (void)
 {
-       GHashTable *hash;
+       GSList *list = NULL;
        char *name;
 
-       hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_object_unref);
+       list = g_slist_append (list, _new_connection ("asdfasdfasdfadf"));
+       list = g_slist_append (list, _new_connection ("Wired connection 1"));
+       list = g_slist_append (list, _new_connection ("random gsm connection"));
 
-       g_hash_table_insert (hash, "a", _new_connection ("asdfasdfasdfadf"));
-       g_hash_table_insert (hash, "b", _new_connection ("Wired connection 1"));
-       g_hash_table_insert (hash, "c", _new_connection ("random gsm connection"));
-
-       name = nm_settings_utils_get_default_wired_name (hash);
+       name = nm_device_ethernet_utils_get_default_wired_name (list);
        g_assert_cmpstr (name, ==, "Wired connection 2");
 
-       g_hash_table_destroy (hash);
+       g_slist_free_full (list, g_object_unref);
 }
 
 /*******************************************/
@@ -99,23 +90,21 @@ test_defname_conflict (void)
 static void
 test_defname_multiple_conflicts (void)
 {
-       GHashTable *hash;
+       GSList *list = NULL;
        char *name;
 
-       hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) g_object_unref);
-
-       g_hash_table_insert (hash, "a", _new_connection ("random gsm connection"));
-       g_hash_table_insert (hash, "b", _new_connection ("home wifi"));
-       g_hash_table_insert (hash, "c", _new_connection ("Wired connection 1"));
-       g_hash_table_insert (hash, "d", _new_connection ("Wired connection 2"));
-       g_hash_table_insert (hash, "e", _new_connection ("Wired connection 3"));
-       g_hash_table_insert (hash, "f", _new_connection ("work wifi"));
-       g_hash_table_insert (hash, "g", _new_connection ("a vpn"));
+       list = g_slist_append (list, _new_connection ("random gsm connection"));
+       list = g_slist_append (list, _new_connection ("home wifi"));
+       list = g_slist_append (list, _new_connection ("Wired connection 1"));
+       list = g_slist_append (list, _new_connection ("Wired connection 2"));
+       list = g_slist_append (list, _new_connection ("Wired connection 3"));
+       list = g_slist_append (list, _new_connection ("work wifi"));
+       list = g_slist_append (list, _new_connection ("a vpn"));
 
-       name = nm_settings_utils_get_default_wired_name (hash);
+       name = nm_device_ethernet_utils_get_default_wired_name (list);
        g_assert_cmpstr (name, ==, "Wired connection 4");
 
-       g_hash_table_destroy (hash);
+       g_slist_free_full (list, g_object_unref);
 }
 
 /*******************************************/