Skip to content

Commit e794676

Browse files
ikhorndavem330
authored andcommitted
net: core: dev_addr_lists: add auxiliary func to handle reference address updates
In order to avoid all table update, and only remove or add new address, the auxiliary function exists, named __hw_addr_sync_dev(). It allows end driver do nothing when nothing changed and add/rm when concrete address is firstly added or lastly removed. But it doesn't include cases when an address of real device or vlan was reused by other vlans or vlan/macval devices. For handaling events when address was reused/unreused the patch adds new auxiliary routine - __hw_addr_ref_sync_dev(). It allows to do nothing when nothing was changed and do updates only for an address being added/reused/deleted/unreused. Thus, clone address changes for vlans can be mirrored in the table. The function is exclusive with __hw_addr_sync_dev(). It's responsibility of the end driver to identify address vlan device, if it needs so. Signed-off-by: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 29e1220 commit e794676

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

‎include/linux/netdevice.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4068,6 +4068,16 @@ int __hw_addr_sync_dev(struct netdev_hw_addr_list *list,
40684068
int (*sync)(struct net_device *, const unsigned char *),
40694069
int (*unsync)(struct net_device *,
40704070
const unsigned char *));
4071+
int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list,
4072+
struct net_device *dev,
4073+
int (*sync)(struct net_device *,
4074+
const unsigned char *, int),
4075+
int (*unsync)(struct net_device *,
4076+
const unsigned char *, int));
4077+
void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list *list,
4078+
struct net_device *dev,
4079+
int (*unsync)(struct net_device *,
4080+
const unsigned char *, int));
40714081
void __hw_addr_unsync_dev(struct netdev_hw_addr_list *list,
40724082
struct net_device *dev,
40734083
int (*unsync)(struct net_device *,

‎net/core/dev_addr_lists.c‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,103 @@ int __hw_addr_sync_dev(struct netdev_hw_addr_list *list,
277277
}
278278
EXPORT_SYMBOL(__hw_addr_sync_dev);
279279

280+
/**
281+
* __hw_addr_ref_sync_dev - Synchronize device's multicast address list taking
282+
* into account references
283+
* @list: address list to synchronize
284+
* @dev: device to sync
285+
* @sync: function to call if address or reference on it should be added
286+
* @unsync: function to call if address or some reference on it should removed
287+
*
288+
* This function is intended to be called from the ndo_set_rx_mode
289+
* function of devices that require explicit address or references on it
290+
* add/remove notifications. The unsync function may be NULL in which case
291+
* the addresses or references on it requiring removal will simply be
292+
* removed without any notification to the device. That is responsibility of
293+
* the driver to identify and distribute address or references on it between
294+
* internal address tables.
295+
**/
296+
int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list,
297+
struct net_device *dev,
298+
int (*sync)(struct net_device *,
299+
const unsigned char *, int),
300+
int (*unsync)(struct net_device *,
301+
const unsigned char *, int))
302+
{
303+
struct netdev_hw_addr *ha, *tmp;
304+
int err, ref_cnt;
305+
306+
/* first go through and flush out any unsynced/stale entries */
307+
list_for_each_entry_safe(ha, tmp, &list->list, list) {
308+
/* sync if address is not used */
309+
if ((ha->sync_cnt << 1) <= ha->refcount)
310+
continue;
311+
312+
/* if fails defer unsyncing address */
313+
ref_cnt = ha->refcount - ha->sync_cnt;
314+
if (unsync && unsync(dev, ha->addr, ref_cnt))
315+
continue;
316+
317+
ha->refcount = (ref_cnt << 1) + 1;
318+
ha->sync_cnt = ref_cnt;
319+
__hw_addr_del_entry(list, ha, false, false);
320+
}
321+
322+
/* go through and sync updated/new entries to the list */
323+
list_for_each_entry_safe(ha, tmp, &list->list, list) {
324+
/* sync if address added or reused */
325+
if ((ha->sync_cnt << 1) >= ha->refcount)
326+
continue;
327+
328+
ref_cnt = ha->refcount - ha->sync_cnt;
329+
err = sync(dev, ha->addr, ref_cnt);
330+
if (err)
331+
return err;
332+
333+
ha->refcount = ref_cnt << 1;
334+
ha->sync_cnt = ref_cnt;
335+
}
336+
337+
return 0;
338+
}
339+
EXPORT_SYMBOL(__hw_addr_ref_sync_dev);
340+
341+
/**
342+
* __hw_addr_ref_unsync_dev - Remove synchronized addresses and references on
343+
* it from device
344+
* @list: address list to remove synchronized addresses (references on it) from
345+
* @dev: device to sync
346+
* @unsync: function to call if address and references on it should be removed
347+
*
348+
* Remove all addresses that were added to the device by
349+
* __hw_addr_ref_sync_dev(). This function is intended to be called from the
350+
* ndo_stop or ndo_open functions on devices that require explicit address (or
351+
* references on it) add/remove notifications. If the unsync function pointer
352+
* is NULL then this function can be used to just reset the sync_cnt for the
353+
* addresses in the list.
354+
**/
355+
void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list *list,
356+
struct net_device *dev,
357+
int (*unsync)(struct net_device *,
358+
const unsigned char *, int))
359+
{
360+
struct netdev_hw_addr *ha, *tmp;
361+
362+
list_for_each_entry_safe(ha, tmp, &list->list, list) {
363+
if (!ha->sync_cnt)
364+
continue;
365+
366+
/* if fails defer unsyncing address */
367+
if (unsync && unsync(dev, ha->addr, ha->sync_cnt))
368+
continue;
369+
370+
ha->refcount -= ha->sync_cnt - 1;
371+
ha->sync_cnt = 0;
372+
__hw_addr_del_entry(list, ha, false, false);
373+
}
374+
}
375+
EXPORT_SYMBOL(__hw_addr_ref_unsync_dev);
376+
280377
/**
281378
* __hw_addr_unsync_dev - Remove synchronized addresses from device
282379
* @list: address list to remove synchronized addresses from

0 commit comments

Comments
 (0)