Add and use neigh_parms_lookup_dev()
Subsystems (3)
v2 cover Add and use neigh_parms_lookup_dev()
This series follows up on a previous submission where it was suggested that neigh_parms_lookup_dev() be accompanied by its users.
Patch 1 adds neigh_parms_lookup_dev() to expose per-device neigh_parms lookup outside of the neighbour subsystem.
Patch 2 updates bonding to reuse an existing neigh_setup() callback from the slave's neigh_parms when available, while preserving the existing ndo_neigh_setup() fallback path.
v2:
- Convert the previous submission into a patch series
- Add bonding user of neigh_parms_lookup_dev() as requested.
Previous post's link:
https://lore.kernel.org/netdev/CAAVpQUBf+asQukcRw7sJz6vS2VdeNO5+Q5ucoCxf4JgK25nZ7g@mail.gmail.com/T/#t
Paritosh Potukuchi (2):
net: neighbour: add neigh_parms_lookup_dev() helper
bonding: reuse neigh_setup from slave neigh_parms
drivers/net/bonding/bond_main.c | 10 +++++++++- include/net/neighbour.h | 2 ++ net/core/neighbour.c | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
PATCH v2 1/2 net: neighbour: add neigh_parms_lookup_dev() helper
Provide a helper to lookup neigh_parms associated with a given (neigh_table, net_device) pair.
The existing lookup_neigh_parms() helper is internal to the neighbour subsystem and cannot be used by other subsystems. Some stacked/virtual devices like bond require access to the underlying device's neigh_parms.
neigh_parms_lookup_dev() is designed to be a wrapper around lookup_neigh_parms(). The function provides controlled access to per device neigh_parms.
The caller is expected to hold rcu_read_lock().
This does not break any existing functionality.
--- include/net/neighbour.h | 2 ++ net/core/neighbour.c | 8 ++++++++ 2 files changed, 10 insertions(+)
include/net/neighbour.h +2 -0
@@ -438,6 +438,8 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, proc_handler *proc_handler);void neigh_sysctl_unregister (struct neigh_parms *p); +struct neigh_parms *neigh_parms_lookup_dev (struct neigh_table *tbl,struct net_device *dev); +static inline void __neigh_parms_put (struct neigh_parms *parms) {refcount_dec (&parms->refcnt);
net/core/neighbour.c +8 -0
@@ -1757,6 +1757,14 @@ static inline struct neigh_parms *lookup_neigh_parms(struct neigh_table *tbl,return NULL ; } +/* Caller must hold rcu_read_lock()*/ + +struct neigh_parms *neigh_parms_lookup_dev (struct neigh_table *tbl,struct net_device *dev) +{ +return lookup_neigh_parms (tbl,dev_net (dev), dev->ifindex); +} +EXPORT_SYMBOL (neigh_parms_lookup_dev); +struct neigh_parms *neigh_parms_alloc (struct net_device *dev,struct neigh_table *tbl) {
Signature · 1 line
2.43.0
PATCH v2 2/2 bonding: reuse neigh_setup from slave neigh_parms
In reply to Paritosh Potukuchibond_neigh_init() currently relies on the slave device's ndo_neigh_setup() callback to obtain a neigh_setup() handler.
When an initialized neigh_parms instance already exists for the slave device, reuse the neigh_setup() callback stored in it instead of invoking ndo_neigh_setup() again.
If no neigh_parms instance is found, or no neigh_setup() callback is present, retain the existing ndo_neigh_setup() fallback path.
This avoids unnecessary ndo_neigh_setup() invocations while preserving existing behaviour.
--- drivers/net/bonding/bond_main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/bonding/bond_main.c +9 -1
@@ -4719,7 +4719,7 @@ static int bond_neigh_init(struct neighbour *n) {struct bonding *bond =netdev_priv (n->dev);const struct net_device_ops *slave_ops; -struct neigh_parms parms; +struct neigh_parms parms, *p;struct slave *slave;int ret =0 ;
@@ -4727,6 +4727,14 @@ static int bond_neigh_init(struct neighbour *n) slave =bond_first_slave_rcu (bond);if (!slave) goto out; + + p =neigh_parms_lookup_dev (n->tbl, slave->dev); + +if (p && p->neigh_setup) { + ret = p->neigh_setup (n); + goto out; + } + slave_ops = slave->dev->netdev_ops;if (!slave_ops->ndo_neigh_setup) goto out;
Signature · 1 line
2.43.0
Re: [PATCH v2 2/2] bonding: reuse neigh_setup from slave neigh_parms
From: Paritosh Potukuchi <[email protected]>
Date: Wed, 1 Jul 2026 08:16:00 +0000
Quoted 32 lines
bond_neigh_init()currently relies on the slave device'sndo_neigh_setup()callback to obtain aneigh_setup()handler.When an initialized
neigh_parmsinstance already exists for the slave device, reuse theneigh_setup()callback stored in it instead of invokingndo_neigh_setup()again.If no
neigh_parmsinstance is found, or noneigh_setup()callback is present, retain the existingndo_neigh_setup()fallback path.This avoids unnecessary
ndo_neigh_setup()invocations while preserving existing behaviour.Signed-off-by Paritosh Potukuchi <[email protected]>--- drivers/net/bonding/bond_main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)drivers/net/bonding/bond_main.c +1
-1@@ -4719,7 +4719,7 @@ static int bond_neigh_init(struct neighbour *n) {struct bonding *bond =netdev_priv (n->dev);const struct net_device_ops *slave_ops; -struct neigh_parms parms; +struct neigh_parms parms, *p;struct slave *slave;int ret =0 ;
drivers/net/bonding/bond_main.c static int bond_neigh_init(struct neighbour *n) · line 4731
Quoted 5 lines
drivers/net/bonding/bond_main.c +2
-0@@ -4727,6 +4727,14 @@ static int bond_neigh_init(struct neighbour *n) slave =bond_first_slave_rcu (bond);if (!slave) goto out; + + p =neigh_parms_lookup_dev (n->tbl, slave->dev);
This introduces O(n) list traversal while it can be done with fixed costs (3 dereferences + 1 call).
Since neigh_table is global (arp_tbl or nd_tbl), the O(n) list traversal could take longer and rather de-optimise.
Quoted 11 lines
drivers/net/bonding/bond_main.c +6
-0+ +if (p && p->neigh_setup) { + ret = p->neigh_setup (n); + goto out; + } + slave_ops = slave->dev->netdev_ops;if (!slave_ops->ndo_neigh_setup) goto out;Signature · 1 line
2.43.0
Re: [PATCH v2 2/2] bonding: reuse neigh_setup from slave neigh_parms
Hi Kuniyuki,
Quoted 5 lines
This introduces O(n) list traversal while it can be done with fixed costs (3 dereferences + 1 call).
Since
neigh_tableis global (arp_tblornd_tbl), the O(n) list traversal could take longer and rather de-optimise.
Yes, that is true. One reason why I chose to do that is because ndo_neigh_setup is a function primarily meant to setup the neigh_parms structure, when neigh_parms does not exist.
On the other hand, lookup_neigh_parms is meant to search for a near-complete neigh_parms structure, that is already associated with a netdev.
Even if we want to use ndo_neigh_setup, since it takes less time, I would suggest using it as a fallback to not finding an already existing parms, setup.
Moreover, time complexity might not be an issue in this path since, this is rarely used aggresively.
One issue with ndo_neigh_setup in bond-like devices is that, to get the underlying netdevs neigh_setup function, it expects us to pass a dummy neigh_parms structure that has been zeroed out. This seems to be fragile as suggested in a TODO in bond_neigh_init().
Generally its main goal is to fill the parms.neigh_setup field.
Can we populate a few more fields in the zeroed- out parms structure, before passing to the driver in ndo_neigh_setup? That seems to be a much safer approach.
Regards,
Paritosh
Re: [PATCH v2 2/2] bonding: reuse neigh_setup from slave neigh_parms
Quoted 20 lines · Paritosh Potukuchi
Hi Kuniyuki,
Quoted 5 lines
This introduces O(n) list traversal while it can be done with fixed costs (3 dereferences + 1 call).
Since
neigh_tableis global (arp_tblornd_tbl), the O(n) list traversal could take longer and rather de-optimise.Yes, that is true. One reason why I chose to do that is because
ndo_neigh_setupis a function primarily meant to setup theneigh_parmsstructure, whenneigh_parmsdoes not exist.
On the other hand,lookup_neigh_parmsis meant to search for a near-completeneigh_parmsstructure, that is already associated with a netdev.
Even if we want to usendo_neigh_setup, since it takes less time, I would suggest using it as a fallback to not finding an already existing parms, setup.
Quoted 2 lines
Moreover, time complexity might not be an issue in this path since, this is rarely used aggresively.
The real user is qeth_l3_main.c only and it's not compiled on 99% host.
We usually bail out at if (!slave_ops->ndo_neigh_setup), which is called after your neigh_parms_lookup_dev(), and there is no need to do O(n) traversal.
With 2K netns, it could incur unnecessary 4K traversal (lo
+ another dev) for each neigh creation, which is done under RTNL or from interrupt context.
So, it will be a problem.
Quoted 10 lines
One issue with
ndo_neigh_setupin bond-like devices is that, to get the underlying netdevsneigh_setupfunction, it expects us to pass a dummyneigh_parmsstructure that has been zeroed out. This seems to be fragile as suggested in a TODO inbond_neigh_init().
Generally its main goal is to fill the parms.neigh_setup field.
Quoted 3 lines
Can we populate a few more fields in the zeroed- out parms structure, before passing to the driver in
ndo_neigh_setup? That seems to be a much safer approach.
It does not help. Just populating fields does not change the loop detection logic.
Re: [PATCH v2 2/2] bonding: reuse neigh_setup from slave neigh_parms
Hi Kuniyuki,
Quoted 7 lines
The real user is
qeth_l3_main.conly and it's not compiled on 99% host.We usually bail out at if (!slave_ops->ndo_neigh_setup), which is called after your
neigh_parms_lookup_dev(), and there is no need to do O(n) traversal.
Quoted 5 lines
With 2K netns, it could incur unnecessary 4K traversal (lo
+ another dev) for each neigh creation, which is done under RTNL or from interrupt context.So, it will be a problem.
That makes sense. Thanks for the insight.
Quoted 2 lines
It does not help. Just populating fields does not change the loop detection logic.
True. Just populating fields would not suffice. Do you think it would a good solution to modify the ndo_neigh_setup function?
Currently it takes a (netdev, parms) pair as an argument.
The way the ndo_neigh_setup is being used , both in stacked/ virtual devices and slave devices, is to expose the underlying netdev's local neigh_setup function, through the parms argument.
There is a TODO in the bond_main.c file that suggests the following:
/* TODO: find another way [1] to implement this.
* Passing a zeroed structure is fragile,
* but at least we do not pass garbage.
*
* [1] One way would be that ndo_neigh_setup() never touch
* struct neigh_parms, but propagate the new neigh_setup()
* back to ___neigh_create() / neigh_parms_alloc()
*/
As the TODO suggests, would it be a good idea to modify the ndo_neigh_setup function to be able to return the netdev's local neigh_setup function.
This would solve the problem of passing a dummy parms structure.
typedef int (*neigh_setup_fn_t)(struct neighbour *);
static neigh_setup_fn_t ndo_neigh_setup(struct net_device *dev)
{
return qeth_l3_neigh_setup_noarp;
}Though the problem with this approach is that ndo_neigh_setup loses the flexibility to modify other fields in the parms structure.
What is your opinion on this?