devlink: introduce notifications filtering
Subsystems (4)
Jiri Pirko
maintainerv5 cover devlink: introduce notifications filtering
From: Jiri Pirko <[email protected]>
Currently the user listening on a socket for devlink notifications gets always all messages for all existing devlink instances and objects, even if he is interested only in one of those. That may cause unnecessary overhead on setups with thousands of instances present.
User is currently able to narrow down the devlink objects replies to dump commands by specifying select attributes.
Allow similar approach for notifications providing user a new notify-filter-set command to select attributes with values the notification message has to match. In that case, it is delivered to the socket.
Note that the filtering is done per-socket, so multiple users may specify different selection of attributes with values.
This patchset initially introduces support for following attributes:DEVLINK_ATTR_BUS_NAMEDEVLINK_ATTR_DEV_NAMEDEVLINK_ATTR_PORT_INDEX
Patches #1 - #4 are preparations in devlink code, patch #3 is
an optimization done on the way.
Patches #5 - #7 are preparations in netlink and generic netlink code. Patch #8 is the main one in this set implementing of
the notify-filter-set command and the actual per-socket filtering.
Patch #9 extends the infrastructure allowing to filter according
to a port index.
Example:
$ devlink mon port pci/0000:08:00.0/32768
[port,new] pci/0000:08:00.0/32768: type notset flavour pcisf controller 0 pfnum 0 sfnum 107 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable
[port,new] pci/0000:08:00.0/32768: type eth flavour pcisf controller 0 pfnum 0 sfnum 107 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable
[port,new] pci/0000:08:00.0/32768: type eth netdev eth3 flavour pcisf controller 0 pfnum 0 sfnum 107 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable
[port,new] pci/0000:08:00.0/32768: type eth netdev eth3 flavour pcisf controller 0 pfnum 0 sfnum 107 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable
[port,new] pci/0000:08:00.0/32768: type eth flavour pcisf controller 0 pfnum 0 sfnum 107 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable
[port,new] pci/0000:08:00.0/32768: type notset flavour pcisf controller 0 pfnum 0 sfnum 107 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable
[port,del] pci/0000:08:00.0/32768: type notset flavour pcisf controller 0 pfnum 0 sfnum 107 splittable false
function:
hw_addr 00:00:00:00:00:00 state inactive opstate detached roce enable---v4->v5:
- converted priv pointer in netlink_sock to genl_sock container, containing xarray pointer
- introduced per-family init/destroy callbacks and priv_size to allocate per-sock private, converted devlink to that
- see patches #5 and #8 for more detailsv3->v4:
- converted from sk_user_data pointer use to nlk(sk)->priv pointer and allow priv to be stored for multiple generic netlink families, see patch #5 for more detailsv2->v3:
- small cosmetical fixes in patch #6v1->v2:
- added patch #6, fixed generated docs
- see individual patches for details
Jiri Pirko (9):
devlink: use devl_is_registered() helper instead xa_get_mark()
devlink: introduce __devl_is_registered() helper and use it instead of
xa_get_mark()
devlink: send notifications only if there are listeners
devlink: introduce a helper for netlink multicast send
genetlink: introduce per-sock family private storage
netlink: introduce typedef for filter function
genetlink: introduce helpers to do filtered multicast
devlink: add a command to set notification filter and use it for
multicasts
devlink: extend multicast filtering by port index
Documentation/netlink/specs/devlink.yaml | 11 ++ drivers/connector/connector.c | 5 +- include/linux/connector.h | 3 +- include/linux/netlink.h | 6 +- include/net/genetlink.h | 41 ++++++- include/net/netlink.h | 31 ++++- include/uapi/linux/devlink.h | 2 + net/devlink/dev.c | 13 +- net/devlink/devl_internal.h | 59 ++++++++- net/devlink/health.c | 10 +- net/devlink/linecard.c | 5 +- net/devlink/netlink.c | 116 ++++++++++++++++++ net/devlink/netlink_gen.c | 16 ++- net/devlink/netlink_gen.h | 4 +- net/devlink/param.c | 5 +- net/devlink/port.c | 8 +- net/devlink/rate.c | 5 +- net/devlink/region.c | 6 +- net/devlink/trap.c | 18 +-- net/netlink/af_netlink.c | 5 +- net/netlink/af_netlink.h | 15 +++ net/netlink/genetlink.c | 146 +++++++++++++++++++++++ 22 files changed, 477 insertions(+), 53 deletions(-)
Signature · 1 line
2.41.0
Jiri Pirko
maintainerPATCH v5 1/9 devlink: use devl_is_registered() helper instead xa_get_mark()
From: Jiri Pirko <[email protected]>
Instead of checking the xarray mark directly using xa_get_mark() helper use devl_is_registered() helper which wraps it up. Note that there are couple more users of xa_get_mark() left which are going to be handled by the next patch.
--- net/devlink/dev.c | 4 ++-- net/devlink/rate.c | 2 +- net/devlink/trap.c | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-)
net/devlink/dev.c +2 -2
@@ -202,7 +202,7 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)int err;WARN_ON (cmd !=DEVLINK_CMD_NEW && cmd !=DEVLINK_CMD_DEL ); -WARN_ON (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )); +WARN_ON (!devl_is_registered (devlink)); msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );if (!msg)
@@ -999,7 +999,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink, cmd !=DEVLINK_CMD_FLASH_UPDATE_END && cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); -if (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )) +if (!devl_is_registered (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/rate.c +1 -1
@@ -146,7 +146,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate,WARN_ON (cmd !=DEVLINK_CMD_RATE_NEW && cmd !=DEVLINK_CMD_RATE_DEL ); -if (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )) +if (!devl_is_registered (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/trap.c +6 -3
@@ -1173,7 +1173,8 @@ devlink_trap_group_notify(struct devlink *devlink,WARN_ON_ONCE (cmd !=DEVLINK_CMD_TRAP_GROUP_NEW && cmd !=DEVLINK_CMD_TRAP_GROUP_DEL ); -if (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )) + +if (!devl_is_registered (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
@@ -1234,7 +1235,8 @@ static void devlink_trap_notify(struct devlink *devlink,WARN_ON_ONCE (cmd !=DEVLINK_CMD_TRAP_NEW && cmd !=DEVLINK_CMD_TRAP_DEL ); -if (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )) + +if (!devl_is_registered (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
@@ -1710,7 +1712,8 @@ devlink_trap_policer_notify(struct devlink *devlink,WARN_ON_ONCE (cmd !=DEVLINK_CMD_TRAP_POLICER_NEW && cmd !=DEVLINK_CMD_TRAP_POLICER_DEL ); -if (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )) + +if (!devl_is_registered (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
Signature · 1 line
2.41.0
Jiri Pirko
maintainerPATCH v5 2/9 devlink: introduce __devl_is_registered() helper and use it instead of xa_get_mark()
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Introduce __devl_is_registered() which does not assert on devlink instance lock and use it in notifications which may be called without devlink instance lock held.
--- net/devlink/devl_internal.h | 7 ++++++- net/devlink/linecard.c | 2 +- net/devlink/port.c | 2 +- net/devlink/region.c | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-)
net/devlink/devl_internal.h +6 -1
@@ -91,10 +91,15 @@ extern struct genl_family devlink_nl_family;struct devlink *devlinks_xa_find_get (struct net *net,unsigned long *indexp); +static inline bool __devl_is_registered (struct devlink *devlink) +{ +return xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED ); +} +static inline bool devl_is_registered (struct devlink *devlink) {devl_assert_locked (devlink); -return xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED ); +return __devl_is_registered (devlink); }static inline void devl_dev_lock (struct devlink *devlink,bool dev_lock)
net/devlink/linecard.c +1 -1
@@ -136,7 +136,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard,WARN_ON (cmd !=DEVLINK_CMD_LINECARD_NEW && cmd !=DEVLINK_CMD_LINECARD_DEL ); -if (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )) +if (!__devl_is_registered (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/port.c +1 -1
@@ -512,7 +512,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port,WARN_ON (cmd !=DEVLINK_CMD_PORT_NEW && cmd !=DEVLINK_CMD_PORT_DEL ); -if (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )) +if (!__devl_is_registered (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/region.c +2 -1
@@ -234,7 +234,8 @@ static void devlink_nl_region_notify(struct devlink_region *region,struct sk_buff *msg;WARN_ON (cmd !=DEVLINK_CMD_REGION_NEW && cmd !=DEVLINK_CMD_REGION_DEL ); -if (!xa_get_mark (&devlinks, devlink->index,DEVLINK_REGISTERED )) + +if (!__devl_is_registered (devlink))return ; msg =devlink_nl_region_notify_build (region, snapshot, cmd,0 ,0 );
Signature · 1 line
2.41.0
Jiri Pirko
maintainerPATCH v5 3/9 devlink: send notifications only if there are listeners
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Introduce devlink_nl_notify_need() helper and using it to check at the beginning of notification functions to avoid overhead of composing notification messages in case nobody listens.
---v4->v5:
- swapped order in test in param.c
--- net/devlink/dev.c | 5 ++++- net/devlink/devl_internal.h | 6 ++++++ net/devlink/health.c | 3 +++ net/devlink/linecard.c | 2 +- net/devlink/param.c | 2 +- net/devlink/port.c | 2 +- net/devlink/rate.c | 2 +- net/devlink/region.c | 2 +- net/devlink/trap.c | 6 +++--- 9 files changed, 21 insertions(+), 9 deletions(-)
net/devlink/dev.c +4 -1
@@ -204,6 +204,9 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)WARN_ON (cmd !=DEVLINK_CMD_NEW && cmd !=DEVLINK_CMD_DEL );WARN_ON (!devl_is_registered (devlink)); +if (!devlink_nl_notify_need (devlink)) +return ; + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );if (!msg)return ;
@@ -999,7 +1002,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink, cmd !=DEVLINK_CMD_FLASH_UPDATE_END && cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS); -if (!devl_is_registered (devlink)) +if (!devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/devl_internal.h +6 -0
@@ -185,6 +185,12 @@ int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net, struct devlink *devlink, int attrtype);int devlink_nl_msg_reply_and_new (struct sk_buff **msg,struct genl_info *info); +static inline bool devlink_nl_notify_need (struct devlink *devlink) +{ +return genl_has_listeners (&devlink_nl_family,devlink_net (devlink), +DEVLINK_MCGRP_CONFIG ); +} +/* Notify */ void devlink_notify_register (struct devlink *devlink);void devlink_notify_unregister (struct devlink *devlink);
net/devlink/health.c +3 -0
@@ -496,6 +496,9 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter,WARN_ON (cmd !=DEVLINK_CMD_HEALTH_REPORTER_RECOVER );ASSERT_DEVLINK_REGISTERED (devlink); +if (!devlink_nl_notify_need (devlink)) +return ; + msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );if (!msg)return ;
net/devlink/linecard.c +1 -1
@@ -136,7 +136,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard,WARN_ON (cmd !=DEVLINK_CMD_LINECARD_NEW && cmd !=DEVLINK_CMD_LINECARD_DEL ); -if (!__devl_is_registered (devlink)) +if (!__devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/param.c +1 -1
@@ -343,7 +343,7 @@ static void devlink_param_notify(struct devlink *devlink,* will replay the notifications if the params are added/removed * outside of the lifetime of the instance. */ -if (!devl_is_registered (devlink)) +if (!devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/port.c +1 -1
@@ -512,7 +512,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port,WARN_ON (cmd !=DEVLINK_CMD_PORT_NEW && cmd !=DEVLINK_CMD_PORT_DEL ); -if (!__devl_is_registered (devlink)) +if (!__devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/rate.c +1 -1
@@ -146,7 +146,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate,WARN_ON (cmd !=DEVLINK_CMD_RATE_NEW && cmd !=DEVLINK_CMD_RATE_DEL ); -if (!devl_is_registered (devlink)) +if (!devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
net/devlink/region.c +1 -1
@@ -235,7 +235,7 @@ static void devlink_nl_region_notify(struct devlink_region *region,WARN_ON (cmd !=DEVLINK_CMD_REGION_NEW && cmd !=DEVLINK_CMD_REGION_DEL ); -if (!__devl_is_registered (devlink)) +if (!__devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =devlink_nl_region_notify_build (region, snapshot, cmd,0 ,0 );
net/devlink/trap.c +3 -3
@@ -1174,7 +1174,7 @@ devlink_trap_group_notify(struct devlink *devlink,WARN_ON_ONCE (cmd !=DEVLINK_CMD_TRAP_GROUP_NEW && cmd !=DEVLINK_CMD_TRAP_GROUP_DEL ); -if (!devl_is_registered (devlink)) +if (!devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
@@ -1236,7 +1236,7 @@ static void devlink_trap_notify(struct devlink *devlink,WARN_ON_ONCE (cmd !=DEVLINK_CMD_TRAP_NEW && cmd !=DEVLINK_CMD_TRAP_DEL ); -if (!devl_is_registered (devlink)) +if (!devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
@@ -1713,7 +1713,7 @@ devlink_trap_policer_notify(struct devlink *devlink,WARN_ON_ONCE (cmd !=DEVLINK_CMD_TRAP_POLICER_NEW && cmd !=DEVLINK_CMD_TRAP_POLICER_DEL ); -if (!devl_is_registered (devlink)) +if (!devl_is_registered (devlink) || !devlink_nl_notify_need (devlink))return ; msg =nlmsg_new (NLMSG_DEFAULT_SIZE ,GFP_KERNEL );
Signature · 1 line
2.41.0
Jiri Pirko
maintainerPATCH v5 4/9 devlink: introduce a helper for netlink multicast send
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Introduce a helper devlink_nl_notify_send() so each object notification function does not have to call genlmsg_multicast_netns() with the same arguments.
--- net/devlink/dev.c | 6 ++---- net/devlink/devl_internal.h | 7 +++++++ net/devlink/health.c | 3 +-- net/devlink/linecard.c | 3 +-- net/devlink/param.c | 3 +-- net/devlink/port.c | 3 +-- net/devlink/rate.c | 3 +-- net/devlink/region.c | 3 +-- net/devlink/trap.c | 9 +++------ 9 files changed, 18 insertions(+), 22 deletions(-)
net/devlink/dev.c +2 -4
@@ -217,8 +217,7 @@ static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), - msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); }int devlink_nl_get_doit (struct sk_buff *skb,struct genl_info *info)
@@ -1013,8 +1012,7 @@ static void __devlink_flash_update_notify(struct devlink *devlink,if (err) goto out_free_msg; -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), - msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg);return ; out_free_msg:
net/devlink/devl_internal.h +7 -0
@@ -191,6 +191,13 @@ static inline bool devlink_nl_notify_need(struct devlink *devlink) DEVLINK_MCGRP_CONFIG); } +static inline void devlink_nl_notify_send (struct devlink *devlink, +struct sk_buff *msg) +{ +genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), + msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +} +/* Notify */ void devlink_notify_register (struct devlink *devlink);void devlink_notify_unregister (struct devlink *devlink);
net/devlink/health.c +1 -2
@@ -509,8 +509,7 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter,return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), msg, -0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); }void
net/devlink/linecard.c +1 -2
@@ -150,8 +150,7 @@ static void devlink_linecard_notify(struct devlink_linecard *linecard,return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), - msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); }void devlink_linecards_notify_register (struct devlink *devlink)
net/devlink/param.c +1 -2
@@ -356,8 +356,7 @@ static void devlink_param_notify(struct devlink *devlink,return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), - msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); } static void devlink_params_notify(struct devlink *devlink,
net/devlink/port.c +1 -2
@@ -525,8 +525,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port,return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), msg, -0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); } static void devlink_ports_notify(struct devlink *devlink,
net/devlink/rate.c +1 -2
@@ -159,8 +159,7 @@ static void devlink_rate_notify(struct devlink_rate *devlink_rate,return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), msg, -0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); }void devlink_rates_notify_register (struct devlink *devlink)
net/devlink/region.c +1 -2
@@ -242,8 +242,7 @@ static void devlink_nl_region_notify(struct devlink_region *region,if (IS_ERR (msg))return ; -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), msg, -0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); }void devlink_regions_notify_register (struct devlink *devlink)
net/devlink/trap.c +3 -6
@@ -1188,8 +1188,7 @@ devlink_trap_group_notify(struct devlink *devlink,return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), - msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); }void devlink_trap_groups_notify_register (struct devlink *devlink)
@@ -1249,8 +1248,7 @@ static void devlink_trap_notify(struct devlink *devlink,return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), - msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); }void devlink_traps_notify_register (struct devlink *devlink)
@@ -1727,8 +1725,7 @@ devlink_trap_policer_notify(struct devlink *devlink,return ; } -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), - msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +devlink_nl_notify_send (devlink, msg); }void devlink_trap_policers_notify_register (struct devlink *devlink)
Signature · 1 line
2.41.0
Jiri Pirko
maintainerPATCH v5 5/9 genetlink: introduce per-sock family private storage
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Introduce a wrapper sock struct for Generic netlink and store a pointer to family privs xarray. This per socket xarray contains family->id indexed priv storage.
Note I used xarray instead of suggested linked list as it is more convenient.
Introduce genl_sk_priv_get() to get the family priv pointer and initialize it in case it does not exist.
Introduce __genl_sk_priv_get() to obtain family the priv pointer under RCU read lock.
Allow family to specify the priv size, init() and destroy() callbacks.
---v4->v5:
- s/Returns/Return/ in function comments
- introduced wrapper genl sock struct and store xarray there
- changed family helpers to genl_sk_priv_get() and __genl_sk_priv_get()
- introduced sock_priv_size for family and use this to allocate the priv in generic netlink code
- introduced init/destroy callbacks for family privsv3->v4:
- new patch
--- include/net/genetlink.h | 6 ++ net/netlink/af_netlink.c | 2 +- net/netlink/af_netlink.h | 15 ++++ net/netlink/genetlink.c | 146 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 1 deletion(-)
include/net/genetlink.h +6 -0
@@ -87,6 +87,9 @@ struct genl_family {int id;/* starting number of multicast group IDs in this family */ unsigned int mcgrp_offset; +size_t sock_priv_size; +void (*sock_priv_init)(void *priv); +void (*sock_priv_destroy)(void *priv); };/**
@@ -301,6 +304,9 @@ int genl_unregister_family(const struct genl_family *family);void genl_notify (const struct genl_family *family,struct sk_buff *skb,struct genl_info *info,u32 group,gfp_t flags); +void *__genl_sk_priv_get (struct sock *sk,struct genl_family *family); +void *genl_sk_priv_get (struct sock *sk,struct genl_family *family); +void *genlmsg_put (struct sk_buff *skb,u32 portid,u32 seq,const struct genl_family *family,int flags,u8 cmd);
net/netlink/af_netlink.c +1 -1
@@ -632,7 +632,7 @@ static void netlink_remove(struct sock *sk)static struct proto netlink_proto = { .name ="NETLINK" , .owner =THIS_MODULE , - .obj_size =sizeof (struct netlink_sock ), + .obj_size =NETLINK_SOCK_SIZE , }; static int __netlink_create(struct net *net, struct socket *sock,
net/netlink/af_netlink.h +15 -0
@@ -60,6 +60,21 @@ static inline struct netlink_sock *nlk_sk(struct sock *sk)#define nlk_test_bit (nr, sk) test_bit(NETLINK_F_##nr, &nlk_sk(sk)->flags) +struct genl_sock { +struct netlink_sock nlk_sk; +struct xarray *family_privs; +}; + +static inline struct genl_sock *genl_sk (struct sock *sk) +{ + return container_of(nlk_sk(sk), struct genl_sock, nlk_sk); +} + +/* Size of netlink sock is size of the biggest user with priv, +* which is currently just Generic Netlink. +*/ +#define NETLINK_SOCK_SIZE sizeof(struct genl_sock) +struct netlink_table {struct rhashtable hash;struct hlist_head mc_list;
net/netlink/genetlink.c +146 -0
164 lines not on this page · raw message
Signature · 1 line
2.41.0
Jakub Kicinski
maintainerRe: [patch net-next v5 5/9] genetlink: introduce per-sock family private storage
include/net/genetlink.h struct genl_family { · line 92
Quoted 11 lines · On Wed, 6 Dec 2023 19:21:16 +0100 Jiri Pirko wrote:
include/net/genetlink.h +3
-0@@ -87,6 +87,9 @@ struct genl_family {int id;/* starting number of multicast group IDs in this family */ unsigned int mcgrp_offset; +size_t sock_priv_size; +void (*sock_priv_init)(void *priv); +void (*sock_priv_destroy)(void *priv);
👍️
but I think it should be above the private fields (and have kdoc) The families are expected to make use the new fields, and are not supposed to touch anything private.
Quoted 10 lines
net/netlink/af_netlink.h +4
-0@@ -60,6 +60,21 @@ static inline struct netlink_sock *nlk_sk(struct sock *sk)#define nlk_test_bit (nr, sk) test_bit(NETLINK_F_##nr, &nlk_sk(sk)->flags) +struct genl_sock { +struct netlink_sock nlk_sk; +struct xarray *family_privs; +};
net/netlink/af_netlink.h static inline struct netlink_sock *nlk_sk(struct sock *sk) · line 76
Quoted 10 lines
net/netlink/af_netlink.h +10
-0+ +static inline struct genl_sock *genl_sk (struct sock *sk) +{ + return container_of(nlk_sk(sk), struct genl_sock, nlk_sk); +} + +/* Size of netlink sock is size of the biggest user with priv, +* which is currently just Generic Netlink. +*/ +#define NETLINK_SOCK_SIZE sizeof(struct genl_sock)
Would feel a little cleaner to me to add
#define NETLINK_SOCK_PROTO_SIZE 8
add that to the size, build time check that struct genl_sock's size is <= than sizeof(struct netlink_sock) + NETLINK_SOCK_PROTO_SIZE
This way we don't have to fumble the layering by putting genl stuff in af_netlink.h
Quoted 4 lines
net/netlink/af_netlink.h +4
-0+struct genl_sk_priv { +void (*destructor)(void *priv); +long priv[]; +};
net/netlink/af_netlink.h static inline struct netlink_sock *nlk_sk(struct sock *sk)
Quoted 10 lines
net/netlink/af_netlink.h +10
-0+ +static struct genl_sk_priv *genl_sk_priv_alloc (struct genl_family *family) +{ +struct genl_sk_priv *priv; + + priv =kzalloc (size_add (sizeof (*priv), family->sock_priv_size), +GFP_KERNEL ); +if (!priv) +return ERR_PTR (-ENOMEM ); + priv->destructor = family->sock_priv_destroy;
family->sock_priv_destroy may be in module memory. I think you need to wipe them when family goes :(
Quoted 17 lines
net/netlink/af_netlink.h +16
-0+if (family->sock_priv_init) + family->sock_priv_init (priv->priv); +return priv; +} +static struct xarray *genl_family_privs_get (struct genl_sock *gsk) +{ +struct xarray *family_privs; + +again: + family_privs =READ_ONCE (gsk->family_privs); +if (family_privs) +return family_privs; + + family_privs =kzalloc (sizeof (*family_privs),GFP_KERNEL ); +if (!family_privs) +return ERR_PTR (-ENOMEM );
net/netlink/af_netlink.h
Quoted 10 lines
net/netlink/af_netlink.h +10
-0+xa_init_flags (family_privs,XA_FLAGS_ALLOC ); + +/* Use genl lock to protect family_privs to be +* initialized in parallel by different CPU. +*/ +genl_lock (); +if (unlikely (gsk->family_privs)) { +xa_destroy (family_privs); +kfree (family_privs); +genl_unlock ();
nit: unlock can be moved up
net/netlink/af_netlink.h
Quoted 1 line
net/netlink/af_netlink.h +1
-0+ goto again;
why not return READ_ONCE(gsk->family_privs); ?
there's no need to loop
One could also be tempted to:
lock()
if (likely(!gsk->family_privs)) {
WRITE
} else {
destory()
free()
family_privs = READ
}unlock()
but it could be argued success path should be flat
Quoted 5 lines
net/netlink/af_netlink.h +5
-0+ } +WRITE_ONCE (gsk->family_privs, family_privs); +genl_unlock (); +return family_privs; +}
Jiri Pirko
maintainerRe: [patch net-next v5 5/9] genetlink: introduce per-sock family private storage
Quoted 15 lines · Fri, Dec 08, 2023 at 03:55:26AM CET, [email protected] wrote:
include/net/genetlink.h struct genl_family { · line 92
Quoted 11 lines · On Wed, 6 Dec 2023 19:21:16 +0100 Jiri Pirko wrote:
include/net/genetlink.h +3
-0@@ -87,6 +87,9 @@ struct genl_family {int id;/* starting number of multicast group IDs in this family */ unsigned int mcgrp_offset; +size_t sock_priv_size; +void (*sock_priv_init)(void *priv); +void (*sock_priv_destroy)(void *priv);👍️
Quoted 3 lines
but I think it should be above the private fields (and have kdoc) The families are expected to make use the new fields, and are not supposed to touch anything private.
Oh, right, good point, I missed that.
Quoted 24 lines
Quoted 11 lines
net/netlink/af_netlink.h +4
-0@@ -60,6 +60,21 @@ static inline struct netlink_sock *nlk_sk(struct sock *sk)#define nlk_test_bit (nr, sk) test_bit(NETLINK_F_##nr, &nlk_sk(sk)->flags) +struct genl_sock { +struct netlink_sock nlk_sk; +struct xarray *family_privs; +};net/netlink/af_netlink.h static inline struct netlink_sock *nlk_sk(struct sock *sk) · line 76
Quoted 10 lines
net/netlink/af_netlink.h +10
-0+ +static inline struct genl_sock *genl_sk (struct sock *sk) +{ + return container_of(nlk_sk(sk), struct genl_sock, nlk_sk); +} + +/* Size of netlink sock is size of the biggest user with priv, +* which is currently just Generic Netlink. +*/ +#define NETLINK_SOCK_SIZE sizeof(struct genl_sock)Would feel a little cleaner to me to add
Quoted 7 lines
#define
NETLINK_SOCK_PROTO_SIZE8add that to the size, build time check that struct genl_sock's size is <= than
sizeof(struct netlink_sock)+NETLINK_SOCK_PROTO_SIZEThis way we don't have to fumble the layering by putting genl stuff in
af_netlink.h
Yeah, I had it like that originally, I didn't like it :) Mainly because if someone adds-in another field in the future, the build time check may only fail on some archs. Also, wasting memory on archs there pointer is 4 bytes :) But as you wish, I don't mind to switch it back.
Quoted 40 lines
Quoted 5 lines
net/netlink/af_netlink.h +4
-0+struct genl_sk_priv { +void (*destructor)(void *priv); +long priv[]; +};net/netlink/af_netlink.h static inline struct netlink_sock *nlk_sk(struct sock *sk)
Quoted 10 lines
net/netlink/af_netlink.h +10
-0+ +static struct genl_sk_priv *genl_sk_priv_alloc (struct genl_family *family) +{ +struct genl_sk_priv *priv; + + priv =kzalloc (size_add (sizeof (*priv), family->sock_priv_size), +GFP_KERNEL ); +if (!priv) +return ERR_PTR (-ENOMEM ); + priv->destructor = family->sock_priv_destroy;
family->sock_priv_destroymay be in module memory. I think you need to wipe them when family goes :(Quoted 21 lines
net/netlink/af_netlink.h +20
-0+if (family->sock_priv_init) + family->sock_priv_init (priv->priv); +return priv; +} +static struct xarray *genl_family_privs_get (struct genl_sock *gsk) +{ +struct xarray *family_privs; + +again: + family_privs =READ_ONCE (gsk->family_privs); +if (family_privs) +return family_privs; + + family_privs =kzalloc (sizeof (*family_privs),GFP_KERNEL ); +if (!family_privs) +return ERR_PTR (-ENOMEM ); +xa_init_flags (family_privs,XA_FLAGS_ALLOC ); + +/* Use genl lock to protect family_privs to be +* initialized in parallel by different CPU.
Quoted 8 lines
net/netlink/af_netlink.h
Quoted 6 lines
net/netlink/af_netlink.h +6
-0+*/ +genl_lock (); +if (unlikely (gsk->family_privs)) { +xa_destroy (family_privs); +kfree (family_privs); +genl_unlock ();nit: unlock can be moved up
Okay.
Quoted 5 lines
net/netlink/af_netlink.h
Quoted 2 lines
net/netlink/af_netlink.h +1
-0+ goto again;why not return
READ_ONCE(gsk->family_privs); ?
there's no need to loop
Right.
Quoted 6 lines
One could also be tempted to:
lock()
if (likely(!gsk->family_privs)) {
WRITE
Quoted 8 lines
} else { destory() free() family_privs = READ }
unlock()but it could be argued success path should be flat
Okay, I will think about it.
Thanks!
Quoted 6 lines
Quoted 6 lines
net/netlink/af_netlink.h +5
-0+ } +WRITE_ONCE (gsk->family_privs, family_privs); +genl_unlock (); +return family_privs; +}
Jiri Pirko
maintainerRe: [patch net-next v5 5/9] genetlink: introduce per-sock family private storage
In reply to Jakub KicinskiQuoted 1 line · Fri, Dec 08, 2023 at 03:55:26AM CET, [email protected] wrote:
On Wed, 6 Dec 2023 19:21:16 +0100 Jiri Pirko wrote:
[...]
Quoted 4 lines
Quoted 4 lines
net/netlink/genetlink.c +4
-0+static struct genl_sk_priv *genl_sk_priv_alloc (struct genl_family *family) +{ +struct genl_sk_priv *priv; +
Quoted 8 lines
net/netlink/genetlink.c static int genl_bind(struct net *net, int group) · line 1717
Quoted 5 lines
net/netlink/genetlink.c +5
-0+ priv =kzalloc (size_add (sizeof (*priv), family->sock_priv_size), +GFP_KERNEL ); +if (!priv) +return ERR_PTR (-ENOMEM ); + priv->destructor = family->sock_priv_destroy;
family->sock_priv_destroymay be in module memory. I think you need to wipe them when family goes :(
Crap. That's a bit problematic. Family can unregister and register again, with user having the same sock sill opened with legitimate expectation of filter being applied. Don't see now how to handle this other then no-destroy and just kfree here in genetlink.c :/ Going back to v4?
Quoted 5 lines
Quoted 5 lines
net/netlink/genetlink.c +4
-0+if (family->sock_priv_init) + family->sock_priv_init (priv->priv); +return priv; +}
[...]
Jakub Kicinski
maintainerRe: [patch net-next v5 5/9] genetlink: introduce per-sock family private storage
Quoted 13 lines · On Fri, 8 Dec 2023 15:21:52 +0100 Jiri Pirko wrote:
Quoted 4 lines
Quoted 4 lines
net/netlink/genetlink.c +4
-0+static struct genl_sk_priv *genl_sk_priv_alloc (struct genl_family *family) +{ +struct genl_sk_priv *priv; +Quoted 8 lines
net/netlink/genetlink.c static int genl_bind(struct net *net, int group) · line 1717
Quoted 5 lines
net/netlink/genetlink.c +5
-0+ priv =kzalloc (size_add (sizeof (*priv), family->sock_priv_size), +GFP_KERNEL ); +if (!priv) +return ERR_PTR (-ENOMEM ); + priv->destructor = family->sock_priv_destroy;
family->sock_priv_destroymay be in module memory. I think you need to wipe them when family goes :(
Quoted 5 lines
Crap. That's a bit problematic. Family can unregister and register again, with user having the same sock sill opened with legitimate expectation of filter being applied. Don't see now how to handle this other then no-destroy and just kfree here in
genetlink.c:/ Going back to v4?
When family gets removed all subs must be cleared. So the user sock will have to resolve the mcast ID again, and re-subscribe again to get any notification. Having to re-sub implies having to re-add filters in my mind.
Jiri Pirko
maintainerRe: [patch net-next v5 5/9] genetlink: introduce per-sock family private storage
Quoted 20 lines · Fri, Dec 08, 2023 at 05:11:23PM CET, [email protected] wrote:
Quoted 13 lines · On Fri, 8 Dec 2023 15:21:52 +0100 Jiri Pirko wrote:
Quoted 4 lines
Quoted 4 lines
net/netlink/genetlink.c +4
-0+static struct genl_sk_priv *genl_sk_priv_alloc (struct genl_family *family) +{ +struct genl_sk_priv *priv; +Quoted 8 lines
net/netlink/genetlink.c static int genl_bind(struct net *net, int group) · line 1717
Quoted 5 lines
net/netlink/genetlink.c +5
-0+ priv =kzalloc (size_add (sizeof (*priv), family->sock_priv_size), +GFP_KERNEL ); +if (!priv) +return ERR_PTR (-ENOMEM ); + priv->destructor = family->sock_priv_destroy;
family->sock_priv_destroymay be in module memory. I think you need to wipe them when family goes :(Quoted 5 lines
Crap. That's a bit problematic. Family can unregister and register again, with user having the same sock sill opened with legitimate expectation of filter being applied. Don't see now how to handle this other then no-destroy and just kfree here in
genetlink.c:/ Going back to v4?
Quoted 4 lines
When family gets removed all subs must be cleared. So the user sock will have to resolve the mcast ID again, and re-subscribe again to get any notification. Having to re-sub implies having to re-add filters in my mind.
Okay, that sounds fine. Thanks!
Jiri Pirko
maintainerPATCH v5 6/9 netlink: introduce typedef for filter function
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Make the code using filter function a bit nicer by consolidating the filter function arguments using typedef.
---v2->v3:
- left the original .c and .h arg names and types
inconsistencies for cn_netlink_send_mult() and
netlink_broadcast_filtered()v1->v2:
- new patch
--- drivers/connector/connector.c | 5 ++--- include/linux/connector.h | 3 +-- include/linux/netlink.h | 6 ++++-- net/netlink/af_netlink.c | 3 +-- 4 files changed, 8 insertions(+), 9 deletions(-)
drivers/connector/connector.c +2 -3
@@ -59,9 +59,8 @@ static int cn_already_initialized;* both, or if both are zero then the group is looked up and sent there. */ int cn_netlink_send_mult (struct cn_msg *msg,u16 len,u32 portid,u32 __group, -gfp_t gfp_mask, -int (*filter)(struct sock *dsk,struct sk_buff *skb,void *data), -void *filter_data) +gfp_t gfp_mask,netlink_filter_fn filter, +void *filter_data) {struct cn_callback_entry *__cbq;unsigned int size;
include/linux/connector.h +1 -2
@@ -100,8 +100,7 @@ void cn_del_callback(const struct cb_id *id);*/ int cn_netlink_send_mult (struct cn_msg *msg,u16 len,u32 portid,u32 group,gfp_t gfp_mask, -int (*filter)(struct sock *dsk,struct sk_buff *skb, -void *data), +netlink_filter_fn filter,void *filter_data);/**
include/linux/netlink.h +4 -2
@@ -228,10 +228,12 @@ bool netlink_strict_get_check(struct sk_buff *skb);int netlink_unicast (struct sock *ssk,struct sk_buff *skb,__u32 portid,int nonblock);int netlink_broadcast (struct sock *ssk,struct sk_buff *skb,__u32 portid,__u32 group,gfp_t allocation); + +typedef int (*netlink_filter_fn )(struct sock *dsk,struct sk_buff *skb,void *data); +int netlink_broadcast_filtered (struct sock *ssk,struct sk_buff *skb,__u32 portid,__u32 group,gfp_t allocation, -int (*filter)(struct sock *dsk, -struct sk_buff *skb,void *data), +netlink_filter_fn filter,void *filter_data);int netlink_set_err (struct sock *ssk,__u32 portid,__u32 group,int code);int netlink_register_notifier (struct notifier_block *nb);
net/netlink/af_netlink.c +1 -2
@@ -1519,8 +1519,7 @@ static void do_one_broadcast(struct sock *sk,int netlink_broadcast_filtered (struct sock *ssk,struct sk_buff *skb,u32 portid,u32 group,gfp_t allocation, -int (*filter)(struct sock *dsk, -struct sk_buff *skb,void *data), +netlink_filter_fn filter,void *filter_data) {struct net *net =sock_net (ssk);
Signature · 1 line
2.41.0
Jiri Pirko
maintainerPATCH v5 7/9 genetlink: introduce helpers to do filtered multicast
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Currently it is possible for netlink kernel user to pass custom filter function to broadcast send function netlink_broadcast_filtered(). However, this is not exposed to multicast send and to generic netlink users.
Extend the api and introduce a netlink helper nlmsg_multicast_filtered() and a generic netlink helper genlmsg_multicast_netns_filtered() to allow generic netlink families to specify filter function while sending multicast messages.
---v1->v2:
- used netlink_filter_fn introduce by the previous patch
- added return comments to silence scripts/kernel-doc warnings
--- include/net/genetlink.h | 35 +++++++++++++++++++++++++++++++---- include/net/netlink.h | 31 +++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 8 deletions(-)
include/net/genetlink.h +31 -4
@@ -441,6 +441,35 @@ static inline void genlmsg_cancel(struct sk_buff *skb, void *hdr)nlmsg_cancel (skb, hdr -GENL_HDRLEN -NLMSG_HDRLEN ); } +/** +* genlmsg_multicast_netns_filtered - multicast a netlink message +* to a specific netns with filter +* function +* @family: the generic netlink family +* @net: the net namespace +* @skb: netlink message as socket buffer +* @portid: own netlink portid to avoid sending to yourself +* @group: offset of multicast group in groups array +* @flags: allocation flags +* @filter: filter function +* @filter_data: filter function private data +* +* Return: 0 on success, negative error code for failure. +*/ +static inline int +genlmsg_multicast_netns_filtered (const struct genl_family *family, +struct net *net,struct sk_buff *skb, +u32 portid,unsigned int group,gfp_t flags, +netlink_filter_fn filter, +void *filter_data) +{ +if (WARN_ON_ONCE (group >= family->n_mcgrps)) +return -EINVAL ; + group = family->mcgrp_offset + group; +return nlmsg_multicast_filtered (net->genl_sock, skb, portid, group, + flags, filter, filter_data); +} +/** * genlmsg_multicast_netns - multicast a netlink message to a specific netns * @family: the generic netlink family
@@ -454,10 +483,8 @@ static inline int genlmsg_multicast_netns(const struct genl_family *family, struct net *net, struct sk_buff *skb, u32 portid, unsigned int group, gfp_t flags) { - if (WARN_ON_ONCE(group >= family->n_mcgrps)) - return -EINVAL; - group = family->mcgrp_offset + group; -return nlmsg_multicast (net->genl_sock, skb, portid, group, flags); + return genlmsg_multicast_netns_filtered(family, net, skb, portid, +group ,flags ,NULL ,NULL ); }/**
include/net/netlink.h +27 -4
@@ -1087,27 +1087,50 @@ static inline void nlmsg_free(struct sk_buff *skb) }/** -* nlmsg_multicast - multicast a netlink message +* nlmsg_multicast_filtered - multicast a netlink message with filter function * @sk: netlink socket to spread messages to * @skb: netlink message as socket buffer * @portid: own netlink portid to avoid sending to yourself * @group: multicast group id * @flags: allocation flags +* @filter: filter function +* @filter_data: filter function private data +* +* Return: 0 on success, negative error code for failure. */ -static inline int nlmsg_multicast (struct sock *sk,struct sk_buff *skb, -u32 portid,unsigned int group,gfp_t flags) +static inline int nlmsg_multicast_filtered (struct sock *sk,struct sk_buff *skb, +u32 portid,unsigned int group, +gfp_t flags, +netlink_filter_fn filter, +void *filter_data) {int err;NETLINK_CB (skb).dst_group = group; - err =netlink_broadcast (sk, skb, portid, group, flags); + err =netlink_broadcast_filtered (sk, skb, portid, group, flags, + filter, filter_data);if (err >0 ) err =0 ;return err; } +/** +* nlmsg_multicast - multicast a netlink message +* @sk: netlink socket to spread messages to +* @skb: netlink message as socket buffer +* @portid: own netlink portid to avoid sending to yourself +* @group: multicast group id +* @flags: allocation flags +*/ +static inline int nlmsg_multicast (struct sock *sk,struct sk_buff *skb, +u32 portid,unsigned int group,gfp_t flags) +{ +return nlmsg_multicast_filtered (sk, skb, portid, group, flags, +NULL ,NULL ); +} +/** * nlmsg_unicast - unicast a netlink message * @sk: netlink socket to spread message to
Signature · 1 line
2.41.0
Jiri Pirko
maintainerPATCH v5 8/9 devlink: add a command to set notification filter and use it for multicasts
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Currently the user listening on a socket for devlink notifications gets always all messages for all existing instances, even if he is interested only in one of those. That may cause unnecessary overhead on setups with thousands of instances present.
User is currently able to narrow down the devlink objects replies to dump commands by specifying select attributes.
Allow similar approach for notifications. Introduce a new devlink NOTIFY_FILTER_SET which the user passes the select attributes. Store these per-socket and use them for filtering messages during multicast send.
---v4->v5:
- rebased on top of generic netlink per sock per family pointer allocation code
- changed the flt to be stored in family priv rcu pointer, protected by spin lock
- changed to use size_add() helper for kzalloc() size computation
- removed generated userspace bitsv3->v4:
- rebased on top of genl_sk_priv_*() introduction
--- Documentation/netlink/specs/devlink.yaml | 10 +++ include/uapi/linux/devlink.h | 2 + net/devlink/devl_internal.h | 34 ++++++- net/devlink/netlink.c | 108 +++++++++++++++++++++++ net/devlink/netlink_gen.c | 15 +++- net/devlink/netlink_gen.h | 4 +- 6 files changed, 169 insertions(+), 4 deletions(-)
Documentation/netlink/specs/devlink.yaml +10 -0
@@ -2055,3 +2055,13 @@ operations: - bus-name - dev-name - selftests + + - + name: notify-filter-set + doc: Set notification messages socket filter. + attribute-set: devlink + do: + request: + attributes: + - bus-name + - dev-name
include/uapi/linux/devlink.h +2 -0
@@ -139,6 +139,8 @@ enum devlink_command {DEVLINK_CMD_SELFTESTS_GET ,/* can dump */ DEVLINK_CMD_SELFTESTS_RUN , +DEVLINK_CMD_NOTIFY_FILTER_SET , +/* add new commands above here */ __DEVLINK_CMD_MAX,DEVLINK_CMD_MAX = __DEVLINK_CMD_MAX -1
net/devlink/devl_internal.h +32 -2
@@ -191,11 +191,41 @@ static inline bool devlink_nl_notify_need(struct devlink *devlink) DEVLINK_MCGRP_CONFIG); } +struct devlink_obj_desc { +struct rcu_head rcu; +const char *bus_name; +const char *dev_name; +long data[]; +}; + +static inline void devlink_nl_obj_desc_init (struct devlink_obj_desc *desc, +struct devlink *devlink) +{ +memset (desc,0 ,sizeof (*desc)); + desc->bus_name = devlink->dev->bus->name; + desc->dev_name =dev_name (devlink->dev); +} + +int devlink_nl_notify_filter (struct sock *dsk,struct sk_buff *skb,void *data); + +static inline void devlink_nl_notify_send_desc (struct devlink *devlink, +struct sk_buff *msg, +struct devlink_obj_desc *desc) +{ +genlmsg_multicast_netns_filtered (&devlink_nl_family, +devlink_net (devlink), + msg,0 ,DEVLINK_MCGRP_CONFIG , +GFP_KERNEL , + devlink_nl_notify_filter, desc); +} +static inline void devlink_nl_notify_send (struct devlink *devlink,struct sk_buff *msg) { -genlmsg_multicast_netns (&devlink_nl_family,devlink_net (devlink), - msg,0 ,DEVLINK_MCGRP_CONFIG ,GFP_KERNEL ); +struct devlink_obj_desc desc; + +devlink_nl_obj_desc_init (&desc, devlink); +devlink_nl_notify_send_desc (devlink, msg, &desc); }/* Notify */
net/devlink/netlink.c +108 -0
118 lines not on this page · raw message
net/devlink/netlink_gen.c +14 -1
@@ -560,8 +560,14 @@ static const struct nla_policy devlink_selftests_run_nl_policy[DEVLINK_ATTR_SELF [DEVLINK_ATTR_SELFTESTS] = NLA_POLICY_NESTED(devlink_dl_selftest_id_nl_policy), }; +/* DEVLINK_CMD_NOTIFY_FILTER_SET - do */ +static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_DEV_NAME +1 ] = { + [DEVLINK_ATTR_BUS_NAME ] = { .type =NLA_NUL_STRING , }, + [DEVLINK_ATTR_DEV_NAME ] = { .type =NLA_NUL_STRING , }, +}; +/* Ops table for devlink */ -const struct genl_split_ops devlink_nl_ops[73 ] = { +const struct genl_split_ops devlink_nl_ops[74 ] = { { .cmd =DEVLINK_CMD_GET , .validate =GENL_DONT_VALIDATE_STRICT ,
@@ -1233,4 +1239,11 @@ const struct genl_split_ops devlink_nl_ops[73] = { .maxattr = DEVLINK_ATTR_SELFTESTS, .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO, }, + { + .cmd = DEVLINK_CMD_NOTIFY_FILTER_SET, + .doit = devlink_nl_notify_filter_set_doit, + .policy = devlink_notify_filter_set_nl_policy, + .maxattr = DEVLINK_ATTR_DEV_NAME, + .flags = GENL_CMD_CAP_DO, + }, };
net/devlink/netlink_gen.h +3 -1
@@ -16,7 +16,7 @@ extern const struct nla_policy devlink_dl_port_function_nl_policy[DEVLINK_PORT_Fextern const struct nla_policy devlink_dl_selftest_id_nl_policy[DEVLINK_ATTR_SELFTEST_ID_FLASH +1 ];/* Ops table for devlink */ -extern const struct genl_split_ops devlink_nl_ops[73 ]; +extern const struct genl_split_ops devlink_nl_ops[74 ];int devlink_nl_pre_doit (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info);
@@ -142,5 +142,7 @@ int devlink_nl_selftests_get_doit(struct sk_buff *skb, struct genl_info *info);int devlink_nl_selftests_get_dumpit (struct sk_buff *skb,struct netlink_callback *cb);int devlink_nl_selftests_run_doit (struct sk_buff *skb,struct genl_info *info); +int devlink_nl_notify_filter_set_doit (struct sk_buff *skb, +struct genl_info *info);#endif /* _LINUX_DEVLINK_GEN_H */
Signature · 1 line
2.41.0
Jiri Pirko
maintainerPATCH v5 9/9 devlink: extend multicast filtering by port index
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Expose the previously introduced notification multicast messages filtering infrastructure and allow the user to select messages using port index.
---v4->v5:
- removed generated userspace bitsv3->v4:
- rebased on top of genl_sk_priv_*() introduction
--- Documentation/netlink/specs/devlink.yaml | 1 + net/devlink/devl_internal.h | 9 +++++++++ net/devlink/health.c | 6 +++++- net/devlink/netlink.c | 10 +++++++++- net/devlink/netlink_gen.c | 5 +++-- net/devlink/port.c | 5 ++++- 6 files changed, 31 insertions(+), 5 deletions(-)
Documentation/netlink/specs/devlink.yaml +1 -0
@@ -2065,3 +2065,4 @@ operations: attributes: - bus-name - dev-name + - port-index
net/devlink/devl_internal.h +9 -0
@@ -195,6 +195,8 @@ struct devlink_obj_desc {struct rcu_head rcu;const char *bus_name;const char *dev_name; +unsigned int port_index; +bool port_index_valid;long data[]; };
@@ -206,6 +208,13 @@ static inline void devlink_nl_obj_desc_init(struct devlink_obj_desc *desc, desc->dev_name =dev_name (devlink->dev); } +static inline void devlink_nl_obj_desc_port_set (struct devlink_obj_desc *desc, +struct devlink_port *devlink_port) +{ + desc->port_index = devlink_port->index; + desc->port_index_valid = true; +} +int devlink_nl_notify_filter (struct sock *dsk,struct sk_buff *skb,void *data); static inline void devlink_nl_notify_send_desc(struct devlink *devlink,
net/devlink/health.c +5 -1
@@ -490,6 +490,7 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter, enum devlink_command cmd) {struct devlink *devlink = reporter->devlink; +struct devlink_obj_desc desc;struct sk_buff *msg;int err;
@@ -509,7 +510,10 @@ static void devlink_recover_notify(struct devlink_health_reporter *reporter,return ; } -devlink_nl_notify_send (devlink, msg); +devlink_nl_obj_desc_init (&desc, devlink); +if (reporter->devlink_port) +devlink_nl_obj_desc_port_set (&desc, reporter->devlink_port); +devlink_nl_notify_send_desc (devlink, msg, &desc); }void
net/devlink/netlink.c +9 -1
@@ -73,8 +73,13 @@ int devlink_nl_notify_filter_set_doit(struct sk_buff *skb, flt->dev_name = pos; } +if (attrs[DEVLINK_ATTR_PORT_INDEX ]) { + flt->port_index =nla_get_u32 (attrs[DEVLINK_ATTR_PORT_INDEX ]); + flt->port_index_valid = true; + } +/* Don't attach empty filter. */ -if (!flt->bus_name && !flt->dev_name) { +if (!flt->bus_name && !flt->dev_name && !flt->port_index_valid) {kfree (flt); flt =NULL ; }
@@ -101,6 +106,9 @@ static bool devlink_obj_desc_match(const struct devlink_obj_desc *desc,if (desc->dev_name && flt->dev_name &&strcmp (desc->dev_name, flt->dev_name))return false; +if (desc->port_index_valid && flt->port_index_valid && + desc->port_index != flt->port_index) +return false;return true; }
net/devlink/netlink_gen.c +3 -2
@@ -561,9 +561,10 @@ static const struct nla_policy devlink_selftests_run_nl_policy[DEVLINK_ATTR_SELF };/* DEVLINK_CMD_NOTIFY_FILTER_SET - do */ -static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_DEV_NAME +1 ] = { +static const struct nla_policy devlink_notify_filter_set_nl_policy[DEVLINK_ATTR_PORT_INDEX +1 ] = { [DEVLINK_ATTR_BUS_NAME ] = { .type =NLA_NUL_STRING , }, [DEVLINK_ATTR_DEV_NAME ] = { .type =NLA_NUL_STRING , }, + [DEVLINK_ATTR_PORT_INDEX ] = { .type =NLA_U32 , }, };/* Ops table for devlink */
@@ -1243,7 +1244,7 @@ const struct genl_split_ops devlink_nl_ops[74] = { .cmd = DEVLINK_CMD_NOTIFY_FILTER_SET, .doit = devlink_nl_notify_filter_set_doit, .policy = devlink_notify_filter_set_nl_policy, - .maxattr = DEVLINK_ATTR_DEV_NAME, + .maxattr = DEVLINK_ATTR_PORT_INDEX, .flags = GENL_CMD_CAP_DO, }, };
net/devlink/port.c +4 -1
@@ -507,6 +507,7 @@ static void devlink_port_notify(struct devlink_port *devlink_port, enum devlink_command cmd) {struct devlink *devlink = devlink_port->devlink; +struct devlink_obj_desc desc;struct sk_buff *msg;int err;
@@ -525,7 +526,9 @@ static void devlink_port_notify(struct devlink_port *devlink_port,return ; } -devlink_nl_notify_send (devlink, msg); +devlink_nl_obj_desc_init (&desc, devlink); +devlink_nl_obj_desc_port_set (&desc, devlink_port); +devlink_nl_notify_send_desc (devlink, msg, &desc); } static void devlink_ports_notify(struct devlink *devlink,
Signature · 1 line
2.41.0