ethtool netlink interface, preliminary patches
cover ethtool netlink interface, preliminary patches
As Jakub Kicinski suggested in ethtool netlink v7 discussion, this submission consists only of preliminary patches which raised no objections; first four patches already have Acked-by or Reviewed-by.
- patch 1 exposes permanent hardware address (as shown by "ethtool -P") via rtnetlink
- patch 2 is renames existing netlink helper to a better name
- patch 3 and 4 reorganize existing ethtool code (no functional change)
- patch 5 makes the table of link mode names available as an ethtool string set (will be needed for the netlink interface)
Once we get these out of the way, v8 of the first part of the ethtool netlink interface will follow.
Michal Kubecek (5):
rtnetlink: provide permanent hardware address in RTM_NEWLINK
netlink: rename nl80211_validate_nested() to nla_validate_nested()
ethtool: move to its own directory
ethtool: move string arrays into common file
ethtool: provide link mode names as a string set
include/linux/ethtool.h | 4 +
include/net/netlink.h | 8 +-
include/uapi/linux/ethtool.h | 2 +
include/uapi/linux/if_link.h | 1 +
net/Makefile | 2 +-
net/core/Makefile | 2 +-
net/core/rtnetlink.c | 5 +
net/ethtool/Makefile | 3 +
net/ethtool/common.c | 171 ++++++++++++++++++++++++
net/ethtool/common.h | 19 +++
net/{core/ethtool.c => ethtool/ioctl.c} | 89 +-----------
net/wireless/nl80211.c | 3 +-
12 files changed, 219 insertions(+), 90 deletions(-)
create mode 100644 net/ethtool/Makefile
create mode 100644 net/ethtool/common.c
create mode 100644 net/ethtool/common.h
rename net/{core/ethtool.c => ethtool/ioctl.c} (95%)Signature · 1 line
2.24.0
PATCH 1/5 rtnetlink: provide permanent hardware address in RTM_NEWLINK
Permanent hardware address of a network device was traditionally provided via ethtool ioctl interface but as Jiri Pirko pointed out in a review of ethtool netlink interface, rtnetlink is much more suitable for it so let's add it to the RTM_NEWLINK message.
Add IFLA_PERM_ADDRESS attribute to RTM_NEWLINK messages unless the permanent address is all zeros (i.e. device driver did not fill it). As permanent address is not modifiable, reject userspace requests containing IFLA_PERM_ADDRESS attribute.
Note: we already provide permanent hardware address for bond slaves; unfortunately we cannot drop that attribute for backward compatibility reasons.
v5 -> v6: only add the attribute if permanent address is not zero
--- include/uapi/linux/if_link.h | 1 + net/core/rtnetlink.c | 5 +++++ 2 files changed, 6 insertions(+)
include/uapi/linux/if_link.h +1 -0
@@ -169,6 +169,7 @@ enum {IFLA_MAX_MTU ,IFLA_PROP_LIST ,IFLA_ALT_IFNAME ,/* Alternative ifname */ +IFLA_PERM_ADDRESS , __IFLA_MAX };
net/core/rtnetlink.c +5 -0
@@ -1041,6 +1041,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev, +nla_total_size (4 )/* IFLA_MIN_MTU */ +nla_total_size (4 )/* IFLA_MAX_MTU */ +rtnl_prop_list_size (dev) + +nla_total_size (MAX_ADDR_LEN )/* IFLA_PERM_ADDRESS */ +0 ; }
@@ -1757,6 +1758,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) goto nla_put_failure; +if (memchr_inv (dev->perm_addr,'\0' , dev->addr_len) && +nla_put (skb,IFLA_PERM_ADDRESS , dev->addr_len, dev->perm_addr)) + goto nla_put_failure;rcu_read_lock ();if (rtnl_fill_link_af (skb, dev, ext_filter_mask))
@@ -1822,6 +1826,7 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = { [IFLA_PROP_LIST] = { .type = NLA_NESTED }, [IFLA_ALT_IFNAME ] = { .type =NLA_STRING , .len = ALTIFNAMSIZ - 1 }, + [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, };static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX +1 ] = {
Signature · 1 line
2.24.0
Johannes Berg
maintainerRe: [PATCH net-next 1/5] rtnetlink: provide permanent hardware address in RTM_NEWLINK
net/core/rtnetlink.c static int rtnl_fill_ifinfo(struct sk_buff *skb, · line 1761
Quoted 2 lines · Michal Kubecek
net/core/rtnetlink.c +1
-0+ if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
Why not simply !is_zero_ether_addr()?
johannes
Johannes Berg
maintainerRe: [PATCH net-next 1/5] rtnetlink: provide permanent hardware address in RTM_NEWLINK
Quoted 4 lines · Johannes Berg
net/core/rtnetlink.c static int rtnl_fill_ifinfo(struct sk_buff *skb, · line 1761
Quoted 1 line · Michal Kubecek
net/core/rtnetlink.c +1
-0+ if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&Why not simply !is_zero_ether_addr()?
Ugh, sorry, ignore me. I need to go sleep instead of looking at patches.
johannes
PATCH 2/5 netlink: rename nl80211_validate_nested() to nla_validate_nested()
In reply to Michal KubecekFunction nl80211_validate_nested() is not specific to nl80211, it's a counterpart to nla_validate_nested_deprecated() with strict validation. For consistency with other validation and parse functions, rename it to nla_validate_nested().
--- include/net/netlink.h | 8 ++++---- net/wireless/nl80211.c | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-)
include/net/netlink.h +4 -4
@@ -1735,7 +1735,7 @@ static inline void nla_nest_cancel(struct sk_buff *skb, struct nlattr *start) }/** -* nla_validate_nested - Validate a stream of nested attributes +* __nla_validate_nested - Validate a stream of nested attributes * @start: container attribute * @maxtype: maximum attribute type to be expected * @policy: validation policy
@@ -1758,9 +1758,9 @@ static inline int __nla_validate_nested(const struct nlattr *start, int maxtype, }static inline int -nl80211_validate_nested (const struct nlattr *start,int maxtype, -const struct nla_policy *policy, -struct netlink_ext_ack *extack) +nla_validate_nested (const struct nlattr *start,int maxtype, +const struct nla_policy *policy, +struct netlink_ext_ack *extack) {return __nla_validate_nested (start, maxtype, policy,NL_VALIDATE_STRICT , extack);
net/wireless/nl80211.c +1 -2
@@ -12900,8 +12900,7 @@ static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,return -EINVAL ; } -return nl80211_validate_nested (attr, vcmd->maxattr, vcmd->policy, - extack); +return nla_validate_nested (attr, vcmd->maxattr, vcmd->policy, extack); }static int nl80211_vendor_cmd (struct sk_buff *skb,struct genl_info *info)
Signature · 1 line
2.24.0
PATCH 3/5 ethtool: move to its own directory
In reply to Michal KubecekThe ethtool netlink interface is going to be split into multiple files so that it will be more convenient to put all of them in a separate directory net/ethtool. Start by moving current ethtool.c with ioctl interface into this directory and renaming it to ioctl.c.
---
net/Makefile | 2 +-
net/core/Makefile | 2 +-
net/ethtool/Makefile | 3 +++
net/{core/ethtool.c => ethtool/ioctl.c} | 0
4 files changed, 5 insertions(+), 2 deletions(-)
create mode 100644 net/ethtool/Makefile
rename net/{core/ethtool.c => ethtool/ioctl.c} (100%)net/Makefile +1 -1
@@ -13,7 +13,7 @@ obj-$(CONFIG_NET) += $(tmp-y) # LLC has to be linked before the files in net/802/ obj-$(CONFIG_LLC) += llc/ -obj-$(CONFIG_NET) += ethernet/ 802/ sched/ netlink/ bpf/ +obj-$(CONFIG_NET) += ethernet/ 802/ sched/ netlink/ bpf/ ethtool/ obj-$(CONFIG_NETFILTER) += netfilter/ obj-$(CONFIG_INET) += ipv4/ obj-$(CONFIG_TLS) += tls/
net/core/Makefile +1 -1
@@ -8,7 +8,7 @@ obj-y := sock.o request_sock.o skbuff.o datagram.o stream.o scm.o \ obj-$(CONFIG_SYSCTL) += sysctl_net_core.o -obj-y += dev.o ethtool.o dev_addr_lists.o dst.o netevent.o \ +obj-y += dev.o dev_addr_lists.o dst.o netevent.o \ neighbour.o rtnetlink.o utils.o link_watch.o filter.o \ sock_diag.o dev_ioctl.o tso.o sock_reuseport.o \ fib_notifier.o xdp.o flow_offload.o
net/ethtool/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y += ioctl.o
net/ethtool/ioctl.c renamed from net/core/ethtool.c +0 -0
Signature · 1 line
2.24.0
PATCH 4/5 ethtool: move string arrays into common file
In reply to Michal KubecekIntroduce file net/ethtool/common.c for code shared by ioctl and netlink ethtool interface. Move name tables of features, RSS hash functions, tunables and PHY tunables into this file.
--- net/ethtool/Makefile | 2 +- net/ethtool/common.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ net/ethtool/common.h | 17 +++++++++ net/ethtool/ioctl.c | 84 ++----------------------------------------- 4 files changed, 105 insertions(+), 83 deletions(-) create mode 100644 net/ethtool/common.c create mode 100644 net/ethtool/common.h
net/ethtool/Makefile +1 -1
@@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 -obj-y += ioctl.o +obj-y += ioctl.o common.o
net/ethtool/common.c new file +85 -0
85 lines not on this page · raw message
net/ethtool/common.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ + +#ifndef _ETHTOOL_COMMON_H +#define _ETHTOOL_COMMON_H + +#include <linux/ethtool.h> + +extern const char +netdev_features_strings[NETDEV_FEATURE_COUNT ][ETH_GSTRING_LEN ]; +extern const char +rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT ][ETH_GSTRING_LEN ]; +extern const char +tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN ]; +extern const char +phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN ]; + +#endif /* _ETHTOOL_COMMON_H */
net/ethtool/ioctl.c +2 -82
96 lines not on this page · raw message
Signature · 1 line
2.24.0
PATCH 5/5 ethtool: provide link mode names as a string set
In reply to Michal KubecekUnlike e.g. netdev features, the ethtool ioctl interface requires link mode table to be in sync between kernel and userspace for userspace to be able to display and set all link modes supported by kernel. The way arbitrary length bitsets are implemented in netlink interface, this will be no longer needed.
To allow userspace to access all link modes running kernel supports, add table of ethernet link mode names and make it available as a string set to userspace GET_STRSET requests. Add build time check to make sure names are defined for all modes declared in enum ethtool_link_mode_bit_indices.
Once the string set is available, make it also accessible via ioctl.
--- include/linux/ethtool.h | 4 ++ include/uapi/linux/ethtool.h | 2 + net/ethtool/common.c | 86 ++++++++++++++++++++++++++++++++++++ net/ethtool/common.h | 2 + net/ethtool/ioctl.c | 5 +++ 5 files changed, 99 insertions(+)
include/linux/ethtool.h +4 -0
@@ -102,6 +102,10 @@ static inline u32 ethtool_rxfh_indir_default(u32 index, u32 n_rx_rings)#define __ETHTOOL_DECLARE_LINK_MODE_MASK (name) \ DECLARE_BITMAP(name, __ETHTOOL_LINK_MODE_MASK_NBITS) +/* compose link mode index from speed, type and duplex */ +#define ETHTOOL_LINK_MODE (speed, type, duplex) \ + ETHTOOL_LINK_MODE_ ## speed ## base ## type ## _ ## duplex ## _BIT +/* drivers must ignore base.cmd and base.link_mode_masks_nwords * fields, but they are allowed to overwrite them (will be ignored). */
include/uapi/linux/ethtool.h +2 -0
@@ -593,6 +593,7 @@ struct ethtool_pauseparam {* @ETH_SS_RSS_HASH_FUNCS: RSS hush function names * @ETH_SS_PHY_STATS: Statistic names, for use with %ETHTOOL_GPHYSTATS * @ETH_SS_PHY_TUNABLES: PHY tunable names +* @ETH_SS_LINK_MODES: link mode names */ enum ethtool_stringset {ETH_SS_TEST =0 ,
@@ -604,6 +605,7 @@ enum ethtool_stringset {ETH_SS_TUNABLES ,ETH_SS_PHY_STATS ,ETH_SS_PHY_TUNABLES , + ETH_SS_LINK_MODES, };/**
net/ethtool/common.c +86 -0
89 lines not on this page · raw message
net/ethtool/common.h +2 -0
@@ -13,5 +13,7 @@ extern const char tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN ];extern const char phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN ]; +extern const char +link_mode_names[__ETHTOOL_LINK_MODE_MASK_NBITS][ETH_GSTRING_LEN ];#endif /* _ETHTOOL_COMMON_H */
net/ethtool/ioctl.c +5 -0
@@ -154,6 +154,9 @@ static int __ethtool_get_sset_count(struct net_device *dev, int sset) !ops->get_ethtool_phy_stats)return phy_ethtool_get_sset_count (dev->phydev); +if (sset ==ETH_SS_LINK_MODES ) +return __ETHTOOL_LINK_MODE_MASK_NBITS; +if (ops->get_sset_count && ops->get_strings)return ops->get_sset_count (dev, sset);else
@@ -178,6 +181,8 @@ static void __ethtool_get_strings(struct net_device *dev,else if (stringset ==ETH_SS_PHY_STATS && dev->phydev && !ops->get_ethtool_phy_stats)phy_ethtool_get_strings (dev->phydev, data); +else if (stringset ==ETH_SS_LINK_MODES ) +memcpy (data, link_mode_names,sizeof (link_mode_names));else /* ops->get_strings is valid because checked earlier */ ops->get_strings (dev, stringset, data);
Signature · 1 line
2.24.0
Andrew Lunn
maintainerRe: [PATCH net-next 5/5] ethtool: provide link mode names as a string set
Quoted 10 lines · Michal Kubecek
Unlike e.g. netdev features, the ethtool ioctl interface requires link mode table to be in sync between kernel and userspace for userspace to be able to display and set all link modes supported by kernel. The way arbitrary length bitsets are implemented in netlink interface, this will be no longer needed.
To allow userspace to access all link modes running kernel supports, add table of ethernet link mode names and make it available as a string set to userspace
GET_STRSETrequests. Add build time check to make sure names are defined for all modes declared in enumethtool_link_mode_bit_indices.
Hi Michal
Having a build time check is a good idea. However, i don't see it in the code. Please could you point it out.
Quoted 77 lines
Once the string set is available, make it also accessible via ioctl.
Signed-off-by Michal Kubecek <[email protected]>--- include/linux/ethtool.h | 4 ++ include/uapi/linux/ethtool.h | 2 + net/ethtool/common.c | 86 ++++++++++++++++++++++++++++++++++++ net/ethtool/common.h | 2 + net/ethtool/ioctl.c | 5 +++ 5 files changed, 99 insertions(+)include/linux/ethtool.h +4
-0@@ -102,6 +102,10 @@ static inline u32 ethtool_rxfh_indir_default(u32 index, u32 n_rx_rings)#define __ETHTOOL_DECLARE_LINK_MODE_MASK (name) \ DECLARE_BITMAP(name, __ETHTOOL_LINK_MODE_MASK_NBITS) +/* compose link mode index from speed, type and duplex */ +#define ETHTOOL_LINK_MODE (speed, type, duplex) \ + ETHTOOL_LINK_MODE_ ## speed ## base ## type ## _ ## duplex ## _BIT +/* drivers must ignore base.cmd and base.link_mode_masks_nwords * fields, but they are allowed to overwrite them (will be ignored). */ include/uapi/linux/ethtool.h +2
-0@@ -593,6 +593,7 @@ struct ethtool_pauseparam {* @ETH_SS_RSS_HASH_FUNCS: RSS hush function names * @ETH_SS_PHY_STATS: Statistic names, for use with %ETHTOOL_GPHYSTATS * @ETH_SS_PHY_TUNABLES: PHY tunable names +* @ETH_SS_LINK_MODES: link mode names */ enum ethtool_stringset {ETH_SS_TEST =0 ,@@ -604,6 +605,7 @@ enum ethtool_stringset {ETH_SS_TUNABLES ,ETH_SS_PHY_STATS ,ETH_SS_PHY_TUNABLES , + ETH_SS_LINK_MODES, };/** net/ethtool/common.c +22
-0@@ -83,3 +83,89 @@ phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = { [ETHTOOL_PHY_FAST_LINK_DOWN] = "phy-fast-link-down", [ETHTOOL_PHY_EDPD] = "phy-energy-detect-power-down", }; + +#define __LINK_MODE_NAME (speed, type, duplex) \ + #speed "base" #type "/" #duplex +#define __DEFINE_LINK_MODE_NAME (speed, type, duplex) \ + [ETHTOOL_LINK_MODE(speed, type, duplex)] = \ + __LINK_MODE_NAME(speed, type, duplex) +#define __DEFINE_SPECIAL_MODE_NAME (_mode, _name) \ + [ETHTOOL_LINK_MODE_ ## _mode ## _BIT] = _name + +const char +link_mode_names[__ETHTOOL_LINK_MODE_MASK_NBITS][ETH_GSTRING_LEN ] = { +__DEFINE_LINK_MODE_NAME (10 ,T , Half), +__DEFINE_LINK_MODE_NAME (10 ,T , Full), +__DEFINE_LINK_MODE_NAME (100 ,T , Half), +__DEFINE_LINK_MODE_NAME (100 ,T , Full), +__DEFINE_LINK_MODE_NAME (1000 ,T , Half), +__DEFINE_LINK_MODE_NAME (1000 ,T , Full), +__DEFINE_SPECIAL_MODE_NAME (Autoneg,"Autoneg" ), +__DEFINE_SPECIAL_MODE_NAME (TP ,"TP" ), +__DEFINE_SPECIAL_MODE_NAME (AUI ,"AUI" ), +__DEFINE_SPECIAL_MODE_NAME (MII ,"MII" ), +__DEFINE_SPECIAL_MODE_NAME (FIBRE ,"FIBRE" ),
net/ethtool/common.c phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = { · line 117
Quoted 10 lines
net/ethtool/common.c +10
-0+__DEFINE_SPECIAL_MODE_NAME (BNC ,"BNC" ), +__DEFINE_LINK_MODE_NAME (10000 ,T , Full), +__DEFINE_SPECIAL_MODE_NAME (Pause,"Pause" ), +__DEFINE_SPECIAL_MODE_NAME (Asym_Pause,"Asym_Pause" ), +__DEFINE_LINK_MODE_NAME (2500 ,X , Full), +__DEFINE_SPECIAL_MODE_NAME (Backplane,"Backplane" ), +__DEFINE_LINK_MODE_NAME (1000 ,KX , Full), +__DEFINE_LINK_MODE_NAME (10000 ,KX4 , Full), + __DEFINE_LINK_MODE_NAME(10000, KR, Full), + [ETHTOOL_LINK_MODE_10000baseR_FEC_BIT] = "10000baseR_FEC",
Would
__DEFINE_SPECIAL_MODE_NAME(10000baseR_FEC, 10000baseR_FEC),
work here?
Andrew
Re: [PATCH net-next 5/5] ethtool: provide link mode names as a string set
Quoted 12 lines · Andrew Lunn
Quoted 10 lines · Michal Kubecek
Unlike e.g. netdev features, the ethtool ioctl interface requires link mode table to be in sync between kernel and userspace for userspace to be able to display and set all link modes supported by kernel. The way arbitrary length bitsets are implemented in netlink interface, this will be no longer needed.
To allow userspace to access all link modes running kernel supports, add table of ethernet link mode names and make it available as a string set to userspace
GET_STRSETrequests. Add build time check to make sure names are defined for all modes declared in enumethtool_link_mode_bit_indices.
Quoted 4 lines
Hi Michal
Having a build time check is a good idea. However, i don't see it in the code. Please could you point it out.
Thanks for noticing. The BUILD_BUG_ON() check got lost when I moved the patch to an earlier place and changed the declaration to fixed size array like other name tables have. But you are right, losing the check would be too high price for the uniformity.
Quoted 34 lines
Quoted 21 lines
net/ethtool/common.c +21
-0+#define __LINK_MODE_NAME (speed, type, duplex) \ + #speed "base" #type "/" #duplex +#define __DEFINE_LINK_MODE_NAME (speed, type, duplex) \ + [ETHTOOL_LINK_MODE(speed, type, duplex)] = \ + __LINK_MODE_NAME(speed, type, duplex) +#define __DEFINE_SPECIAL_MODE_NAME (_mode, _name) \ + [ETHTOOL_LINK_MODE_ ## _mode ## _BIT] = _name + +const char +link_mode_names[__ETHTOOL_LINK_MODE_MASK_NBITS][ETH_GSTRING_LEN ] = { +__DEFINE_LINK_MODE_NAME (10 ,T , Half), +__DEFINE_LINK_MODE_NAME (10 ,T , Full), +__DEFINE_LINK_MODE_NAME (100 ,T , Half), +__DEFINE_LINK_MODE_NAME (100 ,T , Full), +__DEFINE_LINK_MODE_NAME (1000 ,T , Half), +__DEFINE_LINK_MODE_NAME (1000 ,T , Full), +__DEFINE_SPECIAL_MODE_NAME (Autoneg,"Autoneg" ), +__DEFINE_SPECIAL_MODE_NAME (TP ,"TP" ), +__DEFINE_SPECIAL_MODE_NAME (AUI ,"AUI" ), +__DEFINE_SPECIAL_MODE_NAME (MII ,"MII" ), +__DEFINE_SPECIAL_MODE_NAME (FIBRE ,"FIBRE" ),net/ethtool/common.c phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = { · line 117
Quoted 10 lines
net/ethtool/common.c +10
-0+__DEFINE_SPECIAL_MODE_NAME (BNC ,"BNC" ), +__DEFINE_LINK_MODE_NAME (10000 ,T , Full), +__DEFINE_SPECIAL_MODE_NAME (Pause,"Pause" ), +__DEFINE_SPECIAL_MODE_NAME (Asym_Pause,"Asym_Pause" ), +__DEFINE_LINK_MODE_NAME (2500 ,X , Full), +__DEFINE_SPECIAL_MODE_NAME (Backplane,"Backplane" ), +__DEFINE_LINK_MODE_NAME (1000 ,KX , Full), +__DEFINE_LINK_MODE_NAME (10000 ,KX4 , Full), + __DEFINE_LINK_MODE_NAME(10000, KR, Full), + [ETHTOOL_LINK_MODE_10000baseR_FEC_BIT] = "10000baseR_FEC",Would
Quoted 3 lines
__DEFINE_SPECIAL_MODE_NAME(10000baseR_FEC, 10000baseR_FEC),work here?
Right, it would (with double quotes for second argument). As I wrote the macro for bits which don't represent actual link modes, it never occured to me that it can be also used for this special case.
I'll wait until tomorrow to see if there are more comments and send v2.
Michal