NetworkManager/libnm-core/nm-setting-ovs-interface.c

393 lines
12 KiB
C
Raw Normal View History

2017-08-01 18:36:34 +02:00
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Copyright 2017 Red Hat, Inc.
*/
#include "nm-default.h"
#include "nm-setting-ovs-interface.h"
#include "nm-connection-private.h"
#include "nm-setting-connection.h"
#include "nm-setting-private.h"
/**
* SECTION:nm-setting-ovs-interface
* @short_description: Describes connection properties for Open vSwitch interfaces.
2017-08-01 18:36:34 +02:00
*
* The #NMSettingOvsInterface object is a #NMSetting subclass that describes properties
* necessary for Open vSwitch interfaces.
2017-08-01 18:36:34 +02:00
**/
enum {
PROP_0,
PROP_TYPE,
LAST_PROP
};
/**
* NMSettingOvsInterface:
*
* Open vSwitch Interface Settings
2017-08-01 18:36:34 +02:00
*/
struct _NMSettingOvsInterface {
NMSetting parent;
char *type;
};
struct _NMSettingOvsInterfaceClass {
NMSettingClass parent;
};
libnm: use NMMetaSettingInfo for tracking setting priority Previously, each (non abstract) NMSetting class had to register its name and priority via _nm_register_setting(). Note, that libnm-core.la already links against "nm-meta-setting.c", which also redundantly keeps track of the settings name and gtype as well. Re-use NMMetaSettingInfo also in libnm-core.la, to track this meta data. The goal is to get rid of private data structures that track meta data about NMSetting classes. In this case, "registered_settings" hash. Instead, we should have one place where all this meta data is tracked. This was, it is also accessible as internal API, which can be useful (for keyfile). Note that NMSettingClass has some overlap with NMMetaSettingInfo. One difference is, that NMMetaSettingInfo is const, while NMSettingClass is only initialized during the class_init() method. Appart from that, it's mostly a matter of taste, whether we attach meta data to NMSettingClass, to NMMetaSettingInfo, or to a static-array indexed by NMMetaSettingType. Note, that previously, _nm_register_setting() was private API. That means, no user could subclass a functioning NMSetting instance. The same is still true: NMMetaSettingInfo is internal API and users cannot access it to create their own NMSetting subclasses. But that is almost desired. libnm is not designed, to be extensible via subclassing, nor is it clear why that would be a useful thing to do. One day, we should remove the NMSetting and NMSettingClass definitions from public headers. Their only use is subclassing the types, which however does not work. While libnm-core was linking already against nm-meta-setting.c, nm_meta_setting_infos was unreferenced. So, this change increases the binary size of libnm and NetworkManager (1032 bytes). Note however that roughly the same information was previously allocated at runtime.
2018-07-27 14:08:14 +02:00
G_DEFINE_TYPE (NMSettingOvsInterface, nm_setting_ovs_interface, NM_TYPE_SETTING)
2017-08-01 18:36:34 +02:00
/*****************************************************************************/
/**
* nm_setting_ovs_interface_get_interface_type:
* @self: the #NMSettingOvsInterface
*
* Returns: the #NMSettingOvsInterface:type property of the setting
*
* Since: 1.10
**/
const char *
nm_setting_ovs_interface_get_interface_type (NMSettingOvsInterface *self)
{
g_return_val_if_fail (NM_IS_SETTING_OVS_INTERFACE (self), NULL);
return self->type;
}
/*****************************************************************************/
int
_nm_setting_ovs_interface_verify_interface_type (NMSettingOvsInterface *self,
NMConnection *connection,
gboolean normalize,
gboolean *out_modified,
GError **error)
{
gboolean has_patch;
const char *type;
const char *connection_type;
gboolean is_ovs_connection_type;
gboolean missing_patch_setting = FALSE;
g_return_val_if_fail (NM_IS_SETTING_OVS_INTERFACE (self), FALSE);
if (normalize) {
g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
nm_assert (self == nm_connection_get_setting_ovs_interface (connection));
} else
g_return_val_if_fail (!connection || NM_IS_CONNECTION (connection), FALSE);
NM_SET_OUT (out_modified, FALSE);
type = self ? self->type : NULL;
if ( type
&& !NM_IN_STRSET (type,
"internal",
"system",
"patch")) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("'%s' is not a valid interface type"),
type);
g_prefix_error (error, "%s.%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME, NM_SETTING_OVS_INTERFACE_TYPE);
return FALSE;
}
if (!connection)
return TRUE;
connection_type = nm_connection_get_connection_type (connection);
if (!connection_type) {
/* if we have an ovs-interface, then the connection type must be either
* "ovs-interface" (for non "system" type) or anything else (for "system" type).
*
* The connection type usually can be normalized based on the presence of a
* base setting. However, in this case, if the connection type is missing,
* that is too complicate to guess what the user wanted.
*
* Require the use to be explicit and fail. */
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("A connection with a '%s' setting needs connection.type explicitly set"),
NM_SETTING_OVS_INTERFACE_SETTING_NAME);
g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_TYPE);
return FALSE;
}
if (nm_streq (connection_type, NM_SETTING_OVS_INTERFACE_SETTING_NAME)) {
if ( type
&& nm_streq (type, "system")) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("A connection of type '%s' cannot have ovs-interface.type \"system\""),
NM_SETTING_OVS_INTERFACE_SETTING_NAME);
g_prefix_error (error, "%s.%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME, NM_SETTING_OVS_INTERFACE_TYPE);
return FALSE;
}
is_ovs_connection_type = TRUE;
} else {
if ( type
&& !nm_streq (type, "system")) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("A connection of type '%s' cannot have an ovs-interface.type \"%s\""),
connection_type,
type);
g_prefix_error (error, "%s.%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME, NM_SETTING_OVS_INTERFACE_TYPE);
return FALSE;
}
is_ovs_connection_type = FALSE;
}
has_patch = !!nm_connection_get_setting_by_name (connection, NM_SETTING_OVS_PATCH_SETTING_NAME);
if (has_patch) {
if (!is_ovs_connection_type) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("A connection with '%s' setting must be of connection.type \"ovs-interface\" but is \"%s\""),
NM_SETTING_OVS_PATCH_SETTING_NAME,
connection_type);
g_prefix_error (error, "%s.%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME, NM_SETTING_OVS_INTERFACE_TYPE);
return FALSE;
}
if (type) {
if (!nm_streq (type, "patch")) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("A connection with '%s' setting needs to be of 'patch' interface type, not '%s'"),
NM_SETTING_OVS_PATCH_SETTING_NAME,
type);
g_prefix_error (error, "%s.%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME, NM_SETTING_OVS_INTERFACE_TYPE);
return FALSE;
}
return TRUE;
}
type = "patch";
goto normalize;
} else {
if (nm_streq0 (type, "patch")) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_MISSING_SETTING,
_("A connection with ovs-interface.type '%s' setting a 'ovs-patch' setting"),
type);
g_prefix_error (error, "%s.%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME, NM_SETTING_OVS_INTERFACE_TYPE);
return FALSE;
}
}
if (type)
return TRUE;
if (is_ovs_connection_type)
type = "internal";
else
type = "system";
normalize:
if (!normalize) {
if (!self) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_MISSING_SETTING,
_("Missing ovs interface setting"));
g_prefix_error (error, "%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME);
} else {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_MISSING_PROPERTY,
_("Missing ovs interface type"));
g_prefix_error (error, "%s.%s: ", NM_SETTING_OVS_INTERFACE_SETTING_NAME, NM_SETTING_OVS_INTERFACE_TYPE);
}
if (missing_patch_setting) {
}
return NM_SETTING_VERIFY_NORMALIZABLE_ERROR;
}
if (!self) {
self = NM_SETTING_OVS_INTERFACE (nm_setting_ovs_interface_new ());
nm_connection_add_setting (connection, NM_SETTING (self));
}
g_object_set (self,
NM_SETTING_OVS_INTERFACE_TYPE, type,
NULL);
NM_SET_OUT (out_modified, TRUE);
return TRUE;
}
2017-08-01 18:36:34 +02:00
static int
verify (NMSetting *setting, NMConnection *connection, GError **error)
{
NMSettingOvsInterface *self = NM_SETTING_OVS_INTERFACE (setting);
if (connection) {
NMSettingConnection *s_con;
2017-10-02 09:03:19 +02:00
const char *slave_type;
2017-08-01 18:36:34 +02:00
s_con = nm_connection_get_setting_connection (connection);
if (!s_con) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_MISSING_SETTING,
_("missing setting"));
g_prefix_error (error, "%s: ", NM_SETTING_CONNECTION_SETTING_NAME);
return FALSE;
}
if (!nm_setting_connection_get_master (s_con)) {
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("A connection with a '%s' setting must have a master."),
NM_SETTING_OVS_INTERFACE_SETTING_NAME);
g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_MASTER);
return FALSE;
}
2017-10-02 09:03:19 +02:00
slave_type = nm_setting_connection_get_slave_type (s_con);
if ( slave_type
&& !nm_streq (slave_type, NM_SETTING_OVS_PORT_SETTING_NAME)) {
2017-10-02 09:03:19 +02:00
g_set_error (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
_("A connection with a '%s' setting must have the slave-type set to '%s'. Instead it is '%s'"),
NM_SETTING_OVS_INTERFACE_SETTING_NAME,
NM_SETTING_OVS_PORT_SETTING_NAME,
slave_type);
g_prefix_error (error, "%s.%s: ", NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_SLAVE_TYPE);
return FALSE;
}
2017-08-01 18:36:34 +02:00
}
return _nm_setting_ovs_interface_verify_interface_type (self,
connection,
FALSE,
NULL,
error);
2017-08-01 18:36:34 +02:00
}
/*****************************************************************************/
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
NMSettingOvsInterface *self = NM_SETTING_OVS_INTERFACE (object);
switch (prop_id) {
case PROP_TYPE:
g_value_set_string (value, self->type);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
NMSettingOvsInterface *self = NM_SETTING_OVS_INTERFACE (object);
switch (prop_id) {
case PROP_TYPE:
g_free (self->type);
self->type = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/*****************************************************************************/
static void
nm_setting_ovs_interface_init (NMSettingOvsInterface *self)
{
}
/**
* nm_setting_ovs_interface_new:
*
* Creates a new #NMSettingOvsInterface object with default values.
*
* Returns: (transfer full): the new empty #NMSettingOvsInterface object
*
* Since: 1.10
**/
NMSetting *
nm_setting_ovs_interface_new (void)
{
return (NMSetting *) g_object_new (NM_TYPE_SETTING_OVS_INTERFACE, NULL);
}
static void
finalize (GObject *object)
{
NMSettingOvsInterface *self = NM_SETTING_OVS_INTERFACE (object);
g_free (self->type);
G_OBJECT_CLASS (nm_setting_ovs_interface_parent_class)->finalize (object);
}
static void
nm_setting_ovs_interface_class_init (NMSettingOvsInterfaceClass *klass)
2017-08-01 18:36:34 +02:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
NMSettingClass *setting_class = NM_SETTING_CLASS (klass);
2017-08-01 18:36:34 +02:00
object_class->set_property = set_property;
object_class->get_property = get_property;
object_class->finalize = finalize;
libnm: rework setting metadata for property handling NMSetting internally already tracked a list of all proper GObject properties and D-Bus-only properties. Rework the tracking of the list, so that: - instead of attaching the data to the GType of the setting via g_type_set_qdata(), it is tracked in a static array indexed by NMMetaSettingType. This allows to find the setting-data by simple pointer arithmetic, instead of taking a look and iterating (like g_type_set_qdata() does). Note, that this is still thread safe, because the static table entry is initialized in the class-init function with _nm_setting_class_commit(). And it only accessed by following a NMSettingClass instance, thus the class constructor already ran (maybe not for all setting classes, but for the particular one that we look up). I think this makes initialization of the metadata simpler to understand. Previously, in a first phase each class would attach the metadata to the GType as setting_property_overrides_quark(). Then during nm_setting_class_ensure_properties() it would merge them and set as setting_properties_quark(). Now, during the first phase, we only incrementally build a properties_override GArray, which we finally hand over during nm_setting_class_commit(). - sort the property infos by name and do binary search. Also expose this meta data types as internal API in nm-setting-private.h. While not accessed yet, it can prove beneficial, to have direct (internal) access to these structures. Also, rename NMSettingProperty to NMSettInfoProperty to use a distinct naming scheme. We already have 40+ subclasses of NMSetting that are called NMSetting*. Likewise, NMMetaSetting* is heavily used already. So, choose a new, distinct name.
2018-07-28 15:26:03 +02:00
setting_class->verify = verify;
2017-08-01 18:36:34 +02:00
/**
* NMSettingOvsInterface:type:
*
* The interface type. Either "internal", or empty.
*
* Since: 1.10
**/
g_object_class_install_property
(object_class, PROP_TYPE,
g_param_spec_string (NM_SETTING_OVS_INTERFACE_TYPE, "", "",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
NM_SETTING_PARAM_INFERRABLE |
G_PARAM_STATIC_STRINGS));
libnm: rework setting metadata for property handling NMSetting internally already tracked a list of all proper GObject properties and D-Bus-only properties. Rework the tracking of the list, so that: - instead of attaching the data to the GType of the setting via g_type_set_qdata(), it is tracked in a static array indexed by NMMetaSettingType. This allows to find the setting-data by simple pointer arithmetic, instead of taking a look and iterating (like g_type_set_qdata() does). Note, that this is still thread safe, because the static table entry is initialized in the class-init function with _nm_setting_class_commit(). And it only accessed by following a NMSettingClass instance, thus the class constructor already ran (maybe not for all setting classes, but for the particular one that we look up). I think this makes initialization of the metadata simpler to understand. Previously, in a first phase each class would attach the metadata to the GType as setting_property_overrides_quark(). Then during nm_setting_class_ensure_properties() it would merge them and set as setting_properties_quark(). Now, during the first phase, we only incrementally build a properties_override GArray, which we finally hand over during nm_setting_class_commit(). - sort the property infos by name and do binary search. Also expose this meta data types as internal API in nm-setting-private.h. While not accessed yet, it can prove beneficial, to have direct (internal) access to these structures. Also, rename NMSettingProperty to NMSettInfoProperty to use a distinct naming scheme. We already have 40+ subclasses of NMSetting that are called NMSetting*. Likewise, NMMetaSetting* is heavily used already. So, choose a new, distinct name.
2018-07-28 15:26:03 +02:00
_nm_setting_class_commit (setting_class, NM_META_SETTING_TYPE_OVS_INTERFACE);
2017-08-01 18:36:34 +02:00
}