Skip to content

Commit 4cd729b

Browse files
Vlad Yasevichdavem330
authored andcommitted
net: add dev_uc_sync_multiple() and dev_mc_sync_multiple() api
The current implementation of dev_uc_sync/unsync() assumes that there is a strict 1-to-1 relationship between the source and destination of the sync. In other words, once an address has been synced to a destination device, it will not be synced to any other device through the sync API. However, there are some virtual devices that aggreate a number of lower devices and need to sync addresses to all of them. The current API falls short there. This patch introduces a new dev_uc_sync_multiple() api that can be called in the above circumstances and allows sync to work for every invocation. CC: Jiri Pirko <jiri@resnulli.us> Signed-off-by: Vlad Yasevich <vyasevic@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0022d2d commit 4cd729b

2 files changed

Lines changed: 171 additions & 42 deletions

File tree

‎include/linux/netdevice.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ struct netdev_hw_addr {
209209
#define NETDEV_HW_ADDR_T_UNICAST 4
210210
#define NETDEV_HW_ADDR_T_MULTICAST 5
211211
bool global_use;
212+
int sync_cnt;
212213
int refcount;
213214
int synced;
214215
struct rcu_head rcu_head;
@@ -2627,6 +2628,7 @@ extern int dev_uc_add(struct net_device *dev, const unsigned char *addr);
26272628
extern int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr);
26282629
extern int dev_uc_del(struct net_device *dev, const unsigned char *addr);
26292630
extern int dev_uc_sync(struct net_device *to, struct net_device *from);
2631+
extern int dev_uc_sync_multiple(struct net_device *to, struct net_device *from);
26302632
extern void dev_uc_unsync(struct net_device *to, struct net_device *from);
26312633
extern void dev_uc_flush(struct net_device *dev);
26322634
extern void dev_uc_init(struct net_device *dev);
@@ -2638,6 +2640,7 @@ extern int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr);
26382640
extern int dev_mc_del(struct net_device *dev, const unsigned char *addr);
26392641
extern int dev_mc_del_global(struct net_device *dev, const unsigned char *addr);
26402642
extern int dev_mc_sync(struct net_device *to, struct net_device *from);
2643+
extern int dev_mc_sync_multiple(struct net_device *to, struct net_device *from);
26412644
extern void dev_mc_unsync(struct net_device *to, struct net_device *from);
26422645
extern void dev_mc_flush(struct net_device *dev);
26432646
extern void dev_mc_init(struct net_device *dev);

‎net/core/dev_addr_lists.c‎

Lines changed: 168 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
static int __hw_addr_create_ex(struct netdev_hw_addr_list *list,
2424
const unsigned char *addr, int addr_len,
25-
unsigned char addr_type, bool global)
25+
unsigned char addr_type, bool global,
26+
bool sync)
2627
{
2728
struct netdev_hw_addr *ha;
2829
int alloc_size;
@@ -37,7 +38,7 @@ static int __hw_addr_create_ex(struct netdev_hw_addr_list *list,
3738
ha->type = addr_type;
3839
ha->refcount = 1;
3940
ha->global_use = global;
40-
ha->synced = 0;
41+
ha->synced = sync;
4142
list_add_tail_rcu(&ha->list, &list->list);
4243
list->count++;
4344

@@ -46,7 +47,7 @@ static int __hw_addr_create_ex(struct netdev_hw_addr_list *list,
4647

4748
static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
4849
const unsigned char *addr, int addr_len,
49-
unsigned char addr_type, bool global)
50+
unsigned char addr_type, bool global, bool sync)
5051
{
5152
struct netdev_hw_addr *ha;
5253

@@ -63,43 +64,62 @@ static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
6364
else
6465
ha->global_use = true;
6566
}
67+
if (sync) {
68+
if (ha->synced)
69+
return 0;
70+
else
71+
ha->synced = true;
72+
}
6673
ha->refcount++;
6774
return 0;
6875
}
6976
}
7077

71-
return __hw_addr_create_ex(list, addr, addr_len, addr_type, global);
78+
return __hw_addr_create_ex(list, addr, addr_len, addr_type, global,
79+
sync);
7280
}
7381

7482
static int __hw_addr_add(struct netdev_hw_addr_list *list,
7583
const unsigned char *addr, int addr_len,
7684
unsigned char addr_type)
7785
{
78-
return __hw_addr_add_ex(list, addr, addr_len, addr_type, false);
86+
return __hw_addr_add_ex(list, addr, addr_len, addr_type, false, false);
87+
}
88+
89+
static int __hw_addr_del_entry(struct netdev_hw_addr_list *list,
90+
struct netdev_hw_addr *ha, bool global,
91+
bool sync)
92+
{
93+
if (global && !ha->global_use)
94+
return -ENOENT;
95+
96+
if (sync && !ha->synced)
97+
return -ENOENT;
98+
99+
if (global)
100+
ha->global_use = false;
101+
102+
if (sync)
103+
ha->synced = false;
104+
105+
if (--ha->refcount)
106+
return 0;
107+
list_del_rcu(&ha->list);
108+
kfree_rcu(ha, rcu_head);
109+
list->count--;
110+
return 0;
79111
}
80112

81113
static int __hw_addr_del_ex(struct netdev_hw_addr_list *list,
82114
const unsigned char *addr, int addr_len,
83-
unsigned char addr_type, bool global)
115+
unsigned char addr_type, bool global, bool sync)
84116
{
85117
struct netdev_hw_addr *ha;
86118

87119
list_for_each_entry(ha, &list->list, list) {
88120
if (!memcmp(ha->addr, addr, addr_len) &&
89-
(ha->type == addr_type || !addr_type)) {
90-
if (global) {
91-
if (!ha->global_use)
92-
break;
93-
else
94-
ha->global_use = false;
95-
}
96-
if (--ha->refcount)
97-
return 0;
98-
list_del_rcu(&ha->list);
99-
kfree_rcu(ha, rcu_head);
100-
list->count--;
101-
return 0;
102-
}
121+
(ha->type == addr_type || !addr_type))
122+
return __hw_addr_del_entry(list, ha, global, sync);
103123
}
104124
return -ENOENT;
105125
}
@@ -108,7 +128,57 @@ static int __hw_addr_del(struct netdev_hw_addr_list *list,
108128
const unsigned char *addr, int addr_len,
109129
unsigned char addr_type)
110130
{
111-
return __hw_addr_del_ex(list, addr, addr_len, addr_type, false);
131+
return __hw_addr_del_ex(list, addr, addr_len, addr_type, false, false);
132+
}
133+
134+
static int __hw_addr_sync_one(struct netdev_hw_addr_list *to_list,
135+
struct netdev_hw_addr *ha,
136+
int addr_len)
137+
{
138+
int err;
139+
140+
err = __hw_addr_add_ex(to_list, ha->addr, addr_len, ha->type,
141+
false, true);
142+
if (err)
143+
return err;
144+
ha->sync_cnt++;
145+
ha->refcount++;
146+
147+
return 0;
148+
}
149+
150+
static void __hw_addr_unsync_one(struct netdev_hw_addr_list *to_list,
151+
struct netdev_hw_addr_list *from_list,
152+
struct netdev_hw_addr *ha,
153+
int addr_len)
154+
{
155+
int err;
156+
157+
err = __hw_addr_del_ex(to_list, ha->addr, addr_len, ha->type,
158+
false, true);
159+
if (err)
160+
return;
161+
ha->sync_cnt--;
162+
__hw_addr_del_entry(from_list, ha, false, true);
163+
}
164+
165+
static int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list,
166+
struct netdev_hw_addr_list *from_list,
167+
int addr_len)
168+
{
169+
int err = 0;
170+
struct netdev_hw_addr *ha, *tmp;
171+
172+
list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
173+
if (ha->sync_cnt == ha->refcount) {
174+
__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
175+
} else {
176+
err = __hw_addr_sync_one(to_list, ha, addr_len);
177+
if (err)
178+
break;
179+
}
180+
}
181+
return err;
112182
}
113183

114184
int __hw_addr_add_multiple(struct netdev_hw_addr_list *to_list,
@@ -152,6 +222,11 @@ void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
152222
}
153223
EXPORT_SYMBOL(__hw_addr_del_multiple);
154224

225+
/* This function only works where there is a strict 1-1 relationship
226+
* between source and destionation of they synch. If you ever need to
227+
* sync addresses to more then 1 destination, you need to use
228+
* __hw_addr_sync_multiple().
229+
*/
155230
int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
156231
struct netdev_hw_addr_list *from_list,
157232
int addr_len)
@@ -160,17 +235,12 @@ int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
160235
struct netdev_hw_addr *ha, *tmp;
161236

162237
list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
163-
if (!ha->synced) {
164-
err = __hw_addr_add(to_list, ha->addr,
165-
addr_len, ha->type);
238+
if (!ha->sync_cnt) {
239+
err = __hw_addr_sync_one(to_list, ha, addr_len);
166240
if (err)
167241
break;
168-
ha->synced++;
169-
ha->refcount++;
170-
} else if (ha->refcount == 1) {
171-
__hw_addr_del(to_list, ha->addr, addr_len, ha->type);
172-
__hw_addr_del(from_list, ha->addr, addr_len, ha->type);
173-
}
242+
} else if (ha->refcount == 1)
243+
__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
174244
}
175245
return err;
176246
}
@@ -183,13 +253,8 @@ void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
183253
struct netdev_hw_addr *ha, *tmp;
184254

185255
list_for_each_entry_safe(ha, tmp, &from_list->list, list) {
186-
if (ha->synced) {
187-
__hw_addr_del(to_list, ha->addr,
188-
addr_len, ha->type);
189-
ha->synced--;
190-
__hw_addr_del(from_list, ha->addr,
191-
addr_len, ha->type);
192-
}
256+
if (ha->sync_cnt)
257+
__hw_addr_unsync_one(to_list, from_list, ha, addr_len);
193258
}
194259
}
195260
EXPORT_SYMBOL(__hw_addr_unsync);
@@ -406,7 +471,7 @@ int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
406471
}
407472
}
408473
err = __hw_addr_create_ex(&dev->uc, addr, dev->addr_len,
409-
NETDEV_HW_ADDR_T_UNICAST, true);
474+
NETDEV_HW_ADDR_T_UNICAST, true, false);
410475
if (!err)
411476
__dev_set_rx_mode(dev);
412477
out:
@@ -469,7 +534,8 @@ EXPORT_SYMBOL(dev_uc_del);
469534
* locked by netif_addr_lock_bh.
470535
*
471536
* This function is intended to be called from the dev->set_rx_mode
472-
* function of layered software devices.
537+
* function of layered software devices. This function assumes that
538+
* addresses will only ever be synced to the @to devices and no other.
473539
*/
474540
int dev_uc_sync(struct net_device *to, struct net_device *from)
475541
{
@@ -487,6 +553,36 @@ int dev_uc_sync(struct net_device *to, struct net_device *from)
487553
}
488554
EXPORT_SYMBOL(dev_uc_sync);
489555

556+
/**
557+
* dev_uc_sync_multiple - Synchronize device's unicast list to another
558+
* device, but allow for multiple calls to sync to multiple devices.
559+
* @to: destination device
560+
* @from: source device
561+
*
562+
* Add newly added addresses to the destination device and release
563+
* addresses that have been deleted from the source. The source device
564+
* must be locked by netif_addr_lock_bh.
565+
*
566+
* This function is intended to be called from the dev->set_rx_mode
567+
* function of layered software devices. It allows for a single source
568+
* device to be synced to multiple destination devices.
569+
*/
570+
int dev_uc_sync_multiple(struct net_device *to, struct net_device *from)
571+
{
572+
int err = 0;
573+
574+
if (to->addr_len != from->addr_len)
575+
return -EINVAL;
576+
577+
netif_addr_lock_nested(to);
578+
err = __hw_addr_sync_multiple(&to->uc, &from->uc, to->addr_len);
579+
if (!err)
580+
__dev_set_rx_mode(to);
581+
netif_addr_unlock(to);
582+
return err;
583+
}
584+
EXPORT_SYMBOL(dev_uc_sync_multiple);
585+
490586
/**
491587
* dev_uc_unsync - Remove synchronized addresses from the destination device
492588
* @to: destination device
@@ -559,7 +655,7 @@ int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
559655
}
560656
}
561657
err = __hw_addr_create_ex(&dev->mc, addr, dev->addr_len,
562-
NETDEV_HW_ADDR_T_MULTICAST, true);
658+
NETDEV_HW_ADDR_T_MULTICAST, true, false);
563659
if (!err)
564660
__dev_set_rx_mode(dev);
565661
out:
@@ -575,7 +671,7 @@ static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
575671

576672
netif_addr_lock_bh(dev);
577673
err = __hw_addr_add_ex(&dev->mc, addr, dev->addr_len,
578-
NETDEV_HW_ADDR_T_MULTICAST, global);
674+
NETDEV_HW_ADDR_T_MULTICAST, global, false);
579675
if (!err)
580676
__dev_set_rx_mode(dev);
581677
netif_addr_unlock_bh(dev);
@@ -615,7 +711,7 @@ static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
615711

616712
netif_addr_lock_bh(dev);
617713
err = __hw_addr_del_ex(&dev->mc, addr, dev->addr_len,
618-
NETDEV_HW_ADDR_T_MULTICAST, global);
714+
NETDEV_HW_ADDR_T_MULTICAST, global, false);
619715
if (!err)
620716
__dev_set_rx_mode(dev);
621717
netif_addr_unlock_bh(dev);
@@ -678,6 +774,36 @@ int dev_mc_sync(struct net_device *to, struct net_device *from)
678774
}
679775
EXPORT_SYMBOL(dev_mc_sync);
680776

777+
/**
778+
* dev_mc_sync_multiple - Synchronize device's unicast list to another
779+
* device, but allow for multiple calls to sync to multiple devices.
780+
* @to: destination device
781+
* @from: source device
782+
*
783+
* Add newly added addresses to the destination device and release
784+
* addresses that have no users left. The source device must be
785+
* locked by netif_addr_lock_bh.
786+
*
787+
* This function is intended to be called from the ndo_set_rx_mode
788+
* function of layered software devices. It allows for a single
789+
* source device to be synced to multiple destination devices.
790+
*/
791+
int dev_mc_sync_multiple(struct net_device *to, struct net_device *from)
792+
{
793+
int err = 0;
794+
795+
if (to->addr_len != from->addr_len)
796+
return -EINVAL;
797+
798+
netif_addr_lock_nested(to);
799+
err = __hw_addr_sync(&to->mc, &from->mc, to->addr_len);
800+
if (!err)
801+
__dev_set_rx_mode(to);
802+
netif_addr_unlock(to);
803+
return err;
804+
}
805+
EXPORT_SYMBOL(dev_mc_sync_multiple);
806+
681807
/**
682808
* dev_mc_unsync - Remove synchronized addresses from the destination device
683809
* @to: destination device

0 commit comments

Comments
 (0)