From e71e865dc3bf823edf9333ac88413a3e6e3a1268 Mon Sep 17 00:00:00 2001 From: Chris Aumann Date: Tue, 19 Sep 2023 13:49:39 +0200 Subject: [PATCH 1/4] Migrate CriticalConnection (deprecated) to KeepConfiguration See: https://bugs.launchpad.net/netplan/+bug/1896799 --- abi-compat/jammy_0.107.xml | 8 ++++---- doc/netplan-yaml.md | 13 ++++++++----- netplan_cli/cli/commands/apply.py | 10 +++++----- python-cffi/netplan/_build_cffi.py | 2 +- python-cffi/netplan/netdef.py | 4 ++-- src/abi.h | 2 +- src/netplan.c | 4 ++-- src/networkd.c | 10 +++++----- src/parse.c | 2 +- src/types.c | 8 ++++---- src/util-internal.h | 4 ++-- tests/generator/test_common.py | 6 +++--- tests/test_libnetplan.py | 8 ++++---- 13 files changed, 42 insertions(+), 39 deletions(-) diff --git a/abi-compat/jammy_0.107.xml b/abi-compat/jammy_0.107.xml index f5c1b4092..3b64b4d3a 100644 --- a/abi-compat/jammy_0.107.xml +++ b/abi-compat/jammy_0.107.xml @@ -15,7 +15,7 @@ - + @@ -421,7 +421,7 @@ - + @@ -4191,9 +4191,9 @@ - + - + diff --git a/doc/netplan-yaml.md b/doc/netplan-yaml.md index 9963f6d1a..d9b248c10 100644 --- a/doc/netplan-yaml.md +++ b/doc/netplan-yaml.md @@ -374,11 +374,14 @@ Match devices by MAC when setting options like: `wakeonlan` or `*-offload`. > (networkd backend only) Allow the specified interface to be configured even > if it has no carrier. -- **critical** (bool) - - > Designate the connection as "critical to the system", meaning that special - > care will be taken by to not release the assigned IP when the daemon is - > restarted. (not recognized by NetworkManager) +- **keep-configuration** (scalar) + + > When set to "static", static addresses and routes won't be dropped on starting up process. + > When set to "dhcp-on-stop", addresses and routes won't be dropped when stopping the daemon. + > When set to "dhcp", addresses and routes provided by a DHCP server will never be dropped even if the DHCP lease expires, implies "dhcp-on-stop" + > When set to "yes", "dhcp" and "static" is implied. + > Defaults to "no". + > (not recognized by NetworkManager) - **dhcp-identifier** (scalar) diff --git a/netplan_cli/cli/commands/apply.py b/netplan_cli/cli/commands/apply.py index 8bf8a9477..dff832321 100644 --- a/netplan_cli/cli/commands/apply.py +++ b/netplan_cli/cli/commands/apply.py @@ -233,7 +233,7 @@ def command_apply(self, run_generate=True, sync=False, exit_on_error=True, state devices_after_udev = netifaces.interfaces() # apply some more changes manually for iface, settings in changes.items(): - # rename non-critical network interfaces + # rename network interfaces without keep_configuration set new_name = settings.get('name') if new_name: if len(new_name) >= IF_NAMESIZE: @@ -356,7 +356,7 @@ def clear_virtual_links(prev_links, curr_links, devices=[]): def process_link_changes(interfaces, config_manager: ConfigManager): # pragma: nocover (covered in autopkgtest) """ Go through the pending changes and pick what needs special handling. - Only applies to non-critical interfaces which can be safely updated. + Only applies to interfaces not having keep_configuration set, which can be safely updated. """ changes = {} @@ -384,9 +384,9 @@ def process_link_changes(interfaces, config_manager: ConfigManager): # pragma: # Skip interface if it already has the correct name logging.debug('Skipping correctly named interface: {}'.format(newname)) continue - if netdef.critical: - # Skip interfaces defined as critical, as we should not take them down in order to rename - logging.warning('Cannot rename {} ({} -> {}) at runtime (needs reboot), due to being critical' + if netdef.keep_configuration: + # Skip interfaces with keep_configuration set, as we should not take them down in order to rename + logging.warning('Cannot rename {} ({} -> {}) at runtime (needs reboot), due to keep_configuration being set' .format(netdef.id, current_iface_name, newname)) continue diff --git a/python-cffi/netplan/_build_cffi.py b/python-cffi/netplan/_build_cffi.py index ba0af4ec8..c031f5dd1 100644 --- a/python-cffi/netplan/_build_cffi.py +++ b/python-cffi/netplan/_build_cffi.py @@ -112,7 +112,7 @@ ssize_t _netplan_netdef_get_embedded_switch_mode(const NetplanNetDefinition* netdef, char* out_buffer, size_t out_buf_size); gboolean _netplan_netdef_get_sriov_vlan_filter(const NetplanNetDefinition* netdef); guint _netplan_netdef_get_vlan_id(const NetplanNetDefinition* netdef); - gboolean _netplan_netdef_get_critical(const NetplanNetDefinition* netdef); + char* _netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef); gboolean _netplan_netdef_is_trivial_compound_itf(const NetplanNetDefinition* netdef); int _netplan_state_get_vf_count_for_def( const NetplanState* np_state, const NetplanNetDefinition* netdef, NetplanError** error); diff --git a/python-cffi/netplan/netdef.py b/python-cffi/netplan/netdef.py index cc2372567..495d687c4 100644 --- a/python-cffi/netplan/netdef.py +++ b/python-cffi/netplan/netdef.py @@ -75,8 +75,8 @@ def set_name(self) -> str: return _string_realloc_call_no_error(lambda b: lib.netplan_netdef_get_set_name(self._ptr, b, len(b))) @property - def critical(self) -> bool: - return bool(lib._netplan_netdef_get_critical(self._ptr)) + def keep_configuration(self) -> str: + return _string_realloc_call_no_error(lambda b: lib.netplan_netdef_get_keep_configuration(self._ptr, b, len(b))) @property def links(self) -> dict: diff --git a/src/abi.h b/src/abi.h index 11f62ab07..7c7eb04c3 100644 --- a/src/abi.h +++ b/src/abi.h @@ -211,7 +211,7 @@ struct netplan_net_definition { /* status options */ gboolean optional; NetplanOptionalAddressFlag optional_addresses; - gboolean critical; + char* keep_configuration; /* addresses */ gboolean dhcp4; diff --git a/src/netplan.c b/src/netplan.c index 81bddb534..46b100c81 100644 --- a/src/netplan.c +++ b/src/netplan.c @@ -742,8 +742,8 @@ _serialize_yaml( if (def->optional) YAML_NONNULL_STRING_PLAIN(event, emitter, "optional", "true"); - if (def->critical) - YAML_NONNULL_STRING_PLAIN(event, emitter, "critical", "true"); + if (def->keep_configuration) + YAML_STRING_PLAIN(def, event, emitter, "keep-configuration", def->keep_configuration); if (def->ignore_carrier) YAML_NONNULL_STRING_PLAIN(event, emitter, "ignore-carrier", "true"); diff --git a/src/networkd.c b/src/networkd.c index 580304c37..9253320c7 100644 --- a/src/networkd.c +++ b/src/networkd.c @@ -353,7 +353,7 @@ write_bond_parameters(const NetplanNetDefinition* def, GString* s) if (def->bond_params.selection_logic) g_string_append_printf(params, "\nAdSelect=%s", def->bond_params.selection_logic); if (def->bond_params.all_members_active) - g_string_append_printf(params, "\nAllSlavesActive=%d", def->bond_params.all_members_active); /* wokeignore:rule=slave */ + g_string_append_printf(params, "\nAllSlavesActive=%d", def->bond_params.all_members_active); /* wokeignore:rule=slave */ if (def->bond_params.arp_interval) { g_string_append(params, "\nARPIntervalSec="); if (interval_has_suffix(def->bond_params.arp_interval)) @@ -393,7 +393,7 @@ write_bond_parameters(const NetplanNetDefinition* def, GString* s) g_string_append_printf(params, "\nGratuitousARP=%d", def->bond_params.gratuitous_arp); /* TODO: add unsolicited_na, not documented as supported by NM. */ if (def->bond_params.packets_per_member) - g_string_append_printf(params, "\nPacketsPerSlave=%d", def->bond_params.packets_per_member); /* wokeignore:rule=slave */ + g_string_append_printf(params, "\nPacketsPerSlave=%d", def->bond_params.packets_per_member); /* wokeignore:rule=slave */ if (def->bond_params.primary_reselect_policy) g_string_append_printf(params, "\nPrimaryReselectPolicy=%s", def->bond_params.primary_reselect_policy); if (def->bond_params.resend_igmp) @@ -916,13 +916,13 @@ netplan_netdef_write_network_file( } } - if (def->dhcp4 || def->dhcp6 || def->critical) { + if (def->dhcp4 || def->dhcp6 || def->keep_configuration) { /* NetworkManager compatible route metrics */ g_string_append(network, "\n[DHCP]\n"); } - if (def->critical) - g_string_append_printf(network, "CriticalConnection=true\n"); + if (def->keep_configuration) + g_string_append_printf(network, "KeepConfiguration=%s\n", def->keep_configuration); if (def->dhcp4 || def->dhcp6) { if (def->dhcp_identifier) diff --git a/src/parse.c b/src/parse.c index c8b0bb2cc..743808030 100644 --- a/src/parse.c +++ b/src/parse.c @@ -2785,7 +2785,7 @@ static const mapping_entry_handler dhcp6_overrides_handlers[] = { {"accept-ra", YAML_SCALAR_NODE, {.generic=handle_accept_ra}, netdef_offset(accept_ra)}, \ {"activation-mode", YAML_SCALAR_NODE, {.generic=handle_activation_mode}, netdef_offset(activation_mode)}, \ {"addresses", YAML_SEQUENCE_NODE, {.generic=handle_addresses}, NULL}, \ - {"critical", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(critical)}, \ + {"keep-configuration", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(keep_configuration)}, \ {"ignore-carrier", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(ignore_carrier)}, \ {"dhcp4", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(dhcp4)}, \ {"dhcp6", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(dhcp6)}, \ diff --git a/src/types.c b/src/types.c index 462389087..2aebbd9f6 100644 --- a/src/types.c +++ b/src/types.c @@ -225,7 +225,7 @@ reset_netdef(NetplanNetDefinition* netdef, NetplanDefType new_type, NetplanBacke netdef->optional = FALSE; netdef->optional_addresses = 0; - netdef->critical = FALSE; + FREE_AND_NULLIFY(netdef->keep_configuration); netdef->dhcp4 = FALSE; netdef->dhcp6 = FALSE; @@ -594,11 +594,11 @@ _netplan_netdef_get_vlan_id(const NetplanNetDefinition* netdef) return netdef->vlan_id; } -gboolean -_netplan_netdef_get_critical(const NetplanNetDefinition* netdef) +char * +_netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef) { g_assert(netdef); - return netdef->critical; + return netdef->keep_configuration; } gboolean diff --git a/src/util-internal.h b/src/util-internal.h index a27e771dd..41e98486c 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -102,8 +102,8 @@ _netplan_state_get_vf_count_for_def(const NetplanState* np_state, const NetplanN NETPLAN_INTERNAL gboolean _netplan_netdef_get_sriov_vlan_filter(const NetplanNetDefinition* netdef); -NETPLAN_INTERNAL gboolean -_netplan_netdef_get_critical(const NetplanNetDefinition* netdef); +NETPLAN_INTERNAL char * +_netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef); NETPLAN_INTERNAL gboolean _netplan_netdef_get_optional(const NetplanNetDefinition* netdef); diff --git a/tests/generator/test_common.py b/tests/generator/test_common.py index ad96b787b..e8a89d558 100644 --- a/tests/generator/test_common.py +++ b/tests/generator/test_common.py @@ -467,12 +467,12 @@ def test_bond_arp_ip_targets_multi_pass(self): Bond=bond0 '''}) - def test_dhcp_critical_true(self): + def test_dhcp_keep_configuration_true(self): self.generate('''network: version: 2 ethernets: engreen: - critical: yes + keep-configuration: yes ''') self.assert_networkd({'engreen.network': '''[Match] @@ -482,7 +482,7 @@ def test_dhcp_critical_true(self): LinkLocalAddressing=ipv6 [DHCP] -CriticalConnection=true +KeepConfiguration=true '''}) def test_dhcp_identifier_mac(self): diff --git a/tests/test_libnetplan.py b/tests/test_libnetplan.py index 134cfa0c6..811588649 100644 --- a/tests/test_libnetplan.py +++ b/tests/test_libnetplan.py @@ -581,15 +581,15 @@ def test_backend(self): self.assertEqual(state['eth0'].backend, 'networkd') self.assertEqual(state['eth1'].backend, 'NetworkManager') - def test_critical(self): + def test_keep_configuration(self): state = state_from_yaml(self.confdir, '''network: ethernets: eth0: - critical: true + keep-configuration: true eth1: {}''') - self.assertTrue(state['eth0'].critical) - self.assertFalse(state['eth1'].critical) + self.assertTrue(state['eth0'].keep_configuration) + self.assertFalse(state['eth1'].keep_configuration) def test_eq(self): state = state_from_yaml(self.confdir, '''network: From 7a1914e181bb5f34884b6810ce805b06b8177384 Mon Sep 17 00:00:00 2001 From: Chris Aumann Date: Tue, 19 Sep 2023 16:06:30 +0200 Subject: [PATCH 2/4] Make netplan_netdef_get_keep_configuration() public --- abi-compat/jammy_0.107.xml | 10 +++++----- include/netplan.h | 3 +++ python-cffi/netplan/_build_cffi.py | 2 +- src/types.c | 7 ------- src/util-internal.h | 3 --- src/util.c | 7 +++++++ 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/abi-compat/jammy_0.107.xml b/abi-compat/jammy_0.107.xml index 3b64b4d3a..4bb4b014f 100644 --- a/abi-compat/jammy_0.107.xml +++ b/abi-compat/jammy_0.107.xml @@ -15,7 +15,6 @@ - @@ -59,6 +58,7 @@ + @@ -4191,10 +4191,6 @@ - - - - @@ -4373,6 +4369,10 @@ + + + + diff --git a/include/netplan.h b/include/netplan.h index 8fbf3fba9..b083d9896 100644 --- a/include/netplan.h +++ b/include/netplan.h @@ -118,6 +118,9 @@ netplan_netdef_get_vlan_link(const NetplanNetDefinition* netdef); NETPLAN_PUBLIC NetplanNetDefinition* netplan_netdef_get_sriov_link(const NetplanNetDefinition* netdef); +NETPLAN_PUBLIC char * +netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef); + NETPLAN_PUBLIC ssize_t netplan_netdef_get_set_name(const NetplanNetDefinition* netdef, char* out_buffer, size_t out_buffer_size); diff --git a/python-cffi/netplan/_build_cffi.py b/python-cffi/netplan/_build_cffi.py index c031f5dd1..496e643fe 100644 --- a/python-cffi/netplan/_build_cffi.py +++ b/python-cffi/netplan/_build_cffi.py @@ -112,7 +112,7 @@ ssize_t _netplan_netdef_get_embedded_switch_mode(const NetplanNetDefinition* netdef, char* out_buffer, size_t out_buf_size); gboolean _netplan_netdef_get_sriov_vlan_filter(const NetplanNetDefinition* netdef); guint _netplan_netdef_get_vlan_id(const NetplanNetDefinition* netdef); - char* _netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef); + char* netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef); gboolean _netplan_netdef_is_trivial_compound_itf(const NetplanNetDefinition* netdef); int _netplan_state_get_vf_count_for_def( const NetplanState* np_state, const NetplanNetDefinition* netdef, NetplanError** error); diff --git a/src/types.c b/src/types.c index 2aebbd9f6..92fd1f2ee 100644 --- a/src/types.c +++ b/src/types.c @@ -594,13 +594,6 @@ _netplan_netdef_get_vlan_id(const NetplanNetDefinition* netdef) return netdef->vlan_id; } -char * -_netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef) -{ - g_assert(netdef); - return netdef->keep_configuration; -} - gboolean _netplan_netdef_get_optional(const NetplanNetDefinition* netdef) { diff --git a/src/util-internal.h b/src/util-internal.h index 41e98486c..aea6b2db3 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -102,9 +102,6 @@ _netplan_state_get_vf_count_for_def(const NetplanState* np_state, const NetplanN NETPLAN_INTERNAL gboolean _netplan_netdef_get_sriov_vlan_filter(const NetplanNetDefinition* netdef); -NETPLAN_INTERNAL char * -_netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef); - NETPLAN_INTERNAL gboolean _netplan_netdef_get_optional(const NetplanNetDefinition* netdef); diff --git a/src/util.c b/src/util.c index b9b621c2f..8f7dcc453 100644 --- a/src/util.c +++ b/src/util.c @@ -1018,6 +1018,13 @@ netplan_netdef_match_interface(const NetplanNetDefinition* netdef, const char* n return TRUE; } +char * +netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef) +{ + g_assert(netdef); + return netdef->keep_configuration; +} + ssize_t netplan_netdef_get_set_name(const NetplanNetDefinition* netdef, char* out_buffer, size_t out_buf_size) { From 82442d85e8080a6583d6245cfaae96a95fd43def Mon Sep 17 00:00:00 2001 From: Chris Aumann Date: Fri, 22 Sep 2023 23:41:41 +0200 Subject: [PATCH 3/4] [squashme] revamp keep-configuration option backwards compatible --- abi-compat/jammy_0.107.xml | 12 ++++++------ doc/netplan-yaml.md | 15 ++++++++++----- netplan_cli/cli/commands/apply.py | 10 +++++----- src/abi.h | 10 +++++++++- src/netplan.c | 14 ++++++++++++-- src/networkd.c | 19 ++++++++++++++++--- src/parse.c | 2 +- src/types.c | 9 ++++++++- src/util-internal.h | 3 +++ src/util.c | 7 ------- tests/generator/test_common.py | 4 ++-- tests/test_libnetplan.py | 8 ++++---- 12 files changed, 76 insertions(+), 37 deletions(-) diff --git a/abi-compat/jammy_0.107.xml b/abi-compat/jammy_0.107.xml index 4bb4b014f..f5c1b4092 100644 --- a/abi-compat/jammy_0.107.xml +++ b/abi-compat/jammy_0.107.xml @@ -15,6 +15,7 @@ + @@ -58,7 +59,6 @@ - @@ -421,7 +421,7 @@ - + @@ -4191,6 +4191,10 @@ + + + + @@ -4369,10 +4373,6 @@ - - - - diff --git a/doc/netplan-yaml.md b/doc/netplan-yaml.md index d9b248c10..3071cdefc 100644 --- a/doc/netplan-yaml.md +++ b/doc/netplan-yaml.md @@ -374,14 +374,19 @@ Match devices by MAC when setting options like: `wakeonlan` or `*-offload`. > (networkd backend only) Allow the specified interface to be configured even > if it has no carrier. -- **keep-configuration** (scalar) +- **critical** (scalar) - > When set to "static", static addresses and routes won't be dropped on starting up process. - > When set to "dhcp-on-stop", addresses and routes won't be dropped when stopping the daemon. - > When set to "dhcp", addresses and routes provided by a DHCP server will never be dropped even if the DHCP lease expires, implies "dhcp-on-stop" + > This option sets the systemd KeepConfiguration option. + > (not recognized by NetworkManager) + > + > When set to "static", static addresses and routes won't be dropped on + > starting up process. + > When set to "dhcp-on-stop", addresses and routes won't be dropped when + > stopping the daemon. + > When set to "dhcp", addresses and routes provided by a DHCP server will + > never be dropped even if the DHCP lease expires, implies "dhcp-on-stop". > When set to "yes", "dhcp" and "static" is implied. > Defaults to "no". - > (not recognized by NetworkManager) - **dhcp-identifier** (scalar) diff --git a/netplan_cli/cli/commands/apply.py b/netplan_cli/cli/commands/apply.py index dff832321..8bf8a9477 100644 --- a/netplan_cli/cli/commands/apply.py +++ b/netplan_cli/cli/commands/apply.py @@ -233,7 +233,7 @@ def command_apply(self, run_generate=True, sync=False, exit_on_error=True, state devices_after_udev = netifaces.interfaces() # apply some more changes manually for iface, settings in changes.items(): - # rename network interfaces without keep_configuration set + # rename non-critical network interfaces new_name = settings.get('name') if new_name: if len(new_name) >= IF_NAMESIZE: @@ -356,7 +356,7 @@ def clear_virtual_links(prev_links, curr_links, devices=[]): def process_link_changes(interfaces, config_manager: ConfigManager): # pragma: nocover (covered in autopkgtest) """ Go through the pending changes and pick what needs special handling. - Only applies to interfaces not having keep_configuration set, which can be safely updated. + Only applies to non-critical interfaces which can be safely updated. """ changes = {} @@ -384,9 +384,9 @@ def process_link_changes(interfaces, config_manager: ConfigManager): # pragma: # Skip interface if it already has the correct name logging.debug('Skipping correctly named interface: {}'.format(newname)) continue - if netdef.keep_configuration: - # Skip interfaces with keep_configuration set, as we should not take them down in order to rename - logging.warning('Cannot rename {} ({} -> {}) at runtime (needs reboot), due to keep_configuration being set' + if netdef.critical: + # Skip interfaces defined as critical, as we should not take them down in order to rename + logging.warning('Cannot rename {} ({} -> {}) at runtime (needs reboot), due to being critical' .format(netdef.id, current_iface_name, newname)) continue diff --git a/src/abi.h b/src/abi.h index 7c7eb04c3..45917d750 100644 --- a/src/abi.h +++ b/src/abi.h @@ -32,6 +32,14 @@ typedef enum { NETPLAN_OPTIONAL_STATIC = 1<<4, } NetplanOptionalAddressFlag; +typedef enum { + NETPLAN_CRITICAL_FALSE, + NETPLAN_CRITICAL_TRUE, + NETPLAN_CRITICAL_STATIC, + NETPLAN_CRITICAL_DHCP, + NETPLAN_CRITICAL_DHCP_ON_STOP, +} NetplanCriticalOption; + /* Fields below are valid for dhcp4 and dhcp6 unless otherwise noted. */ typedef struct dhcp_overrides { gboolean use_dns; @@ -211,7 +219,7 @@ struct netplan_net_definition { /* status options */ gboolean optional; NetplanOptionalAddressFlag optional_addresses; - char* keep_configuration; + NetplanCriticalOption critical; /* addresses */ gboolean dhcp4; diff --git a/src/netplan.c b/src/netplan.c index 46b100c81..5fd29dc46 100644 --- a/src/netplan.c +++ b/src/netplan.c @@ -742,8 +742,18 @@ _serialize_yaml( if (def->optional) YAML_NONNULL_STRING_PLAIN(event, emitter, "optional", "true"); - if (def->keep_configuration) - YAML_STRING_PLAIN(def, event, emitter, "keep-configuration", def->keep_configuration); + + if (def->critical == NETPLAN_CRITICAL_TRUE) { + YAML_NONNULL_STRING_PLAIN(event, emitter, "critical", "true"); + } else if (def->critical == NETPLAN_CRITICAL_FALSE) { + YAML_NONNULL_STRING_PLAIN(event, emitter, "critical", "false"); + } else if (def->critical == NETPLAN_CRITICAL_STATIC) { + YAML_NONNULL_STRING_PLAIN(event, emitter, "critical", "static"); + } else if (def->critical == NETPLAN_CRITICAL_DHCP) { + YAML_NONNULL_STRING_PLAIN(event, emitter, "critical", "dhcp"); + } else if (def->critical == NETPLAN_CRITICAL_DHCP_ON_STOP) { + YAML_NONNULL_STRING_PLAIN(event, emitter, "critical", "dhcp-on-stop"); + } if (def->ignore_carrier) YAML_NONNULL_STRING_PLAIN(event, emitter, "ignore-carrier", "true"); diff --git a/src/networkd.c b/src/networkd.c index 9253320c7..608d36e6c 100644 --- a/src/networkd.c +++ b/src/networkd.c @@ -916,13 +916,26 @@ netplan_netdef_write_network_file( } } - if (def->dhcp4 || def->dhcp6 || def->keep_configuration) { + if (def->dhcp4 || def->dhcp6 || def->critical) { /* NetworkManager compatible route metrics */ g_string_append(network, "\n[DHCP]\n"); } - if (def->keep_configuration) - g_string_append_printf(network, "KeepConfiguration=%s\n", def->keep_configuration); + switch (def->critical) { + case NETPLAN_CRITICAL_TRUE: + g_string_append(s, "KeepConnection=true\n"); + break; + case NETPLAN_CRITICAL_STATIC: + g_string_append(s, "KeepConnection=static\n"); + break; + case NETPLAN_CRITICAL_DHCP: + g_string_append(s, "KeepConnection=dhcp\n"); + break; + case NETPLAN_CRITICAL_DHCP_ON_STOP: + g_string_append(s, "KeepConnection=dhcp-on-stop\n"); + break; + default: g_assert_not_reached(); // LCOV_EXCL_LINE + } if (def->dhcp4 || def->dhcp6) { if (def->dhcp_identifier) diff --git a/src/parse.c b/src/parse.c index 743808030..c8b0bb2cc 100644 --- a/src/parse.c +++ b/src/parse.c @@ -2785,7 +2785,7 @@ static const mapping_entry_handler dhcp6_overrides_handlers[] = { {"accept-ra", YAML_SCALAR_NODE, {.generic=handle_accept_ra}, netdef_offset(accept_ra)}, \ {"activation-mode", YAML_SCALAR_NODE, {.generic=handle_activation_mode}, netdef_offset(activation_mode)}, \ {"addresses", YAML_SEQUENCE_NODE, {.generic=handle_addresses}, NULL}, \ - {"keep-configuration", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(keep_configuration)}, \ + {"critical", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(critical)}, \ {"ignore-carrier", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(ignore_carrier)}, \ {"dhcp4", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(dhcp4)}, \ {"dhcp6", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(dhcp6)}, \ diff --git a/src/types.c b/src/types.c index 92fd1f2ee..3c8b73b75 100644 --- a/src/types.c +++ b/src/types.c @@ -225,7 +225,7 @@ reset_netdef(NetplanNetDefinition* netdef, NetplanDefType new_type, NetplanBacke netdef->optional = FALSE; netdef->optional_addresses = 0; - FREE_AND_NULLIFY(netdef->keep_configuration); + netdef->critical = NETPLAN_CRITICAL_FALSE; netdef->dhcp4 = FALSE; netdef->dhcp6 = FALSE; @@ -594,6 +594,13 @@ _netplan_netdef_get_vlan_id(const NetplanNetDefinition* netdef) return netdef->vlan_id; } +NetplanCriticalOption +_netplan_netdef_get_critical(const NetplanNetDefinition* netdef) +{ + g_assert(netdef); + return netdef->critical; +} + gboolean _netplan_netdef_get_optional(const NetplanNetDefinition* netdef) { diff --git a/src/util-internal.h b/src/util-internal.h index aea6b2db3..2db88b75c 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -102,6 +102,9 @@ _netplan_state_get_vf_count_for_def(const NetplanState* np_state, const NetplanN NETPLAN_INTERNAL gboolean _netplan_netdef_get_sriov_vlan_filter(const NetplanNetDefinition* netdef); +NETPLAN_INTERNAL NetplanCriticalOption +_netplan_netdef_get_critical(const NetplanNetDefinition* netdef); + NETPLAN_INTERNAL gboolean _netplan_netdef_get_optional(const NetplanNetDefinition* netdef); diff --git a/src/util.c b/src/util.c index 8f7dcc453..b9b621c2f 100644 --- a/src/util.c +++ b/src/util.c @@ -1018,13 +1018,6 @@ netplan_netdef_match_interface(const NetplanNetDefinition* netdef, const char* n return TRUE; } -char * -netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef) -{ - g_assert(netdef); - return netdef->keep_configuration; -} - ssize_t netplan_netdef_get_set_name(const NetplanNetDefinition* netdef, char* out_buffer, size_t out_buf_size) { diff --git a/tests/generator/test_common.py b/tests/generator/test_common.py index e8a89d558..237142aaa 100644 --- a/tests/generator/test_common.py +++ b/tests/generator/test_common.py @@ -467,12 +467,12 @@ def test_bond_arp_ip_targets_multi_pass(self): Bond=bond0 '''}) - def test_dhcp_keep_configuration_true(self): + def test_dhcp_critical_true(self): self.generate('''network: version: 2 ethernets: engreen: - keep-configuration: yes + critical: yes ''') self.assert_networkd({'engreen.network': '''[Match] diff --git a/tests/test_libnetplan.py b/tests/test_libnetplan.py index 811588649..134cfa0c6 100644 --- a/tests/test_libnetplan.py +++ b/tests/test_libnetplan.py @@ -581,15 +581,15 @@ def test_backend(self): self.assertEqual(state['eth0'].backend, 'networkd') self.assertEqual(state['eth1'].backend, 'NetworkManager') - def test_keep_configuration(self): + def test_critical(self): state = state_from_yaml(self.confdir, '''network: ethernets: eth0: - keep-configuration: true + critical: true eth1: {}''') - self.assertTrue(state['eth0'].keep_configuration) - self.assertFalse(state['eth1'].keep_configuration) + self.assertTrue(state['eth0'].critical) + self.assertFalse(state['eth1'].critical) def test_eq(self): state = state_from_yaml(self.confdir, '''network: From 8ae3b07a1e796107f0238388586b8b41056f33a2 Mon Sep 17 00:00:00 2001 From: Chris Aumann Date: Sat, 23 Sep 2023 00:26:42 +0200 Subject: [PATCH 4/4] [squashme] howto pyhon enum plz? --- include/netplan.h | 3 --- python-cffi/netplan/_build_cffi.py | 3 ++- python-cffi/netplan/netdef.py | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/netplan.h b/include/netplan.h index b083d9896..8fbf3fba9 100644 --- a/include/netplan.h +++ b/include/netplan.h @@ -118,9 +118,6 @@ netplan_netdef_get_vlan_link(const NetplanNetDefinition* netdef); NETPLAN_PUBLIC NetplanNetDefinition* netplan_netdef_get_sriov_link(const NetplanNetDefinition* netdef); -NETPLAN_PUBLIC char * -netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef); - NETPLAN_PUBLIC ssize_t netplan_netdef_get_set_name(const NetplanNetDefinition* netdef, char* out_buffer, size_t out_buffer_size); diff --git a/python-cffi/netplan/_build_cffi.py b/python-cffi/netplan/_build_cffi.py index 496e643fe..4a136c6f6 100644 --- a/python-cffi/netplan/_build_cffi.py +++ b/python-cffi/netplan/_build_cffi.py @@ -34,6 +34,7 @@ typedef struct netplan_net_definition NetplanNetDefinition; typedef enum { ... } NetplanBackend; typedef enum { ... } NetplanDefType; + typedef enum { ... } NetplanCriticalOption; // TODO: Introduce getters for .address/.lifetime/.label to avoid exposing the raw struct typedef struct { @@ -112,7 +113,7 @@ ssize_t _netplan_netdef_get_embedded_switch_mode(const NetplanNetDefinition* netdef, char* out_buffer, size_t out_buf_size); gboolean _netplan_netdef_get_sriov_vlan_filter(const NetplanNetDefinition* netdef); guint _netplan_netdef_get_vlan_id(const NetplanNetDefinition* netdef); - char* netplan_netdef_get_keep_configuration(const NetplanNetDefinition* netdef); + NetplanCriticalOption _netplan_netdef_get_critical(const NetplanNetDefinition* netdef); gboolean _netplan_netdef_is_trivial_compound_itf(const NetplanNetDefinition* netdef); int _netplan_state_get_vf_count_for_def( const NetplanState* np_state, const NetplanNetDefinition* netdef, NetplanError** error); diff --git a/python-cffi/netplan/netdef.py b/python-cffi/netplan/netdef.py index 495d687c4..cc2372567 100644 --- a/python-cffi/netplan/netdef.py +++ b/python-cffi/netplan/netdef.py @@ -75,8 +75,8 @@ def set_name(self) -> str: return _string_realloc_call_no_error(lambda b: lib.netplan_netdef_get_set_name(self._ptr, b, len(b))) @property - def keep_configuration(self) -> str: - return _string_realloc_call_no_error(lambda b: lib.netplan_netdef_get_keep_configuration(self._ptr, b, len(b))) + def critical(self) -> bool: + return bool(lib._netplan_netdef_get_critical(self._ptr)) @property def links(self) -> dict: