Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
0004  */
0005 
0006 #include <linux/skbuff.h>
0007 #include <linux/if_ether.h>
0008 #include <linux/netdevice.h>
0009 #include <linux/spinlock.h>
0010 #include <linux/ethtool.h>
0011 #include <linux/etherdevice.h>
0012 #include <linux/if_bonding.h>
0013 #include <linux/pkt_sched.h>
0014 #include <net/net_namespace.h>
0015 #include <net/bonding.h>
0016 #include <net/bond_3ad.h>
0017 #include <net/netlink.h>
0018 
0019 /* General definitions */
0020 #define AD_SHORT_TIMEOUT           1
0021 #define AD_LONG_TIMEOUT            0
0022 #define AD_STANDBY                 0x2
0023 #define AD_MAX_TX_IN_SECOND        3
0024 #define AD_COLLECTOR_MAX_DELAY     0
0025 
0026 /* Timer definitions (43.4.4 in the 802.3ad standard) */
0027 #define AD_FAST_PERIODIC_TIME      1
0028 #define AD_SLOW_PERIODIC_TIME      30
0029 #define AD_SHORT_TIMEOUT_TIME      (3*AD_FAST_PERIODIC_TIME)
0030 #define AD_LONG_TIMEOUT_TIME       (3*AD_SLOW_PERIODIC_TIME)
0031 #define AD_CHURN_DETECTION_TIME    60
0032 #define AD_AGGREGATE_WAIT_TIME     2
0033 
0034 /* Port Variables definitions used by the State Machines (43.4.7 in the
0035  * 802.3ad standard)
0036  */
0037 #define AD_PORT_BEGIN           0x1
0038 #define AD_PORT_LACP_ENABLED    0x2
0039 #define AD_PORT_ACTOR_CHURN     0x4
0040 #define AD_PORT_PARTNER_CHURN   0x8
0041 #define AD_PORT_READY           0x10
0042 #define AD_PORT_READY_N         0x20
0043 #define AD_PORT_MATCHED         0x40
0044 #define AD_PORT_STANDBY         0x80
0045 #define AD_PORT_SELECTED        0x100
0046 #define AD_PORT_MOVED           0x200
0047 #define AD_PORT_CHURNED         (AD_PORT_ACTOR_CHURN | AD_PORT_PARTNER_CHURN)
0048 
0049 /* Port Key definitions
0050  * key is determined according to the link speed, duplex and
0051  * user key (which is yet not supported)
0052  *           --------------------------------------------------------------
0053  * Port key  | User key (10 bits)           | Speed (5 bits)      | Duplex|
0054  *           --------------------------------------------------------------
0055  *           |15                           6|5                   1|0
0056  */
0057 #define  AD_DUPLEX_KEY_MASKS    0x1
0058 #define  AD_SPEED_KEY_MASKS     0x3E
0059 #define  AD_USER_KEY_MASKS      0xFFC0
0060 
0061 enum ad_link_speed_type {
0062     AD_LINK_SPEED_1MBPS = 1,
0063     AD_LINK_SPEED_10MBPS,
0064     AD_LINK_SPEED_100MBPS,
0065     AD_LINK_SPEED_1000MBPS,
0066     AD_LINK_SPEED_2500MBPS,
0067     AD_LINK_SPEED_5000MBPS,
0068     AD_LINK_SPEED_10000MBPS,
0069     AD_LINK_SPEED_14000MBPS,
0070     AD_LINK_SPEED_20000MBPS,
0071     AD_LINK_SPEED_25000MBPS,
0072     AD_LINK_SPEED_40000MBPS,
0073     AD_LINK_SPEED_50000MBPS,
0074     AD_LINK_SPEED_56000MBPS,
0075     AD_LINK_SPEED_100000MBPS,
0076     AD_LINK_SPEED_200000MBPS,
0077     AD_LINK_SPEED_400000MBPS,
0078 };
0079 
0080 /* compare MAC addresses */
0081 #define MAC_ADDRESS_EQUAL(A, B) \
0082     ether_addr_equal_64bits((const u8 *)A, (const u8 *)B)
0083 
0084 static const u8 null_mac_addr[ETH_ALEN + 2] __long_aligned = {
0085     0, 0, 0, 0, 0, 0
0086 };
0087 
0088 static const u16 ad_ticks_per_sec = 1000 / AD_TIMER_INTERVAL;
0089 static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
0090 
0091 const u8 lacpdu_mcast_addr[ETH_ALEN + 2] __long_aligned = {
0092     0x01, 0x80, 0xC2, 0x00, 0x00, 0x02
0093 };
0094 
0095 /* ================= main 802.3ad protocol functions ================== */
0096 static int ad_lacpdu_send(struct port *port);
0097 static int ad_marker_send(struct port *port, struct bond_marker *marker);
0098 static void ad_mux_machine(struct port *port, bool *update_slave_arr);
0099 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
0100 static void ad_tx_machine(struct port *port);
0101 static void ad_periodic_machine(struct port *port, struct bond_params *bond_params);
0102 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr);
0103 static void ad_agg_selection_logic(struct aggregator *aggregator,
0104                    bool *update_slave_arr);
0105 static void ad_clear_agg(struct aggregator *aggregator);
0106 static void ad_initialize_agg(struct aggregator *aggregator);
0107 static void ad_initialize_port(struct port *port, int lacp_fast);
0108 static void ad_enable_collecting_distributing(struct port *port,
0109                           bool *update_slave_arr);
0110 static void ad_disable_collecting_distributing(struct port *port,
0111                            bool *update_slave_arr);
0112 static void ad_marker_info_received(struct bond_marker *marker_info,
0113                     struct port *port);
0114 static void ad_marker_response_received(struct bond_marker *marker,
0115                     struct port *port);
0116 static void ad_update_actor_keys(struct port *port, bool reset);
0117 
0118 
0119 /* ================= api to bonding and kernel code ================== */
0120 
0121 /**
0122  * __get_bond_by_port - get the port's bonding struct
0123  * @port: the port we're looking at
0124  *
0125  * Return @port's bonding struct, or %NULL if it can't be found.
0126  */
0127 static inline struct bonding *__get_bond_by_port(struct port *port)
0128 {
0129     if (port->slave == NULL)
0130         return NULL;
0131 
0132     return bond_get_bond_by_slave(port->slave);
0133 }
0134 
0135 /**
0136  * __get_first_agg - get the first aggregator in the bond
0137  * @port: the port we're looking at
0138  *
0139  * Return the aggregator of the first slave in @bond, or %NULL if it can't be
0140  * found.
0141  * The caller must hold RCU or RTNL lock.
0142  */
0143 static inline struct aggregator *__get_first_agg(struct port *port)
0144 {
0145     struct bonding *bond = __get_bond_by_port(port);
0146     struct slave *first_slave;
0147     struct aggregator *agg;
0148 
0149     /* If there's no bond for this port, or bond has no slaves */
0150     if (bond == NULL)
0151         return NULL;
0152 
0153     rcu_read_lock();
0154     first_slave = bond_first_slave_rcu(bond);
0155     agg = first_slave ? &(SLAVE_AD_INFO(first_slave)->aggregator) : NULL;
0156     rcu_read_unlock();
0157 
0158     return agg;
0159 }
0160 
0161 /**
0162  * __agg_has_partner - see if we have a partner
0163  * @agg: the agregator we're looking at
0164  *
0165  * Return nonzero if aggregator has a partner (denoted by a non-zero ether
0166  * address for the partner). Return 0 if not.
0167  */
0168 static inline int __agg_has_partner(struct aggregator *agg)
0169 {
0170     return !is_zero_ether_addr(agg->partner_system.mac_addr_value);
0171 }
0172 
0173 /**
0174  * __disable_port - disable the port's slave
0175  * @port: the port we're looking at
0176  */
0177 static inline void __disable_port(struct port *port)
0178 {
0179     bond_set_slave_inactive_flags(port->slave, BOND_SLAVE_NOTIFY_LATER);
0180 }
0181 
0182 /**
0183  * __enable_port - enable the port's slave, if it's up
0184  * @port: the port we're looking at
0185  */
0186 static inline void __enable_port(struct port *port)
0187 {
0188     struct slave *slave = port->slave;
0189 
0190     if ((slave->link == BOND_LINK_UP) && bond_slave_is_up(slave))
0191         bond_set_slave_active_flags(slave, BOND_SLAVE_NOTIFY_LATER);
0192 }
0193 
0194 /**
0195  * __port_is_enabled - check if the port's slave is in active state
0196  * @port: the port we're looking at
0197  */
0198 static inline int __port_is_enabled(struct port *port)
0199 {
0200     return bond_is_active_slave(port->slave);
0201 }
0202 
0203 /**
0204  * __get_agg_selection_mode - get the aggregator selection mode
0205  * @port: the port we're looking at
0206  *
0207  * Get the aggregator selection mode. Can be %STABLE, %BANDWIDTH or %COUNT.
0208  */
0209 static inline u32 __get_agg_selection_mode(struct port *port)
0210 {
0211     struct bonding *bond = __get_bond_by_port(port);
0212 
0213     if (bond == NULL)
0214         return BOND_AD_STABLE;
0215 
0216     return bond->params.ad_select;
0217 }
0218 
0219 /**
0220  * __check_agg_selection_timer - check if the selection timer has expired
0221  * @port: the port we're looking at
0222  */
0223 static inline int __check_agg_selection_timer(struct port *port)
0224 {
0225     struct bonding *bond = __get_bond_by_port(port);
0226 
0227     if (bond == NULL)
0228         return 0;
0229 
0230     return atomic_read(&BOND_AD_INFO(bond).agg_select_timer) ? 1 : 0;
0231 }
0232 
0233 /**
0234  * __get_link_speed - get a port's speed
0235  * @port: the port we're looking at
0236  *
0237  * Return @port's speed in 802.3ad enum format. i.e. one of:
0238  *     0,
0239  *     %AD_LINK_SPEED_10MBPS,
0240  *     %AD_LINK_SPEED_100MBPS,
0241  *     %AD_LINK_SPEED_1000MBPS,
0242  *     %AD_LINK_SPEED_2500MBPS,
0243  *     %AD_LINK_SPEED_5000MBPS,
0244  *     %AD_LINK_SPEED_10000MBPS
0245  *     %AD_LINK_SPEED_14000MBPS,
0246  *     %AD_LINK_SPEED_20000MBPS
0247  *     %AD_LINK_SPEED_25000MBPS
0248  *     %AD_LINK_SPEED_40000MBPS
0249  *     %AD_LINK_SPEED_50000MBPS
0250  *     %AD_LINK_SPEED_56000MBPS
0251  *     %AD_LINK_SPEED_100000MBPS
0252  *     %AD_LINK_SPEED_200000MBPS
0253  *     %AD_LINK_SPEED_400000MBPS
0254  */
0255 static u16 __get_link_speed(struct port *port)
0256 {
0257     struct slave *slave = port->slave;
0258     u16 speed;
0259 
0260     /* this if covers only a special case: when the configuration starts
0261      * with link down, it sets the speed to 0.
0262      * This is done in spite of the fact that the e100 driver reports 0
0263      * to be compatible with MVT in the future.
0264      */
0265     if (slave->link != BOND_LINK_UP)
0266         speed = 0;
0267     else {
0268         switch (slave->speed) {
0269         case SPEED_10:
0270             speed = AD_LINK_SPEED_10MBPS;
0271             break;
0272 
0273         case SPEED_100:
0274             speed = AD_LINK_SPEED_100MBPS;
0275             break;
0276 
0277         case SPEED_1000:
0278             speed = AD_LINK_SPEED_1000MBPS;
0279             break;
0280 
0281         case SPEED_2500:
0282             speed = AD_LINK_SPEED_2500MBPS;
0283             break;
0284 
0285         case SPEED_5000:
0286             speed = AD_LINK_SPEED_5000MBPS;
0287             break;
0288 
0289         case SPEED_10000:
0290             speed = AD_LINK_SPEED_10000MBPS;
0291             break;
0292 
0293         case SPEED_14000:
0294             speed = AD_LINK_SPEED_14000MBPS;
0295             break;
0296 
0297         case SPEED_20000:
0298             speed = AD_LINK_SPEED_20000MBPS;
0299             break;
0300 
0301         case SPEED_25000:
0302             speed = AD_LINK_SPEED_25000MBPS;
0303             break;
0304 
0305         case SPEED_40000:
0306             speed = AD_LINK_SPEED_40000MBPS;
0307             break;
0308 
0309         case SPEED_50000:
0310             speed = AD_LINK_SPEED_50000MBPS;
0311             break;
0312 
0313         case SPEED_56000:
0314             speed = AD_LINK_SPEED_56000MBPS;
0315             break;
0316 
0317         case SPEED_100000:
0318             speed = AD_LINK_SPEED_100000MBPS;
0319             break;
0320 
0321         case SPEED_200000:
0322             speed = AD_LINK_SPEED_200000MBPS;
0323             break;
0324 
0325         case SPEED_400000:
0326             speed = AD_LINK_SPEED_400000MBPS;
0327             break;
0328 
0329         default:
0330             /* unknown speed value from ethtool. shouldn't happen */
0331             if (slave->speed != SPEED_UNKNOWN)
0332                 pr_err_once("%s: (slave %s): unknown ethtool speed (%d) for port %d (set it to 0)\n",
0333                         slave->bond->dev->name,
0334                         slave->dev->name, slave->speed,
0335                         port->actor_port_number);
0336             speed = 0;
0337             break;
0338         }
0339     }
0340 
0341     slave_dbg(slave->bond->dev, slave->dev, "Port %d Received link speed %d update from adapter\n",
0342           port->actor_port_number, speed);
0343     return speed;
0344 }
0345 
0346 /**
0347  * __get_duplex - get a port's duplex
0348  * @port: the port we're looking at
0349  *
0350  * Return @port's duplex in 802.3ad bitmask format. i.e.:
0351  *     0x01 if in full duplex
0352  *     0x00 otherwise
0353  */
0354 static u8 __get_duplex(struct port *port)
0355 {
0356     struct slave *slave = port->slave;
0357     u8 retval = 0x0;
0358 
0359     /* handling a special case: when the configuration starts with
0360      * link down, it sets the duplex to 0.
0361      */
0362     if (slave->link == BOND_LINK_UP) {
0363         switch (slave->duplex) {
0364         case DUPLEX_FULL:
0365             retval = 0x1;
0366             slave_dbg(slave->bond->dev, slave->dev, "Port %d Received status full duplex update from adapter\n",
0367                   port->actor_port_number);
0368             break;
0369         case DUPLEX_HALF:
0370         default:
0371             retval = 0x0;
0372             slave_dbg(slave->bond->dev, slave->dev, "Port %d Received status NOT full duplex update from adapter\n",
0373                   port->actor_port_number);
0374             break;
0375         }
0376     }
0377     return retval;
0378 }
0379 
0380 static void __ad_actor_update_port(struct port *port)
0381 {
0382     const struct bonding *bond = bond_get_bond_by_slave(port->slave);
0383 
0384     port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
0385     port->actor_system_priority = BOND_AD_INFO(bond).system.sys_priority;
0386 }
0387 
0388 /* Conversions */
0389 
0390 /**
0391  * __ad_timer_to_ticks - convert a given timer type to AD module ticks
0392  * @timer_type: which timer to operate
0393  * @par: timer parameter. see below
0394  *
0395  * If @timer_type is %current_while_timer, @par indicates long/short timer.
0396  * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
0397  *                           %SLOW_PERIODIC_TIME.
0398  */
0399 static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
0400 {
0401     u16 retval = 0; /* to silence the compiler */
0402 
0403     switch (timer_type) {
0404     case AD_CURRENT_WHILE_TIMER:    /* for rx machine usage */
0405         if (par)
0406             retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec);
0407         else
0408             retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec);
0409         break;
0410     case AD_ACTOR_CHURN_TIMER:  /* for local churn machine */
0411         retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
0412         break;
0413     case AD_PERIODIC_TIMER:     /* for periodic machine */
0414         retval = (par*ad_ticks_per_sec); /* long timeout */
0415         break;
0416     case AD_PARTNER_CHURN_TIMER:    /* for remote churn machine */
0417         retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
0418         break;
0419     case AD_WAIT_WHILE_TIMER:   /* for selection machine */
0420         retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
0421         break;
0422     }
0423 
0424     return retval;
0425 }
0426 
0427 
0428 /* ================= ad_rx_machine helper functions ================== */
0429 
0430 /**
0431  * __choose_matched - update a port's matched variable from a received lacpdu
0432  * @lacpdu: the lacpdu we've received
0433  * @port: the port we're looking at
0434  *
0435  * Update the value of the matched variable, using parameter values from a
0436  * newly received lacpdu. Parameter values for the partner carried in the
0437  * received PDU are compared with the corresponding operational parameter
0438  * values for the actor. Matched is set to TRUE if all of these parameters
0439  * match and the PDU parameter partner_state.aggregation has the same value as
0440  * actor_oper_port_state.aggregation and lacp will actively maintain the link
0441  * in the aggregation. Matched is also set to TRUE if the value of
0442  * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
0443  * an individual link and lacp will actively maintain the link. Otherwise,
0444  * matched is set to FALSE. LACP is considered to be actively maintaining the
0445  * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
0446  * the actor's actor_oper_port_state.lacp_activity and the PDU's
0447  * partner_state.lacp_activity variables are TRUE.
0448  *
0449  * Note: the AD_PORT_MATCHED "variable" is not specified by 802.3ad; it is
0450  * used here to implement the language from 802.3ad 43.4.9 that requires
0451  * recordPDU to "match" the LACPDU parameters to the stored values.
0452  */
0453 static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
0454 {
0455     /* check if all parameters are alike
0456      * or this is individual link(aggregation == FALSE)
0457      * then update the state machine Matched variable.
0458      */
0459     if (((ntohs(lacpdu->partner_port) == port->actor_port_number) &&
0460          (ntohs(lacpdu->partner_port_priority) == port->actor_port_priority) &&
0461          MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) &&
0462          (ntohs(lacpdu->partner_system_priority) == port->actor_system_priority) &&
0463          (ntohs(lacpdu->partner_key) == port->actor_oper_port_key) &&
0464          ((lacpdu->partner_state & LACP_STATE_AGGREGATION) == (port->actor_oper_port_state & LACP_STATE_AGGREGATION))) ||
0465         ((lacpdu->actor_state & LACP_STATE_AGGREGATION) == 0)
0466         ) {
0467         port->sm_vars |= AD_PORT_MATCHED;
0468     } else {
0469         port->sm_vars &= ~AD_PORT_MATCHED;
0470     }
0471 }
0472 
0473 /**
0474  * __record_pdu - record parameters from a received lacpdu
0475  * @lacpdu: the lacpdu we've received
0476  * @port: the port we're looking at
0477  *
0478  * Record the parameter values for the Actor carried in a received lacpdu as
0479  * the current partner operational parameter values and sets
0480  * actor_oper_port_state.defaulted to FALSE.
0481  */
0482 static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
0483 {
0484     if (lacpdu && port) {
0485         struct port_params *partner = &port->partner_oper;
0486 
0487         __choose_matched(lacpdu, port);
0488         /* record the new parameter values for the partner
0489          * operational
0490          */
0491         partner->port_number = ntohs(lacpdu->actor_port);
0492         partner->port_priority = ntohs(lacpdu->actor_port_priority);
0493         partner->system = lacpdu->actor_system;
0494         partner->system_priority = ntohs(lacpdu->actor_system_priority);
0495         partner->key = ntohs(lacpdu->actor_key);
0496         partner->port_state = lacpdu->actor_state;
0497 
0498         /* set actor_oper_port_state.defaulted to FALSE */
0499         port->actor_oper_port_state &= ~LACP_STATE_DEFAULTED;
0500 
0501         /* set the partner sync. to on if the partner is sync,
0502          * and the port is matched
0503          */
0504         if ((port->sm_vars & AD_PORT_MATCHED) &&
0505             (lacpdu->actor_state & LACP_STATE_SYNCHRONIZATION)) {
0506             partner->port_state |= LACP_STATE_SYNCHRONIZATION;
0507             slave_dbg(port->slave->bond->dev, port->slave->dev,
0508                   "partner sync=1\n");
0509         } else {
0510             partner->port_state &= ~LACP_STATE_SYNCHRONIZATION;
0511             slave_dbg(port->slave->bond->dev, port->slave->dev,
0512                   "partner sync=0\n");
0513         }
0514     }
0515 }
0516 
0517 /**
0518  * __record_default - record default parameters
0519  * @port: the port we're looking at
0520  *
0521  * This function records the default parameter values for the partner carried
0522  * in the Partner Admin parameters as the current partner operational parameter
0523  * values and sets actor_oper_port_state.defaulted to TRUE.
0524  */
0525 static void __record_default(struct port *port)
0526 {
0527     if (port) {
0528         /* record the partner admin parameters */
0529         memcpy(&port->partner_oper, &port->partner_admin,
0530                sizeof(struct port_params));
0531 
0532         /* set actor_oper_port_state.defaulted to true */
0533         port->actor_oper_port_state |= LACP_STATE_DEFAULTED;
0534     }
0535 }
0536 
0537 /**
0538  * __update_selected - update a port's Selected variable from a received lacpdu
0539  * @lacpdu: the lacpdu we've received
0540  * @port: the port we're looking at
0541  *
0542  * Update the value of the selected variable, using parameter values from a
0543  * newly received lacpdu. The parameter values for the Actor carried in the
0544  * received PDU are compared with the corresponding operational parameter
0545  * values for the ports partner. If one or more of the comparisons shows that
0546  * the value(s) received in the PDU differ from the current operational values,
0547  * then selected is set to FALSE and actor_oper_port_state.synchronization is
0548  * set to out_of_sync. Otherwise, selected remains unchanged.
0549  */
0550 static void __update_selected(struct lacpdu *lacpdu, struct port *port)
0551 {
0552     if (lacpdu && port) {
0553         const struct port_params *partner = &port->partner_oper;
0554 
0555         /* check if any parameter is different then
0556          * update the state machine selected variable.
0557          */
0558         if (ntohs(lacpdu->actor_port) != partner->port_number ||
0559             ntohs(lacpdu->actor_port_priority) != partner->port_priority ||
0560             !MAC_ADDRESS_EQUAL(&lacpdu->actor_system, &partner->system) ||
0561             ntohs(lacpdu->actor_system_priority) != partner->system_priority ||
0562             ntohs(lacpdu->actor_key) != partner->key ||
0563             (lacpdu->actor_state & LACP_STATE_AGGREGATION) != (partner->port_state & LACP_STATE_AGGREGATION)) {
0564             port->sm_vars &= ~AD_PORT_SELECTED;
0565         }
0566     }
0567 }
0568 
0569 /**
0570  * __update_default_selected - update a port's Selected variable from Partner
0571  * @port: the port we're looking at
0572  *
0573  * This function updates the value of the selected variable, using the partner
0574  * administrative parameter values. The administrative values are compared with
0575  * the corresponding operational parameter values for the partner. If one or
0576  * more of the comparisons shows that the administrative value(s) differ from
0577  * the current operational values, then Selected is set to FALSE and
0578  * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
0579  * Selected remains unchanged.
0580  */
0581 static void __update_default_selected(struct port *port)
0582 {
0583     if (port) {
0584         const struct port_params *admin = &port->partner_admin;
0585         const struct port_params *oper = &port->partner_oper;
0586 
0587         /* check if any parameter is different then
0588          * update the state machine selected variable.
0589          */
0590         if (admin->port_number != oper->port_number ||
0591             admin->port_priority != oper->port_priority ||
0592             !MAC_ADDRESS_EQUAL(&admin->system, &oper->system) ||
0593             admin->system_priority != oper->system_priority ||
0594             admin->key != oper->key ||
0595             (admin->port_state & LACP_STATE_AGGREGATION)
0596             != (oper->port_state & LACP_STATE_AGGREGATION)) {
0597             port->sm_vars &= ~AD_PORT_SELECTED;
0598         }
0599     }
0600 }
0601 
0602 /**
0603  * __update_ntt - update a port's ntt variable from a received lacpdu
0604  * @lacpdu: the lacpdu we've received
0605  * @port: the port we're looking at
0606  *
0607  * Updates the value of the ntt variable, using parameter values from a newly
0608  * received lacpdu. The parameter values for the partner carried in the
0609  * received PDU are compared with the corresponding operational parameter
0610  * values for the Actor. If one or more of the comparisons shows that the
0611  * value(s) received in the PDU differ from the current operational values,
0612  * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
0613  */
0614 static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
0615 {
0616     /* validate lacpdu and port */
0617     if (lacpdu && port) {
0618         /* check if any parameter is different then
0619          * update the port->ntt.
0620          */
0621         if ((ntohs(lacpdu->partner_port) != port->actor_port_number) ||
0622             (ntohs(lacpdu->partner_port_priority) != port->actor_port_priority) ||
0623             !MAC_ADDRESS_EQUAL(&(lacpdu->partner_system), &(port->actor_system)) ||
0624             (ntohs(lacpdu->partner_system_priority) != port->actor_system_priority) ||
0625             (ntohs(lacpdu->partner_key) != port->actor_oper_port_key) ||
0626             ((lacpdu->partner_state & LACP_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & LACP_STATE_LACP_ACTIVITY)) ||
0627             ((lacpdu->partner_state & LACP_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & LACP_STATE_LACP_TIMEOUT)) ||
0628             ((lacpdu->partner_state & LACP_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) ||
0629             ((lacpdu->partner_state & LACP_STATE_AGGREGATION) != (port->actor_oper_port_state & LACP_STATE_AGGREGATION))
0630            ) {
0631             port->ntt = true;
0632         }
0633     }
0634 }
0635 
0636 /**
0637  * __agg_ports_are_ready - check if all ports in an aggregator are ready
0638  * @aggregator: the aggregator we're looking at
0639  *
0640  */
0641 static int __agg_ports_are_ready(struct aggregator *aggregator)
0642 {
0643     struct port *port;
0644     int retval = 1;
0645 
0646     if (aggregator) {
0647         /* scan all ports in this aggregator to verfy if they are
0648          * all ready.
0649          */
0650         for (port = aggregator->lag_ports;
0651              port;
0652              port = port->next_port_in_aggregator) {
0653             if (!(port->sm_vars & AD_PORT_READY_N)) {
0654                 retval = 0;
0655                 break;
0656             }
0657         }
0658     }
0659 
0660     return retval;
0661 }
0662 
0663 /**
0664  * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
0665  * @aggregator: the aggregator we're looking at
0666  * @val: Should the ports' ready bit be set on or off
0667  *
0668  */
0669 static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
0670 {
0671     struct port *port;
0672 
0673     for (port = aggregator->lag_ports; port;
0674          port = port->next_port_in_aggregator) {
0675         if (val)
0676             port->sm_vars |= AD_PORT_READY;
0677         else
0678             port->sm_vars &= ~AD_PORT_READY;
0679     }
0680 }
0681 
0682 static int __agg_active_ports(struct aggregator *agg)
0683 {
0684     struct port *port;
0685     int active = 0;
0686 
0687     for (port = agg->lag_ports; port;
0688          port = port->next_port_in_aggregator) {
0689         if (port->is_enabled)
0690             active++;
0691     }
0692 
0693     return active;
0694 }
0695 
0696 /**
0697  * __get_agg_bandwidth - get the total bandwidth of an aggregator
0698  * @aggregator: the aggregator we're looking at
0699  *
0700  */
0701 static u32 __get_agg_bandwidth(struct aggregator *aggregator)
0702 {
0703     int nports = __agg_active_ports(aggregator);
0704     u32 bandwidth = 0;
0705 
0706     if (nports) {
0707         switch (__get_link_speed(aggregator->lag_ports)) {
0708         case AD_LINK_SPEED_1MBPS:
0709             bandwidth = nports;
0710             break;
0711         case AD_LINK_SPEED_10MBPS:
0712             bandwidth = nports * 10;
0713             break;
0714         case AD_LINK_SPEED_100MBPS:
0715             bandwidth = nports * 100;
0716             break;
0717         case AD_LINK_SPEED_1000MBPS:
0718             bandwidth = nports * 1000;
0719             break;
0720         case AD_LINK_SPEED_2500MBPS:
0721             bandwidth = nports * 2500;
0722             break;
0723         case AD_LINK_SPEED_5000MBPS:
0724             bandwidth = nports * 5000;
0725             break;
0726         case AD_LINK_SPEED_10000MBPS:
0727             bandwidth = nports * 10000;
0728             break;
0729         case AD_LINK_SPEED_14000MBPS:
0730             bandwidth = nports * 14000;
0731             break;
0732         case AD_LINK_SPEED_20000MBPS:
0733             bandwidth = nports * 20000;
0734             break;
0735         case AD_LINK_SPEED_25000MBPS:
0736             bandwidth = nports * 25000;
0737             break;
0738         case AD_LINK_SPEED_40000MBPS:
0739             bandwidth = nports * 40000;
0740             break;
0741         case AD_LINK_SPEED_50000MBPS:
0742             bandwidth = nports * 50000;
0743             break;
0744         case AD_LINK_SPEED_56000MBPS:
0745             bandwidth = nports * 56000;
0746             break;
0747         case AD_LINK_SPEED_100000MBPS:
0748             bandwidth = nports * 100000;
0749             break;
0750         case AD_LINK_SPEED_200000MBPS:
0751             bandwidth = nports * 200000;
0752             break;
0753         case AD_LINK_SPEED_400000MBPS:
0754             bandwidth = nports * 400000;
0755             break;
0756         default:
0757             bandwidth = 0; /* to silence the compiler */
0758         }
0759     }
0760     return bandwidth;
0761 }
0762 
0763 /**
0764  * __get_active_agg - get the current active aggregator
0765  * @aggregator: the aggregator we're looking at
0766  *
0767  * Caller must hold RCU lock.
0768  */
0769 static struct aggregator *__get_active_agg(struct aggregator *aggregator)
0770 {
0771     struct bonding *bond = aggregator->slave->bond;
0772     struct list_head *iter;
0773     struct slave *slave;
0774 
0775     bond_for_each_slave_rcu(bond, slave, iter)
0776         if (SLAVE_AD_INFO(slave)->aggregator.is_active)
0777             return &(SLAVE_AD_INFO(slave)->aggregator);
0778 
0779     return NULL;
0780 }
0781 
0782 /**
0783  * __update_lacpdu_from_port - update a port's lacpdu fields
0784  * @port: the port we're looking at
0785  */
0786 static inline void __update_lacpdu_from_port(struct port *port)
0787 {
0788     struct lacpdu *lacpdu = &port->lacpdu;
0789     const struct port_params *partner = &port->partner_oper;
0790 
0791     /* update current actual Actor parameters
0792      * lacpdu->subtype                   initialized
0793      * lacpdu->version_number            initialized
0794      * lacpdu->tlv_type_actor_info       initialized
0795      * lacpdu->actor_information_length  initialized
0796      */
0797 
0798     lacpdu->actor_system_priority = htons(port->actor_system_priority);
0799     lacpdu->actor_system = port->actor_system;
0800     lacpdu->actor_key = htons(port->actor_oper_port_key);
0801     lacpdu->actor_port_priority = htons(port->actor_port_priority);
0802     lacpdu->actor_port = htons(port->actor_port_number);
0803     lacpdu->actor_state = port->actor_oper_port_state;
0804     slave_dbg(port->slave->bond->dev, port->slave->dev,
0805           "update lacpdu: actor port state %x\n",
0806           port->actor_oper_port_state);
0807 
0808     /* lacpdu->reserved_3_1              initialized
0809      * lacpdu->tlv_type_partner_info     initialized
0810      * lacpdu->partner_information_length initialized
0811      */
0812 
0813     lacpdu->partner_system_priority = htons(partner->system_priority);
0814     lacpdu->partner_system = partner->system;
0815     lacpdu->partner_key = htons(partner->key);
0816     lacpdu->partner_port_priority = htons(partner->port_priority);
0817     lacpdu->partner_port = htons(partner->port_number);
0818     lacpdu->partner_state = partner->port_state;
0819 
0820     /* lacpdu->reserved_3_2              initialized
0821      * lacpdu->tlv_type_collector_info   initialized
0822      * lacpdu->collector_information_length initialized
0823      * collector_max_delay                initialized
0824      * reserved_12[12]                   initialized
0825      * tlv_type_terminator               initialized
0826      * terminator_length                 initialized
0827      * reserved_50[50]                   initialized
0828      */
0829 }
0830 
0831 /* ================= main 802.3ad protocol code ========================= */
0832 
0833 /**
0834  * ad_lacpdu_send - send out a lacpdu packet on a given port
0835  * @port: the port we're looking at
0836  *
0837  * Returns:   0 on success
0838  *          < 0 on error
0839  */
0840 static int ad_lacpdu_send(struct port *port)
0841 {
0842     struct slave *slave = port->slave;
0843     struct sk_buff *skb;
0844     struct lacpdu_header *lacpdu_header;
0845     int length = sizeof(struct lacpdu_header);
0846 
0847     skb = dev_alloc_skb(length);
0848     if (!skb)
0849         return -ENOMEM;
0850 
0851     atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_tx);
0852     atomic64_inc(&BOND_AD_INFO(slave->bond).stats.lacpdu_tx);
0853 
0854     skb->dev = slave->dev;
0855     skb_reset_mac_header(skb);
0856     skb->network_header = skb->mac_header + ETH_HLEN;
0857     skb->protocol = PKT_TYPE_LACPDU;
0858     skb->priority = TC_PRIO_CONTROL;
0859 
0860     lacpdu_header = skb_put(skb, length);
0861 
0862     ether_addr_copy(lacpdu_header->hdr.h_dest, lacpdu_mcast_addr);
0863     /* Note: source address is set to be the member's PERMANENT address,
0864      * because we use it to identify loopback lacpdus in receive.
0865      */
0866     ether_addr_copy(lacpdu_header->hdr.h_source, slave->perm_hwaddr);
0867     lacpdu_header->hdr.h_proto = PKT_TYPE_LACPDU;
0868 
0869     lacpdu_header->lacpdu = port->lacpdu;
0870 
0871     dev_queue_xmit(skb);
0872 
0873     return 0;
0874 }
0875 
0876 /**
0877  * ad_marker_send - send marker information/response on a given port
0878  * @port: the port we're looking at
0879  * @marker: marker data to send
0880  *
0881  * Returns:   0 on success
0882  *          < 0 on error
0883  */
0884 static int ad_marker_send(struct port *port, struct bond_marker *marker)
0885 {
0886     struct slave *slave = port->slave;
0887     struct sk_buff *skb;
0888     struct bond_marker_header *marker_header;
0889     int length = sizeof(struct bond_marker_header);
0890 
0891     skb = dev_alloc_skb(length + 16);
0892     if (!skb)
0893         return -ENOMEM;
0894 
0895     switch (marker->tlv_type) {
0896     case AD_MARKER_INFORMATION_SUBTYPE:
0897         atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_tx);
0898         atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_tx);
0899         break;
0900     case AD_MARKER_RESPONSE_SUBTYPE:
0901         atomic64_inc(&SLAVE_AD_INFO(slave)->stats.marker_resp_tx);
0902         atomic64_inc(&BOND_AD_INFO(slave->bond).stats.marker_resp_tx);
0903         break;
0904     }
0905 
0906     skb_reserve(skb, 16);
0907 
0908     skb->dev = slave->dev;
0909     skb_reset_mac_header(skb);
0910     skb->network_header = skb->mac_header + ETH_HLEN;
0911     skb->protocol = PKT_TYPE_LACPDU;
0912 
0913     marker_header = skb_put(skb, length);
0914 
0915     ether_addr_copy(marker_header->hdr.h_dest, lacpdu_mcast_addr);
0916     /* Note: source address is set to be the member's PERMANENT address,
0917      * because we use it to identify loopback MARKERs in receive.
0918      */
0919     ether_addr_copy(marker_header->hdr.h_source, slave->perm_hwaddr);
0920     marker_header->hdr.h_proto = PKT_TYPE_LACPDU;
0921 
0922     marker_header->marker = *marker;
0923 
0924     dev_queue_xmit(skb);
0925 
0926     return 0;
0927 }
0928 
0929 /**
0930  * ad_mux_machine - handle a port's mux state machine
0931  * @port: the port we're looking at
0932  * @update_slave_arr: Does slave array need update?
0933  */
0934 static void ad_mux_machine(struct port *port, bool *update_slave_arr)
0935 {
0936     mux_states_t last_state;
0937 
0938     /* keep current State Machine state to compare later if it was
0939      * changed
0940      */
0941     last_state = port->sm_mux_state;
0942 
0943     if (port->sm_vars & AD_PORT_BEGIN) {
0944         port->sm_mux_state = AD_MUX_DETACHED;
0945     } else {
0946         switch (port->sm_mux_state) {
0947         case AD_MUX_DETACHED:
0948             if ((port->sm_vars & AD_PORT_SELECTED)
0949                 || (port->sm_vars & AD_PORT_STANDBY))
0950                 /* if SELECTED or STANDBY */
0951                 port->sm_mux_state = AD_MUX_WAITING;
0952             break;
0953         case AD_MUX_WAITING:
0954             /* if SELECTED == FALSE return to DETACH state */
0955             if (!(port->sm_vars & AD_PORT_SELECTED)) {
0956                 port->sm_vars &= ~AD_PORT_READY_N;
0957                 /* in order to withhold the Selection Logic to
0958                  * check all ports READY_N value every callback
0959                  * cycle to update ready variable, we check
0960                  * READY_N and update READY here
0961                  */
0962                 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
0963                 port->sm_mux_state = AD_MUX_DETACHED;
0964                 break;
0965             }
0966 
0967             /* check if the wait_while_timer expired */
0968             if (port->sm_mux_timer_counter
0969                 && !(--port->sm_mux_timer_counter))
0970                 port->sm_vars |= AD_PORT_READY_N;
0971 
0972             /* in order to withhold the selection logic to check
0973              * all ports READY_N value every callback cycle to
0974              * update ready variable, we check READY_N and update
0975              * READY here
0976              */
0977             __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
0978 
0979             /* if the wait_while_timer expired, and the port is
0980              * in READY state, move to ATTACHED state
0981              */
0982             if ((port->sm_vars & AD_PORT_READY)
0983                 && !port->sm_mux_timer_counter)
0984                 port->sm_mux_state = AD_MUX_ATTACHED;
0985             break;
0986         case AD_MUX_ATTACHED:
0987             /* check also if agg_select_timer expired (so the
0988              * edable port will take place only after this timer)
0989              */
0990             if ((port->sm_vars & AD_PORT_SELECTED) &&
0991                 (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) &&
0992                 !__check_agg_selection_timer(port)) {
0993                 if (port->aggregator->is_active)
0994                     port->sm_mux_state =
0995                         AD_MUX_COLLECTING_DISTRIBUTING;
0996             } else if (!(port->sm_vars & AD_PORT_SELECTED) ||
0997                    (port->sm_vars & AD_PORT_STANDBY)) {
0998                 /* if UNSELECTED or STANDBY */
0999                 port->sm_vars &= ~AD_PORT_READY_N;
1000                 /* in order to withhold the selection logic to
1001                  * check all ports READY_N value every callback
1002                  * cycle to update ready variable, we check
1003                  * READY_N and update READY here
1004                  */
1005                 __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
1006                 port->sm_mux_state = AD_MUX_DETACHED;
1007             } else if (port->aggregator->is_active) {
1008                 port->actor_oper_port_state |=
1009                     LACP_STATE_SYNCHRONIZATION;
1010             }
1011             break;
1012         case AD_MUX_COLLECTING_DISTRIBUTING:
1013             if (!(port->sm_vars & AD_PORT_SELECTED) ||
1014                 (port->sm_vars & AD_PORT_STANDBY) ||
1015                 !(port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) ||
1016                 !(port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION)) {
1017                 port->sm_mux_state = AD_MUX_ATTACHED;
1018             } else {
1019                 /* if port state hasn't changed make
1020                  * sure that a collecting distributing
1021                  * port in an active aggregator is enabled
1022                  */
1023                 if (port->aggregator &&
1024                     port->aggregator->is_active &&
1025                     !__port_is_enabled(port)) {
1026                     __enable_port(port);
1027                     *update_slave_arr = true;
1028                 }
1029             }
1030             break;
1031         default:
1032             break;
1033         }
1034     }
1035 
1036     /* check if the state machine was changed */
1037     if (port->sm_mux_state != last_state) {
1038         slave_dbg(port->slave->bond->dev, port->slave->dev,
1039               "Mux Machine: Port=%d, Last State=%d, Curr State=%d\n",
1040               port->actor_port_number,
1041               last_state,
1042               port->sm_mux_state);
1043         switch (port->sm_mux_state) {
1044         case AD_MUX_DETACHED:
1045             port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
1046             ad_disable_collecting_distributing(port,
1047                                update_slave_arr);
1048             port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
1049             port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
1050             port->ntt = true;
1051             break;
1052         case AD_MUX_WAITING:
1053             port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
1054             break;
1055         case AD_MUX_ATTACHED:
1056             if (port->aggregator->is_active)
1057                 port->actor_oper_port_state |=
1058                     LACP_STATE_SYNCHRONIZATION;
1059             else
1060                 port->actor_oper_port_state &=
1061                     ~LACP_STATE_SYNCHRONIZATION;
1062             port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
1063             port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
1064             ad_disable_collecting_distributing(port,
1065                                update_slave_arr);
1066             port->ntt = true;
1067             break;
1068         case AD_MUX_COLLECTING_DISTRIBUTING:
1069             port->actor_oper_port_state |= LACP_STATE_COLLECTING;
1070             port->actor_oper_port_state |= LACP_STATE_DISTRIBUTING;
1071             port->actor_oper_port_state |= LACP_STATE_SYNCHRONIZATION;
1072             ad_enable_collecting_distributing(port,
1073                               update_slave_arr);
1074             port->ntt = true;
1075             break;
1076         default:
1077             break;
1078         }
1079     }
1080 }
1081 
1082 /**
1083  * ad_rx_machine - handle a port's rx State Machine
1084  * @lacpdu: the lacpdu we've received
1085  * @port: the port we're looking at
1086  *
1087  * If lacpdu arrived, stop previous timer (if exists) and set the next state as
1088  * CURRENT. If timer expired set the state machine in the proper state.
1089  * In other cases, this function checks if we need to switch to other state.
1090  */
1091 static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
1092 {
1093     rx_states_t last_state;
1094 
1095     /* keep current State Machine state to compare later if it was
1096      * changed
1097      */
1098     last_state = port->sm_rx_state;
1099 
1100     if (lacpdu) {
1101         atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.lacpdu_rx);
1102         atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.lacpdu_rx);
1103     }
1104     /* check if state machine should change state */
1105 
1106     /* first, check if port was reinitialized */
1107     if (port->sm_vars & AD_PORT_BEGIN) {
1108         port->sm_rx_state = AD_RX_INITIALIZE;
1109         port->sm_vars |= AD_PORT_CHURNED;
1110     /* check if port is not enabled */
1111     } else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled)
1112         port->sm_rx_state = AD_RX_PORT_DISABLED;
1113     /* check if new lacpdu arrived */
1114     else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) ||
1115          (port->sm_rx_state == AD_RX_DEFAULTED) ||
1116          (port->sm_rx_state == AD_RX_CURRENT))) {
1117         if (port->sm_rx_state != AD_RX_CURRENT)
1118             port->sm_vars |= AD_PORT_CHURNED;
1119         port->sm_rx_timer_counter = 0;
1120         port->sm_rx_state = AD_RX_CURRENT;
1121     } else {
1122         /* if timer is on, and if it is expired */
1123         if (port->sm_rx_timer_counter &&
1124             !(--port->sm_rx_timer_counter)) {
1125             switch (port->sm_rx_state) {
1126             case AD_RX_EXPIRED:
1127                 port->sm_rx_state = AD_RX_DEFAULTED;
1128                 break;
1129             case AD_RX_CURRENT:
1130                 port->sm_rx_state = AD_RX_EXPIRED;
1131                 break;
1132             default:
1133                 break;
1134             }
1135         } else {
1136             /* if no lacpdu arrived and no timer is on */
1137             switch (port->sm_rx_state) {
1138             case AD_RX_PORT_DISABLED:
1139                 if (port->is_enabled &&
1140                     (port->sm_vars & AD_PORT_LACP_ENABLED))
1141                     port->sm_rx_state = AD_RX_EXPIRED;
1142                 else if (port->is_enabled
1143                      && ((port->sm_vars
1144                           & AD_PORT_LACP_ENABLED) == 0))
1145                     port->sm_rx_state = AD_RX_LACP_DISABLED;
1146                 break;
1147             default:
1148                 break;
1149 
1150             }
1151         }
1152     }
1153 
1154     /* check if the State machine was changed or new lacpdu arrived */
1155     if ((port->sm_rx_state != last_state) || (lacpdu)) {
1156         slave_dbg(port->slave->bond->dev, port->slave->dev,
1157               "Rx Machine: Port=%d, Last State=%d, Curr State=%d\n",
1158               port->actor_port_number,
1159               last_state,
1160               port->sm_rx_state);
1161         switch (port->sm_rx_state) {
1162         case AD_RX_INITIALIZE:
1163             if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS))
1164                 port->sm_vars &= ~AD_PORT_LACP_ENABLED;
1165             else
1166                 port->sm_vars |= AD_PORT_LACP_ENABLED;
1167             port->sm_vars &= ~AD_PORT_SELECTED;
1168             __record_default(port);
1169             port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1170             port->sm_rx_state = AD_RX_PORT_DISABLED;
1171 
1172             fallthrough;
1173         case AD_RX_PORT_DISABLED:
1174             port->sm_vars &= ~AD_PORT_MATCHED;
1175             break;
1176         case AD_RX_LACP_DISABLED:
1177             port->sm_vars &= ~AD_PORT_SELECTED;
1178             __record_default(port);
1179             port->partner_oper.port_state &= ~LACP_STATE_AGGREGATION;
1180             port->sm_vars |= AD_PORT_MATCHED;
1181             port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1182             break;
1183         case AD_RX_EXPIRED:
1184             /* Reset of the Synchronization flag (Standard 43.4.12)
1185              * This reset cause to disable this port in the
1186              * COLLECTING_DISTRIBUTING state of the mux machine in
1187              * case of EXPIRED even if LINK_DOWN didn't arrive for
1188              * the port.
1189              */
1190             port->partner_oper.port_state &= ~LACP_STATE_SYNCHRONIZATION;
1191             port->sm_vars &= ~AD_PORT_MATCHED;
1192             port->partner_oper.port_state |= LACP_STATE_LACP_TIMEOUT;
1193             port->partner_oper.port_state |= LACP_STATE_LACP_ACTIVITY;
1194             port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
1195             port->actor_oper_port_state |= LACP_STATE_EXPIRED;
1196             port->sm_vars |= AD_PORT_CHURNED;
1197             break;
1198         case AD_RX_DEFAULTED:
1199             __update_default_selected(port);
1200             __record_default(port);
1201             port->sm_vars |= AD_PORT_MATCHED;
1202             port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1203             break;
1204         case AD_RX_CURRENT:
1205             /* detect loopback situation */
1206             if (MAC_ADDRESS_EQUAL(&(lacpdu->actor_system),
1207                           &(port->actor_system))) {
1208                 slave_err(port->slave->bond->dev, port->slave->dev, "An illegal loopback occurred on slave\n"
1209                       "Check the configuration to verify that all adapters are connected to 802.3ad compliant switch ports\n");
1210                 return;
1211             }
1212             __update_selected(lacpdu, port);
1213             __update_ntt(lacpdu, port);
1214             __record_pdu(lacpdu, port);
1215             port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & LACP_STATE_LACP_TIMEOUT));
1216             port->actor_oper_port_state &= ~LACP_STATE_EXPIRED;
1217             break;
1218         default:
1219             break;
1220         }
1221     }
1222 }
1223 
1224 /**
1225  * ad_churn_machine - handle port churn's state machine
1226  * @port: the port we're looking at
1227  *
1228  */
1229 static void ad_churn_machine(struct port *port)
1230 {
1231     if (port->sm_vars & AD_PORT_CHURNED) {
1232         port->sm_vars &= ~AD_PORT_CHURNED;
1233         port->sm_churn_actor_state = AD_CHURN_MONITOR;
1234         port->sm_churn_partner_state = AD_CHURN_MONITOR;
1235         port->sm_churn_actor_timer_counter =
1236             __ad_timer_to_ticks(AD_ACTOR_CHURN_TIMER, 0);
1237         port->sm_churn_partner_timer_counter =
1238              __ad_timer_to_ticks(AD_PARTNER_CHURN_TIMER, 0);
1239         return;
1240     }
1241     if (port->sm_churn_actor_timer_counter &&
1242         !(--port->sm_churn_actor_timer_counter) &&
1243         port->sm_churn_actor_state == AD_CHURN_MONITOR) {
1244         if (port->actor_oper_port_state & LACP_STATE_SYNCHRONIZATION) {
1245             port->sm_churn_actor_state = AD_NO_CHURN;
1246         } else {
1247             port->churn_actor_count++;
1248             port->sm_churn_actor_state = AD_CHURN;
1249         }
1250     }
1251     if (port->sm_churn_partner_timer_counter &&
1252         !(--port->sm_churn_partner_timer_counter) &&
1253         port->sm_churn_partner_state == AD_CHURN_MONITOR) {
1254         if (port->partner_oper.port_state & LACP_STATE_SYNCHRONIZATION) {
1255             port->sm_churn_partner_state = AD_NO_CHURN;
1256         } else {
1257             port->churn_partner_count++;
1258             port->sm_churn_partner_state = AD_CHURN;
1259         }
1260     }
1261 }
1262 
1263 /**
1264  * ad_tx_machine - handle a port's tx state machine
1265  * @port: the port we're looking at
1266  */
1267 static void ad_tx_machine(struct port *port)
1268 {
1269     /* check if tx timer expired, to verify that we do not send more than
1270      * 3 packets per second
1271      */
1272     if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
1273         /* check if there is something to send */
1274         if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
1275             __update_lacpdu_from_port(port);
1276 
1277             if (ad_lacpdu_send(port) >= 0) {
1278                 slave_dbg(port->slave->bond->dev,
1279                       port->slave->dev,
1280                       "Sent LACPDU on port %d\n",
1281                       port->actor_port_number);
1282 
1283                 /* mark ntt as false, so it will not be sent
1284                  * again until demanded
1285                  */
1286                 port->ntt = false;
1287             }
1288         }
1289         /* restart tx timer(to verify that we will not exceed
1290          * AD_MAX_TX_IN_SECOND
1291          */
1292         port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
1293     }
1294 }
1295 
1296 /**
1297  * ad_periodic_machine - handle a port's periodic state machine
1298  * @port: the port we're looking at
1299  * @bond_params: bond parameters we will use
1300  *
1301  * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
1302  */
1303 static void ad_periodic_machine(struct port *port, struct bond_params *bond_params)
1304 {
1305     periodic_states_t last_state;
1306 
1307     /* keep current state machine state to compare later if it was changed */
1308     last_state = port->sm_periodic_state;
1309 
1310     /* check if port was reinitialized */
1311     if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
1312         (!(port->actor_oper_port_state & LACP_STATE_LACP_ACTIVITY) && !(port->partner_oper.port_state & LACP_STATE_LACP_ACTIVITY)) ||
1313         !bond_params->lacp_active) {
1314         port->sm_periodic_state = AD_NO_PERIODIC;
1315     }
1316     /* check if state machine should change state */
1317     else if (port->sm_periodic_timer_counter) {
1318         /* check if periodic state machine expired */
1319         if (!(--port->sm_periodic_timer_counter)) {
1320             /* if expired then do tx */
1321             port->sm_periodic_state = AD_PERIODIC_TX;
1322         } else {
1323             /* If not expired, check if there is some new timeout
1324              * parameter from the partner state
1325              */
1326             switch (port->sm_periodic_state) {
1327             case AD_FAST_PERIODIC:
1328                 if (!(port->partner_oper.port_state
1329                       & LACP_STATE_LACP_TIMEOUT))
1330                     port->sm_periodic_state = AD_SLOW_PERIODIC;
1331                 break;
1332             case AD_SLOW_PERIODIC:
1333                 if ((port->partner_oper.port_state & LACP_STATE_LACP_TIMEOUT)) {
1334                     port->sm_periodic_timer_counter = 0;
1335                     port->sm_periodic_state = AD_PERIODIC_TX;
1336                 }
1337                 break;
1338             default:
1339                 break;
1340             }
1341         }
1342     } else {
1343         switch (port->sm_periodic_state) {
1344         case AD_NO_PERIODIC:
1345             port->sm_periodic_state = AD_FAST_PERIODIC;
1346             break;
1347         case AD_PERIODIC_TX:
1348             if (!(port->partner_oper.port_state &
1349                 LACP_STATE_LACP_TIMEOUT))
1350                 port->sm_periodic_state = AD_SLOW_PERIODIC;
1351             else
1352                 port->sm_periodic_state = AD_FAST_PERIODIC;
1353             break;
1354         default:
1355             break;
1356         }
1357     }
1358 
1359     /* check if the state machine was changed */
1360     if (port->sm_periodic_state != last_state) {
1361         slave_dbg(port->slave->bond->dev, port->slave->dev,
1362               "Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n",
1363               port->actor_port_number, last_state,
1364               port->sm_periodic_state);
1365         switch (port->sm_periodic_state) {
1366         case AD_NO_PERIODIC:
1367             port->sm_periodic_timer_counter = 0;
1368             break;
1369         case AD_FAST_PERIODIC:
1370             /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1371             port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1;
1372             break;
1373         case AD_SLOW_PERIODIC:
1374             /* decrement 1 tick we lost in the PERIODIC_TX cycle */
1375             port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1;
1376             break;
1377         case AD_PERIODIC_TX:
1378             port->ntt = true;
1379             break;
1380         default:
1381             break;
1382         }
1383     }
1384 }
1385 
1386 /**
1387  * ad_port_selection_logic - select aggregation groups
1388  * @port: the port we're looking at
1389  * @update_slave_arr: Does slave array need update?
1390  *
1391  * Select aggregation groups, and assign each port for it's aggregetor. The
1392  * selection logic is called in the inititalization (after all the handshkes),
1393  * and after every lacpdu receive (if selected is off).
1394  */
1395 static void ad_port_selection_logic(struct port *port, bool *update_slave_arr)
1396 {
1397     struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
1398     struct port *last_port = NULL, *curr_port;
1399     struct list_head *iter;
1400     struct bonding *bond;
1401     struct slave *slave;
1402     int found = 0;
1403 
1404     /* if the port is already Selected, do nothing */
1405     if (port->sm_vars & AD_PORT_SELECTED)
1406         return;
1407 
1408     bond = __get_bond_by_port(port);
1409 
1410     /* if the port is connected to other aggregator, detach it */
1411     if (port->aggregator) {
1412         /* detach the port from its former aggregator */
1413         temp_aggregator = port->aggregator;
1414         for (curr_port = temp_aggregator->lag_ports; curr_port;
1415              last_port = curr_port,
1416              curr_port = curr_port->next_port_in_aggregator) {
1417             if (curr_port == port) {
1418                 temp_aggregator->num_of_ports--;
1419                 /* if it is the first port attached to the
1420                  * aggregator
1421                  */
1422                 if (!last_port) {
1423                     temp_aggregator->lag_ports =
1424                         port->next_port_in_aggregator;
1425                 } else {
1426                     /* not the first port attached to the
1427                      * aggregator
1428                      */
1429                     last_port->next_port_in_aggregator =
1430                         port->next_port_in_aggregator;
1431                 }
1432 
1433                 /* clear the port's relations to this
1434                  * aggregator
1435                  */
1436                 port->aggregator = NULL;
1437                 port->next_port_in_aggregator = NULL;
1438                 port->actor_port_aggregator_identifier = 0;
1439 
1440                 slave_dbg(bond->dev, port->slave->dev, "Port %d left LAG %d\n",
1441                       port->actor_port_number,
1442                       temp_aggregator->aggregator_identifier);
1443                 /* if the aggregator is empty, clear its
1444                  * parameters, and set it ready to be attached
1445                  */
1446                 if (!temp_aggregator->lag_ports)
1447                     ad_clear_agg(temp_aggregator);
1448                 break;
1449             }
1450         }
1451         if (!curr_port) {
1452             /* meaning: the port was related to an aggregator
1453              * but was not on the aggregator port list
1454              */
1455             net_warn_ratelimited("%s: (slave %s): Warning: Port %d was related to aggregator %d but was not on its port list\n",
1456                          port->slave->bond->dev->name,
1457                          port->slave->dev->name,
1458                          port->actor_port_number,
1459                          port->aggregator->aggregator_identifier);
1460         }
1461     }
1462     /* search on all aggregators for a suitable aggregator for this port */
1463     bond_for_each_slave(bond, slave, iter) {
1464         aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
1465 
1466         /* keep a free aggregator for later use(if needed) */
1467         if (!aggregator->lag_ports) {
1468             if (!free_aggregator)
1469                 free_aggregator = aggregator;
1470             continue;
1471         }
1472         /* check if current aggregator suits us */
1473         if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && /* if all parameters match AND */
1474              MAC_ADDRESS_EQUAL(&(aggregator->partner_system), &(port->partner_oper.system)) &&
1475              (aggregator->partner_system_priority == port->partner_oper.system_priority) &&
1476              (aggregator->partner_oper_aggregator_key == port->partner_oper.key)
1477             ) &&
1478             ((!MAC_ADDRESS_EQUAL(&(port->partner_oper.system), &(null_mac_addr)) && /* partner answers */
1479               !aggregator->is_individual)  /* but is not individual OR */
1480             )
1481            ) {
1482             /* attach to the founded aggregator */
1483             port->aggregator = aggregator;
1484             port->actor_port_aggregator_identifier =
1485                 port->aggregator->aggregator_identifier;
1486             port->next_port_in_aggregator = aggregator->lag_ports;
1487             port->aggregator->num_of_ports++;
1488             aggregator->lag_ports = port;
1489             slave_dbg(bond->dev, slave->dev, "Port %d joined LAG %d (existing LAG)\n",
1490                   port->actor_port_number,
1491                   port->aggregator->aggregator_identifier);
1492 
1493             /* mark this port as selected */
1494             port->sm_vars |= AD_PORT_SELECTED;
1495             found = 1;
1496             break;
1497         }
1498     }
1499 
1500     /* the port couldn't find an aggregator - attach it to a new
1501      * aggregator
1502      */
1503     if (!found) {
1504         if (free_aggregator) {
1505             /* assign port a new aggregator */
1506             port->aggregator = free_aggregator;
1507             port->actor_port_aggregator_identifier =
1508                 port->aggregator->aggregator_identifier;
1509 
1510             /* update the new aggregator's parameters
1511              * if port was responsed from the end-user
1512              */
1513             if (port->actor_oper_port_key & AD_DUPLEX_KEY_MASKS)
1514                 /* if port is full duplex */
1515                 port->aggregator->is_individual = false;
1516             else
1517                 port->aggregator->is_individual = true;
1518 
1519             port->aggregator->actor_admin_aggregator_key =
1520                 port->actor_admin_port_key;
1521             port->aggregator->actor_oper_aggregator_key =
1522                 port->actor_oper_port_key;
1523             port->aggregator->partner_system =
1524                 port->partner_oper.system;
1525             port->aggregator->partner_system_priority =
1526                 port->partner_oper.system_priority;
1527             port->aggregator->partner_oper_aggregator_key = port->partner_oper.key;
1528             port->aggregator->receive_state = 1;
1529             port->aggregator->transmit_state = 1;
1530             port->aggregator->lag_ports = port;
1531             port->aggregator->num_of_ports++;
1532 
1533             /* mark this port as selected */
1534             port->sm_vars |= AD_PORT_SELECTED;
1535 
1536             slave_dbg(bond->dev, port->slave->dev, "Port %d joined LAG %d (new LAG)\n",
1537                   port->actor_port_number,
1538                   port->aggregator->aggregator_identifier);
1539         } else {
1540             slave_err(bond->dev, port->slave->dev,
1541                   "Port %d did not find a suitable aggregator\n",
1542                   port->actor_port_number);
1543         }
1544     }
1545     /* if all aggregator's ports are READY_N == TRUE, set ready=TRUE
1546      * in all aggregator's ports, else set ready=FALSE in all
1547      * aggregator's ports
1548      */
1549     __set_agg_ports_ready(port->aggregator,
1550                   __agg_ports_are_ready(port->aggregator));
1551 
1552     aggregator = __get_first_agg(port);
1553     ad_agg_selection_logic(aggregator, update_slave_arr);
1554 
1555     if (!port->aggregator->is_active)
1556         port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
1557 }
1558 
1559 /* Decide if "agg" is a better choice for the new active aggregator that
1560  * the current best, according to the ad_select policy.
1561  */
1562 static struct aggregator *ad_agg_selection_test(struct aggregator *best,
1563                         struct aggregator *curr)
1564 {
1565     /* 0. If no best, select current.
1566      *
1567      * 1. If the current agg is not individual, and the best is
1568      *    individual, select current.
1569      *
1570      * 2. If current agg is individual and the best is not, keep best.
1571      *
1572      * 3. Therefore, current and best are both individual or both not
1573      *    individual, so:
1574      *
1575      * 3a. If current agg partner replied, and best agg partner did not,
1576      *     select current.
1577      *
1578      * 3b. If current agg partner did not reply and best agg partner
1579      *     did reply, keep best.
1580      *
1581      * 4.  Therefore, current and best both have partner replies or
1582      *     both do not, so perform selection policy:
1583      *
1584      * BOND_AD_COUNT: Select by count of ports.  If count is equal,
1585      *     select by bandwidth.
1586      *
1587      * BOND_AD_STABLE, BOND_AD_BANDWIDTH: Select by bandwidth.
1588      */
1589     if (!best)
1590         return curr;
1591 
1592     if (!curr->is_individual && best->is_individual)
1593         return curr;
1594 
1595     if (curr->is_individual && !best->is_individual)
1596         return best;
1597 
1598     if (__agg_has_partner(curr) && !__agg_has_partner(best))
1599         return curr;
1600 
1601     if (!__agg_has_partner(curr) && __agg_has_partner(best))
1602         return best;
1603 
1604     switch (__get_agg_selection_mode(curr->lag_ports)) {
1605     case BOND_AD_COUNT:
1606         if (__agg_active_ports(curr) > __agg_active_ports(best))
1607             return curr;
1608 
1609         if (__agg_active_ports(curr) < __agg_active_ports(best))
1610             return best;
1611 
1612         fallthrough;
1613     case BOND_AD_STABLE:
1614     case BOND_AD_BANDWIDTH:
1615         if (__get_agg_bandwidth(curr) > __get_agg_bandwidth(best))
1616             return curr;
1617 
1618         break;
1619 
1620     default:
1621         net_warn_ratelimited("%s: (slave %s): Impossible agg select mode %d\n",
1622                      curr->slave->bond->dev->name,
1623                      curr->slave->dev->name,
1624                      __get_agg_selection_mode(curr->lag_ports));
1625         break;
1626     }
1627 
1628     return best;
1629 }
1630 
1631 static int agg_device_up(const struct aggregator *agg)
1632 {
1633     struct port *port = agg->lag_ports;
1634 
1635     if (!port)
1636         return 0;
1637 
1638     for (port = agg->lag_ports; port;
1639          port = port->next_port_in_aggregator) {
1640         if (netif_running(port->slave->dev) &&
1641             netif_carrier_ok(port->slave->dev))
1642             return 1;
1643     }
1644 
1645     return 0;
1646 }
1647 
1648 /**
1649  * ad_agg_selection_logic - select an aggregation group for a team
1650  * @agg: the aggregator we're looking at
1651  * @update_slave_arr: Does slave array need update?
1652  *
1653  * It is assumed that only one aggregator may be selected for a team.
1654  *
1655  * The logic of this function is to select the aggregator according to
1656  * the ad_select policy:
1657  *
1658  * BOND_AD_STABLE: select the aggregator with the most ports attached to
1659  * it, and to reselect the active aggregator only if the previous
1660  * aggregator has no more ports related to it.
1661  *
1662  * BOND_AD_BANDWIDTH: select the aggregator with the highest total
1663  * bandwidth, and reselect whenever a link state change takes place or the
1664  * set of slaves in the bond changes.
1665  *
1666  * BOND_AD_COUNT: select the aggregator with largest number of ports
1667  * (slaves), and reselect whenever a link state change takes place or the
1668  * set of slaves in the bond changes.
1669  *
1670  * FIXME: this function MUST be called with the first agg in the bond, or
1671  * __get_active_agg() won't work correctly. This function should be better
1672  * called with the bond itself, and retrieve the first agg from it.
1673  */
1674 static void ad_agg_selection_logic(struct aggregator *agg,
1675                    bool *update_slave_arr)
1676 {
1677     struct aggregator *best, *active, *origin;
1678     struct bonding *bond = agg->slave->bond;
1679     struct list_head *iter;
1680     struct slave *slave;
1681     struct port *port;
1682 
1683     rcu_read_lock();
1684     origin = agg;
1685     active = __get_active_agg(agg);
1686     best = (active && agg_device_up(active)) ? active : NULL;
1687 
1688     bond_for_each_slave_rcu(bond, slave, iter) {
1689         agg = &(SLAVE_AD_INFO(slave)->aggregator);
1690 
1691         agg->is_active = 0;
1692 
1693         if (__agg_active_ports(agg) && agg_device_up(agg))
1694             best = ad_agg_selection_test(best, agg);
1695     }
1696 
1697     if (best &&
1698         __get_agg_selection_mode(best->lag_ports) == BOND_AD_STABLE) {
1699         /* For the STABLE policy, don't replace the old active
1700          * aggregator if it's still active (it has an answering
1701          * partner) or if both the best and active don't have an
1702          * answering partner.
1703          */
1704         if (active && active->lag_ports &&
1705             __agg_active_ports(active) &&
1706             (__agg_has_partner(active) ||
1707              (!__agg_has_partner(active) &&
1708              !__agg_has_partner(best)))) {
1709             if (!(!active->actor_oper_aggregator_key &&
1710                   best->actor_oper_aggregator_key)) {
1711                 best = NULL;
1712                 active->is_active = 1;
1713             }
1714         }
1715     }
1716 
1717     if (best && (best == active)) {
1718         best = NULL;
1719         active->is_active = 1;
1720     }
1721 
1722     /* if there is new best aggregator, activate it */
1723     if (best) {
1724         netdev_dbg(bond->dev, "(slave %s): best Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1725                best->slave ? best->slave->dev->name : "NULL",
1726                best->aggregator_identifier, best->num_of_ports,
1727                best->actor_oper_aggregator_key,
1728                best->partner_oper_aggregator_key,
1729                best->is_individual, best->is_active);
1730         netdev_dbg(bond->dev, "(slave %s): best ports %p slave %p\n",
1731                best->slave ? best->slave->dev->name : "NULL",
1732                best->lag_ports, best->slave);
1733 
1734         bond_for_each_slave_rcu(bond, slave, iter) {
1735             agg = &(SLAVE_AD_INFO(slave)->aggregator);
1736 
1737             slave_dbg(bond->dev, slave->dev, "Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1738                   agg->aggregator_identifier, agg->num_of_ports,
1739                   agg->actor_oper_aggregator_key,
1740                   agg->partner_oper_aggregator_key,
1741                   agg->is_individual, agg->is_active);
1742         }
1743 
1744         /* check if any partner replies */
1745         if (best->is_individual)
1746             net_warn_ratelimited("%s: Warning: No 802.3ad response from the link partner for any adapters in the bond\n",
1747                          bond->dev->name);
1748 
1749         best->is_active = 1;
1750         netdev_dbg(bond->dev, "(slave %s): LAG %d chosen as the active LAG\n",
1751                best->slave ? best->slave->dev->name : "NULL",
1752                best->aggregator_identifier);
1753         netdev_dbg(bond->dev, "(slave %s): Agg=%d; P=%d; a k=%d; p k=%d; Ind=%d; Act=%d\n",
1754                best->slave ? best->slave->dev->name : "NULL",
1755                best->aggregator_identifier, best->num_of_ports,
1756                best->actor_oper_aggregator_key,
1757                best->partner_oper_aggregator_key,
1758                best->is_individual, best->is_active);
1759 
1760         /* disable the ports that were related to the former
1761          * active_aggregator
1762          */
1763         if (active) {
1764             for (port = active->lag_ports; port;
1765                  port = port->next_port_in_aggregator) {
1766                 __disable_port(port);
1767             }
1768         }
1769         /* Slave array needs update. */
1770         *update_slave_arr = true;
1771     }
1772 
1773     /* if the selected aggregator is of join individuals
1774      * (partner_system is NULL), enable their ports
1775      */
1776     active = __get_active_agg(origin);
1777 
1778     if (active) {
1779         if (!__agg_has_partner(active)) {
1780             for (port = active->lag_ports; port;
1781                  port = port->next_port_in_aggregator) {
1782                 __enable_port(port);
1783             }
1784             *update_slave_arr = true;
1785         }
1786     }
1787 
1788     rcu_read_unlock();
1789 
1790     bond_3ad_set_carrier(bond);
1791 }
1792 
1793 /**
1794  * ad_clear_agg - clear a given aggregator's parameters
1795  * @aggregator: the aggregator we're looking at
1796  */
1797 static void ad_clear_agg(struct aggregator *aggregator)
1798 {
1799     if (aggregator) {
1800         aggregator->is_individual = false;
1801         aggregator->actor_admin_aggregator_key = 0;
1802         aggregator->actor_oper_aggregator_key = 0;
1803         eth_zero_addr(aggregator->partner_system.mac_addr_value);
1804         aggregator->partner_system_priority = 0;
1805         aggregator->partner_oper_aggregator_key = 0;
1806         aggregator->receive_state = 0;
1807         aggregator->transmit_state = 0;
1808         aggregator->lag_ports = NULL;
1809         aggregator->is_active = 0;
1810         aggregator->num_of_ports = 0;
1811         pr_debug("%s: LAG %d was cleared\n",
1812              aggregator->slave ?
1813              aggregator->slave->dev->name : "NULL",
1814              aggregator->aggregator_identifier);
1815     }
1816 }
1817 
1818 /**
1819  * ad_initialize_agg - initialize a given aggregator's parameters
1820  * @aggregator: the aggregator we're looking at
1821  */
1822 static void ad_initialize_agg(struct aggregator *aggregator)
1823 {
1824     if (aggregator) {
1825         ad_clear_agg(aggregator);
1826 
1827         eth_zero_addr(aggregator->aggregator_mac_address.mac_addr_value);
1828         aggregator->aggregator_identifier = 0;
1829         aggregator->slave = NULL;
1830     }
1831 }
1832 
1833 /**
1834  * ad_initialize_port - initialize a given port's parameters
1835  * @port: the port we're looking at
1836  * @lacp_fast: boolean. whether fast periodic should be used
1837  */
1838 static void ad_initialize_port(struct port *port, int lacp_fast)
1839 {
1840     static const struct port_params tmpl = {
1841         .system_priority = 0xffff,
1842         .key             = 1,
1843         .port_number     = 1,
1844         .port_priority   = 0xff,
1845         .port_state      = 1,
1846     };
1847     static const struct lacpdu lacpdu = {
1848         .subtype        = 0x01,
1849         .version_number = 0x01,
1850         .tlv_type_actor_info = 0x01,
1851         .actor_information_length = 0x14,
1852         .tlv_type_partner_info = 0x02,
1853         .partner_information_length = 0x14,
1854         .tlv_type_collector_info = 0x03,
1855         .collector_information_length = 0x10,
1856         .collector_max_delay = htons(AD_COLLECTOR_MAX_DELAY),
1857     };
1858 
1859     if (port) {
1860         port->actor_port_priority = 0xff;
1861         port->actor_port_aggregator_identifier = 0;
1862         port->ntt = false;
1863         port->actor_admin_port_state = LACP_STATE_AGGREGATION |
1864                            LACP_STATE_LACP_ACTIVITY;
1865         port->actor_oper_port_state  = LACP_STATE_AGGREGATION |
1866                            LACP_STATE_LACP_ACTIVITY;
1867 
1868         if (lacp_fast)
1869             port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
1870 
1871         memcpy(&port->partner_admin, &tmpl, sizeof(tmpl));
1872         memcpy(&port->partner_oper, &tmpl, sizeof(tmpl));
1873 
1874         port->is_enabled = true;
1875         /* private parameters */
1876         port->sm_vars = AD_PORT_BEGIN | AD_PORT_LACP_ENABLED;
1877         port->sm_rx_state = 0;
1878         port->sm_rx_timer_counter = 0;
1879         port->sm_periodic_state = 0;
1880         port->sm_periodic_timer_counter = 0;
1881         port->sm_mux_state = 0;
1882         port->sm_mux_timer_counter = 0;
1883         port->sm_tx_state = 0;
1884         port->aggregator = NULL;
1885         port->next_port_in_aggregator = NULL;
1886         port->transaction_id = 0;
1887 
1888         port->sm_churn_actor_timer_counter = 0;
1889         port->sm_churn_actor_state = 0;
1890         port->churn_actor_count = 0;
1891         port->sm_churn_partner_timer_counter = 0;
1892         port->sm_churn_partner_state = 0;
1893         port->churn_partner_count = 0;
1894 
1895         memcpy(&port->lacpdu, &lacpdu, sizeof(lacpdu));
1896     }
1897 }
1898 
1899 /**
1900  * ad_enable_collecting_distributing - enable a port's transmit/receive
1901  * @port: the port we're looking at
1902  * @update_slave_arr: Does slave array need update?
1903  *
1904  * Enable @port if it's in an active aggregator
1905  */
1906 static void ad_enable_collecting_distributing(struct port *port,
1907                           bool *update_slave_arr)
1908 {
1909     if (port->aggregator->is_active) {
1910         slave_dbg(port->slave->bond->dev, port->slave->dev,
1911               "Enabling port %d (LAG %d)\n",
1912               port->actor_port_number,
1913               port->aggregator->aggregator_identifier);
1914         __enable_port(port);
1915         /* Slave array needs update */
1916         *update_slave_arr = true;
1917     }
1918 }
1919 
1920 /**
1921  * ad_disable_collecting_distributing - disable a port's transmit/receive
1922  * @port: the port we're looking at
1923  * @update_slave_arr: Does slave array need update?
1924  */
1925 static void ad_disable_collecting_distributing(struct port *port,
1926                            bool *update_slave_arr)
1927 {
1928     if (port->aggregator &&
1929         !MAC_ADDRESS_EQUAL(&(port->aggregator->partner_system),
1930                    &(null_mac_addr))) {
1931         slave_dbg(port->slave->bond->dev, port->slave->dev,
1932               "Disabling port %d (LAG %d)\n",
1933               port->actor_port_number,
1934               port->aggregator->aggregator_identifier);
1935         __disable_port(port);
1936         /* Slave array needs an update */
1937         *update_slave_arr = true;
1938     }
1939 }
1940 
1941 /**
1942  * ad_marker_info_received - handle receive of a Marker information frame
1943  * @marker_info: Marker info received
1944  * @port: the port we're looking at
1945  */
1946 static void ad_marker_info_received(struct bond_marker *marker_info,
1947                     struct port *port)
1948 {
1949     struct bond_marker marker;
1950 
1951     atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_rx);
1952     atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_rx);
1953 
1954     /* copy the received marker data to the response marker */
1955     memcpy(&marker, marker_info, sizeof(struct bond_marker));
1956     /* change the marker subtype to marker response */
1957     marker.tlv_type = AD_MARKER_RESPONSE_SUBTYPE;
1958 
1959     /* send the marker response */
1960     if (ad_marker_send(port, &marker) >= 0)
1961         slave_dbg(port->slave->bond->dev, port->slave->dev,
1962               "Sent Marker Response on port %d\n",
1963               port->actor_port_number);
1964 }
1965 
1966 /**
1967  * ad_marker_response_received - handle receive of a marker response frame
1968  * @marker: marker PDU received
1969  * @port: the port we're looking at
1970  *
1971  * This function does nothing since we decided not to implement send and handle
1972  * response for marker PDU's, in this stage, but only to respond to marker
1973  * information.
1974  */
1975 static void ad_marker_response_received(struct bond_marker *marker,
1976                     struct port *port)
1977 {
1978     atomic64_inc(&SLAVE_AD_INFO(port->slave)->stats.marker_resp_rx);
1979     atomic64_inc(&BOND_AD_INFO(port->slave->bond).stats.marker_resp_rx);
1980 
1981     /* DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW */
1982 }
1983 
1984 /* ========= AD exported functions to the main bonding code ========= */
1985 
1986 /* Check aggregators status in team every T seconds */
1987 #define AD_AGGREGATOR_SELECTION_TIMER  8
1988 
1989 /**
1990  * bond_3ad_initiate_agg_selection - initate aggregator selection
1991  * @bond: bonding struct
1992  * @timeout: timeout value to set
1993  *
1994  * Set the aggregation selection timer, to initiate an agg selection in
1995  * the very near future.  Called during first initialization, and during
1996  * any down to up transitions of the bond.
1997  */
1998 void bond_3ad_initiate_agg_selection(struct bonding *bond, int timeout)
1999 {
2000     atomic_set(&BOND_AD_INFO(bond).agg_select_timer, timeout);
2001 }
2002 
2003 /**
2004  * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
2005  * @bond: bonding struct to work on
2006  *
2007  * Can be called only after the mac address of the bond is set.
2008  */
2009 void bond_3ad_initialize(struct bonding *bond)
2010 {
2011     BOND_AD_INFO(bond).aggregator_identifier = 0;
2012     BOND_AD_INFO(bond).system.sys_priority =
2013         bond->params.ad_actor_sys_prio;
2014     if (is_zero_ether_addr(bond->params.ad_actor_system))
2015         BOND_AD_INFO(bond).system.sys_mac_addr =
2016             *((struct mac_addr *)bond->dev->dev_addr);
2017     else
2018         BOND_AD_INFO(bond).system.sys_mac_addr =
2019             *((struct mac_addr *)bond->params.ad_actor_system);
2020 
2021     bond_3ad_initiate_agg_selection(bond,
2022                     AD_AGGREGATOR_SELECTION_TIMER *
2023                     ad_ticks_per_sec);
2024 }
2025 
2026 /**
2027  * bond_3ad_bind_slave - initialize a slave's port
2028  * @slave: slave struct to work on
2029  *
2030  * Returns:   0 on success
2031  *          < 0 on error
2032  */
2033 void bond_3ad_bind_slave(struct slave *slave)
2034 {
2035     struct bonding *bond = bond_get_bond_by_slave(slave);
2036     struct port *port;
2037     struct aggregator *aggregator;
2038 
2039     /* check that the slave has not been initialized yet. */
2040     if (SLAVE_AD_INFO(slave)->port.slave != slave) {
2041 
2042         /* port initialization */
2043         port = &(SLAVE_AD_INFO(slave)->port);
2044 
2045         ad_initialize_port(port, bond->params.lacp_fast);
2046 
2047         port->slave = slave;
2048         port->actor_port_number = SLAVE_AD_INFO(slave)->id;
2049         /* key is determined according to the link speed, duplex and
2050          * user key
2051          */
2052         port->actor_admin_port_key = bond->params.ad_user_port_key << 6;
2053         ad_update_actor_keys(port, false);
2054         /* actor system is the bond's system */
2055         __ad_actor_update_port(port);
2056         /* tx timer(to verify that no more than MAX_TX_IN_SECOND
2057          * lacpdu's are sent in one second)
2058          */
2059         port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
2060 
2061         __disable_port(port);
2062 
2063         /* aggregator initialization */
2064         aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2065 
2066         ad_initialize_agg(aggregator);
2067 
2068         aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
2069         aggregator->aggregator_identifier = ++BOND_AD_INFO(bond).aggregator_identifier;
2070         aggregator->slave = slave;
2071         aggregator->is_active = 0;
2072         aggregator->num_of_ports = 0;
2073     }
2074 }
2075 
2076 /**
2077  * bond_3ad_unbind_slave - deinitialize a slave's port
2078  * @slave: slave struct to work on
2079  *
2080  * Search for the aggregator that is related to this port, remove the
2081  * aggregator and assign another aggregator for other port related to it
2082  * (if any), and remove the port.
2083  */
2084 void bond_3ad_unbind_slave(struct slave *slave)
2085 {
2086     struct port *port, *prev_port, *temp_port;
2087     struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
2088     int select_new_active_agg = 0;
2089     struct bonding *bond = slave->bond;
2090     struct slave *slave_iter;
2091     struct list_head *iter;
2092     bool dummy_slave_update; /* Ignore this value as caller updates array */
2093 
2094     /* Sync against bond_3ad_state_machine_handler() */
2095     spin_lock_bh(&bond->mode_lock);
2096     aggregator = &(SLAVE_AD_INFO(slave)->aggregator);
2097     port = &(SLAVE_AD_INFO(slave)->port);
2098 
2099     /* if slave is null, the whole port is not initialized */
2100     if (!port->slave) {
2101         slave_warn(bond->dev, slave->dev, "Trying to unbind an uninitialized port\n");
2102         goto out;
2103     }
2104 
2105     slave_dbg(bond->dev, slave->dev, "Unbinding Link Aggregation Group %d\n",
2106           aggregator->aggregator_identifier);
2107 
2108     /* Tell the partner that this port is not suitable for aggregation */
2109     port->actor_oper_port_state &= ~LACP_STATE_SYNCHRONIZATION;
2110     port->actor_oper_port_state &= ~LACP_STATE_COLLECTING;
2111     port->actor_oper_port_state &= ~LACP_STATE_DISTRIBUTING;
2112     port->actor_oper_port_state &= ~LACP_STATE_AGGREGATION;
2113     __update_lacpdu_from_port(port);
2114     ad_lacpdu_send(port);
2115 
2116     /* check if this aggregator is occupied */
2117     if (aggregator->lag_ports) {
2118         /* check if there are other ports related to this aggregator
2119          * except the port related to this slave(thats ensure us that
2120          * there is a reason to search for new aggregator, and that we
2121          * will find one
2122          */
2123         if ((aggregator->lag_ports != port) ||
2124             (aggregator->lag_ports->next_port_in_aggregator)) {
2125             /* find new aggregator for the related port(s) */
2126             bond_for_each_slave(bond, slave_iter, iter) {
2127                 new_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2128                 /* if the new aggregator is empty, or it is
2129                  * connected to our port only
2130                  */
2131                 if (!new_aggregator->lag_ports ||
2132                     ((new_aggregator->lag_ports == port) &&
2133                      !new_aggregator->lag_ports->next_port_in_aggregator))
2134                     break;
2135             }
2136             if (!slave_iter)
2137                 new_aggregator = NULL;
2138 
2139             /* if new aggregator found, copy the aggregator's
2140              * parameters and connect the related lag_ports to the
2141              * new aggregator
2142              */
2143             if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
2144                 slave_dbg(bond->dev, slave->dev, "Some port(s) related to LAG %d - replacing with LAG %d\n",
2145                       aggregator->aggregator_identifier,
2146                       new_aggregator->aggregator_identifier);
2147 
2148                 if ((new_aggregator->lag_ports == port) &&
2149                     new_aggregator->is_active) {
2150                     slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2151                     select_new_active_agg = 1;
2152                 }
2153 
2154                 new_aggregator->is_individual = aggregator->is_individual;
2155                 new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
2156                 new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
2157                 new_aggregator->partner_system = aggregator->partner_system;
2158                 new_aggregator->partner_system_priority = aggregator->partner_system_priority;
2159                 new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
2160                 new_aggregator->receive_state = aggregator->receive_state;
2161                 new_aggregator->transmit_state = aggregator->transmit_state;
2162                 new_aggregator->lag_ports = aggregator->lag_ports;
2163                 new_aggregator->is_active = aggregator->is_active;
2164                 new_aggregator->num_of_ports = aggregator->num_of_ports;
2165 
2166                 /* update the information that is written on
2167                  * the ports about the aggregator
2168                  */
2169                 for (temp_port = aggregator->lag_ports; temp_port;
2170                      temp_port = temp_port->next_port_in_aggregator) {
2171                     temp_port->aggregator = new_aggregator;
2172                     temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
2173                 }
2174 
2175                 ad_clear_agg(aggregator);
2176 
2177                 if (select_new_active_agg)
2178                     ad_agg_selection_logic(__get_first_agg(port),
2179                                    &dummy_slave_update);
2180             } else {
2181                 slave_warn(bond->dev, slave->dev, "unbinding aggregator, and could not find a new aggregator for its ports\n");
2182             }
2183         } else {
2184             /* in case that the only port related to this
2185              * aggregator is the one we want to remove
2186              */
2187             select_new_active_agg = aggregator->is_active;
2188             ad_clear_agg(aggregator);
2189             if (select_new_active_agg) {
2190                 slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2191                 /* select new active aggregator */
2192                 temp_aggregator = __get_first_agg(port);
2193                 if (temp_aggregator)
2194                     ad_agg_selection_logic(temp_aggregator,
2195                                    &dummy_slave_update);
2196             }
2197         }
2198     }
2199 
2200     slave_dbg(bond->dev, slave->dev, "Unbinding port %d\n", port->actor_port_number);
2201 
2202     /* find the aggregator that this port is connected to */
2203     bond_for_each_slave(bond, slave_iter, iter) {
2204         temp_aggregator = &(SLAVE_AD_INFO(slave_iter)->aggregator);
2205         prev_port = NULL;
2206         /* search the port in the aggregator's related ports */
2207         for (temp_port = temp_aggregator->lag_ports; temp_port;
2208              prev_port = temp_port,
2209              temp_port = temp_port->next_port_in_aggregator) {
2210             if (temp_port == port) {
2211                 /* the aggregator found - detach the port from
2212                  * this aggregator
2213                  */
2214                 if (prev_port)
2215                     prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
2216                 else
2217                     temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
2218                 temp_aggregator->num_of_ports--;
2219                 if (__agg_active_ports(temp_aggregator) == 0) {
2220                     select_new_active_agg = temp_aggregator->is_active;
2221                     if (temp_aggregator->num_of_ports == 0)
2222                         ad_clear_agg(temp_aggregator);
2223                     if (select_new_active_agg) {
2224                         slave_info(bond->dev, slave->dev, "Removing an active aggregator\n");
2225                         /* select new active aggregator */
2226                         ad_agg_selection_logic(__get_first_agg(port),
2227                                            &dummy_slave_update);
2228                     }
2229                 }
2230                 break;
2231             }
2232         }
2233     }
2234     port->slave = NULL;
2235 
2236 out:
2237     spin_unlock_bh(&bond->mode_lock);
2238 }
2239 
2240 /**
2241  * bond_3ad_update_ad_actor_settings - reflect change of actor settings to ports
2242  * @bond: bonding struct to work on
2243  *
2244  * If an ad_actor setting gets changed we need to update the individual port
2245  * settings so the bond device will use the new values when it gets upped.
2246  */
2247 void bond_3ad_update_ad_actor_settings(struct bonding *bond)
2248 {
2249     struct list_head *iter;
2250     struct slave *slave;
2251 
2252     ASSERT_RTNL();
2253 
2254     BOND_AD_INFO(bond).system.sys_priority = bond->params.ad_actor_sys_prio;
2255     if (is_zero_ether_addr(bond->params.ad_actor_system))
2256         BOND_AD_INFO(bond).system.sys_mac_addr =
2257             *((struct mac_addr *)bond->dev->dev_addr);
2258     else
2259         BOND_AD_INFO(bond).system.sys_mac_addr =
2260             *((struct mac_addr *)bond->params.ad_actor_system);
2261 
2262     spin_lock_bh(&bond->mode_lock);
2263     bond_for_each_slave(bond, slave, iter) {
2264         struct port *port = &(SLAVE_AD_INFO(slave))->port;
2265 
2266         __ad_actor_update_port(port);
2267         port->ntt = true;
2268     }
2269     spin_unlock_bh(&bond->mode_lock);
2270 }
2271 
2272 /**
2273  * bond_agg_timer_advance - advance agg_select_timer
2274  * @bond:  bonding structure
2275  *
2276  * Return true when agg_select_timer reaches 0.
2277  */
2278 static bool bond_agg_timer_advance(struct bonding *bond)
2279 {
2280     int val, nval;
2281 
2282     while (1) {
2283         val = atomic_read(&BOND_AD_INFO(bond).agg_select_timer);
2284         if (!val)
2285             return false;
2286         nval = val - 1;
2287         if (atomic_cmpxchg(&BOND_AD_INFO(bond).agg_select_timer,
2288                    val, nval) == val)
2289             break;
2290     }
2291     return nval == 0;
2292 }
2293 
2294 /**
2295  * bond_3ad_state_machine_handler - handle state machines timeout
2296  * @work: work context to fetch bonding struct to work on from
2297  *
2298  * The state machine handling concept in this module is to check every tick
2299  * which state machine should operate any function. The execution order is
2300  * round robin, so when we have an interaction between state machines, the
2301  * reply of one to each other might be delayed until next tick.
2302  *
2303  * This function also complete the initialization when the agg_select_timer
2304  * times out, and it selects an aggregator for the ports that are yet not
2305  * related to any aggregator, and selects the active aggregator for a bond.
2306  */
2307 void bond_3ad_state_machine_handler(struct work_struct *work)
2308 {
2309     struct bonding *bond = container_of(work, struct bonding,
2310                         ad_work.work);
2311     struct aggregator *aggregator;
2312     struct list_head *iter;
2313     struct slave *slave;
2314     struct port *port;
2315     bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
2316     bool update_slave_arr = false;
2317 
2318     /* Lock to protect data accessed by all (e.g., port->sm_vars) and
2319      * against running with bond_3ad_unbind_slave. ad_rx_machine may run
2320      * concurrently due to incoming LACPDU as well.
2321      */
2322     spin_lock_bh(&bond->mode_lock);
2323     rcu_read_lock();
2324 
2325     /* check if there are any slaves */
2326     if (!bond_has_slaves(bond))
2327         goto re_arm;
2328 
2329     if (bond_agg_timer_advance(bond)) {
2330         slave = bond_first_slave_rcu(bond);
2331         port = slave ? &(SLAVE_AD_INFO(slave)->port) : NULL;
2332 
2333         /* select the active aggregator for the bond */
2334         if (port) {
2335             if (!port->slave) {
2336                 net_warn_ratelimited("%s: Warning: bond's first port is uninitialized\n",
2337                              bond->dev->name);
2338                 goto re_arm;
2339             }
2340 
2341             aggregator = __get_first_agg(port);
2342             ad_agg_selection_logic(aggregator, &update_slave_arr);
2343         }
2344         bond_3ad_set_carrier(bond);
2345     }
2346 
2347     /* for each port run the state machines */
2348     bond_for_each_slave_rcu(bond, slave, iter) {
2349         port = &(SLAVE_AD_INFO(slave)->port);
2350         if (!port->slave) {
2351             net_warn_ratelimited("%s: Warning: Found an uninitialized port\n",
2352                         bond->dev->name);
2353             goto re_arm;
2354         }
2355 
2356         ad_rx_machine(NULL, port);
2357         ad_periodic_machine(port, &bond->params);
2358         ad_port_selection_logic(port, &update_slave_arr);
2359         ad_mux_machine(port, &update_slave_arr);
2360         ad_tx_machine(port);
2361         ad_churn_machine(port);
2362 
2363         /* turn off the BEGIN bit, since we already handled it */
2364         if (port->sm_vars & AD_PORT_BEGIN)
2365             port->sm_vars &= ~AD_PORT_BEGIN;
2366     }
2367 
2368 re_arm:
2369     bond_for_each_slave_rcu(bond, slave, iter) {
2370         if (slave->should_notify) {
2371             should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
2372             break;
2373         }
2374     }
2375     rcu_read_unlock();
2376     spin_unlock_bh(&bond->mode_lock);
2377 
2378     if (update_slave_arr)
2379         bond_slave_arr_work_rearm(bond, 0);
2380 
2381     if (should_notify_rtnl && rtnl_trylock()) {
2382         bond_slave_state_notify(bond);
2383         rtnl_unlock();
2384     }
2385     queue_delayed_work(bond->wq, &bond->ad_work, ad_delta_in_ticks);
2386 }
2387 
2388 /**
2389  * bond_3ad_rx_indication - handle a received frame
2390  * @lacpdu: received lacpdu
2391  * @slave: slave struct to work on
2392  *
2393  * It is assumed that frames that were sent on this NIC don't returned as new
2394  * received frames (loopback). Since only the payload is given to this
2395  * function, it check for loopback.
2396  */
2397 static int bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave)
2398 {
2399     struct bonding *bond = slave->bond;
2400     int ret = RX_HANDLER_ANOTHER;
2401     struct bond_marker *marker;
2402     struct port *port;
2403     atomic64_t *stat;
2404 
2405     port = &(SLAVE_AD_INFO(slave)->port);
2406     if (!port->slave) {
2407         net_warn_ratelimited("%s: Warning: port of slave %s is uninitialized\n",
2408                      slave->dev->name, slave->bond->dev->name);
2409         return ret;
2410     }
2411 
2412     switch (lacpdu->subtype) {
2413     case AD_TYPE_LACPDU:
2414         ret = RX_HANDLER_CONSUMED;
2415         slave_dbg(slave->bond->dev, slave->dev,
2416               "Received LACPDU on port %d\n",
2417               port->actor_port_number);
2418         /* Protect against concurrent state machines */
2419         spin_lock(&slave->bond->mode_lock);
2420         ad_rx_machine(lacpdu, port);
2421         spin_unlock(&slave->bond->mode_lock);
2422         break;
2423     case AD_TYPE_MARKER:
2424         ret = RX_HANDLER_CONSUMED;
2425         /* No need to convert fields to Little Endian since we
2426          * don't use the marker's fields.
2427          */
2428         marker = (struct bond_marker *)lacpdu;
2429         switch (marker->tlv_type) {
2430         case AD_MARKER_INFORMATION_SUBTYPE:
2431             slave_dbg(slave->bond->dev, slave->dev, "Received Marker Information on port %d\n",
2432                   port->actor_port_number);
2433             ad_marker_info_received(marker, port);
2434             break;
2435         case AD_MARKER_RESPONSE_SUBTYPE:
2436             slave_dbg(slave->bond->dev, slave->dev, "Received Marker Response on port %d\n",
2437                   port->actor_port_number);
2438             ad_marker_response_received(marker, port);
2439             break;
2440         default:
2441             slave_dbg(slave->bond->dev, slave->dev, "Received an unknown Marker subtype on port %d\n",
2442                   port->actor_port_number);
2443             stat = &SLAVE_AD_INFO(slave)->stats.marker_unknown_rx;
2444             atomic64_inc(stat);
2445             stat = &BOND_AD_INFO(bond).stats.marker_unknown_rx;
2446             atomic64_inc(stat);
2447         }
2448         break;
2449     default:
2450         atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_unknown_rx);
2451         atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_unknown_rx);
2452     }
2453 
2454     return ret;
2455 }
2456 
2457 /**
2458  * ad_update_actor_keys - Update the oper / admin keys for a port based on
2459  * its current speed and duplex settings.
2460  *
2461  * @port: the port we'are looking at
2462  * @reset: Boolean to just reset the speed and the duplex part of the key
2463  *
2464  * The logic to change the oper / admin keys is:
2465  * (a) A full duplex port can participate in LACP with partner.
2466  * (b) When the speed is changed, LACP need to be reinitiated.
2467  */
2468 static void ad_update_actor_keys(struct port *port, bool reset)
2469 {
2470     u8 duplex = 0;
2471     u16 ospeed = 0, speed = 0;
2472     u16 old_oper_key = port->actor_oper_port_key;
2473 
2474     port->actor_admin_port_key &= ~(AD_SPEED_KEY_MASKS|AD_DUPLEX_KEY_MASKS);
2475     if (!reset) {
2476         speed = __get_link_speed(port);
2477         ospeed = (old_oper_key & AD_SPEED_KEY_MASKS) >> 1;
2478         duplex = __get_duplex(port);
2479         port->actor_admin_port_key |= (speed << 1) | duplex;
2480     }
2481     port->actor_oper_port_key = port->actor_admin_port_key;
2482 
2483     if (old_oper_key != port->actor_oper_port_key) {
2484         /* Only 'duplex' port participates in LACP */
2485         if (duplex)
2486             port->sm_vars |= AD_PORT_LACP_ENABLED;
2487         else
2488             port->sm_vars &= ~AD_PORT_LACP_ENABLED;
2489 
2490         if (!reset) {
2491             if (!speed) {
2492                 slave_err(port->slave->bond->dev,
2493                       port->slave->dev,
2494                       "speed changed to 0 on port %d\n",
2495                       port->actor_port_number);
2496             } else if (duplex && ospeed != speed) {
2497                 /* Speed change restarts LACP state-machine */
2498                 port->sm_vars |= AD_PORT_BEGIN;
2499             }
2500         }
2501     }
2502 }
2503 
2504 /**
2505  * bond_3ad_adapter_speed_duplex_changed - handle a slave's speed / duplex
2506  * change indication
2507  *
2508  * @slave: slave struct to work on
2509  *
2510  * Handle reselection of aggregator (if needed) for this port.
2511  */
2512 void bond_3ad_adapter_speed_duplex_changed(struct slave *slave)
2513 {
2514     struct port *port;
2515 
2516     port = &(SLAVE_AD_INFO(slave)->port);
2517 
2518     /* if slave is null, the whole port is not initialized */
2519     if (!port->slave) {
2520         slave_warn(slave->bond->dev, slave->dev,
2521                "speed/duplex changed for uninitialized port\n");
2522         return;
2523     }
2524 
2525     spin_lock_bh(&slave->bond->mode_lock);
2526     ad_update_actor_keys(port, false);
2527     spin_unlock_bh(&slave->bond->mode_lock);
2528     slave_dbg(slave->bond->dev, slave->dev, "Port %d changed speed/duplex\n",
2529           port->actor_port_number);
2530 }
2531 
2532 /**
2533  * bond_3ad_handle_link_change - handle a slave's link status change indication
2534  * @slave: slave struct to work on
2535  * @link: whether the link is now up or down
2536  *
2537  * Handle reselection of aggregator (if needed) for this port.
2538  */
2539 void bond_3ad_handle_link_change(struct slave *slave, char link)
2540 {
2541     struct aggregator *agg;
2542     struct port *port;
2543     bool dummy;
2544 
2545     port = &(SLAVE_AD_INFO(slave)->port);
2546 
2547     /* if slave is null, the whole port is not initialized */
2548     if (!port->slave) {
2549         slave_warn(slave->bond->dev, slave->dev, "link status changed for uninitialized port\n");
2550         return;
2551     }
2552 
2553     spin_lock_bh(&slave->bond->mode_lock);
2554     /* on link down we are zeroing duplex and speed since
2555      * some of the adaptors(ce1000.lan) report full duplex/speed
2556      * instead of N/A(duplex) / 0(speed).
2557      *
2558      * on link up we are forcing recheck on the duplex and speed since
2559      * some of he adaptors(ce1000.lan) report.
2560      */
2561     if (link == BOND_LINK_UP) {
2562         port->is_enabled = true;
2563         ad_update_actor_keys(port, false);
2564     } else {
2565         /* link has failed */
2566         port->is_enabled = false;
2567         ad_update_actor_keys(port, true);
2568     }
2569     agg = __get_first_agg(port);
2570     ad_agg_selection_logic(agg, &dummy);
2571 
2572     spin_unlock_bh(&slave->bond->mode_lock);
2573 
2574     slave_dbg(slave->bond->dev, slave->dev, "Port %d changed link status to %s\n",
2575           port->actor_port_number,
2576           link == BOND_LINK_UP ? "UP" : "DOWN");
2577 
2578     /* RTNL is held and mode_lock is released so it's safe
2579      * to update slave_array here.
2580      */
2581     bond_update_slave_arr(slave->bond, NULL);
2582 }
2583 
2584 /**
2585  * bond_3ad_set_carrier - set link state for bonding master
2586  * @bond: bonding structure
2587  *
2588  * if we have an active aggregator, we're up, if not, we're down.
2589  * Presumes that we cannot have an active aggregator if there are
2590  * no slaves with link up.
2591  *
2592  * This behavior complies with IEEE 802.3 section 43.3.9.
2593  *
2594  * Called by bond_set_carrier(). Return zero if carrier state does not
2595  * change, nonzero if it does.
2596  */
2597 int bond_3ad_set_carrier(struct bonding *bond)
2598 {
2599     struct aggregator *active;
2600     struct slave *first_slave;
2601     int ret = 1;
2602 
2603     rcu_read_lock();
2604     first_slave = bond_first_slave_rcu(bond);
2605     if (!first_slave) {
2606         ret = 0;
2607         goto out;
2608     }
2609     active = __get_active_agg(&(SLAVE_AD_INFO(first_slave)->aggregator));
2610     if (active) {
2611         /* are enough slaves available to consider link up? */
2612         if (__agg_active_ports(active) < bond->params.min_links) {
2613             if (netif_carrier_ok(bond->dev)) {
2614                 netif_carrier_off(bond->dev);
2615                 goto out;
2616             }
2617         } else if (!netif_carrier_ok(bond->dev)) {
2618             netif_carrier_on(bond->dev);
2619             goto out;
2620         }
2621     } else if (netif_carrier_ok(bond->dev)) {
2622         netif_carrier_off(bond->dev);
2623     }
2624 out:
2625     rcu_read_unlock();
2626     return ret;
2627 }
2628 
2629 /**
2630  * __bond_3ad_get_active_agg_info - get information of the active aggregator
2631  * @bond: bonding struct to work on
2632  * @ad_info: ad_info struct to fill with the bond's info
2633  *
2634  * Returns:   0 on success
2635  *          < 0 on error
2636  */
2637 int __bond_3ad_get_active_agg_info(struct bonding *bond,
2638                    struct ad_info *ad_info)
2639 {
2640     struct aggregator *aggregator = NULL;
2641     struct list_head *iter;
2642     struct slave *slave;
2643     struct port *port;
2644 
2645     bond_for_each_slave_rcu(bond, slave, iter) {
2646         port = &(SLAVE_AD_INFO(slave)->port);
2647         if (port->aggregator && port->aggregator->is_active) {
2648             aggregator = port->aggregator;
2649             break;
2650         }
2651     }
2652 
2653     if (!aggregator)
2654         return -1;
2655 
2656     ad_info->aggregator_id = aggregator->aggregator_identifier;
2657     ad_info->ports = __agg_active_ports(aggregator);
2658     ad_info->actor_key = aggregator->actor_oper_aggregator_key;
2659     ad_info->partner_key = aggregator->partner_oper_aggregator_key;
2660     ether_addr_copy(ad_info->partner_system,
2661             aggregator->partner_system.mac_addr_value);
2662     return 0;
2663 }
2664 
2665 int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
2666 {
2667     int ret;
2668 
2669     rcu_read_lock();
2670     ret = __bond_3ad_get_active_agg_info(bond, ad_info);
2671     rcu_read_unlock();
2672 
2673     return ret;
2674 }
2675 
2676 int bond_3ad_lacpdu_recv(const struct sk_buff *skb, struct bonding *bond,
2677              struct slave *slave)
2678 {
2679     struct lacpdu *lacpdu, _lacpdu;
2680 
2681     if (skb->protocol != PKT_TYPE_LACPDU)
2682         return RX_HANDLER_ANOTHER;
2683 
2684     if (!MAC_ADDRESS_EQUAL(eth_hdr(skb)->h_dest, lacpdu_mcast_addr))
2685         return RX_HANDLER_ANOTHER;
2686 
2687     lacpdu = skb_header_pointer(skb, 0, sizeof(_lacpdu), &_lacpdu);
2688     if (!lacpdu) {
2689         atomic64_inc(&SLAVE_AD_INFO(slave)->stats.lacpdu_illegal_rx);
2690         atomic64_inc(&BOND_AD_INFO(bond).stats.lacpdu_illegal_rx);
2691         return RX_HANDLER_ANOTHER;
2692     }
2693 
2694     return bond_3ad_rx_indication(lacpdu, slave);
2695 }
2696 
2697 /**
2698  * bond_3ad_update_lacp_rate - change the lacp rate
2699  * @bond: bonding struct
2700  *
2701  * When modify lacp_rate parameter via sysfs,
2702  * update actor_oper_port_state of each port.
2703  *
2704  * Hold bond->mode_lock,
2705  * so we can modify port->actor_oper_port_state,
2706  * no matter bond is up or down.
2707  */
2708 void bond_3ad_update_lacp_rate(struct bonding *bond)
2709 {
2710     struct port *port = NULL;
2711     struct list_head *iter;
2712     struct slave *slave;
2713     int lacp_fast;
2714 
2715     lacp_fast = bond->params.lacp_fast;
2716     spin_lock_bh(&bond->mode_lock);
2717     bond_for_each_slave(bond, slave, iter) {
2718         port = &(SLAVE_AD_INFO(slave)->port);
2719         if (lacp_fast)
2720             port->actor_oper_port_state |= LACP_STATE_LACP_TIMEOUT;
2721         else
2722             port->actor_oper_port_state &= ~LACP_STATE_LACP_TIMEOUT;
2723     }
2724     spin_unlock_bh(&bond->mode_lock);
2725 }
2726 
2727 size_t bond_3ad_stats_size(void)
2728 {
2729     return nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_RX */
2730            nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_TX */
2731            nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_UNKNOWN_RX */
2732            nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_LACPDU_ILLEGAL_RX */
2733            nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RX */
2734            nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_TX */
2735            nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_RX */
2736            nla_total_size_64bit(sizeof(u64)) + /* BOND_3AD_STAT_MARKER_RESP_TX */
2737            nla_total_size_64bit(sizeof(u64)); /* BOND_3AD_STAT_MARKER_UNKNOWN_RX */
2738 }
2739 
2740 int bond_3ad_stats_fill(struct sk_buff *skb, struct bond_3ad_stats *stats)
2741 {
2742     u64 val;
2743 
2744     val = atomic64_read(&stats->lacpdu_rx);
2745     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_RX, val,
2746                   BOND_3AD_STAT_PAD))
2747         return -EMSGSIZE;
2748     val = atomic64_read(&stats->lacpdu_tx);
2749     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_TX, val,
2750                   BOND_3AD_STAT_PAD))
2751         return -EMSGSIZE;
2752     val = atomic64_read(&stats->lacpdu_unknown_rx);
2753     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_UNKNOWN_RX, val,
2754                   BOND_3AD_STAT_PAD))
2755         return -EMSGSIZE;
2756     val = atomic64_read(&stats->lacpdu_illegal_rx);
2757     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_LACPDU_ILLEGAL_RX, val,
2758                   BOND_3AD_STAT_PAD))
2759         return -EMSGSIZE;
2760 
2761     val = atomic64_read(&stats->marker_rx);
2762     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RX, val,
2763                   BOND_3AD_STAT_PAD))
2764         return -EMSGSIZE;
2765     val = atomic64_read(&stats->marker_tx);
2766     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_TX, val,
2767                   BOND_3AD_STAT_PAD))
2768         return -EMSGSIZE;
2769     val = atomic64_read(&stats->marker_resp_rx);
2770     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_RX, val,
2771                   BOND_3AD_STAT_PAD))
2772         return -EMSGSIZE;
2773     val = atomic64_read(&stats->marker_resp_tx);
2774     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_RESP_TX, val,
2775                   BOND_3AD_STAT_PAD))
2776         return -EMSGSIZE;
2777     val = atomic64_read(&stats->marker_unknown_rx);
2778     if (nla_put_u64_64bit(skb, BOND_3AD_STAT_MARKER_UNKNOWN_RX, val,
2779                   BOND_3AD_STAT_PAD))
2780         return -EMSGSIZE;
2781 
2782     return 0;
2783 }