[PATCH ethtool v2 3/3] ethtool: pse-pd: Add PSE event monitoring support

Kory Maincent posted 3 patches 4 months, 1 week ago
There is a newer version of this series
[PATCH ethtool v2 3/3] ethtool: pse-pd: Add PSE event monitoring support
Posted by Kory Maincent 4 months, 1 week ago
From: Kory Maincent (Dent Project) <kory.maincent@bootlin.com>

Add support for PSE (Power Sourcing Equipment) event monitoring
capabilities through the monitor command.

Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
---
 netlink/monitor.c |  9 ++++++++-
 netlink/netlink.h |  1 +
 netlink/pse-pd.c  | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/netlink/monitor.c b/netlink/monitor.c
index c511389..a16cb97 100644
--- a/netlink/monitor.c
+++ b/netlink/monitor.c
@@ -79,6 +79,10 @@ static struct {
 		.cmd	= ETHTOOL_MSG_PLCA_NTF,
 		.cb	= plca_get_cfg_reply_cb,
 	},
+	{
+		.cmd	= ETHTOOL_MSG_PSE_NTF,
+		.cb	= pse_ntf_cb,
+	},
 };
 
 static void clear_filter(struct nl_context *nlctx)
@@ -194,7 +198,10 @@ static struct monitor_option monitor_opts[] = {
 		.pattern	= "--get-plca-cfg|--set-plca-cfg",
 		.cmd		= ETHTOOL_MSG_PLCA_NTF,
 	},
-
+	{
+		.pattern	= "--pse-event",
+		.cmd		= ETHTOOL_MSG_PSE_NTF,
+	},
 };
 
 static bool pattern_match(const char *s, const char *pattern)
diff --git a/netlink/netlink.h b/netlink/netlink.h
index 290592b..eefedf7 100644
--- a/netlink/netlink.h
+++ b/netlink/netlink.h
@@ -93,6 +93,7 @@ int cable_test_tdr_ntf_cb(const struct nlmsghdr *nlhdr, void *data);
 int fec_reply_cb(const struct nlmsghdr *nlhdr, void *data);
 int module_reply_cb(const struct nlmsghdr *nlhdr, void *data);
 int plca_get_cfg_reply_cb(const struct nlmsghdr *nlhdr, void *data);
+int pse_ntf_cb(const struct nlmsghdr *nlhdr, void *data);
 
 /* dump helpers */
 
diff --git a/netlink/pse-pd.c b/netlink/pse-pd.c
index 5bde176..3fb0616 100644
--- a/netlink/pse-pd.c
+++ b/netlink/pse-pd.c
@@ -475,6 +475,64 @@ int nl_gpse(struct cmd_context *ctx)
 	return ret;
 }
 
+static const char *pse_events_name(u64 val)
+{
+	switch (val) {
+	case ETHTOOL_PSE_EVENT_OVER_CURRENT:
+		return "over-current";
+	case ETHTOOL_PSE_EVENT_OVER_TEMP:
+		return "over-temperature";
+	case ETHTOOL_C33_PSE_EVENT_DETECTION:
+		return "detection";
+	case ETHTOOL_C33_PSE_EVENT_CLASSIFICATION:
+		return "classification";
+	case ETHTOOL_C33_PSE_EVENT_DISCONNECTION:
+		return "disconnection";
+	case ETHTOOL_PSE_EVENT_OVER_BUDGET:
+		return "over-budget";
+	case ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR:
+		return "software power control error";
+	default:
+		return "unknown";
+	}
+}
+
+int pse_ntf_cb(const struct nlmsghdr *nlhdr, void *data)
+{
+	const struct nlattr *tb[ETHTOOL_A_PSE_MAX + 1] = {};
+	struct nl_context *nlctx = data;
+	DECLARE_ATTR_TB_INFO(tb);
+	u64 val;
+	int ret, i;
+
+	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
+	if (ret < 0)
+		return MNL_CB_OK;
+
+	if (!tb[ETHTOOL_A_PSE_NTF_EVENTS])
+		return MNL_CB_OK;
+
+	nlctx->devname = get_dev_name(tb[ETHTOOL_A_PSE_HEADER]);
+	if (!dev_ok(nlctx))
+		return MNL_CB_OK;
+
+	open_json_object(NULL);
+	print_string(PRINT_ANY, "ifname", "PSE event for %s:\n",
+		     nlctx->devname);
+	open_json_array("events", "Events:");
+	val = attr_get_uint(tb[ETHTOOL_A_PSE_NTF_EVENTS]);
+	for (i = 0; 1 << i <= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR; i++)
+		if (val & 1 << i)
+			print_string(PRINT_ANY, NULL, " %s",
+				     pse_events_name(val & 1 << i));
+	close_json_array("\n");
+	if (ret < 0)
+		return MNL_CB_OK;
+
+	close_json_object();
+	return MNL_CB_OK;
+}
+
 /* PSE_SET */
 
 static const struct lookup_entry_u32 podl_pse_admin_control_values[] = {

-- 
2.43.0
Re: [PATCH ethtool v2 3/3] ethtool: pse-pd: Add PSE event monitoring support
Posted by Oleksij Rempel 4 months, 1 week ago
On Wed, Aug 13, 2025 at 10:57:52AM +0200, Kory Maincent wrote:
> From: Kory Maincent (Dent Project) <kory.maincent@bootlin.com>
.... 
> diff --git a/netlink/pse-pd.c b/netlink/pse-pd.c
> index 5bde176..3fb0616 100644
> --- a/netlink/pse-pd.c
> +++ b/netlink/pse-pd.c
> @@ -475,6 +475,64 @@ int nl_gpse(struct cmd_context *ctx)
>  	return ret;
>  }
>  
> +static const char *pse_events_name(u64 val)
> +{
> +	switch (val) {
> +	case ETHTOOL_PSE_EVENT_OVER_CURRENT:
> +		return "over-current";
> +	case ETHTOOL_PSE_EVENT_OVER_TEMP:
> +		return "over-temperature";
> +	case ETHTOOL_C33_PSE_EVENT_DETECTION:
> +		return "detection";
> +	case ETHTOOL_C33_PSE_EVENT_CLASSIFICATION:
> +		return "classification";
> +	case ETHTOOL_C33_PSE_EVENT_DISCONNECTION:
> +		return "disconnection";
> +	case ETHTOOL_PSE_EVENT_OVER_BUDGET:
> +		return "over-budget";
> +	case ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR:
> +		return "software power control error";
> +	default:
> +		return "unknown";
> +	}
> +}
> +
> +int pse_ntf_cb(const struct nlmsghdr *nlhdr, void *data)
> +{
> +	const struct nlattr *tb[ETHTOOL_A_PSE_MAX + 1] = {};

s/ETHTOOL_A_PSE_MAX/ETHTOOL_A_PSE_NTF_MAX ?

> +	struct nl_context *nlctx = data;
> +	DECLARE_ATTR_TB_INFO(tb);
> +	u64 val;
> +	int ret, i;
> +
> +	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
> +	if (ret < 0)
> +		return MNL_CB_OK;
> +
> +	if (!tb[ETHTOOL_A_PSE_NTF_EVENTS])
> +		return MNL_CB_OK;
> +
> +	nlctx->devname = get_dev_name(tb[ETHTOOL_A_PSE_HEADER]);

s/ETHTOOL_A_PSE_HEADER/ETHTOOL_A_PSE_NTF_HEADER ?

> +	if (!dev_ok(nlctx))
> +		return MNL_CB_OK;
> +
> +	open_json_object(NULL);
> +	print_string(PRINT_ANY, "ifname", "PSE event for %s:\n",
> +		     nlctx->devname);
> +	open_json_array("events", "Events:");
> +	val = attr_get_uint(tb[ETHTOOL_A_PSE_NTF_EVENTS]);

we have here uint but val is u64, is it as expected?

> +	for (i = 0; 1 << i <= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR; i++)
> +		if (val & 1 << i)
> +			print_string(PRINT_ANY, NULL, " %s",
> +				     pse_events_name(val & 1 << i));

Hm, may be it is better to not limit to ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR
and report unknow numeric value. It will keep even old ethtool at least
partially usable.
-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Re: [PATCH ethtool v2 3/3] ethtool: pse-pd: Add PSE event monitoring support
Posted by Kory Maincent 4 months, 1 week ago
Le Wed, 13 Aug 2025 14:44:27 +0200,
Oleksij Rempel <o.rempel@pengutronix.de> a écrit :

> > +int pse_ntf_cb(const struct nlmsghdr *nlhdr, void *data)
> > +{
> > +	const struct nlattr *tb[ETHTOOL_A_PSE_MAX + 1] = {};  
> 
> s/ETHTOOL_A_PSE_MAX/ETHTOOL_A_PSE_NTF_MAX ?

Thanks, well spotted.

> > +	struct nl_context *nlctx = data;
> > +	DECLARE_ATTR_TB_INFO(tb);
> > +	u64 val;
> > +	int ret, i;
> > +
> > +	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
> > +	if (ret < 0)
> > +		return MNL_CB_OK;
> > +
> > +	if (!tb[ETHTOOL_A_PSE_NTF_EVENTS])
> > +		return MNL_CB_OK;
> > +
> > +	nlctx->devname = get_dev_name(tb[ETHTOOL_A_PSE_HEADER]);  
> 
> s/ETHTOOL_A_PSE_HEADER/ETHTOOL_A_PSE_NTF_HEADER ?

Thanks, well spotted.

> > +	if (!dev_ok(nlctx))
> > +		return MNL_CB_OK;
> > +
> > +	open_json_object(NULL);
> > +	print_string(PRINT_ANY, "ifname", "PSE event for %s:\n",
> > +		     nlctx->devname);
> > +	open_json_array("events", "Events:");
> > +	val = attr_get_uint(tb[ETHTOOL_A_PSE_NTF_EVENTS]);  
> 
> we have here uint but val is u64, is it as expected?

Yes, same behavior in Linux using nla_put_uint().

> > +	for (i = 0; 1 << i <= ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR; i++)
> > +		if (val & 1 << i)
> > +			print_string(PRINT_ANY, NULL, " %s",
> > +				     pse_events_name(val & 1 << i));  
> 
> Hm, may be it is better to not limit to ETHTOOL_PSE_EVENT_SW_PW_CONTROL_ERROR
> and report unknow numeric value. It will keep even old ethtool at least
> partially usable.

Ok, I will loop until UINT_MAX then.

Regards,
-- 
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com