diff --git a/src/core/devices/nm-device-vlan.c b/src/core/devices/nm-device-vlan.c index 330b4212df..8f3a89b7db 100644 --- a/src/core/devices/nm-device-vlan.c +++ b/src/core/devices/nm-device-vlan.c @@ -21,6 +21,7 @@ #include "nm-manager.h" #include "libnm-core-intern/nm-core-internal.h" #include "platform/nmp-object.h" +#include "libnm-platform/nm-platform-utils.h" #define _NMLOG_DEVICE_TYPE NMDeviceVlan #include "nm-device-logging.h" @@ -674,7 +675,7 @@ get_connection_iface(NMDeviceFactory *factory, NMConnection *connection, const c * device, we create one for it using the VLAN ID and the parent * interface's name. */ - return nm_utils_new_vlan_name(parent_iface, nm_setting_vlan_get_id(s_vlan)); + return nmp_utils_new_vlan_name(parent_iface, nm_setting_vlan_get_id(s_vlan)); } NM_DEVICE_FACTORY_DEFINE_INTERNAL( diff --git a/src/core/nm-core-utils.c b/src/core/nm-core-utils.c index ac98419a32..48e90f2dc7 100644 --- a/src/core/nm-core-utils.c +++ b/src/core/nm-core-utils.c @@ -2021,65 +2021,6 @@ nm_utils_kernel_cmdline_match_check(const char *const *proc_cmdline, /*****************************************************************************/ -char * -nm_utils_new_vlan_name(const char *parent_iface, guint32 vlan_id) -{ - guint id_len; - gsize parent_len; - char *ifname; - - g_return_val_if_fail(parent_iface && *parent_iface, NULL); - - if (vlan_id < 10) - id_len = 2; - else if (vlan_id < 100) - id_len = 3; - else if (vlan_id < 1000) - id_len = 4; - else { - g_return_val_if_fail(vlan_id < 4095, NULL); - id_len = 5; - } - - ifname = g_new(char, IFNAMSIZ); - - parent_len = strlen(parent_iface); - parent_len = MIN(parent_len, IFNAMSIZ - 1 - id_len); - memcpy(ifname, parent_iface, parent_len); - g_snprintf(&ifname[parent_len], IFNAMSIZ - parent_len, ".%u", vlan_id); - - return ifname; -} - -/* nm_utils_new_infiniband_name: - * @name: the output-buffer where the value will be written. Must be - * not %NULL and point to a string buffer of at least IFNAMSIZ bytes. - * @parent_name: the parent interface name - * @p_key: the partition key. - * - * Returns: the infiniband name will be written to @name and @name - * is returned. - */ -const char * -nm_utils_new_infiniband_name(char *name, const char *parent_name, int p_key) -{ - g_return_val_if_fail(name, NULL); - g_return_val_if_fail(parent_name && parent_name[0], NULL); - g_return_val_if_fail(strlen(parent_name) < IFNAMSIZ, NULL); - - /* technically, p_key of 0x0000 and 0x8000 is not allowed either. But we don't - * want to assert against that in nm_utils_new_infiniband_name(). So be more - * resilient here, and accept those. */ - g_return_val_if_fail(p_key >= 0 && p_key <= 0xffff, NULL); - - /* If parent+suffix is too long, kernel would just truncate - * the name. We do the same. See ipoib_vlan_add(). */ - g_snprintf(name, IFNAMSIZ, "%s.%04x", parent_name, p_key); - return name; -} - -/*****************************************************************************/ - /** * nm_utils_cmp_connection_by_autoconnect_priority: * @a: diff --git a/src/core/nm-core-utils.h b/src/core/nm-core-utils.h index 5e89634fab..23d0382181 100644 --- a/src/core/nm-core-utils.h +++ b/src/core/nm-core-utils.h @@ -220,9 +220,6 @@ gboolean nm_utils_connection_has_default_route(NMConnection *connection, int addr_family, gboolean * out_is_never_default); -char * nm_utils_new_vlan_name(const char *parent_iface, guint32 vlan_id); -const char *nm_utils_new_infiniband_name(char *name, const char *parent_name, int p_key); - int nm_utils_cmp_connection_by_autoconnect_priority(NMConnection *a, NMConnection *b); void nm_utils_log_connection_diff(NMConnection *connection, diff --git a/src/core/platform/nm-fake-platform.c b/src/core/platform/nm-fake-platform.c index 2201e8609a..77a6e2cb77 100644 --- a/src/core/platform/nm-fake-platform.c +++ b/src/core/platform/nm-fake-platform.c @@ -799,7 +799,7 @@ infiniband_partition_add(NMPlatform * platform, parent_device = link_get(platform, parent); g_return_val_if_fail(parent_device != NULL, FALSE); - nm_utils_new_infiniband_name(name, parent_device->obj->link.name, p_key); + nmp_utils_new_infiniband_name(name, parent_device->obj->link.name, p_key); link_add_one(platform, name, NM_LINK_TYPE_INFINIBAND, _infiniband_add_prepare, &d, out_link); return TRUE; @@ -814,7 +814,7 @@ infiniband_partition_delete(NMPlatform *platform, int parent, int p_key) parent_device = link_get(platform, parent); g_return_val_if_fail(parent_device != NULL, FALSE); - nm_utils_new_infiniband_name(name, parent_device->obj->link.name, p_key); + nmp_utils_new_infiniband_name(name, parent_device->obj->link.name, p_key); return link_delete(platform, nm_platform_link_get_ifindex(platform, name)); } diff --git a/src/core/platform/nm-linux-platform.c b/src/core/platform/nm-linux-platform.c index 446aed1360..e950e9b2e0 100644 --- a/src/core/platform/nm-linux-platform.c +++ b/src/core/platform/nm-linux-platform.c @@ -8210,7 +8210,7 @@ _infiniband_partition_action(NMPlatform * platform, return FALSE; } - nm_utils_new_infiniband_name(name, ifname_parent, p_key); + nmp_utils_new_infiniband_name(name, ifname_parent, p_key); do_request_link(platform, 0, name); if (action == INFINIBAND_ACTION_DELETE_CHILD) diff --git a/src/core/platform/nm-platform.c b/src/core/platform/nm-platform.c index cbd1396efa..af502fa8db 100644 --- a/src/core/platform/nm-platform.c +++ b/src/core/platform/nm-platform.c @@ -2806,7 +2806,7 @@ _infiniband_add_add_or_delete(NMPlatform * self, if (parent_link->type != NM_LINK_TYPE_INFINIBAND) return -NME_PL_WRONG_TYPE; - nm_utils_new_infiniband_name(name, parent_link->name, p_key); + nmp_utils_new_infiniband_name(name, parent_link->name, p_key); if (add) { r = _link_add_check_existing(self, name, NM_LINK_TYPE_INFINIBAND, out_link); diff --git a/src/core/tests/test-core-with-expect.c b/src/core/tests/test-core-with-expect.c index 022cf8f39e..01784863b9 100644 --- a/src/core/tests/test-core-with-expect.c +++ b/src/core/tests/test-core-with-expect.c @@ -11,6 +11,7 @@ #include #include "NetworkManagerUtils.h" +#include "libnm-platform/nm-platform-utils.h" #include "nm-test-utils-core.h" @@ -586,7 +587,7 @@ test_nm_ethernet_address_is_valid(void) /*****************************************************************************/ static void -test_nm_utils_new_vlan_name(void) +test_nmp_utils_new_vlan_name(void) { guint i, j; const char *parent_names[] = { @@ -614,7 +615,7 @@ test_nm_utils_new_vlan_name(void) vlan_id_s = g_strdup_printf(".%d", vlan_id); - ifname = nm_utils_new_vlan_name(parent_names[i], vlan_id); + ifname = nmp_utils_new_vlan_name(parent_names[i], vlan_id); g_assert(ifname && ifname[0]); g_assert_cmpint(strlen(ifname), ==, @@ -643,7 +644,7 @@ main(int argc, char **argv) g_test_add_func("/general/nm_utils_array_remove_at_indexes", test_nm_utils_array_remove_at_indexes); g_test_add_func("/general/nm_ethernet_address_is_valid", test_nm_ethernet_address_is_valid); - g_test_add_func("/general/nm_utils_new_vlan_name", test_nm_utils_new_vlan_name); + g_test_add_func("/general/nmp_utils_new_vlan_name", test_nmp_utils_new_vlan_name); return g_test_run(); } diff --git a/src/libnm-platform/nm-platform-utils.c b/src/libnm-platform/nm-platform-utils.c index 7c101213d9..145f6f0a03 100644 --- a/src/libnm-platform/nm-platform-utils.c +++ b/src/libnm-platform/nm-platform-utils.c @@ -1805,3 +1805,64 @@ nmp_utils_sysctl_open_netdir(int ifindex, const char *ifname_guess, char *out_if return -1; } + +/*****************************************************************************/ + +char * +nmp_utils_new_vlan_name(const char *parent_iface, guint32 vlan_id) +{ + guint id_len; + gsize parent_len; + char *ifname; + + g_return_val_if_fail(parent_iface && *parent_iface, NULL); + + if (vlan_id < 10) + id_len = 2; + else if (vlan_id < 100) + id_len = 3; + else if (vlan_id < 1000) + id_len = 4; + else { + g_return_val_if_fail(vlan_id < 4095, NULL); + id_len = 5; + } + + ifname = g_new(char, IFNAMSIZ); + + parent_len = strlen(parent_iface); + parent_len = MIN(parent_len, IFNAMSIZ - 1 - id_len); + memcpy(ifname, parent_iface, parent_len); + g_snprintf(&ifname[parent_len], IFNAMSIZ - parent_len, ".%u", vlan_id); + + return ifname; +} + +/*****************************************************************************/ + +/* nmp_utils_new_infiniband_name: + * @name: the output-buffer where the value will be written. Must be + * not %NULL and point to a string buffer of at least IFNAMSIZ bytes. + * @parent_name: the parent interface name + * @p_key: the partition key. + * + * Returns: the infiniband name will be written to @name and @name + * is returned. + */ +const char * +nmp_utils_new_infiniband_name(char *name, const char *parent_name, int p_key) +{ + g_return_val_if_fail(name, NULL); + g_return_val_if_fail(parent_name && parent_name[0], NULL); + g_return_val_if_fail(strlen(parent_name) < IFNAMSIZ, NULL); + + /* technically, p_key of 0x0000 and 0x8000 is not allowed either. But we don't + * want to assert against that in nmp_utils_new_infiniband_name(). So be more + * resilient here, and accept those. */ + g_return_val_if_fail(p_key >= 0 && p_key <= 0xffff, NULL); + + /* If parent+suffix is too long, kernel would just truncate + * the name. We do the same. See ipoib_vlan_add(). */ + g_snprintf(name, IFNAMSIZ, "%s.%04x", parent_name, p_key); + return name; +} diff --git a/src/libnm-platform/nm-platform-utils.h b/src/libnm-platform/nm-platform-utils.h index 8d9eaa38e0..5ebcd4d9d2 100644 --- a/src/libnm-platform/nm-platform-utils.h +++ b/src/libnm-platform/nm-platform-utils.h @@ -70,4 +70,7 @@ int nmp_utils_if_nametoindex(const char *ifname); int nmp_utils_sysctl_open_netdir(int ifindex, const char *ifname_guess, char *out_ifname); +char * nmp_utils_new_vlan_name(const char *parent_iface, guint32 vlan_id); +const char *nmp_utils_new_infiniband_name(char *name, const char *parent_name, int p_key); + #endif /* __NM_PLATFORM_UTILS_H__ */