summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-05-14 09:16:32 +0200
committerThomas Haller <thaller@redhat.com>2020-05-22 15:58:08 +0200
commit501554732c2d74bebaf8efd630ef2f00d78745a8 (patch)
tree614b910e43ea514c3f90473417b1ff36f08e436d
parentdcb4ed2cb18857e5df0264bad68c92bb34da772f (diff)
downloadNetworkManager-501554732c2d74bebaf8efd630ef2f00d78745a8.tar.gz
libnm: avoid duplicate type checks in "nm-setting-ethtool.c"
Don't duplicate the code that maps the option to the variant type. Also, only resolve the name to NMEthtoolID once. Multiple calls to nm_ethtool_optname_is_*() unnecessarily need to convert the string to the ethtool id multiple times.
-rw-r--r--libnm-core/nm-setting-ethtool.c78
1 files changed, 43 insertions, 35 deletions
diff --git a/libnm-core/nm-setting-ethtool.c b/libnm-core/nm-setting-ethtool.c
index 58fa720b37..03245629db 100644
--- a/libnm-core/nm-setting-ethtool.c
+++ b/libnm-core/nm-setting-ethtool.c
@@ -22,6 +22,21 @@
/*****************************************************************************/
+static const GVariantType *
+get_variant_type_from_ethtool_id (NMEthtoolID ethtool_id)
+{
+ if (nm_ethtool_id_is_feature (ethtool_id))
+ return G_VARIANT_TYPE_BOOLEAN;
+
+ if ( nm_ethtool_id_is_coalesce (ethtool_id)
+ || nm_ethtool_id_is_ring (ethtool_id))
+ return G_VARIANT_TYPE_UINT32;
+
+ return NULL;
+}
+
+/*****************************************************************************/
+
/**
* nm_ethtool_optname_is_feature:
* @optname: (allow-none): the option name to check
@@ -497,32 +512,18 @@ verify (NMSetting *setting, NMConnection *connection, GError **error)
GVariant *variant;
hash = _nm_setting_gendata_hash (setting, FALSE);
-
if (!hash)
- goto out;
+ return TRUE;
g_hash_table_iter_init (&iter, hash);
while (g_hash_table_iter_next (&iter, (gpointer *) &optname, (gpointer *) &variant)) {
- if (nm_ethtool_optname_is_feature (optname)) {
- if (!g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN)) {
- g_set_error_literal (error,
- NM_CONNECTION_ERROR,
- NM_CONNECTION_ERROR_INVALID_PROPERTY,
- _("offload feature has invalid variant type"));
- g_prefix_error (error, "%s.%s: ", NM_SETTING_ETHTOOL_SETTING_NAME, optname);
- return FALSE;
- }
- } else if ( nm_ethtool_optname_is_coalesce (optname)
- || nm_ethtool_optname_is_ring (optname)) {
- if (!g_variant_is_of_type (variant, G_VARIANT_TYPE_UINT32)) {
- g_set_error_literal (error,
- NM_CONNECTION_ERROR,
- NM_CONNECTION_ERROR_INVALID_PROPERTY,
- _("setting has invalid variant type"));
- g_prefix_error (error, "%s.%s: ", NM_SETTING_ETHTOOL_SETTING_NAME, optname);
- return FALSE;
- }
- } else {
+ const GVariantType *variant_type;
+ NMEthtoolID ethtool_id;
+
+ ethtool_id = nm_ethtool_id_get_by_name (optname);
+ variant_type = get_variant_type_from_ethtool_id (ethtool_id);
+
+ if (!variant_type) {
g_set_error_literal (error,
NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_INVALID_PROPERTY,
@@ -530,9 +531,17 @@ verify (NMSetting *setting, NMConnection *connection, GError **error)
g_prefix_error (error, "%s.%s: ", NM_SETTING_ETHTOOL_SETTING_NAME, optname);
return FALSE;
}
+
+ if (!g_variant_is_of_type (variant, variant_type)) {
+ g_set_error_literal (error,
+ NM_CONNECTION_ERROR,
+ NM_CONNECTION_ERROR_INVALID_PROPERTY,
+ _("setting has invalid variant type"));
+ g_prefix_error (error, "%s.%s: ", NM_SETTING_ETHTOOL_SETTING_NAME, optname);
+ return FALSE;
+ }
}
-out:
return TRUE;
}
@@ -543,21 +552,20 @@ get_variant_type (const NMSettInfoSetting *sett_info,
const char *name,
GError **error)
{
- NMEthtoolID ethtool_id = nm_ethtool_id_get_by_name (name);
+ const GVariantType *variant_type;
- if (nm_ethtool_id_is_feature (ethtool_id))
- return G_VARIANT_TYPE_BOOLEAN;
+ variant_type = get_variant_type_from_ethtool_id (nm_ethtool_id_get_by_name (name));
- if ( nm_ethtool_id_is_coalesce (ethtool_id)
- || nm_ethtool_id_is_ring (ethtool_id))
- return G_VARIANT_TYPE_UINT32;
+ if (!variant_type) {
+ g_set_error (error,
+ NM_CONNECTION_ERROR,
+ NM_CONNECTION_ERROR_INVALID_PROPERTY,
+ _("unknown ethtool option '%s'"),
+ name);
+ return NULL;
+ }
- g_set_error (error,
- NM_CONNECTION_ERROR,
- NM_CONNECTION_ERROR_INVALID_PROPERTY,
- _("unknown ethtool option '%s'"),
- name);
- return NULL;
+ return variant_type;
}
/*****************************************************************************/