netlink: provide an ability to set default extack message
Subsystems (5)
PATCH v3 1/1 netlink: provide an ability to set default extack message
From: Leon Romanovsky <[email protected]>
In netdev common pattern, extack pointer is forwarded to the drivers to be filled with error message. However, the caller can easily overwrite the filled message.
Instead of adding multiple "if (!extack->_msg)" checks before any NL_SET_ERR_MSG() call, which appears after call to the driver, let's add new macro to common code.
[1] https://lore.kernel.org/all/Y9Irgrgf3uxOjwUm@unreal
---
Changelog:
v3:
* Changed From field
v2: https://lore.kernel.org/all/c1a88f471a8aa6d780dde690e76b73ba15618b6d.1675010984.git.leon@kernel.org
* Removed () brackets around msg to fix compilation error.
v1: https://lore.kernel.org/all/d4843760219f20367c27472f084bd8aa729cf321.1674995155.git.leon@kernel.org
* Added special *_WEAK() macro instead of embedding same check in NL_SET_ERR_MSG_MOD/NL_SET_ERR_MSG_FMT.
* Reuse same macro for XFRM code which triggered this patch.
v0: https://lore.kernel.org/all/2919eb55e2e9b92265a3ba600afc8137a901ae5f.1674760340.git.leon@kernel.org
--- include/linux/netlink.h | 10 ++++++++++ net/bridge/br_switchdev.c | 10 ++++------ net/dsa/master.c | 4 +--- net/dsa/slave.c | 4 +--- net/xfrm/xfrm_device.c | 5 ++++- 5 files changed, 20 insertions(+), 13 deletions(-)
include/linux/netlink.h +10 -0
@@ -130,6 +130,16 @@ struct netlink_ext_ack { #define NL_SET_ERR_MSG_FMT_MOD(extack, fmt, args...) \ NL_SET_ERR_MSG_FMT((extack), KBUILD_MODNAME ": " fmt, ##args) +#define NL_SET_ERR_MSG_WEAK (extack, msg) do { \ + if ((extack) && !(extack)->_msg) \ + NL_SET_ERR_MSG((extack), msg); \ +} while (0) + +#define NL_SET_ERR_MSG_WEAK_MOD (extack, msg) do { \ + if ((extack) && !(extack)->_msg) \ + NL_SET_ERR_MSG_MOD((extack), msg); \ +} while (0) +#define NL_SET_BAD_ATTR_POLICY (extack, attr, pol) do { \ if ((extack)) { \ (extack)->bad_attr = (attr); \
net/bridge/br_switchdev.c +4 -6
@@ -104,9 +104,8 @@ int br_switchdev_set_port_flag(struct net_bridge_port *p,return 0 ;if (err) { -if (extack && !extack->_msg) -NL_SET_ERR_MSG_MOD (extack, -"bridge flag offload is not supported" ); +NL_SET_ERR_MSG_WEAK_MOD (extack, +"bridge flag offload is not supported" );return -EOPNOTSUPP ; }
@@ -115,9 +114,8 @@ int br_switchdev_set_port_flag(struct net_bridge_port *p, err =switchdev_port_attr_set (p->dev, &attr, extack);if (err) { -if (extack && !extack->_msg) -NL_SET_ERR_MSG_MOD (extack, -"error setting offload flag on port" ); +NL_SET_ERR_MSG_WEAK_MOD (extack, +"error setting offload flag on port" );return err; }
net/dsa/master.c +1 -3
@@ -464,9 +464,7 @@ int dsa_master_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp, err =dsa_port_lag_join (cpu_dp, lag_dev, uinfo, extack);if (err) { -if (extack && !extack->_msg) -NL_SET_ERR_MSG_MOD (extack, -"CPU port failed to join LAG" ); +NL_SET_ERR_MSG_WEAK_MOD (extack,"CPU port failed to join LAG" ); goto out_master_teardown; }
net/dsa/slave.c +1 -3
@@ -2692,9 +2692,7 @@ static int dsa_slave_changeupper(struct net_device *dev,if (!err)dsa_bridge_mtu_normalization (dp);if (err == -EOPNOTSUPP ) { -if (extack && !extack->_msg) -NL_SET_ERR_MSG_MOD (extack, -"Offloading not supported" ); +NL_SET_ERR_MSG_WEAK_MOD (extack,"Offloading not supported" ); err =0 ; } err =notifier_from_errno (err);
net/xfrm/xfrm_device.c +4 -1
@@ -325,8 +325,10 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,* authors to do not return -EOPNOTSUPP in packet offload mode. */ WARN_ON (err == -EOPNOTSUPP && is_packet_offload); -if (err != -EOPNOTSUPP || is_packet_offload) +if (err != -EOPNOTSUPP || is_packet_offload) { +NL_SET_ERR_MSG_WEAK (extack,"Device failed to offload this state" );return err; + } }return 0 ;
@@ -388,6 +390,7 @@ int xfrm_dev_policy_add(struct net *net, struct xfrm_policy *xp, xdo->type =XFRM_DEV_OFFLOAD_UNSPECIFIED ; xdo->dir =0 ;netdev_put (dev, &xdo->dev_tracker); +NL_SET_ERR_MSG_WEAK (extack,"Device failed to offload this policy" );return err; }
Signature · 1 line
2.39.1
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Tue, 31 Jan 2023 15:31:57 +0200 you wrote:
From: Leon Romanovsky <[email protected]>
In netdev common pattern, extack pointer is forwarded to the drivers to be filled with error message. However, the caller can easily overwrite the filled message.
Instead of adding multiple "if (!extack->_msg)" checks before any
NL_SET_ERR_MSG()call, which appears after call to the driver, let's add new macro to common code.[...]
Here is the summary with links:
- [net-next,v3] netlink: provide an ability to set default extack message
https://git.kernel.org/netdev/net-next/c/028fb19c6ba7
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html