Skip to content

Commit a53f3d4

Browse files
mkubecekdavem330
authored andcommitted
ethtool: set link settings with LINKINFO_SET request
Implement LINKINFO_SET netlink request to set link settings queried by LINKINFO_GET message. Only physical port, phy MDIO address and MDI(-X) control can be set, attempt to modify MDI(-X) status and transceiver is rejected. Signed-off-by: Michal Kubecek <mkubecek@suse.cz> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 459e0b8 commit a53f3d4

5 files changed

Lines changed: 101 additions & 2 deletions

File tree

‎Documentation/networking/ethtool-netlink.rst‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Userspace to kernel:
181181
===================================== ================================
182182
``ETHTOOL_MSG_STRSET_GET`` get string set
183183
``ETHTOOL_MSG_LINKINFO_GET`` get link settings
184+
``ETHTOOL_MSG_LINKINFO_SET`` set link settings
184185
===================================== ================================
185186

186187
Kernel to userspace:
@@ -311,6 +312,25 @@ corresponding ioctl structures.
311312
devices supporting the request).
312313

313314

315+
LINKINFO_SET
316+
============
317+
318+
``LINKINFO_SET`` request allows setting some of the attributes reported by
319+
``LINKINFO_GET``.
320+
321+
Request contents:
322+
323+
==================================== ====== ==========================
324+
``ETHTOOL_A_LINKINFO_HEADER`` nested request header
325+
``ETHTOOL_A_LINKINFO_PORT`` u8 physical port
326+
``ETHTOOL_A_LINKINFO_PHYADDR`` u8 phy MDIO address
327+
``ETHTOOL_A_LINKINFO_TP_MDIX_CTRL`` u8 MDI(-X) control
328+
==================================== ====== ==========================
329+
330+
MDI(-X) status and transceiver cannot be set, request with the corresponding
331+
attributes is rejected.
332+
333+
314334
Request translation
315335
===================
316336

@@ -322,7 +342,7 @@ have their netlink replacement yet.
322342
ioctl command netlink command
323343
=================================== =====================================
324344
``ETHTOOL_GSET`` ``ETHTOOL_MSG_LINKINFO_GET``
325-
``ETHTOOL_SSET`` n/a
345+
``ETHTOOL_SSET`` ``ETHTOOL_MSG_LINKINFO_SET``
326346
``ETHTOOL_GDRVINFO`` n/a
327347
``ETHTOOL_GREGS`` n/a
328348
``ETHTOOL_GWOL`` n/a
@@ -396,7 +416,7 @@ have their netlink replacement yet.
396416
``ETHTOOL_GPHYSTATS`` n/a
397417
``ETHTOOL_PERQUEUE`` n/a
398418
``ETHTOOL_GLINKSETTINGS`` ``ETHTOOL_MSG_LINKINFO_GET``
399-
``ETHTOOL_SLINKSETTINGS`` n/a
419+
``ETHTOOL_SLINKSETTINGS`` ``ETHTOOL_MSG_LINKINFO_SET``
400420
``ETHTOOL_PHY_GTUNABLE`` n/a
401421
``ETHTOOL_PHY_STUNABLE`` n/a
402422
``ETHTOOL_GFECPARAM`` n/a

‎include/uapi/linux/ethtool_netlink.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum {
1616
ETHTOOL_MSG_USER_NONE,
1717
ETHTOOL_MSG_STRSET_GET,
1818
ETHTOOL_MSG_LINKINFO_GET,
19+
ETHTOOL_MSG_LINKINFO_SET,
1920

2021
/* add new constants above here */
2122
__ETHTOOL_MSG_USER_CNT,

‎net/ethtool/linkinfo.c‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,74 @@ const struct ethnl_request_ops ethnl_linkinfo_request_ops = {
9292
.reply_size = linkinfo_reply_size,
9393
.fill_reply = linkinfo_fill_reply,
9494
};
95+
96+
/* LINKINFO_SET */
97+
98+
static const struct nla_policy
99+
linkinfo_set_policy[ETHTOOL_A_LINKINFO_MAX + 1] = {
100+
[ETHTOOL_A_LINKINFO_UNSPEC] = { .type = NLA_REJECT },
101+
[ETHTOOL_A_LINKINFO_HEADER] = { .type = NLA_NESTED },
102+
[ETHTOOL_A_LINKINFO_PORT] = { .type = NLA_U8 },
103+
[ETHTOOL_A_LINKINFO_PHYADDR] = { .type = NLA_U8 },
104+
[ETHTOOL_A_LINKINFO_TP_MDIX] = { .type = NLA_REJECT },
105+
[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL] = { .type = NLA_U8 },
106+
[ETHTOOL_A_LINKINFO_TRANSCEIVER] = { .type = NLA_REJECT },
107+
};
108+
109+
int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info)
110+
{
111+
struct nlattr *tb[ETHTOOL_A_LINKINFO_MAX + 1];
112+
struct ethtool_link_ksettings ksettings = {};
113+
struct ethtool_link_settings *lsettings;
114+
struct ethnl_req_info req_info = {};
115+
struct net_device *dev;
116+
bool mod = false;
117+
int ret;
118+
119+
ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb,
120+
ETHTOOL_A_LINKINFO_MAX, linkinfo_set_policy,
121+
info->extack);
122+
if (ret < 0)
123+
return ret;
124+
ret = ethnl_parse_header(&req_info, tb[ETHTOOL_A_LINKINFO_HEADER],
125+
genl_info_net(info), info->extack, true);
126+
if (ret < 0)
127+
return ret;
128+
dev = req_info.dev;
129+
if (!dev->ethtool_ops->get_link_ksettings ||
130+
!dev->ethtool_ops->set_link_ksettings)
131+
return -EOPNOTSUPP;
132+
133+
rtnl_lock();
134+
ret = ethnl_ops_begin(dev);
135+
if (ret < 0)
136+
goto out_rtnl;
137+
138+
ret = __ethtool_get_link_ksettings(dev, &ksettings);
139+
if (ret < 0) {
140+
if (info)
141+
GENL_SET_ERR_MSG(info, "failed to retrieve link settings");
142+
goto out_ops;
143+
}
144+
lsettings = &ksettings.base;
145+
146+
ethnl_update_u8(&lsettings->port, tb[ETHTOOL_A_LINKINFO_PORT], &mod);
147+
ethnl_update_u8(&lsettings->phy_address, tb[ETHTOOL_A_LINKINFO_PHYADDR],
148+
&mod);
149+
ethnl_update_u8(&lsettings->eth_tp_mdix_ctrl,
150+
tb[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL], &mod);
151+
ret = 0;
152+
if (!mod)
153+
goto out_ops;
154+
155+
ret = dev->ethtool_ops->set_link_ksettings(dev, &ksettings);
156+
if (ret < 0)
157+
GENL_SET_ERR_MSG(info, "link settings update failed");
158+
159+
out_ops:
160+
ethnl_ops_complete(dev);
161+
out_rtnl:
162+
rtnl_unlock();
163+
dev_put(dev);
164+
return ret;
165+
}

‎net/ethtool/netlink.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,11 @@ static const struct genl_ops ethtool_genl_ops[] = {
534534
.dumpit = ethnl_default_dumpit,
535535
.done = ethnl_default_done,
536536
},
537+
{
538+
.cmd = ETHTOOL_MSG_LINKINFO_SET,
539+
.flags = GENL_UNS_ADMIN_PERM,
540+
.doit = ethnl_set_linkinfo,
541+
},
537542
};
538543

539544
static const struct genl_multicast_group ethtool_nl_mcgrps[] = {

‎net/ethtool/netlink.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,6 @@ struct ethnl_request_ops {
331331
extern const struct ethnl_request_ops ethnl_strset_request_ops;
332332
extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
333333

334+
int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
335+
334336
#endif /* _NET_ETHTOOL_NETLINK_H */

0 commit comments

Comments
 (0)