From 2c0dffe82db574b6b9e850e48f444674e4e1d7ea Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Wed, 26 Aug 2020 16:54:45 +0200 Subject: [PATCH 1/2] networkd: use socket activation when starting networkd Add After=systemd-networkd.socket to avoid a race condition and networkd falling back to the non-socket activation code. Also add Wants=systemd-networkd.socket, so the socket is started when networkd is started via `systemctl start systemd-networkd.service`. A Requires is not strictly necessary, as networkd still ships the non-socket activation code. Should this code be removed one day, the Wants should be bumped to Requires accordingly. See also 5544ee85163733eaa50f598fcf3bd9421d4a42f9. Fixes: #16809 --- units/systemd-networkd.service.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/units/systemd-networkd.service.in b/units/systemd-networkd.service.in index 6ccbb5a95d..938aef9d17 100644 --- a/units/systemd-networkd.service.in +++ b/units/systemd-networkd.service.in @@ -13,10 +13,10 @@ Documentation=man:systemd-networkd.service(8) ConditionCapability=CAP_NET_ADMIN DefaultDependencies=no # systemd-udevd.service can be dropped once tuntap is moved to netlink -After=systemd-udevd.service network-pre.target systemd-sysusers.service systemd-sysctl.service +After=systemd-networkd.socket systemd-udevd.service network-pre.target systemd-sysusers.service systemd-sysctl.service Before=network.target multi-user.target shutdown.target Conflicts=shutdown.target -Wants=network.target +Wants=systemd-networkd.socket network.target [Service] AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_RAW From 3aa645f0c0bb7697ef397ffef4647ff105d98fda Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Fri, 28 Aug 2020 17:21:27 +0200 Subject: [PATCH 2/2] test-network: stop networkd and its socket With the changes from 2c0dffe82db574b6b9e850e48f444674e4e1d7ea, starting systemd-networkd.service will also activate systemd-networkd.socket. When tearing down a test, we need to stop the socket as well, to make sure networkd can't be activated accidentally with the wrong configuration. --- test/networkd-test.py | 12 ++++++++---- test/test-network/systemd-networkd-tests.py | 14 +++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/test/networkd-test.py b/test/networkd-test.py index 140561d25d..4116995f8c 100755 --- a/test/networkd-test.py +++ b/test/networkd-test.py @@ -223,7 +223,8 @@ Gateway=192.168.250.1''') subprocess.check_call(['systemctl', 'start', 'systemd-networkd']) def tearDown(self): - subprocess.check_call(['systemctl', 'stop', 'systemd-networkd']) + subprocess.check_call(['systemctl', 'stop', 'systemd-networkd.socket']) + subprocess.check_call(['systemctl', 'stop', 'systemd-networkd.service']) subprocess.check_call(['ip', 'link', 'del', 'mybridge']) subprocess.check_call(['ip', 'link', 'del', 'port1']) subprocess.check_call(['ip', 'link', 'del', 'port2']) @@ -309,7 +310,8 @@ class ClientTestBase(NetworkdTestingUtilities): def tearDown(self): self.shutdown_iface() - subprocess.call(['systemctl', 'stop', 'systemd-networkd']) + subprocess.call(['systemctl', 'stop', 'systemd-networkd.socket']) + subprocess.call(['systemctl', 'stop', 'systemd-networkd.service']) subprocess.call(['ip', 'link', 'del', 'dummy0'], stderr=subprocess.DEVNULL) @@ -987,7 +989,8 @@ class MatchClientTest(unittest.TestCase, NetworkdTestingUtilities): def tearDown(self): """Stop networkd.""" - subprocess.call(['systemctl', 'stop', 'systemd-networkd']) + subprocess.call(['systemctl', 'stop', 'systemd-networkd.socket']) + subprocess.call(['systemctl', 'stop', 'systemd-networkd.service']) def test_basic_matching(self): """Verify the Name= line works throughout this class.""" @@ -1037,7 +1040,8 @@ class UnmanagedClientTest(unittest.TestCase, NetworkdTestingUtilities): def tearDown(self): """Stop networkd.""" - subprocess.call(['systemctl', 'stop', 'systemd-networkd']) + subprocess.call(['systemctl', 'stop', 'systemd-networkd.socket']) + subprocess.call(['systemctl', 'stop', 'systemd-networkd.service']) def create_iface(self): """Create temporary veth pairs for interface matching.""" diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py index f12917cdd9..e3b1a68f5a 100755 --- a/test/test-network/systemd-networkd-tests.py +++ b/test/test-network/systemd-networkd-tests.py @@ -328,7 +328,7 @@ def tearDownModule(): shutil.rmtree(networkd_ci_path) - for u in ['systemd-networkd.service', 'systemd-resolved.service']: + for u in ['systemd-networkd.socket', 'systemd-networkd.service', 'systemd-resolved.service']: check_output(f'systemctl stop {u}') shutil.rmtree('/run/systemd/system/systemd-networkd.service.d') @@ -464,7 +464,8 @@ def remove_networkd_state_files(): def stop_networkd(show_logs=True, remove_state_files=True): if show_logs: invocation_id = check_output('systemctl show systemd-networkd -p InvocationID --value') - check_output('systemctl stop systemd-networkd') + check_output('systemctl stop systemd-networkd.socket') + check_output('systemctl stop systemd-networkd.service') if show_logs: print(check_output('journalctl _SYSTEMD_INVOCATION_ID=' + invocation_id)) if remove_state_files: @@ -2447,7 +2448,8 @@ class NetworkdNetworkTests(unittest.TestCase, Utilities): self.assertRegex(output, 'Search Domains: one') def test_keep_configuration_static(self): - check_output('systemctl stop systemd-networkd') + check_output('systemctl stop systemd-networkd.socket') + check_output('systemctl stop systemd-networkd.service') check_output('ip link add name dummy98 type dummy') check_output('ip address add 10.1.2.3/16 dev dummy98') @@ -3610,7 +3612,8 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): print(output) self.assertRegex(output, r'192.168.5.*') - check_output('systemctl stop systemd-networkd') + check_output('systemctl stop systemd-networkd.socket') + check_output('systemctl stop systemd-networkd.service') print('The lease address should be kept after networkd stopped') output = check_output('ip address show dev veth99 scope global') @@ -3645,7 +3648,8 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities): self.assertRegex(output, r'192.168.5.*') stop_dnsmasq(dnsmasq_pid_file) - check_output('systemctl stop systemd-networkd') + check_output('systemctl stop systemd-networkd.socket') + check_output('systemctl stop systemd-networkd.service') output = check_output('ip address show dev veth99 scope global') print(output)