23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/netdevice.h>
30 #include <linux/slab.h>
33 #include <linux/ipv6.h>
34 #include <linux/if_arp.h>
35 #include <linux/if_ether.h>
37 #include <linux/if_vlan.h>
42 #include <asm/byteorder.h>
48 #ifndef __long_aligned
49 #define __long_aligned __attribute__((aligned((sizeof(long)))))
52 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
55 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
82 return (
struct arp_pkt *)skb_network_header(skb);
88 static inline u8 _simple_hash(
const u8 *hash_start,
int hash_size)
93 for (i = 0; i < hash_size; i++) {
94 hash ^= hash_start[
i];
102 static inline void _lock_tx_hashtbl_bh(
struct bonding *bond)
107 static inline void _unlock_tx_hashtbl_bh(
struct bonding *bond)
112 static inline void _lock_tx_hashtbl(
struct bonding *bond)
117 static inline void _unlock_tx_hashtbl(
struct bonding *bond)
136 static inline void tlb_init_slave(
struct slave *
slave)
157 tlb_init_table_entry(&tx_hash_table[index], save_load);
162 tlb_init_slave(slave);
166 static void tlb_clear_slave(
struct bonding *bond,
struct slave *slave,
169 _lock_tx_hashtbl_bh(bond);
170 __tlb_clear_slave(bond, slave, save_load);
171 _unlock_tx_hashtbl_bh(bond);
175 static int tlb_initialize(
struct bonding *bond)
186 _lock_tx_hashtbl_bh(bond);
191 tlb_init_table_entry(&bond_info->
tx_hashtbl[i], 0);
194 _unlock_tx_hashtbl_bh(bond);
200 static void tlb_deinitialize(
struct bonding *bond)
204 _lock_tx_hashtbl_bh(bond);
209 _unlock_tx_hashtbl_bh(bond);
212 static long long compute_gap(
struct slave *slave)
219 static struct slave *tlb_get_least_loaded_slave(
struct bonding *bond)
221 struct slave *slave, *least_loaded;
231 long long gap = compute_gap(slave);
234 least_loaded = slave;
243 static struct slave *__tlb_choose_channel(
struct bonding *bond,
u32 hash_index,
248 struct slave *assigned_slave;
251 assigned_slave = hash_table[hash_index].
tx_slave;
252 if (!assigned_slave) {
253 assigned_slave = tlb_get_least_loaded_slave(bond);
255 if (assigned_slave) {
258 u32 next_index = slave_info->
head;
260 hash_table[hash_index].
tx_slave = assigned_slave;
261 hash_table[hash_index].
next = next_index;
265 hash_table[next_index].
prev = hash_index;
268 slave_info->
head = hash_index;
274 if (assigned_slave) {
275 hash_table[hash_index].
tx_bytes += skb_len;
278 return assigned_slave;
282 static struct slave *tlb_choose_channel(
struct bonding *bond,
u32 hash_index,
285 struct slave *tx_slave;
291 _lock_tx_hashtbl(bond);
292 tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
293 _unlock_tx_hashtbl(bond);
298 static inline void _lock_rx_hashtbl_bh(
struct bonding *bond)
303 static inline void _unlock_rx_hashtbl_bh(
struct bonding *bond)
308 static inline void _lock_rx_hashtbl(
struct bonding *bond)
313 static inline void _unlock_rx_hashtbl(
struct bonding *bond)
321 static void rlb_update_entry_from_arp(
struct bonding *bond,
struct arp_pkt *arp)
327 _lock_rx_hashtbl_bh(bond);
329 hash_index = _simple_hash((
u8*)&(arp->
ip_src),
sizeof(arp->
ip_src));
330 client_info = &(bond_info->
rx_hashtbl[hash_index]);
338 client_info->
ntt = 1;
342 _unlock_rx_hashtbl_bh(bond);
353 arp = skb_header_pointer(skb, 0,
sizeof(_arp), &_arp);
359 rlb_update_entry_from_arp(bond, arp);
360 pr_debug(
"Server received an ARP Reply from client\n");
367 static struct slave *rlb_next_rx_slave(
struct bonding *bond)
370 struct slave *rx_slave, *slave, *start_at;
403 static void rlb_teach_disabled_mac_on_primary(
struct bonding *bond,
u8 addr[])
409 if (!bond->
alb_info.primary_is_promisc) {
411 bond->
alb_info.primary_is_promisc = 1;
413 bond->
alb_info.primary_is_promisc = 0;
416 bond->
alb_info.rlb_promisc_timeout_counter = 0;
425 static void rlb_clear_slave(
struct bonding *bond,
struct slave *slave)
432 _lock_rx_hashtbl_bh(bond);
438 if (rx_hash_table[index].slave == slave) {
439 struct slave *assigned_slave = rlb_next_rx_slave(bond);
441 if (assigned_slave) {
443 if (!ether_addr_equal_64bits(rx_hash_table[index].mac_dst,
463 _unlock_rx_hashtbl_bh(bond);
468 rlb_teach_disabled_mac_on_primary(bond, slave->
dev->dev_addr);
478 if (!client_info->
slave) {
487 client_info->
slave->dev,
490 client_info->
slave->dev->dev_addr,
493 pr_err(
"%s: Error: failed to create an ARP packet\n",
494 client_info->
slave->dev->master->name);
500 if (client_info->
tag) {
501 skb = vlan_put_tag(skb, client_info->
vlan_id);
503 pr_err(
"%s: Error: failed to insert VLAN tag\n",
504 client_info->
slave->dev->master->name);
514 static void rlb_update_rx_clients(
struct bonding *bond)
520 _lock_rx_hashtbl_bh(bond);
524 client_info = &(bond_info->
rx_hashtbl[hash_index]);
525 if (client_info->
ntt) {
526 rlb_update_client(client_info);
528 client_info->
ntt = 0;
538 _unlock_rx_hashtbl_bh(bond);
542 static void rlb_req_update_slave_clients(
struct bonding *bond,
struct slave *slave)
549 _lock_rx_hashtbl_bh(bond);
553 client_info = &(bond_info->
rx_hashtbl[hash_index]);
555 if ((client_info->
slave == slave) &&
556 !ether_addr_equal_64bits(client_info->
mac_dst, mac_bcast)) {
557 client_info->
ntt = 1;
569 _unlock_rx_hashtbl_bh(bond);
573 static void rlb_req_update_subnet_clients(
struct bonding *bond,
__be32 src_ip)
579 _lock_rx_hashtbl(bond);
583 client_info = &(bond_info->
rx_hashtbl[hash_index]);
585 if (!client_info->
slave) {
586 pr_err(
"%s: Error: found a client with no channel in the client's hash table\n",
594 if ((client_info->
ip_src == src_ip) &&
595 !ether_addr_equal_64bits(client_info->
slave->dev->dev_addr,
596 bond->
dev->dev_addr) &&
597 !ether_addr_equal_64bits(client_info->
mac_dst, mac_bcast)) {
598 client_info->
ntt = 1;
603 _unlock_rx_hashtbl(bond);
607 static struct slave *rlb_choose_channel(
struct sk_buff *skb,
struct bonding *bond)
611 struct slave *assigned_slave;
615 _lock_rx_hashtbl(bond);
617 hash_index = _simple_hash((
u8 *)&arp->
ip_dst,
sizeof(arp->
ip_dst));
618 client_info = &(bond_info->
rx_hashtbl[hash_index]);
624 if (!ether_addr_equal_64bits(arp->
mac_dst, mac_bcast)) {
629 assigned_slave = client_info->
slave;
630 if (assigned_slave) {
631 _unlock_rx_hashtbl(bond);
632 return assigned_slave;
642 rlb_update_client(client_info);
647 assigned_slave = rlb_next_rx_slave(bond);
649 if (assigned_slave) {
657 client_info->
slave = assigned_slave;
659 if (!ether_addr_equal_64bits(client_info->
mac_dst, mac_bcast)) {
660 client_info->
ntt = 1;
663 client_info->
ntt = 0;
666 if (bond_vlan_used(bond)) {
667 if (!vlan_get_tag(skb, &client_info->
vlan_id))
668 client_info->
tag = 1;
674 client_info->
next = prev_tbl_head;
683 _unlock_rx_hashtbl(bond);
685 return assigned_slave;
692 static struct slave *rlb_arp_xmit(
struct sk_buff *skb,
struct bonding *bond)
695 struct slave *tx_slave =
NULL;
701 tx_slave = rlb_choose_channel(skb, bond);
705 pr_debug(
"Server sent ARP Reply packet\n");
712 rlb_choose_channel(skb, bond);
724 rlb_req_update_subnet_clients(bond, arp->
ip_src);
725 pr_debug(
"Server sent ARP Request packet\n");
732 static void rlb_rebalance(
struct bonding *bond)
735 struct slave *assigned_slave;
740 _lock_rx_hashtbl_bh(bond);
745 client_info = &(bond_info->
rx_hashtbl[hash_index]);
746 assigned_slave = rlb_next_rx_slave(bond);
747 if (assigned_slave && (client_info->
slave != assigned_slave)) {
748 client_info->
slave = assigned_slave;
749 client_info->
ntt = 1;
758 _unlock_rx_hashtbl_bh(bond);
769 static int rlb_initialize(
struct bonding *bond)
780 _lock_rx_hashtbl_bh(bond);
787 rlb_init_table_entry(bond_info->
rx_hashtbl + i);
790 _unlock_rx_hashtbl_bh(bond);
798 static void rlb_deinitialize(
struct bonding *bond)
802 _lock_rx_hashtbl_bh(bond);
808 _unlock_rx_hashtbl_bh(bond);
811 static void rlb_clear_vlan(
struct bonding *bond,
unsigned short vlan_id)
816 _lock_rx_hashtbl_bh(bond);
824 if (curr->
tag && (curr->
vlan_id == vlan_id)) {
829 bond_info->
rx_hashtbl[prev_index].next = next_index;
832 bond_info->
rx_hashtbl[next_index].prev = prev_index;
835 rlb_init_table_entry(curr);
838 curr_index = next_index;
841 _unlock_rx_hashtbl_bh(bond);
846 static void alb_send_learning_packets(
struct slave *slave,
u8 mac_addr[])
848 struct bonding *bond = bond_get_bond_by_slave(slave);
862 skb = dev_alloc_skb(size);
870 skb_reset_mac_header(skb);
876 if (bond_vlan_used(bond)) {
888 skb = vlan_put_tag(skb, vlan->
vlan_id);
890 pr_err(
"%s: Error: failed to insert VLAN tag\n",
900 static int alb_set_slave_mac_addr(
struct slave *slave,
u8 addr[])
913 s_addr.sa_family = dev->
type;
915 pr_err(
"%s: Error: dev_set_mac_address of dev %s failed!\n"
916 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
930 static void alb_swap_mac_addr(
struct bonding *bond,
struct slave *slave1,
struct slave *slave2)
935 alb_set_slave_mac_addr(slave1, slave2->
dev->dev_addr);
936 alb_set_slave_mac_addr(slave2, tmp_mac_addr);
945 static void alb_fasten_mac_swap(
struct bonding *bond,
struct slave *slave1,
946 struct slave *slave2)
949 struct slave *disabled_slave =
NULL;
955 alb_send_learning_packets(slave1, slave1->
dev->dev_addr);
960 rlb_req_update_slave_clients(bond, slave1);
963 disabled_slave = slave1;
967 alb_send_learning_packets(slave2, slave2->
dev->dev_addr);
972 rlb_req_update_slave_clients(bond, slave2);
975 disabled_slave = slave2;
978 if (bond->
alb_info.rlb_enabled && slaves_state_differ) {
980 rlb_teach_disabled_mac_on_primary(bond,
981 disabled_slave->
dev->dev_addr);
999 static void alb_change_hw_addr_on_detach(
struct bonding *bond,
struct slave *slave)
1004 perm_curr_diff = !ether_addr_equal_64bits(slave->
perm_hwaddr,
1005 slave->
dev->dev_addr);
1006 perm_bond_diff = !ether_addr_equal_64bits(slave->
perm_hwaddr,
1007 bond->
dev->dev_addr);
1009 if (perm_curr_diff && perm_bond_diff) {
1010 struct slave *tmp_slave;
1015 tmp_slave->
dev->dev_addr)) {
1023 alb_swap_mac_addr(bond, slave, tmp_slave);
1024 alb_fasten_mac_swap(bond, slave, tmp_slave);
1054 static int alb_handle_addr_collision_on_attach(
struct bonding *bond,
struct slave *slave)
1056 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1058 int i,
j, found = 0;
1069 if (!ether_addr_equal_64bits(slave->
perm_hwaddr, bond->
dev->dev_addr)) {
1071 if (ether_addr_equal_64bits(tmp_slave1->
dev->dev_addr,
1072 slave->
dev->dev_addr)) {
1083 alb_set_slave_mac_addr(slave, bond->
dev->dev_addr);
1089 free_mac_slave =
NULL;
1094 if (ether_addr_equal_64bits(tmp_slave1->
perm_hwaddr,
1095 tmp_slave2->
dev->dev_addr)) {
1105 free_mac_slave = tmp_slave1;
1109 if (!has_bond_addr) {
1110 if (ether_addr_equal_64bits(tmp_slave1->
dev->dev_addr,
1111 bond->
dev->dev_addr)) {
1113 has_bond_addr = tmp_slave1;
1118 if (free_mac_slave) {
1119 alb_set_slave_mac_addr(slave, free_mac_slave->
perm_hwaddr);
1121 pr_warning(
"%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1122 bond->
dev->name, slave->
dev->name,
1123 free_mac_slave->
dev->name);
1125 }
else if (has_bond_addr) {
1126 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",
1127 bond->
dev->name, slave->
dev->name);
1148 static int alb_set_mac_address(
struct bonding *bond,
void *addr)
1151 struct slave *slave, *stop_at;
1177 sa.sa_family = bond->
dev->type;
1196 res = tlb_initialize(bond);
1204 res = rlb_initialize(bond);
1206 tlb_deinitialize(bond);
1220 tlb_deinitialize(bond);
1223 rlb_deinitialize(bond);
1229 struct bonding *bond = netdev_priv(bond_dev);
1232 struct slave *tx_slave =
NULL;
1235 int do_tx_balance = 1;
1237 const u8 *hash_start =
NULL;
1241 skb_reset_mac_header(skb);
1242 eth_data = eth_hdr(skb);
1250 const struct iphdr *iph = ip_hdr(skb);
1252 if (ether_addr_equal_64bits(eth_data->
h_dest, mac_bcast) ||
1253 (iph->
daddr == ip_bcast) ||
1258 hash_start = (
char *)&(iph->
daddr);
1259 hash_size =
sizeof(iph->
daddr);
1266 if (ether_addr_equal_64bits(eth_data->
h_dest, mac_bcast)) {
1274 if (ether_addr_equal_64bits(eth_data->
h_dest, mac_v6_allmcast)) {
1283 ip6hdr = ipv6_hdr(skb);
1284 if (ipv6_addr_any(&ip6hdr->
saddr)) {
1289 hash_start = (
char *)&(ipv6_hdr(skb)->daddr);
1290 hash_size =
sizeof(ipv6_hdr(skb)->daddr);
1308 hash_start = (
char*)eth_data->
h_dest;
1314 tx_slave = rlb_arp_xmit(skb, bond);
1322 if (do_tx_balance) {
1323 hash_index = _simple_hash(hash_start, hash_size);
1324 tx_slave = tlb_choose_channel(bond, hash_index, skb->
len);
1336 tx_slave->
dev->dev_addr,
1343 _lock_tx_hashtbl(bond);
1344 __tlb_clear_slave(bond, tx_slave, 0);
1345 _unlock_tx_hashtbl(bond);
1363 struct slave *slave;
1387 alb_send_learning_packets(slave, slave->
dev->dev_addr);
1401 tlb_clear_slave(bond, slave, 1);
1445 rlb_rebalance(bond);
1453 rlb_update_rx_clients(bond);
1476 res = alb_set_slave_mac_addr(slave, slave->
perm_hwaddr);
1486 res = alb_handle_addr_collision_on_attach(bond, slave);
1494 tlb_init_slave(slave);
1515 alb_change_hw_addr_on_detach(bond, slave);
1518 tlb_clear_slave(bond, slave, 0);
1522 rlb_clear_slave(bond, slave);
1532 tlb_clear_slave(bond, slave, 0);
1534 rlb_clear_slave(bond, slave);
1571 struct slave *swap_slave;
1580 bond->
alb_info.primary_is_promisc = 0;
1581 bond->
alb_info.rlb_promisc_timeout_counter = 0;
1587 if (!new_slave || (bond->
slave_cnt == 0)) {
1595 struct slave *tmp_slave;
1598 if (ether_addr_equal_64bits(tmp_slave->
dev->dev_addr,
1599 bond->
dev->dev_addr)) {
1600 swap_slave = tmp_slave;
1612 tlb_clear_slave(bond, swap_slave, 1);
1614 tlb_clear_slave(bond, new_slave, 1);
1624 alb_swap_mac_addr(bond, swap_slave, new_slave);
1627 alb_set_slave_mac_addr(new_slave, bond->
dev->dev_addr);
1631 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1635 alb_send_learning_packets(new_slave, bond->
dev->dev_addr);
1648 struct bonding *bond = netdev_priv(bond_dev);
1650 struct slave *slave, *swap_slave;
1654 if (!is_valid_ether_addr(sa->
sa_data)) {
1658 res = alb_set_mac_address(bond, addr);
1663 memcpy(bond_dev->dev_addr, sa->
sa_data, bond_dev->addr_len);
1676 if (ether_addr_equal_64bits(slave->
dev->dev_addr,
1677 bond_dev->dev_addr)) {
1703 if (bond->
alb_info.current_alb_vlan &&
1704 (bond->
alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1709 rlb_clear_vlan(bond, vlan_id);