From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, edumazet@google.com, pabeni@redhat.com,
Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next 06/10] tools: ynl-gen: generate enum-to-string helpers
Date: Thu, 1 Jun 2023 19:35:44 -0700 [thread overview]
Message-ID: <20230602023548.463441-7-kuba@kernel.org> (raw)
In-Reply-To: <20230602023548.463441-1-kuba@kernel.org>
It's sometimes useful to print the name of an enum value,
flag or name of the op. Python can do it, add C helper
code gen for getting names of things.
Example:
static const char * const netdev_xdp_act_strmap[] = {
[0] = "basic",
[1] = "redirect",
[2] = "ndo-xmit",
[3] = "xsk-zerocopy",
[4] = "hw-offload",
[5] = "rx-sg",
[6] = "ndo-xmit-sg",
};
const char *netdev_xdp_act_str(enum netdev_xdp_act value)
{
value = ffs(value) - 1;
if (value < 0 || value >= (int)MNL_ARRAY_SIZE(netdev_xdp_act_strmap))
return NULL;
return netdev_xdp_act_strmap[value];
}
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
tools/net/ynl/ynl-gen-c.py | 66 ++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py
index 8bf4b70216d7..5318edfdb874 100755
--- a/tools/net/ynl/ynl-gen-c.py
+++ b/tools/net/ynl/ynl-gen-c.py
@@ -1168,6 +1168,56 @@ _C_KW = {
cw.nl()
+def put_op_name_fwd(family, cw):
+ cw.write_func_prot('const char *', f'{family.name}_op_str', ['int op'], suffix=';')
+
+
+def put_op_name(family, cw):
+ map_name = f'{family.name}_op_strmap'
+ cw.block_start(line=f"static const char * const {map_name}[] =")
+ for op_name, op in family.msgs.items():
+ cw.p(f'[{op.enum_name}] = "{op_name}",')
+ cw.block_end(line=';')
+ cw.nl()
+
+ cw.write_func_prot('const char *', f'{family.name}_op_str', ['int op'])
+ cw.block_start()
+ cw.p(f'if (op < 0 || op >= (int)MNL_ARRAY_SIZE({map_name}))')
+ cw.p('return NULL;')
+ cw.p(f'return {map_name}[op];')
+ cw.block_end()
+ cw.nl()
+
+
+def put_enum_to_str_fwd(family, cw, enum):
+ args = [f'enum {enum.render_name} value']
+ if 'enum-name' in enum and not enum['enum-name']:
+ args = ['int value']
+ cw.write_func_prot('const char *', f'{enum.render_name}_str', args, suffix=';')
+
+
+def put_enum_to_str(family, cw, enum):
+ map_name = f'{enum.render_name}_strmap'
+ cw.block_start(line=f"static const char * const {map_name}[] =")
+ for entry in enum.entries.values():
+ cw.p(f'[{entry.value}] = "{entry.name}",')
+ cw.block_end(line=';')
+ cw.nl()
+
+ args = [f'enum {enum.render_name} value']
+ if 'enum-name' in enum and not enum['enum-name']:
+ args = ['int value']
+ cw.write_func_prot('const char *', f'{enum.render_name}_str', args)
+ cw.block_start()
+ if enum.type == 'flags':
+ cw.p('value = ffs(value) - 1;')
+ cw.p(f'if (value < 0 || value >= (int)MNL_ARRAY_SIZE({map_name}))')
+ cw.p('return NULL;')
+ cw.p(f'return {map_name}[value];')
+ cw.block_end()
+ cw.nl()
+
+
def put_req_nested(ri, struct):
func_args = ['struct nlmsghdr *nlh',
'unsigned int attr_type',
@@ -2210,6 +2260,14 @@ _C_KW = {
if args.mode == "user":
has_ntf = False
if args.header:
+ cw.p('/* Enums */')
+ put_op_name_fwd(parsed, cw)
+
+ for name, const in parsed.consts.items():
+ if isinstance(const, EnumSet):
+ put_enum_to_str_fwd(parsed, cw, const)
+ cw.nl()
+
cw.p('/* Common nested types */')
for attr_set, struct in sorted(parsed.pure_nested_structs.items()):
ri = RenderInfo(cw, parsed, args.mode, "", "", "", attr_set)
@@ -2262,6 +2320,14 @@ _C_KW = {
print_ntf_parse_prototype(parsed, cw)
cw.nl()
else:
+ cw.p('/* Enums */')
+ put_op_name(parsed, cw)
+
+ for name, const in parsed.consts.items():
+ if isinstance(const, EnumSet):
+ put_enum_to_str(parsed, cw, const)
+ cw.nl()
+
cw.p('/* Policies */')
for name, _ in parsed.attr_sets.items():
struct = Struct(parsed, name)
--
2.40.1
next prev parent reply other threads:[~2023-06-02 2:35 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-02 2:35 [PATCH net-next 00/10] tools: ynl-gen: dust off the user space code Jakub Kicinski
2023-06-02 2:35 ` [PATCH net-next 01/10] tools: ynl-gen: add extra headers for user space Jakub Kicinski
2023-06-02 2:35 ` [PATCH net-next 02/10] tools: ynl-gen: fix unused / pad attribute handling Jakub Kicinski
2023-06-02 2:35 ` [PATCH net-next 03/10] tools: ynl-gen: don't override pure nested struct Jakub Kicinski
2023-06-02 2:35 ` [PATCH net-next 04/10] tools: ynl-gen: loosen type consistency check for events Jakub Kicinski
2023-06-02 2:35 ` [PATCH net-next 05/10] tools: ynl-gen: add error checking for nested structs Jakub Kicinski
2023-06-02 2:35 ` Jakub Kicinski [this message]
2023-06-02 2:35 ` [PATCH net-next 07/10] tools: ynl-gen: move the response reading logic into YNL Jakub Kicinski
2023-06-02 2:35 ` [PATCH net-next 08/10] tools: ynl-gen: generate alloc and free helpers for req Jakub Kicinski
2023-06-02 2:35 ` [PATCH net-next 09/10] tools: ynl-gen: switch to family struct Jakub Kicinski
2023-06-02 2:35 ` [PATCH net-next 10/10] tools: ynl-gen: generate static descriptions of notifications Jakub Kicinski
2023-06-03 6:30 ` [PATCH net-next 00/10] tools: ynl-gen: dust off the user space code patchwork-bot+netdevbpf
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=20230602023548.463441-7-kuba@kernel.org \
--to=kuba@kernel.org \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.