libnm: use nm_net_devname_infiniband() for virtual interface-name of infiniband

In nm_setting_infiniband_get_virtual_interface_name(), no longer try to
detect whether the cached value is still up to date.  Instead, as we now
have a fix sized buffer for the name, just always generate the name on
every call. It's simpler.
This commit is contained in:
Thomas Haller
2023-05-25 21:56:50 +02:00
parent 8062137cd0
commit b25ddc2055
4 changed files with 17 additions and 53 deletions

View File

@@ -1066,6 +1066,8 @@ write_infiniband_setting(NMConnection *connection,
svSetValueStr(ifcfg, "PHYSDEV", parent);
if (parent && nm_connection_get_interface_name(connection)) {
char name[NM_IFNAMSIZ];
/* The connection.interface-name depends on the p-key. Also,
* nm_connection_normalize() will automatically adjust the
* interface-name to match the p-key.
@@ -1073,8 +1075,8 @@ write_infiniband_setting(NMConnection *connection,
* As we patched the p-key above, also anticipate that change, and
* don't write a DEVICE= to the file, which would we normalize
* differently, when reading it back. */
*out_interface_name =
nm_setting_infiniband_create_virtual_interface_name(parent, p_key);
nm_net_devname_infiniband(name, parent, p_key);
*out_interface_name = g_strdup(name);
}
}

View File

@@ -36,9 +36,7 @@ typedef struct {
char *mac_address;
char *transport_mode;
char *parent;
char *virtual_iface_name;
gsize virtual_iface_name_parent_length;
gint32 virtual_iface_name_p_key;
char virtual_iface_name[NM_IFNAMSIZ];
gint32 p_key;
guint32 mtu;
} NMSettingInfinibandPrivate;
@@ -145,17 +143,6 @@ nm_setting_infiniband_get_parent(NMSettingInfiniband *setting)
return NM_SETTING_INFINIBAND_GET_PRIVATE(setting)->parent;
}
char *
nm_setting_infiniband_create_virtual_interface_name(const char *parent, int p_key)
{
char *s;
s = g_strdup_printf("%s.%04x", parent, (guint) p_key);
if (strlen(s) >= NM_IFNAMSIZ)
s[NM_IFNAMSIZ - 1] = '\0';
return s;
}
/**
* nm_setting_infiniband_get_virtual_interface_name:
* @setting: the #NMSettingInfiniband
@@ -170,25 +157,11 @@ const char *
nm_setting_infiniband_get_virtual_interface_name(NMSettingInfiniband *setting)
{
NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE(setting);
gsize len;
if (priv->p_key == -1 || !priv->parent) {
nm_clear_g_free(&priv->virtual_iface_name);
if (priv->p_key == -1 || !priv->parent)
return NULL;
}
len = strlen(priv->parent);
if (!priv->virtual_iface_name || priv->virtual_iface_name_p_key != priv->p_key
|| priv->virtual_iface_name_parent_length != len
|| memcmp(priv->parent, priv->virtual_iface_name, len) != 0) {
priv->virtual_iface_name_p_key = priv->p_key;
priv->virtual_iface_name_parent_length = len;
g_free(priv->virtual_iface_name);
priv->virtual_iface_name =
nm_setting_infiniband_create_virtual_interface_name(priv->parent, priv->p_key);
}
return priv->virtual_iface_name;
return nm_net_devname_infiniband(priv->virtual_iface_name, priv->parent, priv->p_key);
}
static gboolean
@@ -352,16 +325,6 @@ nm_setting_infiniband_new(void)
return g_object_new(NM_TYPE_SETTING_INFINIBAND, NULL);
}
static void
finalize(GObject *object)
{
NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE(object);
g_free(priv->virtual_iface_name);
G_OBJECT_CLASS(nm_setting_infiniband_parent_class)->finalize(object);
}
static void
nm_setting_infiniband_class_init(NMSettingInfinibandClass *klass)
{
@@ -373,7 +336,6 @@ nm_setting_infiniband_class_init(NMSettingInfinibandClass *klass)
object_class->get_property = _nm_setting_property_get_property_direct;
object_class->set_property = _nm_setting_property_set_property_direct;
object_class->finalize = finalize;
setting_class->verify = verify;

View File

@@ -6255,14 +6255,16 @@ test_connection_normalize_infiniband(void)
nmtst_connection_normalize(con);
g_assert_cmpstr(nm_connection_get_interface_name(con), ==, "x234567890123.0");
#define iface_name(parent, p_key, expected) \
G_STMT_START \
{ \
gs_free char *_s = nm_setting_infiniband_create_virtual_interface_name((parent), (p_key)); \
\
g_assert(nm_utils_ifname_valid_kernel(_s, NULL)); \
g_assert_cmpstr(_s, ==, (expected)); \
} \
#define iface_name(parent, p_key, expected) \
G_STMT_START \
{ \
char _name[NM_IFNAMSIZ]; \
\
nm_net_devname_infiniband(_name, (parent), (p_key)); \
\
g_assert(nm_utils_ifname_valid_kernel(_name, NULL)); \
g_assert_cmpstr(_name, ==, (expected)); \
} \
G_STMT_END
iface_name("foo", 15, "foo.000f");

View File

@@ -323,8 +323,6 @@ typedef gpointer (*NMUtilsCopyFunc)(gpointer);
const char **
_nm_ip_address_get_attribute_names(const NMIPAddress *addr, gboolean sorted, guint *out_length);
char *nm_setting_infiniband_create_virtual_interface_name(const char *parent, int p_key);
#define NM_SETTING_WIRED_S390_OPTION_MAX_LEN 200u
void _nm_setting_wired_clear_s390_options(NMSettingWired *setting);