Commit 7c976c7c authored by Moshe Shemesh's avatar Moshe Shemesh Committed by Jakub Kicinski

devlink: Move devlink dev selftest code to dev

Move devlink dev selftest callbacks and related code from leftover.c to
file dev.c. No functional change in this patch.
Signed-off-by: default avatarMoshe Shemesh <moshe@nvidia.com>
Reviewed-by: default avatarJiri Pirko <jiri@nvidia.com>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent ec4a0ce9
......@@ -1159,3 +1159,185 @@ int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
return ret;
}
static int
devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
u32 portid, u32 seq, int flags,
struct netlink_ext_ack *extack)
{
struct nlattr *selftests;
void *hdr;
int err;
int i;
hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
DEVLINK_CMD_SELFTESTS_GET);
if (!hdr)
return -EMSGSIZE;
err = -EMSGSIZE;
if (devlink_nl_put_handle(msg, devlink))
goto err_cancel_msg;
selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
if (!selftests)
goto err_cancel_msg;
for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
if (devlink->ops->selftest_check(devlink, i, extack)) {
err = nla_put_flag(msg, i);
if (err)
goto err_cancel_msg;
}
}
nla_nest_end(msg, selftests);
genlmsg_end(msg, hdr);
return 0;
err_cancel_msg:
genlmsg_cancel(msg, hdr);
return err;
}
int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb,
struct genl_info *info)
{
struct devlink *devlink = info->user_ptr[0];
struct sk_buff *msg;
int err;
if (!devlink->ops->selftest_check)
return -EOPNOTSUPP;
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
info->snd_seq, 0, info->extack);
if (err) {
nlmsg_free(msg);
return err;
}
return genlmsg_reply(msg, info);
}
static int
devlink_nl_cmd_selftests_get_dump_one(struct sk_buff *msg,
struct devlink *devlink,
struct netlink_callback *cb)
{
if (!devlink->ops->selftest_check)
return 0;
return devlink_nl_selftests_fill(msg, devlink,
NETLINK_CB(cb->skb).portid,
cb->nlh->nlmsg_seq, NLM_F_MULTI,
cb->extack);
}
const struct devlink_cmd devl_cmd_selftests_get = {
.dump_one = devlink_nl_cmd_selftests_get_dump_one,
};
static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
enum devlink_selftest_status test_status)
{
struct nlattr *result_attr;
result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
if (!result_attr)
return -EMSGSIZE;
if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
test_status))
goto nla_put_failure;
nla_nest_end(skb, result_attr);
return 0;
nla_put_failure:
nla_nest_cancel(skb, result_attr);
return -EMSGSIZE;
}
static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
[DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
};
int devlink_nl_cmd_selftests_run(struct sk_buff *skb, struct genl_info *info)
{
struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
struct devlink *devlink = info->user_ptr[0];
struct nlattr *attrs, *selftests;
struct sk_buff *msg;
void *hdr;
int err;
int i;
if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
return -EOPNOTSUPP;
if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
return -EINVAL;
attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
devlink_selftest_nl_policy, info->extack);
if (err < 0)
return err;
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
err = -EMSGSIZE;
hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
&devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
if (!hdr)
goto free_msg;
if (devlink_nl_put_handle(msg, devlink))
goto genlmsg_cancel;
selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
if (!selftests)
goto genlmsg_cancel;
for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
enum devlink_selftest_status test_status;
if (nla_get_flag(tb[i])) {
if (!devlink->ops->selftest_check(devlink, i,
info->extack)) {
if (devlink_selftest_result_put(msg, i,
DEVLINK_SELFTEST_STATUS_SKIP))
goto selftests_nest_cancel;
continue;
}
test_status = devlink->ops->selftest_run(devlink, i,
info->extack);
if (devlink_selftest_result_put(msg, i, test_status))
goto selftests_nest_cancel;
}
}
nla_nest_end(msg, selftests);
genlmsg_end(msg, hdr);
return genlmsg_reply(msg, info);
selftests_nest_cancel:
nla_nest_cancel(msg, selftests);
genlmsg_cancel:
genlmsg_cancel(msg, hdr);
free_msg:
nlmsg_free(msg);
return err;
}
......@@ -216,3 +216,5 @@ int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info)
int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info);
int devlink_nl_cmd_info_get_doit(struct sk_buff *skb, struct genl_info *info);
int devlink_nl_cmd_flash_update(struct sk_buff *skb, struct genl_info *info);
int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb, struct genl_info *info);
int devlink_nl_cmd_selftests_run(struct sk_buff *skb, struct genl_info *info);
......@@ -143,10 +143,6 @@ static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_
NLA_POLICY_BITFIELD32(DEVLINK_PORT_FN_CAPS_VALID_MASK),
};
static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
[DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
};
#define ASSERT_DEVLINK_PORT_REGISTERED(devlink_port) \
WARN_ON_ONCE(!(devlink_port)->registered)
#define ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port) \
......@@ -3848,185 +3844,6 @@ int devlink_resources_validate(struct devlink *devlink,
return err;
}
static int
devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
u32 portid, u32 seq, int flags,
struct netlink_ext_ack *extack)
{
struct nlattr *selftests;
void *hdr;
int err;
int i;
hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
DEVLINK_CMD_SELFTESTS_GET);
if (!hdr)
return -EMSGSIZE;
err = -EMSGSIZE;
if (devlink_nl_put_handle(msg, devlink))
goto err_cancel_msg;
selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
if (!selftests)
goto err_cancel_msg;
for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
if (devlink->ops->selftest_check(devlink, i, extack)) {
err = nla_put_flag(msg, i);
if (err)
goto err_cancel_msg;
}
}
nla_nest_end(msg, selftests);
genlmsg_end(msg, hdr);
return 0;
err_cancel_msg:
genlmsg_cancel(msg, hdr);
return err;
}
static int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb,
struct genl_info *info)
{
struct devlink *devlink = info->user_ptr[0];
struct sk_buff *msg;
int err;
if (!devlink->ops->selftest_check)
return -EOPNOTSUPP;
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
info->snd_seq, 0, info->extack);
if (err) {
nlmsg_free(msg);
return err;
}
return genlmsg_reply(msg, info);
}
static int
devlink_nl_cmd_selftests_get_dump_one(struct sk_buff *msg,
struct devlink *devlink,
struct netlink_callback *cb)
{
if (!devlink->ops->selftest_check)
return 0;
return devlink_nl_selftests_fill(msg, devlink,
NETLINK_CB(cb->skb).portid,
cb->nlh->nlmsg_seq, NLM_F_MULTI,
cb->extack);
}
const struct devlink_cmd devl_cmd_selftests_get = {
.dump_one = devlink_nl_cmd_selftests_get_dump_one,
};
static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
enum devlink_selftest_status test_status)
{
struct nlattr *result_attr;
result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
if (!result_attr)
return -EMSGSIZE;
if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
test_status))
goto nla_put_failure;
nla_nest_end(skb, result_attr);
return 0;
nla_put_failure:
nla_nest_cancel(skb, result_attr);
return -EMSGSIZE;
}
static int devlink_nl_cmd_selftests_run(struct sk_buff *skb,
struct genl_info *info)
{
struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
struct devlink *devlink = info->user_ptr[0];
struct nlattr *attrs, *selftests;
struct sk_buff *msg;
void *hdr;
int err;
int i;
if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
return -EOPNOTSUPP;
if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
return -EINVAL;
attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
devlink_selftest_nl_policy, info->extack);
if (err < 0)
return err;
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!msg)
return -ENOMEM;
err = -EMSGSIZE;
hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
&devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
if (!hdr)
goto free_msg;
if (devlink_nl_put_handle(msg, devlink))
goto genlmsg_cancel;
selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
if (!selftests)
goto genlmsg_cancel;
for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
enum devlink_selftest_status test_status;
if (nla_get_flag(tb[i])) {
if (!devlink->ops->selftest_check(devlink, i,
info->extack)) {
if (devlink_selftest_result_put(msg, i,
DEVLINK_SELFTEST_STATUS_SKIP))
goto selftests_nest_cancel;
continue;
}
test_status = devlink->ops->selftest_run(devlink, i,
info->extack);
if (devlink_selftest_result_put(msg, i, test_status))
goto selftests_nest_cancel;
}
}
nla_nest_end(msg, selftests);
genlmsg_end(msg, hdr);
return genlmsg_reply(msg, info);
selftests_nest_cancel:
nla_nest_cancel(msg, selftests);
genlmsg_cancel:
genlmsg_cancel(msg, hdr);
free_msg:
nlmsg_free(msg);
return err;
}
static const struct devlink_param devlink_param_generic[] = {
{
.id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment