platform: move nm_utils_new_{vlan,infiniband}_name() to libnm-platform

This commit is contained in:
Thomas Haller 2021-03-04 10:00:39 +01:00
parent 2d9a9d0ded
commit 690105c616
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
9 changed files with 74 additions and 70 deletions

View file

@ -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(

View file

@ -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:

View file

@ -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,

View file

@ -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));
}

View file

@ -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)

View file

@ -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);

View file

@ -11,6 +11,7 @@
#include <fcntl.h>
#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();
}

View file

@ -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;
}

View file

@ -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__ */