Skip to content

Commit 0524399

Browse files
mkubecekdavem330
authored andcommitted
ethtool: provide netdev features with FEATURES_GET request
Implement FEATURES_GET request to get network device features. These are traditionally available via ETHTOOL_GFEATURES ioctl request. v2: - style cleanup suggested by Jakub Kicinski Signed-off-by: Michal Kubecek <mkubecek@suse.cz> Reviewed-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f70bb06 commit 0524399

8 files changed

Lines changed: 202 additions & 12 deletions

File tree

‎Documentation/networking/ethtool-netlink.rst‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Userspace to kernel:
189189
``ETHTOOL_MSG_DEBUG_SET`` set debugging settings
190190
``ETHTOOL_MSG_WOL_GET`` get wake-on-lan settings
191191
``ETHTOOL_MSG_WOL_SET`` set wake-on-lan settings
192+
``ETHTOOL_MSG_FEATURES_GET`` get device features
192193
===================================== ================================
193194

194195
Kernel to userspace:
@@ -204,6 +205,7 @@ Kernel to userspace:
204205
``ETHTOOL_MSG_DEBUG_NTF`` debugging settings notification
205206
``ETHTOOL_MSG_WOL_GET_REPLY`` wake-on-lan settings
206207
``ETHTOOL_MSG_WOL_NTF`` wake-on-lan settings notification
208+
``ETHTOOL_MSG_FEATURES_GET_REPLY`` device features
207209
===================================== =================================
208210

209211
``GET`` requests are sent by userspace applications to retrieve device
@@ -521,6 +523,37 @@ Request contents:
521523
``WAKE_MAGICSECURE`` mode.
522524

523525

526+
FEATURES_GET
527+
============
528+
529+
Gets netdev features like ``ETHTOOL_GFEATURES`` ioctl request.
530+
531+
Request contents:
532+
533+
==================================== ====== ==========================
534+
``ETHTOOL_A_FEATURES_HEADER`` nested request header
535+
==================================== ====== ==========================
536+
537+
Kernel response contents:
538+
539+
==================================== ====== ==========================
540+
``ETHTOOL_A_FEATURES_HEADER`` nested reply header
541+
``ETHTOOL_A_FEATURES_HW`` bitset dev->hw_features
542+
``ETHTOOL_A_FEATURES_WANTED`` bitset dev->wanted_features
543+
``ETHTOOL_A_FEATURES_ACTIVE`` bitset dev->features
544+
``ETHTOOL_A_FEATURES_NOCHANGE`` bitset NETIF_F_NEVER_CHANGE
545+
==================================== ====== ==========================
546+
547+
Bitmaps in kernel response have the same meaning as bitmaps used in ioctl
548+
interference but attribute names are different (they are based on
549+
corresponding members of struct net_device). Legacy "flags" are not provided,
550+
if userspace needs them (most likely only ethtool for backward compatibility),
551+
it can calculate their values from related feature bits itself.
552+
ETHA_FEATURES_HW uses mask consisting of all features recognized by kernel (to
553+
provide all names when using verbose bitmap format), the other three use no
554+
mask (simple bit lists).
555+
556+
524557
Request translation
525558
===================
526559

@@ -551,30 +584,30 @@ have their netlink replacement yet.
551584
``ETHTOOL_SRINGPARAM`` n/a
552585
``ETHTOOL_GPAUSEPARAM`` n/a
553586
``ETHTOOL_SPAUSEPARAM`` n/a
554-
``ETHTOOL_GRXCSUM`` n/a
587+
``ETHTOOL_GRXCSUM`` ``ETHTOOL_MSG_FEATURES_GET``
555588
``ETHTOOL_SRXCSUM`` n/a
556-
``ETHTOOL_GTXCSUM`` n/a
589+
``ETHTOOL_GTXCSUM`` ``ETHTOOL_MSG_FEATURES_GET``
557590
``ETHTOOL_STXCSUM`` n/a
558-
``ETHTOOL_GSG`` n/a
591+
``ETHTOOL_GSG`` ``ETHTOOL_MSG_FEATURES_GET``
559592
``ETHTOOL_SSG`` n/a
560593
``ETHTOOL_TEST`` n/a
561594
``ETHTOOL_GSTRINGS`` ``ETHTOOL_MSG_STRSET_GET``
562595
``ETHTOOL_PHYS_ID`` n/a
563596
``ETHTOOL_GSTATS`` n/a
564-
``ETHTOOL_GTSO`` n/a
597+
``ETHTOOL_GTSO`` ``ETHTOOL_MSG_FEATURES_GET``
565598
``ETHTOOL_STSO`` n/a
566599
``ETHTOOL_GPERMADDR`` rtnetlink ``RTM_GETLINK``
567-
``ETHTOOL_GUFO`` n/a
600+
``ETHTOOL_GUFO`` ``ETHTOOL_MSG_FEATURES_GET``
568601
``ETHTOOL_SUFO`` n/a
569-
``ETHTOOL_GGSO`` n/a
602+
``ETHTOOL_GGSO`` ``ETHTOOL_MSG_FEATURES_GET``
570603
``ETHTOOL_SGSO`` n/a
571-
``ETHTOOL_GFLAGS`` n/a
604+
``ETHTOOL_GFLAGS`` ``ETHTOOL_MSG_FEATURES_GET``
572605
``ETHTOOL_SFLAGS`` n/a
573606
``ETHTOOL_GPFLAGS`` n/a
574607
``ETHTOOL_SPFLAGS`` n/a
575608
``ETHTOOL_GRXFH`` n/a
576609
``ETHTOOL_SRXFH`` n/a
577-
``ETHTOOL_GGRO`` n/a
610+
``ETHTOOL_GGRO`` ``ETHTOOL_MSG_FEATURES_GET``
578611
``ETHTOOL_SGRO`` n/a
579612
``ETHTOOL_GRXRINGS`` n/a
580613
``ETHTOOL_GRXCLSRLCNT`` n/a
@@ -589,7 +622,7 @@ have their netlink replacement yet.
589622
``ETHTOOL_GSSET_INFO`` ``ETHTOOL_MSG_STRSET_GET``
590623
``ETHTOOL_GRXFHINDIR`` n/a
591624
``ETHTOOL_SRXFHINDIR`` n/a
592-
``ETHTOOL_GFEATURES`` n/a
625+
``ETHTOOL_GFEATURES`` ``ETHTOOL_MSG_FEATURES_GET``
593626
``ETHTOOL_SFEATURES`` n/a
594627
``ETHTOOL_GCHANNELS`` n/a
595628
``ETHTOOL_SCHANNELS`` n/a

‎include/uapi/linux/ethtool_netlink.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum {
2424
ETHTOOL_MSG_DEBUG_SET,
2525
ETHTOOL_MSG_WOL_GET,
2626
ETHTOOL_MSG_WOL_SET,
27+
ETHTOOL_MSG_FEATURES_GET,
2728

2829
/* add new constants above here */
2930
__ETHTOOL_MSG_USER_CNT,
@@ -43,6 +44,7 @@ enum {
4344
ETHTOOL_MSG_DEBUG_NTF,
4445
ETHTOOL_MSG_WOL_GET_REPLY,
4546
ETHTOOL_MSG_WOL_NTF,
47+
ETHTOOL_MSG_FEATURES_GET_REPLY,
4648

4749
/* add new constants above here */
4850
__ETHTOOL_MSG_KERNEL_CNT,
@@ -228,6 +230,21 @@ enum {
228230
ETHTOOL_A_WOL_MAX = __ETHTOOL_A_WOL_CNT - 1
229231
};
230232

233+
/* FEATURES */
234+
235+
enum {
236+
ETHTOOL_A_FEATURES_UNSPEC,
237+
ETHTOOL_A_FEATURES_HEADER, /* nest - _A_HEADER_* */
238+
ETHTOOL_A_FEATURES_HW, /* bitset */
239+
ETHTOOL_A_FEATURES_WANTED, /* bitset */
240+
ETHTOOL_A_FEATURES_ACTIVE, /* bitset */
241+
ETHTOOL_A_FEATURES_NOCHANGE, /* bitset */
242+
243+
/* add new constants above here */
244+
__ETHTOOL_A_FEATURES_CNT,
245+
ETHTOOL_A_FEATURES_MAX = __ETHTOOL_A_FEATURES_CNT - 1
246+
};
247+
231248
/* generic netlink info */
232249
#define ETHTOOL_GENL_NAME "ethtool"
233250
#define ETHTOOL_GENL_VERSION 1

‎net/ethtool/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ obj-y += ioctl.o common.o
55
obj-$(CONFIG_ETHTOOL_NETLINK) += ethtool_nl.o
66

77
ethtool_nl-y := netlink.o bitset.o strset.o linkinfo.o linkmodes.o \
8-
linkstate.o debug.o wol.o
8+
linkstate.o debug.o wol.o features.o

‎net/ethtool/common.h‎

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

9+
#define ETHTOOL_DEV_FEATURE_WORDS DIV_ROUND_UP(NETDEV_FEATURE_COUNT, 32)
10+
911
/* compose link mode index from speed, type and duplex */
1012
#define ETHTOOL_LINK_MODE(speed, type, duplex) \
1113
ETHTOOL_LINK_MODE_ ## speed ## base ## type ## _ ## duplex ## _BIT

‎net/ethtool/features.c‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include "netlink.h"
4+
#include "common.h"
5+
#include "bitset.h"
6+
7+
struct features_req_info {
8+
struct ethnl_req_info base;
9+
};
10+
11+
struct features_reply_data {
12+
struct ethnl_reply_data base;
13+
u32 hw[ETHTOOL_DEV_FEATURE_WORDS];
14+
u32 wanted[ETHTOOL_DEV_FEATURE_WORDS];
15+
u32 active[ETHTOOL_DEV_FEATURE_WORDS];
16+
u32 nochange[ETHTOOL_DEV_FEATURE_WORDS];
17+
u32 all[ETHTOOL_DEV_FEATURE_WORDS];
18+
};
19+
20+
#define FEATURES_REPDATA(__reply_base) \
21+
container_of(__reply_base, struct features_reply_data, base)
22+
23+
static const struct nla_policy
24+
features_get_policy[ETHTOOL_A_FEATURES_MAX + 1] = {
25+
[ETHTOOL_A_FEATURES_UNSPEC] = { .type = NLA_REJECT },
26+
[ETHTOOL_A_FEATURES_HEADER] = { .type = NLA_NESTED },
27+
[ETHTOOL_A_FEATURES_HW] = { .type = NLA_REJECT },
28+
[ETHTOOL_A_FEATURES_WANTED] = { .type = NLA_REJECT },
29+
[ETHTOOL_A_FEATURES_ACTIVE] = { .type = NLA_REJECT },
30+
[ETHTOOL_A_FEATURES_NOCHANGE] = { .type = NLA_REJECT },
31+
};
32+
33+
static void ethnl_features_to_bitmap32(u32 *dest, netdev_features_t src)
34+
{
35+
unsigned int i;
36+
37+
for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; i++)
38+
dest[i] = src >> (32 * i);
39+
}
40+
41+
static int features_prepare_data(const struct ethnl_req_info *req_base,
42+
struct ethnl_reply_data *reply_base,
43+
struct genl_info *info)
44+
{
45+
struct features_reply_data *data = FEATURES_REPDATA(reply_base);
46+
struct net_device *dev = reply_base->dev;
47+
netdev_features_t all_features;
48+
49+
ethnl_features_to_bitmap32(data->hw, dev->hw_features);
50+
ethnl_features_to_bitmap32(data->wanted, dev->wanted_features);
51+
ethnl_features_to_bitmap32(data->active, dev->features);
52+
ethnl_features_to_bitmap32(data->nochange, NETIF_F_NEVER_CHANGE);
53+
all_features = GENMASK_ULL(NETDEV_FEATURE_COUNT - 1, 0);
54+
ethnl_features_to_bitmap32(data->all, all_features);
55+
56+
return 0;
57+
}
58+
59+
static int features_reply_size(const struct ethnl_req_info *req_base,
60+
const struct ethnl_reply_data *reply_base)
61+
{
62+
const struct features_reply_data *data = FEATURES_REPDATA(reply_base);
63+
bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
64+
unsigned int len = 0;
65+
int ret;
66+
67+
ret = ethnl_bitset32_size(data->hw, data->all, NETDEV_FEATURE_COUNT,
68+
netdev_features_strings, compact);
69+
if (ret < 0)
70+
return ret;
71+
len += ret;
72+
ret = ethnl_bitset32_size(data->wanted, NULL, NETDEV_FEATURE_COUNT,
73+
netdev_features_strings, compact);
74+
if (ret < 0)
75+
return ret;
76+
len += ret;
77+
ret = ethnl_bitset32_size(data->active, NULL, NETDEV_FEATURE_COUNT,
78+
netdev_features_strings, compact);
79+
if (ret < 0)
80+
return ret;
81+
len += ret;
82+
ret = ethnl_bitset32_size(data->nochange, NULL, NETDEV_FEATURE_COUNT,
83+
netdev_features_strings, compact);
84+
if (ret < 0)
85+
return ret;
86+
len += ret;
87+
88+
return len;
89+
}
90+
91+
static int features_fill_reply(struct sk_buff *skb,
92+
const struct ethnl_req_info *req_base,
93+
const struct ethnl_reply_data *reply_base)
94+
{
95+
const struct features_reply_data *data = FEATURES_REPDATA(reply_base);
96+
bool compact = req_base->flags & ETHTOOL_FLAG_COMPACT_BITSETS;
97+
int ret;
98+
99+
ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_HW, data->hw,
100+
data->all, NETDEV_FEATURE_COUNT,
101+
netdev_features_strings, compact);
102+
if (ret < 0)
103+
return ret;
104+
ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_WANTED, data->wanted,
105+
NULL, NETDEV_FEATURE_COUNT,
106+
netdev_features_strings, compact);
107+
if (ret < 0)
108+
return ret;
109+
ret = ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_ACTIVE, data->active,
110+
NULL, NETDEV_FEATURE_COUNT,
111+
netdev_features_strings, compact);
112+
if (ret < 0)
113+
return ret;
114+
return ethnl_put_bitset32(skb, ETHTOOL_A_FEATURES_NOCHANGE,
115+
data->nochange, NULL, NETDEV_FEATURE_COUNT,
116+
netdev_features_strings, compact);
117+
}
118+
119+
const struct ethnl_request_ops ethnl_features_request_ops = {
120+
.request_cmd = ETHTOOL_MSG_FEATURES_GET,
121+
.reply_cmd = ETHTOOL_MSG_FEATURES_GET_REPLY,
122+
.hdr_attr = ETHTOOL_A_FEATURES_HEADER,
123+
.max_attr = ETHTOOL_A_FEATURES_MAX,
124+
.req_info_size = sizeof(struct features_req_info),
125+
.reply_data_size = sizeof(struct features_reply_data),
126+
.request_policy = features_get_policy,
127+
128+
.prepare_data = features_prepare_data,
129+
.reply_size = features_reply_size,
130+
.fill_reply = features_fill_reply,
131+
};

‎net/ethtool/ioctl.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ EXPORT_SYMBOL(ethtool_op_get_ts_info);
5656

5757
/* Handlers for each ethtool command */
5858

59-
#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
60-
6159
static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
6260
{
6361
struct ethtool_gfeatures cmd = {

‎net/ethtool/netlink.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
215215
[ETHTOOL_MSG_LINKSTATE_GET] = &ethnl_linkstate_request_ops,
216216
[ETHTOOL_MSG_DEBUG_GET] = &ethnl_debug_request_ops,
217217
[ETHTOOL_MSG_WOL_GET] = &ethnl_wol_request_ops,
218+
[ETHTOOL_MSG_FEATURES_GET] = &ethnl_features_request_ops,
218219
};
219220

220221
static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
@@ -695,6 +696,13 @@ static const struct genl_ops ethtool_genl_ops[] = {
695696
.flags = GENL_UNS_ADMIN_PERM,
696697
.doit = ethnl_set_wol,
697698
},
699+
{
700+
.cmd = ETHTOOL_MSG_FEATURES_GET,
701+
.doit = ethnl_default_doit,
702+
.start = ethnl_default_start,
703+
.dumpit = ethnl_default_dumpit,
704+
.done = ethnl_default_done,
705+
},
698706
};
699707

700708
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
@@ -337,6 +337,7 @@ extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
337337
extern const struct ethnl_request_ops ethnl_linkstate_request_ops;
338338
extern const struct ethnl_request_ops ethnl_debug_request_ops;
339339
extern const struct ethnl_request_ops ethnl_wol_request_ops;
340+
extern const struct ethnl_request_ops ethnl_features_request_ops;
340341

341342
int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info);
342343
int ethnl_set_linkmodes(struct sk_buff *skb, struct genl_info *info);

0 commit comments

Comments
 (0)