[PATCH 2/4] bridge: Add NMSettingsBridge and extend ifcfg-rh reader to parse bridges



Adds an initial NMSettingBridge class reprsenting a master bridge
configuration. Bridging options are stored in a hashtable.

Extends the ifcfg-rh reader plugin to parse TYPE=bridge files and
create a NMSettingBridge.

The following keywords are currently recognized:
 TYPE="bridge"
 DEVICE="br0"
 BRIDGING_OPTS="key1=val1 key2=val2 ..."
 DELAY=<INT>
 STP="yes"

Signed-off-by: Thomas Graf <tgraf redhat com>
---
 libnm-util/Makefile.am                 |    2 +
 libnm-util/libnm-util.ver              |   11 +
 libnm-util/nm-connection.c             |   18 ++
 libnm-util/nm-connection.h             |    2 +
 libnm-util/nm-setting-bridge.c         |  441 ++++++++++++++++++++++++++++++++
 libnm-util/nm-setting-bridge.h         |   96 +++++++
 src/settings/plugins/ifcfg-rh/reader.c |  138 ++++++++++-
 7 files changed, 704 insertions(+), 4 deletions(-)
 create mode 100644 libnm-util/nm-setting-bridge.c
 create mode 100644 libnm-util/nm-setting-bridge.h

diff --git a/libnm-util/Makefile.am b/libnm-util/Makefile.am
index 1ccb5a6..dd28561 100644
--- a/libnm-util/Makefile.am
+++ b/libnm-util/Makefile.am
@@ -16,6 +16,7 @@ libnm_util_include_HEADERS = 		\
 	nm-setting-8021x.h		\
 	nm-setting-bluetooth.h		\
 	nm-setting-bond.h		\
+	nm-setting-bridge.h		\
 	nm-setting-connection.h		\
 	nm-setting-infiniband.h		\
 	nm-setting-ip4-config.h		\
@@ -47,6 +48,7 @@ libnm_util_la_csources = \
 	nm-setting-8021x.c		\
 	nm-setting-bluetooth.c		\
 	nm-setting-bond.c		\
+	nm-setting-bridge.c		\
 	nm-setting-connection.c		\
 	nm-setting-infiniband.c		\
 	nm-setting-ip4-config.c		\
diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver
index 0e8b30b..a143f29 100644
--- a/libnm-util/libnm-util.ver
+++ b/libnm-util/libnm-util.ver
@@ -17,6 +17,7 @@ global:
 	nm_connection_get_setting_802_1x;
 	nm_connection_get_setting_bluetooth;
 	nm_connection_get_setting_bond;
+	nm_connection_get_setting_bridge;
 	nm_connection_get_setting_by_name;
 	nm_connection_get_setting_cdma;
 	nm_connection_get_setting_connection;
@@ -185,6 +186,16 @@ global:
 	nm_setting_bond_get_type;
 	nm_setting_bond_get_updelay;
 	nm_setting_bond_new;
+	nm_setting_bridge_add_option;
+	nm_setting_bridge_error_get_type;
+	nm_setting_bridge_error_quark;
+	nm_setting_bridge_get_interface_name;
+	nm_setting_bridge_get_num_options;
+	nm_setting_bridge_get_option;
+	nm_setting_bridge_get_option_by_key;
+	nm_setting_bridge_get_type;
+	nm_setting_bridge_new;
+	nm_setting_bridge_remove_option;
 	nm_setting_cdma_error_get_type;
 	nm_setting_cdma_error_quark;
 	nm_setting_cdma_get_number;
diff --git a/libnm-util/nm-connection.c b/libnm-util/nm-connection.c
index a63050e..fcae07c 100644
--- a/libnm-util/nm-connection.c
+++ b/libnm-util/nm-connection.c
@@ -47,6 +47,7 @@
 #include "nm-setting-vpn.h"
 #include "nm-setting-olpc-mesh.h"
 #include "nm-setting-bond.h"
+#include "nm-setting-bridge.h"
 
 #include "nm-setting-serial.h"
 #include "nm-setting-gsm.h"
@@ -1420,6 +1421,23 @@ nm_connection_get_setting_bond (NMConnection *connection)
 }
 
 /**
+ * nm_connection_get_setting_bridge:
+ * @connection: the #NMConnection
+ *
+ * A shortcut to return any #NMSettingBridge the connection might contain.
+ *
+ * Returns: (transfer none): an #NMSettingBridge if the connection contains one, otherwise NULL
+ **/
+NMSettingBridge *
+nm_connection_get_setting_bridge (NMConnection *connection)
+{
+	g_return_val_if_fail (connection != NULL, NULL);
+	g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
+
+	return (NMSettingBridge *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BRIDGE);
+}
+
+/**
  * nm_connection_get_setting_cdma:
  * @connection: the #NMConnection
  *
diff --git a/libnm-util/nm-connection.h b/libnm-util/nm-connection.h
index 28dce12..24ce1df 100644
--- a/libnm-util/nm-connection.h
+++ b/libnm-util/nm-connection.h
@@ -33,6 +33,7 @@
 #include <nm-setting-8021x.h>
 #include <nm-setting-bluetooth.h>
 #include <nm-setting-bond.h>
+#include <nm-setting-bridge.h>
 #include <nm-setting-cdma.h>
 #include <nm-setting-connection.h>
 #include <nm-setting-gsm.h>
@@ -189,6 +190,7 @@ const char *  nm_connection_get_id        (NMConnection *connection);
 NMSetting8021x *           nm_connection_get_setting_802_1x            (NMConnection *connection);
 NMSettingBluetooth *       nm_connection_get_setting_bluetooth         (NMConnection *connection);
 NMSettingBond *            nm_connection_get_setting_bond              (NMConnection *connection);
+NMSettingBridge *          nm_connection_get_setting_bridge            (NMConnection *connection);
 NMSettingCdma *            nm_connection_get_setting_cdma              (NMConnection *connection);
 NMSettingConnection *      nm_connection_get_setting_connection        (NMConnection *connection);
 NMSettingGsm *             nm_connection_get_setting_gsm               (NMConnection *connection);
diff --git a/libnm-util/nm-setting-bridge.c b/libnm-util/nm-setting-bridge.c
new file mode 100644
index 0000000..78f96a8
--- /dev/null
+++ b/libnm-util/nm-setting-bridge.c
@@ -0,0 +1,441 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+
+/*
+ * Thomas Graf <tgraf redhat com>
+ *
+ * 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.
+ *
+ * (C) Copyright 2011-2012 Red Hat, Inc.
+ */
+
+#include <string.h>
+#include <ctype.h>
+#include <dbus/dbus-glib.h>
+
+#include "nm-setting-bridge.h"
+#include "nm-param-spec-specialized.h"
+#include "nm-utils.h"
+#include "nm-utils-private.h"
+#include "nm-dbus-glib-types.h"
+
+/**
+ * SECTION:nm-setting-bridge
+ * @short_description: Describes connection properties for bridges
+ * @include: nm-setting-bridge.h
+ *
+ * The #NMSettingBridge object is a #NMSetting subclass that describes properties
+ * necessary for bridge connections.
+ **/
+
+/**
+ * nm_setting_bridge_error_quark:
+ *
+ * Registers an error quark for #NMSettingBridge if necessary.
+ *
+ * Returns: the error quark used for #NMSettingBridge errors.
+ **/
+GQuark
+nm_setting_bridge_error_quark (void)
+{
+	static GQuark quark;
+
+	if (G_UNLIKELY (!quark))
+		quark = g_quark_from_static_string ("nm-setting-bridge-error-quark");
+	return quark;
+}
+
+/* This should really be standard. */
+#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
+
+GType
+nm_setting_bridge_error_get_type (void)
+{
+	static GType etype = 0;
+
+	if (etype == 0) {
+		static const GEnumValue values[] = {
+			/* Unknown error. */
+			ENUM_ENTRY (NM_SETTING_BRIDGE_ERROR_UNKNOWN, "UnknownError"),
+			/* The specified property was invalid. */
+			ENUM_ENTRY (NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY, "InvalidProperty"),
+			/* The specified property was missing and is required. */
+			ENUM_ENTRY (NM_SETTING_BRIDGE_ERROR_MISSING_PROPERTY, "MissingProperty"),
+			{ 0, 0, 0 }
+		};
+
+		etype = g_enum_register_static ("NMSettingBridgeError", values);
+	}
+
+	return etype;
+}
+
+
+G_DEFINE_TYPE (NMSettingBridge, nm_setting_bridge, NM_TYPE_SETTING)
+
+#define NM_SETTING_BRIDGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BRIDGE, NMSettingBridgePrivate))
+
+typedef struct {
+	char *	interface_name;
+	GHashTable *options;
+} NMSettingBridgePrivate;
+
+enum {
+	PROP_0,
+	PROP_INTERFACE_NAME,
+	PROP_OPTIONS,
+	LAST_PROP
+};
+
+static const char *valid_opts[] = {
+	"delay", "stp",
+	NULL
+};
+
+/**
+ * nm_setting_bridge_new:
+ *
+ * Creates a new #NMSettingBridge object with default values.
+ *
+ * Returns: (transfer full): the new empty #NMSettingBridge object
+ **/
+NMSetting *
+nm_setting_bridge_new (void)
+{
+	return (NMSetting *) g_object_new (NM_TYPE_SETTING_BRIDGE, NULL);
+}
+
+/**
+ * nm_setting_bridge_get_interface_name
+ * @setting: the #NMSettingBridge
+ *
+ * Returns: the #NMSettingBridge:interface-name property of the setting
+ **/
+const char *
+nm_setting_bridge_get_interface_name (NMSettingBridge *setting)
+{
+	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
+
+	return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->interface_name;
+}
+
+/**
+ * nm_setting_bridge_get_num_options:
+ * @setting: the #NMSettingBridge
+ *
+ * Returns the number of options that should be set for this bridge when it
+ * is activated. This can be used to retrieve each option individually
+ * using nm_setting_bridge_get_option().
+ *
+ * Returns: the number of bridging options
+ **/
+guint32
+nm_setting_bridge_get_num_options (NMSettingBridge *setting)
+{
+	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
+
+	return g_hash_table_size (NM_SETTING_BRIDGE_GET_PRIVATE (setting)->options);
+}
+
+/**
+ * nm_setting_bridge_get_option:
+ * @setting: the #NMSettingBridge
+ * @idx: index of the desired option, from 0 to
+ * nm_setting_bridge_get_num_options() - 1
+ * @out_key: (out): on return, the key name of the bridging option; this
+ * value is owned by the setting and should not be modified
+ * @out_value: (out): on return, the value of the key of the bridging
+ * option; this value is owned by the setting and should not be modified
+ *
+ * Given an index, return the value of the bridging option at that index.  indexes
+ * are *not* guaranteed to be static across modifications to options done by
+ * nm_setting_bridge_add_option() and nm_setting_bridge_remove_option(),
+ * and should not be used to refer to options except for short periods of time
+ * such as during option iteration.
+ *
+ * Returns: %TRUE on success if the index was valid and an option was found,
+ * %FALSE if the index was invalid (ie, greater than the number of options
+ * currently held by the setting)
+ **/
+gboolean
+nm_setting_bridge_get_option (NMSettingBridge *setting,
+                              guint32 idx,
+                              const char **out_key,
+                              const char **out_value)
+{
+	NMSettingBridgePrivate *priv;
+	guint32 num_keys;
+	GList *keys;
+	const char *_key = NULL, *_value = NULL;
+
+	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), FALSE);
+
+	priv = NM_SETTING_BRIDGE_GET_PRIVATE (setting);
+
+	num_keys = nm_setting_bridge_get_num_options (setting);
+	g_return_val_if_fail (idx < num_keys, FALSE);
+
+	keys = g_hash_table_get_keys (priv->options);
+	_key = g_list_nth_data (keys, idx);
+	_value = g_hash_table_lookup (priv->options, _key);
+
+	if (out_key)
+		*out_key = _key;
+	if (out_value)
+		*out_value = _value;
+	return TRUE;
+}
+
+/**
+ * nm_setting_bridge_get_option_by_key:
+ * @setting: the #NMSettingBridge
+ * @key: the key for which to retrieve the value
+ *
+ * Returns the value associated with the bridging option specified by
+ * @key, if it exists.
+ *
+ * Returns: the value, or NULL if the key/value pair was never added to the
+ * setting; the value is owned by the setting and must not be modified
+ **/
+const char *
+nm_setting_bridge_get_option_by_key (NMSettingBridge *setting,
+                                     const char *key)
+{
+	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), NULL);
+	g_return_val_if_fail (key != NULL, NULL);
+	g_return_val_if_fail (strlen (key), NULL);
+
+	return g_hash_table_lookup (NM_SETTING_BRIDGE_GET_PRIVATE (setting)->options, key);
+}
+
+/**
+ * nm_setting_bridge_add_options:
+ * @setting: the #NMSettingBridge
+ * @key: key name for the option
+ * @value: value for the option
+ *
+ * Add an option to the table.  The option is compared to an internal list
+ * of allowed options.  Key names may contain only alphanumeric characters
+ * (ie [a-zA-Z0-9]).  Adding a new key replaces any existing key/value pair that
+ * may already exist.
+ *
+ * Returns: %TRUE if the option was valid and was added to the internal option
+ * list, %FALSE if it was not.
+ **/
+gboolean nm_setting_bridge_add_option (NMSettingBridge *setting,
+                                       const char *key,
+                                       const char *value)
+{
+	size_t value_len;
+
+	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), FALSE);
+	g_return_val_if_fail (key != NULL, FALSE);
+	g_return_val_if_fail (strlen (key), FALSE);
+	g_return_val_if_fail (_nm_utils_string_in_list (key, valid_opts), FALSE);
+	g_return_val_if_fail (value != NULL, FALSE);
+
+	value_len = strlen (value);
+	g_return_val_if_fail (value_len > 0 && value_len < 200, FALSE);
+
+	g_hash_table_insert (NM_SETTING_BRIDGE_GET_PRIVATE (setting)->options,
+	                     g_strdup (key), g_strdup (value));
+	return TRUE;
+}
+
+/**
+ * nm_setting_bridge_remove_options:
+ * @setting: the #NMSettingBridge
+ * @key: key name for the option to remove
+ *
+ * Remove the bridging option referenced by @key from the internal option
+ * list.
+ *
+ * Returns: %TRUE if the option was found and removed from the internal option
+ * list, %FALSE if it was not.
+ **/
+gboolean
+nm_setting_bridge_remove_option (NMSettingBridge *setting,
+                                 const char *key)
+{
+	g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), FALSE);
+	g_return_val_if_fail (key != NULL, FALSE);
+	g_return_val_if_fail (strlen (key), FALSE);
+
+	return g_hash_table_remove (NM_SETTING_BRIDGE_GET_PRIVATE (setting)->options, key);
+}
+
+static gboolean
+verify (NMSetting *setting, GSList *all_settings, GError **error)
+{
+	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (setting);
+	GHashTableIter iter;
+	const char *key, *value;
+
+	if (!priv->interface_name || !strlen(priv->interface_name)) {
+		g_set_error (error,
+		             NM_SETTING_BRIDGE_ERROR,
+		             NM_SETTING_BRIDGE_ERROR_MISSING_PROPERTY,
+		             NM_SETTING_BRIDGE_INTERFACE_NAME);
+		return FALSE;
+	}
+
+	if (!nm_utils_iface_name_valid (priv->interface_name)) {
+		g_set_error (error,
+		             NM_SETTING_BRIDGE_ERROR,
+		             NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
+		             NM_SETTING_BRIDGE_INTERFACE_NAME);
+		return FALSE;
+	}
+
+	g_hash_table_iter_init (&iter, priv->options);
+	while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value)) {
+		if (   !_nm_utils_string_in_list (key, valid_opts)
+		    || !strlen (value)
+		    || (strlen (value) > 200)) {
+			g_set_error (error,
+				         NM_SETTING_BRIDGE_ERROR,
+				         NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
+				         NM_SETTING_BRIDGE_OPTIONS);
+			return FALSE;
+		}
+	}
+
+	return TRUE;
+}
+
+static const char *
+get_virtual_iface_name (NMSetting *setting)
+{
+	NMSettingBridge *self = NM_SETTING_BRIDGE (setting);
+
+	return nm_setting_bridge_get_interface_name (self);
+}
+
+static void
+nm_setting_bridge_init (NMSettingBridge *setting)
+{
+	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (setting);
+
+	g_object_set (setting, NM_SETTING_NAME, NM_SETTING_BRIDGE_SETTING_NAME,
+	              NULL);
+	priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+}
+
+static void
+finalize (GObject *object)
+{
+	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
+
+	g_free (priv->interface_name);
+	g_hash_table_destroy (priv->options);
+
+	G_OBJECT_CLASS (nm_setting_bridge_parent_class)->finalize (object);
+}
+
+static void
+copy_hash (gpointer key, gpointer value, gpointer user_data)
+{
+	g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value));
+}
+
+static void
+set_property (GObject *object, guint prop_id,
+              const GValue *value, GParamSpec *pspec)
+{
+	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
+	GHashTable *new_hash;
+
+	switch (prop_id) {
+	case PROP_INTERFACE_NAME:
+		priv->interface_name = g_value_dup_string (value);
+		break;
+	case PROP_OPTIONS:
+		/* Must make a deep copy of the hash table here... */
+		g_hash_table_remove_all (priv->options);
+		new_hash = g_value_get_boxed (value);
+		if (new_hash)
+			g_hash_table_foreach (new_hash, copy_hash, priv->options);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+static void
+get_property (GObject *object, guint prop_id,
+              GValue *value, GParamSpec *pspec)
+{
+	NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
+	NMSettingBridge *setting = NM_SETTING_BRIDGE (object);
+
+	switch (prop_id) {
+	case PROP_INTERFACE_NAME:
+		g_value_set_string (value, nm_setting_bridge_get_interface_name (setting));
+		break;
+	case PROP_OPTIONS:
+		g_value_set_boxed (value, priv->options);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
+}
+
+static void
+nm_setting_bridge_class_init (NMSettingBridgeClass *setting_class)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
+	NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
+
+	g_type_class_add_private (setting_class, sizeof (NMSettingBridgePrivate));
+
+	/* virtual methods */
+	object_class->set_property = set_property;
+	object_class->get_property = get_property;
+	object_class->finalize     = finalize;
+	parent_class->verify       = verify;
+	parent_class->get_virtual_iface_name = get_virtual_iface_name;
+
+	/* Properties */
+	/**
+	 * NMSettingBridge:interface-name:
+	 *
+	 * Name of virtual kernel interface
+	 **/
+	g_object_class_install_property
+		(object_class, PROP_INTERFACE_NAME,
+		 g_param_spec_string (NM_SETTING_BRIDGE_INTERFACE_NAME,
+		                      "InterfaceName",
+		                      "The name of the virtual in-kernel bridgeing nework interface",
+		                      NULL,
+		                      G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE));
+
+	/**
+	 * NMSettingBridge:options:
+	 *
+	 * Dictionary of key/value pairs of bridging options.  Both keys
+	 * and values must be strings. Key names must contain only
+	 * alphanumeric characters (ie, [a-zA-Z0-9]).
+	 **/
+	g_object_class_install_property
+		(object_class, PROP_OPTIONS,
+		 _nm_param_spec_specialized (NM_SETTING_BRIDGE_OPTIONS,
+							   "Options",
+							   "Dictionary of key/value pairs of bridging options. "
+							   "Both keys and values must be strings.",
+							   DBUS_TYPE_G_MAP_OF_STRING,
+							   G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE));
+}
diff --git a/libnm-util/nm-setting-bridge.h b/libnm-util/nm-setting-bridge.h
new file mode 100644
index 0000000..13b3094
--- /dev/null
+++ b/libnm-util/nm-setting-bridge.h
@@ -0,0 +1,96 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
+
+/*
+ * Thomas Graf <tgraf redhat com>
+ *
+ * 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.
+ *
+ * (C) Copyright 2011-2012 Red Hat, Inc.
+ */
+
+#ifndef NM_SETTING_BRIDGE_H
+#define NM_SETTING_BRIDGE_H
+
+#include <nm-setting.h>
+
+G_BEGIN_DECLS
+
+#define NM_TYPE_SETTING_BRIDGE            (nm_setting_bridge_get_type ())
+#define NM_SETTING_BRIDGE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_SETTING_BRIDGE, NMSettingBridge))
+#define NM_SETTING_BRIDGE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_SETTING_BRIDGE, NMSettingBrigeClass))
+#define NM_IS_SETTING_BRIDGE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_SETTING_BRIDGE))
+#define NM_IS_SETTING_BRIDGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_SETTING_BRIDGE))
+#define NM_SETTING_BRIDGE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_SETTING_BRIDGE, NMSettingBridgeClass))
+
+#define NM_SETTING_BRIDGE_SETTING_NAME "bridge"
+
+/**
+ * NMSettingBridgeError:
+ * @NM_SETTING_BRIDGE_ERROR_UNKNOWN: unknown or unclassified error
+ * @NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY: the property was invalid
+ * @NM_SETTING_BRIDGE_ERROR_MISSING_PROPERTY: the property was missing and is
+ * required
+ */
+typedef enum {
+	NM_SETTING_BRIDGE_ERROR_UNKNOWN = 0,
+	NM_SETTING_BRIDGE_ERROR_INVALID_PROPERTY,
+	NM_SETTING_BRIDGE_ERROR_MISSING_PROPERTY,
+} NMSettingBridgeError;
+
+#define NM_TYPE_SETTING_BRIDGE_ERROR (nm_setting_bridge_error_get_type ())
+GType nm_setting_bridge_error_get_type (void);
+
+#define NM_SETTING_BRIDGE_ERROR nm_setting_bridge_error_quark ()
+GQuark nm_setting_bridge_error_quark (void);
+
+#define NM_SETTING_BRIDGE_INTERFACE_NAME "interface-name"
+#define NM_SETTING_BRIDGE_OPTIONS "options"
+
+typedef struct {
+	NMSetting parent;
+} NMSettingBridge;
+
+typedef struct {
+	NMSettingClass parent;
+
+	/* Padding for future expansion */
+	void (*_reserved1) (void);
+	void (*_reserved2) (void);
+	void (*_reserved3) (void);
+	void (*_reserved4) (void);
+} NMSettingBridgeClass;
+
+GType nm_setting_bridge_get_type (void);
+
+NMSetting *  nm_setting_bridge_new                (void);
+const char * nm_setting_bridge_get_interface_name (NMSettingBridge *setting);
+
+guint32      nm_setting_bridge_get_num_options (NMSettingBridge *setting);
+gboolean     nm_setting_bridge_get_option      (NMSettingBridge *setting,
+                                                guint32 idx,
+                                                const char **out_key,
+                                                const char **out_value);
+const char * nm_setting_bridge_get_option_by_key (NMSettingBridge *setting,
+                                                  const char *key);
+gboolean     nm_setting_bridge_add_option      (NMSettingBridge *setting,
+                                                const char *key,
+                                                const char *item);
+gboolean     nm_setting_bridge_remove_option   (NMSettingBridge *setting,
+                                                const char *key);
+
+G_END_DECLS
+
+#endif /* NM_SETTING_BRIDGE_H */
diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c
index 15a8629..e649be3 100644
--- a/src/settings/plugins/ifcfg-rh/reader.c
+++ b/src/settings/plugins/ifcfg-rh/reader.c
@@ -45,6 +45,7 @@
 #include <nm-setting-wireless.h>
 #include <nm-setting-8021x.h>
 #include <nm-setting-bond.h>
+#include <nm-setting-bridge.h>
 #include <nm-utils.h>
 
 #include "common.h"
@@ -3691,6 +3692,136 @@ bond_connection_from_ifcfg (const char *file,
 	return connection;
 }
 
+static void
+handle_bridge_option (NMSettingBridge *s_bridge,
+                      const char *key,
+                      const char *value)
+{
+	if (!nm_setting_bridge_add_option (s_bridge, key, value))
+		PLUGIN_WARN (IFCFG_PLUGIN_NAME, "    warning: invalid bridging option '%s'", key);
+}
+
+static NMSetting *
+make_bridge_setting (shvarFile *ifcfg,
+                     const char *file,
+                     gboolean nm_controlled,
+                     char **unmanaged,
+                     GError **error)
+{
+	NMSettingBridge *s_bridge;
+	char *value;
+
+	s_bridge = NM_SETTING_BRIDGE (nm_setting_bridge_new ());
+
+	value = svGetValue (ifcfg, "DEVICE", FALSE);
+	if (!value || !strlen (value)) {
+		g_set_error (error, IFCFG_PLUGIN_ERROR, 0, "mandatory DEVICE keyword missing");
+		goto error;
+	}
+
+	g_object_set (s_bridge, NM_SETTING_BRIDGE_INTERFACE_NAME, value, NULL);
+	g_free (value);
+
+	value = svGetValue (ifcfg, "DELAY", FALSE);
+	if (value) {
+		handle_bridge_option (s_bridge, "delay", value);
+		g_free (value);
+	}
+
+	value = svGetValue (ifcfg, "STP", FALSE);
+	if (value) {
+		handle_bridge_option (s_bridge, "stp", value);
+		g_free (value);
+	}
+
+	value = svGetValue (ifcfg, "BRIDGING_OPTS", FALSE);
+	if (value) {
+		char **items, **iter;
+
+		items = g_strsplit_set (value, " ", -1);
+		for (iter = items; iter && *iter; iter++) {
+			if (strlen (*iter)) {
+				char **keys, *key, *val;
+
+				keys = g_strsplit_set (*iter, "=", 2);
+				if (keys && *keys) {
+					key = *keys;
+					val = *(keys + 1);
+					if (val && strlen(key) && strlen(val))
+						handle_bridge_option (s_bridge, key, val);
+				}
+
+				g_strfreev (keys);
+			}
+		}
+		g_free (value);
+		g_strfreev (items);
+	}
+
+	return (NMSetting *) s_bridge;
+
+error:
+	g_object_unref (s_bridge);
+	return NULL;
+}
+
+static NMConnection *
+bridge_connection_from_ifcfg (const char *file,
+                              shvarFile *ifcfg,
+                              gboolean nm_controlled,
+                              char **unmanaged,
+                              GError **error)
+{
+	NMConnection *connection = NULL;
+	NMSetting *con_setting = NULL;
+	NMSetting *bridge_setting = NULL;
+	NMSetting *wired_setting = NULL;
+	NMSetting8021x *s_8021x = NULL;
+
+	g_return_val_if_fail (file != NULL, NULL);
+	g_return_val_if_fail (ifcfg != NULL, NULL);
+
+	connection = nm_connection_new ();
+	if (!connection) {
+		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
+		             "Failed to allocate new connection for %s.", file);
+		return NULL;
+	}
+
+	con_setting = make_connection_setting (file, ifcfg, NM_SETTING_BRIDGE_SETTING_NAME, NULL, _("Bridge"));
+	if (!con_setting) {
+		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
+		             "Failed to create connection setting.");
+		g_object_unref (connection);
+		return NULL;
+	}
+	nm_connection_add_setting (connection, con_setting);
+
+	bridge_setting = make_bridge_setting (ifcfg, file, nm_controlled, unmanaged, error);
+	if (!bridge_setting) {
+		g_object_unref (connection);
+		return NULL;
+	}
+	nm_connection_add_setting (connection, bridge_setting);
+
+	wired_setting = make_wired_setting (ifcfg, file, nm_controlled, unmanaged, &s_8021x, error);
+	if (!wired_setting) {
+		g_object_unref (connection);
+		return NULL;
+	}
+	nm_connection_add_setting (connection, wired_setting);
+
+	if (s_8021x)
+		nm_connection_add_setting (connection, NM_SETTING (s_8021x));
+
+	if (!nm_connection_verify (connection, error)) {
+		g_object_unref (connection);
+		return NULL;
+	}
+
+	return connection;
+}
+
 static gboolean
 disabling_ip4_config_allowed (NMConnection *connection)
 {
@@ -3867,10 +3998,9 @@ connection_from_file (const char *filename,
 		connection = wireless_connection_from_ifcfg (filename, parsed, nm_controlled, unmanaged, &error);
 	else if (!strcasecmp (type, TYPE_INFINIBAND))
 		connection = infiniband_connection_from_ifcfg (filename, parsed, nm_controlled, unmanaged, &error);
-	else if (!strcasecmp (type, TYPE_BRIDGE)) {
-		g_set_error (&error, IFCFG_PLUGIN_ERROR, 0,
-		             "Bridge connections are not yet supported");
-	} else if (!strcasecmp (type, TYPE_BOND))
+	else if (!strcasecmp (type, TYPE_BRIDGE))
+		connection = bridge_connection_from_ifcfg  (filename, parsed, nm_controlled, unmanaged, &error);
+	else if (!strcasecmp (type, TYPE_BOND))
 		connection = bond_connection_from_ifcfg (filename, parsed, nm_controlled, unmanaged, &error);
 	else {
 		g_set_error (&error, IFCFG_PLUGIN_ERROR, 0,
-- 
1.7.7.6



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]