aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/bonding
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-11-13 03:40:34 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2013-11-13 03:40:34 -0500
commit42a2d923cc349583ebf6fdd52a7d35e1c2f7e6bd (patch)
tree2b2b0c03b5389c1301800119333967efafd994ca /drivers/net/bonding
parent5cbb3d216e2041700231bcfc383ee5f8b7fc8b74 (diff)
parent75ecab1df14d90e86cebef9ec5c76befde46e65f (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
Pull networking updates from David Miller: 1) The addition of nftables. No longer will we need protocol aware firewall filtering modules, it can all live in userspace. At the core of nftables is a, for lack of a better term, virtual machine that executes byte codes to inspect packet or metadata (arriving interface index, etc.) and make verdict decisions. Besides support for loading packet contents and comparing them, the interpreter supports lookups in various datastructures as fundamental operations. For example sets are supports, and therefore one could create a set of whitelist IP address entries which have ACCEPT verdicts attached to them, and use the appropriate byte codes to do such lookups. Since the interpreted code is composed in userspace, userspace can do things like optimize things before giving it to the kernel. Another major improvement is the capability of atomically updating portions of the ruleset. In the existing netfilter implementation, one has to update the entire rule set in order to make a change and this is very expensive. Userspace tools exist to create nftables rules using existing netfilter rule sets, but both kernel implementations will need to co-exist for quite some time as we transition from the old to the new stuff. Kudos to Patrick McHardy, Pablo Neira Ayuso, and others who have worked so hard on this. 2) Daniel Borkmann and Hannes Frederic Sowa made several improvements to our pseudo-random number generator, mostly used for things like UDP port randomization and netfitler, amongst other things. In particular the taus88 generater is updated to taus113, and test cases are added. 3) Support 64-bit rates in HTB and TBF schedulers, from Eric Dumazet and Yang Yingliang. 4) Add support for new 577xx tigon3 chips to tg3 driver, from Nithin Sujir. 5) Fix two fatal flaws in TCP dynamic right sizing, from Eric Dumazet, Neal Cardwell, and Yuchung Cheng. 6) Allow IP_TOS and IP_TTL to be specified in sendmsg() ancillary control message data, much like other socket option attributes. From Francesco Fusco. 7) Allow applications to specify a cap on the rate computed automatically by the kernel for pacing flows, via a new SO_MAX_PACING_RATE socket option. From Eric Dumazet. 8) Make the initial autotuned send buffer sizing in TCP more closely reflect actual needs, from Eric Dumazet. 9) Currently early socket demux only happens for TCP sockets, but we can do it for connected UDP sockets too. Implementation from Shawn Bohrer. 10) Refactor inet socket demux with the goal of improving hash demux performance for listening sockets. With the main goals being able to use RCU lookups on even request sockets, and eliminating the listening lock contention. From Eric Dumazet. 11) The bonding layer has many demuxes in it's fast path, and an RCU conversion was started back in 3.11, several changes here extend the RCU usage to even more locations. From Ding Tianhong and Wang Yufen, based upon suggestions by Nikolay Aleksandrov and Veaceslav Falico. 12) Allow stackability of segmentation offloads to, in particular, allow segmentation offloading over tunnels. From Eric Dumazet. 13) Significantly improve the handling of secret keys we input into the various hash functions in the inet hashtables, TCP fast open, as well as syncookies. From Hannes Frederic Sowa. The key fundamental operation is "net_get_random_once()" which uses static keys. Hannes even extended this to ipv4/ipv6 fragmentation handling and our generic flow dissector. 14) The generic driver layer takes care now to set the driver data to NULL on device removal, so it's no longer necessary for drivers to explicitly set it to NULL any more. Many drivers have been cleaned up in this way, from Jingoo Han. 15) Add a BPF based packet scheduler classifier, from Daniel Borkmann. 16) Improve CRC32 interfaces and generic SKB checksum iterators so that SCTP's checksumming can more cleanly be handled. Also from Daniel Borkmann. 17) Add a new PMTU discovery mode, IP_PMTUDISC_INTERFACE, which forces using the interface MTU value. This helps avoid PMTU attacks, particularly on DNS servers. From Hannes Frederic Sowa. 18) Use generic XPS for transmit queue steering rather than internal (re-)implementation in virtio-net. From Jason Wang. * git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next: (1622 commits) random32: add test cases for taus113 implementation random32: upgrade taus88 generator to taus113 from errata paper random32: move rnd_state to linux/random.h random32: add prandom_reseed_late() and call when nonblocking pool becomes initialized random32: add periodic reseeding random32: fix off-by-one in seeding requirement PHY: Add RTL8201CP phy_driver to realtek xtsonic: add missing platform_set_drvdata() in xtsonic_probe() macmace: add missing platform_set_drvdata() in mace_probe() ethernet/arc/arc_emac: add missing platform_set_drvdata() in arc_emac_probe() ipv6: protect for_each_sk_fl_rcu in mem_check with rcu_read_lock_bh vlan: Implement vlan_dev_get_egress_qos_mask as an inline. ixgbe: add warning when max_vfs is out of range. igb: Update link modes display in ethtool netfilter: push reasm skb through instead of original frag skbs ip6_output: fragment outgoing reassembled skb properly MAINTAINERS: mv643xx_eth: take over maintainership from Lennart net_sched: tbf: support of 64bit rates ixgbe: deleting dfwd stations out of order can cause null ptr deref ixgbe: fix build err, num_rx_queues is only available with CONFIG_RPS ...
Diffstat (limited to 'drivers/net/bonding')
-rw-r--r--drivers/net/bonding/Makefile2
-rw-r--r--drivers/net/bonding/bond_3ad.c199
-rw-r--r--drivers/net/bonding/bond_alb.c150
-rw-r--r--drivers/net/bonding/bond_alb.h4
-rw-r--r--drivers/net/bonding/bond_main.c715
-rw-r--r--drivers/net/bonding/bond_netlink.c131
-rw-r--r--drivers/net/bonding/bond_options.c142
-rw-r--r--drivers/net/bonding/bond_procfs.c21
-rw-r--r--drivers/net/bonding/bond_sysfs.c264
-rw-r--r--drivers/net/bonding/bonding.h113
10 files changed, 932 insertions, 809 deletions
diff --git a/drivers/net/bonding/Makefile b/drivers/net/bonding/Makefile
index 4c21bf6b8b2f..5a5d720da929 100644
--- a/drivers/net/bonding/Makefile
+++ b/drivers/net/bonding/Makefile
@@ -4,7 +4,7 @@
4 4
5obj-$(CONFIG_BONDING) += bonding.o 5obj-$(CONFIG_BONDING) += bonding.o
6 6
7bonding-objs := bond_main.o bond_3ad.o bond_alb.o bond_sysfs.o bond_debugfs.o 7bonding-objs := bond_main.o bond_3ad.o bond_alb.o bond_sysfs.o bond_debugfs.o bond_netlink.o bond_options.o
8 8
9proc-$(CONFIG_PROC_FS) += bond_procfs.o 9proc-$(CONFIG_PROC_FS) += bond_procfs.o
10bonding-objs += $(proc-y) 10bonding-objs += $(proc-y)
diff --git a/drivers/net/bonding/bond_3ad.c b/drivers/net/bonding/bond_3ad.c
index 0d8f427ade93..187b1b7772ef 100644
--- a/drivers/net/bonding/bond_3ad.c
+++ b/drivers/net/bonding/bond_3ad.c
@@ -136,41 +136,6 @@ static inline struct bonding *__get_bond_by_port(struct port *port)
136} 136}
137 137
138/** 138/**
139 * __get_first_port - get the first port in the bond
140 * @bond: the bond we're looking at
141 *
142 * Return the port of the first slave in @bond, or %NULL if it can't be found.
143 */
144static inline struct port *__get_first_port(struct bonding *bond)
145{
146 struct slave *first_slave = bond_first_slave(bond);
147
148 return first_slave ? &(SLAVE_AD_INFO(first_slave).port) : NULL;
149}
150
151/**
152 * __get_next_port - get the next port in the bond
153 * @port: the port we're looking at
154 *
155 * Return the port of the slave that is next in line of @port's slave in the
156 * bond, or %NULL if it can't be found.
157 */
158static inline struct port *__get_next_port(struct port *port)
159{
160 struct bonding *bond = __get_bond_by_port(port);
161 struct slave *slave = port->slave, *slave_next;
162
163 // If there's no bond for this port, or this is the last slave
164 if (bond == NULL)
165 return NULL;
166 slave_next = bond_next_slave(bond, slave);
167 if (!slave_next || bond_is_first_slave(bond, slave_next))
168 return NULL;
169
170 return &(SLAVE_AD_INFO(slave_next).port);
171}
172
173/**
174 * __get_first_agg - get the first aggregator in the bond 139 * __get_first_agg - get the first aggregator in the bond
175 * @bond: the bond we're looking at 140 * @bond: the bond we're looking at
176 * 141 *
@@ -190,28 +155,6 @@ static inline struct aggregator *__get_first_agg(struct port *port)
190 return first_slave ? &(SLAVE_AD_INFO(first_slave).aggregator) : NULL; 155 return first_slave ? &(SLAVE_AD_INFO(first_slave).aggregator) : NULL;
191} 156}
192 157
193/**
194 * __get_next_agg - get the next aggregator in the bond
195 * @aggregator: the aggregator we're looking at
196 *
197 * Return the aggregator of the slave that is next in line of @aggregator's
198 * slave in the bond, or %NULL if it can't be found.
199 */
200static inline struct aggregator *__get_next_agg(struct aggregator *aggregator)
201{
202 struct slave *slave = aggregator->slave, *slave_next;
203 struct bonding *bond = bond_get_bond_by_slave(slave);
204
205 // If there's no bond for this aggregator, or this is the last slave
206 if (bond == NULL)
207 return NULL;
208 slave_next = bond_next_slave(bond, slave);
209 if (!slave_next || bond_is_first_slave(bond, slave_next))
210 return NULL;
211
212 return &(SLAVE_AD_INFO(slave_next).aggregator);
213}
214
215/* 158/*
216 * __agg_has_partner 159 * __agg_has_partner
217 * 160 *
@@ -755,16 +698,15 @@ static u32 __get_agg_bandwidth(struct aggregator *aggregator)
755 */ 698 */
756static struct aggregator *__get_active_agg(struct aggregator *aggregator) 699static struct aggregator *__get_active_agg(struct aggregator *aggregator)
757{ 700{
758 struct aggregator *retval = NULL; 701 struct bonding *bond = aggregator->slave->bond;
702 struct list_head *iter;
703 struct slave *slave;
759 704
760 for (; aggregator; aggregator = __get_next_agg(aggregator)) { 705 bond_for_each_slave(bond, slave, iter)
761 if (aggregator->is_active) { 706 if (SLAVE_AD_INFO(slave).aggregator.is_active)
762 retval = aggregator; 707 return &(SLAVE_AD_INFO(slave).aggregator);
763 break;
764 }
765 }
766 708
767 return retval; 709 return NULL;
768} 710}
769 711
770/** 712/**
@@ -1274,12 +1216,17 @@ static void ad_port_selection_logic(struct port *port)
1274{ 1216{
1275 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator; 1217 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1276 struct port *last_port = NULL, *curr_port; 1218 struct port *last_port = NULL, *curr_port;
1219 struct list_head *iter;
1220 struct bonding *bond;
1221 struct slave *slave;
1277 int found = 0; 1222 int found = 0;
1278 1223
1279 // if the port is already Selected, do nothing 1224 // if the port is already Selected, do nothing
1280 if (port->sm_vars & AD_PORT_SELECTED) 1225 if (port->sm_vars & AD_PORT_SELECTED)
1281 return; 1226 return;
1282 1227
1228 bond = __get_bond_by_port(port);
1229
1283 // if the port is connected to other aggregator, detach it 1230 // if the port is connected to other aggregator, detach it
1284 if (port->aggregator) { 1231 if (port->aggregator) {
1285 // detach the port from its former aggregator 1232 // detach the port from its former aggregator
@@ -1320,8 +1267,8 @@ static void ad_port_selection_logic(struct port *port)
1320 } 1267 }
1321 } 1268 }
1322 // search on all aggregators for a suitable aggregator for this port 1269 // search on all aggregators for a suitable aggregator for this port
1323 for (aggregator = __get_first_agg(port); aggregator; 1270 bond_for_each_slave(bond, slave, iter) {
1324 aggregator = __get_next_agg(aggregator)) { 1271 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
1325 1272
1326 // keep a free aggregator for later use(if needed) 1273 // keep a free aggregator for later use(if needed)
1327 if (!aggregator->lag_ports) { 1274 if (!aggregator->lag_ports) {
@@ -1515,19 +1462,23 @@ static int agg_device_up(const struct aggregator *agg)
1515static void ad_agg_selection_logic(struct aggregator *agg) 1462static void ad_agg_selection_logic(struct aggregator *agg)
1516{ 1463{
1517 struct aggregator *best, *active, *origin; 1464 struct aggregator *best, *active, *origin;
1465 struct bonding *bond = agg->slave->bond;
1466 struct list_head *iter;
1467 struct slave *slave;
1518 struct port *port; 1468 struct port *port;
1519 1469
1520 origin = agg; 1470 origin = agg;
1521 active = __get_active_agg(agg); 1471 active = __get_active_agg(agg);
1522 best = (active && agg_device_up(active)) ? active : NULL; 1472 best = (active && agg_device_up(active)) ? active : NULL;
1523 1473
1524 do { 1474 bond_for_each_slave(bond, slave, iter) {
1475 agg = &(SLAVE_AD_INFO(slave).aggregator);
1476
1525 agg->is_active = 0; 1477 agg->is_active = 0;
1526 1478
1527 if (agg->num_of_ports && agg_device_up(agg)) 1479 if (agg->num_of_ports && agg_device_up(agg))
1528 best = ad_agg_selection_test(best, agg); 1480 best = ad_agg_selection_test(best, agg);
1529 1481 }
1530 } while ((agg = __get_next_agg(agg)));
1531 1482
1532 if (best && 1483 if (best &&
1533 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) { 1484 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
@@ -1565,8 +1516,8 @@ static void ad_agg_selection_logic(struct aggregator *agg)
1565 best->lag_ports, best->slave, 1516 best->lag_ports, best->slave,
1566 best->slave ? best->slave->dev->name : "NULL"); 1517 best->slave ? best->slave->dev->name : "NULL");
1567 1518
1568 for (agg = __get_first_agg(best->lag_ports); agg; 1519 bond_for_each_slave(bond, slave, iter) {
1569 agg = __get_next_agg(agg)) { 1520 agg = &(SLAVE_AD_INFO(slave).aggregator);
1570 1521
1571 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n", 1522 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1572 agg->aggregator_identifier, agg->num_of_ports, 1523 agg->aggregator_identifier, agg->num_of_ports,
@@ -1614,13 +1565,7 @@ static void ad_agg_selection_logic(struct aggregator *agg)
1614 } 1565 }
1615 } 1566 }
1616 1567
1617 if (origin->slave) { 1568 bond_3ad_set_carrier(bond);
1618 struct bonding *bond;
1619
1620 bond = bond_get_bond_by_slave(origin->slave);
1621 if (bond)
1622 bond_3ad_set_carrier(bond);
1623 }
1624} 1569}
1625 1570
1626/** 1571/**
@@ -1969,6 +1914,9 @@ void bond_3ad_unbind_slave(struct slave *slave)
1969 struct port *port, *prev_port, *temp_port; 1914 struct port *port, *prev_port, *temp_port;
1970 struct aggregator *aggregator, *new_aggregator, *temp_aggregator; 1915 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1971 int select_new_active_agg = 0; 1916 int select_new_active_agg = 0;
1917 struct bonding *bond = slave->bond;
1918 struct slave *slave_iter;
1919 struct list_head *iter;
1972 1920
1973 // find the aggregator related to this slave 1921 // find the aggregator related to this slave
1974 aggregator = &(SLAVE_AD_INFO(slave).aggregator); 1922 aggregator = &(SLAVE_AD_INFO(slave).aggregator);
@@ -1998,14 +1946,16 @@ void bond_3ad_unbind_slave(struct slave *slave)
1998 // reason to search for new aggregator, and that we will find one 1946 // reason to search for new aggregator, and that we will find one
1999 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) { 1947 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
2000 // find new aggregator for the related port(s) 1948 // find new aggregator for the related port(s)
2001 new_aggregator = __get_first_agg(port); 1949 bond_for_each_slave(bond, slave_iter, iter) {
2002 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) { 1950 new_aggregator = &(SLAVE_AD_INFO(slave_iter).aggregator);
2003 // if the new aggregator is empty, or it is connected to our port only 1951 // if the new aggregator is empty, or it is connected to our port only
2004 if (!new_aggregator->lag_ports 1952 if (!new_aggregator->lag_ports
2005 || ((new_aggregator->lag_ports == port) 1953 || ((new_aggregator->lag_ports == port)
2006 && !new_aggregator->lag_ports->next_port_in_aggregator)) 1954 && !new_aggregator->lag_ports->next_port_in_aggregator))
2007 break; 1955 break;
2008 } 1956 }
1957 if (!slave_iter)
1958 new_aggregator = NULL;
2009 // if new aggregator found, copy the aggregator's parameters 1959 // if new aggregator found, copy the aggregator's parameters
2010 // and connect the related lag_ports to the new aggregator 1960 // and connect the related lag_ports to the new aggregator
2011 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) { 1961 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
@@ -2056,15 +2006,17 @@ void bond_3ad_unbind_slave(struct slave *slave)
2056 pr_info("%s: Removing an active aggregator\n", 2006 pr_info("%s: Removing an active aggregator\n",
2057 slave->bond->dev->name); 2007 slave->bond->dev->name);
2058 // select new active aggregator 2008 // select new active aggregator
2059 ad_agg_selection_logic(__get_first_agg(port)); 2009 temp_aggregator = __get_first_agg(port);
2010 if (temp_aggregator)
2011 ad_agg_selection_logic(temp_aggregator);
2060 } 2012 }
2061 } 2013 }
2062 } 2014 }
2063 2015
2064 pr_debug("Unbinding port %d\n", port->actor_port_number); 2016 pr_debug("Unbinding port %d\n", port->actor_port_number);
2065 // find the aggregator that this port is connected to 2017 // find the aggregator that this port is connected to
2066 temp_aggregator = __get_first_agg(port); 2018 bond_for_each_slave(bond, slave_iter, iter) {
2067 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) { 2019 temp_aggregator = &(SLAVE_AD_INFO(slave_iter).aggregator);
2068 prev_port = NULL; 2020 prev_port = NULL;
2069 // search the port in the aggregator's related ports 2021 // search the port in the aggregator's related ports
2070 for (temp_port = temp_aggregator->lag_ports; temp_port; 2022 for (temp_port = temp_aggregator->lag_ports; temp_port;
@@ -2111,19 +2063,24 @@ void bond_3ad_state_machine_handler(struct work_struct *work)
2111{ 2063{
2112 struct bonding *bond = container_of(work, struct bonding, 2064 struct bonding *bond = container_of(work, struct bonding,
2113 ad_work.work); 2065 ad_work.work);
2114 struct port *port;
2115 struct aggregator *aggregator; 2066 struct aggregator *aggregator;
2067 struct list_head *iter;
2068 struct slave *slave;
2069 struct port *port;
2116 2070
2117 read_lock(&bond->lock); 2071 read_lock(&bond->lock);
2118 2072
2119 //check if there are any slaves 2073 //check if there are any slaves
2120 if (list_empty(&bond->slave_list)) 2074 if (!bond_has_slaves(bond))
2121 goto re_arm; 2075 goto re_arm;
2122 2076
2123 // check if agg_select_timer timer after initialize is timed out 2077 // check if agg_select_timer timer after initialize is timed out
2124 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) { 2078 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
2079 slave = bond_first_slave(bond);
2080 port = slave ? &(SLAVE_AD_INFO(slave).port) : NULL;
2081
2125 // select the active aggregator for the bond 2082 // select the active aggregator for the bond
2126 if ((port = __get_first_port(bond))) { 2083 if (port) {
2127 if (!port->slave) { 2084 if (!port->slave) {
2128 pr_warning("%s: Warning: bond's first port is uninitialized\n", 2085 pr_warning("%s: Warning: bond's first port is uninitialized\n",
2129 bond->dev->name); 2086 bond->dev->name);
@@ -2137,7 +2094,8 @@ void bond_3ad_state_machine_handler(struct work_struct *work)
2137 } 2094 }
2138 2095
2139 // for each port run the state machines 2096 // for each port run the state machines
2140 for (port = __get_first_port(bond); port; port = __get_next_port(port)) { 2097 bond_for_each_slave(bond, slave, iter) {
2098 port = &(SLAVE_AD_INFO(slave).port);
2141 if (!port->slave) { 2099 if (!port->slave) {
2142 pr_warning("%s: Warning: Found an uninitialized port\n", 2100 pr_warning("%s: Warning: Found an uninitialized port\n",
2143 bond->dev->name); 2101 bond->dev->name);
@@ -2382,9 +2340,12 @@ int __bond_3ad_get_active_agg_info(struct bonding *bond,
2382 struct ad_info *ad_info) 2340 struct ad_info *ad_info)
2383{ 2341{
2384 struct aggregator *aggregator = NULL; 2342 struct aggregator *aggregator = NULL;
2343 struct list_head *iter;
2344 struct slave *slave;
2385 struct port *port; 2345 struct port *port;
2386 2346
2387 for (port = __get_first_port(bond); port; port = __get_next_port(port)) { 2347 bond_for_each_slave_rcu(bond, slave, iter) {
2348 port = &(SLAVE_AD_INFO(slave).port);
2388 if (port->aggregator && port->aggregator->is_active) { 2349 if (port->aggregator && port->aggregator->is_active) {
2389 aggregator = port->aggregator; 2350 aggregator = port->aggregator;
2390 break; 2351 break;
@@ -2408,25 +2369,25 @@ int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2408{ 2369{
2409 int ret; 2370 int ret;
2410 2371
2411 read_lock(&bond->lock); 2372 rcu_read_lock();
2412 ret = __bond_3ad_get_active_agg_info(bond, ad_info); 2373 ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2413 read_unlock(&bond->lock); 2374 rcu_read_unlock();
2414 2375
2415 return ret; 2376 return ret;
2416} 2377}
2417 2378
2418int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev) 2379int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2419{ 2380{
2420 struct slave *slave, *start_at;
2421 struct bonding *bond = netdev_priv(dev); 2381 struct bonding *bond = netdev_priv(dev);
2422 int slave_agg_no; 2382 struct slave *slave, *first_ok_slave;
2423 int slaves_in_agg; 2383 struct aggregator *agg;
2424 int agg_id;
2425 int i;
2426 struct ad_info ad_info; 2384 struct ad_info ad_info;
2385 struct list_head *iter;
2386 int slaves_in_agg;
2387 int slave_agg_no;
2427 int res = 1; 2388 int res = 1;
2389 int agg_id;
2428 2390
2429 read_lock(&bond->lock);
2430 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) { 2391 if (__bond_3ad_get_active_agg_info(bond, &ad_info)) {
2431 pr_debug("%s: Error: __bond_3ad_get_active_agg_info failed\n", 2392 pr_debug("%s: Error: __bond_3ad_get_active_agg_info failed\n",
2432 dev->name); 2393 dev->name);
@@ -2437,20 +2398,28 @@ int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2437 agg_id = ad_info.aggregator_id; 2398 agg_id = ad_info.aggregator_id;
2438 2399
2439 if (slaves_in_agg == 0) { 2400 if (slaves_in_agg == 0) {
2440 /*the aggregator is empty*/
2441 pr_debug("%s: Error: active aggregator is empty\n", dev->name); 2401 pr_debug("%s: Error: active aggregator is empty\n", dev->name);
2442 goto out; 2402 goto out;
2443 } 2403 }
2444 2404
2445 slave_agg_no = bond->xmit_hash_policy(skb, slaves_in_agg); 2405 slave_agg_no = bond_xmit_hash(bond, skb, slaves_in_agg);
2406 first_ok_slave = NULL;
2446 2407
2447 bond_for_each_slave(bond, slave) { 2408 bond_for_each_slave_rcu(bond, slave, iter) {
2448 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator; 2409 agg = SLAVE_AD_INFO(slave).port.aggregator;
2410 if (!agg || agg->aggregator_identifier != agg_id)
2411 continue;
2449 2412
2450 if (agg && (agg->aggregator_identifier == agg_id)) { 2413 if (slave_agg_no >= 0) {
2414 if (!first_ok_slave && SLAVE_IS_OK(slave))
2415 first_ok_slave = slave;
2451 slave_agg_no--; 2416 slave_agg_no--;
2452 if (slave_agg_no < 0) 2417 continue;
2453 break; 2418 }
2419
2420 if (SLAVE_IS_OK(slave)) {
2421 res = bond_dev_queue_xmit(bond, skb, slave->dev);
2422 goto out;
2454 } 2423 }
2455 } 2424 }
2456 2425
@@ -2460,23 +2429,12 @@ int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
2460 goto out; 2429 goto out;
2461 } 2430 }
2462 2431
2463 start_at = slave; 2432 /* we couldn't find any suitable slave after the agg_no, so use the
2464 2433 * first suitable found, if found. */
2465 bond_for_each_slave_from(bond, slave, i, start_at) { 2434 if (first_ok_slave)
2466 int slave_agg_id = 0; 2435 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 2436
2478out: 2437out:
2479 read_unlock(&bond->lock);
2480 if (res) { 2438 if (res) {
2481 /* no suitable interface, frame not sent */ 2439 /* no suitable interface, frame not sent */
2482 kfree_skb(skb); 2440 kfree_skb(skb);
@@ -2515,11 +2473,12 @@ int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2515void bond_3ad_update_lacp_rate(struct bonding *bond) 2473void bond_3ad_update_lacp_rate(struct bonding *bond)
2516{ 2474{
2517 struct port *port = NULL; 2475 struct port *port = NULL;
2476 struct list_head *iter;
2518 struct slave *slave; 2477 struct slave *slave;
2519 int lacp_fast; 2478 int lacp_fast;
2520 2479
2521 lacp_fast = bond->params.lacp_fast; 2480 lacp_fast = bond->params.lacp_fast;
2522 bond_for_each_slave(bond, slave) { 2481 bond_for_each_slave(bond, slave, iter) {
2523 port = &(SLAVE_AD_INFO(slave).port); 2482 port = &(SLAVE_AD_INFO(slave).port);
2524 __get_state_machine_lock(port); 2483 __get_state_machine_lock(port);
2525 if (lacp_fast) 2484 if (lacp_fast)
diff --git a/drivers/net/bonding/bond_alb.c b/drivers/net/bonding/bond_alb.c
index f428ef574372..02872405d35d 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_rcu(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,64 @@ 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;
388 bool found = false;
387 389
388 if (bond_info->next_rx_slave) 390 bond_for_each_slave(bond, slave, iter) {
389 start_at = bond_info->next_rx_slave; 391 if (!SLAVE_IS_OK(slave))
390 else 392 continue;
391 start_at = bond_first_slave(bond); 393 if (!found) {
394 if (!before || before->speed < slave->speed)
395 before = slave;
396 } else {
397 if (!rx_slave || rx_slave->speed < slave->speed)
398 rx_slave = slave;
399 }
400 if (slave == bond_info->rx_slave)
401 found = true;
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;
392 408
393 rx_slave = NULL; 409 if (rx_slave)
410 bond_info->rx_slave = rx_slave;
394 411
395 bond_for_each_slave_from(bond, slave, i, start_at) { 412 return rx_slave;
396 if (SLAVE_IS_OK(slave)) { 413}
397 if (!rx_slave) { 414
398 rx_slave = slave; 415/* Caller must hold rcu_read_lock() for read */
399 } else if (slave->speed > rx_slave->speed) { 416static struct slave *__rlb_next_rx_slave(struct bonding *bond)
417{
418 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
419 struct slave *before = NULL, *rx_slave = NULL, *slave;
420 struct list_head *iter;
421 bool found = false;
422
423 bond_for_each_slave_rcu(bond, slave, iter) {
424 if (!SLAVE_IS_OK(slave))
425 continue;
426 if (!found) {
427 if (!before || before->speed < slave->speed)
428 before = slave;
429 } else {
430 if (!rx_slave || rx_slave->speed < slave->speed)
400 rx_slave = slave; 431 rx_slave = slave;
401 }
402 } 432 }
433 if (slave == bond_info->rx_slave)
434 found = true;
403 } 435 }
436 /* we didn't find anything after the current or we have something
437 * better before and up to the current slave
438 */
439 if (!rx_slave || (before && rx_slave->speed < before->speed))
440 rx_slave = before;
404 441
405 if (rx_slave) { 442 if (rx_slave)
406 slave = bond_next_slave(bond, rx_slave); 443 bond_info->rx_slave = rx_slave;
407 bond_info->next_rx_slave = slave;
408 }
409 444
410 return rx_slave; 445 return rx_slave;
411} 446}
@@ -626,12 +661,14 @@ static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bon
626{ 661{
627 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 662 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
628 struct arp_pkt *arp = arp_pkt(skb); 663 struct arp_pkt *arp = arp_pkt(skb);
629 struct slave *assigned_slave; 664 struct slave *assigned_slave, *curr_active_slave;
630 struct rlb_client_info *client_info; 665 struct rlb_client_info *client_info;
631 u32 hash_index = 0; 666 u32 hash_index = 0;
632 667
633 _lock_rx_hashtbl(bond); 668 _lock_rx_hashtbl(bond);
634 669
670 curr_active_slave = rcu_dereference(bond->curr_active_slave);
671
635 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst)); 672 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
636 client_info = &(bond_info->rx_hashtbl[hash_index]); 673 client_info = &(bond_info->rx_hashtbl[hash_index]);
637 674
@@ -656,14 +693,14 @@ static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bon
656 * that the new client can be assigned to this entry. 693 * that the new client can be assigned to this entry.
657 */ 694 */
658 if (bond->curr_active_slave && 695 if (bond->curr_active_slave &&
659 client_info->slave != bond->curr_active_slave) { 696 client_info->slave != curr_active_slave) {
660 client_info->slave = bond->curr_active_slave; 697 client_info->slave = curr_active_slave;
661 rlb_update_client(client_info); 698 rlb_update_client(client_info);
662 } 699 }
663 } 700 }
664 } 701 }
665 /* assign a new slave */ 702 /* assign a new slave */
666 assigned_slave = rlb_next_rx_slave(bond); 703 assigned_slave = __rlb_next_rx_slave(bond);
667 704
668 if (assigned_slave) { 705 if (assigned_slave) {
669 if (!(client_info->assigned && 706 if (!(client_info->assigned &&
@@ -726,7 +763,7 @@ static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
726 /* Don't modify or load balance ARPs that do not originate locally 763 /* Don't modify or load balance ARPs that do not originate locally
727 * (e.g.,arrive via a bridge). 764 * (e.g.,arrive via a bridge).
728 */ 765 */
729 if (!bond_slave_has_mac(bond, arp->mac_src)) 766 if (!bond_slave_has_mac_rcu(bond, arp->mac_src))
730 return NULL; 767 return NULL;
731 768
732 if (arp->op_code == htons(ARPOP_REPLY)) { 769 if (arp->op_code == htons(ARPOP_REPLY)) {
@@ -1019,7 +1056,7 @@ static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
1019 1056
1020 /* loop through vlans and send one packet for each */ 1057 /* loop through vlans and send one packet for each */
1021 rcu_read_lock(); 1058 rcu_read_lock();
1022 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) { 1059 netdev_for_each_all_upper_dev_rcu(bond->dev, upper, iter) {
1023 if (upper->priv_flags & IFF_802_1Q_VLAN) 1060 if (upper->priv_flags & IFF_802_1Q_VLAN)
1024 alb_send_lp_vid(slave, mac_addr, 1061 alb_send_lp_vid(slave, mac_addr,
1025 vlan_dev_vlan_id(upper)); 1062 vlan_dev_vlan_id(upper));
@@ -1172,10 +1209,11 @@ static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *sla
1172 */ 1209 */
1173static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave) 1210static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1174{ 1211{
1175 struct slave *tmp_slave1, *free_mac_slave = NULL;
1176 struct slave *has_bond_addr = bond->curr_active_slave; 1212 struct slave *has_bond_addr = bond->curr_active_slave;
1213 struct slave *tmp_slave1, *free_mac_slave = NULL;
1214 struct list_head *iter;
1177 1215
1178 if (list_empty(&bond->slave_list)) { 1216 if (!bond_has_slaves(bond)) {
1179 /* this is the first slave */ 1217 /* this is the first slave */
1180 return 0; 1218 return 0;
1181 } 1219 }
@@ -1196,7 +1234,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. 1234 /* The slave's address is equal to the address of the bond.
1197 * Search for a spare address in the bond for this slave. 1235 * Search for a spare address in the bond for this slave.
1198 */ 1236 */
1199 bond_for_each_slave(bond, tmp_slave1) { 1237 bond_for_each_slave(bond, tmp_slave1, iter) {
1200 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) { 1238 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
1201 /* no slave has tmp_slave1's perm addr 1239 /* no slave has tmp_slave1's perm addr
1202 * as its curr addr 1240 * as its curr addr
@@ -1246,15 +1284,16 @@ static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slav
1246 */ 1284 */
1247static int alb_set_mac_address(struct bonding *bond, void *addr) 1285static int alb_set_mac_address(struct bonding *bond, void *addr)
1248{ 1286{
1249 char tmp_addr[ETH_ALEN]; 1287 struct slave *slave, *rollback_slave;
1250 struct slave *slave; 1288 struct list_head *iter;
1251 struct sockaddr sa; 1289 struct sockaddr sa;
1290 char tmp_addr[ETH_ALEN];
1252 int res; 1291 int res;
1253 1292
1254 if (bond->alb_info.rlb_enabled) 1293 if (bond->alb_info.rlb_enabled)
1255 return 0; 1294 return 0;
1256 1295
1257 bond_for_each_slave(bond, slave) { 1296 bond_for_each_slave(bond, slave, iter) {
1258 /* save net_device's current hw address */ 1297 /* save net_device's current hw address */
1259 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN); 1298 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1260 1299
@@ -1274,10 +1313,12 @@ unwind:
1274 sa.sa_family = bond->dev->type; 1313 sa.sa_family = bond->dev->type;
1275 1314
1276 /* unwind from head to the slave that failed */ 1315 /* unwind from head to the slave that failed */
1277 bond_for_each_slave_continue_reverse(bond, slave) { 1316 bond_for_each_slave(bond, rollback_slave, iter) {
1278 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN); 1317 if (rollback_slave == slave)
1279 dev_set_mac_address(slave->dev, &sa); 1318 break;
1280 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN); 1319 memcpy(tmp_addr, rollback_slave->dev->dev_addr, ETH_ALEN);
1320 dev_set_mac_address(rollback_slave->dev, &sa);
1321 memcpy(rollback_slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1281 } 1322 }
1282 1323
1283 return res; 1324 return res;
@@ -1337,11 +1378,6 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1337 skb_reset_mac_header(skb); 1378 skb_reset_mac_header(skb);
1338 eth_data = eth_hdr(skb); 1379 eth_data = eth_hdr(skb);
1339 1380
1340 /* make sure that the curr_active_slave do not change during tx
1341 */
1342 read_lock(&bond->lock);
1343 read_lock(&bond->curr_slave_lock);
1344
1345 switch (ntohs(skb->protocol)) { 1381 switch (ntohs(skb->protocol)) {
1346 case ETH_P_IP: { 1382 case ETH_P_IP: {
1347 const struct iphdr *iph = ip_hdr(skb); 1383 const struct iphdr *iph = ip_hdr(skb);
@@ -1423,12 +1459,12 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1423 1459
1424 if (!tx_slave) { 1460 if (!tx_slave) {
1425 /* unbalanced or unassigned, send through primary */ 1461 /* unbalanced or unassigned, send through primary */
1426 tx_slave = bond->curr_active_slave; 1462 tx_slave = rcu_dereference(bond->curr_active_slave);
1427 bond_info->unbalanced_load += skb->len; 1463 bond_info->unbalanced_load += skb->len;
1428 } 1464 }
1429 1465
1430 if (tx_slave && SLAVE_IS_OK(tx_slave)) { 1466 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1431 if (tx_slave != bond->curr_active_slave) { 1467 if (tx_slave != rcu_dereference(bond->curr_active_slave)) {
1432 memcpy(eth_data->h_source, 1468 memcpy(eth_data->h_source,
1433 tx_slave->dev->dev_addr, 1469 tx_slave->dev->dev_addr,
1434 ETH_ALEN); 1470 ETH_ALEN);
@@ -1443,8 +1479,6 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1443 } 1479 }
1444 } 1480 }
1445 1481
1446 read_unlock(&bond->curr_slave_lock);
1447 read_unlock(&bond->lock);
1448 if (res) { 1482 if (res) {
1449 /* no suitable interface, frame not sent */ 1483 /* no suitable interface, frame not sent */
1450 kfree_skb(skb); 1484 kfree_skb(skb);
@@ -1458,11 +1492,12 @@ void bond_alb_monitor(struct work_struct *work)
1458 struct bonding *bond = container_of(work, struct bonding, 1492 struct bonding *bond = container_of(work, struct bonding,
1459 alb_work.work); 1493 alb_work.work);
1460 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 1494 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1495 struct list_head *iter;
1461 struct slave *slave; 1496 struct slave *slave;
1462 1497
1463 read_lock(&bond->lock); 1498 read_lock(&bond->lock);
1464 1499
1465 if (list_empty(&bond->slave_list)) { 1500 if (!bond_has_slaves(bond)) {
1466 bond_info->tx_rebalance_counter = 0; 1501 bond_info->tx_rebalance_counter = 0;
1467 bond_info->lp_counter = 0; 1502 bond_info->lp_counter = 0;
1468 goto re_arm; 1503 goto re_arm;
@@ -1480,7 +1515,7 @@ void bond_alb_monitor(struct work_struct *work)
1480 */ 1515 */
1481 read_lock(&bond->curr_slave_lock); 1516 read_lock(&bond->curr_slave_lock);
1482 1517
1483 bond_for_each_slave(bond, slave) 1518 bond_for_each_slave(bond, slave, iter)
1484 alb_send_learning_packets(slave, slave->dev->dev_addr); 1519 alb_send_learning_packets(slave, slave->dev->dev_addr);
1485 1520
1486 read_unlock(&bond->curr_slave_lock); 1521 read_unlock(&bond->curr_slave_lock);
@@ -1493,7 +1528,7 @@ void bond_alb_monitor(struct work_struct *work)
1493 1528
1494 read_lock(&bond->curr_slave_lock); 1529 read_lock(&bond->curr_slave_lock);
1495 1530
1496 bond_for_each_slave(bond, slave) { 1531 bond_for_each_slave(bond, slave, iter) {
1497 tlb_clear_slave(bond, slave, 1); 1532 tlb_clear_slave(bond, slave, 1);
1498 if (slave == bond->curr_active_slave) { 1533 if (slave == bond->curr_active_slave) {
1499 SLAVE_TLB_INFO(slave).load = 1534 SLAVE_TLB_INFO(slave).load =
@@ -1599,13 +1634,13 @@ int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1599 */ 1634 */
1600void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave) 1635void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1601{ 1636{
1602 if (!list_empty(&bond->slave_list)) 1637 if (bond_has_slaves(bond))
1603 alb_change_hw_addr_on_detach(bond, slave); 1638 alb_change_hw_addr_on_detach(bond, slave);
1604 1639
1605 tlb_clear_slave(bond, slave, 0); 1640 tlb_clear_slave(bond, slave, 0);
1606 1641
1607 if (bond->alb_info.rlb_enabled) { 1642 if (bond->alb_info.rlb_enabled) {
1608 bond->alb_info.next_rx_slave = NULL; 1643 bond->alb_info.rx_slave = NULL;
1609 rlb_clear_slave(bond, slave); 1644 rlb_clear_slave(bond, slave);
1610 } 1645 }
1611} 1646}
@@ -1669,7 +1704,7 @@ void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave
1669 swap_slave = bond->curr_active_slave; 1704 swap_slave = bond->curr_active_slave;
1670 rcu_assign_pointer(bond->curr_active_slave, new_slave); 1705 rcu_assign_pointer(bond->curr_active_slave, new_slave);
1671 1706
1672 if (!new_slave || list_empty(&bond->slave_list)) 1707 if (!new_slave || !bond_has_slaves(bond))
1673 return; 1708 return;
1674 1709
1675 /* set the new curr_active_slave to the bonds mac address 1710 /* set the new curr_active_slave to the bonds mac address
@@ -1692,6 +1727,23 @@ void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave
1692 1727
1693 ASSERT_RTNL(); 1728 ASSERT_RTNL();
1694 1729
1730 /* in TLB mode, the slave might flip down/up with the old dev_addr,
1731 * and thus filter bond->dev_addr's packets, so force bond's mac
1732 */
1733 if (bond->params.mode == BOND_MODE_TLB) {
1734 struct sockaddr sa;
1735 u8 tmp_addr[ETH_ALEN];
1736
1737 memcpy(tmp_addr, new_slave->dev->dev_addr, ETH_ALEN);
1738
1739 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1740 sa.sa_family = bond->dev->type;
1741 /* we don't care if it can't change its mac, best effort */
1742 dev_set_mac_address(new_slave->dev, &sa);
1743
1744 memcpy(new_slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1745 }
1746
1695 /* curr_active_slave must be set before calling alb_swap_mac_addr */ 1747 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1696 if (swap_slave) { 1748 if (swap_slave) {
1697 /* swap mac address */ 1749 /* swap 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 e883bfe2e727..4dd5ee2a34cc 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -78,6 +78,8 @@
78#include <net/netns/generic.h> 78#include <net/netns/generic.h>
79#include <net/pkt_sched.h> 79#include <net/pkt_sched.h>
80#include <linux/rculist.h> 80#include <linux/rculist.h>
81#include <net/flow_keys.h>
82#include <linux/reciprocal_div.h>
81#include "bonding.h" 83#include "bonding.h"
82#include "bond_3ad.h" 84#include "bond_3ad.h"
83#include "bond_alb.h" 85#include "bond_alb.h"
@@ -110,6 +112,7 @@ static char *fail_over_mac;
110static int all_slaves_active; 112static int all_slaves_active;
111static struct bond_params bonding_defaults; 113static struct bond_params bonding_defaults;
112static int resend_igmp = BOND_DEFAULT_RESEND_IGMP; 114static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
115static int packets_per_slave = 1;
113 116
114module_param(max_bonds, int, 0); 117module_param(max_bonds, int, 0);
115MODULE_PARM_DESC(max_bonds, "Max number of bonded devices"); 118MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
@@ -159,7 +162,8 @@ MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on
159module_param(xmit_hash_policy, charp, 0); 162module_param(xmit_hash_policy, charp, 0);
160MODULE_PARM_DESC(xmit_hash_policy, "balance-xor and 802.3ad hashing method; " 163MODULE_PARM_DESC(xmit_hash_policy, "balance-xor and 802.3ad hashing method; "
161 "0 for layer 2 (default), 1 for layer 3+4, " 164 "0 for layer 2 (default), 1 for layer 3+4, "
162 "2 for layer 2+3"); 165 "2 for layer 2+3, 3 for encap layer 2+3, "
166 "4 for encap layer 3+4");
163module_param(arp_interval, int, 0); 167module_param(arp_interval, int, 0);
164MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds"); 168MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
165module_param_array(arp_ip_target, charp, NULL, 0); 169module_param_array(arp_ip_target, charp, NULL, 0);
@@ -181,6 +185,10 @@ MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface"
181module_param(resend_igmp, int, 0); 185module_param(resend_igmp, int, 0);
182MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on " 186MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
183 "link failure"); 187 "link failure");
188module_param(packets_per_slave, int, 0);
189MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
190 "mode; 0 for a random slave, 1 packet per "
191 "slave (default), >1 packets per slave.");
184 192
185/*----------------------------- Global variables ----------------------------*/ 193/*----------------------------- Global variables ----------------------------*/
186 194
@@ -217,6 +225,8 @@ const struct bond_parm_tbl xmit_hashtype_tbl[] = {
217{ "layer2", BOND_XMIT_POLICY_LAYER2}, 225{ "layer2", BOND_XMIT_POLICY_LAYER2},
218{ "layer3+4", BOND_XMIT_POLICY_LAYER34}, 226{ "layer3+4", BOND_XMIT_POLICY_LAYER34},
219{ "layer2+3", BOND_XMIT_POLICY_LAYER23}, 227{ "layer2+3", BOND_XMIT_POLICY_LAYER23},
228{ "encap2+3", BOND_XMIT_POLICY_ENCAP23},
229{ "encap3+4", BOND_XMIT_POLICY_ENCAP34},
220{ NULL, -1}, 230{ NULL, -1},
221}; 231};
222 232
@@ -332,10 +342,11 @@ static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
332 __be16 proto, u16 vid) 342 __be16 proto, u16 vid)
333{ 343{
334 struct bonding *bond = netdev_priv(bond_dev); 344 struct bonding *bond = netdev_priv(bond_dev);
335 struct slave *slave; 345 struct slave *slave, *rollback_slave;
346 struct list_head *iter;
336 int res; 347 int res;
337 348
338 bond_for_each_slave(bond, slave) { 349 bond_for_each_slave(bond, slave, iter) {
339 res = vlan_vid_add(slave->dev, proto, vid); 350 res = vlan_vid_add(slave->dev, proto, vid);
340 if (res) 351 if (res)
341 goto unwind; 352 goto unwind;
@@ -344,9 +355,13 @@ static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
344 return 0; 355 return 0;
345 356
346unwind: 357unwind:
347 /* unwind from the slave that failed */ 358 /* unwind to the slave that failed */
348 bond_for_each_slave_continue_reverse(bond, slave) 359 bond_for_each_slave(bond, rollback_slave, iter) {
349 vlan_vid_del(slave->dev, proto, vid); 360 if (rollback_slave == slave)
361 break;
362
363 vlan_vid_del(rollback_slave->dev, proto, vid);
364 }
350 365
351 return res; 366 return res;
352} 367}
@@ -360,9 +375,10 @@ static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
360 __be16 proto, u16 vid) 375 __be16 proto, u16 vid)
361{ 376{
362 struct bonding *bond = netdev_priv(bond_dev); 377 struct bonding *bond = netdev_priv(bond_dev);
378 struct list_head *iter;
363 struct slave *slave; 379 struct slave *slave;
364 380
365 bond_for_each_slave(bond, slave) 381 bond_for_each_slave(bond, slave, iter)
366 vlan_vid_del(slave->dev, proto, vid); 382 vlan_vid_del(slave->dev, proto, vid);
367 383
368 if (bond_is_lb(bond)) 384 if (bond_is_lb(bond))
@@ -382,15 +398,16 @@ static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
382 */ 398 */
383static int bond_set_carrier(struct bonding *bond) 399static int bond_set_carrier(struct bonding *bond)
384{ 400{
401 struct list_head *iter;
385 struct slave *slave; 402 struct slave *slave;
386 403
387 if (list_empty(&bond->slave_list)) 404 if (!bond_has_slaves(bond))
388 goto down; 405 goto down;
389 406
390 if (bond->params.mode == BOND_MODE_8023AD) 407 if (bond->params.mode == BOND_MODE_8023AD)
391 return bond_3ad_set_carrier(bond); 408 return bond_3ad_set_carrier(bond);
392 409
393 bond_for_each_slave(bond, slave) { 410 bond_for_each_slave(bond, slave, iter) {
394 if (slave->link == BOND_LINK_UP) { 411 if (slave->link == BOND_LINK_UP) {
395 if (!netif_carrier_ok(bond->dev)) { 412 if (!netif_carrier_ok(bond->dev)) {
396 netif_carrier_on(bond->dev); 413 netif_carrier_on(bond->dev);
@@ -522,7 +539,9 @@ static int bond_check_dev_link(struct bonding *bond,
522 */ 539 */
523static int bond_set_promiscuity(struct bonding *bond, int inc) 540static int bond_set_promiscuity(struct bonding *bond, int inc)
524{ 541{
542 struct list_head *iter;
525 int err = 0; 543 int err = 0;
544
526 if (USES_PRIMARY(bond->params.mode)) { 545 if (USES_PRIMARY(bond->params.mode)) {
527 /* write lock already acquired */ 546 /* write lock already acquired */
528 if (bond->curr_active_slave) { 547 if (bond->curr_active_slave) {
@@ -532,7 +551,7 @@ static int bond_set_promiscuity(struct bonding *bond, int inc)
532 } else { 551 } else {
533 struct slave *slave; 552 struct slave *slave;
534 553
535 bond_for_each_slave(bond, slave) { 554 bond_for_each_slave(bond, slave, iter) {
536 err = dev_set_promiscuity(slave->dev, inc); 555 err = dev_set_promiscuity(slave->dev, inc);
537 if (err) 556 if (err)
538 return err; 557 return err;
@@ -546,7 +565,9 @@ static int bond_set_promiscuity(struct bonding *bond, int inc)
546 */ 565 */
547static int bond_set_allmulti(struct bonding *bond, int inc) 566static int bond_set_allmulti(struct bonding *bond, int inc)
548{ 567{
568 struct list_head *iter;
549 int err = 0; 569 int err = 0;
570
550 if (USES_PRIMARY(bond->params.mode)) { 571 if (USES_PRIMARY(bond->params.mode)) {
551 /* write lock already acquired */ 572 /* write lock already acquired */
552 if (bond->curr_active_slave) { 573 if (bond->curr_active_slave) {
@@ -556,7 +577,7 @@ static int bond_set_allmulti(struct bonding *bond, int inc)
556 } else { 577 } else {
557 struct slave *slave; 578 struct slave *slave;
558 579
559 bond_for_each_slave(bond, slave) { 580 bond_for_each_slave(bond, slave, iter) {
560 err = dev_set_allmulti(slave->dev, inc); 581 err = dev_set_allmulti(slave->dev, inc);
561 if (err) 582 if (err)
562 return err; 583 return err;
@@ -774,43 +795,24 @@ static bool bond_should_change_active(struct bonding *bond)
774/** 795/**
775 * find_best_interface - select the best available slave to be the active one 796 * find_best_interface - select the best available slave to be the active one
776 * @bond: our bonding struct 797 * @bond: our bonding struct
777 *
778 * Warning: Caller must hold curr_slave_lock for writing.
779 */ 798 */
780static struct slave *bond_find_best_slave(struct bonding *bond) 799static struct slave *bond_find_best_slave(struct bonding *bond)
781{ 800{
782 struct slave *new_active, *old_active; 801 struct slave *slave, *bestslave = NULL;
783 struct slave *bestslave = NULL; 802 struct list_head *iter;
784 int mintime = bond->params.updelay; 803 int mintime = bond->params.updelay;
785 int i;
786 804
787 new_active = bond->curr_active_slave; 805 if (bond->primary_slave && bond->primary_slave->link == BOND_LINK_UP &&
788 806 bond_should_change_active(bond))
789 if (!new_active) { /* there were no active slaves left */ 807 return bond->primary_slave;
790 new_active = bond_first_slave(bond); 808
791 if (!new_active) 809 bond_for_each_slave(bond, slave, iter) {
792 return NULL; /* still no slave, return NULL */ 810 if (slave->link == BOND_LINK_UP)
793 } 811 return slave;
794 812 if (slave->link == BOND_LINK_BACK && IS_UP(slave->dev) &&
795 if ((bond->primary_slave) && 813 slave->delay < mintime) {
796 bond->primary_slave->link == BOND_LINK_UP && 814 mintime = slave->delay;
797 bond_should_change_active(bond)) { 815 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 } 816 }
815 } 817 }
816 818
@@ -971,35 +973,6 @@ void bond_select_active_slave(struct bonding *bond)
971 } 973 }
972} 974}
973 975
974/*--------------------------- slave list handling ---------------------------*/
975
976/*
977 * This function attaches the slave to the end of list.
978 *
979 * bond->lock held for writing by caller.
980 */
981static void bond_attach_slave(struct bonding *bond, struct slave *new_slave)
982{
983 list_add_tail_rcu(&new_slave->list, &bond->slave_list);
984 bond->slave_cnt++;
985}
986
987/*
988 * This function detaches the slave from the list.
989 * WARNING: no check is made to verify if the slave effectively
990 * belongs to <bond>.
991 * Nothing is freed on return, structures are just unchained.
992 * If any slave pointer in bond was pointing to <slave>,
993 * it should be changed by the calling function.
994 *
995 * bond->lock held for writing by caller.
996 */
997static void bond_detach_slave(struct bonding *bond, struct slave *slave)
998{
999 list_del_rcu(&slave->list);
1000 bond->slave_cnt--;
1001}
1002
1003#ifdef CONFIG_NET_POLL_CONTROLLER 976#ifdef CONFIG_NET_POLL_CONTROLLER
1004static inline int slave_enable_netpoll(struct slave *slave) 977static inline int slave_enable_netpoll(struct slave *slave)
1005{ 978{
@@ -1046,9 +1019,10 @@ static void bond_poll_controller(struct net_device *bond_dev)
1046static void bond_netpoll_cleanup(struct net_device *bond_dev) 1019static void bond_netpoll_cleanup(struct net_device *bond_dev)
1047{ 1020{
1048 struct bonding *bond = netdev_priv(bond_dev); 1021 struct bonding *bond = netdev_priv(bond_dev);
1022 struct list_head *iter;
1049 struct slave *slave; 1023 struct slave *slave;
1050 1024
1051 bond_for_each_slave(bond, slave) 1025 bond_for_each_slave(bond, slave, iter)
1052 if (IS_UP(slave->dev)) 1026 if (IS_UP(slave->dev))
1053 slave_disable_netpoll(slave); 1027 slave_disable_netpoll(slave);
1054} 1028}
@@ -1056,10 +1030,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) 1030static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni, gfp_t gfp)
1057{ 1031{
1058 struct bonding *bond = netdev_priv(dev); 1032 struct bonding *bond = netdev_priv(dev);
1033 struct list_head *iter;
1059 struct slave *slave; 1034 struct slave *slave;
1060 int err = 0; 1035 int err = 0;
1061 1036
1062 bond_for_each_slave(bond, slave) { 1037 bond_for_each_slave(bond, slave, iter) {
1063 err = slave_enable_netpoll(slave); 1038 err = slave_enable_netpoll(slave);
1064 if (err) { 1039 if (err) {
1065 bond_netpoll_cleanup(dev); 1040 bond_netpoll_cleanup(dev);
@@ -1087,10 +1062,11 @@ static netdev_features_t bond_fix_features(struct net_device *dev,
1087 netdev_features_t features) 1062 netdev_features_t features)
1088{ 1063{
1089 struct bonding *bond = netdev_priv(dev); 1064 struct bonding *bond = netdev_priv(dev);
1065 struct list_head *iter;
1090 netdev_features_t mask; 1066 netdev_features_t mask;
1091 struct slave *slave; 1067 struct slave *slave;
1092 1068
1093 if (list_empty(&bond->slave_list)) { 1069 if (!bond_has_slaves(bond)) {
1094 /* Disable adding VLANs to empty bond. But why? --mq */ 1070 /* Disable adding VLANs to empty bond. But why? --mq */
1095 features |= NETIF_F_VLAN_CHALLENGED; 1071 features |= NETIF_F_VLAN_CHALLENGED;
1096 return features; 1072 return features;
@@ -1100,7 +1076,7 @@ static netdev_features_t bond_fix_features(struct net_device *dev,
1100 features &= ~NETIF_F_ONE_FOR_ALL; 1076 features &= ~NETIF_F_ONE_FOR_ALL;
1101 features |= NETIF_F_ALL_FOR_ALL; 1077 features |= NETIF_F_ALL_FOR_ALL;
1102 1078
1103 bond_for_each_slave(bond, slave) { 1079 bond_for_each_slave(bond, slave, iter) {
1104 features = netdev_increment_features(features, 1080 features = netdev_increment_features(features,
1105 slave->dev->features, 1081 slave->dev->features,
1106 mask); 1082 mask);
@@ -1118,16 +1094,17 @@ static void bond_compute_features(struct bonding *bond)
1118{ 1094{
1119 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE; 1095 unsigned int flags, dst_release_flag = IFF_XMIT_DST_RELEASE;
1120 netdev_features_t vlan_features = BOND_VLAN_FEATURES; 1096 netdev_features_t vlan_features = BOND_VLAN_FEATURES;
1097 struct net_device *bond_dev = bond->dev;
1098 struct list_head *iter;
1099 struct slave *slave;
1121 unsigned short max_hard_header_len = ETH_HLEN; 1100 unsigned short max_hard_header_len = ETH_HLEN;
1122 unsigned int gso_max_size = GSO_MAX_SIZE; 1101 unsigned int gso_max_size = GSO_MAX_SIZE;
1123 struct net_device *bond_dev = bond->dev;
1124 u16 gso_max_segs = GSO_MAX_SEGS; 1102 u16 gso_max_segs = GSO_MAX_SEGS;
1125 struct slave *slave;
1126 1103
1127 if (list_empty(&bond->slave_list)) 1104 if (!bond_has_slaves(bond))
1128 goto done; 1105 goto done;
1129 1106
1130 bond_for_each_slave(bond, slave) { 1107 bond_for_each_slave(bond, slave, iter) {
1131 vlan_features = netdev_increment_features(vlan_features, 1108 vlan_features = netdev_increment_features(vlan_features,
1132 slave->dev->vlan_features, BOND_VLAN_FEATURES); 1109 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1133 1110
@@ -1233,15 +1210,16 @@ static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
1233} 1210}
1234 1211
1235static int bond_master_upper_dev_link(struct net_device *bond_dev, 1212static int bond_master_upper_dev_link(struct net_device *bond_dev,
1236 struct net_device *slave_dev) 1213 struct net_device *slave_dev,
1214 struct slave *slave)
1237{ 1215{
1238 int err; 1216 int err;
1239 1217
1240 err = netdev_master_upper_dev_link(slave_dev, bond_dev); 1218 err = netdev_master_upper_dev_link_private(slave_dev, bond_dev, slave);
1241 if (err) 1219 if (err)
1242 return err; 1220 return err;
1243 slave_dev->flags |= IFF_SLAVE; 1221 slave_dev->flags |= IFF_SLAVE;
1244 rtmsg_ifinfo(RTM_NEWLINK, slave_dev, IFF_SLAVE); 1222 rtmsg_ifinfo(RTM_NEWLINK, slave_dev, IFF_SLAVE, GFP_KERNEL);
1245 return 0; 1223 return 0;
1246} 1224}
1247 1225
@@ -1250,7 +1228,7 @@ static void bond_upper_dev_unlink(struct net_device *bond_dev,
1250{ 1228{
1251 netdev_upper_dev_unlink(slave_dev, bond_dev); 1229 netdev_upper_dev_unlink(slave_dev, bond_dev);
1252 slave_dev->flags &= ~IFF_SLAVE; 1230 slave_dev->flags &= ~IFF_SLAVE;
1253 rtmsg_ifinfo(RTM_NEWLINK, slave_dev, IFF_SLAVE); 1231 rtmsg_ifinfo(RTM_NEWLINK, slave_dev, IFF_SLAVE, GFP_KERNEL);
1254} 1232}
1255 1233
1256/* enslave device <slave> to bond device <master> */ 1234/* enslave device <slave> to bond device <master> */
@@ -1258,7 +1236,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1258{ 1236{
1259 struct bonding *bond = netdev_priv(bond_dev); 1237 struct bonding *bond = netdev_priv(bond_dev);
1260 const struct net_device_ops *slave_ops = slave_dev->netdev_ops; 1238 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1261 struct slave *new_slave = NULL; 1239 struct slave *new_slave = NULL, *prev_slave;
1262 struct sockaddr addr; 1240 struct sockaddr addr;
1263 int link_reporting; 1241 int link_reporting;
1264 int res = 0, i; 1242 int res = 0, i;
@@ -1313,7 +1291,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 1291 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1314 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond 1292 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1315 */ 1293 */
1316 if (list_empty(&bond->slave_list)) { 1294 if (!bond_has_slaves(bond)) {
1317 if (bond_dev->type != slave_dev->type) { 1295 if (bond_dev->type != slave_dev->type) {
1318 pr_debug("%s: change device type from %d to %d\n", 1296 pr_debug("%s: change device type from %d to %d\n",
1319 bond_dev->name, 1297 bond_dev->name,
@@ -1352,7 +1330,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1352 } 1330 }
1353 1331
1354 if (slave_ops->ndo_set_mac_address == NULL) { 1332 if (slave_ops->ndo_set_mac_address == NULL) {
1355 if (list_empty(&bond->slave_list)) { 1333 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.", 1334 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); 1335 bond_dev->name);
1358 bond->params.fail_over_mac = BOND_FOM_ACTIVE; 1336 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
@@ -1368,7 +1346,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1368 1346
1369 /* If this is the first slave, then we need to set the master's hardware 1347 /* 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. */ 1348 * address to be the same as the slave's. */
1371 if (list_empty(&bond->slave_list) && 1349 if (!bond_has_slaves(bond) &&
1372 bond->dev->addr_assign_type == NET_ADDR_RANDOM) 1350 bond->dev->addr_assign_type == NET_ADDR_RANDOM)
1373 bond_set_dev_addr(bond->dev, slave_dev); 1351 bond_set_dev_addr(bond->dev, slave_dev);
1374 1352
@@ -1377,7 +1355,6 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1377 res = -ENOMEM; 1355 res = -ENOMEM;
1378 goto err_undo_flags; 1356 goto err_undo_flags;
1379 } 1357 }
1380 INIT_LIST_HEAD(&new_slave->list);
1381 /* 1358 /*
1382 * Set the new_slave's queue_id to be zero. Queue ID mapping 1359 * Set the new_slave's queue_id to be zero. Queue ID mapping
1383 * is set via sysfs or module option if desired. 1360 * is set via sysfs or module option if desired.
@@ -1413,17 +1390,11 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1413 } 1390 }
1414 } 1391 }
1415 1392
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 */ 1393 /* open the slave since the application closed it */
1423 res = dev_open(slave_dev); 1394 res = dev_open(slave_dev);
1424 if (res) { 1395 if (res) {
1425 pr_debug("Opening slave %s failed\n", slave_dev->name); 1396 pr_debug("Opening slave %s failed\n", slave_dev->name);
1426 goto err_unset_master; 1397 goto err_restore_mac;
1427 } 1398 }
1428 1399
1429 new_slave->bond = bond; 1400 new_slave->bond = bond;
@@ -1479,21 +1450,13 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1479 goto err_close; 1450 goto err_close;
1480 } 1451 }
1481 1452
1482 write_lock_bh(&bond->lock); 1453 prev_slave = bond_last_slave(bond);
1483
1484 bond_attach_slave(bond, new_slave);
1485 1454
1486 new_slave->delay = 0; 1455 new_slave->delay = 0;
1487 new_slave->link_failure_count = 0; 1456 new_slave->link_failure_count = 0;
1488 1457
1489 write_unlock_bh(&bond->lock);
1490
1491 bond_compute_features(bond);
1492
1493 bond_update_speed_duplex(new_slave); 1458 bond_update_speed_duplex(new_slave);
1494 1459
1495 read_lock(&bond->lock);
1496
1497 new_slave->last_arp_rx = jiffies - 1460 new_slave->last_arp_rx = jiffies -
1498 (msecs_to_jiffies(bond->params.arp_interval) + 1); 1461 (msecs_to_jiffies(bond->params.arp_interval) + 1);
1499 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) 1462 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
@@ -1554,12 +1517,9 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1554 } 1517 }
1555 } 1518 }
1556 1519
1557 write_lock_bh(&bond->curr_slave_lock);
1558
1559 switch (bond->params.mode) { 1520 switch (bond->params.mode) {
1560 case BOND_MODE_ACTIVEBACKUP: 1521 case BOND_MODE_ACTIVEBACKUP:
1561 bond_set_slave_inactive_flags(new_slave); 1522 bond_set_slave_inactive_flags(new_slave);
1562 bond_select_active_slave(bond);
1563 break; 1523 break;
1564 case BOND_MODE_8023AD: 1524 case BOND_MODE_8023AD:
1565 /* in 802.3ad mode, the internal mechanism 1525 /* in 802.3ad mode, the internal mechanism
@@ -1568,16 +1528,13 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1568 */ 1528 */
1569 bond_set_slave_inactive_flags(new_slave); 1529 bond_set_slave_inactive_flags(new_slave);
1570 /* if this is the first slave */ 1530 /* if this is the first slave */
1571 if (bond_first_slave(bond) == new_slave) { 1531 if (!prev_slave) {
1572 SLAVE_AD_INFO(new_slave).id = 1; 1532 SLAVE_AD_INFO(new_slave).id = 1;
1573 /* Initialize AD with the number of times that the AD timer is called in 1 second 1533 /* Initialize AD with the number of times that the AD timer is called in 1 second
1574 * can be called only after the mac address of the bond is set 1534 * can be called only after the mac address of the bond is set
1575 */ 1535 */
1576 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL); 1536 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL);
1577 } else { 1537 } else {
1578 struct slave *prev_slave;
1579
1580 prev_slave = bond_prev_slave(bond, new_slave);
1581 SLAVE_AD_INFO(new_slave).id = 1538 SLAVE_AD_INFO(new_slave).id =
1582 SLAVE_AD_INFO(prev_slave).id + 1; 1539 SLAVE_AD_INFO(prev_slave).id + 1;
1583 } 1540 }
@@ -1588,7 +1545,6 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1588 case BOND_MODE_ALB: 1545 case BOND_MODE_ALB:
1589 bond_set_active_slave(new_slave); 1546 bond_set_active_slave(new_slave);
1590 bond_set_slave_inactive_flags(new_slave); 1547 bond_set_slave_inactive_flags(new_slave);
1591 bond_select_active_slave(bond);
1592 break; 1548 break;
1593 default: 1549 default:
1594 pr_debug("This slave is always active in trunk mode\n"); 1550 pr_debug("This slave is always active in trunk mode\n");
@@ -1606,10 +1562,6 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1606 break; 1562 break;
1607 } /* switch(bond_mode) */ 1563 } /* switch(bond_mode) */
1608 1564
1609 write_unlock_bh(&bond->curr_slave_lock);
1610
1611 bond_set_carrier(bond);
1612
1613#ifdef CONFIG_NET_POLL_CONTROLLER 1565#ifdef CONFIG_NET_POLL_CONTROLLER
1614 slave_dev->npinfo = bond->dev->npinfo; 1566 slave_dev->npinfo = bond->dev->npinfo;
1615 if (slave_dev->npinfo) { 1567 if (slave_dev->npinfo) {
@@ -1624,17 +1576,29 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1624 } 1576 }
1625#endif 1577#endif
1626 1578
1627 read_unlock(&bond->lock);
1628
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, 1579 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1634 new_slave); 1580 new_slave);
1635 if (res) { 1581 if (res) {
1636 pr_debug("Error %d calling netdev_rx_handler_register\n", res); 1582 pr_debug("Error %d calling netdev_rx_handler_register\n", res);
1637 goto err_dest_symlinks; 1583 goto err_detach;
1584 }
1585
1586 res = bond_master_upper_dev_link(bond_dev, slave_dev, new_slave);
1587 if (res) {
1588 pr_debug("Error %d calling bond_master_upper_dev_link\n", res);
1589 goto err_unregister;
1590 }
1591
1592 bond->slave_cnt++;
1593 bond_compute_features(bond);
1594 bond_set_carrier(bond);
1595
1596 if (USES_PRIMARY(bond->params.mode)) {
1597 read_lock(&bond->lock);
1598 write_lock_bh(&bond->curr_slave_lock);
1599 bond_select_active_slave(bond);
1600 write_unlock_bh(&bond->curr_slave_lock);
1601 read_unlock(&bond->lock);
1638 } 1602 }
1639 1603
1640 pr_info("%s: enslaving %s as a%s interface with a%s link.\n", 1604 pr_info("%s: enslaving %s as a%s interface with a%s link.\n",
@@ -1646,8 +1610,8 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev)
1646 return 0; 1610 return 0;
1647 1611
1648/* Undo stages on error */ 1612/* Undo stages on error */
1649err_dest_symlinks: 1613err_unregister:
1650 bond_destroy_slave_symlinks(bond_dev, slave_dev); 1614 netdev_rx_handler_unregister(slave_dev);
1651 1615
1652err_detach: 1616err_detach:
1653 if (!USES_PRIMARY(bond->params.mode)) 1617 if (!USES_PRIMARY(bond->params.mode))
@@ -1655,7 +1619,6 @@ err_detach:
1655 1619
1656 vlan_vids_del_by_dev(slave_dev, bond_dev); 1620 vlan_vids_del_by_dev(slave_dev, bond_dev);
1657 write_lock_bh(&bond->lock); 1621 write_lock_bh(&bond->lock);
1658 bond_detach_slave(bond, new_slave);
1659 if (bond->primary_slave == new_slave) 1622 if (bond->primary_slave == new_slave)
1660 bond->primary_slave = NULL; 1623 bond->primary_slave = NULL;
1661 if (bond->curr_active_slave == new_slave) { 1624 if (bond->curr_active_slave == new_slave) {
@@ -1675,9 +1638,6 @@ err_close:
1675 slave_dev->priv_flags &= ~IFF_BONDING; 1638 slave_dev->priv_flags &= ~IFF_BONDING;
1676 dev_close(slave_dev); 1639 dev_close(slave_dev);
1677 1640
1678err_unset_master:
1679 bond_upper_dev_unlink(bond_dev, slave_dev);
1680
1681err_restore_mac: 1641err_restore_mac:
1682 if (!bond->params.fail_over_mac) { 1642 if (!bond->params.fail_over_mac) {
1683 /* XXX TODO - fom follow mode needs to change master's 1643 /* XXX TODO - fom follow mode needs to change master's
@@ -1696,9 +1656,8 @@ err_free:
1696 kfree(new_slave); 1656 kfree(new_slave);
1697 1657
1698err_undo_flags: 1658err_undo_flags:
1699 bond_compute_features(bond);
1700 /* Enslave of first slave has failed and we need to fix master's mac */ 1659 /* Enslave of first slave has failed and we need to fix master's mac */
1701 if (list_empty(&bond->slave_list) && 1660 if (!bond_has_slaves(bond) &&
1702 ether_addr_equal(bond_dev->dev_addr, slave_dev->dev_addr)) 1661 ether_addr_equal(bond_dev->dev_addr, slave_dev->dev_addr))
1703 eth_hw_addr_random(bond_dev); 1662 eth_hw_addr_random(bond_dev);
1704 1663
@@ -1749,6 +1708,11 @@ static int __bond_release_one(struct net_device *bond_dev,
1749 } 1708 }
1750 1709
1751 write_unlock_bh(&bond->lock); 1710 write_unlock_bh(&bond->lock);
1711
1712 /* release the slave from its bond */
1713 bond->slave_cnt--;
1714
1715 bond_upper_dev_unlink(bond_dev, slave_dev);
1752 /* unregister rx_handler early so bond_handle_frame wouldn't be called 1716 /* unregister rx_handler early so bond_handle_frame wouldn't be called
1753 * for this slave anymore. 1717 * for this slave anymore.
1754 */ 1718 */
@@ -1772,12 +1736,9 @@ static int __bond_release_one(struct net_device *bond_dev,
1772 1736
1773 bond->current_arp_slave = NULL; 1737 bond->current_arp_slave = NULL;
1774 1738
1775 /* release the slave from its bond */
1776 bond_detach_slave(bond, slave);
1777
1778 if (!all && !bond->params.fail_over_mac) { 1739 if (!all && !bond->params.fail_over_mac) {
1779 if (ether_addr_equal(bond_dev->dev_addr, slave->perm_hwaddr) && 1740 if (ether_addr_equal(bond_dev->dev_addr, slave->perm_hwaddr) &&
1780 !list_empty(&bond->slave_list)) 1741 bond_has_slaves(bond))
1781 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", 1742 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",
1782 bond_dev->name, slave_dev->name, 1743 bond_dev->name, slave_dev->name,
1783 slave->perm_hwaddr, 1744 slave->perm_hwaddr,
@@ -1820,7 +1781,7 @@ static int __bond_release_one(struct net_device *bond_dev,
1820 write_lock_bh(&bond->lock); 1781 write_lock_bh(&bond->lock);
1821 } 1782 }
1822 1783
1823 if (list_empty(&bond->slave_list)) { 1784 if (!bond_has_slaves(bond)) {
1824 bond_set_carrier(bond); 1785 bond_set_carrier(bond);
1825 eth_hw_addr_random(bond_dev); 1786 eth_hw_addr_random(bond_dev);
1826 1787
@@ -1836,7 +1797,7 @@ static int __bond_release_one(struct net_device *bond_dev,
1836 unblock_netpoll_tx(); 1797 unblock_netpoll_tx();
1837 synchronize_rcu(); 1798 synchronize_rcu();
1838 1799
1839 if (list_empty(&bond->slave_list)) { 1800 if (!bond_has_slaves(bond)) {
1840 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev); 1801 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
1841 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev); 1802 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
1842 } 1803 }
@@ -1848,8 +1809,6 @@ static int __bond_release_one(struct net_device *bond_dev,
1848 bond_dev->name, slave_dev->name, bond_dev->name); 1809 bond_dev->name, slave_dev->name, bond_dev->name);
1849 1810
1850 /* must do this from outside any spinlocks */ 1811 /* must do this from outside any spinlocks */
1851 bond_destroy_slave_symlinks(bond_dev, slave_dev);
1852
1853 vlan_vids_del_by_dev(slave_dev, bond_dev); 1812 vlan_vids_del_by_dev(slave_dev, bond_dev);
1854 1813
1855 /* If the mode USES_PRIMARY, then this cases was handled above by 1814 /* If the mode USES_PRIMARY, then this cases was handled above by
@@ -1873,8 +1832,6 @@ static int __bond_release_one(struct net_device *bond_dev,
1873 bond_hw_addr_flush(bond_dev, slave_dev); 1832 bond_hw_addr_flush(bond_dev, slave_dev);
1874 } 1833 }
1875 1834
1876 bond_upper_dev_unlink(bond_dev, slave_dev);
1877
1878 slave_disable_netpoll(slave); 1835 slave_disable_netpoll(slave);
1879 1836
1880 /* close slave before restoring its mac address */ 1837 /* close slave before restoring its mac address */
@@ -1913,7 +1870,7 @@ static int bond_release_and_destroy(struct net_device *bond_dev,
1913 int ret; 1870 int ret;
1914 1871
1915 ret = bond_release(bond_dev, slave_dev); 1872 ret = bond_release(bond_dev, slave_dev);
1916 if (ret == 0 && list_empty(&bond->slave_list)) { 1873 if (ret == 0 && !bond_has_slaves(bond)) {
1917 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL; 1874 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
1918 pr_info("%s: destroying bond %s.\n", 1875 pr_info("%s: destroying bond %s.\n",
1919 bond_dev->name, bond_dev->name); 1876 bond_dev->name, bond_dev->name);
@@ -1922,61 +1879,6 @@ static int bond_release_and_destroy(struct net_device *bond_dev,
1922 return ret; 1879 return ret;
1923} 1880}
1924 1881
1925/*
1926 * This function changes the active slave to slave <slave_dev>.
1927 * It returns -EINVAL in the following cases.
1928 * - <slave_dev> is not found in the list.
1929 * - There is not active slave now.
1930 * - <slave_dev> is already active.
1931 * - The link state of <slave_dev> is not BOND_LINK_UP.
1932 * - <slave_dev> is not running.
1933 * In these cases, this function does nothing.
1934 * In the other cases, current_slave pointer is changed and 0 is returned.
1935 */
1936static int bond_ioctl_change_active(struct net_device *bond_dev, struct net_device *slave_dev)
1937{
1938 struct bonding *bond = netdev_priv(bond_dev);
1939 struct slave *old_active = NULL;
1940 struct slave *new_active = NULL;
1941 int res = 0;
1942
1943 if (!USES_PRIMARY(bond->params.mode))
1944 return -EINVAL;
1945
1946 /* Verify that bond_dev is indeed the master of slave_dev */
1947 if (!(slave_dev->flags & IFF_SLAVE) ||
1948 !netdev_has_upper_dev(slave_dev, bond_dev))
1949 return -EINVAL;
1950
1951 read_lock(&bond->lock);
1952
1953 old_active = bond->curr_active_slave;
1954 new_active = bond_get_slave_by_dev(bond, slave_dev);
1955 /*
1956 * Changing to the current active: do nothing; return success.
1957 */
1958 if (new_active && new_active == old_active) {
1959 read_unlock(&bond->lock);
1960 return 0;
1961 }
1962
1963 if (new_active &&
1964 old_active &&
1965 new_active->link == BOND_LINK_UP &&
1966 IS_UP(new_active->dev)) {
1967 block_netpoll_tx();
1968 write_lock_bh(&bond->curr_slave_lock);
1969 bond_change_active_slave(bond, new_active);
1970 write_unlock_bh(&bond->curr_slave_lock);
1971 unblock_netpoll_tx();
1972 } else
1973 res = -EINVAL;
1974
1975 read_unlock(&bond->lock);
1976
1977 return res;
1978}
1979
1980static int bond_info_query(struct net_device *bond_dev, struct ifbond *info) 1882static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
1981{ 1883{
1982 struct bonding *bond = netdev_priv(bond_dev); 1884 struct bonding *bond = netdev_priv(bond_dev);
@@ -1994,11 +1896,12 @@ static int bond_info_query(struct net_device *bond_dev, struct ifbond *info)
1994static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info) 1896static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
1995{ 1897{
1996 struct bonding *bond = netdev_priv(bond_dev); 1898 struct bonding *bond = netdev_priv(bond_dev);
1899 struct list_head *iter;
1997 int i = 0, res = -ENODEV; 1900 int i = 0, res = -ENODEV;
1998 struct slave *slave; 1901 struct slave *slave;
1999 1902
2000 read_lock(&bond->lock); 1903 read_lock(&bond->lock);
2001 bond_for_each_slave(bond, slave) { 1904 bond_for_each_slave(bond, slave, iter) {
2002 if (i++ == (int)info->slave_id) { 1905 if (i++ == (int)info->slave_id) {
2003 res = 0; 1906 res = 0;
2004 strcpy(info->slave_name, slave->dev->name); 1907 strcpy(info->slave_name, slave->dev->name);
@@ -2019,12 +1922,13 @@ static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *in
2019static int bond_miimon_inspect(struct bonding *bond) 1922static int bond_miimon_inspect(struct bonding *bond)
2020{ 1923{
2021 int link_state, commit = 0; 1924 int link_state, commit = 0;
1925 struct list_head *iter;
2022 struct slave *slave; 1926 struct slave *slave;
2023 bool ignore_updelay; 1927 bool ignore_updelay;
2024 1928
2025 ignore_updelay = !bond->curr_active_slave ? true : false; 1929 ignore_updelay = !bond->curr_active_slave ? true : false;
2026 1930
2027 bond_for_each_slave(bond, slave) { 1931 bond_for_each_slave(bond, slave, iter) {
2028 slave->new_link = BOND_LINK_NOCHANGE; 1932 slave->new_link = BOND_LINK_NOCHANGE;
2029 1933
2030 link_state = bond_check_dev_link(bond, slave->dev, 0); 1934 link_state = bond_check_dev_link(bond, slave->dev, 0);
@@ -2118,9 +2022,10 @@ static int bond_miimon_inspect(struct bonding *bond)
2118 2022
2119static void bond_miimon_commit(struct bonding *bond) 2023static void bond_miimon_commit(struct bonding *bond)
2120{ 2024{
2025 struct list_head *iter;
2121 struct slave *slave; 2026 struct slave *slave;
2122 2027
2123 bond_for_each_slave(bond, slave) { 2028 bond_for_each_slave(bond, slave, iter) {
2124 switch (slave->new_link) { 2029 switch (slave->new_link) {
2125 case BOND_LINK_NOCHANGE: 2030 case BOND_LINK_NOCHANGE:
2126 continue; 2031 continue;
@@ -2225,7 +2130,7 @@ void bond_mii_monitor(struct work_struct *work)
2225 2130
2226 delay = msecs_to_jiffies(bond->params.miimon); 2131 delay = msecs_to_jiffies(bond->params.miimon);
2227 2132
2228 if (list_empty(&bond->slave_list)) 2133 if (!bond_has_slaves(bond))
2229 goto re_arm; 2134 goto re_arm;
2230 2135
2231 should_notify_peers = bond_should_notify_peers(bond); 2136 should_notify_peers = bond_should_notify_peers(bond);
@@ -2274,7 +2179,7 @@ static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
2274 return true; 2179 return true;
2275 2180
2276 rcu_read_lock(); 2181 rcu_read_lock();
2277 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) { 2182 netdev_for_each_all_upper_dev_rcu(bond->dev, upper, iter) {
2278 if (ip == bond_confirm_addr(upper, 0, ip)) { 2183 if (ip == bond_confirm_addr(upper, 0, ip)) {
2279 ret = true; 2184 ret = true;
2280 break; 2185 break;
@@ -2349,10 +2254,12 @@ static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2349 * 2254 *
2350 * TODO: QinQ? 2255 * TODO: QinQ?
2351 */ 2256 */
2352 netdev_for_each_upper_dev_rcu(bond->dev, vlan_upper, vlan_iter) { 2257 netdev_for_each_all_upper_dev_rcu(bond->dev, vlan_upper,
2258 vlan_iter) {
2353 if (!is_vlan_dev(vlan_upper)) 2259 if (!is_vlan_dev(vlan_upper))
2354 continue; 2260 continue;
2355 netdev_for_each_upper_dev_rcu(vlan_upper, upper, iter) { 2261 netdev_for_each_all_upper_dev_rcu(vlan_upper, upper,
2262 iter) {
2356 if (upper == rt->dst.dev) { 2263 if (upper == rt->dst.dev) {
2357 vlan_id = vlan_dev_vlan_id(vlan_upper); 2264 vlan_id = vlan_dev_vlan_id(vlan_upper);
2358 rcu_read_unlock(); 2265 rcu_read_unlock();
@@ -2365,7 +2272,7 @@ static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2365 * our upper vlans, then just search for any dev that 2272 * our upper vlans, then just search for any dev that
2366 * matches, and in case it's a vlan - save the id 2273 * matches, and in case it's a vlan - save the id
2367 */ 2274 */
2368 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) { 2275 netdev_for_each_all_upper_dev_rcu(bond->dev, upper, iter) {
2369 if (upper == rt->dst.dev) { 2276 if (upper == rt->dst.dev) {
2370 /* if it's a vlan - get its VID */ 2277 /* if it's a vlan - get its VID */
2371 if (is_vlan_dev(upper)) 2278 if (is_vlan_dev(upper))
@@ -2512,11 +2419,12 @@ void bond_loadbalance_arp_mon(struct work_struct *work)
2512 struct bonding *bond = container_of(work, struct bonding, 2419 struct bonding *bond = container_of(work, struct bonding,
2513 arp_work.work); 2420 arp_work.work);
2514 struct slave *slave, *oldcurrent; 2421 struct slave *slave, *oldcurrent;
2422 struct list_head *iter;
2515 int do_failover = 0; 2423 int do_failover = 0;
2516 2424
2517 read_lock(&bond->lock); 2425 read_lock(&bond->lock);
2518 2426
2519 if (list_empty(&bond->slave_list)) 2427 if (!bond_has_slaves(bond))
2520 goto re_arm; 2428 goto re_arm;
2521 2429
2522 oldcurrent = bond->curr_active_slave; 2430 oldcurrent = bond->curr_active_slave;
@@ -2528,7 +2436,7 @@ void bond_loadbalance_arp_mon(struct work_struct *work)
2528 * TODO: what about up/down delay in arp mode? it wasn't here before 2436 * TODO: what about up/down delay in arp mode? it wasn't here before
2529 * so it can wait 2437 * so it can wait
2530 */ 2438 */
2531 bond_for_each_slave(bond, slave) { 2439 bond_for_each_slave(bond, slave, iter) {
2532 unsigned long trans_start = dev_trans_start(slave->dev); 2440 unsigned long trans_start = dev_trans_start(slave->dev);
2533 2441
2534 if (slave->link != BOND_LINK_UP) { 2442 if (slave->link != BOND_LINK_UP) {
@@ -2619,10 +2527,11 @@ re_arm:
2619static int bond_ab_arp_inspect(struct bonding *bond) 2527static int bond_ab_arp_inspect(struct bonding *bond)
2620{ 2528{
2621 unsigned long trans_start, last_rx; 2529 unsigned long trans_start, last_rx;
2530 struct list_head *iter;
2622 struct slave *slave; 2531 struct slave *slave;
2623 int commit = 0; 2532 int commit = 0;
2624 2533
2625 bond_for_each_slave(bond, slave) { 2534 bond_for_each_slave(bond, slave, iter) {
2626 slave->new_link = BOND_LINK_NOCHANGE; 2535 slave->new_link = BOND_LINK_NOCHANGE;
2627 last_rx = slave_last_rx(bond, slave); 2536 last_rx = slave_last_rx(bond, slave);
2628 2537
@@ -2689,9 +2598,10 @@ static int bond_ab_arp_inspect(struct bonding *bond)
2689static void bond_ab_arp_commit(struct bonding *bond) 2598static void bond_ab_arp_commit(struct bonding *bond)
2690{ 2599{
2691 unsigned long trans_start; 2600 unsigned long trans_start;
2601 struct list_head *iter;
2692 struct slave *slave; 2602 struct slave *slave;
2693 2603
2694 bond_for_each_slave(bond, slave) { 2604 bond_for_each_slave(bond, slave, iter) {
2695 switch (slave->new_link) { 2605 switch (slave->new_link) {
2696 case BOND_LINK_NOCHANGE: 2606 case BOND_LINK_NOCHANGE:
2697 continue; 2607 continue;
@@ -2762,8 +2672,9 @@ do_failover:
2762 */ 2672 */
2763static void bond_ab_arp_probe(struct bonding *bond) 2673static void bond_ab_arp_probe(struct bonding *bond)
2764{ 2674{
2765 struct slave *slave, *next_slave; 2675 struct slave *slave, *before = NULL, *new_slave = NULL;
2766 int i; 2676 struct list_head *iter;
2677 bool found = false;
2767 2678
2768 read_lock(&bond->curr_slave_lock); 2679 read_lock(&bond->curr_slave_lock);
2769 2680
@@ -2793,18 +2704,12 @@ static void bond_ab_arp_probe(struct bonding *bond)
2793 2704
2794 bond_set_slave_inactive_flags(bond->current_arp_slave); 2705 bond_set_slave_inactive_flags(bond->current_arp_slave);
2795 2706
2796 /* search for next candidate */ 2707 bond_for_each_slave(bond, slave, iter) {
2797 next_slave = bond_next_slave(bond, bond->current_arp_slave); 2708 if (!found && !before && IS_UP(slave->dev))
2798 bond_for_each_slave_from(bond, slave, i, next_slave) { 2709 before = slave;
2799 if (IS_UP(slave->dev)) {
2800 slave->link = BOND_LINK_BACK;
2801 bond_set_slave_active_flags(slave);
2802 bond_arp_send_all(bond, slave);
2803 slave->jiffies = jiffies;
2804 bond->current_arp_slave = slave;
2805 break;
2806 }
2807 2710
2711 if (found && !new_slave && IS_UP(slave->dev))
2712 new_slave = slave;
2808 /* if the link state is up at this point, we 2713 /* if the link state is up at this point, we
2809 * mark it down - this can happen if we have 2714 * mark it down - this can happen if we have
2810 * simultaneous link failures and 2715 * simultaneous link failures and
@@ -2812,7 +2717,7 @@ static void bond_ab_arp_probe(struct bonding *bond)
2812 * one the current slave so it is still marked 2717 * one the current slave so it is still marked
2813 * up when it is actually down 2718 * up when it is actually down
2814 */ 2719 */
2815 if (slave->link == BOND_LINK_UP) { 2720 if (!IS_UP(slave->dev) && slave->link == BOND_LINK_UP) {
2816 slave->link = BOND_LINK_DOWN; 2721 slave->link = BOND_LINK_DOWN;
2817 if (slave->link_failure_count < UINT_MAX) 2722 if (slave->link_failure_count < UINT_MAX)
2818 slave->link_failure_count++; 2723 slave->link_failure_count++;
@@ -2822,7 +2727,22 @@ static void bond_ab_arp_probe(struct bonding *bond)
2822 pr_info("%s: backup interface %s is now down.\n", 2727 pr_info("%s: backup interface %s is now down.\n",
2823 bond->dev->name, slave->dev->name); 2728 bond->dev->name, slave->dev->name);
2824 } 2729 }
2730 if (slave == bond->current_arp_slave)
2731 found = true;
2825 } 2732 }
2733
2734 if (!new_slave && before)
2735 new_slave = before;
2736
2737 if (!new_slave)
2738 return;
2739
2740 new_slave->link = BOND_LINK_BACK;
2741 bond_set_slave_active_flags(new_slave);
2742 bond_arp_send_all(bond, new_slave);
2743 new_slave->jiffies = jiffies;
2744 bond->current_arp_slave = new_slave;
2745
2826} 2746}
2827 2747
2828void bond_activebackup_arp_mon(struct work_struct *work) 2748void bond_activebackup_arp_mon(struct work_struct *work)
@@ -2836,7 +2756,7 @@ void bond_activebackup_arp_mon(struct work_struct *work)
2836 2756
2837 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval); 2757 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2838 2758
2839 if (list_empty(&bond->slave_list)) 2759 if (!bond_has_slaves(bond))
2840 goto re_arm; 2760 goto re_arm;
2841 2761
2842 should_notify_peers = bond_should_notify_peers(bond); 2762 should_notify_peers = bond_should_notify_peers(bond);
@@ -3033,99 +2953,85 @@ static struct notifier_block bond_netdev_notifier = {
3033 2953
3034/*---------------------------- Hashing Policies -----------------------------*/ 2954/*---------------------------- Hashing Policies -----------------------------*/
3035 2955
3036/* 2956/* L2 hash helper */
3037 * Hash for the output device based upon layer 2 data 2957static inline u32 bond_eth_hash(struct sk_buff *skb)
3038 */
3039static int bond_xmit_hash_policy_l2(struct sk_buff *skb, int count)
3040{ 2958{
3041 struct ethhdr *data = (struct ethhdr *)skb->data; 2959 struct ethhdr *data = (struct ethhdr *)skb->data;
3042 2960
3043 if (skb_headlen(skb) >= offsetof(struct ethhdr, h_proto)) 2961 if (skb_headlen(skb) >= offsetof(struct ethhdr, h_proto))
3044 return (data->h_dest[5] ^ data->h_source[5]) % count; 2962 return data->h_dest[5] ^ data->h_source[5];
3045 2963
3046 return 0; 2964 return 0;
3047} 2965}
3048 2966
3049/* 2967/* Extract the appropriate headers based on bond's xmit policy */
3050 * Hash for the output device based upon layer 2 and layer 3 data. If 2968static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb,
3051 * the packet is not IP, fall back on bond_xmit_hash_policy_l2() 2969 struct flow_keys *fk)
3052 */
3053static int bond_xmit_hash_policy_l23(struct sk_buff *skb, int count)
3054{ 2970{
3055 const struct ethhdr *data; 2971 const struct ipv6hdr *iph6;
3056 const struct iphdr *iph; 2972 const struct iphdr *iph;
3057 const struct ipv6hdr *ipv6h; 2973 int noff, proto = -1;
3058 u32 v6hash; 2974
3059 const __be32 *s, *d; 2975 if (bond->params.xmit_policy > BOND_XMIT_POLICY_LAYER23)
2976 return skb_flow_dissect(skb, fk);
3060 2977
3061 if (skb->protocol == htons(ETH_P_IP) && 2978 fk->ports = 0;
3062 pskb_network_may_pull(skb, sizeof(*iph))) { 2979 noff = skb_network_offset(skb);
2980 if (skb->protocol == htons(ETH_P_IP)) {
2981 if (!pskb_may_pull(skb, noff + sizeof(*iph)))
2982 return false;
3063 iph = ip_hdr(skb); 2983 iph = ip_hdr(skb);
3064 data = (struct ethhdr *)skb->data; 2984 fk->src = iph->saddr;
3065 return ((ntohl(iph->saddr ^ iph->daddr) & 0xffff) ^ 2985 fk->dst = iph->daddr;
3066 (data->h_dest[5] ^ data->h_source[5])) % count; 2986 noff += iph->ihl << 2;
3067 } else if (skb->protocol == htons(ETH_P_IPV6) && 2987 if (!ip_is_fragment(iph))
3068 pskb_network_may_pull(skb, sizeof(*ipv6h))) { 2988 proto = iph->protocol;
3069 ipv6h = ipv6_hdr(skb); 2989 } else if (skb->protocol == htons(ETH_P_IPV6)) {
3070 data = (struct ethhdr *)skb->data; 2990 if (!pskb_may_pull(skb, noff + sizeof(*iph6)))
3071 s = &ipv6h->saddr.s6_addr32[0]; 2991 return false;
3072 d = &ipv6h->daddr.s6_addr32[0]; 2992 iph6 = ipv6_hdr(skb);
3073 v6hash = (s[1] ^ d[1]) ^ (s[2] ^ d[2]) ^ (s[3] ^ d[3]); 2993 fk->src = (__force __be32)ipv6_addr_hash(&iph6->saddr);
3074 v6hash ^= (v6hash >> 24) ^ (v6hash >> 16) ^ (v6hash >> 8); 2994 fk->dst = (__force __be32)ipv6_addr_hash(&iph6->daddr);
3075 return (v6hash ^ data->h_dest[5] ^ data->h_source[5]) % count; 2995 noff += sizeof(*iph6);
3076 } 2996 proto = iph6->nexthdr;
3077 2997 } else {
3078 return bond_xmit_hash_policy_l2(skb, count); 2998 return false;
2999 }
3000 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34 && proto >= 0)
3001 fk->ports = skb_flow_get_ports(skb, noff, proto);
3002
3003 return true;
3079} 3004}
3080 3005
3081/* 3006/**
3082 * Hash for the output device based upon layer 3 and layer 4 data. If 3007 * bond_xmit_hash - generate a hash value based on the xmit policy
3083 * the packet is a frag or not TCP or UDP, just use layer 3 data. If it is 3008 * @bond: bonding device
3084 * altogether not IP, fall back on bond_xmit_hash_policy_l2() 3009 * @skb: buffer to use for headers
3010 * @count: modulo value
3011 *
3012 * This function will extract the necessary headers from the skb buffer and use
3013 * them to generate a hash based on the xmit_policy set in the bonding device
3014 * which will be reduced modulo count before returning.
3085 */ 3015 */
3086static int bond_xmit_hash_policy_l34(struct sk_buff *skb, int count) 3016int bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, int count)
3087{ 3017{
3088 u32 layer4_xor = 0; 3018 struct flow_keys flow;
3089 const struct iphdr *iph; 3019 u32 hash;
3090 const struct ipv6hdr *ipv6h;
3091 const __be32 *s, *d;
3092 const __be16 *l4 = NULL;
3093 __be16 _l4[2];
3094 int noff = skb_network_offset(skb);
3095 int poff;
3096
3097 if (skb->protocol == htons(ETH_P_IP) &&
3098 pskb_may_pull(skb, noff + sizeof(*iph))) {
3099 iph = ip_hdr(skb);
3100 poff = proto_ports_offset(iph->protocol);
3101 3020
3102 if (!ip_is_fragment(iph) && poff >= 0) { 3021 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
3103 l4 = skb_header_pointer(skb, noff + (iph->ihl << 2) + poff, 3022 !bond_flow_dissect(bond, skb, &flow))
3104 sizeof(_l4), &_l4); 3023 return bond_eth_hash(skb) % count;
3105 if (l4) 3024
3106 layer4_xor = ntohs(l4[0] ^ l4[1]); 3025 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
3107 } 3026 bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23)
3108 return (layer4_xor ^ 3027 hash = bond_eth_hash(skb);
3109 ((ntohl(iph->saddr ^ iph->daddr)) & 0xffff)) % count; 3028 else
3110 } else if (skb->protocol == htons(ETH_P_IPV6) && 3029 hash = (__force u32)flow.ports;
3111 pskb_may_pull(skb, noff + sizeof(*ipv6h))) { 3030 hash ^= (__force u32)flow.dst ^ (__force u32)flow.src;
3112 ipv6h = ipv6_hdr(skb); 3031 hash ^= (hash >> 16);
3113 poff = proto_ports_offset(ipv6h->nexthdr); 3032 hash ^= (hash >> 8);
3114 if (poff >= 0) {
3115 l4 = skb_header_pointer(skb, noff + sizeof(*ipv6h) + poff,
3116 sizeof(_l4), &_l4);
3117 if (l4)
3118 layer4_xor = ntohs(l4[0] ^ l4[1]);
3119 }
3120 s = &ipv6h->saddr.s6_addr32[0];
3121 d = &ipv6h->daddr.s6_addr32[0];
3122 layer4_xor ^= (s[1] ^ d[1]) ^ (s[2] ^ d[2]) ^ (s[3] ^ d[3]);
3123 layer4_xor ^= (layer4_xor >> 24) ^ (layer4_xor >> 16) ^
3124 (layer4_xor >> 8);
3125 return layer4_xor % count;
3126 }
3127 3033
3128 return bond_xmit_hash_policy_l2(skb, count); 3034 return hash % count;
3129} 3035}
3130 3036
3131/*-------------------------- Device entry points ----------------------------*/ 3037/*-------------------------- Device entry points ----------------------------*/
@@ -3155,13 +3061,14 @@ static void bond_work_cancel_all(struct bonding *bond)
3155static int bond_open(struct net_device *bond_dev) 3061static int bond_open(struct net_device *bond_dev)
3156{ 3062{
3157 struct bonding *bond = netdev_priv(bond_dev); 3063 struct bonding *bond = netdev_priv(bond_dev);
3064 struct list_head *iter;
3158 struct slave *slave; 3065 struct slave *slave;
3159 3066
3160 /* reset slave->backup and slave->inactive */ 3067 /* reset slave->backup and slave->inactive */
3161 read_lock(&bond->lock); 3068 read_lock(&bond->lock);
3162 if (!list_empty(&bond->slave_list)) { 3069 if (bond_has_slaves(bond)) {
3163 read_lock(&bond->curr_slave_lock); 3070 read_lock(&bond->curr_slave_lock);
3164 bond_for_each_slave(bond, slave) { 3071 bond_for_each_slave(bond, slave, iter) {
3165 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP) 3072 if ((bond->params.mode == BOND_MODE_ACTIVEBACKUP)
3166 && (slave != bond->curr_active_slave)) { 3073 && (slave != bond->curr_active_slave)) {
3167 bond_set_slave_inactive_flags(slave); 3074 bond_set_slave_inactive_flags(slave);
@@ -3221,12 +3128,13 @@ static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3221{ 3128{
3222 struct bonding *bond = netdev_priv(bond_dev); 3129 struct bonding *bond = netdev_priv(bond_dev);
3223 struct rtnl_link_stats64 temp; 3130 struct rtnl_link_stats64 temp;
3131 struct list_head *iter;
3224 struct slave *slave; 3132 struct slave *slave;
3225 3133
3226 memset(stats, 0, sizeof(*stats)); 3134 memset(stats, 0, sizeof(*stats));
3227 3135
3228 read_lock_bh(&bond->lock); 3136 read_lock_bh(&bond->lock);
3229 bond_for_each_slave(bond, slave) { 3137 bond_for_each_slave(bond, slave, iter) {
3230 const struct rtnl_link_stats64 *sstats = 3138 const struct rtnl_link_stats64 *sstats =
3231 dev_get_stats(slave->dev, &temp); 3139 dev_get_stats(slave->dev, &temp);
3232 3140
@@ -3263,6 +3171,7 @@ static struct rtnl_link_stats64 *bond_get_stats(struct net_device *bond_dev,
3263 3171
3264static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd) 3172static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3265{ 3173{
3174 struct bonding *bond = netdev_priv(bond_dev);
3266 struct net_device *slave_dev = NULL; 3175 struct net_device *slave_dev = NULL;
3267 struct ifbond k_binfo; 3176 struct ifbond k_binfo;
3268 struct ifbond __user *u_binfo = NULL; 3177 struct ifbond __user *u_binfo = NULL;
@@ -3293,7 +3202,6 @@ static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd
3293 3202
3294 3203
3295 if (mii->reg_num == 1) { 3204 if (mii->reg_num == 1) {
3296 struct bonding *bond = netdev_priv(bond_dev);
3297 mii->val_out = 0; 3205 mii->val_out = 0;
3298 read_lock(&bond->lock); 3206 read_lock(&bond->lock);
3299 read_lock(&bond->curr_slave_lock); 3207 read_lock(&bond->curr_slave_lock);
@@ -3365,7 +3273,7 @@ static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd
3365 break; 3273 break;
3366 case BOND_CHANGE_ACTIVE_OLD: 3274 case BOND_CHANGE_ACTIVE_OLD:
3367 case SIOCBONDCHANGEACTIVE: 3275 case SIOCBONDCHANGEACTIVE:
3368 res = bond_ioctl_change_active(bond_dev, slave_dev); 3276 res = bond_option_active_slave_set(bond, slave_dev);
3369 break; 3277 break;
3370 default: 3278 default:
3371 res = -EOPNOTSUPP; 3279 res = -EOPNOTSUPP;
@@ -3393,22 +3301,24 @@ static void bond_change_rx_flags(struct net_device *bond_dev, int change)
3393static void bond_set_rx_mode(struct net_device *bond_dev) 3301static void bond_set_rx_mode(struct net_device *bond_dev)
3394{ 3302{
3395 struct bonding *bond = netdev_priv(bond_dev); 3303 struct bonding *bond = netdev_priv(bond_dev);
3304 struct list_head *iter;
3396 struct slave *slave; 3305 struct slave *slave;
3397 3306
3398 ASSERT_RTNL();
3399 3307
3308 rcu_read_lock();
3400 if (USES_PRIMARY(bond->params.mode)) { 3309 if (USES_PRIMARY(bond->params.mode)) {
3401 slave = rtnl_dereference(bond->curr_active_slave); 3310 slave = rcu_dereference(bond->curr_active_slave);
3402 if (slave) { 3311 if (slave) {
3403 dev_uc_sync(slave->dev, bond_dev); 3312 dev_uc_sync(slave->dev, bond_dev);
3404 dev_mc_sync(slave->dev, bond_dev); 3313 dev_mc_sync(slave->dev, bond_dev);
3405 } 3314 }
3406 } else { 3315 } else {
3407 bond_for_each_slave(bond, slave) { 3316 bond_for_each_slave_rcu(bond, slave, iter) {
3408 dev_uc_sync_multiple(slave->dev, bond_dev); 3317 dev_uc_sync_multiple(slave->dev, bond_dev);
3409 dev_mc_sync_multiple(slave->dev, bond_dev); 3318 dev_mc_sync_multiple(slave->dev, bond_dev);
3410 } 3319 }
3411 } 3320 }
3321 rcu_read_unlock();
3412} 3322}
3413 3323
3414static int bond_neigh_init(struct neighbour *n) 3324static int bond_neigh_init(struct neighbour *n)
@@ -3471,7 +3381,8 @@ static int bond_neigh_setup(struct net_device *dev,
3471static int bond_change_mtu(struct net_device *bond_dev, int new_mtu) 3381static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3472{ 3382{
3473 struct bonding *bond = netdev_priv(bond_dev); 3383 struct bonding *bond = netdev_priv(bond_dev);
3474 struct slave *slave; 3384 struct slave *slave, *rollback_slave;
3385 struct list_head *iter;
3475 int res = 0; 3386 int res = 0;
3476 3387
3477 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond, 3388 pr_debug("bond=%p, name=%s, new_mtu=%d\n", bond,
@@ -3492,10 +3403,9 @@ static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3492 * call to the base driver. 3403 * call to the base driver.
3493 */ 3404 */
3494 3405
3495 bond_for_each_slave(bond, slave) { 3406 bond_for_each_slave(bond, slave, iter) {
3496 pr_debug("s %p s->p %p c_m %p\n", 3407 pr_debug("s %p c_m %p\n",
3497 slave, 3408 slave,
3498 bond_prev_slave(bond, slave),
3499 slave->dev->netdev_ops->ndo_change_mtu); 3409 slave->dev->netdev_ops->ndo_change_mtu);
3500 3410
3501 res = dev_set_mtu(slave->dev, new_mtu); 3411 res = dev_set_mtu(slave->dev, new_mtu);
@@ -3520,13 +3430,16 @@ static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3520 3430
3521unwind: 3431unwind:
3522 /* unwind from head to the slave that failed */ 3432 /* unwind from head to the slave that failed */
3523 bond_for_each_slave_continue_reverse(bond, slave) { 3433 bond_for_each_slave(bond, rollback_slave, iter) {
3524 int tmp_res; 3434 int tmp_res;
3525 3435
3526 tmp_res = dev_set_mtu(slave->dev, bond_dev->mtu); 3436 if (rollback_slave == slave)
3437 break;
3438
3439 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
3527 if (tmp_res) { 3440 if (tmp_res) {
3528 pr_debug("unwind err %d dev %s\n", 3441 pr_debug("unwind err %d dev %s\n",
3529 tmp_res, slave->dev->name); 3442 tmp_res, rollback_slave->dev->name);
3530 } 3443 }
3531 } 3444 }
3532 3445
@@ -3543,8 +3456,9 @@ unwind:
3543static int bond_set_mac_address(struct net_device *bond_dev, void *addr) 3456static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3544{ 3457{
3545 struct bonding *bond = netdev_priv(bond_dev); 3458 struct bonding *bond = netdev_priv(bond_dev);
3459 struct slave *slave, *rollback_slave;
3546 struct sockaddr *sa = addr, tmp_sa; 3460 struct sockaddr *sa = addr, tmp_sa;
3547 struct slave *slave; 3461 struct list_head *iter;
3548 int res = 0; 3462 int res = 0;
3549 3463
3550 if (bond->params.mode == BOND_MODE_ALB) 3464 if (bond->params.mode == BOND_MODE_ALB)
@@ -3578,7 +3492,7 @@ static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3578 * call to the base driver. 3492 * call to the base driver.
3579 */ 3493 */
3580 3494
3581 bond_for_each_slave(bond, slave) { 3495 bond_for_each_slave(bond, slave, iter) {
3582 const struct net_device_ops *slave_ops = slave->dev->netdev_ops; 3496 const struct net_device_ops *slave_ops = slave->dev->netdev_ops;
3583 pr_debug("slave %p %s\n", slave, slave->dev->name); 3497 pr_debug("slave %p %s\n", slave, slave->dev->name);
3584 3498
@@ -3610,13 +3524,16 @@ unwind:
3610 tmp_sa.sa_family = bond_dev->type; 3524 tmp_sa.sa_family = bond_dev->type;
3611 3525
3612 /* unwind from head to the slave that failed */ 3526 /* unwind from head to the slave that failed */
3613 bond_for_each_slave_continue_reverse(bond, slave) { 3527 bond_for_each_slave(bond, rollback_slave, iter) {
3614 int tmp_res; 3528 int tmp_res;
3615 3529
3616 tmp_res = dev_set_mac_address(slave->dev, &tmp_sa); 3530 if (rollback_slave == slave)
3531 break;
3532
3533 tmp_res = dev_set_mac_address(rollback_slave->dev, &tmp_sa);
3617 if (tmp_res) { 3534 if (tmp_res) {
3618 pr_debug("unwind err %d dev %s\n", 3535 pr_debug("unwind err %d dev %s\n",
3619 tmp_res, slave->dev->name); 3536 tmp_res, rollback_slave->dev->name);
3620 } 3537 }
3621 } 3538 }
3622 3539
@@ -3635,11 +3552,12 @@ unwind:
3635 */ 3552 */
3636void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id) 3553void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id)
3637{ 3554{
3555 struct list_head *iter;
3638 struct slave *slave; 3556 struct slave *slave;
3639 int i = slave_id; 3557 int i = slave_id;
3640 3558
3641 /* Here we start from the slave with slave_id */ 3559 /* Here we start from the slave with slave_id */
3642 bond_for_each_slave_rcu(bond, slave) { 3560 bond_for_each_slave_rcu(bond, slave, iter) {
3643 if (--i < 0) { 3561 if (--i < 0) {
3644 if (slave_can_tx(slave)) { 3562 if (slave_can_tx(slave)) {
3645 bond_dev_queue_xmit(bond, skb, slave->dev); 3563 bond_dev_queue_xmit(bond, skb, slave->dev);
@@ -3650,7 +3568,7 @@ void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id)
3650 3568
3651 /* Here we start from the first slave up to slave_id */ 3569 /* Here we start from the first slave up to slave_id */
3652 i = slave_id; 3570 i = slave_id;
3653 bond_for_each_slave_rcu(bond, slave) { 3571 bond_for_each_slave_rcu(bond, slave, iter) {
3654 if (--i < 0) 3572 if (--i < 0)
3655 break; 3573 break;
3656 if (slave_can_tx(slave)) { 3574 if (slave_can_tx(slave)) {
@@ -3662,14 +3580,44 @@ void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id)
3662 kfree_skb(skb); 3580 kfree_skb(skb);
3663} 3581}
3664 3582
3583/**
3584 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
3585 * @bond: bonding device to use
3586 *
3587 * Based on the value of the bonding device's packets_per_slave parameter
3588 * this function generates a slave id, which is usually used as the next
3589 * slave to transmit through.
3590 */
3591static u32 bond_rr_gen_slave_id(struct bonding *bond)
3592{
3593 int packets_per_slave = bond->params.packets_per_slave;
3594 u32 slave_id;
3595
3596 switch (packets_per_slave) {
3597 case 0:
3598 slave_id = prandom_u32();
3599 break;
3600 case 1:
3601 slave_id = bond->rr_tx_counter;
3602 break;
3603 default:
3604 slave_id = reciprocal_divide(bond->rr_tx_counter,
3605 packets_per_slave);
3606 break;
3607 }
3608 bond->rr_tx_counter++;
3609
3610 return slave_id;
3611}
3612
3665static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev) 3613static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev)
3666{ 3614{
3667 struct bonding *bond = netdev_priv(bond_dev); 3615 struct bonding *bond = netdev_priv(bond_dev);
3668 struct iphdr *iph = ip_hdr(skb); 3616 struct iphdr *iph = ip_hdr(skb);
3669 struct slave *slave; 3617 struct slave *slave;
3618 u32 slave_id;
3670 3619
3671 /* 3620 /* Start with the curr_active_slave that joined the bond as the
3672 * Start with the curr_active_slave that joined the bond as the
3673 * default for sending IGMP traffic. For failover purposes one 3621 * default for sending IGMP traffic. For failover purposes one
3674 * needs to maintain some consistency for the interface that will 3622 * needs to maintain some consistency for the interface that will
3675 * send the join/membership reports. The curr_active_slave found 3623 * send the join/membership reports. The curr_active_slave found
@@ -3682,8 +3630,8 @@ static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *bond_dev
3682 else 3630 else
3683 bond_xmit_slave_id(bond, skb, 0); 3631 bond_xmit_slave_id(bond, skb, 0);
3684 } else { 3632 } else {
3685 bond_xmit_slave_id(bond, skb, 3633 slave_id = bond_rr_gen_slave_id(bond);
3686 bond->rr_tx_counter++ % bond->slave_cnt); 3634 bond_xmit_slave_id(bond, skb, slave_id % bond->slave_cnt);
3687 } 3635 }
3688 3636
3689 return NETDEV_TX_OK; 3637 return NETDEV_TX_OK;
@@ -3707,8 +3655,7 @@ static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *bond_d
3707 return NETDEV_TX_OK; 3655 return NETDEV_TX_OK;
3708} 3656}
3709 3657
3710/* 3658/* In bond_xmit_xor() , we determine the output device by using a pre-
3711 * In bond_xmit_xor() , we determine the output device by using a pre-
3712 * determined xmit_hash_policy(), If the selected device is not enabled, 3659 * determined xmit_hash_policy(), If the selected device is not enabled,
3713 * find the next active slave. 3660 * find the next active slave.
3714 */ 3661 */
@@ -3716,8 +3663,7 @@ static int bond_xmit_xor(struct sk_buff *skb, struct net_device *bond_dev)
3716{ 3663{
3717 struct bonding *bond = netdev_priv(bond_dev); 3664 struct bonding *bond = netdev_priv(bond_dev);
3718 3665
3719 bond_xmit_slave_id(bond, skb, 3666 bond_xmit_slave_id(bond, skb, bond_xmit_hash(bond, skb, bond->slave_cnt));
3720 bond->xmit_hash_policy(skb, bond->slave_cnt));
3721 3667
3722 return NETDEV_TX_OK; 3668 return NETDEV_TX_OK;
3723} 3669}
@@ -3727,8 +3673,9 @@ static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
3727{ 3673{
3728 struct bonding *bond = netdev_priv(bond_dev); 3674 struct bonding *bond = netdev_priv(bond_dev);
3729 struct slave *slave = NULL; 3675 struct slave *slave = NULL;
3676 struct list_head *iter;
3730 3677
3731 bond_for_each_slave_rcu(bond, slave) { 3678 bond_for_each_slave_rcu(bond, slave, iter) {
3732 if (bond_is_last_slave(bond, slave)) 3679 if (bond_is_last_slave(bond, slave))
3733 break; 3680 break;
3734 if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP) { 3681 if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP) {
@@ -3753,22 +3700,6 @@ static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *bond_dev)
3753 3700
3754/*------------------------- Device initialization ---------------------------*/ 3701/*------------------------- Device initialization ---------------------------*/
3755 3702
3756static void bond_set_xmit_hash_policy(struct bonding *bond)
3757{
3758 switch (bond->params.xmit_policy) {
3759 case BOND_XMIT_POLICY_LAYER23:
3760 bond->xmit_hash_policy = bond_xmit_hash_policy_l23;
3761 break;
3762 case BOND_XMIT_POLICY_LAYER34:
3763 bond->xmit_hash_policy = bond_xmit_hash_policy_l34;
3764 break;
3765 case BOND_XMIT_POLICY_LAYER2:
3766 default:
3767 bond->xmit_hash_policy = bond_xmit_hash_policy_l2;
3768 break;
3769 }
3770}
3771
3772/* 3703/*
3773 * Lookup the slave that corresponds to a qid 3704 * Lookup the slave that corresponds to a qid
3774 */ 3705 */
@@ -3777,13 +3708,14 @@ static inline int bond_slave_override(struct bonding *bond,
3777{ 3708{
3778 struct slave *slave = NULL; 3709 struct slave *slave = NULL;
3779 struct slave *check_slave; 3710 struct slave *check_slave;
3711 struct list_head *iter;
3780 int res = 1; 3712 int res = 1;
3781 3713
3782 if (!skb->queue_mapping) 3714 if (!skb->queue_mapping)
3783 return 1; 3715 return 1;
3784 3716
3785 /* Find out if any slaves have the same mapping as this skb. */ 3717 /* Find out if any slaves have the same mapping as this skb. */
3786 bond_for_each_slave_rcu(bond, check_slave) { 3718 bond_for_each_slave_rcu(bond, check_slave, iter) {
3787 if (check_slave->queue_id == skb->queue_mapping) { 3719 if (check_slave->queue_id == skb->queue_mapping) {
3788 slave = check_slave; 3720 slave = check_slave;
3789 break; 3721 break;
@@ -3869,7 +3801,7 @@ static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
3869 return NETDEV_TX_BUSY; 3801 return NETDEV_TX_BUSY;
3870 3802
3871 rcu_read_lock(); 3803 rcu_read_lock();
3872 if (!list_empty(&bond->slave_list)) 3804 if (bond_has_slaves(bond))
3873 ret = __bond_start_xmit(skb, dev); 3805 ret = __bond_start_xmit(skb, dev);
3874 else 3806 else
3875 kfree_skb(skb); 3807 kfree_skb(skb);
@@ -3878,43 +3810,12 @@ static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
3878 return ret; 3810 return ret;
3879} 3811}
3880 3812
3881/*
3882 * set bond mode specific net device operations
3883 */
3884void bond_set_mode_ops(struct bonding *bond, int mode)
3885{
3886 struct net_device *bond_dev = bond->dev;
3887
3888 switch (mode) {
3889 case BOND_MODE_ROUNDROBIN:
3890 break;
3891 case BOND_MODE_ACTIVEBACKUP:
3892 break;
3893 case BOND_MODE_XOR:
3894 bond_set_xmit_hash_policy(bond);
3895 break;
3896 case BOND_MODE_BROADCAST:
3897 break;
3898 case BOND_MODE_8023AD:
3899 bond_set_xmit_hash_policy(bond);
3900 break;
3901 case BOND_MODE_ALB:
3902 /* FALLTHRU */
3903 case BOND_MODE_TLB:
3904 break;
3905 default:
3906 /* Should never happen, mode already checked */
3907 pr_err("%s: Error: Unknown bonding mode %d\n",
3908 bond_dev->name, mode);
3909 break;
3910 }
3911}
3912
3913static int bond_ethtool_get_settings(struct net_device *bond_dev, 3813static int bond_ethtool_get_settings(struct net_device *bond_dev,
3914 struct ethtool_cmd *ecmd) 3814 struct ethtool_cmd *ecmd)
3915{ 3815{
3916 struct bonding *bond = netdev_priv(bond_dev); 3816 struct bonding *bond = netdev_priv(bond_dev);
3917 unsigned long speed = 0; 3817 unsigned long speed = 0;
3818 struct list_head *iter;
3918 struct slave *slave; 3819 struct slave *slave;
3919 3820
3920 ecmd->duplex = DUPLEX_UNKNOWN; 3821 ecmd->duplex = DUPLEX_UNKNOWN;
@@ -3926,7 +3827,7 @@ static int bond_ethtool_get_settings(struct net_device *bond_dev,
3926 * this is an accurate maximum. 3827 * this is an accurate maximum.
3927 */ 3828 */
3928 read_lock(&bond->lock); 3829 read_lock(&bond->lock);
3929 bond_for_each_slave(bond, slave) { 3830 bond_for_each_slave(bond, slave, iter) {
3930 if (SLAVE_IS_OK(slave)) { 3831 if (SLAVE_IS_OK(slave)) {
3931 if (slave->speed != SPEED_UNKNOWN) 3832 if (slave->speed != SPEED_UNKNOWN)
3932 speed += slave->speed; 3833 speed += slave->speed;
@@ -3994,14 +3895,13 @@ static void bond_destructor(struct net_device *bond_dev)
3994 free_netdev(bond_dev); 3895 free_netdev(bond_dev);
3995} 3896}
3996 3897
3997static void bond_setup(struct net_device *bond_dev) 3898void bond_setup(struct net_device *bond_dev)
3998{ 3899{
3999 struct bonding *bond = netdev_priv(bond_dev); 3900 struct bonding *bond = netdev_priv(bond_dev);
4000 3901
4001 /* initialize rwlocks */ 3902 /* initialize rwlocks */
4002 rwlock_init(&bond->lock); 3903 rwlock_init(&bond->lock);
4003 rwlock_init(&bond->curr_slave_lock); 3904 rwlock_init(&bond->curr_slave_lock);
4004 INIT_LIST_HEAD(&bond->slave_list);
4005 bond->params = bonding_defaults; 3905 bond->params = bonding_defaults;
4006 3906
4007 /* Initialize pointers */ 3907 /* Initialize pointers */
@@ -4011,7 +3911,6 @@ static void bond_setup(struct net_device *bond_dev)
4011 ether_setup(bond_dev); 3911 ether_setup(bond_dev);
4012 bond_dev->netdev_ops = &bond_netdev_ops; 3912 bond_dev->netdev_ops = &bond_netdev_ops;
4013 bond_dev->ethtool_ops = &bond_ethtool_ops; 3913 bond_dev->ethtool_ops = &bond_ethtool_ops;
4014 bond_set_mode_ops(bond, bond->params.mode);
4015 3914
4016 bond_dev->destructor = bond_destructor; 3915 bond_dev->destructor = bond_destructor;
4017 3916
@@ -4057,12 +3956,13 @@ static void bond_setup(struct net_device *bond_dev)
4057static void bond_uninit(struct net_device *bond_dev) 3956static void bond_uninit(struct net_device *bond_dev)
4058{ 3957{
4059 struct bonding *bond = netdev_priv(bond_dev); 3958 struct bonding *bond = netdev_priv(bond_dev);
4060 struct slave *slave, *tmp_slave; 3959 struct list_head *iter;
3960 struct slave *slave;
4061 3961
4062 bond_netpoll_cleanup(bond_dev); 3962 bond_netpoll_cleanup(bond_dev);
4063 3963
4064 /* Release the bonded slaves */ 3964 /* Release the bonded slaves */
4065 list_for_each_entry_safe(slave, tmp_slave, &bond->slave_list, list) 3965 bond_for_each_slave(bond, slave, iter)
4066 __bond_release_one(bond_dev, slave->dev, true); 3966 __bond_release_one(bond_dev, slave->dev, true);
4067 pr_info("%s: released all slaves\n", bond_dev->name); 3967 pr_info("%s: released all slaves\n", bond_dev->name);
4068 3968
@@ -4235,6 +4135,12 @@ static int bond_check_params(struct bond_params *params)
4235 resend_igmp = BOND_DEFAULT_RESEND_IGMP; 4135 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4236 } 4136 }
4237 4137
4138 if (packets_per_slave < 0 || packets_per_slave > USHRT_MAX) {
4139 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
4140 packets_per_slave, USHRT_MAX);
4141 packets_per_slave = 1;
4142 }
4143
4238 /* reset values for TLB/ALB */ 4144 /* reset values for TLB/ALB */
4239 if ((bond_mode == BOND_MODE_TLB) || 4145 if ((bond_mode == BOND_MODE_TLB) ||
4240 (bond_mode == BOND_MODE_ALB)) { 4146 (bond_mode == BOND_MODE_ALB)) {
@@ -4424,7 +4330,10 @@ static int bond_check_params(struct bond_params *params)
4424 params->resend_igmp = resend_igmp; 4330 params->resend_igmp = resend_igmp;
4425 params->min_links = min_links; 4331 params->min_links = min_links;
4426 params->lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL; 4332 params->lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
4427 4333 if (packets_per_slave > 1)
4334 params->packets_per_slave = reciprocal_value(packets_per_slave);
4335 else
4336 params->packets_per_slave = packets_per_slave;
4428 if (primary) { 4337 if (primary) {
4429 strncpy(params->primary, primary, IFNAMSIZ); 4338 strncpy(params->primary, primary, IFNAMSIZ);
4430 params->primary[IFNAMSIZ - 1] = 0; 4339 params->primary[IFNAMSIZ - 1] = 0;
@@ -4495,32 +4404,11 @@ static int bond_init(struct net_device *bond_dev)
4495 return 0; 4404 return 0;
4496} 4405}
4497 4406
4498static int bond_validate(struct nlattr *tb[], struct nlattr *data[]) 4407unsigned int bond_get_num_tx_queues(void)
4499{
4500 if (tb[IFLA_ADDRESS]) {
4501 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
4502 return -EINVAL;
4503 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
4504 return -EADDRNOTAVAIL;
4505 }
4506 return 0;
4507}
4508
4509static unsigned int bond_get_num_tx_queues(void)
4510{ 4408{
4511 return tx_queues; 4409 return tx_queues;
4512} 4410}
4513 4411
4514static struct rtnl_link_ops bond_link_ops __read_mostly = {
4515 .kind = "bond",
4516 .priv_size = sizeof(struct bonding),
4517 .setup = bond_setup,
4518 .validate = bond_validate,
4519 .get_num_tx_queues = bond_get_num_tx_queues,
4520 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
4521 as for TX queues */
4522};
4523
4524/* Create a new bond based on the specified name and bonding parameters. 4412/* Create a new bond based on the specified name and bonding parameters.
4525 * If name is NULL, obtain a suitable "bond%d" name for us. 4413 * If name is NULL, obtain a suitable "bond%d" name for us.
4526 * Caller must NOT hold rtnl_lock; we need to release it here before we 4414 * Caller must NOT hold rtnl_lock; we need to release it here before we
@@ -4607,7 +4495,7 @@ static int __init bonding_init(void)
4607 if (res) 4495 if (res)
4608 goto out; 4496 goto out;
4609 4497
4610 res = rtnl_link_register(&bond_link_ops); 4498 res = bond_netlink_init();
4611 if (res) 4499 if (res)
4612 goto err_link; 4500 goto err_link;
4613 4501
@@ -4623,7 +4511,7 @@ static int __init bonding_init(void)
4623out: 4511out:
4624 return res; 4512 return res;
4625err: 4513err:
4626 rtnl_link_unregister(&bond_link_ops); 4514 bond_netlink_fini();
4627err_link: 4515err_link:
4628 unregister_pernet_subsys(&bond_net_ops); 4516 unregister_pernet_subsys(&bond_net_ops);
4629 goto out; 4517 goto out;
@@ -4636,7 +4524,7 @@ static void __exit bonding_exit(void)
4636 4524
4637 bond_destroy_debugfs(); 4525 bond_destroy_debugfs();
4638 4526
4639 rtnl_link_unregister(&bond_link_ops); 4527 bond_netlink_fini();
4640 unregister_pernet_subsys(&bond_net_ops); 4528 unregister_pernet_subsys(&bond_net_ops);
4641 4529
4642#ifdef CONFIG_NET_POLL_CONTROLLER 4530#ifdef CONFIG_NET_POLL_CONTROLLER
@@ -4653,4 +4541,3 @@ MODULE_LICENSE("GPL");
4653MODULE_VERSION(DRV_VERSION); 4541MODULE_VERSION(DRV_VERSION);
4654MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION); 4542MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4655MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others"); 4543MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
4656MODULE_ALIAS_RTNL_LINK("bond");
diff --git a/drivers/net/bonding/bond_netlink.c b/drivers/net/bonding/bond_netlink.c
new file mode 100644
index 000000000000..40e7b1cb4aea
--- /dev/null
+++ b/drivers/net/bonding/bond_netlink.c
@@ -0,0 +1,131 @@
1/*
2 * drivers/net/bond/bond_netlink.c - Netlink interface for bonding
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/module.h>
14#include <linux/errno.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <linux/if_link.h>
18#include <linux/if_ether.h>
19#include <net/netlink.h>
20#include <net/rtnetlink.h>
21#include "bonding.h"
22
23static const struct nla_policy bond_policy[IFLA_BOND_MAX + 1] = {
24 [IFLA_BOND_MODE] = { .type = NLA_U8 },
25 [IFLA_BOND_ACTIVE_SLAVE] = { .type = NLA_U32 },
26};
27
28static int bond_validate(struct nlattr *tb[], struct nlattr *data[])
29{
30 if (tb[IFLA_ADDRESS]) {
31 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
32 return -EINVAL;
33 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
34 return -EADDRNOTAVAIL;
35 }
36 return 0;
37}
38
39static int bond_changelink(struct net_device *bond_dev,
40 struct nlattr *tb[], struct nlattr *data[])
41{
42 struct bonding *bond = netdev_priv(bond_dev);
43 int err;
44
45 if (data && data[IFLA_BOND_MODE]) {
46 int mode = nla_get_u8(data[IFLA_BOND_MODE]);
47
48 err = bond_option_mode_set(bond, mode);
49 if (err)
50 return err;
51 }
52 if (data && data[IFLA_BOND_ACTIVE_SLAVE]) {
53 int ifindex = nla_get_u32(data[IFLA_BOND_ACTIVE_SLAVE]);
54 struct net_device *slave_dev;
55
56 if (ifindex == 0) {
57 slave_dev = NULL;
58 } else {
59 slave_dev = __dev_get_by_index(dev_net(bond_dev),
60 ifindex);
61 if (!slave_dev)
62 return -ENODEV;
63 }
64 err = bond_option_active_slave_set(bond, slave_dev);
65 if (err)
66 return err;
67 }
68 return 0;
69}
70
71static int bond_newlink(struct net *src_net, struct net_device *bond_dev,
72 struct nlattr *tb[], struct nlattr *data[])
73{
74 int err;
75
76 err = bond_changelink(bond_dev, tb, data);
77 if (err < 0)
78 return err;
79
80 return register_netdevice(bond_dev);
81}
82
83static size_t bond_get_size(const struct net_device *bond_dev)
84{
85 return nla_total_size(sizeof(u8)) + /* IFLA_BOND_MODE */
86 nla_total_size(sizeof(u32)); /* IFLA_BOND_ACTIVE_SLAVE */
87}
88
89static int bond_fill_info(struct sk_buff *skb,
90 const struct net_device *bond_dev)
91{
92 struct bonding *bond = netdev_priv(bond_dev);
93 struct net_device *slave_dev = bond_option_active_slave_get(bond);
94
95 if (nla_put_u8(skb, IFLA_BOND_MODE, bond->params.mode) ||
96 (slave_dev &&
97 nla_put_u32(skb, IFLA_BOND_ACTIVE_SLAVE, slave_dev->ifindex)))
98 goto nla_put_failure;
99 return 0;
100
101nla_put_failure:
102 return -EMSGSIZE;
103}
104
105struct rtnl_link_ops bond_link_ops __read_mostly = {
106 .kind = "bond",
107 .priv_size = sizeof(struct bonding),
108 .setup = bond_setup,
109 .maxtype = IFLA_BOND_MAX,
110 .policy = bond_policy,
111 .validate = bond_validate,
112 .newlink = bond_newlink,
113 .changelink = bond_changelink,
114 .get_size = bond_get_size,
115 .fill_info = bond_fill_info,
116 .get_num_tx_queues = bond_get_num_tx_queues,
117 .get_num_rx_queues = bond_get_num_tx_queues, /* Use the same number
118 as for TX queues */
119};
120
121int __init bond_netlink_init(void)
122{
123 return rtnl_link_register(&bond_link_ops);
124}
125
126void bond_netlink_fini(void)
127{
128 rtnl_link_unregister(&bond_link_ops);
129}
130
131MODULE_ALIAS_RTNL_LINK("bond");
diff --git a/drivers/net/bonding/bond_options.c b/drivers/net/bonding/bond_options.c
new file mode 100644
index 000000000000..9a5223c7b4d1
--- /dev/null
+++ b/drivers/net/bonding/bond_options.c
@@ -0,0 +1,142 @@
1/*
2 * drivers/net/bond/bond_options.c - bonding options
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/errno.h>
14#include <linux/if.h>
15#include <linux/netdevice.h>
16#include <linux/rwlock.h>
17#include <linux/rcupdate.h>
18#include "bonding.h"
19
20static bool bond_mode_is_valid(int mode)
21{
22 int i;
23
24 for (i = 0; bond_mode_tbl[i].modename; i++);
25
26 return mode >= 0 && mode < i;
27}
28
29int bond_option_mode_set(struct bonding *bond, int mode)
30{
31 if (!bond_mode_is_valid(mode)) {
32 pr_err("invalid mode value %d.\n", mode);
33 return -EINVAL;
34 }
35
36 if (bond->dev->flags & IFF_UP) {
37 pr_err("%s: unable to update mode because interface is up.\n",
38 bond->dev->name);
39 return -EPERM;
40 }
41
42 if (bond_has_slaves(bond)) {
43 pr_err("%s: unable to update mode because bond has slaves.\n",
44 bond->dev->name);
45 return -EPERM;
46 }
47
48 if (BOND_MODE_IS_LB(mode) && bond->params.arp_interval) {
49 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
50 bond->dev->name, bond_mode_tbl[mode].modename);
51 return -EINVAL;
52 }
53
54 /* don't cache arp_validate between modes */
55 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
56 bond->params.mode = mode;
57 return 0;
58}
59
60static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
61 struct slave *slave)
62{
63 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
64}
65
66struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
67{
68 struct slave *slave = rcu_dereference(bond->curr_active_slave);
69
70 return __bond_option_active_slave_get(bond, slave);
71}
72
73struct net_device *bond_option_active_slave_get(struct bonding *bond)
74{
75 return __bond_option_active_slave_get(bond, bond->curr_active_slave);
76}
77
78int bond_option_active_slave_set(struct bonding *bond,
79 struct net_device *slave_dev)
80{
81 int ret = 0;
82
83 if (slave_dev) {
84 if (!netif_is_bond_slave(slave_dev)) {
85 pr_err("Device %s is not bonding slave.\n",
86 slave_dev->name);
87 return -EINVAL;
88 }
89
90 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
91 pr_err("%s: Device %s is not our slave.\n",
92 bond->dev->name, slave_dev->name);
93 return -EINVAL;
94 }
95 }
96
97 if (!USES_PRIMARY(bond->params.mode)) {
98 pr_err("%s: Unable to change active slave; %s is in mode %d\n",
99 bond->dev->name, bond->dev->name, bond->params.mode);
100 return -EINVAL;
101 }
102
103 block_netpoll_tx();
104 read_lock(&bond->lock);
105 write_lock_bh(&bond->curr_slave_lock);
106
107 /* check to see if we are clearing active */
108 if (!slave_dev) {
109 pr_info("%s: Clearing current active slave.\n",
110 bond->dev->name);
111 rcu_assign_pointer(bond->curr_active_slave, NULL);
112 bond_select_active_slave(bond);
113 } else {
114 struct slave *old_active = bond->curr_active_slave;
115 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
116
117 BUG_ON(!new_active);
118
119 if (new_active == old_active) {
120 /* do nothing */
121 pr_info("%s: %s is already the current active slave.\n",
122 bond->dev->name, new_active->dev->name);
123 } else {
124 if (old_active && (new_active->link == BOND_LINK_UP) &&
125 IS_UP(new_active->dev)) {
126 pr_info("%s: Setting %s as active slave.\n",
127 bond->dev->name, new_active->dev->name);
128 bond_change_active_slave(bond, new_active);
129 } else {
130 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
131 bond->dev->name, new_active->dev->name,
132 new_active->dev->name);
133 ret = -EINVAL;
134 }
135 }
136 }
137
138 write_unlock_bh(&bond->curr_slave_lock);
139 read_unlock(&bond->lock);
140 unblock_netpoll_tx();
141 return ret;
142}
diff --git a/drivers/net/bonding/bond_procfs.c b/drivers/net/bonding/bond_procfs.c
index 20a6ee25bb63..fb868d6c22da 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
@@ -30,17 +31,25 @@ static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
30static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos) 31static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
31{ 32{
32 struct bonding *bond = seq->private; 33 struct bonding *bond = seq->private;
33 struct slave *slave = v; 34 struct list_head *iter;
35 struct slave *slave;
36 bool found = false;
34 37
35 ++*pos; 38 ++*pos;
36 if (v == SEQ_START_TOKEN) 39 if (v == SEQ_START_TOKEN)
37 return bond_first_slave(bond); 40 return bond_first_slave(bond);
38 41
39 if (bond_is_last_slave(bond, slave)) 42 if (bond_is_last_slave(bond, v))
40 return NULL; 43 return NULL;
41 slave = bond_next_slave(bond, slave);
42 44
43 return slave; 45 bond_for_each_slave(bond, slave, iter) {
46 if (found)
47 return slave;
48 if (slave == v)
49 found = true;
50 }
51
52 return NULL;
44} 53}
45 54
46static void bond_info_seq_stop(struct seq_file *seq, void *v) 55static void bond_info_seq_stop(struct seq_file *seq, void *v)
diff --git a/drivers/net/bonding/bond_sysfs.c b/drivers/net/bonding/bond_sysfs.c
index ec9b6460a38d..bc8fd362a5aa 100644
--- a/drivers/net/bonding/bond_sysfs.c
+++ b/drivers/net/bonding/bond_sysfs.c
@@ -40,6 +40,7 @@
40#include <net/net_namespace.h> 40#include <net/net_namespace.h>
41#include <net/netns/generic.h> 41#include <net/netns/generic.h>
42#include <linux/nsproxy.h> 42#include <linux/nsproxy.h>
43#include <linux/reciprocal_div.h>
43 44
44#include "bonding.h" 45#include "bonding.h"
45 46
@@ -159,41 +160,6 @@ static const struct class_attribute class_attr_bonding_masters = {
159 .store = bonding_store_bonds, 160 .store = bonding_store_bonds,
160}; 161};
161 162
162int bond_create_slave_symlinks(struct net_device *master,
163 struct net_device *slave)
164{
165 char linkname[IFNAMSIZ+7];
166 int ret = 0;
167
168 /* first, create a link from the slave back to the master */
169 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
170 "master");
171 if (ret)
172 return ret;
173 /* next, create a link from the master to the slave */
174 sprintf(linkname, "slave_%s", slave->name);
175 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
176 linkname);
177
178 /* free the master link created earlier in case of error */
179 if (ret)
180 sysfs_remove_link(&(slave->dev.kobj), "master");
181
182 return ret;
183
184}
185
186void bond_destroy_slave_symlinks(struct net_device *master,
187 struct net_device *slave)
188{
189 char linkname[IFNAMSIZ+7];
190
191 sysfs_remove_link(&(slave->dev.kobj), "master");
192 sprintf(linkname, "slave_%s", slave->name);
193 sysfs_remove_link(&(master->dev.kobj), linkname);
194}
195
196
197/* 163/*
198 * Show the slaves in the current bond. 164 * Show the slaves in the current bond.
199 */ 165 */
@@ -201,11 +167,14 @@ static ssize_t bonding_show_slaves(struct device *d,
201 struct device_attribute *attr, char *buf) 167 struct device_attribute *attr, char *buf)
202{ 168{
203 struct bonding *bond = to_bond(d); 169 struct bonding *bond = to_bond(d);
170 struct list_head *iter;
204 struct slave *slave; 171 struct slave *slave;
205 int res = 0; 172 int res = 0;
206 173
207 read_lock(&bond->lock); 174 if (!rtnl_trylock())
208 bond_for_each_slave(bond, slave) { 175 return restart_syscall();
176
177 bond_for_each_slave(bond, slave, iter) {
209 if (res > (PAGE_SIZE - IFNAMSIZ)) { 178 if (res > (PAGE_SIZE - IFNAMSIZ)) {
210 /* not enough space for another interface name */ 179 /* not enough space for another interface name */
211 if ((PAGE_SIZE - res) > 10) 180 if ((PAGE_SIZE - res) > 10)
@@ -215,7 +184,9 @@ static ssize_t bonding_show_slaves(struct device *d,
215 } 184 }
216 res += sprintf(buf + res, "%s ", slave->dev->name); 185 res += sprintf(buf + res, "%s ", slave->dev->name);
217 } 186 }
218 read_unlock(&bond->lock); 187
188 rtnl_unlock();
189
219 if (res) 190 if (res)
220 buf[res-1] = '\n'; /* eat the leftover space */ 191 buf[res-1] = '\n'; /* eat the leftover space */
221 192
@@ -304,50 +275,26 @@ static ssize_t bonding_store_mode(struct device *d,
304 struct device_attribute *attr, 275 struct device_attribute *attr,
305 const char *buf, size_t count) 276 const char *buf, size_t count)
306{ 277{
307 int new_value, ret = count; 278 int new_value, ret;
308 struct bonding *bond = to_bond(d); 279 struct bonding *bond = to_bond(d);
309 280
310 if (!rtnl_trylock())
311 return restart_syscall();
312
313 if (bond->dev->flags & IFF_UP) {
314 pr_err("unable to update mode of %s because interface is up.\n",
315 bond->dev->name);
316 ret = -EPERM;
317 goto out;
318 }
319
320 if (!list_empty(&bond->slave_list)) {
321 pr_err("unable to update mode of %s because it has slaves.\n",
322 bond->dev->name);
323 ret = -EPERM;
324 goto out;
325 }
326
327 new_value = bond_parse_parm(buf, bond_mode_tbl); 281 new_value = bond_parse_parm(buf, bond_mode_tbl);
328 if (new_value < 0) { 282 if (new_value < 0) {
329 pr_err("%s: Ignoring invalid mode value %.*s.\n", 283 pr_err("%s: Ignoring invalid mode value %.*s.\n",
330 bond->dev->name, (int)strlen(buf) - 1, buf); 284 bond->dev->name, (int)strlen(buf) - 1, buf);
331 ret = -EINVAL; 285 return -EINVAL;
332 goto out;
333 } 286 }
334 if ((new_value == BOND_MODE_ALB || 287 if (!rtnl_trylock())
335 new_value == BOND_MODE_TLB) && 288 return restart_syscall();
336 bond->params.arp_interval) { 289
337 pr_err("%s: %s mode is incompatible with arp monitoring.\n", 290 ret = bond_option_mode_set(bond, new_value);
338 bond->dev->name, bond_mode_tbl[new_value].modename); 291 if (!ret) {
339 ret = -EINVAL; 292 pr_info("%s: setting mode to %s (%d).\n",
340 goto out; 293 bond->dev->name, bond_mode_tbl[new_value].modename,
294 new_value);
295 ret = count;
341 } 296 }
342 297
343 /* don't cache arp_validate between modes */
344 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
345 bond->params.mode = new_value;
346 bond_set_mode_ops(bond, bond->params.mode);
347 pr_info("%s: setting mode to %s (%d).\n",
348 bond->dev->name, bond_mode_tbl[new_value].modename,
349 new_value);
350out:
351 rtnl_unlock(); 298 rtnl_unlock();
352 return ret; 299 return ret;
353} 300}
@@ -383,7 +330,6 @@ static ssize_t bonding_store_xmit_hash(struct device *d,
383 ret = -EINVAL; 330 ret = -EINVAL;
384 } else { 331 } else {
385 bond->params.xmit_policy = new_value; 332 bond->params.xmit_policy = new_value;
386 bond_set_mode_ops(bond, bond->params.mode);
387 pr_info("%s: setting xmit hash policy to %s (%d).\n", 333 pr_info("%s: setting xmit hash policy to %s (%d).\n",
388 bond->dev->name, 334 bond->dev->name,
389 xmit_hashtype_tbl[new_value].modename, new_value); 335 xmit_hashtype_tbl[new_value].modename, new_value);
@@ -513,7 +459,7 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
513 if (!rtnl_trylock()) 459 if (!rtnl_trylock())
514 return restart_syscall(); 460 return restart_syscall();
515 461
516 if (!list_empty(&bond->slave_list)) { 462 if (bond_has_slaves(bond)) {
517 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n", 463 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
518 bond->dev->name); 464 bond->dev->name);
519 ret = -EPERM; 465 ret = -EPERM;
@@ -647,11 +593,15 @@ static ssize_t bonding_store_arp_targets(struct device *d,
647 const char *buf, size_t count) 593 const char *buf, size_t count)
648{ 594{
649 struct bonding *bond = to_bond(d); 595 struct bonding *bond = to_bond(d);
596 struct list_head *iter;
650 struct slave *slave; 597 struct slave *slave;
651 __be32 newtarget, *targets; 598 __be32 newtarget, *targets;
652 unsigned long *targets_rx; 599 unsigned long *targets_rx;
653 int ind, i, j, ret = -EINVAL; 600 int ind, i, j, ret = -EINVAL;
654 601
602 if (!rtnl_trylock())
603 return restart_syscall();
604
655 targets = bond->params.arp_targets; 605 targets = bond->params.arp_targets;
656 newtarget = in_aton(buf + 1); 606 newtarget = in_aton(buf + 1);
657 /* look for adds */ 607 /* look for adds */
@@ -679,7 +629,7 @@ static ssize_t bonding_store_arp_targets(struct device *d,
679 &newtarget); 629 &newtarget);
680 /* not to race with bond_arp_rcv */ 630 /* not to race with bond_arp_rcv */
681 write_lock_bh(&bond->lock); 631 write_lock_bh(&bond->lock);
682 bond_for_each_slave(bond, slave) 632 bond_for_each_slave(bond, slave, iter)
683 slave->target_last_arp_rx[ind] = jiffies; 633 slave->target_last_arp_rx[ind] = jiffies;
684 targets[ind] = newtarget; 634 targets[ind] = newtarget;
685 write_unlock_bh(&bond->lock); 635 write_unlock_bh(&bond->lock);
@@ -705,7 +655,7 @@ static ssize_t bonding_store_arp_targets(struct device *d,
705 &newtarget); 655 &newtarget);
706 656
707 write_lock_bh(&bond->lock); 657 write_lock_bh(&bond->lock);
708 bond_for_each_slave(bond, slave) { 658 bond_for_each_slave(bond, slave, iter) {
709 targets_rx = slave->target_last_arp_rx; 659 targets_rx = slave->target_last_arp_rx;
710 j = ind; 660 j = ind;
711 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++) 661 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
@@ -725,6 +675,7 @@ static ssize_t bonding_store_arp_targets(struct device *d,
725 675
726 ret = count; 676 ret = count;
727out: 677out:
678 rtnl_unlock();
728 return ret; 679 return ret;
729} 680}
730static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets); 681static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
@@ -1102,6 +1053,7 @@ static ssize_t bonding_store_primary(struct device *d,
1102 const char *buf, size_t count) 1053 const char *buf, size_t count)
1103{ 1054{
1104 struct bonding *bond = to_bond(d); 1055 struct bonding *bond = to_bond(d);
1056 struct list_head *iter;
1105 char ifname[IFNAMSIZ]; 1057 char ifname[IFNAMSIZ];
1106 struct slave *slave; 1058 struct slave *slave;
1107 1059
@@ -1129,7 +1081,7 @@ static ssize_t bonding_store_primary(struct device *d,
1129 goto out; 1081 goto out;
1130 } 1082 }
1131 1083
1132 bond_for_each_slave(bond, slave) { 1084 bond_for_each_slave(bond, slave, iter) {
1133 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) { 1085 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1134 pr_info("%s: Setting %s as primary slave.\n", 1086 pr_info("%s: Setting %s as primary slave.\n",
1135 bond->dev->name, slave->dev->name); 1087 bond->dev->name, slave->dev->name);
@@ -1259,13 +1211,13 @@ static ssize_t bonding_show_active_slave(struct device *d,
1259 char *buf) 1211 char *buf)
1260{ 1212{
1261 struct bonding *bond = to_bond(d); 1213 struct bonding *bond = to_bond(d);
1262 struct slave *curr; 1214 struct net_device *slave_dev;
1263 int count = 0; 1215 int count = 0;
1264 1216
1265 rcu_read_lock(); 1217 rcu_read_lock();
1266 curr = rcu_dereference(bond->curr_active_slave); 1218 slave_dev = bond_option_active_slave_get_rcu(bond);
1267 if (USES_PRIMARY(bond->params.mode) && curr) 1219 if (slave_dev)
1268 count = sprintf(buf, "%s\n", curr->dev->name); 1220 count = sprintf(buf, "%s\n", slave_dev->name);
1269 rcu_read_unlock(); 1221 rcu_read_unlock();
1270 1222
1271 return count; 1223 return count;
@@ -1275,80 +1227,33 @@ static ssize_t bonding_store_active_slave(struct device *d,
1275 struct device_attribute *attr, 1227 struct device_attribute *attr,
1276 const char *buf, size_t count) 1228 const char *buf, size_t count)
1277{ 1229{
1278 struct slave *slave, *old_active, *new_active; 1230 int ret;
1279 struct bonding *bond = to_bond(d); 1231 struct bonding *bond = to_bond(d);
1280 char ifname[IFNAMSIZ]; 1232 char ifname[IFNAMSIZ];
1233 struct net_device *dev;
1281 1234
1282 if (!rtnl_trylock()) 1235 if (!rtnl_trylock())
1283 return restart_syscall(); 1236 return restart_syscall();
1284 1237
1285 old_active = new_active = NULL;
1286 block_netpoll_tx();
1287 read_lock(&bond->lock);
1288 write_lock_bh(&bond->curr_slave_lock);
1289
1290 if (!USES_PRIMARY(bond->params.mode)) {
1291 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1292 bond->dev->name, bond->dev->name, bond->params.mode);
1293 goto out;
1294 }
1295
1296 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */ 1238 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1297
1298 /* check to see if we are clearing active */
1299 if (!strlen(ifname) || buf[0] == '\n') { 1239 if (!strlen(ifname) || buf[0] == '\n') {
1300 pr_info("%s: Clearing current active slave.\n", 1240 dev = NULL;
1301 bond->dev->name); 1241 } else {
1302 rcu_assign_pointer(bond->curr_active_slave, NULL); 1242 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1303 bond_select_active_slave(bond); 1243 if (!dev) {
1304 goto out; 1244 ret = -ENODEV;
1305 } 1245 goto out;
1306
1307 bond_for_each_slave(bond, slave) {
1308 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1309 old_active = bond->curr_active_slave;
1310 new_active = slave;
1311 if (new_active == old_active) {
1312 /* do nothing */
1313 pr_info("%s: %s is already the current"
1314 " active slave.\n",
1315 bond->dev->name,
1316 slave->dev->name);
1317 goto out;
1318 } else {
1319 if ((new_active) &&
1320 (old_active) &&
1321 (new_active->link == BOND_LINK_UP) &&
1322 IS_UP(new_active->dev)) {
1323 pr_info("%s: Setting %s as active"
1324 " slave.\n",
1325 bond->dev->name,
1326 slave->dev->name);
1327 bond_change_active_slave(bond,
1328 new_active);
1329 } else {
1330 pr_info("%s: Could not set %s as"
1331 " active slave; either %s is"
1332 " down or the link is down.\n",
1333 bond->dev->name,
1334 slave->dev->name,
1335 slave->dev->name);
1336 }
1337 goto out;
1338 }
1339 } 1246 }
1340 } 1247 }
1341 1248
1342 pr_info("%s: Unable to set %.*s as active slave.\n", 1249 ret = bond_option_active_slave_set(bond, dev);
1343 bond->dev->name, (int)strlen(buf) - 1, buf); 1250 if (!ret)
1344 out: 1251 ret = count;
1345 write_unlock_bh(&bond->curr_slave_lock);
1346 read_unlock(&bond->lock);
1347 unblock_netpoll_tx();
1348 1252
1253 out:
1349 rtnl_unlock(); 1254 rtnl_unlock();
1350 1255
1351 return count; 1256 return ret;
1352 1257
1353} 1258}
1354static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, 1259static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
@@ -1484,14 +1389,14 @@ static ssize_t bonding_show_queue_id(struct device *d,
1484 char *buf) 1389 char *buf)
1485{ 1390{
1486 struct bonding *bond = to_bond(d); 1391 struct bonding *bond = to_bond(d);
1392 struct list_head *iter;
1487 struct slave *slave; 1393 struct slave *slave;
1488 int res = 0; 1394 int res = 0;
1489 1395
1490 if (!rtnl_trylock()) 1396 if (!rtnl_trylock())
1491 return restart_syscall(); 1397 return restart_syscall();
1492 1398
1493 read_lock(&bond->lock); 1399 bond_for_each_slave(bond, slave, iter) {
1494 bond_for_each_slave(bond, slave) {
1495 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) { 1400 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1496 /* not enough space for another interface_name:queue_id pair */ 1401 /* not enough space for another interface_name:queue_id pair */
1497 if ((PAGE_SIZE - res) > 10) 1402 if ((PAGE_SIZE - res) > 10)
@@ -1502,9 +1407,9 @@ static ssize_t bonding_show_queue_id(struct device *d,
1502 res += sprintf(buf + res, "%s:%d ", 1407 res += sprintf(buf + res, "%s:%d ",
1503 slave->dev->name, slave->queue_id); 1408 slave->dev->name, slave->queue_id);
1504 } 1409 }
1505 read_unlock(&bond->lock);
1506 if (res) 1410 if (res)
1507 buf[res-1] = '\n'; /* eat the leftover space */ 1411 buf[res-1] = '\n'; /* eat the leftover space */
1412
1508 rtnl_unlock(); 1413 rtnl_unlock();
1509 1414
1510 return res; 1415 return res;
@@ -1520,6 +1425,7 @@ static ssize_t bonding_store_queue_id(struct device *d,
1520{ 1425{
1521 struct slave *slave, *update_slave; 1426 struct slave *slave, *update_slave;
1522 struct bonding *bond = to_bond(d); 1427 struct bonding *bond = to_bond(d);
1428 struct list_head *iter;
1523 u16 qid; 1429 u16 qid;
1524 int ret = count; 1430 int ret = count;
1525 char *delim; 1431 char *delim;
@@ -1552,11 +1458,9 @@ static ssize_t bonding_store_queue_id(struct device *d,
1552 if (!sdev) 1458 if (!sdev)
1553 goto err_no_cmd; 1459 goto err_no_cmd;
1554 1460
1555 read_lock(&bond->lock);
1556
1557 /* Search for thes slave and check for duplicate qids */ 1461 /* Search for thes slave and check for duplicate qids */
1558 update_slave = NULL; 1462 update_slave = NULL;
1559 bond_for_each_slave(bond, slave) { 1463 bond_for_each_slave(bond, slave, iter) {
1560 if (sdev == slave->dev) 1464 if (sdev == slave->dev)
1561 /* 1465 /*
1562 * We don't need to check the matching 1466 * We don't need to check the matching
@@ -1564,23 +1468,20 @@ static ssize_t bonding_store_queue_id(struct device *d,
1564 */ 1468 */
1565 update_slave = slave; 1469 update_slave = slave;
1566 else if (qid && qid == slave->queue_id) { 1470 else if (qid && qid == slave->queue_id) {
1567 goto err_no_cmd_unlock; 1471 goto err_no_cmd;
1568 } 1472 }
1569 } 1473 }
1570 1474
1571 if (!update_slave) 1475 if (!update_slave)
1572 goto err_no_cmd_unlock; 1476 goto err_no_cmd;
1573 1477
1574 /* Actually set the qids for the slave */ 1478 /* Actually set the qids for the slave */
1575 update_slave->queue_id = qid; 1479 update_slave->queue_id = qid;
1576 1480
1577 read_unlock(&bond->lock);
1578out: 1481out:
1579 rtnl_unlock(); 1482 rtnl_unlock();
1580 return ret; 1483 return ret;
1581 1484
1582err_no_cmd_unlock:
1583 read_unlock(&bond->lock);
1584err_no_cmd: 1485err_no_cmd:
1585 pr_info("invalid input for queue_id set for %s.\n", 1486 pr_info("invalid input for queue_id set for %s.\n",
1586 bond->dev->name); 1487 bond->dev->name);
@@ -1610,8 +1511,12 @@ static ssize_t bonding_store_slaves_active(struct device *d,
1610{ 1511{
1611 struct bonding *bond = to_bond(d); 1512 struct bonding *bond = to_bond(d);
1612 int new_value, ret = count; 1513 int new_value, ret = count;
1514 struct list_head *iter;
1613 struct slave *slave; 1515 struct slave *slave;
1614 1516
1517 if (!rtnl_trylock())
1518 return restart_syscall();
1519
1615 if (sscanf(buf, "%d", &new_value) != 1) { 1520 if (sscanf(buf, "%d", &new_value) != 1) {
1616 pr_err("%s: no all_slaves_active value specified.\n", 1521 pr_err("%s: no all_slaves_active value specified.\n",
1617 bond->dev->name); 1522 bond->dev->name);
@@ -1631,8 +1536,7 @@ static ssize_t bonding_store_slaves_active(struct device *d,
1631 goto out; 1536 goto out;
1632 } 1537 }
1633 1538
1634 read_lock(&bond->lock); 1539 bond_for_each_slave(bond, slave, iter) {
1635 bond_for_each_slave(bond, slave) {
1636 if (!bond_is_active_slave(slave)) { 1540 if (!bond_is_active_slave(slave)) {
1637 if (new_value) 1541 if (new_value)
1638 slave->inactive = 0; 1542 slave->inactive = 0;
@@ -1640,8 +1544,8 @@ static ssize_t bonding_store_slaves_active(struct device *d,
1640 slave->inactive = 1; 1544 slave->inactive = 1;
1641 } 1545 }
1642 } 1546 }
1643 read_unlock(&bond->lock);
1644out: 1547out:
1548 rtnl_unlock();
1645 return ret; 1549 return ret;
1646} 1550}
1647static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR, 1551static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
@@ -1728,6 +1632,53 @@ out:
1728static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR, 1632static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1729 bonding_show_lp_interval, bonding_store_lp_interval); 1633 bonding_show_lp_interval, bonding_store_lp_interval);
1730 1634
1635static ssize_t bonding_show_packets_per_slave(struct device *d,
1636 struct device_attribute *attr,
1637 char *buf)
1638{
1639 struct bonding *bond = to_bond(d);
1640 int packets_per_slave = bond->params.packets_per_slave;
1641
1642 if (packets_per_slave > 1)
1643 packets_per_slave = reciprocal_value(packets_per_slave);
1644
1645 return sprintf(buf, "%d\n", packets_per_slave);
1646}
1647
1648static ssize_t bonding_store_packets_per_slave(struct device *d,
1649 struct device_attribute *attr,
1650 const char *buf, size_t count)
1651{
1652 struct bonding *bond = to_bond(d);
1653 int new_value, ret = count;
1654
1655 if (sscanf(buf, "%d", &new_value) != 1) {
1656 pr_err("%s: no packets_per_slave value specified.\n",
1657 bond->dev->name);
1658 ret = -EINVAL;
1659 goto out;
1660 }
1661 if (new_value < 0 || new_value > USHRT_MAX) {
1662 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1663 bond->dev->name, USHRT_MAX);
1664 ret = -EINVAL;
1665 goto out;
1666 }
1667 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1668 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1669 bond->dev->name);
1670 if (new_value > 1)
1671 bond->params.packets_per_slave = reciprocal_value(new_value);
1672 else
1673 bond->params.packets_per_slave = new_value;
1674out:
1675 return ret;
1676}
1677
1678static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1679 bonding_show_packets_per_slave,
1680 bonding_store_packets_per_slave);
1681
1731static struct attribute *per_bond_attrs[] = { 1682static struct attribute *per_bond_attrs[] = {
1732 &dev_attr_slaves.attr, 1683 &dev_attr_slaves.attr,
1733 &dev_attr_mode.attr, 1684 &dev_attr_mode.attr,
@@ -1759,6 +1710,7 @@ static struct attribute *per_bond_attrs[] = {
1759 &dev_attr_resend_igmp.attr, 1710 &dev_attr_resend_igmp.attr,
1760 &dev_attr_min_links.attr, 1711 &dev_attr_min_links.attr,
1761 &dev_attr_lp_interval.attr, 1712 &dev_attr_lp_interval.attr,
1713 &dev_attr_packets_per_slave.attr,
1762 NULL, 1714 NULL,
1763}; 1715};
1764 1716
diff --git a/drivers/net/bonding/bonding.h b/drivers/net/bonding/bonding.h
index 03cf3fd14490..77a07a12e77f 100644
--- a/drivers/net/bonding/bonding.h
+++ b/drivers/net/bonding/bonding.h
@@ -58,6 +58,11 @@
58#define TX_QUEUE_OVERRIDE(mode) \ 58#define TX_QUEUE_OVERRIDE(mode) \
59 (((mode) == BOND_MODE_ACTIVEBACKUP) || \ 59 (((mode) == BOND_MODE_ACTIVEBACKUP) || \
60 ((mode) == BOND_MODE_ROUNDROBIN)) 60 ((mode) == BOND_MODE_ROUNDROBIN))
61
62#define BOND_MODE_IS_LB(mode) \
63 (((mode) == BOND_MODE_TLB) || \
64 ((mode) == BOND_MODE_ALB))
65
61/* 66/*
62 * Less bad way to call ioctl from within the kernel; this needs to be 67 * Less bad way to call ioctl from within the kernel; this needs to be
63 * done some other way to get the call out of interrupt context. 68 * done some other way to get the call out of interrupt context.
@@ -72,63 +77,37 @@
72 res; }) 77 res; })
73 78
74/* slave list primitives */ 79/* slave list primitives */
75#define bond_to_slave(ptr) list_entry(ptr, struct slave, list) 80#define bond_slave_list(bond) (&(bond)->dev->adj_list.lower)
81
82#define bond_has_slaves(bond) !list_empty(bond_slave_list(bond))
76 83
77/* IMPORTANT: bond_first/last_slave can return NULL in case of an empty list */ 84/* IMPORTANT: bond_first/last_slave can return NULL in case of an empty list */
78#define bond_first_slave(bond) \ 85#define bond_first_slave(bond) \
79 list_first_entry_or_null(&(bond)->slave_list, struct slave, list) 86 (bond_has_slaves(bond) ? \
87 netdev_adjacent_get_private(bond_slave_list(bond)->next) : \
88 NULL)
80#define bond_last_slave(bond) \ 89#define bond_last_slave(bond) \
81 (list_empty(&(bond)->slave_list) ? NULL : \ 90 (bond_has_slaves(bond) ? \
82 bond_to_slave((bond)->slave_list.prev)) 91 netdev_adjacent_get_private(bond_slave_list(bond)->prev) : \
92 NULL)
83 93
84#define bond_is_first_slave(bond, pos) ((pos)->list.prev == &(bond)->slave_list) 94#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) 95#define bond_is_last_slave(bond, pos) (pos == bond_last_slave(bond))
86
87/* Since bond_first/last_slave can return NULL, these can return NULL too */
88#define 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 96
109/** 97/**
110 * bond_for_each_slave - iterate over all slaves 98 * bond_for_each_slave - iterate over all slaves
111 * @bond: the bond holding this list 99 * @bond: the bond holding this list
112 * @pos: current slave 100 * @pos: current slave
101 * @iter: list_head * iterator
113 * 102 *
114 * Caller must hold bond->lock 103 * Caller must hold bond->lock
115 */ 104 */
116#define bond_for_each_slave(bond, pos) \ 105#define bond_for_each_slave(bond, pos, iter) \
117 list_for_each_entry(pos, &(bond)->slave_list, list) 106 netdev_for_each_lower_private((bond)->dev, pos, iter)
118 107
119/* Caller must have rcu_read_lock */ 108/* Caller must have rcu_read_lock */
120#define bond_for_each_slave_rcu(bond, pos) \ 109#define bond_for_each_slave_rcu(bond, pos, iter) \
121 list_for_each_entry_rcu(pos, &(bond)->slave_list, list) 110 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 111
133#ifdef CONFIG_NET_POLL_CONTROLLER 112#ifdef CONFIG_NET_POLL_CONTROLLER
134extern atomic_t netpoll_block_tx; 113extern atomic_t netpoll_block_tx;
@@ -177,6 +156,7 @@ struct bond_params {
177 int all_slaves_active; 156 int all_slaves_active;
178 int resend_igmp; 157 int resend_igmp;
179 int lp_interval; 158 int lp_interval;
159 int packets_per_slave;
180}; 160};
181 161
182struct bond_parm_tbl { 162struct bond_parm_tbl {
@@ -188,7 +168,6 @@ struct bond_parm_tbl {
188 168
189struct slave { 169struct slave {
190 struct net_device *dev; /* first - useful for panic debug */ 170 struct net_device *dev; /* first - useful for panic debug */
191 struct list_head list;
192 struct bonding *bond; /* our master */ 171 struct bonding *bond; /* our master */
193 int delay; 172 int delay;
194 unsigned long jiffies; 173 unsigned long jiffies;
@@ -228,7 +207,6 @@ struct slave {
228 */ 207 */
229struct bonding { 208struct bonding {
230 struct net_device *dev; /* first - useful for panic debug */ 209 struct net_device *dev; /* first - useful for panic debug */
231 struct list_head slave_list;
232 struct slave *curr_active_slave; 210 struct slave *curr_active_slave;
233 struct slave *current_arp_slave; 211 struct slave *current_arp_slave;
234 struct slave *primary_slave; 212 struct slave *primary_slave;
@@ -245,8 +223,7 @@ struct bonding {
245 char proc_file_name[IFNAMSIZ]; 223 char proc_file_name[IFNAMSIZ];
246#endif /* CONFIG_PROC_FS */ 224#endif /* CONFIG_PROC_FS */
247 struct list_head bond_list; 225 struct list_head bond_list;
248 int (*xmit_hash_policy)(struct sk_buff *, int); 226 u32 rr_tx_counter;
249 u16 rr_tx_counter;
250 struct ad_bond_info ad_info; 227 struct ad_bond_info ad_info;
251 struct alb_bond_info alb_info; 228 struct alb_bond_info alb_info;
252 struct bond_params params; 229 struct bond_params params;
@@ -276,13 +253,7 @@ struct bonding {
276static inline struct slave *bond_get_slave_by_dev(struct bonding *bond, 253static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
277 struct net_device *slave_dev) 254 struct net_device *slave_dev)
278{ 255{
279 struct slave *slave = NULL; 256 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} 257}
287 258
288static inline struct bonding *bond_get_bond_by_slave(struct slave *slave) 259static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
@@ -294,8 +265,7 @@ static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
294 265
295static inline bool bond_is_lb(const struct bonding *bond) 266static inline bool bond_is_lb(const struct bonding *bond)
296{ 267{
297 return (bond->params.mode == BOND_MODE_TLB || 268 return BOND_MODE_IS_LB(bond->params.mode);
298 bond->params.mode == BOND_MODE_ALB);
299} 269}
300 270
301static inline void bond_set_active_slave(struct slave *slave) 271static inline void bond_set_active_slave(struct slave *slave)
@@ -432,21 +402,18 @@ static inline bool slave_can_tx(struct slave *slave)
432struct bond_net; 402struct bond_net;
433 403
434int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond, struct slave *slave); 404int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond, struct slave *slave);
435struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
436int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev); 405int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
437void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id); 406void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id);
438int bond_create(struct net *net, const char *name); 407int bond_create(struct net *net, const char *name);
439int bond_create_sysfs(struct bond_net *net); 408int bond_create_sysfs(struct bond_net *net);
440void bond_destroy_sysfs(struct bond_net *net); 409void bond_destroy_sysfs(struct bond_net *net);
441void bond_prepare_sysfs_group(struct bonding *bond); 410void 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); 411int 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); 412int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
446void bond_mii_monitor(struct work_struct *); 413void bond_mii_monitor(struct work_struct *);
447void bond_loadbalance_arp_mon(struct work_struct *); 414void bond_loadbalance_arp_mon(struct work_struct *);
448void bond_activebackup_arp_mon(struct work_struct *); 415void bond_activebackup_arp_mon(struct work_struct *);
449void bond_set_mode_ops(struct bonding *bond, int mode); 416int bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, int count);
450int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl); 417int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
451void bond_select_active_slave(struct bonding *bond); 418void bond_select_active_slave(struct bonding *bond);
452void bond_change_active_slave(struct bonding *bond, struct slave *new_active); 419void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
@@ -456,6 +423,14 @@ void bond_debug_register(struct bonding *bond);
456void bond_debug_unregister(struct bonding *bond); 423void bond_debug_unregister(struct bonding *bond);
457void bond_debug_reregister(struct bonding *bond); 424void bond_debug_reregister(struct bonding *bond);
458const char *bond_mode_name(int mode); 425const char *bond_mode_name(int mode);
426void bond_setup(struct net_device *bond_dev);
427unsigned int bond_get_num_tx_queues(void);
428int bond_netlink_init(void);
429void bond_netlink_fini(void);
430int bond_option_mode_set(struct bonding *bond, int mode);
431int bond_option_active_slave_set(struct bonding *bond, struct net_device *slave_dev);
432struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond);
433struct net_device *bond_option_active_slave_get(struct bonding *bond);
459 434
460struct bond_net { 435struct bond_net {
461 struct net * net; /* Associated network namespace */ 436 struct net * net; /* Associated network namespace */
@@ -492,9 +467,24 @@ static inline void bond_destroy_proc_dir(struct bond_net *bn)
492static inline struct slave *bond_slave_has_mac(struct bonding *bond, 467static inline struct slave *bond_slave_has_mac(struct bonding *bond,
493 const u8 *mac) 468 const u8 *mac)
494{ 469{
470 struct list_head *iter;
495 struct slave *tmp; 471 struct slave *tmp;
496 472
497 bond_for_each_slave(bond, tmp) 473 bond_for_each_slave(bond, tmp, iter)
474 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
475 return tmp;
476
477 return NULL;
478}
479
480/* Caller must hold rcu_read_lock() for read */
481static inline struct slave *bond_slave_has_mac_rcu(struct bonding *bond,
482 const u8 *mac)
483{
484 struct list_head *iter;
485 struct slave *tmp;
486
487 bond_for_each_slave_rcu(bond, tmp, iter)
498 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr)) 488 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
499 return tmp; 489 return tmp;
500 490
@@ -528,4 +518,7 @@ extern const struct bond_parm_tbl fail_over_mac_tbl[];
528extern const struct bond_parm_tbl pri_reselect_tbl[]; 518extern const struct bond_parm_tbl pri_reselect_tbl[];
529extern struct bond_parm_tbl ad_select_tbl[]; 519extern struct bond_parm_tbl ad_select_tbl[];
530 520
521/* exported from bond_netlink.c */
522extern struct rtnl_link_ops bond_link_ops;
523
531#endif /* _LINUX_BONDING_H */ 524#endif /* _LINUX_BONDING_H */