[PATCH net-next 13/14] devlink: Implement devlink param multi attribute nested data values

Saeed Mahameed
2025-02-28T02:13:35Z

From: Saeed Mahameed <saeedm@nvidia.com>

Devlink param value attribute is not defined since devlink is handling
the value validating and parsing internally, this allows us to implement
multi attribute values without breaking any policies.

Devlink param multi-attribute values are considered to be dynamically
sized arrays of u32 values, by introducing a new devlink param type
DEVLINK_PARAM_TYPE_ARR_U32, driver and user space can set a variable
count of u32 values into the DEVLINK_ATTR_PARAM_VALUE_DATA attribute.

Implement get/set parsing and add to the internal value structure passed
to drivers.

This is useful for devices that need to configure a list of values for
a specific configuration.

example:
$ devlink dev param show pci/... name multi-value-param
name multi-value-param type driver-specific
values:
cmode permanent value: 0,1,2,3,4,5,6,7

$ devlink dev param set pci/... name multi-value-param \
value 4,5,6,7,0,1,2,3 cmode permanent

Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
---
include/net/devlink.h | 7 ++++++
include/uapi/linux/devlink.h | 1 +
net/devlink/param.c | 47 +++++++++++++++++++++++++++++++++++-
3 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/include/net/devlink.h b/include/net/devlink.h
index ca32c61583cf..c8f14ea3604e 100644
--- a/include/net/devlink.h
+++ b/include/net/devlink.h
@@ -420,12 +420,15 @@ typedef u64 devlink_resource_occ_get_t(void *priv);
#define DEVLINK_RESOURCE_GENERIC_NAME_PORTS "physical_ports"

#define __DEVLINK_PARAM_MAX_STRING_VALUE 32
+#define __DEVLINK_PARAM_MAX_ARRAY_SIZE 32
+
enum devlink_param_type {
DEVLINK_PARAM_TYPE_U8,
DEVLINK_PARAM_TYPE_U16,
DEVLINK_PARAM_TYPE_U32,
DEVLINK_PARAM_TYPE_STRING,
DEVLINK_PARAM_TYPE_BOOL,
+ DEVLINK_PARAM_TYPE_ARR_U32,
};

union devlink_param_value {
@@ -434,6 +437,10 @@ union devlink_param_value {
u32 vu32;
char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];
bool vbool;
+ struct {
+ u32 size;
+ u32 vu32[__DEVLINK_PARAM_MAX_ARRAY_SIZE];
+ } arr;
};

struct devlink_param_gset_ctx {
diff --git a/include/uapi/linux/devlink.h b/include/uapi/linux/devlink.h
index 8cdd60eb3c43..df7c29bbb7a7 100644
--- a/include/uapi/linux/devlink.h
+++ b/include/uapi/linux/devlink.h
@@ -400,6 +400,7 @@ enum devlink_dyn_attr_type {
DEVLINK_DYN_ATTR_TYPE_BINARY,
__DEVLINK_DYN_ATTR_TYPE_CUSTOM_BASE = 0x80,
/* Any possible custom types, unrelated to NLA_* values go below */
+ DEVLINK_DYN_ATTR_TYPE_U32_ARRAY,
};

enum devlink_attr {
diff --git a/net/devlink/param.c b/net/devlink/param.c
index 1922ca5b9cbc..31a8e61bae09 100644
--- a/net/devlink/param.c
+++ b/net/devlink/param.c
@@ -195,6 +195,8 @@ devlink_param_type_to_dyn_attr_type(enum devlink_param_type param_type)
return DEVLINK_DYN_ATTR_TYPE_STRING;
case DEVLINK_PARAM_TYPE_BOOL:
return DEVLINK_DYN_ATTR_TYPE_FLAG;
+ case DEVLINK_PARAM_TYPE_ARR_U32:
+ return DEVLINK_DYN_ATTR_TYPE_U32_ARRAY;
default:
return -EINVAL;
}
@@ -239,6 +241,13 @@ devlink_nl_param_value_fill_one(struct sk_buff *msg,
nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
goto value_nest_cancel;
break;
+ case DEVLINK_PARAM_TYPE_ARR_U32:
+ for (int i = 0; i < val.arr.size; i++) {
+ if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
+ val.arr.vu32[i]))
+ goto value_nest_cancel;
+ }
+ break;
}

nla_nest_end(msg, param_value_attr);
@@ -451,6 +460,9 @@ devlink_param_type_get_from_info(struct genl_info *info,
case DEVLINK_DYN_ATTR_TYPE_FLAG:
*param_type = DEVLINK_PARAM_TYPE_BOOL;
break;
+ case DEVLINK_DYN_ATTR_TYPE_U32_ARRAY:
+ *param_type = DEVLINK_PARAM_TYPE_ARR_U32;
+ break;
default:
return -EINVAL;
}
@@ -464,7 +476,7 @@ devlink_param_value_validate(struct genl_info *info,
{
struct netlink_ext_ack *extack = info->extack;
struct nlattr *param_data;
- int len = 0;
+ int len = 0, rem;

if (type != DEVLINK_PARAM_TYPE_BOOL &&
GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_VALUE_DATA))
@@ -507,6 +519,28 @@ devlink_param_value_validate(struct genl_info *info,
return 0;
NL_SET_ERR_MSG_MOD(extack, "Expected flag, got data");
break;
+ case DEVLINK_PARAM_TYPE_ARR_U32:
+ len = 0;
+ nla_for_each_attr_type(param_data,
+ DEVLINK_ATTR_PARAM_VALUE_DATA,
+ genlmsg_data(info->genlhdr),
+ genlmsg_len(info->genlhdr), rem) {
+ if (nla_len(param_data) != sizeof(u32)) {
+ NL_SET_ERR_MSG_MOD(extack,
+ "Array element size must be 4 bytes");
+ return -EINVAL;
+ }
+ if (++len > __DEVLINK_PARAM_MAX_ARRAY_SIZE) {
+ NL_SET_ERR_MSG_MOD(extack,
+ "Array size exceeds maximum");
+ return -EINVAL;
+ }
+ }
+ if (len)
+ return 0;
+ NL_SET_ERR_MSG_MOD(extack,
+ "Value array must have at least one entry");
+ break;
default:
NL_SET_ERR_MSG_FMT_MOD(extack,
"Not supported value type %d", type);
@@ -521,6 +555,7 @@ devlink_param_value_get_from_info(const struct devlink_param *param,
union devlink_param_value *value)
{
struct nlattr *param_data;
+ int rem, i = 0;

if (devlink_param_value_validate(info, param->type))
return -EINVAL;
@@ -543,6 +578,16 @@ devlink_param_value_get_from_info(const struct devlink_param *param,
case DEVLINK_PARAM_TYPE_BOOL:
value->vbool = nla_get_flag(param_data);
break;
+ case DEVLINK_PARAM_TYPE_ARR_U32: {
+ nla_for_each_attr_type(param_data,
+ DEVLINK_ATTR_PARAM_VALUE_DATA,
+ genlmsg_data(info->genlhdr),
+ genlmsg_len(info->genlhdr), rem)
+ value->arr.vu32[i++] = nla_get_u32(param_data);
+
+ value->arr.size = i;
+ break;
+ }
}
return 0;
}
--
2.48.1