From b8b74f4000e38525b7b845e00a163a418315f1ce Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 25 May 2023 21:33:02 +0200 Subject: [PATCH] libnm-base: move nmp_utils_new_infiniband_name() to nm_net_devname_infiniband() --- src/core/platform/nm-fake-platform.c | 4 +-- .../plugins/ifcfg-rh/nms-ifcfg-rh-reader.c | 2 +- src/libnm-base/nm-base.c | 29 +++++++++++++++++++ src/libnm-base/nm-base.h | 5 ++++ src/libnm-platform/nm-linux-platform.c | 2 +- src/libnm-platform/nm-platform-utils.c | 29 ------------------- src/libnm-platform/nm-platform-utils.h | 3 +- src/libnm-platform/nm-platform.c | 2 +- 8 files changed, 40 insertions(+), 36 deletions(-) diff --git a/src/core/platform/nm-fake-platform.c b/src/core/platform/nm-fake-platform.c index 91f48adaad..f6f377d470 100644 --- a/src/core/platform/nm-fake-platform.c +++ b/src/core/platform/nm-fake-platform.c @@ -849,7 +849,7 @@ infiniband_partition_add(NMPlatform *platform, parent_device = link_get(platform, parent); g_return_val_if_fail(parent_device != NULL, FALSE); - nmp_utils_new_infiniband_name(name, parent_device->obj->link.name, p_key); + nm_net_devname_infiniband(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; @@ -864,7 +864,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); - nmp_utils_new_infiniband_name(name, parent_device->obj->link.name, p_key); + nm_net_devname_infiniband(name, parent_device->obj->link.name, p_key); return link_delete(platform, nm_platform_link_get_ifindex(platform, name)); } diff --git a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index c3cba7c693..935a19ee52 100644 --- a/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/core/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -5424,7 +5424,7 @@ parse_infiniband_p_key(shvarFile *ifcfg, int *out_p_key, char **out_parent, GErr * automatically sets. * * NetworkManager supports p-keys without the high bit set. That affects - * the interface name (nmp_utils_new_infiniband_name()) and is what + * the interface name (nm_net_devname_infiniband()) and is what * we write to "create_child"/"delete_child" sysctl. Kernel will honor * such p-keys for the interface name, but for other purposes it adds the * highest bit. That makes using p-keys without the highest bit odd. diff --git a/src/libnm-base/nm-base.c b/src/libnm-base/nm-base.c index fa64372fd8..c5f9478754 100644 --- a/src/libnm-base/nm-base.c +++ b/src/libnm-base/nm-base.c @@ -39,3 +39,32 @@ nm_dhcp_iaid_from_hexstr(const char *str, guint32 *out_value) NM_SET_OUT(out_value, be32toh(iaid.num)); return TRUE; } + +/*****************************************************************************/ + +/* nm_net_devname_infiniband: + * @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_net_devname_infiniband(char name[static NM_IFNAMSIZ], 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) < NM_IFNAMSIZ, NULL); + + /* technically, p_key of 0x0000 and 0x8000 is not allowed either. But we don't + * want to assert against that in nm_net_devname_infiniband(). 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, NM_IFNAMSIZ, "%s.%04x", parent_name, p_key); + return name; +} diff --git a/src/libnm-base/nm-base.h b/src/libnm-base/nm-base.h index 440fbc90c7..65fc9299d1 100644 --- a/src/libnm-base/nm-base.h +++ b/src/libnm-base/nm-base.h @@ -432,4 +432,9 @@ char *nm_dhcp_iaid_to_hexstr(guint32 iaid, char buf[static NM_DHCP_IAID_TO_HEXST gboolean nm_dhcp_iaid_from_hexstr(const char *str, guint32 *out_value); +/*****************************************************************************/ + +const char * +nm_net_devname_infiniband(char name[static NM_IFNAMSIZ], const char *parent_name, int p_key); + #endif /* __NM_LIBNM_BASE_H__ */ diff --git a/src/libnm-platform/nm-linux-platform.c b/src/libnm-platform/nm-linux-platform.c index d0a61832e7..9181595b0d 100644 --- a/src/libnm-platform/nm-linux-platform.c +++ b/src/libnm-platform/nm-linux-platform.c @@ -9387,7 +9387,7 @@ _infiniband_partition_action(NMPlatform *platform, return FALSE; } - nmp_utils_new_infiniband_name(name, ifname_parent, p_key); + nm_net_devname_infiniband(name, ifname_parent, p_key); do_request_link(platform, 0, name); if (action == INFINIBAND_ACTION_DELETE_CHILD) diff --git a/src/libnm-platform/nm-platform-utils.c b/src/libnm-platform/nm-platform-utils.c index 8d3281dbbe..2af4a6ab40 100644 --- a/src/libnm-platform/nm-platform-utils.c +++ b/src/libnm-platform/nm-platform-utils.c @@ -2064,35 +2064,6 @@ nmp_utils_new_vlan_name(const char *parent_iface, guint32 vlan_id) /*****************************************************************************/ -/* 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; -} - -/*****************************************************************************/ - /** * Takes a pair @timestamp and @duration, and returns the remaining duration based * on the new timestamp @now. diff --git a/src/libnm-platform/nm-platform-utils.h b/src/libnm-platform/nm-platform-utils.h index 9f17da4886..14b09a793e 100644 --- a/src/libnm-platform/nm-platform-utils.h +++ b/src/libnm-platform/nm-platform-utils.h @@ -77,8 +77,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); +char *nmp_utils_new_vlan_name(const char *parent_iface, guint32 vlan_id); guint32 nmp_utils_lifetime_rebase_relative_time_on_now(guint32 timestamp, guint32 duration, gint32 now); diff --git a/src/libnm-platform/nm-platform.c b/src/libnm-platform/nm-platform.c index defa08eb27..165b9f0f5d 100644 --- a/src/libnm-platform/nm-platform.c +++ b/src/libnm-platform/nm-platform.c @@ -2999,7 +2999,7 @@ _infiniband_add_add_or_delete(NMPlatform *self, if (parent_link->type != NM_LINK_TYPE_INFINIBAND) return -NME_PL_WRONG_TYPE; - nmp_utils_new_infiniband_name(name, parent_link->name, p_key); + nm_net_devname_infiniband(name, parent_link->name, p_key); if (add) { r = _link_add_check_existing(self, name, NM_LINK_TYPE_INFINIBAND, out_link);