23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/if_ether.h>
27 #include <linux/netdevice.h>
29 #include <linux/ethtool.h>
38 #define AD_SHORT_TIMEOUT 1
39 #define AD_LONG_TIMEOUT 0
40 #define AD_STANDBY 0x2
41 #define AD_MAX_TX_IN_SECOND 3
42 #define AD_COLLECTOR_MAX_DELAY 0
45 #define AD_FAST_PERIODIC_TIME 1
46 #define AD_SLOW_PERIODIC_TIME 30
47 #define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
48 #define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
49 #define AD_CHURN_DETECTION_TIME 60
50 #define AD_AGGREGATE_WAIT_TIME 2
53 #define AD_STATE_LACP_ACTIVITY 0x1
54 #define AD_STATE_LACP_TIMEOUT 0x2
55 #define AD_STATE_AGGREGATION 0x4
56 #define AD_STATE_SYNCHRONIZATION 0x8
57 #define AD_STATE_COLLECTING 0x10
58 #define AD_STATE_DISTRIBUTING 0x20
59 #define AD_STATE_DEFAULTED 0x40
60 #define AD_STATE_EXPIRED 0x80
63 #define AD_PORT_BEGIN 0x1
64 #define AD_PORT_LACP_ENABLED 0x2
65 #define AD_PORT_ACTOR_CHURN 0x4
66 #define AD_PORT_PARTNER_CHURN 0x8
67 #define AD_PORT_READY 0x10
68 #define AD_PORT_READY_N 0x20
69 #define AD_PORT_MATCHED 0x40
70 #define AD_PORT_STANDBY 0x80
71 #define AD_PORT_SELECTED 0x100
72 #define AD_PORT_MOVED 0x200
81 #define AD_DUPLEX_KEY_BITS 0x1
82 #define AD_SPEED_KEY_BITS 0x3E
83 #define AD_USER_KEY_BITS 0xFFC0
86 #define AD_LINK_SPEED_BITMASK_1MBPS 0x1
87 #define AD_LINK_SPEED_BITMASK_10MBPS 0x2
88 #define AD_LINK_SPEED_BITMASK_100MBPS 0x4
89 #define AD_LINK_SPEED_BITMASK_1000MBPS 0x8
90 #define AD_LINK_SPEED_BITMASK_10000MBPS 0x10
94 #define MAC_ADDRESS_COMPARE(A, B) memcmp(A, B, ETH_ALEN)
96 static struct mac_addr null_mac_addr = { { 0, 0, 0, 0, 0, 0 } };
97 static u16 ad_ticks_per_sec;
103 static int ad_lacpdu_send(
struct port *
port);
105 static void ad_mux_machine(
struct port *
port);
107 static void ad_tx_machine(
struct port *
port);
108 static void ad_periodic_machine(
struct port *
port);
109 static void ad_port_selection_logic(
struct port *
port);
113 static void ad_initialize_port(
struct port *
port,
int lacp_fast);
114 static void ad_enable_collecting_distributing(
struct port *
port);
115 static void ad_disable_collecting_distributing(
struct port *
port);
135 return bond_get_bond_by_slave(port->
slave);
144 static inline struct port *__get_first_port(
struct bonding *bond)
159 static inline struct port *__get_next_port(
struct port *port)
161 struct bonding *bond = __get_bond_by_port(port);
178 static inline struct aggregator *__get_first_agg(
struct port *port)
180 struct bonding *bond = __get_bond_by_port(port);
198 struct slave *slave = aggregator->
slave;
199 struct bonding *bond = bond_get_bond_by_slave(slave);
214 static inline int __agg_has_partner(
struct aggregator *agg)
224 static inline void __disable_port(
struct port *port)
226 bond_set_slave_inactive_flags(port->
slave);
234 static inline void __enable_port(
struct port *port)
236 struct slave *slave = port->
slave;
239 bond_set_slave_active_flags(slave);
247 static inline int __port_is_enabled(
struct port *port)
249 return bond_is_active_slave(port->
slave);
258 static inline u32 __get_agg_selection_mode(
struct port *port)
260 struct bonding *bond = __get_bond_by_port(port);
265 return bond->
params.ad_select;
273 static inline int __check_agg_selection_timer(
struct port *port)
275 struct bonding *bond = __get_bond_by_port(port);
288 static inline void __get_state_machine_lock(
struct port *port)
298 static inline void __release_state_machine_lock(
struct port *port)
314 static u16 __get_link_speed(
struct port *port)
316 struct slave *slave = port->
slave;
326 switch (slave->
speed) {
349 pr_debug(
"Port %d Received link speed %d update from adapter\n",
362 static u8 __get_duplex(
struct port *port)
364 struct slave *slave = port->
slave;
376 pr_debug(
"Port %d Received status full duplex update from adapter\n",
382 pr_debug(
"Port %d Received status NOT full duplex update from adapter\n",
395 static inline void __initialize_port_locks(
struct port *port)
412 static u16 __ad_timer_to_ticks(
u16 timer_type,
u16 par)
416 switch (timer_type) {
427 retval = (par*ad_ticks_per_sec);
467 static void __choose_matched(
struct lacpdu *
lacpdu,
struct port *port)
495 static void __record_pdu(
struct lacpdu *lacpdu,
struct port *port)
497 if (lacpdu && port) {
500 __choose_matched(lacpdu, port);
529 static void __record_default(
struct port *port)
554 static void __update_selected(
struct lacpdu *lacpdu,
struct port *port)
556 if (lacpdu && port) {
584 static void __update_default_selected(
struct port *port)
595 admin->
key != oper->
key ||
616 static void __update_ntt(
struct lacpdu *lacpdu,
struct port *port)
619 if (lacpdu && port) {
645 static void __attach_bond_to_agg(
struct port *port)
660 static void __detach_bond_from_agg(
struct port *port)
672 static int __agg_ports_are_ready(
struct aggregator *aggregator)
698 static void __set_agg_ports_ready(
struct aggregator *aggregator,
int val)
716 static u32 __get_agg_bandwidth(
struct aggregator *aggregator)
721 switch (__get_link_speed(aggregator->
lag_ports)) {
749 static struct aggregator *__get_active_agg(
struct aggregator *aggregator)
751 struct aggregator *retval =
NULL;
753 for (; aggregator; aggregator = __get_next_agg(aggregator)) {
768 static inline void __update_lacpdu_from_port(
struct port *port)
770 struct lacpdu *lacpdu = &port->
lacpdu;
821 static int ad_lacpdu_send(
struct port *port)
823 struct slave *slave = port->
slave;
826 int length =
sizeof(
struct lacpdu_header);
828 skb = dev_alloc_skb(length);
833 skb_reset_mac_header(skb);
838 lacpdu_header = (
struct lacpdu_header *)
skb_put(skb, length);
863 struct slave *slave = port->
slave;
868 skb = dev_alloc_skb(length + 16);
872 skb_reserve(skb, 16);
875 skb_reset_mac_header(skb);
899 static void ad_mux_machine(
struct port *port)
965 !__port_is_enabled(port)) {
978 pr_debug(
"Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
983 __detach_bond_from_agg(port);
985 ad_disable_collecting_distributing(port);
994 __attach_bond_to_agg(port);
998 ad_disable_collecting_distributing(port);
1004 ad_enable_collecting_distributing(port);
1022 static void ad_rx_machine(
struct lacpdu *lacpdu,
struct port *port)
1079 if ((port->
sm_rx_state != last_state) || (lacpdu)) {
1080 pr_debug(
"Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1090 __record_default(port);
1102 __record_default(port);
1119 __update_default_selected(port);
1120 __record_default(port);
1128 pr_err(
"%s: An illegal loopback occurred on adapter (%s).\n"
1129 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n",
1130 port->
slave->dev->master->name, port->
slave->dev->name);
1133 __update_selected(lacpdu, port);
1134 __update_ntt(lacpdu, port);
1135 __record_pdu(lacpdu, port);
1150 static void ad_tx_machine(
struct port *port)
1156 __update_lacpdu_from_port(port);
1158 if (ad_lacpdu_send(port) >= 0) {
1159 pr_debug(
"Sent LACPDU on port %d\n",
1179 static void ad_periodic_machine(
struct port *port)
1236 pr_debug(
"Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1266 static void ad_port_selection_logic(
struct port *port)
1268 struct aggregator *aggregator, *free_aggregator =
NULL, *temp_aggregator;
1269 struct port *last_port =
NULL, *curr_port;
1280 for (curr_port = temp_aggregator->lag_ports; curr_port;
1281 last_port = curr_port,
1283 if (curr_port == port) {
1284 temp_aggregator->num_of_ports--;
1286 temp_aggregator->lag_ports =
1300 temp_aggregator->aggregator_identifier);
1302 if (!temp_aggregator->lag_ports)
1303 ad_clear_agg(temp_aggregator);
1308 pr_warning(
"%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n",
1309 port->
slave->dev->master->name,
1311 port->
slave->dev->name,
1316 for (aggregator = __get_first_agg(port);
aggregator;
1317 aggregator = __get_next_agg(aggregator)) {
1321 if (!free_aggregator)
1342 pr_debug(
"Port %d joined LAG %d(existing LAG)\n",
1355 if (free_aggregator) {
1384 pr_debug(
"Port %d joined LAG %d(new LAG)\n",
1388 pr_err(
"%s: Port %d (on %s) did not find a suitable aggregator\n",
1389 port->
slave->dev->master->name,
1397 aggregator = __get_first_agg(port);
1398 ad_agg_selection_logic(aggregator);
1405 static struct aggregator *ad_agg_selection_test(
struct aggregator *best,
1406 struct aggregator *
curr)
1442 if (__agg_has_partner(curr) && !__agg_has_partner(best))
1445 if (!__agg_has_partner(curr) && __agg_has_partner(best))
1448 switch (__get_agg_selection_mode(curr->
lag_ports)) {
1459 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1465 pr_warning(
"%s: Impossible agg select mode %d\n",
1466 curr->
slave->dev->master->name,
1467 __get_agg_selection_mode(curr->
lag_ports));
1474 static int agg_device_up(
const struct aggregator *agg)
1479 return (netif_running(port->
slave->dev) &&
1480 netif_carrier_ok(port->
slave->dev));
1508 static void ad_agg_selection_logic(
struct aggregator *agg)
1514 active = __get_active_agg(agg);
1515 best = (active && agg_device_up(active)) ? active :
NULL;
1521 best = ad_agg_selection_test(best, agg);
1523 }
while ((agg = __get_next_agg(agg)));
1535 (__agg_has_partner(active) ||
1536 (!__agg_has_partner(active) && !__agg_has_partner(best)))) {
1545 if (best && (best == active)) {
1552 pr_debug(
"best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1557 pr_debug(
"best ports %p slave %p %s\n",
1559 best->
slave ? best->
slave->dev->name :
"NULL");
1561 for (agg = __get_first_agg(best->
lag_ports); agg;
1562 agg = __get_next_agg(agg)) {
1564 pr_debug(
"Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1573 pr_warning(
"%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1574 best->
slave ? best->
slave->dev->master->name :
"NULL");
1578 pr_debug(
"LAG %d chosen as the active LAG\n",
1580 pr_debug(
"Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1590 __disable_port(port);
1599 active = __get_active_agg(origin);
1602 if (!__agg_has_partner(active)) {
1605 __enable_port(port);
1610 if (origin->
slave) {
1613 bond = bond_get_bond_by_slave(origin->
slave);
1624 static void ad_clear_agg(
struct aggregator *aggregator)
1648 static void ad_initialize_agg(
struct aggregator *aggregator)
1651 ad_clear_agg(aggregator);
1665 static void ad_initialize_port(
struct port *port,
int lacp_fast)
1671 .port_priority = 0xff,
1674 static const struct lacpdu lacpdu = {
1676 .version_number = 0x01,
1677 .tlv_type_actor_info = 0x01,
1678 .actor_information_length = 0x14,
1679 .tlv_type_partner_info = 0x02,
1680 .partner_information_length = 0x14,
1681 .tlv_type_collector_info = 0x03,
1682 .collector_information_length = 0x10,
1730 static void ad_enable_collecting_distributing(
struct port *port)
1733 pr_debug(
"Enabling port %d(LAG %d)\n",
1736 __enable_port(port);
1745 static void ad_disable_collecting_distributing(
struct port *port)
1748 pr_debug(
"Disabling port %d(LAG %d)\n",
1751 __disable_port(port);
1764 static void ad_marker_info_send(
struct port *port)
1786 for (index = 0; index < 90; index++)
1790 if (ad_marker_send(port, &marker) >= 0) {
1791 pr_debug(
"Sent Marker Information on port %d\n",
1803 static void ad_marker_info_received(
struct bond_marker *marker_info,
1815 if (ad_marker_send(port, &marker) >= 0) {
1816 pr_debug(
"Sent Marker Response on port %d\n",
1830 static void ad_marker_response_received(
struct bond_marker *marker,
1843 #define AD_AGGREGATOR_SELECTION_TIMER 8
1857 static u16 aggregator_identifier;
1870 bond->
dev->dev_addr)) {
1872 aggregator_identifier = 0;
1878 ad_ticks_per_sec = tick_resolution;
1895 struct bonding *bond = bond_get_bond_by_slave(slave);
1897 struct aggregator *aggregator;
1900 pr_err(
"%s: The slave %s is not attached to its bond\n",
1901 slave->
dev->master->name, slave->
dev->name);
1911 ad_initialize_port(port, bond->
params.lacp_fast);
1934 __disable_port(port);
1935 __initialize_port_locks(port);
1941 ad_initialize_agg(aggregator);
1945 aggregator->
slave = slave;
1963 struct port *
port, *prev_port, *temp_port;
1964 struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
1965 int select_new_active_agg = 0;
1975 pr_warning(
"Warning: %s: Trying to unbind an uninitialized port on %s\n",
1976 slave->
dev->master->name, slave->
dev->name);
1980 pr_debug(
"Unbinding Link Aggregation Group %d\n",
1985 __update_lacpdu_from_port(port);
1986 ad_lacpdu_send(port);
1993 if ((aggregator->
lag_ports != port) || (aggregator->
lag_ports->next_port_in_aggregator)) {
1995 new_aggregator = __get_first_agg(port);
1996 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
2000 && !new_aggregator->
lag_ports->next_port_in_aggregator))
2005 if ((new_aggregator) && ((!new_aggregator->
lag_ports) || ((new_aggregator->
lag_ports == port) && !new_aggregator->
lag_ports->next_port_in_aggregator))) {
2006 pr_debug(
"Some port(s) related to LAG %d - replaceing with LAG %d\n",
2011 pr_info(
"%s: Removing an active aggregator\n",
2012 aggregator->
slave->dev->master->name);
2014 select_new_active_agg = 1;
2030 for (temp_port = aggregator->
lag_ports; temp_port;
2037 ad_clear_agg(aggregator);
2039 if (select_new_active_agg)
2040 ad_agg_selection_logic(__get_first_agg(port));
2042 pr_warning(
"%s: Warning: unbinding aggregator, and could not find a new aggregator for its ports\n",
2043 slave->
dev->master->name);
2046 select_new_active_agg = aggregator->
is_active;
2048 ad_clear_agg(aggregator);
2049 if (select_new_active_agg) {
2050 pr_info(
"%s: Removing an active aggregator\n",
2051 slave->
dev->master->name);
2053 ad_agg_selection_logic(__get_first_agg(port));
2060 temp_aggregator = __get_first_agg(port);
2061 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
2064 for (temp_port = temp_aggregator->
lag_ports; temp_port;
2065 prev_port = temp_port,
2067 if (temp_port == port) {
2074 select_new_active_agg = temp_aggregator->
is_active;
2076 ad_clear_agg(temp_aggregator);
2077 if (select_new_active_agg) {
2078 pr_info(
"%s: Removing an active aggregator\n",
2079 slave->
dev->master->name);
2081 ad_agg_selection_logic(__get_first_agg(port));
2109 struct aggregator *aggregator;
2120 if ((port = __get_first_port(bond))) {
2122 pr_warning(
"%s: Warning: bond's first port is uninitialized\n",
2127 aggregator = __get_first_agg(port);
2128 ad_agg_selection_logic(aggregator);
2134 for (port = __get_first_port(bond);
port; port = __get_next_port(port)) {
2136 pr_warning(
"%s: Warning: Found an uninitialized port\n",
2145 __get_state_machine_lock(port);
2147 ad_rx_machine(
NULL, port);
2148 ad_periodic_machine(port);
2149 ad_port_selection_logic(port);
2150 ad_mux_machine(port);
2151 ad_tx_machine(port);
2157 __release_state_machine_lock(port);
2176 static int bond_3ad_rx_indication(
struct lacpdu *lacpdu,
struct slave *slave,
u16 length)
2181 if (length >=
sizeof(
struct lacpdu)) {
2186 pr_warning(
"%s: Warning: port of slave %s is uninitialized\n",
2187 slave->
dev->name, slave->
dev->master->name);
2194 pr_debug(
"Received LACPDU on port %d\n",
2197 __get_state_machine_lock(port);
2198 ad_rx_machine(lacpdu, port);
2199 __release_state_machine_lock(port);
2208 pr_debug(
"Received Marker Information on port %d\n",
2210 ad_marker_info_received((
struct bond_marker *)lacpdu, port);
2214 pr_debug(
"Received Marker Response on port %d\n",
2216 ad_marker_response_received((
struct bond_marker *)lacpdu, port);
2220 pr_debug(
"Received an unknown Marker subtype on slot %d\n",
2242 pr_warning(
"Warning: %s: speed changed for uninitialized port on %s\n",
2243 slave->
dev->master->name, slave->
dev->name);
2249 (__get_link_speed(port) << 1);
2270 pr_warning(
"%s: Warning: duplex changed for uninitialized port on %s\n",
2271 slave->
dev->master->name, slave->
dev->name);
2299 pr_warning(
"Warning: %s: link status changed for uninitialized port on %s\n",
2300 slave->
dev->master->name, slave->
dev->name);
2313 (__get_link_speed(port) << 1);
2339 struct aggregator *
active;
2345 if (netif_carrier_ok(bond->
dev)) {
2349 }
else if (!netif_carrier_ok(bond->
dev)) {
2356 if (netif_carrier_ok(bond->
dev)) {
2373 struct aggregator *aggregator =
NULL;
2376 for (port = __get_first_port(bond);
port; port = __get_next_port(port)) {
2397 struct slave *slave, *start_at;
2398 struct bonding *bond = netdev_priv(dev);
2407 pr_debug(
"%s: Error: bond_3ad_get_active_agg_info failed\n",
2412 slaves_in_agg = ad_info.
ports;
2415 if (slaves_in_agg == 0) {
2417 pr_debug(
"%s: Error: active aggregator is empty\n", dev->
name);
2424 struct aggregator *agg =
SLAVE_AD_INFO(slave).port.aggregator;
2428 if (slave_agg_no < 0)
2433 if (slave_agg_no >= 0) {
2434 pr_err(
"%s: Error: Couldn't find a slave to tx on for aggregator ID %d\n",
2442 int slave_agg_id = 0;
2443 struct aggregator *agg =
SLAVE_AD_INFO(slave).port.aggregator;
2448 if (
SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
2464 struct slave *slave)
2467 struct lacpdu *lacpdu, _lacpdu;
2472 lacpdu = skb_header_pointer(skb, 0,
sizeof(_lacpdu), &_lacpdu);
2477 ret = bond_3ad_rx_indication(lacpdu, slave, skb->
len);
2493 struct slave *slave;
2494 struct port *port =
NULL;
2498 lacp_fast = bond->
params.lacp_fast;
2502 __get_state_machine_lock(port);
2507 __release_state_machine_lock(port);