From: Saeed Mahameed cmd_dev_param_set_cb and cmd_port_param_set_cb are almost identical, except the DEVLINK_ATTR_PORT_INDEX part, which is easily identifiable in cmd_dev_param_set_cb. Check for port handle and port index attribute in cmd_dev_param_set_cb then we can reuse it for cmd_port_param_set. This allows single location for param values attribute parsing for set operations. Signed-off-by: Saeed Mahameed --- devlink/devlink.c | 79 ++++------------------------------------------- 1 file changed, 6 insertions(+), 73 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 2abf8ff7..fb89757c 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -3640,7 +3640,7 @@ struct param_ctx { } value; }; -static int cmd_dev_param_set_cb(const struct nlmsghdr *nlh, void *data) +static int cmd_param_set_cb(const struct nlmsghdr *nlh, void *data) { struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); struct nlattr *nla_param[DEVLINK_ATTR_MAX + 1] = {}; @@ -3657,6 +3657,9 @@ static int cmd_dev_param_set_cb(const struct nlmsghdr *nlh, void *data) !tb[DEVLINK_ATTR_PARAM]) return MNL_CB_ERROR; + if ((dl->opts.present & DL_OPT_HANDLEP) && !tb[DEVLINK_ATTR_PORT_INDEX]) + return MNL_CB_ERROR; + err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_PARAM], attr_cb, nla_param); if (err != MNL_CB_OK) return MNL_CB_ERROR; @@ -3735,7 +3738,7 @@ static int cmd_dev_param_set(struct dl *dl) dl_opts_put(nlh, dl); ctx.dl = dl; - err = mnlu_gen_socket_sndrcv(&dl->nlg, nlh, cmd_dev_param_set_cb, &ctx); + err = mnlu_gen_socket_sndrcv(&dl->nlg, nlh, cmd_param_set_cb, &ctx); if (err) return err; if (!ctx.cmode_found) { @@ -5219,76 +5222,6 @@ static int cmd_port_function_set(struct dl *dl) return mnlu_gen_socket_sndrcv(&dl->nlg, nlh, NULL, NULL); } -static int cmd_port_param_set_cb(const struct nlmsghdr *nlh, void *data) -{ - struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); - struct nlattr *nla_param[DEVLINK_ATTR_MAX + 1] = {}; - struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; - struct nlattr *param_value_attr; - enum devlink_var_attr_type type; - enum devlink_param_cmode cmode; - struct param_ctx *ctx = data; - struct dl *dl = ctx->dl; - int err; - - mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); - if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || - !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_PARAM]) - return MNL_CB_ERROR; - - err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_PARAM], attr_cb, nla_param); - if (err != MNL_CB_OK) - return MNL_CB_ERROR; - - if (!nla_param[DEVLINK_ATTR_PARAM_TYPE] || - !nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) - return MNL_CB_ERROR; - - type = mnl_attr_get_u8(nla_param[DEVLINK_ATTR_PARAM_TYPE]); - mnl_attr_for_each_nested(param_value_attr, - nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) { - struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {}; - struct nlattr *val_attr; - - err = mnl_attr_parse_nested(param_value_attr, - attr_cb, nla_value); - if (err != MNL_CB_OK) - return MNL_CB_ERROR; - - if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] || - (type != DEVLINK_VAR_ATTR_TYPE_FLAG && - !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA])) - return MNL_CB_ERROR; - - cmode = mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE]); - if (cmode == dl->opts.cmode) { - val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]; - switch (type) { - case DEVLINK_VAR_ATTR_TYPE_U8: - ctx->value.vu8 = mnl_attr_get_u8(val_attr); - break; - case DEVLINK_VAR_ATTR_TYPE_U16: - ctx->value.vu16 = mnl_attr_get_u16(val_attr); - break; - case DEVLINK_VAR_ATTR_TYPE_U32: - ctx->value.vu32 = mnl_attr_get_u32(val_attr); - break; - case DEVLINK_VAR_ATTR_TYPE_STRING: - ctx->value.vstr = mnl_attr_get_str(val_attr); - break; - case DEVLINK_VAR_ATTR_TYPE_FLAG: - ctx->value.vbool = val_attr ? true : false; - break; - default: - break; - } - break; - } - } - ctx->type = type; - return MNL_CB_OK; -} - static int cmd_port_param_set(struct dl *dl) { struct param_ctx ctx = {}; @@ -5313,7 +5246,7 @@ static int cmd_port_param_set(struct dl *dl) dl_opts_put(nlh, dl); ctx.dl = dl; - err = mnlu_gen_socket_sndrcv(&dl->nlg, nlh, cmd_port_param_set_cb, &ctx); + err = mnlu_gen_socket_sndrcv(&dl->nlg, nlh, cmd_param_set_cb, &ctx); if (err) return err; -- 2.50.0