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/netdevice.h>
0008 #include <linux/etherdevice.h>
0009 #include <linux/pkt_sched.h>
0010 #include <linux/spinlock.h>
0011 #include <linux/slab.h>
0012 #include <linux/timer.h>
0013 #include <linux/ip.h>
0014 #include <linux/ipv6.h>
0015 #include <linux/if_arp.h>
0016 #include <linux/if_ether.h>
0017 #include <linux/if_bonding.h>
0018 #include <linux/if_vlan.h>
0019 #include <linux/in.h>
0020 #include <net/arp.h>
0021 #include <net/ipv6.h>
0022 #include <net/ndisc.h>
0023 #include <asm/byteorder.h>
0024 #include <net/bonding.h>
0025 #include <net/bond_alb.h>
0026 
0027 static const u8 mac_v6_allmcast[ETH_ALEN + 2] __long_aligned = {
0028     0x33, 0x33, 0x00, 0x00, 0x00, 0x01
0029 };
0030 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
0031 
0032 #pragma pack(1)
0033 struct learning_pkt {
0034     u8 mac_dst[ETH_ALEN];
0035     u8 mac_src[ETH_ALEN];
0036     __be16 type;
0037     u8 padding[ETH_ZLEN - ETH_HLEN];
0038 };
0039 
0040 struct arp_pkt {
0041     __be16  hw_addr_space;
0042     __be16  prot_addr_space;
0043     u8      hw_addr_len;
0044     u8      prot_addr_len;
0045     __be16  op_code;
0046     u8      mac_src[ETH_ALEN];  /* sender hardware address */
0047     __be32  ip_src;         /* sender IP address */
0048     u8      mac_dst[ETH_ALEN];  /* target hardware address */
0049     __be32  ip_dst;         /* target IP address */
0050 };
0051 #pragma pack()
0052 
0053 /* Forward declaration */
0054 static void alb_send_learning_packets(struct slave *slave, const u8 mac_addr[],
0055                       bool strict_match);
0056 static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp);
0057 static void rlb_src_unlink(struct bonding *bond, u32 index);
0058 static void rlb_src_link(struct bonding *bond, u32 ip_src_hash,
0059              u32 ip_dst_hash);
0060 
0061 static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
0062 {
0063     int i;
0064     u8 hash = 0;
0065 
0066     for (i = 0; i < hash_size; i++)
0067         hash ^= hash_start[i];
0068 
0069     return hash;
0070 }
0071 
0072 /*********************** tlb specific functions ***************************/
0073 
0074 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
0075 {
0076     if (save_load) {
0077         entry->load_history = 1 + entry->tx_bytes /
0078                       BOND_TLB_REBALANCE_INTERVAL;
0079         entry->tx_bytes = 0;
0080     }
0081 
0082     entry->tx_slave = NULL;
0083     entry->next = TLB_NULL_INDEX;
0084     entry->prev = TLB_NULL_INDEX;
0085 }
0086 
0087 static inline void tlb_init_slave(struct slave *slave)
0088 {
0089     SLAVE_TLB_INFO(slave).load = 0;
0090     SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
0091 }
0092 
0093 static void __tlb_clear_slave(struct bonding *bond, struct slave *slave,
0094              int save_load)
0095 {
0096     struct tlb_client_info *tx_hash_table;
0097     u32 index;
0098 
0099     /* clear slave from tx_hashtbl */
0100     tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
0101 
0102     /* skip this if we've already freed the tx hash table */
0103     if (tx_hash_table) {
0104         index = SLAVE_TLB_INFO(slave).head;
0105         while (index != TLB_NULL_INDEX) {
0106             u32 next_index = tx_hash_table[index].next;
0107 
0108             tlb_init_table_entry(&tx_hash_table[index], save_load);
0109             index = next_index;
0110         }
0111     }
0112 
0113     tlb_init_slave(slave);
0114 }
0115 
0116 static void tlb_clear_slave(struct bonding *bond, struct slave *slave,
0117              int save_load)
0118 {
0119     spin_lock_bh(&bond->mode_lock);
0120     __tlb_clear_slave(bond, slave, save_load);
0121     spin_unlock_bh(&bond->mode_lock);
0122 }
0123 
0124 /* Must be called before starting the monitor timer */
0125 static int tlb_initialize(struct bonding *bond)
0126 {
0127     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0128     int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
0129     struct tlb_client_info *new_hashtbl;
0130     int i;
0131 
0132     new_hashtbl = kzalloc(size, GFP_KERNEL);
0133     if (!new_hashtbl)
0134         return -ENOMEM;
0135 
0136     spin_lock_bh(&bond->mode_lock);
0137 
0138     bond_info->tx_hashtbl = new_hashtbl;
0139 
0140     for (i = 0; i < TLB_HASH_TABLE_SIZE; i++)
0141         tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
0142 
0143     spin_unlock_bh(&bond->mode_lock);
0144 
0145     return 0;
0146 }
0147 
0148 /* Must be called only after all slaves have been released */
0149 static void tlb_deinitialize(struct bonding *bond)
0150 {
0151     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0152 
0153     spin_lock_bh(&bond->mode_lock);
0154 
0155     kfree(bond_info->tx_hashtbl);
0156     bond_info->tx_hashtbl = NULL;
0157 
0158     spin_unlock_bh(&bond->mode_lock);
0159 }
0160 
0161 static long long compute_gap(struct slave *slave)
0162 {
0163     return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
0164            (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
0165 }
0166 
0167 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
0168 {
0169     struct slave *slave, *least_loaded;
0170     struct list_head *iter;
0171     long long max_gap;
0172 
0173     least_loaded = NULL;
0174     max_gap = LLONG_MIN;
0175 
0176     /* Find the slave with the largest gap */
0177     bond_for_each_slave_rcu(bond, slave, iter) {
0178         if (bond_slave_can_tx(slave)) {
0179             long long gap = compute_gap(slave);
0180 
0181             if (max_gap < gap) {
0182                 least_loaded = slave;
0183                 max_gap = gap;
0184             }
0185         }
0186     }
0187 
0188     return least_loaded;
0189 }
0190 
0191 static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
0192                         u32 skb_len)
0193 {
0194     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0195     struct tlb_client_info *hash_table;
0196     struct slave *assigned_slave;
0197 
0198     hash_table = bond_info->tx_hashtbl;
0199     assigned_slave = hash_table[hash_index].tx_slave;
0200     if (!assigned_slave) {
0201         assigned_slave = tlb_get_least_loaded_slave(bond);
0202 
0203         if (assigned_slave) {
0204             struct tlb_slave_info *slave_info =
0205                 &(SLAVE_TLB_INFO(assigned_slave));
0206             u32 next_index = slave_info->head;
0207 
0208             hash_table[hash_index].tx_slave = assigned_slave;
0209             hash_table[hash_index].next = next_index;
0210             hash_table[hash_index].prev = TLB_NULL_INDEX;
0211 
0212             if (next_index != TLB_NULL_INDEX)
0213                 hash_table[next_index].prev = hash_index;
0214 
0215             slave_info->head = hash_index;
0216             slave_info->load +=
0217                 hash_table[hash_index].load_history;
0218         }
0219     }
0220 
0221     if (assigned_slave)
0222         hash_table[hash_index].tx_bytes += skb_len;
0223 
0224     return assigned_slave;
0225 }
0226 
0227 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index,
0228                     u32 skb_len)
0229 {
0230     struct slave *tx_slave;
0231 
0232     /* We don't need to disable softirq here, because
0233      * tlb_choose_channel() is only called by bond_alb_xmit()
0234      * which already has softirq disabled.
0235      */
0236     spin_lock(&bond->mode_lock);
0237     tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
0238     spin_unlock(&bond->mode_lock);
0239 
0240     return tx_slave;
0241 }
0242 
0243 /*********************** rlb specific functions ***************************/
0244 
0245 /* when an ARP REPLY is received from a client update its info
0246  * in the rx_hashtbl
0247  */
0248 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
0249 {
0250     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0251     struct rlb_client_info *client_info;
0252     u32 hash_index;
0253 
0254     spin_lock_bh(&bond->mode_lock);
0255 
0256     hash_index = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
0257     client_info = &(bond_info->rx_hashtbl[hash_index]);
0258 
0259     if ((client_info->assigned) &&
0260         (client_info->ip_src == arp->ip_dst) &&
0261         (client_info->ip_dst == arp->ip_src) &&
0262         (!ether_addr_equal_64bits(client_info->mac_dst, arp->mac_src))) {
0263         /* update the clients MAC address */
0264         ether_addr_copy(client_info->mac_dst, arp->mac_src);
0265         client_info->ntt = 1;
0266         bond_info->rx_ntt = 1;
0267     }
0268 
0269     spin_unlock_bh(&bond->mode_lock);
0270 }
0271 
0272 static int rlb_arp_recv(const struct sk_buff *skb, struct bonding *bond,
0273             struct slave *slave)
0274 {
0275     struct arp_pkt *arp, _arp;
0276 
0277     if (skb->protocol != cpu_to_be16(ETH_P_ARP))
0278         goto out;
0279 
0280     arp = skb_header_pointer(skb, 0, sizeof(_arp), &_arp);
0281     if (!arp)
0282         goto out;
0283 
0284     /* We received an ARP from arp->ip_src.
0285      * We might have used this IP address previously (on the bonding host
0286      * itself or on a system that is bridged together with the bond).
0287      * However, if arp->mac_src is different than what is stored in
0288      * rx_hashtbl, some other host is now using the IP and we must prevent
0289      * sending out client updates with this IP address and the old MAC
0290      * address.
0291      * Clean up all hash table entries that have this address as ip_src but
0292      * have a different mac_src.
0293      */
0294     rlb_purge_src_ip(bond, arp);
0295 
0296     if (arp->op_code == htons(ARPOP_REPLY)) {
0297         /* update rx hash table for this ARP */
0298         rlb_update_entry_from_arp(bond, arp);
0299         slave_dbg(bond->dev, slave->dev, "Server received an ARP Reply from client\n");
0300     }
0301 out:
0302     return RX_HANDLER_ANOTHER;
0303 }
0304 
0305 /* Caller must hold rcu_read_lock() */
0306 static struct slave *__rlb_next_rx_slave(struct bonding *bond)
0307 {
0308     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0309     struct slave *before = NULL, *rx_slave = NULL, *slave;
0310     struct list_head *iter;
0311     bool found = false;
0312 
0313     bond_for_each_slave_rcu(bond, slave, iter) {
0314         if (!bond_slave_can_tx(slave))
0315             continue;
0316         if (!found) {
0317             if (!before || before->speed < slave->speed)
0318                 before = slave;
0319         } else {
0320             if (!rx_slave || rx_slave->speed < slave->speed)
0321                 rx_slave = slave;
0322         }
0323         if (slave == bond_info->rx_slave)
0324             found = true;
0325     }
0326     /* we didn't find anything after the current or we have something
0327      * better before and up to the current slave
0328      */
0329     if (!rx_slave || (before && rx_slave->speed < before->speed))
0330         rx_slave = before;
0331 
0332     if (rx_slave)
0333         bond_info->rx_slave = rx_slave;
0334 
0335     return rx_slave;
0336 }
0337 
0338 /* Caller must hold RTNL, rcu_read_lock is obtained only to silence checkers */
0339 static struct slave *rlb_next_rx_slave(struct bonding *bond)
0340 {
0341     struct slave *rx_slave;
0342 
0343     ASSERT_RTNL();
0344 
0345     rcu_read_lock();
0346     rx_slave = __rlb_next_rx_slave(bond);
0347     rcu_read_unlock();
0348 
0349     return rx_slave;
0350 }
0351 
0352 /* teach the switch the mac of a disabled slave
0353  * on the primary for fault tolerance
0354  *
0355  * Caller must hold RTNL
0356  */
0357 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond,
0358                           const u8 addr[])
0359 {
0360     struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
0361 
0362     if (!curr_active)
0363         return;
0364 
0365     if (!bond->alb_info.primary_is_promisc) {
0366         if (!dev_set_promiscuity(curr_active->dev, 1))
0367             bond->alb_info.primary_is_promisc = 1;
0368         else
0369             bond->alb_info.primary_is_promisc = 0;
0370     }
0371 
0372     bond->alb_info.rlb_promisc_timeout_counter = 0;
0373 
0374     alb_send_learning_packets(curr_active, addr, true);
0375 }
0376 
0377 /* slave being removed should not be active at this point
0378  *
0379  * Caller must hold rtnl.
0380  */
0381 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
0382 {
0383     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0384     struct rlb_client_info *rx_hash_table;
0385     u32 index, next_index;
0386 
0387     /* clear slave from rx_hashtbl */
0388     spin_lock_bh(&bond->mode_lock);
0389 
0390     rx_hash_table = bond_info->rx_hashtbl;
0391     index = bond_info->rx_hashtbl_used_head;
0392     for (; index != RLB_NULL_INDEX; index = next_index) {
0393         next_index = rx_hash_table[index].used_next;
0394         if (rx_hash_table[index].slave == slave) {
0395             struct slave *assigned_slave = rlb_next_rx_slave(bond);
0396 
0397             if (assigned_slave) {
0398                 rx_hash_table[index].slave = assigned_slave;
0399                 if (is_valid_ether_addr(rx_hash_table[index].mac_dst)) {
0400                     bond_info->rx_hashtbl[index].ntt = 1;
0401                     bond_info->rx_ntt = 1;
0402                     /* A slave has been removed from the
0403                      * table because it is either disabled
0404                      * or being released. We must retry the
0405                      * update to avoid clients from not
0406                      * being updated & disconnecting when
0407                      * there is stress
0408                      */
0409                     bond_info->rlb_update_retry_counter =
0410                         RLB_UPDATE_RETRY;
0411                 }
0412             } else {  /* there is no active slave */
0413                 rx_hash_table[index].slave = NULL;
0414             }
0415         }
0416     }
0417 
0418     spin_unlock_bh(&bond->mode_lock);
0419 
0420     if (slave != rtnl_dereference(bond->curr_active_slave))
0421         rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
0422 }
0423 
0424 static void rlb_update_client(struct rlb_client_info *client_info)
0425 {
0426     int i;
0427 
0428     if (!client_info->slave || !is_valid_ether_addr(client_info->mac_dst))
0429         return;
0430 
0431     for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
0432         struct sk_buff *skb;
0433 
0434         skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
0435                  client_info->ip_dst,
0436                  client_info->slave->dev,
0437                  client_info->ip_src,
0438                  client_info->mac_dst,
0439                  client_info->slave->dev->dev_addr,
0440                  client_info->mac_dst);
0441         if (!skb) {
0442             slave_err(client_info->slave->bond->dev,
0443                   client_info->slave->dev,
0444                   "failed to create an ARP packet\n");
0445             continue;
0446         }
0447 
0448         skb->dev = client_info->slave->dev;
0449 
0450         if (client_info->vlan_id) {
0451             __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
0452                            client_info->vlan_id);
0453         }
0454 
0455         arp_xmit(skb);
0456     }
0457 }
0458 
0459 /* sends ARP REPLIES that update the clients that need updating */
0460 static void rlb_update_rx_clients(struct bonding *bond)
0461 {
0462     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0463     struct rlb_client_info *client_info;
0464     u32 hash_index;
0465 
0466     spin_lock_bh(&bond->mode_lock);
0467 
0468     hash_index = bond_info->rx_hashtbl_used_head;
0469     for (; hash_index != RLB_NULL_INDEX;
0470          hash_index = client_info->used_next) {
0471         client_info = &(bond_info->rx_hashtbl[hash_index]);
0472         if (client_info->ntt) {
0473             rlb_update_client(client_info);
0474             if (bond_info->rlb_update_retry_counter == 0)
0475                 client_info->ntt = 0;
0476         }
0477     }
0478 
0479     /* do not update the entries again until this counter is zero so that
0480      * not to confuse the clients.
0481      */
0482     bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
0483 
0484     spin_unlock_bh(&bond->mode_lock);
0485 }
0486 
0487 /* The slave was assigned a new mac address - update the clients */
0488 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
0489 {
0490     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0491     struct rlb_client_info *client_info;
0492     int ntt = 0;
0493     u32 hash_index;
0494 
0495     spin_lock_bh(&bond->mode_lock);
0496 
0497     hash_index = bond_info->rx_hashtbl_used_head;
0498     for (; hash_index != RLB_NULL_INDEX;
0499          hash_index = client_info->used_next) {
0500         client_info = &(bond_info->rx_hashtbl[hash_index]);
0501 
0502         if ((client_info->slave == slave) &&
0503             is_valid_ether_addr(client_info->mac_dst)) {
0504             client_info->ntt = 1;
0505             ntt = 1;
0506         }
0507     }
0508 
0509     /* update the team's flag only after the whole iteration */
0510     if (ntt) {
0511         bond_info->rx_ntt = 1;
0512         /* fasten the change */
0513         bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
0514     }
0515 
0516     spin_unlock_bh(&bond->mode_lock);
0517 }
0518 
0519 /* mark all clients using src_ip to be updated */
0520 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
0521 {
0522     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0523     struct rlb_client_info *client_info;
0524     u32 hash_index;
0525 
0526     spin_lock(&bond->mode_lock);
0527 
0528     hash_index = bond_info->rx_hashtbl_used_head;
0529     for (; hash_index != RLB_NULL_INDEX;
0530          hash_index = client_info->used_next) {
0531         client_info = &(bond_info->rx_hashtbl[hash_index]);
0532 
0533         if (!client_info->slave) {
0534             netdev_err(bond->dev, "found a client with no channel in the client's hash table\n");
0535             continue;
0536         }
0537         /* update all clients using this src_ip, that are not assigned
0538          * to the team's address (curr_active_slave) and have a known
0539          * unicast mac address.
0540          */
0541         if ((client_info->ip_src == src_ip) &&
0542             !ether_addr_equal_64bits(client_info->slave->dev->dev_addr,
0543                          bond->dev->dev_addr) &&
0544             is_valid_ether_addr(client_info->mac_dst)) {
0545             client_info->ntt = 1;
0546             bond_info->rx_ntt = 1;
0547         }
0548     }
0549 
0550     spin_unlock(&bond->mode_lock);
0551 }
0552 
0553 static struct slave *rlb_choose_channel(struct sk_buff *skb,
0554                     struct bonding *bond,
0555                     const struct arp_pkt *arp)
0556 {
0557     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0558     struct slave *assigned_slave, *curr_active_slave;
0559     struct rlb_client_info *client_info;
0560     u32 hash_index = 0;
0561 
0562     spin_lock(&bond->mode_lock);
0563 
0564     curr_active_slave = rcu_dereference(bond->curr_active_slave);
0565 
0566     hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
0567     client_info = &(bond_info->rx_hashtbl[hash_index]);
0568 
0569     if (client_info->assigned) {
0570         if ((client_info->ip_src == arp->ip_src) &&
0571             (client_info->ip_dst == arp->ip_dst)) {
0572             /* the entry is already assigned to this client */
0573             if (!is_broadcast_ether_addr(arp->mac_dst)) {
0574                 /* update mac address from arp */
0575                 ether_addr_copy(client_info->mac_dst, arp->mac_dst);
0576             }
0577             ether_addr_copy(client_info->mac_src, arp->mac_src);
0578 
0579             assigned_slave = client_info->slave;
0580             if (assigned_slave) {
0581                 spin_unlock(&bond->mode_lock);
0582                 return assigned_slave;
0583             }
0584         } else {
0585             /* the entry is already assigned to some other client,
0586              * move the old client to primary (curr_active_slave) so
0587              * that the new client can be assigned to this entry.
0588              */
0589             if (curr_active_slave &&
0590                 client_info->slave != curr_active_slave) {
0591                 client_info->slave = curr_active_slave;
0592                 rlb_update_client(client_info);
0593             }
0594         }
0595     }
0596     /* assign a new slave */
0597     assigned_slave = __rlb_next_rx_slave(bond);
0598 
0599     if (assigned_slave) {
0600         if (!(client_info->assigned &&
0601               client_info->ip_src == arp->ip_src)) {
0602             /* ip_src is going to be updated,
0603              * fix the src hash list
0604              */
0605             u32 hash_src = _simple_hash((u8 *)&arp->ip_src,
0606                             sizeof(arp->ip_src));
0607             rlb_src_unlink(bond, hash_index);
0608             rlb_src_link(bond, hash_src, hash_index);
0609         }
0610 
0611         client_info->ip_src = arp->ip_src;
0612         client_info->ip_dst = arp->ip_dst;
0613         /* arp->mac_dst is broadcast for arp requests.
0614          * will be updated with clients actual unicast mac address
0615          * upon receiving an arp reply.
0616          */
0617         ether_addr_copy(client_info->mac_dst, arp->mac_dst);
0618         ether_addr_copy(client_info->mac_src, arp->mac_src);
0619         client_info->slave = assigned_slave;
0620 
0621         if (is_valid_ether_addr(client_info->mac_dst)) {
0622             client_info->ntt = 1;
0623             bond->alb_info.rx_ntt = 1;
0624         } else {
0625             client_info->ntt = 0;
0626         }
0627 
0628         if (vlan_get_tag(skb, &client_info->vlan_id))
0629             client_info->vlan_id = 0;
0630 
0631         if (!client_info->assigned) {
0632             u32 prev_tbl_head = bond_info->rx_hashtbl_used_head;
0633 
0634             bond_info->rx_hashtbl_used_head = hash_index;
0635             client_info->used_next = prev_tbl_head;
0636             if (prev_tbl_head != RLB_NULL_INDEX) {
0637                 bond_info->rx_hashtbl[prev_tbl_head].used_prev =
0638                     hash_index;
0639             }
0640             client_info->assigned = 1;
0641         }
0642     }
0643 
0644     spin_unlock(&bond->mode_lock);
0645 
0646     return assigned_slave;
0647 }
0648 
0649 /* chooses (and returns) transmit channel for arp reply
0650  * does not choose channel for other arp types since they are
0651  * sent on the curr_active_slave
0652  */
0653 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
0654 {
0655     struct slave *tx_slave = NULL;
0656     struct net_device *dev;
0657     struct arp_pkt *arp;
0658 
0659     if (!pskb_network_may_pull(skb, sizeof(*arp)))
0660         return NULL;
0661     arp = (struct arp_pkt *)skb_network_header(skb);
0662 
0663     /* Don't modify or load balance ARPs that do not originate locally
0664      * (e.g.,arrive via a bridge).
0665      */
0666     if (!bond_slave_has_mac_rx(bond, arp->mac_src))
0667         return NULL;
0668 
0669     dev = ip_dev_find(dev_net(bond->dev), arp->ip_src);
0670     if (dev) {
0671         if (netif_is_bridge_master(dev)) {
0672             dev_put(dev);
0673             return NULL;
0674         }
0675         dev_put(dev);
0676     }
0677 
0678     if (arp->op_code == htons(ARPOP_REPLY)) {
0679         /* the arp must be sent on the selected rx channel */
0680         tx_slave = rlb_choose_channel(skb, bond, arp);
0681         if (tx_slave)
0682             bond_hw_addr_copy(arp->mac_src, tx_slave->dev->dev_addr,
0683                       tx_slave->dev->addr_len);
0684         netdev_dbg(bond->dev, "(slave %s): Server sent ARP Reply packet\n",
0685                tx_slave ? tx_slave->dev->name : "NULL");
0686     } else if (arp->op_code == htons(ARPOP_REQUEST)) {
0687         /* Create an entry in the rx_hashtbl for this client as a
0688          * place holder.
0689          * When the arp reply is received the entry will be updated
0690          * with the correct unicast address of the client.
0691          */
0692         tx_slave = rlb_choose_channel(skb, bond, arp);
0693 
0694         /* The ARP reply packets must be delayed so that
0695          * they can cancel out the influence of the ARP request.
0696          */
0697         bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
0698 
0699         /* arp requests are broadcast and are sent on the primary
0700          * the arp request will collapse all clients on the subnet to
0701          * the primary slave. We must register these clients to be
0702          * updated with their assigned mac.
0703          */
0704         rlb_req_update_subnet_clients(bond, arp->ip_src);
0705         netdev_dbg(bond->dev, "(slave %s): Server sent ARP Request packet\n",
0706                tx_slave ? tx_slave->dev->name : "NULL");
0707     }
0708 
0709     return tx_slave;
0710 }
0711 
0712 static void rlb_rebalance(struct bonding *bond)
0713 {
0714     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0715     struct slave *assigned_slave;
0716     struct rlb_client_info *client_info;
0717     int ntt;
0718     u32 hash_index;
0719 
0720     spin_lock_bh(&bond->mode_lock);
0721 
0722     ntt = 0;
0723     hash_index = bond_info->rx_hashtbl_used_head;
0724     for (; hash_index != RLB_NULL_INDEX;
0725          hash_index = client_info->used_next) {
0726         client_info = &(bond_info->rx_hashtbl[hash_index]);
0727         assigned_slave = __rlb_next_rx_slave(bond);
0728         if (assigned_slave && (client_info->slave != assigned_slave)) {
0729             client_info->slave = assigned_slave;
0730             if (!is_zero_ether_addr(client_info->mac_dst)) {
0731                 client_info->ntt = 1;
0732                 ntt = 1;
0733             }
0734         }
0735     }
0736 
0737     /* update the team's flag only after the whole iteration */
0738     if (ntt)
0739         bond_info->rx_ntt = 1;
0740     spin_unlock_bh(&bond->mode_lock);
0741 }
0742 
0743 /* Caller must hold mode_lock */
0744 static void rlb_init_table_entry_dst(struct rlb_client_info *entry)
0745 {
0746     entry->used_next = RLB_NULL_INDEX;
0747     entry->used_prev = RLB_NULL_INDEX;
0748     entry->assigned = 0;
0749     entry->slave = NULL;
0750     entry->vlan_id = 0;
0751 }
0752 static void rlb_init_table_entry_src(struct rlb_client_info *entry)
0753 {
0754     entry->src_first = RLB_NULL_INDEX;
0755     entry->src_prev = RLB_NULL_INDEX;
0756     entry->src_next = RLB_NULL_INDEX;
0757 }
0758 
0759 static void rlb_init_table_entry(struct rlb_client_info *entry)
0760 {
0761     memset(entry, 0, sizeof(struct rlb_client_info));
0762     rlb_init_table_entry_dst(entry);
0763     rlb_init_table_entry_src(entry);
0764 }
0765 
0766 static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index)
0767 {
0768     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0769     u32 next_index = bond_info->rx_hashtbl[index].used_next;
0770     u32 prev_index = bond_info->rx_hashtbl[index].used_prev;
0771 
0772     if (index == bond_info->rx_hashtbl_used_head)
0773         bond_info->rx_hashtbl_used_head = next_index;
0774     if (prev_index != RLB_NULL_INDEX)
0775         bond_info->rx_hashtbl[prev_index].used_next = next_index;
0776     if (next_index != RLB_NULL_INDEX)
0777         bond_info->rx_hashtbl[next_index].used_prev = prev_index;
0778 }
0779 
0780 /* unlink a rlb hash table entry from the src list */
0781 static void rlb_src_unlink(struct bonding *bond, u32 index)
0782 {
0783     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0784     u32 next_index = bond_info->rx_hashtbl[index].src_next;
0785     u32 prev_index = bond_info->rx_hashtbl[index].src_prev;
0786 
0787     bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX;
0788     bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX;
0789 
0790     if (next_index != RLB_NULL_INDEX)
0791         bond_info->rx_hashtbl[next_index].src_prev = prev_index;
0792 
0793     if (prev_index == RLB_NULL_INDEX)
0794         return;
0795 
0796     /* is prev_index pointing to the head of this list? */
0797     if (bond_info->rx_hashtbl[prev_index].src_first == index)
0798         bond_info->rx_hashtbl[prev_index].src_first = next_index;
0799     else
0800         bond_info->rx_hashtbl[prev_index].src_next = next_index;
0801 
0802 }
0803 
0804 static void rlb_delete_table_entry(struct bonding *bond, u32 index)
0805 {
0806     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0807     struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
0808 
0809     rlb_delete_table_entry_dst(bond, index);
0810     rlb_init_table_entry_dst(entry);
0811 
0812     rlb_src_unlink(bond, index);
0813 }
0814 
0815 /* add the rx_hashtbl[ip_dst_hash] entry to the list
0816  * of entries with identical ip_src_hash
0817  */
0818 static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash)
0819 {
0820     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0821     u32 next;
0822 
0823     bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash;
0824     next = bond_info->rx_hashtbl[ip_src_hash].src_first;
0825     bond_info->rx_hashtbl[ip_dst_hash].src_next = next;
0826     if (next != RLB_NULL_INDEX)
0827         bond_info->rx_hashtbl[next].src_prev = ip_dst_hash;
0828     bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash;
0829 }
0830 
0831 /* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does
0832  * not match arp->mac_src
0833  */
0834 static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp)
0835 {
0836     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0837     u32 ip_src_hash = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
0838     u32 index;
0839 
0840     spin_lock_bh(&bond->mode_lock);
0841 
0842     index = bond_info->rx_hashtbl[ip_src_hash].src_first;
0843     while (index != RLB_NULL_INDEX) {
0844         struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
0845         u32 next_index = entry->src_next;
0846 
0847         if (entry->ip_src == arp->ip_src &&
0848             !ether_addr_equal_64bits(arp->mac_src, entry->mac_src))
0849             rlb_delete_table_entry(bond, index);
0850         index = next_index;
0851     }
0852     spin_unlock_bh(&bond->mode_lock);
0853 }
0854 
0855 static int rlb_initialize(struct bonding *bond)
0856 {
0857     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0858     struct rlb_client_info  *new_hashtbl;
0859     int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
0860     int i;
0861 
0862     new_hashtbl = kmalloc(size, GFP_KERNEL);
0863     if (!new_hashtbl)
0864         return -1;
0865 
0866     spin_lock_bh(&bond->mode_lock);
0867 
0868     bond_info->rx_hashtbl = new_hashtbl;
0869 
0870     bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
0871 
0872     for (i = 0; i < RLB_HASH_TABLE_SIZE; i++)
0873         rlb_init_table_entry(bond_info->rx_hashtbl + i);
0874 
0875     spin_unlock_bh(&bond->mode_lock);
0876 
0877     /* register to receive ARPs */
0878     bond->recv_probe = rlb_arp_recv;
0879 
0880     return 0;
0881 }
0882 
0883 static void rlb_deinitialize(struct bonding *bond)
0884 {
0885     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0886 
0887     spin_lock_bh(&bond->mode_lock);
0888 
0889     kfree(bond_info->rx_hashtbl);
0890     bond_info->rx_hashtbl = NULL;
0891     bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
0892 
0893     spin_unlock_bh(&bond->mode_lock);
0894 }
0895 
0896 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
0897 {
0898     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
0899     u32 curr_index;
0900 
0901     spin_lock_bh(&bond->mode_lock);
0902 
0903     curr_index = bond_info->rx_hashtbl_used_head;
0904     while (curr_index != RLB_NULL_INDEX) {
0905         struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
0906         u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
0907 
0908         if (curr->vlan_id == vlan_id)
0909             rlb_delete_table_entry(bond, curr_index);
0910 
0911         curr_index = next_index;
0912     }
0913 
0914     spin_unlock_bh(&bond->mode_lock);
0915 }
0916 
0917 /*********************** tlb/rlb shared functions *********************/
0918 
0919 static void alb_send_lp_vid(struct slave *slave, const u8 mac_addr[],
0920                 __be16 vlan_proto, u16 vid)
0921 {
0922     struct learning_pkt pkt;
0923     struct sk_buff *skb;
0924     int size = sizeof(struct learning_pkt);
0925 
0926     memset(&pkt, 0, size);
0927     ether_addr_copy(pkt.mac_dst, mac_addr);
0928     ether_addr_copy(pkt.mac_src, mac_addr);
0929     pkt.type = cpu_to_be16(ETH_P_LOOPBACK);
0930 
0931     skb = dev_alloc_skb(size);
0932     if (!skb)
0933         return;
0934 
0935     skb_put_data(skb, &pkt, size);
0936 
0937     skb_reset_mac_header(skb);
0938     skb->network_header = skb->mac_header + ETH_HLEN;
0939     skb->protocol = pkt.type;
0940     skb->priority = TC_PRIO_CONTROL;
0941     skb->dev = slave->dev;
0942 
0943     slave_dbg(slave->bond->dev, slave->dev,
0944           "Send learning packet: mac %pM vlan %d\n", mac_addr, vid);
0945 
0946     if (vid)
0947         __vlan_hwaccel_put_tag(skb, vlan_proto, vid);
0948 
0949     dev_queue_xmit(skb);
0950 }
0951 
0952 struct alb_walk_data {
0953     struct bonding *bond;
0954     struct slave *slave;
0955     const u8 *mac_addr;
0956     bool strict_match;
0957 };
0958 
0959 static int alb_upper_dev_walk(struct net_device *upper,
0960                   struct netdev_nested_priv *priv)
0961 {
0962     struct alb_walk_data *data = (struct alb_walk_data *)priv->data;
0963     bool strict_match = data->strict_match;
0964     const u8 *mac_addr = data->mac_addr;
0965     struct bonding *bond = data->bond;
0966     struct slave *slave = data->slave;
0967     struct bond_vlan_tag *tags;
0968 
0969     if (is_vlan_dev(upper) &&
0970         bond->dev->lower_level == upper->lower_level - 1) {
0971         if (upper->addr_assign_type == NET_ADDR_STOLEN) {
0972             alb_send_lp_vid(slave, mac_addr,
0973                     vlan_dev_vlan_proto(upper),
0974                     vlan_dev_vlan_id(upper));
0975         } else {
0976             alb_send_lp_vid(slave, upper->dev_addr,
0977                     vlan_dev_vlan_proto(upper),
0978                     vlan_dev_vlan_id(upper));
0979         }
0980     }
0981 
0982     /* If this is a macvlan device, then only send updates
0983      * when strict_match is turned off.
0984      */
0985     if (netif_is_macvlan(upper) && !strict_match) {
0986         tags = bond_verify_device_path(bond->dev, upper, 0);
0987         if (IS_ERR_OR_NULL(tags))
0988             BUG();
0989         alb_send_lp_vid(slave, upper->dev_addr,
0990                 tags[0].vlan_proto, tags[0].vlan_id);
0991         kfree(tags);
0992     }
0993 
0994     return 0;
0995 }
0996 
0997 static void alb_send_learning_packets(struct slave *slave, const u8 mac_addr[],
0998                       bool strict_match)
0999 {
1000     struct bonding *bond = bond_get_bond_by_slave(slave);
1001     struct netdev_nested_priv priv;
1002     struct alb_walk_data data = {
1003         .strict_match = strict_match,
1004         .mac_addr = mac_addr,
1005         .slave = slave,
1006         .bond = bond,
1007     };
1008 
1009     priv.data = (void *)&data;
1010     /* send untagged */
1011     alb_send_lp_vid(slave, mac_addr, 0, 0);
1012 
1013     /* loop through all devices and see if we need to send a packet
1014      * for that device.
1015      */
1016     rcu_read_lock();
1017     netdev_walk_all_upper_dev_rcu(bond->dev, alb_upper_dev_walk, &priv);
1018     rcu_read_unlock();
1019 }
1020 
1021 static int alb_set_slave_mac_addr(struct slave *slave, const u8 addr[],
1022                   unsigned int len)
1023 {
1024     struct net_device *dev = slave->dev;
1025     struct sockaddr_storage ss;
1026 
1027     if (BOND_MODE(slave->bond) == BOND_MODE_TLB) {
1028         __dev_addr_set(dev, addr, len);
1029         return 0;
1030     }
1031 
1032     /* for rlb each slave must have a unique hw mac addresses so that
1033      * each slave will receive packets destined to a different mac
1034      */
1035     memcpy(ss.__data, addr, len);
1036     ss.ss_family = dev->type;
1037     if (dev_set_mac_address(dev, (struct sockaddr *)&ss, NULL)) {
1038         slave_err(slave->bond->dev, dev, "dev_set_mac_address on slave failed! ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n");
1039         return -EOPNOTSUPP;
1040     }
1041     return 0;
1042 }
1043 
1044 /* Swap MAC addresses between two slaves.
1045  *
1046  * Called with RTNL held, and no other locks.
1047  */
1048 static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
1049 {
1050     u8 tmp_mac_addr[MAX_ADDR_LEN];
1051 
1052     bond_hw_addr_copy(tmp_mac_addr, slave1->dev->dev_addr,
1053               slave1->dev->addr_len);
1054     alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr,
1055                    slave2->dev->addr_len);
1056     alb_set_slave_mac_addr(slave2, tmp_mac_addr,
1057                    slave1->dev->addr_len);
1058 
1059 }
1060 
1061 /* Send learning packets after MAC address swap.
1062  *
1063  * Called with RTNL and no other locks
1064  */
1065 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
1066                 struct slave *slave2)
1067 {
1068     int slaves_state_differ = (bond_slave_can_tx(slave1) != bond_slave_can_tx(slave2));
1069     struct slave *disabled_slave = NULL;
1070 
1071     ASSERT_RTNL();
1072 
1073     /* fasten the change in the switch */
1074     if (bond_slave_can_tx(slave1)) {
1075         alb_send_learning_packets(slave1, slave1->dev->dev_addr, false);
1076         if (bond->alb_info.rlb_enabled) {
1077             /* inform the clients that the mac address
1078              * has changed
1079              */
1080             rlb_req_update_slave_clients(bond, slave1);
1081         }
1082     } else {
1083         disabled_slave = slave1;
1084     }
1085 
1086     if (bond_slave_can_tx(slave2)) {
1087         alb_send_learning_packets(slave2, slave2->dev->dev_addr, false);
1088         if (bond->alb_info.rlb_enabled) {
1089             /* inform the clients that the mac address
1090              * has changed
1091              */
1092             rlb_req_update_slave_clients(bond, slave2);
1093         }
1094     } else {
1095         disabled_slave = slave2;
1096     }
1097 
1098     if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1099         /* A disabled slave was assigned an active mac addr */
1100         rlb_teach_disabled_mac_on_primary(bond,
1101                           disabled_slave->dev->dev_addr);
1102     }
1103 }
1104 
1105 /**
1106  * alb_change_hw_addr_on_detach
1107  * @bond: bonding we're working on
1108  * @slave: the slave that was just detached
1109  *
1110  * We assume that @slave was already detached from the slave list.
1111  *
1112  * If @slave's permanent hw address is different both from its current
1113  * address and from @bond's address, then somewhere in the bond there's
1114  * a slave that has @slave's permanet address as its current address.
1115  * We'll make sure that slave no longer uses @slave's permanent address.
1116  *
1117  * Caller must hold RTNL and no other locks
1118  */
1119 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1120 {
1121     int perm_curr_diff;
1122     int perm_bond_diff;
1123     struct slave *found_slave;
1124 
1125     perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1126                           slave->dev->dev_addr);
1127     perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1128                           bond->dev->dev_addr);
1129 
1130     if (perm_curr_diff && perm_bond_diff) {
1131         found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr);
1132 
1133         if (found_slave) {
1134             alb_swap_mac_addr(slave, found_slave);
1135             alb_fasten_mac_swap(bond, slave, found_slave);
1136         }
1137     }
1138 }
1139 
1140 /**
1141  * alb_handle_addr_collision_on_attach
1142  * @bond: bonding we're working on
1143  * @slave: the slave that was just attached
1144  *
1145  * checks uniqueness of slave's mac address and handles the case the
1146  * new slave uses the bonds mac address.
1147  *
1148  * If the permanent hw address of @slave is @bond's hw address, we need to
1149  * find a different hw address to give @slave, that isn't in use by any other
1150  * slave in the bond. This address must be, of course, one of the permanent
1151  * addresses of the other slaves.
1152  *
1153  * We go over the slave list, and for each slave there we compare its
1154  * permanent hw address with the current address of all the other slaves.
1155  * If no match was found, then we've found a slave with a permanent address
1156  * that isn't used by any other slave in the bond, so we can assign it to
1157  * @slave.
1158  *
1159  * assumption: this function is called before @slave is attached to the
1160  *         bond slave list.
1161  */
1162 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1163 {
1164     struct slave *has_bond_addr = rcu_access_pointer(bond->curr_active_slave);
1165     struct slave *tmp_slave1, *free_mac_slave = NULL;
1166     struct list_head *iter;
1167 
1168     if (!bond_has_slaves(bond)) {
1169         /* this is the first slave */
1170         return 0;
1171     }
1172 
1173     /* if slave's mac address differs from bond's mac address
1174      * check uniqueness of slave's mac address against the other
1175      * slaves in the bond.
1176      */
1177     if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
1178         if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
1179             return 0;
1180 
1181         /* Try setting slave mac to bond address and fall-through
1182          * to code handling that situation below...
1183          */
1184         alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1185                        bond->dev->addr_len);
1186     }
1187 
1188     /* The slave's address is equal to the address of the bond.
1189      * Search for a spare address in the bond for this slave.
1190      */
1191     bond_for_each_slave(bond, tmp_slave1, iter) {
1192         if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
1193             /* no slave has tmp_slave1's perm addr
1194              * as its curr addr
1195              */
1196             free_mac_slave = tmp_slave1;
1197             break;
1198         }
1199 
1200         if (!has_bond_addr) {
1201             if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
1202                             bond->dev->dev_addr)) {
1203 
1204                 has_bond_addr = tmp_slave1;
1205             }
1206         }
1207     }
1208 
1209     if (free_mac_slave) {
1210         alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1211                        free_mac_slave->dev->addr_len);
1212 
1213         slave_warn(bond->dev, slave->dev, "the slave hw address is in use by the bond; giving it the hw address of %s\n",
1214                free_mac_slave->dev->name);
1215 
1216     } else if (has_bond_addr) {
1217         slave_err(bond->dev, slave->dev, "the slave hw address is in use by the bond; couldn't find a slave with a free hw address to give it (this should not have happened)\n");
1218         return -EFAULT;
1219     }
1220 
1221     return 0;
1222 }
1223 
1224 /**
1225  * alb_set_mac_address
1226  * @bond: bonding we're working on
1227  * @addr: MAC address to set
1228  *
1229  * In TLB mode all slaves are configured to the bond's hw address, but set
1230  * their dev_addr field to different addresses (based on their permanent hw
1231  * addresses).
1232  *
1233  * For each slave, this function sets the interface to the new address and then
1234  * changes its dev_addr field to its previous value.
1235  *
1236  * Unwinding assumes bond's mac address has not yet changed.
1237  */
1238 static int alb_set_mac_address(struct bonding *bond, void *addr)
1239 {
1240     struct slave *slave, *rollback_slave;
1241     struct list_head *iter;
1242     struct sockaddr_storage ss;
1243     char tmp_addr[MAX_ADDR_LEN];
1244     int res;
1245 
1246     if (bond->alb_info.rlb_enabled)
1247         return 0;
1248 
1249     bond_for_each_slave(bond, slave, iter) {
1250         /* save net_device's current hw address */
1251         bond_hw_addr_copy(tmp_addr, slave->dev->dev_addr,
1252                   slave->dev->addr_len);
1253 
1254         res = dev_set_mac_address(slave->dev, addr, NULL);
1255 
1256         /* restore net_device's hw address */
1257         dev_addr_set(slave->dev, tmp_addr);
1258 
1259         if (res)
1260             goto unwind;
1261     }
1262 
1263     return 0;
1264 
1265 unwind:
1266     memcpy(ss.__data, bond->dev->dev_addr, bond->dev->addr_len);
1267     ss.ss_family = bond->dev->type;
1268 
1269     /* unwind from head to the slave that failed */
1270     bond_for_each_slave(bond, rollback_slave, iter) {
1271         if (rollback_slave == slave)
1272             break;
1273         bond_hw_addr_copy(tmp_addr, rollback_slave->dev->dev_addr,
1274                   rollback_slave->dev->addr_len);
1275         dev_set_mac_address(rollback_slave->dev,
1276                     (struct sockaddr *)&ss, NULL);
1277         dev_addr_set(rollback_slave->dev, tmp_addr);
1278     }
1279 
1280     return res;
1281 }
1282 
1283 /* determine if the packet is NA or NS */
1284 static bool alb_determine_nd(struct sk_buff *skb, struct bonding *bond)
1285 {
1286     struct ipv6hdr *ip6hdr;
1287     struct icmp6hdr *hdr;
1288 
1289     if (!pskb_network_may_pull(skb, sizeof(*ip6hdr)))
1290         return true;
1291 
1292     ip6hdr = ipv6_hdr(skb);
1293     if (ip6hdr->nexthdr != IPPROTO_ICMPV6)
1294         return false;
1295 
1296     if (!pskb_network_may_pull(skb, sizeof(*ip6hdr) + sizeof(*hdr)))
1297         return true;
1298 
1299     hdr = icmp6_hdr(skb);
1300     return hdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT ||
1301         hdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION;
1302 }
1303 
1304 /************************ exported alb functions ************************/
1305 
1306 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1307 {
1308     int res;
1309 
1310     res = tlb_initialize(bond);
1311     if (res)
1312         return res;
1313 
1314     if (rlb_enabled) {
1315         res = rlb_initialize(bond);
1316         if (res) {
1317             tlb_deinitialize(bond);
1318             return res;
1319         }
1320         bond->alb_info.rlb_enabled = 1;
1321     } else {
1322         bond->alb_info.rlb_enabled = 0;
1323     }
1324 
1325     return 0;
1326 }
1327 
1328 void bond_alb_deinitialize(struct bonding *bond)
1329 {
1330     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1331 
1332     tlb_deinitialize(bond);
1333 
1334     if (bond_info->rlb_enabled)
1335         rlb_deinitialize(bond);
1336 }
1337 
1338 static netdev_tx_t bond_do_alb_xmit(struct sk_buff *skb, struct bonding *bond,
1339                     struct slave *tx_slave)
1340 {
1341     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1342     struct ethhdr *eth_data = eth_hdr(skb);
1343 
1344     if (!tx_slave) {
1345         /* unbalanced or unassigned, send through primary */
1346         tx_slave = rcu_dereference(bond->curr_active_slave);
1347         if (bond->params.tlb_dynamic_lb)
1348             bond_info->unbalanced_load += skb->len;
1349     }
1350 
1351     if (tx_slave && bond_slave_can_tx(tx_slave)) {
1352         if (tx_slave != rcu_access_pointer(bond->curr_active_slave)) {
1353             ether_addr_copy(eth_data->h_source,
1354                     tx_slave->dev->dev_addr);
1355         }
1356 
1357         return bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1358     }
1359 
1360     if (tx_slave && bond->params.tlb_dynamic_lb) {
1361         spin_lock(&bond->mode_lock);
1362         __tlb_clear_slave(bond, tx_slave, 0);
1363         spin_unlock(&bond->mode_lock);
1364     }
1365 
1366     /* no suitable interface, frame not sent */
1367     return bond_tx_drop(bond->dev, skb);
1368 }
1369 
1370 struct slave *bond_xmit_tlb_slave_get(struct bonding *bond,
1371                       struct sk_buff *skb)
1372 {
1373     struct slave *tx_slave = NULL;
1374     struct ethhdr *eth_data;
1375     u32 hash_index;
1376 
1377     skb_reset_mac_header(skb);
1378     eth_data = eth_hdr(skb);
1379 
1380     /* Do not TX balance any multicast or broadcast */
1381     if (!is_multicast_ether_addr(eth_data->h_dest)) {
1382         switch (skb->protocol) {
1383         case htons(ETH_P_IPV6):
1384             if (alb_determine_nd(skb, bond))
1385                 break;
1386             fallthrough;
1387         case htons(ETH_P_IP):
1388             hash_index = bond_xmit_hash(bond, skb);
1389             if (bond->params.tlb_dynamic_lb) {
1390                 tx_slave = tlb_choose_channel(bond,
1391                                   hash_index & 0xFF,
1392                                   skb->len);
1393             } else {
1394                 struct bond_up_slave *slaves;
1395                 unsigned int count;
1396 
1397                 slaves = rcu_dereference(bond->usable_slaves);
1398                 count = slaves ? READ_ONCE(slaves->count) : 0;
1399                 if (likely(count))
1400                     tx_slave = slaves->arr[hash_index %
1401                                    count];
1402             }
1403             break;
1404         }
1405     }
1406     return tx_slave;
1407 }
1408 
1409 netdev_tx_t bond_tlb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1410 {
1411     struct bonding *bond = netdev_priv(bond_dev);
1412     struct slave *tx_slave;
1413 
1414     tx_slave = bond_xmit_tlb_slave_get(bond, skb);
1415     return bond_do_alb_xmit(skb, bond, tx_slave);
1416 }
1417 
1418 struct slave *bond_xmit_alb_slave_get(struct bonding *bond,
1419                       struct sk_buff *skb)
1420 {
1421     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1422     static const __be32 ip_bcast = htonl(0xffffffff);
1423     struct slave *tx_slave = NULL;
1424     const u8 *hash_start = NULL;
1425     bool do_tx_balance = true;
1426     struct ethhdr *eth_data;
1427     u32 hash_index = 0;
1428     int hash_size = 0;
1429 
1430     skb_reset_mac_header(skb);
1431     eth_data = eth_hdr(skb);
1432 
1433     switch (ntohs(skb->protocol)) {
1434     case ETH_P_IP: {
1435         const struct iphdr *iph;
1436 
1437         if (is_broadcast_ether_addr(eth_data->h_dest) ||
1438             !pskb_network_may_pull(skb, sizeof(*iph))) {
1439             do_tx_balance = false;
1440             break;
1441         }
1442         iph = ip_hdr(skb);
1443         if (iph->daddr == ip_bcast || iph->protocol == IPPROTO_IGMP) {
1444             do_tx_balance = false;
1445             break;
1446         }
1447         hash_start = (char *)&(iph->daddr);
1448         hash_size = sizeof(iph->daddr);
1449         break;
1450     }
1451     case ETH_P_IPV6: {
1452         const struct ipv6hdr *ip6hdr;
1453 
1454         /* IPv6 doesn't really use broadcast mac address, but leave
1455          * that here just in case.
1456          */
1457         if (is_broadcast_ether_addr(eth_data->h_dest)) {
1458             do_tx_balance = false;
1459             break;
1460         }
1461 
1462         /* IPv6 uses all-nodes multicast as an equivalent to
1463          * broadcasts in IPv4.
1464          */
1465         if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
1466             do_tx_balance = false;
1467             break;
1468         }
1469 
1470         if (alb_determine_nd(skb, bond)) {
1471             do_tx_balance = false;
1472             break;
1473         }
1474 
1475         /* The IPv6 header is pulled by alb_determine_nd */
1476         /* Additionally, DAD probes should not be tx-balanced as that
1477          * will lead to false positives for duplicate addresses and
1478          * prevent address configuration from working.
1479          */
1480         ip6hdr = ipv6_hdr(skb);
1481         if (ipv6_addr_any(&ip6hdr->saddr)) {
1482             do_tx_balance = false;
1483             break;
1484         }
1485 
1486         hash_start = (char *)&ip6hdr->daddr;
1487         hash_size = sizeof(ip6hdr->daddr);
1488         break;
1489     }
1490     case ETH_P_ARP:
1491         do_tx_balance = false;
1492         if (bond_info->rlb_enabled)
1493             tx_slave = rlb_arp_xmit(skb, bond);
1494         break;
1495     default:
1496         do_tx_balance = false;
1497         break;
1498     }
1499 
1500     if (do_tx_balance) {
1501         if (bond->params.tlb_dynamic_lb) {
1502             hash_index = _simple_hash(hash_start, hash_size);
1503             tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1504         } else {
1505             /*
1506              * do_tx_balance means we are free to select the tx_slave
1507              * So we do exactly what tlb would do for hash selection
1508              */
1509 
1510             struct bond_up_slave *slaves;
1511             unsigned int count;
1512 
1513             slaves = rcu_dereference(bond->usable_slaves);
1514             count = slaves ? READ_ONCE(slaves->count) : 0;
1515             if (likely(count))
1516                 tx_slave = slaves->arr[bond_xmit_hash(bond, skb) %
1517                                count];
1518         }
1519     }
1520     return tx_slave;
1521 }
1522 
1523 netdev_tx_t bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1524 {
1525     struct bonding *bond = netdev_priv(bond_dev);
1526     struct slave *tx_slave = NULL;
1527 
1528     tx_slave = bond_xmit_alb_slave_get(bond, skb);
1529     return bond_do_alb_xmit(skb, bond, tx_slave);
1530 }
1531 
1532 void bond_alb_monitor(struct work_struct *work)
1533 {
1534     struct bonding *bond = container_of(work, struct bonding,
1535                         alb_work.work);
1536     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1537     struct list_head *iter;
1538     struct slave *slave;
1539 
1540     if (!bond_has_slaves(bond)) {
1541         atomic_set(&bond_info->tx_rebalance_counter, 0);
1542         bond_info->lp_counter = 0;
1543         goto re_arm;
1544     }
1545 
1546     rcu_read_lock();
1547 
1548     atomic_inc(&bond_info->tx_rebalance_counter);
1549     bond_info->lp_counter++;
1550 
1551     /* send learning packets */
1552     if (bond_info->lp_counter >= BOND_ALB_LP_TICKS(bond)) {
1553         bool strict_match;
1554 
1555         bond_for_each_slave_rcu(bond, slave, iter) {
1556             /* If updating current_active, use all currently
1557              * user mac addresses (!strict_match).  Otherwise, only
1558              * use mac of the slave device.
1559              * In RLB mode, we always use strict matches.
1560              */
1561             strict_match = (slave != rcu_access_pointer(bond->curr_active_slave) ||
1562                     bond_info->rlb_enabled);
1563             alb_send_learning_packets(slave, slave->dev->dev_addr,
1564                           strict_match);
1565         }
1566         bond_info->lp_counter = 0;
1567     }
1568 
1569     /* rebalance tx traffic */
1570     if (atomic_read(&bond_info->tx_rebalance_counter) >= BOND_TLB_REBALANCE_TICKS) {
1571         bond_for_each_slave_rcu(bond, slave, iter) {
1572             tlb_clear_slave(bond, slave, 1);
1573             if (slave == rcu_access_pointer(bond->curr_active_slave)) {
1574                 SLAVE_TLB_INFO(slave).load =
1575                     bond_info->unbalanced_load /
1576                         BOND_TLB_REBALANCE_INTERVAL;
1577                 bond_info->unbalanced_load = 0;
1578             }
1579         }
1580         atomic_set(&bond_info->tx_rebalance_counter, 0);
1581     }
1582 
1583     if (bond_info->rlb_enabled) {
1584         if (bond_info->primary_is_promisc &&
1585             (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1586 
1587             /* dev_set_promiscuity requires rtnl and
1588              * nothing else.  Avoid race with bond_close.
1589              */
1590             rcu_read_unlock();
1591             if (!rtnl_trylock())
1592                 goto re_arm;
1593 
1594             bond_info->rlb_promisc_timeout_counter = 0;
1595 
1596             /* If the primary was set to promiscuous mode
1597              * because a slave was disabled then
1598              * it can now leave promiscuous mode.
1599              */
1600             dev_set_promiscuity(rtnl_dereference(bond->curr_active_slave)->dev,
1601                         -1);
1602             bond_info->primary_is_promisc = 0;
1603 
1604             rtnl_unlock();
1605             rcu_read_lock();
1606         }
1607 
1608         if (bond_info->rlb_rebalance) {
1609             bond_info->rlb_rebalance = 0;
1610             rlb_rebalance(bond);
1611         }
1612 
1613         /* check if clients need updating */
1614         if (bond_info->rx_ntt) {
1615             if (bond_info->rlb_update_delay_counter) {
1616                 --bond_info->rlb_update_delay_counter;
1617             } else {
1618                 rlb_update_rx_clients(bond);
1619                 if (bond_info->rlb_update_retry_counter)
1620                     --bond_info->rlb_update_retry_counter;
1621                 else
1622                     bond_info->rx_ntt = 0;
1623             }
1624         }
1625     }
1626     rcu_read_unlock();
1627 re_arm:
1628     queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1629 }
1630 
1631 /* assumption: called before the slave is attached to the bond
1632  * and not locked by the bond lock
1633  */
1634 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1635 {
1636     int res;
1637 
1638     res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1639                      slave->dev->addr_len);
1640     if (res)
1641         return res;
1642 
1643     res = alb_handle_addr_collision_on_attach(bond, slave);
1644     if (res)
1645         return res;
1646 
1647     tlb_init_slave(slave);
1648 
1649     /* order a rebalance ASAP */
1650     atomic_set(&bond->alb_info.tx_rebalance_counter,
1651            BOND_TLB_REBALANCE_TICKS);
1652 
1653     if (bond->alb_info.rlb_enabled)
1654         bond->alb_info.rlb_rebalance = 1;
1655 
1656     return 0;
1657 }
1658 
1659 /* Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1660  * if necessary.
1661  *
1662  * Caller must hold RTNL and no other locks
1663  */
1664 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1665 {
1666     if (bond_has_slaves(bond))
1667         alb_change_hw_addr_on_detach(bond, slave);
1668 
1669     tlb_clear_slave(bond, slave, 0);
1670 
1671     if (bond->alb_info.rlb_enabled) {
1672         bond->alb_info.rx_slave = NULL;
1673         rlb_clear_slave(bond, slave);
1674     }
1675 
1676 }
1677 
1678 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1679 {
1680     struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1681 
1682     if (link == BOND_LINK_DOWN) {
1683         tlb_clear_slave(bond, slave, 0);
1684         if (bond->alb_info.rlb_enabled)
1685             rlb_clear_slave(bond, slave);
1686     } else if (link == BOND_LINK_UP) {
1687         /* order a rebalance ASAP */
1688         atomic_set(&bond_info->tx_rebalance_counter,
1689                BOND_TLB_REBALANCE_TICKS);
1690         if (bond->alb_info.rlb_enabled) {
1691             bond->alb_info.rlb_rebalance = 1;
1692             /* If the updelay module parameter is smaller than the
1693              * forwarding delay of the switch the rebalance will
1694              * not work because the rebalance arp replies will
1695              * not be forwarded to the clients..
1696              */
1697         }
1698     }
1699 
1700     if (bond_is_nondyn_tlb(bond)) {
1701         if (bond_update_slave_arr(bond, NULL))
1702             pr_err("Failed to build slave-array for TLB mode.\n");
1703     }
1704 }
1705 
1706 /**
1707  * bond_alb_handle_active_change - assign new curr_active_slave
1708  * @bond: our bonding struct
1709  * @new_slave: new slave to assign
1710  *
1711  * Set the bond->curr_active_slave to @new_slave and handle
1712  * mac address swapping and promiscuity changes as needed.
1713  *
1714  * Caller must hold RTNL
1715  */
1716 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1717 {
1718     struct slave *swap_slave;
1719     struct slave *curr_active;
1720 
1721     curr_active = rtnl_dereference(bond->curr_active_slave);
1722     if (curr_active == new_slave)
1723         return;
1724 
1725     if (curr_active && bond->alb_info.primary_is_promisc) {
1726         dev_set_promiscuity(curr_active->dev, -1);
1727         bond->alb_info.primary_is_promisc = 0;
1728         bond->alb_info.rlb_promisc_timeout_counter = 0;
1729     }
1730 
1731     swap_slave = curr_active;
1732     rcu_assign_pointer(bond->curr_active_slave, new_slave);
1733 
1734     if (!new_slave || !bond_has_slaves(bond))
1735         return;
1736 
1737     /* set the new curr_active_slave to the bonds mac address
1738      * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1739      */
1740     if (!swap_slave)
1741         swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
1742 
1743     /* Arrange for swap_slave and new_slave to temporarily be
1744      * ignored so we can mess with their MAC addresses without
1745      * fear of interference from transmit activity.
1746      */
1747     if (swap_slave)
1748         tlb_clear_slave(bond, swap_slave, 1);
1749     tlb_clear_slave(bond, new_slave, 1);
1750 
1751     /* in TLB mode, the slave might flip down/up with the old dev_addr,
1752      * and thus filter bond->dev_addr's packets, so force bond's mac
1753      */
1754     if (BOND_MODE(bond) == BOND_MODE_TLB) {
1755         struct sockaddr_storage ss;
1756         u8 tmp_addr[MAX_ADDR_LEN];
1757 
1758         bond_hw_addr_copy(tmp_addr, new_slave->dev->dev_addr,
1759                   new_slave->dev->addr_len);
1760 
1761         bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
1762                   bond->dev->addr_len);
1763         ss.ss_family = bond->dev->type;
1764         /* we don't care if it can't change its mac, best effort */
1765         dev_set_mac_address(new_slave->dev, (struct sockaddr *)&ss,
1766                     NULL);
1767 
1768         dev_addr_set(new_slave->dev, tmp_addr);
1769     }
1770 
1771     /* curr_active_slave must be set before calling alb_swap_mac_addr */
1772     if (swap_slave) {
1773         /* swap mac address */
1774         alb_swap_mac_addr(swap_slave, new_slave);
1775         alb_fasten_mac_swap(bond, swap_slave, new_slave);
1776     } else {
1777         /* set the new_slave to the bond mac address */
1778         alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1779                        bond->dev->addr_len);
1780         alb_send_learning_packets(new_slave, bond->dev->dev_addr,
1781                       false);
1782     }
1783 }
1784 
1785 /* Called with RTNL */
1786 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1787 {
1788     struct bonding *bond = netdev_priv(bond_dev);
1789     struct sockaddr_storage *ss = addr;
1790     struct slave *curr_active;
1791     struct slave *swap_slave;
1792     int res;
1793 
1794     if (!is_valid_ether_addr(ss->__data))
1795         return -EADDRNOTAVAIL;
1796 
1797     res = alb_set_mac_address(bond, addr);
1798     if (res)
1799         return res;
1800 
1801     dev_addr_set(bond_dev, ss->__data);
1802 
1803     /* If there is no curr_active_slave there is nothing else to do.
1804      * Otherwise we'll need to pass the new address to it and handle
1805      * duplications.
1806      */
1807     curr_active = rtnl_dereference(bond->curr_active_slave);
1808     if (!curr_active)
1809         return 0;
1810 
1811     swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
1812 
1813     if (swap_slave) {
1814         alb_swap_mac_addr(swap_slave, curr_active);
1815         alb_fasten_mac_swap(bond, swap_slave, curr_active);
1816     } else {
1817         alb_set_slave_mac_addr(curr_active, bond_dev->dev_addr,
1818                        bond_dev->addr_len);
1819 
1820         alb_send_learning_packets(curr_active,
1821                       bond_dev->dev_addr, false);
1822         if (bond->alb_info.rlb_enabled) {
1823             /* inform clients mac address has changed */
1824             rlb_req_update_slave_clients(bond, curr_active);
1825         }
1826     }
1827 
1828     return 0;
1829 }
1830 
1831 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1832 {
1833     if (bond->alb_info.rlb_enabled)
1834         rlb_clear_vlan(bond, vlan_id);
1835 }
1836