Reapply "cloud-setup: lookup device by MAC + type instead of just MAC"

This will be useful for updating configuration of Vlans and MacVlans,
some of having same MAC addresses as devices of other type.

This reverts commit cee0515f1399fe05f650adec147435aa0a933d78.

(cherry picked from commit 6cedd594b6)
This commit is contained in:
Lubomir Rintel
2025-02-05 14:35:11 +01:00
parent fd6f4f86e4
commit 369db80086

View File

@@ -207,39 +207,38 @@ _device_get_hwaddr(NMDevice *device)
nm_assert(NM_IS_DEVICE_ETHERNET(device) || NM_IS_DEVICE_MACVLAN(device)
|| NM_IS_DEVICE_VLAN(device));
/* Network interfaces in cloud environments are identified by their permanent
* MAC address.
*
* For testing, we can set NMCS_ENV_NM_CLOUD_SETUP_MAP_INTERFACES
* to a ';' separate list of "$INTERFACE=$HWADDR", which means that we
* pretend that device with ip-interface "$INTERFACE" has the specified permanent
* MAC address. */
if (NM_IS_DEVICE_ETHERNET(device)) {
/* Ethernet interfaces in cloud environments are identified by their permanent
* MAC address.
*
* For testing, we can set NMCS_ENV_NM_CLOUD_SETUP_MAP_INTERFACES
* to a ';' separate list of "$INTERFACE=$HWADDR", which means that we
* pretend that device with ip-interface "$INTERFACE" has the specified permanent
* MAC address. */
if (g_once_init_enter(&gl_initialized)) {
gl_map_interfaces_map = _map_interfaces_parse();
g_once_init_leave(&gl_initialized, 1);
}
map = gl_map_interfaces_map;
if (G_UNLIKELY(map)) {
const char *const iface = nm_device_get_iface(NM_DEVICE(device));
/* For testing, the device<->hwaddr is remapped and the actual permanent
* MAC address of the device ignored. This mapping is configured via
* NMCS_ENV_NM_CLOUD_SETUP_MAP_INTERFACES environment variable. */
if (!iface)
return NULL;
for (; map->name; map++) {
if (nm_streq(map->name, iface))
return map->value_str;
if (g_once_init_enter(&gl_initialized)) {
gl_map_interfaces_map = _map_interfaces_parse();
g_once_init_leave(&gl_initialized, 1);
}
return NULL;
}
map = gl_map_interfaces_map;
if (G_UNLIKELY(map)) {
const char *const iface = nm_device_get_iface(NM_DEVICE(device));
/* For testing, the device<->hwaddr is remapped and the actual permanent
* MAC address of the device ignored. This mapping is configured via
* NMCS_ENV_NM_CLOUD_SETUP_MAP_INTERFACES environment variable. */
if (!iface)
return NULL;
for (; map->name; map++) {
if (nm_streq(map->name, iface))
return map->value_str;
}
return NULL;
}
if (NM_IS_DEVICE_ETHERNET(device)) {
return nm_device_ethernet_get_permanent_hw_address(NM_DEVICE_ETHERNET(device));
} else {
return nm_device_get_hw_address(device);
@@ -295,7 +294,7 @@ _nmc_get_ethernet_hwaddrs(NMClient *nmc)
}
static NMDevice *
_nmc_get_device_by_hwaddr(NMClient *nmc, const char *hwaddr)
_nmc_get_device_by_hwaddr(NMClient *nmc, const GType type_device, const char *hwaddr)
{
const GPtrArray *devices;
guint i;
@@ -307,7 +306,7 @@ _nmc_get_device_by_hwaddr(NMClient *nmc, const char *hwaddr)
const char *hwaddr_dev;
gs_free char *s = NULL;
if (!NM_IS_DEVICE_ETHERNET(device))
if (!G_TYPE_CHECK_INSTANCE_TYPE(device, type_device))
continue;
hwaddr_dev = _device_get_hwaddr(device);
@@ -589,7 +588,7 @@ _config_one(SigTermData *sigterm_data,
if (g_cancellable_is_cancelled(sigterm_data->cancellable))
return FALSE;
device = nm_g_object_ref(_nmc_get_device_by_hwaddr(nmc, hwaddr));
device = nm_g_object_ref(_nmc_get_device_by_hwaddr(nmc, NM_DEVICE_TYPE_ETHERNET, hwaddr));
if (!device) {
_LOGD("config device %s: skip because device not found", hwaddr);
return FALSE;