1/* 2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License as published by the Free 6 * Software Foundation; either version 2 of the License, or (at your option) 7 * any later version. 8 * 9 * This program is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program; if not, write to the Free Software Foundation, Inc., 59 16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * The full GNU General Public License is included in this distribution in the 19 * file called LICENSE. 20 * 21 */ 22 23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 24 25#include <linux/skbuff.h> 26#include <linux/if_ether.h> 27#include <linux/netdevice.h> 28#include <linux/spinlock.h> 29#include <linux/ethtool.h> 30#include <linux/etherdevice.h> 31#include <linux/if_bonding.h> 32#include <linux/pkt_sched.h> 33#include <net/net_namespace.h> 34#include "bonding.h" 35#include "bond_3ad.h" 36 37// General definitions 38#define AD_SHORT_TIMEOUT 1 39#define AD_LONG_TIMEOUT 0 40#define AD_STANDBY 0x2 41#define AD_MAX_TX_IN_SECOND 3 42#define AD_COLLECTOR_MAX_DELAY 0 43 44// Timer definitions(43.4.4 in the 802.3ad standard) 45#define AD_FAST_PERIODIC_TIME 1 46#define AD_SLOW_PERIODIC_TIME 30 47#define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME) 48#define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME) 49#define AD_CHURN_DETECTION_TIME 60 50#define AD_AGGREGATE_WAIT_TIME 2 51 52// Port state definitions(43.4.2.2 in the 802.3ad standard) 53#define AD_STATE_LACP_ACTIVITY 0x1 54#define AD_STATE_LACP_TIMEOUT 0x2 55#define AD_STATE_AGGREGATION 0x4 56#define AD_STATE_SYNCHRONIZATION 0x8 57#define AD_STATE_COLLECTING 0x10 58#define AD_STATE_DISTRIBUTING 0x20 59#define AD_STATE_DEFAULTED 0x40 60#define AD_STATE_EXPIRED 0x80 61 62// Port Variables definitions used by the State Machines(43.4.7 in the 802.3ad standard) 63#define AD_PORT_BEGIN 0x1 64#define AD_PORT_LACP_ENABLED 0x2 65#define AD_PORT_ACTOR_CHURN 0x4 66#define AD_PORT_PARTNER_CHURN 0x8 67#define AD_PORT_READY 0x10 68#define AD_PORT_READY_N 0x20 69#define AD_PORT_MATCHED 0x40 70#define AD_PORT_STANDBY 0x80 71#define AD_PORT_SELECTED 0x100 72#define AD_PORT_MOVED 0x200 73 74// Port Key definitions 75// key is determined according to the link speed, duplex and 76// user key(which is yet not supported) 77// ------------------------------------------------------------ 78// Port key : | User key | Speed |Duplex| 79// ------------------------------------------------------------ 80// 16 6 1 0 81#define AD_DUPLEX_KEY_BITS 0x1 82#define AD_SPEED_KEY_BITS 0x3E 83#define AD_USER_KEY_BITS 0xFFC0 84 85//dalloun 86#define AD_LINK_SPEED_BITMASK_1MBPS 0x1 87#define AD_LINK_SPEED_BITMASK_10MBPS 0x2 88#define AD_LINK_SPEED_BITMASK_100MBPS 0x4 89#define AD_LINK_SPEED_BITMASK_1000MBPS 0x8 90#define AD_LINK_SPEED_BITMASK_10000MBPS 0x10 91//endalloun 92 93// compare MAC addresses 94#define MAC_ADDRESS_COMPARE(A, B) memcmp(A, B, ETH_ALEN) 95 96static struct mac_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}}; 97static u16 ad_ticks_per_sec; 98static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000; 99 100static const u8 lacpdu_mcast_addr[ETH_ALEN] = MULTICAST_LACPDU_ADDR; 101 102// ================= main 802.3ad protocol functions ================== 103static int ad_lacpdu_send(struct port *port); 104static int ad_marker_send(struct port *port, struct bond_marker *marker); 105static void ad_mux_machine(struct port *port); 106static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port); 107static void ad_tx_machine(struct port *port); 108static void ad_periodic_machine(struct port *port); 109static void ad_port_selection_logic(struct port *port); 110static void ad_agg_selection_logic(struct aggregator *aggregator); 111static void ad_clear_agg(struct aggregator *aggregator); 112static void ad_initialize_agg(struct aggregator *aggregator); 113static void ad_initialize_port(struct port *port, int lacp_fast); 114static void ad_enable_collecting_distributing(struct port *port); 115static void ad_disable_collecting_distributing(struct port *port); 116static void ad_marker_info_received(struct bond_marker *marker_info, struct port *port); 117static void ad_marker_response_received(struct bond_marker *marker, struct port *port); 118 119 120///////////////////////////////////////////////////////////////////////////////// 121// ================= api to bonding and kernel code ================== 122///////////////////////////////////////////////////////////////////////////////// 123 124/** 125 * __get_bond_by_port - get the port's bonding struct 126 * @port: the port we're looking at 127 * 128 * Return @port's bonding struct, or %NULL if it can't be found. 129 */ 130static inline struct bonding *__get_bond_by_port(struct port *port) 131{ 132 if (port->slave == NULL) { 133 return NULL; 134 } 135 136 return bond_get_bond_by_slave(port->slave); 137} 138 139/** 140 * __get_first_port - get the first port in the bond 141 * @bond: the bond we're looking at 142 * 143 * Return the port of the first slave in @bond, or %NULL if it can't be found. 144 */ 145static inline struct port *__get_first_port(struct bonding *bond) 146{ 147 if (bond->slave_cnt == 0) { 148 return NULL; 149 } 150 151 return &(SLAVE_AD_INFO(bond->first_slave).port); 152} 153 154/** 155 * __get_next_port - get the next port in the bond 156 * @port: the port we're looking at 157 * 158 * Return the port of the slave that is next in line of @port's slave in the 159 * bond, or %NULL if it can't be found. 160 */ 161static inline struct port *__get_next_port(struct port *port) 162{ 163 struct bonding *bond = __get_bond_by_port(port); 164 struct slave *slave = port->slave; 165 166 // If there's no bond for this port, or this is the last slave 167 if ((bond == NULL) || (slave->next == bond->first_slave)) { 168 return NULL; 169 } 170 171 return &(SLAVE_AD_INFO(slave->next).port); 172} 173 174/** 175 * __get_first_agg - get the first aggregator in the bond 176 * @bond: the bond we're looking at 177 * 178 * Return the aggregator of the first slave in @bond, or %NULL if it can't be 179 * found. 180 */ 181static inline struct aggregator *__get_first_agg(struct port *port) 182{ 183 struct bonding *bond = __get_bond_by_port(port); 184 185 // If there's no bond for this port, or bond has no slaves 186 if ((bond == NULL) || (bond->slave_cnt == 0)) { 187 return NULL; 188 } 189 190 return &(SLAVE_AD_INFO(bond->first_slave).aggregator); 191} 192 193/** 194 * __get_next_agg - get the next aggregator in the bond 195 * @aggregator: the aggregator we're looking at 196 * 197 * Return the aggregator of the slave that is next in line of @aggregator's 198 * slave in the bond, or %NULL if it can't be found. 199 */ 200static inline struct aggregator *__get_next_agg(struct aggregator *aggregator) 201{ 202 struct slave *slave = aggregator->slave; 203 struct bonding *bond = bond_get_bond_by_slave(slave); 204 205 // If there's no bond for this aggregator, or this is the last slave 206 if ((bond == NULL) || (slave->next == bond->first_slave)) { 207 return NULL; 208 } 209 210 return &(SLAVE_AD_INFO(slave->next).aggregator); 211} 212 213/* 214 * __agg_has_partner 215 * 216 * Return nonzero if aggregator has a partner (denoted by a non-zero ether 217 * address for the partner). Return 0 if not. 218 */ 219static inline int __agg_has_partner(struct aggregator *agg) 220{ 221 return !is_zero_ether_addr(agg->partner_system.mac_addr_value); 222} 223 224/** 225 * __disable_port - disable the port's slave 226 * @port: the port we're looking at 227 * 228 */ 229static inline void __disable_port(struct port *port) 230{ 231 bond_set_slave_inactive_flags(port->slave); 232} 233 234/** 235 * __enable_port - enable the port's slave, if it's up 236 * @port: the port we're looking at 237 * 238 */ 239static inline void __enable_port(struct port *port) 240{ 241 struct slave *slave = port->slave; 242 243 if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev)) { 244 bond_set_slave_active_flags(slave); 245 } 246} 247 248/** 249 * __port_is_enabled - check if the port's slave is in active state 250 * @port: the port we're looking at 251 * 252 */ 253static inline int __port_is_enabled(struct port *port) 254{ 255 return(port->slave->state == BOND_STATE_ACTIVE); 256} 257 258/** 259 * __get_agg_selection_mode - get the aggregator selection mode 260 * @port: the port we're looking at 261 * 262 * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT. 263 */ 264static inline u32 __get_agg_selection_mode(struct port *port) 265{ 266 struct bonding *bond = __get_bond_by_port(port); 267 268 if (bond == NULL) { 269 return BOND_AD_STABLE; 270 } 271 272 return BOND_AD_INFO(bond).agg_select_mode; 273} 274 275/** 276 * __check_agg_selection_timer - check if the selection timer has expired 277 * @port: the port we're looking at 278 * 279 */ 280static inline int __check_agg_selection_timer(struct port *port) 281{ 282 struct bonding *bond = __get_bond_by_port(port); 283 284 if (bond == NULL) { 285 return 0; 286 } 287 288 return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0; 289} 290 291/** 292 * __get_rx_machine_lock - lock the port's RX machine 293 * @port: the port we're looking at 294 * 295 */ 296static inline void __get_rx_machine_lock(struct port *port) 297{ 298 spin_lock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock)); 299} 300 301/** 302 * __release_rx_machine_lock - unlock the port's RX machine 303 * @port: the port we're looking at 304 * 305 */ 306static inline void __release_rx_machine_lock(struct port *port) 307{ 308 spin_unlock_bh(&(SLAVE_AD_INFO(port->slave).rx_machine_lock)); 309} 310 311/** 312 * __get_link_speed - get a port's speed 313 * @port: the port we're looking at 314 * 315 * Return @port's speed in 802.3ad bitmask format. i.e. one of: 316 * 0, 317 * %AD_LINK_SPEED_BITMASK_10MBPS, 318 * %AD_LINK_SPEED_BITMASK_100MBPS, 319 * %AD_LINK_SPEED_BITMASK_1000MBPS, 320 * %AD_LINK_SPEED_BITMASK_10000MBPS 321 */ 322static u16 __get_link_speed(struct port *port) 323{ 324 struct slave *slave = port->slave; 325 u16 speed; 326 327 /* this if covers only a special case: when the configuration starts with 328 * link down, it sets the speed to 0. 329 * This is done in spite of the fact that the e100 driver reports 0 to be 330 * compatible with MVT in the future.*/ 331 if (slave->link != BOND_LINK_UP) { 332 speed=0; 333 } else { 334 switch (slave->speed) { 335 case SPEED_10: 336 speed = AD_LINK_SPEED_BITMASK_10MBPS; 337 break; 338 339 case SPEED_100: 340 speed = AD_LINK_SPEED_BITMASK_100MBPS; 341 break; 342 343 case SPEED_1000: 344 speed = AD_LINK_SPEED_BITMASK_1000MBPS; 345 break; 346 347 case SPEED_10000: 348 speed = AD_LINK_SPEED_BITMASK_10000MBPS; 349 break; 350 351 default: 352 speed = 0; // unknown speed value from ethtool. shouldn't happen 353 break; 354 } 355 } 356 357 pr_debug("Port %d Received link speed %d update from adapter\n", 358 port->actor_port_number, speed); 359 return speed; 360} 361 362/** 363 * __get_duplex - get a port's duplex 364 * @port: the port we're looking at 365 * 366 * Return @port's duplex in 802.3ad bitmask format. i.e.: 367 * 0x01 if in full duplex 368 * 0x00 otherwise 369 */ 370static u8 __get_duplex(struct port *port) 371{ 372 struct slave *slave = port->slave; 373 374 u8 retval; 375 376 // handling a special case: when the configuration starts with 377 // link down, it sets the duplex to 0. 378 if (slave->link != BOND_LINK_UP) { 379 retval=0x0; 380 } else { 381 switch (slave->duplex) { 382 case DUPLEX_FULL: 383 retval=0x1; 384 pr_debug("Port %d Received status full duplex update from adapter\n", 385 port->actor_port_number); 386 break; 387 case DUPLEX_HALF: 388 default: 389 retval=0x0; 390 pr_debug("Port %d Received status NOT full duplex update from adapter\n", 391 port->actor_port_number); 392 break; 393 } 394 } 395 return retval; 396} 397 398/** 399 * __initialize_port_locks - initialize a port's RX machine spinlock 400 * @port: the port we're looking at 401 * 402 */ 403static inline void __initialize_port_locks(struct port *port) 404{ 405 // make sure it isn't called twice 406 spin_lock_init(&(SLAVE_AD_INFO(port->slave).rx_machine_lock)); 407} 408 409//conversions 410 411/** 412 * __ad_timer_to_ticks - convert a given timer type to AD module ticks 413 * @timer_type: which timer to operate 414 * @par: timer parameter. see below 415 * 416 * If @timer_type is %current_while_timer, @par indicates long/short timer. 417 * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME, 418 * %SLOW_PERIODIC_TIME. 419 */ 420static u16 __ad_timer_to_ticks(u16 timer_type, u16 par) 421{ 422 u16 retval=0; //to silence the compiler 423 424 switch (timer_type) { 425 case AD_CURRENT_WHILE_TIMER: // for rx machine usage 426 if (par) { // for short or long timeout 427 retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout 428 } else { 429 retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout 430 } 431 break; 432 case AD_ACTOR_CHURN_TIMER: // for local churn machine 433 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec); 434 break; 435 case AD_PERIODIC_TIMER: // for periodic machine 436 retval = (par*ad_ticks_per_sec); // long timeout 437 break; 438 case AD_PARTNER_CHURN_TIMER: // for remote churn machine 439 retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec); 440 break; 441 case AD_WAIT_WHILE_TIMER: // for selection machine 442 retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec); 443 break; 444 } 445 return retval; 446} 447 448 449///////////////////////////////////////////////////////////////////////////////// 450// ================= ad_rx_machine helper functions ================== 451///////////////////////////////////////////////////////////////////////////////// 452 453/** 454 * __choose_matched - update a port's matched variable from a received lacpdu 455 * @lacpdu: the lacpdu we've received 456 * @port: the port we're looking at 457 * 458 * Update the value of the matched variable, using parameter values from a 459 * newly received lacpdu. Parameter values for the partner carried in the 460 * received PDU are compared with the corresponding operational parameter 461 * values for the actor. Matched is set to TRUE if all of these parameters 462 * match and the PDU parameter partner_state.aggregation has the same value as 463 * actor_oper_port_state.aggregation and lacp will actively maintain the link 464 * in the aggregation. Matched is also set to TRUE if the value of 465 * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates 466 * an individual link and lacp will actively maintain the link. Otherwise, 467 * matched is set to FALSE. LACP is considered to be actively maintaining the 468 * link if either the PDU's actor_state.lacp_activity variable is TRUE or both 469 * the actor's actor_oper_port_state.lacp_activity and the PDU's 470 * partner_state.lacp_activity variables are TRUE. 471 * 472 * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is 473 * used here to implement the language from 802.3ad 43.4.9 that requires 474 * recordPDU to "match" the LACPDU parameters to the stored values. 475 */ 476static void __choose_matched(struct lacpdu *lacpdu, struct port *port) 477{ 478 // check if all parameters are alike 479 if (((ntohs(lacpdu->partner_port) == port->actor_port_number) && 480 (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) && 481 !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) && 482 (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) && 483 (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) && 484 ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) || 485 // or this is individual link(aggregation == FALSE) 486 ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0) 487 ) { 488 // update the state machine Matched variable 489 port->sm_vars |= AD_PORT_MATCHED; 490 } else { 491 port->sm_vars &= ~AD_PORT_MATCHED; 492 } 493} 494 495/** 496 * __record_pdu - record parameters from a received lacpdu 497 * @lacpdu: the lacpdu we've received 498 * @port: the port we're looking at 499 * 500 * Record the parameter values for the Actor carried in a received lacpdu as 501 * the current partner operational parameter values and sets 502 * actor_oper_port_state.defaulted to FALSE. 503 */ 504static void __record_pdu(struct lacpdu *lacpdu, struct port *port) 505{ 506 if (lacpdu && port) { 507 struct port_params *partner = &port->partner_oper; 508 509 __choose_matched(lacpdu, port); 510 // record the new parameter values for the partner operational 511 partner->port_number = ntohs(lacpdu->actor_port); 512 partner->port_priority = ntohs(lacpdu->actor_port_priority); 513 partner->system = lacpdu->actor_system; 514 partner->system_priority = ntohs(lacpdu->actor_system_priority); 515 partner->key = ntohs(lacpdu->actor_key); 516 partner->port_state = lacpdu->actor_state; 517 518 // set actor_oper_port_state.defaulted to FALSE 519 port->actor_oper_port_state &= ~AD_STATE_DEFAULTED; 520 521 // set the partner sync. to on if the partner is sync. and the port is matched 522 if ((port->sm_vars & AD_PORT_MATCHED) && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) { 523 partner->port_state |= AD_STATE_SYNCHRONIZATION; 524 } else { 525 partner->port_state &= ~AD_STATE_SYNCHRONIZATION; 526 } 527 } 528} 529 530/** 531 * __record_default - record default parameters 532 * @port: the port we're looking at 533 * 534 * This function records the default parameter values for the partner carried 535 * in the Partner Admin parameters as the current partner operational parameter 536 * values and sets actor_oper_port_state.defaulted to TRUE. 537 */ 538static void __record_default(struct port *port) 539{ 540 if (port) { 541 // record the partner admin parameters 542 memcpy(&port->partner_oper, &port->partner_admin, 543 sizeof(struct port_params)); 544 545 // set actor_oper_port_state.defaulted to true 546 port->actor_oper_port_state |= AD_STATE_DEFAULTED; 547 } 548} 549 550/** 551 * __update_selected - update a port's Selected variable from a received lacpdu 552 * @lacpdu: the lacpdu we've received 553 * @port: the port we're looking at 554 * 555 * Update the value of the selected variable, using parameter values from a 556 * newly received lacpdu. The parameter values for the Actor carried in the 557 * received PDU are compared with the corresponding operational parameter 558 * values for the ports partner. If one or more of the comparisons shows that 559 * the value(s) received in the PDU differ from the current operational values, 560 * then selected is set to FALSE and actor_oper_port_state.synchronization is 561 * set to out_of_sync. Otherwise, selected remains unchanged. 562 */ 563static void __update_selected(struct lacpdu *lacpdu, struct port *port) 564{ 565 if (lacpdu && port) { 566 const struct port_params *partner = &port->partner_oper; 567 568 // check if any parameter is different 569 if (ntohs(lacpdu->actor_port) != partner->port_number || 570 ntohs(lacpdu->actor_port_priority) != partner->port_priority || 571 MAC_ADDRESS_COMPARE(&lacpdu->actor_system, &partner->system) || 572 ntohs(lacpdu->actor_system_priority) != partner->system_priority || 573 ntohs(lacpdu->actor_key) != partner->key || 574 (lacpdu->actor_state & AD_STATE_AGGREGATION) != (partner->port_state & AD_STATE_AGGREGATION)) { 575 // update the state machine Selected variable 576 port->sm_vars &= ~AD_PORT_SELECTED; 577 } 578 } 579} 580 581/** 582 * __update_default_selected - update a port's Selected variable from Partner 583 * @port: the port we're looking at 584 * 585 * This function updates the value of the selected variable, using the partner 586 * administrative parameter values. The administrative values are compared with 587 * the corresponding operational parameter values for the partner. If one or 588 * more of the comparisons shows that the administrative value(s) differ from 589 * the current operational values, then Selected is set to FALSE and 590 * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise, 591 * Selected remains unchanged. 592 */ 593static void __update_default_selected(struct port *port) 594{ 595 if (port) { 596 const struct port_params *admin = &port->partner_admin; 597 const struct port_params *oper = &port->partner_oper; 598 599 // check if any parameter is different 600 if (admin->port_number != oper->port_number || 601 admin->port_priority != oper->port_priority || 602 MAC_ADDRESS_COMPARE(&admin->system, &oper->system) || 603 admin->system_priority != oper->system_priority || 604 admin->key != oper->key || 605 (admin->port_state & AD_STATE_AGGREGATION) 606 != (oper->port_state & AD_STATE_AGGREGATION)) { 607 // update the state machine Selected variable 608 port->sm_vars &= ~AD_PORT_SELECTED; 609 } 610 } 611} 612 613/** 614 * __update_ntt - update a port's ntt variable from a received lacpdu 615 * @lacpdu: the lacpdu we've received 616 * @port: the port we're looking at 617 * 618 * Updates the value of the ntt variable, using parameter values from a newly 619 * received lacpdu. The parameter values for the partner carried in the 620 * received PDU are compared with the corresponding operational parameter 621 * values for the Actor. If one or more of the comparisons shows that the 622 * value(s) received in the PDU differ from the current operational values, 623 * then ntt is set to TRUE. Otherwise, ntt remains unchanged. 624 */ 625static void __update_ntt(struct lacpdu *lacpdu, struct port *port) 626{ 627 // validate lacpdu and port 628 if (lacpdu && port) { 629 // check if any parameter is different 630 if ((ntohs(lacpdu->partner_port) != port->actor_port_number) || 631 (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) || 632 MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) || 633 (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) || 634 (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) || 635 ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) || 636 ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) || 637 ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) || 638 ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION)) 639 ) { 640 641 port->ntt = true; 642 } 643 } 644} 645 646/** 647 * __attach_bond_to_agg 648 * @port: the port we're looking at 649 * 650 * Handle the attaching of the port's control parser/multiplexer and the 651 * aggregator. This function does nothing since the parser/multiplexer of the 652 * receive and the parser/multiplexer of the aggregator are already combined. 653 */ 654static void __attach_bond_to_agg(struct port *port) 655{ 656 port=NULL; // just to satisfy the compiler 657 // This function does nothing since the parser/multiplexer of the receive 658 // and the parser/multiplexer of the aggregator are already combined 659} 660 661/** 662 * __detach_bond_from_agg 663 * @port: the port we're looking at 664 * 665 * Handle the detaching of the port's control parser/multiplexer from the 666 * aggregator. This function does nothing since the parser/multiplexer of the 667 * receive and the parser/multiplexer of the aggregator are already combined. 668 */ 669static void __detach_bond_from_agg(struct port *port) 670{ 671 port=NULL; // just to satisfy the compiler 672 // This function does nothing sience the parser/multiplexer of the receive 673 // and the parser/multiplexer of the aggregator are already combined 674} 675 676/** 677 * __agg_ports_are_ready - check if all ports in an aggregator are ready 678 * @aggregator: the aggregator we're looking at 679 * 680 */ 681static int __agg_ports_are_ready(struct aggregator *aggregator) 682{ 683 struct port *port; 684 int retval = 1; 685 686 if (aggregator) { 687 // scan all ports in this aggregator to verfy if they are all ready 688 for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) { 689 if (!(port->sm_vars & AD_PORT_READY_N)) { 690 retval = 0; 691 break; 692 } 693 } 694 } 695 696 return retval; 697} 698 699/** 700 * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator 701 * @aggregator: the aggregator we're looking at 702 * @val: Should the ports' ready bit be set on or off 703 * 704 */ 705static void __set_agg_ports_ready(struct aggregator *aggregator, int val) 706{ 707 struct port *port; 708 709 for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) { 710 if (val) { 711 port->sm_vars |= AD_PORT_READY; 712 } else { 713 port->sm_vars &= ~AD_PORT_READY; 714 } 715 } 716} 717 718/** 719 * __get_agg_bandwidth - get the total bandwidth of an aggregator 720 * @aggregator: the aggregator we're looking at 721 * 722 */ 723static u32 __get_agg_bandwidth(struct aggregator *aggregator) 724{ 725 u32 bandwidth=0; 726 u32 basic_speed; 727 728 if (aggregator->num_of_ports) { 729 basic_speed = __get_link_speed(aggregator->lag_ports); 730 switch (basic_speed) { 731 case AD_LINK_SPEED_BITMASK_1MBPS: 732 bandwidth = aggregator->num_of_ports; 733 break; 734 case AD_LINK_SPEED_BITMASK_10MBPS: 735 bandwidth = aggregator->num_of_ports * 10; 736 break; 737 case AD_LINK_SPEED_BITMASK_100MBPS: 738 bandwidth = aggregator->num_of_ports * 100; 739 break; 740 case AD_LINK_SPEED_BITMASK_1000MBPS: 741 bandwidth = aggregator->num_of_ports * 1000; 742 break; 743 case AD_LINK_SPEED_BITMASK_10000MBPS: 744 bandwidth = aggregator->num_of_ports * 10000; 745 break; 746 default: 747 bandwidth=0; // to silent the compilor .... 748 } 749 } 750 return bandwidth; 751} 752 753/** 754 * __get_active_agg - get the current active aggregator 755 * @aggregator: the aggregator we're looking at 756 * 757 */ 758static struct aggregator *__get_active_agg(struct aggregator *aggregator) 759{ 760 struct aggregator *retval = NULL; 761 762 for (; aggregator; aggregator = __get_next_agg(aggregator)) { 763 if (aggregator->is_active) { 764 retval = aggregator; 765 break; 766 } 767 } 768 769 return retval; 770} 771 772/** 773 * __update_lacpdu_from_port - update a port's lacpdu fields 774 * @port: the port we're looking at 775 * 776 */ 777static inline void __update_lacpdu_from_port(struct port *port) 778{ 779 struct lacpdu *lacpdu = &port->lacpdu; 780 const struct port_params *partner = &port->partner_oper; 781 782 /* update current actual Actor parameters */ 783 /* lacpdu->subtype initialized 784 * lacpdu->version_number initialized 785 * lacpdu->tlv_type_actor_info initialized 786 * lacpdu->actor_information_length initialized 787 */ 788 789 lacpdu->actor_system_priority = htons(port->actor_system_priority); 790 lacpdu->actor_system = port->actor_system; 791 lacpdu->actor_key = htons(port->actor_oper_port_key); 792 lacpdu->actor_port_priority = htons(port->actor_port_priority); 793 lacpdu->actor_port = htons(port->actor_port_number); 794 lacpdu->actor_state = port->actor_oper_port_state; 795 796 /* lacpdu->reserved_3_1 initialized 797 * lacpdu->tlv_type_partner_info initialized 798 * lacpdu->partner_information_length initialized 799 */ 800 801 lacpdu->partner_system_priority = htons(partner->system_priority); 802 lacpdu->partner_system = partner->system; 803 lacpdu->partner_key = htons(partner->key); 804 lacpdu->partner_port_priority = htons(partner->port_priority); 805 lacpdu->partner_port = htons(partner->port_number); 806 lacpdu->partner_state = partner->port_state; 807 808 /* lacpdu->reserved_3_2 initialized 809 * lacpdu->tlv_type_collector_info initialized 810 * lacpdu->collector_information_length initialized 811 * collector_max_delay initialized 812 * reserved_12[12] initialized 813 * tlv_type_terminator initialized 814 * terminator_length initialized 815 * reserved_50[50] initialized 816 */ 817} 818 819////////////////////////////////////////////////////////////////////////////////////// 820// ================= main 802.3ad protocol code ====================================== 821////////////////////////////////////////////////////////////////////////////////////// 822 823/** 824 * ad_lacpdu_send - send out a lacpdu packet on a given port 825 * @port: the port we're looking at 826 * 827 * Returns: 0 on success 828 * < 0 on error 829 */ 830static int ad_lacpdu_send(struct port *port) 831{ 832 struct slave *slave = port->slave; 833 struct sk_buff *skb; 834 struct lacpdu_header *lacpdu_header; 835 int length = sizeof(struct lacpdu_header); 836 837 skb = dev_alloc_skb(length); 838 if (!skb) { 839 return -ENOMEM; 840 } 841 842 skb->dev = slave->dev; 843 skb_reset_mac_header(skb); 844 skb->network_header = skb->mac_header + ETH_HLEN; 845 skb->protocol = PKT_TYPE_LACPDU; 846 skb->priority = TC_PRIO_CONTROL; 847 848 lacpdu_header = (struct lacpdu_header *)skb_put(skb, length); 849 850 memcpy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN); 851 /* Note: source addres is set to be the member's PERMANENT address, 852 because we use it to identify loopback lacpdus in receive. */ 853 memcpy(lacpdu_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN); 854 lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU; 855 856 lacpdu_header->lacpdu = port->lacpdu; // struct copy 857 858 dev_queue_xmit(skb); 859 860 return 0; 861} 862 863/** 864 * ad_marker_send - send marker information/response on a given port 865 * @port: the port we're looking at 866 * @marker: marker data to send 867 * 868 * Returns: 0 on success 869 * < 0 on error 870 */ 871static int ad_marker_send(struct port *port, struct bond_marker *marker) 872{ 873 struct slave *slave = port->slave; 874 struct sk_buff *skb; 875 struct bond_marker_header *marker_header; 876 int length = sizeof(struct bond_marker_header); 877 878 skb = dev_alloc_skb(length + 16); 879 if (!skb) { 880 return -ENOMEM; 881 } 882 883 skb_reserve(skb, 16); 884 885 skb->dev = slave->dev; 886 skb_reset_mac_header(skb); 887 skb->network_header = skb->mac_header + ETH_HLEN; 888 skb->protocol = PKT_TYPE_LACPDU; 889 890 marker_header = (struct bond_marker_header *)skb_put(skb, length); 891 892 memcpy(marker_header->hdr.h_dest, lacpdu_mcast_addr, ETH_ALEN); 893 /* Note: source addres is set to be the member's PERMANENT address, 894 because we use it to identify loopback MARKERs in receive. */ 895 memcpy(marker_header->hdr.h_source, slave->perm_hwaddr, ETH_ALEN); 896 marker_header->hdr.h_proto = PKT_TYPE_LACPDU; 897 898 marker_header->marker = *marker; // struct copy 899 900 dev_queue_xmit(skb); 901 902 return 0; 903} 904 905/** 906 * ad_mux_machine - handle a port's mux state machine 907 * @port: the port we're looking at 908 * 909 */ 910static void ad_mux_machine(struct port *port) 911{ 912 mux_states_t last_state; 913 914 // keep current State Machine state to compare later if it was changed 915 last_state = port->sm_mux_state; 916 917 if (port->sm_vars & AD_PORT_BEGIN) { 918 port->sm_mux_state = AD_MUX_DETACHED; // next state 919 } else { 920 switch (port->sm_mux_state) { 921 case AD_MUX_DETACHED: 922 if ((port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if SELECTED or STANDBY 923 port->sm_mux_state = AD_MUX_WAITING; // next state 924 } 925 break; 926 case AD_MUX_WAITING: 927 // if SELECTED == FALSE return to DETACH state 928 if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED 929 port->sm_vars &= ~AD_PORT_READY_N; 930 // in order to withhold the Selection Logic to check all ports READY_N value 931 // every callback cycle to update ready variable, we check READY_N and update READY here 932 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator)); 933 port->sm_mux_state = AD_MUX_DETACHED; // next state 934 break; 935 } 936 937 // check if the wait_while_timer expired 938 if (port->sm_mux_timer_counter && !(--port->sm_mux_timer_counter)) { 939 port->sm_vars |= AD_PORT_READY_N; 940 } 941 942 // in order to withhold the selection logic to check all ports READY_N value 943 // every callback cycle to update ready variable, we check READY_N and update READY here 944 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator)); 945 946 // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state 947 if ((port->sm_vars & AD_PORT_READY) && !port->sm_mux_timer_counter) { 948 port->sm_mux_state = AD_MUX_ATTACHED; // next state 949 } 950 break; 951 case AD_MUX_ATTACHED: 952 // check also if agg_select_timer expired(so the edable port will take place only after this timer) 953 if ((port->sm_vars & AD_PORT_SELECTED) && (port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) && !__check_agg_selection_timer(port)) { 954 port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state 955 } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY 956 port->sm_vars &= ~AD_PORT_READY_N; 957 // in order to withhold the selection logic to check all ports READY_N value 958 // every callback cycle to update ready variable, we check READY_N and update READY here 959 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator)); 960 port->sm_mux_state = AD_MUX_DETACHED;// next state 961 } 962 break; 963 case AD_MUX_COLLECTING_DISTRIBUTING: 964 if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) || 965 !(port->partner_oper.port_state & AD_STATE_SYNCHRONIZATION) 966 ) { 967 port->sm_mux_state = AD_MUX_ATTACHED;// next state 968 969 } else { 970 // if port state hasn't changed make 971 // sure that a collecting distributing 972 // port in an active aggregator is enabled 973 if (port->aggregator && 974 port->aggregator->is_active && 975 !__port_is_enabled(port)) { 976 977 __enable_port(port); 978 } 979 } 980 break; 981 default: //to silence the compiler 982 break; 983 } 984 } 985 986 // check if the state machine was changed 987 if (port->sm_mux_state != last_state) { 988 pr_debug("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n", 989 port->actor_port_number, last_state, 990 port->sm_mux_state); 991 switch (port->sm_mux_state) { 992 case AD_MUX_DETACHED: 993 __detach_bond_from_agg(port); 994 port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION; 995 ad_disable_collecting_distributing(port); 996 port->actor_oper_port_state &= ~AD_STATE_COLLECTING; 997 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING; 998 port->ntt = true; 999 break; 1000 case AD_MUX_WAITING: 1001 port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0); 1002 break; 1003 case AD_MUX_ATTACHED: 1004 __attach_bond_to_agg(port); 1005 port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION; 1006 port->actor_oper_port_state &= ~AD_STATE_COLLECTING; 1007 port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING; 1008 ad_disable_collecting_distributing(port); 1009 port->ntt = true; 1010 break; 1011 case AD_MUX_COLLECTING_DISTRIBUTING: 1012 port->actor_oper_port_state |= AD_STATE_COLLECTING; 1013 port->actor_oper_port_state |= AD_STATE_DISTRIBUTING; 1014 ad_enable_collecting_distributing(port); 1015 port->ntt = true; 1016 break; 1017 default: //to silence the compiler 1018 break; 1019 } 1020 } 1021} 1022 1023/** 1024 * ad_rx_machine - handle a port's rx State Machine 1025 * @lacpdu: the lacpdu we've received 1026 * @port: the port we're looking at 1027 * 1028 * If lacpdu arrived, stop previous timer (if exists) and set the next state as 1029 * CURRENT. If timer expired set the state machine in the proper state. 1030 * In other cases, this function checks if we need to switch to other state. 1031 */ 1032static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port) 1033{ 1034 rx_states_t last_state; 1035 1036 // Lock to prevent 2 instances of this function to run simultaneously(rx interrupt and periodic machine callback) 1037 __get_rx_machine_lock(port); 1038 1039 // keep current State Machine state to compare later if it was changed 1040 last_state = port->sm_rx_state; 1041 1042 // check if state machine should change state 1043 // first, check if port was reinitialized 1044 if (port->sm_vars & AD_PORT_BEGIN) { 1045 port->sm_rx_state = AD_RX_INITIALIZE; // next state 1046 } 1047 // check if port is not enabled 1048 else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED)) { 1049 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state 1050 } 1051 // check if new lacpdu arrived 1052 else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) { 1053 port->sm_rx_timer_counter = 0; // zero timer 1054 port->sm_rx_state = AD_RX_CURRENT; 1055 } else { 1056 // if timer is on, and if it is expired 1057 if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) { 1058 switch (port->sm_rx_state) { 1059 case AD_RX_EXPIRED: 1060 port->sm_rx_state = AD_RX_DEFAULTED; // next state 1061 break; 1062 case AD_RX_CURRENT: 1063 port->sm_rx_state = AD_RX_EXPIRED; // next state 1064 break; 1065 default: //to silence the compiler 1066 break; 1067 } 1068 } else { 1069 // if no lacpdu arrived and no timer is on 1070 switch (port->sm_rx_state) { 1071 case AD_RX_PORT_DISABLED: 1072 if (port->sm_vars & AD_PORT_MOVED) { 1073 port->sm_rx_state = AD_RX_INITIALIZE; // next state 1074 } else if (port->is_enabled && (port->sm_vars & AD_PORT_LACP_ENABLED)) { 1075 port->sm_rx_state = AD_RX_EXPIRED; // next state 1076 } else if (port->is_enabled && ((port->sm_vars & AD_PORT_LACP_ENABLED) == 0)) { 1077 port->sm_rx_state = AD_RX_LACP_DISABLED; // next state 1078 } 1079 break; 1080 default: //to silence the compiler 1081 break; 1082 1083 } 1084 } 1085 } 1086 1087 // check if the State machine was changed or new lacpdu arrived 1088 if ((port->sm_rx_state != last_state) || (lacpdu)) { 1089 pr_debug("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n", 1090 port->actor_port_number, last_state, 1091 port->sm_rx_state); 1092 switch (port->sm_rx_state) { 1093 case AD_RX_INITIALIZE: 1094 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) { 1095 port->sm_vars &= ~AD_PORT_LACP_ENABLED; 1096 } else { 1097 port->sm_vars |= AD_PORT_LACP_ENABLED; 1098 } 1099 port->sm_vars &= ~AD_PORT_SELECTED; 1100 __record_default(port); 1101 port->actor_oper_port_state &= ~AD_STATE_EXPIRED; 1102 port->sm_vars &= ~AD_PORT_MOVED; 1103 port->sm_rx_state = AD_RX_PORT_DISABLED; // next state 1104 1105 /*- Fall Through -*/ 1106 1107 case AD_RX_PORT_DISABLED: 1108 port->sm_vars &= ~AD_PORT_MATCHED; 1109 break; 1110 case AD_RX_LACP_DISABLED: 1111 port->sm_vars &= ~AD_PORT_SELECTED; 1112 __record_default(port); 1113 port->partner_oper.port_state &= ~AD_STATE_AGGREGATION; 1114 port->sm_vars |= AD_PORT_MATCHED; 1115 port->actor_oper_port_state &= ~AD_STATE_EXPIRED; 1116 break; 1117 case AD_RX_EXPIRED: 1118 //Reset of the Synchronization flag. (Standard 43.4.12) 1119 //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the 1120 //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port. 1121 port->partner_oper.port_state &= ~AD_STATE_SYNCHRONIZATION; 1122 port->sm_vars &= ~AD_PORT_MATCHED; 1123 port->partner_oper.port_state |= 1124 AD_STATE_LACP_ACTIVITY; 1125 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT)); 1126 port->actor_oper_port_state |= AD_STATE_EXPIRED; 1127 break; 1128 case AD_RX_DEFAULTED: 1129 __update_default_selected(port); 1130 __record_default(port); 1131 port->sm_vars |= AD_PORT_MATCHED; 1132 port->actor_oper_port_state &= ~AD_STATE_EXPIRED; 1133 break; 1134 case AD_RX_CURRENT: 1135 // detect loopback situation 1136 if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) { 1137 // INFO_RECEIVED_LOOPBACK_FRAMES 1138 pr_err("%s: An illegal loopback occurred on adapter (%s).\n" 1139 "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n", 1140 port->slave->dev->master->name, port->slave->dev->name); 1141 __release_rx_machine_lock(port); 1142 return; 1143 } 1144 __update_selected(lacpdu, port); 1145 __update_ntt(lacpdu, port); 1146 __record_pdu(lacpdu, port); 1147 port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)); 1148 port->actor_oper_port_state &= ~AD_STATE_EXPIRED; 1149 // verify that if the aggregator is enabled, the port is enabled too. 1150 //(because if the link goes down for a short time, the 802.3ad will not 1151 // catch it, and the port will continue to be disabled) 1152 if (port->aggregator && port->aggregator->is_active && !__port_is_enabled(port)) { 1153 __enable_port(port); 1154 } 1155 break; 1156 default: //to silence the compiler 1157 break; 1158 } 1159 } 1160 __release_rx_machine_lock(port); 1161} 1162 1163/** 1164 * ad_tx_machine - handle a port's tx state machine 1165 * @port: the port we're looking at 1166 * 1167 */ 1168static void ad_tx_machine(struct port *port) 1169{ 1170 // check if tx timer expired, to verify that we do not send more than 3 packets per second 1171 if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) { 1172 // check if there is something to send 1173 if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) { 1174 __update_lacpdu_from_port(port); 1175 1176 if (ad_lacpdu_send(port) >= 0) { 1177 pr_debug("Sent LACPDU on port %d\n", 1178 port->actor_port_number); 1179 1180 /* mark ntt as false, so it will not be sent again until 1181 demanded */ 1182 port->ntt = false; 1183 } 1184 } 1185 // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND 1186 port->sm_tx_timer_counter=ad_ticks_per_sec/AD_MAX_TX_IN_SECOND; 1187 } 1188} 1189 1190/** 1191 * ad_periodic_machine - handle a port's periodic state machine 1192 * @port: the port we're looking at 1193 * 1194 * Turn ntt flag on priodically to perform periodic transmission of lacpdu's. 1195 */ 1196static void ad_periodic_machine(struct port *port) 1197{ 1198 periodic_states_t last_state; 1199 1200 // keep current state machine state to compare later if it was changed 1201 last_state = port->sm_periodic_state; 1202 1203 // check if port was reinitialized 1204 if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) || 1205 (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & AD_STATE_LACP_ACTIVITY)) 1206 ) { 1207 port->sm_periodic_state = AD_NO_PERIODIC; // next state 1208 } 1209 // check if state machine should change state 1210 else if (port->sm_periodic_timer_counter) { 1211 // check if periodic state machine expired 1212 if (!(--port->sm_periodic_timer_counter)) { 1213 // if expired then do tx 1214 port->sm_periodic_state = AD_PERIODIC_TX; // next state 1215 } else { 1216 // If not expired, check if there is some new timeout parameter from the partner state 1217 switch (port->sm_periodic_state) { 1218 case AD_FAST_PERIODIC: 1219 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) { 1220 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state 1221 } 1222 break; 1223 case AD_SLOW_PERIODIC: 1224 if ((port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) { 1225 // stop current timer 1226 port->sm_periodic_timer_counter = 0; 1227 port->sm_periodic_state = AD_PERIODIC_TX; // next state 1228 } 1229 break; 1230 default: //to silence the compiler 1231 break; 1232 } 1233 } 1234 } else { 1235 switch (port->sm_periodic_state) { 1236 case AD_NO_PERIODIC: 1237 port->sm_periodic_state = AD_FAST_PERIODIC; // next state 1238 break; 1239 case AD_PERIODIC_TX: 1240 if (!(port->partner_oper.port_state & AD_STATE_LACP_TIMEOUT)) { 1241 port->sm_periodic_state = AD_SLOW_PERIODIC; // next state 1242 } else { 1243 port->sm_periodic_state = AD_FAST_PERIODIC; // next state 1244 } 1245 break; 1246 default: //to silence the compiler 1247 break; 1248 } 1249 } 1250 1251 // check if the state machine was changed 1252 if (port->sm_periodic_state != last_state) { 1253 pr_debug("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n", 1254 port->actor_port_number, last_state, 1255 port->sm_periodic_state); 1256 switch (port->sm_periodic_state) { 1257 case AD_NO_PERIODIC: 1258 port->sm_periodic_timer_counter = 0; // zero timer 1259 break; 1260 case AD_FAST_PERIODIC: 1261 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1; // decrement 1 tick we lost in the PERIODIC_TX cycle 1262 break; 1263 case AD_SLOW_PERIODIC: 1264 port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1; // decrement 1 tick we lost in the PERIODIC_TX cycle 1265 break; 1266 case AD_PERIODIC_TX: 1267 port->ntt = true; 1268 break; 1269 default: //to silence the compiler 1270 break; 1271 } 1272 } 1273} 1274 1275/** 1276 * ad_port_selection_logic - select aggregation groups 1277 * @port: the port we're looking at 1278 * 1279 * Select aggregation groups, and assign each port for it's aggregetor. The 1280 * selection logic is called in the inititalization (after all the handshkes), 1281 * and after every lacpdu receive (if selected is off). 1282 */ 1283static void ad_port_selection_logic(struct port *port) 1284{ 1285 struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator; 1286 struct port *last_port = NULL, *curr_port; 1287 int found = 0; 1288 1289 // if the port is already Selected, do nothing 1290 if (port->sm_vars & AD_PORT_SELECTED) { 1291 return; 1292 } 1293 1294 // if the port is connected to other aggregator, detach it 1295 if (port->aggregator) { 1296 // detach the port from its former aggregator 1297 temp_aggregator=port->aggregator; 1298 for (curr_port=temp_aggregator->lag_ports; curr_port; last_port=curr_port, curr_port=curr_port->next_port_in_aggregator) { 1299 if (curr_port == port) { 1300 temp_aggregator->num_of_ports--; 1301 if (!last_port) {// if it is the first port attached to the aggregator 1302 temp_aggregator->lag_ports=port->next_port_in_aggregator; 1303 } else {// not the first port attached to the aggregator 1304 last_port->next_port_in_aggregator=port->next_port_in_aggregator; 1305 } 1306 1307 // clear the port's relations to this aggregator 1308 port->aggregator = NULL; 1309 port->next_port_in_aggregator=NULL; 1310 port->actor_port_aggregator_identifier=0; 1311 1312 pr_debug("Port %d left LAG %d\n", 1313 port->actor_port_number, 1314 temp_aggregator->aggregator_identifier); 1315 // if the aggregator is empty, clear its parameters, and set it ready to be attached 1316 if (!temp_aggregator->lag_ports) { 1317 ad_clear_agg(temp_aggregator); 1318 } 1319 break; 1320 } 1321 } 1322 if (!curr_port) { // meaning: the port was related to an aggregator but was not on the aggregator port list 1323 pr_warning("%s: Warning: Port %d (on %s) was related to aggregator %d but was not on its port list\n", 1324 port->slave->dev->master->name, 1325 port->actor_port_number, 1326 port->slave->dev->name, 1327 port->aggregator->aggregator_identifier); 1328 } 1329 } 1330 // search on all aggregators for a suitable aggregator for this port 1331 for (aggregator = __get_first_agg(port); aggregator; 1332 aggregator = __get_next_agg(aggregator)) { 1333 1334 // keep a free aggregator for later use(if needed) 1335 if (!aggregator->lag_ports) { 1336 if (!free_aggregator) { 1337 free_aggregator=aggregator; 1338 } 1339 continue; 1340 } 1341 // check if current aggregator suits us 1342 if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND 1343 !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper.system)) && 1344 (aggregator->partner_system_priority == port->partner_oper.system_priority) && 1345 (aggregator->partner_oper_aggregator_key == port->partner_oper.key) 1346 ) && 1347 ((MAC_ADDRESS_COMPARE(&(port->partner_oper.system), &(null_mac_addr)) && // partner answers 1348 !aggregator->is_individual) // but is not individual OR 1349 ) 1350 ) { 1351 // attach to the founded aggregator 1352 port->aggregator = aggregator; 1353 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier; 1354 port->next_port_in_aggregator=aggregator->lag_ports; 1355 port->aggregator->num_of_ports++; 1356 aggregator->lag_ports=port; 1357 pr_debug("Port %d joined LAG %d(existing LAG)\n", 1358 port->actor_port_number, 1359 port->aggregator->aggregator_identifier); 1360 1361 // mark this port as selected 1362 port->sm_vars |= AD_PORT_SELECTED; 1363 found = 1; 1364 break; 1365 } 1366 } 1367 1368 // the port couldn't find an aggregator - attach it to a new aggregator 1369 if (!found) { 1370 if (free_aggregator) { 1371 // assign port a new aggregator 1372 port->aggregator = free_aggregator; 1373 port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier; 1374 1375 // update the new aggregator's parameters 1376 // if port was responsed from the end-user 1377 if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS) {// if port is full duplex 1378 port->aggregator->is_individual = false; 1379 } else { 1380 port->aggregator->is_individual = true; 1381 } 1382 1383 port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key; 1384 port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key; 1385 port->aggregator->partner_system=port->partner_oper.system; 1386 port->aggregator->partner_system_priority = port->partner_oper.system_priority; 1387 port->aggregator->partner_oper_aggregator_key = port->partner_oper.key; 1388 port->aggregator->receive_state = 1; 1389 port->aggregator->transmit_state = 1; 1390 port->aggregator->lag_ports = port; 1391 port->aggregator->num_of_ports++; 1392 1393 // mark this port as selected 1394 port->sm_vars |= AD_PORT_SELECTED; 1395 1396 pr_debug("Port %d joined LAG %d(new LAG)\n", 1397 port->actor_port_number, 1398 port->aggregator->aggregator_identifier); 1399 } else { 1400 pr_err("%s: Port %d (on %s) did not find a suitable aggregator\n", 1401 port->slave->dev->master->name, 1402 port->actor_port_number, port->slave->dev->name); 1403 } 1404 } 1405 // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports 1406 // else set ready=FALSE in all aggregator's ports 1407 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator)); 1408 1409 aggregator = __get_first_agg(port); 1410 ad_agg_selection_logic(aggregator); 1411} 1412 1413/* 1414 * Decide if "agg" is a better choice for the new active aggregator that 1415 * the current best, according to the ad_select policy. 1416 */ 1417static struct aggregator *ad_agg_selection_test(struct aggregator *best, 1418 struct aggregator *curr) 1419{ 1420 /* 1421 * 0. If no best, select current. 1422 * 1423 * 1. If the current agg is not individual, and the best is 1424 * individual, select current. 1425 * 1426 * 2. If current agg is individual and the best is not, keep best. 1427 * 1428 * 3. Therefore, current and best are both individual or both not 1429 * individual, so: 1430 * 1431 * 3a. If current agg partner replied, and best agg partner did not, 1432 * select current. 1433 * 1434 * 3b. If current agg partner did not reply and best agg partner 1435 * did reply, keep best. 1436 * 1437 * 4. Therefore, current and best both have partner replies or 1438 * both do not, so perform selection policy: 1439 * 1440 * BOND_AD_COUNT: Select by count of ports. If count is equal, 1441 * select by bandwidth. 1442 * 1443 * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth. 1444 */ 1445 if (!best) 1446 return curr; 1447 1448 if (!curr->is_individual && best->is_individual) 1449 return curr; 1450 1451 if (curr->is_individual && !best->is_individual) 1452 return best; 1453 1454 if (__agg_has_partner(curr) && !__agg_has_partner(best)) 1455 return curr; 1456 1457 if (!__agg_has_partner(curr) && __agg_has_partner(best)) 1458 return best; 1459 1460 switch (__get_agg_selection_mode(curr->lag_ports)) { 1461 case BOND_AD_COUNT: 1462 if (curr->num_of_ports > best->num_of_ports) 1463 return curr; 1464 1465 if (curr->num_of_ports < best->num_of_ports) 1466 return best; 1467 1468 /*FALLTHROUGH*/ 1469 case BOND_AD_STABLE: 1470 case BOND_AD_BANDWIDTH: 1471 if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best)) 1472 return curr; 1473 1474 break; 1475 1476 default: 1477 pr_warning("%s: Impossible agg select mode %d\n", 1478 curr->slave->dev->master->name, 1479 __get_agg_selection_mode(curr->lag_ports)); 1480 break; 1481 } 1482 1483 return best; 1484} 1485 1486static int agg_device_up(const struct aggregator *agg) 1487{ 1488 return (netif_running(agg->slave->dev) && 1489 netif_carrier_ok(agg->slave->dev)); 1490} 1491 1492static void ad_agg_selection_logic(struct aggregator *agg) 1493{ 1494 struct aggregator *best, *active, *origin; 1495 struct port *port; 1496 1497 origin = agg; 1498 active = __get_active_agg(agg); 1499 best = (active && agg_device_up(active)) ? active : NULL; 1500 1501 do { 1502 agg->is_active = 0; 1503 1504 if (agg->num_of_ports && agg_device_up(agg)) 1505 best = ad_agg_selection_test(best, agg); 1506 1507 } while ((agg = __get_next_agg(agg))); 1508 1509 if (best && 1510 __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) { 1511 /* 1512 * For the STABLE policy, don't replace the old active 1513 * aggregator if it's still active (it has an answering 1514 * partner) or if both the best and active don't have an 1515 * answering partner. 1516 */ 1517 if (active && active->lag_ports && 1518 active->lag_ports->is_enabled && 1519 (__agg_has_partner(active) || 1520 (!__agg_has_partner(active) && !__agg_has_partner(best)))) { 1521 if (!(!active->actor_oper_aggregator_key && 1522 best->actor_oper_aggregator_key)) { 1523 best = NULL; 1524 active->is_active = 1; 1525 } 1526 } 1527 } 1528 1529 if (best && (best == active)) { 1530 best = NULL; 1531 active->is_active = 1; 1532 } 1533 1534 // if there is new best aggregator, activate it 1535 if (best) { 1536 pr_debug("best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n", 1537 best->aggregator_identifier, best->num_of_ports, 1538 best->actor_oper_aggregator_key, 1539 best->partner_oper_aggregator_key, 1540 best->is_individual, best->is_active); 1541 pr_debug("best ports %p slave %p %s\n", 1542 best->lag_ports, best->slave, 1543 best->slave ? best->slave->dev->name : "NULL"); 1544 1545 for (agg = __get_first_agg(best->lag_ports); agg; 1546 agg = __get_next_agg(agg)) { 1547 1548 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n", 1549 agg->aggregator_identifier, agg->num_of_ports, 1550 agg->actor_oper_aggregator_key, 1551 agg->partner_oper_aggregator_key, 1552 agg->is_individual, agg->is_active); 1553 } 1554 1555 // check if any partner replys 1556 if (best->is_individual) { 1557 pr_warning("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n", 1558 best->slave ? best->slave->dev->master->name : "NULL"); 1559 } 1560 1561 best->is_active = 1; 1562 pr_debug("LAG %d chosen as the active LAG\n", 1563 best->aggregator_identifier); 1564 pr_debug("Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n", 1565 best->aggregator_identifier, best->num_of_ports, 1566 best->actor_oper_aggregator_key, 1567 best->partner_oper_aggregator_key, 1568 best->is_individual, best->is_active); 1569 1570 // disable the ports that were related to the former active_aggregator 1571 if (active) { 1572 for (port = active->lag_ports; port; 1573 port = port->next_port_in_aggregator) { 1574 __disable_port(port); 1575 } 1576 } 1577 } 1578 1579 /* 1580 * if the selected aggregator is of join individuals 1581 * (partner_system is NULL), enable their ports 1582 */ 1583 active = __get_active_agg(origin); 1584 1585 if (active) { 1586 if (!__agg_has_partner(active)) { 1587 for (port = active->lag_ports; port; 1588 port = port->next_port_in_aggregator) { 1589 __enable_port(port); 1590 } 1591 } 1592 } 1593 1594 if (origin->slave) { 1595 struct bonding *bond; 1596 1597 bond = bond_get_bond_by_slave(origin->slave); 1598 if (bond) 1599 bond_3ad_set_carrier(bond); 1600 } 1601} 1602 1603/** 1604 * ad_clear_agg - clear a given aggregator's parameters 1605 * @aggregator: the aggregator we're looking at 1606 * 1607 */ 1608static void ad_clear_agg(struct aggregator *aggregator) 1609{ 1610 if (aggregator) { 1611 aggregator->is_individual = false; 1612 aggregator->actor_admin_aggregator_key = 0; 1613 aggregator->actor_oper_aggregator_key = 0; 1614 aggregator->partner_system = null_mac_addr; 1615 aggregator->partner_system_priority = 0; 1616 aggregator->partner_oper_aggregator_key = 0; 1617 aggregator->receive_state = 0; 1618 aggregator->transmit_state = 0; 1619 aggregator->lag_ports = NULL; 1620 aggregator->is_active = 0; 1621 aggregator->num_of_ports = 0; 1622 pr_debug("LAG %d was cleared\n", 1623 aggregator->aggregator_identifier); 1624 } 1625} 1626 1627/** 1628 * ad_initialize_agg - initialize a given aggregator's parameters 1629 * @aggregator: the aggregator we're looking at 1630 * 1631 */ 1632static void ad_initialize_agg(struct aggregator *aggregator) 1633{ 1634 if (aggregator) { 1635 ad_clear_agg(aggregator); 1636 1637 aggregator->aggregator_mac_address = null_mac_addr; 1638 aggregator->aggregator_identifier = 0; 1639 aggregator->slave = NULL; 1640 } 1641} 1642 1643/** 1644 * ad_initialize_port - initialize a given port's parameters 1645 * @aggregator: the aggregator we're looking at 1646 * @lacp_fast: boolean. whether fast periodic should be used 1647 * 1648 */ 1649static void ad_initialize_port(struct port *port, int lacp_fast) 1650{ 1651 static const struct port_params tmpl = { 1652 .system_priority = 0xffff, 1653 .key = 1, 1654 .port_number = 1, 1655 .port_priority = 0xff, 1656 .port_state = 1, 1657 }; 1658 static const struct lacpdu lacpdu = { 1659 .subtype = 0x01, 1660 .version_number = 0x01, 1661 .tlv_type_actor_info = 0x01, 1662 .actor_information_length = 0x14, 1663 .tlv_type_partner_info = 0x02, 1664 .partner_information_length = 0x14, 1665 .tlv_type_collector_info = 0x03, 1666 .collector_information_length = 0x10, 1667 .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY), 1668 }; 1669 1670 if (port) { 1671 port->actor_port_number = 1; 1672 port->actor_port_priority = 0xff; 1673 port->actor_system = null_mac_addr; 1674 port->actor_system_priority = 0xffff; 1675 port->actor_port_aggregator_identifier = 0; 1676 port->ntt = false; 1677 port->actor_admin_port_key = 1; 1678 port->actor_oper_port_key = 1; 1679 port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY; 1680 port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY; 1681 1682 if (lacp_fast) { 1683 port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT; 1684 } 1685 1686 memcpy(&port->partner_admin, &tmpl, sizeof(tmpl)); 1687 memcpy(&port->partner_oper, &tmpl, sizeof(tmpl)); 1688 1689 port->is_enabled = true; 1690 // ****** private parameters ****** 1691 port->sm_vars = 0x3; 1692 port->sm_rx_state = 0; 1693 port->sm_rx_timer_counter = 0; 1694 port->sm_periodic_state = 0; 1695 port->sm_periodic_timer_counter = 0; 1696 port->sm_mux_state = 0; 1697 port->sm_mux_timer_counter = 0; 1698 port->sm_tx_state = 0; 1699 port->sm_tx_timer_counter = 0; 1700 port->slave = NULL; 1701 port->aggregator = NULL; 1702 port->next_port_in_aggregator = NULL; 1703 port->transaction_id = 0; 1704 1705 memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu)); 1706 } 1707} 1708 1709/** 1710 * ad_enable_collecting_distributing - enable a port's transmit/receive 1711 * @port: the port we're looking at 1712 * 1713 * Enable @port if it's in an active aggregator 1714 */ 1715static void ad_enable_collecting_distributing(struct port *port) 1716{ 1717 if (port->aggregator->is_active) { 1718 pr_debug("Enabling port %d(LAG %d)\n", 1719 port->actor_port_number, 1720 port->aggregator->aggregator_identifier); 1721 __enable_port(port); 1722 } 1723} 1724 1725/** 1726 * ad_disable_collecting_distributing - disable a port's transmit/receive 1727 * @port: the port we're looking at 1728 * 1729 */ 1730static void ad_disable_collecting_distributing(struct port *port) 1731{ 1732 if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) { 1733 pr_debug("Disabling port %d(LAG %d)\n", 1734 port->actor_port_number, 1735 port->aggregator->aggregator_identifier); 1736 __disable_port(port); 1737 } 1738} 1739 1740 1741/** 1742 * ad_marker_info_received - handle receive of a Marker information frame 1743 * @marker_info: Marker info received 1744 * @port: the port we're looking at 1745 * 1746 */ 1747static void ad_marker_info_received(struct bond_marker *marker_info, 1748 struct port *port) 1749{ 1750 struct bond_marker marker; 1751 1752 // copy the received marker data to the response marker 1753 //marker = *marker_info; 1754 memcpy(&marker, marker_info, sizeof(struct bond_marker)); 1755 // change the marker subtype to marker response 1756 marker.tlv_type=AD_MARKER_RESPONSE_SUBTYPE; 1757 // send the marker response 1758 1759 if (ad_marker_send(port, &marker) >= 0) { 1760 pr_debug("Sent Marker Response on port %d\n", 1761 port->actor_port_number); 1762 } 1763} 1764 1765/** 1766 * ad_marker_response_received - handle receive of a marker response frame 1767 * @marker: marker PDU received 1768 * @port: the port we're looking at 1769 * 1770 * This function does nothing since we decided not to implement send and handle 1771 * response for marker PDU's, in this stage, but only to respond to marker 1772 * information. 1773 */ 1774static void ad_marker_response_received(struct bond_marker *marker, 1775 struct port *port) 1776{ 1777 marker=NULL; // just to satisfy the compiler 1778 port=NULL; // just to satisfy the compiler 1779 // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW 1780} 1781 1782////////////////////////////////////////////////////////////////////////////////////// 1783// ================= AD exported functions to the main bonding code ================== 1784////////////////////////////////////////////////////////////////////////////////////// 1785 1786// Check aggregators status in team every T seconds 1787#define AD_AGGREGATOR_SELECTION_TIMER 8 1788 1789/* 1790 * bond_3ad_initiate_agg_selection(struct bonding *bond) 1791 * 1792 * Set the aggregation selection timer, to initiate an agg selection in 1793 * the very near future. Called during first initialization, and during 1794 * any down to up transitions of the bond. 1795 */ 1796void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout) 1797{ 1798 BOND_AD_INFO(bond).agg_select_timer = timeout; 1799 BOND_AD_INFO(bond).agg_select_mode = bond->params.ad_select; 1800} 1801 1802static u16 aggregator_identifier; 1803 1804/** 1805 * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures 1806 * @bond: bonding struct to work on 1807 * @tick_resolution: tick duration (millisecond resolution) 1808 * @lacp_fast: boolean. whether fast periodic should be used 1809 * 1810 * Can be called only after the mac address of the bond is set. 1811 */ 1812void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution, int lacp_fast) 1813{ 1814 // check that the bond is not initialized yet 1815 if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr), 1816 bond->dev->dev_addr)) { 1817 1818 aggregator_identifier = 0; 1819 1820 BOND_AD_INFO(bond).lacp_fast = lacp_fast; 1821 BOND_AD_INFO(bond).system.sys_priority = 0xFFFF; 1822 BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr); 1823 1824 // initialize how many times this module is called in one second(should be about every 100ms) 1825 ad_ticks_per_sec = tick_resolution; 1826 1827 bond_3ad_initiate_agg_selection(bond, 1828 AD_AGGREGATOR_SELECTION_TIMER * 1829 ad_ticks_per_sec); 1830 } 1831} 1832 1833/** 1834 * bond_3ad_bind_slave - initialize a slave's port 1835 * @slave: slave struct to work on 1836 * 1837 * Returns: 0 on success 1838 * < 0 on error 1839 */ 1840int bond_3ad_bind_slave(struct slave *slave) 1841{ 1842 struct bonding *bond = bond_get_bond_by_slave(slave); 1843 struct port *port; 1844 struct aggregator *aggregator; 1845 1846 if (bond == NULL) { 1847 pr_err("%s: The slave %s is not attached to its bond\n", 1848 slave->dev->master->name, slave->dev->name); 1849 return -1; 1850 } 1851 1852 //check that the slave has not been intialized yet. 1853 if (SLAVE_AD_INFO(slave).port.slave != slave) { 1854 1855 // port initialization 1856 port = &(SLAVE_AD_INFO(slave).port); 1857 1858 ad_initialize_port(port, BOND_AD_INFO(bond).lacp_fast); 1859 1860 port->slave = slave; 1861 port->actor_port_number = SLAVE_AD_INFO(slave).id; 1862 // key is determined according to the link speed, duplex and user key(which is yet not supported) 1863 // ------------------------------------------------------------ 1864 // Port key : | User key | Speed |Duplex| 1865 // ------------------------------------------------------------ 1866 // 16 6 1 0 1867 port->actor_admin_port_key = 0; // initialize this parameter 1868 port->actor_admin_port_key |= __get_duplex(port); 1869 port->actor_admin_port_key |= (__get_link_speed(port) << 1); 1870 port->actor_oper_port_key = port->actor_admin_port_key; 1871 // if the port is not full duplex, then the port should be not lacp Enabled 1872 if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) { 1873 port->sm_vars &= ~AD_PORT_LACP_ENABLED; 1874 } 1875 // actor system is the bond's system 1876 port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr; 1877 // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second) 1878 port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND; 1879 port->aggregator = NULL; 1880 port->next_port_in_aggregator = NULL; 1881 1882 __disable_port(port); 1883 __initialize_port_locks(port); 1884 1885 1886 // aggregator initialization 1887 aggregator = &(SLAVE_AD_INFO(slave).aggregator); 1888 1889 ad_initialize_agg(aggregator); 1890 1891 aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr); 1892 aggregator->aggregator_identifier = (++aggregator_identifier); 1893 aggregator->slave = slave; 1894 aggregator->is_active = 0; 1895 aggregator->num_of_ports = 0; 1896 } 1897 1898 return 0; 1899} 1900 1901/** 1902 * bond_3ad_unbind_slave - deinitialize a slave's port 1903 * @slave: slave struct to work on 1904 * 1905 * Search for the aggregator that is related to this port, remove the 1906 * aggregator and assign another aggregator for other port related to it 1907 * (if any), and remove the port. 1908 */ 1909void bond_3ad_unbind_slave(struct slave *slave) 1910{ 1911 struct port *port, *prev_port, *temp_port; 1912 struct aggregator *aggregator, *new_aggregator, *temp_aggregator; 1913 int select_new_active_agg = 0; 1914 1915 // find the aggregator related to this slave 1916 aggregator = &(SLAVE_AD_INFO(slave).aggregator); 1917 1918 // find the port related to this slave 1919 port = &(SLAVE_AD_INFO(slave).port); 1920 1921 // if slave is null, the whole port is not initialized 1922 if (!port->slave) { 1923 pr_warning("Warning: %s: Trying to unbind an uninitialized port on %s\n", 1924 slave->dev->master->name, slave->dev->name); 1925 return; 1926 } 1927 1928 pr_debug("Unbinding Link Aggregation Group %d\n", 1929 aggregator->aggregator_identifier); 1930 1931 /* Tell the partner that this port is not suitable for aggregation */ 1932 port->actor_oper_port_state &= ~AD_STATE_AGGREGATION; 1933 __update_lacpdu_from_port(port); 1934 ad_lacpdu_send(port); 1935 1936 // check if this aggregator is occupied 1937 if (aggregator->lag_ports) { 1938 // check if there are other ports related to this aggregator except 1939 // the port related to this slave(thats ensure us that there is a 1940 // reason to search for new aggregator, and that we will find one 1941 if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) { 1942 // find new aggregator for the related port(s) 1943 new_aggregator = __get_first_agg(port); 1944 for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) { 1945 // if the new aggregator is empty, or it is connected to our port only 1946 if (!new_aggregator->lag_ports || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator)) { 1947 break; 1948 } 1949 } 1950 // if new aggregator found, copy the aggregator's parameters 1951 // and connect the related lag_ports to the new aggregator 1952 if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) { 1953 pr_debug("Some port(s) related to LAG %d - replaceing with LAG %d\n", 1954 aggregator->aggregator_identifier, 1955 new_aggregator->aggregator_identifier); 1956 1957 if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) { 1958 pr_info("%s: Removing an active aggregator\n", 1959 aggregator->slave->dev->master->name); 1960 // select new active aggregator 1961 select_new_active_agg = 1; 1962 } 1963 1964 new_aggregator->is_individual = aggregator->is_individual; 1965 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key; 1966 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key; 1967 new_aggregator->partner_system = aggregator->partner_system; 1968 new_aggregator->partner_system_priority = aggregator->partner_system_priority; 1969 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key; 1970 new_aggregator->receive_state = aggregator->receive_state; 1971 new_aggregator->transmit_state = aggregator->transmit_state; 1972 new_aggregator->lag_ports = aggregator->lag_ports; 1973 new_aggregator->is_active = aggregator->is_active; 1974 new_aggregator->num_of_ports = aggregator->num_of_ports; 1975 1976 // update the information that is written on the ports about the aggregator 1977 for (temp_port=aggregator->lag_ports; temp_port; temp_port=temp_port->next_port_in_aggregator) { 1978 temp_port->aggregator=new_aggregator; 1979 temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier; 1980 } 1981 1982 // clear the aggregator 1983 ad_clear_agg(aggregator); 1984 1985 if (select_new_active_agg) { 1986 ad_agg_selection_logic(__get_first_agg(port)); 1987 } 1988 } else { 1989 pr_warning("%s: Warning: unbinding aggregator, and could not find a new aggregator for its ports\n", 1990 slave->dev->master->name); 1991 } 1992 } else { // in case that the only port related to this aggregator is the one we want to remove 1993 select_new_active_agg = aggregator->is_active; 1994 // clear the aggregator 1995 ad_clear_agg(aggregator); 1996 if (select_new_active_agg) { 1997 pr_info("%s: Removing an active aggregator\n", 1998 slave->dev->master->name); 1999 // select new active aggregator 2000 ad_agg_selection_logic(__get_first_agg(port)); 2001 } 2002 } 2003 } 2004 2005 pr_debug("Unbinding port %d\n", port->actor_port_number); 2006 // find the aggregator that this port is connected to 2007 temp_aggregator = __get_first_agg(port); 2008 for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) { 2009 prev_port = NULL; 2010 // search the port in the aggregator's related ports 2011 for (temp_port=temp_aggregator->lag_ports; temp_port; prev_port=temp_port, temp_port=temp_port->next_port_in_aggregator) { 2012 if (temp_port == port) { // the aggregator found - detach the port from this aggregator 2013 if (prev_port) { 2014 prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator; 2015 } else { 2016 temp_aggregator->lag_ports = temp_port->next_port_in_aggregator; 2017 } 2018 temp_aggregator->num_of_ports--; 2019 if (temp_aggregator->num_of_ports==0) { 2020 select_new_active_agg = temp_aggregator->is_active; 2021 // clear the aggregator 2022 ad_clear_agg(temp_aggregator); 2023 if (select_new_active_agg) { 2024 pr_info("%s: Removing an active aggregator\n", 2025 slave->dev->master->name); 2026 // select new active aggregator 2027 ad_agg_selection_logic(__get_first_agg(port)); 2028 } 2029 } 2030 break; 2031 } 2032 } 2033 } 2034 port->slave=NULL; 2035} 2036 2037/** 2038 * bond_3ad_state_machine_handler - handle state machines timeout 2039 * @bond: bonding struct to work on 2040 * 2041 * The state machine handling concept in this module is to check every tick 2042 * which state machine should operate any function. The execution order is 2043 * round robin, so when we have an interaction between state machines, the 2044 * reply of one to each other might be delayed until next tick. 2045 * 2046 * This function also complete the initialization when the agg_select_timer 2047 * times out, and it selects an aggregator for the ports that are yet not 2048 * related to any aggregator, and selects the active aggregator for a bond. 2049 */ 2050void bond_3ad_state_machine_handler(struct work_struct *work) 2051{ 2052 struct bonding *bond = container_of(work, struct bonding, 2053 ad_work.work); 2054 struct port *port; 2055 struct aggregator *aggregator; 2056 2057 read_lock(&bond->lock); 2058 2059 if (bond->kill_timers) { 2060 goto out; 2061 } 2062 2063 //check if there are any slaves 2064 if (bond->slave_cnt == 0) { 2065 goto re_arm; 2066 } 2067 2068 // check if agg_select_timer timer after initialize is timed out 2069 if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) { 2070 // select the active aggregator for the bond 2071 if ((port = __get_first_port(bond))) { 2072 if (!port->slave) { 2073 pr_warning("%s: Warning: bond's first port is uninitialized\n", 2074 bond->dev->name); 2075 goto re_arm; 2076 } 2077 2078 aggregator = __get_first_agg(port); 2079 ad_agg_selection_logic(aggregator); 2080 } 2081 bond_3ad_set_carrier(bond); 2082 } 2083 2084 // for each port run the state machines 2085 for (port = __get_first_port(bond); port; port = __get_next_port(port)) { 2086 if (!port->slave) { 2087 pr_warning("%s: Warning: Found an uninitialized port\n", 2088 bond->dev->name); 2089 goto re_arm; 2090 } 2091 2092 ad_rx_machine(NULL, port); 2093 ad_periodic_machine(port); 2094 ad_port_selection_logic(port); 2095 ad_mux_machine(port); 2096 ad_tx_machine(port); 2097 2098 // turn off the BEGIN bit, since we already handled it 2099 if (port->sm_vars & AD_PORT_BEGIN) { 2100 port->sm_vars &= ~AD_PORT_BEGIN; 2101 } 2102 } 2103 2104re_arm: 2105 queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks); 2106out: 2107 read_unlock(&bond->lock); 2108} 2109 2110/** 2111 * bond_3ad_rx_indication - handle a received frame 2112 * @lacpdu: received lacpdu 2113 * @slave: slave struct to work on 2114 * @length: length of the data received 2115 * 2116 * It is assumed that frames that were sent on this NIC don't returned as new 2117 * received frames (loopback). Since only the payload is given to this 2118 * function, it check for loopback. 2119 */ 2120static void bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length) 2121{ 2122 struct port *port; 2123 2124 if (length >= sizeof(struct lacpdu)) { 2125 2126 port = &(SLAVE_AD_INFO(slave).port); 2127 2128 if (!port->slave) { 2129 pr_warning("%s: Warning: port of slave %s is uninitialized\n", 2130 slave->dev->name, slave->dev->master->name); 2131 return; 2132 } 2133 2134 switch (lacpdu->subtype) { 2135 case AD_TYPE_LACPDU: 2136 pr_debug("Received LACPDU on port %d\n", 2137 port->actor_port_number); 2138 ad_rx_machine(lacpdu, port); 2139 break; 2140 2141 case AD_TYPE_MARKER: 2142 // No need to convert fields to Little Endian since we don't use the marker's fields. 2143 2144 switch (((struct bond_marker *)lacpdu)->tlv_type) { 2145 case AD_MARKER_INFORMATION_SUBTYPE: 2146 pr_debug("Received Marker Information on port %d\n", 2147 port->actor_port_number); 2148 ad_marker_info_received((struct bond_marker *)lacpdu, port); 2149 break; 2150 2151 case AD_MARKER_RESPONSE_SUBTYPE: 2152 pr_debug("Received Marker Response on port %d\n", 2153 port->actor_port_number); 2154 ad_marker_response_received((struct bond_marker *)lacpdu, port); 2155 break; 2156 2157 default: 2158 pr_debug("Received an unknown Marker subtype on slot %d\n", 2159 port->actor_port_number); 2160 } 2161 } 2162 } 2163} 2164 2165/** 2166 * bond_3ad_adapter_speed_changed - handle a slave's speed change indication 2167 * @slave: slave struct to work on 2168 * 2169 * Handle reselection of aggregator (if needed) for this port. 2170 */ 2171void bond_3ad_adapter_speed_changed(struct slave *slave) 2172{ 2173 struct port *port; 2174 2175 port = &(SLAVE_AD_INFO(slave).port); 2176 2177 // if slave is null, the whole port is not initialized 2178 if (!port->slave) { 2179 pr_warning("Warning: %s: speed changed for uninitialized port on %s\n", 2180 slave->dev->master->name, slave->dev->name); 2181 return; 2182 } 2183 2184 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS; 2185 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1); 2186 pr_debug("Port %d changed speed\n", port->actor_port_number); 2187 // there is no need to reselect a new aggregator, just signal the 2188 // state machines to reinitialize 2189 port->sm_vars |= AD_PORT_BEGIN; 2190} 2191 2192/** 2193 * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication 2194 * @slave: slave struct to work on 2195 * 2196 * Handle reselection of aggregator (if needed) for this port. 2197 */ 2198void bond_3ad_adapter_duplex_changed(struct slave *slave) 2199{ 2200 struct port *port; 2201 2202 port=&(SLAVE_AD_INFO(slave).port); 2203 2204 // if slave is null, the whole port is not initialized 2205 if (!port->slave) { 2206 pr_warning("%s: Warning: duplex changed for uninitialized port on %s\n", 2207 slave->dev->master->name, slave->dev->name); 2208 return; 2209 } 2210 2211 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS; 2212 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port); 2213 pr_debug("Port %d changed duplex\n", port->actor_port_number); 2214 // there is no need to reselect a new aggregator, just signal the 2215 // state machines to reinitialize 2216 port->sm_vars |= AD_PORT_BEGIN; 2217} 2218 2219/** 2220 * bond_3ad_handle_link_change - handle a slave's link status change indication 2221 * @slave: slave struct to work on 2222 * @status: whether the link is now up or down 2223 * 2224 * Handle reselection of aggregator (if needed) for this port. 2225 */ 2226void bond_3ad_handle_link_change(struct slave *slave, char link) 2227{ 2228 struct port *port; 2229 2230 port = &(SLAVE_AD_INFO(slave).port); 2231 2232 // if slave is null, the whole port is not initialized 2233 if (!port->slave) { 2234 pr_warning("Warning: %s: link status changed for uninitialized port on %s\n", 2235 slave->dev->master->name, slave->dev->name); 2236 return; 2237 } 2238 2239 // on link down we are zeroing duplex and speed since some of the adaptors(ce1000.lan) report full duplex/speed instead of N/A(duplex) / 0(speed) 2240 // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report 2241 if (link == BOND_LINK_UP) { 2242 port->is_enabled = true; 2243 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS; 2244 port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port); 2245 port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS; 2246 port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1); 2247 } else { 2248 /* link has failed */ 2249 port->is_enabled = false; 2250 port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS; 2251 port->actor_oper_port_key= (port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS); 2252 } 2253 //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN"))); 2254 // there is no need to reselect a new aggregator, just signal the 2255 // state machines to reinitialize 2256 port->sm_vars |= AD_PORT_BEGIN; 2257} 2258 2259/* 2260 * set link state for bonding master: if we have an active 2261 * aggregator, we're up, if not, we're down. Presumes that we cannot 2262 * have an active aggregator if there are no slaves with link up. 2263 * 2264 * This behavior complies with IEEE 802.3 section 43.3.9. 2265 * 2266 * Called by bond_set_carrier(). Return zero if carrier state does not 2267 * change, nonzero if it does. 2268 */ 2269int bond_3ad_set_carrier(struct bonding *bond) 2270{ 2271 if (__get_active_agg(&(SLAVE_AD_INFO(bond->first_slave).aggregator))) { 2272 if (!netif_carrier_ok(bond->dev)) { 2273 netif_carrier_on(bond->dev); 2274 return 1; 2275 } 2276 return 0; 2277 } 2278 2279 if (netif_carrier_ok(bond->dev)) { 2280 netif_carrier_off(bond->dev); 2281 return 1; 2282 } 2283 return 0; 2284} 2285 2286/** 2287 * bond_3ad_get_active_agg_info - get information of the active aggregator 2288 * @bond: bonding struct to work on 2289 * @ad_info: ad_info struct to fill with the bond's info 2290 * 2291 * Returns: 0 on success 2292 * < 0 on error 2293 */ 2294int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info) 2295{ 2296 struct aggregator *aggregator = NULL; 2297 struct port *port; 2298 2299 for (port = __get_first_port(bond); port; port = __get_next_port(port)) { 2300 if (port->aggregator && port->aggregator->is_active) { 2301 aggregator = port->aggregator; 2302 break; 2303 } 2304 } 2305 2306 if (aggregator) { 2307 ad_info->aggregator_id = aggregator->aggregator_identifier; 2308 ad_info->ports = aggregator->num_of_ports; 2309 ad_info->actor_key = aggregator->actor_oper_aggregator_key; 2310 ad_info->partner_key = aggregator->partner_oper_aggregator_key; 2311 memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN); 2312 return 0; 2313 } 2314 2315 return -1; 2316} 2317 2318int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev) 2319{ 2320 struct slave *slave, *start_at; 2321 struct bonding *bond = netdev_priv(dev); 2322 int slave_agg_no; 2323 int slaves_in_agg; 2324 int agg_id; 2325 int i; 2326 struct ad_info ad_info; 2327 int res = 1; 2328 2329 /* make sure that the slaves list will 2330 * not change during tx 2331 */ 2332 read_lock(&bond->lock); 2333 2334 if (!BOND_IS_OK(bond)) { 2335 goto out; 2336 } 2337 2338 if (bond_3ad_get_active_agg_info(bond, &ad_info)) { 2339 pr_debug("%s: Error: bond_3ad_get_active_agg_info failed\n", 2340 dev->name); 2341 goto out; 2342 } 2343 2344 slaves_in_agg = ad_info.ports; 2345 agg_id = ad_info.aggregator_id; 2346 2347 if (slaves_in_agg == 0) { 2348 /*the aggregator is empty*/ 2349 pr_debug("%s: Error: active aggregator is empty\n", dev->name); 2350 goto out; 2351 } 2352 2353 slave_agg_no = bond->xmit_hash_policy(skb, slaves_in_agg); 2354 2355 bond_for_each_slave(bond, slave, i) { 2356 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator; 2357 2358 if (agg && (agg->aggregator_identifier == agg_id)) { 2359 slave_agg_no--; 2360 if (slave_agg_no < 0) { 2361 break; 2362 } 2363 } 2364 } 2365 2366 if (slave_agg_no >= 0) { 2367 pr_err("%s: Error: Couldn't find a slave to tx on for aggregator ID %d\n", 2368 dev->name, agg_id); 2369 goto out; 2370 } 2371 2372 start_at = slave; 2373 2374 bond_for_each_slave_from(bond, slave, i, start_at) { 2375 int slave_agg_id = 0; 2376 struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator; 2377 2378 if (agg) { 2379 slave_agg_id = agg->aggregator_identifier; 2380 } 2381 2382 if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) { 2383 res = bond_dev_queue_xmit(bond, skb, slave->dev); 2384 break; 2385 } 2386 } 2387 2388out: 2389 if (res) { 2390 /* no suitable interface, frame not sent */ 2391 dev_kfree_skb(skb); 2392 } 2393 read_unlock(&bond->lock); 2394 return NETDEV_TX_OK; 2395} 2396 2397int bond_3ad_lacpdu_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type* ptype, struct net_device *orig_dev) 2398{ 2399 struct bonding *bond = netdev_priv(dev); 2400 struct slave *slave = NULL; 2401 int ret = NET_RX_DROP; 2402 2403 if (!(dev->flags & IFF_MASTER)) 2404 goto out; 2405 2406 if (!pskb_may_pull(skb, sizeof(struct lacpdu))) 2407 goto out; 2408 2409 read_lock(&bond->lock); 2410 slave = bond_get_slave_by_dev((struct bonding *)netdev_priv(dev), 2411 orig_dev); 2412 if (!slave) 2413 goto out_unlock; 2414 2415 bond_3ad_rx_indication((struct lacpdu *) skb->data, slave, skb->len); 2416 2417 ret = NET_RX_SUCCESS; 2418 2419out_unlock: 2420 read_unlock(&bond->lock); 2421out: 2422 dev_kfree_skb(skb); 2423 2424 return ret; 2425} 2426