devlink: support default flag attr for param-get and param-set commands
Subsystems (3)
cover devlink: support default flag attr for param-get and param-set commands
Add cli support for default param values introduced in the accompanying kerenl series [1]. Here is some sample usage:
[1]: https://lore.kernel.org/netdev/[email protected]/
# dump params with defaults
./devlink dev param show pci/0000:01:00.0
pci/0000:01:00.0:
name max_macs type generic
values:
cmode driverinit value 128 default 128
...
name swp_l4_csum_mode type driver-specific
values:
cmode permanent value default default default # set to l4_only
./devlink dev param set pci/0000:01:00.0 name swp_l4_csum_mode value l4_only cmode permanent
./devlink dev param show pci/0000:01:00.0 name swp_l4_csum_mode
pci/0000:01:00.0:
name swp_l4_csum_mode type driver-specific
values:
cmode permanent value l4_only default default # reset to default
./devlink dev param set pci/0000:01:00.0 name swp_l4_csum_mode default cmode permanent
./devlink dev param show pci/0000:01:00.0 name swp_l4_csum_mode
pci/0000:01:00.0:
name swp_l4_csum_mode type driver-specific
values:
cmode permanent value default default default---
Daniel Zahka (2):
devlink: Pull the value printing logic out of pr_out_param_value()
devlink: support displaying and resetting to default params
devlink/devlink.c | 165 ++++++++++++++++++++++++++++++++----------- include/uapi/linux/devlink.h | 3 + man/man8/devlink-dev.8 | 22 +++++- man/man8/devlink-port.8 | 20 +++++- 4 files changed, 167 insertions(+), 43 deletions(-)
---
base-commit: da3525408f9607f1e7e41984c034d7e349317a3b
change-id: 20251112-param-defaults-d796ffdb572b
Best regards,
Signature · 1 line
Daniel Zahka <[email protected]>
PATCH 1/2 devlink: Pull the value printing logic out of pr_out_param_value()
Split the type demux and value print out of pr_out_param_value() into a new function pr_out_param_value_print(). This new function can be re-used for printing additional kinds of values e.g., a default value reported by the kernel.
--- devlink/devlink.c | 88 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 34 deletions(-)
devlink/devlink.c +54 -34
@@ -3518,33 +3518,14 @@ static const struct param_val_conv param_val_conv[] = {#define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv) -static void pr_out_param_value (struct dl *dl,const char *nla_name, -int nla_type,struct nlattr *nl) +static int pr_out_param_value_print (const char *nla_name,int nla_type, +struct nlattr *val_attr,bool conv_exists, +const char *label) { -struct nlattr *nla_value[DEVLINK_ATTR_MAX +1 ] = {}; -struct nlattr *val_attr; +char format_str[32 ];const char *vstr; -bool conv_exists;int err; - err =mnl_attr_parse_nested (nl, attr_cb, nla_value); -if (err !=MNL_CB_OK ) -return ; - -if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE ] || - (nla_type !=MNL_TYPE_FLAG && - !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA ])) -return ; - -check_indent_newline (dl); -print_string (PRINT_ANY ,"cmode" ,"cmode %s" , -param_cmode_name (mnl_attr_get_u8 (nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE ]))); - - val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA ]; - - conv_exists =param_val_conv_exists (param_val_conv,PARAM_VAL_CONV_LEN , - nla_name); -switch (nla_type) {case MNL_TYPE_U8 :if (conv_exists) {
@@ -3554,10 +3535,12 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name,mnl_attr_get_u8 (val_attr), &vstr);if (err) -return ; -print_string (PRINT_ANY ,"value" ," value %s" , vstr); +return err; +snprintf (format_str,sizeof (format_str)," %s %%s" , label); +print_string (PRINT_ANY , label, format_str, vstr); }else { -print_uint (PRINT_ANY ,"value" ," value %u" , +snprintf (format_str,sizeof (format_str)," %s %%u" , label); +print_uint (PRINT_ANY , label, format_str,mnl_attr_get_u8 (val_attr)); }break ;
@@ -3569,10 +3552,12 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name,mnl_attr_get_u16 (val_attr), &vstr);if (err) -return ; -print_string (PRINT_ANY ,"value" ," value %s" , vstr); +return err; +snprintf (format_str,sizeof (format_str)," %s %%s" , label); +print_string (PRINT_ANY , label, format_str, vstr); }else { -print_uint (PRINT_ANY ,"value" ," value %u" , +snprintf (format_str,sizeof (format_str)," %s %%u" , label); +print_uint (PRINT_ANY , label, format_str,mnl_attr_get_u16 (val_attr)); }break ;
@@ -3584,21 +3569,56 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name,mnl_attr_get_u32 (val_attr), &vstr);if (err) -return ; -print_string (PRINT_ANY ,"value" ," value %s" , vstr); +return err; +snprintf (format_str,sizeof (format_str)," %s %%s" , label); +print_string (PRINT_ANY , label, format_str, vstr); }else { -print_uint (PRINT_ANY ,"value" ," value %u" , +snprintf (format_str,sizeof (format_str)," %s %%u" , label); +print_uint (PRINT_ANY , label, format_str,mnl_attr_get_u32 (val_attr)); }break ;case MNL_TYPE_STRING : -print_string (PRINT_ANY ,"value" ," value %s" , +snprintf (format_str,sizeof (format_str)," %s %%s" , label); +print_string (PRINT_ANY , label, format_str,mnl_attr_get_str (val_attr));break ;case MNL_TYPE_FLAG : -print_bool (PRINT_ANY ,"value" ," value %s" , val_attr); +snprintf (format_str,sizeof (format_str)," %s %%s" , label); +print_bool (PRINT_ANY , label, format_str, val_attr);break ; } + +return 0 ; +} + +static void pr_out_param_value (struct dl *dl,const char *nla_name, +int nla_type,struct nlattr *nl) +{ +struct nlattr *nla_value[DEVLINK_ATTR_MAX +1 ] = {}; +struct nlattr *val_attr; +bool conv_exists; +int err; + + err =mnl_attr_parse_nested (nl, attr_cb, nla_value); +if (err !=MNL_CB_OK ) +return ; + +if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE ] || + (nla_type !=MNL_TYPE_FLAG && + !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA ])) +return ; + +check_indent_newline (dl); +print_string (PRINT_ANY ,"cmode" ,"cmode %s" , +param_cmode_name (mnl_attr_get_u8 (nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE ]))); + + val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA ]; + + conv_exists =param_val_conv_exists (param_val_conv,PARAM_VAL_CONV_LEN , + nla_name); + +pr_out_param_value_print (nla_name, nla_type, val_attr, conv_exists,"value" ); } static void pr_out_param(struct dl *dl, struct nlattr **tb, bool array,
Signature · 1 line
2.47.3
Re: [PATCH iproute2-next 1/2] devlink: Pull the value printing logic out of pr_out_param_value()
devlink/devlink.c static void pr_out_param_value(struct dl *dl, const char *nla_name, · line 3543
Quoted 3 lines · On Mon, 17 Nov 2025 16:40:02 -0800 Daniel Zahka <[email protected]> wrote:
devlink/devlink.c +2
-1- print_uint(PRINT_ANY, "value", " value %u", +snprintf (format_str,sizeof (format_str)," %s %%u" , label); + print_uint(PRINT_ANY, label, format_str,
The problem with generating format strings is that it makes it difficult to impossible to use any of the compiler format validation flags.
PATCH 2/2 devlink: support displaying and resetting to default params
In reply to Daniel ZahkaAdd devlink cli support default param values.
For param-get, any default values provided by the kernel will be displayed next to the current value. For param-set, DEVLINK_ATTR_PARAM_RESET_DEFAULT can be included in the request to request that the parameter be reset to its default value.
--- devlink/devlink.c | 83 +++++++++++++++++++++++++++++++++++++++----- include/uapi/linux/devlink.h | 3 ++ man/man8/devlink-dev.8 | 22 ++++++++++-- man/man8/devlink-port.8 | 20 ++++++++++- 4 files changed, 116 insertions(+), 12 deletions(-)
devlink/devlink.c +74 -9
@@ -313,6 +313,7 @@ static int ifname_map_update(struct ifname_map *ifname_map, const char *ifname)#define DL_OPT_PORT_FN_MAX_IO_EQS BIT(58)#define DL_OPT_PORT_FN_RATE_TC_BWS BIT(59)#define DL_OPT_HEALTH_REPORTER_BURST_PERIOD BIT(60) +#define DL_OPT_PARAM_SET_DEFAULT BIT(61)struct dl_opts {uint64_t present;/* flags of present items */
@@ -340,6 +341,7 @@ struct dl_opts {const char *param_name;const char *param_value;enum devlink_param_cmode cmode; +bool param_default;char *region_name;uint32_t region_snapshot_id;__u64 region_address;
@@ -1672,6 +1674,7 @@ static const struct dl_args_metadata dl_args_required[] = { {DL_OPT_PARAM_NAME, "Parameter name expected."}, {DL_OPT_PARAM_VALUE, "Value to set expected."}, {DL_OPT_PARAM_CMODE, "Configuration mode expected."}, + {DL_OPT_PARAM_SET_DEFAULT, "Parameter default flag expected."}, {DL_OPT_REGION_SNAPSHOT_ID, "Region snapshot id expected."}, {DL_OPT_REGION_ADDRESS, "Region address value expected."}, {DL_OPT_REGION_LENGTH, "Region length value expected."},
@@ -2027,6 +2030,11 @@ static int dl_argv_parse(struct dl *dl, uint64_t o_required,if (err)return err; o_found |=DL_OPT_PARAM_CMODE ; + }else if (dl_argv_match (dl,"default" ) && + (o_all &DL_OPT_PARAM_SET_DEFAULT )) { +dl_arg_inc (dl); + opts->param_default = true; + o_found |=DL_OPT_PARAM_SET_DEFAULT ; }else if (dl_argv_match (dl,"snapshot" ) && (o_all &DL_OPT_REGION_SNAPSHOT_ID )) {dl_arg_inc (dl);
@@ -2688,6 +2696,8 @@ static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)if (opts->present &DL_OPT_PARAM_CMODE )mnl_attr_put_u8 (nlh,DEVLINK_ATTR_PARAM_VALUE_CMODE , opts->cmode); +if (opts->present &DL_OPT_PARAM_SET_DEFAULT ) +mnl_attr_put (nlh,DEVLINK_ATTR_PARAM_RESET_DEFAULT ,0 ,NULL );if (opts->present &DL_OPT_REGION_SNAPSHOT_ID )mnl_attr_put_u32 (nlh,DEVLINK_ATTR_REGION_SNAPSHOT_ID , opts->region_snapshot_id);
@@ -2855,7 +2865,7 @@ static void cmd_dev_help(void)pr_err (" [ inline-mode { none | link | network | transport } ]\n" );pr_err (" [ encap-mode { none | basic } ]\n" );pr_err (" devlink dev eswitch show DEV\n" ); -pr_err (" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n" ); +pr_err (" devlink dev param set DEV name PARAMETER [ value VALUE | default ] cmode { permanent | driverinit | runtime }\n" );pr_err (" devlink dev param show [DEV name PARAMETER]\n" );pr_err (" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n" );pr_err (" [ action { driver_reinit | fw_activate } ] [ limit no_reset ]\n" );
@@ -3520,7 +3530,7 @@ static const struct param_val_conv param_val_conv[] = {static int pr_out_param_value_print (const char *nla_name,int nla_type,struct nlattr *val_attr,bool conv_exists, -const char *label) +const char *label,bool flag_as_u8) {char format_str[32 ];const char *vstr;
@@ -3585,7 +3595,11 @@ static int pr_out_param_value_print(const char *nla_name, int nla_type,break ;case MNL_TYPE_FLAG :snprintf (format_str,sizeof (format_str)," %s %%s" , label); -print_bool (PRINT_ANY , label, format_str, val_attr); +if (flag_as_u8) +print_bool (PRINT_ANY , label, format_str, +mnl_attr_get_u8 (val_attr)); +else +print_bool (PRINT_ANY , label, format_str, val_attr);break ; }
@@ -3618,7 +3632,18 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, conv_exists =param_val_conv_exists (param_val_conv,PARAM_VAL_CONV_LEN , nla_name); -pr_out_param_value_print (nla_name, nla_type, val_attr, conv_exists,"value" ); + err =pr_out_param_value_print (nla_name, nla_type, val_attr, + conv_exists,"value" , false); +if (err) +return ; + + val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DEFAULT ]; +if (val_attr) { + err =pr_out_param_value_print (nla_name, nla_type, val_attr, + conv_exists,"default" , true); +if (err) +return ; + } } static void pr_out_param(struct dl *dl, struct nlattr **tb, bool array,
@@ -3787,11 +3812,23 @@ static int cmd_dev_param_set(struct dl *dl) err =dl_argv_parse (dl,DL_OPT_HANDLE |DL_OPT_PARAM_NAME | -DL_OPT_PARAM_VALUE | -DL_OPT_PARAM_CMODE ,0 ); +DL_OPT_PARAM_CMODE , +DL_OPT_PARAM_VALUE |DL_OPT_PARAM_SET_DEFAULT );if (err)return err; +if ((dl->opts.present &DL_OPT_PARAM_VALUE ) && + (dl->opts.present &DL_OPT_PARAM_SET_DEFAULT )) { +pr_err ("Cannot specify both value and default\n" ); +return -EINVAL ; + } + +if (!(dl->opts.present &DL_OPT_PARAM_VALUE ) && + !(dl->opts.present &DL_OPT_PARAM_SET_DEFAULT )) { +pr_err ("Either value or default must be specified\n" ); +return -EINVAL ; + } +/* Get value type */ nlh =mnlu_gen_socket_cmd_prepare (&dl->nlg,DEVLINK_CMD_PARAM_GET ,NLM_F_REQUEST |NLM_F_ACK );
@@ -3801,6 +3838,15 @@ static int cmd_dev_param_set(struct dl *dl) err =mnlu_gen_socket_sndrcv (&dl->nlg, nlh, cmd_dev_param_set_cb, &ctx);if (err)return err; + +if (dl->opts.present &DL_OPT_PARAM_SET_DEFAULT ) { + nlh =mnlu_gen_socket_cmd_prepare (&dl->nlg,DEVLINK_CMD_PARAM_SET , +NLM_F_REQUEST |NLM_F_ACK ); +dl_opts_put (nlh, dl); +mnl_attr_put_u8 (nlh,DEVLINK_ATTR_PARAM_TYPE , ctx.nla_type); +return mnlu_gen_socket_sndrcv (&dl->nlg, nlh,NULL ,NULL ); + } +if (!ctx.cmode_found) {pr_err ("Configuration mode not supported\n" );return -ENOTSUP ;
@@ -4939,7 +4985,7 @@ static void cmd_port_help(void)pr_err (" [ ipsec_crypto { enable | disable } ] [ ipsec_packet { enable | disable } ]\n" );pr_err (" [ max_io_eqs EQS ]\n" );pr_err (" devlink port function rate { help | show | add | del | set }\n" ); -pr_err (" devlink port param set DEV/PORT_INDEX name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n" ); +pr_err (" devlink port param set DEV/PORT_INDEX name PARAMETER [ value VALUE | default ] cmode { permanent | driverinit | runtime }\n" );pr_err (" devlink port param show [DEV/PORT_INDEX name PARAMETER]\n" );pr_err (" devlink port health show [ DEV/PORT_INDEX reporter REPORTER_NAME ]\n" ); pr_err(" devlink port add DEV/PORT_INDEX flavour FLAVOUR pfnum PFNUM\n"
@@ -5383,11 +5429,30 @@ static int cmd_port_param_set(struct dl *dl) err =dl_argv_parse (dl,DL_OPT_HANDLEP |DL_OPT_PARAM_NAME | -DL_OPT_PARAM_VALUE | -DL_OPT_PARAM_CMODE ,0 ); +DL_OPT_PARAM_CMODE , +DL_OPT_PARAM_VALUE |DL_OPT_PARAM_SET_DEFAULT );if (err)return err; +if ((dl->opts.present &DL_OPT_PARAM_VALUE ) && + (dl->opts.present &DL_OPT_PARAM_SET_DEFAULT )) { +pr_err ("Cannot specify both value and default\n" ); +return -EINVAL ; + } + +if (!(dl->opts.present &DL_OPT_PARAM_VALUE ) && + !(dl->opts.present &DL_OPT_PARAM_SET_DEFAULT )) { +pr_err ("Either value or default must be specified\n" ); +return -EINVAL ; + } + +if (dl->opts.present &DL_OPT_PARAM_SET_DEFAULT ) { + nlh =mnlu_gen_socket_cmd_prepare (&dl->nlg,DEVLINK_CMD_PORT_PARAM_SET , +NLM_F_REQUEST |NLM_F_ACK ); +dl_opts_put (nlh, dl); +return mnlu_gen_socket_sndrcv (&dl->nlg, nlh,NULL ,NULL ); + } +/* Get value type */ nlh =mnlu_gen_socket_cmd_prepare (&dl->nlg,DEVLINK_CMD_PORT_PARAM_GET ,NLM_F_REQUEST |NLM_F_ACK );
include/uapi/linux/devlink.h +3 -0
@@ -639,6 +639,9 @@ enum devlink_attr {DEVLINK_ATTR_HEALTH_REPORTER_BURST_PERIOD ,/* u64 */ +DEVLINK_ATTR_PARAM_VALUE_DEFAULT ,/* dynamic */ +DEVLINK_ATTR_PARAM_RESET_DEFAULT ,/* flag */ +/* Add new attributes above here, update the spec in * Documentation/netlink/specs/devlink.yaml and re-generate * net/devlink/netlink_gen.c.
man/man8/devlink-dev.8 +20 -2
@@ -45,8 +45,12 @@ devlink-dev \- devlink device configuration .I DEV .B name .I PARAMETER +[ .B value .I VALUE +| +.B default +] .BR cmode " { " runtime " | " driverinit " | " permanent " } " .ti -8
@@ -159,7 +163,13 @@ Specify parameter name to set. .TP .BI value " VALUE" -New value to set. +New value to set. Mutually exclusive with +.BR default . + +.TP +.B default +Restore parameter to its default value. Mutually exclusive with +.BR value . .TP .BR cmode " { " runtime " | " driverinit " | " permanent " } "
@@ -176,10 +186,13 @@ Configuration mode in which the new value is set. .SS devlink dev param show - display devlink device supported configuration parameters attributes +.TP .B name .I PARAMETER Specify parameter name to show. If this argument is omitted all parameters supported by devlink devices are listed. +When the kernel provides a default value for a parameter, it will be automatically displayed +in the output alongside the current value. .SS devlink dev reload - perform hot reload of the driver.
@@ -309,7 +322,7 @@ Sets the eswitch mode of specified devlink device to switchdev. .PP devlink dev param show pci/0000:01:00.0 name max_macs .RS 4 -Shows the parameter max_macs attributes. +Shows the parameter max_macs attributes. If a default value exists, it will be displayed alongside the current value. .RE .PP devlink dev param set pci/0000:01:00.0 name internal_error_reset value true cmode runtime
@@ -317,6 +330,11 @@ devlink dev param set pci/0000:01:00.0 name internal_error_reset value true cmod Sets the parameter internal_error_reset of specified devlink device to true. .RE .PP +devlink dev param set pci/0000:01:00.0 name internal_error_reset default cmode runtime +.RS 4 +Restores the parameter internal_error_reset of specified devlink device to its default value. +.RE +.PP devlink dev reload pci/0000:01:00.0 .RS 4 Performs hot reload of specified devlink device.
man/man8/devlink-port.8 +19 -1
@@ -96,8 +96,12 @@ devlink-port \- devlink port configuration .I DEV/PORT_INDEX .B name .I PARAMETER +[ .B value .I VALUE +| +.B default +] .BR cmode " { " runtime " | " driverinit " | " permanent " } " .ti -8
@@ -263,7 +267,13 @@ Specify parameter name to set. .TP .BI value " VALUE" -New value to set. +New value to set. Mutually exclusive with +.BR default . + +.TP +.B default +Restore parameter to its default value. Mutually exclusive with +.BR value . .TP .BR cmode " { " runtime " | " driverinit " | " permanent " } "
@@ -284,10 +294,13 @@ Configuration mode in which the new value is set. .I "DEV/PORT_INDEX" - specifies the devlink port to operate on. +.TP .B name .I PARAMETER Specify parameter name to show. If this argument, as well as port index, are omitted - all parameters supported by devlink device ports are listed. +When the kernel provides a default value for a parameter, it will be automatically displayed +in the output alongside the current value. .SS devlink port function rate - manage devlink rate objects Is an alias for
@@ -406,6 +419,11 @@ devlink dev param set pci/0000:01:00.0/1 name internal_error_reset value true cm Sets the parameter internal_error_reset of specified devlink port (#1) to true. .RE .PP +devlink dev param set pci/0000:01:00.0/1 name internal_error_reset default cmode runtime +.RS 4 +Restores the parameter internal_error_reset of specified devlink port (#1) to its default value. +.RE +.PP devlink port add pci/0000:06:00.0 flavour pcisf pfnum 0 sfnum 88 controller 1 .RS 4 Add a devlink port of flavour PCI SF on controller 1 which has PCI PF of number
Signature · 1 line
2.47.3
Re: [PATCH iproute2-next 2/2] devlink: support displaying and resetting to default params
Quoted 11 lines · Daniel Zahka
Add devlink cli support default param values.
For param-get, any default values provided by the kernel will be displayed next to the current value. For param-set,
DEVLINK_ATTR_PARAM_RESET_DEFAULTcan be included in the request to request that the parameter be reset to its default value.Signed-off-by Daniel Zahka <[email protected]>--- devlink/devlink.c | 83 +++++++++++++++++++++++++++++++++++++++----- include/uapi/linux/devlink.h | 3 ++
uapi changes need to be a separate patch that I can drop when applying.