bond_alb.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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. * Changes:
  23. *
  24. * 2003/06/25 - Shmulik Hen <shmulik.hen at intel dot com>
  25. * - Fixed signed/unsigned calculation errors that caused load sharing
  26. * to collapse to one slave under very heavy UDP Tx stress.
  27. *
  28. * 2003/08/06 - Amir Noam <amir.noam at intel dot com>
  29. * - Add support for setting bond's MAC address with special
  30. * handling required for ALB/TLB.
  31. *
  32. * 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
  33. * - Code cleanup and style changes
  34. *
  35. * 2003/12/30 - Amir Noam <amir.noam at intel dot com>
  36. * - Fixed: Cannot remove and re-enslave the original active slave.
  37. *
  38. * 2004/01/14 - Shmulik Hen <shmulik.hen at intel dot com>
  39. * - Add capability to tag self generated packets in ALB/TLB modes.
  40. */
  41. //#define BONDING_DEBUG 1
  42. #include <linux/skbuff.h>
  43. #include <linux/netdevice.h>
  44. #include <linux/etherdevice.h>
  45. #include <linux/pkt_sched.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/slab.h>
  48. #include <linux/timer.h>
  49. #include <linux/ip.h>
  50. #include <linux/ipv6.h>
  51. #include <linux/if_arp.h>
  52. #include <linux/if_ether.h>
  53. #include <linux/if_bonding.h>
  54. #include <linux/if_vlan.h>
  55. #include <linux/in.h>
  56. #include <net/ipx.h>
  57. #include <net/arp.h>
  58. #include <asm/byteorder.h>
  59. #include "bonding.h"
  60. #include "bond_alb.h"
  61. #define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
  62. #define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
  63. * Used for division - never set
  64. * to zero !!!
  65. */
  66. #define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
  67. * learning packets to the switch
  68. */
  69. #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
  70. * ALB_TIMER_TICKS_PER_SEC)
  71. #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
  72. * ALB_TIMER_TICKS_PER_SEC)
  73. #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
  74. * Note that this value MUST NOT be smaller
  75. * because the key hash table is BYTE wide !
  76. */
  77. #define TLB_NULL_INDEX 0xffffffff
  78. #define MAX_LP_BURST 3
  79. /* rlb defs */
  80. #define RLB_HASH_TABLE_SIZE 256
  81. #define RLB_NULL_INDEX 0xffffffff
  82. #define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
  83. #define RLB_ARP_BURST_SIZE 2
  84. #define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
  85. * rebalance interval (5 min).
  86. */
  87. /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
  88. * promiscuous after failover
  89. */
  90. #define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
  91. static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
  92. static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
  93. #pragma pack(1)
  94. struct learning_pkt {
  95. u8 mac_dst[ETH_ALEN];
  96. u8 mac_src[ETH_ALEN];
  97. u16 type;
  98. u8 padding[ETH_ZLEN - ETH_HLEN];
  99. };
  100. struct arp_pkt {
  101. u16 hw_addr_space;
  102. u16 prot_addr_space;
  103. u8 hw_addr_len;
  104. u8 prot_addr_len;
  105. u16 op_code;
  106. u8 mac_src[ETH_ALEN]; /* sender hardware address */
  107. u32 ip_src; /* sender IP address */
  108. u8 mac_dst[ETH_ALEN]; /* target hardware address */
  109. u32 ip_dst; /* target IP address */
  110. };
  111. #pragma pack()
  112. /* Forward declaration */
  113. static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
  114. static inline u8 _simple_hash(u8 *hash_start, int hash_size)
  115. {
  116. int i;
  117. u8 hash = 0;
  118. for (i = 0; i < hash_size; i++) {
  119. hash ^= hash_start[i];
  120. }
  121. return hash;
  122. }
  123. /*********************** tlb specific functions ***************************/
  124. static inline void _lock_tx_hashtbl(struct bonding *bond)
  125. {
  126. spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
  127. }
  128. static inline void _unlock_tx_hashtbl(struct bonding *bond)
  129. {
  130. spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
  131. }
  132. /* Caller must hold tx_hashtbl lock */
  133. static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
  134. {
  135. if (save_load) {
  136. entry->load_history = 1 + entry->tx_bytes /
  137. BOND_TLB_REBALANCE_INTERVAL;
  138. entry->tx_bytes = 0;
  139. }
  140. entry->tx_slave = NULL;
  141. entry->next = TLB_NULL_INDEX;
  142. entry->prev = TLB_NULL_INDEX;
  143. }
  144. static inline void tlb_init_slave(struct slave *slave)
  145. {
  146. SLAVE_TLB_INFO(slave).load = 0;
  147. SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
  148. }
  149. /* Caller must hold bond lock for read */
  150. static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
  151. {
  152. struct tlb_client_info *tx_hash_table;
  153. u32 index;
  154. _lock_tx_hashtbl(bond);
  155. /* clear slave from tx_hashtbl */
  156. tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
  157. index = SLAVE_TLB_INFO(slave).head;
  158. while (index != TLB_NULL_INDEX) {
  159. u32 next_index = tx_hash_table[index].next;
  160. tlb_init_table_entry(&tx_hash_table[index], save_load);
  161. index = next_index;
  162. }
  163. _unlock_tx_hashtbl(bond);
  164. tlb_init_slave(slave);
  165. }
  166. /* Must be called before starting the monitor timer */
  167. static int tlb_initialize(struct bonding *bond)
  168. {
  169. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  170. int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
  171. int i;
  172. spin_lock_init(&(bond_info->tx_hashtbl_lock));
  173. _lock_tx_hashtbl(bond);
  174. bond_info->tx_hashtbl = kmalloc(size, GFP_KERNEL);
  175. if (!bond_info->tx_hashtbl) {
  176. printk(KERN_ERR DRV_NAME
  177. ": Error: %s: Failed to allocate TLB hash table\n",
  178. bond->dev->name);
  179. _unlock_tx_hashtbl(bond);
  180. return -1;
  181. }
  182. memset(bond_info->tx_hashtbl, 0, size);
  183. for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
  184. tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
  185. }
  186. _unlock_tx_hashtbl(bond);
  187. return 0;
  188. }
  189. /* Must be called only after all slaves have been released */
  190. static void tlb_deinitialize(struct bonding *bond)
  191. {
  192. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  193. _lock_tx_hashtbl(bond);
  194. kfree(bond_info->tx_hashtbl);
  195. bond_info->tx_hashtbl = NULL;
  196. _unlock_tx_hashtbl(bond);
  197. }
  198. /* Caller must hold bond lock for read */
  199. static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
  200. {
  201. struct slave *slave, *least_loaded;
  202. s64 max_gap;
  203. int i, found = 0;
  204. /* Find the first enabled slave */
  205. bond_for_each_slave(bond, slave, i) {
  206. if (SLAVE_IS_OK(slave)) {
  207. found = 1;
  208. break;
  209. }
  210. }
  211. if (!found) {
  212. return NULL;
  213. }
  214. least_loaded = slave;
  215. max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
  216. (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
  217. /* Find the slave with the largest gap */
  218. bond_for_each_slave_from(bond, slave, i, least_loaded) {
  219. if (SLAVE_IS_OK(slave)) {
  220. s64 gap = (s64)(slave->speed << 20) -
  221. (s64)(SLAVE_TLB_INFO(slave).load << 3);
  222. if (max_gap < gap) {
  223. least_loaded = slave;
  224. max_gap = gap;
  225. }
  226. }
  227. }
  228. return least_loaded;
  229. }
  230. /* Caller must hold bond lock for read */
  231. static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
  232. {
  233. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  234. struct tlb_client_info *hash_table;
  235. struct slave *assigned_slave;
  236. _lock_tx_hashtbl(bond);
  237. hash_table = bond_info->tx_hashtbl;
  238. assigned_slave = hash_table[hash_index].tx_slave;
  239. if (!assigned_slave) {
  240. assigned_slave = tlb_get_least_loaded_slave(bond);
  241. if (assigned_slave) {
  242. struct tlb_slave_info *slave_info =
  243. &(SLAVE_TLB_INFO(assigned_slave));
  244. u32 next_index = slave_info->head;
  245. hash_table[hash_index].tx_slave = assigned_slave;
  246. hash_table[hash_index].next = next_index;
  247. hash_table[hash_index].prev = TLB_NULL_INDEX;
  248. if (next_index != TLB_NULL_INDEX) {
  249. hash_table[next_index].prev = hash_index;
  250. }
  251. slave_info->head = hash_index;
  252. slave_info->load +=
  253. hash_table[hash_index].load_history;
  254. }
  255. }
  256. if (assigned_slave) {
  257. hash_table[hash_index].tx_bytes += skb_len;
  258. }
  259. _unlock_tx_hashtbl(bond);
  260. return assigned_slave;
  261. }
  262. /*********************** rlb specific functions ***************************/
  263. static inline void _lock_rx_hashtbl(struct bonding *bond)
  264. {
  265. spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
  266. }
  267. static inline void _unlock_rx_hashtbl(struct bonding *bond)
  268. {
  269. spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
  270. }
  271. /* when an ARP REPLY is received from a client update its info
  272. * in the rx_hashtbl
  273. */
  274. static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
  275. {
  276. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  277. struct rlb_client_info *client_info;
  278. u32 hash_index;
  279. _lock_rx_hashtbl(bond);
  280. hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
  281. client_info = &(bond_info->rx_hashtbl[hash_index]);
  282. if ((client_info->assigned) &&
  283. (client_info->ip_src == arp->ip_dst) &&
  284. (client_info->ip_dst == arp->ip_src)) {
  285. /* update the clients MAC address */
  286. memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
  287. client_info->ntt = 1;
  288. bond_info->rx_ntt = 1;
  289. }
  290. _unlock_rx_hashtbl(bond);
  291. }
  292. static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype)
  293. {
  294. struct bonding *bond = bond_dev->priv;
  295. struct arp_pkt *arp = (struct arp_pkt *)skb->data;
  296. int res = NET_RX_DROP;
  297. if (!(bond_dev->flags & IFF_MASTER)) {
  298. goto out;
  299. }
  300. if (!arp) {
  301. dprintk("Packet has no ARP data\n");
  302. goto out;
  303. }
  304. if (skb->len < sizeof(struct arp_pkt)) {
  305. dprintk("Packet is too small to be an ARP\n");
  306. goto out;
  307. }
  308. if (arp->op_code == htons(ARPOP_REPLY)) {
  309. /* update rx hash table for this ARP */
  310. rlb_update_entry_from_arp(bond, arp);
  311. dprintk("Server received an ARP Reply from client\n");
  312. }
  313. res = NET_RX_SUCCESS;
  314. out:
  315. dev_kfree_skb(skb);
  316. return res;
  317. }
  318. /* Caller must hold bond lock for read */
  319. static struct slave *rlb_next_rx_slave(struct bonding *bond)
  320. {
  321. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  322. struct slave *rx_slave, *slave, *start_at;
  323. int i = 0;
  324. if (bond_info->next_rx_slave) {
  325. start_at = bond_info->next_rx_slave;
  326. } else {
  327. start_at = bond->first_slave;
  328. }
  329. rx_slave = NULL;
  330. bond_for_each_slave_from(bond, slave, i, start_at) {
  331. if (SLAVE_IS_OK(slave)) {
  332. if (!rx_slave) {
  333. rx_slave = slave;
  334. } else if (slave->speed > rx_slave->speed) {
  335. rx_slave = slave;
  336. }
  337. }
  338. }
  339. if (rx_slave) {
  340. bond_info->next_rx_slave = rx_slave->next;
  341. }
  342. return rx_slave;
  343. }
  344. /* teach the switch the mac of a disabled slave
  345. * on the primary for fault tolerance
  346. *
  347. * Caller must hold bond->curr_slave_lock for write or bond lock for write
  348. */
  349. static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
  350. {
  351. if (!bond->curr_active_slave) {
  352. return;
  353. }
  354. if (!bond->alb_info.primary_is_promisc) {
  355. bond->alb_info.primary_is_promisc = 1;
  356. dev_set_promiscuity(bond->curr_active_slave->dev, 1);
  357. }
  358. bond->alb_info.rlb_promisc_timeout_counter = 0;
  359. alb_send_learning_packets(bond->curr_active_slave, addr);
  360. }
  361. /* slave being removed should not be active at this point
  362. *
  363. * Caller must hold bond lock for read
  364. */
  365. static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
  366. {
  367. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  368. struct rlb_client_info *rx_hash_table;
  369. u32 index, next_index;
  370. /* clear slave from rx_hashtbl */
  371. _lock_rx_hashtbl(bond);
  372. rx_hash_table = bond_info->rx_hashtbl;
  373. index = bond_info->rx_hashtbl_head;
  374. for (; index != RLB_NULL_INDEX; index = next_index) {
  375. next_index = rx_hash_table[index].next;
  376. if (rx_hash_table[index].slave == slave) {
  377. struct slave *assigned_slave = rlb_next_rx_slave(bond);
  378. if (assigned_slave) {
  379. rx_hash_table[index].slave = assigned_slave;
  380. if (memcmp(rx_hash_table[index].mac_dst,
  381. mac_bcast, ETH_ALEN)) {
  382. bond_info->rx_hashtbl[index].ntt = 1;
  383. bond_info->rx_ntt = 1;
  384. /* A slave has been removed from the
  385. * table because it is either disabled
  386. * or being released. We must retry the
  387. * update to avoid clients from not
  388. * being updated & disconnecting when
  389. * there is stress
  390. */
  391. bond_info->rlb_update_retry_counter =
  392. RLB_UPDATE_RETRY;
  393. }
  394. } else { /* there is no active slave */
  395. rx_hash_table[index].slave = NULL;
  396. }
  397. }
  398. }
  399. _unlock_rx_hashtbl(bond);
  400. write_lock(&bond->curr_slave_lock);
  401. if (slave != bond->curr_active_slave) {
  402. rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
  403. }
  404. write_unlock(&bond->curr_slave_lock);
  405. }
  406. static void rlb_update_client(struct rlb_client_info *client_info)
  407. {
  408. int i;
  409. if (!client_info->slave) {
  410. return;
  411. }
  412. for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
  413. struct sk_buff *skb;
  414. skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
  415. client_info->ip_dst,
  416. client_info->slave->dev,
  417. client_info->ip_src,
  418. client_info->mac_dst,
  419. client_info->slave->dev->dev_addr,
  420. client_info->mac_dst);
  421. if (!skb) {
  422. printk(KERN_ERR DRV_NAME
  423. ": Error: failed to create an ARP packet\n");
  424. continue;
  425. }
  426. skb->dev = client_info->slave->dev;
  427. if (client_info->tag) {
  428. skb = vlan_put_tag(skb, client_info->vlan_id);
  429. if (!skb) {
  430. printk(KERN_ERR DRV_NAME
  431. ": Error: failed to insert VLAN tag\n");
  432. continue;
  433. }
  434. }
  435. arp_xmit(skb);
  436. }
  437. }
  438. /* sends ARP REPLIES that update the clients that need updating */
  439. static void rlb_update_rx_clients(struct bonding *bond)
  440. {
  441. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  442. struct rlb_client_info *client_info;
  443. u32 hash_index;
  444. _lock_rx_hashtbl(bond);
  445. hash_index = bond_info->rx_hashtbl_head;
  446. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  447. client_info = &(bond_info->rx_hashtbl[hash_index]);
  448. if (client_info->ntt) {
  449. rlb_update_client(client_info);
  450. if (bond_info->rlb_update_retry_counter == 0) {
  451. client_info->ntt = 0;
  452. }
  453. }
  454. }
  455. /* do not update the entries again untill this counter is zero so that
  456. * not to confuse the clients.
  457. */
  458. bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
  459. _unlock_rx_hashtbl(bond);
  460. }
  461. /* The slave was assigned a new mac address - update the clients */
  462. static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
  463. {
  464. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  465. struct rlb_client_info *client_info;
  466. int ntt = 0;
  467. u32 hash_index;
  468. _lock_rx_hashtbl(bond);
  469. hash_index = bond_info->rx_hashtbl_head;
  470. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  471. client_info = &(bond_info->rx_hashtbl[hash_index]);
  472. if ((client_info->slave == slave) &&
  473. memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  474. client_info->ntt = 1;
  475. ntt = 1;
  476. }
  477. }
  478. // update the team's flag only after the whole iteration
  479. if (ntt) {
  480. bond_info->rx_ntt = 1;
  481. //fasten the change
  482. bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
  483. }
  484. _unlock_rx_hashtbl(bond);
  485. }
  486. /* mark all clients using src_ip to be updated */
  487. static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
  488. {
  489. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  490. struct rlb_client_info *client_info;
  491. u32 hash_index;
  492. _lock_rx_hashtbl(bond);
  493. hash_index = bond_info->rx_hashtbl_head;
  494. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  495. client_info = &(bond_info->rx_hashtbl[hash_index]);
  496. if (!client_info->slave) {
  497. printk(KERN_ERR DRV_NAME
  498. ": Error: found a client with no channel in "
  499. "the client's hash table\n");
  500. continue;
  501. }
  502. /*update all clients using this src_ip, that are not assigned
  503. * to the team's address (curr_active_slave) and have a known
  504. * unicast mac address.
  505. */
  506. if ((client_info->ip_src == src_ip) &&
  507. memcmp(client_info->slave->dev->dev_addr,
  508. bond->dev->dev_addr, ETH_ALEN) &&
  509. memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  510. client_info->ntt = 1;
  511. bond_info->rx_ntt = 1;
  512. }
  513. }
  514. _unlock_rx_hashtbl(bond);
  515. }
  516. /* Caller must hold both bond and ptr locks for read */
  517. static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
  518. {
  519. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  520. struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
  521. struct slave *assigned_slave;
  522. struct rlb_client_info *client_info;
  523. u32 hash_index = 0;
  524. _lock_rx_hashtbl(bond);
  525. hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
  526. client_info = &(bond_info->rx_hashtbl[hash_index]);
  527. if (client_info->assigned) {
  528. if ((client_info->ip_src == arp->ip_src) &&
  529. (client_info->ip_dst == arp->ip_dst)) {
  530. /* the entry is already assigned to this client */
  531. if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
  532. /* update mac address from arp */
  533. memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
  534. }
  535. assigned_slave = client_info->slave;
  536. if (assigned_slave) {
  537. _unlock_rx_hashtbl(bond);
  538. return assigned_slave;
  539. }
  540. } else {
  541. /* the entry is already assigned to some other client,
  542. * move the old client to primary (curr_active_slave) so
  543. * that the new client can be assigned to this entry.
  544. */
  545. if (bond->curr_active_slave &&
  546. client_info->slave != bond->curr_active_slave) {
  547. client_info->slave = bond->curr_active_slave;
  548. rlb_update_client(client_info);
  549. }
  550. }
  551. }
  552. /* assign a new slave */
  553. assigned_slave = rlb_next_rx_slave(bond);
  554. if (assigned_slave) {
  555. client_info->ip_src = arp->ip_src;
  556. client_info->ip_dst = arp->ip_dst;
  557. /* arp->mac_dst is broadcast for arp reqeusts.
  558. * will be updated with clients actual unicast mac address
  559. * upon receiving an arp reply.
  560. */
  561. memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
  562. client_info->slave = assigned_slave;
  563. if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  564. client_info->ntt = 1;
  565. bond->alb_info.rx_ntt = 1;
  566. } else {
  567. client_info->ntt = 0;
  568. }
  569. if (!list_empty(&bond->vlan_list)) {
  570. unsigned short vlan_id;
  571. int res = vlan_get_tag(skb, &vlan_id);
  572. if (!res) {
  573. client_info->tag = 1;
  574. client_info->vlan_id = vlan_id;
  575. }
  576. }
  577. if (!client_info->assigned) {
  578. u32 prev_tbl_head = bond_info->rx_hashtbl_head;
  579. bond_info->rx_hashtbl_head = hash_index;
  580. client_info->next = prev_tbl_head;
  581. if (prev_tbl_head != RLB_NULL_INDEX) {
  582. bond_info->rx_hashtbl[prev_tbl_head].prev =
  583. hash_index;
  584. }
  585. client_info->assigned = 1;
  586. }
  587. }
  588. _unlock_rx_hashtbl(bond);
  589. return assigned_slave;
  590. }
  591. /* chooses (and returns) transmit channel for arp reply
  592. * does not choose channel for other arp types since they are
  593. * sent on the curr_active_slave
  594. */
  595. static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
  596. {
  597. struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
  598. struct slave *tx_slave = NULL;
  599. if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
  600. /* the arp must be sent on the selected
  601. * rx channel
  602. */
  603. tx_slave = rlb_choose_channel(skb, bond);
  604. if (tx_slave) {
  605. memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
  606. }
  607. dprintk("Server sent ARP Reply packet\n");
  608. } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
  609. /* Create an entry in the rx_hashtbl for this client as a
  610. * place holder.
  611. * When the arp reply is received the entry will be updated
  612. * with the correct unicast address of the client.
  613. */
  614. rlb_choose_channel(skb, bond);
  615. /* The ARP relpy packets must be delayed so that
  616. * they can cancel out the influence of the ARP request.
  617. */
  618. bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
  619. /* arp requests are broadcast and are sent on the primary
  620. * the arp request will collapse all clients on the subnet to
  621. * the primary slave. We must register these clients to be
  622. * updated with their assigned mac.
  623. */
  624. rlb_req_update_subnet_clients(bond, arp->ip_src);
  625. dprintk("Server sent ARP Request packet\n");
  626. }
  627. return tx_slave;
  628. }
  629. /* Caller must hold bond lock for read */
  630. static void rlb_rebalance(struct bonding *bond)
  631. {
  632. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  633. struct slave *assigned_slave;
  634. struct rlb_client_info *client_info;
  635. int ntt;
  636. u32 hash_index;
  637. _lock_rx_hashtbl(bond);
  638. ntt = 0;
  639. hash_index = bond_info->rx_hashtbl_head;
  640. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  641. client_info = &(bond_info->rx_hashtbl[hash_index]);
  642. assigned_slave = rlb_next_rx_slave(bond);
  643. if (assigned_slave && (client_info->slave != assigned_slave)) {
  644. client_info->slave = assigned_slave;
  645. client_info->ntt = 1;
  646. ntt = 1;
  647. }
  648. }
  649. /* update the team's flag only after the whole iteration */
  650. if (ntt) {
  651. bond_info->rx_ntt = 1;
  652. }
  653. _unlock_rx_hashtbl(bond);
  654. }
  655. /* Caller must hold rx_hashtbl lock */
  656. static void rlb_init_table_entry(struct rlb_client_info *entry)
  657. {
  658. memset(entry, 0, sizeof(struct rlb_client_info));
  659. entry->next = RLB_NULL_INDEX;
  660. entry->prev = RLB_NULL_INDEX;
  661. }
  662. static int rlb_initialize(struct bonding *bond)
  663. {
  664. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  665. struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
  666. int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
  667. int i;
  668. spin_lock_init(&(bond_info->rx_hashtbl_lock));
  669. _lock_rx_hashtbl(bond);
  670. bond_info->rx_hashtbl = kmalloc(size, GFP_KERNEL);
  671. if (!bond_info->rx_hashtbl) {
  672. printk(KERN_ERR DRV_NAME
  673. ": Error: %s: Failed to allocate RLB hash table\n",
  674. bond->dev->name);
  675. _unlock_rx_hashtbl(bond);
  676. return -1;
  677. }
  678. bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
  679. for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
  680. rlb_init_table_entry(bond_info->rx_hashtbl + i);
  681. }
  682. _unlock_rx_hashtbl(bond);
  683. /*initialize packet type*/
  684. pk_type->type = __constant_htons(ETH_P_ARP);
  685. pk_type->dev = bond->dev;
  686. pk_type->func = rlb_arp_recv;
  687. /* register to receive ARPs */
  688. dev_add_pack(pk_type);
  689. return 0;
  690. }
  691. static void rlb_deinitialize(struct bonding *bond)
  692. {
  693. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  694. dev_remove_pack(&(bond_info->rlb_pkt_type));
  695. _lock_rx_hashtbl(bond);
  696. kfree(bond_info->rx_hashtbl);
  697. bond_info->rx_hashtbl = NULL;
  698. bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
  699. _unlock_rx_hashtbl(bond);
  700. }
  701. static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  702. {
  703. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  704. u32 curr_index;
  705. _lock_rx_hashtbl(bond);
  706. curr_index = bond_info->rx_hashtbl_head;
  707. while (curr_index != RLB_NULL_INDEX) {
  708. struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
  709. u32 next_index = bond_info->rx_hashtbl[curr_index].next;
  710. u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
  711. if (curr->tag && (curr->vlan_id == vlan_id)) {
  712. if (curr_index == bond_info->rx_hashtbl_head) {
  713. bond_info->rx_hashtbl_head = next_index;
  714. }
  715. if (prev_index != RLB_NULL_INDEX) {
  716. bond_info->rx_hashtbl[prev_index].next = next_index;
  717. }
  718. if (next_index != RLB_NULL_INDEX) {
  719. bond_info->rx_hashtbl[next_index].prev = prev_index;
  720. }
  721. rlb_init_table_entry(curr);
  722. }
  723. curr_index = next_index;
  724. }
  725. _unlock_rx_hashtbl(bond);
  726. }
  727. /*********************** tlb/rlb shared functions *********************/
  728. static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
  729. {
  730. struct bonding *bond = bond_get_bond_by_slave(slave);
  731. struct learning_pkt pkt;
  732. int size = sizeof(struct learning_pkt);
  733. int i;
  734. memset(&pkt, 0, size);
  735. memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
  736. memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
  737. pkt.type = __constant_htons(ETH_P_LOOP);
  738. for (i = 0; i < MAX_LP_BURST; i++) {
  739. struct sk_buff *skb;
  740. char *data;
  741. skb = dev_alloc_skb(size);
  742. if (!skb) {
  743. return;
  744. }
  745. data = skb_put(skb, size);
  746. memcpy(data, &pkt, size);
  747. skb->mac.raw = data;
  748. skb->nh.raw = data + ETH_HLEN;
  749. skb->protocol = pkt.type;
  750. skb->priority = TC_PRIO_CONTROL;
  751. skb->dev = slave->dev;
  752. if (!list_empty(&bond->vlan_list)) {
  753. struct vlan_entry *vlan;
  754. vlan = bond_next_vlan(bond,
  755. bond->alb_info.current_alb_vlan);
  756. bond->alb_info.current_alb_vlan = vlan;
  757. if (!vlan) {
  758. kfree_skb(skb);
  759. continue;
  760. }
  761. skb = vlan_put_tag(skb, vlan->vlan_id);
  762. if (!skb) {
  763. printk(KERN_ERR DRV_NAME
  764. ": Error: failed to insert VLAN tag\n");
  765. continue;
  766. }
  767. }
  768. dev_queue_xmit(skb);
  769. }
  770. }
  771. /* hw is a boolean parameter that determines whether we should try and
  772. * set the hw address of the device as well as the hw address of the
  773. * net_device
  774. */
  775. static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
  776. {
  777. struct net_device *dev = slave->dev;
  778. struct sockaddr s_addr;
  779. if (!hw) {
  780. memcpy(dev->dev_addr, addr, dev->addr_len);
  781. return 0;
  782. }
  783. /* for rlb each slave must have a unique hw mac addresses so that */
  784. /* each slave will receive packets destined to a different mac */
  785. memcpy(s_addr.sa_data, addr, dev->addr_len);
  786. s_addr.sa_family = dev->type;
  787. if (dev_set_mac_address(dev, &s_addr)) {
  788. printk(KERN_ERR DRV_NAME
  789. ": Error: dev_set_mac_address of dev %s failed! ALB "
  790. "mode requires that the base driver support setting "
  791. "the hw address also when the network device's "
  792. "interface is open\n",
  793. dev->name);
  794. return -EOPNOTSUPP;
  795. }
  796. return 0;
  797. }
  798. /* Caller must hold bond lock for write or curr_slave_lock for write*/
  799. static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
  800. {
  801. struct slave *disabled_slave = NULL;
  802. u8 tmp_mac_addr[ETH_ALEN];
  803. int slaves_state_differ;
  804. slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
  805. memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
  806. alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
  807. alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
  808. /* fasten the change in the switch */
  809. if (SLAVE_IS_OK(slave1)) {
  810. alb_send_learning_packets(slave1, slave1->dev->dev_addr);
  811. if (bond->alb_info.rlb_enabled) {
  812. /* inform the clients that the mac address
  813. * has changed
  814. */
  815. rlb_req_update_slave_clients(bond, slave1);
  816. }
  817. } else {
  818. disabled_slave = slave1;
  819. }
  820. if (SLAVE_IS_OK(slave2)) {
  821. alb_send_learning_packets(slave2, slave2->dev->dev_addr);
  822. if (bond->alb_info.rlb_enabled) {
  823. /* inform the clients that the mac address
  824. * has changed
  825. */
  826. rlb_req_update_slave_clients(bond, slave2);
  827. }
  828. } else {
  829. disabled_slave = slave2;
  830. }
  831. if (bond->alb_info.rlb_enabled && slaves_state_differ) {
  832. /* A disabled slave was assigned an active mac addr */
  833. rlb_teach_disabled_mac_on_primary(bond,
  834. disabled_slave->dev->dev_addr);
  835. }
  836. }
  837. /**
  838. * alb_change_hw_addr_on_detach
  839. * @bond: bonding we're working on
  840. * @slave: the slave that was just detached
  841. *
  842. * We assume that @slave was already detached from the slave list.
  843. *
  844. * If @slave's permanent hw address is different both from its current
  845. * address and from @bond's address, then somewhere in the bond there's
  846. * a slave that has @slave's permanet address as its current address.
  847. * We'll make sure that that slave no longer uses @slave's permanent address.
  848. *
  849. * Caller must hold bond lock
  850. */
  851. static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
  852. {
  853. int perm_curr_diff;
  854. int perm_bond_diff;
  855. perm_curr_diff = memcmp(slave->perm_hwaddr,
  856. slave->dev->dev_addr,
  857. ETH_ALEN);
  858. perm_bond_diff = memcmp(slave->perm_hwaddr,
  859. bond->dev->dev_addr,
  860. ETH_ALEN);
  861. if (perm_curr_diff && perm_bond_diff) {
  862. struct slave *tmp_slave;
  863. int i, found = 0;
  864. bond_for_each_slave(bond, tmp_slave, i) {
  865. if (!memcmp(slave->perm_hwaddr,
  866. tmp_slave->dev->dev_addr,
  867. ETH_ALEN)) {
  868. found = 1;
  869. break;
  870. }
  871. }
  872. if (found) {
  873. alb_swap_mac_addr(bond, slave, tmp_slave);
  874. }
  875. }
  876. }
  877. /**
  878. * alb_handle_addr_collision_on_attach
  879. * @bond: bonding we're working on
  880. * @slave: the slave that was just attached
  881. *
  882. * checks uniqueness of slave's mac address and handles the case the
  883. * new slave uses the bonds mac address.
  884. *
  885. * If the permanent hw address of @slave is @bond's hw address, we need to
  886. * find a different hw address to give @slave, that isn't in use by any other
  887. * slave in the bond. This address must be, of course, one of the premanent
  888. * addresses of the other slaves.
  889. *
  890. * We go over the slave list, and for each slave there we compare its
  891. * permanent hw address with the current address of all the other slaves.
  892. * If no match was found, then we've found a slave with a permanent address
  893. * that isn't used by any other slave in the bond, so we can assign it to
  894. * @slave.
  895. *
  896. * assumption: this function is called before @slave is attached to the
  897. * bond slave list.
  898. *
  899. * caller must hold the bond lock for write since the mac addresses are compared
  900. * and may be swapped.
  901. */
  902. static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
  903. {
  904. struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
  905. struct slave *has_bond_addr = bond->curr_active_slave;
  906. int i, j, found = 0;
  907. if (bond->slave_cnt == 0) {
  908. /* this is the first slave */
  909. return 0;
  910. }
  911. /* if slave's mac address differs from bond's mac address
  912. * check uniqueness of slave's mac address against the other
  913. * slaves in the bond.
  914. */
  915. if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
  916. bond_for_each_slave(bond, tmp_slave1, i) {
  917. if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
  918. ETH_ALEN)) {
  919. found = 1;
  920. break;
  921. }
  922. }
  923. if (found) {
  924. /* a slave was found that is using the mac address
  925. * of the new slave
  926. */
  927. printk(KERN_ERR DRV_NAME
  928. ": Error: the hw address of slave %s is not "
  929. "unique - cannot enslave it!",
  930. slave->dev->name);
  931. return -EINVAL;
  932. }
  933. return 0;
  934. }
  935. /* The slave's address is equal to the address of the bond.
  936. * Search for a spare address in the bond for this slave.
  937. */
  938. free_mac_slave = NULL;
  939. bond_for_each_slave(bond, tmp_slave1, i) {
  940. found = 0;
  941. bond_for_each_slave(bond, tmp_slave2, j) {
  942. if (!memcmp(tmp_slave1->perm_hwaddr,
  943. tmp_slave2->dev->dev_addr,
  944. ETH_ALEN)) {
  945. found = 1;
  946. break;
  947. }
  948. }
  949. if (!found) {
  950. /* no slave has tmp_slave1's perm addr
  951. * as its curr addr
  952. */
  953. free_mac_slave = tmp_slave1;
  954. break;
  955. }
  956. if (!has_bond_addr) {
  957. if (!memcmp(tmp_slave1->dev->dev_addr,
  958. bond->dev->dev_addr,
  959. ETH_ALEN)) {
  960. has_bond_addr = tmp_slave1;
  961. }
  962. }
  963. }
  964. if (free_mac_slave) {
  965. alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
  966. bond->alb_info.rlb_enabled);
  967. printk(KERN_WARNING DRV_NAME
  968. ": Warning: the hw address of slave %s is in use by "
  969. "the bond; giving it the hw address of %s\n",
  970. slave->dev->name, free_mac_slave->dev->name);
  971. } else if (has_bond_addr) {
  972. printk(KERN_ERR DRV_NAME
  973. ": Error: the hw address of slave %s is in use by the "
  974. "bond; couldn't find a slave with a free hw address to "
  975. "give it (this should not have happened)\n",
  976. slave->dev->name);
  977. return -EFAULT;
  978. }
  979. return 0;
  980. }
  981. /**
  982. * alb_set_mac_address
  983. * @bond:
  984. * @addr:
  985. *
  986. * In TLB mode all slaves are configured to the bond's hw address, but set
  987. * their dev_addr field to different addresses (based on their permanent hw
  988. * addresses).
  989. *
  990. * For each slave, this function sets the interface to the new address and then
  991. * changes its dev_addr field to its previous value.
  992. *
  993. * Unwinding assumes bond's mac address has not yet changed.
  994. */
  995. static int alb_set_mac_address(struct bonding *bond, void *addr)
  996. {
  997. struct sockaddr sa;
  998. struct slave *slave, *stop_at;
  999. char tmp_addr[ETH_ALEN];
  1000. int res;
  1001. int i;
  1002. if (bond->alb_info.rlb_enabled) {
  1003. return 0;
  1004. }
  1005. bond_for_each_slave(bond, slave, i) {
  1006. if (slave->dev->set_mac_address == NULL) {
  1007. res = -EOPNOTSUPP;
  1008. goto unwind;
  1009. }
  1010. /* save net_device's current hw address */
  1011. memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
  1012. res = dev_set_mac_address(slave->dev, addr);
  1013. /* restore net_device's hw address */
  1014. memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
  1015. if (res) {
  1016. goto unwind;
  1017. }
  1018. }
  1019. return 0;
  1020. unwind:
  1021. memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
  1022. sa.sa_family = bond->dev->type;
  1023. /* unwind from head to the slave that failed */
  1024. stop_at = slave;
  1025. bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
  1026. memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
  1027. dev_set_mac_address(slave->dev, &sa);
  1028. memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
  1029. }
  1030. return res;
  1031. }
  1032. /************************ exported alb funcions ************************/
  1033. int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
  1034. {
  1035. int res;
  1036. res = tlb_initialize(bond);
  1037. if (res) {
  1038. return res;
  1039. }
  1040. if (rlb_enabled) {
  1041. bond->alb_info.rlb_enabled = 1;
  1042. /* initialize rlb */
  1043. res = rlb_initialize(bond);
  1044. if (res) {
  1045. tlb_deinitialize(bond);
  1046. return res;
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. void bond_alb_deinitialize(struct bonding *bond)
  1052. {
  1053. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1054. tlb_deinitialize(bond);
  1055. if (bond_info->rlb_enabled) {
  1056. rlb_deinitialize(bond);
  1057. }
  1058. }
  1059. int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
  1060. {
  1061. struct bonding *bond = bond_dev->priv;
  1062. struct ethhdr *eth_data;
  1063. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1064. struct slave *tx_slave = NULL;
  1065. static u32 ip_bcast = 0xffffffff;
  1066. int hash_size = 0;
  1067. int do_tx_balance = 1;
  1068. u32 hash_index = 0;
  1069. u8 *hash_start = NULL;
  1070. int res = 1;
  1071. skb->mac.raw = (unsigned char *)skb->data;
  1072. eth_data = eth_hdr(skb);
  1073. /* make sure that the curr_active_slave and the slaves list do
  1074. * not change during tx
  1075. */
  1076. read_lock(&bond->lock);
  1077. read_lock(&bond->curr_slave_lock);
  1078. if (!BOND_IS_OK(bond)) {
  1079. goto out;
  1080. }
  1081. switch (ntohs(skb->protocol)) {
  1082. case ETH_P_IP:
  1083. if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
  1084. (skb->nh.iph->daddr == ip_bcast) ||
  1085. (skb->nh.iph->protocol == IPPROTO_IGMP)) {
  1086. do_tx_balance = 0;
  1087. break;
  1088. }
  1089. hash_start = (char*)&(skb->nh.iph->daddr);
  1090. hash_size = sizeof(skb->nh.iph->daddr);
  1091. break;
  1092. case ETH_P_IPV6:
  1093. if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
  1094. do_tx_balance = 0;
  1095. break;
  1096. }
  1097. hash_start = (char*)&(skb->nh.ipv6h->daddr);
  1098. hash_size = sizeof(skb->nh.ipv6h->daddr);
  1099. break;
  1100. case ETH_P_IPX:
  1101. if (ipx_hdr(skb)->ipx_checksum !=
  1102. __constant_htons(IPX_NO_CHECKSUM)) {
  1103. /* something is wrong with this packet */
  1104. do_tx_balance = 0;
  1105. break;
  1106. }
  1107. if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
  1108. /* The only protocol worth balancing in
  1109. * this family since it has an "ARP" like
  1110. * mechanism
  1111. */
  1112. do_tx_balance = 0;
  1113. break;
  1114. }
  1115. hash_start = (char*)eth_data->h_dest;
  1116. hash_size = ETH_ALEN;
  1117. break;
  1118. case ETH_P_ARP:
  1119. do_tx_balance = 0;
  1120. if (bond_info->rlb_enabled) {
  1121. tx_slave = rlb_arp_xmit(skb, bond);
  1122. }
  1123. break;
  1124. default:
  1125. do_tx_balance = 0;
  1126. break;
  1127. }
  1128. if (do_tx_balance) {
  1129. hash_index = _simple_hash(hash_start, hash_size);
  1130. tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
  1131. }
  1132. if (!tx_slave) {
  1133. /* unbalanced or unassigned, send through primary */
  1134. tx_slave = bond->curr_active_slave;
  1135. bond_info->unbalanced_load += skb->len;
  1136. }
  1137. if (tx_slave && SLAVE_IS_OK(tx_slave)) {
  1138. if (tx_slave != bond->curr_active_slave) {
  1139. memcpy(eth_data->h_source,
  1140. tx_slave->dev->dev_addr,
  1141. ETH_ALEN);
  1142. }
  1143. res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
  1144. } else {
  1145. if (tx_slave) {
  1146. tlb_clear_slave(bond, tx_slave, 0);
  1147. }
  1148. }
  1149. out:
  1150. if (res) {
  1151. /* no suitable interface, frame not sent */
  1152. dev_kfree_skb(skb);
  1153. }
  1154. read_unlock(&bond->curr_slave_lock);
  1155. read_unlock(&bond->lock);
  1156. return 0;
  1157. }
  1158. void bond_alb_monitor(struct bonding *bond)
  1159. {
  1160. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1161. struct slave *slave;
  1162. int i;
  1163. read_lock(&bond->lock);
  1164. if (bond->kill_timers) {
  1165. goto out;
  1166. }
  1167. if (bond->slave_cnt == 0) {
  1168. bond_info->tx_rebalance_counter = 0;
  1169. bond_info->lp_counter = 0;
  1170. goto re_arm;
  1171. }
  1172. bond_info->tx_rebalance_counter++;
  1173. bond_info->lp_counter++;
  1174. /* send learning packets */
  1175. if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
  1176. /* change of curr_active_slave involves swapping of mac addresses.
  1177. * in order to avoid this swapping from happening while
  1178. * sending the learning packets, the curr_slave_lock must be held for
  1179. * read.
  1180. */
  1181. read_lock(&bond->curr_slave_lock);
  1182. bond_for_each_slave(bond, slave, i) {
  1183. alb_send_learning_packets(slave,slave->dev->dev_addr);
  1184. }
  1185. read_unlock(&bond->curr_slave_lock);
  1186. bond_info->lp_counter = 0;
  1187. }
  1188. /* rebalance tx traffic */
  1189. if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
  1190. read_lock(&bond->curr_slave_lock);
  1191. bond_for_each_slave(bond, slave, i) {
  1192. tlb_clear_slave(bond, slave, 1);
  1193. if (slave == bond->curr_active_slave) {
  1194. SLAVE_TLB_INFO(slave).load =
  1195. bond_info->unbalanced_load /
  1196. BOND_TLB_REBALANCE_INTERVAL;
  1197. bond_info->unbalanced_load = 0;
  1198. }
  1199. }
  1200. read_unlock(&bond->curr_slave_lock);
  1201. bond_info->tx_rebalance_counter = 0;
  1202. }
  1203. /* handle rlb stuff */
  1204. if (bond_info->rlb_enabled) {
  1205. /* the following code changes the promiscuity of the
  1206. * the curr_active_slave. It needs to be locked with a
  1207. * write lock to protect from other code that also
  1208. * sets the promiscuity.
  1209. */
  1210. write_lock(&bond->curr_slave_lock);
  1211. if (bond_info->primary_is_promisc &&
  1212. (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
  1213. bond_info->rlb_promisc_timeout_counter = 0;
  1214. /* If the primary was set to promiscuous mode
  1215. * because a slave was disabled then
  1216. * it can now leave promiscuous mode.
  1217. */
  1218. dev_set_promiscuity(bond->curr_active_slave->dev, -1);
  1219. bond_info->primary_is_promisc = 0;
  1220. }
  1221. write_unlock(&bond->curr_slave_lock);
  1222. if (bond_info->rlb_rebalance) {
  1223. bond_info->rlb_rebalance = 0;
  1224. rlb_rebalance(bond);
  1225. }
  1226. /* check if clients need updating */
  1227. if (bond_info->rx_ntt) {
  1228. if (bond_info->rlb_update_delay_counter) {
  1229. --bond_info->rlb_update_delay_counter;
  1230. } else {
  1231. rlb_update_rx_clients(bond);
  1232. if (bond_info->rlb_update_retry_counter) {
  1233. --bond_info->rlb_update_retry_counter;
  1234. } else {
  1235. bond_info->rx_ntt = 0;
  1236. }
  1237. }
  1238. }
  1239. }
  1240. re_arm:
  1241. mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
  1242. out:
  1243. read_unlock(&bond->lock);
  1244. }
  1245. /* assumption: called before the slave is attached to the bond
  1246. * and not locked by the bond lock
  1247. */
  1248. int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
  1249. {
  1250. int res;
  1251. res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
  1252. bond->alb_info.rlb_enabled);
  1253. if (res) {
  1254. return res;
  1255. }
  1256. /* caller must hold the bond lock for write since the mac addresses
  1257. * are compared and may be swapped.
  1258. */
  1259. write_lock_bh(&bond->lock);
  1260. res = alb_handle_addr_collision_on_attach(bond, slave);
  1261. write_unlock_bh(&bond->lock);
  1262. if (res) {
  1263. return res;
  1264. }
  1265. tlb_init_slave(slave);
  1266. /* order a rebalance ASAP */
  1267. bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1268. if (bond->alb_info.rlb_enabled) {
  1269. bond->alb_info.rlb_rebalance = 1;
  1270. }
  1271. return 0;
  1272. }
  1273. /* Caller must hold bond lock for write */
  1274. void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
  1275. {
  1276. if (bond->slave_cnt > 1) {
  1277. alb_change_hw_addr_on_detach(bond, slave);
  1278. }
  1279. tlb_clear_slave(bond, slave, 0);
  1280. if (bond->alb_info.rlb_enabled) {
  1281. bond->alb_info.next_rx_slave = NULL;
  1282. rlb_clear_slave(bond, slave);
  1283. }
  1284. }
  1285. /* Caller must hold bond lock for read */
  1286. void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
  1287. {
  1288. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1289. if (link == BOND_LINK_DOWN) {
  1290. tlb_clear_slave(bond, slave, 0);
  1291. if (bond->alb_info.rlb_enabled) {
  1292. rlb_clear_slave(bond, slave);
  1293. }
  1294. } else if (link == BOND_LINK_UP) {
  1295. /* order a rebalance ASAP */
  1296. bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1297. if (bond->alb_info.rlb_enabled) {
  1298. bond->alb_info.rlb_rebalance = 1;
  1299. /* If the updelay module parameter is smaller than the
  1300. * forwarding delay of the switch the rebalance will
  1301. * not work because the rebalance arp replies will
  1302. * not be forwarded to the clients..
  1303. */
  1304. }
  1305. }
  1306. }
  1307. /**
  1308. * bond_alb_handle_active_change - assign new curr_active_slave
  1309. * @bond: our bonding struct
  1310. * @new_slave: new slave to assign
  1311. *
  1312. * Set the bond->curr_active_slave to @new_slave and handle
  1313. * mac address swapping and promiscuity changes as needed.
  1314. *
  1315. * Caller must hold bond curr_slave_lock for write (or bond lock for write)
  1316. */
  1317. void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
  1318. {
  1319. struct slave *swap_slave;
  1320. int i;
  1321. if (bond->curr_active_slave == new_slave) {
  1322. return;
  1323. }
  1324. if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
  1325. dev_set_promiscuity(bond->curr_active_slave->dev, -1);
  1326. bond->alb_info.primary_is_promisc = 0;
  1327. bond->alb_info.rlb_promisc_timeout_counter = 0;
  1328. }
  1329. swap_slave = bond->curr_active_slave;
  1330. bond->curr_active_slave = new_slave;
  1331. if (!new_slave || (bond->slave_cnt == 0)) {
  1332. return;
  1333. }
  1334. /* set the new curr_active_slave to the bonds mac address
  1335. * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
  1336. */
  1337. if (!swap_slave) {
  1338. struct slave *tmp_slave;
  1339. /* find slave that is holding the bond's mac address */
  1340. bond_for_each_slave(bond, tmp_slave, i) {
  1341. if (!memcmp(tmp_slave->dev->dev_addr,
  1342. bond->dev->dev_addr, ETH_ALEN)) {
  1343. swap_slave = tmp_slave;
  1344. break;
  1345. }
  1346. }
  1347. }
  1348. /* curr_active_slave must be set before calling alb_swap_mac_addr */
  1349. if (swap_slave) {
  1350. /* swap mac address */
  1351. alb_swap_mac_addr(bond, swap_slave, new_slave);
  1352. } else {
  1353. /* set the new_slave to the bond mac address */
  1354. alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
  1355. bond->alb_info.rlb_enabled);
  1356. /* fasten bond mac on new current slave */
  1357. alb_send_learning_packets(new_slave, bond->dev->dev_addr);
  1358. }
  1359. }
  1360. int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
  1361. {
  1362. struct bonding *bond = bond_dev->priv;
  1363. struct sockaddr *sa = addr;
  1364. struct slave *slave, *swap_slave;
  1365. int res;
  1366. int i;
  1367. if (!is_valid_ether_addr(sa->sa_data)) {
  1368. return -EADDRNOTAVAIL;
  1369. }
  1370. res = alb_set_mac_address(bond, addr);
  1371. if (res) {
  1372. return res;
  1373. }
  1374. memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
  1375. /* If there is no curr_active_slave there is nothing else to do.
  1376. * Otherwise we'll need to pass the new address to it and handle
  1377. * duplications.
  1378. */
  1379. if (!bond->curr_active_slave) {
  1380. return 0;
  1381. }
  1382. swap_slave = NULL;
  1383. bond_for_each_slave(bond, slave, i) {
  1384. if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
  1385. swap_slave = slave;
  1386. break;
  1387. }
  1388. }
  1389. if (swap_slave) {
  1390. alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
  1391. } else {
  1392. alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
  1393. bond->alb_info.rlb_enabled);
  1394. alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
  1395. if (bond->alb_info.rlb_enabled) {
  1396. /* inform clients mac address has changed */
  1397. rlb_req_update_slave_clients(bond, bond->curr_active_slave);
  1398. }
  1399. }
  1400. return 0;
  1401. }
  1402. void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  1403. {
  1404. if (bond->alb_info.current_alb_vlan &&
  1405. (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
  1406. bond->alb_info.current_alb_vlan = NULL;
  1407. }
  1408. if (bond->alb_info.rlb_enabled) {
  1409. rlb_clear_vlan(bond, vlan_id);
  1410. }
  1411. }