Blame SOURCES/0034-netlink-support-u32-enumerated-types-in-pretty-print.patch

6975f2
From 856c0c40eb221499b7326c216aaf87691062eacb Mon Sep 17 00:00:00 2001
6975f2
From: Michal Kubecek <mkubecek@suse.cz>
6975f2
Date: Mon, 19 Oct 2020 23:32:40 +0200
6975f2
Subject: [PATCH 34/37] netlink: support u32 enumerated types in pretty
6975f2
 printing
6975f2
6975f2
Some numeric attributes take values from a short list/range with symbolic
6975f2
names. Showing the symbolic names instead of numeric values will make the
6975f2
pretty printed netlink messages easier to read. If the value is too big for
6975f2
provided names array (e.g. running on newer kernel) or the name is omitted,
6975f2
numeric attribute value is shown.
6975f2
6975f2
Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
6975f2
(cherry picked from commit c7fe2f38477f5f90569f73bacd6fab3a8a739f62)
6975f2
---
6975f2
 netlink/prettymsg.c |  9 +++++++++
6975f2
 netlink/prettymsg.h | 18 ++++++++++++++++--
6975f2
 2 files changed, 25 insertions(+), 2 deletions(-)
6975f2
6975f2
diff --git a/netlink/prettymsg.c b/netlink/prettymsg.c
6975f2
index f992dcaf071f..d5d999fddfbb 100644
6975f2
--- a/netlink/prettymsg.c
6975f2
+++ b/netlink/prettymsg.c
6975f2
@@ -137,6 +137,15 @@ static int pretty_print_attr(const struct nlattr *attr,
6975f2
 	case NLA_BOOL:
6975f2
 		printf("%s", mnl_attr_get_u8(attr) ? "on" : "off");
6975f2
 		break;
6975f2
+	case NLA_U32_ENUM: {
6975f2
+		uint32_t val = mnl_attr_get_u32(attr);
6975f2
+
6975f2
+		if (adesc && val < adesc->n_names && adesc->names[val])
6975f2
+			printf("%s", adesc->names[val]);
6975f2
+		else
6975f2
+			printf("%u", val);
6975f2
+		break;
6975f2
+	}
6975f2
 	default:
6975f2
 		if (alen <= __DUMP_LINE)
6975f2
 			__print_binary_short(adata, alen);
6975f2
diff --git a/netlink/prettymsg.h b/netlink/prettymsg.h
6975f2
index b5e5f735ac8a..6987c6ec5bca 100644
6975f2
--- a/netlink/prettymsg.h
6975f2
+++ b/netlink/prettymsg.h
6975f2
@@ -28,13 +28,20 @@ enum pretty_nla_format {
6975f2
 	NLA_BOOL,
6975f2
 	NLA_NESTED,
6975f2
 	NLA_ARRAY,
6975f2
+	NLA_U32_ENUM,
6975f2
 };
6975f2
 
6975f2
 struct pretty_nla_desc {
6975f2
 	enum pretty_nla_format		format;
6975f2
 	const char			*name;
6975f2
-	const struct pretty_nla_desc	*children;
6975f2
-	unsigned int			n_children;
6975f2
+	union {
6975f2
+		const struct pretty_nla_desc	*children;
6975f2
+		const char			*const *names;
6975f2
+	};
6975f2
+	union {
6975f2
+		unsigned int			n_children;
6975f2
+		unsigned int			n_names;
6975f2
+	};
6975f2
 };
6975f2
 
6975f2
 struct pretty_nlmsg_desc {
6975f2
@@ -81,6 +88,13 @@ struct pretty_nlmsg_desc {
6975f2
 		.children = __ ## _children_desc ## _desc, \
6975f2
 		.n_children = 1, \
6975f2
 	}
6975f2
+#define NLATTR_DESC_U32_ENUM(_name, _names_table) \
6975f2
+	[_name] = { \
6975f2
+		.format = NLA_U32_ENUM, \
6975f2
+		.name = #_name, \
6975f2
+		.names = __ ## _names_table ## _names, \
6975f2
+		.n_children = ARRAY_SIZE(__ ## _names_table ## _names), \
6975f2
+	}
6975f2
 
6975f2
 #define NLMSG_DESC(_name, _attrs) \
6975f2
 	[_name] = { \
6975f2
-- 
6975f2
2.26.2
6975f2