Skip to content

Commit c15e065

Browse files
minimaxwelldavem330
authored andcommitted
net: ethtool: Allow passing a phy index for some commands
Some netlink commands are target towards ethernet PHYs, to control some of their features. As there's several such commands, add the ability to pass a PHY index in the ethnl request, which will populate the generic ethnl_req_info with the passed phy_index. Add a helper that netlink command handlers need to use to grab the targeted PHY from the req_info. This helper needs to hold rtnl_lock() while interacting with the PHY, as it may be removed at any point. Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com> Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu> Tested-by: Christophe Leroy <christophe.leroy@csgroup.eu> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0a2f7de commit c15e065

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

Documentation/networking/ethtool-netlink.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Structure of this header is
5757
``ETHTOOL_A_HEADER_DEV_INDEX`` u32 device ifindex
5858
``ETHTOOL_A_HEADER_DEV_NAME`` string device name
5959
``ETHTOOL_A_HEADER_FLAGS`` u32 flags common for all requests
60+
``ETHTOOL_A_HEADER_PHY_INDEX`` u32 phy device index
6061
============================== ====== =============================
6162

6263
``ETHTOOL_A_HEADER_DEV_INDEX`` and ``ETHTOOL_A_HEADER_DEV_NAME`` identify the
@@ -81,6 +82,12 @@ the behaviour is backward compatible, i.e. requests from old clients not aware
8182
of the flag should be interpreted the way the client expects. A client must
8283
not set flags it does not understand.
8384

85+
``ETHTOOL_A_HEADER_PHY_INDEX`` identifies the Ethernet PHY the message relates to.
86+
As there are numerous commands that are related to PHY configuration, and because
87+
there may be more than one PHY on the link, the PHY index can be passed in the
88+
request for the commands that needs it. It is, however, not mandatory, and if it
89+
is not passed for commands that target a PHY, the net_device.phydev pointer
90+
is used.
8491

8592
Bit sets
8693
========

include/uapi/linux/ethtool_netlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ enum {
134134
ETHTOOL_A_HEADER_DEV_INDEX, /* u32 */
135135
ETHTOOL_A_HEADER_DEV_NAME, /* string */
136136
ETHTOOL_A_HEADER_FLAGS, /* u32 - ETHTOOL_FLAG_* */
137+
ETHTOOL_A_HEADER_PHY_INDEX, /* u32 */
137138

138139
/* add new constants above here */
139140
__ETHTOOL_A_HEADER_CNT,

net/ethtool/netlink.c

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <net/sock.h>
44
#include <linux/ethtool_netlink.h>
5+
#include <linux/phy_link_topology.h>
56
#include <linux/pm_runtime.h>
67
#include "netlink.h"
78
#include "module_fw.h"
@@ -31,6 +32,24 @@ const struct nla_policy ethnl_header_policy_stats[] = {
3132
ETHTOOL_FLAGS_STATS),
3233
};
3334

35+
const struct nla_policy ethnl_header_policy_phy[] = {
36+
[ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 },
37+
[ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING,
38+
.len = ALTIFNAMSIZ - 1 },
39+
[ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32,
40+
ETHTOOL_FLAGS_BASIC),
41+
[ETHTOOL_A_HEADER_PHY_INDEX] = NLA_POLICY_MIN(NLA_U32, 1),
42+
};
43+
44+
const struct nla_policy ethnl_header_policy_phy_stats[] = {
45+
[ETHTOOL_A_HEADER_DEV_INDEX] = { .type = NLA_U32 },
46+
[ETHTOOL_A_HEADER_DEV_NAME] = { .type = NLA_NUL_STRING,
47+
.len = ALTIFNAMSIZ - 1 },
48+
[ETHTOOL_A_HEADER_FLAGS] = NLA_POLICY_MASK(NLA_U32,
49+
ETHTOOL_FLAGS_STATS),
50+
[ETHTOOL_A_HEADER_PHY_INDEX] = NLA_POLICY_MIN(NLA_U32, 1),
51+
};
52+
3453
int ethnl_sock_priv_set(struct sk_buff *skb, struct net_device *dev, u32 portid,
3554
enum ethnl_sock_type type)
3655
{
@@ -119,7 +138,7 @@ int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
119138
const struct nlattr *header, struct net *net,
120139
struct netlink_ext_ack *extack, bool require_dev)
121140
{
122-
struct nlattr *tb[ARRAY_SIZE(ethnl_header_policy)];
141+
struct nlattr *tb[ARRAY_SIZE(ethnl_header_policy_phy)];
123142
const struct nlattr *devname_attr;
124143
struct net_device *dev = NULL;
125144
u32 flags = 0;
@@ -134,7 +153,7 @@ int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
134153
/* No validation here, command policy should have a nested policy set
135154
* for the header, therefore validation should have already been done.
136155
*/
137-
ret = nla_parse_nested(tb, ARRAY_SIZE(ethnl_header_policy) - 1, header,
156+
ret = nla_parse_nested(tb, ARRAY_SIZE(ethnl_header_policy_phy) - 1, header,
138157
NULL, extack);
139158
if (ret < 0)
140159
return ret;
@@ -175,11 +194,45 @@ int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
175194
return -EINVAL;
176195
}
177196

197+
if (tb[ETHTOOL_A_HEADER_PHY_INDEX]) {
198+
if (dev) {
199+
req_info->phy_index = nla_get_u32(tb[ETHTOOL_A_HEADER_PHY_INDEX]);
200+
} else {
201+
NL_SET_ERR_MSG_ATTR(extack, header,
202+
"phy_index set without a netdev");
203+
return -EINVAL;
204+
}
205+
}
206+
178207
req_info->dev = dev;
179208
req_info->flags = flags;
180209
return 0;
181210
}
182211

212+
struct phy_device *ethnl_req_get_phydev(const struct ethnl_req_info *req_info,
213+
const struct nlattr *header,
214+
struct netlink_ext_ack *extack)
215+
{
216+
struct phy_device *phydev;
217+
218+
ASSERT_RTNL();
219+
220+
if (!req_info->dev)
221+
return NULL;
222+
223+
if (!req_info->phy_index)
224+
return req_info->dev->phydev;
225+
226+
phydev = phy_link_topo_get_phy(req_info->dev, req_info->phy_index);
227+
if (!phydev) {
228+
NL_SET_ERR_MSG_ATTR(extack, header,
229+
"no phy matching phyindex");
230+
return ERR_PTR(-ENODEV);
231+
}
232+
233+
return phydev;
234+
}
235+
183236
/**
184237
* ethnl_fill_reply_header() - Put common header into a reply message
185238
* @skb: skb with the message

net/ethtool/netlink.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ static inline unsigned int ethnl_reply_header_size(void)
251251
* @dev: network device the request is for (may be null)
252252
* @dev_tracker: refcount tracker for @dev reference
253253
* @flags: request flags common for all request types
254+
* @phy_index: phy_device index connected to @dev this request is for. Can be
255+
* 0 if the request doesn't target a phy, or if the @dev's attached
256+
* phy is targeted.
254257
*
255258
* This is a common base for request specific structures holding data from
256259
* parsed userspace request. These always embed struct ethnl_req_info at
@@ -260,13 +263,35 @@ struct ethnl_req_info {
260263
struct net_device *dev;
261264
netdevice_tracker dev_tracker;
262265
u32 flags;
266+
u32 phy_index;
263267
};
264268

265269
static inline void ethnl_parse_header_dev_put(struct ethnl_req_info *req_info)
266270
{
267271
netdev_put(req_info->dev, &req_info->dev_tracker);
268272
}
269273

274+
/**
275+
* ethnl_req_get_phydev() - Gets the phy_device targeted by this request,
276+
* if any. Must be called under rntl_lock().
277+
* @req_info: The ethnl request to get the phy from.
278+
* @header: The netlink header, used for error reporting.
279+
* @extack: The netlink extended ACK, for error reporting.
280+
*
281+
* The caller must hold RTNL, until it's done interacting with the returned
282+
* phy_device.
283+
*
284+
* Return: A phy_device pointer corresponding either to the passed phy_index
285+
* if one is provided. If not, the phy_device attached to the
286+
* net_device targeted by this request is returned. If there's no
287+
* targeted net_device, or no phy_device is attached, NULL is
288+
* returned. If the provided phy_index is invalid, an error pointer
289+
* is returned.
290+
*/
291+
struct phy_device *ethnl_req_get_phydev(const struct ethnl_req_info *req_info,
292+
const struct nlattr *header,
293+
struct netlink_ext_ack *extack);
294+
270295
/**
271296
* struct ethnl_reply_data - base type of reply data for GET requests
272297
* @dev: device for current reply message; in single shot requests it is
@@ -409,9 +434,12 @@ extern const struct ethnl_request_ops ethnl_rss_request_ops;
409434
extern const struct ethnl_request_ops ethnl_plca_cfg_request_ops;
410435
extern const struct ethnl_request_ops ethnl_plca_status_request_ops;
411436
extern const struct ethnl_request_ops ethnl_mm_request_ops;
437+
extern const struct ethnl_request_ops ethnl_phy_request_ops;
412438

413439
extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS + 1];
414440
extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_FLAGS + 1];
441+
extern const struct nla_policy ethnl_header_policy_phy[ETHTOOL_A_HEADER_PHY_INDEX + 1];
442+
extern const struct nla_policy ethnl_header_policy_phy_stats[ETHTOOL_A_HEADER_PHY_INDEX + 1];
415443
extern const struct nla_policy ethnl_strset_get_policy[ETHTOOL_A_STRSET_COUNTS_ONLY + 1];
416444
extern const struct nla_policy ethnl_linkinfo_get_policy[ETHTOOL_A_LINKINFO_HEADER + 1];
417445
extern const struct nla_policy ethnl_linkinfo_set_policy[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL + 1];

0 commit comments

Comments
 (0)