Commit 1b3b289f authored by David S. Miller's avatar David S. Miller

Merge branch 'ethtool-netlink-part-one'

Michal Kubecek says:

====================
ethtool netlink interface, part 1

This is first part of netlink based alternative userspace interface for
ethtool. It aims to address some long known issues with the ioctl
interface, mainly lack of extensibility, raciness, limited error reporting
and absence of notifications. The goal is to allow userspace ethtool
utility to provide all features it currently does but without using the
ioctl interface. However, some features provided by ethtool ioctl API will
be available through other netlink interfaces (rtnetlink, devlink) if it's
more appropriate.

The interface uses generic netlink family "ethtool" and provides multicast
group "monitor" which is used for notifications. Documentation for the
interface is in Documentation/networking/ethtool-netlink.rst file. The
netlink interface is optional, it is built when CONFIG_ETHTOOL_NETLINK
(bool) option is enabled.

There are three types of request messages distinguished by suffix "_GET"
(query for information), "_SET" (modify parameters) and "_ACT" (perform an
action). Kernel reply messages have name with additional suffix "_REPLY"
(e.g. ETHTOOL_MSG_SETTINGS_GET_REPLY). Most "_SET" and "_ACT" message types
do not have matching reply type as only some of them need additional reply
data beyond numeric error code and extack. Kernel also broadcasts
notification messages ("_NTF" suffix) on changes.

Basic concepts:

- make extensions easier not only by allowing new attributes but also by
  imposing as few artificial limits as possible, e.g. by using arbitrary
  size bit sets for most bitmap attributes or by not using fixed size
  strings
- use extack for error reporting and warnings
- send netlink notifications on changes (even if they were done using the
  ioctl interface) and actions
- avoid the racy read/modify/write cycle between kernel and userspace by
  sending only attributes which userspace wants to change; there is still
  a read/modify/write cycle between generic kernel code and ethtool_ops
  handler in NIC driver but it is only in kernel and under RTNL lock
- reduce the number of name lists that need to be kept in sync between
  kernel and userspace (e.g. recognized link modes)
- where feasible, allow dump requests to query specific information for all
  network devices
- as parsing and generating netlink messages is more complicated than
  simply copying data structures between userspace API and ethtool_ops
  handlers (which most ioctl commands do), split the code into multiple
  files in net/ethtool directory; move net/core/ethtool.c also to this
  directory and rename it to ioctl.c

Changes between v8 and v9:

- fix ethnl_update_u8()
- fix description of ETHTOOL_A_LINKSTATE_LINK in rst file
- add explanation of verbose vs. compact bitset usage to documentation
- link ethtool-netlink.rst into toctree

Main changes between v7 and v8:

- preliminary patches sent as a separate series (already in net-next)
- split notification related changes out of _SET patches
- drop request specific flags from common header
- use FLAG/flag rather than GFLAG/gflag for global flags (as there are
  only global flags now)
- allow device names up to ALTIFNAMSIZ characters
- rename ETHTOOL_A_BITSET_LIST to ETHTOOL_A_BITSET_NOMASK
- rename ETHTOOL_A_BIT{,S}_* to ETHTOOL_A_BITSET_BIT{,S}_*
- use standard bitset helpers for link modes (rather than in-place
  conversion)
- use "default" rather than "standard" for unified _GET handlers
- fixed 64-bit big endian bitset code

Main changes between v6 and v7:

- split complex messages into small single purpose ones (drop info and
  request masks and one level of nesting)
- separate request information and reply data into two structures
- refactor bitset handling (no simultaneous u32/ulong handling but avoid
  kmalloc() except for long bitmaps on 64-bit big endian architectures)
- use only fixed size strings internally (will be replaced by char *
  eventually but that will require rewriting also existing ioctl code)
- rework ethnl_update_* helpers to return error code
- rename request flag constants (to ETHTOOL_[GR]FLAG_ prefix)
- convert documentation to rst

Main changes between v5 and v6:

- use ETHTOOL_MSG_ prefix for message types
- replace ETHA_ prefix for netlink attributes by ETHTOOL_A_
- replace ETH_x_IM_y for infomask bits by ETHTOOL_IM_x_y
- split GET reply types from SET requests and notifications
- split kernel and userspace message types into different enums
- remove INFO_GET requests from submitted part
- drop EVENT notifications (use rtnetlink and on-demand string set load)
- reorganize patches to reduce the number of intermitent warnings
- unify request/reply header and its processing
- another nest around strings in a string set for consistency
- more consistent identifier naming
- coding style cleanup
- get rid of some of the helpers
- set bad attribute in extack where applicable
- various bug fixes
- improve documentation and code comments, more kerneldoc comments
- more verbose commit messages

Changes between v4 and v5:

- do not panic on failed initialization, only WARN()

Main changes between RFC v3 and v4:

- use more kerneldoc style comments
- strict attribute policy checking
- use macros for tables of link mode names and parameters
- provide permanent hardware address in rtnetlink
- coding style cleanup
- split too long patches, reorder
- wrap more ETHA_SETTINGS_* attributes in nests
- add also some SET_* implementation into submitted part

Main changes between RFC v2 and RFC v3:

- do not allow building as a module (no netdev notifiers needed)
- drop some obsolete fields
- add permanent hw address, timestamping and private flags support
- rework bitset handling to get rid of variable length arrays
- notify monitor on device renames
- restructure GET_SETTINGS/SET_SETTINGS messages
- split too long patches and submit only first part of the series

Main changes between RFC v1 and RFC v2:

- support dumps for all "get" requests
- provide notifications for changes related to supported request types
- support getting string sets (both global and per device)
- support getting/setting device features
- get rid of family specific header, everything passed as attributes
- split netlink code into multiple files in net/ethtool/ directory
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 356b23c0 3d2b847f
This diff is collapsed.
......@@ -16,6 +16,7 @@ Contents:
devlink-info-versions
devlink-trap
devlink-trap-netdevsim
ethtool-netlink
ieee802154
j1939
kapi
......
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _LINUX_ETHTOOL_NETLINK_H_
#define _LINUX_ETHTOOL_NETLINK_H_
#include <uapi/linux/ethtool_netlink.h>
#include <linux/ethtool.h>
#include <linux/netdevice.h>
#define __ETHTOOL_LINK_MODE_MASK_NWORDS \
DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
enum ethtool_multicast_groups {
ETHNL_MCGRP_MONITOR,
};
#endif /* _LINUX_ETHTOOL_NETLINK_H_ */
......@@ -4393,6 +4393,15 @@ struct netdev_notifier_bonding_info {
void netdev_bonding_info_change(struct net_device *dev,
struct netdev_bonding_info *bonding_info);
#if IS_ENABLED(CONFIG_ETHTOOL_NETLINK)
void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data);
#else
static inline void ethtool_notify(struct net_device *dev, unsigned int cmd,
const void *data)
{
}
#endif
static inline
struct sk_buff *skb_gso_segment(struct sk_buff *skb, netdev_features_t features)
{
......
......@@ -606,6 +606,9 @@ enum ethtool_stringset {
ETH_SS_PHY_STATS,
ETH_SS_PHY_TUNABLES,
ETH_SS_LINK_MODES,
/* add new constants above here */
ETH_SS_COUNT
};
/**
......
/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
/*
* include/uapi/linux/ethtool_netlink.h - netlink interface for ethtool
*
* See Documentation/networking/ethtool-netlink.txt in kernel source tree for
* doucumentation of the interface.
*/
#ifndef _UAPI_LINUX_ETHTOOL_NETLINK_H_
#define _UAPI_LINUX_ETHTOOL_NETLINK_H_
#include <linux/ethtool.h>
/* message types - userspace to kernel */
enum {
ETHTOOL_MSG_USER_NONE,
ETHTOOL_MSG_STRSET_GET,
ETHTOOL_MSG_LINKINFO_GET,
ETHTOOL_MSG_LINKINFO_SET,
ETHTOOL_MSG_LINKMODES_GET,
ETHTOOL_MSG_LINKMODES_SET,
ETHTOOL_MSG_LINKSTATE_GET,
/* add new constants above here */
__ETHTOOL_MSG_USER_CNT,
ETHTOOL_MSG_USER_MAX = __ETHTOOL_MSG_USER_CNT - 1
};
/* message types - kernel to userspace */
enum {
ETHTOOL_MSG_KERNEL_NONE,
ETHTOOL_MSG_STRSET_GET_REPLY,
ETHTOOL_MSG_LINKINFO_GET_REPLY,
ETHTOOL_MSG_LINKINFO_NTF,
ETHTOOL_MSG_LINKMODES_GET_REPLY,
ETHTOOL_MSG_LINKMODES_NTF,
ETHTOOL_MSG_LINKSTATE_GET_REPLY,
/* add new constants above here */
__ETHTOOL_MSG_KERNEL_CNT,
ETHTOOL_MSG_KERNEL_MAX = __ETHTOOL_MSG_KERNEL_CNT - 1
};
/* request header */
/* use compact bitsets in reply */
#define ETHTOOL_FLAG_COMPACT_BITSETS (1 << 0)
/* provide optional reply for SET or ACT requests */
#define ETHTOOL_FLAG_OMIT_REPLY (1 << 1)
#define ETHTOOL_FLAG_ALL (ETHTOOL_FLAG_COMPACT_BITSETS | \
ETHTOOL_FLAG_OMIT_REPLY)
enum {
ETHTOOL_A_HEADER_UNSPEC,
ETHTOOL_A_HEADER_DEV_INDEX, /* u32 */
ETHTOOL_A_HEADER_DEV_NAME, /* string */
ETHTOOL_A_HEADER_FLAGS, /* u32 - ETHTOOL_FLAG_* */
/* add new constants above here */
__ETHTOOL_A_HEADER_CNT,
ETHTOOL_A_HEADER_MAX = __ETHTOOL_A_HEADER_CNT - 1
};
/* bit sets */
enum {
ETHTOOL_A_BITSET_BIT_UNSPEC,
ETHTOOL_A_BITSET_BIT_INDEX, /* u32 */
ETHTOOL_A_BITSET_BIT_NAME, /* string */
ETHTOOL_A_BITSET_BIT_VALUE, /* flag */
/* add new constants above here */
__ETHTOOL_A_BITSET_BIT_CNT,
ETHTOOL_A_BITSET_BIT_MAX = __ETHTOOL_A_BITSET_BIT_CNT - 1
};
enum {
ETHTOOL_A_BITSET_BITS_UNSPEC,
ETHTOOL_A_BITSET_BITS_BIT, /* nest - _A_BITSET_BIT_* */
/* add new constants above here */
__ETHTOOL_A_BITSET_BITS_CNT,
ETHTOOL_A_BITSET_BITS_MAX = __ETHTOOL_A_BITSET_BITS_CNT - 1
};
enum {
ETHTOOL_A_BITSET_UNSPEC,
ETHTOOL_A_BITSET_NOMASK, /* flag */
ETHTOOL_A_BITSET_SIZE, /* u32 */
ETHTOOL_A_BITSET_BITS, /* nest - _A_BITSET_BITS_* */
ETHTOOL_A_BITSET_VALUE, /* binary */
ETHTOOL_A_BITSET_MASK, /* binary */
/* add new constants above here */
__ETHTOOL_A_BITSET_CNT,
ETHTOOL_A_BITSET_MAX = __ETHTOOL_A_BITSET_CNT - 1
};
/* string sets */
enum {
ETHTOOL_A_STRING_UNSPEC,
ETHTOOL_A_STRING_INDEX, /* u32 */
ETHTOOL_A_STRING_VALUE, /* string */
/* add new constants above here */
__ETHTOOL_A_STRING_CNT,
ETHTOOL_A_STRING_MAX = __ETHTOOL_A_STRING_CNT - 1
};
enum {
ETHTOOL_A_STRINGS_UNSPEC,
ETHTOOL_A_STRINGS_STRING, /* nest - _A_STRINGS_* */
/* add new constants above here */
__ETHTOOL_A_STRINGS_CNT,
ETHTOOL_A_STRINGS_MAX = __ETHTOOL_A_STRINGS_CNT - 1
};
enum {
ETHTOOL_A_STRINGSET_UNSPEC,
ETHTOOL_A_STRINGSET_ID, /* u32 */
ETHTOOL_A_STRINGSET_COUNT, /* u32 */
ETHTOOL_A_STRINGSET_STRINGS, /* nest - _A_STRINGS_* */
/* add new constants above here */
__ETHTOOL_A_STRINGSET_CNT,
ETHTOOL_A_STRINGSET_MAX = __ETHTOOL_A_STRINGSET_CNT - 1
};
enum {
ETHTOOL_A_STRINGSETS_UNSPEC,
ETHTOOL_A_STRINGSETS_STRINGSET, /* nest - _A_STRINGSET_* */
/* add new constants above here */
__ETHTOOL_A_STRINGSETS_CNT,
ETHTOOL_A_STRINGSETS_MAX = __ETHTOOL_A_STRINGSETS_CNT - 1
};
/* STRSET */
enum {
ETHTOOL_A_STRSET_UNSPEC,
ETHTOOL_A_STRSET_HEADER, /* nest - _A_HEADER_* */
ETHTOOL_A_STRSET_STRINGSETS, /* nest - _A_STRINGSETS_* */
ETHTOOL_A_STRSET_COUNTS_ONLY, /* flag */
/* add new constants above here */
__ETHTOOL_A_STRSET_CNT,
ETHTOOL_A_STRSET_MAX = __ETHTOOL_A_STRSET_CNT - 1
};
/* LINKINFO */
enum {
ETHTOOL_A_LINKINFO_UNSPEC,
ETHTOOL_A_LINKINFO_HEADER, /* nest - _A_HEADER_* */
ETHTOOL_A_LINKINFO_PORT, /* u8 */
ETHTOOL_A_LINKINFO_PHYADDR, /* u8 */
ETHTOOL_A_LINKINFO_TP_MDIX, /* u8 */
ETHTOOL_A_LINKINFO_TP_MDIX_CTRL, /* u8 */
ETHTOOL_A_LINKINFO_TRANSCEIVER, /* u8 */
/* add new constants above here */
__ETHTOOL_A_LINKINFO_CNT,
ETHTOOL_A_LINKINFO_MAX = __ETHTOOL_A_LINKINFO_CNT - 1
};
/* LINKMODES */
enum {
ETHTOOL_A_LINKMODES_UNSPEC,
ETHTOOL_A_LINKMODES_HEADER, /* nest - _A_HEADER_* */
ETHTOOL_A_LINKMODES_AUTONEG, /* u8 */
ETHTOOL_A_LINKMODES_OURS, /* bitset */
ETHTOOL_A_LINKMODES_PEER, /* bitset */
ETHTOOL_A_LINKMODES_SPEED, /* u32 */
ETHTOOL_A_LINKMODES_DUPLEX, /* u8 */
/* add new constants above here */
__ETHTOOL_A_LINKMODES_CNT,
ETHTOOL_A_LINKMODES_MAX = __ETHTOOL_A_LINKMODES_CNT - 1
};
/* LINKSTATE */
enum {
ETHTOOL_A_LINKSTATE_UNSPEC,
ETHTOOL_A_LINKSTATE_HEADER, /* nest - _A_HEADER_* */
ETHTOOL_A_LINKSTATE_LINK, /* u8 */
/* add new constants above here */
__ETHTOOL_A_LINKSTATE_CNT,
ETHTOOL_A_LINKSTATE_MAX = __ETHTOOL_A_LINKSTATE_CNT - 1
};
/* generic netlink info */
#define ETHTOOL_GENL_NAME "ethtool"
#define ETHTOOL_GENL_VERSION 1
#define ETHTOOL_MCGRP_MONITOR_NAME "monitor"
#endif /* _UAPI_LINUX_ETHTOOL_NETLINK_H_ */
......@@ -449,6 +449,14 @@ config FAILOVER
migration of VMs with direct attached VFs by failing over to the
paravirtual datapath when the VF is unplugged.
config ETHTOOL_NETLINK
bool "Netlink interface for ethtool"
default y
help
An alternative userspace interface for ethtool based on generic
netlink. It provides better extensibility and some new features,
e.g. notification messages.
endif # if NET
# Used by archs to tell that they support BPF JIT compiler plus which flavour.
......
# SPDX-License-Identifier: GPL-2.0-only
obj-y += ioctl.o common.o
obj-$(CONFIG_ETHTOOL_NETLINK) += ethtool_nl.o
ethtool_nl-y := netlink.o bitset.o strset.o linkinfo.o linkmodes.o \
linkstate.o
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _NET_ETHTOOL_BITSET_H
#define _NET_ETHTOOL_BITSET_H
typedef const char (*const ethnl_string_array_t)[ETH_GSTRING_LEN];
int ethnl_bitset_is_compact(const struct nlattr *bitset, bool *compact);
int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
unsigned int nbits, ethnl_string_array_t names,
bool compact);
int ethnl_bitset32_size(const u32 *val, const u32 *mask, unsigned int nbits,
ethnl_string_array_t names, bool compact);
int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
const unsigned long *val, const unsigned long *mask,
unsigned int nbits, ethnl_string_array_t names,
bool compact);
int ethnl_put_bitset32(struct sk_buff *skb, int attrtype, const u32 *val,
const u32 *mask, unsigned int nbits,
ethnl_string_array_t names, bool compact);
int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
const struct nlattr *attr, ethnl_string_array_t names,
struct netlink_ext_ack *extack, bool *mod);
int ethnl_update_bitset32(u32 *bitmap, unsigned int nbits,
const struct nlattr *attr, ethnl_string_array_t names,
struct netlink_ext_ack *extack, bool *mod);
#endif /* _NET_ETHTOOL_BITSET_H */
......@@ -169,3 +169,59 @@ const char link_mode_names[][ETH_GSTRING_LEN] = {
__DEFINE_LINK_MODE_NAME(400000, CR8, Full),
};
static_assert(ARRAY_SIZE(link_mode_names) == __ETHTOOL_LINK_MODE_MASK_NBITS);
/* return false if legacy contained non-0 deprecated fields
* maxtxpkt/maxrxpkt. rest of ksettings always updated
*/
bool
convert_legacy_settings_to_link_ksettings(
struct ethtool_link_ksettings *link_ksettings,
const struct ethtool_cmd *legacy_settings)
{
bool retval = true;
memset(link_ksettings, 0, sizeof(*link_ksettings));
/* This is used to tell users that driver is still using these
* deprecated legacy fields, and they should not use
* %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
*/
if (legacy_settings->maxtxpkt ||
legacy_settings->maxrxpkt)
retval = false;
ethtool_convert_legacy_u32_to_link_mode(
link_ksettings->link_modes.supported,
legacy_settings->supported);
ethtool_convert_legacy_u32_to_link_mode(
link_ksettings->link_modes.advertising,
legacy_settings->advertising);
ethtool_convert_legacy_u32_to_link_mode(
link_ksettings->link_modes.lp_advertising,
legacy_settings->lp_advertising);
link_ksettings->base.speed
= ethtool_cmd_speed(legacy_settings);
link_ksettings->base.duplex
= legacy_settings->duplex;
link_ksettings->base.port
= legacy_settings->port;
link_ksettings->base.phy_address
= legacy_settings->phy_address;
link_ksettings->base.autoneg
= legacy_settings->autoneg;
link_ksettings->base.mdio_support
= legacy_settings->mdio_support;
link_ksettings->base.eth_tp_mdix
= legacy_settings->eth_tp_mdix;
link_ksettings->base.eth_tp_mdix_ctrl
= legacy_settings->eth_tp_mdix_ctrl;
return retval;
}
int __ethtool_get_link(struct net_device *dev)
{
if (!dev->ethtool_ops->get_link)
return -EOPNOTSUPP;
return netif_running(dev) && dev->ethtool_ops->get_link(dev);
}
......@@ -3,6 +3,7 @@
#ifndef _ETHTOOL_COMMON_H
#define _ETHTOOL_COMMON_H
#include <linux/netdevice.h>
#include <linux/ethtool.h>
/* compose link mode index from speed, type and duplex */
......@@ -19,4 +20,10 @@ extern const char
phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN];
extern const char link_mode_names[][ETH_GSTRING_LEN];
int __ethtool_get_link(struct net_device *dev);
bool convert_legacy_settings_to_link_ksettings(
struct ethtool_link_ksettings *link_ksettings,
const struct ethtool_cmd *legacy_settings);
#endif /* _ETHTOOL_COMMON_H */
......@@ -26,6 +26,7 @@
#include <net/devlink.h>
#include <net/xdp_sock.h>
#include <net/flow_offload.h>
#include <linux/ethtool_netlink.h>
#include "common.h"
......@@ -358,54 +359,6 @@ bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
}
EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
/* return false if legacy contained non-0 deprecated fields
* maxtxpkt/maxrxpkt. rest of ksettings always updated
*/
static bool
convert_legacy_settings_to_link_ksettings(
struct ethtool_link_ksettings *link_ksettings,
const struct ethtool_cmd *legacy_settings)
{
bool retval = true;
memset(link_ksettings, 0, sizeof(*link_ksettings));
/* This is used to tell users that driver is still using these
* deprecated legacy fields, and they should not use
* %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
*/
if (legacy_settings->maxtxpkt ||
legacy_settings->maxrxpkt)
retval = false;
ethtool_convert_legacy_u32_to_link_mode(
link_ksettings->link_modes.supported,
legacy_settings->supported);
ethtool_convert_legacy_u32_to_link_mode(
link_ksettings->link_modes.advertising,
legacy_settings->advertising);
ethtool_convert_legacy_u32_to_link_mode(
link_ksettings->link_modes.lp_advertising,
legacy_settings->lp_advertising);
link_ksettings->base.speed
= ethtool_cmd_speed(legacy_settings);
link_ksettings->base.duplex
= legacy_settings->duplex;
link_ksettings->base.port
= legacy_settings->port;
link_ksettings->base.phy_address
= legacy_settings->phy_address;
link_ksettings->base.autoneg
= legacy_settings->autoneg;
link_ksettings->base.mdio_support
= legacy_settings->mdio_support;
link_ksettings->base.eth_tp_mdix
= legacy_settings->eth_tp_mdix;
link_ksettings->base.eth_tp_mdix_ctrl
= legacy_settings->eth_tp_mdix_ctrl;
return retval;
}
/* return false if ksettings link modes had higher bits
* set. legacy_settings always updated (best effort)
*/
......@@ -619,7 +572,12 @@ static int ethtool_set_link_ksettings(struct net_device *dev,
!= link_ksettings.base.link_mode_masks_nwords)
return -EINVAL;
return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
if (err >= 0) {
ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
}
return err;
}
/* Query device for its ethtool_cmd settings.
......@@ -668,6 +626,7 @@ static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
{
struct ethtool_link_ksettings link_ksettings;
struct ethtool_cmd cmd;
int ret;
ASSERT_RTNL();
......@@ -680,7 +639,12 @@ static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
return -EINVAL;
link_ksettings.base.link_mode_masks_nwords =
__ETHTOOL_LINK_MODE_MASK_NU32;
return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
if (ret >= 0) {
ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
}
return ret;
}
static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
......@@ -1401,12 +1365,12 @@ static int ethtool_nway_reset(struct net_device *dev)
static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
{
struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
int link = __ethtool_get_link(dev);
if (!dev->ethtool_ops->get_link)
return -EOPNOTSUPP;
edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
if (link < 0)
return link;
edata.data = link;
if (copy_to_user(useraddr, &edata, sizeof(edata)))
return -EFAULT;
return 0;
......
// SPDX-License-Identifier: GPL-2.0-only
#include "netlink.h"
#include "common.h"
struct linkinfo_req_info {
struct ethnl_req_info base;
};
struct linkinfo_reply_data {
struct ethnl_reply_data base;
struct ethtool_link_ksettings ksettings;
struct ethtool_link_settings *lsettings;
};
#define LINKINFO_REPDATA(__reply_base) \
container_of(__reply_base, struct linkinfo_reply_data, base)
static const struct nla_policy
linkinfo_get_policy[ETHTOOL_A_LINKINFO_MAX + 1] = {
[ETHTOOL_A_LINKINFO_UNSPEC] = { .type = NLA_REJECT },
[ETHTOOL_A_LINKINFO_HEADER] = { .type = NLA_NESTED },
[ETHTOOL_A_LINKINFO_PORT] = { .type = NLA_REJECT },
[ETHTOOL_A_LINKINFO_PHYADDR] = { .type = NLA_REJECT },
[ETHTOOL_A_LINKINFO_TP_MDIX] = { .type = NLA_REJECT },
[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL] = { .type = NLA_REJECT },
[ETHTOOL_A_LINKINFO_TRANSCEIVER] = { .type = NLA_REJECT },
};
static int linkinfo_prepare_data(const struct ethnl_req_info *req_base,
struct ethnl_reply_data *reply_base,
struct genl_info *info)
{
struct linkinfo_reply_data *data = LINKINFO_REPDATA(reply_base);
struct net_device *dev = reply_base->dev;
int ret;
data->lsettings = &data->ksettings.base;
ret = ethnl_ops_begin(dev);
if (ret < 0)
return ret;
ret = __ethtool_get_link_ksettings(dev, &data->ksettings);
if (ret < 0 && info)
GENL_SET_ERR_MSG(info, "failed to retrieve link settings");
ethnl_ops_complete(dev);
return ret;
}
static int linkinfo_reply_size(const struct ethnl_req_info *req_base,
const struct ethnl_reply_data *reply_base)
{
return nla_total_size(sizeof(u8)) /* LINKINFO_PORT */
+ nla_total_size(sizeof(u8)) /* LINKINFO_PHYADDR */
+ nla_total_size(sizeof(u8)) /* LINKINFO_TP_MDIX */
+ nla_total_size(sizeof(u8)) /* LINKINFO_TP_MDIX_CTRL */
+ nla_total_size(sizeof(u8)) /* LINKINFO_TRANSCEIVER */
+ 0;
}
static int linkinfo_fill_reply(struct sk_buff *skb,
const struct ethnl_req_info *req_base,
const struct ethnl_reply_data *reply_base)
{
const struct linkinfo_reply_data *data = LINKINFO_REPDATA(reply_base);
if (nla_put_u8(skb, ETHTOOL_A_LINKINFO_PORT, data->lsettings->port) ||
nla_put_u8(skb, ETHTOOL_A_LINKINFO_PHYADDR,
data->lsettings->phy_address) ||
nla_put_u8(skb, ETHTOOL_A_LINKINFO_TP_MDIX,
data->lsettings->eth_tp_mdix) ||
nla_put_u8(skb, ETHTOOL_A_LINKINFO_TP_MDIX_CTRL,
data->lsettings->eth_tp_mdix_ctrl) ||
nla_put_u8(skb, ETHTOOL_A_LINKINFO_TRANSCEIVER,
data->lsettings->transceiver))
return -EMSGSIZE;
return 0;
}
const struct ethnl_request_ops ethnl_linkinfo_request_ops = {
.request_cmd = ETHTOOL_MSG_LINKINFO_GET,
.reply_cmd = ETHTOOL_MSG_LINKINFO_GET_REPLY,
.hdr_attr = ETHTOOL_A_LINKINFO_HEADER,
.max_attr = ETHTOOL_A_LINKINFO_MAX,
.req_info_size = sizeof(struct linkinfo_req_info),
.reply_data_size = sizeof(struct linkinfo_reply_data),
.request_policy = linkinfo_get_policy,
.prepare_data = linkinfo_prepare_data,
.reply_size = linkinfo_reply_size,
.fill_reply = linkinfo_fill_reply,
};
/* LINKINFO_SET */
static const struct nla_policy
linkinfo_set_policy[ETHTOOL_A_LINKINFO_MAX + 1] = {
[ETHTOOL_A_LINKINFO_UNSPEC] = { .type = NLA_REJECT },
[ETHTOOL_A_LINKINFO_HEADER] = { .type = NLA_NESTED },
[ETHTOOL_A_LINKINFO_PORT] = { .type = NLA_U8 },
[ETHTOOL_A_LINKINFO_PHYADDR] = { .type = NLA_U8 },
[ETHTOOL_A_LINKINFO_TP_MDIX] = { .type = NLA_REJECT },
[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL] = { .type = NLA_U8 },
[ETHTOOL_A_LINKINFO_TRANSCEIVER] = { .type = NLA_REJECT },
};
int ethnl_set_linkinfo(struct sk_buff *skb, struct genl_info *info)
{
struct nlattr *tb[ETHTOOL_A_LINKINFO_MAX + 1];
struct ethtool_link_ksettings ksettings = {};
struct ethtool_link_settings *lsettings;
struct ethnl_req_info req_info = {};
struct net_device *dev;
bool mod = false;
int ret;
ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb,
ETHTOOL_A_LINKINFO_MAX, linkinfo_set_policy,
info->extack);
if (ret < 0)
return ret;
ret = ethnl_parse_header(&req_info, tb[ETHTOOL_A_LINKINFO_HEADER],
genl_info_net(info), info->extack, true);
if (ret < 0)
return ret;
dev = req_info.dev;
if (!dev->ethtool_ops->get_link_ksettings ||
!dev->ethtool_ops->set_link_ksettings)
return -EOPNOTSUPP;
rtnl_lock();
ret = ethnl_ops_begin(dev);
if (ret < 0)
goto out_rtnl;
ret = __ethtool_get_link_ksettings(dev, &ksettings);
if (ret < 0) {
if (info)
GENL_SET_ERR_MSG(info, "failed to retrieve link settings");
goto out_ops;
}
lsettings = &ksettings.base;
ethnl_update_u8(&lsettings->port, tb[ETHTOOL_A_LINKINFO_PORT], &mod);
ethnl_update_u8(&lsettings->phy_address, tb[ETHTOOL_A_LINKINFO_PHYADDR],
&mod);
ethnl_update_u8(&lsettings->eth_tp_mdix_ctrl,
tb[ETHTOOL_A_LINKINFO_TP_MDIX_CTRL], &mod);
ret = 0;
if (!mod)
goto out_ops;
ret = dev->ethtool_ops->set_link_ksettings(dev, &ksettings);
if (ret < 0)
GENL_SET_ERR_MSG(info, "link settings update failed");
else
ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
out_ops:
ethnl_ops_complete(dev);
out_rtnl:
rtnl_unlock();
dev_put(dev);
return ret;
}
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0-only
#include "netlink.h"
#include "common.h"
struct linkstate_req_info {
struct ethnl_req_info base;
};
struct linkstate_reply_data {
struct ethnl_reply_data base;
int link;
};
#define LINKSTATE_REPDATA(__reply_base) \
container_of(__reply_base, struct linkstate_reply_data, base)
static const struct nla_policy
linkstate_get_policy[ETHTOOL_A_LINKSTATE_MAX + 1] = {
[ETHTOOL_A_LINKSTATE_UNSPEC] = { .type = NLA_REJECT },
[ETHTOOL_A_LINKSTATE_HEADER] = { .type = NLA_NESTED },
[ETHTOOL_A_LINKSTATE_LINK] = { .type = NLA_REJECT },
};
static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
struct ethnl_reply_data *reply_base,
struct genl_info *info)
{
struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
struct net_device *dev = reply_base->dev;
int ret;
ret = ethnl_ops_begin(dev);
if (ret < 0)
return ret;
data->link = __ethtool_get_link(dev);
ethnl_ops_complete(dev);
return 0;
}
static int linkstate_reply_size(const struct ethnl_req_info *req_base,
const struct ethnl_reply_data *reply_base)
{
return nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
+ 0;
}
static int linkstate_fill_reply(struct sk_buff *skb,
const struct ethnl_req_info *req_base,
const struct ethnl_reply_data *reply_base)
{
struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
if (data->link >= 0 &&
nla_put_u8(skb, ETHTOOL_A_LINKSTATE_LINK, !!data->link))
return -EMSGSIZE;
return 0;
}
const struct ethnl_request_ops ethnl_linkstate_request_ops = {
.request_cmd = ETHTOOL_MSG_LINKSTATE_GET,
.reply_cmd = ETHTOOL_MSG_LINKSTATE_GET_REPLY,
.hdr_attr = ETHTOOL_A_LINKSTATE_HEADER,
.max_attr = ETHTOOL_A_LINKSTATE_MAX,
.req_info_size = sizeof(struct linkstate_req_info),
.reply_data_size = sizeof(struct linkstate_reply_data),
.request_policy = linkstate_get_policy,
.prepare_data = linkstate_prepare_data,
.reply_size = linkstate_reply_size,
.fill_reply = linkstate_fill_reply,
};
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment