From 56e2915536f6958f9103e080539240f9e258ea20 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 5 Sep 2014 13:15:44 -0500 Subject: trivial: spacing and code cleanups in nm-manager.h Remove some dead code and a redundant function. --- src/nm-manager.c | 39 ++++---------------------------------- src/nm-manager.h | 57 +++++++++++++++++++++++++------------------------------- 2 files changed, 29 insertions(+), 67 deletions(-) diff --git a/src/nm-manager.c b/src/nm-manager.c index bee2c55f11..53cf4c26cb 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -496,24 +496,6 @@ nm_manager_get_device_by_path (NMManager *manager, const char *path) return NULL; } -NMDevice * -nm_manager_get_device_by_master (NMManager *manager, const char *master, const char *driver) -{ - GSList *iter; - - g_return_val_if_fail (master != NULL, NULL); - - for (iter = NM_MANAGER_GET_PRIVATE (manager)->devices; iter; iter = iter->next) { - NMDevice *device = NM_DEVICE (iter->data); - - if (!strcmp (nm_device_get_iface (device), master) && - (!driver || !strcmp (nm_device_get_driver (device), driver))) - return device; - } - - return NULL; -} - NMDevice * nm_manager_get_device_by_ifindex (NMManager *manager, int ifindex) { @@ -1864,20 +1846,7 @@ find_device_by_ip_iface (NMManager *self, const gchar *iface) return NULL; } -static NMDevice * -find_device_by_ifindex (NMManager *self, guint32 ifindex) -{ - NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); - GSList *iter; - - for (iter = priv->devices; iter; iter = g_slist_next (iter)) { - NMDevice *candidate = NM_DEVICE (iter->data); - - if (ifindex == nm_device_get_ifindex (candidate)) - return candidate; - } - return NULL; -} +/*******************************************************************/ static void factory_device_added_cb (NMDeviceFactory *factory, @@ -2105,7 +2074,7 @@ platform_link_added (NMManager *self, if (priv->ignore_link_added_cb > 0) return; - if (find_device_by_ifindex (self, ifindex)) + if (nm_manager_get_device_by_ifindex (self, ifindex)) return; /* Try registered device factories */ @@ -2154,7 +2123,7 @@ platform_link_added (NMManager *self, case NM_LINK_TYPE_VLAN: /* Have to find the parent device */ if (nm_platform_vlan_get_info (ifindex, &parent_ifindex, NULL)) { - parent = find_device_by_ifindex (self, parent_ifindex); + parent = nm_manager_get_device_by_ifindex (self, parent_ifindex); if (parent) device = nm_device_vlan_new (plink, parent); else { @@ -2228,7 +2197,7 @@ platform_link_cb (NMPlatform *platform, NMManager *self = NM_MANAGER (user_data); NMDevice *device; - device = find_device_by_ifindex (self, ifindex); + device = nm_manager_get_device_by_ifindex (self, ifindex); if (device) remove_device (self, device, FALSE); break; diff --git a/src/nm-manager.h b/src/nm-manager.h index 2c6c9a04d3..55b0b3fc9f 100644 --- a/src/nm-manager.h +++ b/src/nm-manager.h @@ -92,45 +92,38 @@ typedef struct { GType nm_manager_get_type (void); /* nm_manager_new() should only be used by main.c */ -NMManager *nm_manager_new (NMSettings *settings, - const char *state_file, - gboolean initial_net_enabled, - gboolean initial_wifi_enabled, - gboolean initial_wwan_enabled, - gboolean initial_wimax_enabled, - GError **error); +NMManager * nm_manager_new (NMSettings *settings, + const char *state_file, + gboolean initial_net_enabled, + gboolean initial_wifi_enabled, + gboolean initial_wwan_enabled, + gboolean initial_wimax_enabled, + GError **error); -NMManager *nm_manager_get (void); +NMManager * nm_manager_get (void); -void nm_manager_start (NMManager *manager); - -const GSList *nm_manager_get_active_connections (NMManager *manager); -GSList *nm_manager_get_activatable_connections (NMManager *manager); +void nm_manager_start (NMManager *manager); +NMState nm_manager_get_state (NMManager *manager); +const GSList *nm_manager_get_active_connections (NMManager *manager); +GSList * nm_manager_get_activatable_connections (NMManager *manager); /* Device handling */ -const GSList *nm_manager_get_devices (NMManager *manager); - -NMDevice *nm_manager_get_device_by_master (NMManager *manager, - const char *master, - const char *driver); -NMDevice *nm_manager_get_device_by_ifindex (NMManager *manager, - int ifindex); - -NMActiveConnection *nm_manager_activate_connection (NMManager *manager, - NMConnection *connection, - const char *specific_object, - NMDevice *device, - NMAuthSubject *subject, - GError **error); +const GSList * nm_manager_get_devices (NMManager *manager); -gboolean nm_manager_deactivate_connection (NMManager *manager, - const char *connection_path, - NMDeviceStateReason reason, - GError **error); +NMDevice * nm_manager_get_device_by_ifindex (NMManager *manager, + int ifindex); -/* State handling */ +NMActiveConnection *nm_manager_activate_connection (NMManager *manager, + NMConnection *connection, + const char *specific_object, + NMDevice *device, + NMAuthSubject *subject, + GError **error); -NMState nm_manager_get_state (NMManager *manager); +gboolean nm_manager_deactivate_connection (NMManager *manager, + const char *connection_path, + NMDeviceStateReason reason, + GError **error); #endif /* __NETWORKMANAGER_MANAGER_H__ */ -- cgit v1.2.3 From beb18050b5a00a704a74bff5c10919b44f087122 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 16:11:51 -0500 Subject: settings: create default wired connection from NMDeviceEthernet 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(). --- .gitignore | 2 +- po/POTFILES.in | 2 +- src/Makefile.am | 5 +- src/devices/nm-device-ethernet-utils.c | 53 ++++++++++++ src/devices/nm-device-ethernet-utils.h | 26 ++++++ src/devices/nm-device-ethernet.c | 49 +++++++++++ src/devices/nm-device.c | 8 ++ src/devices/nm-device.h | 4 + src/settings/nm-settings-utils.c | 66 --------------- src/settings/nm-settings-utils.h | 26 ------ src/settings/nm-settings.c | 40 +-------- src/settings/tests/Makefile.am | 24 ------ src/settings/tests/test-wired-defname.c | 139 -------------------------------- src/tests/Makefile.am | 21 ++++- src/tests/test-wired-defname.c | 128 +++++++++++++++++++++++++++++ 15 files changed, 294 insertions(+), 299 deletions(-) create mode 100644 src/devices/nm-device-ethernet-utils.c create mode 100644 src/devices/nm-device-ethernet-utils.h delete mode 100644 src/settings/nm-settings-utils.c delete mode 100644 src/settings/nm-settings-utils.h delete mode 100644 src/settings/tests/Makefile.am delete mode 100644 src/settings/tests/test-wired-defname.c create mode 100644 src/tests/test-wired-defname.c diff --git a/.gitignore b/.gitignore index 41c0d90796..57325ccf51 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/po/POTFILES.in b/po/POTFILES.in index a7e75c43a7..b6be03f126 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index ae4f26768e..494bc8ef3a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/devices/nm-device-ethernet-utils.c b/src/devices/nm-device-ethernet-utils.c new file mode 100644 index 0000000000..631dd34d8b --- /dev/null +++ b/src/devices/nm-device-ethernet-utils.c @@ -0,0 +1,53 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * (C) Copyright 2011 Red Hat, Inc. + */ + +#include +#include +#include + +#include +#include "nm-device-ethernet-utils.h" + +char * +nm_device_ethernet_utils_get_default_wired_name (const GSList *connections) +{ + const GSList *iter; + char *cname = NULL; + int i = 0; + + /* 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 (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; + } + } + + if (found == FALSE) + cname = temp; + } + + return cname; +} + diff --git a/src/devices/nm-device-ethernet-utils.h b/src/devices/nm-device-ethernet-utils.h new file mode 100644 index 0000000000..5848f7e604 --- /dev/null +++ b/src/devices/nm-device-ethernet-utils.h @@ -0,0 +1,26 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * (C) Copyright 2011 Red Hat, Inc. + */ + +#ifndef __NETWORKMANAGER_DEVICE_ETHERNET_UTILS_H__ +#define __NETWORKMANAGER_DEVICE_ETHERNET_UTILS_H__ + +#include + +char *nm_device_ethernet_utils_get_default_wired_name (const GSList *connections); + +#endif /* NETWORKMANAGER_DEVICE_ETHERNET_UTILS_H */ diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index d657c07480..44159320c9 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -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; diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 8bcdadf518..1afb5d118c 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -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, diff --git a/src/devices/nm-device.h b/src/devices/nm-device.h index 3ac3fd6897..28a2ff0be9 100644 --- a/src/devices/nm-device.h +++ b/src/devices/nm-device.h @@ -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 */ diff --git a/src/settings/nm-settings-utils.c b/src/settings/nm-settings-utils.c deleted file mode 100644 index d0e21fbdfb..0000000000 --- a/src/settings/nm-settings-utils.c +++ /dev/null @@ -1,66 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * (C) Copyright 2011 Red Hat, Inc. - */ - -#include -#include -#include - -#include -#include "nm-settings-utils.h" - -char * -nm_settings_utils_get_default_wired_name (GHashTable *connections) -{ - GHashTableIter iter; - NMConnection *connection = NULL; - GSList *names = NULL, *niter; - 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) { - found = TRUE; - g_free (temp); - break; - } - } - - if (found == FALSE) - cname = temp; - } - g_slist_free (names); - - return cname; -} - diff --git a/src/settings/nm-settings-utils.h b/src/settings/nm-settings-utils.h deleted file mode 100644 index 4ab8d44eee..0000000000 --- a/src/settings/nm-settings-utils.h +++ /dev/null @@ -1,26 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * (C) Copyright 2011 Red Hat, Inc. - */ - -#ifndef __NETWORKMANAGER_SETTINGS_UTILS_H__ -#define __NETWORKMANAGER_SETTINGS_UTILS_H__ - -#include - -char *nm_settings_utils_get_default_wired_name (GHashTable *connections); - -#endif /* NM_SETTINGS_UTILS_H */ diff --git a/src/settings/nm-settings.c b/src/settings/nm-settings.c index 2957a21004..0ad85e614f 100644 --- a/src/settings/nm-settings.c +++ b/src/settings/nm-settings.c @@ -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 index 28accaa8bd..0000000000 --- a/src/settings/tests/Makefile.am +++ /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 diff --git a/src/settings/tests/test-wired-defname.c b/src/settings/tests/test-wired-defname.c deleted file mode 100644 index f8944fcd01..0000000000 --- a/src/settings/tests/test-wired-defname.c +++ /dev/null @@ -1,139 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright (C) 2010 Red Hat, Inc. - * - */ - -#include -#include - -#include -#include -#include "nm-settings-utils.h" - -static NMConnection * -_new_connection (const char *id) -{ - NMConnection *a; - NMSetting *setting; - - a = nm_simple_connection_new (); - setting = nm_setting_connection_new (); - g_object_set (setting, NM_SETTING_CONNECTION_ID, id, NULL); - nm_connection_add_setting (a, setting); - return a; -} - -/*******************************************/ - -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); - g_assert_cmpstr (name, ==, "Wired connection 1"); - - g_hash_table_destroy (hash); -} - -/*******************************************/ - -static void -test_defname_no_conflict (void) -{ - GHashTable *hash; - 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")); - - name = nm_settings_utils_get_default_wired_name (hash); - g_assert_cmpstr (name, ==, "Wired connection 1"); - - g_hash_table_destroy (hash); -} - -/*******************************************/ - -static void -test_defname_conflict (void) -{ - GHashTable *hash; - 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 ("Wired connection 1")); - g_hash_table_insert (hash, "c", _new_connection ("random gsm connection")); - - name = nm_settings_utils_get_default_wired_name (hash); - g_assert_cmpstr (name, ==, "Wired connection 2"); - - g_hash_table_destroy (hash); -} - -/*******************************************/ - -static void -test_defname_multiple_conflicts (void) -{ - GHashTable *hash; - 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")); - - name = nm_settings_utils_get_default_wired_name (hash); - g_assert_cmpstr (name, ==, "Wired connection 4"); - - g_hash_table_destroy (hash); -} - -/*******************************************/ - -int -main (int argc, char **argv) -{ -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - - g_test_init (&argc, &argv, NULL); - - g_test_add_func ("/defname/no_connections", test_defname_no_connections); - g_test_add_func ("/defname/no_conflict", test_defname_no_conflict); - g_test_add_func ("/defname/conflict", test_defname_conflict); - g_test_add_func ("/defname/multiple_conflicts", test_defname_multiple_conflicts); - - return g_test_run (); -} - diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index aca9b3cc8f..a5824c96a3 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -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 diff --git a/src/tests/test-wired-defname.c b/src/tests/test-wired-defname.c new file mode 100644 index 0000000000..968971d981 --- /dev/null +++ b/src/tests/test-wired-defname.c @@ -0,0 +1,128 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2010 Red Hat, Inc. + * + */ + +#include +#include + +#include +#include +#include "nm-device-ethernet-utils.h" + +static NMConnection * +_new_connection (const char *id) +{ + NMConnection *a; + NMSetting *setting; + + a = nm_simple_connection_new (); + setting = nm_setting_connection_new (); + g_object_set (setting, NM_SETTING_CONNECTION_ID, id, NULL); + nm_connection_add_setting (a, setting); + return a; +} + +/*******************************************/ + +static void +test_defname_no_connections (void) +{ + char *name; + + name = nm_device_ethernet_utils_get_default_wired_name (NULL); + g_assert_cmpstr (name, ==, "Wired connection 1"); +} + +/*******************************************/ + +static void +test_defname_no_conflict (void) +{ + GSList *list = NULL; + char *name; + + 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_device_ethernet_utils_get_default_wired_name (list); + g_assert_cmpstr (name, ==, "Wired connection 1"); + + g_slist_free_full (list, g_object_unref); +} + +/*******************************************/ + +static void +test_defname_conflict (void) +{ + GSList *list = NULL; + char *name; + + 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")); + + name = nm_device_ethernet_utils_get_default_wired_name (list); + g_assert_cmpstr (name, ==, "Wired connection 2"); + + g_slist_free_full (list, g_object_unref); +} + +/*******************************************/ + +static void +test_defname_multiple_conflicts (void) +{ + GSList *list = NULL; + char *name; + + 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_device_ethernet_utils_get_default_wired_name (list); + g_assert_cmpstr (name, ==, "Wired connection 4"); + + g_slist_free_full (list, g_object_unref); +} + +/*******************************************/ + +int +main (int argc, char **argv) +{ +#if !GLIB_CHECK_VERSION (2, 35, 0) + g_type_init (); +#endif + + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/defname/no_connections", test_defname_no_connections); + g_test_add_func ("/defname/no_conflict", test_defname_no_conflict); + g_test_add_func ("/defname/conflict", test_defname_conflict); + g_test_add_func ("/defname/multiple_conflicts", test_defname_multiple_conflicts); + + return g_test_run (); +} + -- cgit v1.2.3 From 00fe31f5cdbfb68923960b7f75db990035f1cc25 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 5 Sep 2014 14:48:21 -0500 Subject: core: move device factory type function into factory object In preparation for internal device types exposing factories too, it's easier to have the device type that the factory creates be returned by the factory object instead of the plugin, because internal device types don't have plugins. This requires that we create the factory objects earlier, which further requires that any operations that trigger signals must be moved out of each factory's construction path to a separate start() function. --- src/devices/adsl/exports.ver | 1 - src/devices/adsl/nm-atm-manager.c | 31 +++++++------------- src/devices/bluetooth/exports.ver | 1 - src/devices/bluetooth/nm-bluez-manager.c | 33 +++++++++------------ src/devices/nm-device-factory.c | 17 +++++++++++ src/devices/nm-device-factory.h | 35 ++++++++++++++++------- src/devices/team/exports.ver | 1 - src/devices/team/nm-team-factory.c | 40 +++++++------------------- src/devices/wifi/exports.ver | 1 - src/devices/wifi/nm-wifi-factory.c | 15 +++++----- src/devices/wimax/exports.ver | 1 - src/devices/wimax/nm-wimax-factory.c | 15 +++++----- src/devices/wwan/exports.ver | 1 - src/devices/wwan/nm-wwan-factory.c | 25 ++++++++++------ src/nm-manager.c | 49 ++++++++++++++------------------ 15 files changed, 127 insertions(+), 139 deletions(-) diff --git a/src/devices/adsl/exports.ver b/src/devices/adsl/exports.ver index d2c451244b..24cd848ce1 100644 --- a/src/devices/adsl/exports.ver +++ b/src/devices/adsl/exports.ver @@ -1,7 +1,6 @@ { global: nm_device_factory_create; - nm_device_factory_get_device_type; local: *; }; diff --git a/src/devices/adsl/nm-atm-manager.c b/src/devices/adsl/nm-atm-manager.c index e8db3596d7..37be69c6e4 100644 --- a/src/devices/adsl/nm-atm-manager.c +++ b/src/devices/adsl/nm-atm-manager.c @@ -32,7 +32,6 @@ typedef struct { GUdevClient *client; GSList *devices; - guint start_id; } NMAtmManagerPrivate; #define NM_ATM_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_ATM_MANAGER, NMAtmManagerPrivate)) @@ -46,20 +45,12 @@ G_DEFINE_TYPE_EXTENDED (NMAtmManager, nm_atm_manager, G_TYPE_OBJECT, 0, /**************************************************************************/ -#define PLUGIN_TYPE NM_DEVICE_TYPE_ADSL - G_MODULE_EXPORT NMDeviceFactory * nm_device_factory_create (GError **error) { return (NMDeviceFactory *) g_object_new (NM_TYPE_ATM_MANAGER, NULL); } -G_MODULE_EXPORT NMDeviceType -nm_device_factory_get_device_type (void) -{ - return PLUGIN_TYPE; -} - /************************************************************************/ static gboolean @@ -163,9 +154,10 @@ adsl_remove (NMAtmManager *self, GUdevDevice *udev_device) } } -static gboolean -query_devices (NMAtmManager *self) +static void +start (NMDeviceFactory *factory) { + NMAtmManager *self = NM_ATM_MANAGER (factory); NMAtmManagerPrivate *priv = NM_ATM_MANAGER_GET_PRIVATE (self); GUdevEnumerator *enumerator; GList *devices, *iter; @@ -180,8 +172,6 @@ query_devices (NMAtmManager *self) } g_list_free (devices); g_object_unref (enumerator); - - return G_SOURCE_REMOVE; } static void @@ -212,6 +202,12 @@ handle_uevent (GUdevClient *client, adsl_remove (self, device); } +static NMDeviceType +get_device_type (NMDeviceFactory *factory) +{ + return NM_DEVICE_TYPE_ADSL; +} + /*********************************************************************/ static void @@ -222,13 +218,13 @@ nm_atm_manager_init (NMAtmManager *self) priv->client = g_udev_client_new (subsys); g_signal_connect (priv->client, "uevent", G_CALLBACK (handle_uevent), self); - - priv->start_id = g_idle_add ((GSourceFunc) query_devices, self); } static void device_factory_interface_init (NMDeviceFactory *factory_iface) { + factory_iface->get_device_type = get_device_type; + factory_iface->start = start; } static void @@ -242,11 +238,6 @@ dispose (GObject *object) g_signal_handlers_disconnect_by_func (priv->client, handle_uevent, self); g_clear_object (&priv->client); - if (priv->start_id) { - g_source_remove (priv->start_id); - priv->start_id = 0; - } - for (iter = priv->devices; iter; iter = iter->next) g_object_weak_unref (G_OBJECT (iter->data), device_destroyed, self); g_clear_pointer (&priv->devices, g_slist_free); diff --git a/src/devices/bluetooth/exports.ver b/src/devices/bluetooth/exports.ver index d2c451244b..24cd848ce1 100644 --- a/src/devices/bluetooth/exports.ver +++ b/src/devices/bluetooth/exports.ver @@ -1,7 +1,6 @@ { global: nm_device_factory_create; - nm_device_factory_get_device_type; local: *; }; diff --git a/src/devices/bluetooth/nm-bluez-manager.c b/src/devices/bluetooth/nm-bluez-manager.c index 04ffb0a476..f054e5b308 100644 --- a/src/devices/bluetooth/nm-bluez-manager.c +++ b/src/devices/bluetooth/nm-bluez-manager.c @@ -62,20 +62,12 @@ static void check_bluez_and_try_setup (NMBluezManager *self); /**************************************************************************/ -#define PLUGIN_TYPE NM_DEVICE_TYPE_BT - G_MODULE_EXPORT NMDeviceFactory * nm_device_factory_create (GError **error) { return (NMDeviceFactory *) g_object_new (NM_TYPE_BLUEZ_MANAGER, NULL); } -G_MODULE_EXPORT NMDeviceType -nm_device_factory_get_device_type (void) -{ - return PLUGIN_TYPE; -} - /************************************************************************/ struct AsyncData { @@ -368,6 +360,18 @@ check_bluez_and_try_setup (NMBluezManager *self) async_data_pack (self)); } +static void +start (NMDeviceFactory *factory) +{ + check_bluez_and_try_setup (NM_BLUEZ_MANAGER (factory)); +} + +static NMDeviceType +get_device_type (NMDeviceFactory *factory) +{ + return NM_DEVICE_TYPE_BT; +} + /*********************************************************************/ static void @@ -390,16 +394,6 @@ dispose (GObject *object) priv->bluez_version = 0; } -static void -constructed (GObject *object) -{ - NMBluezManager *self = NM_BLUEZ_MANAGER (object); - - G_OBJECT_CLASS (nm_bluez_manager_parent_class)->constructed (object); - - check_bluez_and_try_setup (self); -} - static void nm_bluez_manager_init (NMBluezManager *self) { @@ -412,6 +406,8 @@ nm_bluez_manager_init (NMBluezManager *self) static void device_factory_interface_init (NMDeviceFactory *factory_iface) { + factory_iface->get_device_type = get_device_type; + factory_iface->start = start; } static void @@ -423,6 +419,5 @@ nm_bluez_manager_class_init (NMBluezManagerClass *klass) /* virtual methods */ object_class->dispose = dispose; - object_class->constructed = constructed; } diff --git a/src/devices/nm-device-factory.c b/src/devices/nm-device-factory.c index 5e84531135..2e5f099204 100644 --- a/src/devices/nm-device-factory.c +++ b/src/devices/nm-device-factory.c @@ -91,6 +91,23 @@ nm_device_factory_get_type (void) return device_factory_type; } +NMDeviceType +nm_device_factory_get_device_type (NMDeviceFactory *factory) +{ + g_return_val_if_fail (factory != NULL, NM_DEVICE_TYPE_UNKNOWN); + + return NM_DEVICE_FACTORY_GET_INTERFACE (factory)->get_device_type (factory); +} + +void +nm_device_factory_start (NMDeviceFactory *factory) +{ + g_return_if_fail (factory != NULL); + + if (NM_DEVICE_FACTORY_GET_INTERFACE (factory)->start) + NM_DEVICE_FACTORY_GET_INTERFACE (factory)->start (factory); +} + NMDevice * nm_device_factory_new_link (NMDeviceFactory *factory, NMPlatformLink *plink, diff --git a/src/devices/nm-device-factory.h b/src/devices/nm-device-factory.h index 0f3871021e..e842bd7ec8 100644 --- a/src/devices/nm-device-factory.h +++ b/src/devices/nm-device-factory.h @@ -42,7 +42,7 @@ typedef struct _NMDeviceFactory NMDeviceFactory; * Creates a #GObject that implements the #NMDeviceFactory interface. This * function must not emit any signals or perform any actions that would cause * devices or components to be created immediately. Instead these should be - * deferred to an idle handler. + * deferred to the "start" interface method. * * Returns: the #GObject implementing #NMDeviceFactory or %NULL */ @@ -51,16 +51,6 @@ NMDeviceFactory *nm_device_factory_create (GError **error); /* Should match nm_device_factory_create() */ typedef NMDeviceFactory * (*NMDeviceFactoryCreateFunc) (GError **error); -/** - * nm_device_factory_get_device_type: - * - * Returns: the #NMDeviceType that this plugin creates - */ -NMDeviceType nm_device_factory_get_device_type (void); - -/* Should match nm_device_factory_get_device_type() */ -typedef NMDeviceType (*NMDeviceFactoryDeviceTypeFunc) (void); - /********************************************************************/ #define NM_TYPE_DEVICE_FACTORY (nm_device_factory_get_type ()) @@ -75,6 +65,25 @@ typedef NMDeviceType (*NMDeviceFactoryDeviceTypeFunc) (void); struct _NMDeviceFactory { GTypeInterface g_iface; + /** + * get_device_type: + * @factory: the #NMDeviceFactory + * + * This function MUST be implemented. + * + * Returns: the #NMDeviceType that this plugin creates + */ + NMDeviceType (*get_device_type) (NMDeviceFactory *factory); + + /** + * start: + * @factory: the #NMDeviceFactory + * + * Start the factory and discover any existing devices that the factory + * can manage. + */ + void (*start) (NMDeviceFactory *factory); + /** * new_link: * @factory: the #NMDeviceFactory @@ -141,6 +150,10 @@ struct _NMDeviceFactory { GType nm_device_factory_get_type (void); +NMDeviceType nm_device_factory_get_device_type (NMDeviceFactory *factory); + +void nm_device_factory_start (NMDeviceFactory *factory); + NMDevice * nm_device_factory_new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error); diff --git a/src/devices/team/exports.ver b/src/devices/team/exports.ver index d2c451244b..24cd848ce1 100644 --- a/src/devices/team/exports.ver +++ b/src/devices/team/exports.ver @@ -1,7 +1,6 @@ { global: nm_device_factory_create; - nm_device_factory_get_device_type; local: *; }; diff --git a/src/devices/team/nm-team-factory.c b/src/devices/team/nm-team-factory.c index 49e5945c70..499c37795d 100644 --- a/src/devices/team/nm-team-factory.c +++ b/src/devices/team/nm-team-factory.c @@ -35,12 +35,13 @@ static void device_factory_interface_init (NMDeviceFactory *factory_iface); G_DEFINE_TYPE_EXTENDED (NMTeamFactory, nm_team_factory, G_TYPE_OBJECT, 0, G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init)) -#define NM_TEAM_FACTORY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_TEAM_FACTORY, NMTeamFactoryPrivate)) - -typedef struct { - char dummy; -} NMTeamFactoryPrivate; +/************************************************************************/ +G_MODULE_EXPORT NMDeviceFactory * +nm_device_factory_create (GError **error) +{ + return (NMDeviceFactory *) g_object_new (NM_TYPE_TEAM_FACTORY, NULL); +} /************************************************************************/ @@ -62,20 +63,10 @@ create_virtual_device_for_connection (NMDeviceFactory *factory, return NULL; } -/************************************************************************/ - -#define PLUGIN_TYPE NM_DEVICE_TYPE_TEAM - -G_MODULE_EXPORT NMDeviceFactory * -nm_device_factory_create (GError **error) -{ - return (NMDeviceFactory *) g_object_new (NM_TYPE_TEAM_FACTORY, NULL); -} - -G_MODULE_EXPORT NMDeviceType -nm_device_factory_get_device_type (void) +static NMDeviceType +get_device_type (NMDeviceFactory *factory) { - return PLUGIN_TYPE; + return NM_DEVICE_TYPE_TEAM; } /************************************************************************/ @@ -90,21 +81,10 @@ device_factory_interface_init (NMDeviceFactory *factory_iface) { factory_iface->new_link = new_link; factory_iface->create_virtual_device_for_connection = create_virtual_device_for_connection; -} - -static void -dispose (GObject *object) -{ - /* Chain up to the parent class */ - G_OBJECT_CLASS (nm_team_factory_parent_class)->dispose (object); + factory_iface->get_device_type = get_device_type; } static void nm_team_factory_class_init (NMTeamFactoryClass *klass) { - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - g_type_class_add_private (object_class, sizeof (NMTeamFactoryPrivate)); - - object_class->dispose = dispose; } diff --git a/src/devices/wifi/exports.ver b/src/devices/wifi/exports.ver index d2c451244b..24cd848ce1 100644 --- a/src/devices/wifi/exports.ver +++ b/src/devices/wifi/exports.ver @@ -1,7 +1,6 @@ { global: nm_device_factory_create; - nm_device_factory_get_device_type; local: *; }; diff --git a/src/devices/wifi/nm-wifi-factory.c b/src/devices/wifi/nm-wifi-factory.c index 5d52d66c40..bca588e6eb 100644 --- a/src/devices/wifi/nm-wifi-factory.c +++ b/src/devices/wifi/nm-wifi-factory.c @@ -46,20 +46,12 @@ G_DEFINE_TYPE_EXTENDED (NMWifiFactory, nm_wifi_factory, G_TYPE_OBJECT, 0, /**************************************************************************/ -#define PLUGIN_TYPE NM_DEVICE_TYPE_WIFI - G_MODULE_EXPORT NMDeviceFactory * nm_device_factory_create (GError **error) { return (NMDeviceFactory *) g_object_new (NM_TYPE_WIFI_FACTORY, NULL); } -G_MODULE_EXPORT NMDeviceType -nm_device_factory_get_device_type (void) -{ - return PLUGIN_TYPE; -} - /**************************************************************************/ static NMDevice * @@ -72,10 +64,17 @@ new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) return NULL; } +static NMDeviceType +get_device_type (NMDeviceFactory *factory) +{ + return NM_DEVICE_TYPE_WIFI; +} + static void device_factory_interface_init (NMDeviceFactory *factory_iface) { factory_iface->new_link = new_link; + factory_iface->get_device_type = get_device_type; } static void diff --git a/src/devices/wimax/exports.ver b/src/devices/wimax/exports.ver index d2c451244b..24cd848ce1 100644 --- a/src/devices/wimax/exports.ver +++ b/src/devices/wimax/exports.ver @@ -1,7 +1,6 @@ { global: nm_device_factory_create; - nm_device_factory_get_device_type; local: *; }; diff --git a/src/devices/wimax/nm-wimax-factory.c b/src/devices/wimax/nm-wimax-factory.c index e3aa0be676..9205919842 100644 --- a/src/devices/wimax/nm-wimax-factory.c +++ b/src/devices/wimax/nm-wimax-factory.c @@ -44,20 +44,12 @@ G_DEFINE_TYPE_EXTENDED (NMWimaxFactory, nm_wimax_factory, G_TYPE_OBJECT, 0, /**************************************************************************/ -#define PLUGIN_TYPE NM_DEVICE_TYPE_WIMAX - G_MODULE_EXPORT NMDeviceFactory * nm_device_factory_create (GError **error) { return (NMDeviceFactory *) g_object_new (NM_TYPE_WIMAX_FACTORY, NULL); } -G_MODULE_EXPORT NMDeviceType -nm_device_factory_get_device_type (void) -{ - return PLUGIN_TYPE; -} - /**************************************************************************/ static NMDevice * @@ -72,10 +64,17 @@ new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) return (NMDevice *) nm_device_wimax_new (plink); } +static NMDeviceType +get_device_type (NMDeviceFactory *factory) +{ + return NM_DEVICE_TYPE_WIMAX; +} + static void device_factory_interface_init (NMDeviceFactory *factory_iface) { factory_iface->new_link = new_link; + factory_iface->get_device_type = get_device_type; } static void diff --git a/src/devices/wwan/exports.ver b/src/devices/wwan/exports.ver index d2c451244b..24cd848ce1 100644 --- a/src/devices/wwan/exports.ver +++ b/src/devices/wwan/exports.ver @@ -1,7 +1,6 @@ { global: nm_device_factory_create; - nm_device_factory_get_device_type; local: *; }; diff --git a/src/devices/wwan/nm-wwan-factory.c b/src/devices/wwan/nm-wwan-factory.c index b1e2307e6a..a2f3d3576a 100644 --- a/src/devices/wwan/nm-wwan-factory.c +++ b/src/devices/wwan/nm-wwan-factory.c @@ -43,20 +43,12 @@ typedef struct { /************************************************************************/ -#define PLUGIN_TYPE NM_DEVICE_TYPE_MODEM - G_MODULE_EXPORT NMDeviceFactory * nm_device_factory_create (GError **error) { return (NMDeviceFactory *) g_object_new (NM_TYPE_WWAN_FACTORY, NULL); } -G_MODULE_EXPORT NMDeviceType -nm_device_factory_get_device_type (void) -{ - return PLUGIN_TYPE; -} - /************************************************************************/ static void @@ -93,9 +85,17 @@ modem_added_cb (NMModemManager *manager, g_object_unref (device); } + +static NMDeviceType +get_device_type (NMDeviceFactory *factory) +{ + return NM_DEVICE_TYPE_MODEM; +} + static void -nm_wwan_factory_init (NMWwanFactory *self) +start (NMDeviceFactory *factory) { + NMWwanFactory *self = NM_WWAN_FACTORY (factory); NMWwanFactoryPrivate *priv = NM_WWAN_FACTORY_GET_PRIVATE (self); priv->mm = g_object_new (NM_TYPE_MODEM_MANAGER, NULL); @@ -106,9 +106,16 @@ nm_wwan_factory_init (NMWwanFactory *self) self); } +static void +nm_wwan_factory_init (NMWwanFactory *self) +{ +} + static void device_factory_interface_init (NMDeviceFactory *factory_iface) { + factory_iface->get_device_type = get_device_type; + factory_iface->start = start; } static void diff --git a/src/nm-manager.c b/src/nm-manager.c index 53cf4c26cb..5e89b59cc3 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1873,7 +1873,6 @@ factory_component_added_cb (NMDeviceFactory *factory, #define PLUGIN_PREFIX "libnm-device-plugin-" #define PLUGIN_PATH_TAG "NMManager-plugin-path" -#define PLUGIN_TYPEFUNC_TAG "typefunc" struct read_device_factory_paths_data { char *path; @@ -1980,8 +1979,7 @@ load_device_factories (NMManager *self) GModule *plugin; NMDeviceFactory *factory; NMDeviceFactoryCreateFunc create_func; - NMDeviceFactoryDeviceTypeFunc type_func; - NMDeviceType dev_type; + NMDeviceType plugin_type; const char *found = NULL; GSList *iter; const char *item; @@ -1996,30 +1994,6 @@ load_device_factories (NMManager *self) continue; } - if (!g_module_symbol (plugin, "nm_device_factory_get_device_type", (gpointer) &type_func)) { - nm_log_warn (LOGD_HW, "(%s): failed to find device factory type: %s", item, g_module_error ()); - g_module_close (plugin); - continue; - } - - /* Make sure we don't double-load plugins */ - dev_type = type_func (); - for (iter = priv->factories; iter; iter = iter->next) { - NMDeviceFactoryDeviceTypeFunc loaded_type_func; - - loaded_type_func = g_object_get_data (G_OBJECT (iter->data), PLUGIN_TYPEFUNC_TAG); - if (dev_type == loaded_type_func ()) { - found = g_object_get_data (G_OBJECT (iter->data), PLUGIN_PATH_TAG); - break; - } - } - if (found) { - nm_log_warn (LOGD_HW, "Found multiple device plugins for same type: use '%s' instead of '%s'", - found, g_module_name (plugin)); - g_module_close (plugin); - continue; - } - if (!g_module_symbol (plugin, "nm_device_factory_create", (gpointer) &create_func)) { nm_log_warn (LOGD_HW, "(%s): failed to find device factory creator: %s", item, g_module_error ()); g_module_close (plugin); @@ -2036,6 +2010,21 @@ load_device_factories (NMManager *self) } g_clear_error (&error); + /* Make sure we don't double-register plugins */ + plugin_type = nm_device_factory_get_device_type (factory); + for (iter = priv->factories; iter; iter = iter->next) { + if (plugin_type == nm_device_factory_get_device_type (iter->data)) { + found = g_object_get_data (G_OBJECT (iter->data), PLUGIN_PATH_TAG); + break; + } + } + if (found) { + nm_log_warn (LOGD_HW, "Found multiple device plugins for same type: use '%s' instead of '%s'", + found, g_module_name (plugin)); + g_module_close (plugin); + continue; + } + g_module_make_resident (plugin); priv->factories = g_slist_prepend (priv->factories, factory); @@ -2049,7 +2038,6 @@ load_device_factories (NMManager *self) self); g_object_set_data_full (G_OBJECT (factory), PLUGIN_PATH_TAG, g_strdup (g_module_name (plugin)), g_free); - g_object_set_data (G_OBJECT (factory), PLUGIN_TYPEFUNC_TAG, type_func); nm_log_info (LOGD_HW, "Loaded device plugin: %s", g_module_name (plugin)); }; @@ -4144,6 +4132,7 @@ void nm_manager_start (NMManager *self) { NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); + GSList *iter; guint i; /* Set initial radio enabled/disabled state */ @@ -4174,6 +4163,10 @@ nm_manager_start (NMManager *self) system_unmanaged_devices_changed_cb (priv->settings, NULL, self); system_hostname_changed_cb (priv->settings, NULL, self); + /* Start device factories */ + for (iter = priv->factories; iter; iter = iter->next) + nm_device_factory_start (iter->data); + nm_platform_query_devices (); /* -- cgit v1.2.3 From 560fe126b58c1d01f0211fc8d1a4e9b8cb026a60 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 5 Sep 2014 15:05:40 -0500 Subject: core: split out device factory registration function We'll soon use it for both shared-library-based plugins, and internal device factories. --- src/nm-manager.c | 80 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/src/nm-manager.c b/src/nm-manager.c index 5e89b59cc3..12ffc5be15 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1963,10 +1963,46 @@ NEXT: return result; } +static gboolean +_register_device_factory (NMManager *self, + NMDeviceFactory *factory, + const char *path, + GError **error) +{ + NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); + NMDeviceType ftype; + GSList *iter; + + /* Make sure we don't double-register factories */ + ftype = nm_device_factory_get_device_type (factory); + for (iter = priv->factories; iter; iter = iter->next) { + if (ftype == nm_device_factory_get_device_type (iter->data)) { + g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_INTERNAL, + "multiple plugins for same type (using '%s' instead of '%s')", + (char *) g_object_get_data (G_OBJECT (iter->data), PLUGIN_PATH_TAG), + path); + return FALSE; + } + } + + priv->factories = g_slist_append (priv->factories, factory); + + g_signal_connect (factory, + NM_DEVICE_FACTORY_DEVICE_ADDED, + G_CALLBACK (factory_device_added_cb), + self); + g_signal_connect (factory, + NM_DEVICE_FACTORY_COMPONENT_ADDED, + G_CALLBACK (factory_component_added_cb), + self); + g_object_set_data_full (G_OBJECT (factory), PLUGIN_PATH_TAG, + g_strdup (path), g_free); + return TRUE; +} + static void load_device_factories (NMManager *self) { - NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (self); char **path; char **paths; @@ -1979,9 +2015,6 @@ load_device_factories (NMManager *self) GModule *plugin; NMDeviceFactory *factory; NMDeviceFactoryCreateFunc create_func; - NMDeviceType plugin_type; - const char *found = NULL; - GSList *iter; const char *item; item = strrchr (*path, '/'); @@ -2010,40 +2043,17 @@ load_device_factories (NMManager *self) } g_clear_error (&error); - /* Make sure we don't double-register plugins */ - plugin_type = nm_device_factory_get_device_type (factory); - for (iter = priv->factories; iter; iter = iter->next) { - if (plugin_type == nm_device_factory_get_device_type (iter->data)) { - found = g_object_get_data (G_OBJECT (iter->data), PLUGIN_PATH_TAG); - break; - } - } - if (found) { - nm_log_warn (LOGD_HW, "Found multiple device plugins for same type: use '%s' instead of '%s'", - found, g_module_name (plugin)); + if (_register_device_factory (self, factory, g_module_name (plugin), &error)) { + nm_log_info (LOGD_HW, "Loaded device plugin: %s", g_module_name (plugin)); + g_module_make_resident (plugin); + } else { + nm_log_warn (LOGD_HW, "Loading device plugin failed: %s", error->message); + g_object_unref (factory); g_module_close (plugin); - continue; + g_clear_error (&error); } - - g_module_make_resident (plugin); - priv->factories = g_slist_prepend (priv->factories, factory); - - g_signal_connect (factory, - NM_DEVICE_FACTORY_DEVICE_ADDED, - G_CALLBACK (factory_device_added_cb), - self); - g_signal_connect (factory, - NM_DEVICE_FACTORY_COMPONENT_ADDED, - G_CALLBACK (factory_component_added_cb), - self); - g_object_set_data_full (G_OBJECT (factory), PLUGIN_PATH_TAG, - g_strdup (g_module_name (plugin)), g_free); - - nm_log_info (LOGD_HW, "Loaded device plugin: %s", g_module_name (plugin)); - }; + } g_strfreev (paths); - - priv->factories = g_slist_reverse (priv->factories); } static void -- cgit v1.2.3 From 706b9d2056362b2ec7a84f1d952fcbf00f357b0f Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 11:36:15 -0500 Subject: core: pass parent to device factories when creating devices for connections We'll use it later for InfiniBand and VLAN. --- src/devices/nm-device-factory.c | 3 ++- src/devices/nm-device-factory.h | 2 ++ src/devices/team/nm-team-factory.c | 1 + src/nm-manager.c | 6 ++++-- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/devices/nm-device-factory.c b/src/devices/nm-device-factory.c index 2e5f099204..dc229bd5c8 100644 --- a/src/devices/nm-device-factory.c +++ b/src/devices/nm-device-factory.c @@ -124,6 +124,7 @@ nm_device_factory_new_link (NMDeviceFactory *factory, NMDevice * nm_device_factory_create_virtual_device_for_connection (NMDeviceFactory *factory, NMConnection *connection, + NMDevice *parent, GError **error) { NMDeviceFactory *interface; @@ -134,7 +135,7 @@ nm_device_factory_create_virtual_device_for_connection (NMDeviceFactory *factory interface = NM_DEVICE_FACTORY_GET_INTERFACE (factory); if (interface->create_virtual_device_for_connection) - return interface->create_virtual_device_for_connection (factory, connection, error); + return interface->create_virtual_device_for_connection (factory, connection, parent, error); return NULL; } diff --git a/src/devices/nm-device-factory.h b/src/devices/nm-device-factory.h index e842bd7ec8..dd8a907961 100644 --- a/src/devices/nm-device-factory.h +++ b/src/devices/nm-device-factory.h @@ -118,6 +118,7 @@ struct _NMDeviceFactory { */ NMDevice * (*create_virtual_device_for_connection) (NMDeviceFactory *factory, NMConnection *connection, + NMDevice *parent, GError **error); @@ -160,6 +161,7 @@ NMDevice * nm_device_factory_new_link (NMDeviceFactory *factory, NMDevice * nm_device_factory_create_virtual_device_for_connection (NMDeviceFactory *factory, NMConnection *connection, + NMDevice *parent, GError **error); /* For use by implementations */ diff --git a/src/devices/team/nm-team-factory.c b/src/devices/team/nm-team-factory.c index 499c37795d..6743660015 100644 --- a/src/devices/team/nm-team-factory.c +++ b/src/devices/team/nm-team-factory.c @@ -56,6 +56,7 @@ new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) static NMDevice * create_virtual_device_for_connection (NMDeviceFactory *factory, NMConnection *connection, + NMDevice *parent, GError **error) { if (nm_connection_is_type (connection, NM_SETTING_TEAM_SETTING_NAME)) diff --git a/src/nm-manager.c b/src/nm-manager.c index 12ffc5be15..6dd70eea93 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1063,8 +1063,10 @@ system_create_virtual_device (NMManager *self, NMConnection *connection) device = nm_device_infiniband_new_partition (connection, parent); } else { for (iter = priv->factories; iter; iter = iter->next) { - device = nm_device_factory_create_virtual_device_for_connection (NM_DEVICE_FACTORY (iter->data), connection, &error); - + device = nm_device_factory_create_virtual_device_for_connection (NM_DEVICE_FACTORY (iter->data), + connection, + parent, + &error); if (device || error) { if (device) g_assert_no_error (error); -- cgit v1.2.3 From 38b076de8fc0e531ff80e9c9237960e0b2126467 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 15:12:48 -0500 Subject: build: ensure device source file constructors can be linked and called gcc's linker does not add constructors from object files to the main executable if they are built into a convenience library and then the library is linked to the executable, unless something outside of the object file with the constructor references a symbol from the object file. http://osdir.com/ml/libtool-gnu/2011-06/msg00003.html "Yes, when convenience libraries are used to create a shared library, all the objects are included in the output, when the output is an application they are used like a normal archive library. Either use them to create a shared library or, if creating an application, don't use them, use the objects instead." Further patches will remove all references to the NMDevice subclasses from nm-manager.c, and have each NMDevice subclass register itself with a factory through a constructor. But due to the above issue, we need to somehow ensure the constructor in each nm-device-*.c file gets added to the executable. This is accomplished by explicitly linking each NMDevice subclass' object file into the main executable. (Note that we cannot use -Wl,-whole-archive here because libtool only supports this option for linking a convenience library to a shared library, but not to an executable, and will actively prevent using -whole-archive in LDFLAGS) --- src/Makefile.am | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 494bc8ef3a..f0b22d2e29 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -55,21 +55,35 @@ AM_CPPFLAGS += $(foreach d,$(sort $(dir $(libNetworkManager_la_SOURCES))),-I$(to sbin_PROGRAMS = NetworkManager NetworkManager_SOURCES = \ + $(nm_device_sources) $(nm_device_headers) \ main.c NetworkManager_LDADD = libNetworkManager.la noinst_LTLIBRARIES = libNetworkManager.la +nm_device_sources = \ + $(NULL) + +nm_device_headers = \ + devices/nm-device-bond.h \ + devices/nm-device-bridge.h \ + devices/nm-device-ethernet.h \ + devices/nm-device-gre.h \ + devices/nm-device-infiniband.h \ + devices/nm-device-macvlan.h \ + devices/nm-device-tun.h \ + devices/nm-device-veth.h \ + devices/nm-device-vlan.h \ + devices/nm-device-vxlan.h + nm_sources = \ + $(nm_device_headers) \ devices/nm-device.c \ devices/nm-device.h \ devices/nm-device-bond.c \ - devices/nm-device-bond.h \ devices/nm-device-bridge.c \ - 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 \ @@ -77,21 +91,14 @@ nm_sources = \ devices/nm-device-generic.c \ devices/nm-device-generic.h \ devices/nm-device-gre.c \ - devices/nm-device-gre.h \ devices/nm-device-infiniband.c \ - devices/nm-device-infiniband.h \ devices/nm-device-logging.h \ devices/nm-device-macvlan.c \ - devices/nm-device-macvlan.h \ devices/nm-device-private.h \ devices/nm-device-tun.c \ - devices/nm-device-tun.h \ devices/nm-device-veth.c \ - devices/nm-device-veth.h \ devices/nm-device-vlan.c \ - devices/nm-device-vlan.h \ devices/nm-device-vxlan.c \ - devices/nm-device-vxlan.h \ \ dhcp-manager/nm-dhcp-client.c \ dhcp-manager/nm-dhcp-client.h \ -- cgit v1.2.3 From 0bc1b5138ad8f7a64c74c374c866d615c69887bb Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 5 Sep 2014 15:57:40 -0500 Subject: core: add support for internal device factories --- src/devices/nm-device-factory.c | 15 +++++++++++ src/devices/nm-device-factory.h | 57 ++++++++++++++++++++++++++++++++++++++++- src/nm-manager.c | 48 ++++++++++++++++++++++++---------- src/tests/Makefile.am | 16 ++++++++++++ 4 files changed, 121 insertions(+), 15 deletions(-) diff --git a/src/devices/nm-device-factory.c b/src/devices/nm-device-factory.c index dc229bd5c8..80488aec37 100644 --- a/src/devices/nm-device-factory.c +++ b/src/devices/nm-device-factory.c @@ -27,6 +27,21 @@ enum { }; static guint signals[LAST_SIGNAL] = { 0 }; +static GSList *internal_types = NULL; + +void +_nm_device_factory_internal_register_type (GType factory_type) +{ + g_return_if_fail (g_slist_find (internal_types, GUINT_TO_POINTER (factory_type)) == NULL); + internal_types = g_slist_prepend (internal_types, GUINT_TO_POINTER (factory_type)); +} + +const GSList * +nm_device_factory_get_internal_factory_types (void) +{ + return internal_types; +} + gboolean nm_device_factory_emit_component_added (NMDeviceFactory *factory, GObject *component) { diff --git a/src/devices/nm-device-factory.h b/src/devices/nm-device-factory.h index dd8a907961..ce672b8bfb 100644 --- a/src/devices/nm-device-factory.h +++ b/src/devices/nm-device-factory.h @@ -168,5 +168,60 @@ NMDevice * nm_device_factory_create_virtual_device_for_connection (NMDeviceFacto gboolean nm_device_factory_emit_component_added (NMDeviceFactory *factory, GObject *component); -#endif /* __NETWORKMANAGER_DEVICE_FACTORY_H__ */ +/************************************************************************** + * INTERNAL DEVICE FACTORY FUNCTIONS - devices provided by plugins should + * not use these functions. + **************************************************************************/ + +#define DEFINE_DEVICE_FACTORY_INTERNAL(upper, mixed, lower, dfi_code) \ + DEFINE_DEVICE_FACTORY_INTERNAL_WITH_DEVTYPE(upper, mixed, lower, upper, dfi_code) + +#define DEFINE_DEVICE_FACTORY_INTERNAL_WITH_DEVTYPE(upper, mixed, lower, devtype, dfi_code) \ + typedef GObject NM##mixed##Factory; \ + typedef GObjectClass NM##mixed##FactoryClass; \ + \ + static GType nm_##lower##_factory_get_type (void); \ + static void device_factory_interface_init (NMDeviceFactory *factory_iface); \ + \ + G_DEFINE_TYPE_EXTENDED (NM##mixed##Factory, nm_##lower##_factory, G_TYPE_OBJECT, 0, \ + G_IMPLEMENT_INTERFACE (NM_TYPE_DEVICE_FACTORY, device_factory_interface_init) \ + _nm_device_factory_internal_register_type (g_define_type_id);) \ + \ + /* Use a module constructor to register the factory's GType at load \ + * time, which then calls _nm_device_factory_internal_register_type() \ + * to register the factory's GType with the Manager. \ + */ \ + static void __attribute__((constructor)) \ + register_device_factory_internal_##lower (void) \ + { \ + g_type_init (); \ + g_type_ensure (NM_TYPE_##upper##_FACTORY); \ + } \ + \ + static NMDeviceType \ + get_device_type (NMDeviceFactory *factory) \ + { \ + return NM_DEVICE_TYPE_##devtype; \ + } \ + \ + static void \ + device_factory_interface_init (NMDeviceFactory *factory_iface) \ + { \ + factory_iface->get_device_type = get_device_type; \ + dfi_code \ + } \ + \ + static void \ + nm_##lower##_factory_init (NM##mixed##Factory *self) \ + { \ + } \ + \ + static void \ + nm_##lower##_factory_class_init (NM##mixed##FactoryClass *lower##_class) \ + { \ + } + +void _nm_device_factory_internal_register_type (GType factory_type); +const GSList *nm_device_factory_get_internal_factory_types (void); +#endif /* __NETWORKMANAGER_DEVICE_FACTORY_H__ */ diff --git a/src/nm-manager.c b/src/nm-manager.c index 6dd70eea93..cf7d2f5a9b 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1968,6 +1968,7 @@ NEXT: static gboolean _register_device_factory (NMManager *self, NMDeviceFactory *factory, + gboolean duplicate_check, const char *path, GError **error) { @@ -1975,15 +1976,17 @@ _register_device_factory (NMManager *self, NMDeviceType ftype; GSList *iter; - /* Make sure we don't double-register factories */ - ftype = nm_device_factory_get_device_type (factory); - for (iter = priv->factories; iter; iter = iter->next) { - if (ftype == nm_device_factory_get_device_type (iter->data)) { - g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_INTERNAL, - "multiple plugins for same type (using '%s' instead of '%s')", - (char *) g_object_get_data (G_OBJECT (iter->data), PLUGIN_PATH_TAG), - path); - return FALSE; + if (duplicate_check) { + /* Make sure we don't double-register factories */ + ftype = nm_device_factory_get_device_type (factory); + for (iter = priv->factories; iter; iter = iter->next) { + if (ftype == nm_device_factory_get_device_type (iter->data)) { + g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_INTERNAL, + "multiple plugins for same type (using '%s' instead of '%s')", + (char *) g_object_get_data (G_OBJECT (iter->data), PLUGIN_PATH_TAG), + path); + return FALSE; + } } } @@ -2005,17 +2008,32 @@ _register_device_factory (NMManager *self, static void load_device_factories (NMManager *self) { - char **path; - char **paths; + NMDeviceFactory *factory; + const GSList *iter; + GError *error = NULL; + char **path, **paths; + + /* Register internal factories first */ + for (iter = nm_device_factory_get_internal_factory_types (); iter; iter = iter->next) { + GType ftype = GPOINTER_TO_UINT (iter->data); + + factory = (NMDeviceFactory *) g_object_new (ftype, NULL); + g_assert (factory); + if (_register_device_factory (self, factory, FALSE, "internal", &error)) { + nm_log_dbg (LOGD_HW, "Loaded device plugin: %s", g_type_name (ftype)); + } else { + nm_log_warn (LOGD_HW, "Loading device plugin failed: %s", error->message); + g_object_unref (factory); + g_clear_error (&error); + } + } paths = read_device_factory_paths (); if (!paths) return; for (path = paths; *path; path++) { - GError *error = NULL; GModule *plugin; - NMDeviceFactory *factory; NMDeviceFactoryCreateFunc create_func; const char *item; @@ -2045,7 +2063,7 @@ load_device_factories (NMManager *self) } g_clear_error (&error); - if (_register_device_factory (self, factory, g_module_name (plugin), &error)) { + if (_register_device_factory (self, factory, TRUE, g_module_name (plugin), &error)) { nm_log_info (LOGD_HW, "Loaded device plugin: %s", g_module_name (plugin)); g_module_make_resident (plugin); } else { @@ -2058,6 +2076,8 @@ load_device_factories (NMManager *self) g_strfreev (paths); } +/*******************************************************************/ + static void platform_link_added (NMManager *self, int ifindex, diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index a5824c96a3..221d3f5704 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -95,3 +95,19 @@ TESTS = \ test-general-with-expect \ test-wired-defname + +if ENABLE_TESTS + +check-local: + @for t in ; do \ + # Ensure the device subclass factory registration constructors exist \ + # which could inadvertently break if src/Makefile.am gets changed \ + if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ + echo "Testing device factory symbols... FAILED" ; \ + exit 1 ; \ + fi \ + done + @echo -n "Testing device factory symbols... PASSED" + +endif + -- cgit v1.2.3 From 388e53b1808ad49809392784899809631f372d4c Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 10:57:31 -0500 Subject: veth: port to internal device factory We must port NMDeviceVeth before NMDeviceEthernet because veth is an ethernet subclass and uses symbols from nm-device-ethernet.c. --- src/Makefile.am | 2 +- src/devices/nm-device-veth.c | 36 ++++++++++++++++++++++++------------ src/devices/nm-device-veth.h | 12 ++---------- src/nm-manager.c | 4 ---- src/tests/Makefile.am | 2 +- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index f0b22d2e29..bc961bb524 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -63,6 +63,7 @@ NetworkManager_LDADD = libNetworkManager.la noinst_LTLIBRARIES = libNetworkManager.la nm_device_sources = \ + devices/nm-device-veth.c \ $(NULL) nm_device_headers = \ @@ -96,7 +97,6 @@ nm_sources = \ devices/nm-device-macvlan.c \ devices/nm-device-private.h \ devices/nm-device-tun.c \ - devices/nm-device-veth.c \ devices/nm-device-vlan.c \ devices/nm-device-vxlan.c \ \ diff --git a/src/devices/nm-device-veth.c b/src/devices/nm-device-veth.c index ce417be504..db29c00d2f 100644 --- a/src/devices/nm-device-veth.c +++ b/src/devices/nm-device-veth.c @@ -33,6 +33,7 @@ #include "nm-manager.h" #include "nm-platform.h" #include "nm-dbus-manager.h" +#include "nm-device-factory.h" #include "nm-device-veth-glue.h" @@ -98,18 +99,6 @@ get_peer (NMDeviceVeth *self) /**************************************************************/ -NMDevice * -nm_device_veth_new (NMPlatformLink *platform_device) -{ - g_return_val_if_fail (platform_device != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_VETH, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_TYPE_DESC, "Veth", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_ETHERNET, - NULL); -} - static void nm_device_veth_init (NMDeviceVeth *self) { @@ -170,3 +159,26 @@ nm_device_veth_class_init (NMDeviceVethClass *klass) G_TYPE_FROM_CLASS (klass), &dbus_glib_nm_device_veth_object_info); } + +/*************************************************************/ + +#define NM_TYPE_VETH_FACTORY (nm_veth_factory_get_type ()) +#define NM_VETH_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VETH_FACTORY, NMVethFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + if (plink->type == NM_LINK_TYPE_VETH) { + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_VETH, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_TYPE_DESC, "Veth", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_ETHERNET, + NULL); + } + return NULL; +} + +DEFINE_DEVICE_FACTORY_INTERNAL_WITH_DEVTYPE(VETH, Veth, veth, ETHERNET, \ + factory_iface->new_link = new_link; \ + ) + diff --git a/src/devices/nm-device-veth.h b/src/devices/nm-device-veth.h index 584ec8572c..51e8db45ea 100644 --- a/src/devices/nm-device-veth.h +++ b/src/devices/nm-device-veth.h @@ -36,19 +36,11 @@ G_BEGIN_DECLS #define NM_DEVICE_VETH_PEER "peer" -typedef struct { - NMDeviceEthernet parent; -} NMDeviceVeth; - -typedef struct { - NMDeviceEthernetClass parent; - -} NMDeviceVethClass; +typedef NMDeviceEthernet NMDeviceVeth; +typedef NMDeviceEthernetClass NMDeviceVethClass; GType nm_device_veth_get_type (void); -NMDevice *nm_device_veth_new (NMPlatformLink *platform_device); - G_END_DECLS #endif /* NM_DEVICE_VETH_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index cf7d2f5a9b..57d60d1891 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -45,7 +45,6 @@ #include "nm-device-bridge.h" #include "nm-device-vlan.h" #include "nm-device-generic.h" -#include "nm-device-veth.h" #include "nm-device-tun.h" #include "nm-device-macvlan.h" #include "nm-device-vxlan.h" @@ -2157,9 +2156,6 @@ platform_link_added (NMManager *self, } else nm_log_err (LOGD_HW, "(%s): failed to get VLAN parent ifindex", plink->name); break; - case NM_LINK_TYPE_VETH: - device = nm_device_veth_new (plink); - break; case NM_LINK_TYPE_TUN: case NM_LINK_TYPE_TAP: device = nm_device_tun_new (plink); diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 221d3f5704..fe76e402ad 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in ; do \ + @for t in veth; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 2a55c450bdfdf92ee76002443fc919bf28aabbc9 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 10 Sep 2014 10:59:40 -0500 Subject: ethernet: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-ethernet.c | 36 ++++++++++++++++++++++++------------ src/devices/nm-device-ethernet.h | 14 ++------------ src/nm-manager.c | 4 ---- src/tests/Makefile.am | 2 +- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index bc961bb524..c2880b5cae 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -63,6 +63,7 @@ NetworkManager_LDADD = libNetworkManager.la noinst_LTLIBRARIES = libNetworkManager.la nm_device_sources = \ + devices/nm-device-ethernet.c \ devices/nm-device-veth.c \ $(NULL) @@ -84,7 +85,6 @@ nm_sources = \ devices/nm-device.h \ devices/nm-device-bond.c \ devices/nm-device-bridge.c \ - devices/nm-device-ethernet.c \ devices/nm-device-ethernet-utils.c \ devices/nm-device-ethernet-utils.h \ devices/nm-device-factory.c \ diff --git a/src/devices/nm-device-ethernet.c b/src/devices/nm-device-ethernet.c index 44159320c9..92d0b3dc7d 100644 --- a/src/devices/nm-device-ethernet.c +++ b/src/devices/nm-device-ethernet.c @@ -58,6 +58,7 @@ #include "nm-config.h" #include "nm-device-ethernet-utils.h" #include "nm-connection-provider.h" +#include "nm-device-factory.h" #include "nm-device-ethernet-glue.h" @@ -322,18 +323,6 @@ nm_device_ethernet_init (NMDeviceEthernet *self) priv->s390_options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); } -NMDevice * -nm_device_ethernet_new (NMPlatformLink *platform_device) -{ - g_return_val_if_fail (platform_device != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_ETHERNET, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_TYPE_DESC, "Ethernet", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_ETHERNET, - NULL); -} - static void update_permanent_hw_address (NMDevice *dev) { @@ -1761,3 +1750,26 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass) dbus_g_error_domain_register (NM_ETHERNET_ERROR, NULL, NM_TYPE_ETHERNET_ERROR); } + +/*************************************************************/ + +#define NM_TYPE_ETHERNET_FACTORY (nm_ethernet_factory_get_type ()) +#define NM_ETHERNET_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_ETHERNET_FACTORY, NMEthernetFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + if (plink->type == NM_LINK_TYPE_ETHERNET) { + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_ETHERNET, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_TYPE_DESC, "Ethernet", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_ETHERNET, + NULL); + } + return NULL; +} + +DEFINE_DEVICE_FACTORY_INTERNAL(ETHERNET, Ethernet, ethernet, \ + factory_iface->new_link = new_link; \ + ) + diff --git a/src/devices/nm-device-ethernet.h b/src/devices/nm-device-ethernet.h index c0ad7f2e24..ff21bcad99 100644 --- a/src/devices/nm-device-ethernet.h +++ b/src/devices/nm-device-ethernet.h @@ -45,21 +45,11 @@ typedef enum #define NM_DEVICE_ETHERNET_PERMANENT_HW_ADDRESS "perm-hw-address" #define NM_DEVICE_ETHERNET_SPEED "speed" -typedef struct { - NMDevice parent; -} NMDeviceEthernet; - -typedef struct { - NMDeviceClass parent; - -} NMDeviceEthernetClass; - +typedef NMDevice NMDeviceEthernet; +typedef NMDeviceClass NMDeviceEthernetClass; GType nm_device_ethernet_get_type (void); - -NMDevice *nm_device_ethernet_new (NMPlatformLink *platform_device); - G_END_DECLS #endif /* NM_DEVICE_ETHERNET_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index 57d60d1891..1e278f0dfa 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -39,7 +39,6 @@ #include "nm-dbus-manager.h" #include "nm-vpn-manager.h" #include "nm-device.h" -#include "nm-device-ethernet.h" #include "nm-device-infiniband.h" #include "nm-device-bond.h" #include "nm-device-bridge.h" @@ -2127,9 +2126,6 @@ platform_link_added (NMManager *self, NMDevice *parent; switch (plink->type) { - case NM_LINK_TYPE_ETHERNET: - device = nm_device_ethernet_new (plink); - break; case NM_LINK_TYPE_INFINIBAND: device = nm_device_infiniband_new (plink); break; diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index fe76e402ad..70797b6229 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in veth; do \ + @for t in ethernet veth; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 6d190f92d5c040a9d05e61c8c006246bad1c132f Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Fri, 5 Sep 2014 16:07:43 -0500 Subject: infiniband: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-infiniband.c | 111 +++++++++++++++++++++---------------- src/devices/nm-device-infiniband.h | 15 +---- src/nm-manager.c | 6 -- src/tests/Makefile.am | 2 +- 5 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index c2880b5cae..8477d3caae 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -64,6 +64,7 @@ noinst_LTLIBRARIES = libNetworkManager.la nm_device_sources = \ devices/nm-device-ethernet.c \ + devices/nm-device-infiniband.c \ devices/nm-device-veth.c \ $(NULL) @@ -92,7 +93,6 @@ nm_sources = \ devices/nm-device-generic.c \ devices/nm-device-generic.h \ devices/nm-device-gre.c \ - devices/nm-device-infiniband.c \ devices/nm-device-logging.h \ devices/nm-device-macvlan.c \ devices/nm-device-private.h \ diff --git a/src/devices/nm-device-infiniband.c b/src/devices/nm-device-infiniband.c index bbb12dff92..8101c3e12c 100644 --- a/src/devices/nm-device-infiniband.c +++ b/src/devices/nm-device-infiniband.c @@ -35,6 +35,7 @@ #include "nm-activation-request.h" #include "nm-ip4-config.h" #include "nm-platform.h" +#include "nm-device-factory.h" #include "nm-device-infiniband-glue.h" @@ -69,53 +70,6 @@ nm_device_infiniband_init (NMDeviceInfiniband * self) { } -NMDevice * -nm_device_infiniband_new (NMPlatformLink *platform_device) -{ - g_return_val_if_fail (platform_device != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_INFINIBAND, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_TYPE_DESC, "InfiniBand", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_INFINIBAND, - NULL); -} - -NMDevice * -nm_device_infiniband_new_partition (NMConnection *connection, - NMDevice *parent) -{ - NMSettingInfiniband *s_infiniband; - int p_key, parent_ifindex; - const char *iface; - - g_return_val_if_fail (connection != NULL, NULL); - g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (parent), NULL); - - s_infiniband = nm_connection_get_setting_infiniband (connection); - - iface = nm_setting_infiniband_get_virtual_interface_name (s_infiniband); - g_return_val_if_fail (iface != NULL, NULL); - - parent_ifindex = nm_device_get_ifindex (parent); - p_key = nm_setting_infiniband_get_p_key (s_infiniband); - - if ( !nm_platform_infiniband_partition_add (parent_ifindex, p_key) - && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { - nm_log_warn (LOGD_DEVICE | LOGD_INFINIBAND, "(%s): failed to add InfiniBand P_Key interface for '%s': %s", - iface, nm_connection_get_id (connection), - nm_platform_get_error_msg ()); - return NULL; - } - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_INFINIBAND, - NM_DEVICE_IFACE, iface, - NM_DEVICE_DRIVER, nm_device_get_driver (parent), - NM_DEVICE_TYPE_DESC, "InfiniBand", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_INFINIBAND, - NULL); -} - static guint32 get_generic_capabilities (NMDevice *dev) { @@ -344,3 +298,66 @@ nm_device_infiniband_class_init (NMDeviceInfinibandClass *klass) dbus_g_error_domain_register (NM_INFINIBAND_ERROR, NULL, NM_TYPE_INFINIBAND_ERROR); } + +/*************************************************************/ + +#define NM_TYPE_INFINIBAND_FACTORY (nm_infiniband_factory_get_type ()) +#define NM_INFINIBAND_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_INFINIBAND_FACTORY, NMInfinibandFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + if (plink->type == NM_LINK_TYPE_INFINIBAND) { + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_INFINIBAND, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_TYPE_DESC, "InfiniBand", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_INFINIBAND, + NULL); + } + return NULL; +} + +static NMDevice * +create_virtual_device_for_connection (NMDeviceFactory *factory, + NMConnection *connection, + NMDevice *parent, + GError **error) +{ + NMSettingInfiniband *s_infiniband; + int p_key, parent_ifindex; + const char *iface; + + if (!nm_connection_is_type (connection, NM_SETTING_INFINIBAND_SETTING_NAME)) + return NULL; + + g_return_val_if_fail (NM_IS_DEVICE_INFINIBAND (parent), NULL); + + s_infiniband = nm_connection_get_setting_infiniband (connection); + + iface = nm_setting_infiniband_get_virtual_interface_name (s_infiniband); + g_return_val_if_fail (iface != NULL, NULL); + + parent_ifindex = nm_device_get_ifindex (parent); + p_key = nm_setting_infiniband_get_p_key (s_infiniband); + + if ( !nm_platform_infiniband_partition_add (parent_ifindex, p_key) + && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { + nm_log_warn (LOGD_DEVICE | LOGD_INFINIBAND, "(%s): failed to add InfiniBand P_Key interface for '%s': %s", + iface, nm_connection_get_id (connection), + nm_platform_get_error_msg ()); + return NULL; + } + + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_INFINIBAND, + NM_DEVICE_IFACE, iface, + NM_DEVICE_DRIVER, nm_device_get_driver (parent), + NM_DEVICE_TYPE_DESC, "InfiniBand", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_INFINIBAND, + NULL); +} + +DEFINE_DEVICE_FACTORY_INTERNAL(INFINIBAND, Infiniband, infiniband, \ + factory_iface->new_link = new_link; \ + factory_iface->create_virtual_device_for_connection = create_virtual_device_for_connection; + ) + diff --git a/src/devices/nm-device-infiniband.h b/src/devices/nm-device-infiniband.h index 032fbddf7b..e9b5be92ba 100644 --- a/src/devices/nm-device-infiniband.h +++ b/src/devices/nm-device-infiniband.h @@ -40,22 +40,11 @@ typedef enum { NM_INFINIBAND_ERROR_CONNECTION_INCOMPATIBLE, /*< nick=ConnectionIncompatible >*/ } NMInfinibandError; -typedef struct { - NMDevice parent; -} NMDeviceInfiniband; - -typedef struct { - NMDeviceClass parent; - -} NMDeviceInfinibandClass; - +typedef NMDevice NMDeviceInfiniband; +typedef NMDeviceClass NMDeviceInfinibandClass; GType nm_device_infiniband_get_type (void); -NMDevice *nm_device_infiniband_new (NMPlatformLink *platform_device); -NMDevice *nm_device_infiniband_new_partition (NMConnection *connection, - NMDevice *parent); - G_END_DECLS #endif /* NM_DEVICE_INFINIBAND_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index 1e278f0dfa..4fa996d0d8 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -39,7 +39,6 @@ #include "nm-dbus-manager.h" #include "nm-vpn-manager.h" #include "nm-device.h" -#include "nm-device-infiniband.h" #include "nm-device-bond.h" #include "nm-device-bridge.h" #include "nm-device-vlan.h" @@ -1057,8 +1056,6 @@ system_create_virtual_device (NMManager *self, NMConnection *connection) device = nm_device_bridge_new_for_connection (connection); } else if (nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) { device = nm_device_vlan_new_for_connection (connection, parent); - } else if (nm_connection_is_type (connection, NM_SETTING_INFINIBAND_SETTING_NAME)) { - device = nm_device_infiniband_new_partition (connection, parent); } else { for (iter = priv->factories; iter; iter = iter->next) { device = nm_device_factory_create_virtual_device_for_connection (NM_DEVICE_FACTORY (iter->data), @@ -2126,9 +2123,6 @@ platform_link_added (NMManager *self, NMDevice *parent; switch (plink->type) { - case NM_LINK_TYPE_INFINIBAND: - device = nm_device_infiniband_new (plink); - break; case NM_LINK_TYPE_BOND: device = nm_device_bond_new (plink); break; diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 70797b6229..9dbc44eec2 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in ethernet veth; do \ + @for t in ethernet infiniband veth; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 097eb3a6af1f91324e75db04ac0422099fba1fc0 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 10:12:28 -0500 Subject: bridge: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-bridge.c | 129 +++++++++++++++++++++++------------------ src/devices/nm-device-bridge.h | 14 +---- src/nm-manager.c | 6 -- src/tests/Makefile.am | 2 +- 5 files changed, 78 insertions(+), 75 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 8477d3caae..3e0424de03 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -63,6 +63,7 @@ NetworkManager_LDADD = libNetworkManager.la noinst_LTLIBRARIES = libNetworkManager.la nm_device_sources = \ + devices/nm-device-bridge.c \ devices/nm-device-ethernet.c \ devices/nm-device-infiniband.c \ devices/nm-device-veth.c \ @@ -85,7 +86,6 @@ nm_sources = \ devices/nm-device.c \ devices/nm-device.h \ devices/nm-device-bond.c \ - devices/nm-device-bridge.c \ devices/nm-device-ethernet-utils.c \ devices/nm-device-ethernet-utils.h \ devices/nm-device-factory.c \ diff --git a/src/devices/nm-device-bridge.c b/src/devices/nm-device-bridge.c index 76a9c89bcd..caf62d76d0 100644 --- a/src/devices/nm-device-bridge.c +++ b/src/devices/nm-device-bridge.c @@ -35,6 +35,7 @@ #include "nm-dbus-manager.h" #include "nm-enum-types.h" #include "nm-platform.h" +#include "nm-device-factory.h" #include "nm-device-bridge-glue.h" @@ -404,61 +405,6 @@ release_slave (NMDevice *device, /******************************************************************/ -NMDevice * -nm_device_bridge_new (NMPlatformLink *platform_device) -{ - g_return_val_if_fail (platform_device != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BRIDGE, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_DRIVER, "bridge", - NM_DEVICE_TYPE_DESC, "Bridge", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BRIDGE, - NM_DEVICE_IS_MASTER, TRUE, - NULL); -} - -NMDevice * -nm_device_bridge_new_for_connection (NMConnection *connection) -{ - const char *iface; - NMSettingBridge *s_bridge; - const char *mac_address_str; - guint8 mac_address[NM_UTILS_HWADDR_LEN_MAX]; - - g_return_val_if_fail (connection != NULL, NULL); - - iface = nm_connection_get_interface_name (connection); - g_return_val_if_fail (iface != NULL, NULL); - - s_bridge = nm_connection_get_setting_bridge (connection); - g_return_val_if_fail (s_bridge, NULL); - - mac_address_str = nm_setting_bridge_get_mac_address (s_bridge); - if (mac_address_str) { - if (!nm_utils_hwaddr_aton (mac_address_str, mac_address, ETH_ALEN)) - mac_address_str = NULL; - } - - if ( !nm_platform_bridge_add (iface, - mac_address_str ? mac_address : NULL, - mac_address_str ? ETH_ALEN : 0) - && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { - nm_log_warn (LOGD_DEVICE | LOGD_BRIDGE, "(%s): failed to create bridge master interface for '%s': %s", - iface, nm_connection_get_id (connection), - nm_platform_get_error_msg ()); - return NULL; - } - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BRIDGE, - NM_DEVICE_IFACE, iface, - NM_DEVICE_DRIVER, "bridge", - NM_DEVICE_TYPE_DESC, "Bridge", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BRIDGE, - NM_DEVICE_IS_MASTER, TRUE, - NULL); -} - static void nm_device_bridge_init (NMDeviceBridge * self) { @@ -539,3 +485,76 @@ nm_device_bridge_class_init (NMDeviceBridgeClass *klass) dbus_g_error_domain_register (NM_BRIDGE_ERROR, NULL, NM_TYPE_BRIDGE_ERROR); } + +/*************************************************************/ + +#define NM_TYPE_BRIDGE_FACTORY (nm_bridge_factory_get_type ()) +#define NM_BRIDGE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_BRIDGE_FACTORY, NMBridgeFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + if (plink->type == NM_LINK_TYPE_BRIDGE) { + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BRIDGE, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_DRIVER, "bridge", + NM_DEVICE_TYPE_DESC, "Bridge", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BRIDGE, + NM_DEVICE_IS_MASTER, TRUE, + NULL); + } + return NULL; +} + +static NMDevice * +create_virtual_device_for_connection (NMDeviceFactory *factory, + NMConnection *connection, + NMDevice *parent, + GError **error) +{ + const char *iface; + NMSettingBridge *s_bridge; + const char *mac_address_str; + guint8 mac_address[NM_UTILS_HWADDR_LEN_MAX]; + + if (!nm_connection_is_type (connection, NM_SETTING_BRIDGE_SETTING_NAME)) + return NULL; + + g_return_val_if_fail (connection != NULL, NULL); + + iface = nm_connection_get_interface_name (connection); + g_return_val_if_fail (iface != NULL, NULL); + + s_bridge = nm_connection_get_setting_bridge (connection); + g_return_val_if_fail (s_bridge, NULL); + + mac_address_str = nm_setting_bridge_get_mac_address (s_bridge); + if (mac_address_str) { + if (!nm_utils_hwaddr_aton (mac_address_str, mac_address, ETH_ALEN)) + mac_address_str = NULL; + } + + if ( !nm_platform_bridge_add (iface, + mac_address_str ? mac_address : NULL, + mac_address_str ? ETH_ALEN : 0) + && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { + nm_log_warn (LOGD_DEVICE | LOGD_BRIDGE, "(%s): failed to create bridge master interface for '%s': %s", + iface, nm_connection_get_id (connection), + nm_platform_get_error_msg ()); + return NULL; + } + + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BRIDGE, + NM_DEVICE_IFACE, iface, + NM_DEVICE_DRIVER, "bridge", + NM_DEVICE_TYPE_DESC, "Bridge", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BRIDGE, + NM_DEVICE_IS_MASTER, TRUE, + NULL); +} + +DEFINE_DEVICE_FACTORY_INTERNAL(BRIDGE, Bridge, bridge, + factory_iface->new_link = new_link; + factory_iface->create_virtual_device_for_connection = create_virtual_device_for_connection; + ) + diff --git a/src/devices/nm-device-bridge.h b/src/devices/nm-device-bridge.h index d39d64e559..7c6d7c53af 100644 --- a/src/devices/nm-device-bridge.h +++ b/src/devices/nm-device-bridge.h @@ -42,21 +42,11 @@ typedef enum { #define NM_DEVICE_BRIDGE_SLAVES "slaves" -typedef struct { - NMDevice parent; -} NMDeviceBridge; - -typedef struct { - NMDeviceClass parent; - -} NMDeviceBridgeClass; - +typedef NMDevice NMDeviceBridge; +typedef NMDeviceClass NMDeviceBridgeClass; GType nm_device_bridge_get_type (void); -NMDevice *nm_device_bridge_new (NMPlatformLink *platform_device); -NMDevice *nm_device_bridge_new_for_connection (NMConnection *connection); - G_END_DECLS #endif /* NM_DEVICE_BRIDGE_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index 4fa996d0d8..c360062656 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -40,7 +40,6 @@ #include "nm-vpn-manager.h" #include "nm-device.h" #include "nm-device-bond.h" -#include "nm-device-bridge.h" #include "nm-device-vlan.h" #include "nm-device-generic.h" #include "nm-device-tun.h" @@ -1052,8 +1051,6 @@ system_create_virtual_device (NMManager *self, NMConnection *connection) if (nm_connection_is_type (connection, NM_SETTING_BOND_SETTING_NAME)) { device = nm_device_bond_new_for_connection (connection); - } else if (nm_connection_is_type (connection, NM_SETTING_BRIDGE_SETTING_NAME)) { - device = nm_device_bridge_new_for_connection (connection); } else if (nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) { device = nm_device_vlan_new_for_connection (connection, parent); } else { @@ -2126,9 +2123,6 @@ platform_link_added (NMManager *self, case NM_LINK_TYPE_BOND: device = nm_device_bond_new (plink); break; - case NM_LINK_TYPE_BRIDGE: - device = nm_device_bridge_new (plink); - break; case NM_LINK_TYPE_VLAN: /* Have to find the parent device */ if (nm_platform_vlan_get_info (ifindex, &parent_ifindex, NULL)) { diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 9dbc44eec2..635829db15 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in ethernet infiniband veth; do \ + @for t in bridge ethernet infiniband veth; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 1553b3e2230d55765cf345e2b00c366009091b4e Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 10:15:02 -0500 Subject: bond: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-bond.c | 99 ++++++++++++++++++++++++++------------------ src/devices/nm-device-bond.h | 14 +------ src/nm-manager.c | 8 +--- src/tests/Makefile.am | 2 +- 5 files changed, 63 insertions(+), 62 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 3e0424de03..759d8765c4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -63,6 +63,7 @@ NetworkManager_LDADD = libNetworkManager.la noinst_LTLIBRARIES = libNetworkManager.la nm_device_sources = \ + devices/nm-device-bond.c \ devices/nm-device-bridge.c \ devices/nm-device-ethernet.c \ devices/nm-device-infiniband.c \ @@ -85,7 +86,6 @@ nm_sources = \ $(nm_device_headers) \ devices/nm-device.c \ devices/nm-device.h \ - devices/nm-device-bond.c \ devices/nm-device-ethernet-utils.c \ devices/nm-device-ethernet-utils.h \ devices/nm-device-factory.c \ diff --git a/src/devices/nm-device-bond.c b/src/devices/nm-device-bond.c index d2a0712851..40151d4722 100644 --- a/src/devices/nm-device-bond.c +++ b/src/devices/nm-device-bond.c @@ -36,6 +36,7 @@ #include "nm-dbus-glib-types.h" #include "nm-dbus-manager.h" #include "nm-enum-types.h" +#include "nm-device-factory.h" #include "nm-device-bond-glue.h" @@ -454,47 +455,6 @@ release_slave (NMDevice *device, /******************************************************************/ -NMDevice * -nm_device_bond_new (NMPlatformLink *platform_device) -{ - g_return_val_if_fail (platform_device != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BOND, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_DRIVER, "bonding", - NM_DEVICE_TYPE_DESC, "Bond", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BOND, - NM_DEVICE_IS_MASTER, TRUE, - NULL); -} - -NMDevice * -nm_device_bond_new_for_connection (NMConnection *connection) -{ - const char *iface; - - g_return_val_if_fail (connection != NULL, NULL); - - iface = nm_connection_get_interface_name (connection); - g_return_val_if_fail (iface != NULL, NULL); - - if ( !nm_platform_bond_add (iface) - && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { - nm_log_warn (LOGD_DEVICE | LOGD_BOND, "(%s): failed to create bonding master interface for '%s': %s", - iface, nm_connection_get_id (connection), - nm_platform_get_error_msg ()); - return NULL; - } - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BOND, - NM_DEVICE_IFACE, iface, - NM_DEVICE_DRIVER, "bonding", - NM_DEVICE_TYPE_DESC, "Bond", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BOND, - NM_DEVICE_IS_MASTER, TRUE, - NULL); -} - static void nm_device_bond_init (NMDeviceBond * self) { @@ -575,3 +535,60 @@ nm_device_bond_class_init (NMDeviceBondClass *klass) dbus_g_error_domain_register (NM_BOND_ERROR, NULL, NM_TYPE_BOND_ERROR); } + +/*************************************************************/ + +#define NM_TYPE_BOND_FACTORY (nm_bond_factory_get_type ()) +#define NM_BOND_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_BOND_FACTORY, NMBondFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + if (plink->type == NM_LINK_TYPE_BOND) { + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BOND, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_DRIVER, "bonding", + NM_DEVICE_TYPE_DESC, "Bond", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BOND, + NM_DEVICE_IS_MASTER, TRUE, + NULL); + } + return NULL; +} + +static NMDevice * +create_virtual_device_for_connection (NMDeviceFactory *factory, + NMConnection *connection, + NMDevice *parent, + GError **error) +{ + const char *iface; + + if (!nm_connection_is_type (connection, NM_SETTING_BOND_SETTING_NAME)) + return NULL; + + iface = nm_connection_get_interface_name (connection); + g_return_val_if_fail (iface != NULL, NULL); + + if ( !nm_platform_bond_add (iface) + && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { + nm_log_warn (LOGD_DEVICE | LOGD_BOND, "(%s): failed to create bonding master interface for '%s': %s", + iface, nm_connection_get_id (connection), + nm_platform_get_error_msg ()); + return NULL; + } + + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_BOND, + NM_DEVICE_IFACE, iface, + NM_DEVICE_DRIVER, "bonding", + NM_DEVICE_TYPE_DESC, "Bond", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_BOND, + NM_DEVICE_IS_MASTER, TRUE, + NULL); +} + +DEFINE_DEVICE_FACTORY_INTERNAL(BOND, Bond, bond, + factory_iface->new_link = new_link; + factory_iface->create_virtual_device_for_connection = create_virtual_device_for_connection; + ) + diff --git a/src/devices/nm-device-bond.h b/src/devices/nm-device-bond.h index 216589387a..ed1c023434 100644 --- a/src/devices/nm-device-bond.h +++ b/src/devices/nm-device-bond.h @@ -42,21 +42,11 @@ typedef enum { #define NM_DEVICE_BOND_SLAVES "slaves" -typedef struct { - NMDevice parent; -} NMDeviceBond; - -typedef struct { - NMDeviceClass parent; - -} NMDeviceBondClass; - +typedef NMDevice NMDeviceBond; +typedef NMDeviceClass NMDeviceBondClass; GType nm_device_bond_get_type (void); -NMDevice *nm_device_bond_new (NMPlatformLink *platform_device); -NMDevice *nm_device_bond_new_for_connection (NMConnection *connection); - G_END_DECLS #endif /* NM_DEVICE_BOND_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index c360062656..774abee390 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -39,7 +39,6 @@ #include "nm-dbus-manager.h" #include "nm-vpn-manager.h" #include "nm-device.h" -#include "nm-device-bond.h" #include "nm-device-vlan.h" #include "nm-device-generic.h" #include "nm-device-tun.h" @@ -1049,9 +1048,7 @@ system_create_virtual_device (NMManager *self, NMConnection *connection) nm_owned = !nm_platform_link_exists (iface); - if (nm_connection_is_type (connection, NM_SETTING_BOND_SETTING_NAME)) { - device = nm_device_bond_new_for_connection (connection); - } else if (nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) { + if (nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) { device = nm_device_vlan_new_for_connection (connection, parent); } else { for (iter = priv->factories; iter; iter = iter->next) { @@ -2120,9 +2117,6 @@ platform_link_added (NMManager *self, NMDevice *parent; switch (plink->type) { - case NM_LINK_TYPE_BOND: - device = nm_device_bond_new (plink); - break; case NM_LINK_TYPE_VLAN: /* Have to find the parent device */ if (nm_platform_vlan_get_info (ifindex, &parent_ifindex, NULL)) { diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 635829db15..5761aeb505 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in bridge ethernet infiniband veth; do \ + @for t in bond bridge ethernet infiniband veth; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 15db28e74b4aef9f536a578487eb172e3f132f48 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 10:24:11 -0500 Subject: vlan: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-vlan.c | 174 ++++++++++++++++++++++++++----------------- src/devices/nm-device-vlan.h | 16 +--- src/nm-manager.c | 51 ++++--------- src/tests/Makefile.am | 2 +- 5 files changed, 122 insertions(+), 123 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 759d8765c4..9a9c2b052c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -68,6 +68,7 @@ nm_device_sources = \ devices/nm-device-ethernet.c \ devices/nm-device-infiniband.c \ devices/nm-device-veth.c \ + devices/nm-device-vlan.c \ $(NULL) nm_device_headers = \ @@ -97,7 +98,6 @@ nm_sources = \ devices/nm-device-macvlan.c \ devices/nm-device-private.h \ devices/nm-device-tun.c \ - devices/nm-device-vlan.c \ devices/nm-device-vxlan.c \ \ dhcp-manager/nm-dhcp-client.c \ diff --git a/src/devices/nm-device-vlan.c b/src/devices/nm-device-vlan.c index 2b2e5711e1..71013a99c8 100644 --- a/src/devices/nm-device-vlan.c +++ b/src/devices/nm-device-vlan.c @@ -38,6 +38,8 @@ #include "nm-ip4-config.h" #include "nm-platform.h" #include "nm-utils.h" +#include "nm-device-factory.h" +#include "nm-manager.h" #include "nm-device-vlan-glue.h" @@ -431,75 +433,6 @@ parent_state_changed (NMDevice *parent, /******************************************************************/ -NMDevice * -nm_device_vlan_new (NMPlatformLink *platform_device, NMDevice *parent) -{ - NMDevice *device; - - g_return_val_if_fail (platform_device != NULL, NULL); - g_return_val_if_fail (NM_IS_DEVICE (parent), NULL); - - device = (NMDevice *) g_object_new (NM_TYPE_DEVICE_VLAN, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_VLAN_PARENT, parent, - NM_DEVICE_DRIVER, "8021q", - NM_DEVICE_TYPE_DESC, "VLAN", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_VLAN, - NULL); - if (NM_DEVICE_VLAN_GET_PRIVATE (device)->invalid) { - g_object_unref (device); - device = NULL; - } - - return device; -} - -NMDevice * -nm_device_vlan_new_for_connection (NMConnection *connection, NMDevice *parent) -{ - NMDevice *device; - NMSettingVlan *s_vlan; - char *iface; - - g_return_val_if_fail (connection != NULL, NULL); - g_return_val_if_fail (NM_IS_DEVICE (parent), NULL); - - s_vlan = nm_connection_get_setting_vlan (connection); - g_return_val_if_fail (s_vlan != NULL, NULL); - - iface = g_strdup (nm_connection_get_interface_name (connection)); - if (!iface) { - iface = nm_utils_new_vlan_name (nm_device_get_ip_iface (parent), - nm_setting_vlan_get_id (s_vlan)); - } - - if ( !nm_platform_vlan_add (iface, - nm_device_get_ifindex (parent), - nm_setting_vlan_get_id (s_vlan), - nm_setting_vlan_get_flags (s_vlan)) - && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { - nm_log_warn (LOGD_DEVICE | LOGD_VLAN, "(%s) failed to add VLAN interface for '%s'", - iface, nm_connection_get_id (connection)); - g_free (iface); - return NULL; - } - - device = (NMDevice *) g_object_new (NM_TYPE_DEVICE_VLAN, - NM_DEVICE_IFACE, iface, - NM_DEVICE_VLAN_PARENT, parent, - NM_DEVICE_DRIVER, "8021q", - NM_DEVICE_TYPE_DESC, "VLAN", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_VLAN, - NULL); - g_free (iface); - if (NM_DEVICE_VLAN_GET_PRIVATE (device)->invalid) { - g_object_unref (device); - device = NULL; - } - - return device; -} - static void nm_device_vlan_init (NMDeviceVlan * self) { @@ -661,3 +594,106 @@ nm_device_vlan_class_init (NMDeviceVlanClass *klass) dbus_g_error_domain_register (NM_VLAN_ERROR, NULL, NM_TYPE_VLAN_ERROR); } + +/*************************************************************/ + +#define NM_TYPE_VLAN_FACTORY (nm_vlan_factory_get_type ()) +#define NM_VLAN_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VLAN_FACTORY, NMVlanFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + int parent_ifindex = -1; + NMDevice *parent, *device; + + if (plink->type != NM_LINK_TYPE_VLAN) + return NULL; + + /* Have to find the parent device */ + if (!nm_platform_vlan_get_info (plink->ifindex, &parent_ifindex, NULL)) { + nm_log_err (LOGD_HW, "(%s): failed to get VLAN parent ifindex", plink->name); + return NULL; + } + + parent = nm_manager_get_device_by_ifindex (nm_manager_get (), parent_ifindex); + if (!parent) { + /* If udev signaled the VLAN interface before it signaled + * the VLAN's parent at startup we may not know about the + * parent device yet. But we'll find it on the second pass + * from nm_manager_start(). + */ + nm_log_dbg (LOGD_HW, "(%s): VLAN parent interface unknown", plink->name); + return NULL; + } + + device = (NMDevice *) g_object_new (NM_TYPE_DEVICE_VLAN, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_VLAN_PARENT, parent, + NM_DEVICE_DRIVER, "8021q", + NM_DEVICE_TYPE_DESC, "VLAN", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_VLAN, + NULL); + if (NM_DEVICE_VLAN_GET_PRIVATE (device)->invalid) { + g_object_unref (device); + device = NULL; + } + + return device; +} + +static NMDevice * +create_virtual_device_for_connection (NMDeviceFactory *factory, + NMConnection *connection, + NMDevice *parent, + GError **error) +{ + NMDevice *device; + NMSettingVlan *s_vlan; + char *iface; + + if (!nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) + return NULL; + + g_return_val_if_fail (NM_IS_DEVICE (parent), NULL); + + s_vlan = nm_connection_get_setting_vlan (connection); + g_return_val_if_fail (s_vlan != NULL, NULL); + + iface = g_strdup (nm_connection_get_interface_name (connection)); + if (!iface) { + iface = nm_utils_new_vlan_name (nm_device_get_ip_iface (parent), + nm_setting_vlan_get_id (s_vlan)); + } + + if ( !nm_platform_vlan_add (iface, + nm_device_get_ifindex (parent), + nm_setting_vlan_get_id (s_vlan), + nm_setting_vlan_get_flags (s_vlan)) + && nm_platform_get_error () != NM_PLATFORM_ERROR_EXISTS) { + nm_log_warn (LOGD_DEVICE | LOGD_VLAN, "(%s) failed to add VLAN interface for '%s'", + iface, nm_connection_get_id (connection)); + g_free (iface); + return NULL; + } + + device = (NMDevice *) g_object_new (NM_TYPE_DEVICE_VLAN, + NM_DEVICE_IFACE, iface, + NM_DEVICE_VLAN_PARENT, parent, + NM_DEVICE_DRIVER, "8021q", + NM_DEVICE_TYPE_DESC, "VLAN", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_VLAN, + NULL); + g_free (iface); + if (NM_DEVICE_VLAN_GET_PRIVATE (device)->invalid) { + g_object_unref (device); + device = NULL; + } + + return device; +} + +DEFINE_DEVICE_FACTORY_INTERNAL(VLAN, Vlan, vlan, \ + factory_iface->new_link = new_link; \ + factory_iface->create_virtual_device_for_connection = create_virtual_device_for_connection; + ) + diff --git a/src/devices/nm-device-vlan.h b/src/devices/nm-device-vlan.h index b1d0ed9679..e1618db440 100644 --- a/src/devices/nm-device-vlan.h +++ b/src/devices/nm-device-vlan.h @@ -43,23 +43,11 @@ typedef enum { #define NM_DEVICE_VLAN_PARENT "parent" #define NM_DEVICE_VLAN_ID "vlan-id" -typedef struct { - NMDevice parent; -} NMDeviceVlan; - -typedef struct { - NMDeviceClass parent; - -} NMDeviceVlanClass; - +typedef NMDevice NMDeviceVlan; +typedef NMDeviceClass NMDeviceVlanClass; GType nm_device_vlan_get_type (void); -NMDevice *nm_device_vlan_new (NMPlatformLink *platform_link, - NMDevice *parent); -NMDevice *nm_device_vlan_new_for_connection (NMConnection *connection, - NMDevice *parent); - G_END_DECLS #endif /* NM_DEVICE_VLAN_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index 774abee390..e793867764 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -39,7 +39,6 @@ #include "nm-dbus-manager.h" #include "nm-vpn-manager.h" #include "nm-device.h" -#include "nm-device-vlan.h" #include "nm-device-generic.h" #include "nm-device-tun.h" #include "nm-device-macvlan.h" @@ -1048,24 +1047,20 @@ system_create_virtual_device (NMManager *self, NMConnection *connection) nm_owned = !nm_platform_link_exists (iface); - if (nm_connection_is_type (connection, NM_SETTING_VLAN_SETTING_NAME)) { - device = nm_device_vlan_new_for_connection (connection, parent); - } else { - for (iter = priv->factories; iter; iter = iter->next) { - device = nm_device_factory_create_virtual_device_for_connection (NM_DEVICE_FACTORY (iter->data), - connection, - parent, - &error); - if (device || error) { - if (device) - g_assert_no_error (error); - else { - nm_log_err (LOGD_DEVICE, "(%s) failed to create virtual device: %s", - nm_connection_get_id (connection), error ? error->message : "(unknown error)"); - g_clear_error (&error); - } - break; + for (iter = priv->factories; iter; iter = iter->next) { + device = nm_device_factory_create_virtual_device_for_connection (NM_DEVICE_FACTORY (iter->data), + connection, + parent, + &error); + if (device || error) { + if (device) + g_assert_no_error (error); + else { + nm_log_err (LOGD_DEVICE, "(%s) failed to create virtual device: %s", + nm_connection_get_id (connection), error ? error->message : "(unknown error)"); + g_clear_error (&error); } + break; } } @@ -2113,27 +2108,7 @@ platform_link_added (NMManager *self, return; if (device == NULL) { - int parent_ifindex = -1; - NMDevice *parent; - switch (plink->type) { - case NM_LINK_TYPE_VLAN: - /* Have to find the parent device */ - if (nm_platform_vlan_get_info (ifindex, &parent_ifindex, NULL)) { - parent = nm_manager_get_device_by_ifindex (self, parent_ifindex); - if (parent) - device = nm_device_vlan_new (plink, parent); - else { - /* If udev signaled the VLAN interface before it signaled - * the VLAN's parent at startup we may not know about the - * parent device yet. But we'll find it on the second pass - * from nm_manager_start(). - */ - nm_log_dbg (LOGD_HW, "(%s): VLAN parent interface unknown", plink->name); - } - } else - nm_log_err (LOGD_HW, "(%s): failed to get VLAN parent ifindex", plink->name); - break; case NM_LINK_TYPE_TUN: case NM_LINK_TYPE_TAP: device = nm_device_tun_new (plink); diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 5761aeb505..6a634f82ea 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in bond bridge ethernet infiniband veth; do \ + @for t in bond bridge ethernet infiniband veth vlan; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 1cf7b6d3dd9a1774df4a0bb783a6b07915d60021 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 11:11:19 -0500 Subject: macvlan: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-macvlan.c | 36 ++++++++++++++++++++++++------------ src/devices/nm-device-macvlan.h | 12 ++---------- src/nm-manager.c | 5 ----- src/tests/Makefile.am | 2 +- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 9a9c2b052c..7bd441c568 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -67,6 +67,7 @@ nm_device_sources = \ devices/nm-device-bridge.c \ devices/nm-device-ethernet.c \ devices/nm-device-infiniband.c \ + devices/nm-device-macvlan.c \ devices/nm-device-veth.c \ devices/nm-device-vlan.c \ $(NULL) @@ -95,7 +96,6 @@ nm_sources = \ devices/nm-device-generic.h \ devices/nm-device-gre.c \ devices/nm-device-logging.h \ - devices/nm-device-macvlan.c \ devices/nm-device-private.h \ devices/nm-device-tun.c \ devices/nm-device-vxlan.c \ diff --git a/src/devices/nm-device-macvlan.c b/src/devices/nm-device-macvlan.c index cd49a9b50f..bcd3861774 100644 --- a/src/devices/nm-device-macvlan.c +++ b/src/devices/nm-device-macvlan.c @@ -28,6 +28,7 @@ #include "nm-logging.h" #include "nm-manager.h" #include "nm-platform.h" +#include "nm-device-factory.h" #include "nm-device-macvlan-glue.h" @@ -91,18 +92,6 @@ link_changed (NMDevice *device, NMPlatformLink *info) /**************************************************************/ -NMDevice * -nm_device_macvlan_new (NMPlatformLink *platform_device) -{ - g_return_val_if_fail (platform_device != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_MACVLAN, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_TYPE_DESC, "Macvlan", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, - NULL); -} - static void nm_device_macvlan_init (NMDeviceMacvlan *self) { @@ -179,3 +168,26 @@ nm_device_macvlan_class_init (NMDeviceMacvlanClass *klass) G_TYPE_FROM_CLASS (klass), &dbus_glib_nm_device_macvlan_object_info); } + +/*************************************************************/ + +#define NM_TYPE_MACVLAN_FACTORY (nm_macvlan_factory_get_type ()) +#define NM_MACVLAN_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_MACVLAN_FACTORY, NMMacvlanFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + if (plink->type == NM_LINK_TYPE_MACVLAN || plink->type == NM_LINK_TYPE_MACVTAP) { + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_MACVLAN, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_TYPE_DESC, "Macvlan", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, + NULL); + } + return NULL; +} + +DEFINE_DEVICE_FACTORY_INTERNAL_WITH_DEVTYPE(MACVLAN, Macvlan, macvlan, ETHERNET, \ + factory_iface->new_link = new_link; \ + ) + diff --git a/src/devices/nm-device-macvlan.h b/src/devices/nm-device-macvlan.h index 1e350ce1b3..24f15437c3 100644 --- a/src/devices/nm-device-macvlan.h +++ b/src/devices/nm-device-macvlan.h @@ -38,19 +38,11 @@ G_BEGIN_DECLS #define NM_DEVICE_MACVLAN_MODE "mode" #define NM_DEVICE_MACVLAN_NO_PROMISC "no-promisc" -typedef struct { - NMDeviceGeneric parent; -} NMDeviceMacvlan; - -typedef struct { - NMDeviceGenericClass parent; - -} NMDeviceMacvlanClass; +typedef NMDeviceGeneric NMDeviceMacvlan; +typedef NMDeviceGenericClass NMDeviceMacvlanClass; GType nm_device_macvlan_get_type (void); -NMDevice *nm_device_macvlan_new (NMPlatformLink *platform_device); - G_END_DECLS #endif /* NM_DEVICE_MACVLAN_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index e793867764..e59047dd36 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -41,7 +41,6 @@ #include "nm-device.h" #include "nm-device-generic.h" #include "nm-device-tun.h" -#include "nm-device-macvlan.h" #include "nm-device-vxlan.h" #include "nm-device-gre.h" #include "nm-setting-connection.h" @@ -2113,10 +2112,6 @@ platform_link_added (NMManager *self, case NM_LINK_TYPE_TAP: device = nm_device_tun_new (plink); break; - case NM_LINK_TYPE_MACVLAN: - case NM_LINK_TYPE_MACVTAP: - device = nm_device_macvlan_new (plink); - break; case NM_LINK_TYPE_VXLAN: device = nm_device_vxlan_new (plink); break; diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 6a634f82ea..14ffd0b97d 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in bond bridge ethernet infiniband veth vlan; do \ + @for t in bond bridge ethernet infiniband macvlan veth vlan; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 51aa432283ea0b18833dbc682a0405b7a3b99514 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 11:13:42 -0500 Subject: vxlan: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-vxlan.c | 36 ++++++++++++++++++++++++------------ src/devices/nm-device-vxlan.h | 12 ++---------- src/nm-manager.c | 4 ---- src/tests/Makefile.am | 2 +- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 7bd441c568..8b8cb63647 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -70,6 +70,7 @@ nm_device_sources = \ devices/nm-device-macvlan.c \ devices/nm-device-veth.c \ devices/nm-device-vlan.c \ + devices/nm-device-vxlan.c \ $(NULL) nm_device_headers = \ @@ -98,7 +99,6 @@ nm_sources = \ devices/nm-device-logging.h \ devices/nm-device-private.h \ devices/nm-device-tun.c \ - devices/nm-device-vxlan.c \ \ dhcp-manager/nm-dhcp-client.c \ dhcp-manager/nm-dhcp-client.h \ diff --git a/src/devices/nm-device-vxlan.c b/src/devices/nm-device-vxlan.c index 7c92e2a384..ed6b8fa051 100644 --- a/src/devices/nm-device-vxlan.c +++ b/src/devices/nm-device-vxlan.c @@ -29,6 +29,7 @@ #include "nm-manager.h" #include "nm-platform.h" #include "nm-utils.h" +#include "nm-device-factory.h" #include "nm-device-vxlan-glue.h" @@ -133,18 +134,6 @@ link_changed (NMDevice *device, NMPlatformLink *info) /**************************************************************/ -NMDevice * -nm_device_vxlan_new (NMPlatformLink *platform_device) -{ - g_return_val_if_fail (platform_device != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_VXLAN, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_TYPE_DESC, "Vxlan", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, - NULL); -} - static void nm_device_vxlan_init (NMDeviceVxlan *self) { @@ -357,3 +346,26 @@ nm_device_vxlan_class_init (NMDeviceVxlanClass *klass) G_TYPE_FROM_CLASS (klass), &dbus_glib_nm_device_vxlan_object_info); } + +/*************************************************************/ + +#define NM_TYPE_VXLAN_FACTORY (nm_vxlan_factory_get_type ()) +#define NM_VXLAN_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_VXLAN_FACTORY, NMVxlanFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + if (plink->type == NM_LINK_TYPE_VXLAN) { + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_VXLAN, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_TYPE_DESC, "Vxlan", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, + NULL); + } + return NULL; +} + +DEFINE_DEVICE_FACTORY_INTERNAL_WITH_DEVTYPE(VXLAN, Vxlan, vxlan, GENERIC, \ + factory_iface->new_link = new_link; \ + ) + diff --git a/src/devices/nm-device-vxlan.h b/src/devices/nm-device-vxlan.h index 0e08571066..cc6081e657 100644 --- a/src/devices/nm-device-vxlan.h +++ b/src/devices/nm-device-vxlan.h @@ -51,19 +51,11 @@ G_BEGIN_DECLS #define NM_DEVICE_VXLAN_L2MISS "l2miss" #define NM_DEVICE_VXLAN_L3MISS "l3miss" -typedef struct { - NMDeviceGeneric parent; -} NMDeviceVxlan; - -typedef struct { - NMDeviceGenericClass parent; - -} NMDeviceVxlanClass; +typedef NMDeviceGeneric NMDeviceVxlan; +typedef NMDeviceGenericClass NMDeviceVxlanClass; GType nm_device_vxlan_get_type (void); -NMDevice *nm_device_vxlan_new (NMPlatformLink *platform_device); - G_END_DECLS #endif /* NM_DEVICE_VXLAN_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index e59047dd36..1f955f2ab8 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -41,7 +41,6 @@ #include "nm-device.h" #include "nm-device-generic.h" #include "nm-device-tun.h" -#include "nm-device-vxlan.h" #include "nm-device-gre.h" #include "nm-setting-connection.h" #include "nm-setting-wireless.h" @@ -2112,9 +2111,6 @@ platform_link_added (NMManager *self, case NM_LINK_TYPE_TAP: device = nm_device_tun_new (plink); break; - case NM_LINK_TYPE_VXLAN: - device = nm_device_vxlan_new (plink); - break; case NM_LINK_TYPE_GRE: case NM_LINK_TYPE_GRETAP: device = nm_device_gre_new (plink); diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 14ffd0b97d..b05d7eca76 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in bond bridge ethernet infiniband macvlan veth vlan; do \ + @for t in bond bridge ethernet infiniband macvlan veth vlan vxlan; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 11eb99e9a7fc1bf160143124a3738c6628cdc52f Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 11:17:58 -0500 Subject: gre: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-gre.c | 36 ++++++++++++++++++++++++------------ src/devices/nm-device-gre.h | 12 ++---------- src/nm-manager.c | 5 ----- src/tests/Makefile.am | 2 +- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 8b8cb63647..252d653775 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -66,6 +66,7 @@ nm_device_sources = \ devices/nm-device-bond.c \ devices/nm-device-bridge.c \ devices/nm-device-ethernet.c \ + devices/nm-device-gre.c \ devices/nm-device-infiniband.c \ devices/nm-device-macvlan.c \ devices/nm-device-veth.c \ @@ -95,7 +96,6 @@ nm_sources = \ devices/nm-device-factory.h \ devices/nm-device-generic.c \ devices/nm-device-generic.h \ - devices/nm-device-gre.c \ devices/nm-device-logging.h \ devices/nm-device-private.h \ devices/nm-device-tun.c \ diff --git a/src/devices/nm-device-gre.c b/src/devices/nm-device-gre.c index eded6777a2..2f432c18f0 100644 --- a/src/devices/nm-device-gre.c +++ b/src/devices/nm-device-gre.c @@ -28,6 +28,7 @@ #include "nm-logging.h" #include "nm-manager.h" #include "nm-platform.h" +#include "nm-device-factory.h" #include "nm-device-gre-glue.h" @@ -110,18 +111,6 @@ link_changed (NMDevice *device, NMPlatformLink *info) /**************************************************************/ -NMDevice * -nm_device_gre_new (NMPlatformLink *platform_device) -{ - g_return_val_if_fail (platform_device != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_GRE, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_TYPE_DESC, "Gre", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, - NULL); -} - static void nm_device_gre_init (NMDeviceGre *self) { @@ -269,3 +258,26 @@ nm_device_gre_class_init (NMDeviceGreClass *klass) G_TYPE_FROM_CLASS (klass), &dbus_glib_nm_device_gre_object_info); } + +/*************************************************************/ + +#define NM_TYPE_GRE_FACTORY (nm_gre_factory_get_type ()) +#define NM_GRE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_GRE_FACTORY, NMGreFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + if (plink->type == NM_LINK_TYPE_GRE || plink->type == NM_LINK_TYPE_GRETAP) { + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_GRE, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_TYPE_DESC, "Gre", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, + NULL); + } + return NULL; +} + +DEFINE_DEVICE_FACTORY_INTERNAL_WITH_DEVTYPE(GRE, Gre, gre, ETHERNET, \ + factory_iface->new_link = new_link; \ + ) + diff --git a/src/devices/nm-device-gre.h b/src/devices/nm-device-gre.h index b3f8b984c0..7acff5f7f5 100644 --- a/src/devices/nm-device-gre.h +++ b/src/devices/nm-device-gre.h @@ -45,19 +45,11 @@ G_BEGIN_DECLS #define NM_DEVICE_GRE_TOS "tos" #define NM_DEVICE_GRE_PATH_MTU_DISCOVERY "path-mtu-discovery" -typedef struct { - NMDeviceGeneric parent; -} NMDeviceGre; - -typedef struct { - NMDeviceGenericClass parent; - -} NMDeviceGreClass; +typedef NMDeviceGeneric NMDeviceGre; +typedef NMDeviceGenericClass NMDeviceGreClass; GType nm_device_gre_get_type (void); -NMDevice *nm_device_gre_new (NMPlatformLink *platform_device); - G_END_DECLS #endif /* NM_DEVICE_GRE_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index 1f955f2ab8..f608e5e1e3 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -41,7 +41,6 @@ #include "nm-device.h" #include "nm-device-generic.h" #include "nm-device-tun.h" -#include "nm-device-gre.h" #include "nm-setting-connection.h" #include "nm-setting-wireless.h" #include "nm-setting-vpn.h" @@ -2111,10 +2110,6 @@ platform_link_added (NMManager *self, case NM_LINK_TYPE_TAP: device = nm_device_tun_new (plink); break; - case NM_LINK_TYPE_GRE: - case NM_LINK_TYPE_GRETAP: - device = nm_device_gre_new (plink); - break; case NM_LINK_TYPE_WWAN_ETHERNET: /* WWAN pseudo-ethernet interfaces are handled automatically by diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index b05d7eca76..4bea114786 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in bond bridge ethernet infiniband macvlan veth vlan vxlan; do \ + @for t in bond bridge ethernet gre infiniband macvlan veth vlan vxlan; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3 From 3deb3ff6839273f73895817fbcf443a346f64dae Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Sep 2014 11:50:37 -0500 Subject: tun: port to internal device factory --- src/Makefile.am | 2 +- src/devices/nm-device-tun.c | 53 +++++++++++++++++++++++++++------------------ src/devices/nm-device-tun.h | 12 ++-------- src/nm-manager.c | 5 ----- src/tests/Makefile.am | 2 +- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 252d653775..62a4aa55ef 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -69,6 +69,7 @@ nm_device_sources = \ devices/nm-device-gre.c \ devices/nm-device-infiniband.c \ devices/nm-device-macvlan.c \ + devices/nm-device-tun.c \ devices/nm-device-veth.c \ devices/nm-device-vlan.c \ devices/nm-device-vxlan.c \ @@ -98,7 +99,6 @@ nm_sources = \ devices/nm-device-generic.h \ devices/nm-device-logging.h \ devices/nm-device-private.h \ - devices/nm-device-tun.c \ \ dhcp-manager/nm-dhcp-client.c \ dhcp-manager/nm-dhcp-client.h \ diff --git a/src/devices/nm-device-tun.c b/src/devices/nm-device-tun.c index 030c2e23bf..81442274a0 100644 --- a/src/devices/nm-device-tun.c +++ b/src/devices/nm-device-tun.c @@ -28,6 +28,7 @@ #include "nm-dbus-manager.h" #include "nm-logging.h" #include "nm-platform.h" +#include "nm-device-factory.h" #include "nm-device-tun-glue.h" @@ -110,27 +111,6 @@ delay_tun_get_properties_cb (gpointer user_data) /**************************************************************/ -NMDevice * -nm_device_tun_new (NMPlatformLink *platform_device) -{ - const char *mode = NULL; - - g_return_val_if_fail (platform_device != NULL, NULL); - - if (platform_device->type == NM_LINK_TYPE_TUN) - mode = "tun"; - else if (platform_device->type == NM_LINK_TYPE_TAP) - mode = "tap"; - g_return_val_if_fail (mode != NULL, NULL); - - return (NMDevice *) g_object_new (NM_TYPE_DEVICE_TUN, - NM_DEVICE_PLATFORM_DEVICE, platform_device, - NM_DEVICE_TYPE_DESC, "Tun", - NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, - NM_DEVICE_TUN_MODE, mode, - NULL); -} - static void nm_device_tun_init (NMDeviceTun *self) { @@ -282,3 +262,34 @@ nm_device_tun_class_init (NMDeviceTunClass *klass) G_TYPE_FROM_CLASS (klass), &dbus_glib_nm_device_tun_object_info); } + + +/*************************************************************/ + +#define NM_TYPE_TUN_FACTORY (nm_tun_factory_get_type ()) +#define NM_TUN_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_TUN_FACTORY, NMTunFactory)) + +static NMDevice * +new_link (NMDeviceFactory *factory, NMPlatformLink *plink, GError **error) +{ + const char *mode = NULL; + + if (plink->type == NM_LINK_TYPE_TUN) + mode = "tun"; + else if (plink->type == NM_LINK_TYPE_TAP) + mode = "tap"; + else + return NULL; + + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_TUN, + NM_DEVICE_PLATFORM_DEVICE, plink, + NM_DEVICE_TYPE_DESC, "Tun", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, + NM_DEVICE_TUN_MODE, mode, + NULL); +} + +DEFINE_DEVICE_FACTORY_INTERNAL_WITH_DEVTYPE(TUN, Tun, tun, GENERIC, \ + factory_iface->new_link = new_link; \ + ) + diff --git a/src/devices/nm-device-tun.h b/src/devices/nm-device-tun.h index 0ff1db1eca..393c58018f 100644 --- a/src/devices/nm-device-tun.h +++ b/src/devices/nm-device-tun.h @@ -41,19 +41,11 @@ G_BEGIN_DECLS #define NM_DEVICE_TUN_VNET_HDR "vnet-hdr" #define NM_DEVICE_TUN_MULTI_QUEUE "multi-queue" -typedef struct { - NMDeviceGeneric parent; -} NMDeviceTun; - -typedef struct { - NMDeviceGenericClass parent; - -} NMDeviceTunClass; +typedef NMDeviceGeneric NMDeviceTun; +typedef NMDeviceGenericClass NMDeviceTunClass; GType nm_device_tun_get_type (void); -NMDevice *nm_device_tun_new (NMPlatformLink *platform_device); - G_END_DECLS #endif /* NM_DEVICE_TUN_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index f608e5e1e3..cf0849b5d8 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -40,7 +40,6 @@ #include "nm-vpn-manager.h" #include "nm-device.h" #include "nm-device-generic.h" -#include "nm-device-tun.h" #include "nm-setting-connection.h" #include "nm-setting-wireless.h" #include "nm-setting-vpn.h" @@ -2106,10 +2105,6 @@ platform_link_added (NMManager *self, if (device == NULL) { switch (plink->type) { - case NM_LINK_TYPE_TUN: - case NM_LINK_TYPE_TAP: - device = nm_device_tun_new (plink); - break; case NM_LINK_TYPE_WWAN_ETHERNET: /* WWAN pseudo-ethernet interfaces are handled automatically by diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 4bea114786..07bdee5bcc 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -99,7 +99,7 @@ TESTS = \ if ENABLE_TESTS check-local: - @for t in bond bridge ethernet gre infiniband macvlan veth vlan vxlan; do \ + @for t in bond bridge ethernet gre infiniband macvlan tun veth vlan vxlan; do \ # Ensure the device subclass factory registration constructors exist \ # which could inadvertently break if src/Makefile.am gets changed \ if ! LC_ALL=C nm $(top_builddir)/src/NetworkManager | LC_ALL=C grep -q "register_device_factory_internal_$$t" ; then \ -- cgit v1.2.3