ifcfg-rh: add support for 'match' setting

This commit is contained in:
Beniamino Galvani 2018-08-10 17:31:59 +02:00
parent 9b9dce9486
commit d47e0beb7d
5 changed files with 140 additions and 1 deletions

View file

@ -2254,6 +2254,7 @@ EXTRA_DIST += \
src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_Wired_Auto-Negotiate.cexpected \
src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_Wired_Static_Routes.cexpected \
src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_Wired_Wake-on-LAN.cexpected \
src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Test_Write_Wired_match.cexpected \
src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-Vlan_test-vlan-interface.cexpected \
src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-dcb-test.cexpected \
src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-netmask-1 \

View file

@ -1218,6 +1218,32 @@ make_user_setting (shvarFile *ifcfg)
: NULL;
}
static NMSetting *
make_match_setting (shvarFile *ifcfg)
{
NMSettingMatch *s_match = NULL;
gs_free const char **strv = NULL;
gs_free char *value = NULL;
const char *v;
gsize i;
v = svGetValueStr (ifcfg, "MATCH_INTERFACE_NAME", &value);
if (!v)
return NULL;
strv = nm_utils_strsplit_set (v, " \t", TRUE);
if (strv) {
for (i = 0; strv[i]; i++) {
if (!s_match)
s_match = (NMSettingMatch *) nm_setting_match_new ();
nm_setting_match_add_interface_name (s_match,
_nm_utils_unescape_spaces ((char *) strv[i]));
}
}
return (NMSetting *) s_match;
}
static NMSetting *
make_proxy_setting (shvarFile *ifcfg)
{
@ -5441,7 +5467,7 @@ connection_from_file_full (const char *filename,
gs_free char *type = NULL;
char *devtype, *bootproto;
NMSetting *s_ip4, *s_ip6, *s_tc, *s_proxy, *s_port, *s_dcb = NULL, *s_user;
NMSetting *s_sriov;
NMSetting *s_sriov, *s_match;
const char *ifcfg_name = NULL;
gboolean has_ip4_defroute = FALSE;
gboolean has_complex_routes_v4;
@ -5721,6 +5747,10 @@ connection_from_file_full (const char *filename,
if (s_user)
nm_connection_add_setting (connection, s_user);
s_match = make_match_setting (parsed);
if (s_match)
nm_connection_add_setting (connection, s_match);
/* Bridge port? */
s_port = make_bridge_port_setting (parsed);
if (s_port)

View file

@ -2285,6 +2285,38 @@ write_tc_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
return TRUE;
}
static gboolean
write_match_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
{
NMSettingMatch *s_match;
nm_auto_free_gstring GString *str = NULL;
guint i, num;
svUnsetValue (ifcfg, "MATCH_INTERFACE_NAME");
s_match = (NMSettingMatch *) nm_connection_get_setting (connection, NM_TYPE_SETTING_MATCH);
if (!s_match)
return TRUE;
num = nm_setting_match_get_num_interface_names (s_match);
for (i = 0; i < num; i++) {
gs_free char *to_free = NULL;
const char *name;
if (i == 0)
str = g_string_new ("");
else
g_string_append_c (str, ' ');
name = nm_setting_match_get_interface_name (s_match, i);
g_string_append (str, _nm_utils_escape_spaces (name, &to_free));
}
if (str)
svSetValueStr (ifcfg, "MATCH_INTERFACE_NAME", str->str);
return TRUE;
}
static void
write_res_options (shvarFile *ifcfg, NMSettingIPConfig *s_ip, const char *var)
{
@ -3030,6 +3062,9 @@ do_write_construct (NMConnection *connection,
if (!write_user_setting (connection, ifcfg, error))
return FALSE;
if (!write_match_setting (connection, ifcfg, error))
return FALSE;
write_sriov_setting (connection, ifcfg);
if (!write_tc_setting (connection, ifcfg, error))

View file

@ -0,0 +1,11 @@
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
MATCH_INTERFACE_NAME="ens* eth\\ 1? !veth*"
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME="Test Write Wired with Match setting"
UUID=${UUID}
ONBOOT=yes

View file

@ -4434,6 +4434,67 @@ test_write_wired_dhcp (void)
nmtst_assert_connection_equals (connection, TRUE, reread, FALSE);
}
static void
test_write_wired_match (void)
{
nmtst_auto_unlinkfile char *testfile = NULL;
gs_unref_object NMConnection *connection = NULL;
gs_unref_object NMConnection *reread = NULL;
NMSettingConnection *s_con;
NMSettingWired *s_wired;
NMSettingMatch *s_match;
NMSettingIPConfig *s_ip4;
NMSettingIPConfig *s_ip6;
connection = nm_simple_connection_new ();
/* Connection setting */
s_con = (NMSettingConnection *) nm_setting_connection_new ();
nm_connection_add_setting (connection, NM_SETTING (s_con));
g_object_set (s_con,
NM_SETTING_CONNECTION_ID, "Test Write Wired with Match setting",
NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (),
NM_SETTING_CONNECTION_AUTOCONNECT, TRUE,
NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME,
NULL);
/* Wired setting */
s_wired = (NMSettingWired *) nm_setting_wired_new ();
nm_connection_add_setting (connection, NM_SETTING (s_wired));
/* IP4 setting */
s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new ();
nm_connection_add_setting (connection, NM_SETTING (s_ip4));
g_object_set (s_ip4,
NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
NULL);
/* IP6 setting */
s_ip6 = (NMSettingIPConfig *) nm_setting_ip6_config_new ();
nm_connection_add_setting (connection, NM_SETTING (s_ip6));
g_object_set (s_ip6,
NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE,
NULL);
/* Match setting */
s_match = (NMSettingMatch *) nm_setting_match_new ();
nm_setting_match_add_interface_name (s_match, "ens*");
nm_setting_match_add_interface_name (s_match, "eth 1?");
nm_setting_match_add_interface_name (s_match, "!veth*");
nm_connection_add_setting (connection, NM_SETTING (s_match));
nmtst_assert_connection_verifies (connection);
_writer_new_connec_exp (connection,
TEST_SCRATCH_DIR,
TEST_IFCFG_DIR"/ifcfg-Test_Write_Wired_match.cexpected",
&testfile);
reread = _connection_from_file (testfile, NULL, TYPE_ETHERNET, NULL);
nmtst_assert_connection_equals (connection, TRUE, reread, FALSE);
}
static void
test_write_wired_dhcp_plus_ip (void)
{
@ -10072,6 +10133,7 @@ int main (int argc, char **argv)
g_test_add_func (TPATH "wired/write/dhcp", test_write_wired_dhcp);
g_test_add_func (TPATH "wired/write-dhcp-plus-ip", test_write_wired_dhcp_plus_ip);
g_test_add_func (TPATH "wired/write/dhcp-8021x-peap-mschapv2", test_write_wired_dhcp_8021x_peap_mschapv2);
g_test_add_func (TPATH "wired/write/match", test_write_wired_match);
#define _add_test_write_wired_8021x_tls(testpath, scheme, flags) \
nmtst_add_test_func (testpath, test_write_wired_8021x_tls, GINT_TO_POINTER (scheme), GINT_TO_POINTER (flags))