Skip to content

Commit f625aa9

Browse files
mkubecekdavem330
authored andcommitted
ethtool: provide link mode information with LINKMODES_GET request
Implement LINKMODES_GET netlink request to get link modes related information provided by ETHTOOL_GLINKSETTINGS and ETHTOOL_GSET ioctl commands. This request provides supported, advertised and peer advertised link modes, autonegotiation flag, speed and duplex. LINKMODES_GET request can be used with NLM_F_DUMP (without device identification) to request the information for all devices in current network namespace providing the data. 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 7328673 commit f625aa9

7 files changed

Lines changed: 207 additions & 1 deletion

File tree

‎Documentation/networking/ethtool-netlink.rst‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Userspace to kernel:
182182
``ETHTOOL_MSG_STRSET_GET`` get string set
183183
``ETHTOOL_MSG_LINKINFO_GET`` get link settings
184184
``ETHTOOL_MSG_LINKINFO_SET`` set link settings
185+
``ETHTOOL_MSG_LINKMODES_GET`` get link modes info
185186
===================================== ================================
186187

187188
Kernel to userspace:
@@ -190,6 +191,7 @@ Kernel to userspace:
190191
``ETHTOOL_MSG_STRSET_GET_REPLY`` string set contents
191192
``ETHTOOL_MSG_LINKINFO_GET_REPLY`` link settings
192193
``ETHTOOL_MSG_LINKINFO_NTF`` link settings notification
194+
``ETHTOOL_MSG_LINKMODES_GET_REPLY`` link modes info
193195
===================================== ================================
194196

195197
``GET`` requests are sent by userspace applications to retrieve device
@@ -332,6 +334,38 @@ MDI(-X) status and transceiver cannot be set, request with the corresponding
332334
attributes is rejected.
333335

334336

337+
LINKMODES_GET
338+
=============
339+
340+
Requests link modes (supported, advertised and peer advertised) and related
341+
information (autonegotiation status, link speed and duplex) as provided by
342+
``ETHTOOL_GLINKSETTINGS``. The request does not use any attributes.
343+
344+
Request contents:
345+
346+
==================================== ====== ==========================
347+
``ETHTOOL_A_LINKMODES_HEADER`` nested request header
348+
==================================== ====== ==========================
349+
350+
Kernel response contents:
351+
352+
==================================== ====== ==========================
353+
``ETHTOOL_A_LINKMODES_HEADER`` nested reply header
354+
``ETHTOOL_A_LINKMODES_AUTONEG`` u8 autonegotiation status
355+
``ETHTOOL_A_LINKMODES_OURS`` bitset advertised link modes
356+
``ETHTOOL_A_LINKMODES_PEER`` bitset partner link modes
357+
``ETHTOOL_A_LINKMODES_SPEED`` u32 link speed (Mb/s)
358+
``ETHTOOL_A_LINKMODES_DUPLEX`` u8 duplex mode
359+
==================================== ====== ==========================
360+
361+
For ``ETHTOOL_A_LINKMODES_OURS``, value represents advertised modes and mask
362+
represents supported modes. ``ETHTOOL_A_LINKMODES_PEER`` in the reply is a bit
363+
list.
364+
365+
``LINKMODES_GET`` allows dump requests (kernel returns reply messages for all
366+
devices supporting the request).
367+
368+
335369
Request translation
336370
===================
337371

@@ -343,6 +377,7 @@ have their netlink replacement yet.
343377
ioctl command netlink command
344378
=================================== =====================================
345379
``ETHTOOL_GSET`` ``ETHTOOL_MSG_LINKINFO_GET``
380+
``ETHTOOL_MSG_LINKMODES_GET``
346381
``ETHTOOL_SSET`` ``ETHTOOL_MSG_LINKINFO_SET``
347382
``ETHTOOL_GDRVINFO`` n/a
348383
``ETHTOOL_GREGS`` n/a
@@ -417,6 +452,7 @@ have their netlink replacement yet.
417452
``ETHTOOL_GPHYSTATS`` n/a
418453
``ETHTOOL_PERQUEUE`` n/a
419454
``ETHTOOL_GLINKSETTINGS`` ``ETHTOOL_MSG_LINKINFO_GET``
455+
``ETHTOOL_MSG_LINKMODES_GET``
420456
``ETHTOOL_SLINKSETTINGS`` ``ETHTOOL_MSG_LINKINFO_SET``
421457
``ETHTOOL_PHY_GTUNABLE`` n/a
422458
``ETHTOOL_PHY_STUNABLE`` n/a

‎include/linux/ethtool_netlink.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <linux/ethtool.h>
88
#include <linux/netdevice.h>
99

10+
#define __ETHTOOL_LINK_MODE_MASK_NWORDS \
11+
DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
12+
1013
enum ethtool_multicast_groups {
1114
ETHNL_MCGRP_MONITOR,
1215
};

‎include/uapi/linux/ethtool_netlink.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum {
1717
ETHTOOL_MSG_STRSET_GET,
1818
ETHTOOL_MSG_LINKINFO_GET,
1919
ETHTOOL_MSG_LINKINFO_SET,
20+
ETHTOOL_MSG_LINKMODES_GET,
2021

2122
/* add new constants above here */
2223
__ETHTOOL_MSG_USER_CNT,
@@ -29,6 +30,7 @@ enum {
2930
ETHTOOL_MSG_STRSET_GET_REPLY,
3031
ETHTOOL_MSG_LINKINFO_GET_REPLY,
3132
ETHTOOL_MSG_LINKINFO_NTF,
33+
ETHTOOL_MSG_LINKMODES_GET_REPLY,
3234

3335
/* add new constants above here */
3436
__ETHTOOL_MSG_KERNEL_CNT,
@@ -161,6 +163,22 @@ enum {
161163
ETHTOOL_A_LINKINFO_MAX = __ETHTOOL_A_LINKINFO_CNT - 1
162164
};
163165

166+
/* LINKMODES */
167+
168+
enum {
169+
ETHTOOL_A_LINKMODES_UNSPEC,
170+
ETHTOOL_A_LINKMODES_HEADER, /* nest - _A_HEADER_* */
171+
ETHTOOL_A_LINKMODES_AUTONEG, /* u8 */
172+
ETHTOOL_A_LINKMODES_OURS, /* bitset */
173+
ETHTOOL_A_LINKMODES_PEER, /* bitset */
174+
ETHTOOL_A_LINKMODES_SPEED, /* u32 */
175+
ETHTOOL_A_LINKMODES_DUPLEX, /* u8 */
176+
177+
/* add new constants above here */
178+
__ETHTOOL_A_LINKMODES_CNT,
179+
ETHTOOL_A_LINKMODES_MAX = __ETHTOOL_A_LINKMODES_CNT - 1
180+
};
181+
164182
/* generic netlink info */
165183
#define ETHTOOL_GENL_NAME "ethtool"
166184
#define ETHTOOL_GENL_VERSION 1

‎net/ethtool/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ obj-y += ioctl.o common.o
44

55
obj-$(CONFIG_ETHTOOL_NETLINK) += ethtool_nl.o
66

7-
ethtool_nl-y := netlink.o bitset.o strset.o linkinfo.o
7+
ethtool_nl-y := netlink.o bitset.o strset.o linkinfo.o linkmodes.o

‎net/ethtool/linkmodes.c‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include "netlink.h"
4+
#include "common.h"
5+
#include "bitset.h"
6+
7+
struct linkmodes_req_info {
8+
struct ethnl_req_info base;
9+
};
10+
11+
struct linkmodes_reply_data {
12+
struct ethnl_reply_data base;
13+
struct ethtool_link_ksettings ksettings;
14+
struct ethtool_link_settings *lsettings;
15+
bool peer_empty;
16+
};
17+
18+
#define LINKMODES_REPDATA(__reply_base) \
19+
container_of(__reply_base, struct linkmodes_reply_data, base)
20+
21+
static const struct nla_policy
22+
linkmodes_get_policy[ETHTOOL_A_LINKMODES_MAX + 1] = {
23+
[ETHTOOL_A_LINKMODES_UNSPEC] = { .type = NLA_REJECT },
24+
[ETHTOOL_A_LINKMODES_HEADER] = { .type = NLA_NESTED },
25+
[ETHTOOL_A_LINKMODES_AUTONEG] = { .type = NLA_REJECT },
26+
[ETHTOOL_A_LINKMODES_OURS] = { .type = NLA_REJECT },
27+
[ETHTOOL_A_LINKMODES_PEER] = { .type = NLA_REJECT },
28+
[ETHTOOL_A_LINKMODES_SPEED] = { .type = NLA_REJECT },
29+
[ETHTOOL_A_LINKMODES_DUPLEX] = { .type = NLA_REJECT },
30+
};
31+
32+
static int linkmodes_prepare_data(const struct ethnl_req_info *req_base,
33+
struct ethnl_reply_data *reply_base,
34+
struct genl_info *info)
35+
{
36+
struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
37+
struct net_device *dev = reply_base->dev;
38+
int ret;
39+
40+
data->lsettings = &data->ksettings.base;
41+
42+
ret = ethnl_ops_begin(dev);
43+
if (ret < 0)
44+
return ret;
45+
46+
ret = __ethtool_get_link_ksettings(dev, &data->ksettings);
47+
if (ret < 0 && info) {
48+
GENL_SET_ERR_MSG(info, "failed to retrieve link settings");
49+
goto out;
50+
}
51+
52+
data->peer_empty =
53+
bitmap_empty(data->ksettings.link_modes.lp_advertising,
54+
__ETHTOOL_LINK_MODE_MASK_NBITS);
55+
56+
out:
57+
ethnl_ops_complete(dev);
58+
return ret;
59+
}
60+
61+
static int linkmodes_reply_size(const struct ethnl_req_info *req_base,
62+
const struct ethnl_reply_data *reply_base)
63+
{
64+
const struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
65+
const struct ethtool_link_ksettings *ksettings = &data->ksettings;
66+
bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
67+
int len, ret;
68+
69+
len = nla_total_size(sizeof(u8)) /* LINKMODES_AUTONEG */
70+
+ nla_total_size(sizeof(u32)) /* LINKMODES_SPEED */
71+
+ nla_total_size(sizeof(u8)) /* LINKMODES_DUPLEX */
72+
+ 0;
73+
ret = ethnl_bitset_size(ksettings->link_modes.advertising,
74+
ksettings->link_modes.supported,
75+
__ETHTOOL_LINK_MODE_MASK_NBITS,
76+
link_mode_names, compact);
77+
if (ret < 0)
78+
return ret;
79+
len += ret;
80+
if (!data->peer_empty) {
81+
ret = ethnl_bitset_size(ksettings->link_modes.lp_advertising,
82+
NULL, __ETHTOOL_LINK_MODE_MASK_NBITS,
83+
link_mode_names, compact);
84+
if (ret < 0)
85+
return ret;
86+
len += ret;
87+
}
88+
89+
return len;
90+
}
91+
92+
static int linkmodes_fill_reply(struct sk_buff *skb,
93+
const struct ethnl_req_info *req_base,
94+
const struct ethnl_reply_data *reply_base)
95+
{
96+
const struct linkmodes_reply_data *data = LINKMODES_REPDATA(reply_base);
97+
const struct ethtool_link_ksettings *ksettings = &data->ksettings;
98+
const struct ethtool_link_settings *lsettings = &ksettings->base;
99+
bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
100+
int ret;
101+
102+
if (nla_put_u8(skb, ETHTOOL_A_LINKMODES_AUTONEG, lsettings->autoneg))
103+
return -EMSGSIZE;
104+
105+
ret = ethnl_put_bitset(skb, ETHTOOL_A_LINKMODES_OURS,
106+
ksettings->link_modes.advertising,
107+
ksettings->link_modes.supported,
108+
__ETHTOOL_LINK_MODE_MASK_NBITS, link_mode_names,
109+
compact);
110+
if (ret < 0)
111+
return -EMSGSIZE;
112+
if (!data->peer_empty) {
113+
ret = ethnl_put_bitset(skb, ETHTOOL_A_LINKMODES_PEER,
114+
ksettings->link_modes.lp_advertising,
115+
NULL, __ETHTOOL_LINK_MODE_MASK_NBITS,
116+
link_mode_names, compact);
117+
if (ret < 0)
118+
return -EMSGSIZE;
119+
}
120+
121+
if (nla_put_u32(skb, ETHTOOL_A_LINKMODES_SPEED, lsettings->speed) ||
122+
nla_put_u8(skb, ETHTOOL_A_LINKMODES_DUPLEX, lsettings->duplex))
123+
return -EMSGSIZE;
124+
125+
return 0;
126+
}
127+
128+
const struct ethnl_request_ops ethnl_linkmodes_request_ops = {
129+
.request_cmd = ETHTOOL_MSG_LINKMODES_GET,
130+
.reply_cmd = ETHTOOL_MSG_LINKMODES_GET_REPLY,
131+
.hdr_attr = ETHTOOL_A_LINKMODES_HEADER,
132+
.max_attr = ETHTOOL_A_LINKMODES_MAX,
133+
.req_info_size = sizeof(struct linkmodes_req_info),
134+
.reply_data_size = sizeof(struct linkmodes_reply_data),
135+
.request_policy = linkmodes_get_policy,
136+
137+
.prepare_data = linkmodes_prepare_data,
138+
.reply_size = linkmodes_reply_size,
139+
.fill_reply = linkmodes_fill_reply,
140+
};

‎net/ethtool/netlink.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ static const struct ethnl_request_ops *
209209
ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
210210
[ETHTOOL_MSG_STRSET_GET] = &ethnl_strset_request_ops,
211211
[ETHTOOL_MSG_LINKINFO_GET] = &ethnl_linkinfo_request_ops,
212+
[ETHTOOL_MSG_LINKMODES_GET] = &ethnl_linkmodes_request_ops,
212213
};
213214

214215
static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
@@ -630,6 +631,13 @@ static const struct genl_ops ethtool_genl_ops[] = {
630631
.flags = GENL_UNS_ADMIN_PERM,
631632
.doit = ethnl_set_linkinfo,
632633
},
634+
{
635+
.cmd = ETHTOOL_MSG_LINKMODES_GET,
636+
.doit = ethnl_default_doit,
637+
.start = ethnl_default_start,
638+
.dumpit = ethnl_default_dumpit,
639+
.done = ethnl_default_done,
640+
},
633641
};
634642

635643
static const struct genl_multicast_group ethtool_nl_mcgrps[] = {

‎net/ethtool/netlink.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ struct ethnl_request_ops {
332332

333333
extern const struct ethnl_request_ops ethnl_strset_request_ops;
334334
extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
335+
extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
335336

336337
int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
337338

0 commit comments

Comments
 (0)