From a61738b3a637b17aa4208d98ae9fe01e9bba1c0a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 21 Dec 2023 04:08:53 +0900 Subject: [PATCH 1/3] network/address: make Address= in [Network] support an empty string Closes #30485. --- man/systemd.network.xml | 3 +++ src/network/networkd-address.c | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/man/systemd.network.xml b/man/systemd.network.xml index 7dc447d0a7f..b587573a5f6 100644 --- a/man/systemd.network.xml +++ b/man/systemd.network.xml @@ -662,6 +662,9 @@ Table=1234 number of dynamically created network interfaces with the same network configuration and automatic address range assignment. + If an empty string is specified, then the all previous assignments in both [Network] and + [Address] sections are cleared. + diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index e3bdc66c587..ad49793a168 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -1994,10 +1994,16 @@ int config_parse_address( assert(rvalue); assert(data); - if (streq(section, "Network")) + if (streq(section, "Network")) { + if (isempty(rvalue)) { + /* If an empty string specified in [Network] section, clear previously assigned addresses. */ + network->addresses_by_section = ordered_hashmap_free_with_destructor(network->addresses_by_section, address_free); + return 0; + } + /* we are not in an Address section, so use line number instead. */ r = address_new_static(network, filename, line, &n); - else + } else r = address_new_static(network, filename, section_line, &n); if (r == -ENOMEM) return log_oom(); From 67150a7bd4687261c043a2cb4491ef23cd0e5ecb Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 21 Dec 2023 04:08:02 +0900 Subject: [PATCH 2/3] test-network: use the main .network file These tests are not interested in most addresses specified in the .network file. As 10-many-address.conf drop-in config for the .network file contains so many addresses, hopefully this improves test performance. --- test/test-network/systemd-networkd-tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index adb60cd4893..a3f3c6085cb 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -1087,7 +1087,7 @@ class NetworkctlTests(unittest.TestCase, Utilities): self.assertNotIn('dummy98', output) def test_reconfigure(self): - copy_network_unit('25-address-static.network', '12-dummy.netdev') + copy_network_unit('25-address-static.network', '12-dummy.netdev', copy_dropins=False) start_networkd() self.wait_online(['dummy98:routable']) @@ -1121,7 +1121,7 @@ class NetworkctlTests(unittest.TestCase, Utilities): self.assertNotIn('inet 10.1.2.4/16 brd 10.1.255.255 scope global secondary dummy98', output) self.assertNotIn('inet 10.2.2.4/16 brd 10.2.255.255 scope global dummy98', output) - copy_network_unit('25-address-static.network') + copy_network_unit('25-address-static.network', copy_dropins=False) networkctl_reload() self.wait_online(['dummy98:routable']) @@ -1154,7 +1154,7 @@ class NetworkctlTests(unittest.TestCase, Utilities): check() def test_up_down(self): - copy_network_unit('25-address-static.network', '12-dummy.netdev') + copy_network_unit('25-address-static.network', '12-dummy.netdev', copy_dropins=False) start_networkd() self.wait_online(['dummy98:routable']) @@ -3186,7 +3186,7 @@ class NetworkdNetworkTests(unittest.TestCase, Utilities): output = check_output(*networkctl_cmd, '--json=short', 'status', env=env) check_json(output) - copy_network_unit('25-address-static.network') + copy_network_unit('25-address-static.network', copy_dropins=False) networkctl_reload() self.wait_online(['dummy98:routable']) From 3a2ef59d2e73ad0a03262f595eb07b4223df6375 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 21 Dec 2023 04:09:47 +0900 Subject: [PATCH 3/3] test-network: add test case of an empty string assignment for Address= --- .../25-address-static.network.d/20-clear-addresses.conf | 6 ++++++ test/test-network/systemd-networkd-tests.py | 9 +++++++++ 2 files changed, 15 insertions(+) create mode 100644 test/test-network/conf/25-address-static.network.d/20-clear-addresses.conf diff --git a/test/test-network/conf/25-address-static.network.d/20-clear-addresses.conf b/test/test-network/conf/25-address-static.network.d/20-clear-addresses.conf new file mode 100644 index 00000000000..a38b07c1fb0 --- /dev/null +++ b/test/test-network/conf/25-address-static.network.d/20-clear-addresses.conf @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later + +[Network] +# An empty string clears previously configured addresses. +Address= +Address=10.4.0.1/16 diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index a3f3c6085cb..6af6e6dc10f 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -2756,6 +2756,15 @@ class NetworkdNetworkTests(unittest.TestCase, Utilities): for i in range(1, 254): self.assertIn(f'inet 10.3.3.{i}/16 brd 10.3.255.255', output) + # test for an empty string assignment for Address= in [Network] + copy_network_unit('25-address-static.network.d/20-clear-addresses.conf') + networkctl_reload() + self.wait_online(['dummy98:routable']) + output = check_output('ip -4 address show dev dummy98') + for i in range(1, 254): + self.assertNotIn(f'inet 10.3.3.{i}/16 brd 10.3.255.255', output) + self.assertIn('inet 10.4.0.1/16 brd 10.4.255.255', output) + def test_address_ipv4acd(self): check_output('ip netns add ns99') check_output('ip link add veth99 type veth peer veth-peer')