Skip to content

Commit 3d2b847

Browse files
mkubecekdavem330
authored andcommitted
ethtool: provide link state with LINKSTATE_GET request
Implement LINKSTATE_GET netlink request to get link state information. At the moment, only link up flag as provided by ETHTOOL_GLINK ioctl command is returned. LINKSTATE_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 <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1b1b184 commit 3d2b847

File tree

9 files changed

+146
-6
lines changed

9 files changed

+146
-6
lines changed

Documentation/networking/ethtool-netlink.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Userspace to kernel:
184184
``ETHTOOL_MSG_LINKINFO_SET`` set link settings
185185
``ETHTOOL_MSG_LINKMODES_GET`` get link modes info
186186
``ETHTOOL_MSG_LINKMODES_SET`` set link modes info
187+
``ETHTOOL_MSG_LINKSTATE_GET`` get link state
187188
===================================== ================================
188189

189190
Kernel to userspace:
@@ -194,6 +195,7 @@ Kernel to userspace:
194195
``ETHTOOL_MSG_LINKINFO_NTF`` link settings notification
195196
``ETHTOOL_MSG_LINKMODES_GET_REPLY`` link modes info
196197
``ETHTOOL_MSG_LINKMODES_NTF`` link modes notification
198+
``ETHTOOL_MSG_LINKSTATE_GET_REPLY`` link state info
197199
===================================== ================================
198200

199201
``GET`` requests are sent by userspace applications to retrieve device
@@ -392,6 +394,35 @@ is supposed to allow requesting changes without knowing what exactly kernel
392394
supports.
393395

394396

397+
LINKSTATE_GET
398+
=============
399+
400+
Requests link state information. At the moment, only link up/down flag (as
401+
provided by ``ETHTOOL_GLINK`` ioctl command) is provided but some future
402+
extensions are planned (e.g. link down reason). This request does not have any
403+
attributes.
404+
405+
Request contents:
406+
407+
==================================== ====== ==========================
408+
``ETHTOOL_A_LINKSTATE_HEADER`` nested request header
409+
==================================== ====== ==========================
410+
411+
Kernel response contents:
412+
413+
==================================== ====== ==========================
414+
``ETHTOOL_A_LINKSTATE_HEADER`` nested reply header
415+
``ETHTOOL_A_LINKSTATE_LINK`` bool link state (up/down)
416+
==================================== ====== ==========================
417+
418+
For most NIC drivers, the value of ``ETHTOOL_A_LINKSTATE_LINK`` returns
419+
carrier flag provided by ``netif_carrier_ok()`` but there are drivers which
420+
define their own handler.
421+
422+
``LINKSTATE_GET`` allows dump requests (kernel returns reply messages for all
423+
devices supporting the request).
424+
425+
395426
Request translation
396427
===================
397428

@@ -413,7 +444,7 @@ have their netlink replacement yet.
413444
``ETHTOOL_GMSGLVL`` n/a
414445
``ETHTOOL_SMSGLVL`` n/a
415446
``ETHTOOL_NWAY_RST`` n/a
416-
``ETHTOOL_GLINK`` n/a
447+
``ETHTOOL_GLINK`` ``ETHTOOL_MSG_LINKSTATE_GET``
417448
``ETHTOOL_GEEPROM`` n/a
418449
``ETHTOOL_SEEPROM`` n/a
419450
``ETHTOOL_GCOALESCE`` n/a

include/uapi/linux/ethtool_netlink.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum {
1919
ETHTOOL_MSG_LINKINFO_SET,
2020
ETHTOOL_MSG_LINKMODES_GET,
2121
ETHTOOL_MSG_LINKMODES_SET,
22+
ETHTOOL_MSG_LINKSTATE_GET,
2223

2324
/* add new constants above here */
2425
__ETHTOOL_MSG_USER_CNT,
@@ -33,6 +34,7 @@ enum {
3334
ETHTOOL_MSG_LINKINFO_NTF,
3435
ETHTOOL_MSG_LINKMODES_GET_REPLY,
3536
ETHTOOL_MSG_LINKMODES_NTF,
37+
ETHTOOL_MSG_LINKSTATE_GET_REPLY,
3638

3739
/* add new constants above here */
3840
__ETHTOOL_MSG_KERNEL_CNT,
@@ -181,6 +183,18 @@ enum {
181183
ETHTOOL_A_LINKMODES_MAX = __ETHTOOL_A_LINKMODES_CNT - 1
182184
};
183185

186+
/* LINKSTATE */
187+
188+
enum {
189+
ETHTOOL_A_LINKSTATE_UNSPEC,
190+
ETHTOOL_A_LINKSTATE_HEADER, /* nest - _A_HEADER_* */
191+
ETHTOOL_A_LINKSTATE_LINK, /* u8 */
192+
193+
/* add new constants above here */
194+
__ETHTOOL_A_LINKSTATE_CNT,
195+
ETHTOOL_A_LINKSTATE_MAX = __ETHTOOL_A_LINKSTATE_CNT - 1
196+
};
197+
184198
/* generic netlink info */
185199
#define ETHTOOL_GENL_NAME "ethtool"
186200
#define ETHTOOL_GENL_VERSION 1

net/ethtool/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ 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 linkmodes.o
7+
ethtool_nl-y := netlink.o bitset.o strset.o linkinfo.o linkmodes.o \
8+
linkstate.o

net/ethtool/common.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,11 @@ convert_legacy_settings_to_link_ksettings(
217217
= legacy_settings->eth_tp_mdix_ctrl;
218218
return retval;
219219
}
220+
221+
int __ethtool_get_link(struct net_device *dev)
222+
{
223+
if (!dev->ethtool_ops->get_link)
224+
return -EOPNOTSUPP;
225+
226+
return netif_running(dev) && dev->ethtool_ops->get_link(dev);
227+
}

net/ethtool/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef _ETHTOOL_COMMON_H
44
#define _ETHTOOL_COMMON_H
55

6+
#include <linux/netdevice.h>
67
#include <linux/ethtool.h>
78

89
/* compose link mode index from speed, type and duplex */
@@ -19,6 +20,8 @@ extern const char
1920
phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN];
2021
extern const char link_mode_names[][ETH_GSTRING_LEN];
2122

23+
int __ethtool_get_link(struct net_device *dev);
24+
2225
bool convert_legacy_settings_to_link_ksettings(
2326
struct ethtool_link_ksettings *link_ksettings,
2427
const struct ethtool_cmd *legacy_settings);

net/ethtool/ioctl.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,12 +1365,12 @@ static int ethtool_nway_reset(struct net_device *dev)
13651365
static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
13661366
{
13671367
struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1368+
int link = __ethtool_get_link(dev);
13681369

1369-
if (!dev->ethtool_ops->get_link)
1370-
return -EOPNOTSUPP;
1371-
1372-
edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1370+
if (link < 0)
1371+
return link;
13731372

1373+
edata.data = link;
13741374
if (copy_to_user(useraddr, &edata, sizeof(edata)))
13751375
return -EFAULT;
13761376
return 0;

net/ethtool/linkstate.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
#include "netlink.h"
4+
#include "common.h"
5+
6+
struct linkstate_req_info {
7+
struct ethnl_req_info base;
8+
};
9+
10+
struct linkstate_reply_data {
11+
struct ethnl_reply_data base;
12+
int link;
13+
};
14+
15+
#define LINKSTATE_REPDATA(__reply_base) \
16+
container_of(__reply_base, struct linkstate_reply_data, base)
17+
18+
static const struct nla_policy
19+
linkstate_get_policy[ETHTOOL_A_LINKSTATE_MAX + 1] = {
20+
[ETHTOOL_A_LINKSTATE_UNSPEC] = { .type = NLA_REJECT },
21+
[ETHTOOL_A_LINKSTATE_HEADER] = { .type = NLA_NESTED },
22+
[ETHTOOL_A_LINKSTATE_LINK] = { .type = NLA_REJECT },
23+
};
24+
25+
static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
26+
struct ethnl_reply_data *reply_base,
27+
struct genl_info *info)
28+
{
29+
struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
30+
struct net_device *dev = reply_base->dev;
31+
int ret;
32+
33+
ret = ethnl_ops_begin(dev);
34+
if (ret < 0)
35+
return ret;
36+
data->link = __ethtool_get_link(dev);
37+
ethnl_ops_complete(dev);
38+
39+
return 0;
40+
}
41+
42+
static int linkstate_reply_size(const struct ethnl_req_info *req_base,
43+
const struct ethnl_reply_data *reply_base)
44+
{
45+
return nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
46+
+ 0;
47+
}
48+
49+
static int linkstate_fill_reply(struct sk_buff *skb,
50+
const struct ethnl_req_info *req_base,
51+
const struct ethnl_reply_data *reply_base)
52+
{
53+
struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
54+
55+
if (data->link >= 0 &&
56+
nla_put_u8(skb, ETHTOOL_A_LINKSTATE_LINK, !!data->link))
57+
return -EMSGSIZE;
58+
59+
return 0;
60+
}
61+
62+
const struct ethnl_request_ops ethnl_linkstate_request_ops = {
63+
.request_cmd = ETHTOOL_MSG_LINKSTATE_GET,
64+
.reply_cmd = ETHTOOL_MSG_LINKSTATE_GET_REPLY,
65+
.hdr_attr = ETHTOOL_A_LINKSTATE_HEADER,
66+
.max_attr = ETHTOOL_A_LINKSTATE_MAX,
67+
.req_info_size = sizeof(struct linkstate_req_info),
68+
.reply_data_size = sizeof(struct linkstate_reply_data),
69+
.request_policy = linkstate_get_policy,
70+
71+
.prepare_data = linkstate_prepare_data,
72+
.reply_size = linkstate_reply_size,
73+
.fill_reply = linkstate_fill_reply,
74+
};

net/ethtool/netlink.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ 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,
212212
[ETHTOOL_MSG_LINKMODES_GET] = &ethnl_linkmodes_request_ops,
213+
[ETHTOOL_MSG_LINKSTATE_GET] = &ethnl_linkstate_request_ops,
213214
};
214215

215216
static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
@@ -645,6 +646,13 @@ static const struct genl_ops ethtool_genl_ops[] = {
645646
.flags = GENL_UNS_ADMIN_PERM,
646647
.doit = ethnl_set_linkmodes,
647648
},
649+
{
650+
.cmd = ETHTOOL_MSG_LINKSTATE_GET,
651+
.doit = ethnl_default_doit,
652+
.start = ethnl_default_start,
653+
.dumpit = ethnl_default_dumpit,
654+
.done = ethnl_default_done,
655+
},
648656
};
649657

650658
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
@@ -333,6 +333,7 @@ struct ethnl_request_ops {
333333
extern const struct ethnl_request_ops ethnl_strset_request_ops;
334334
extern const struct ethnl_request_ops ethnl_linkinfo_request_ops;
335335
extern const struct ethnl_request_ops ethnl_linkmodes_request_ops;
336+
extern const struct ethnl_request_ops ethnl_linkstate_request_ops;
336337

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

0 commit comments

Comments
 (0)