[PATCH 5/5] vlan: add ifcfg-vlan writer



add write_vlan_setting() and modify test-ifcfg-rh.c to test it.

Signed-off-by: Weiping Pan <wpan redhat com>
---
 .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c         |   38 ++++++++
 src/settings/plugins/ifcfg-rh/writer.c             |   90 ++++++++++++++++++++
 2 files changed, 128 insertions(+), 0 deletions(-)

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 4b84a59..3c72407 100644
--- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c
+++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c
@@ -43,6 +43,7 @@
 #include <nm-setting-gsm.h>
 #include <nm-setting-cdma.h>
 #include <nm-setting-serial.h>
+#include <nm-setting-vlan.h>
 
 #include "nm-test-helpers.h"
 
@@ -11775,6 +11776,42 @@ test_read_vlan_interface (void)
 	g_free (route6file);
 }
 
+static void
+test_write_vlan(void)
+{
+	NMConnection *connection;
+	char *unmanaged = NULL;
+	char *keyfile = NULL;
+	char *routefile = NULL;
+	char *route6file = NULL;
+	gboolean ignore_error = FALSE;
+	GError *error = NULL;
+	gboolean success = FALSE;
+
+	connection = connection_from_file (TEST_IFCFG_VLAN_INTERFACE,
+	                                   NULL,
+	                                   TYPE_VLAN,
+	                                   NULL,
+	                                   &unmanaged,
+	                                   &keyfile,
+	                                   &routefile,
+	                                   &route6file,
+	                                   &error,
+	                                   &ignore_error);
+	g_assert (connection != NULL);
+
+	success = writer_new_connection (connection,
+	                                 TEST_SCRATCH_DIR "/network-scripts/",
+	                                 NULL,
+	                                 &error);
+	g_assert (success);
+
+	g_free (unmanaged);
+	g_free (keyfile);
+	g_free (routefile);
+	g_free (route6file);
+}
+
 #define TEST_IFCFG_WIFI_OPEN_SSID_BAD_HEX TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wifi-open-ssid-bad-hex"
 #define TEST_IFCFG_WIFI_OPEN_SSID_LONG_QUOTED TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wifi-open-ssid-long-quoted"
 #define TEST_IFCFG_WIFI_OPEN_SSID_LONG_HEX TEST_IFCFG_DIR"/network-scripts/ifcfg-test-wifi-open-ssid-long-hex"
@@ -11939,6 +11976,7 @@ int main (int argc, char **argv)
 	test_read_bridge_main ();
 	test_read_bridge_component ();
 	test_read_vlan_interface ();
+	test_write_vlan();
 
 	base = g_path_get_basename (argv[0]);
 	fprintf (stdout, "%s: SUCCESS\n", base);
diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c
index 068bcda..9124e25 100644
--- a/src/settings/plugins/ifcfg-rh/writer.c
+++ b/src/settings/plugins/ifcfg-rh/writer.c
@@ -36,6 +36,7 @@
 #include <nm-setting-ip4-config.h>
 #include <nm-setting-ip6-config.h>
 #include <nm-setting-pppoe.h>
+#include <nm-setting-vlan.h>
 #include <nm-utils.h>
 
 #include "common.h"
@@ -1064,6 +1065,92 @@ write_wired_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
 	return TRUE;
 }
 
+static GString *vlan_priority_maplist_to_stringlist(const GSList *list)
+{
+	GString *text = NULL;
+	const GSList *iterator = NULL;
+	vlan_priority_map *item = NULL;
+	gsize len = 0;
+
+	g_return_val_if_fail(list != NULL, NULL);
+	text = g_string_new("");
+
+	for (iterator = list; iterator; iterator = iterator->next) {
+		item = (vlan_priority_map*)(iterator->data);
+		g_string_append_printf(text, "%d:%d,", item->from, item->to);
+	}
+
+	len = text->len;
+	g_string_truncate(text, --len);
+	return text;
+}
+
+static gboolean
+write_vlan_setting(NMConnection *connection, shvarFile *ifcfg, GError **error)
+{
+	NMSettingVlan *s_vlan;
+	const char *interface_name = NULL;
+	const char *vlan_slave = NULL;
+	guint32 vlan_flags = 0;
+	const GSList *list= NULL;
+	GString *text = NULL;
+
+	s_vlan = (NMSettingVlan *) nm_connection_get_setting (connection, NM_TYPE_SETTING_VLAN);
+	if (!s_vlan) {
+		g_set_error (error, IFCFG_PLUGIN_ERROR, 0,
+		             "Missing '%s' setting", NM_SETTING_VLAN_SETTING_NAME);
+		return FALSE;
+	}
+
+	svSetValue (ifcfg, "TYPE", TYPE_VLAN, FALSE);
+	svSetValue (ifcfg, "VLAN", "yes", FALSE);
+
+	interface_name = nm_setting_vlan_get_interface_name(s_vlan);
+	if (!interface_name)
+		return FALSE;
+	svSetValue (ifcfg, "DEVICE", interface_name, FALSE);
+
+	vlan_slave = nm_setting_vlan_get_slave(s_vlan);
+	if (!vlan_slave)
+		return FALSE;
+	svSetValue (ifcfg, "PHYSDEV", vlan_slave, FALSE);
+
+	vlan_flags = nm_setting_vlan_get_flags(s_vlan);
+	if (vlan_flags & NM_VLAN_FLAG_REORDER_HDR)
+		svSetValue (ifcfg, "REORDER_HDR", "1", FALSE);
+	else
+		svSetValue (ifcfg, "REORDER_HDR", "0", FALSE);
+
+	if (vlan_flags & NM_VLAN_FLAG_GVRP)
+		if (vlan_flags & NM_VLAN_FLAG_LOOSE_BINDING)
+			svSetValue (ifcfg, "VLAN_FLAGS", "GVRP,LOOSE_BINDING", FALSE);
+		else
+			svSetValue (ifcfg, "VLAN_FLAGS", "GVRP", FALSE);
+	else
+		if (vlan_flags & NM_VLAN_FLAG_LOOSE_BINDING)
+			svSetValue (ifcfg, "VLAN_FLAGS", "LOOSE_BINDING", FALSE);
+
+	list = nm_setting_vlan_get_ingress_priority_map(s_vlan);
+	if (list) {
+		text = vlan_priority_maplist_to_stringlist(list);
+		if (text->len != 0)
+			svSetValue (ifcfg, "VLAN_INGRESS_PRIORITY_MAP", text->str, FALSE);
+		g_string_free(text, TRUE);
+		text = NULL;
+	}
+
+	list = nm_setting_vlan_get_egress_priority_map(s_vlan);
+	if (list) {
+		text = vlan_priority_maplist_to_stringlist(list);
+		if (text->len != 0)
+			svSetValue (ifcfg, "VLAN_EGRESS_PRIORITY_MAP", text->str, FALSE);
+		g_string_free(text, TRUE);
+		text = NULL;
+	}
+
+	return TRUE;
+}
+
 static void
 write_connection_setting (NMSettingConnection *s_con, shvarFile *ifcfg)
 {
@@ -1769,6 +1856,9 @@ write_connection (NMConnection *connection,
 		if (!write_wired_setting (connection, ifcfg, error))
 			goto out;
 		wired = TRUE;
+	} else if (!strcmp (type, NM_SETTING_VLAN_SETTING_NAME)) {
+		if (!write_vlan_setting(connection, ifcfg, error))
+			goto out;
 	} else if (!strcmp (type, NM_SETTING_WIRELESS_SETTING_NAME)) {
 		if (!write_wireless_setting (connection, ifcfg, &no_8021x, error))
 			goto out;
-- 
1.7.4.4



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