linux/drivers/net/bonding/bond_alb.c
<<
>>
Prefs
   1/*
   2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License as published by the
   6 * Free Software Foundation; either version 2 of the License, or
   7 * (at your option) any later version.
   8 *
   9 * This program is distributed in the hope that it will be useful, but
  10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License along
  15 * with this program; if not, write to the Free Software Foundation, Inc.,
  16 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17 *
  18 * The full GNU General Public License is included in this distribution in the
  19 * file called LICENSE.
  20 *
  21 */
  22
  23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24
  25#include <linux/skbuff.h>
  26#include <linux/netdevice.h>
  27#include <linux/etherdevice.h>
  28#include <linux/pkt_sched.h>
  29#include <linux/spinlock.h>
  30#include <linux/slab.h>
  31#include <linux/timer.h>
  32#include <linux/ip.h>
  33#include <linux/ipv6.h>
  34#include <linux/if_arp.h>
  35#include <linux/if_ether.h>
  36#include <linux/if_bonding.h>
  37#include <linux/if_vlan.h>
  38#include <linux/in.h>
  39#include <net/ipx.h>
  40#include <net/arp.h>
  41#include <net/ipv6.h>
  42#include <asm/byteorder.h>
  43#include "bonding.h"
  44#include "bond_alb.h"
  45
  46
  47
  48#ifndef __long_aligned
  49#define __long_aligned __attribute__((aligned((sizeof(long)))))
  50#endif
  51static const u8 mac_bcast[ETH_ALEN] __long_aligned = {
  52        0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  53};
  54static const u8 mac_v6_allmcast[ETH_ALEN] __long_aligned = {
  55        0x33, 0x33, 0x00, 0x00, 0x00, 0x01
  56};
  57static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
  58
  59#pragma pack(1)
  60struct learning_pkt {
  61        u8 mac_dst[ETH_ALEN];
  62        u8 mac_src[ETH_ALEN];
  63        __be16 type;
  64        u8 padding[ETH_ZLEN - ETH_HLEN];
  65};
  66
  67struct arp_pkt {
  68        __be16  hw_addr_space;
  69        __be16  prot_addr_space;
  70        u8      hw_addr_len;
  71        u8      prot_addr_len;
  72        __be16  op_code;
  73        u8      mac_src[ETH_ALEN];      /* sender hardware address */
  74        __be32  ip_src;                 /* sender IP address */
  75        u8      mac_dst[ETH_ALEN];      /* target hardware address */
  76        __be32  ip_dst;                 /* target IP address */
  77};
  78#pragma pack()
  79
  80static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
  81{
  82        return (struct arp_pkt *)skb_network_header(skb);
  83}
  84
  85/* Forward declaration */
  86static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
  87
  88static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
  89{
  90        int i;
  91        u8 hash = 0;
  92
  93        for (i = 0; i < hash_size; i++) {
  94                hash ^= hash_start[i];
  95        }
  96
  97        return hash;
  98}
  99
 100/*********************** tlb specific functions ***************************/
 101
 102static inline void _lock_tx_hashtbl(struct bonding *bond)
 103{
 104        spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
 105}
 106
 107static inline void _unlock_tx_hashtbl(struct bonding *bond)
 108{
 109        spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
 110}
 111
 112/* Caller must hold tx_hashtbl lock */
 113static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
 114{
 115        if (save_load) {
 116                entry->load_history = 1 + entry->tx_bytes /
 117                                      BOND_TLB_REBALANCE_INTERVAL;
 118                entry->tx_bytes = 0;
 119        }
 120
 121        entry->tx_slave = NULL;
 122        entry->next = TLB_NULL_INDEX;
 123        entry->prev = TLB_NULL_INDEX;
 124}
 125
 126static inline void tlb_init_slave(struct slave *slave)
 127{
 128        SLAVE_TLB_INFO(slave).load = 0;
 129        SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
 130}
 131
 132/* Caller must hold bond lock for read */
 133static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
 134{
 135        struct tlb_client_info *tx_hash_table;
 136        u32 index;
 137
 138        _lock_tx_hashtbl(bond);
 139
 140        /* clear slave from tx_hashtbl */
 141        tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
 142
 143        /* skip this if we've already freed the tx hash table */
 144        if (tx_hash_table) {
 145                index = SLAVE_TLB_INFO(slave).head;
 146                while (index != TLB_NULL_INDEX) {
 147                        u32 next_index = tx_hash_table[index].next;
 148                        tlb_init_table_entry(&tx_hash_table[index], save_load);
 149                        index = next_index;
 150                }
 151        }
 152
 153        tlb_init_slave(slave);
 154
 155        _unlock_tx_hashtbl(bond);
 156}
 157
 158/* Must be called before starting the monitor timer */
 159static int tlb_initialize(struct bonding *bond)
 160{
 161        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 162        int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
 163        struct tlb_client_info *new_hashtbl;
 164        int i;
 165
 166        spin_lock_init(&(bond_info->tx_hashtbl_lock));
 167
 168        new_hashtbl = kzalloc(size, GFP_KERNEL);
 169        if (!new_hashtbl) {
 170                pr_err("%s: Error: Failed to allocate TLB hash table\n",
 171                       bond->dev->name);
 172                return -1;
 173        }
 174        _lock_tx_hashtbl(bond);
 175
 176        bond_info->tx_hashtbl = new_hashtbl;
 177
 178        for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
 179                tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
 180        }
 181
 182        _unlock_tx_hashtbl(bond);
 183
 184        return 0;
 185}
 186
 187/* Must be called only after all slaves have been released */
 188static void tlb_deinitialize(struct bonding *bond)
 189{
 190        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 191
 192        _lock_tx_hashtbl(bond);
 193
 194        kfree(bond_info->tx_hashtbl);
 195        bond_info->tx_hashtbl = NULL;
 196
 197        _unlock_tx_hashtbl(bond);
 198}
 199
 200static long long compute_gap(struct slave *slave)
 201{
 202        return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
 203               (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
 204}
 205
 206/* Caller must hold bond lock for read */
 207static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
 208{
 209        struct slave *slave, *least_loaded;
 210        long long max_gap;
 211        int i;
 212
 213        least_loaded = NULL;
 214        max_gap = LLONG_MIN;
 215
 216        /* Find the slave with the largest gap */
 217        bond_for_each_slave(bond, slave, i) {
 218                if (SLAVE_IS_OK(slave)) {
 219                        long long gap = compute_gap(slave);
 220
 221                        if (max_gap < gap) {
 222                                least_loaded = slave;
 223                                max_gap = gap;
 224                        }
 225                }
 226        }
 227
 228        return least_loaded;
 229}
 230
 231/* Caller must hold bond lock for read */
 232static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
 233{
 234        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 235        struct tlb_client_info *hash_table;
 236        struct slave *assigned_slave;
 237
 238        _lock_tx_hashtbl(bond);
 239
 240        hash_table = bond_info->tx_hashtbl;
 241        assigned_slave = hash_table[hash_index].tx_slave;
 242        if (!assigned_slave) {
 243                assigned_slave = tlb_get_least_loaded_slave(bond);
 244
 245                if (assigned_slave) {
 246                        struct tlb_slave_info *slave_info =
 247                                &(SLAVE_TLB_INFO(assigned_slave));
 248                        u32 next_index = slave_info->head;
 249
 250                        hash_table[hash_index].tx_slave = assigned_slave;
 251                        hash_table[hash_index].next = next_index;
 252                        hash_table[hash_index].prev = TLB_NULL_INDEX;
 253
 254                        if (next_index != TLB_NULL_INDEX) {
 255                                hash_table[next_index].prev = hash_index;
 256                        }
 257
 258                        slave_info->head = hash_index;
 259                        slave_info->load +=
 260                                hash_table[hash_index].load_history;
 261                }
 262        }
 263
 264        if (assigned_slave) {
 265                hash_table[hash_index].tx_bytes += skb_len;
 266        }
 267
 268        _unlock_tx_hashtbl(bond);
 269
 270        return assigned_slave;
 271}
 272
 273/*********************** rlb specific functions ***************************/
 274static inline void _lock_rx_hashtbl(struct bonding *bond)
 275{
 276        spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
 277}
 278
 279static inline void _unlock_rx_hashtbl(struct bonding *bond)
 280{
 281        spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
 282}
 283
 284/* when an ARP REPLY is received from a client update its info
 285 * in the rx_hashtbl
 286 */
 287static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
 288{
 289        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 290        struct rlb_client_info *client_info;
 291        u32 hash_index;
 292
 293        _lock_rx_hashtbl(bond);
 294
 295        hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
 296        client_info = &(bond_info->rx_hashtbl[hash_index]);
 297
 298        if ((client_info->assigned) &&
 299            (client_info->ip_src == arp->ip_dst) &&
 300            (client_info->ip_dst == arp->ip_src) &&
 301            (compare_ether_addr_64bits(client_info->mac_dst, arp->mac_src))) {
 302                /* update the clients MAC address */
 303                memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
 304                client_info->ntt = 1;
 305                bond_info->rx_ntt = 1;
 306        }
 307
 308        _unlock_rx_hashtbl(bond);
 309}
 310
 311static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
 312{
 313        struct bonding *bond;
 314        struct arp_pkt *arp = (struct arp_pkt *)skb->data;
 315        int res = NET_RX_DROP;
 316
 317        while (bond_dev->priv_flags & IFF_802_1Q_VLAN)
 318                bond_dev = vlan_dev_real_dev(bond_dev);
 319
 320        if (!(bond_dev->priv_flags & IFF_BONDING) ||
 321            !(bond_dev->flags & IFF_MASTER))
 322                goto out;
 323
 324        if (!arp) {
 325                pr_debug("Packet has no ARP data\n");
 326                goto out;
 327        }
 328
 329        skb = skb_share_check(skb, GFP_ATOMIC);
 330        if (!skb)
 331                goto out;
 332
 333        if (!pskb_may_pull(skb, arp_hdr_len(bond_dev)))
 334                goto out;
 335
 336        if (skb->len < sizeof(struct arp_pkt)) {
 337                pr_debug("Packet is too small to be an ARP\n");
 338                goto out;
 339        }
 340
 341        if (arp->op_code == htons(ARPOP_REPLY)) {
 342                /* update rx hash table for this ARP */
 343                bond = netdev_priv(bond_dev);
 344                rlb_update_entry_from_arp(bond, arp);
 345                pr_debug("Server received an ARP Reply from client\n");
 346        }
 347
 348        res = NET_RX_SUCCESS;
 349
 350out:
 351        dev_kfree_skb(skb);
 352
 353        return res;
 354}
 355
 356/* Caller must hold bond lock for read */
 357static struct slave *rlb_next_rx_slave(struct bonding *bond)
 358{
 359        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 360        struct slave *rx_slave, *slave, *start_at;
 361        int i = 0;
 362
 363        if (bond_info->next_rx_slave) {
 364                start_at = bond_info->next_rx_slave;
 365        } else {
 366                start_at = bond->first_slave;
 367        }
 368
 369        rx_slave = NULL;
 370
 371        bond_for_each_slave_from(bond, slave, i, start_at) {
 372                if (SLAVE_IS_OK(slave)) {
 373                        if (!rx_slave) {
 374                                rx_slave = slave;
 375                        } else if (slave->speed > rx_slave->speed) {
 376                                rx_slave = slave;
 377                        }
 378                }
 379        }
 380
 381        if (rx_slave) {
 382                bond_info->next_rx_slave = rx_slave->next;
 383        }
 384
 385        return rx_slave;
 386}
 387
 388/* teach the switch the mac of a disabled slave
 389 * on the primary for fault tolerance
 390 *
 391 * Caller must hold bond->curr_slave_lock for write or bond lock for write
 392 */
 393static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
 394{
 395        if (!bond->curr_active_slave) {
 396                return;
 397        }
 398
 399        if (!bond->alb_info.primary_is_promisc) {
 400                if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
 401                        bond->alb_info.primary_is_promisc = 1;
 402                else
 403                        bond->alb_info.primary_is_promisc = 0;
 404        }
 405
 406        bond->alb_info.rlb_promisc_timeout_counter = 0;
 407
 408        alb_send_learning_packets(bond->curr_active_slave, addr);
 409}
 410
 411/* slave being removed should not be active at this point
 412 *
 413 * Caller must hold bond lock for read
 414 */
 415static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
 416{
 417        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 418        struct rlb_client_info *rx_hash_table;
 419        u32 index, next_index;
 420
 421        /* clear slave from rx_hashtbl */
 422        _lock_rx_hashtbl(bond);
 423
 424        rx_hash_table = bond_info->rx_hashtbl;
 425        index = bond_info->rx_hashtbl_head;
 426        for (; index != RLB_NULL_INDEX; index = next_index) {
 427                next_index = rx_hash_table[index].next;
 428                if (rx_hash_table[index].slave == slave) {
 429                        struct slave *assigned_slave = rlb_next_rx_slave(bond);
 430
 431                        if (assigned_slave) {
 432                                rx_hash_table[index].slave = assigned_slave;
 433                                if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
 434                                                              mac_bcast)) {
 435                                        bond_info->rx_hashtbl[index].ntt = 1;
 436                                        bond_info->rx_ntt = 1;
 437                                        /* A slave has been removed from the
 438                                         * table because it is either disabled
 439                                         * or being released. We must retry the
 440                                         * update to avoid clients from not
 441                                         * being updated & disconnecting when
 442                                         * there is stress
 443                                         */
 444                                        bond_info->rlb_update_retry_counter =
 445                                                RLB_UPDATE_RETRY;
 446                                }
 447                        } else {  /* there is no active slave */
 448                                rx_hash_table[index].slave = NULL;
 449                        }
 450                }
 451        }
 452
 453        _unlock_rx_hashtbl(bond);
 454
 455        write_lock_bh(&bond->curr_slave_lock);
 456
 457        if (slave != bond->curr_active_slave) {
 458                rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
 459        }
 460
 461        write_unlock_bh(&bond->curr_slave_lock);
 462}
 463
 464static void rlb_update_client(struct rlb_client_info *client_info)
 465{
 466        int i;
 467
 468        if (!client_info->slave) {
 469                return;
 470        }
 471
 472        for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
 473                struct sk_buff *skb;
 474
 475                skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
 476                                 client_info->ip_dst,
 477                                 client_info->slave->dev,
 478                                 client_info->ip_src,
 479                                 client_info->mac_dst,
 480                                 client_info->slave->dev->dev_addr,
 481                                 client_info->mac_dst);
 482                if (!skb) {
 483                        pr_err("%s: Error: failed to create an ARP packet\n",
 484                               client_info->slave->dev->master->name);
 485                        continue;
 486                }
 487
 488                skb->dev = client_info->slave->dev;
 489
 490                if (client_info->tag) {
 491                        skb = vlan_put_tag(skb, client_info->vlan_id);
 492                        if (!skb) {
 493                                pr_err("%s: Error: failed to insert VLAN tag\n",
 494                                       client_info->slave->dev->master->name);
 495                                continue;
 496                        }
 497                }
 498
 499                arp_xmit(skb);
 500        }
 501}
 502
 503/* sends ARP REPLIES that update the clients that need updating */
 504static void rlb_update_rx_clients(struct bonding *bond)
 505{
 506        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 507        struct rlb_client_info *client_info;
 508        u32 hash_index;
 509
 510        _lock_rx_hashtbl(bond);
 511
 512        hash_index = bond_info->rx_hashtbl_head;
 513        for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
 514                client_info = &(bond_info->rx_hashtbl[hash_index]);
 515                if (client_info->ntt) {
 516                        rlb_update_client(client_info);
 517                        if (bond_info->rlb_update_retry_counter == 0) {
 518                                client_info->ntt = 0;
 519                        }
 520                }
 521        }
 522
 523        /* do not update the entries again until this counter is zero so that
 524         * not to confuse the clients.
 525         */
 526        bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
 527
 528        _unlock_rx_hashtbl(bond);
 529}
 530
 531/* The slave was assigned a new mac address - update the clients */
 532static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
 533{
 534        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 535        struct rlb_client_info *client_info;
 536        int ntt = 0;
 537        u32 hash_index;
 538
 539        _lock_rx_hashtbl(bond);
 540
 541        hash_index = bond_info->rx_hashtbl_head;
 542        for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
 543                client_info = &(bond_info->rx_hashtbl[hash_index]);
 544
 545                if ((client_info->slave == slave) &&
 546                    compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
 547                        client_info->ntt = 1;
 548                        ntt = 1;
 549                }
 550        }
 551
 552        // update the team's flag only after the whole iteration
 553        if (ntt) {
 554                bond_info->rx_ntt = 1;
 555                //fasten the change
 556                bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
 557        }
 558
 559        _unlock_rx_hashtbl(bond);
 560}
 561
 562/* mark all clients using src_ip to be updated */
 563static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
 564{
 565        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 566        struct rlb_client_info *client_info;
 567        u32 hash_index;
 568
 569        _lock_rx_hashtbl(bond);
 570
 571        hash_index = bond_info->rx_hashtbl_head;
 572        for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
 573                client_info = &(bond_info->rx_hashtbl[hash_index]);
 574
 575                if (!client_info->slave) {
 576                        pr_err("%s: Error: found a client with no channel in the client's hash table\n",
 577                               bond->dev->name);
 578                        continue;
 579                }
 580                /*update all clients using this src_ip, that are not assigned
 581                 * to the team's address (curr_active_slave) and have a known
 582                 * unicast mac address.
 583                 */
 584                if ((client_info->ip_src == src_ip) &&
 585                    compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
 586                           bond->dev->dev_addr) &&
 587                    compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
 588                        client_info->ntt = 1;
 589                        bond_info->rx_ntt = 1;
 590                }
 591        }
 592
 593        _unlock_rx_hashtbl(bond);
 594}
 595
 596/* Caller must hold both bond and ptr locks for read */
 597static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
 598{
 599        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 600        struct arp_pkt *arp = arp_pkt(skb);
 601        struct slave *assigned_slave;
 602        struct rlb_client_info *client_info;
 603        u32 hash_index = 0;
 604
 605        _lock_rx_hashtbl(bond);
 606
 607        hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
 608        client_info = &(bond_info->rx_hashtbl[hash_index]);
 609
 610        if (client_info->assigned) {
 611                if ((client_info->ip_src == arp->ip_src) &&
 612                    (client_info->ip_dst == arp->ip_dst)) {
 613                        /* the entry is already assigned to this client */
 614                        if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
 615                                /* update mac address from arp */
 616                                memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
 617                        }
 618
 619                        assigned_slave = client_info->slave;
 620                        if (assigned_slave) {
 621                                _unlock_rx_hashtbl(bond);
 622                                return assigned_slave;
 623                        }
 624                } else {
 625                        /* the entry is already assigned to some other client,
 626                         * move the old client to primary (curr_active_slave) so
 627                         * that the new client can be assigned to this entry.
 628                         */
 629                        if (bond->curr_active_slave &&
 630                            client_info->slave != bond->curr_active_slave) {
 631                                client_info->slave = bond->curr_active_slave;
 632                                rlb_update_client(client_info);
 633                        }
 634                }
 635        }
 636        /* assign a new slave */
 637        assigned_slave = rlb_next_rx_slave(bond);
 638
 639        if (assigned_slave) {
 640                client_info->ip_src = arp->ip_src;
 641                client_info->ip_dst = arp->ip_dst;
 642                /* arp->mac_dst is broadcast for arp reqeusts.
 643                 * will be updated with clients actual unicast mac address
 644                 * upon receiving an arp reply.
 645                 */
 646                memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
 647                client_info->slave = assigned_slave;
 648
 649                if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
 650                        client_info->ntt = 1;
 651                        bond->alb_info.rx_ntt = 1;
 652                } else {
 653                        client_info->ntt = 0;
 654                }
 655
 656                if (bond->vlgrp) {
 657                        if (!vlan_get_tag(skb, &client_info->vlan_id))
 658                                client_info->tag = 1;
 659                }
 660
 661                if (!client_info->assigned) {
 662                        u32 prev_tbl_head = bond_info->rx_hashtbl_head;
 663                        bond_info->rx_hashtbl_head = hash_index;
 664                        client_info->next = prev_tbl_head;
 665                        if (prev_tbl_head != RLB_NULL_INDEX) {
 666                                bond_info->rx_hashtbl[prev_tbl_head].prev =
 667                                        hash_index;
 668                        }
 669                        client_info->assigned = 1;
 670                }
 671        }
 672
 673        _unlock_rx_hashtbl(bond);
 674
 675        return assigned_slave;
 676}
 677
 678/* chooses (and returns) transmit channel for arp reply
 679 * does not choose channel for other arp types since they are
 680 * sent on the curr_active_slave
 681 */
 682static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
 683{
 684        struct arp_pkt *arp = arp_pkt(skb);
 685        struct slave *tx_slave = NULL;
 686
 687        if (arp->op_code == htons(ARPOP_REPLY)) {
 688                /* the arp must be sent on the selected
 689                * rx channel
 690                */
 691                tx_slave = rlb_choose_channel(skb, bond);
 692                if (tx_slave) {
 693                        memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
 694                }
 695                pr_debug("Server sent ARP Reply packet\n");
 696        } else if (arp->op_code == htons(ARPOP_REQUEST)) {
 697                /* Create an entry in the rx_hashtbl for this client as a
 698                 * place holder.
 699                 * When the arp reply is received the entry will be updated
 700                 * with the correct unicast address of the client.
 701                 */
 702                rlb_choose_channel(skb, bond);
 703
 704                /* The ARP relpy packets must be delayed so that
 705                 * they can cancel out the influence of the ARP request.
 706                 */
 707                bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
 708
 709                /* arp requests are broadcast and are sent on the primary
 710                 * the arp request will collapse all clients on the subnet to
 711                 * the primary slave. We must register these clients to be
 712                 * updated with their assigned mac.
 713                 */
 714                rlb_req_update_subnet_clients(bond, arp->ip_src);
 715                pr_debug("Server sent ARP Request packet\n");
 716        }
 717
 718        return tx_slave;
 719}
 720
 721/* Caller must hold bond lock for read */
 722static void rlb_rebalance(struct bonding *bond)
 723{
 724        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 725        struct slave *assigned_slave;
 726        struct rlb_client_info *client_info;
 727        int ntt;
 728        u32 hash_index;
 729
 730        _lock_rx_hashtbl(bond);
 731
 732        ntt = 0;
 733        hash_index = bond_info->rx_hashtbl_head;
 734        for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
 735                client_info = &(bond_info->rx_hashtbl[hash_index]);
 736                assigned_slave = rlb_next_rx_slave(bond);
 737                if (assigned_slave && (client_info->slave != assigned_slave)) {
 738                        client_info->slave = assigned_slave;
 739                        client_info->ntt = 1;
 740                        ntt = 1;
 741                }
 742        }
 743
 744        /* update the team's flag only after the whole iteration */
 745        if (ntt) {
 746                bond_info->rx_ntt = 1;
 747        }
 748        _unlock_rx_hashtbl(bond);
 749}
 750
 751/* Caller must hold rx_hashtbl lock */
 752static void rlb_init_table_entry(struct rlb_client_info *entry)
 753{
 754        memset(entry, 0, sizeof(struct rlb_client_info));
 755        entry->next = RLB_NULL_INDEX;
 756        entry->prev = RLB_NULL_INDEX;
 757}
 758
 759static int rlb_initialize(struct bonding *bond)
 760{
 761        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 762        struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
 763        struct rlb_client_info  *new_hashtbl;
 764        int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
 765        int i;
 766
 767        spin_lock_init(&(bond_info->rx_hashtbl_lock));
 768
 769        new_hashtbl = kmalloc(size, GFP_KERNEL);
 770        if (!new_hashtbl) {
 771                pr_err("%s: Error: Failed to allocate RLB hash table\n",
 772                       bond->dev->name);
 773                return -1;
 774        }
 775        _lock_rx_hashtbl(bond);
 776
 777        bond_info->rx_hashtbl = new_hashtbl;
 778
 779        bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
 780
 781        for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
 782                rlb_init_table_entry(bond_info->rx_hashtbl + i);
 783        }
 784
 785        _unlock_rx_hashtbl(bond);
 786
 787        /*initialize packet type*/
 788        pk_type->type = cpu_to_be16(ETH_P_ARP);
 789        pk_type->dev = bond->dev;
 790        pk_type->func = rlb_arp_recv;
 791
 792        /* register to receive ARPs */
 793        dev_add_pack(pk_type);
 794
 795        return 0;
 796}
 797
 798static void rlb_deinitialize(struct bonding *bond)
 799{
 800        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 801
 802        dev_remove_pack(&(bond_info->rlb_pkt_type));
 803
 804        _lock_rx_hashtbl(bond);
 805
 806        kfree(bond_info->rx_hashtbl);
 807        bond_info->rx_hashtbl = NULL;
 808        bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
 809
 810        _unlock_rx_hashtbl(bond);
 811}
 812
 813static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
 814{
 815        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 816        u32 curr_index;
 817
 818        _lock_rx_hashtbl(bond);
 819
 820        curr_index = bond_info->rx_hashtbl_head;
 821        while (curr_index != RLB_NULL_INDEX) {
 822                struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
 823                u32 next_index = bond_info->rx_hashtbl[curr_index].next;
 824                u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
 825
 826                if (curr->tag && (curr->vlan_id == vlan_id)) {
 827                        if (curr_index == bond_info->rx_hashtbl_head) {
 828                                bond_info->rx_hashtbl_head = next_index;
 829                        }
 830                        if (prev_index != RLB_NULL_INDEX) {
 831                                bond_info->rx_hashtbl[prev_index].next = next_index;
 832                        }
 833                        if (next_index != RLB_NULL_INDEX) {
 834                                bond_info->rx_hashtbl[next_index].prev = prev_index;
 835                        }
 836
 837                        rlb_init_table_entry(curr);
 838                }
 839
 840                curr_index = next_index;
 841        }
 842
 843        _unlock_rx_hashtbl(bond);
 844}
 845
 846/*********************** tlb/rlb shared functions *********************/
 847
 848static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
 849{
 850        struct bonding *bond = bond_get_bond_by_slave(slave);
 851        struct learning_pkt pkt;
 852        int size = sizeof(struct learning_pkt);
 853        int i;
 854
 855        memset(&pkt, 0, size);
 856        memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
 857        memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
 858        pkt.type = cpu_to_be16(ETH_P_LOOP);
 859
 860        for (i = 0; i < MAX_LP_BURST; i++) {
 861                struct sk_buff *skb;
 862                char *data;
 863
 864                skb = dev_alloc_skb(size);
 865                if (!skb) {
 866                        return;
 867                }
 868
 869                data = skb_put(skb, size);
 870                memcpy(data, &pkt, size);
 871
 872                skb_reset_mac_header(skb);
 873                skb->network_header = skb->mac_header + ETH_HLEN;
 874                skb->protocol = pkt.type;
 875                skb->priority = TC_PRIO_CONTROL;
 876                skb->dev = slave->dev;
 877
 878                if (bond->vlgrp) {
 879                        struct vlan_entry *vlan;
 880
 881                        vlan = bond_next_vlan(bond,
 882                                              bond->alb_info.current_alb_vlan);
 883
 884                        bond->alb_info.current_alb_vlan = vlan;
 885                        if (!vlan) {
 886                                kfree_skb(skb);
 887                                continue;
 888                        }
 889
 890                        skb = vlan_put_tag(skb, vlan->vlan_id);
 891                        if (!skb) {
 892                                pr_err("%s: Error: failed to insert VLAN tag\n",
 893                                       bond->dev->name);
 894                                continue;
 895                        }
 896                }
 897
 898                dev_queue_xmit(skb);
 899        }
 900}
 901
 902/* hw is a boolean parameter that determines whether we should try and
 903 * set the hw address of the device as well as the hw address of the
 904 * net_device
 905 */
 906static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
 907{
 908        struct net_device *dev = slave->dev;
 909        struct sockaddr s_addr;
 910
 911        if (!hw) {
 912                memcpy(dev->dev_addr, addr, dev->addr_len);
 913                return 0;
 914        }
 915
 916        /* for rlb each slave must have a unique hw mac addresses so that */
 917        /* each slave will receive packets destined to a different mac */
 918        memcpy(s_addr.sa_data, addr, dev->addr_len);
 919        s_addr.sa_family = dev->type;
 920        if (dev_set_mac_address(dev, &s_addr)) {
 921                pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
 922                       "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
 923                       dev->master->name, dev->name);
 924                return -EOPNOTSUPP;
 925        }
 926        return 0;
 927}
 928
 929/*
 930 * Swap MAC addresses between two slaves.
 931 *
 932 * Called with RTNL held, and no other locks.
 933 *
 934 */
 935
 936static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
 937{
 938        u8 tmp_mac_addr[ETH_ALEN];
 939
 940        memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
 941        alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
 942        alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
 943
 944}
 945
 946/*
 947 * Send learning packets after MAC address swap.
 948 *
 949 * Called with RTNL and no other locks
 950 */
 951static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
 952                                struct slave *slave2)
 953{
 954        int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
 955        struct slave *disabled_slave = NULL;
 956
 957        ASSERT_RTNL();
 958
 959        /* fasten the change in the switch */
 960        if (SLAVE_IS_OK(slave1)) {
 961                alb_send_learning_packets(slave1, slave1->dev->dev_addr);
 962                if (bond->alb_info.rlb_enabled) {
 963                        /* inform the clients that the mac address
 964                         * has changed
 965                         */
 966                        rlb_req_update_slave_clients(bond, slave1);
 967                }
 968        } else {
 969                disabled_slave = slave1;
 970        }
 971
 972        if (SLAVE_IS_OK(slave2)) {
 973                alb_send_learning_packets(slave2, slave2->dev->dev_addr);
 974                if (bond->alb_info.rlb_enabled) {
 975                        /* inform the clients that the mac address
 976                         * has changed
 977                         */
 978                        rlb_req_update_slave_clients(bond, slave2);
 979                }
 980        } else {
 981                disabled_slave = slave2;
 982        }
 983
 984        if (bond->alb_info.rlb_enabled && slaves_state_differ) {
 985                /* A disabled slave was assigned an active mac addr */
 986                rlb_teach_disabled_mac_on_primary(bond,
 987                                                  disabled_slave->dev->dev_addr);
 988        }
 989}
 990
 991/**
 992 * alb_change_hw_addr_on_detach
 993 * @bond: bonding we're working on
 994 * @slave: the slave that was just detached
 995 *
 996 * We assume that @slave was already detached from the slave list.
 997 *
 998 * If @slave's permanent hw address is different both from its current
 999 * address and from @bond's address, then somewhere in the bond there's
1000 * a slave that has @slave's permanet address as its current address.
1001 * We'll make sure that that slave no longer uses @slave's permanent address.
1002 *
1003 * Caller must hold RTNL and no other locks
1004 */
1005static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1006{
1007        int perm_curr_diff;
1008        int perm_bond_diff;
1009
1010        perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1011                                                   slave->dev->dev_addr);
1012        perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1013                                                   bond->dev->dev_addr);
1014
1015        if (perm_curr_diff && perm_bond_diff) {
1016                struct slave *tmp_slave;
1017                int i, found = 0;
1018
1019                bond_for_each_slave(bond, tmp_slave, i) {
1020                        if (!compare_ether_addr_64bits(slave->perm_hwaddr,
1021                                                       tmp_slave->dev->dev_addr)) {
1022                                found = 1;
1023                                break;
1024                        }
1025                }
1026
1027                if (found) {
1028                        /* locking: needs RTNL and nothing else */
1029                        alb_swap_mac_addr(bond, slave, tmp_slave);
1030                        alb_fasten_mac_swap(bond, slave, tmp_slave);
1031                }
1032        }
1033}
1034
1035/**
1036 * alb_handle_addr_collision_on_attach
1037 * @bond: bonding we're working on
1038 * @slave: the slave that was just attached
1039 *
1040 * checks uniqueness of slave's mac address and handles the case the
1041 * new slave uses the bonds mac address.
1042 *
1043 * If the permanent hw address of @slave is @bond's hw address, we need to
1044 * find a different hw address to give @slave, that isn't in use by any other
1045 * slave in the bond. This address must be, of course, one of the premanent
1046 * addresses of the other slaves.
1047 *
1048 * We go over the slave list, and for each slave there we compare its
1049 * permanent hw address with the current address of all the other slaves.
1050 * If no match was found, then we've found a slave with a permanent address
1051 * that isn't used by any other slave in the bond, so we can assign it to
1052 * @slave.
1053 *
1054 * assumption: this function is called before @slave is attached to the
1055 *             bond slave list.
1056 *
1057 * caller must hold the bond lock for write since the mac addresses are compared
1058 * and may be swapped.
1059 */
1060static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1061{
1062        struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1063        struct slave *has_bond_addr = bond->curr_active_slave;
1064        int i, j, found = 0;
1065
1066        if (bond->slave_cnt == 0) {
1067                /* this is the first slave */
1068                return 0;
1069        }
1070
1071        /* if slave's mac address differs from bond's mac address
1072         * check uniqueness of slave's mac address against the other
1073         * slaves in the bond.
1074         */
1075        if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
1076                bond_for_each_slave(bond, tmp_slave1, i) {
1077                        if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1078                                                       slave->dev->dev_addr)) {
1079                                found = 1;
1080                                break;
1081                        }
1082                }
1083
1084                if (!found)
1085                        return 0;
1086
1087                /* Try setting slave mac to bond address and fall-through
1088                   to code handling that situation below... */
1089                alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1090                                       bond->alb_info.rlb_enabled);
1091        }
1092
1093        /* The slave's address is equal to the address of the bond.
1094         * Search for a spare address in the bond for this slave.
1095         */
1096        free_mac_slave = NULL;
1097
1098        bond_for_each_slave(bond, tmp_slave1, i) {
1099                found = 0;
1100                bond_for_each_slave(bond, tmp_slave2, j) {
1101                        if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1102                                                       tmp_slave2->dev->dev_addr)) {
1103                                found = 1;
1104                                break;
1105                        }
1106                }
1107
1108                if (!found) {
1109                        /* no slave has tmp_slave1's perm addr
1110                         * as its curr addr
1111                         */
1112                        free_mac_slave = tmp_slave1;
1113                        break;
1114                }
1115
1116                if (!has_bond_addr) {
1117                        if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1118                                                       bond->dev->dev_addr)) {
1119
1120                                has_bond_addr = tmp_slave1;
1121                        }
1122                }
1123        }
1124
1125        if (free_mac_slave) {
1126                alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1127                                       bond->alb_info.rlb_enabled);
1128
1129                pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1130                           bond->dev->name, slave->dev->name,
1131                           free_mac_slave->dev->name);
1132
1133        } else if (has_bond_addr) {
1134                pr_err("%s: Error: the hw address of slave %s 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",
1135                       bond->dev->name, slave->dev->name);
1136                return -EFAULT;
1137        }
1138
1139        return 0;
1140}
1141
1142/**
1143 * alb_set_mac_address
1144 * @bond:
1145 * @addr:
1146 *
1147 * In TLB mode all slaves are configured to the bond's hw address, but set
1148 * their dev_addr field to different addresses (based on their permanent hw
1149 * addresses).
1150 *
1151 * For each slave, this function sets the interface to the new address and then
1152 * changes its dev_addr field to its previous value.
1153 *
1154 * Unwinding assumes bond's mac address has not yet changed.
1155 */
1156static int alb_set_mac_address(struct bonding *bond, void *addr)
1157{
1158        struct sockaddr sa;
1159        struct slave *slave, *stop_at;
1160        char tmp_addr[ETH_ALEN];
1161        int res;
1162        int i;
1163
1164        if (bond->alb_info.rlb_enabled) {
1165                return 0;
1166        }
1167
1168        bond_for_each_slave(bond, slave, i) {
1169                /* save net_device's current hw address */
1170                memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1171
1172                res = dev_set_mac_address(slave->dev, addr);
1173
1174                /* restore net_device's hw address */
1175                memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1176
1177                if (res)
1178                        goto unwind;
1179        }
1180
1181        return 0;
1182
1183unwind:
1184        memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1185        sa.sa_family = bond->dev->type;
1186
1187        /* unwind from head to the slave that failed */
1188        stop_at = slave;
1189        bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1190                memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1191                dev_set_mac_address(slave->dev, &sa);
1192                memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1193        }
1194
1195        return res;
1196}
1197
1198/************************ exported alb funcions ************************/
1199
1200int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1201{
1202        int res;
1203
1204        res = tlb_initialize(bond);
1205        if (res) {
1206                return res;
1207        }
1208
1209        if (rlb_enabled) {
1210                bond->alb_info.rlb_enabled = 1;
1211                /* initialize rlb */
1212                res = rlb_initialize(bond);
1213                if (res) {
1214                        tlb_deinitialize(bond);
1215                        return res;
1216                }
1217        } else {
1218                bond->alb_info.rlb_enabled = 0;
1219        }
1220
1221        return 0;
1222}
1223
1224void bond_alb_deinitialize(struct bonding *bond)
1225{
1226        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1227
1228        tlb_deinitialize(bond);
1229
1230        if (bond_info->rlb_enabled) {
1231                rlb_deinitialize(bond);
1232        }
1233}
1234
1235int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1236{
1237        struct bonding *bond = netdev_priv(bond_dev);
1238        struct ethhdr *eth_data;
1239        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1240        struct slave *tx_slave = NULL;
1241        static const __be32 ip_bcast = htonl(0xffffffff);
1242        int hash_size = 0;
1243        int do_tx_balance = 1;
1244        u32 hash_index = 0;
1245        const u8 *hash_start = NULL;
1246        int res = 1;
1247        struct ipv6hdr *ip6hdr;
1248
1249        skb_reset_mac_header(skb);
1250        eth_data = eth_hdr(skb);
1251
1252        /* make sure that the curr_active_slave and the slaves list do
1253         * not change during tx
1254         */
1255        read_lock(&bond->lock);
1256        read_lock(&bond->curr_slave_lock);
1257
1258        if (!BOND_IS_OK(bond)) {
1259                goto out;
1260        }
1261
1262        switch (ntohs(skb->protocol)) {
1263        case ETH_P_IP: {
1264                const struct iphdr *iph = ip_hdr(skb);
1265
1266                if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
1267                    (iph->daddr == ip_bcast) ||
1268                    (iph->protocol == IPPROTO_IGMP)) {
1269                        do_tx_balance = 0;
1270                        break;
1271                }
1272                hash_start = (char *)&(iph->daddr);
1273                hash_size = sizeof(iph->daddr);
1274        }
1275                break;
1276        case ETH_P_IPV6:
1277                /* IPv6 doesn't really use broadcast mac address, but leave
1278                 * that here just in case.
1279                 */
1280                if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
1281                        do_tx_balance = 0;
1282                        break;
1283                }
1284
1285                /* IPv6 uses all-nodes multicast as an equivalent to
1286                 * broadcasts in IPv4.
1287                 */
1288                if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
1289                        do_tx_balance = 0;
1290                        break;
1291                }
1292
1293                /* Additianally, DAD probes should not be tx-balanced as that
1294                 * will lead to false positives for duplicate addresses and
1295                 * prevent address configuration from working.
1296                 */
1297                ip6hdr = ipv6_hdr(skb);
1298                if (ipv6_addr_any(&ip6hdr->saddr)) {
1299                        do_tx_balance = 0;
1300                        break;
1301                }
1302
1303                hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1304                hash_size = sizeof(ipv6_hdr(skb)->daddr);
1305                break;
1306        case ETH_P_IPX:
1307                if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1308                        /* something is wrong with this packet */
1309                        do_tx_balance = 0;
1310                        break;
1311                }
1312
1313                if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1314                        /* The only protocol worth balancing in
1315                         * this family since it has an "ARP" like
1316                         * mechanism
1317                         */
1318                        do_tx_balance = 0;
1319                        break;
1320                }
1321
1322                hash_start = (char*)eth_data->h_dest;
1323                hash_size = ETH_ALEN;
1324                break;
1325        case ETH_P_ARP:
1326                do_tx_balance = 0;
1327                if (bond_info->rlb_enabled) {
1328                        tx_slave = rlb_arp_xmit(skb, bond);
1329                }
1330                break;
1331        default:
1332                do_tx_balance = 0;
1333                break;
1334        }
1335
1336        if (do_tx_balance) {
1337                hash_index = _simple_hash(hash_start, hash_size);
1338                tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1339        }
1340
1341        if (!tx_slave) {
1342                /* unbalanced or unassigned, send through primary */
1343                tx_slave = bond->curr_active_slave;
1344                bond_info->unbalanced_load += skb->len;
1345        }
1346
1347        if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1348                if (tx_slave != bond->curr_active_slave) {
1349                        memcpy(eth_data->h_source,
1350                               tx_slave->dev->dev_addr,
1351                               ETH_ALEN);
1352                }
1353
1354                res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1355        } else {
1356                if (tx_slave) {
1357                        tlb_clear_slave(bond, tx_slave, 0);
1358                }
1359        }
1360
1361out:
1362        if (res) {
1363                /* no suitable interface, frame not sent */
1364                dev_kfree_skb(skb);
1365        }
1366        read_unlock(&bond->curr_slave_lock);
1367        read_unlock(&bond->lock);
1368        return NETDEV_TX_OK;
1369}
1370
1371void bond_alb_monitor(struct work_struct *work)
1372{
1373        struct bonding *bond = container_of(work, struct bonding,
1374                                            alb_work.work);
1375        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1376        struct slave *slave;
1377        int i;
1378
1379        read_lock(&bond->lock);
1380
1381        if (bond->kill_timers) {
1382                goto out;
1383        }
1384
1385        if (bond->slave_cnt == 0) {
1386                bond_info->tx_rebalance_counter = 0;
1387                bond_info->lp_counter = 0;
1388                goto re_arm;
1389        }
1390
1391        bond_info->tx_rebalance_counter++;
1392        bond_info->lp_counter++;
1393
1394        /* send learning packets */
1395        if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1396                /* change of curr_active_slave involves swapping of mac addresses.
1397                 * in order to avoid this swapping from happening while
1398                 * sending the learning packets, the curr_slave_lock must be held for
1399                 * read.
1400                 */
1401                read_lock(&bond->curr_slave_lock);
1402
1403                bond_for_each_slave(bond, slave, i) {
1404                        alb_send_learning_packets(slave, slave->dev->dev_addr);
1405                }
1406
1407                read_unlock(&bond->curr_slave_lock);
1408
1409                bond_info->lp_counter = 0;
1410        }
1411
1412        /* rebalance tx traffic */
1413        if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1414
1415                read_lock(&bond->curr_slave_lock);
1416
1417                bond_for_each_slave(bond, slave, i) {
1418                        tlb_clear_slave(bond, slave, 1);
1419                        if (slave == bond->curr_active_slave) {
1420                                SLAVE_TLB_INFO(slave).load =
1421                                        bond_info->unbalanced_load /
1422                                                BOND_TLB_REBALANCE_INTERVAL;
1423                                bond_info->unbalanced_load = 0;
1424                        }
1425                }
1426
1427                read_unlock(&bond->curr_slave_lock);
1428
1429                bond_info->tx_rebalance_counter = 0;
1430        }
1431
1432        /* handle rlb stuff */
1433        if (bond_info->rlb_enabled) {
1434                if (bond_info->primary_is_promisc &&
1435                    (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1436
1437                        /*
1438                         * dev_set_promiscuity requires rtnl and
1439                         * nothing else.
1440                         */
1441                        read_unlock(&bond->lock);
1442                        rtnl_lock();
1443
1444                        bond_info->rlb_promisc_timeout_counter = 0;
1445
1446                        /* If the primary was set to promiscuous mode
1447                         * because a slave was disabled then
1448                         * it can now leave promiscuous mode.
1449                         */
1450                        dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1451                        bond_info->primary_is_promisc = 0;
1452
1453                        rtnl_unlock();
1454                        read_lock(&bond->lock);
1455                }
1456
1457                if (bond_info->rlb_rebalance) {
1458                        bond_info->rlb_rebalance = 0;
1459                        rlb_rebalance(bond);
1460                }
1461
1462                /* check if clients need updating */
1463                if (bond_info->rx_ntt) {
1464                        if (bond_info->rlb_update_delay_counter) {
1465                                --bond_info->rlb_update_delay_counter;
1466                        } else {
1467                                rlb_update_rx_clients(bond);
1468                                if (bond_info->rlb_update_retry_counter) {
1469                                        --bond_info->rlb_update_retry_counter;
1470                                } else {
1471                                        bond_info->rx_ntt = 0;
1472                                }
1473                        }
1474                }
1475        }
1476
1477re_arm:
1478        queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1479out:
1480        read_unlock(&bond->lock);
1481}
1482
1483/* assumption: called before the slave is attached to the bond
1484 * and not locked by the bond lock
1485 */
1486int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1487{
1488        int res;
1489
1490        res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1491                                     bond->alb_info.rlb_enabled);
1492        if (res) {
1493                return res;
1494        }
1495
1496        /* caller must hold the bond lock for write since the mac addresses
1497         * are compared and may be swapped.
1498         */
1499        read_lock(&bond->lock);
1500
1501        res = alb_handle_addr_collision_on_attach(bond, slave);
1502
1503        read_unlock(&bond->lock);
1504
1505        if (res) {
1506                return res;
1507        }
1508
1509        tlb_init_slave(slave);
1510
1511        /* order a rebalance ASAP */
1512        bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1513
1514        if (bond->alb_info.rlb_enabled) {
1515                bond->alb_info.rlb_rebalance = 1;
1516        }
1517
1518        return 0;
1519}
1520
1521/*
1522 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1523 * if necessary.
1524 *
1525 * Caller must hold RTNL and no other locks
1526 */
1527void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1528{
1529        if (bond->slave_cnt > 1) {
1530                alb_change_hw_addr_on_detach(bond, slave);
1531        }
1532
1533        tlb_clear_slave(bond, slave, 0);
1534
1535        if (bond->alb_info.rlb_enabled) {
1536                bond->alb_info.next_rx_slave = NULL;
1537                rlb_clear_slave(bond, slave);
1538        }
1539}
1540
1541/* Caller must hold bond lock for read */
1542void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1543{
1544        struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1545
1546        if (link == BOND_LINK_DOWN) {
1547                tlb_clear_slave(bond, slave, 0);
1548                if (bond->alb_info.rlb_enabled) {
1549                        rlb_clear_slave(bond, slave);
1550                }
1551        } else if (link == BOND_LINK_UP) {
1552                /* order a rebalance ASAP */
1553                bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1554                if (bond->alb_info.rlb_enabled) {
1555                        bond->alb_info.rlb_rebalance = 1;
1556                        /* If the updelay module parameter is smaller than the
1557                         * forwarding delay of the switch the rebalance will
1558                         * not work because the rebalance arp replies will
1559                         * not be forwarded to the clients..
1560                         */
1561                }
1562        }
1563}
1564
1565/**
1566 * bond_alb_handle_active_change - assign new curr_active_slave
1567 * @bond: our bonding struct
1568 * @new_slave: new slave to assign
1569 *
1570 * Set the bond->curr_active_slave to @new_slave and handle
1571 * mac address swapping and promiscuity changes as needed.
1572 *
1573 * If new_slave is NULL, caller must hold curr_slave_lock or
1574 * bond->lock for write.
1575 *
1576 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1577 * read and curr_slave_lock for write.  Processing here may sleep, so
1578 * no other locks may be held.
1579 */
1580void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1581        __releases(&bond->curr_slave_lock)
1582        __releases(&bond->lock)
1583        __acquires(&bond->lock)
1584        __acquires(&bond->curr_slave_lock)
1585{
1586        struct slave *swap_slave;
1587        int i;
1588
1589        if (bond->curr_active_slave == new_slave) {
1590                return;
1591        }
1592
1593        if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1594                dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1595                bond->alb_info.primary_is_promisc = 0;
1596                bond->alb_info.rlb_promisc_timeout_counter = 0;
1597        }
1598
1599        swap_slave = bond->curr_active_slave;
1600        bond->curr_active_slave = new_slave;
1601
1602        if (!new_slave || (bond->slave_cnt == 0)) {
1603                return;
1604        }
1605
1606        /* set the new curr_active_slave to the bonds mac address
1607         * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1608         */
1609        if (!swap_slave) {
1610                struct slave *tmp_slave;
1611                /* find slave that is holding the bond's mac address */
1612                bond_for_each_slave(bond, tmp_slave, i) {
1613                        if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1614                                                       bond->dev->dev_addr)) {
1615                                swap_slave = tmp_slave;
1616                                break;
1617                        }
1618                }
1619        }
1620
1621        /*
1622         * Arrange for swap_slave and new_slave to temporarily be
1623         * ignored so we can mess with their MAC addresses without
1624         * fear of interference from transmit activity.
1625         */
1626        if (swap_slave) {
1627                tlb_clear_slave(bond, swap_slave, 1);
1628        }
1629        tlb_clear_slave(bond, new_slave, 1);
1630
1631        write_unlock_bh(&bond->curr_slave_lock);
1632        read_unlock(&bond->lock);
1633
1634        ASSERT_RTNL();
1635
1636        /* curr_active_slave must be set before calling alb_swap_mac_addr */
1637        if (swap_slave) {
1638                /* swap mac address */
1639                alb_swap_mac_addr(bond, swap_slave, new_slave);
1640        } else {
1641                /* set the new_slave to the bond mac address */
1642                alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1643                                       bond->alb_info.rlb_enabled);
1644        }
1645
1646        if (swap_slave) {
1647                alb_fasten_mac_swap(bond, swap_slave, new_slave);
1648                read_lock(&bond->lock);
1649        } else {
1650                read_lock(&bond->lock);
1651                alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1652        }
1653
1654        write_lock_bh(&bond->curr_slave_lock);
1655}
1656
1657/*
1658 * Called with RTNL
1659 */
1660int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1661        __acquires(&bond->lock)
1662        __releases(&bond->lock)
1663{
1664        struct bonding *bond = netdev_priv(bond_dev);
1665        struct sockaddr *sa = addr;
1666        struct slave *slave, *swap_slave;
1667        int res;
1668        int i;
1669
1670        if (!is_valid_ether_addr(sa->sa_data)) {
1671                return -EADDRNOTAVAIL;
1672        }
1673
1674        res = alb_set_mac_address(bond, addr);
1675        if (res) {
1676                return res;
1677        }
1678
1679        memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1680
1681        /* If there is no curr_active_slave there is nothing else to do.
1682         * Otherwise we'll need to pass the new address to it and handle
1683         * duplications.
1684         */
1685        if (!bond->curr_active_slave) {
1686                return 0;
1687        }
1688
1689        swap_slave = NULL;
1690
1691        bond_for_each_slave(bond, slave, i) {
1692                if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1693                                               bond_dev->dev_addr)) {
1694                        swap_slave = slave;
1695                        break;
1696                }
1697        }
1698
1699        if (swap_slave) {
1700                alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1701                alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
1702        } else {
1703                alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1704                                       bond->alb_info.rlb_enabled);
1705
1706                read_lock(&bond->lock);
1707                alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1708                if (bond->alb_info.rlb_enabled) {
1709                        /* inform clients mac address has changed */
1710                        rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1711                }
1712                read_unlock(&bond->lock);
1713        }
1714
1715        return 0;
1716}
1717
1718void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1719{
1720        if (bond->alb_info.current_alb_vlan &&
1721            (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1722                bond->alb_info.current_alb_vlan = NULL;
1723        }
1724
1725        if (bond->alb_info.rlb_enabled) {
1726                rlb_clear_vlan(bond, vlan_id);
1727        }
1728}
1729
1730