1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-28 02:50:16 +03:00

Merge pull request #12157 from yuwata/network-netdev-name-conflict

network: handle NetDev.Name= conflict nicely
This commit is contained in:
Lennart Poettering 2019-04-01 15:17:07 +02:00 committed by GitHub
commit 245d386a03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 23 deletions

View File

@ -684,12 +684,12 @@ int netdev_load_one(Manager *manager, const char *filename) {
}
if (netdev_raw->kind == _NETDEV_KIND_INVALID) {
log_warning("NetDev has no Kind configured in %s. Ignoring", filename);
log_warning("NetDev has no Kind= configured in %s. Ignoring", filename);
return 0;
}
if (!netdev_raw->ifname) {
log_warning("NetDev without Name configured in %s. Ignoring", filename);
log_warning("NetDev without Name= configured in %s. Ignoring", filename);
return 0;
}
@ -704,7 +704,8 @@ int netdev_load_one(Manager *manager, const char *filename) {
netdev->n_ref = 1;
netdev->manager = manager;
netdev->kind = netdev_raw->kind;
netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time, so that done() will be called on destruction */
netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time,
so that done() will be called on destruction */
if (NETDEV_VTABLE(netdev)->init)
NETDEV_VTABLE(netdev)->init(netdev);
@ -730,7 +731,9 @@ int netdev_load_one(Manager *manager, const char *filename) {
if (!netdev->mac && netdev->kind != NETDEV_KIND_VLAN) {
r = netdev_get_mac(netdev->ifname, &netdev->mac);
if (r < 0)
return log_error_errno(r, "Failed to generate predictable MAC address for %s: %m", netdev->ifname);
return log_netdev_error_errno(netdev, r,
"Failed to generate predictable MAC address for %s: %m",
netdev->ifname);
}
r = hashmap_ensure_allocated(&netdev->manager->netdevs, &string_hash_ops);
@ -738,6 +741,19 @@ int netdev_load_one(Manager *manager, const char *filename) {
return r;
r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev);
if (r == -EEXIST) {
NetDev *n = hashmap_get(netdev->manager->netdevs, netdev->ifname);
assert(n);
log_netdev_warning_errno(netdev, r,
"The setting Name=%s in %s conflicts with the one in %s, ignoring",
netdev->ifname, netdev->filename, n->filename);
/* Clear ifname before netdev_free() is called. Otherwise, the NetDev object 'n' is
* removed from the hashmap 'manager->netdevs'. */
netdev->ifname = mfree(netdev->ifname);
return 0;
}
if (r < 0)
return r;
@ -810,7 +826,7 @@ int netdev_load(Manager *manager) {
if (r < 0)
return log_error_errno(r, "Failed to enumerate netdev files: %m");
STRV_FOREACH_BACKWARDS(f, files) {
STRV_FOREACH(f, files) {
r = netdev_load_one(manager, *f);
if (r < 0)
return r;

View File

@ -166,9 +166,7 @@ int link_load_one(link_config_ctx *ctx, const char *filename) {
log_debug("Parsed configuration file %s", filename);
LIST_PREPEND(links, ctx->links, link);
link = NULL;
LIST_PREPEND(links, ctx->links, TAKE_PTR(link));
return 0;
}

View File

@ -0,0 +1,4 @@
[NetDev]
Name=dropin-test
Kind=dummy
MACAddress=00:50:56:c0:00:38

View File

@ -0,0 +1,4 @@
[WireGuardPeer]
PublicKey=lsDtM3AbjxNlauRKzHEPfgS1Zp7cp/VX5Use/P4PQSc=
AllowedIPs=fdbc:bae2:7871:0500:e1fe:0793:8636:dad1/128
AllowedIPs=fdbc:bae2:7871:e1fe:0793:8636::/96

View File

@ -0,0 +1,2 @@
[Match]
Name=wg99

View File

@ -245,6 +245,7 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities):
'10-dropin-test.netdev',
'11-dummy.netdev',
'12-dummy.netdev',
'15-name-conflict-test.netdev',
'21-macvlan.netdev',
'21-macvtap.netdev',
'21-vlan-test1.network',
@ -295,6 +296,7 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities):
'25-wireguard-23-peers.network',
'25-wireguard-private-key.txt',
'25-wireguard.netdev',
'25-wireguard.network',
'6rd.network',
'erspan.network',
'gre.network',
@ -321,11 +323,13 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities):
self.remove_unit_from_networkd_path(self.units)
def test_dropin(self):
self.copy_unit_to_networkd_unit_path('10-dropin-test.netdev')
self.copy_unit_to_networkd_unit_path('10-dropin-test.netdev', '15-name-conflict-test.netdev')
self.start_networkd()
self.assertTrue(self.link_exits('dropin-test'))
# This also tests NetDev.Name= conflict and basic networkctl functionalities
output = subprocess.check_output(['ip', 'link', 'show', 'dropin-test']).rstrip().decode('utf-8')
print(output)
self.assertRegex(output, '00:50:56:c0:00:28')
@ -493,17 +497,25 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities):
@expectedFailureIfModuleIsNotAvailable('wireguard')
def test_wireguard(self):
self.copy_unit_to_networkd_unit_path('25-wireguard.netdev')
self.start_networkd()
self.copy_unit_to_networkd_unit_path('25-wireguard.netdev', '25-wireguard.network',
'25-wireguard-23-peers.netdev', '25-wireguard-23-peers.network',
'25-wireguard-private-key.txt')
self.start_networkd(0)
self.wait_online(['wg99:carrier', 'wg98:routable'])
self.assertTrue(self.link_exits('wg99'))
self.assertTrue(self.link_exits('wg98'))
if shutil.which('wg'):
subprocess.call('wg')
output = subprocess.check_output(['wg', 'show', 'wg99', 'listen-port']).rstrip().decode('utf-8')
self.assertTrue(output, '51820')
output = subprocess.check_output(['wg', 'show', 'wg99', 'fwmark']).rstrip().decode('utf-8')
self.assertTrue(output, '0x4d2')
output = subprocess.check_output(['wg', 'show', 'wg99', 'allowed-ips']).rstrip().decode('utf-8')
self.assertTrue(output, 'RDf+LSpeEre7YEIKaxg+wbpsNV7du+ktR99uBEtIiCA=\t192.168.26.0/24 fd31:bf08:57cb::/48')
self.assertTrue(output, 'lsDtM3AbjxNlauRKzHEPfgS1Zp7cp/VX5Use/P4PQSc=\tfdbc:bae2:7871:e1fe:793:8636::/96 fdbc:bae2:7871:500:e1fe:793:8636:dad1/128')
output = subprocess.check_output(['wg', 'show', 'wg99', 'persistent-keepalive']).rstrip().decode('utf-8')
self.assertTrue(output, 'RDf+LSpeEre7YEIKaxg+wbpsNV7du+ktR99uBEtIiCA=\t20')
output = subprocess.check_output(['wg', 'show', 'wg99', 'endpoints']).rstrip().decode('utf-8')
@ -511,21 +523,9 @@ class NetworkdNetDevTests(unittest.TestCase, Utilities):
output = subprocess.check_output(['wg', 'show', 'wg99', 'private-key']).rstrip().decode('utf-8')
self.assertTrue(output, 'EEGlnEPYJV//kbvvIqxKkQwOiS+UENyPncC4bF46ong=')
self.assertTrue(self.link_exits('wg99'))
@expectedFailureIfModuleIsNotAvailable('wireguard')
def test_wireguard_23_peers(self):
self.copy_unit_to_networkd_unit_path('25-wireguard-23-peers.netdev', '25-wireguard-23-peers.network',
'25-wireguard-private-key.txt')
self.start_networkd()
if shutil.which('wg'):
subprocess.call('wg')
output = subprocess.check_output(['wg', 'show', 'wg98', 'private-key']).rstrip().decode('utf-8')
self.assertTrue(output, 'CJQUtcS9emY2fLYqDlpSZiE/QJyHkPWr+WHtZLZ90FU=')
self.assertTrue(self.link_exits('wg98'))
def test_geneve(self):
self.copy_unit_to_networkd_unit_path('25-geneve.netdev')
self.start_networkd()