netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michal Kubecek <mkubecek@suse.cz>
To: John Linville <linville@tuxdriver.com>, netdev@vger.kernel.org
Cc: Andrew Lunn <andrew@lunn.ch>, Florian Fainelli <f.fainelli@gmail.com>
Subject: [PATCH ethtool v2 21/25] netlink: support for pretty printing netlink messages
Date: Wed,  4 Mar 2020 21:26:21 +0100 (CET)	[thread overview]
Message-ID: <54bf747aa2e8d890096c865d06414d3f7ec164a6.1583347351.git.mkubecek@suse.cz> (raw)
In-Reply-To: <cover.1583347351.git.mkubecek@suse.cz>

To improve message reporting and debugging, add support for displaying
netlink messages in human readable form, e.g.

    # ethtool --debug 0x10 -s eth0 msglvl drv on foo on probe off
    netlink error: bit name not found
    offending message and attribute:
        ETHTOOL_MSG_DEBUG_SET
            ETHTOOL_A_DEBUG_HEADER
                ETHTOOL_A_HEADER_DEV_NAME = "eth0"
            ETHTOOL_A_DEBUG_MSGMASK
                ETHTOOL_A_BITSET_BITS
                    ETHTOOL_A_BITSET_BITS_BIT
                        ETHTOOL_A_BITSET_BIT_NAME = "drv"
                        ETHTOOL_A_BITSET_BIT_VALUE = true
                    ETHTOOL_A_BITSET_BITS_BIT
    ===>                ETHTOOL_A_BITSET_BIT_NAME = "foo"
                        ETHTOOL_A_BITSET_BIT_VALUE = true
                    ETHTOOL_A_BITSET_BITS_BIT
                        ETHTOOL_A_BITSET_BIT_NAME = "probe"

This commit only adds support for parsing and displaying a message and
(optionally) highlighting an attribute on given offset (for extack error
reporting). To actually use it, one also needs message descriptions, i.e.
mapping of netlink attribute types to their symbolic names and payload
formats (depending on context).

Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
---
 Makefile.am         |   2 +-
 netlink/prettymsg.c | 193 ++++++++++++++++++++++++++++++++++++++++++++
 netlink/prettymsg.h | 102 +++++++++++++++++++++++
 3 files changed, 296 insertions(+), 1 deletion(-)
 create mode 100644 netlink/prettymsg.c
 create mode 100644 netlink/prettymsg.h

diff --git a/Makefile.am b/Makefile.am
index 38268932a955..baef0d20fa41 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -31,7 +31,7 @@ ethtool_SOURCES += \
 		  netlink/nlsock.h netlink/strset.c netlink/strset.h \
 		  netlink/monitor.c netlink/bitset.c netlink/bitset.h \
 		  netlink/settings.c netlink/parser.c netlink/parser.h \
-		  netlink/permaddr.c \
+		  netlink/permaddr.c netlink/prettymsg.c netlink/prettymsg.h \
 		  uapi/linux/ethtool_netlink.h \
 		  uapi/linux/netlink.h uapi/linux/genetlink.h \
 		  uapi/linux/rtnetlink.h uapi/linux/if_link.h
diff --git a/netlink/prettymsg.c b/netlink/prettymsg.c
new file mode 100644
index 000000000000..74fe6f2db7ed
--- /dev/null
+++ b/netlink/prettymsg.c
@@ -0,0 +1,193 @@
+/*
+ * prettymsg.c - human readable message dump
+ *
+ * Support for pretty print of an ethtool netlink message
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <stdint.h>
+#include <limits.h>
+#include <linux/genetlink.h>
+#include <linux/rtnetlink.h>
+#include <linux/if_link.h>
+#include <libmnl/libmnl.h>
+
+#include "prettymsg.h"
+
+#define __INDENT 4
+#define __DUMP_LINE 16
+#define __DUMP_BLOCK 4
+
+static void __print_binary_short(uint8_t *adata, unsigned int alen)
+{
+	unsigned int i;
+
+	if (!alen)
+		return;
+	printf("%02x", adata[0]);
+	for (i = 1; i < alen; i++)
+		printf("%c%02x", (i % __DUMP_BLOCK) ? ':' : ' ',  adata[i]);
+}
+
+static void __print_binary_long(uint8_t *adata, unsigned int alen,
+				unsigned int level)
+{
+	unsigned int i;
+
+	for (i = 0; i < alen; i++) {
+		if (i % __DUMP_LINE == 0)
+			printf("\n%*s", __INDENT * (level + 2), "");
+		else if (i % __DUMP_BLOCK == 0)
+			printf("  ");
+		else
+			putchar(' ');
+		printf("%02x", adata[i]);
+	}
+}
+
+static int pretty_print_attr(const struct nlattr *attr,
+			     const struct pretty_nla_desc *desc,
+			     unsigned int ndesc, unsigned int level,
+			     int err_offset, bool in_array)
+{
+	unsigned int alen = mnl_attr_get_payload_len(attr);
+	unsigned int atype = mnl_attr_get_type(attr);
+	unsigned int desc_idx = in_array ? 0 : atype;
+	void *adata = mnl_attr_get_payload(attr);
+	const struct pretty_nla_desc *adesc;
+	const char *prefix = "    ";
+	bool nested;
+
+	adesc = (desc && desc_idx < ndesc) ? &desc[desc_idx] : NULL;
+	nested = (adesc && (adesc->format == NLA_NESTED ||
+			    adesc->format == NLA_ARRAY)) ||
+		 (attr->nla_type & NLA_F_NESTED);
+	if (err_offset >= 0 &&
+	    err_offset < (nested ? NLA_HDRLEN : attr->nla_len)) {
+		prefix = "===>";
+		if (err_offset)
+			fprintf(stderr,
+				"ethtool: bad_attr inside an attribute (offset %d)\n",
+				err_offset);
+	}
+	if (adesc && adesc->name && !in_array)
+		printf("%s%*s%s", prefix, level * __INDENT, "", adesc->name);
+	else
+		printf("%s%*s[%u]", prefix, level * __INDENT, "", atype);
+
+	if (nested) {
+		struct nlattr *child;
+		int ret = 0;
+
+		putchar('\n');
+		mnl_attr_for_each_nested(child, attr) {
+			bool array = adesc && adesc->format == NLA_ARRAY;
+			unsigned int child_off;
+
+			child_off = (const char *)child - (const char *)attr;
+			ret = pretty_print_attr(child,
+						adesc ? adesc->children : NULL,
+						adesc ? adesc->n_children : 0,
+						level + 1,
+						err_offset - child_off, array);
+			if (ret < 0)
+				break;
+		}
+
+		return ret;
+	}
+
+	printf(" = ");
+	switch(adesc ? adesc->format : NLA_BINARY) {
+	case NLA_U8:
+		printf("%u", mnl_attr_get_u8(attr));
+		break;
+	case NLA_U16:
+		printf("%u", mnl_attr_get_u16(attr));
+		break;
+	case NLA_U32:
+		printf("%u", mnl_attr_get_u32(attr));
+		break;
+	case NLA_X8:
+		printf("0x%02x", mnl_attr_get_u8(attr));
+		break;
+	case NLA_X16:
+		printf("0x%04x", mnl_attr_get_u16(attr));
+		break;
+	case NLA_X32:
+		printf("0x%08x", mnl_attr_get_u32(attr));
+		break;
+	case NLA_S8:
+		printf("%d", (int)mnl_attr_get_u8(attr));
+		break;
+	case NLA_S16:
+		printf("%d", (int)mnl_attr_get_u16(attr));
+		break;
+	case NLA_S32:
+		printf("%d", (int)mnl_attr_get_u32(attr));
+		break;
+	case NLA_STRING:
+		printf("\"%.*s\"", alen, (const char *)adata);
+		break;
+	case NLA_FLAG:
+		printf("true");
+		break;
+	case NLA_BOOL:
+		printf("%s", mnl_attr_get_u8(attr) ? "on" : "off");
+		break;
+	default:
+		if (alen <= __DUMP_LINE)
+			__print_binary_short(adata, alen);
+		else
+			__print_binary_long(adata, alen, level);
+	}
+	putchar('\n');
+
+	return 0;
+}
+
+static int pretty_print_nlmsg(const struct nlmsghdr *nlhdr,
+			      unsigned int payload_offset,
+			      const struct pretty_nla_desc *desc,
+			      unsigned int ndesc, unsigned int err_offset)
+{
+	const struct nlattr *attr;
+	int attr_offset;
+	int ret;
+
+	mnl_attr_for_each(attr, nlhdr, payload_offset) {
+		attr_offset = (const char *)attr - (const char *)nlhdr;
+		ret = pretty_print_attr(attr, desc, ndesc, 1,
+					err_offset - attr_offset, false);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+int pretty_print_genlmsg(const struct nlmsghdr *nlhdr,
+			 const struct pretty_nlmsg_desc *desc,
+			 unsigned int ndesc, unsigned int err_offset)
+{
+	const struct pretty_nlmsg_desc *msg_desc;
+	const struct genlmsghdr *genlhdr;
+
+	if (mnl_nlmsg_get_payload_len(nlhdr) < GENL_HDRLEN) {
+		fprintf(stderr, "ethtool: message too short (%u bytes)\n",
+			nlhdr->nlmsg_len);
+		return -EINVAL;
+	}
+	genlhdr = mnl_nlmsg_get_payload(nlhdr);
+	msg_desc = (desc && genlhdr->cmd < ndesc) ? &desc[genlhdr->cmd] : NULL;
+	if (msg_desc && msg_desc->name)
+		printf("    %s\n", msg_desc->name);
+	else
+		printf("    [%u]\n", genlhdr->cmd);
+
+	return pretty_print_nlmsg(nlhdr, GENL_HDRLEN,
+				  msg_desc ? msg_desc->attrs : NULL,
+				  msg_desc ? msg_desc->n_attrs : 0, err_offset);
+}
diff --git a/netlink/prettymsg.h b/netlink/prettymsg.h
new file mode 100644
index 000000000000..68ec275a22f6
--- /dev/null
+++ b/netlink/prettymsg.h
@@ -0,0 +1,102 @@
+/*
+ * prettymsg.h - human readable message dump
+ *
+ * Support for pretty print of an ethtool netlink message
+ */
+
+#ifndef ETHTOOL_NETLINK_PRETTYMSG_H__
+#define ETHTOOL_NETLINK_PRETTYMSG_H__
+
+#include <linux/netlink.h>
+
+/* data structures for message format descriptions */
+
+enum pretty_nla_format {
+	NLA_INVALID,
+	NLA_BINARY,
+	NLA_U8,
+	NLA_U16,
+	NLA_U32,
+	NLA_X8,
+	NLA_X16,
+	NLA_X32,
+	NLA_S8,
+	NLA_S16,
+	NLA_S32,
+	NLA_STRING,
+	NLA_FLAG,
+	NLA_BOOL,
+	NLA_NESTED,
+	NLA_ARRAY,
+};
+
+struct pretty_nla_desc {
+	enum pretty_nla_format		format;
+	const char			*name;
+	const struct pretty_nla_desc	*children;
+	unsigned int			n_children;
+};
+
+struct pretty_nlmsg_desc {
+	const char			*name;
+	const struct pretty_nla_desc	*attrs;
+	unsigned int			n_attrs;
+};
+
+/* helper macros for message format descriptions */
+
+#define NLATTR_DESC(_name, _fmt) \
+	[_name] = { \
+		.format = _fmt, \
+		.name = #_name, \
+	}
+
+#define NLATTR_DESC_INVALID(_name)	NLATTR_DESC(_name, NLA_INVALID)
+#define NLATTR_DESC_U8(_name)		NLATTR_DESC(_name, NLA_U8)
+#define NLATTR_DESC_U16(_name)		NLATTR_DESC(_name, NLA_U16)
+#define NLATTR_DESC_U32(_name)		NLATTR_DESC(_name, NLA_U32)
+#define NLATTR_DESC_X8(_name)		NLATTR_DESC(_name, NLA_X8)
+#define NLATTR_DESC_X16(_name)		NLATTR_DESC(_name, NLA_X16)
+#define NLATTR_DESC_X32(_name)		NLATTR_DESC(_name, NLA_X32)
+#define NLATTR_DESC_S8(_name)		NLATTR_DESC(_name, NLA_U8)
+#define NLATTR_DESC_S16(_name)		NLATTR_DESC(_name, NLA_U16)
+#define NLATTR_DESC_S32(_name)		NLATTR_DESC(_name, NLA_U32)
+#define NLATTR_DESC_STRING(_name)	NLATTR_DESC(_name, NLA_STRING)
+#define NLATTR_DESC_FLAG(_name)		NLATTR_DESC(_name, NLA_FLAG)
+#define NLATTR_DESC_BOOL(_name)		NLATTR_DESC(_name, NLA_BOOL)
+#define NLATTR_DESC_BINARY(_name)	NLATTR_DESC(_name, NLA_BINARY)
+
+#define NLATTR_DESC_NESTED(_name, _children_desc) \
+	[_name] = { \
+		.format = NLA_NESTED, \
+		.name = #_name, \
+		.children = __ ## _children_desc ## _desc, \
+		.n_children = ARRAY_SIZE(__ ## _children_desc ## _desc), \
+	}
+#define NLATTR_DESC_NESTED_NODESC(_name) NLATTR_DESC(_name, NLA_NESTED)
+#define NLATTR_DESC_ARRAY(_name, _children_desc) \
+	[_name] = { \
+		.format = NLA_ARRAY, \
+		.name = #_name, \
+		.children = __ ## _children_desc ## _desc, \
+		.n_children = 1, \
+	}
+
+#define NLMSG_DESC(_name, _attrs) \
+	[_name] = { \
+		.name = #_name, \
+		.attrs = __ ## _attrs ## _desc, \
+		.n_attrs = ARRAY_SIZE(__ ## _attrs ## _desc), \
+	}
+
+#define NLMSG_DESC_INVALID(_name) \
+	[_name] = { \
+		.name = #_name, \
+	}
+
+/* function to pretty print a genetlink message */
+int pretty_print_genlmsg(const struct nlmsghdr *nlhdr,
+			 const struct pretty_nlmsg_desc *desc,
+			 unsigned int ndesc, unsigned int err_offset);
+
+#endif /* ETHTOOL_NETLINK_PRETTYMSG_H__ */
-- 
2.25.1


  parent reply	other threads:[~2020-03-04 20:26 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-04 20:24 [PATCH ethtool v2 00/25] initial netlink interface implementation for 5.6 release Michal Kubecek
2020-03-04 20:24 ` [PATCH ethtool v2 01/25] move UAPI header copies to a separate directory Michal Kubecek
2020-03-04 20:24 ` [PATCH ethtool v2 02/25] update UAPI header copies Michal Kubecek
2020-03-04 20:24 ` [PATCH ethtool v2 03/25] add --debug option to control debugging messages Michal Kubecek
2020-03-04 20:24 ` [PATCH ethtool v2 04/25] use named initializers in command line option list Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 05/25] netlink: add netlink related UAPI header files Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 06/25] netlink: introduce the netlink interface Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 07/25] netlink: message buffer and composition helpers Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 08/25] netlink: netlink socket wrapper and helpers Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 09/25] netlink: initialize ethtool netlink socket Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 10/25] netlink: add support for string sets Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 11/25] netlink: add notification monitor Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 12/25] move shared code into a common file Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 13/25] netlink: add bitset helpers Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 14/25] netlink: partial netlink handler for gset (no option) Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 15/25] netlink: support getting wake-on-lan and debugging settings Michal Kubecek
2020-03-04 20:25 ` [PATCH ethtool v2 16/25] netlink: add basic command line parsing helpers Michal Kubecek
2020-03-04 20:26 ` [PATCH ethtool v2 17/25] netlink: add bitset command line parser handlers Michal Kubecek
2020-03-04 20:26 ` [PATCH ethtool v2 18/25] netlink: add netlink handler for sset (-s) Michal Kubecek
2020-03-04 20:26 ` [PATCH ethtool v2 19/25] netlink: support tests with netlink enabled Michal Kubecek
2020-03-04 20:26 ` [PATCH ethtool v2 20/25] netlink: add handler for permaddr (-P) Michal Kubecek
2020-03-04 20:26 ` Michal Kubecek [this message]
2020-03-04 20:26 ` [PATCH ethtool v2 22/25] netlink: message format description for ethtool netlink Michal Kubecek
2020-03-04 20:26 ` [PATCH ethtool v2 23/25] netlink: message format descriptions for genetlink control Michal Kubecek
2020-03-04 20:26 ` [PATCH ethtool v2 24/25] netlink: message format descriptions for rtnetlink Michal Kubecek
2020-03-04 20:26 ` [PATCH ethtool v2 25/25] netlink: use pretty printing for ethtool netlink messages Michal Kubecek
2020-03-04 20:34 ` [PATCH ethtool v2 00/25] initial netlink interface implementation for 5.6 release Michal Kubecek
2020-03-05 19:24 ` John W. Linville
2020-03-05 20:32   ` Michal Kubecek
2020-03-05 21:14     ` Michal Kubecek

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=54bf747aa2e8d890096c865d06414d3f7ec164a6.1583347351.git.mkubecek@suse.cz \
    --to=mkubecek@suse.cz \
    --cc=andrew@lunn.ch \
    --cc=f.fainelli@gmail.com \
    --cc=linville@tuxdriver.com \
    --cc=netdev@vger.kernel.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).