From: Jiri Pirko <jiri@resnulli.us>
To: netdev@vger.kernel.org
Cc: stephen@networkplumber.org, dsahern@gmail.com
Subject: [patch iproute2-next 5/6] mnl_utils: introduce a helper to check if dump policy exists for command
Date: Thu, 31 Aug 2023 15:22:28 +0200 [thread overview]
Message-ID: <20230831132229.471693-6-jiri@resnulli.us> (raw)
In-Reply-To: <20230831132229.471693-1-jiri@resnulli.us>
From: Jiri Pirko <jiri@nvidia.com>
Benefit from GET_POLICY command of ctrl netlink and introduce a helper
that dumps policies and finds out, if there is a separate policy
specified for dump op of specified command.
Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
include/mnl_utils.h | 1 +
lib/mnl_utils.c | 121 +++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 120 insertions(+), 2 deletions(-)
diff --git a/include/mnl_utils.h b/include/mnl_utils.h
index 2193934849e1..76fe1dfec938 100644
--- a/include/mnl_utils.h
+++ b/include/mnl_utils.h
@@ -30,5 +30,6 @@ int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, siz
mnl_cb_t cb, void *data);
int mnlu_gen_socket_recv_run(struct mnlu_gen_socket *nlg, mnl_cb_t cb,
void *data);
+int mnlu_gen_cmd_dump_policy(struct mnlu_gen_socket *nlg, uint8_t cmd);
#endif /* __MNL_UTILS_H__ */
diff --git a/lib/mnl_utils.c b/lib/mnl_utils.c
index f8e07d2f467f..1c78222828ff 100644
--- a/lib/mnl_utils.c
+++ b/lib/mnl_utils.c
@@ -110,7 +110,7 @@ int mnlu_socket_recv_run(struct mnl_socket *nl, unsigned int seq, void *buf, siz
return err;
}
-static int get_family_attrs_cb(const struct nlattr *attr, void *data)
+static int ctrl_attrs_cb(const struct nlattr *attr, void *data)
{
int type = mnl_attr_get_type(attr);
const struct nlattr **tb = data;
@@ -124,6 +124,12 @@ static int get_family_attrs_cb(const struct nlattr *attr, void *data)
if (type == CTRL_ATTR_MAXATTR &&
mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
return MNL_CB_ERROR;
+ if (type == CTRL_ATTR_POLICY &&
+ mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
+ return MNL_CB_ERROR;
+ if (type == CTRL_ATTR_OP_POLICY &&
+ mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
+ return MNL_CB_ERROR;
tb[type] = attr;
return MNL_CB_OK;
}
@@ -134,7 +140,7 @@ static int get_family_cb(const struct nlmsghdr *nlh, void *data)
struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
struct mnlu_gen_socket *nlg = data;
- mnl_attr_parse(nlh, sizeof(*genl), get_family_attrs_cb, tb);
+ mnl_attr_parse(nlh, sizeof(*genl), ctrl_attrs_cb, tb);
if (!tb[CTRL_ATTR_FAMILY_ID])
return MNL_CB_ERROR;
if (!tb[CTRL_ATTR_MAXATTR])
@@ -252,3 +258,114 @@ int mnlu_gen_socket_recv_run(struct mnlu_gen_socket *nlg, mnl_cb_t cb,
MNL_SOCKET_BUFFER_SIZE,
cb, data);
}
+
+static int ctrl_policy_attrs_cb(const struct nlattr *attr, void *data)
+{
+ int type = mnl_attr_get_type(attr);
+ const struct nlattr **tb = data;
+
+ if (mnl_attr_type_valid(attr, CTRL_ATTR_POLICY_DUMP_MAX) < 0)
+ return MNL_CB_ERROR;
+
+ if (type == CTRL_ATTR_POLICY_DO &&
+ mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ return MNL_CB_ERROR;
+ if (type == CTRL_ATTR_POLICY_DUMP &&
+ mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
+ return MNL_CB_ERROR;
+
+ tb[type] = attr;
+ return MNL_CB_OK;
+}
+
+struct cmd_dump_policy_ctx {
+ uint8_t cmd;
+ uint8_t do_policy_idx_found:1,
+ dump_policy_idx_found:1;
+ uint32_t do_policy_idx;
+ uint32_t dump_policy_idx;
+ uint32_t dump_policy_attr_count;
+};
+
+static void process_dump_op_policy_nest(const struct nlattr *op_policy_nest,
+ struct cmd_dump_policy_ctx *ctx)
+{
+ struct nlattr *tb[CTRL_ATTR_POLICY_DUMP_MAX + 1] = {};
+ const struct nlattr *attr;
+ int err;
+
+ mnl_attr_for_each_nested(attr, op_policy_nest) {
+ if (ctx->cmd != (attr->nla_type & ~NLA_F_NESTED))
+ continue;
+ err = mnl_attr_parse_nested(attr, ctrl_policy_attrs_cb, tb);
+ if (err != MNL_CB_OK)
+ continue;
+ if (tb[CTRL_ATTR_POLICY_DO]) {
+ ctx->do_policy_idx = mnl_attr_get_u32(tb[CTRL_ATTR_POLICY_DO]);
+ ctx->do_policy_idx_found = true;
+ }
+ if (tb[CTRL_ATTR_POLICY_DUMP]) {
+ ctx->dump_policy_idx = mnl_attr_get_u32(tb[CTRL_ATTR_POLICY_DUMP]);
+ ctx->dump_policy_idx_found = true;
+ }
+ break;
+ }
+}
+
+static void process_dump_policy_nest(const struct nlattr *policy_nest,
+ struct cmd_dump_policy_ctx *ctx)
+{
+ const struct nlattr *attr;
+
+ if (!ctx->dump_policy_idx_found)
+ return;
+
+ mnl_attr_for_each_nested(attr, policy_nest)
+ if (ctx->dump_policy_idx == (attr->nla_type & ~NLA_F_NESTED))
+ ctx->dump_policy_attr_count++;
+}
+
+static int cmd_dump_policy_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
+ struct nlattr *tb[CTRL_ATTR_MAX + 1] = {};
+ struct cmd_dump_policy_ctx *ctx = data;
+
+ mnl_attr_parse(nlh, sizeof(*genl), ctrl_attrs_cb, tb);
+ if (!tb[CTRL_ATTR_FAMILY_ID])
+ return MNL_CB_OK;
+
+ if (tb[CTRL_ATTR_OP_POLICY])
+ process_dump_op_policy_nest(tb[CTRL_ATTR_OP_POLICY], ctx);
+
+ if (tb[CTRL_ATTR_POLICY])
+ process_dump_policy_nest(tb[CTRL_ATTR_POLICY], ctx);
+
+ return MNL_CB_OK;
+}
+
+int mnlu_gen_cmd_dump_policy(struct mnlu_gen_socket *nlg, uint8_t cmd)
+{
+ struct cmd_dump_policy_ctx ctx = {
+ .cmd = cmd,
+ };
+ struct nlmsghdr *nlh;
+ int err;
+
+ nlh = _mnlu_gen_socket_cmd_prepare(nlg, CTRL_CMD_GETPOLICY,
+ NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
+ GENL_ID_CTRL, 1);
+
+ mnl_attr_put_u16(nlh, CTRL_ATTR_FAMILY_ID, nlg->family);
+
+ err = mnlu_gen_socket_sndrcv(nlg, nlh, cmd_dump_policy_cb, &ctx);
+ if (err)
+ return err;
+
+ if (!ctx.dump_policy_idx_found || !ctx.do_policy_idx_found ||
+ ctx.do_policy_idx == ctx.dump_policy_idx ||
+ !ctx.dump_policy_attr_count)
+ return -ENOTSUP;
+
+ return 0;
+}
--
2.41.0
next prev parent reply other threads:[~2023-08-31 13:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-31 13:22 [patch iproute2-next 0/6] devlink: implement dump selector for devlink objects show commands Jiri Pirko
2023-08-31 13:22 ` [patch iproute2-next 1/6] devlink: move DL_OPT_SB into required options Jiri Pirko
2023-08-31 13:22 ` [patch iproute2-next 2/6] devlink: make parsing of handle non-destructive to argv Jiri Pirko
2023-08-31 13:22 ` [patch iproute2-next 3/6] devlink: implement command line args dry parsing Jiri Pirko
2023-08-31 13:22 ` [patch iproute2-next 4/6] devlink: return -ENOENT if argument is missing Jiri Pirko
2023-08-31 13:22 ` Jiri Pirko [this message]
2023-08-31 13:22 ` [patch iproute2-next 6/6] devlink: implement dump selector for devlink objects show commands Jiri Pirko
2023-09-04 9:33 ` [patch iproute2-next 0/6] " Jiri Pirko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230831132229.471693-6-jiri@resnulli.us \
--to=jiri@resnulli.us \
--cc=dsahern@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).