The example of ifcfg-vlan is as followed: VLAN=yes TYPE=Vlan DEVICE=vlan43 or "DEVICE=eth9.43" PHYSDEV=eth9 REORDER_HDR=0 VLAN_FLAGS=GVRP,LOOSE_BINDING VLAN_INGRESS_PRIORITY_MAP=0:1,2:5 VLAN_EGRESS_PRIORITY_MAP=12:3,14:7 ONBOOT=yes BOOTPROTO=static IPADDR=192.168.43.149 NETMASK=255.255.255.0
And we try to make it compitable with the format used by initscripts, and there is no need to change anything in ifcfg-eth9. V4: 1 misc cleanup 2 use g_value_get_boxed() and g_value_take_boxed() 3 add nm_setting_vlan_add_priority_str() V3: 1 parse format "DEVICE=eth9.43" 2 modify NMSettingVlan->get_property() V2: 1 use "Vlan" and "vlan" to keep consistency 2 remove duplicate "VLAN" or "vlan" 3 add enum NMVlanFlags Signed-off-by: Weiping Pan <w...@redhat.com> --- libnm-util/Makefile.am | 2 + libnm-util/libnm-util.ver | 12 + libnm-util/nm-connection.c | 26 +- libnm-util/nm-connection.h | 2 + libnm-util/nm-setting-vlan.c | 640 ++++++++++++++++++++ libnm-util/nm-setting-vlan.h | 118 ++++ src/settings/plugins/ifcfg-rh/common.h | 1 + src/settings/plugins/ifcfg-rh/reader.c | 175 +++++- .../network-scripts/ifcfg-test-vlan-interface | 11 +- .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 3 +- 10 files changed, 970 insertions(+), 20 deletions(-) create mode 100644 libnm-util/nm-setting-vlan.c create mode 100644 libnm-util/nm-setting-vlan.h diff --git a/libnm-util/Makefile.am b/libnm-util/Makefile.am index d0bf73a..093c8b3 100644 --- a/libnm-util/Makefile.am +++ b/libnm-util/Makefile.am @@ -19,6 +19,7 @@ libnm_util_include_HEADERS = \ nm-setting-connection.h \ nm-setting-infiniband.h \ nm-setting-ip4-config.h \ + nm-setting-vlan.h \ nm-setting-ip6-config.h \ nm-setting-ppp.h \ nm-setting-pppoe.h \ @@ -50,6 +51,7 @@ libnm_util_la_csources = \ nm-setting-connection.c \ nm-setting-infiniband.c \ nm-setting-ip4-config.c \ + nm-setting-vlan.c \ nm-setting-ip6-config.c \ nm-setting-ppp.c \ nm-setting-pppoe.c \ diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver index 84ef039..e62ff8b 100644 --- a/libnm-util/libnm-util.ver +++ b/libnm-util/libnm-util.ver @@ -28,6 +28,7 @@ global: nm_connection_get_setting_ppp; nm_connection_get_setting_pppoe; nm_connection_get_setting_serial; + nm_connection_get_setting_vlan; nm_connection_get_setting_vpn; nm_connection_get_setting_wimax; nm_connection_get_setting_wired; @@ -358,6 +359,17 @@ global: nm_setting_to_string; nm_setting_update_secrets; nm_setting_verify; + nm_setting_vlan_add_priority_str; + nm_setting_vlan_error_get_type; + nm_setting_vlan_error_quark; + nm_setting_vlan_get_egress_priority_map; + nm_setting_vlan_get_flags; + nm_setting_vlan_get_id; + nm_setting_vlan_get_ingress_priority_map; + nm_setting_vlan_get_interface_name; + nm_setting_vlan_get_slave; + nm_setting_vlan_get_type; + nm_setting_vlan_new; nm_setting_vpn_add_data_item; nm_setting_vpn_add_secret; nm_setting_vpn_error_get_type; diff --git a/libnm-util/nm-connection.c b/libnm-util/nm-connection.c index a63050e..173c761 100644 --- a/libnm-util/nm-connection.c +++ b/libnm-util/nm-connection.c @@ -47,7 +47,7 @@ #include "nm-setting-vpn.h" #include "nm-setting-olpc-mesh.h" #include "nm-setting-bond.h" - +#include "nm-setting-vlan.h" #include "nm-setting-serial.h" #include "nm-setting-gsm.h" #include "nm-setting-cdma.h" @@ -138,7 +138,7 @@ static guint signals[LAST_SIGNAL] = { 0 }; static GHashTable *registered_settings = NULL; -#define DEFAULT_MAP_SIZE 18 +#define DEFAULT_MAP_SIZE 19 static struct SettingInfo { const char *name; @@ -255,6 +255,11 @@ register_default_settings (void) NM_SETTING_INFINIBAND_ERROR, 1, TRUE); + register_one_setting (NM_SETTING_VLAN_SETTING_NAME, + NM_TYPE_SETTING_VLAN, + NM_SETTING_VLAN_ERROR, + 1, TRUE); + register_one_setting (NM_SETTING_WIRELESS_SECURITY_SETTING_NAME, NM_TYPE_SETTING_WIRELESS_SECURITY, NM_SETTING_WIRELESS_SECURITY_ERROR, @@ -1674,6 +1679,23 @@ nm_connection_get_setting_wireless_security (NMConnection *connection) return (NMSettingWirelessSecurity *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY); } +/** + * nm_connection_get_setting_vlan: + * @connection: the #NMConnection + * + * A shortcut to return any #NMSettingVlan the connection might contain. + * + * Returns: (transfer none): an #NMSettingVlan if the connection contains one, otherwise NULL + **/ +NMSettingVlan * +nm_connection_get_setting_vlan (NMConnection *connection) +{ + g_return_val_if_fail (connection != NULL, NULL); + g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL); + + return (NMSettingVlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VLAN); +} + /*************************************************************/ static void diff --git a/libnm-util/nm-connection.h b/libnm-util/nm-connection.h index 28dce12..9ec937d 100644 --- a/libnm-util/nm-connection.h +++ b/libnm-util/nm-connection.h @@ -48,6 +48,7 @@ #include <nm-setting-wired.h> #include <nm-setting-wireless.h> #include <nm-setting-wireless-security.h> +#include <nm-setting-vlan.h> G_BEGIN_DECLS @@ -204,6 +205,7 @@ NMSettingWimax * nm_connection_get_setting_wimax (NMConnec NMSettingWired * nm_connection_get_setting_wired (NMConnection *connection); NMSettingWireless * nm_connection_get_setting_wireless (NMConnection *connection); NMSettingWirelessSecurity *nm_connection_get_setting_wireless_security (NMConnection *connection); +NMSettingVlan * nm_connection_get_setting_vlan (NMConnection *connection); G_END_DECLS diff --git a/libnm-util/nm-setting-vlan.c b/libnm-util/nm-setting-vlan.c new file mode 100644 index 0000000..c9384d0 --- /dev/null +++ b/libnm-util/nm-setting-vlan.c @@ -0,0 +1,640 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * Weiping Pan <w...@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 Red Hat, Inc. + */ + +#include <dbus/dbus-glib.h> +#include "nm-setting-vlan.h" +#include "nm-param-spec-specialized.h" +#include "nm-utils.h" +#include "nm-dbus-glib-types.h" + +/** + * SECTION:nm-setting-vlan + * @short_description: Describes connection properties for VLAN devices + * @include: nm-setting-vlan.h + * + * The #NMSettingVlan object is a #NMSetting subclass that describes properties + * necessary for connection to VLAN devices. + **/ + +/** + * nm_setting_vlan_error_quark: + * Registers an error quark for #NMSettingVlan if necessary. + * Returns: the error quark used for #NMSettingVlan errors. + **/ +GQuark +nm_setting_vlan_error_quark (void) +{ + static GQuark quark; + + if (G_UNLIKELY (!quark)) + quark = g_quark_from_static_string ("nm-setting-vlan-error-quark"); + return quark; +} + +/* This should really be standard. */ +#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC } + +GType +nm_setting_vlan_error_get_type (void) +{ + static GType etype = 0; + + if (etype == 0) { + static const GEnumValue values[] = { + /* Unknown error. */ + ENUM_ENTRY (NM_SETTING_VLAN_ERROR_UNKNOWN, "UnknownError"), + /* The specified property was invalid. */ + ENUM_ENTRY (NM_SETTING_VLAN_ERROR_INVALID_PROPERTY, "InvalidProperty"), + /* The specified property was missing and is required. */ + ENUM_ENTRY (NM_SETTING_VLAN_ERROR_MISSING_PROPERTY, "MissingProperty"), + { 0, 0, 0 } + }; + etype = g_enum_register_static ("NMSettingVlanError", values); + } + return etype; +} + +G_DEFINE_TYPE (NMSettingVlan, nm_setting_vlan, NM_TYPE_SETTING) + +#define NM_SETTING_VLAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_VLAN, NMSettingVlanPrivate)) + +typedef struct { + char *interface_name; + char *vlan_slave; + guint32 vlan_id; + guint32 vlan_flags; + GSList *vlan_ingress_priority_map; + GSList *vlan_egress_priority_map; +} NMSettingVlanPrivate; + +enum { + PROP_0, + PROP_VLAN_DEVICE, + PROP_VLAN_SLAVE, + PROP_VLAN_ID, + PROP_VLAN_FLAGS, + PROP_VLAN_INGRESS_PRIORITY_MAP, + PROP_VLAN_EGRESS_PRIORITY_MAP, + LAST_PROP +}; + +/** + * nm_setting_vlan_new: + * Creates a new #NMSettingVlan object with default values. + * + * Returns: (transfer full): the new empty #NMSettingVlan object + **/ +NMSetting * +nm_setting_vlan_new (void) +{ + return (NMSetting *) g_object_new (NM_TYPE_SETTING_VLAN, NULL); +} + +/** + * nm_setting_vlan_get_interface_name: + * @setting: the #NMSettingVlan + * + * Returns: the #NMSettingVlan:interface_name property of the setting + **/ +const char * +nm_setting_vlan_get_interface_name (NMSettingVlan *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), NULL); + return NM_SETTING_VLAN_GET_PRIVATE (setting)->interface_name; +} + +/** + * nm_setting_vlan_get_slave: + * @setting: the #NMSettingVlan + * + * Returns: the #NMSettingVlan:vlan_slave property of the setting + **/ +const char * +nm_setting_vlan_get_slave (NMSettingVlan *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), NULL); + return NM_SETTING_VLAN_GET_PRIVATE (setting)->vlan_slave; +} + +/** + * nm_setting_vlan_get_id: + * @setting: the #NMSettingVlan + * + * Returns: the #NMSettingVlan:vlan_id property of the setting + **/ +guint32 +nm_setting_vlan_get_id (NMSettingVlan *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), 0); + return NM_SETTING_VLAN_GET_PRIVATE (setting)->vlan_id; +} + +/** + * nm_setting_vlan_get_flags: + * @setting: the #NMSettingVlan + * + * Returns: the #NMSettingVlan:vlan_flags property of the setting + **/ +guint32 +nm_setting_vlan_get_flags (NMSettingVlan *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), 0); + return NM_SETTING_VLAN_GET_PRIVATE (setting)->vlan_flags; +} + +/** + * nm_setting_vlan_get_ingress_priority_map: + * @setting: the #NMSettingVlan + * + * Returns: the #NMSettingVlan:vlan_ingress_priority_map property of the setting + **/ +const GSList * +nm_setting_vlan_get_ingress_priority_map(NMSettingVlan *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), NULL); + + return NM_SETTING_VLAN_GET_PRIVATE (setting)->vlan_ingress_priority_map; +} + +/** + * nm_setting_vlan_get_egress_priority_map: + * @setting: the #NMSettingVlan + * + * Returns: the #NMSettingVlan:vlan_egress_priority_map property of the setting + **/ +const GSList * +nm_setting_vlan_get_egress_priority_map(NMSettingVlan *setting) +{ + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), NULL); + + return NM_SETTING_VLAN_GET_PRIVATE (setting)->vlan_egress_priority_map; +} + +static void +nm_setting_vlan_init (NMSettingVlan *setting) +{ + g_object_set (setting, NM_SETTING_NAME, NM_SETTING_VLAN_SETTING_NAME, NULL); + + return; +} + +static void +finalize (GObject *object) +{ + NMSettingVlan *setting = NM_SETTING_VLAN (object); + NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + + g_free (priv->interface_name); + g_free (priv->vlan_slave); + nm_utils_slist_free (priv->vlan_ingress_priority_map, g_free); + nm_utils_slist_free (priv->vlan_egress_priority_map, g_free); + + return; +} + +static vlan_priority_map * +priority_map_new_from_str (const char *str) +{ + vlan_priority_map *p = NULL; + gchar **t = NULL; + + if (!str) + return NULL; + p = g_malloc0 (sizeof (vlan_priority_map)); + g_return_val_if_fail (p != NULL, NULL); + + t = g_strsplit (str, ":", 0); + if (g_strv_length (t) != 2) + goto error; + p->from = g_ascii_strtoull (t[0], NULL, 10); + p->to = g_ascii_strtoull (t[1], NULL, 10); + + g_strfreev (t); + + return p; +error: + g_free (p); + return NULL; +} + +static GSList * +priority_stringlist_to_maplist (GSList *strlist) +{ + GSList *list = NULL, *iter; + + for (iter = strlist; iter; iter = g_slist_next (iter)) { + vlan_priority_map *item = NULL; + item = priority_map_new_from_str ((const char *) iter->data); + if (item) + list = g_slist_append (list, item); + } + + return list; +} + +static GSList * +priority_maplist_to_stringlist (GSList *list) +{ + GSList *strlist = NULL, *iterator; + vlan_priority_map *item = NULL; + + for (iterator = list; iterator; iterator = iterator->next) { + item = iterator->data; + strlist = g_slist_append (strlist, g_strdup_printf ("%d:%d", item->from, item->to)); + } + + return strlist; +} + +/** + * nm_setting_vlan_add_priority_str + * @setting: the #NMSettingVlan + * @map: the type of priority map + * @str: the string which contains a priority map, like "3:7" + * + * Adds a priority map entry into + * #NMSettingVlan:vlan_ingress_priority_map/vlan_egress_priority_map + * + * Returns: TRUE if the entry was successfully added to the + * list, or it overwrote the old value, FALSE if error + */ +gboolean +nm_setting_vlan_add_priority_str (NMSettingVlan *setting, + NMVlanPriorityMap map, + const char *str) +{ + NMSettingVlanPrivate *priv = NULL; + GSList *list = NULL, *iter = NULL; + vlan_priority_map *item = NULL, *p = NULL; + + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE); + g_return_val_if_fail (g_utf8_strlen (str, -1) > 0, FALSE); + + priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + if (map == NM_VLAN_INGRESS_MAP) + list = priv->vlan_ingress_priority_map; + else if (map == NM_VLAN_EGRESS_MAP) + list = priv->vlan_egress_priority_map; + else + return FALSE; + + item = priority_map_new_from_str (str); + g_return_val_if_fail (item != NULL, FALSE); + + for (iter = list; iter; iter = g_slist_next (iter)) { + p = iter->data; + if (p->from == item->from) { + p->from = item->to; + return TRUE; + } + } + + list = g_slist_append (list, item); + if (map == NM_VLAN_INGRESS_MAP) + priv->vlan_ingress_priority_map = list; + else if (map == NM_VLAN_EGRESS_MAP) + priv->vlan_egress_priority_map = list; + + return TRUE; +} + +static void +set_property (GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + NMSettingVlan *setting = NM_SETTING_VLAN (object); + NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + + switch (prop_id) { + case PROP_VLAN_DEVICE: + g_free (priv->interface_name); + priv->interface_name = g_value_dup_string (value); + break; + case PROP_VLAN_SLAVE: + g_free (priv->vlan_slave); + priv->vlan_slave = g_value_dup_string (value); + break; + case PROP_VLAN_ID: + priv->vlan_id = g_value_get_uint(value); + break; + case PROP_VLAN_FLAGS: + priv->vlan_flags = g_value_get_uint(value); + break; + case PROP_VLAN_INGRESS_PRIORITY_MAP: + nm_utils_slist_free (priv->vlan_ingress_priority_map, g_free); + priv->vlan_ingress_priority_map = + priority_stringlist_to_maplist (g_value_get_boxed (value)); + break; + case PROP_VLAN_EGRESS_PRIORITY_MAP: + nm_utils_slist_free (priv->vlan_egress_priority_map, g_free); + priv->vlan_egress_priority_map = + priority_stringlist_to_maplist (g_value_get_boxed (value)); + break; + default: + break; + } +} + +static void +get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + NMSettingVlan *setting = NM_SETTING_VLAN (object); + NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + + switch (prop_id) { + case PROP_VLAN_DEVICE: + g_value_set_string (value, nm_setting_vlan_get_interface_name (setting)); + break; + case PROP_VLAN_SLAVE: + g_value_set_string (value, nm_setting_vlan_get_slave (setting)); + break; + case PROP_VLAN_ID: + g_value_set_uint (value, nm_setting_vlan_get_id (setting)); + break; + case PROP_VLAN_FLAGS: + g_value_set_uint (value, nm_setting_vlan_get_flags (setting)); + break; + case PROP_VLAN_INGRESS_PRIORITY_MAP: + g_value_take_boxed (value, priority_maplist_to_stringlist (priv->vlan_ingress_priority_map)); + break; + case PROP_VLAN_EGRESS_PRIORITY_MAP: + g_value_take_boxed (value, priority_maplist_to_stringlist (priv->vlan_egress_priority_map)); + break; + default: + break; + } +} + +/** + * nm_setting_vlan_get_num_priorities: + * @map: the type of priority map + * @setting: the #NMSettingVlan + * + * Returns the number of entires in the + * #NMSettingVlan:vlan_ingress_priority_map/vlan_egress_priority_map property of this setting. + * + * Returns: return the number of ingress/egress priority entries, -1 if error + **/ +gint32 nm_setting_vlan_get_num_priorities (NMVlanPriorityMap map, NMSettingVlan *setting) +{ + NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + GSList *list = NULL; + + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE); + + if (map == NM_VLAN_INGRESS_MAP) + list = priv->vlan_ingress_priority_map; + else if (map == NM_VLAN_EGRESS_MAP) + list = priv->vlan_egress_priority_map; + else + return -1; + + return g_slist_length (list); +} + +/** + * nm_setting_vlan_get_priority: + * @map: the type of priority map + * @setting: the #NMSettingVlan + * @idx: the zero-based index of the ingress/egress priority map entry + * @from: on return, the value of vlan_priority_map->from + * @to: on return, the value of vlan_priority_map->to + * + * Retrieve one of the entries of the + * #NMSettingVlan:vlan_ingress_priority_map/vlan_egress_priority_map property of this setting. + * + * Returns: %TRUE if a priority map was returned, %FALSE if error + **/ +gboolean nm_setting_vlan_get_priority (NMVlanPriorityMap map, + NMSettingVlan *setting, guint32 idx, + guint32 *from, guint32 *to) +{ + NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + GSList *list = NULL; + vlan_priority_map *item = NULL; + + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE); + g_return_val_if_fail (from != NULL, FALSE); + g_return_val_if_fail (to != NULL, FALSE); + + if (map == NM_VLAN_INGRESS_MAP) + list = priv->vlan_ingress_priority_map; + else if (map == NM_VLAN_EGRESS_MAP) + list = priv->vlan_egress_priority_map; + else + return FALSE; + + g_return_val_if_fail (idx < g_slist_length(list), FALSE); + + item = g_slist_nth_data (list, idx); + if (item) { + *from = item->from; + *to = item->to; + return TRUE; + } + + return FALSE; +} + +/** + * nm_setting_vlan_add_priority: + * @map: the type of priority map + * @setting: the #NMSettingVlan + * @from: the value of vlan_priority_map->from + * @to: the value of vlan_priority_map->to + * + * Adds a vlan_priority_map to + * #NMSettingVlan:vlan_ingress_priority_map/vlan_egress_priority_map list. + * + * Note: + * If @from is already in + * #NMSettingVlan:vlan_ingress_priority_map/vlan_egress_priority_map, + * then we will overwrite the entry with <@from, @to>. + * + * Returns: TRUE if the a vlan_priority_map was successfully added to the + * list, FALSE if error + */ +gboolean nm_setting_vlan_add_priority (NMVlanPriorityMap map, + NMSettingVlan *setting, guint32 from, guint32 to) +{ + NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + GSList *list = NULL, *iterator = NULL; + vlan_priority_map *item = NULL; + + g_return_val_if_fail (NM_IS_SETTING_VLAN (setting), FALSE); + + if (map == NM_VLAN_INGRESS_MAP) + list = priv->vlan_ingress_priority_map; + else if (map == NM_VLAN_EGRESS_MAP) + list = priv->vlan_egress_priority_map; + else + return FALSE; + + for (iterator = list; iterator; iterator = iterator->next) { + item = iterator->data; + if (item->from == from) { + item->to = to; + return TRUE; + } + } + + item = g_malloc0 (sizeof(vlan_priority_map)); + g_return_val_if_fail (item != NULL, FALSE); + + item->from = from; + item->to = to; + + list = g_slist_append (list, item); + + return TRUE; +} + +/** + * nm_setting_vlan_remove_priority: + * @map: the type of priority map + * @setting: the #NMSettingVlan + * @idx: the zero-based index of the priority map to remove + * + * Removes the priority map at index @idx from + * #NMSettingVlan:vlan_ingress_priority_map/vlan_egress_priority_map list. + */ +void nm_setting_vlan_remove_priority (NMVlanPriorityMap map, + NMSettingVlan *setting, guint32 idx) +{ + NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + GSList *list = NULL, *item = NULL; + + g_return_if_fail (NM_IS_SETTING_VLAN (setting)); + + if (map == NM_VLAN_INGRESS_MAP) + list = priv->vlan_ingress_priority_map; + else if (map == NM_VLAN_EGRESS_MAP) + list = priv->vlan_egress_priority_map; + else + return; + + g_return_if_fail (idx < g_slist_length (list)); + item = g_slist_nth_data (list, idx); + g_free (item->data); + list = g_slist_delete_link (list, item); + + return; +} + +/** + * nm_setting_vlan_clear_priorities: + * @map: the type of priority map + * @setting: the #NMSettingVlan + * + * Clear all the entires from + * #NMSettingVlan:vlan_ingress_priority_map/vlan_egress_priority_map list. + */ +void nm_setting_vlan_clear_priorities (NMVlanPriorityMap map, NMSettingVlan *setting) +{ + NMSettingVlanPrivate *priv = NM_SETTING_VLAN_GET_PRIVATE (setting); + GSList *list = NULL; + + g_return_if_fail (NM_IS_SETTING_VLAN (setting)); + + if (map == NM_VLAN_INGRESS_MAP) + list = priv->vlan_ingress_priority_map; + else if (map == NM_VLAN_EGRESS_MAP) + list = priv->vlan_egress_priority_map; + else + return; + + nm_utils_slist_free (list, g_free); + + if (map == NM_VLAN_INGRESS_MAP) + priv->vlan_ingress_priority_map = NULL; + else if (map == NM_VLAN_EGRESS_MAP) + priv->vlan_egress_priority_map = NULL; + + return; +} + +static void +nm_setting_vlan_class_init (NMSettingVlanClass *setting_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (setting_class); + g_type_class_add_private (setting_class, sizeof(NMSettingVlanPrivate)); + + /* virtual methods */ + object_class->set_property = set_property; + object_class->get_property = get_property; + object_class->finalize = finalize; + + /* Properties */ + g_object_class_install_property + (object_class, PROP_VLAN_DEVICE, + g_param_spec_string (NM_SETTING_VLAN_INTERFACE_NAME, + "InterfaceName", + "The vlan device name in kernel", + NULL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT | NM_SETTING_PARAM_SERIALIZE)); + + g_object_class_install_property + (object_class, PROP_VLAN_SLAVE, + g_param_spec_string (NM_SETTING_VLAN_SLAVE, + "vlan slave", + "The underlying physical ethernet device", + NULL, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT | NM_SETTING_PARAM_SERIALIZE)); + + g_object_class_install_property + (object_class, PROP_VLAN_ID, + g_param_spec_uint (NM_SETTING_VLAN_ID, + "vlan id", + "vlan id", + 0, + 4095, + 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT | NM_SETTING_PARAM_SERIALIZE)); + + g_object_class_install_property + (object_class, PROP_VLAN_FLAGS, + g_param_spec_uint (NM_SETTING_VLAN_FLAGS, + "vlan flags", + "vlan flags", + 0, + G_MAXUINT32, + 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT | NM_SETTING_PARAM_SERIALIZE)); + + g_object_class_install_property + (object_class, PROP_VLAN_INGRESS_PRIORITY_MAP, + _nm_param_spec_specialized (NM_SETTING_VLAN_INGRESS_PRIORITY_MAP, + "vlan ingress priority mapping", + "vlan ingress priority mapping", + DBUS_TYPE_G_LIST_OF_STRING, + G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE)); + + g_object_class_install_property + (object_class, PROP_VLAN_EGRESS_PRIORITY_MAP, + _nm_param_spec_specialized (NM_SETTING_VLAN_EGRESS_PRIORITY_MAP, + "vlan egress priority mapping", + "vlan egress priority mapping", + DBUS_TYPE_G_LIST_OF_STRING, + G_PARAM_READWRITE | NM_SETTING_PARAM_SERIALIZE)); +} diff --git a/libnm-util/nm-setting-vlan.h b/libnm-util/nm-setting-vlan.h new file mode 100644 index 0000000..2479a11 --- /dev/null +++ b/libnm-util/nm-setting-vlan.h @@ -0,0 +1,118 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* + * Weiping Pan <w...@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 Red Hat, Inc. + */ + +#ifndef NM_SETTING_VLAN_H +#define NM_SETTING_VLAN_H + +#include "nm-setting.h" +#include <linux/if_vlan.h> + +G_BEGIN_DECLS + +#define NM_TYPE_SETTING_VLAN (nm_setting_vlan_get_type ()) +#define NM_SETTING_VLAN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_SETTING_VLAN, NMSettingVlan)) +#define NM_SETTING_VLAN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_SETTING_VLANCONFIG, NMSettingVlanClass)) +#define NM_IS_SETTING_VLAN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_SETTING_VLAN)) +#define NM_IS_SETTING_VLAN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_SETTING_VLAN)) +#define NM_SETTING_VLAN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_SETTING_VLAN, NMSettingVlanClass)) + +#define NM_SETTING_VLAN_SETTING_NAME "vlan" + +/** + * NMSettingVlanError: + * @NM_SETTING_VLAN_ERROR_UNKNOWN: unknown or unclassified error + * @NM_SETTING_VLAN_ERROR_INVALID_PROPERTY: the property was invalid + * @NM_SETTING_VLAN_ERROR_MISSING_PROPERTY: the property was missing and is + * required + */ +typedef enum { + NM_SETTING_VLAN_ERROR_UNKNOWN = 0, + NM_SETTING_VLAN_ERROR_INVALID_PROPERTY, + NM_SETTING_VLAN_ERROR_MISSING_PROPERTY +} NMSettingVlanError; + +#define NM_TYPE_SETTING_VLAN_ERROR (nm_setting_vlan_error_get_type ()) +GType nm_setting_vlan_error_get_type (void); + +#define NM_SETTING_VLAN_ERROR nm_setting_vlan_error_quark () +GQuark nm_setting_vlan_error_quark (void); + +GType nm_setting_vlan_get_type (void); + +#define NM_SETTING_VLAN_INTERFACE_NAME "interface-name" +#define NM_SETTING_VLAN_SLAVE "slave" +#define NM_SETTING_VLAN_ID "id" +#define NM_SETTING_VLAN_FLAGS "flags" +#define NM_SETTING_VLAN_INGRESS_PRIORITY_MAP "ingress-priority-map" +#define NM_SETTING_VLAN_EGRESS_PRIORITY_MAP "egress-priority-map" + +typedef struct { + NMSetting parent; +} NMSettingVlan; + +typedef struct { + NMSettingClass parent; + + /* Padding for future expansion */ + void (*_reserved1) (void); + void (*_reserved2) (void); + void (*_reserved3) (void); + void (*_reserved4) (void); +} NMSettingVlanClass; + +typedef enum { + NM_VLAN_INGRESS_MAP, + NM_VLAN_EGRESS_MAP +} NMVlanPriorityMap; + +typedef struct { + guint32 from; + guint32 to; +} vlan_priority_map; + +enum NMVlanFlags { + NM_VLAN_FLAG_REORDER_HDR = 0x1, + NM_VLAN_FLAG_GVRP = 0x2, + NM_VLAN_FLAG_LOOSE_BINDING = 0x4, +}; + +NMSetting *nm_setting_vlan_new (void); +const char *nm_setting_vlan_get_interface_name (NMSettingVlan *setting); +const char *nm_setting_vlan_get_slave (NMSettingVlan *setting); +guint32 nm_setting_vlan_get_id (NMSettingVlan *setting); +guint32 nm_setting_vlan_get_flags (NMSettingVlan *setting); + +const GSList *nm_setting_vlan_get_ingress_priority_map (NMSettingVlan *setting); +const GSList *nm_setting_vlan_get_egress_priority_map (NMSettingVlan *setting); + +gint32 nm_setting_vlan_get_num_priorities (NMVlanPriorityMap map, NMSettingVlan *setting); +gboolean nm_setting_vlan_get_priority (NMVlanPriorityMap map, NMSettingVlan *setting, guint32 idx, + guint32 *out_a, guint32 *out_b); +gboolean nm_setting_vlan_add_priority (NMVlanPriorityMap map, NMSettingVlan *setting, guint32 a, guint32 b); +void nm_setting_vlan_remove_priority (NMVlanPriorityMap map, NMSettingVlan *setting, guint32 idx); +void nm_setting_vlan_clear_priorities (NMVlanPriorityMap map, NMSettingVlan *setting); +gboolean nm_setting_vlan_add_priority_str (NMSettingVlan *setting, NMVlanPriorityMap map, const char *str); + +G_END_DECLS + +#endif /* NM_SETTING_VLAN_H */ diff --git a/src/settings/plugins/ifcfg-rh/common.h b/src/settings/plugins/ifcfg-rh/common.h index 13b89d7..7993af2 100644 --- a/src/settings/plugins/ifcfg-rh/common.h +++ b/src/settings/plugins/ifcfg-rh/common.h @@ -46,6 +46,7 @@ #define TYPE_INFINIBAND "Infiniband" #define TYPE_BRIDGE "Bridge" #define TYPE_BOND "Bond" +#define TYPE_VLAN "Vlan" #define SECRET_FLAG_AGENT "user" #define SECRET_FLAG_NOT_SAVED "ask" diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index 521a9d0..0d9eca3 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -46,6 +46,7 @@ #include <NetworkManager.h> #include <nm-setting-connection.h> #include <nm-setting-ip4-config.h> +#include <nm-setting-vlan.h> #include <nm-setting-ip6-config.h> #include <nm-setting-wired.h> #include <nm-setting-wireless.h> @@ -3617,10 +3618,163 @@ is_bond_device (const char *name, shvarFile *parsed) return FALSE; } +static NMSetting * +make_vlan_setting (shvarFile *ifcfg, + const char *file, + gboolean nm_controlled, + char **unmanaged, + NMSetting8021x **s_8021x, + GError **error) +{ + NMSettingVlan *s_vlan = NULL; + char *value = NULL; + char *interface_name = NULL; + char *vlan_slave = NULL; + char *p = NULL; + guint32 vlan_id = 0; + guint32 vlan_flags = 0; + + s_vlan = NM_SETTING_VLAN (nm_setting_vlan_new ()); + + interface_name = svGetValue (ifcfg, "DEVICE", FALSE); + if (!interface_name) + goto error_return; + else { + g_object_set (s_vlan, NM_SETTING_VLAN_INTERFACE_NAME, interface_name, NULL); + + p = g_utf8_strchr (interface_name, -1, '.'); + if (p) { + /* eth0.43 */ + p++; + } else { + /* vlan43 */ + p = interface_name + 4; + } + vlan_id = g_ascii_strtoull (p, NULL, 10); + if (vlan_id >= 0 && vlan_id < 4096) + g_object_set (s_vlan, NM_SETTING_VLAN_ID, vlan_id, NULL); + else + goto free_interface_name; + } + + vlan_slave = svGetValue (ifcfg, "PHYSDEV", FALSE); + if (vlan_slave) { + g_object_set (s_vlan, NM_SETTING_VLAN_SLAVE, vlan_slave, NULL); + g_free (vlan_slave); + } + + value = svGetValue (ifcfg, "REORDER_HDR", FALSE); + if (value) + vlan_flags |= NM_VLAN_FLAG_REORDER_HDR; + g_free (value); + + value = svGetValue (ifcfg, "VLAN_FLAGS", FALSE); + if (g_strstr_len (value, -1, "GVRP")) + vlan_flags |= NM_VLAN_FLAG_GVRP; + if (g_strstr_len (value, -1, "LOOSE_BINDING")) + vlan_flags |= NM_VLAN_FLAG_LOOSE_BINDING; + + g_object_set (s_vlan, NM_SETTING_VLAN_FLAGS, vlan_flags, NULL); + g_free (value); + + value = svGetValue (ifcfg, "VLAN_INGRESS_PRIORITY_MAP", FALSE); + if (value) { + gchar **list = NULL, **iter; + list = g_strsplit_set (value, ",", -1); + for (iter = list; iter && *iter; iter++) { + if (!g_strrstr (*iter, ":")) + continue; + + if (!nm_setting_vlan_add_priority_str (s_vlan, NM_VLAN_INGRESS_MAP, *iter)) + PLUGIN_WARN (IFCFG_PLUGIN_NAME, " warning: invalid priority map '%s'", *iter); + } + g_free (value); + g_strfreev (list); + } + + value = svGetValue (ifcfg, "VLAN_EGRESS_PRIORITY_MAP", FALSE); + if (value) { + gchar **list = NULL, **iter; + list = g_strsplit_set (value, ",", -1); + for (iter = list; iter && *iter; iter++) { + if (!g_strrstr (*iter, ":")) + continue; + + if (!nm_setting_vlan_add_priority_str (s_vlan, NM_VLAN_EGRESS_MAP, *iter)) + PLUGIN_WARN (IFCFG_PLUGIN_NAME, " warning: invalid priority map '%s'", *iter); + } + g_free (value); + g_strfreev (list); + } + + return (NMSetting *) s_vlan; + +free_interface_name: + g_free (interface_name); +error_return: + g_object_unref (s_vlan); + return NULL; +} + +static NMConnection * +vlan_connection_from_ifcfg (const char *file, + shvarFile *ifcfg, + gboolean nm_controlled, + char **unmanaged, + GError **error) +{ + NMConnection *connection = NULL; + NMSetting *con_setting = NULL; + NMSetting *wired_setting = NULL; + NMSetting *vlan_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_VLAN_SETTING_NAME, NULL, "Vlan"); + 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); + + vlan_setting = make_vlan_setting (ifcfg, file, nm_controlled, unmanaged, &s_8021x, error); + if (!vlan_setting) { + g_object_unref (connection); + return NULL; + } + nm_connection_add_setting (connection, vlan_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; +} + enum { IGNORE_REASON_NONE = 0x00, IGNORE_REASON_BRIDGE = 0x01, - IGNORE_REASON_VLAN = 0x02, }; NMConnection * @@ -3740,7 +3894,7 @@ connection_from_file (const char *filename, goto done; } - /* Ignore BRIDGE= and VLAN= connections for now too (rh #619863) */ + /* Ignore BRIDGE= connections for now too (rh #619863) */ tmp = svGetValue (parsed, "BRIDGE", FALSE); if (tmp) { g_free (tmp); @@ -3748,15 +3902,6 @@ connection_from_file (const char *filename, ignore_reason = IGNORE_REASON_BRIDGE; } - if (nm_controlled) { - tmp = svGetValue (parsed, "VLAN", FALSE); - if (tmp) { - g_free (tmp); - nm_controlled = FALSE; - ignore_reason = IGNORE_REASON_VLAN; - } - } - /* Construct the connection */ if (!strcasecmp (type, TYPE_ETHERNET)) connection = wired_connection_from_ifcfg (filename, parsed, nm_controlled, unmanaged, &error); @@ -3764,11 +3909,13 @@ 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)) { + else if (!strcasecmp (type, TYPE_BOND)) + connection = bond_connection_from_ifcfg (filename, parsed, nm_controlled, unmanaged, &error); + else if (!strcasecmp (type, TYPE_VLAN)) + connection = vlan_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)) - connection = bond_connection_from_ifcfg (filename, parsed, nm_controlled, unmanaged, &error); else { g_set_error (&error, IFCFG_PLUGIN_ERROR, 0, "Unknown connection type '%s'", type); diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-vlan-interface b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-vlan-interface index 6c84185..bb0da81 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-vlan-interface +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-test-vlan-interface @@ -1,7 +1,12 @@ -DEVICE=eth1.43 VLAN=yes +TYPE=Vlan +DEVICE=vlan43 +PHYSDEV=eth9 +REORDER_HDR=0 +VLAN_FLAGS=GVRP,LOOSE_BINDING +VLAN_INGRESS_PRIORITY_MAP=0:1,2:5 +VLAN_EGRESS_PRIORITY_MAP=12:3,14:7 ONBOOT=yes -BOOTPROTO=none +BOOTPROTO=static IPADDR=192.168.43.149 NETMASK=255.255.255.0 - diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 15a3c7f..d36a01a 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -11765,9 +11765,10 @@ test_read_vlan_interface (void) &route6file, &error, &ignore_error); - ASSERT (connection == NULL, + ASSERT (connection != NULL, "vlan-interface-read", "unexpected success reading %s", TEST_IFCFG_VLAN_INTERFACE); + nm_connection_dump(connection); g_free (unmanaged); g_free (keyfile); g_free (routefile); -- 1.7.4.4 _______________________________________________ networkmanager-list mailing list networkmanager-list@gnome.org http://mail.gnome.org/mailman/listinfo/networkmanager-list