aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2013-09-26 16:02:19 -0400
committerDavid S. Miller <davem@davemloft.net>2013-09-26 16:02:19 -0400
commitaae8c287e664d49df4aa315ad263c33b9a2af3e1 (patch)
treedc71bea5d018fdcc1f68af27ea8af52be02628e5
parent4ed377e36ec2f385484d12e516faf88516fad31c (diff)
parent5831d66e8097aedfa3bc35941cf265ada2352317 (diff)
Merge branch 'bonding_neighbours'
bonding: use neighbours instead of own lists Veaceslav Falico says: ==================== This patchset introduces all the needed infrastructure, on top of current adjacent lists, to be able to remove bond's slave_list/slave->list. The overhead in memory/CPU is minimal, and after the patchset bonding can rely on its slave-related functions, given the proper locking. I've done some netperf benchmarks on a vm, and the delta was about 0.1gbps for 35gbps as a whole, so no speed fluctuations. It also automatically creates lower/upper and master symlinks in dev's sysfs directory. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--drivers/net/bonding/bond_3ad.c54
-rw-r--r--drivers/net/bonding/bond_alb.c81
-rw-r--r--drivers/net/bonding/bond_alb.h4
-rw-r--r--drivers/net/bonding/bond_main.c296
-rw-r--r--drivers/net/bonding/bond_procfs.c5
-rw-r--r--drivers/net/bonding/bond_sysfs.c62
-rw-r--r--drivers/net/bonding/bonding.h100
-rw-r--r--drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c3
-rw-r--r--include/linux/netdevice.h55
-rw-r--r--net/8021q/vlan.c18
-rw-r--r--net/core/dev.c404
11 files changed, 638 insertions, 444 deletions
diff --git a/drivers/net/bonding/bond_3ad.c b/drivers/net/bonding/bond_3ad.c
index 0d8f427ade93..1337eafe4311 100644
--- a/drivers/net/bonding/bond_3ad.c
+++ b/drivers/net/bonding/bond_3ad.c
@@ -2117,7 +2117,7 @@ void bond_3ad_state_machine_handler(struct work_struct *work)
2117 read_lock(&bond->lock); 2117 read_lock(&bond->lock);
2118 2118
2119 //check if there are any slaves 2119 //check if there are any slaves
2120 if (list_empty(&bond->slave_list)) 2120 if (!bond_has_slaves(bond))
2121 goto re_arm; 2121 goto re_arm;
2122 2122
2123 // check if agg_select_timer timer after initialize is timed out 2123 // check if agg_select_timer timer after initialize is timed out
@@ -2417,14 +2417,15 @@ int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2417 2417
2418int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev) 2418int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2419{ 2419{
2420 struct slave *slave, *start_at;
2421 struct bonding *bond = netdev_priv(dev); 2420 struct bonding *bond = netdev_priv(dev);
2422 int slave_agg_no; 2421 struct slave *slave, *first_ok_slave;
2423 int slaves_in_agg; 2422 struct aggregator *agg;
2424 int agg_id;
2425 int i;
2426 struct ad_info ad_info; 2423 struct ad_info ad_info;
2424 struct list_head *iter;
2425 int slaves_in_agg;
2426 int slave_agg_no;
2427 int res = 1; 2427 int res = 1;
2428 int agg_id;
2428 2429
2429 read_lock(&bond->lock); 2430 read_lock(&bond->lock);
2430 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) { 2431 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
@@ -2437,20 +2438,28 @@ int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2437 agg_id = ad_info.aggregator_id; 2438 agg_id = ad_info.aggregator_id;
2438 2439
2439 if (slaves_in_agg == 0) { 2440 if (slaves_in_agg == 0) {
2440 /*the aggregator is empty*/
2441 pr_debug("%s: Error: active aggregator is empty\n", dev->name); 2441 pr_debug("%s: Error: active aggregator is empty\n", dev->name);
2442 goto out; 2442 goto out;
2443 } 2443 }
2444 2444
2445 slave_agg_no = bond->xmit_hash_policy(skb, slaves_in_agg); 2445 slave_agg_no = bond->xmit_hash_policy(skb, slaves_in_agg);
2446 first_ok_slave = NULL;
2446 2447
2447 bond_for_each_slave(bond, slave) { 2448 bond_for_each_slave(bond, slave, iter) {
2448 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator; 2449 agg = SLAVE_AD_INFO(slave).port.aggregator;
2450 if (!agg || agg->aggregator_identifier != agg_id)
2451 continue;
2449 2452
2450 if (agg && (agg->aggregator_identifier == agg_id)) { 2453 if (slave_agg_no >= 0) {
2454 if (!first_ok_slave && SLAVE_IS_OK(slave))
2455 first_ok_slave = slave;
2451 slave_agg_no--; 2456 slave_agg_no--;
2452 if (slave_agg_no < 0) 2457 continue;
2453 break; 2458 }
2459
2460 if (SLAVE_IS_OK(slave)) {
2461 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2462 goto out;
2454 } 2463 }
2455 } 2464 }
2456 2465
@@ -2460,20 +2469,10 @@ int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2460 goto out; 2469 goto out;
2461 } 2470 }
2462 2471
2463 start_at = slave; 2472 /* we couldn't find any suitable slave after the agg_no, so use the
2464 2473 * first suitable found, if found. */
2465 bond_for_each_slave_from(bond, slave, i, start_at) { 2474 if (first_ok_slave)
2466 int slave_agg_id = 0; 2475 res = bond_dev_queue_xmit(bond, skb, first_ok_slave->dev);
2467 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
2468
2469 if (agg)
2470 slave_agg_id = agg->aggregator_identifier;
2471
2472 if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
2473 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2474 break;
2475 }
2476 }
2477 2476
2478out: 2477out:
2479 read_unlock(&bond->lock); 2478 read_unlock(&bond->lock);
@@ -2515,11 +2514,12 @@ int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2515void bond_3ad_update_lacp_rate(struct bonding *bond) 2514void bond_3ad_update_lacp_rate(struct bonding *bond)
2516{ 2515{
2517 struct port *port = NULL; 2516 struct port *port = NULL;
2517 struct list_head *iter;
2518 struct slave *slave; 2518 struct slave *slave;
2519 int lacp_fast; 2519 int lacp_fast;
2520 2520
2521 lacp_fast = bond->params.lacp_fast; 2521 lacp_fast = bond->params.lacp_fast;
2522 bond_for_each_slave(bond, slave) { 2522 bond_for_each_slave(bond, slave, iter) {
2523 port = &(SLAVE_AD_INFO(slave).port); 2523 port = &(SLAVE_AD_INFO(slave).port);
2524 __get_state_machine_lock(port); 2524 __get_state_machine_lock(port);
2525 if (lacp_fast) 2525 if (lacp_fast)
diff --git a/drivers/net/bonding/bond_alb.c b/drivers/net/bonding/bond_alb.c
index f428ef574372..e96041816b5b 100644
--- a/drivers/net/bonding/bond_alb.c
+++ b/drivers/net/bonding/bond_alb.c
@@ -223,13 +223,14 @@ static long long compute_gap(struct slave *slave)
223static struct slave *tlb_get_least_loaded_slave(struct bonding *bond) 223static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
224{ 224{
225 struct slave *slave, *least_loaded; 225 struct slave *slave, *least_loaded;
226 struct list_head *iter;
226 long long max_gap; 227 long long max_gap;
227 228
228 least_loaded = NULL; 229 least_loaded = NULL;
229 max_gap = LLONG_MIN; 230 max_gap = LLONG_MIN;
230 231
231 /* Find the slave with the largest gap */ 232 /* Find the slave with the largest gap */
232 bond_for_each_slave(bond, slave) { 233 bond_for_each_slave(bond, slave, iter) {
233 if (SLAVE_IS_OK(slave)) { 234 if (SLAVE_IS_OK(slave)) {
234 long long gap = compute_gap(slave); 235 long long gap = compute_gap(slave);
235 236
@@ -382,30 +383,31 @@ out:
382static struct slave *rlb_next_rx_slave(struct bonding *bond) 383static struct slave *rlb_next_rx_slave(struct bonding *bond)
383{ 384{
384 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 385 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
385 struct slave *rx_slave, *slave, *start_at; 386 struct slave *before = NULL, *rx_slave = NULL, *slave;
386 int i = 0; 387 struct list_head *iter;
387 388 bool found = false;
388 if (bond_info->next_rx_slave)
389 start_at = bond_info->next_rx_slave;
390 else
391 start_at = bond_first_slave(bond);
392
393 rx_slave = NULL;
394 389
395 bond_for_each_slave_from(bond, slave, i, start_at) { 390 bond_for_each_slave(bond, slave, iter) {
396 if (SLAVE_IS_OK(slave)) { 391 if (!SLAVE_IS_OK(slave))
397 if (!rx_slave) { 392 continue;
398 rx_slave = slave; 393 if (!found) {
399 } else if (slave->speed > rx_slave->speed) { 394 if (!before || before->speed < slave->speed)
395 before = slave;
396 } else {
397 if (!rx_slave || rx_slave->speed < slave->speed)
400 rx_slave = slave; 398 rx_slave = slave;
401 }
402 } 399 }
400 if (slave == bond_info->rx_slave)
401 found = true;
403 } 402 }
403 /* we didn't find anything after the current or we have something
404 * better before and up to the current slave
405 */
406 if (!rx_slave || (before && rx_slave->speed < before->speed))
407 rx_slave = before;
404 408
405 if (rx_slave) { 409 if (rx_slave)
406 slave = bond_next_slave(bond, rx_slave); 410 bond_info->rx_slave = rx_slave;
407 bond_info->next_rx_slave = slave;
408 }
409 411
410 return rx_slave; 412 return rx_slave;
411} 413}
@@ -1019,7 +1021,7 @@ static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
1019 1021
1020 /* loop through vlans and send one packet for each */ 1022 /* loop through vlans and send one packet for each */
1021 rcu_read_lock(); 1023 rcu_read_lock();
1022 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) { 1024 netdev_for_each_all_upper_dev_rcu(bond->dev, upper, iter) {
1023 if (upper->priv_flags & IFF_802_1Q_VLAN) 1025 if (upper->priv_flags & IFF_802_1Q_VLAN)
1024 alb_send_lp_vid(slave, mac_addr, 1026 alb_send_lp_vid(slave, mac_addr,
1025 vlan_dev_vlan_id(upper)); 1027 vlan_dev_vlan_id(upper));
@@ -1172,10 +1174,11 @@ static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *sla
1172 */ 1174 */
1173static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave) 1175static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1174{ 1176{
1175 struct slave *tmp_slave1, *free_mac_slave = NULL;
1176 struct slave *has_bond_addr = bond->curr_active_slave; 1177 struct slave *has_bond_addr = bond->curr_active_slave;
1178 struct slave *tmp_slave1, *free_mac_slave = NULL;
1179 struct list_head *iter;
1177 1180
1178 if (list_empty(&bond->slave_list)) { 1181 if (!bond_has_slaves(bond)) {
1179 /* this is the first slave */ 1182 /* this is the first slave */
1180 return 0; 1183 return 0;
1181 } 1184 }
@@ -1196,7 +1199,7 @@ static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slav
1196 /* The slave's address is equal to the address of the bond. 1199 /* The slave's address is equal to the address of the bond.
1197 * Search for a spare address in the bond for this slave. 1200 * Search for a spare address in the bond for this slave.
1198 */ 1201 */
1199 bond_for_each_slave(bond, tmp_slave1) { 1202 bond_for_each_slave(bond, tmp_slave1, iter) {
1200 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) { 1203 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
1201 /* no slave has tmp_slave1's perm addr 1204 /* no slave has tmp_slave1's perm addr
1202 * as its curr addr 1205 * as its curr addr
@@ -1246,15 +1249,16 @@ static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slav
1246 */ 1249 */
1247static int alb_set_mac_address(struct bonding *bond, void *addr) 1250static int alb_set_mac_address(struct bonding *bond, void *addr)
1248{ 1251{
1249 char tmp_addr[ETH_ALEN]; 1252 struct slave *slave, *rollback_slave;
1250 struct slave *slave; 1253 struct list_head *iter;
1251 struct sockaddr sa; 1254 struct sockaddr sa;
1255 char tmp_addr[ETH_ALEN];
1252 int res; 1256 int res;
1253 1257
1254 if (bond->alb_info.rlb_enabled) 1258 if (bond->alb_info.rlb_enabled)
1255 return 0; 1259 return 0;
1256 1260
1257 bond_for_each_slave(bond, slave) { 1261 bond_for_each_slave(bond, slave, iter) {
1258 /* save net_device's current hw address */ 1262 /* save net_device's current hw address */
1259 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN); 1263 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1260 1264
@@ -1274,10 +1278,12 @@ unwind:
1274 sa.sa_family = bond->dev->type; 1278 sa.sa_family = bond->dev->type;
1275 1279
1276 /* unwind from head to the slave that failed */ 1280 /* unwind from head to the slave that failed */
1277 bond_for_each_slave_continue_reverse(bond, slave) { 1281 bond_for_each_slave(bond, rollback_slave, iter) {
1278 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN); 1282 if (rollback_slave == slave)
1279 dev_set_mac_address(slave->dev, &sa); 1283 break;
1280 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN); 1284 memcpy(tmp_addr, rollback_slave->dev->dev_addr, ETH_ALEN);
1285 dev_set_mac_address(rollback_slave->dev, &sa);
1286 memcpy(rollback_slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1281 } 1287 }
1282 1288
1283 return res; 1289 return res;
@@ -1458,11 +1464,12 @@ void bond_alb_monitor(struct work_struct *work)
1458 struct bonding *bond = container_of(work, struct bonding, 1464 struct bonding *bond = container_of(work, struct bonding,
1459 alb_work.work); 1465 alb_work.work);
1460 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 1466 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1467 struct list_head *iter;
1461 struct slave *slave; 1468 struct slave *slave;
1462 1469
1463 read_lock(&bond->lock); 1470 read_lock(&bond->lock);
1464 1471
1465 if (list_empty(&bond->slave_list)) { 1472 if (!bond_has_slaves(bond)) {
1466 bond_info->tx_rebalance_counter = 0; 1473 bond_info->tx_rebalance_counter = 0;
1467 bond_info->lp_counter = 0; 1474 bond_info->lp_counter = 0;
1468 goto re_arm; 1475 goto re_arm;
@@ -1480,7 +1487,7 @@ void bond_alb_monitor(struct work_struct *work)
1480 */ 1487 */
1481 read_lock(&bond->curr_slave_lock); 1488 read_lock(&bond->curr_slave_lock);
1482 1489
1483 bond_for_each_slave(bond, slave) 1490 bond_for_each_slave(bond, slave, iter)
1484 alb_send_learning_packets(slave, slave->dev->dev_addr); 1491 alb_send_learning_packets(slave, slave->dev->dev_addr);
1485 1492
1486 read_unlock(&bond->curr_slave_lock); 1493 read_unlock(&bond->curr_slave_lock);
@@ -1493,7 +1500,7 @@ void bond_alb_monitor(struct work_struct *work)
1493 1500
1494 read_lock(&bond->curr_slave_lock); 1501 read_lock(&bond->curr_slave_lock);
1495 1502
1496 bond_for_each_slave(bond, slave) { 1503 bond_for_each_slave(bond, slave, iter) {
1497 tlb_clear_slave(bond, slave, 1); 1504 tlb_clear_slave(bond, slave, 1);
1498 if (slave == bond->curr_active_slave) { 1505 if (slave == bond->curr_active_slave) {
1499 SLAVE_TLB_INFO(slave).load = 1506 SLAVE_TLB_INFO(slave).load =
@@ -1599,13 +1606,13 @@ int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1599 */ 1606 */
1600void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave) 1607void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1601{ 1608{
1602 if (!list_empty(&bond->slave_list)) 1609 if (bond_has_slaves(bond))
1603 alb_change_hw_addr_on_detach(bond, slave); 1610 alb_change_hw_addr_on_detach(bond, slave);
1604 1611
1605 tlb_clear_slave(bond, slave, 0); 1612 tlb_clear_slave(bond, slave, 0);
1606 1613
1607 if (bond->alb_info.rlb_enabled) { 1614 if (bond->alb_info.rlb_enabled) {
1608 bond->alb_info.next_rx_slave = NULL; 1615 bond->alb_info.rx_slave = NULL;
1609 rlb_clear_slave(bond, slave); 1616 rlb_clear_slave(bond, slave);
1610 } 1617 }
1611} 1618}
@@ -1669,7 +1676,7 @@ void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave
1669 swap_slave = bond->curr_active_slave; 1676 swap_slave = bond->curr_active_slave;
1670 rcu_assign_pointer(bond->curr_active_slave, new_slave); 1677 rcu_assign_pointer(bond->curr_active_slave, new_slave);
1671 1678
1672 if (!new_slave || list_empty(&bond->slave_list)) 1679 if (!new_slave || !bond_has_slaves(bond))
1673 return; 1680 return;
1674 1681
1675 /* set the new curr_active_slave to the bonds mac address 1682 /* set the new curr_active_slave to the bonds mac address
diff --git a/drivers/net/bonding/bond_alb.h b/drivers/net/bonding/bond_alb.h
index c5eff5dafdfe..4226044efd08 100644
--- a/drivers/net/bonding/bond_alb.h
+++ b/drivers/net/bonding/bond_alb.h
@@ -154,9 +154,7 @@ struct alb_bond_info {
154 u8 rx_ntt; /* flag - need to transmit 154 u8 rx_ntt; /* flag - need to transmit
155 * to all rx clients 155 * to all rx clients
156 */ 156 */
157 struct slave *next_rx_slave;/* next slave to be assigned 157 struct slave *rx_slave;/* last slave to xmit from */
158 * to a new rx client for
159 */
160 u8 primary_is_promisc; /* boolean */ 158 u8 primary_is_promisc; /* boolean */
161 u32 rlb_promisc_timeout_counter;/* counts primary 159 u32 rlb_promisc_timeout_counter;/* counts primary
162 * promiscuity time 160 * promiscuity time
diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index 55bbb8b8200c..d5c3153226b7 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -332,10 +332,11 @@ static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
332 __be16 proto, u16 vid) 332 __be16 proto, u16 vid)
333{ 333{
334 struct bonding *bond = netdev_priv(bond_dev); 334 struct bonding *bond = netdev_priv(bond_dev);
335 struct slave *slave; 335 struct slave *slave, *rollback_slave;
336 struct list_head *iter;
336 int res; 337 int res;
337 338
338 bond_for_each_slave(bond, slave) { 339 bond_for_each_slave(bond, slave, iter) {
339 res = vlan_vid_add(slave->dev, proto, vid); 340 res = vlan_vid_add(slave->dev, proto, vid);
340 if (res) 341 if (res)
341 goto unwind; 342 goto unwind;
@@ -344,9 +345,13 @@ static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
344 return 0; 345 return 0;
345 346
346unwind: 347unwind:
347 /* unwind from the slave that failed */ 348 /* unwind to the slave that failed */
348 bond_for_each_slave_continue_reverse(bond, slave) 349 bond_for_each_slave(bond, rollback_slave, iter) {
349 vlan_vid_del(slave->dev, proto, vid); 350 if (rollback_slave == slave)
351 break;
352
353 vlan_vid_del(rollback_slave->dev, proto, vid);
354 }
350 355
351 return res; 356 return res;
352} 357}
@@ -360,9 +365,10 @@ static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
360 __be16 proto, u16 vid) 365 __be16 proto, u16 vid)
361{ 366{
362 struct bonding *bond = netdev_priv(bond_dev); 367 struct bonding *bond = netdev_priv(bond_dev);
368 struct list_head *iter;
363 struct slave *slave; 369 struct slave *slave;
364 370
365 bond_for_each_slave(bond, slave) 371 bond_for_each_slave(bond, slave, iter)
366 vlan_vid_del(slave->dev, proto, vid); 372 vlan_vid_del(slave->dev, proto, vid);
367 373
368 if (bond_is_lb(bond)) 374 if (bond_is_lb(bond))
@@ -382,15 +388,16 @@ static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
382 */ 388 */
383static int bond_set_carrier(struct bonding *bond) 389static int bond_set_carrier(struct bonding *bond)
384{ 390{
391 struct list_head *iter;
385 struct slave *slave; 392 struct slave *slave;
386 393
387 if (list_empty(&bond->slave_list)) 394 if (!bond_has_slaves(bond))
388 goto down; 395 goto down;
389 396
390 if (bond->params.mode == BOND_MODE_8023AD) 397 if (bond->params.mode == BOND_MODE_8023AD)
391 return bond_3ad_set_carrier(bond); 398 return bond_3ad_set_carrier(bond);
392 399
393 bond_for_each_slave(bond, slave) { 400 bond_for_each_slave(bond, slave, iter) {
394 if (slave->link == BOND_LINK_UP) { 401 if (slave->link == BOND_LINK_UP) {
395 if (!netif_carrier_ok(bond->dev)) { 402 if (!netif_carrier_ok(bond->dev)) {
396 netif_carrier_on(bond->dev); 403 netif_carrier_on(bond->dev);
@@ -522,7 +529,9 @@ static int bond_check_dev_link(struct bonding *bond,
522 */ 529 */
523static int bond_set_promiscuity(struct bonding *bond, int inc) 530static int bond_set_promiscuity(struct bonding *bond, int inc)
524{ 531{
532 struct list_head *iter;
525 int err = 0; 533 int err = 0;
534
526 if (USES_PRIMARY(bond->params.mode)) { 535 if (USES_PRIMARY(bond->params.mode)) {
527 /* write lock already acquired */ 536 /* write lock already acquired */
528 if (bond->curr_active_slave) { 537 if (bond->curr_active_slave) {
@@ -532,7 +541,7 @@ static int bond_set_promiscuity(struct bonding *bond, int inc)
532 } else { 541 } else {
533 struct slave *slave; 542 struct slave *slave;
534 543
535 bond_for_each_slave(bond, slave) { 544 bond_for_each_slave(bond, slave, iter) {
536 err = dev_set_promiscuity(slave->dev, inc); 545 err = dev_set_promiscuity(slave->dev, inc);
537 if (err) 546 if (err)
538 return err; 547 return err;
@@ -546,7 +555,9 @@ static int bond_set_promiscuity(struct bonding *bond, int inc)
546 */ 555 */
547static int bond_set_allmulti(struct bonding *bond, int inc) 556static int bond_set_allmulti(struct bonding *bond, int inc)
548{ 557{
558 struct list_head *iter;
549 int err = 0; 559 int err = 0;
560
550 if (USES_PRIMARY(bond->params.mode)) { 561 if (USES_PRIMARY(bond->params.mode)) {
551 /* write lock already acquired */ 562 /* write lock already acquired */
552 if (bond->curr_active_slave) { 563 if (bond->curr_active_slave) {
@@ -556,7 +567,7 @@ static int bond_set_allmulti(struct bonding *bond, int inc)
556 } else { 567 } else {
557 struct slave *slave; 568 struct slave *slave;
558 569
559 bond_for_each_slave(bond, slave) { 570 bond_for_each_slave(bond, slave, iter) {
560 err = dev_set_allmulti(slave->dev, inc); 571 err = dev_set_allmulti(slave->dev, inc);
561 if (err) 572 if (err)
562 return err; 573 return err;
@@ -774,43 +785,24 @@ static bool bond_should_change_active(struct bonding *bond)
774/** 785/**
775 * find_best_interface - select the best available slave to be the active one 786 * find_best_interface - select the best available slave to be the active one
776 * @bond: our bonding struct 787 * @bond: our bonding struct
777 *
778 * Warning: Caller must hold curr_slave_lock for writing.
779 */ 788 */
780static struct slave *bond_find_best_slave(struct bonding *bond) 789static struct slave *bond_find_best_slave(struct bonding *bond)
781{ 790{
782 struct slave *new_active, *old_active; 791 struct slave *slave, *bestslave = NULL;
783 struct slave *bestslave = NULL; 792 struct list_head *iter;
784 int mintime = bond->params.updelay; 793 int mintime = bond->params.updelay;
785 int i;
786 794
787 new_active = bond->curr_active_slave; 795 if (bond->primary_slave && bond->primary_slave->link == BOND_LINK_UP &&
788 796 bond_should_change_active(bond))
789 if (!new_active) { /* there were no active slaves left */ 797 return bond->primary_slave;
790 new_active = bond_first_slave(bond); 798
791 if (!new_active) 799 bond_for_each_slave(bond, slave, iter) {
792 return NULL; /* still no slave, return NULL */ 800 if (slave->link == BOND_LINK_UP)
793 } 801 return slave;
794 802 if (slave->link == BOND_LINK_BACK && IS_UP(slave->dev) &&
795 if ((bond->primary_slave) && 803 slave->delay < mintime) {
796 bond->primary_slave->link == BOND_LINK_UP && 804 mintime = slave->delay;
797 bond_should_change_active(bond)) { 805 bestslave = slave;
798 new_active = bond->primary_slave;
799 }
800
801 /* remember where to stop iterating over the slaves */
802 old_active = new_active;
803
804 bond_for_each_slave_from(bond, new_active, i, old_active) {
805 if (new_active->link == BOND_LINK_UP) {
806 return new_active;
807 } else if (new_active->link == BOND_LINK_BACK &&
808 IS_UP(new_active->dev)) {
809 /* link up, but waiting for stabilization */
810 if (new_active->delay < mintime) {
811 mintime = new_active->delay;
812 bestslave = new_active;
813 }
814 } 806 }
815 } 807 }
816 808
@@ -980,7 +972,6 @@ void bond_select_active_slave(struct bonding *bond)
980 */ 972 */
981static void bond_attach_slave(struct bonding *bond, struct slave *new_slave) 973static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
982{ 974{
983 list_add_tail_rcu(&new_slave->list, &bond->slave_list);
984 bond->slave_cnt++; 975 bond->slave_cnt++;
985} 976}
986 977
@@ -996,7 +987,6 @@ static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
996 */ 987 */
997static void bond_detach_slave(struct bonding *bond, struct slave *slave) 988static void bond_detach_slave(struct bonding *bond, struct slave *slave)
998{ 989{
999 list_del_rcu(&slave->list);
1000 bond->slave_cnt--; 990 bond->slave_cnt--;
1001} 991}
1002 992
@@ -1046,9 +1036,10 @@ static void bond_poll_controller(struct net_device *bond_dev)
1046static void bond_netpoll_cleanup(struct net_device *bond_dev) 1036static void bond_netpoll_cleanup(struct net_device *bond_dev)
1047{ 1037{
1048 struct bonding *bond = netdev_priv(bond_dev); 1038 struct bonding *bond = netdev_priv(bond_dev);
1039 struct list_head *iter;
1049 struct slave *slave; 1040 struct slave *slave;
1050 1041
1051 bond_for_each_slave(bond, slave) 1042 bond_for_each_slave(bond, slave, iter)
1052 if (IS_UP(slave->dev)) 1043 if (IS_UP(slave->dev))
1053 slave_disable_netpoll(slave); 1044 slave_disable_netpoll(slave);
1054} 1045}
@@ -1056,10 +1047,11 @@ static void bond_netpoll_cleanup(struct net_device *bond_dev)
1056static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni, gfp_t gfp) 1047static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni, gfp_t gfp)
1057{ 1048{
1058 struct bonding *bond = netdev_priv(dev); 1049 struct bonding *bond = netdev_priv(dev);
1050 struct list_head *iter;
1059 struct slave *slave; 1051 struct slave *slave;
1060 int err = 0; 1052 int err = 0;
1061 1053
1062 bond_for_each_slave(bond, slave) { 1054 bond_for_each_slave(bond, slave, iter) {
1063 err = slave_enable_netpoll(slave); 1055 err = slave_enable_netpoll(slave);
1064 if (err) { 1056 if (err) {
1065 bond_netpoll_cleanup(dev); 1057 bond_netpoll_cleanup(dev);
@@ -1087,10 +1079,11 @@ static netdev_features_t bond_fix_features(struct net_device *dev,
1087 netdev_features_t features) 1079 netdev_features_t features)
1088{ 1080{
1089 struct bonding *bond = netdev_priv(dev); 1081 struct bonding *bond = netdev_priv(dev);
1082 struct list_head *iter;
1090 netdev_features_t mask; 1083 netdev_features_t mask;
1091 struct slave *slave; 1084 struct slave *slave;
1092 1085
1093 if (list_empty(&bond->slave_list)) { 1086 if (!bond_has_slaves(bond)) {
1094 /* Disable adding VLANs to empty bond. But why? --mq */ 1087 /* Disable adding VLANs to empty bond. But why? --mq */
1095 features |= NETIF_F_VLAN_CHALLENGED; 1088 features |= NETIF_F_VLAN_CHALLENGED;
1096 return features; 1089 return features;
@@ -1100,7 +1093,7 @@ static netdev_features_t bond_fix_features(struct net_device *dev,
1100 features &= ~NETIF_F_ONE_FOR_ALL; 1093 features &= ~NETIF_F_ONE_FOR_ALL;
1101 features |= NETIF_F_ALL_FOR_ALL; 1094 features |= NETIF_F_ALL_FOR_ALL;
1102 1095
1103 bond_for_each_slave(bond, slave) { 1096 bond_for_each_slave(bond, slave, iter) {
1104 features = netdev_increment_features(features, 1097 features = netdev_increment_features(features,
1105 slave->dev->features, 1098 slave->dev->features,
1106 mask); 1099 mask);
@@ -1118,16 +1111,17 @@ static void bond_compute_features(struct bonding *bond)
1118{ 1111{
1119 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE; 1112 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
1120 netdev_features_t vlan_features = BOND_VLAN_FEATURES; 1113 netdev_features_t vlan_features = BOND_VLAN_FEATURES;
1114 struct net_device *bond_dev = bond->dev;
1115 struct list_head *iter;
1116 struct slave *slave;
1121 unsigned short max_hard_header_len = ETH_HLEN; 1117 unsigned short max_hard_header_len = ETH_HLEN;
1122 unsigned int gso_max_size = GSO_MAX_SIZE; 1118 unsigned int gso_max_size = GSO_MAX_SIZE;
1123 struct net_device *bond_dev = bond->dev;
1124 u16 gso_max_segs = GSO_MAX_SEGS; 1119 u16 gso_max_segs = GSO_MAX_SEGS;
1125 struct slave *slave;
1126 1120
1127 if (list_empty(&bond->slave_list)) 1121 if (!bond_has_slaves(bond))
1128 goto done; 1122 goto done;
1129 1123
1130 bond_for_each_slave(bond, slave) { 1124 bond_for_each_slave(bond, slave, iter) {
1131 vlan_features = netdev_increment_features(vlan_features, 1125 vlan_features = netdev_increment_features(vlan_features,
1132 slave->dev->vlan_features, BOND_VLAN_FEATURES); 1126 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1133 1127
@@ -1233,11 +1227,12 @@ static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
1233} 1227}
1234 1228
1235static int bond_master_upper_dev_link(struct net_device *bond_dev, 1229static int bond_master_upper_dev_link(struct net_device *bond_dev,
1236 struct net_device *slave_dev) 1230 struct net_device *slave_dev,
1231 struct slave *slave)
1237{ 1232{
1238 int err; 1233 int err;
1239 1234
1240 err = netdev_master_upper_dev_link(slave_dev, bond_dev); 1235 err = netdev_master_upper_dev_link_private(slave_dev, bond_dev, slave);
1241 if (err) 1236 if (err)
1242 return err; 1237 return err;
1243 slave_dev->flags |= IFF_SLAVE; 1238 slave_dev->flags |= IFF_SLAVE;
@@ -1258,7 +1253,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1258{ 1253{
1259 struct bonding *bond = netdev_priv(bond_dev); 1254 struct bonding *bond = netdev_priv(bond_dev);
1260 const struct net_device_ops *slave_ops = slave_dev->netdev_ops; 1255 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1261 struct slave *new_slave = NULL; 1256 struct slave *new_slave = NULL, *prev_slave;
1262 struct sockaddr addr; 1257 struct sockaddr addr;
1263 int link_reporting; 1258 int link_reporting;
1264 int res = 0, i; 1259 int res = 0, i;
@@ -1313,7 +1308,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1313 * bond ether type mutual exclusion - don't allow slaves of dissimilar 1308 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1314 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond 1309 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1315 */ 1310 */
1316 if (list_empty(&bond->slave_list)) { 1311 if (!bond_has_slaves(bond)) {
1317 if (bond_dev->type != slave_dev->type) { 1312 if (bond_dev->type != slave_dev->type) {
1318 pr_debug("%s: change device type from %d to %d\n", 1313 pr_debug("%s: change device type from %d to %d\n",
1319 bond_dev->name, 1314 bond_dev->name,
@@ -1352,7 +1347,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1352 } 1347 }
1353 1348
1354 if (slave_ops->ndo_set_mac_address == NULL) { 1349 if (slave_ops->ndo_set_mac_address == NULL) {
1355 if (list_empty(&bond->slave_list)) { 1350 if (!bond_has_slaves(bond)) {
1356 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.", 1351 pr_warning("%s: Warning: The first slave device specified does not support setting the MAC address. Setting fail_over_mac to active.",
1357 bond_dev->name); 1352 bond_dev->name);
1358 bond->params.fail_over_mac = BOND_FOM_ACTIVE; 1353 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
@@ -1368,7 +1363,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1368 1363
1369 /* If this is the first slave, then we need to set the master's hardware 1364 /* If this is the first slave, then we need to set the master's hardware
1370 * address to be the same as the slave's. */ 1365 * address to be the same as the slave's. */
1371 if (list_empty(&bond->slave_list) && 1366 if (!bond_has_slaves(bond) &&
1372 bond->dev->addr_assign_type == NET_ADDR_RANDOM) 1367 bond->dev->addr_assign_type == NET_ADDR_RANDOM)
1373 bond_set_dev_addr(bond->dev, slave_dev); 1368 bond_set_dev_addr(bond->dev, slave_dev);
1374 1369
@@ -1377,7 +1372,6 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1377 res = -ENOMEM; 1372 res = -ENOMEM;
1378 goto err_undo_flags; 1373 goto err_undo_flags;
1379 } 1374 }
1380 INIT_LIST_HEAD(&new_slave->list);
1381 /* 1375 /*
1382 * Set the new_slave's queue_id to be zero. Queue ID mapping 1376 * Set the new_slave's queue_id to be zero. Queue ID mapping
1383 * is set via sysfs or module option if desired. 1377 * is set via sysfs or module option if desired.
@@ -1413,17 +1407,11 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1413 } 1407 }
1414 } 1408 }
1415 1409
1416 res = bond_master_upper_dev_link(bond_dev, slave_dev);
1417 if (res) {
1418 pr_debug("Error %d calling bond_master_upper_dev_link\n", res);
1419 goto err_restore_mac;
1420 }
1421
1422 /* open the slave since the application closed it */ 1410 /* open the slave since the application closed it */
1423 res = dev_open(slave_dev); 1411 res = dev_open(slave_dev);
1424 if (res) { 1412 if (res) {
1425 pr_debug("Opening slave %s failed\n", slave_dev->name); 1413 pr_debug("Opening slave %s failed\n", slave_dev->name);
1426 goto err_unset_master; 1414 goto err_restore_mac;
1427 } 1415 }
1428 1416
1429 new_slave->bond = bond; 1417 new_slave->bond = bond;
@@ -1481,6 +1469,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1481 1469
1482 write_lock_bh(&bond->lock); 1470 write_lock_bh(&bond->lock);
1483 1471
1472 prev_slave = bond_last_slave(bond);
1484 bond_attach_slave(bond, new_slave); 1473 bond_attach_slave(bond, new_slave);
1485 1474
1486 new_slave->delay = 0; 1475 new_slave->delay = 0;
@@ -1575,9 +1564,6 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1575 */ 1564 */
1576 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL); 1565 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL);
1577 } else { 1566 } else {
1578 struct slave *prev_slave;
1579
1580 prev_slave = bond_prev_slave(bond, new_slave);
1581 SLAVE_AD_INFO(new_slave).id = 1567 SLAVE_AD_INFO(new_slave).id =
1582 SLAVE_AD_INFO(prev_slave).id + 1; 1568 SLAVE_AD_INFO(prev_slave).id + 1;
1583 } 1569 }
@@ -1626,17 +1612,20 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1626 1612
1627 read_unlock(&bond->lock); 1613 read_unlock(&bond->lock);
1628 1614
1629 res = bond_create_slave_symlinks(bond_dev, slave_dev);
1630 if (res)
1631 goto err_detach;
1632
1633 res = netdev_rx_handler_register(slave_dev, bond_handle_frame, 1615 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1634 new_slave); 1616 new_slave);
1635 if (res) { 1617 if (res) {
1636 pr_debug("Error %d calling netdev_rx_handler_register\n", res); 1618 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1637 goto err_dest_symlinks; 1619 goto err_detach;
1620 }
1621
1622 res = bond_master_upper_dev_link(bond_dev, slave_dev, new_slave);
1623 if (res) {
1624 pr_debug("Error %d calling bond_master_upper_dev_link\n", res);
1625 goto err_unregister;
1638 } 1626 }
1639 1627
1628
1640 pr_info("%s: enslaving %s as a%s interface with a%s link.\n", 1629 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
1641 bond_dev->name, slave_dev->name, 1630 bond_dev->name, slave_dev->name,
1642 bond_is_active_slave(new_slave) ? "n active" : " backup", 1631 bond_is_active_slave(new_slave) ? "n active" : " backup",
@@ -1646,8 +1635,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1646 return 0; 1635 return 0;
1647 1636
1648/* Undo stages on error */ 1637/* Undo stages on error */
1649err_dest_symlinks: 1638err_unregister:
1650 bond_destroy_slave_symlinks(bond_dev, slave_dev); 1639 netdev_rx_handler_unregister(slave_dev);
1651 1640
1652err_detach: 1641err_detach:
1653 if (!USES_PRIMARY(bond->params.mode)) 1642 if (!USES_PRIMARY(bond->params.mode))
@@ -1675,9 +1664,6 @@ err_close:
1675 slave_dev->priv_flags &= ~IFF_BONDING; 1664 slave_dev->priv_flags &= ~IFF_BONDING;
1676 dev_close(slave_dev); 1665 dev_close(slave_dev);
1677 1666
1678err_unset_master:
1679 bond_upper_dev_unlink(bond_dev, slave_dev);
1680
1681err_restore_mac: 1667err_restore_mac:
1682 if (!bond->params.fail_over_mac) { 1668 if (!bond->params.fail_over_mac) {
1683 /* XXX TODO - fom follow mode needs to change master's 1669 /* XXX TODO - fom follow mode needs to change master's
@@ -1698,7 +1684,7 @@ err_free:
1698err_undo_flags: 1684err_undo_flags:
1699 bond_compute_features(bond); 1685 bond_compute_features(bond);
1700 /* Enslave of first slave has failed and we need to fix master's mac */ 1686 /* Enslave of first slave has failed and we need to fix master's mac */
1701 if (list_empty(&bond->slave_list) && 1687 if (!bond_has_slaves(bond) &&
1702 ether_addr_equal(bond_dev->dev_addr, slave_dev->dev_addr)) 1688 ether_addr_equal(bond_dev->dev_addr, slave_dev->dev_addr))
1703 eth_hw_addr_random(bond_dev); 1689 eth_hw_addr_random(bond_dev);
1704 1690
@@ -1748,6 +1734,8 @@ static int __bond_release_one(struct net_device *bond_dev,
1748 } 1734 }
1749 1735
1750 write_unlock_bh(&bond->lock); 1736 write_unlock_bh(&bond->lock);
1737
1738 bond_upper_dev_unlink(bond_dev, slave_dev);
1751 /* unregister rx_handler early so bond_handle_frame wouldn't be called 1739 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1752 * for this slave anymore. 1740 * for this slave anymore.
1753 */ 1741 */
@@ -1776,7 +1764,7 @@ static int __bond_release_one(struct net_device *bond_dev,
1776 1764
1777 if (!all && !bond->params.fail_over_mac) { 1765 if (!all && !bond->params.fail_over_mac) {
1778 if (ether_addr_equal(bond_dev->dev_addr, slave->perm_hwaddr) && 1766 if (ether_addr_equal(bond_dev->dev_addr, slave->perm_hwaddr) &&
1779 !list_empty(&bond->slave_list)) 1767 bond_has_slaves(bond))
1780 pr_warn("%s: Warning: the permanent HWaddr of %s - %pM - is still in use by %s. Set the HWaddr of %s to a different address to avoid conflicts.\n", 1768 pr_warn("%s: Warning: the permanent HWaddr of %s - %pM - is still in use by %s. Set the HWaddr of %s to a different address to avoid conflicts.\n",
1781 bond_dev->name, slave_dev->name, 1769 bond_dev->name, slave_dev->name,
1782 slave->perm_hwaddr, 1770 slave->perm_hwaddr,
@@ -1819,7 +1807,7 @@ static int __bond_release_one(struct net_device *bond_dev,
1819 write_lock_bh(&bond->lock); 1807 write_lock_bh(&bond->lock);
1820 } 1808 }
1821 1809
1822 if (list_empty(&bond->slave_list)) { 1810 if (!bond_has_slaves(bond)) {
1823 bond_set_carrier(bond); 1811 bond_set_carrier(bond);
1824 eth_hw_addr_random(bond_dev); 1812 eth_hw_addr_random(bond_dev);
1825 1813
@@ -1835,7 +1823,7 @@ static int __bond_release_one(struct net_device *bond_dev,
1835 unblock_netpoll_tx(); 1823 unblock_netpoll_tx();
1836 synchronize_rcu(); 1824 synchronize_rcu();
1837 1825
1838 if (list_empty(&bond->slave_list)) { 1826 if (!bond_has_slaves(bond)) {
1839 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev); 1827 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
1840 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev); 1828 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
1841 } 1829 }
@@ -1847,8 +1835,6 @@ static int __bond_release_one(struct net_device *bond_dev,
1847 bond_dev->name, slave_dev->name, bond_dev->name); 1835 bond_dev->name, slave_dev->name, bond_dev->name);
1848 1836
1849 /* must do this from outside any spinlocks */ 1837 /* must do this from outside any spinlocks */
1850 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1851
1852 vlan_vids_del_by_dev(slave_dev, bond_dev); 1838 vlan_vids_del_by_dev(slave_dev, bond_dev);
1853 1839
1854 /* If the mode USES_PRIMARY, then this cases was handled above by 1840 /* If the mode USES_PRIMARY, then this cases was handled above by
@@ -1866,8 +1852,6 @@ static int __bond_release_one(struct net_device *bond_dev,
1866 bond_hw_addr_flush(bond_dev, slave_dev); 1852 bond_hw_addr_flush(bond_dev, slave_dev);
1867 } 1853 }
1868 1854
1869 bond_upper_dev_unlink(bond_dev, slave_dev);
1870
1871 slave_disable_netpoll(slave); 1855 slave_disable_netpoll(slave);
1872 1856
1873 /* close slave before restoring its mac address */ 1857 /* close slave before restoring its mac address */
@@ -1906,7 +1890,7 @@ static int bond_release_and_destroy(struct net_device *bond_dev,
1906 int ret; 1890 int ret;
1907 1891
1908 ret = bond_release(bond_dev, slave_dev); 1892 ret = bond_release(bond_dev, slave_dev);
1909 if (ret == 0 && list_empty(&bond->slave_list)) { 1893 if (ret == 0 && !bond_has_slaves(bond)) {
1910 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL; 1894 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
1911 pr_info("%s: destroying bond %s.\n", 1895 pr_info("%s: destroying bond %s.\n",
1912 bond_dev->name, bond_dev->name); 1896 bond_dev->name, bond_dev->name);
@@ -1987,11 +1971,12 @@ static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
1987static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info) 1971static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
1988{ 1972{
1989 struct bonding *bond = netdev_priv(bond_dev); 1973 struct bonding *bond = netdev_priv(bond_dev);
1974 struct list_head *iter;
1990 int i = 0, res = -ENODEV; 1975 int i = 0, res = -ENODEV;
1991 struct slave *slave; 1976 struct slave *slave;
1992 1977
1993 read_lock(&bond->lock); 1978 read_lock(&bond->lock);
1994 bond_for_each_slave(bond, slave) { 1979 bond_for_each_slave(bond, slave, iter) {
1995 if (i++ == (int)info->slave_id) { 1980 if (i++ == (int)info->slave_id) {
1996 res = 0; 1981 res = 0;
1997 strcpy(info->slave_name, slave->dev->name); 1982 strcpy(info->slave_name, slave->dev->name);
@@ -2012,12 +1997,13 @@ static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *in
2012static int bond_miimon_inspect(struct bonding *bond) 1997static int bond_miimon_inspect(struct bonding *bond)
2013{ 1998{
2014 int link_state, commit = 0; 1999 int link_state, commit = 0;
2000 struct list_head *iter;
2015 struct slave *slave; 2001 struct slave *slave;
2016 bool ignore_updelay; 2002 bool ignore_updelay;
2017 2003
2018 ignore_updelay = !bond->curr_active_slave ? true : false; 2004 ignore_updelay = !bond->curr_active_slave ? true : false;
2019 2005
2020 bond_for_each_slave(bond, slave) { 2006 bond_for_each_slave(bond, slave, iter) {
2021 slave->new_link = BOND_LINK_NOCHANGE; 2007 slave->new_link = BOND_LINK_NOCHANGE;
2022 2008
2023 link_state = bond_check_dev_link(bond, slave->dev, 0); 2009 link_state = bond_check_dev_link(bond, slave->dev, 0);
@@ -2111,9 +2097,10 @@ static int bond_miimon_inspect(struct bonding *bond)
2111 2097
2112static void bond_miimon_commit(struct bonding *bond) 2098static void bond_miimon_commit(struct bonding *bond)
2113{ 2099{
2100 struct list_head *iter;
2114 struct slave *slave; 2101 struct slave *slave;
2115 2102
2116 bond_for_each_slave(bond, slave) { 2103 bond_for_each_slave(bond, slave, iter) {
2117 switch (slave->new_link) { 2104 switch (slave->new_link) {
2118 case BOND_LINK_NOCHANGE: 2105 case BOND_LINK_NOCHANGE:
2119 continue; 2106 continue;
@@ -2218,7 +2205,7 @@ void bond_mii_monitor(struct work_struct *work)
2218 2205
2219 delay = msecs_to_jiffies(bond->params.miimon); 2206 delay = msecs_to_jiffies(bond->params.miimon);
2220 2207
2221 if (list_empty(&bond->slave_list)) 2208 if (!bond_has_slaves(bond))
2222 goto re_arm; 2209 goto re_arm;
2223 2210
2224 should_notify_peers = bond_should_notify_peers(bond); 2211 should_notify_peers = bond_should_notify_peers(bond);
@@ -2267,7 +2254,7 @@ static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
2267 return true; 2254 return true;
2268 2255
2269 rcu_read_lock(); 2256 rcu_read_lock();
2270 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) { 2257 netdev_for_each_all_upper_dev_rcu(bond->dev, upper, iter) {
2271 if (ip == bond_confirm_addr(upper, 0, ip)) { 2258 if (ip == bond_confirm_addr(upper, 0, ip)) {
2272 ret = true; 2259 ret = true;
2273 break; 2260 break;
@@ -2342,10 +2329,12 @@ static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2342 * 2329 *
2343 * TODO: QinQ? 2330 * TODO: QinQ?
2344 */ 2331 */
2345 netdev_for_each_upper_dev_rcu(bond->dev, vlan_upper, vlan_iter) { 2332 netdev_for_each_all_upper_dev_rcu(bond->dev, vlan_upper,
2333 vlan_iter) {
2346 if (!is_vlan_dev(vlan_upper)) 2334 if (!is_vlan_dev(vlan_upper))
2347 continue; 2335 continue;
2348 netdev_for_each_upper_dev_rcu(vlan_upper, upper, iter) { 2336 netdev_for_each_all_upper_dev_rcu(vlan_upper, upper,
2337 iter) {
2349 if (upper == rt->dst.dev) { 2338 if (upper == rt->dst.dev) {
2350 vlan_id = vlan_dev_vlan_id(vlan_upper); 2339 vlan_id = vlan_dev_vlan_id(vlan_upper);
2351 rcu_read_unlock(); 2340 rcu_read_unlock();
@@ -2358,7 +2347,7 @@ static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2358 * our upper vlans, then just search for any dev that 2347 * our upper vlans, then just search for any dev that
2359 * matches, and in case it's a vlan - save the id 2348 * matches, and in case it's a vlan - save the id
2360 */ 2349 */
2361 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) { 2350 netdev_for_each_all_upper_dev_rcu(bond->dev, upper, iter) {
2362 if (upper == rt->dst.dev) { 2351 if (upper == rt->dst.dev) {
2363 /* if it's a vlan - get its VID */ 2352 /* if it's a vlan - get its VID */
2364 if (is_vlan_dev(upper)) 2353 if (is_vlan_dev(upper))
@@ -2505,11 +2494,12 @@ void bond_loadbalance_arp_mon(struct work_struct *work)
2505 struct bonding *bond = container_of(work, struct bonding, 2494 struct bonding *bond = container_of(work, struct bonding,
2506 arp_work.work); 2495 arp_work.work);
2507 struct slave *slave, *oldcurrent; 2496 struct slave *slave, *oldcurrent;
2497 struct list_head *iter;
2508 int do_failover = 0; 2498 int do_failover = 0;
2509 2499
2510 read_lock(&bond->lock); 2500 read_lock(&bond->lock);
2511 2501
2512 if (list_empty(&bond->slave_list)) 2502 if (!bond_has_slaves(bond))
2513 goto re_arm; 2503 goto re_arm;
2514 2504
2515 oldcurrent = bond->curr_active_slave; 2505 oldcurrent = bond->curr_active_slave;
@@ -2521,7 +2511,7 @@ void bond_loadbalance_arp_mon(struct work_struct *work)
2521 * TODO: what about up/down delay in arp mode? it wasn't here before 2511 * TODO: what about up/down delay in arp mode? it wasn't here before
2522 * so it can wait 2512 * so it can wait
2523 */ 2513 */
2524 bond_for_each_slave(bond, slave) { 2514 bond_for_each_slave(bond, slave, iter) {
2525 unsigned long trans_start = dev_trans_start(slave->dev); 2515 unsigned long trans_start = dev_trans_start(slave->dev);
2526 2516
2527 if (slave->link != BOND_LINK_UP) { 2517 if (slave->link != BOND_LINK_UP) {
@@ -2612,10 +2602,11 @@ re_arm:
2612static int bond_ab_arp_inspect(struct bonding *bond) 2602static int bond_ab_arp_inspect(struct bonding *bond)
2613{ 2603{
2614 unsigned long trans_start, last_rx; 2604 unsigned long trans_start, last_rx;
2605 struct list_head *iter;
2615 struct slave *slave; 2606 struct slave *slave;
2616 int commit = 0; 2607 int commit = 0;
2617 2608
2618 bond_for_each_slave(bond, slave) { 2609 bond_for_each_slave(bond, slave, iter) {
2619 slave->new_link = BOND_LINK_NOCHANGE; 2610 slave->new_link = BOND_LINK_NOCHANGE;
2620 last_rx = slave_last_rx(bond, slave); 2611 last_rx = slave_last_rx(bond, slave);
2621 2612
@@ -2682,9 +2673,10 @@ static int bond_ab_arp_inspect(struct bonding *bond)
2682static void bond_ab_arp_commit(struct bonding *bond) 2673static void bond_ab_arp_commit(struct bonding *bond)
2683{ 2674{
2684 unsigned long trans_start; 2675 unsigned long trans_start;
2676 struct list_head *iter;
2685 struct slave *slave; 2677 struct slave *slave;
2686 2678
2687 bond_for_each_slave(bond, slave) { 2679 bond_for_each_slave(bond, slave, iter) {
2688 switch (slave->new_link) { 2680 switch (slave->new_link) {
2689 case BOND_LINK_NOCHANGE: 2681 case BOND_LINK_NOCHANGE:
2690 continue; 2682 continue;
@@ -2755,8 +2747,9 @@ do_failover:
2755 */ 2747 */
2756static void bond_ab_arp_probe(struct bonding *bond) 2748static void bond_ab_arp_probe(struct bonding *bond)
2757{ 2749{
2758 struct slave *slave, *next_slave; 2750 struct slave *slave, *before = NULL, *new_slave = NULL;
2759 int i; 2751 struct list_head *iter;
2752 bool found = false;
2760 2753
2761 read_lock(&bond->curr_slave_lock); 2754 read_lock(&bond->curr_slave_lock);
2762 2755
@@ -2786,18 +2779,12 @@ static void bond_ab_arp_probe(struct bonding *bond)
2786 2779
2787 bond_set_slave_inactive_flags(bond->current_arp_slave); 2780 bond_set_slave_inactive_flags(bond->current_arp_slave);
2788 2781
2789 /* search for next candidate */ 2782 bond_for_each_slave(bond, slave, iter) {
2790 next_slave = bond_next_slave(bond, bond->current_arp_slave); 2783 if (!found && !before && IS_UP(slave->dev))
2791 bond_for_each_slave_from(bond, slave, i, next_slave) { 2784 before = slave;
2792 if (IS_UP(slave->dev)) {
2793 slave->link = BOND_LINK_BACK;
2794 bond_set_slave_active_flags(slave);
2795 bond_arp_send_all(bond, slave);
2796 slave->jiffies = jiffies;
2797 bond->current_arp_slave = slave;
2798 break;
2799 }
2800 2785
2786 if (found && !new_slave && IS_UP(slave->dev))
2787 new_slave = slave;
2801 /* if the link state is up at this point, we 2788 /* if the link state is up at this point, we
2802 * mark it down - this can happen if we have 2789 * mark it down - this can happen if we have
2803 * simultaneous link failures and 2790 * simultaneous link failures and
@@ -2805,7 +2792,7 @@ static void bond_ab_arp_probe(struct bonding *bond)
2805 * one the current slave so it is still marked 2792 * one the current slave so it is still marked
2806 * up when it is actually down 2793 * up when it is actually down
2807 */ 2794 */
2808 if (slave->link == BOND_LINK_UP) { 2795 if (!IS_UP(slave->dev) && slave->link == BOND_LINK_UP) {
2809 slave->link = BOND_LINK_DOWN; 2796 slave->link = BOND_LINK_DOWN;
2810 if (slave->link_failure_count < UINT_MAX) 2797 if (slave->link_failure_count < UINT_MAX)
2811 slave->link_failure_count++; 2798 slave->link_failure_count++;
@@ -2815,7 +2802,22 @@ static void bond_ab_arp_probe(struct bonding *bond)
2815 pr_info("%s: backup interface %s is now down.\n", 2802 pr_info("%s: backup interface %s is now down.\n",
2816 bond->dev->name, slave->dev->name); 2803 bond->dev->name, slave->dev->name);
2817 } 2804 }
2805 if (slave == bond->current_arp_slave)
2806 found = true;
2818 } 2807 }
2808
2809 if (!new_slave && before)
2810 new_slave = before;
2811
2812 if (!new_slave)
2813 return;
2814
2815 new_slave->link = BOND_LINK_BACK;
2816 bond_set_slave_active_flags(new_slave);
2817 bond_arp_send_all(bond, new_slave);
2818 new_slave->jiffies = jiffies;
2819 bond->current_arp_slave = new_slave;
2820
2819} 2821}
2820 2822
2821void bond_activebackup_arp_mon(struct work_struct *work) 2823void bond_activebackup_arp_mon(struct work_struct *work)
@@ -2829,7 +2831,7 @@ void bond_activebackup_arp_mon(struct work_struct *work)
2829 2831
2830 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval); 2832 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2831 2833
2832 if (list_empty(&bond->slave_list)) 2834 if (!bond_has_slaves(bond))
2833 goto re_arm; 2835 goto re_arm;
2834 2836
2835 should_notify_peers = bond_should_notify_peers(bond); 2837 should_notify_peers = bond_should_notify_peers(bond);
@@ -3148,13 +3150,14 @@ static void bond_work_cancel_all(struct bonding *bond)
3148static int bond_open(struct net_device *bond_dev) 3150static int bond_open(struct net_device *bond_dev)
3149{ 3151{
3150 struct bonding *bond = netdev_priv(bond_dev); 3152 struct bonding *bond = netdev_priv(bond_dev);
3153 struct list_head *iter;
3151 struct slave *slave; 3154 struct slave *slave;
3152 3155
3153 /* reset slave->backup and slave->inactive */ 3156 /* reset slave->backup and slave->inactive */
3154 read_lock(&bond->lock); 3157 read_lock(&bond->lock);
3155 if (!list_empty(&bond->slave_list)) { 3158 if (bond_has_slaves(bond)) {
3156 read_lock(&bond->curr_slave_lock); 3159 read_lock(&bond->curr_slave_lock);
3157 bond_for_each_slave(bond, slave) { 3160 bond_for_each_slave(bond, slave, iter) {
3158 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) 3161 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3159 && (slave != bond->curr_active_slave)) { 3162 && (slave != bond->curr_active_slave)) {
3160 bond_set_slave_inactive_flags(slave); 3163 bond_set_slave_inactive_flags(slave);
@@ -3214,12 +3217,13 @@ static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3214{ 3217{
3215 struct bonding *bond = netdev_priv(bond_dev); 3218 struct bonding *bond = netdev_priv(bond_dev);
3216 struct rtnl_link_stats64 temp; 3219 struct rtnl_link_stats64 temp;
3220 struct list_head *iter;
3217 struct slave *slave; 3221 struct slave *slave;
3218 3222
3219 memset(stats, 0, sizeof(*stats)); 3223 memset(stats, 0, sizeof(*stats));
3220 3224
3221 read_lock_bh(&bond->lock); 3225 read_lock_bh(&bond->lock);
3222 bond_for_each_slave(bond, slave) { 3226 bond_for_each_slave(bond, slave, iter) {
3223 const struct rtnl_link_stats64 *sstats = 3227 const struct rtnl_link_stats64 *sstats =
3224 dev_get_stats(slave->dev, &temp); 3228 dev_get_stats(slave->dev, &temp);
3225 3229
@@ -3386,6 +3390,7 @@ static void bond_change_rx_flags(struct net_device *bond_dev, int change)
3386static void bond_set_rx_mode(struct net_device *bond_dev) 3390static void bond_set_rx_mode(struct net_device *bond_dev)
3387{ 3391{
3388 struct bonding *bond = netdev_priv(bond_dev); 3392 struct bonding *bond = netdev_priv(bond_dev);
3393 struct list_head *iter;
3389 struct slave *slave; 3394 struct slave *slave;
3390 3395
3391 ASSERT_RTNL(); 3396 ASSERT_RTNL();
@@ -3397,7 +3402,7 @@ static void bond_set_rx_mode(struct net_device *bond_dev)
3397 dev_mc_sync(slave->dev, bond_dev); 3402 dev_mc_sync(slave->dev, bond_dev);
3398 } 3403 }
3399 } else { 3404 } else {
3400 bond_for_each_slave(bond, slave) { 3405 bond_for_each_slave(bond, slave, iter) {
3401 dev_uc_sync_multiple(slave->dev, bond_dev); 3406 dev_uc_sync_multiple(slave->dev, bond_dev);
3402 dev_mc_sync_multiple(slave->dev, bond_dev); 3407 dev_mc_sync_multiple(slave->dev, bond_dev);
3403 } 3408 }
@@ -3464,7 +3469,8 @@ static int bond_neigh_setup(struct net_device *dev,
3464static int bond_change_mtu(struct net_device *bond_dev, int new_mtu) 3469static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3465{ 3470{
3466 struct bonding *bond = netdev_priv(bond_dev); 3471 struct bonding *bond = netdev_priv(bond_dev);
3467 struct slave *slave; 3472 struct slave *slave, *rollback_slave;
3473 struct list_head *iter;
3468 int res = 0; 3474 int res = 0;
3469 3475
3470 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond, 3476 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
@@ -3485,10 +3491,9 @@ static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3485 * call to the base driver. 3491 * call to the base driver.
3486 */ 3492 */
3487 3493
3488 bond_for_each_slave(bond, slave) { 3494 bond_for_each_slave(bond, slave, iter) {
3489 pr_debug("s %p s->p %p c_m %p\n", 3495 pr_debug("s %p c_m %p\n",
3490 slave, 3496 slave,
3491 bond_prev_slave(bond, slave),
3492 slave->dev->netdev_ops->ndo_change_mtu); 3497 slave->dev->netdev_ops->ndo_change_mtu);
3493 3498
3494 res = dev_set_mtu(slave->dev, new_mtu); 3499 res = dev_set_mtu(slave->dev, new_mtu);
@@ -3513,13 +3518,16 @@ static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3513 3518
3514unwind: 3519unwind:
3515 /* unwind from head to the slave that failed */ 3520 /* unwind from head to the slave that failed */
3516 bond_for_each_slave_continue_reverse(bond, slave) { 3521 bond_for_each_slave(bond, rollback_slave, iter) {
3517 int tmp_res; 3522 int tmp_res;
3518 3523
3519 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu); 3524 if (rollback_slave == slave)
3525 break;
3526
3527 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
3520 if (tmp_res) { 3528 if (tmp_res) {
3521 pr_debug("unwind err %d dev %s\n", 3529 pr_debug("unwind err %d dev %s\n",
3522 tmp_res, slave->dev->name); 3530 tmp_res, rollback_slave->dev->name);
3523 } 3531 }
3524 } 3532 }
3525 3533
@@ -3536,8 +3544,9 @@ unwind:
3536static int bond_set_mac_address(struct net_device *bond_dev, void *addr) 3544static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3537{ 3545{
3538 struct bonding *bond = netdev_priv(bond_dev); 3546 struct bonding *bond = netdev_priv(bond_dev);
3547 struct slave *slave, *rollback_slave;
3539 struct sockaddr *sa = addr, tmp_sa; 3548 struct sockaddr *sa = addr, tmp_sa;
3540 struct slave *slave; 3549 struct list_head *iter;
3541 int res = 0; 3550 int res = 0;
3542 3551
3543 if (bond->params.mode == BOND_MODE_ALB) 3552 if (bond->params.mode == BOND_MODE_ALB)
@@ -3571,7 +3580,7 @@ static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3571 * call to the base driver. 3580 * call to the base driver.
3572 */ 3581 */
3573 3582
3574 bond_for_each_slave(bond, slave) { 3583 bond_for_each_slave(bond, slave, iter) {
3575 const struct net_device_ops *slave_ops = slave->dev->netdev_ops; 3584 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
3576 pr_debug("slave %p %s\n", slave, slave->dev->name); 3585 pr_debug("slave %p %s\n", slave, slave->dev->name);
3577 3586
@@ -3603,13 +3612,16 @@ unwind:
3603 tmp_sa.sa_family = bond_dev->type; 3612 tmp_sa.sa_family = bond_dev->type;
3604 3613
3605 /* unwind from head to the slave that failed */ 3614 /* unwind from head to the slave that failed */
3606 bond_for_each_slave_continue_reverse(bond, slave) { 3615 bond_for_each_slave(bond, rollback_slave, iter) {
3607 int tmp_res; 3616 int tmp_res;
3608 3617
3609 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa); 3618 if (rollback_slave == slave)
3619 break;
3620
3621 tmp_res = dev_set_mac_address(rollback_slave->dev, &tmp_sa);
3610 if (tmp_res) { 3622 if (tmp_res) {
3611 pr_debug("unwind err %d dev %s\n", 3623 pr_debug("unwind err %d dev %s\n",
3612 tmp_res, slave->dev->name); 3624 tmp_res, rollback_slave->dev->name);
3613 } 3625 }
3614 } 3626 }
3615 3627
@@ -3628,11 +3640,12 @@ unwind:
3628 */ 3640 */
3629void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id) 3641void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id)
3630{ 3642{
3643 struct list_head *iter;
3631 struct slave *slave; 3644 struct slave *slave;
3632 int i = slave_id; 3645 int i = slave_id;
3633 3646
3634 /* Here we start from the slave with slave_id */ 3647 /* Here we start from the slave with slave_id */
3635 bond_for_each_slave_rcu(bond, slave) { 3648 bond_for_each_slave_rcu(bond, slave, iter) {
3636 if (--i < 0) { 3649 if (--i < 0) {
3637 if (slave_can_tx(slave)) { 3650 if (slave_can_tx(slave)) {
3638 bond_dev_queue_xmit(bond, skb, slave->dev); 3651 bond_dev_queue_xmit(bond, skb, slave->dev);
@@ -3643,7 +3656,7 @@ void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id)
3643 3656
3644 /* Here we start from the first slave up to slave_id */ 3657 /* Here we start from the first slave up to slave_id */
3645 i = slave_id; 3658 i = slave_id;
3646 bond_for_each_slave_rcu(bond, slave) { 3659 bond_for_each_slave_rcu(bond, slave, iter) {
3647 if (--i < 0) 3660 if (--i < 0)
3648 break; 3661 break;
3649 if (slave_can_tx(slave)) { 3662 if (slave_can_tx(slave)) {
@@ -3720,8 +3733,9 @@ static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
3720{ 3733{
3721 struct bonding *bond = netdev_priv(bond_dev); 3734 struct bonding *bond = netdev_priv(bond_dev);
3722 struct slave *slave = NULL; 3735 struct slave *slave = NULL;
3736 struct list_head *iter;
3723 3737
3724 bond_for_each_slave_rcu(bond, slave) { 3738 bond_for_each_slave_rcu(bond, slave, iter) {
3725 if (bond_is_last_slave(bond, slave)) 3739 if (bond_is_last_slave(bond, slave))
3726 break; 3740 break;
3727 if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP) { 3741 if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP) {
@@ -3770,13 +3784,14 @@ static inline int bond_slave_override(struct bonding *bond,
3770{ 3784{
3771 struct slave *slave = NULL; 3785 struct slave *slave = NULL;
3772 struct slave *check_slave; 3786 struct slave *check_slave;
3787 struct list_head *iter;
3773 int res = 1; 3788 int res = 1;
3774 3789
3775 if (!skb->queue_mapping) 3790 if (!skb->queue_mapping)
3776 return 1; 3791 return 1;
3777 3792
3778 /* Find out if any slaves have the same mapping as this skb. */ 3793 /* Find out if any slaves have the same mapping as this skb. */
3779 bond_for_each_slave_rcu(bond, check_slave) { 3794 bond_for_each_slave_rcu(bond, check_slave, iter) {
3780 if (check_slave->queue_id == skb->queue_mapping) { 3795 if (check_slave->queue_id == skb->queue_mapping) {
3781 slave = check_slave; 3796 slave = check_slave;
3782 break; 3797 break;
@@ -3862,7 +3877,7 @@ static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
3862 return NETDEV_TX_BUSY; 3877 return NETDEV_TX_BUSY;
3863 3878
3864 rcu_read_lock(); 3879 rcu_read_lock();
3865 if (!list_empty(&bond->slave_list)) 3880 if (bond_has_slaves(bond))
3866 ret = __bond_start_xmit(skb, dev); 3881 ret = __bond_start_xmit(skb, dev);
3867 else 3882 else
3868 kfree_skb(skb); 3883 kfree_skb(skb);
@@ -3908,6 +3923,7 @@ static int bond_ethtool_get_settings(struct net_device *bond_dev,
3908{ 3923{
3909 struct bonding *bond = netdev_priv(bond_dev); 3924 struct bonding *bond = netdev_priv(bond_dev);
3910 unsigned long speed = 0; 3925 unsigned long speed = 0;
3926 struct list_head *iter;
3911 struct slave *slave; 3927 struct slave *slave;
3912 3928
3913 ecmd->duplex = DUPLEX_UNKNOWN; 3929 ecmd->duplex = DUPLEX_UNKNOWN;
@@ -3919,7 +3935,7 @@ static int bond_ethtool_get_settings(struct net_device *bond_dev,
3919 * this is an accurate maximum. 3935 * this is an accurate maximum.
3920 */ 3936 */
3921 read_lock(&bond->lock); 3937 read_lock(&bond->lock);
3922 bond_for_each_slave(bond, slave) { 3938 bond_for_each_slave(bond, slave, iter) {
3923 if (SLAVE_IS_OK(slave)) { 3939 if (SLAVE_IS_OK(slave)) {
3924 if (slave->speed != SPEED_UNKNOWN) 3940 if (slave->speed != SPEED_UNKNOWN)
3925 speed += slave->speed; 3941 speed += slave->speed;
@@ -3994,7 +4010,6 @@ static void bond_setup(struct net_device *bond_dev)
3994 /* initialize rwlocks */ 4010 /* initialize rwlocks */
3995 rwlock_init(&bond->lock); 4011 rwlock_init(&bond->lock);
3996 rwlock_init(&bond->curr_slave_lock); 4012 rwlock_init(&bond->curr_slave_lock);
3997 INIT_LIST_HEAD(&bond->slave_list);
3998 bond->params = bonding_defaults; 4013 bond->params = bonding_defaults;
3999 4014
4000 /* Initialize pointers */ 4015 /* Initialize pointers */
@@ -4050,12 +4065,13 @@ static void bond_setup(struct net_device *bond_dev)
4050static void bond_uninit(struct net_device *bond_dev) 4065static void bond_uninit(struct net_device *bond_dev)
4051{ 4066{
4052 struct bonding *bond = netdev_priv(bond_dev); 4067 struct bonding *bond = netdev_priv(bond_dev);
4053 struct slave *slave, *tmp_slave; 4068 struct list_head *iter;
4069 struct slave *slave;
4054 4070
4055 bond_netpoll_cleanup(bond_dev); 4071 bond_netpoll_cleanup(bond_dev);
4056 4072
4057 /* Release the bonded slaves */ 4073 /* Release the bonded slaves */
4058 list_for_each_entry_safe(slave, tmp_slave, &bond->slave_list, list) 4074 bond_for_each_slave(bond, slave, iter)
4059 __bond_release_one(bond_dev, slave->dev, true); 4075 __bond_release_one(bond_dev, slave->dev, true);
4060 pr_info("%s: released all slaves\n", bond_dev->name); 4076 pr_info("%s: released all slaves\n", bond_dev->name);
4061 4077
diff --git a/drivers/net/bonding/bond_procfs.c b/drivers/net/bonding/bond_procfs.c
index 20a6ee25bb63..7af5646e4410 100644
--- a/drivers/net/bonding/bond_procfs.c
+++ b/drivers/net/bonding/bond_procfs.c
@@ -10,8 +10,9 @@ static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
10 __acquires(&bond->lock) 10 __acquires(&bond->lock)
11{ 11{
12 struct bonding *bond = seq->private; 12 struct bonding *bond = seq->private;
13 loff_t off = 0; 13 struct list_head *iter;
14 struct slave *slave; 14 struct slave *slave;
15 loff_t off = 0;
15 16
16 /* make sure the bond won't be taken away */ 17 /* make sure the bond won't be taken away */
17 rcu_read_lock(); 18 rcu_read_lock();
@@ -20,7 +21,7 @@ static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
20 if (*pos == 0) 21 if (*pos == 0)
21 return SEQ_START_TOKEN; 22 return SEQ_START_TOKEN;
22 23
23 bond_for_each_slave(bond, slave) 24 bond_for_each_slave(bond, slave, iter)
24 if (++off == *pos) 25 if (++off == *pos)
25 return slave; 26 return slave;
26 27
diff --git a/drivers/net/bonding/bond_sysfs.c b/drivers/net/bonding/bond_sysfs.c
index c29b836749b6..e06c644470b1 100644
--- a/drivers/net/bonding/bond_sysfs.c
+++ b/drivers/net/bonding/bond_sysfs.c
@@ -168,41 +168,6 @@ static const struct class_attribute class_attr_bonding_masters = {
168 .namespace = bonding_namespace, 168 .namespace = bonding_namespace,
169}; 169};
170 170
171int bond_create_slave_symlinks(struct net_device *master,
172 struct net_device *slave)
173{
174 char linkname[IFNAMSIZ+7];
175 int ret = 0;
176
177 /* first, create a link from the slave back to the master */
178 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
179 "master");
180 if (ret)
181 return ret;
182 /* next, create a link from the master to the slave */
183 sprintf(linkname, "slave_%s", slave->name);
184 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
185 linkname);
186
187 /* free the master link created earlier in case of error */
188 if (ret)
189 sysfs_remove_link(&(slave->dev.kobj), "master");
190
191 return ret;
192
193}
194
195void bond_destroy_slave_symlinks(struct net_device *master,
196 struct net_device *slave)
197{
198 char linkname[IFNAMSIZ+7];
199
200 sysfs_remove_link(&(slave->dev.kobj), "master");
201 sprintf(linkname, "slave_%s", slave->name);
202 sysfs_remove_link(&(master->dev.kobj), linkname);
203}
204
205
206/* 171/*
207 * Show the slaves in the current bond. 172 * Show the slaves in the current bond.
208 */ 173 */
@@ -210,11 +175,12 @@ static ssize_t bonding_show_slaves(struct device *d,
210 struct device_attribute *attr, char *buf) 175 struct device_attribute *attr, char *buf)
211{ 176{
212 struct bonding *bond = to_bond(d); 177 struct bonding *bond = to_bond(d);
178 struct list_head *iter;
213 struct slave *slave; 179 struct slave *slave;
214 int res = 0; 180 int res = 0;
215 181
216 read_lock(&bond->lock); 182 read_lock(&bond->lock);
217 bond_for_each_slave(bond, slave) { 183 bond_for_each_slave(bond, slave, iter) {
218 if (res > (PAGE_SIZE - IFNAMSIZ)) { 184 if (res > (PAGE_SIZE - IFNAMSIZ)) {
219 /* not enough space for another interface name */ 185 /* not enough space for another interface name */
220 if ((PAGE_SIZE - res) > 10) 186 if ((PAGE_SIZE - res) > 10)
@@ -326,7 +292,7 @@ static ssize_t bonding_store_mode(struct device *d,
326 goto out; 292 goto out;
327 } 293 }
328 294
329 if (!list_empty(&bond->slave_list)) { 295 if (bond_has_slaves(bond)) {
330 pr_err("unable to update mode of %s because it has slaves.\n", 296 pr_err("unable to update mode of %s because it has slaves.\n",
331 bond->dev->name); 297 bond->dev->name);
332 ret = -EPERM; 298 ret = -EPERM;
@@ -522,7 +488,7 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
522 if (!rtnl_trylock()) 488 if (!rtnl_trylock())
523 return restart_syscall(); 489 return restart_syscall();
524 490
525 if (!list_empty(&bond->slave_list)) { 491 if (bond_has_slaves(bond)) {
526 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n", 492 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
527 bond->dev->name); 493 bond->dev->name);
528 ret = -EPERM; 494 ret = -EPERM;
@@ -656,6 +622,7 @@ static ssize_t bonding_store_arp_targets(struct device *d,
656 const char *buf, size_t count) 622 const char *buf, size_t count)
657{ 623{
658 struct bonding *bond = to_bond(d); 624 struct bonding *bond = to_bond(d);
625 struct list_head *iter;
659 struct slave *slave; 626 struct slave *slave;
660 __be32 newtarget, *targets; 627 __be32 newtarget, *targets;
661 unsigned long *targets_rx; 628 unsigned long *targets_rx;
@@ -688,7 +655,7 @@ static ssize_t bonding_store_arp_targets(struct device *d,
688 &newtarget); 655 &newtarget);
689 /* not to race with bond_arp_rcv */ 656 /* not to race with bond_arp_rcv */
690 write_lock_bh(&bond->lock); 657 write_lock_bh(&bond->lock);
691 bond_for_each_slave(bond, slave) 658 bond_for_each_slave(bond, slave, iter)
692 slave->target_last_arp_rx[ind] = jiffies; 659 slave->target_last_arp_rx[ind] = jiffies;
693 targets[ind] = newtarget; 660 targets[ind] = newtarget;
694 write_unlock_bh(&bond->lock); 661 write_unlock_bh(&bond->lock);
@@ -714,7 +681,7 @@ static ssize_t bonding_store_arp_targets(struct device *d,
714 &newtarget); 681 &newtarget);
715 682
716 write_lock_bh(&bond->lock); 683 write_lock_bh(&bond->lock);
717 bond_for_each_slave(bond, slave) { 684 bond_for_each_slave(bond, slave, iter) {
718 targets_rx = slave->target_last_arp_rx; 685 targets_rx = slave->target_last_arp_rx;
719 j = ind; 686 j = ind;
720 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++) 687 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
@@ -1111,6 +1078,7 @@ static ssize_t bonding_store_primary(struct device *d,
1111 const char *buf, size_t count) 1078 const char *buf, size_t count)
1112{ 1079{
1113 struct bonding *bond = to_bond(d); 1080 struct bonding *bond = to_bond(d);
1081 struct list_head *iter;
1114 char ifname[IFNAMSIZ]; 1082 char ifname[IFNAMSIZ];
1115 struct slave *slave; 1083 struct slave *slave;
1116 1084
@@ -1138,7 +1106,7 @@ static ssize_t bonding_store_primary(struct device *d,
1138 goto out; 1106 goto out;
1139 } 1107 }
1140 1108
1141 bond_for_each_slave(bond, slave) { 1109 bond_for_each_slave(bond, slave, iter) {
1142 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) { 1110 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1143 pr_info("%s: Setting %s as primary slave.\n", 1111 pr_info("%s: Setting %s as primary slave.\n",
1144 bond->dev->name, slave->dev->name); 1112 bond->dev->name, slave->dev->name);
@@ -1286,6 +1254,7 @@ static ssize_t bonding_store_active_slave(struct device *d,
1286{ 1254{
1287 struct slave *slave, *old_active, *new_active; 1255 struct slave *slave, *old_active, *new_active;
1288 struct bonding *bond = to_bond(d); 1256 struct bonding *bond = to_bond(d);
1257 struct list_head *iter;
1289 char ifname[IFNAMSIZ]; 1258 char ifname[IFNAMSIZ];
1290 1259
1291 if (!rtnl_trylock()) 1260 if (!rtnl_trylock())
@@ -1313,7 +1282,7 @@ static ssize_t bonding_store_active_slave(struct device *d,
1313 goto out; 1282 goto out;
1314 } 1283 }
1315 1284
1316 bond_for_each_slave(bond, slave) { 1285 bond_for_each_slave(bond, slave, iter) {
1317 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) { 1286 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1318 old_active = bond->curr_active_slave; 1287 old_active = bond->curr_active_slave;
1319 new_active = slave; 1288 new_active = slave;
@@ -1493,6 +1462,7 @@ static ssize_t bonding_show_queue_id(struct device *d,
1493 char *buf) 1462 char *buf)
1494{ 1463{
1495 struct bonding *bond = to_bond(d); 1464 struct bonding *bond = to_bond(d);
1465 struct list_head *iter;
1496 struct slave *slave; 1466 struct slave *slave;
1497 int res = 0; 1467 int res = 0;
1498 1468
@@ -1500,7 +1470,7 @@ static ssize_t bonding_show_queue_id(struct device *d,
1500 return restart_syscall(); 1470 return restart_syscall();
1501 1471
1502 read_lock(&bond->lock); 1472 read_lock(&bond->lock);
1503 bond_for_each_slave(bond, slave) { 1473 bond_for_each_slave(bond, slave, iter) {
1504 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) { 1474 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1505 /* not enough space for another interface_name:queue_id pair */ 1475 /* not enough space for another interface_name:queue_id pair */
1506 if ((PAGE_SIZE - res) > 10) 1476 if ((PAGE_SIZE - res) > 10)
@@ -1529,6 +1499,7 @@ static ssize_t bonding_store_queue_id(struct device *d,
1529{ 1499{
1530 struct slave *slave, *update_slave; 1500 struct slave *slave, *update_slave;
1531 struct bonding *bond = to_bond(d); 1501 struct bonding *bond = to_bond(d);
1502 struct list_head *iter;
1532 u16 qid; 1503 u16 qid;
1533 int ret = count; 1504 int ret = count;
1534 char *delim; 1505 char *delim;
@@ -1565,7 +1536,7 @@ static ssize_t bonding_store_queue_id(struct device *d,
1565 1536
1566 /* Search for thes slave and check for duplicate qids */ 1537 /* Search for thes slave and check for duplicate qids */
1567 update_slave = NULL; 1538 update_slave = NULL;
1568 bond_for_each_slave(bond, slave) { 1539 bond_for_each_slave(bond, slave, iter) {
1569 if (sdev == slave->dev) 1540 if (sdev == slave->dev)
1570 /* 1541 /*
1571 * We don't need to check the matching 1542 * We don't need to check the matching
@@ -1619,6 +1590,7 @@ static ssize_t bonding_store_slaves_active(struct device *d,
1619{ 1590{
1620 struct bonding *bond = to_bond(d); 1591 struct bonding *bond = to_bond(d);
1621 int new_value, ret = count; 1592 int new_value, ret = count;
1593 struct list_head *iter;
1622 struct slave *slave; 1594 struct slave *slave;
1623 1595
1624 if (sscanf(buf, "%d", &new_value) != 1) { 1596 if (sscanf(buf, "%d", &new_value) != 1) {
@@ -1641,7 +1613,7 @@ static ssize_t bonding_store_slaves_active(struct device *d,
1641 } 1613 }
1642 1614
1643 read_lock(&bond->lock); 1615 read_lock(&bond->lock);
1644 bond_for_each_slave(bond, slave) { 1616 bond_for_each_slave(bond, slave, iter) {
1645 if (!bond_is_active_slave(slave)) { 1617 if (!bond_is_active_slave(slave)) {
1646 if (new_value) 1618 if (new_value)
1647 slave->inactive = 0; 1619 slave->inactive = 0;
diff --git a/drivers/net/bonding/bonding.h b/drivers/net/bonding/bonding.h
index 03cf3fd14490..5b71601666cd 100644
--- a/drivers/net/bonding/bonding.h
+++ b/drivers/net/bonding/bonding.h
@@ -72,63 +72,40 @@
72 res; }) 72 res; })
73 73
74/* slave list primitives */ 74/* slave list primitives */
75#define bond_to_slave(ptr) list_entry(ptr, struct slave, list) 75#define bond_slave_list(bond) (&(bond)->dev->adj_list.lower)
76
77#define bond_has_slaves(bond) !list_empty(bond_slave_list(bond))
76 78
77/* IMPORTANT: bond_first/last_slave can return NULL in case of an empty list */ 79/* IMPORTANT: bond_first/last_slave can return NULL in case of an empty list */
78#define bond_first_slave(bond) \ 80#define bond_first_slave(bond) \
79 list_first_entry_or_null(&(bond)->slave_list, struct slave, list) 81 (bond_has_slaves(bond) ? \
82 netdev_adjacent_get_private(bond_slave_list(bond)->next) : \
83 NULL)
80#define bond_last_slave(bond) \ 84#define bond_last_slave(bond) \
81 (list_empty(&(bond)->slave_list) ? NULL : \ 85 (bond_has_slaves(bond) ? \
82 bond_to_slave((bond)->slave_list.prev)) 86 netdev_adjacent_get_private(bond_slave_list(bond)->prev) : \
87 NULL)
83 88
84#define bond_is_first_slave(bond, pos) ((pos)->list.prev == &(bond)->slave_list) 89#define bond_is_first_slave(bond, pos) (pos == bond_first_slave(bond))
85#define bond_is_last_slave(bond, pos) ((pos)->list.next == &(bond)->slave_list) 90#define bond_is_last_slave(bond, pos) (pos == bond_last_slave(bond))
86 91
87/* Since bond_first/last_slave can return NULL, these can return NULL too */ 92/* Since bond_first/last_slave can return NULL, these can return NULL too */
88#define bond_next_slave(bond, pos) \ 93#define bond_next_slave(bond, pos) __bond_next_slave(bond, pos)
89 (bond_is_last_slave(bond, pos) ? bond_first_slave(bond) : \
90 bond_to_slave((pos)->list.next))
91
92#define bond_prev_slave(bond, pos) \
93 (bond_is_first_slave(bond, pos) ? bond_last_slave(bond) : \
94 bond_to_slave((pos)->list.prev))
95
96/**
97 * bond_for_each_slave_from - iterate the slaves list from a starting point
98 * @bond: the bond holding this list.
99 * @pos: current slave.
100 * @cnt: counter for max number of moves
101 * @start: starting point.
102 *
103 * Caller must hold bond->lock
104 */
105#define bond_for_each_slave_from(bond, pos, cnt, start) \
106 for (cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
107 cnt++, pos = bond_next_slave(bond, pos))
108 94
109/** 95/**
110 * bond_for_each_slave - iterate over all slaves 96 * bond_for_each_slave - iterate over all slaves
111 * @bond: the bond holding this list 97 * @bond: the bond holding this list
112 * @pos: current slave 98 * @pos: current slave
99 * @iter: list_head * iterator
113 * 100 *
114 * Caller must hold bond->lock 101 * Caller must hold bond->lock
115 */ 102 */
116#define bond_for_each_slave(bond, pos) \ 103#define bond_for_each_slave(bond, pos, iter) \
117 list_for_each_entry(pos, &(bond)->slave_list, list) 104 netdev_for_each_lower_private((bond)->dev, pos, iter)
118 105
119/* Caller must have rcu_read_lock */ 106/* Caller must have rcu_read_lock */
120#define bond_for_each_slave_rcu(bond, pos) \ 107#define bond_for_each_slave_rcu(bond, pos, iter) \
121 list_for_each_entry_rcu(pos, &(bond)->slave_list, list) 108 netdev_for_each_lower_private_rcu((bond)->dev, pos, iter)
122
123/**
124 * bond_for_each_slave_reverse - iterate in reverse from a given position
125 * @bond: the bond holding this list
126 * @pos: slave to continue from
127 *
128 * Caller must hold bond->lock
129 */
130#define bond_for_each_slave_continue_reverse(bond, pos) \
131 list_for_each_entry_continue_reverse(pos, &(bond)->slave_list, list)
132 109
133#ifdef CONFIG_NET_POLL_CONTROLLER 110#ifdef CONFIG_NET_POLL_CONTROLLER
134extern atomic_t netpoll_block_tx; 111extern atomic_t netpoll_block_tx;
@@ -188,7 +165,6 @@ struct bond_parm_tbl {
188 165
189struct slave { 166struct slave {
190 struct net_device *dev; /* first - useful for panic debug */ 167 struct net_device *dev; /* first - useful for panic debug */
191 struct list_head list;
192 struct bonding *bond; /* our master */ 168 struct bonding *bond; /* our master */
193 int delay; 169 int delay;
194 unsigned long jiffies; 170 unsigned long jiffies;
@@ -228,7 +204,6 @@ struct slave {
228 */ 204 */
229struct bonding { 205struct bonding {
230 struct net_device *dev; /* first - useful for panic debug */ 206 struct net_device *dev; /* first - useful for panic debug */
231 struct list_head slave_list;
232 struct slave *curr_active_slave; 207 struct slave *curr_active_slave;
233 struct slave *current_arp_slave; 208 struct slave *current_arp_slave;
234 struct slave *primary_slave; 209 struct slave *primary_slave;
@@ -269,6 +244,34 @@ struct bonding {
269 ((struct slave *) rtnl_dereference(dev->rx_handler_data)) 244 ((struct slave *) rtnl_dereference(dev->rx_handler_data))
270 245
271/** 246/**
247 * __bond_next_slave - get the next slave after the one provided
248 * @bond - bonding struct
249 * @slave - the slave provided
250 *
251 * Returns the next slave after the slave provided, first slave if the
252 * slave provided is the last slave and NULL if slave is not found
253 */
254static inline struct slave *__bond_next_slave(struct bonding *bond,
255 struct slave *slave)
256{
257 struct slave *slave_iter;
258 struct list_head *iter;
259 bool found = false;
260
261 netdev_for_each_lower_private(bond->dev, slave_iter, iter) {
262 if (found)
263 return slave_iter;
264 if (slave_iter == slave)
265 found = true;
266 }
267
268 if (found)
269 return bond_first_slave(bond);
270
271 return NULL;
272}
273
274/**
272 * Returns NULL if the net_device does not belong to any of the bond's slaves 275 * Returns NULL if the net_device does not belong to any of the bond's slaves
273 * 276 *
274 * Caller must hold bond lock for read 277 * Caller must hold bond lock for read
@@ -276,13 +279,7 @@ struct bonding {
276static inline struct slave *bond_get_slave_by_dev(struct bonding *bond, 279static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
277 struct net_device *slave_dev) 280 struct net_device *slave_dev)
278{ 281{
279 struct slave *slave = NULL; 282 return netdev_lower_dev_get_private(bond->dev, slave_dev);
280
281 bond_for_each_slave(bond, slave)
282 if (slave->dev == slave_dev)
283 return slave;
284
285 return NULL;
286} 283}
287 284
288static inline struct bonding *bond_get_bond_by_slave(struct slave *slave) 285static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
@@ -439,8 +436,6 @@ int bond_create(struct net *net, const char *name);
439int bond_create_sysfs(struct bond_net *net); 436int bond_create_sysfs(struct bond_net *net);
440void bond_destroy_sysfs(struct bond_net *net); 437void bond_destroy_sysfs(struct bond_net *net);
441void bond_prepare_sysfs_group(struct bonding *bond); 438void bond_prepare_sysfs_group(struct bonding *bond);
442int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
443void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
444int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev); 439int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
445int bond_release(struct net_device *bond_dev, struct net_device *slave_dev); 440int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
446void bond_mii_monitor(struct work_struct *); 441void bond_mii_monitor(struct work_struct *);
@@ -492,9 +487,10 @@ static inline void bond_destroy_proc_dir(struct bond_net *bn)
492static inline struct slave *bond_slave_has_mac(struct bonding *bond, 487static inline struct slave *bond_slave_has_mac(struct bonding *bond,
493 const u8 *mac) 488 const u8 *mac)
494{ 489{
490 struct list_head *iter;
495 struct slave *tmp; 491 struct slave *tmp;
496 492
497 bond_for_each_slave(bond, tmp) 493 bond_for_each_slave(bond, tmp, iter)
498 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr)) 494 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
499 return tmp; 495 return tmp;
500 496
diff --git a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c
index c73cabdbd4c0..85d0cda5fbfa 100644
--- a/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c
+++ b/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c
@@ -3983,6 +3983,7 @@ static int cxgb4_inet6addr_handler(struct notifier_block *this,
3983 struct net_device *event_dev; 3983 struct net_device *event_dev;
3984 int ret = NOTIFY_DONE; 3984 int ret = NOTIFY_DONE;
3985 struct bonding *bond = netdev_priv(ifa->idev->dev); 3985 struct bonding *bond = netdev_priv(ifa->idev->dev);
3986 struct list_head *iter;
3986 struct slave *slave; 3987 struct slave *slave;
3987 struct pci_dev *first_pdev = NULL; 3988 struct pci_dev *first_pdev = NULL;
3988 3989
@@ -3995,7 +3996,7 @@ static int cxgb4_inet6addr_handler(struct notifier_block *this,
3995 * in all of them only once. 3996 * in all of them only once.
3996 */ 3997 */
3997 read_lock(&bond->lock); 3998 read_lock(&bond->lock);
3998 bond_for_each_slave(bond, slave) { 3999 bond_for_each_slave(bond, slave, iter) {
3999 if (!first_pdev) { 4000 if (!first_pdev) {
4000 ret = clip_add(slave->dev, ifa, event); 4001 ret = clip_add(slave->dev, ifa, event);
4001 /* If clip_add is success then only initialize 4002 /* If clip_add is success then only initialize
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 3de49aca4519..b4cfb63f264e 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -1143,8 +1143,18 @@ struct net_device {
1143 struct list_head dev_list; 1143 struct list_head dev_list;
1144 struct list_head napi_list; 1144 struct list_head napi_list;
1145 struct list_head unreg_list; 1145 struct list_head unreg_list;
1146 struct list_head upper_dev_list; /* List of upper devices */ 1146
1147 struct list_head lower_dev_list; 1147 /* directly linked devices, like slaves for bonding */
1148 struct {
1149 struct list_head upper;
1150 struct list_head lower;
1151 } adj_list;
1152
1153 /* all linked devices, *including* neighbours */
1154 struct {
1155 struct list_head upper;
1156 struct list_head lower;
1157 } all_adj_list;
1148 1158
1149 1159
1150 /* currently active device features */ 1160 /* currently active device features */
@@ -2813,24 +2823,49 @@ extern int bpf_jit_enable;
2813extern bool netdev_has_upper_dev(struct net_device *dev, 2823extern bool netdev_has_upper_dev(struct net_device *dev,
2814 struct net_device *upper_dev); 2824 struct net_device *upper_dev);
2815extern bool netdev_has_any_upper_dev(struct net_device *dev); 2825extern bool netdev_has_any_upper_dev(struct net_device *dev);
2816extern struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, 2826extern struct net_device *netdev_all_upper_get_next_dev_rcu(struct net_device *dev,
2817 struct list_head **iter); 2827 struct list_head **iter);
2818 2828
2819/* iterate through upper list, must be called under RCU read lock */ 2829/* iterate through upper list, must be called under RCU read lock */
2820#define netdev_for_each_upper_dev_rcu(dev, upper, iter) \ 2830#define netdev_for_each_all_upper_dev_rcu(dev, updev, iter) \
2821 for (iter = &(dev)->upper_dev_list, \ 2831 for (iter = &(dev)->all_adj_list.upper, \
2822 upper = netdev_upper_get_next_dev_rcu(dev, &(iter)); \ 2832 updev = netdev_all_upper_get_next_dev_rcu(dev, &(iter)); \
2823 upper; \ 2833 updev; \
2824 upper = netdev_upper_get_next_dev_rcu(dev, &(iter))) 2834 updev = netdev_all_upper_get_next_dev_rcu(dev, &(iter)))
2825 2835
2836extern void *netdev_lower_get_next_private(struct net_device *dev,
2837 struct list_head **iter);
2838extern void *netdev_lower_get_next_private_rcu(struct net_device *dev,
2839 struct list_head **iter);
2840
2841#define netdev_for_each_lower_private(dev, priv, iter) \
2842 for (iter = (dev)->adj_list.lower.next, \
2843 priv = netdev_lower_get_next_private(dev, &(iter)); \
2844 priv; \
2845 priv = netdev_lower_get_next_private(dev, &(iter)))
2846
2847#define netdev_for_each_lower_private_rcu(dev, priv, iter) \
2848 for (iter = &(dev)->adj_list.lower, \
2849 priv = netdev_lower_get_next_private_rcu(dev, &(iter)); \
2850 priv; \
2851 priv = netdev_lower_get_next_private_rcu(dev, &(iter)))
2852
2853extern void *netdev_adjacent_get_private(struct list_head *adj_list);
2826extern struct net_device *netdev_master_upper_dev_get(struct net_device *dev); 2854extern struct net_device *netdev_master_upper_dev_get(struct net_device *dev);
2827extern struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev); 2855extern struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev);
2828extern int netdev_upper_dev_link(struct net_device *dev, 2856extern int netdev_upper_dev_link(struct net_device *dev,
2829 struct net_device *upper_dev); 2857 struct net_device *upper_dev);
2830extern int netdev_master_upper_dev_link(struct net_device *dev, 2858extern int netdev_master_upper_dev_link(struct net_device *dev,
2831 struct net_device *upper_dev); 2859 struct net_device *upper_dev);
2860extern int netdev_master_upper_dev_link_private(struct net_device *dev,
2861 struct net_device *upper_dev,
2862 void *private);
2832extern void netdev_upper_dev_unlink(struct net_device *dev, 2863extern void netdev_upper_dev_unlink(struct net_device *dev,
2833 struct net_device *upper_dev); 2864 struct net_device *upper_dev);
2865extern void *netdev_lower_dev_get_private_rcu(struct net_device *dev,
2866 struct net_device *lower_dev);
2867extern void *netdev_lower_dev_get_private(struct net_device *dev,
2868 struct net_device *lower_dev);
2834extern int skb_checksum_help(struct sk_buff *skb); 2869extern int skb_checksum_help(struct sk_buff *skb);
2835extern struct sk_buff *__skb_gso_segment(struct sk_buff *skb, 2870extern struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2836 netdev_features_t features, bool tx_path); 2871 netdev_features_t features, bool tx_path);
diff --git a/net/8021q/vlan.c b/net/8021q/vlan.c
index 61fc573f1142..b3d17d1c49c3 100644
--- a/net/8021q/vlan.c
+++ b/net/8021q/vlan.c
@@ -98,14 +98,14 @@ void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
98 vlan_gvrp_request_leave(dev); 98 vlan_gvrp_request_leave(dev);
99 99
100 vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, NULL); 100 vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, NULL);
101
102 netdev_upper_dev_unlink(real_dev, dev);
101 /* Because unregister_netdevice_queue() makes sure at least one rcu 103 /* Because unregister_netdevice_queue() makes sure at least one rcu
102 * grace period is respected before device freeing, 104 * grace period is respected before device freeing,
103 * we dont need to call synchronize_net() here. 105 * we dont need to call synchronize_net() here.
104 */ 106 */
105 unregister_netdevice_queue(dev, head); 107 unregister_netdevice_queue(dev, head);
106 108
107 netdev_upper_dev_unlink(real_dev, dev);
108
109 if (grp->nr_vlan_devs == 0) { 109 if (grp->nr_vlan_devs == 0) {
110 vlan_mvrp_uninit_applicant(real_dev); 110 vlan_mvrp_uninit_applicant(real_dev);
111 vlan_gvrp_uninit_applicant(real_dev); 111 vlan_gvrp_uninit_applicant(real_dev);
@@ -169,13 +169,13 @@ int register_vlan_dev(struct net_device *dev)
169 if (err < 0) 169 if (err < 0)
170 goto out_uninit_mvrp; 170 goto out_uninit_mvrp;
171 171
172 err = netdev_upper_dev_link(real_dev, dev);
173 if (err)
174 goto out_uninit_mvrp;
175
176 err = register_netdevice(dev); 172 err = register_netdevice(dev);
177 if (err < 0) 173 if (err < 0)
178 goto out_upper_dev_unlink; 174 goto out_uninit_mvrp;
175
176 err = netdev_upper_dev_link(real_dev, dev);
177 if (err)
178 goto out_unregister_netdev;
179 179
180 /* Account for reference in struct vlan_dev_priv */ 180 /* Account for reference in struct vlan_dev_priv */
181 dev_hold(real_dev); 181 dev_hold(real_dev);
@@ -191,8 +191,8 @@ int register_vlan_dev(struct net_device *dev)
191 191
192 return 0; 192 return 0;
193 193
194out_upper_dev_unlink: 194out_unregister_netdev:
195 netdev_upper_dev_unlink(real_dev, dev); 195 unregister_netdevice(dev);
196out_uninit_mvrp: 196out_uninit_mvrp:
197 if (grp->nr_vlan_devs == 0) 197 if (grp->nr_vlan_devs == 0)
198 vlan_mvrp_uninit_applicant(real_dev); 198 vlan_mvrp_uninit_applicant(real_dev);
diff --git a/net/core/dev.c b/net/core/dev.c
index 5c713f2239cc..25ab6fe80da2 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -4373,42 +4373,40 @@ struct netdev_adjacent {
4373 /* upper master flag, there can only be one master device per list */ 4373 /* upper master flag, there can only be one master device per list */
4374 bool master; 4374 bool master;
4375 4375
4376 /* indicates that this dev is our first-level lower/upper device */
4377 bool neighbour;
4378
4379 /* counter for the number of times this device was added to us */ 4376 /* counter for the number of times this device was added to us */
4380 u16 ref_nr; 4377 u16 ref_nr;
4381 4378
4379 /* private field for the users */
4380 void *private;
4381
4382 struct list_head list; 4382 struct list_head list;
4383 struct rcu_head rcu; 4383 struct rcu_head rcu;
4384}; 4384};
4385 4385
4386static struct netdev_adjacent *__netdev_find_adj(struct net_device *dev, 4386static struct netdev_adjacent *__netdev_find_adj_rcu(struct net_device *dev,
4387 struct net_device *adj_dev, 4387 struct net_device *adj_dev,
4388 bool upper) 4388 struct list_head *adj_list)
4389{ 4389{
4390 struct netdev_adjacent *adj; 4390 struct netdev_adjacent *adj;
4391 struct list_head *dev_list;
4392 4391
4393 dev_list = upper ? &dev->upper_dev_list : &dev->lower_dev_list; 4392 list_for_each_entry_rcu(adj, adj_list, list) {
4394
4395 list_for_each_entry(adj, dev_list, list) {
4396 if (adj->dev == adj_dev) 4393 if (adj->dev == adj_dev)
4397 return adj; 4394 return adj;
4398 } 4395 }
4399 return NULL; 4396 return NULL;
4400} 4397}
4401 4398
4402static inline struct netdev_adjacent *__netdev_find_upper(struct net_device *dev, 4399static struct netdev_adjacent *__netdev_find_adj(struct net_device *dev,
4403 struct net_device *udev) 4400 struct net_device *adj_dev,
4401 struct list_head *adj_list)
4404{ 4402{
4405 return __netdev_find_adj(dev, udev, true); 4403 struct netdev_adjacent *adj;
4406}
4407 4404
4408static inline struct netdev_adjacent *__netdev_find_lower(struct net_device *dev, 4405 list_for_each_entry(adj, adj_list, list) {
4409 struct net_device *ldev) 4406 if (adj->dev == adj_dev)
4410{ 4407 return adj;
4411 return __netdev_find_adj(dev, ldev, false); 4408 }
4409 return NULL;
4412} 4410}
4413 4411
4414/** 4412/**
@@ -4425,7 +4423,7 @@ bool netdev_has_upper_dev(struct net_device *dev,
4425{ 4423{
4426 ASSERT_RTNL(); 4424 ASSERT_RTNL();
4427 4425
4428 return __netdev_find_upper(dev, upper_dev); 4426 return __netdev_find_adj(dev, upper_dev, &dev->all_adj_list.upper);
4429} 4427}
4430EXPORT_SYMBOL(netdev_has_upper_dev); 4428EXPORT_SYMBOL(netdev_has_upper_dev);
4431 4429
@@ -4440,7 +4438,7 @@ bool netdev_has_any_upper_dev(struct net_device *dev)
4440{ 4438{
4441 ASSERT_RTNL(); 4439 ASSERT_RTNL();
4442 4440
4443 return !list_empty(&dev->upper_dev_list); 4441 return !list_empty(&dev->all_adj_list.upper);
4444} 4442}
4445EXPORT_SYMBOL(netdev_has_any_upper_dev); 4443EXPORT_SYMBOL(netdev_has_any_upper_dev);
4446 4444
@@ -4457,10 +4455,10 @@ struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4457 4455
4458 ASSERT_RTNL(); 4456 ASSERT_RTNL();
4459 4457
4460 if (list_empty(&dev->upper_dev_list)) 4458 if (list_empty(&dev->adj_list.upper))
4461 return NULL; 4459 return NULL;
4462 4460
4463 upper = list_first_entry(&dev->upper_dev_list, 4461 upper = list_first_entry(&dev->adj_list.upper,
4464 struct netdev_adjacent, list); 4462 struct netdev_adjacent, list);
4465 if (likely(upper->master)) 4463 if (likely(upper->master))
4466 return upper->dev; 4464 return upper->dev;
@@ -4468,15 +4466,26 @@ struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4468} 4466}
4469EXPORT_SYMBOL(netdev_master_upper_dev_get); 4467EXPORT_SYMBOL(netdev_master_upper_dev_get);
4470 4468
4471/* netdev_upper_get_next_dev_rcu - Get the next dev from upper list 4469void *netdev_adjacent_get_private(struct list_head *adj_list)
4470{
4471 struct netdev_adjacent *adj;
4472
4473 adj = list_entry(adj_list, struct netdev_adjacent, list);
4474
4475 return adj->private;
4476}
4477EXPORT_SYMBOL(netdev_adjacent_get_private);
4478
4479/**
4480 * netdev_all_upper_get_next_dev_rcu - Get the next dev from upper list
4472 * @dev: device 4481 * @dev: device
4473 * @iter: list_head ** of the current position 4482 * @iter: list_head ** of the current position
4474 * 4483 *
4475 * Gets the next device from the dev's upper list, starting from iter 4484 * Gets the next device from the dev's upper list, starting from iter
4476 * position. The caller must hold RCU read lock. 4485 * position. The caller must hold RCU read lock.
4477 */ 4486 */
4478struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, 4487struct net_device *netdev_all_upper_get_next_dev_rcu(struct net_device *dev,
4479 struct list_head **iter) 4488 struct list_head **iter)
4480{ 4489{
4481 struct netdev_adjacent *upper; 4490 struct netdev_adjacent *upper;
4482 4491
@@ -4484,14 +4493,71 @@ struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
4484 4493
4485 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); 4494 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
4486 4495
4487 if (&upper->list == &dev->upper_dev_list) 4496 if (&upper->list == &dev->all_adj_list.upper)
4488 return NULL; 4497 return NULL;
4489 4498
4490 *iter = &upper->list; 4499 *iter = &upper->list;
4491 4500
4492 return upper->dev; 4501 return upper->dev;
4493} 4502}
4494EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); 4503EXPORT_SYMBOL(netdev_all_upper_get_next_dev_rcu);
4504
4505/**
4506 * netdev_lower_get_next_private - Get the next ->private from the
4507 * lower neighbour list
4508 * @dev: device
4509 * @iter: list_head ** of the current position
4510 *
4511 * Gets the next netdev_adjacent->private from the dev's lower neighbour
4512 * list, starting from iter position. The caller must hold either hold the
4513 * RTNL lock or its own locking that guarantees that the neighbour lower
4514 * list will remain unchainged.
4515 */
4516void *netdev_lower_get_next_private(struct net_device *dev,
4517 struct list_head **iter)
4518{
4519 struct netdev_adjacent *lower;
4520
4521 lower = list_entry(*iter, struct netdev_adjacent, list);
4522
4523 if (&lower->list == &dev->adj_list.lower)
4524 return NULL;
4525
4526 if (iter)
4527 *iter = lower->list.next;
4528
4529 return lower->private;
4530}
4531EXPORT_SYMBOL(netdev_lower_get_next_private);
4532
4533/**
4534 * netdev_lower_get_next_private_rcu - Get the next ->private from the
4535 * lower neighbour list, RCU
4536 * variant
4537 * @dev: device
4538 * @iter: list_head ** of the current position
4539 *
4540 * Gets the next netdev_adjacent->private from the dev's lower neighbour
4541 * list, starting from iter position. The caller must hold RCU read lock.
4542 */
4543void *netdev_lower_get_next_private_rcu(struct net_device *dev,
4544 struct list_head **iter)
4545{
4546 struct netdev_adjacent *lower;
4547
4548 WARN_ON_ONCE(!rcu_read_lock_held());
4549
4550 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
4551
4552 if (&lower->list == &dev->adj_list.lower)
4553 return NULL;
4554
4555 if (iter)
4556 *iter = &lower->list;
4557
4558 return lower->private;
4559}
4560EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
4495 4561
4496/** 4562/**
4497 * netdev_master_upper_dev_get_rcu - Get master upper device 4563 * netdev_master_upper_dev_get_rcu - Get master upper device
@@ -4504,7 +4570,7 @@ struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
4504{ 4570{
4505 struct netdev_adjacent *upper; 4571 struct netdev_adjacent *upper;
4506 4572
4507 upper = list_first_or_null_rcu(&dev->upper_dev_list, 4573 upper = list_first_or_null_rcu(&dev->adj_list.upper,
4508 struct netdev_adjacent, list); 4574 struct netdev_adjacent, list);
4509 if (upper && likely(upper->master)) 4575 if (upper && likely(upper->master))
4510 return upper->dev; 4576 return upper->dev;
@@ -4514,15 +4580,16 @@ EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
4514 4580
4515static int __netdev_adjacent_dev_insert(struct net_device *dev, 4581static int __netdev_adjacent_dev_insert(struct net_device *dev,
4516 struct net_device *adj_dev, 4582 struct net_device *adj_dev,
4517 bool neighbour, bool master, 4583 struct list_head *dev_list,
4518 bool upper) 4584 void *private, bool master)
4519{ 4585{
4520 struct netdev_adjacent *adj; 4586 struct netdev_adjacent *adj;
4587 char linkname[IFNAMSIZ+7];
4588 int ret;
4521 4589
4522 adj = __netdev_find_adj(dev, adj_dev, upper); 4590 adj = __netdev_find_adj(dev, adj_dev, dev_list);
4523 4591
4524 if (adj) { 4592 if (adj) {
4525 BUG_ON(neighbour);
4526 adj->ref_nr++; 4593 adj->ref_nr++;
4527 return 0; 4594 return 0;
4528 } 4595 }
@@ -4533,124 +4600,178 @@ static int __netdev_adjacent_dev_insert(struct net_device *dev,
4533 4600
4534 adj->dev = adj_dev; 4601 adj->dev = adj_dev;
4535 adj->master = master; 4602 adj->master = master;
4536 adj->neighbour = neighbour;
4537 adj->ref_nr = 1; 4603 adj->ref_nr = 1;
4538 4604 adj->private = private;
4539 dev_hold(adj_dev); 4605 dev_hold(adj_dev);
4540 pr_debug("dev_hold for %s, because of %s link added from %s to %s\n",
4541 adj_dev->name, upper ? "upper" : "lower", dev->name,
4542 adj_dev->name);
4543 4606
4544 if (!upper) { 4607 pr_debug("dev_hold for %s, because of link added from %s to %s\n",
4545 list_add_tail_rcu(&adj->list, &dev->lower_dev_list); 4608 adj_dev->name, dev->name, adj_dev->name);
4546 return 0; 4609
4610 if (dev_list == &dev->adj_list.lower) {
4611 sprintf(linkname, "lower_%s", adj_dev->name);
4612 ret = sysfs_create_link(&(dev->dev.kobj),
4613 &(adj_dev->dev.kobj), linkname);
4614 if (ret)
4615 goto free_adj;
4616 } else if (dev_list == &dev->adj_list.upper) {
4617 sprintf(linkname, "upper_%s", adj_dev->name);
4618 ret = sysfs_create_link(&(dev->dev.kobj),
4619 &(adj_dev->dev.kobj), linkname);
4620 if (ret)
4621 goto free_adj;
4547 } 4622 }
4548 4623
4549 /* Ensure that master upper link is always the first item in list. */ 4624 /* Ensure that master link is always the first item in list. */
4550 if (master) 4625 if (master) {
4551 list_add_rcu(&adj->list, &dev->upper_dev_list); 4626 ret = sysfs_create_link(&(dev->dev.kobj),
4552 else 4627 &(adj_dev->dev.kobj), "master");
4553 list_add_tail_rcu(&adj->list, &dev->upper_dev_list); 4628 if (ret)
4629 goto remove_symlinks;
4630
4631 list_add_rcu(&adj->list, dev_list);
4632 } else {
4633 list_add_tail_rcu(&adj->list, dev_list);
4634 }
4554 4635
4555 return 0; 4636 return 0;
4556}
4557 4637
4558static inline int __netdev_upper_dev_insert(struct net_device *dev, 4638remove_symlinks:
4559 struct net_device *udev, 4639 if (dev_list == &dev->adj_list.lower) {
4560 bool master, bool neighbour) 4640 sprintf(linkname, "lower_%s", adj_dev->name);
4561{ 4641 sysfs_remove_link(&(dev->dev.kobj), linkname);
4562 return __netdev_adjacent_dev_insert(dev, udev, neighbour, master, 4642 } else if (dev_list == &dev->adj_list.upper) {
4563 true); 4643 sprintf(linkname, "upper_%s", adj_dev->name);
4564} 4644 sysfs_remove_link(&(dev->dev.kobj), linkname);
4645 }
4565 4646
4566static inline int __netdev_lower_dev_insert(struct net_device *dev, 4647free_adj:
4567 struct net_device *ldev, 4648 kfree(adj);
4568 bool neighbour) 4649
4569{ 4650 return ret;
4570 return __netdev_adjacent_dev_insert(dev, ldev, neighbour, false,
4571 false);
4572} 4651}
4573 4652
4574void __netdev_adjacent_dev_remove(struct net_device *dev, 4653void __netdev_adjacent_dev_remove(struct net_device *dev,
4575 struct net_device *adj_dev, bool upper) 4654 struct net_device *adj_dev,
4655 struct list_head *dev_list)
4576{ 4656{
4577 struct netdev_adjacent *adj; 4657 struct netdev_adjacent *adj;
4658 char linkname[IFNAMSIZ+7];
4578 4659
4579 if (upper) 4660 adj = __netdev_find_adj(dev, adj_dev, dev_list);
4580 adj = __netdev_find_upper(dev, adj_dev);
4581 else
4582 adj = __netdev_find_lower(dev, adj_dev);
4583 4661
4584 if (!adj) 4662 if (!adj) {
4663 pr_err("tried to remove device %s from %s\n",
4664 dev->name, adj_dev->name);
4585 BUG(); 4665 BUG();
4666 }
4586 4667
4587 if (adj->ref_nr > 1) { 4668 if (adj->ref_nr > 1) {
4669 pr_debug("%s to %s ref_nr-- = %d\n", dev->name, adj_dev->name,
4670 adj->ref_nr-1);
4588 adj->ref_nr--; 4671 adj->ref_nr--;
4589 return; 4672 return;
4590 } 4673 }
4591 4674
4675 if (adj->master)
4676 sysfs_remove_link(&(dev->dev.kobj), "master");
4677
4678 if (dev_list == &dev->adj_list.lower) {
4679 sprintf(linkname, "lower_%s", adj_dev->name);
4680 sysfs_remove_link(&(dev->dev.kobj), linkname);
4681 } else if (dev_list == &dev->adj_list.upper) {
4682 sprintf(linkname, "upper_%s", adj_dev->name);
4683 sysfs_remove_link(&(dev->dev.kobj), linkname);
4684 }
4685
4592 list_del_rcu(&adj->list); 4686 list_del_rcu(&adj->list);
4593 pr_debug("dev_put for %s, because of %s link removed from %s to %s\n", 4687 pr_debug("dev_put for %s, because link removed from %s to %s\n",
4594 adj_dev->name, upper ? "upper" : "lower", dev->name, 4688 adj_dev->name, dev->name, adj_dev->name);
4595 adj_dev->name);
4596 dev_put(adj_dev); 4689 dev_put(adj_dev);
4597 kfree_rcu(adj, rcu); 4690 kfree_rcu(adj, rcu);
4598} 4691}
4599 4692
4600static inline void __netdev_upper_dev_remove(struct net_device *dev, 4693int __netdev_adjacent_dev_link_lists(struct net_device *dev,
4601 struct net_device *udev) 4694 struct net_device *upper_dev,
4602{ 4695 struct list_head *up_list,
4603 return __netdev_adjacent_dev_remove(dev, udev, true); 4696 struct list_head *down_list,
4604} 4697 void *private, bool master)
4605
4606static inline void __netdev_lower_dev_remove(struct net_device *dev,
4607 struct net_device *ldev)
4608{
4609 return __netdev_adjacent_dev_remove(dev, ldev, false);
4610}
4611
4612int __netdev_adjacent_dev_insert_link(struct net_device *dev,
4613 struct net_device *upper_dev,
4614 bool master, bool neighbour)
4615{ 4698{
4616 int ret; 4699 int ret;
4617 4700
4618 ret = __netdev_upper_dev_insert(dev, upper_dev, master, neighbour); 4701 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, private,
4702 master);
4619 if (ret) 4703 if (ret)
4620 return ret; 4704 return ret;
4621 4705
4622 ret = __netdev_lower_dev_insert(upper_dev, dev, neighbour); 4706 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, private,
4707 false);
4623 if (ret) { 4708 if (ret) {
4624 __netdev_upper_dev_remove(dev, upper_dev); 4709 __netdev_adjacent_dev_remove(dev, upper_dev, up_list);
4625 return ret; 4710 return ret;
4626 } 4711 }
4627 4712
4628 return 0; 4713 return 0;
4629} 4714}
4630 4715
4631static inline int __netdev_adjacent_dev_link(struct net_device *dev, 4716int __netdev_adjacent_dev_link(struct net_device *dev,
4632 struct net_device *udev) 4717 struct net_device *upper_dev)
4633{ 4718{
4634 return __netdev_adjacent_dev_insert_link(dev, udev, false, false); 4719 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
4720 &dev->all_adj_list.upper,
4721 &upper_dev->all_adj_list.lower,
4722 NULL, false);
4635} 4723}
4636 4724
4637static inline int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, 4725void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
4638 struct net_device *udev, 4726 struct net_device *upper_dev,
4639 bool master) 4727 struct list_head *up_list,
4728 struct list_head *down_list)
4640{ 4729{
4641 return __netdev_adjacent_dev_insert_link(dev, udev, master, true); 4730 __netdev_adjacent_dev_remove(dev, upper_dev, up_list);
4731 __netdev_adjacent_dev_remove(upper_dev, dev, down_list);
4642} 4732}
4643 4733
4644void __netdev_adjacent_dev_unlink(struct net_device *dev, 4734void __netdev_adjacent_dev_unlink(struct net_device *dev,
4645 struct net_device *upper_dev) 4735 struct net_device *upper_dev)
4646{ 4736{
4647 __netdev_upper_dev_remove(dev, upper_dev); 4737 __netdev_adjacent_dev_unlink_lists(dev, upper_dev,
4648 __netdev_lower_dev_remove(upper_dev, dev); 4738 &dev->all_adj_list.upper,
4739 &upper_dev->all_adj_list.lower);
4740}
4741
4742int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
4743 struct net_device *upper_dev,
4744 void *private, bool master)
4745{
4746 int ret = __netdev_adjacent_dev_link(dev, upper_dev);
4747
4748 if (ret)
4749 return ret;
4750
4751 ret = __netdev_adjacent_dev_link_lists(dev, upper_dev,
4752 &dev->adj_list.upper,
4753 &upper_dev->adj_list.lower,
4754 private, master);
4755 if (ret) {
4756 __netdev_adjacent_dev_unlink(dev, upper_dev);
4757 return ret;
4758 }
4759
4760 return 0;
4649} 4761}
4650 4762
4763void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
4764 struct net_device *upper_dev)
4765{
4766 __netdev_adjacent_dev_unlink(dev, upper_dev);
4767 __netdev_adjacent_dev_unlink_lists(dev, upper_dev,
4768 &dev->adj_list.upper,
4769 &upper_dev->adj_list.lower);
4770}
4651 4771
4652static int __netdev_upper_dev_link(struct net_device *dev, 4772static int __netdev_upper_dev_link(struct net_device *dev,
4653 struct net_device *upper_dev, bool master) 4773 struct net_device *upper_dev, bool master,
4774 void *private)
4654{ 4775{
4655 struct netdev_adjacent *i, *j, *to_i, *to_j; 4776 struct netdev_adjacent *i, *j, *to_i, *to_j;
4656 int ret = 0; 4777 int ret = 0;
@@ -4661,26 +4782,29 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4661 return -EBUSY; 4782 return -EBUSY;
4662 4783
4663 /* To prevent loops, check if dev is not upper device to upper_dev. */ 4784 /* To prevent loops, check if dev is not upper device to upper_dev. */
4664 if (__netdev_find_upper(upper_dev, dev)) 4785 if (__netdev_find_adj(upper_dev, dev, &upper_dev->all_adj_list.upper))
4665 return -EBUSY; 4786 return -EBUSY;
4666 4787
4667 if (__netdev_find_upper(dev, upper_dev)) 4788 if (__netdev_find_adj(dev, upper_dev, &dev->all_adj_list.upper))
4668 return -EEXIST; 4789 return -EEXIST;
4669 4790
4670 if (master && netdev_master_upper_dev_get(dev)) 4791 if (master && netdev_master_upper_dev_get(dev))
4671 return -EBUSY; 4792 return -EBUSY;
4672 4793
4673 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, master); 4794 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, private,
4795 master);
4674 if (ret) 4796 if (ret)
4675 return ret; 4797 return ret;
4676 4798
4677 /* Now that we linked these devs, make all the upper_dev's 4799 /* Now that we linked these devs, make all the upper_dev's
4678 * upper_dev_list visible to every dev's lower_dev_list and vice 4800 * all_adj_list.upper visible to every dev's all_adj_list.lower an
4679 * versa, and don't forget the devices itself. All of these 4801 * versa, and don't forget the devices itself. All of these
4680 * links are non-neighbours. 4802 * links are non-neighbours.
4681 */ 4803 */
4682 list_for_each_entry(i, &dev->lower_dev_list, list) { 4804 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4683 list_for_each_entry(j, &upper_dev->upper_dev_list, list) { 4805 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list) {
4806 pr_debug("Interlinking %s with %s, non-neighbour\n",
4807 i->dev->name, j->dev->name);
4684 ret = __netdev_adjacent_dev_link(i->dev, j->dev); 4808 ret = __netdev_adjacent_dev_link(i->dev, j->dev);
4685 if (ret) 4809 if (ret)
4686 goto rollback_mesh; 4810 goto rollback_mesh;
@@ -4688,14 +4812,18 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4688 } 4812 }
4689 4813
4690 /* add dev to every upper_dev's upper device */ 4814 /* add dev to every upper_dev's upper device */
4691 list_for_each_entry(i, &upper_dev->upper_dev_list, list) { 4815 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list) {
4816 pr_debug("linking %s's upper device %s with %s\n",
4817 upper_dev->name, i->dev->name, dev->name);
4692 ret = __netdev_adjacent_dev_link(dev, i->dev); 4818 ret = __netdev_adjacent_dev_link(dev, i->dev);
4693 if (ret) 4819 if (ret)
4694 goto rollback_upper_mesh; 4820 goto rollback_upper_mesh;
4695 } 4821 }
4696 4822
4697 /* add upper_dev to every dev's lower device */ 4823 /* add upper_dev to every dev's lower device */
4698 list_for_each_entry(i, &dev->lower_dev_list, list) { 4824 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4825 pr_debug("linking %s's lower device %s with %s\n", dev->name,
4826 i->dev->name, upper_dev->name);
4699 ret = __netdev_adjacent_dev_link(i->dev, upper_dev); 4827 ret = __netdev_adjacent_dev_link(i->dev, upper_dev);
4700 if (ret) 4828 if (ret)
4701 goto rollback_lower_mesh; 4829 goto rollback_lower_mesh;
@@ -4706,7 +4834,7 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4706 4834
4707rollback_lower_mesh: 4835rollback_lower_mesh:
4708 to_i = i; 4836 to_i = i;
4709 list_for_each_entry(i, &dev->lower_dev_list, list) { 4837 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4710 if (i == to_i) 4838 if (i == to_i)
4711 break; 4839 break;
4712 __netdev_adjacent_dev_unlink(i->dev, upper_dev); 4840 __netdev_adjacent_dev_unlink(i->dev, upper_dev);
@@ -4716,7 +4844,7 @@ rollback_lower_mesh:
4716 4844
4717rollback_upper_mesh: 4845rollback_upper_mesh:
4718 to_i = i; 4846 to_i = i;
4719 list_for_each_entry(i, &upper_dev->upper_dev_list, list) { 4847 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list) {
4720 if (i == to_i) 4848 if (i == to_i)
4721 break; 4849 break;
4722 __netdev_adjacent_dev_unlink(dev, i->dev); 4850 __netdev_adjacent_dev_unlink(dev, i->dev);
@@ -4727,8 +4855,8 @@ rollback_upper_mesh:
4727rollback_mesh: 4855rollback_mesh:
4728 to_i = i; 4856 to_i = i;
4729 to_j = j; 4857 to_j = j;
4730 list_for_each_entry(i, &dev->lower_dev_list, list) { 4858 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4731 list_for_each_entry(j, &upper_dev->upper_dev_list, list) { 4859 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list) {
4732 if (i == to_i && j == to_j) 4860 if (i == to_i && j == to_j)
4733 break; 4861 break;
4734 __netdev_adjacent_dev_unlink(i->dev, j->dev); 4862 __netdev_adjacent_dev_unlink(i->dev, j->dev);
@@ -4737,7 +4865,7 @@ rollback_mesh:
4737 break; 4865 break;
4738 } 4866 }
4739 4867
4740 __netdev_adjacent_dev_unlink(dev, upper_dev); 4868 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
4741 4869
4742 return ret; 4870 return ret;
4743} 4871}
@@ -4755,7 +4883,7 @@ rollback_mesh:
4755int netdev_upper_dev_link(struct net_device *dev, 4883int netdev_upper_dev_link(struct net_device *dev,
4756 struct net_device *upper_dev) 4884 struct net_device *upper_dev)
4757{ 4885{
4758 return __netdev_upper_dev_link(dev, upper_dev, false); 4886 return __netdev_upper_dev_link(dev, upper_dev, false, NULL);
4759} 4887}
4760EXPORT_SYMBOL(netdev_upper_dev_link); 4888EXPORT_SYMBOL(netdev_upper_dev_link);
4761 4889
@@ -4773,10 +4901,18 @@ EXPORT_SYMBOL(netdev_upper_dev_link);
4773int netdev_master_upper_dev_link(struct net_device *dev, 4901int netdev_master_upper_dev_link(struct net_device *dev,
4774 struct net_device *upper_dev) 4902 struct net_device *upper_dev)
4775{ 4903{
4776 return __netdev_upper_dev_link(dev, upper_dev, true); 4904 return __netdev_upper_dev_link(dev, upper_dev, true, NULL);
4777} 4905}
4778EXPORT_SYMBOL(netdev_master_upper_dev_link); 4906EXPORT_SYMBOL(netdev_master_upper_dev_link);
4779 4907
4908int netdev_master_upper_dev_link_private(struct net_device *dev,
4909 struct net_device *upper_dev,
4910 void *private)
4911{
4912 return __netdev_upper_dev_link(dev, upper_dev, true, private);
4913}
4914EXPORT_SYMBOL(netdev_master_upper_dev_link_private);
4915
4780/** 4916/**
4781 * netdev_upper_dev_unlink - Removes a link to upper device 4917 * netdev_upper_dev_unlink - Removes a link to upper device
4782 * @dev: device 4918 * @dev: device
@@ -4791,29 +4927,59 @@ void netdev_upper_dev_unlink(struct net_device *dev,
4791 struct netdev_adjacent *i, *j; 4927 struct netdev_adjacent *i, *j;
4792 ASSERT_RTNL(); 4928 ASSERT_RTNL();
4793 4929
4794 __netdev_adjacent_dev_unlink(dev, upper_dev); 4930 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
4795 4931
4796 /* Here is the tricky part. We must remove all dev's lower 4932 /* Here is the tricky part. We must remove all dev's lower
4797 * devices from all upper_dev's upper devices and vice 4933 * devices from all upper_dev's upper devices and vice
4798 * versa, to maintain the graph relationship. 4934 * versa, to maintain the graph relationship.
4799 */ 4935 */
4800 list_for_each_entry(i, &dev->lower_dev_list, list) 4936 list_for_each_entry(i, &dev->all_adj_list.lower, list)
4801 list_for_each_entry(j, &upper_dev->upper_dev_list, list) 4937 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list)
4802 __netdev_adjacent_dev_unlink(i->dev, j->dev); 4938 __netdev_adjacent_dev_unlink(i->dev, j->dev);
4803 4939
4804 /* remove also the devices itself from lower/upper device 4940 /* remove also the devices itself from lower/upper device
4805 * list 4941 * list
4806 */ 4942 */
4807 list_for_each_entry(i, &dev->lower_dev_list, list) 4943 list_for_each_entry(i, &dev->all_adj_list.lower, list)
4808 __netdev_adjacent_dev_unlink(i->dev, upper_dev); 4944 __netdev_adjacent_dev_unlink(i->dev, upper_dev);
4809 4945
4810 list_for_each_entry(i, &upper_dev->upper_dev_list, list) 4946 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list)
4811 __netdev_adjacent_dev_unlink(dev, i->dev); 4947 __netdev_adjacent_dev_unlink(dev, i->dev);
4812 4948
4813 call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev); 4949 call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev);
4814} 4950}
4815EXPORT_SYMBOL(netdev_upper_dev_unlink); 4951EXPORT_SYMBOL(netdev_upper_dev_unlink);
4816 4952
4953void *netdev_lower_dev_get_private_rcu(struct net_device *dev,
4954 struct net_device *lower_dev)
4955{
4956 struct netdev_adjacent *lower;
4957
4958 if (!lower_dev)
4959 return NULL;
4960 lower = __netdev_find_adj_rcu(dev, lower_dev, &dev->adj_list.lower);
4961 if (!lower)
4962 return NULL;
4963
4964 return lower->private;
4965}
4966EXPORT_SYMBOL(netdev_lower_dev_get_private_rcu);
4967
4968void *netdev_lower_dev_get_private(struct net_device *dev,
4969 struct net_device *lower_dev)
4970{
4971 struct netdev_adjacent *lower;
4972
4973 if (!lower_dev)
4974 return NULL;
4975 lower = __netdev_find_adj(dev, lower_dev, &dev->adj_list.lower);
4976 if (!lower)
4977 return NULL;
4978
4979 return lower->private;
4980}
4981EXPORT_SYMBOL(netdev_lower_dev_get_private);
4982
4817static void dev_change_rx_flags(struct net_device *dev, int flags) 4983static void dev_change_rx_flags(struct net_device *dev, int flags)
4818{ 4984{
4819 const struct net_device_ops *ops = dev->netdev_ops; 4985 const struct net_device_ops *ops = dev->netdev_ops;
@@ -6069,8 +6235,10 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6069 INIT_LIST_HEAD(&dev->napi_list); 6235 INIT_LIST_HEAD(&dev->napi_list);
6070 INIT_LIST_HEAD(&dev->unreg_list); 6236 INIT_LIST_HEAD(&dev->unreg_list);
6071 INIT_LIST_HEAD(&dev->link_watch_list); 6237 INIT_LIST_HEAD(&dev->link_watch_list);
6072 INIT_LIST_HEAD(&dev->upper_dev_list); 6238 INIT_LIST_HEAD(&dev->adj_list.upper);
6073 INIT_LIST_HEAD(&dev->lower_dev_list); 6239 INIT_LIST_HEAD(&dev->adj_list.lower);
6240 INIT_LIST_HEAD(&dev->all_adj_list.upper);
6241 INIT_LIST_HEAD(&dev->all_adj_list.lower);
6074 dev->priv_flags = IFF_XMIT_DST_RELEASE; 6242 dev->priv_flags = IFF_XMIT_DST_RELEASE;
6075 setup(dev); 6243 setup(dev);
6076 6244