summary refs log tree commit diff
path: root/src/libnm-core-impl/nm-setting-infiniband.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnm-core-impl/nm-setting-infiniband.c')
-rw-r--r--src/libnm-core-impl/nm-setting-infiniband.c180
1 files changed, 66 insertions, 114 deletions
diff --git a/src/libnm-core-impl/nm-setting-infiniband.c b/src/libnm-core-impl/nm-setting-infiniband.c
index adbf3636..eb6c9536 100644
--- a/src/libnm-core-impl/nm-setting-infiniband.c
+++ b/src/libnm-core-impl/nm-setting-infiniband.c
@@ -32,11 +32,13 @@ NM_GOBJECT_PROPERTIES_DEFINE_BASE(PROP_MAC_ADDRESS,
                                   PROP_PARENT, );
 
 typedef struct {
-    char *  mac_address;
-    char *  transport_mode;
-    char *  parent;
-    char *  virtual_iface_name;
-    int     p_key;
+    char   *mac_address;
+    char   *transport_mode;
+    char   *parent;
+    char   *virtual_iface_name;
+    gsize   virtual_iface_name_parent_length;
+    gint32  virtual_iface_name_p_key;
+    gint32  p_key;
     guint32 mtu;
 } NMSettingInfinibandPrivate;
 
@@ -156,20 +158,30 @@ 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)
+    if (priv->p_key == -1 || !priv->parent) {
+        nm_clear_g_free(&priv->virtual_iface_name);
         return NULL;
+    }
 
-    if (!priv->virtual_iface_name)
+    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 = g_strdup_printf("%s.%04x", priv->parent, priv->p_key);
+    }
 
-    return NM_SETTING_INFINIBAND_GET_PRIVATE(setting)->virtual_iface_name;
+    return priv->virtual_iface_name;
 }
 
 static gboolean
 verify(NMSetting *setting, NMConnection *connection, GError **error)
 {
-    NMSettingConnection *       s_con = NULL;
+    NMSettingConnection        *s_con = NULL;
     NMSettingInfinibandPrivate *priv  = NM_SETTING_INFINIBAND_GET_PRIVATE(setting);
 
     if (priv->mac_address && !nm_utils_hwaddr_valid(priv->mac_address, INFINIBAND_ALEN)) {
@@ -237,10 +249,12 @@ verify(NMSetting *setting, NMConnection *connection, GError **error)
         const char *interface_name = nm_setting_connection_get_interface_name(s_con);
 
         if (interface_name && priv->p_key != -1) {
-            if (!priv->virtual_iface_name)
-                priv->virtual_iface_name = g_strdup_printf("%s.%04x", priv->parent, priv->p_key);
+            const char *virtual_iface_name;
 
-            if (strcmp(interface_name, priv->virtual_iface_name) != 0) {
+            virtual_iface_name =
+                nm_setting_infiniband_get_virtual_interface_name(NM_SETTING_INFINIBAND(setting));
+
+            if (!nm_streq(interface_name, virtual_iface_name)) {
                 /* We don't support renaming software infiniband devices. Later we might, but
                  * for now just reject such connections.
                  **/
@@ -249,7 +263,7 @@ verify(NMSetting *setting, NMConnection *connection, GError **error)
                             NM_CONNECTION_ERROR_INVALID_PROPERTY,
                             _("interface name of software infiniband device must be '%s' or unset "
                               "(instead it is '%s')"),
-                            priv->virtual_iface_name,
+                            virtual_iface_name,
                             interface_name);
                 g_prefix_error(error,
                                "%s.%s: ",
@@ -291,74 +305,8 @@ verify(NMSetting *setting, NMConnection *connection, GError **error)
 /*****************************************************************************/
 
 static void
-get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
-{
-    NMSettingInfiniband *setting = NM_SETTING_INFINIBAND(object);
-
-    switch (prop_id) {
-    case PROP_MAC_ADDRESS:
-        g_value_set_string(value, nm_setting_infiniband_get_mac_address(setting));
-        break;
-    case PROP_MTU:
-        g_value_set_uint(value, nm_setting_infiniband_get_mtu(setting));
-        break;
-    case PROP_TRANSPORT_MODE:
-        g_value_set_string(value, nm_setting_infiniband_get_transport_mode(setting));
-        break;
-    case PROP_P_KEY:
-        g_value_set_int(value, nm_setting_infiniband_get_p_key(setting));
-        break;
-    case PROP_PARENT:
-        g_value_set_string(value, nm_setting_infiniband_get_parent(setting));
-        break;
-    default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
-        break;
-    }
-}
-
-static void
-set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
-{
-    NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE(object);
-
-    switch (prop_id) {
-    case PROP_MAC_ADDRESS:
-        g_free(priv->mac_address);
-        priv->mac_address =
-            _nm_utils_hwaddr_canonical_or_invalid(g_value_get_string(value), INFINIBAND_ALEN);
-        break;
-    case PROP_MTU:
-        priv->mtu = g_value_get_uint(value);
-        break;
-    case PROP_TRANSPORT_MODE:
-        g_free(priv->transport_mode);
-        priv->transport_mode = g_value_dup_string(value);
-        break;
-    case PROP_P_KEY:
-        priv->p_key = g_value_get_int(value);
-        nm_clear_g_free(&priv->virtual_iface_name);
-        break;
-    case PROP_PARENT:
-        g_free(priv->parent);
-        priv->parent = g_value_dup_string(value);
-        nm_clear_g_free(&priv->virtual_iface_name);
-        break;
-    default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
-        break;
-    }
-}
-
-/*****************************************************************************/
-
-static void
 nm_setting_infiniband_init(NMSettingInfiniband *self)
-{
-    NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE(self);
-
-    priv->p_key = -1;
-}
+{}
 
 /**
  * nm_setting_infiniband_new:
@@ -378,8 +326,6 @@ finalize(GObject *object)
 {
     NMSettingInfinibandPrivate *priv = NM_SETTING_INFINIBAND_GET_PRIVATE(object);
 
-    g_free(priv->transport_mode);
-    g_free(priv->parent);
     g_free(priv->virtual_iface_name);
 
     G_OBJECT_CLASS(nm_setting_infiniband_parent_class)->finalize(object);
@@ -388,14 +334,14 @@ finalize(GObject *object)
 static void
 nm_setting_infiniband_class_init(NMSettingInfinibandClass *klass)
 {
-    GObjectClass *  object_class        = G_OBJECT_CLASS(klass);
+    GObjectClass   *object_class        = G_OBJECT_CLASS(klass);
     NMSettingClass *setting_class       = NM_SETTING_CLASS(klass);
-    GArray *        properties_override = _nm_sett_info_property_override_create_array();
+    GArray         *properties_override = _nm_sett_info_property_override_create_array();
 
     g_type_class_add_private(klass, sizeof(NMSettingInfinibandPrivate));
 
-    object_class->get_property = get_property;
-    object_class->set_property = set_property;
+    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;
@@ -447,14 +393,16 @@ nm_setting_infiniband_class_init(NMSettingInfinibandClass *klass)
      * description: MTU of the interface.
      * ---end---
      */
-    obj_properties[PROP_MTU] = g_param_spec_uint(NM_SETTING_INFINIBAND_MTU,
-                                                 "",
-                                                 "",
-                                                 0,
-                                                 G_MAXUINT32,
-                                                 0,
-                                                 G_PARAM_READWRITE | NM_SETTING_PARAM_FUZZY_IGNORE
-                                                     | G_PARAM_STATIC_STRINGS);
+    _nm_setting_property_define_direct_uint32(properties_override,
+                                              obj_properties,
+                                              NM_SETTING_INFINIBAND_MTU,
+                                              PROP_MTU,
+                                              0,
+                                              G_MAXUINT32,
+                                              0,
+                                              NM_SETTING_PARAM_FUZZY_IGNORE,
+                                              NMSettingInfinibandPrivate,
+                                              mtu);
 
     /**
      * NMSettingInfiniband:transport-mode:
@@ -470,12 +418,13 @@ nm_setting_infiniband_class_init(NMSettingInfinibandClass *klass)
      *   "datagram" mode
      * ---end---
      */
-    obj_properties[PROP_TRANSPORT_MODE] = g_param_spec_string(
-        NM_SETTING_INFINIBAND_TRANSPORT_MODE,
-        "",
-        "",
-        NULL,
-        G_PARAM_READWRITE | NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS);
+    _nm_setting_property_define_direct_string(properties_override,
+                                              obj_properties,
+                                              NM_SETTING_INFINIBAND_TRANSPORT_MODE,
+                                              PROP_TRANSPORT_MODE,
+                                              NM_SETTING_PARAM_INFERRABLE,
+                                              NMSettingInfinibandPrivate,
+                                              transport_mode);
 
     /**
      * NMSettingInfiniband:p-key:
@@ -495,14 +444,16 @@ nm_setting_infiniband_class_init(NMSettingInfinibandClass *klass)
      * example: PKEY=yes PKEY_ID=2 PHYSDEV=mlx4_ib0 DEVICE=mlx4_ib0.8002
      * ---end---
      */
-    obj_properties[PROP_P_KEY] =
-        g_param_spec_int(NM_SETTING_INFINIBAND_P_KEY,
-                         "",
-                         "",
-                         -1,
-                         0xFFFF,
-                         -1,
-                         G_PARAM_READWRITE | NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS);
+    _nm_setting_property_define_direct_int32(properties_override,
+                                             obj_properties,
+                                             NM_SETTING_INFINIBAND_P_KEY,
+                                             PROP_P_KEY,
+                                             -1,
+                                             0xFFFF,
+                                             -1,
+                                             NM_SETTING_PARAM_INFERRABLE,
+                                             NMSettingInfinibandPrivate,
+                                             p_key);
 
     /**
      * NMSettingInfiniband:parent:
@@ -520,12 +471,13 @@ nm_setting_infiniband_class_init(NMSettingInfinibandClass *klass)
      * example: PHYSDEV=ib0
      * ---end---
      */
-    obj_properties[PROP_PARENT] = g_param_spec_string(
-        NM_SETTING_INFINIBAND_PARENT,
-        "",
-        "",
-        NULL,
-        G_PARAM_READWRITE | NM_SETTING_PARAM_INFERRABLE | G_PARAM_STATIC_STRINGS);
+    _nm_setting_property_define_direct_string(properties_override,
+                                              obj_properties,
+                                              NM_SETTING_INFINIBAND_PARENT,
+                                              PROP_PARENT,
+                                              NM_SETTING_PARAM_INFERRABLE,
+                                              NMSettingInfinibandPrivate,
+                                              parent);
 
     g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);