1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved. 4 */ 5 6 #include <linux/skbuff.h> 7 #include <linux/if_ether.h> 8 #include <linux/netdevice.h> 9 #include <linux/spinlock.h> 10 #include <linux/ethtool.h> 11 #include <linux/etherdevice.h> 12 #include <linux/if_bonding.h> 13 #include <linux/pkt_sched.h> 14 #include <net/net_namespace.h> 15 #include <net/bonding.h> 16 #include <net/bond_3ad.h> 17 #include <net/netlink.h> 18 19 /* General definitions */ 20 #define AD_SHORT_TIMEOUT 1 21 #define AD_LONG_TIMEOUT 0 22 #define AD_STANDBY 0x2 23 #define AD_MAX_TX_IN_SECOND 3 24 #define AD_COLLECTOR_MAX_DELAY 0 25 26 /* Timer definitions (43.4.4 in the 802.3ad standard) */ 27 #define AD_FAST_PERIODIC_TIME 1 28 #define AD_SLOW_PERIODIC_TIME 30 29 #define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME) 30 #define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME) 31 #define AD_CHURN_DETECTION_TIME 60 32 #define AD_AGGREGATE_WAIT_TIME 2 33 34 /* Port Variables definitions used by the State Machines (43.4.7 in the 35 * 802.3ad standard) 36 */ 37 #define AD_PORT_BEGIN 0x1 38 #define AD_PORT_LACP_ENABLED 0x2 39 #define AD_PORT_ACTOR_CHURN 0x4 40 #define AD_PORT_PARTNER_CHURN 0x8 41 #define AD_PORT_READY 0x10 42 #define AD_PORT_READY_N 0x20 43 #define AD_PORT_MATCHED 0x40 44 #define AD_PORT_STANDBY 0x80 45 #define AD_PORT_SELECTED 0x100 46 #define AD_PORT_MOVED 0x200 47 #define AD_PORT_CHURNED (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN) 48 49 /* Port Key definitions 50 * key is determined according to the link speed, duplex and 51 * user key (which is yet not supported) 52 * -------------------------------------------------------------- 53 * Port key | User key (10 bits) | Speed (5 bits) | Duplex| 54 * -------------------------------------------------------------- 55 * |15 6|5 1|0 56 */ 57 #define AD_DUPLEX_KEY_MASKS 0x1 58 #define AD_SPEED_KEY_MASKS 0x3E 59 #define AD_USER_KEY_MASKS 0xFFC0 60 61 enum ad_link_speed_type { 62 AD_LINK_SPEED_1MBPS = 1, 63 AD_LINK_SPEED_10MBPS, 64 AD_LINK_SPEED_100MBPS, 65 AD_LINK_SPEED_1000MBPS, 66 AD_LINK_SPEED_2500MBPS, 67 AD_LINK_SPEED_5000MBPS, 68 AD_LINK_SPEED_10000MBPS, 69 AD_LINK_SPEED_14000MBPS, 70 AD_LINK_SPEED_20000MBPS, 71 AD_LINK_SPEED_25000MBPS, 72 AD_LINK_SPEED_40000MBPS, 73 AD_LINK_SPEED_50000MBPS, 74 AD_LINK_SPEED_56000MBPS, 75 AD_LINK_SPEED_80000MBPS, 76 AD_LINK_SPEED_100000MBPS, 77 AD_LINK_SPEED_200000MBPS, 78 AD_LINK_SPEED_400000MBPS, 79 AD_LINK_SPEED_800000MBPS, 80 AD_LINK_SPEED_1600000MBPS, 81 }; 82 83 /* compare MAC addresses */ 84 #define MAC_ADDRESS_EQUAL(A, B) \ 85 ether_addr_equal_64bits((const u8 *)A, (const u8 *)B) 86 87 static const u16 ad_ticks_per_sec = 1000 / AD_TIMER_INTERVAL; 88 static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000; 89 90 const u8 lacpdu_mcast_addr[ETH_ALEN + 2] __long_aligned = { 91 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 92 }; 93 94 /* ================= main 802.3ad protocol functions ================== */ 95 static int ad_lacpdu_send(struct port *port); 96 static int ad_marker_send(struct port *port, struct bond_marker *marker); 97 static void ad_mux_machine(struct port *port, bool *update_slave_arr); 98 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port); 99 static void ad_tx_machine(struct port *port); 100 static void ad_periodic_machine(struct port *port); 101 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr); 102 static void ad_agg_selection_logic(struct aggregator *aggregator, 103 bool *update_slave_arr); 104 static void ad_clear_agg(struct aggregator *aggregator); 105 static void ad_initialize_agg(struct aggregator *aggregator); 106 static void ad_initialize_port(struct port *port, const struct bond_params *bond_params); 107 static void ad_enable_collecting(struct port *port); 108 static void ad_disable_distributing(struct port *port, 109 bool *update_slave_arr); 110 static void ad_enable_collecting_distributing(struct port *port, 111 bool *update_slave_arr); 112 static void ad_disable_collecting_distributing(struct port *port, 113 bool *update_slave_arr); 114 static void ad_marker_info_received(struct bond_marker *marker_info, 115 struct port *port); 116 static void ad_marker_response_received(struct bond_marker *marker, 117 struct port *port); 118 static void ad_update_actor_keys(struct port *port, bool reset); 119 120 121 /* ================= api to bonding and kernel code ================== */ 122 123 /** 124 * __get_bond_by_port - get the port's bonding struct 125 * @port: the port we're looking at 126 * 127 * Return @port's bonding struct, or %NULL if it can't be found. 128 */ 129 static inline struct bonding *__get_bond_by_port(struct port *port) 130 { 131 if (port->slave == NULL) 132 return NULL; 133 134 return bond_get_bond_by_slave(port->slave); 135 } 136 137 /** 138 * __get_first_agg - get the first aggregator in the bond 139 * @port: the port we're looking at 140 * 141 * Return the aggregator of the first slave in @bond, or %NULL if it can't be 142 * found. 143 * The caller must hold RCU or RTNL lock. 144 */ 145 static inline struct aggregator *__get_first_agg(struct port *port) 146 { 147 struct bonding *bond = __get_bond_by_port(port); 148 struct slave *first_slave; 149 struct aggregator *agg; 150 151 /* If there's no bond for this port, or bond has no slaves */ 152 if (bond == NULL) 153 return NULL; 154 155 rcu_read_lock(); 156 first_slave = bond_first_slave_rcu(bond); 157 agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL; 158 rcu_read_unlock(); 159 160 return agg; 161 } 162 163 /** 164 * __agg_has_partner - see if we have a partner 165 * @agg: the agregator we're looking at 166 * 167 * Return nonzero if aggregator has a partner (denoted by a non-zero ether 168 * address for the partner). Return 0 if not. 169 */ 170 static inline int __agg_has_partner(struct aggregator *agg) 171 { 172 return !is_zero_ether_addr(agg->partner_system.mac_addr_value); 173 } 174 175 /** 176 * __disable_distributing_port - disable the port's slave for distributing. 177 * Port will still be able to collect. 178 * @port: the port we're looking at 179 * 180 * This will disable only distributing on the port's slave. 181 */ 182 static void __disable_distributing_port(struct port *port) 183 { 184 bond_set_slave_tx_disabled_flags(port->slave, BOND_SLAVE_NOTIFY_LATER); 185 } 186 187 /** 188 * __enable_collecting_port - enable the port's slave for collecting, 189 * if it's up 190 * @port: the port we're looking at 191 * 192 * This will enable only collecting on the port's slave. 193 */ 194 static void __enable_collecting_port(struct port *port) 195 { 196 struct slave *slave = port->slave; 197 198 if (slave->link == BOND_LINK_UP && bond_slave_is_up(slave)) 199 bond_set_slave_rx_enabled_flags(slave, BOND_SLAVE_NOTIFY_LATER); 200 } 201 202 /** 203 * __disable_port - disable the port's slave 204 * @port: the port we're looking at 205 * 206 * This will disable both collecting and distributing on the port's slave. 207 */ 208 static inline void __disable_port(struct port *port) 209 { 210 bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER); 211 } 212 213 /** 214 * __enable_port - enable the port's slave, if it's up 215 * @port: the port we're looking at 216 * 217 * This will enable both collecting and distributing on the port's slave. 218 */ 219 static inline void __enable_port(struct port *port) 220 { 221 struct slave *slave = port->slave; 222 223 if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave)) 224 bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER); 225 } 226 227 /** 228 * __port_move_to_attached_state - check if port should transition back to attached 229 * state. 230 * @port: the port we're looking at 231 */ 232 static bool __port_move_to_attached_state(struct port *port) 233 { 234 if (!(port->sm_vars & AD_PORT_SELECTED) || 235 (port->sm_vars & AD_PORT_STANDBY) || 236 !(port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) || 237 !(port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) 238 port->sm_mux_state = AD_MUX_ATTACHED; 239 240 return port->sm_mux_state == AD_MUX_ATTACHED; 241 } 242 243 /** 244 * __port_is_collecting_distributing - check if the port's slave is in the 245 * combined collecting/distributing state 246 * @port: the port we're looking at 247 */ 248 static int __port_is_collecting_distributing(struct port *port) 249 { 250 return bond_is_active_slave(port->slave); 251 } 252 253 /** 254 * __get_agg_selection_mode - get the aggregator selection mode 255 * @port: the port we're looking at 256 * 257 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT. 258 */ 259 static inline u32 __get_agg_selection_mode(struct port *port) 260 { 261 struct bonding *bond = __get_bond_by_port(port); 262 263 if (bond == NULL) 264 return BOND_AD_STABLE; 265 266 return bond->params.ad_select; 267 } 268 269 /** 270 * __check_agg_selection_timer - check if the selection timer has expired 271 * @port: the port we're looking at 272 */ 273 static inline int __check_agg_selection_timer(struct port *port) 274 { 275 struct bonding *bond = __get_bond_by_port(port); 276 277 if (bond == NULL) 278 return 0; 279 280 return atomic_read(&BOND_AD_INFO(bond).agg_select_timer) ? 1 : 0; 281 } 282 283 /** 284 * __get_link_speed - get a port's speed 285 * @port: the port we're looking at 286 * 287 * Return @port's speed in 802.3ad enum format. i.e. one of: 288 * 0, 289 * %AD_LINK_SPEED_10MBPS, 290 * %AD_LINK_SPEED_100MBPS, 291 * %AD_LINK_SPEED_1000MBPS, 292 * %AD_LINK_SPEED_2500MBPS, 293 * %AD_LINK_SPEED_5000MBPS, 294 * %AD_LINK_SPEED_10000MBPS 295 * %AD_LINK_SPEED_14000MBPS, 296 * %AD_LINK_SPEED_20000MBPS 297 * %AD_LINK_SPEED_25000MBPS 298 * %AD_LINK_SPEED_40000MBPS 299 * %AD_LINK_SPEED_50000MBPS 300 * %AD_LINK_SPEED_56000MBPS 301 * %AD_LINK_SPEED_80000MBPS 302 * %AD_LINK_SPEED_100000MBPS 303 * %AD_LINK_SPEED_200000MBPS 304 * %AD_LINK_SPEED_400000MBPS 305 * %AD_LINK_SPEED_800000MBPS 306 * %AD_LINK_SPEED_1600000MBPS 307 */ 308 static u16 __get_link_speed(struct port *port) 309 { 310 struct slave *slave = port->slave; 311 u16 speed; 312 313 /* this if covers only a special case: when the configuration starts 314 * with link down, it sets the speed to 0. 315 * This is done in spite of the fact that the e100 driver reports 0 316 * to be compatible with MVT in the future. 317 */ 318 if (slave->link != BOND_LINK_UP) 319 speed = 0; 320 else { 321 switch (slave->speed) { 322 case SPEED_10: 323 speed = AD_LINK_SPEED_10MBPS; 324 break; 325 326 case SPEED_100: 327 speed = AD_LINK_SPEED_100MBPS; 328 break; 329 330 case SPEED_1000: 331 speed = AD_LINK_SPEED_1000MBPS; 332 break; 333 334 case SPEED_2500: 335 speed = AD_LINK_SPEED_2500MBPS; 336 break; 337 338 case SPEED_5000: 339 speed = AD_LINK_SPEED_5000MBPS; 340 break; 341 342 case SPEED_10000: 343 speed = AD_LINK_SPEED_10000MBPS; 344 break; 345 346 case SPEED_14000: 347 speed = AD_LINK_SPEED_14000MBPS; 348 break; 349 350 case SPEED_20000: 351 speed = AD_LINK_SPEED_20000MBPS; 352 break; 353 354 case SPEED_25000: 355 speed = AD_LINK_SPEED_25000MBPS; 356 break; 357 358 case SPEED_40000: 359 speed = AD_LINK_SPEED_40000MBPS; 360 break; 361 362 case SPEED_50000: 363 speed = AD_LINK_SPEED_50000MBPS; 364 break; 365 366 case SPEED_56000: 367 speed = AD_LINK_SPEED_56000MBPS; 368 break; 369 370 case SPEED_80000: 371 speed = AD_LINK_SPEED_80000MBPS; 372 break; 373 374 case SPEED_100000: 375 speed = AD_LINK_SPEED_100000MBPS; 376 break; 377 378 case SPEED_200000: 379 speed = AD_LINK_SPEED_200000MBPS; 380 break; 381 382 case SPEED_400000: 383 speed = AD_LINK_SPEED_400000MBPS; 384 break; 385 386 case SPEED_800000: 387 speed = AD_LINK_SPEED_800000MBPS; 388 break; 389 390 case SPEED_1600000: 391 speed = AD_LINK_SPEED_1600000MBPS; 392 break; 393 394 default: 395 /* unknown speed value from ethtool. shouldn't happen */ 396 if (slave->speed != SPEED_UNKNOWN) 397 pr_err_once("%s: (slave %s): unknown ethtool speed (%d) for port %d (set it to 0)\n", 398 slave->bond->dev->name, 399 slave->dev->name, slave->speed, 400 port->actor_port_number); 401 speed = 0; 402 break; 403 } 404 } 405 406 slave_dbg(slave->bond->dev, slave->dev, "Port %d Received link speed %d update from adapter\n", 407 port->actor_port_number, speed); 408 return speed; 409 } 410 411 /** 412 * __get_duplex - get a port's duplex 413 * @port: the port we're looking at 414 * 415 * Return @port's duplex in 802.3ad bitmask format. i.e.: 416 * 0x01 if in full duplex 417 * 0x00 otherwise 418 */ 419 static u8 __get_duplex(struct port *port) 420 { 421 struct slave *slave = port->slave; 422 u8 retval = 0x0; 423 424 /* handling a special case: when the configuration starts with 425 * link down, it sets the duplex to 0. 426 */ 427 if (slave->link == BOND_LINK_UP) { 428 switch (slave->duplex) { 429 case DUPLEX_FULL: 430 retval = 0x1; 431 slave_dbg(slave->bond->dev, slave->dev, "Port %d Received status full duplex update from adapter\n", 432 port->actor_port_number); 433 break; 434 case DUPLEX_HALF: 435 default: 436 retval = 0x0; 437 slave_dbg(slave->bond->dev, slave->dev, "Port %d Received status NOT full duplex update from adapter\n", 438 port->actor_port_number); 439 break; 440 } 441 } 442 return retval; 443 } 444 445 static void __ad_actor_update_port(struct port *port) 446 { 447 const struct bonding *bond = bond_get_bond_by_slave(port->slave); 448 449 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr; 450 port->actor_system_priority = BOND_AD_INFO(bond).system.sys_priority; 451 port->actor_port_priority = SLAVE_AD_INFO(port->slave)->port_priority; 452 } 453 454 /* Conversions */ 455 456 /** 457 * __ad_timer_to_ticks - convert a given timer type to AD module ticks 458 * @timer_type: which timer to operate 459 * @par: timer parameter. see below 460 * 461 * If @timer_type is %current_while_timer, @par indicates long/short timer. 462 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME, 463 * %SLOW_PERIODIC_TIME. 464 */ 465 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par) 466 { 467 u16 retval = 0; /* to silence the compiler */ 468 469 switch (timer_type) { 470 case AD_CURRENT_WHILE_TIMER: /* for rx machine usage */ 471 if (par) 472 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); 473 else 474 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); 475 break; 476 case AD_ACTOR_CHURN_TIMER: /* for local churn machine */ 477 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec); 478 break; 479 case AD_PERIODIC_TIMER: /* for periodic machine */ 480 retval = (par*ad_ticks_per_sec); /* long timeout */ 481 break; 482 case AD_PARTNER_CHURN_TIMER: /* for remote churn machine */ 483 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec); 484 break; 485 case AD_WAIT_WHILE_TIMER: /* for selection machine */ 486 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec); 487 break; 488 } 489 490 return retval; 491 } 492 493 494 /* ================= ad_rx_machine helper functions ================== */ 495 496 /** 497 * __choose_matched - update a port's matched variable from a received lacpdu 498 * @lacpdu: the lacpdu we've received 499 * @port: the port we're looking at 500 * 501 * Update the value of the matched variable, using parameter values from a 502 * newly received lacpdu. Parameter values for the partner carried in the 503 * received PDU are compared with the corresponding operational parameter 504 * values for the actor. Matched is set to TRUE if all of these parameters 505 * match and the PDU parameter partner_state.aggregation has the same value as 506 * actor_oper_port_state.aggregation and lacp will actively maintain the link 507 * in the aggregation. Matched is also set to TRUE if the value of 508 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates 509 * an individual link and lacp will actively maintain the link. Otherwise, 510 * matched is set to FALSE. LACP is considered to be actively maintaining the 511 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both 512 * the actor's actor_oper_port_state.lacp_activity and the PDU's 513 * partner_state.lacp_activity variables are TRUE. 514 * 515 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is 516 * used here to implement the language from 802.3ad 43.4.9 that requires 517 * recordPDU to "match" the LACPDU parameters to the stored values. 518 */ 519 static void __choose_matched(struct lacpdu *lacpdu, struct port *port) 520 { 521 /* check if all parameters are alike 522 * or this is individual link(aggregation == FALSE) 523 * then update the state machine Matched variable. 524 */ 525 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) && 526 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) && 527 MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) && 528 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) && 529 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) && 530 ((lacpdu->partner_state & LACP_STATE_AGGREGATION) == (port->actor_oper_port_state & LACP_STATE_AGGREGATION))) || 531 ((lacpdu->actor_state & LACP_STATE_AGGREGATION) == 0) 532 ) { 533 port->sm_vars |= AD_PORT_MATCHED; 534 } else { 535 port->sm_vars &= ~AD_PORT_MATCHED; 536 } 537 } 538 539 /** 540 * __record_pdu - record parameters from a received lacpdu 541 * @lacpdu: the lacpdu we've received 542 * @port: the port we're looking at 543 * 544 * Record the parameter values for the Actor carried in a received lacpdu as 545 * the current partner operational parameter values and sets 546 * actor_oper_port_state.defaulted to FALSE. 547 */ 548 static void __record_pdu(struct lacpdu *lacpdu, struct port *port) 549 { 550 if (lacpdu && port) { 551 struct port_params *partner = &port->partner_oper; 552 553 __choose_matched(lacpdu, port); 554 /* record the new parameter values for the partner 555 * operational 556 */ 557 partner->port_number = ntohs(lacpdu->actor_port); 558 partner->port_priority = ntohs(lacpdu->actor_port_priority); 559 partner->system = lacpdu->actor_system; 560 partner->system_priority = ntohs(lacpdu->actor_system_priority); 561 partner->key = ntohs(lacpdu->actor_key); 562 partner->port_state = lacpdu->actor_state; 563 564 /* set actor_oper_port_state.defaulted to FALSE */ 565 port->actor_oper_port_state &= ~LACP_STATE_DEFAULTED; 566 567 /* set the partner sync. to on if the partner is sync, 568 * and the port is matched 569 */ 570 if ((port->sm_vars & AD_PORT_MATCHED) && 571 (lacpdu->actor_state & LACP_STATE_SYNCHRONIZATION)) { 572 partner->port_state |= LACP_STATE_SYNCHRONIZATION; 573 slave_dbg(port->slave->bond->dev, port->slave->dev, 574 "partner sync=1\n"); 575 } else { 576 partner->port_state &= ~LACP_STATE_SYNCHRONIZATION; 577 slave_dbg(port->slave->bond->dev, port->slave->dev, 578 "partner sync=0\n"); 579 } 580 } 581 } 582 583 /** 584 * __record_default - record default parameters 585 * @port: the port we're looking at 586 * 587 * This function records the default parameter values for the partner carried 588 * in the Partner Admin parameters as the current partner operational parameter 589 * values and sets actor_oper_port_state.defaulted to TRUE. 590 */ 591 static void __record_default(struct port *port) 592 { 593 if (port) { 594 /* record the partner admin parameters */ 595 memcpy(&port->partner_oper, &port->partner_admin, 596 sizeof(struct port_params)); 597 598 /* set actor_oper_port_state.defaulted to true */ 599 port->actor_oper_port_state |= LACP_STATE_DEFAULTED; 600 } 601 } 602 603 /** 604 * __update_selected - update a port's Selected variable from a received lacpdu 605 * @lacpdu: the lacpdu we've received 606 * @port: the port we're looking at 607 * 608 * Update the value of the selected variable, using parameter values from a 609 * newly received lacpdu. The parameter values for the Actor carried in the 610 * received PDU are compared with the corresponding operational parameter 611 * values for the ports partner. If one or more of the comparisons shows that 612 * the value(s) received in the PDU differ from the current operational values, 613 * then selected is set to FALSE and actor_oper_port_state.synchronization is 614 * set to out_of_sync. Otherwise, selected remains unchanged. 615 */ 616 static void __update_selected(struct lacpdu *lacpdu, struct port *port) 617 { 618 if (lacpdu && port) { 619 const struct port_params *partner = &port->partner_oper; 620 621 /* check if any parameter is different then 622 * update the state machine selected variable. 623 */ 624 if (ntohs(lacpdu->actor_port) != partner->port_number || 625 ntohs(lacpdu->actor_port_priority) != partner->port_priority || 626 !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) || 627 ntohs(lacpdu->actor_system_priority) != partner->system_priority || 628 ntohs(lacpdu->actor_key) != partner->key || 629 (lacpdu->actor_state & LACP_STATE_AGGREGATION) != (partner->port_state & LACP_STATE_AGGREGATION)) { 630 port->sm_vars &= ~AD_PORT_SELECTED; 631 } 632 } 633 } 634 635 /** 636 * __update_default_selected - update a port's Selected variable from Partner 637 * @port: the port we're looking at 638 * 639 * This function updates the value of the selected variable, using the partner 640 * administrative parameter values. The administrative values are compared with 641 * the corresponding operational parameter values for the partner. If one or 642 * more of the comparisons shows that the administrative value(s) differ from 643 * the current operational values, then Selected is set to FALSE and 644 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise, 645 * Selected remains unchanged. 646 */ 647 static void __update_default_selected(struct port *port) 648 { 649 if (port) { 650 const struct port_params *admin = &port->partner_admin; 651 const struct port_params *oper = &port->partner_oper; 652 653 /* check if any parameter is different then 654 * update the state machine selected variable. 655 */ 656 if (admin->port_number != oper->port_number || 657 admin->port_priority != oper->port_priority || 658 !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) || 659 admin->system_priority != oper->system_priority || 660 admin->key != oper->key || 661 (admin->port_state & LACP_STATE_AGGREGATION) 662 != (oper->port_state & LACP_STATE_AGGREGATION)) { 663 port->sm_vars &= ~AD_PORT_SELECTED; 664 } 665 } 666 } 667 668 /** 669 * __update_ntt - update a port's ntt variable from a received lacpdu 670 * @lacpdu: the lacpdu we've received 671 * @port: the port we're looking at 672 * 673 * Updates the value of the ntt variable, using parameter values from a newly 674 * received lacpdu. The parameter values for the partner carried in the 675 * received PDU are compared with the corresponding operational parameter 676 * values for the Actor. If one or more of the comparisons shows that the 677 * value(s) received in the PDU differ from the current operational values, 678 * then ntt is set to TRUE. Otherwise, ntt remains unchanged. 679 */ 680 static void __update_ntt(struct lacpdu *lacpdu, struct port *port) 681 { 682 /* validate lacpdu and port */ 683 if (lacpdu && port) { 684 /* check if any parameter is different then 685 * update the port->ntt. 686 */ 687 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) || 688 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) || 689 !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) || 690 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) || 691 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) || 692 ((lacpdu->partner_state & LACP_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & LACP_STATE_LACP_ACTIVITY)) || 693 ((lacpdu->partner_state & LACP_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & LACP_STATE_LACP_TIMEOUT)) || 694 ((lacpdu->partner_state & LACP_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) || 695 ((lacpdu->partner_state & LACP_STATE_AGGREGATION) != (port->actor_oper_port_state & LACP_STATE_AGGREGATION)) 696 ) { 697 port->ntt = true; 698 } 699 } 700 } 701 702 /** 703 * __agg_ports_are_ready - check if all ports in an aggregator are ready 704 * @aggregator: the aggregator we're looking at 705 * 706 */ 707 static int __agg_ports_are_ready(struct aggregator *aggregator) 708 { 709 struct port *port; 710 int retval = 1; 711 712 if (aggregator) { 713 /* scan all ports in this aggregator to verfy if they are 714 * all ready. 715 */ 716 for (port = aggregator->lag_ports; 717 port; 718 port = port->next_port_in_aggregator) { 719 if (!(port->sm_vars & AD_PORT_READY_N)) { 720 retval = 0; 721 break; 722 } 723 } 724 } 725 726 return retval; 727 } 728 729 /** 730 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator 731 * @aggregator: the aggregator we're looking at 732 * @val: Should the ports' ready bit be set on or off 733 * 734 */ 735 static void __set_agg_ports_ready(struct aggregator *aggregator, int val) 736 { 737 struct port *port; 738 739 for (port = aggregator->lag_ports; port; 740 port = port->next_port_in_aggregator) { 741 if (val) 742 port->sm_vars |= AD_PORT_READY; 743 else 744 port->sm_vars &= ~AD_PORT_READY; 745 } 746 } 747 748 static int __agg_active_ports(struct aggregator *agg) 749 { 750 struct port *port; 751 int active = 0; 752 753 for (port = agg->lag_ports; port; 754 port = port->next_port_in_aggregator) { 755 if (port->is_enabled) 756 active++; 757 } 758 759 return active; 760 } 761 762 static unsigned int __agg_ports_priority(const struct aggregator *agg) 763 { 764 struct port *port = agg->lag_ports; 765 unsigned int prio = 0; 766 767 for (; port; port = port->next_port_in_aggregator) 768 if (port->is_enabled) 769 prio += port->actor_port_priority; 770 771 return prio; 772 } 773 774 /** 775 * __get_agg_bandwidth - get the total bandwidth of an aggregator 776 * @aggregator: the aggregator we're looking at 777 * 778 */ 779 static u32 __get_agg_bandwidth(struct aggregator *aggregator) 780 { 781 int nports = __agg_active_ports(aggregator); 782 u32 bandwidth = 0; 783 784 if (nports) { 785 switch (__get_link_speed(aggregator->lag_ports)) { 786 case AD_LINK_SPEED_1MBPS: 787 bandwidth = nports; 788 break; 789 case AD_LINK_SPEED_10MBPS: 790 bandwidth = nports * 10; 791 break; 792 case AD_LINK_SPEED_100MBPS: 793 bandwidth = nports * 100; 794 break; 795 case AD_LINK_SPEED_1000MBPS: 796 bandwidth = nports * 1000; 797 break; 798 case AD_LINK_SPEED_2500MBPS: 799 bandwidth = nports * 2500; 800 break; 801 case AD_LINK_SPEED_5000MBPS: 802 bandwidth = nports * 5000; 803 break; 804 case AD_LINK_SPEED_10000MBPS: 805 bandwidth = nports * 10000; 806 break; 807 case AD_LINK_SPEED_14000MBPS: 808 bandwidth = nports * 14000; 809 break; 810 case AD_LINK_SPEED_20000MBPS: 811 bandwidth = nports * 20000; 812 break; 813 case AD_LINK_SPEED_25000MBPS: 814 bandwidth = nports * 25000; 815 break; 816 case AD_LINK_SPEED_40000MBPS: 817 bandwidth = nports * 40000; 818 break; 819 case AD_LINK_SPEED_50000MBPS: 820 bandwidth = nports * 50000; 821 break; 822 case AD_LINK_SPEED_56000MBPS: 823 bandwidth = nports * 56000; 824 break; 825 case AD_LINK_SPEED_80000MBPS: 826 bandwidth = nports * 80000; 827 break; 828 case AD_LINK_SPEED_100000MBPS: 829 bandwidth = nports * 100000; 830 break; 831 case AD_LINK_SPEED_200000MBPS: 832 bandwidth = nports * 200000; 833 break; 834 case AD_LINK_SPEED_400000MBPS: 835 bandwidth = nports * 400000; 836 break; 837 case AD_LINK_SPEED_800000MBPS: 838 bandwidth = nports * 800000; 839 break; 840 case AD_LINK_SPEED_1600000MBPS: 841 bandwidth = nports * 1600000; 842 break; 843 default: 844 bandwidth = 0; /* to silence the compiler */ 845 } 846 } 847 return bandwidth; 848 } 849 850 /** 851 * __get_active_agg - get the current active aggregator 852 * @aggregator: the aggregator we're looking at 853 * 854 * Caller must hold RCU lock. 855 */ 856 static struct aggregator *__get_active_agg(struct aggregator *aggregator) 857 { 858 struct bonding *bond = aggregator->slave->bond; 859 struct list_head *iter; 860 struct slave *slave; 861 862 bond_for_each_slave_rcu(bond, slave, iter) 863 if (SLAVE_AD_INFO(slave)->aggregator.is_active) 864 return &(SLAVE_AD_INFO(slave)->aggregator); 865 866 return NULL; 867 } 868 869 /** 870 * __update_lacpdu_from_port - update a port's lacpdu fields 871 * @port: the port we're looking at 872 */ 873 static inline void __update_lacpdu_from_port(struct port *port) 874 { 875 struct lacpdu *lacpdu = &port->lacpdu; 876 const struct port_params *partner = &port->partner_oper; 877 878 /* update current actual Actor parameters 879 * lacpdu->subtype initialized 880 * lacpdu->version_number initialized 881 * lacpdu->tlv_type_actor_info initialized 882 * lacpdu->actor_information_length initialized 883 */ 884 885 lacpdu->actor_system_priority = htons(port->actor_system_priority); 886 lacpdu->actor_system = port->actor_system; 887 lacpdu->actor_key = htons(port->actor_oper_port_key); 888 lacpdu->actor_port_priority = htons(port->actor_port_priority); 889 lacpdu->actor_port = htons(port->actor_port_number); 890 lacpdu->actor_state = port->actor_oper_port_state; 891 slave_dbg(port->slave->bond->dev, port->slave->dev, 892 "update lacpdu: actor port state %x\n", 893 port->actor_oper_port_state); 894 895 /* lacpdu->reserved_3_1 initialized 896 * lacpdu->tlv_type_partner_info initialized 897 * lacpdu->partner_information_length initialized 898 */ 899 900 lacpdu->partner_system_priority = htons(partner->system_priority); 901 lacpdu->partner_system = partner->system; 902 lacpdu->partner_key = htons(partner->key); 903 lacpdu->partner_port_priority = htons(partner->port_priority); 904 lacpdu->partner_port = htons(partner->port_number); 905 lacpdu->partner_state = partner->port_state; 906 907 /* lacpdu->reserved_3_2 initialized 908 * lacpdu->tlv_type_collector_info initialized 909 * lacpdu->collector_information_length initialized 910 * collector_max_delay initialized 911 * reserved_12[12] initialized 912 * tlv_type_terminator initialized 913 * terminator_length initialized 914 * reserved_50[50] initialized 915 */ 916 } 917 918 /* ================= main 802.3ad protocol code ========================= */ 919 920 /** 921 * ad_lacpdu_send - send out a lacpdu packet on a given port 922 * @port: the port we're looking at 923 * 924 * Returns: 0 on success 925 * < 0 on error 926 */ 927 static int ad_lacpdu_send(struct port *port) 928 { 929 struct slave *slave = port->slave; 930 struct sk_buff *skb; 931 struct lacpdu_header *lacpdu_header; 932 int length = sizeof(struct lacpdu_header); 933 934 skb = dev_alloc_skb(length); 935 if (!skb) 936 return -ENOMEM; 937 938 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_tx); 939 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.lacpdu_tx); 940 941 skb->dev = slave->dev; 942 skb_reset_mac_header(skb); 943 skb->network_header = skb->mac_header + ETH_HLEN; 944 skb->protocol = PKT_TYPE_LACPDU; 945 skb->priority = TC_PRIO_CONTROL; 946 947 lacpdu_header = skb_put(skb, length); 948 949 ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr); 950 /* Note: source address is set to be the member's PERMANENT address, 951 * because we use it to identify loopback lacpdus in receive. 952 */ 953 ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr); 954 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU; 955 956 lacpdu_header->lacpdu = port->lacpdu; 957 958 dev_queue_xmit(skb); 959 960 return 0; 961 } 962 963 /** 964 * ad_marker_send - send marker information/response on a given port 965 * @port: the port we're looking at 966 * @marker: marker data to send 967 * 968 * Returns: 0 on success 969 * < 0 on error 970 */ 971 static int ad_marker_send(struct port *port, struct bond_marker *marker) 972 { 973 struct slave *slave = port->slave; 974 struct sk_buff *skb; 975 struct bond_marker_header *marker_header; 976 int length = sizeof(struct bond_marker_header); 977 978 skb = dev_alloc_skb(length + 16); 979 if (!skb) 980 return -ENOMEM; 981 982 switch (marker->tlv_type) { 983 case AD_MARKER_INFORMATION_SUBTYPE: 984 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_tx); 985 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_tx); 986 break; 987 case AD_MARKER_RESPONSE_SUBTYPE: 988 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_resp_tx); 989 atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_resp_tx); 990 break; 991 } 992 993 skb_reserve(skb, 16); 994 995 skb->dev = slave->dev; 996 skb_reset_mac_header(skb); 997 skb->network_header = skb->mac_header + ETH_HLEN; 998 skb->protocol = PKT_TYPE_LACPDU; 999 1000 marker_header = skb_put(skb, length); 1001 1002 ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr); 1003 /* Note: source address is set to be the member's PERMANENT address, 1004 * because we use it to identify loopback MARKERs in receive. 1005 */ 1006 ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr); 1007 marker_header->hdr.h_proto = PKT_TYPE_LACPDU; 1008 1009 marker_header->marker = *marker; 1010 1011 dev_queue_xmit(skb); 1012 1013 return 0; 1014 } 1015 1016 static void ad_cond_set_peer_notif(struct port *port) 1017 { 1018 struct bonding *bond = port->slave->bond; 1019 1020 if (bond->params.broadcast_neighbor && rtnl_trylock()) { 1021 bond->send_peer_notif = bond->params.num_peer_notif * 1022 max(1, bond->params.peer_notif_delay); 1023 rtnl_unlock(); 1024 } 1025 } 1026 1027 /** 1028 * ad_mux_machine - handle a port's mux state machine 1029 * @port: the port we're looking at 1030 * @update_slave_arr: Does slave array need update? 1031 */ 1032 static void ad_mux_machine(struct port *port, bool *update_slave_arr) 1033 { 1034 struct bonding *bond = __get_bond_by_port(port); 1035 mux_states_t last_state; 1036 1037 /* keep current State Machine state to compare later if it was 1038 * changed 1039 */ 1040 last_state = port->sm_mux_state; 1041 1042 if (port->sm_vars & AD_PORT_BEGIN) { 1043 port->sm_mux_state = AD_MUX_DETACHED; 1044 } else { 1045 switch (port->sm_mux_state) { 1046 case AD_MUX_DETACHED: 1047 if ((port->sm_vars & AD_PORT_SELECTED) 1048 || (port->sm_vars & AD_PORT_STANDBY)) 1049 /* if SELECTED or STANDBY */ 1050 port->sm_mux_state = AD_MUX_WAITING; 1051 break; 1052 case AD_MUX_WAITING: 1053 /* if SELECTED == FALSE return to DETACH state */ 1054 if (!(port->sm_vars & AD_PORT_SELECTED)) { 1055 port->sm_vars &= ~AD_PORT_READY_N; 1056 /* in order to withhold the Selection Logic to 1057 * check all ports READY_N value every callback 1058 * cycle to update ready variable, we check 1059 * READY_N and update READY here 1060 */ 1061 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator)); 1062 port->sm_mux_state = AD_MUX_DETACHED; 1063 break; 1064 } 1065 1066 /* check if the wait_while_timer expired */ 1067 if (port->sm_mux_timer_counter 1068 && !(--port->sm_mux_timer_counter)) 1069 port->sm_vars |= AD_PORT_READY_N; 1070 1071 /* in order to withhold the selection logic to check 1072 * all ports READY_N value every callback cycle to 1073 * update ready variable, we check READY_N and update 1074 * READY here 1075 */ 1076 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator)); 1077 1078 /* if the wait_while_timer expired, and the port is 1079 * in READY state, move to ATTACHED state 1080 */ 1081 if ((port->sm_vars & AD_PORT_READY) 1082 && !port->sm_mux_timer_counter) 1083 port->sm_mux_state = AD_MUX_ATTACHED; 1084 break; 1085 case AD_MUX_ATTACHED: 1086 /* check also if agg_select_timer expired (so the 1087 * edable port will take place only after this timer) 1088 */ 1089 if ((port->sm_vars & AD_PORT_SELECTED) && 1090 (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) && 1091 !__check_agg_selection_timer(port)) { 1092 if (port->aggregator->is_active) { 1093 int state = AD_MUX_COLLECTING_DISTRIBUTING; 1094 1095 if (!bond->params.coupled_control) 1096 state = AD_MUX_COLLECTING; 1097 port->sm_mux_state = state; 1098 } 1099 } else if (!(port->sm_vars & AD_PORT_SELECTED) || 1100 (port->sm_vars & AD_PORT_STANDBY)) { 1101 /* if UNSELECTED or STANDBY */ 1102 port->sm_vars &= ~AD_PORT_READY_N; 1103 /* in order to withhold the selection logic to 1104 * check all ports READY_N value every callback 1105 * cycle to update ready variable, we check 1106 * READY_N and update READY here 1107 */ 1108 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator)); 1109 port->sm_mux_state = AD_MUX_DETACHED; 1110 } else if (port->aggregator->is_active) { 1111 port->actor_oper_port_state |= 1112 LACP_STATE_SYNCHRONIZATION; 1113 } 1114 break; 1115 case AD_MUX_COLLECTING_DISTRIBUTING: 1116 if (!__port_move_to_attached_state(port)) { 1117 /* if port state hasn't changed make 1118 * sure that a collecting distributing 1119 * port in an active aggregator is enabled 1120 */ 1121 if (port->aggregator->is_active && 1122 !__port_is_collecting_distributing(port)) { 1123 __enable_port(port); 1124 *update_slave_arr = true; 1125 } 1126 } 1127 break; 1128 case AD_MUX_COLLECTING: 1129 if (!__port_move_to_attached_state(port)) { 1130 if ((port->sm_vars & AD_PORT_SELECTED) && 1131 (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) && 1132 (port->partner_oper.port_state & LACP_STATE_COLLECTING)) { 1133 port->sm_mux_state = AD_MUX_DISTRIBUTING; 1134 } else { 1135 /* If port state hasn't changed, make sure that a collecting 1136 * port is enabled for an active aggregator. 1137 */ 1138 struct slave *slave = port->slave; 1139 1140 if (port->aggregator->is_active && 1141 bond_is_slave_rx_disabled(slave)) { 1142 ad_enable_collecting(port); 1143 *update_slave_arr = true; 1144 } 1145 } 1146 } 1147 break; 1148 case AD_MUX_DISTRIBUTING: 1149 if (!(port->sm_vars & AD_PORT_SELECTED) || 1150 (port->sm_vars & AD_PORT_STANDBY) || 1151 !(port->partner_oper.port_state & LACP_STATE_COLLECTING) || 1152 !(port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) || 1153 !(port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) { 1154 port->sm_mux_state = AD_MUX_COLLECTING; 1155 } else { 1156 /* if port state hasn't changed make 1157 * sure that a collecting distributing 1158 * port in an active aggregator is enabled 1159 */ 1160 if (port->aggregator && 1161 port->aggregator->is_active && 1162 !__port_is_collecting_distributing(port)) { 1163 __enable_port(port); 1164 *update_slave_arr = true; 1165 } 1166 } 1167 break; 1168 default: 1169 break; 1170 } 1171 } 1172 1173 /* check if the state machine was changed */ 1174 if (port->sm_mux_state != last_state) { 1175 slave_dbg(port->slave->bond->dev, port->slave->dev, 1176 "Mux Machine: Port=%d, Last State=%d, Curr State=%d\n", 1177 port->actor_port_number, 1178 last_state, 1179 port->sm_mux_state); 1180 switch (port->sm_mux_state) { 1181 case AD_MUX_DETACHED: 1182 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION; 1183 ad_disable_collecting_distributing(port, 1184 update_slave_arr); 1185 port->actor_oper_port_state &= ~LACP_STATE_COLLECTING; 1186 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING; 1187 port->ntt = true; 1188 break; 1189 case AD_MUX_WAITING: 1190 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0); 1191 break; 1192 case AD_MUX_ATTACHED: 1193 if (port->aggregator->is_active) 1194 port->actor_oper_port_state |= 1195 LACP_STATE_SYNCHRONIZATION; 1196 else 1197 port->actor_oper_port_state &= 1198 ~LACP_STATE_SYNCHRONIZATION; 1199 port->actor_oper_port_state &= ~LACP_STATE_COLLECTING; 1200 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING; 1201 ad_disable_collecting_distributing(port, 1202 update_slave_arr); 1203 port->ntt = true; 1204 break; 1205 case AD_MUX_COLLECTING_DISTRIBUTING: 1206 port->actor_oper_port_state |= LACP_STATE_COLLECTING; 1207 port->actor_oper_port_state |= LACP_STATE_DISTRIBUTING; 1208 port->actor_oper_port_state |= LACP_STATE_SYNCHRONIZATION; 1209 ad_enable_collecting_distributing(port, 1210 update_slave_arr); 1211 port->ntt = true; 1212 break; 1213 case AD_MUX_COLLECTING: 1214 port->actor_oper_port_state |= LACP_STATE_COLLECTING; 1215 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING; 1216 port->actor_oper_port_state |= LACP_STATE_SYNCHRONIZATION; 1217 ad_enable_collecting(port); 1218 ad_disable_distributing(port, update_slave_arr); 1219 port->ntt = true; 1220 break; 1221 case AD_MUX_DISTRIBUTING: 1222 port->actor_oper_port_state |= LACP_STATE_DISTRIBUTING; 1223 port->actor_oper_port_state |= LACP_STATE_SYNCHRONIZATION; 1224 ad_enable_collecting_distributing(port, 1225 update_slave_arr); 1226 break; 1227 default: 1228 break; 1229 } 1230 } 1231 } 1232 1233 /** 1234 * ad_rx_machine - handle a port's rx State Machine 1235 * @lacpdu: the lacpdu we've received 1236 * @port: the port we're looking at 1237 * 1238 * If lacpdu arrived, stop previous timer (if exists) and set the next state as 1239 * CURRENT. If timer expired set the state machine in the proper state. 1240 * In other cases, this function checks if we need to switch to other state. 1241 */ 1242 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port) 1243 { 1244 rx_states_t last_state; 1245 1246 /* keep current State Machine state to compare later if it was 1247 * changed 1248 */ 1249 last_state = port->sm_rx_state; 1250 1251 if (lacpdu) { 1252 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.lacpdu_rx); 1253 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.lacpdu_rx); 1254 } 1255 /* check if state machine should change state */ 1256 1257 /* first, check if port was reinitialized */ 1258 if (port->sm_vars & AD_PORT_BEGIN) { 1259 port->sm_rx_state = AD_RX_INITIALIZE; 1260 port->sm_vars |= AD_PORT_CHURNED; 1261 /* check if port is not enabled */ 1262 } else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled) 1263 port->sm_rx_state = AD_RX_PORT_DISABLED; 1264 /* check if new lacpdu arrived */ 1265 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || 1266 (port->sm_rx_state == AD_RX_DEFAULTED) || 1267 (port->sm_rx_state == AD_RX_CURRENT))) { 1268 if (port->sm_rx_state != AD_RX_CURRENT) 1269 port->sm_vars |= AD_PORT_CHURNED; 1270 port->sm_rx_timer_counter = 0; 1271 port->sm_rx_state = AD_RX_CURRENT; 1272 } else { 1273 /* if timer is on, and if it is expired */ 1274 if (port->sm_rx_timer_counter && 1275 !(--port->sm_rx_timer_counter)) { 1276 switch (port->sm_rx_state) { 1277 case AD_RX_EXPIRED: 1278 port->sm_rx_state = AD_RX_DEFAULTED; 1279 break; 1280 case AD_RX_CURRENT: 1281 port->sm_rx_state = AD_RX_EXPIRED; 1282 break; 1283 default: 1284 break; 1285 } 1286 } else { 1287 /* if no lacpdu arrived and no timer is on */ 1288 switch (port->sm_rx_state) { 1289 case AD_RX_PORT_DISABLED: 1290 if (port->is_enabled && 1291 (port->sm_vars & AD_PORT_LACP_ENABLED)) 1292 port->sm_rx_state = AD_RX_EXPIRED; 1293 else if (port->is_enabled 1294 && ((port->sm_vars 1295 & AD_PORT_LACP_ENABLED) == 0)) 1296 port->sm_rx_state = AD_RX_LACP_DISABLED; 1297 break; 1298 default: 1299 break; 1300 1301 } 1302 } 1303 } 1304 1305 /* check if the State machine was changed or new lacpdu arrived */ 1306 if ((port->sm_rx_state != last_state) || (lacpdu)) { 1307 slave_dbg(port->slave->bond->dev, port->slave->dev, 1308 "Rx Machine: Port=%d, Last State=%d, Curr State=%d\n", 1309 port->actor_port_number, 1310 last_state, 1311 port->sm_rx_state); 1312 switch (port->sm_rx_state) { 1313 case AD_RX_INITIALIZE: 1314 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)) 1315 port->sm_vars &= ~AD_PORT_LACP_ENABLED; 1316 else 1317 port->sm_vars |= AD_PORT_LACP_ENABLED; 1318 port->sm_vars &= ~AD_PORT_SELECTED; 1319 __record_default(port); 1320 port->actor_oper_port_state &= ~LACP_STATE_EXPIRED; 1321 port->sm_rx_state = AD_RX_PORT_DISABLED; 1322 1323 fallthrough; 1324 case AD_RX_PORT_DISABLED: 1325 port->sm_vars &= ~AD_PORT_MATCHED; 1326 break; 1327 case AD_RX_LACP_DISABLED: 1328 port->sm_vars &= ~AD_PORT_SELECTED; 1329 __record_default(port); 1330 port->partner_oper.port_state &= ~LACP_STATE_AGGREGATION; 1331 port->sm_vars |= AD_PORT_MATCHED; 1332 port->actor_oper_port_state &= ~LACP_STATE_EXPIRED; 1333 break; 1334 case AD_RX_EXPIRED: 1335 /* Reset of the Synchronization flag (Standard 43.4.12) 1336 * This reset cause to disable this port in the 1337 * COLLECTING_DISTRIBUTING state of the mux machine in 1338 * case of EXPIRED even if LINK_DOWN didn't arrive for 1339 * the port. 1340 */ 1341 port->sm_vars &= ~AD_PORT_MATCHED; 1342 /* Based on IEEE 8021AX-2014, Figure 6-18 - Receive 1343 * machine state diagram, the statue should be 1344 * Partner_Oper_Port_State.Synchronization = FALSE; 1345 * Partner_Oper_Port_State.LACP_Timeout = Short Timeout; 1346 * start current_while_timer(Short Timeout); 1347 * Actor_Oper_Port_State.Expired = TRUE; 1348 */ 1349 port->partner_oper.port_state &= ~LACP_STATE_SYNCHRONIZATION; 1350 port->partner_oper.port_state |= LACP_STATE_LACP_TIMEOUT; 1351 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT)); 1352 port->actor_oper_port_state |= LACP_STATE_EXPIRED; 1353 port->sm_vars |= AD_PORT_CHURNED; 1354 break; 1355 case AD_RX_DEFAULTED: 1356 __update_default_selected(port); 1357 __record_default(port); 1358 port->sm_vars |= AD_PORT_MATCHED; 1359 port->actor_oper_port_state &= ~LACP_STATE_EXPIRED; 1360 break; 1361 case AD_RX_CURRENT: 1362 /* detect loopback situation */ 1363 if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system), 1364 &(port->actor_system))) { 1365 slave_err(port->slave->bond->dev, port->slave->dev, "An illegal loopback occurred on slave\n" 1366 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n"); 1367 return; 1368 } 1369 __update_selected(lacpdu, port); 1370 __update_ntt(lacpdu, port); 1371 __record_pdu(lacpdu, port); 1372 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & LACP_STATE_LACP_TIMEOUT)); 1373 port->actor_oper_port_state &= ~LACP_STATE_EXPIRED; 1374 break; 1375 default: 1376 break; 1377 } 1378 } 1379 } 1380 1381 /** 1382 * ad_churn_machine - handle port churn's state machine 1383 * @port: the port we're looking at 1384 * 1385 */ 1386 static void ad_churn_machine(struct port *port) 1387 { 1388 if (port->sm_vars & AD_PORT_CHURNED) { 1389 port->sm_vars &= ~AD_PORT_CHURNED; 1390 port->sm_churn_actor_state = AD_CHURN_MONITOR; 1391 port->sm_churn_partner_state = AD_CHURN_MONITOR; 1392 port->sm_churn_actor_timer_counter = 1393 __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0); 1394 port->sm_churn_partner_timer_counter = 1395 __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0); 1396 return; 1397 } 1398 if (port->sm_churn_actor_timer_counter && 1399 !(--port->sm_churn_actor_timer_counter) && 1400 port->sm_churn_actor_state == AD_CHURN_MONITOR) { 1401 if (port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION) { 1402 port->sm_churn_actor_state = AD_NO_CHURN; 1403 } else { 1404 port->churn_actor_count++; 1405 port->sm_churn_actor_state = AD_CHURN; 1406 } 1407 } 1408 if (port->sm_churn_partner_timer_counter && 1409 !(--port->sm_churn_partner_timer_counter) && 1410 port->sm_churn_partner_state == AD_CHURN_MONITOR) { 1411 if (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) { 1412 port->sm_churn_partner_state = AD_NO_CHURN; 1413 } else { 1414 port->churn_partner_count++; 1415 port->sm_churn_partner_state = AD_CHURN; 1416 } 1417 } 1418 } 1419 1420 /** 1421 * ad_tx_machine - handle a port's tx state machine 1422 * @port: the port we're looking at 1423 */ 1424 static void ad_tx_machine(struct port *port) 1425 { 1426 /* check if tx timer expired, to verify that we do not send more than 1427 * 3 packets per second 1428 */ 1429 if (!port->sm_tx_timer_counter || !(--port->sm_tx_timer_counter)) { 1430 /* check if there is something to send */ 1431 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) { 1432 __update_lacpdu_from_port(port); 1433 1434 if (ad_lacpdu_send(port) >= 0) { 1435 slave_dbg(port->slave->bond->dev, 1436 port->slave->dev, 1437 "Sent LACPDU on port %d\n", 1438 port->actor_port_number); 1439 1440 /* mark ntt as false, so it will not be sent 1441 * again until demanded 1442 */ 1443 port->ntt = false; 1444 1445 /* restart tx timer(to verify that we will not 1446 * exceed AD_MAX_TX_IN_SECOND 1447 */ 1448 port->sm_tx_timer_counter = ad_ticks_per_sec / AD_MAX_TX_IN_SECOND; 1449 } 1450 } 1451 } 1452 } 1453 1454 /** 1455 * ad_periodic_machine - handle a port's periodic state machine 1456 * @port: the port we're looking at 1457 * 1458 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's. 1459 */ 1460 static void ad_periodic_machine(struct port *port) 1461 { 1462 periodic_states_t last_state; 1463 1464 /* keep current state machine state to compare later if it was changed */ 1465 last_state = port->sm_periodic_state; 1466 1467 /* check if port was reinitialized */ 1468 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) || 1469 (!(port->actor_oper_port_state & LACP_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & LACP_STATE_LACP_ACTIVITY))) { 1470 port->sm_periodic_state = AD_NO_PERIODIC; 1471 } 1472 /* check if state machine should change state */ 1473 else if (port->sm_periodic_timer_counter) { 1474 /* check if periodic state machine expired */ 1475 if (!(--port->sm_periodic_timer_counter)) { 1476 /* if expired then do tx */ 1477 port->sm_periodic_state = AD_PERIODIC_TX; 1478 } else { 1479 /* If not expired, check if there is some new timeout 1480 * parameter from the partner state 1481 */ 1482 switch (port->sm_periodic_state) { 1483 case AD_FAST_PERIODIC: 1484 if (!(port->partner_oper.port_state 1485 & LACP_STATE_LACP_TIMEOUT)) 1486 port->sm_periodic_state = AD_SLOW_PERIODIC; 1487 break; 1488 case AD_SLOW_PERIODIC: 1489 if ((port->partner_oper.port_state & LACP_STATE_LACP_TIMEOUT)) { 1490 port->sm_periodic_timer_counter = 0; 1491 port->sm_periodic_state = AD_PERIODIC_TX; 1492 } 1493 break; 1494 default: 1495 break; 1496 } 1497 } 1498 } else { 1499 switch (port->sm_periodic_state) { 1500 case AD_NO_PERIODIC: 1501 port->sm_periodic_state = AD_FAST_PERIODIC; 1502 break; 1503 case AD_PERIODIC_TX: 1504 if (!(port->partner_oper.port_state & 1505 LACP_STATE_LACP_TIMEOUT)) 1506 port->sm_periodic_state = AD_SLOW_PERIODIC; 1507 else 1508 port->sm_periodic_state = AD_FAST_PERIODIC; 1509 break; 1510 default: 1511 break; 1512 } 1513 } 1514 1515 /* check if the state machine was changed */ 1516 if (port->sm_periodic_state != last_state) { 1517 slave_dbg(port->slave->bond->dev, port->slave->dev, 1518 "Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n", 1519 port->actor_port_number, last_state, 1520 port->sm_periodic_state); 1521 switch (port->sm_periodic_state) { 1522 case AD_NO_PERIODIC: 1523 port->sm_periodic_timer_counter = 0; 1524 break; 1525 case AD_FAST_PERIODIC: 1526 /* decrement 1 tick we lost in the PERIODIC_TX cycle */ 1527 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1; 1528 break; 1529 case AD_SLOW_PERIODIC: 1530 /* decrement 1 tick we lost in the PERIODIC_TX cycle */ 1531 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1; 1532 break; 1533 case AD_PERIODIC_TX: 1534 port->ntt = true; 1535 break; 1536 default: 1537 break; 1538 } 1539 } 1540 } 1541 1542 /** 1543 * ad_port_selection_logic - select aggregation groups 1544 * @port: the port we're looking at 1545 * @update_slave_arr: Does slave array need update? 1546 * 1547 * Select aggregation groups, and assign each port for it's aggregetor. The 1548 * selection logic is called in the inititalization (after all the handshkes), 1549 * and after every lacpdu receive (if selected is off). 1550 */ 1551 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr) 1552 { 1553 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator; 1554 struct port *last_port = NULL, *curr_port; 1555 struct list_head *iter; 1556 struct bonding *bond; 1557 struct slave *slave; 1558 int found = 0; 1559 1560 /* if the port is already Selected, do nothing */ 1561 if (port->sm_vars & AD_PORT_SELECTED) 1562 return; 1563 1564 bond = __get_bond_by_port(port); 1565 1566 /* if the port is connected to other aggregator, detach it */ 1567 if (port->aggregator) { 1568 /* detach the port from its former aggregator */ 1569 temp_aggregator = port->aggregator; 1570 for (curr_port = temp_aggregator->lag_ports; curr_port; 1571 last_port = curr_port, 1572 curr_port = curr_port->next_port_in_aggregator) { 1573 if (curr_port == port) { 1574 temp_aggregator->num_of_ports--; 1575 /* if it is the first port attached to the 1576 * aggregator 1577 */ 1578 if (!last_port) { 1579 temp_aggregator->lag_ports = 1580 port->next_port_in_aggregator; 1581 } else { 1582 /* not the first port attached to the 1583 * aggregator 1584 */ 1585 last_port->next_port_in_aggregator = 1586 port->next_port_in_aggregator; 1587 } 1588 1589 /* clear the port's relations to this 1590 * aggregator 1591 */ 1592 port->aggregator = NULL; 1593 port->next_port_in_aggregator = NULL; 1594 port->actor_port_aggregator_identifier = 0; 1595 1596 slave_dbg(bond->dev, port->slave->dev, "Port %d left LAG %d\n", 1597 port->actor_port_number, 1598 temp_aggregator->aggregator_identifier); 1599 /* if the aggregator is empty, clear its 1600 * parameters, and set it ready to be attached 1601 */ 1602 if (!temp_aggregator->lag_ports) 1603 ad_clear_agg(temp_aggregator); 1604 break; 1605 } 1606 } 1607 if (!curr_port) { 1608 /* meaning: the port was related to an aggregator 1609 * but was not on the aggregator port list 1610 */ 1611 net_warn_ratelimited("%s: (slave %s): Warning: Port %d was related to aggregator %d but was not on its port list\n", 1612 port->slave->bond->dev->name, 1613 port->slave->dev->name, 1614 port->actor_port_number, 1615 port->aggregator->aggregator_identifier); 1616 } 1617 } 1618 /* search on all aggregators for a suitable aggregator for this port */ 1619 bond_for_each_slave(bond, slave, iter) { 1620 aggregator = &(SLAVE_AD_INFO(slave)->aggregator); 1621 1622 /* keep a free aggregator for later use(if needed) */ 1623 if (!aggregator->lag_ports) { 1624 if (!free_aggregator) 1625 free_aggregator = aggregator; 1626 continue; 1627 } 1628 /* check if current aggregator suits us */ 1629 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */ 1630 MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) && 1631 (aggregator->partner_system_priority == port->partner_oper.system_priority) && 1632 (aggregator->partner_oper_aggregator_key == port->partner_oper.key) 1633 ) && 1634 ((__agg_has_partner(aggregator) && /* partner answers */ 1635 !aggregator->is_individual) /* but is not individual OR */ 1636 ) 1637 ) { 1638 /* attach to the founded aggregator */ 1639 port->aggregator = aggregator; 1640 port->actor_port_aggregator_identifier = 1641 port->aggregator->aggregator_identifier; 1642 port->next_port_in_aggregator = aggregator->lag_ports; 1643 port->aggregator->num_of_ports++; 1644 aggregator->lag_ports = port; 1645 slave_dbg(bond->dev, slave->dev, "Port %d joined LAG %d (existing LAG)\n", 1646 port->actor_port_number, 1647 port->aggregator->aggregator_identifier); 1648 1649 /* mark this port as selected */ 1650 port->sm_vars |= AD_PORT_SELECTED; 1651 found = 1; 1652 break; 1653 } 1654 } 1655 1656 /* the port couldn't find an aggregator - attach it to a new 1657 * aggregator 1658 */ 1659 if (!found) { 1660 if (free_aggregator) { 1661 /* assign port a new aggregator */ 1662 port->aggregator = free_aggregator; 1663 port->actor_port_aggregator_identifier = 1664 port->aggregator->aggregator_identifier; 1665 1666 /* update the new aggregator's parameters 1667 * if port was responsed from the end-user 1668 */ 1669 if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS) 1670 /* if port is full duplex */ 1671 port->aggregator->is_individual = false; 1672 else 1673 port->aggregator->is_individual = true; 1674 1675 port->aggregator->actor_admin_aggregator_key = 1676 port->actor_admin_port_key; 1677 port->aggregator->actor_oper_aggregator_key = 1678 port->actor_oper_port_key; 1679 port->aggregator->partner_system = 1680 port->partner_oper.system; 1681 port->aggregator->partner_system_priority = 1682 port->partner_oper.system_priority; 1683 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key; 1684 port->aggregator->receive_state = 1; 1685 port->aggregator->transmit_state = 1; 1686 port->aggregator->lag_ports = port; 1687 port->aggregator->num_of_ports++; 1688 1689 /* mark this port as selected */ 1690 port->sm_vars |= AD_PORT_SELECTED; 1691 1692 slave_dbg(bond->dev, port->slave->dev, "Port %d joined LAG %d (new LAG)\n", 1693 port->actor_port_number, 1694 port->aggregator->aggregator_identifier); 1695 } else { 1696 slave_err(bond->dev, port->slave->dev, 1697 "Port %d did not find a suitable aggregator\n", 1698 port->actor_port_number); 1699 return; 1700 } 1701 } 1702 /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE 1703 * in all aggregator's ports, else set ready=FALSE in all 1704 * aggregator's ports 1705 */ 1706 __set_agg_ports_ready(port->aggregator, 1707 __agg_ports_are_ready(port->aggregator)); 1708 1709 aggregator = __get_first_agg(port); 1710 ad_agg_selection_logic(aggregator, update_slave_arr); 1711 1712 if (!port->aggregator->is_active) 1713 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION; 1714 } 1715 1716 /* Decide if "agg" is a better choice for the new active aggregator that 1717 * the current best, according to the ad_select policy. 1718 */ 1719 static struct aggregator *ad_agg_selection_test(struct aggregator *best, 1720 struct aggregator *curr) 1721 { 1722 /* 0. If no best, select current. 1723 * 1724 * 1. If the current agg is not individual, and the best is 1725 * individual, select current. 1726 * 1727 * 2. If current agg is individual and the best is not, keep best. 1728 * 1729 * 3. Therefore, current and best are both individual or both not 1730 * individual, so: 1731 * 1732 * 3a. If current agg partner replied, and best agg partner did not, 1733 * select current. 1734 * 1735 * 3b. If current agg partner did not reply and best agg partner 1736 * did reply, keep best. 1737 * 1738 * 4. Therefore, current and best both have partner replies or 1739 * both do not, so perform selection policy: 1740 * 1741 * BOND_AD_PRIO: Select by total priority of ports. If priority 1742 * is equal, select by count. 1743 * 1744 * BOND_AD_COUNT: Select by count of ports. If count is equal, 1745 * select by bandwidth. 1746 * 1747 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth. 1748 */ 1749 if (!best) 1750 return curr; 1751 1752 if (!curr->is_individual && best->is_individual) 1753 return curr; 1754 1755 if (curr->is_individual && !best->is_individual) 1756 return best; 1757 1758 if (__agg_has_partner(curr) && !__agg_has_partner(best)) 1759 return curr; 1760 1761 if (!__agg_has_partner(curr) && __agg_has_partner(best)) 1762 return best; 1763 1764 switch (__get_agg_selection_mode(curr->lag_ports)) { 1765 case BOND_AD_PRIO: 1766 if (__agg_ports_priority(curr) > __agg_ports_priority(best)) 1767 return curr; 1768 1769 if (__agg_ports_priority(curr) < __agg_ports_priority(best)) 1770 return best; 1771 1772 fallthrough; 1773 case BOND_AD_COUNT: 1774 if (__agg_active_ports(curr) > __agg_active_ports(best)) 1775 return curr; 1776 1777 if (__agg_active_ports(curr) < __agg_active_ports(best)) 1778 return best; 1779 1780 fallthrough; 1781 case BOND_AD_STABLE: 1782 case BOND_AD_BANDWIDTH: 1783 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best)) 1784 return curr; 1785 1786 break; 1787 1788 default: 1789 net_warn_ratelimited("%s: (slave %s): Impossible agg select mode %d\n", 1790 curr->slave->bond->dev->name, 1791 curr->slave->dev->name, 1792 __get_agg_selection_mode(curr->lag_ports)); 1793 break; 1794 } 1795 1796 return best; 1797 } 1798 1799 static int agg_device_up(const struct aggregator *agg) 1800 { 1801 struct port *port = agg->lag_ports; 1802 1803 if (!port) 1804 return 0; 1805 1806 for (port = agg->lag_ports; port; 1807 port = port->next_port_in_aggregator) { 1808 if (netif_running(port->slave->dev) && 1809 netif_carrier_ok(port->slave->dev)) 1810 return 1; 1811 } 1812 1813 return 0; 1814 } 1815 1816 /** 1817 * ad_agg_selection_logic - select an aggregation group for a team 1818 * @agg: the aggregator we're looking at 1819 * @update_slave_arr: Does slave array need update? 1820 * 1821 * It is assumed that only one aggregator may be selected for a team. 1822 * 1823 * The logic of this function is to select the aggregator according to 1824 * the ad_select policy: 1825 * 1826 * BOND_AD_STABLE: select the aggregator with the most ports attached to 1827 * it, and to reselect the active aggregator only if the previous 1828 * aggregator has no more ports related to it. 1829 * 1830 * BOND_AD_BANDWIDTH: select the aggregator with the highest total 1831 * bandwidth, and reselect whenever a link state change takes place or the 1832 * set of slaves in the bond changes. 1833 * 1834 * BOND_AD_COUNT: select the aggregator with largest number of ports 1835 * (slaves), and reselect whenever a link state change takes place or the 1836 * set of slaves in the bond changes. 1837 * 1838 * BOND_AD_PRIO: select the aggregator with highest total priority of ports 1839 * (slaves), and reselect whenever a link state change takes place or the 1840 * set of slaves in the bond changes. 1841 * 1842 * FIXME: this function MUST be called with the first agg in the bond, or 1843 * __get_active_agg() won't work correctly. This function should be better 1844 * called with the bond itself, and retrieve the first agg from it. 1845 */ 1846 static void ad_agg_selection_logic(struct aggregator *agg, 1847 bool *update_slave_arr) 1848 { 1849 struct aggregator *best, *active, *origin; 1850 struct bonding *bond = agg->slave->bond; 1851 struct list_head *iter; 1852 struct slave *slave; 1853 struct port *port; 1854 1855 rcu_read_lock(); 1856 origin = agg; 1857 active = __get_active_agg(agg); 1858 best = (active && agg_device_up(active)) ? active : NULL; 1859 1860 bond_for_each_slave_rcu(bond, slave, iter) { 1861 agg = &(SLAVE_AD_INFO(slave)->aggregator); 1862 1863 agg->is_active = 0; 1864 1865 if (__agg_active_ports(agg) && agg_device_up(agg)) 1866 best = ad_agg_selection_test(best, agg); 1867 } 1868 1869 if (best && 1870 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) { 1871 /* For the STABLE policy, don't replace the old active 1872 * aggregator if it's still active (it has an answering 1873 * partner) or if both the best and active don't have an 1874 * answering partner. 1875 */ 1876 if (active && active->lag_ports && 1877 __agg_active_ports(active) && 1878 (__agg_has_partner(active) || 1879 (!__agg_has_partner(active) && 1880 !__agg_has_partner(best)))) { 1881 if (!(!active->actor_oper_aggregator_key && 1882 best->actor_oper_aggregator_key)) { 1883 best = NULL; 1884 active->is_active = 1; 1885 } 1886 } 1887 } 1888 1889 if (best && (best == active)) { 1890 best = NULL; 1891 active->is_active = 1; 1892 } 1893 1894 /* if there is new best aggregator, activate it */ 1895 if (best) { 1896 netdev_dbg(bond->dev, "(slave %s): best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n", 1897 best->slave ? best->slave->dev->name : "NULL", 1898 best->aggregator_identifier, best->num_of_ports, 1899 best->actor_oper_aggregator_key, 1900 best->partner_oper_aggregator_key, 1901 best->is_individual, best->is_active); 1902 netdev_dbg(bond->dev, "(slave %s): best ports %p slave %p\n", 1903 best->slave ? best->slave->dev->name : "NULL", 1904 best->lag_ports, best->slave); 1905 1906 bond_for_each_slave_rcu(bond, slave, iter) { 1907 agg = &(SLAVE_AD_INFO(slave)->aggregator); 1908 1909 slave_dbg(bond->dev, slave->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n", 1910 agg->aggregator_identifier, agg->num_of_ports, 1911 agg->actor_oper_aggregator_key, 1912 agg->partner_oper_aggregator_key, 1913 agg->is_individual, agg->is_active); 1914 } 1915 1916 /* check if any partner replies */ 1917 if (best->is_individual) 1918 net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n", 1919 bond->dev->name); 1920 1921 best->is_active = 1; 1922 netdev_dbg(bond->dev, "(slave %s): LAG %d chosen as the active LAG\n", 1923 best->slave ? best->slave->dev->name : "NULL", 1924 best->aggregator_identifier); 1925 netdev_dbg(bond->dev, "(slave %s): Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n", 1926 best->slave ? best->slave->dev->name : "NULL", 1927 best->aggregator_identifier, best->num_of_ports, 1928 best->actor_oper_aggregator_key, 1929 best->partner_oper_aggregator_key, 1930 best->is_individual, best->is_active); 1931 1932 /* disable the ports that were related to the former 1933 * active_aggregator 1934 */ 1935 if (active) { 1936 for (port = active->lag_ports; port; 1937 port = port->next_port_in_aggregator) { 1938 __disable_port(port); 1939 } 1940 } 1941 /* Slave array needs update. */ 1942 *update_slave_arr = true; 1943 } 1944 1945 /* if the selected aggregator is of join individuals 1946 * (partner_system is NULL), enable their ports 1947 */ 1948 active = __get_active_agg(origin); 1949 1950 if (active) { 1951 if (!__agg_has_partner(active)) { 1952 for (port = active->lag_ports; port; 1953 port = port->next_port_in_aggregator) { 1954 __enable_port(port); 1955 } 1956 *update_slave_arr = true; 1957 } 1958 } 1959 1960 rcu_read_unlock(); 1961 1962 bond_3ad_set_carrier(bond); 1963 } 1964 1965 /** 1966 * ad_clear_agg - clear a given aggregator's parameters 1967 * @aggregator: the aggregator we're looking at 1968 */ 1969 static void ad_clear_agg(struct aggregator *aggregator) 1970 { 1971 if (aggregator) { 1972 aggregator->is_individual = false; 1973 aggregator->actor_admin_aggregator_key = 0; 1974 aggregator->actor_oper_aggregator_key = 0; 1975 eth_zero_addr(aggregator->partner_system.mac_addr_value); 1976 aggregator->partner_system_priority = 0; 1977 aggregator->partner_oper_aggregator_key = 0; 1978 aggregator->receive_state = 0; 1979 aggregator->transmit_state = 0; 1980 aggregator->lag_ports = NULL; 1981 aggregator->is_active = 0; 1982 aggregator->num_of_ports = 0; 1983 pr_debug("%s: LAG %d was cleared\n", 1984 aggregator->slave ? 1985 aggregator->slave->dev->name : "NULL", 1986 aggregator->aggregator_identifier); 1987 } 1988 } 1989 1990 /** 1991 * ad_initialize_agg - initialize a given aggregator's parameters 1992 * @aggregator: the aggregator we're looking at 1993 */ 1994 static void ad_initialize_agg(struct aggregator *aggregator) 1995 { 1996 if (aggregator) { 1997 ad_clear_agg(aggregator); 1998 1999 eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value); 2000 aggregator->aggregator_identifier = 0; 2001 aggregator->slave = NULL; 2002 } 2003 } 2004 2005 /** 2006 * ad_initialize_port - initialize a given port's parameters 2007 * @port: the port we're looking at 2008 * @bond_params: bond parameters we will use 2009 */ 2010 static void ad_initialize_port(struct port *port, const struct bond_params *bond_params) 2011 { 2012 static const struct port_params tmpl = { 2013 .system_priority = 0xffff, 2014 .key = 1, 2015 .port_number = 1, 2016 .port_priority = 0xff, 2017 .port_state = 0, 2018 }; 2019 static const struct lacpdu lacpdu = { 2020 .subtype = 0x01, 2021 .version_number = 0x01, 2022 .tlv_type_actor_info = 0x01, 2023 .actor_information_length = 0x14, 2024 .tlv_type_partner_info = 0x02, 2025 .partner_information_length = 0x14, 2026 .tlv_type_collector_info = 0x03, 2027 .collector_information_length = 0x10, 2028 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY), 2029 }; 2030 2031 if (port) { 2032 port->actor_port_priority = 0xff; 2033 port->actor_port_aggregator_identifier = 0; 2034 port->ntt = false; 2035 port->actor_admin_port_state = LACP_STATE_AGGREGATION; 2036 port->actor_oper_port_state = LACP_STATE_AGGREGATION; 2037 if (bond_params->lacp_active) { 2038 port->actor_admin_port_state |= LACP_STATE_LACP_ACTIVITY; 2039 port->actor_oper_port_state |= LACP_STATE_LACP_ACTIVITY; 2040 } 2041 2042 if (bond_params->lacp_fast) 2043 port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT; 2044 2045 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl)); 2046 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl)); 2047 2048 port->is_enabled = true; 2049 /* private parameters */ 2050 port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED; 2051 port->sm_rx_state = 0; 2052 port->sm_rx_timer_counter = 0; 2053 port->sm_periodic_state = 0; 2054 port->sm_periodic_timer_counter = 0; 2055 port->sm_mux_state = 0; 2056 port->sm_mux_timer_counter = 0; 2057 port->sm_tx_state = 0; 2058 port->aggregator = NULL; 2059 port->next_port_in_aggregator = NULL; 2060 port->transaction_id = 0; 2061 2062 port->sm_churn_actor_timer_counter = 0; 2063 port->sm_churn_actor_state = 0; 2064 port->churn_actor_count = 0; 2065 port->sm_churn_partner_timer_counter = 0; 2066 port->sm_churn_partner_state = 0; 2067 port->churn_partner_count = 0; 2068 2069 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu)); 2070 } 2071 } 2072 2073 /** 2074 * ad_enable_collecting - enable a port's receive 2075 * @port: the port we're looking at 2076 * 2077 * Enable @port if it's in an active aggregator 2078 */ 2079 static void ad_enable_collecting(struct port *port) 2080 { 2081 if (port->aggregator->is_active) { 2082 struct slave *slave = port->slave; 2083 2084 slave_dbg(slave->bond->dev, slave->dev, 2085 "Enabling collecting on port %d (LAG %d)\n", 2086 port->actor_port_number, 2087 port->aggregator->aggregator_identifier); 2088 __enable_collecting_port(port); 2089 } 2090 } 2091 2092 /** 2093 * ad_disable_distributing - disable a port's transmit 2094 * @port: the port we're looking at 2095 * @update_slave_arr: Does slave array need update? 2096 */ 2097 static void ad_disable_distributing(struct port *port, bool *update_slave_arr) 2098 { 2099 if (port->aggregator && __agg_has_partner(port->aggregator)) { 2100 slave_dbg(port->slave->bond->dev, port->slave->dev, 2101 "Disabling distributing on port %d (LAG %d)\n", 2102 port->actor_port_number, 2103 port->aggregator->aggregator_identifier); 2104 __disable_distributing_port(port); 2105 /* Slave array needs an update */ 2106 *update_slave_arr = true; 2107 } 2108 } 2109 2110 /** 2111 * ad_enable_collecting_distributing - enable a port's transmit/receive 2112 * @port: the port we're looking at 2113 * @update_slave_arr: Does slave array need update? 2114 * 2115 * Enable @port if it's in an active aggregator 2116 */ 2117 static void ad_enable_collecting_distributing(struct port *port, 2118 bool *update_slave_arr) 2119 { 2120 if (port->aggregator->is_active) { 2121 slave_dbg(port->slave->bond->dev, port->slave->dev, 2122 "Enabling port %d (LAG %d)\n", 2123 port->actor_port_number, 2124 port->aggregator->aggregator_identifier); 2125 __enable_port(port); 2126 /* Slave array needs update */ 2127 *update_slave_arr = true; 2128 /* Should notify peers if possible */ 2129 ad_cond_set_peer_notif(port); 2130 } 2131 } 2132 2133 /** 2134 * ad_disable_collecting_distributing - disable a port's transmit/receive 2135 * @port: the port we're looking at 2136 * @update_slave_arr: Does slave array need update? 2137 */ 2138 static void ad_disable_collecting_distributing(struct port *port, 2139 bool *update_slave_arr) 2140 { 2141 if (port->aggregator && __agg_has_partner(port->aggregator)) { 2142 slave_dbg(port->slave->bond->dev, port->slave->dev, 2143 "Disabling port %d (LAG %d)\n", 2144 port->actor_port_number, 2145 port->aggregator->aggregator_identifier); 2146 __disable_port(port); 2147 /* Slave array needs an update */ 2148 *update_slave_arr = true; 2149 } 2150 } 2151 2152 /** 2153 * ad_marker_info_received - handle receive of a Marker information frame 2154 * @marker_info: Marker info received 2155 * @port: the port we're looking at 2156 */ 2157 static void ad_marker_info_received(struct bond_marker *marker_info, 2158 struct port *port) 2159 { 2160 struct bond_marker marker; 2161 2162 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_rx); 2163 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_rx); 2164 2165 /* copy the received marker data to the response marker */ 2166 memcpy(&marker, marker_info, sizeof(struct bond_marker)); 2167 /* change the marker subtype to marker response */ 2168 marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE; 2169 2170 /* send the marker response */ 2171 if (ad_marker_send(port, &marker) >= 0) 2172 slave_dbg(port->slave->bond->dev, port->slave->dev, 2173 "Sent Marker Response on port %d\n", 2174 port->actor_port_number); 2175 } 2176 2177 /** 2178 * ad_marker_response_received - handle receive of a marker response frame 2179 * @marker: marker PDU received 2180 * @port: the port we're looking at 2181 * 2182 * This function does nothing since we decided not to implement send and handle 2183 * response for marker PDU's, in this stage, but only to respond to marker 2184 * information. 2185 */ 2186 static void ad_marker_response_received(struct bond_marker *marker, 2187 struct port *port) 2188 { 2189 atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_resp_rx); 2190 atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_resp_rx); 2191 2192 /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */ 2193 } 2194 2195 /* ========= AD exported functions to the main bonding code ========= */ 2196 2197 /* Check aggregators status in team every T seconds */ 2198 #define AD_AGGREGATOR_SELECTION_TIMER 8 2199 2200 /** 2201 * bond_3ad_initiate_agg_selection - initate aggregator selection 2202 * @bond: bonding struct 2203 * @timeout: timeout value to set 2204 * 2205 * Set the aggregation selection timer, to initiate an agg selection in 2206 * the very near future. Called during first initialization, and during 2207 * any down to up transitions of the bond. 2208 */ 2209 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout) 2210 { 2211 atomic_set(&BOND_AD_INFO(bond).agg_select_timer, timeout); 2212 } 2213 2214 /** 2215 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures 2216 * @bond: bonding struct to work on 2217 * 2218 * Can be called only after the mac address of the bond is set. 2219 */ 2220 void bond_3ad_initialize(struct bonding *bond) 2221 { 2222 BOND_AD_INFO(bond).aggregator_identifier = 0; 2223 BOND_AD_INFO(bond).system.sys_priority = 2224 bond->params.ad_actor_sys_prio; 2225 if (is_zero_ether_addr(bond->params.ad_actor_system)) 2226 BOND_AD_INFO(bond).system.sys_mac_addr = 2227 *((struct mac_addr *)bond->dev->dev_addr); 2228 else 2229 BOND_AD_INFO(bond).system.sys_mac_addr = 2230 *((struct mac_addr *)bond->params.ad_actor_system); 2231 2232 bond_3ad_initiate_agg_selection(bond, 2233 AD_AGGREGATOR_SELECTION_TIMER * 2234 ad_ticks_per_sec); 2235 } 2236 2237 /** 2238 * bond_3ad_bind_slave - initialize a slave's port 2239 * @slave: slave struct to work on 2240 * 2241 * Returns: 0 on success 2242 * < 0 on error 2243 */ 2244 void bond_3ad_bind_slave(struct slave *slave) 2245 { 2246 struct bonding *bond = bond_get_bond_by_slave(slave); 2247 struct port *port; 2248 struct aggregator *aggregator; 2249 2250 /* check that the slave has not been initialized yet. */ 2251 if (SLAVE_AD_INFO(slave)->port.slave != slave) { 2252 2253 /* port initialization */ 2254 port = &(SLAVE_AD_INFO(slave)->port); 2255 2256 ad_initialize_port(port, &bond->params); 2257 2258 /* Port priority is initialized. Update it to slave's ad info */ 2259 SLAVE_AD_INFO(slave)->port_priority = port->actor_port_priority; 2260 2261 port->slave = slave; 2262 port->actor_port_number = SLAVE_AD_INFO(slave)->id; 2263 /* key is determined according to the link speed, duplex and 2264 * user key 2265 */ 2266 port->actor_admin_port_key = bond->params.ad_user_port_key << 6; 2267 ad_update_actor_keys(port, false); 2268 /* actor system is the bond's system */ 2269 __ad_actor_update_port(port); 2270 /* tx timer(to verify that no more than MAX_TX_IN_SECOND 2271 * lacpdu's are sent in one second) 2272 */ 2273 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND; 2274 2275 __disable_port(port); 2276 2277 /* aggregator initialization */ 2278 aggregator = &(SLAVE_AD_INFO(slave)->aggregator); 2279 2280 ad_initialize_agg(aggregator); 2281 2282 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr); 2283 aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier; 2284 aggregator->slave = slave; 2285 aggregator->is_active = 0; 2286 aggregator->num_of_ports = 0; 2287 } 2288 } 2289 2290 /** 2291 * bond_3ad_unbind_slave - deinitialize a slave's port 2292 * @slave: slave struct to work on 2293 * 2294 * Search for the aggregator that is related to this port, remove the 2295 * aggregator and assign another aggregator for other port related to it 2296 * (if any), and remove the port. 2297 */ 2298 void bond_3ad_unbind_slave(struct slave *slave) 2299 { 2300 struct port *port, *prev_port, *temp_port; 2301 struct aggregator *aggregator, *new_aggregator, *temp_aggregator; 2302 int select_new_active_agg = 0; 2303 struct bonding *bond = slave->bond; 2304 struct slave *slave_iter; 2305 struct list_head *iter; 2306 bool dummy_slave_update; /* Ignore this value as caller updates array */ 2307 2308 /* Sync against bond_3ad_state_machine_handler() */ 2309 spin_lock_bh(&bond->mode_lock); 2310 aggregator = &(SLAVE_AD_INFO(slave)->aggregator); 2311 port = &(SLAVE_AD_INFO(slave)->port); 2312 2313 /* if slave is null, the whole port is not initialized */ 2314 if (!port->slave) { 2315 slave_warn(bond->dev, slave->dev, "Trying to unbind an uninitialized port\n"); 2316 goto out; 2317 } 2318 2319 slave_dbg(bond->dev, slave->dev, "Unbinding Link Aggregation Group %d\n", 2320 aggregator->aggregator_identifier); 2321 2322 /* Tell the partner that this port is not suitable for aggregation */ 2323 port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION; 2324 port->actor_oper_port_state &= ~LACP_STATE_COLLECTING; 2325 port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING; 2326 port->actor_oper_port_state &= ~LACP_STATE_AGGREGATION; 2327 __update_lacpdu_from_port(port); 2328 ad_lacpdu_send(port); 2329 2330 /* check if this aggregator is occupied */ 2331 if (aggregator->lag_ports) { 2332 /* check if there are other ports related to this aggregator 2333 * except the port related to this slave(thats ensure us that 2334 * there is a reason to search for new aggregator, and that we 2335 * will find one 2336 */ 2337 if ((aggregator->lag_ports != port) || 2338 (aggregator->lag_ports->next_port_in_aggregator)) { 2339 /* find new aggregator for the related port(s) */ 2340 bond_for_each_slave(bond, slave_iter, iter) { 2341 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator); 2342 /* if the new aggregator is empty, or it is 2343 * connected to our port only 2344 */ 2345 if (!new_aggregator->lag_ports || 2346 ((new_aggregator->lag_ports == port) && 2347 !new_aggregator->lag_ports->next_port_in_aggregator)) 2348 break; 2349 } 2350 if (!slave_iter) 2351 new_aggregator = NULL; 2352 2353 /* if new aggregator found, copy the aggregator's 2354 * parameters and connect the related lag_ports to the 2355 * new aggregator 2356 */ 2357 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) { 2358 slave_dbg(bond->dev, slave->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n", 2359 aggregator->aggregator_identifier, 2360 new_aggregator->aggregator_identifier); 2361 2362 if ((new_aggregator->lag_ports == port) && 2363 new_aggregator->is_active) { 2364 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n"); 2365 select_new_active_agg = 1; 2366 } 2367 2368 new_aggregator->is_individual = aggregator->is_individual; 2369 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key; 2370 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key; 2371 new_aggregator->partner_system = aggregator->partner_system; 2372 new_aggregator->partner_system_priority = aggregator->partner_system_priority; 2373 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key; 2374 new_aggregator->receive_state = aggregator->receive_state; 2375 new_aggregator->transmit_state = aggregator->transmit_state; 2376 new_aggregator->lag_ports = aggregator->lag_ports; 2377 new_aggregator->is_active = aggregator->is_active; 2378 new_aggregator->num_of_ports = aggregator->num_of_ports; 2379 2380 /* update the information that is written on 2381 * the ports about the aggregator 2382 */ 2383 for (temp_port = aggregator->lag_ports; temp_port; 2384 temp_port = temp_port->next_port_in_aggregator) { 2385 temp_port->aggregator = new_aggregator; 2386 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier; 2387 } 2388 2389 ad_clear_agg(aggregator); 2390 2391 if (select_new_active_agg) 2392 ad_agg_selection_logic(__get_first_agg(port), 2393 &dummy_slave_update); 2394 } else { 2395 slave_warn(bond->dev, slave->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n"); 2396 } 2397 } else { 2398 /* in case that the only port related to this 2399 * aggregator is the one we want to remove 2400 */ 2401 select_new_active_agg = aggregator->is_active; 2402 ad_clear_agg(aggregator); 2403 if (select_new_active_agg) { 2404 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n"); 2405 /* select new active aggregator */ 2406 temp_aggregator = __get_first_agg(port); 2407 if (temp_aggregator) 2408 ad_agg_selection_logic(temp_aggregator, 2409 &dummy_slave_update); 2410 } 2411 } 2412 } 2413 2414 slave_dbg(bond->dev, slave->dev, "Unbinding port %d\n", port->actor_port_number); 2415 2416 /* find the aggregator that this port is connected to */ 2417 bond_for_each_slave(bond, slave_iter, iter) { 2418 temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator); 2419 prev_port = NULL; 2420 /* search the port in the aggregator's related ports */ 2421 for (temp_port = temp_aggregator->lag_ports; temp_port; 2422 prev_port = temp_port, 2423 temp_port = temp_port->next_port_in_aggregator) { 2424 if (temp_port == port) { 2425 /* the aggregator found - detach the port from 2426 * this aggregator 2427 */ 2428 if (prev_port) 2429 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator; 2430 else 2431 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator; 2432 temp_aggregator->num_of_ports--; 2433 if (__agg_active_ports(temp_aggregator) == 0) { 2434 select_new_active_agg = temp_aggregator->is_active; 2435 if (temp_aggregator->num_of_ports == 0) 2436 ad_clear_agg(temp_aggregator); 2437 if (select_new_active_agg) { 2438 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n"); 2439 /* select new active aggregator */ 2440 ad_agg_selection_logic(__get_first_agg(port), 2441 &dummy_slave_update); 2442 } 2443 } 2444 break; 2445 } 2446 } 2447 } 2448 port->slave = NULL; 2449 2450 out: 2451 spin_unlock_bh(&bond->mode_lock); 2452 } 2453 2454 /** 2455 * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports 2456 * @bond: bonding struct to work on 2457 * 2458 * If an ad_actor setting gets changed we need to update the individual port 2459 * settings so the bond device will use the new values when it gets upped. 2460 */ 2461 void bond_3ad_update_ad_actor_settings(struct bonding *bond) 2462 { 2463 struct list_head *iter; 2464 struct slave *slave; 2465 2466 ASSERT_RTNL(); 2467 2468 BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio; 2469 if (is_zero_ether_addr(bond->params.ad_actor_system)) 2470 BOND_AD_INFO(bond).system.sys_mac_addr = 2471 *((struct mac_addr *)bond->dev->dev_addr); 2472 else 2473 BOND_AD_INFO(bond).system.sys_mac_addr = 2474 *((struct mac_addr *)bond->params.ad_actor_system); 2475 2476 spin_lock_bh(&bond->mode_lock); 2477 bond_for_each_slave(bond, slave, iter) { 2478 struct port *port = &(SLAVE_AD_INFO(slave))->port; 2479 2480 __ad_actor_update_port(port); 2481 port->ntt = true; 2482 } 2483 spin_unlock_bh(&bond->mode_lock); 2484 } 2485 2486 /** 2487 * bond_agg_timer_advance - advance agg_select_timer 2488 * @bond: bonding structure 2489 * 2490 * Return true when agg_select_timer reaches 0. 2491 */ 2492 static bool bond_agg_timer_advance(struct bonding *bond) 2493 { 2494 int val, nval; 2495 2496 while (1) { 2497 val = atomic_read(&BOND_AD_INFO(bond).agg_select_timer); 2498 if (!val) 2499 return false; 2500 nval = val - 1; 2501 if (atomic_cmpxchg(&BOND_AD_INFO(bond).agg_select_timer, 2502 val, nval) == val) 2503 break; 2504 } 2505 return nval == 0; 2506 } 2507 2508 /** 2509 * bond_3ad_state_machine_handler - handle state machines timeout 2510 * @work: work context to fetch bonding struct to work on from 2511 * 2512 * The state machine handling concept in this module is to check every tick 2513 * which state machine should operate any function. The execution order is 2514 * round robin, so when we have an interaction between state machines, the 2515 * reply of one to each other might be delayed until next tick. 2516 * 2517 * This function also complete the initialization when the agg_select_timer 2518 * times out, and it selects an aggregator for the ports that are yet not 2519 * related to any aggregator, and selects the active aggregator for a bond. 2520 */ 2521 void bond_3ad_state_machine_handler(struct work_struct *work) 2522 { 2523 struct bonding *bond = container_of(work, struct bonding, 2524 ad_work.work); 2525 struct aggregator *aggregator; 2526 struct list_head *iter; 2527 struct slave *slave; 2528 struct port *port; 2529 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER; 2530 bool update_slave_arr = false; 2531 2532 /* Lock to protect data accessed by all (e.g., port->sm_vars) and 2533 * against running with bond_3ad_unbind_slave. ad_rx_machine may run 2534 * concurrently due to incoming LACPDU as well. 2535 */ 2536 spin_lock_bh(&bond->mode_lock); 2537 rcu_read_lock(); 2538 2539 /* check if there are any slaves */ 2540 if (!bond_has_slaves(bond)) 2541 goto re_arm; 2542 2543 if (bond_agg_timer_advance(bond)) { 2544 slave = bond_first_slave_rcu(bond); 2545 port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL; 2546 2547 /* select the active aggregator for the bond */ 2548 if (port) { 2549 if (!port->slave) { 2550 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n", 2551 bond->dev->name); 2552 goto re_arm; 2553 } 2554 2555 aggregator = __get_first_agg(port); 2556 ad_agg_selection_logic(aggregator, &update_slave_arr); 2557 } 2558 bond_3ad_set_carrier(bond); 2559 } 2560 2561 /* for each port run the state machines */ 2562 bond_for_each_slave_rcu(bond, slave, iter) { 2563 port = &(SLAVE_AD_INFO(slave)->port); 2564 if (!port->slave) { 2565 net_warn_ratelimited("%s: Warning: Found an uninitialized port\n", 2566 bond->dev->name); 2567 goto re_arm; 2568 } 2569 2570 ad_rx_machine(NULL, port); 2571 ad_periodic_machine(port); 2572 ad_port_selection_logic(port, &update_slave_arr); 2573 ad_mux_machine(port, &update_slave_arr); 2574 ad_tx_machine(port); 2575 ad_churn_machine(port); 2576 2577 /* turn off the BEGIN bit, since we already handled it */ 2578 if (port->sm_vars & AD_PORT_BEGIN) 2579 port->sm_vars &= ~AD_PORT_BEGIN; 2580 } 2581 2582 re_arm: 2583 bond_for_each_slave_rcu(bond, slave, iter) { 2584 if (slave->should_notify) { 2585 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW; 2586 break; 2587 } 2588 } 2589 rcu_read_unlock(); 2590 spin_unlock_bh(&bond->mode_lock); 2591 2592 if (update_slave_arr) 2593 bond_slave_arr_work_rearm(bond, 0); 2594 2595 if (should_notify_rtnl && rtnl_trylock()) { 2596 bond_slave_state_notify(bond); 2597 rtnl_unlock(); 2598 } 2599 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks); 2600 } 2601 2602 /** 2603 * bond_3ad_rx_indication - handle a received frame 2604 * @lacpdu: received lacpdu 2605 * @slave: slave struct to work on 2606 * 2607 * It is assumed that frames that were sent on this NIC don't returned as new 2608 * received frames (loopback). Since only the payload is given to this 2609 * function, it check for loopback. 2610 */ 2611 static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave) 2612 { 2613 struct bonding *bond = slave->bond; 2614 int ret = RX_HANDLER_ANOTHER; 2615 struct bond_marker *marker; 2616 struct port *port; 2617 atomic64_t *stat; 2618 2619 port = &(SLAVE_AD_INFO(slave)->port); 2620 if (!port->slave) { 2621 net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n", 2622 slave->dev->name, slave->bond->dev->name); 2623 return ret; 2624 } 2625 2626 switch (lacpdu->subtype) { 2627 case AD_TYPE_LACPDU: 2628 ret = RX_HANDLER_CONSUMED; 2629 slave_dbg(slave->bond->dev, slave->dev, 2630 "Received LACPDU on port %d\n", 2631 port->actor_port_number); 2632 /* Protect against concurrent state machines */ 2633 spin_lock(&slave->bond->mode_lock); 2634 ad_rx_machine(lacpdu, port); 2635 spin_unlock(&slave->bond->mode_lock); 2636 break; 2637 case AD_TYPE_MARKER: 2638 ret = RX_HANDLER_CONSUMED; 2639 /* No need to convert fields to Little Endian since we 2640 * don't use the marker's fields. 2641 */ 2642 marker = (struct bond_marker *)lacpdu; 2643 switch (marker->tlv_type) { 2644 case AD_MARKER_INFORMATION_SUBTYPE: 2645 slave_dbg(slave->bond->dev, slave->dev, "Received Marker Information on port %d\n", 2646 port->actor_port_number); 2647 ad_marker_info_received(marker, port); 2648 break; 2649 case AD_MARKER_RESPONSE_SUBTYPE: 2650 slave_dbg(slave->bond->dev, slave->dev, "Received Marker Response on port %d\n", 2651 port->actor_port_number); 2652 ad_marker_response_received(marker, port); 2653 break; 2654 default: 2655 slave_dbg(slave->bond->dev, slave->dev, "Received an unknown Marker subtype on port %d\n", 2656 port->actor_port_number); 2657 stat = &SLAVE_AD_INFO(slave)->stats.marker_unknown_rx; 2658 atomic64_inc(stat); 2659 stat = &BOND_AD_INFO(bond).stats.marker_unknown_rx; 2660 atomic64_inc(stat); 2661 } 2662 break; 2663 default: 2664 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_unknown_rx); 2665 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_unknown_rx); 2666 } 2667 2668 return ret; 2669 } 2670 2671 /** 2672 * ad_update_actor_keys - Update the oper / admin keys for a port based on 2673 * its current speed and duplex settings. 2674 * 2675 * @port: the port we'are looking at 2676 * @reset: Boolean to just reset the speed and the duplex part of the key 2677 * 2678 * The logic to change the oper / admin keys is: 2679 * (a) A full duplex port can participate in LACP with partner. 2680 * (b) When the speed is changed, LACP need to be reinitiated. 2681 */ 2682 static void ad_update_actor_keys(struct port *port, bool reset) 2683 { 2684 u8 duplex = 0; 2685 u16 ospeed = 0, speed = 0; 2686 u16 old_oper_key = port->actor_oper_port_key; 2687 2688 port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS); 2689 if (!reset) { 2690 speed = __get_link_speed(port); 2691 ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1; 2692 duplex = __get_duplex(port); 2693 port->actor_admin_port_key |= (speed << 1) | duplex; 2694 } 2695 port->actor_oper_port_key = port->actor_admin_port_key; 2696 2697 if (old_oper_key != port->actor_oper_port_key) { 2698 /* Only 'duplex' port participates in LACP */ 2699 if (duplex) 2700 port->sm_vars |= AD_PORT_LACP_ENABLED; 2701 else 2702 port->sm_vars &= ~AD_PORT_LACP_ENABLED; 2703 2704 if (!reset) { 2705 if (!speed) { 2706 slave_err(port->slave->bond->dev, 2707 port->slave->dev, 2708 "speed changed to 0 on port %d\n", 2709 port->actor_port_number); 2710 } else if (duplex && ospeed != speed) { 2711 /* Speed change restarts LACP state-machine */ 2712 port->sm_vars |= AD_PORT_BEGIN; 2713 } 2714 } 2715 } 2716 } 2717 2718 /** 2719 * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex 2720 * change indication 2721 * 2722 * @slave: slave struct to work on 2723 * 2724 * Handle reselection of aggregator (if needed) for this port. 2725 */ 2726 void bond_3ad_adapter_speed_duplex_changed(struct slave *slave) 2727 { 2728 struct port *port; 2729 2730 port = &(SLAVE_AD_INFO(slave)->port); 2731 2732 /* if slave is null, the whole port is not initialized */ 2733 if (!port->slave) { 2734 slave_warn(slave->bond->dev, slave->dev, 2735 "speed/duplex changed for uninitialized port\n"); 2736 return; 2737 } 2738 2739 spin_lock_bh(&slave->bond->mode_lock); 2740 ad_update_actor_keys(port, false); 2741 spin_unlock_bh(&slave->bond->mode_lock); 2742 slave_dbg(slave->bond->dev, slave->dev, "Port %d changed speed/duplex\n", 2743 port->actor_port_number); 2744 } 2745 2746 /** 2747 * bond_3ad_handle_link_change - handle a slave's link status change indication 2748 * @slave: slave struct to work on 2749 * @link: whether the link is now up or down 2750 * 2751 * Handle reselection of aggregator (if needed) for this port. 2752 */ 2753 void bond_3ad_handle_link_change(struct slave *slave, char link) 2754 { 2755 struct aggregator *agg; 2756 struct port *port; 2757 bool dummy; 2758 2759 port = &(SLAVE_AD_INFO(slave)->port); 2760 2761 /* if slave is null, the whole port is not initialized */ 2762 if (!port->slave) { 2763 slave_warn(slave->bond->dev, slave->dev, "link status changed for uninitialized port\n"); 2764 return; 2765 } 2766 2767 spin_lock_bh(&slave->bond->mode_lock); 2768 /* on link down we are zeroing duplex and speed since 2769 * some of the adaptors(ce1000.lan) report full duplex/speed 2770 * instead of N/A(duplex) / 0(speed). 2771 * 2772 * on link up we are forcing recheck on the duplex and speed since 2773 * some of he adaptors(ce1000.lan) report. 2774 */ 2775 if (link == BOND_LINK_UP) { 2776 port->is_enabled = true; 2777 ad_update_actor_keys(port, false); 2778 } else { 2779 /* link has failed */ 2780 port->is_enabled = false; 2781 ad_update_actor_keys(port, true); 2782 } 2783 agg = __get_first_agg(port); 2784 ad_agg_selection_logic(agg, &dummy); 2785 2786 spin_unlock_bh(&slave->bond->mode_lock); 2787 2788 slave_dbg(slave->bond->dev, slave->dev, "Port %d changed link status to %s\n", 2789 port->actor_port_number, 2790 link == BOND_LINK_UP ? "UP" : "DOWN"); 2791 2792 /* RTNL is held and mode_lock is released so it's safe 2793 * to update slave_array here. 2794 */ 2795 bond_update_slave_arr(slave->bond, NULL); 2796 } 2797 2798 /** 2799 * bond_3ad_set_carrier - set link state for bonding master 2800 * @bond: bonding structure 2801 * 2802 * if we have an active aggregator, we're up, if not, we're down. 2803 * Presumes that we cannot have an active aggregator if there are 2804 * no slaves with link up. 2805 * 2806 * This behavior complies with IEEE 802.3 section 43.3.9. 2807 * 2808 * Called by bond_set_carrier(). Return zero if carrier state does not 2809 * change, nonzero if it does. 2810 */ 2811 int bond_3ad_set_carrier(struct bonding *bond) 2812 { 2813 struct aggregator *active; 2814 struct slave *first_slave; 2815 int ret = 1; 2816 2817 rcu_read_lock(); 2818 first_slave = bond_first_slave_rcu(bond); 2819 if (!first_slave) { 2820 ret = 0; 2821 goto out; 2822 } 2823 active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator)); 2824 if (active) { 2825 /* are enough slaves available to consider link up? */ 2826 if (__agg_active_ports(active) < bond->params.min_links) { 2827 if (netif_carrier_ok(bond->dev)) { 2828 netif_carrier_off(bond->dev); 2829 goto out; 2830 } 2831 } else if (!netif_carrier_ok(bond->dev)) { 2832 netif_carrier_on(bond->dev); 2833 goto out; 2834 } 2835 } else if (netif_carrier_ok(bond->dev)) { 2836 netif_carrier_off(bond->dev); 2837 } 2838 out: 2839 rcu_read_unlock(); 2840 return ret; 2841 } 2842 2843 /** 2844 * __bond_3ad_get_active_agg_info - get information of the active aggregator 2845 * @bond: bonding struct to work on 2846 * @ad_info: ad_info struct to fill with the bond's info 2847 * 2848 * Returns: 0 on success 2849 * < 0 on error 2850 */ 2851 int __bond_3ad_get_active_agg_info(struct bonding *bond, 2852 struct ad_info *ad_info) 2853 { 2854 struct aggregator *aggregator = NULL; 2855 struct list_head *iter; 2856 struct slave *slave; 2857 struct port *port; 2858 2859 bond_for_each_slave_rcu(bond, slave, iter) { 2860 port = &(SLAVE_AD_INFO(slave)->port); 2861 if (port->aggregator && port->aggregator->is_active) { 2862 aggregator = port->aggregator; 2863 break; 2864 } 2865 } 2866 2867 if (!aggregator) 2868 return -1; 2869 2870 ad_info->aggregator_id = aggregator->aggregator_identifier; 2871 ad_info->ports = __agg_active_ports(aggregator); 2872 ad_info->actor_key = aggregator->actor_oper_aggregator_key; 2873 ad_info->partner_key = aggregator->partner_oper_aggregator_key; 2874 ether_addr_copy(ad_info->partner_system, 2875 aggregator->partner_system.mac_addr_value); 2876 return 0; 2877 } 2878 2879 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info) 2880 { 2881 int ret; 2882 2883 rcu_read_lock(); 2884 ret = __bond_3ad_get_active_agg_info(bond, ad_info); 2885 rcu_read_unlock(); 2886 2887 return ret; 2888 } 2889 2890 int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond, 2891 struct slave *slave) 2892 { 2893 struct lacpdu *lacpdu, _lacpdu; 2894 2895 if (skb->protocol != PKT_TYPE_LACPDU) 2896 return RX_HANDLER_ANOTHER; 2897 2898 if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr)) 2899 return RX_HANDLER_ANOTHER; 2900 2901 lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu); 2902 if (!lacpdu) { 2903 atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_illegal_rx); 2904 atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_illegal_rx); 2905 return RX_HANDLER_ANOTHER; 2906 } 2907 2908 return bond_3ad_rx_indication(lacpdu, slave); 2909 } 2910 2911 /** 2912 * bond_3ad_update_lacp_rate - change the lacp rate 2913 * @bond: bonding struct 2914 * 2915 * When modify lacp_rate parameter via sysfs, 2916 * update actor_oper_port_state of each port. 2917 * 2918 * Hold bond->mode_lock, 2919 * so we can modify port->actor_oper_port_state, 2920 * no matter bond is up or down. 2921 */ 2922 void bond_3ad_update_lacp_rate(struct bonding *bond) 2923 { 2924 struct port *port = NULL; 2925 struct list_head *iter; 2926 struct slave *slave; 2927 int lacp_fast; 2928 2929 lacp_fast = bond->params.lacp_fast; 2930 spin_lock_bh(&bond->mode_lock); 2931 bond_for_each_slave(bond, slave, iter) { 2932 port = &(SLAVE_AD_INFO(slave)->port); 2933 if (lacp_fast) 2934 port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT; 2935 else 2936 port->actor_oper_port_state &= ~LACP_STATE_LACP_TIMEOUT; 2937 } 2938 spin_unlock_bh(&bond->mode_lock); 2939 } 2940 2941 /** 2942 * bond_3ad_update_lacp_active - change the lacp active 2943 * @bond: bonding struct 2944 * 2945 * Update actor_oper_port_state when lacp_active is modified. 2946 */ 2947 void bond_3ad_update_lacp_active(struct bonding *bond) 2948 { 2949 struct port *port = NULL; 2950 struct list_head *iter; 2951 struct slave *slave; 2952 int lacp_active; 2953 2954 lacp_active = bond->params.lacp_active; 2955 spin_lock_bh(&bond->mode_lock); 2956 bond_for_each_slave(bond, slave, iter) { 2957 port = &(SLAVE_AD_INFO(slave)->port); 2958 if (lacp_active) 2959 port->actor_oper_port_state |= LACP_STATE_LACP_ACTIVITY; 2960 else 2961 port->actor_oper_port_state &= ~LACP_STATE_LACP_ACTIVITY; 2962 } 2963 spin_unlock_bh(&bond->mode_lock); 2964 } 2965 2966 size_t bond_3ad_stats_size(void) 2967 { 2968 return nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_RX */ 2969 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_TX */ 2970 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_UNKNOWN_RX */ 2971 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_ILLEGAL_RX */ 2972 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RX */ 2973 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_TX */ 2974 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_RX */ 2975 nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_TX */ 2976 nla_total_size_64bit(sizeof(u64)); /* BOND_3AD_STAT_MARKER_UNKNOWN_RX */ 2977 } 2978 2979 int bond_3ad_stats_fill(struct sk_buff *skb, struct bond_3ad_stats *stats) 2980 { 2981 u64 val; 2982 2983 val = atomic64_read(&stats->lacpdu_rx); 2984 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_RX, val, 2985 BOND_3AD_STAT_PAD)) 2986 return -EMSGSIZE; 2987 val = atomic64_read(&stats->lacpdu_tx); 2988 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_TX, val, 2989 BOND_3AD_STAT_PAD)) 2990 return -EMSGSIZE; 2991 val = atomic64_read(&stats->lacpdu_unknown_rx); 2992 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_UNKNOWN_RX, val, 2993 BOND_3AD_STAT_PAD)) 2994 return -EMSGSIZE; 2995 val = atomic64_read(&stats->lacpdu_illegal_rx); 2996 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_ILLEGAL_RX, val, 2997 BOND_3AD_STAT_PAD)) 2998 return -EMSGSIZE; 2999 3000 val = atomic64_read(&stats->marker_rx); 3001 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RX, val, 3002 BOND_3AD_STAT_PAD)) 3003 return -EMSGSIZE; 3004 val = atomic64_read(&stats->marker_tx); 3005 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_TX, val, 3006 BOND_3AD_STAT_PAD)) 3007 return -EMSGSIZE; 3008 val = atomic64_read(&stats->marker_resp_rx); 3009 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_RX, val, 3010 BOND_3AD_STAT_PAD)) 3011 return -EMSGSIZE; 3012 val = atomic64_read(&stats->marker_resp_tx); 3013 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_TX, val, 3014 BOND_3AD_STAT_PAD)) 3015 return -EMSGSIZE; 3016 val = atomic64_read(&stats->marker_unknown_rx); 3017 if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_UNKNOWN_RX, val, 3018 BOND_3AD_STAT_PAD)) 3019 return -EMSGSIZE; 3020 3021 return 0; 3022 } 3023