All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: Maxime Chevallier <maxime.chevallier@bootlin.com>
Cc: davem@davemloft.net, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com,
	"Andrew Lunn" <andrew@lunn.ch>,
	"Eric Dumazet" <edumazet@google.com>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Russell King" <linux@armlinux.org.uk>,
	linux-arm-kernel@lists.infradead.org,
	"Christophe Leroy" <christophe.leroy@csgroup.eu>,
	"Herve Codina" <herve.codina@bootlin.com>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Heiner Kallweit" <hkallweit1@gmail.com>,
	"Vladimir Oltean" <vladimir.oltean@nxp.com>,
	"Köry Maincent" <kory.maincent@bootlin.com>,
	"Jesse Brandeburg" <jesse.brandeburg@intel.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Marek Behún" <kabel@kernel.org>,
	"Piergiorgio Beruto" <piergiorgio.beruto@gmail.com>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>,
	"Nicolò Veronese" <nicveronese@gmail.com>,
	"Simon Horman" <horms@kernel.org>,
	mwojtas@chromium.org
Subject: Re: [PATCH net-next v10 07/13] net: ethtool: Introduce a command to list PHYs on an interface
Date: Fri, 8 Mar 2024 21:18:42 -0800	[thread overview]
Message-ID: <20240308211842.6d64b5bc@kernel.org> (raw)
In-Reply-To: <20240304151011.1610175-8-maxime.chevallier@bootlin.com>

On Mon,  4 Mar 2024 16:10:03 +0100 Maxime Chevallier wrote:
> +PHY_GET
> +=======
> +
> +Retrieve information about a given Ethernet PHY sitting on the link. As there
> +can be more than one PHY, the DUMP operation can be used to list the PHYs
> +present on a given interface, by passing an interface index or name in
> +the dump request

Could be worth re-stating the default behavior of the DO request?
That the DO will return the dev->phydev by default and if PHY_INDEX
is specified in the header, the particular PHY with that index?

> +	if (phydev->drv && nla_put_string(skb, ETHTOOL_A_PHY_DRVNAME, phydev->drv->name))

could you break the lines at 80 where it doesn't hurt readability?
This way:

	if (phydev->drv && 
	    nla_put_string(skb, ETHTOOL_A_PHY_DRVNAME, phydev->drv->name))

> +		return -EMSGSIZE;
> +
> +	if (ptype == PHY_UPSTREAM_PHY) {
> +		struct phy_device *upstream = pdn->upstream.phydev;
> +		const char *sfp_upstream_name;
> +
> +		/* Parent index */
> +		if (nla_put_u32(skb, ETHTOOL_A_PHY_UPSTREAM_INDEX, upstream->phyindex))
> +			return -EMSGSIZE;
> +
> +		if (pdn->parent_sfp_bus) {
> +			sfp_upstream_name = sfp_get_name(pdn->parent_sfp_bus);
> +			if (sfp_upstream_name && nla_put_string(skb,
> +								ETHTOOL_A_PHY_UPSTREAM_SFP_NAME,
> +								sfp_upstream_name))

ditto

> +				return -EMSGSIZE;
> +		}
> +	}

> +int ethnl_phy_doit(struct sk_buff *skb, struct genl_info *info)
> +{
> +	struct phy_req_info req_info = {};
> +	struct nlattr **tb = info->attrs;
> +	struct sk_buff *rskb;
> +	void *reply_payload;
> +	int reply_len;
> +	int ret;
> +
> +	ret = ethnl_parse_header_dev_get(&req_info.base,
> +					 tb[ETHTOOL_A_PHY_HEADER],
> +					 genl_info_net(info), info->extack,
> +					 true);
> +	if (ret < 0)
> +		return ret;
> +
> +	rtnl_lock();
> +
> +	ret = ethnl_phy_parse_request(&req_info.base, tb);
> +	if (ret < 0)
> +		goto err_unlock_rtnl;
> +
> +	/* No PHY, return early */
> +	if (!req_info.pdn.phy)
> +		goto err_unlock_rtnl;
> +
> +	ret = ethnl_phy_reply_size(&req_info.base, info->extack);
> +	if (ret < 0)
> +		goto err_unlock_rtnl;
> +	reply_len = ret + ethnl_reply_header_size();
> +
> +	rskb = ethnl_reply_init(reply_len, req_info.base.dev,
> +				ETHTOOL_MSG_PHY_GET_REPLY,
> +				ETHTOOL_A_PHY_HEADER,
> +				info, &reply_payload);
> +	if (!rskb) {
> +		ret = -ENOMEM;
> +		goto err_unlock_rtnl;
> +	}
> +
> +	ret = ethnl_phy_fill_reply(&req_info.base, rskb);
> +	if (ret)
> +		goto err_free_msg;
> +
> +	rtnl_unlock();
> +	ethnl_parse_header_dev_put(&req_info.base);
> +	genlmsg_end(rskb, reply_payload);
> +
> +	return genlmsg_reply(rskb, info);
> +
> +err_free_msg:
> +	nlmsg_free(rskb);
> +err_unlock_rtnl:
> +	rtnl_unlock();
> +	ethnl_parse_header_dev_put(&req_info.base);
> +	return ret;
> +}
> +
> +struct ethnl_phy_dump_ctx {
> +	struct phy_req_info	*phy_req_info;
> +	unsigned long ifindex;
> +	unsigned long phy_index;
> +};
> +
> +int ethnl_phy_start(struct netlink_callback *cb)
> +{
> +	const struct genl_dumpit_info *info = genl_dumpit_info(cb);

You can save some chars by using genl_info_dump() here

> +	struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx;
> +	struct nlattr **tb = info->info.attrs;

then you can ditch this

> +	int ret;
> +
> +	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
> +
> +	ctx->phy_req_info = kzalloc(sizeof(*ctx->phy_req_info), GFP_KERNEL);
> +	if (!ctx->phy_req_info)
> +		return -ENOMEM;
> +
> +	ret = ethnl_parse_header_dev_get(&ctx->phy_req_info->base,
> +					 tb[ETHTOOL_A_PHY_HEADER],

and:

					 info->attrs[ETHTOOL_A_PHY_HEADER],

> +					 sock_net(cb->skb->sk), cb->extack,
> +					 false);

leaking ctx->phy_req_info on error?

> +	ctx->ifindex = 0;
> +	ctx->phy_index = 0;
> +	return ret;
> +}

> +static int ethnl_phy_dump_one_dev(struct sk_buff *skb, struct net_device *dev,
> +				  struct netlink_callback *cb)
> +{
> +	struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx;
> +	struct phy_req_info *pri = ctx->phy_req_info;
> +	struct phy_device_node *pdn;
> +	int ret = 0;
> +	void *ehdr;
> +
> +	pri->base.dev = dev;
> +
> +	xa_for_each_start(&dev->link_topo->phys, ctx->phy_index, pdn, ctx->phy_index) {
> +		ehdr = ethnl_dump_put(skb, cb,
> +				      ETHTOOL_MSG_PHY_GET_REPLY);

this one OTOH fits on a line :)

> +		if (!ehdr) {
> +			ret = -EMSGSIZE;
> +			break;
> +		}
> +
> +		ret = ethnl_fill_reply_header(skb, dev,
> +					      ETHTOOL_A_PHY_HEADER);

ditto

> +		if (ret < 0) {
> +			genlmsg_cancel(skb, ehdr);
> +			break;
> +		}
> +
> +		memcpy(&pri->pdn, pdn, sizeof(*pdn));
> +		ret = ethnl_phy_fill_reply(&pri->base, skb);

On a DO fill() shouldn't fail, because we size the skb, but if we pack
many entries into the skb on a DUMP it can. So:

		if (ret < 0) {
			genlmsg_cancel(skb, ehdr);
			break;
		}

no?

> +		genlmsg_end(skb, ehdr);
> +	}
> +
> +	return ret;
> +}
> +
> +int ethnl_phy_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
> +{
> +	struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx;
> +	struct net *net = sock_net(skb->sk);
> +	struct net_device *dev;
> +	int ret = 0;
> +
> +	rtnl_lock();
> +
> +	if (ctx->phy_req_info->base.dev) {
> +		ret = ethnl_phy_dump_one_dev(skb, ctx->phy_req_info->base.dev, cb);
> +	} else {
> +		for_each_netdev_dump(net, dev, ctx->ifindex) {
> +			ret = ethnl_phy_dump_one_dev(skb, dev, cb);
> +			if (ret)
> +				break;
> +
> +			ctx->phy_index = 0;
> +		}
> +	}
> +	rtnl_unlock();
> +
> +	if (ret == -EMSGSIZE && skb->len)
> +		return skb->len;

you can remove this if thanks to (very recent) commit b5a899154aa9

> +	return ret;
> +}

Very sorry for the late review, BTW :(

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

WARNING: multiple messages have this Message-ID (diff)
From: Jakub Kicinski <kuba@kernel.org>
To: Maxime Chevallier <maxime.chevallier@bootlin.com>
Cc: davem@davemloft.net, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, thomas.petazzoni@bootlin.com,
	"Andrew Lunn" <andrew@lunn.ch>,
	"Eric Dumazet" <edumazet@google.com>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Russell King" <linux@armlinux.org.uk>,
	linux-arm-kernel@lists.infradead.org,
	"Christophe Leroy" <christophe.leroy@csgroup.eu>,
	"Herve Codina" <herve.codina@bootlin.com>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Heiner Kallweit" <hkallweit1@gmail.com>,
	"Vladimir Oltean" <vladimir.oltean@nxp.com>,
	"Köry Maincent" <kory.maincent@bootlin.com>,
	"Jesse Brandeburg" <jesse.brandeburg@intel.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Marek Behún" <kabel@kernel.org>,
	"Piergiorgio Beruto" <piergiorgio.beruto@gmail.com>,
	"Oleksij Rempel" <o.rempel@pengutronix.de>,
	"Nicolò Veronese" <nicveronese@gmail.com>,
	"Simon Horman" <horms@kernel.org>,
	mwojtas@chromium.org
Subject: Re: [PATCH net-next v10 07/13] net: ethtool: Introduce a command to list PHYs on an interface
Date: Fri, 8 Mar 2024 21:18:42 -0800	[thread overview]
Message-ID: <20240308211842.6d64b5bc@kernel.org> (raw)
In-Reply-To: <20240304151011.1610175-8-maxime.chevallier@bootlin.com>

On Mon,  4 Mar 2024 16:10:03 +0100 Maxime Chevallier wrote:
> +PHY_GET
> +=======
> +
> +Retrieve information about a given Ethernet PHY sitting on the link. As there
> +can be more than one PHY, the DUMP operation can be used to list the PHYs
> +present on a given interface, by passing an interface index or name in
> +the dump request

Could be worth re-stating the default behavior of the DO request?
That the DO will return the dev->phydev by default and if PHY_INDEX
is specified in the header, the particular PHY with that index?

> +	if (phydev->drv && nla_put_string(skb, ETHTOOL_A_PHY_DRVNAME, phydev->drv->name))

could you break the lines at 80 where it doesn't hurt readability?
This way:

	if (phydev->drv && 
	    nla_put_string(skb, ETHTOOL_A_PHY_DRVNAME, phydev->drv->name))

> +		return -EMSGSIZE;
> +
> +	if (ptype == PHY_UPSTREAM_PHY) {
> +		struct phy_device *upstream = pdn->upstream.phydev;
> +		const char *sfp_upstream_name;
> +
> +		/* Parent index */
> +		if (nla_put_u32(skb, ETHTOOL_A_PHY_UPSTREAM_INDEX, upstream->phyindex))
> +			return -EMSGSIZE;
> +
> +		if (pdn->parent_sfp_bus) {
> +			sfp_upstream_name = sfp_get_name(pdn->parent_sfp_bus);
> +			if (sfp_upstream_name && nla_put_string(skb,
> +								ETHTOOL_A_PHY_UPSTREAM_SFP_NAME,
> +								sfp_upstream_name))

ditto

> +				return -EMSGSIZE;
> +		}
> +	}

> +int ethnl_phy_doit(struct sk_buff *skb, struct genl_info *info)
> +{
> +	struct phy_req_info req_info = {};
> +	struct nlattr **tb = info->attrs;
> +	struct sk_buff *rskb;
> +	void *reply_payload;
> +	int reply_len;
> +	int ret;
> +
> +	ret = ethnl_parse_header_dev_get(&req_info.base,
> +					 tb[ETHTOOL_A_PHY_HEADER],
> +					 genl_info_net(info), info->extack,
> +					 true);
> +	if (ret < 0)
> +		return ret;
> +
> +	rtnl_lock();
> +
> +	ret = ethnl_phy_parse_request(&req_info.base, tb);
> +	if (ret < 0)
> +		goto err_unlock_rtnl;
> +
> +	/* No PHY, return early */
> +	if (!req_info.pdn.phy)
> +		goto err_unlock_rtnl;
> +
> +	ret = ethnl_phy_reply_size(&req_info.base, info->extack);
> +	if (ret < 0)
> +		goto err_unlock_rtnl;
> +	reply_len = ret + ethnl_reply_header_size();
> +
> +	rskb = ethnl_reply_init(reply_len, req_info.base.dev,
> +				ETHTOOL_MSG_PHY_GET_REPLY,
> +				ETHTOOL_A_PHY_HEADER,
> +				info, &reply_payload);
> +	if (!rskb) {
> +		ret = -ENOMEM;
> +		goto err_unlock_rtnl;
> +	}
> +
> +	ret = ethnl_phy_fill_reply(&req_info.base, rskb);
> +	if (ret)
> +		goto err_free_msg;
> +
> +	rtnl_unlock();
> +	ethnl_parse_header_dev_put(&req_info.base);
> +	genlmsg_end(rskb, reply_payload);
> +
> +	return genlmsg_reply(rskb, info);
> +
> +err_free_msg:
> +	nlmsg_free(rskb);
> +err_unlock_rtnl:
> +	rtnl_unlock();
> +	ethnl_parse_header_dev_put(&req_info.base);
> +	return ret;
> +}
> +
> +struct ethnl_phy_dump_ctx {
> +	struct phy_req_info	*phy_req_info;
> +	unsigned long ifindex;
> +	unsigned long phy_index;
> +};
> +
> +int ethnl_phy_start(struct netlink_callback *cb)
> +{
> +	const struct genl_dumpit_info *info = genl_dumpit_info(cb);

You can save some chars by using genl_info_dump() here

> +	struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx;
> +	struct nlattr **tb = info->info.attrs;

then you can ditch this

> +	int ret;
> +
> +	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
> +
> +	ctx->phy_req_info = kzalloc(sizeof(*ctx->phy_req_info), GFP_KERNEL);
> +	if (!ctx->phy_req_info)
> +		return -ENOMEM;
> +
> +	ret = ethnl_parse_header_dev_get(&ctx->phy_req_info->base,
> +					 tb[ETHTOOL_A_PHY_HEADER],

and:

					 info->attrs[ETHTOOL_A_PHY_HEADER],

> +					 sock_net(cb->skb->sk), cb->extack,
> +					 false);

leaking ctx->phy_req_info on error?

> +	ctx->ifindex = 0;
> +	ctx->phy_index = 0;
> +	return ret;
> +}

> +static int ethnl_phy_dump_one_dev(struct sk_buff *skb, struct net_device *dev,
> +				  struct netlink_callback *cb)
> +{
> +	struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx;
> +	struct phy_req_info *pri = ctx->phy_req_info;
> +	struct phy_device_node *pdn;
> +	int ret = 0;
> +	void *ehdr;
> +
> +	pri->base.dev = dev;
> +
> +	xa_for_each_start(&dev->link_topo->phys, ctx->phy_index, pdn, ctx->phy_index) {
> +		ehdr = ethnl_dump_put(skb, cb,
> +				      ETHTOOL_MSG_PHY_GET_REPLY);

this one OTOH fits on a line :)

> +		if (!ehdr) {
> +			ret = -EMSGSIZE;
> +			break;
> +		}
> +
> +		ret = ethnl_fill_reply_header(skb, dev,
> +					      ETHTOOL_A_PHY_HEADER);

ditto

> +		if (ret < 0) {
> +			genlmsg_cancel(skb, ehdr);
> +			break;
> +		}
> +
> +		memcpy(&pri->pdn, pdn, sizeof(*pdn));
> +		ret = ethnl_phy_fill_reply(&pri->base, skb);

On a DO fill() shouldn't fail, because we size the skb, but if we pack
many entries into the skb on a DUMP it can. So:

		if (ret < 0) {
			genlmsg_cancel(skb, ehdr);
			break;
		}

no?

> +		genlmsg_end(skb, ehdr);
> +	}
> +
> +	return ret;
> +}
> +
> +int ethnl_phy_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
> +{
> +	struct ethnl_phy_dump_ctx *ctx = (void *)cb->ctx;
> +	struct net *net = sock_net(skb->sk);
> +	struct net_device *dev;
> +	int ret = 0;
> +
> +	rtnl_lock();
> +
> +	if (ctx->phy_req_info->base.dev) {
> +		ret = ethnl_phy_dump_one_dev(skb, ctx->phy_req_info->base.dev, cb);
> +	} else {
> +		for_each_netdev_dump(net, dev, ctx->ifindex) {
> +			ret = ethnl_phy_dump_one_dev(skb, dev, cb);
> +			if (ret)
> +				break;
> +
> +			ctx->phy_index = 0;
> +		}
> +	}
> +	rtnl_unlock();
> +
> +	if (ret == -EMSGSIZE && skb->len)
> +		return skb->len;

you can remove this if thanks to (very recent) commit b5a899154aa9

> +	return ret;
> +}

Very sorry for the late review, BTW :(

  reply	other threads:[~2024-03-09  5:19 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-04 15:09 [PATCH net-next v10 00/13] Introduce PHY listing and link_topology tracking Maxime Chevallier
2024-03-04 15:09 ` Maxime Chevallier
2024-03-04 15:09 ` [PATCH net-next v10 01/13] net: phy: Introduce ethernet link topology representation Maxime Chevallier
2024-03-04 15:09   ` Maxime Chevallier
2024-03-04 15:09 ` [PATCH net-next v10 02/13] net: sfp: pass the phy_device when disconnecting an sfp module's PHY Maxime Chevallier
2024-03-04 15:09   ` Maxime Chevallier
2024-03-04 15:09 ` [PATCH net-next v10 03/13] net: phy: add helpers to handle sfp phy connect/disconnect Maxime Chevallier
2024-03-04 15:09   ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 04/13] net: sfp: Add helper to return the SFP bus name Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 05/13] net: ethtool: Allow passing a phy index for some commands Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-09  5:01   ` Jakub Kicinski
2024-03-09  5:01     ` Jakub Kicinski
2024-03-04 15:10 ` [PATCH net-next v10 06/13] netlink: specs: add phy-index as a header parameter Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 07/13] net: ethtool: Introduce a command to list PHYs on an interface Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-09  5:18   ` Jakub Kicinski [this message]
2024-03-09  5:18     ` Jakub Kicinski
2024-04-04  8:38     ` Maxime Chevallier
2024-04-04  8:38       ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 08/13] netlink: specs: add ethnl PHY_GET command set Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 09/13] net: ethtool: plca: Target the command to the requested PHY Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 10/13] net: ethtool: pse-pd: " Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 11/13] net: ethtool: cable-test: " Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 12/13] net: ethtool: strset: Allow querying phy stats by index Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-04 15:10 ` [PATCH net-next v10 13/13] Documentation: networking: document phy_link_topology Maxime Chevallier
2024-03-04 15:10   ` Maxime Chevallier
2024-03-09  5:27   ` Jakub Kicinski
2024-03-09  5:27     ` Jakub Kicinski

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=20240308211842.6d64b5bc@kernel.org \
    --to=kuba@kernel.org \
    --cc=andrew@lunn.ch \
    --cc=christophe.leroy@csgroup.eu \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=f.fainelli@gmail.com \
    --cc=herve.codina@bootlin.com \
    --cc=hkallweit1@gmail.com \
    --cc=horms@kernel.org \
    --cc=jesse.brandeburg@intel.com \
    --cc=kabel@kernel.org \
    --cc=kory.maincent@bootlin.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=maxime.chevallier@bootlin.com \
    --cc=mwojtas@chromium.org \
    --cc=netdev@vger.kernel.org \
    --cc=nicveronese@gmail.com \
    --cc=o.rempel@pengutronix.de \
    --cc=pabeni@redhat.com \
    --cc=piergiorgio.beruto@gmail.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=vladimir.oltean@nxp.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.