From 8f7029d132d81f8d454cccfeb8381001bf405fa9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 20 Apr 2016 10:45:31 +0200 Subject: [PATCH] core: add nm_utils_new_infiniband_name() util --- src/nm-core-utils.c | 27 +++++++++++++++++++++++++++ src/nm-core-utils.h | 1 + 2 files changed, 28 insertions(+) diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index bbe466545d..6ece1b089c 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -1866,6 +1866,33 @@ nm_utils_new_vlan_name (const char *parent_iface, guint32 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_read_resolv_conf_nameservers(): * @rc_contents: contents of a resolv.conf; or %NULL to read /etc/resolv.conf diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 528288c34c..486942a22a 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -281,6 +281,7 @@ const char *nm_utils_get_ip_config_method (NMConnection *connection, GType ip_setting_type); 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); GPtrArray *nm_utils_read_resolv_conf_nameservers (const char *rc_contents); GPtrArray *nm_utils_read_resolv_conf_dns_options (const char *rc_contents);