bond_alb.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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, struct net_device *orig_dev)
  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. if (!arp) {
  300. dprintk("Packet has no ARP data\n");
  301. goto out;
  302. }
  303. if (skb->len < sizeof(struct arp_pkt)) {
  304. dprintk("Packet is too small to be an ARP\n");
  305. goto out;
  306. }
  307. if (arp->op_code == htons(ARPOP_REPLY)) {
  308. /* update rx hash table for this ARP */
  309. rlb_update_entry_from_arp(bond, arp);
  310. dprintk("Server received an ARP Reply from client\n");
  311. }
  312. res = NET_RX_SUCCESS;
  313. out:
  314. dev_kfree_skb(skb);
  315. return res;
  316. }
  317. /* Caller must hold bond lock for read */
  318. static struct slave *rlb_next_rx_slave(struct bonding *bond)
  319. {
  320. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  321. struct slave *rx_slave, *slave, *start_at;
  322. int i = 0;
  323. if (bond_info->next_rx_slave) {
  324. start_at = bond_info->next_rx_slave;
  325. } else {
  326. start_at = bond->first_slave;
  327. }
  328. rx_slave = NULL;
  329. bond_for_each_slave_from(bond, slave, i, start_at) {
  330. if (SLAVE_IS_OK(slave)) {
  331. if (!rx_slave) {
  332. rx_slave = slave;
  333. } else if (slave->speed > rx_slave->speed) {
  334. rx_slave = slave;
  335. }
  336. }
  337. }
  338. if (rx_slave) {
  339. bond_info->next_rx_slave = rx_slave->next;
  340. }
  341. return rx_slave;
  342. }
  343. /* teach the switch the mac of a disabled slave
  344. * on the primary for fault tolerance
  345. *
  346. * Caller must hold bond->curr_slave_lock for write or bond lock for write
  347. */
  348. static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
  349. {
  350. if (!bond->curr_active_slave) {
  351. return;
  352. }
  353. if (!bond->alb_info.primary_is_promisc) {
  354. bond->alb_info.primary_is_promisc = 1;
  355. dev_set_promiscuity(bond->curr_active_slave->dev, 1);
  356. }
  357. bond->alb_info.rlb_promisc_timeout_counter = 0;
  358. alb_send_learning_packets(bond->curr_active_slave, addr);
  359. }
  360. /* slave being removed should not be active at this point
  361. *
  362. * Caller must hold bond lock for read
  363. */
  364. static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
  365. {
  366. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  367. struct rlb_client_info *rx_hash_table;
  368. u32 index, next_index;
  369. /* clear slave from rx_hashtbl */
  370. _lock_rx_hashtbl(bond);
  371. rx_hash_table = bond_info->rx_hashtbl;
  372. index = bond_info->rx_hashtbl_head;
  373. for (; index != RLB_NULL_INDEX; index = next_index) {
  374. next_index = rx_hash_table[index].next;
  375. if (rx_hash_table[index].slave == slave) {
  376. struct slave *assigned_slave = rlb_next_rx_slave(bond);
  377. if (assigned_slave) {
  378. rx_hash_table[index].slave = assigned_slave;
  379. if (memcmp(rx_hash_table[index].mac_dst,
  380. mac_bcast, ETH_ALEN)) {
  381. bond_info->rx_hashtbl[index].ntt = 1;
  382. bond_info->rx_ntt = 1;
  383. /* A slave has been removed from the
  384. * table because it is either disabled
  385. * or being released. We must retry the
  386. * update to avoid clients from not
  387. * being updated & disconnecting when
  388. * there is stress
  389. */
  390. bond_info->rlb_update_retry_counter =
  391. RLB_UPDATE_RETRY;
  392. }
  393. } else { /* there is no active slave */
  394. rx_hash_table[index].slave = NULL;
  395. }
  396. }
  397. }
  398. _unlock_rx_hashtbl(bond);
  399. write_lock(&bond->curr_slave_lock);
  400. if (slave != bond->curr_active_slave) {
  401. rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
  402. }
  403. write_unlock(&bond->curr_slave_lock);
  404. }
  405. static void rlb_update_client(struct rlb_client_info *client_info)
  406. {
  407. int i;
  408. if (!client_info->slave) {
  409. return;
  410. }
  411. for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
  412. struct sk_buff *skb;
  413. skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
  414. client_info->ip_dst,
  415. client_info->slave->dev,
  416. client_info->ip_src,
  417. client_info->mac_dst,
  418. client_info->slave->dev->dev_addr,
  419. client_info->mac_dst);
  420. if (!skb) {
  421. printk(KERN_ERR DRV_NAME
  422. ": Error: failed to create an ARP packet\n");
  423. continue;
  424. }
  425. skb->dev = client_info->slave->dev;
  426. if (client_info->tag) {
  427. skb = vlan_put_tag(skb, client_info->vlan_id);
  428. if (!skb) {
  429. printk(KERN_ERR DRV_NAME
  430. ": Error: failed to insert VLAN tag\n");
  431. continue;
  432. }
  433. }
  434. arp_xmit(skb);
  435. }
  436. }
  437. /* sends ARP REPLIES that update the clients that need updating */
  438. static void rlb_update_rx_clients(struct bonding *bond)
  439. {
  440. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  441. struct rlb_client_info *client_info;
  442. u32 hash_index;
  443. _lock_rx_hashtbl(bond);
  444. hash_index = bond_info->rx_hashtbl_head;
  445. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  446. client_info = &(bond_info->rx_hashtbl[hash_index]);
  447. if (client_info->ntt) {
  448. rlb_update_client(client_info);
  449. if (bond_info->rlb_update_retry_counter == 0) {
  450. client_info->ntt = 0;
  451. }
  452. }
  453. }
  454. /* do not update the entries again untill this counter is zero so that
  455. * not to confuse the clients.
  456. */
  457. bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
  458. _unlock_rx_hashtbl(bond);
  459. }
  460. /* The slave was assigned a new mac address - update the clients */
  461. static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
  462. {
  463. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  464. struct rlb_client_info *client_info;
  465. int ntt = 0;
  466. u32 hash_index;
  467. _lock_rx_hashtbl(bond);
  468. hash_index = bond_info->rx_hashtbl_head;
  469. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  470. client_info = &(bond_info->rx_hashtbl[hash_index]);
  471. if ((client_info->slave == slave) &&
  472. memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  473. client_info->ntt = 1;
  474. ntt = 1;
  475. }
  476. }
  477. // update the team's flag only after the whole iteration
  478. if (ntt) {
  479. bond_info->rx_ntt = 1;
  480. //fasten the change
  481. bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
  482. }
  483. _unlock_rx_hashtbl(bond);
  484. }
  485. /* mark all clients using src_ip to be updated */
  486. static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
  487. {
  488. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  489. struct rlb_client_info *client_info;
  490. u32 hash_index;
  491. _lock_rx_hashtbl(bond);
  492. hash_index = bond_info->rx_hashtbl_head;
  493. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  494. client_info = &(bond_info->rx_hashtbl[hash_index]);
  495. if (!client_info->slave) {
  496. printk(KERN_ERR DRV_NAME
  497. ": Error: found a client with no channel in "
  498. "the client's hash table\n");
  499. continue;
  500. }
  501. /*update all clients using this src_ip, that are not assigned
  502. * to the team's address (curr_active_slave) and have a known
  503. * unicast mac address.
  504. */
  505. if ((client_info->ip_src == src_ip) &&
  506. memcmp(client_info->slave->dev->dev_addr,
  507. bond->dev->dev_addr, ETH_ALEN) &&
  508. memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  509. client_info->ntt = 1;
  510. bond_info->rx_ntt = 1;
  511. }
  512. }
  513. _unlock_rx_hashtbl(bond);
  514. }
  515. /* Caller must hold both bond and ptr locks for read */
  516. static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
  517. {
  518. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  519. struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
  520. struct slave *assigned_slave;
  521. struct rlb_client_info *client_info;
  522. u32 hash_index = 0;
  523. _lock_rx_hashtbl(bond);
  524. hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
  525. client_info = &(bond_info->rx_hashtbl[hash_index]);
  526. if (client_info->assigned) {
  527. if ((client_info->ip_src == arp->ip_src) &&
  528. (client_info->ip_dst == arp->ip_dst)) {
  529. /* the entry is already assigned to this client */
  530. if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
  531. /* update mac address from arp */
  532. memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
  533. }
  534. assigned_slave = client_info->slave;
  535. if (assigned_slave) {
  536. _unlock_rx_hashtbl(bond);
  537. return assigned_slave;
  538. }
  539. } else {
  540. /* the entry is already assigned to some other client,
  541. * move the old client to primary (curr_active_slave) so
  542. * that the new client can be assigned to this entry.
  543. */
  544. if (bond->curr_active_slave &&
  545. client_info->slave != bond->curr_active_slave) {
  546. client_info->slave = bond->curr_active_slave;
  547. rlb_update_client(client_info);
  548. }
  549. }
  550. }
  551. /* assign a new slave */
  552. assigned_slave = rlb_next_rx_slave(bond);
  553. if (assigned_slave) {
  554. client_info->ip_src = arp->ip_src;
  555. client_info->ip_dst = arp->ip_dst;
  556. /* arp->mac_dst is broadcast for arp reqeusts.
  557. * will be updated with clients actual unicast mac address
  558. * upon receiving an arp reply.
  559. */
  560. memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
  561. client_info->slave = assigned_slave;
  562. if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  563. client_info->ntt = 1;
  564. bond->alb_info.rx_ntt = 1;
  565. } else {
  566. client_info->ntt = 0;
  567. }
  568. if (!list_empty(&bond->vlan_list)) {
  569. unsigned short vlan_id;
  570. int res = vlan_get_tag(skb, &vlan_id);
  571. if (!res) {
  572. client_info->tag = 1;
  573. client_info->vlan_id = vlan_id;
  574. }
  575. }
  576. if (!client_info->assigned) {
  577. u32 prev_tbl_head = bond_info->rx_hashtbl_head;
  578. bond_info->rx_hashtbl_head = hash_index;
  579. client_info->next = prev_tbl_head;
  580. if (prev_tbl_head != RLB_NULL_INDEX) {
  581. bond_info->rx_hashtbl[prev_tbl_head].prev =
  582. hash_index;
  583. }
  584. client_info->assigned = 1;
  585. }
  586. }
  587. _unlock_rx_hashtbl(bond);
  588. return assigned_slave;
  589. }
  590. /* chooses (and returns) transmit channel for arp reply
  591. * does not choose channel for other arp types since they are
  592. * sent on the curr_active_slave
  593. */
  594. static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
  595. {
  596. struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
  597. struct slave *tx_slave = NULL;
  598. if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
  599. /* the arp must be sent on the selected
  600. * rx channel
  601. */
  602. tx_slave = rlb_choose_channel(skb, bond);
  603. if (tx_slave) {
  604. memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
  605. }
  606. dprintk("Server sent ARP Reply packet\n");
  607. } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
  608. /* Create an entry in the rx_hashtbl for this client as a
  609. * place holder.
  610. * When the arp reply is received the entry will be updated
  611. * with the correct unicast address of the client.
  612. */
  613. rlb_choose_channel(skb, bond);
  614. /* The ARP relpy packets must be delayed so that
  615. * they can cancel out the influence of the ARP request.
  616. */
  617. bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
  618. /* arp requests are broadcast and are sent on the primary
  619. * the arp request will collapse all clients on the subnet to
  620. * the primary slave. We must register these clients to be
  621. * updated with their assigned mac.
  622. */
  623. rlb_req_update_subnet_clients(bond, arp->ip_src);
  624. dprintk("Server sent ARP Request packet\n");
  625. }
  626. return tx_slave;
  627. }
  628. /* Caller must hold bond lock for read */
  629. static void rlb_rebalance(struct bonding *bond)
  630. {
  631. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  632. struct slave *assigned_slave;
  633. struct rlb_client_info *client_info;
  634. int ntt;
  635. u32 hash_index;
  636. _lock_rx_hashtbl(bond);
  637. ntt = 0;
  638. hash_index = bond_info->rx_hashtbl_head;
  639. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  640. client_info = &(bond_info->rx_hashtbl[hash_index]);
  641. assigned_slave = rlb_next_rx_slave(bond);
  642. if (assigned_slave && (client_info->slave != assigned_slave)) {
  643. client_info->slave = assigned_slave;
  644. client_info->ntt = 1;
  645. ntt = 1;
  646. }
  647. }
  648. /* update the team's flag only after the whole iteration */
  649. if (ntt) {
  650. bond_info->rx_ntt = 1;
  651. }
  652. _unlock_rx_hashtbl(bond);
  653. }
  654. /* Caller must hold rx_hashtbl lock */
  655. static void rlb_init_table_entry(struct rlb_client_info *entry)
  656. {
  657. memset(entry, 0, sizeof(struct rlb_client_info));
  658. entry->next = RLB_NULL_INDEX;
  659. entry->prev = RLB_NULL_INDEX;
  660. }
  661. static int rlb_initialize(struct bonding *bond)
  662. {
  663. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  664. struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
  665. int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
  666. int i;
  667. spin_lock_init(&(bond_info->rx_hashtbl_lock));
  668. _lock_rx_hashtbl(bond);
  669. bond_info->rx_hashtbl = kmalloc(size, GFP_KERNEL);
  670. if (!bond_info->rx_hashtbl) {
  671. printk(KERN_ERR DRV_NAME
  672. ": Error: %s: Failed to allocate RLB hash table\n",
  673. bond->dev->name);
  674. _unlock_rx_hashtbl(bond);
  675. return -1;
  676. }
  677. bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
  678. for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
  679. rlb_init_table_entry(bond_info->rx_hashtbl + i);
  680. }
  681. _unlock_rx_hashtbl(bond);
  682. /*initialize packet type*/
  683. pk_type->type = __constant_htons(ETH_P_ARP);
  684. pk_type->dev = bond->dev;
  685. pk_type->func = rlb_arp_recv;
  686. /* register to receive ARPs */
  687. dev_add_pack(pk_type);
  688. return 0;
  689. }
  690. static void rlb_deinitialize(struct bonding *bond)
  691. {
  692. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  693. dev_remove_pack(&(bond_info->rlb_pkt_type));
  694. _lock_rx_hashtbl(bond);
  695. kfree(bond_info->rx_hashtbl);
  696. bond_info->rx_hashtbl = NULL;
  697. bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
  698. _unlock_rx_hashtbl(bond);
  699. }
  700. static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  701. {
  702. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  703. u32 curr_index;
  704. _lock_rx_hashtbl(bond);
  705. curr_index = bond_info->rx_hashtbl_head;
  706. while (curr_index != RLB_NULL_INDEX) {
  707. struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
  708. u32 next_index = bond_info->rx_hashtbl[curr_index].next;
  709. u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
  710. if (curr->tag && (curr->vlan_id == vlan_id)) {
  711. if (curr_index == bond_info->rx_hashtbl_head) {
  712. bond_info->rx_hashtbl_head = next_index;
  713. }
  714. if (prev_index != RLB_NULL_INDEX) {
  715. bond_info->rx_hashtbl[prev_index].next = next_index;
  716. }
  717. if (next_index != RLB_NULL_INDEX) {
  718. bond_info->rx_hashtbl[next_index].prev = prev_index;
  719. }
  720. rlb_init_table_entry(curr);
  721. }
  722. curr_index = next_index;
  723. }
  724. _unlock_rx_hashtbl(bond);
  725. }
  726. /*********************** tlb/rlb shared functions *********************/
  727. static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
  728. {
  729. struct bonding *bond = bond_get_bond_by_slave(slave);
  730. struct learning_pkt pkt;
  731. int size = sizeof(struct learning_pkt);
  732. int i;
  733. memset(&pkt, 0, size);
  734. memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
  735. memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
  736. pkt.type = __constant_htons(ETH_P_LOOP);
  737. for (i = 0; i < MAX_LP_BURST; i++) {
  738. struct sk_buff *skb;
  739. char *data;
  740. skb = dev_alloc_skb(size);
  741. if (!skb) {
  742. return;
  743. }
  744. data = skb_put(skb, size);
  745. memcpy(data, &pkt, size);
  746. skb->mac.raw = data;
  747. skb->nh.raw = data + ETH_HLEN;
  748. skb->protocol = pkt.type;
  749. skb->priority = TC_PRIO_CONTROL;
  750. skb->dev = slave->dev;
  751. if (!list_empty(&bond->vlan_list)) {
  752. struct vlan_entry *vlan;
  753. vlan = bond_next_vlan(bond,
  754. bond->alb_info.current_alb_vlan);
  755. bond->alb_info.current_alb_vlan = vlan;
  756. if (!vlan) {
  757. kfree_skb(skb);
  758. continue;
  759. }
  760. skb = vlan_put_tag(skb, vlan->vlan_id);
  761. if (!skb) {
  762. printk(KERN_ERR DRV_NAME
  763. ": Error: failed to insert VLAN tag\n");
  764. continue;
  765. }
  766. }
  767. dev_queue_xmit(skb);
  768. }
  769. }
  770. /* hw is a boolean parameter that determines whether we should try and
  771. * set the hw address of the device as well as the hw address of the
  772. * net_device
  773. */
  774. static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
  775. {
  776. struct net_device *dev = slave->dev;
  777. struct sockaddr s_addr;
  778. if (!hw) {
  779. memcpy(dev->dev_addr, addr, dev->addr_len);
  780. return 0;
  781. }
  782. /* for rlb each slave must have a unique hw mac addresses so that */
  783. /* each slave will receive packets destined to a different mac */
  784. memcpy(s_addr.sa_data, addr, dev->addr_len);
  785. s_addr.sa_family = dev->type;
  786. if (dev_set_mac_address(dev, &s_addr)) {
  787. printk(KERN_ERR DRV_NAME
  788. ": Error: dev_set_mac_address of dev %s failed! ALB "
  789. "mode requires that the base driver support setting "
  790. "the hw address also when the network device's "
  791. "interface is open\n",
  792. dev->name);
  793. return -EOPNOTSUPP;
  794. }
  795. return 0;
  796. }
  797. /* Caller must hold bond lock for write or curr_slave_lock for write*/
  798. static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
  799. {
  800. struct slave *disabled_slave = NULL;
  801. u8 tmp_mac_addr[ETH_ALEN];
  802. int slaves_state_differ;
  803. slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
  804. memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
  805. alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
  806. alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
  807. /* fasten the change in the switch */
  808. if (SLAVE_IS_OK(slave1)) {
  809. alb_send_learning_packets(slave1, slave1->dev->dev_addr);
  810. if (bond->alb_info.rlb_enabled) {
  811. /* inform the clients that the mac address
  812. * has changed
  813. */
  814. rlb_req_update_slave_clients(bond, slave1);
  815. }
  816. } else {
  817. disabled_slave = slave1;
  818. }
  819. if (SLAVE_IS_OK(slave2)) {
  820. alb_send_learning_packets(slave2, slave2->dev->dev_addr);
  821. if (bond->alb_info.rlb_enabled) {
  822. /* inform the clients that the mac address
  823. * has changed
  824. */
  825. rlb_req_update_slave_clients(bond, slave2);
  826. }
  827. } else {
  828. disabled_slave = slave2;
  829. }
  830. if (bond->alb_info.rlb_enabled && slaves_state_differ) {
  831. /* A disabled slave was assigned an active mac addr */
  832. rlb_teach_disabled_mac_on_primary(bond,
  833. disabled_slave->dev->dev_addr);
  834. }
  835. }
  836. /**
  837. * alb_change_hw_addr_on_detach
  838. * @bond: bonding we're working on
  839. * @slave: the slave that was just detached
  840. *
  841. * We assume that @slave was already detached from the slave list.
  842. *
  843. * If @slave's permanent hw address is different both from its current
  844. * address and from @bond's address, then somewhere in the bond there's
  845. * a slave that has @slave's permanet address as its current address.
  846. * We'll make sure that that slave no longer uses @slave's permanent address.
  847. *
  848. * Caller must hold bond lock
  849. */
  850. static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
  851. {
  852. int perm_curr_diff;
  853. int perm_bond_diff;
  854. perm_curr_diff = memcmp(slave->perm_hwaddr,
  855. slave->dev->dev_addr,
  856. ETH_ALEN);
  857. perm_bond_diff = memcmp(slave->perm_hwaddr,
  858. bond->dev->dev_addr,
  859. ETH_ALEN);
  860. if (perm_curr_diff && perm_bond_diff) {
  861. struct slave *tmp_slave;
  862. int i, found = 0;
  863. bond_for_each_slave(bond, tmp_slave, i) {
  864. if (!memcmp(slave->perm_hwaddr,
  865. tmp_slave->dev->dev_addr,
  866. ETH_ALEN)) {
  867. found = 1;
  868. break;
  869. }
  870. }
  871. if (found) {
  872. alb_swap_mac_addr(bond, slave, tmp_slave);
  873. }
  874. }
  875. }
  876. /**
  877. * alb_handle_addr_collision_on_attach
  878. * @bond: bonding we're working on
  879. * @slave: the slave that was just attached
  880. *
  881. * checks uniqueness of slave's mac address and handles the case the
  882. * new slave uses the bonds mac address.
  883. *
  884. * If the permanent hw address of @slave is @bond's hw address, we need to
  885. * find a different hw address to give @slave, that isn't in use by any other
  886. * slave in the bond. This address must be, of course, one of the premanent
  887. * addresses of the other slaves.
  888. *
  889. * We go over the slave list, and for each slave there we compare its
  890. * permanent hw address with the current address of all the other slaves.
  891. * If no match was found, then we've found a slave with a permanent address
  892. * that isn't used by any other slave in the bond, so we can assign it to
  893. * @slave.
  894. *
  895. * assumption: this function is called before @slave is attached to the
  896. * bond slave list.
  897. *
  898. * caller must hold the bond lock for write since the mac addresses are compared
  899. * and may be swapped.
  900. */
  901. static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
  902. {
  903. struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
  904. struct slave *has_bond_addr = bond->curr_active_slave;
  905. int i, j, found = 0;
  906. if (bond->slave_cnt == 0) {
  907. /* this is the first slave */
  908. return 0;
  909. }
  910. /* if slave's mac address differs from bond's mac address
  911. * check uniqueness of slave's mac address against the other
  912. * slaves in the bond.
  913. */
  914. if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
  915. bond_for_each_slave(bond, tmp_slave1, i) {
  916. if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
  917. ETH_ALEN)) {
  918. found = 1;
  919. break;
  920. }
  921. }
  922. if (!found)
  923. return 0;
  924. /* Try setting slave mac to bond address and fall-through
  925. to code handling that situation below... */
  926. alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
  927. bond->alb_info.rlb_enabled);
  928. }
  929. /* The slave's address is equal to the address of the bond.
  930. * Search for a spare address in the bond for this slave.
  931. */
  932. free_mac_slave = NULL;
  933. bond_for_each_slave(bond, tmp_slave1, i) {
  934. found = 0;
  935. bond_for_each_slave(bond, tmp_slave2, j) {
  936. if (!memcmp(tmp_slave1->perm_hwaddr,
  937. tmp_slave2->dev->dev_addr,
  938. ETH_ALEN)) {
  939. found = 1;
  940. break;
  941. }
  942. }
  943. if (!found) {
  944. /* no slave has tmp_slave1's perm addr
  945. * as its curr addr
  946. */
  947. free_mac_slave = tmp_slave1;
  948. break;
  949. }
  950. if (!has_bond_addr) {
  951. if (!memcmp(tmp_slave1->dev->dev_addr,
  952. bond->dev->dev_addr,
  953. ETH_ALEN)) {
  954. has_bond_addr = tmp_slave1;
  955. }
  956. }
  957. }
  958. if (free_mac_slave) {
  959. alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
  960. bond->alb_info.rlb_enabled);
  961. printk(KERN_WARNING DRV_NAME
  962. ": Warning: the hw address of slave %s is in use by "
  963. "the bond; giving it the hw address of %s\n",
  964. slave->dev->name, free_mac_slave->dev->name);
  965. } else if (has_bond_addr) {
  966. printk(KERN_ERR DRV_NAME
  967. ": Error: the hw address of slave %s is in use by the "
  968. "bond; couldn't find a slave with a free hw address to "
  969. "give it (this should not have happened)\n",
  970. slave->dev->name);
  971. return -EFAULT;
  972. }
  973. return 0;
  974. }
  975. /**
  976. * alb_set_mac_address
  977. * @bond:
  978. * @addr:
  979. *
  980. * In TLB mode all slaves are configured to the bond's hw address, but set
  981. * their dev_addr field to different addresses (based on their permanent hw
  982. * addresses).
  983. *
  984. * For each slave, this function sets the interface to the new address and then
  985. * changes its dev_addr field to its previous value.
  986. *
  987. * Unwinding assumes bond's mac address has not yet changed.
  988. */
  989. static int alb_set_mac_address(struct bonding *bond, void *addr)
  990. {
  991. struct sockaddr sa;
  992. struct slave *slave, *stop_at;
  993. char tmp_addr[ETH_ALEN];
  994. int res;
  995. int i;
  996. if (bond->alb_info.rlb_enabled) {
  997. return 0;
  998. }
  999. bond_for_each_slave(bond, slave, i) {
  1000. if (slave->dev->set_mac_address == NULL) {
  1001. res = -EOPNOTSUPP;
  1002. goto unwind;
  1003. }
  1004. /* save net_device's current hw address */
  1005. memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
  1006. res = dev_set_mac_address(slave->dev, addr);
  1007. /* restore net_device's hw address */
  1008. memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
  1009. if (res) {
  1010. goto unwind;
  1011. }
  1012. }
  1013. return 0;
  1014. unwind:
  1015. memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
  1016. sa.sa_family = bond->dev->type;
  1017. /* unwind from head to the slave that failed */
  1018. stop_at = slave;
  1019. bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
  1020. memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
  1021. dev_set_mac_address(slave->dev, &sa);
  1022. memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
  1023. }
  1024. return res;
  1025. }
  1026. /************************ exported alb funcions ************************/
  1027. int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
  1028. {
  1029. int res;
  1030. res = tlb_initialize(bond);
  1031. if (res) {
  1032. return res;
  1033. }
  1034. if (rlb_enabled) {
  1035. bond->alb_info.rlb_enabled = 1;
  1036. /* initialize rlb */
  1037. res = rlb_initialize(bond);
  1038. if (res) {
  1039. tlb_deinitialize(bond);
  1040. return res;
  1041. }
  1042. }
  1043. return 0;
  1044. }
  1045. void bond_alb_deinitialize(struct bonding *bond)
  1046. {
  1047. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1048. tlb_deinitialize(bond);
  1049. if (bond_info->rlb_enabled) {
  1050. rlb_deinitialize(bond);
  1051. }
  1052. }
  1053. int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
  1054. {
  1055. struct bonding *bond = bond_dev->priv;
  1056. struct ethhdr *eth_data;
  1057. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1058. struct slave *tx_slave = NULL;
  1059. static u32 ip_bcast = 0xffffffff;
  1060. int hash_size = 0;
  1061. int do_tx_balance = 1;
  1062. u32 hash_index = 0;
  1063. u8 *hash_start = NULL;
  1064. int res = 1;
  1065. skb->mac.raw = (unsigned char *)skb->data;
  1066. eth_data = eth_hdr(skb);
  1067. /* make sure that the curr_active_slave and the slaves list do
  1068. * not change during tx
  1069. */
  1070. read_lock(&bond->lock);
  1071. read_lock(&bond->curr_slave_lock);
  1072. if (!BOND_IS_OK(bond)) {
  1073. goto out;
  1074. }
  1075. switch (ntohs(skb->protocol)) {
  1076. case ETH_P_IP:
  1077. if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
  1078. (skb->nh.iph->daddr == ip_bcast) ||
  1079. (skb->nh.iph->protocol == IPPROTO_IGMP)) {
  1080. do_tx_balance = 0;
  1081. break;
  1082. }
  1083. hash_start = (char*)&(skb->nh.iph->daddr);
  1084. hash_size = sizeof(skb->nh.iph->daddr);
  1085. break;
  1086. case ETH_P_IPV6:
  1087. if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
  1088. do_tx_balance = 0;
  1089. break;
  1090. }
  1091. hash_start = (char*)&(skb->nh.ipv6h->daddr);
  1092. hash_size = sizeof(skb->nh.ipv6h->daddr);
  1093. break;
  1094. case ETH_P_IPX:
  1095. if (ipx_hdr(skb)->ipx_checksum !=
  1096. __constant_htons(IPX_NO_CHECKSUM)) {
  1097. /* something is wrong with this packet */
  1098. do_tx_balance = 0;
  1099. break;
  1100. }
  1101. if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
  1102. /* The only protocol worth balancing in
  1103. * this family since it has an "ARP" like
  1104. * mechanism
  1105. */
  1106. do_tx_balance = 0;
  1107. break;
  1108. }
  1109. hash_start = (char*)eth_data->h_dest;
  1110. hash_size = ETH_ALEN;
  1111. break;
  1112. case ETH_P_ARP:
  1113. do_tx_balance = 0;
  1114. if (bond_info->rlb_enabled) {
  1115. tx_slave = rlb_arp_xmit(skb, bond);
  1116. }
  1117. break;
  1118. default:
  1119. do_tx_balance = 0;
  1120. break;
  1121. }
  1122. if (do_tx_balance) {
  1123. hash_index = _simple_hash(hash_start, hash_size);
  1124. tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
  1125. }
  1126. if (!tx_slave) {
  1127. /* unbalanced or unassigned, send through primary */
  1128. tx_slave = bond->curr_active_slave;
  1129. bond_info->unbalanced_load += skb->len;
  1130. }
  1131. if (tx_slave && SLAVE_IS_OK(tx_slave)) {
  1132. if (tx_slave != bond->curr_active_slave) {
  1133. memcpy(eth_data->h_source,
  1134. tx_slave->dev->dev_addr,
  1135. ETH_ALEN);
  1136. }
  1137. res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
  1138. } else {
  1139. if (tx_slave) {
  1140. tlb_clear_slave(bond, tx_slave, 0);
  1141. }
  1142. }
  1143. out:
  1144. if (res) {
  1145. /* no suitable interface, frame not sent */
  1146. dev_kfree_skb(skb);
  1147. }
  1148. read_unlock(&bond->curr_slave_lock);
  1149. read_unlock(&bond->lock);
  1150. return 0;
  1151. }
  1152. void bond_alb_monitor(struct bonding *bond)
  1153. {
  1154. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1155. struct slave *slave;
  1156. int i;
  1157. read_lock(&bond->lock);
  1158. if (bond->kill_timers) {
  1159. goto out;
  1160. }
  1161. if (bond->slave_cnt == 0) {
  1162. bond_info->tx_rebalance_counter = 0;
  1163. bond_info->lp_counter = 0;
  1164. goto re_arm;
  1165. }
  1166. bond_info->tx_rebalance_counter++;
  1167. bond_info->lp_counter++;
  1168. /* send learning packets */
  1169. if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
  1170. /* change of curr_active_slave involves swapping of mac addresses.
  1171. * in order to avoid this swapping from happening while
  1172. * sending the learning packets, the curr_slave_lock must be held for
  1173. * read.
  1174. */
  1175. read_lock(&bond->curr_slave_lock);
  1176. bond_for_each_slave(bond, slave, i) {
  1177. alb_send_learning_packets(slave,slave->dev->dev_addr);
  1178. }
  1179. read_unlock(&bond->curr_slave_lock);
  1180. bond_info->lp_counter = 0;
  1181. }
  1182. /* rebalance tx traffic */
  1183. if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
  1184. read_lock(&bond->curr_slave_lock);
  1185. bond_for_each_slave(bond, slave, i) {
  1186. tlb_clear_slave(bond, slave, 1);
  1187. if (slave == bond->curr_active_slave) {
  1188. SLAVE_TLB_INFO(slave).load =
  1189. bond_info->unbalanced_load /
  1190. BOND_TLB_REBALANCE_INTERVAL;
  1191. bond_info->unbalanced_load = 0;
  1192. }
  1193. }
  1194. read_unlock(&bond->curr_slave_lock);
  1195. bond_info->tx_rebalance_counter = 0;
  1196. }
  1197. /* handle rlb stuff */
  1198. if (bond_info->rlb_enabled) {
  1199. /* the following code changes the promiscuity of the
  1200. * the curr_active_slave. It needs to be locked with a
  1201. * write lock to protect from other code that also
  1202. * sets the promiscuity.
  1203. */
  1204. write_lock(&bond->curr_slave_lock);
  1205. if (bond_info->primary_is_promisc &&
  1206. (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
  1207. bond_info->rlb_promisc_timeout_counter = 0;
  1208. /* If the primary was set to promiscuous mode
  1209. * because a slave was disabled then
  1210. * it can now leave promiscuous mode.
  1211. */
  1212. dev_set_promiscuity(bond->curr_active_slave->dev, -1);
  1213. bond_info->primary_is_promisc = 0;
  1214. }
  1215. write_unlock(&bond->curr_slave_lock);
  1216. if (bond_info->rlb_rebalance) {
  1217. bond_info->rlb_rebalance = 0;
  1218. rlb_rebalance(bond);
  1219. }
  1220. /* check if clients need updating */
  1221. if (bond_info->rx_ntt) {
  1222. if (bond_info->rlb_update_delay_counter) {
  1223. --bond_info->rlb_update_delay_counter;
  1224. } else {
  1225. rlb_update_rx_clients(bond);
  1226. if (bond_info->rlb_update_retry_counter) {
  1227. --bond_info->rlb_update_retry_counter;
  1228. } else {
  1229. bond_info->rx_ntt = 0;
  1230. }
  1231. }
  1232. }
  1233. }
  1234. re_arm:
  1235. mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
  1236. out:
  1237. read_unlock(&bond->lock);
  1238. }
  1239. /* assumption: called before the slave is attached to the bond
  1240. * and not locked by the bond lock
  1241. */
  1242. int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
  1243. {
  1244. int res;
  1245. res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
  1246. bond->alb_info.rlb_enabled);
  1247. if (res) {
  1248. return res;
  1249. }
  1250. /* caller must hold the bond lock for write since the mac addresses
  1251. * are compared and may be swapped.
  1252. */
  1253. write_lock_bh(&bond->lock);
  1254. res = alb_handle_addr_collision_on_attach(bond, slave);
  1255. write_unlock_bh(&bond->lock);
  1256. if (res) {
  1257. return res;
  1258. }
  1259. tlb_init_slave(slave);
  1260. /* order a rebalance ASAP */
  1261. bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1262. if (bond->alb_info.rlb_enabled) {
  1263. bond->alb_info.rlb_rebalance = 1;
  1264. }
  1265. return 0;
  1266. }
  1267. /* Caller must hold bond lock for write */
  1268. void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
  1269. {
  1270. if (bond->slave_cnt > 1) {
  1271. alb_change_hw_addr_on_detach(bond, slave);
  1272. }
  1273. tlb_clear_slave(bond, slave, 0);
  1274. if (bond->alb_info.rlb_enabled) {
  1275. bond->alb_info.next_rx_slave = NULL;
  1276. rlb_clear_slave(bond, slave);
  1277. }
  1278. }
  1279. /* Caller must hold bond lock for read */
  1280. void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
  1281. {
  1282. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1283. if (link == BOND_LINK_DOWN) {
  1284. tlb_clear_slave(bond, slave, 0);
  1285. if (bond->alb_info.rlb_enabled) {
  1286. rlb_clear_slave(bond, slave);
  1287. }
  1288. } else if (link == BOND_LINK_UP) {
  1289. /* order a rebalance ASAP */
  1290. bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1291. if (bond->alb_info.rlb_enabled) {
  1292. bond->alb_info.rlb_rebalance = 1;
  1293. /* If the updelay module parameter is smaller than the
  1294. * forwarding delay of the switch the rebalance will
  1295. * not work because the rebalance arp replies will
  1296. * not be forwarded to the clients..
  1297. */
  1298. }
  1299. }
  1300. }
  1301. /**
  1302. * bond_alb_handle_active_change - assign new curr_active_slave
  1303. * @bond: our bonding struct
  1304. * @new_slave: new slave to assign
  1305. *
  1306. * Set the bond->curr_active_slave to @new_slave and handle
  1307. * mac address swapping and promiscuity changes as needed.
  1308. *
  1309. * Caller must hold bond curr_slave_lock for write (or bond lock for write)
  1310. */
  1311. void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
  1312. {
  1313. struct slave *swap_slave;
  1314. int i;
  1315. if (bond->curr_active_slave == new_slave) {
  1316. return;
  1317. }
  1318. if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
  1319. dev_set_promiscuity(bond->curr_active_slave->dev, -1);
  1320. bond->alb_info.primary_is_promisc = 0;
  1321. bond->alb_info.rlb_promisc_timeout_counter = 0;
  1322. }
  1323. swap_slave = bond->curr_active_slave;
  1324. bond->curr_active_slave = new_slave;
  1325. if (!new_slave || (bond->slave_cnt == 0)) {
  1326. return;
  1327. }
  1328. /* set the new curr_active_slave to the bonds mac address
  1329. * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
  1330. */
  1331. if (!swap_slave) {
  1332. struct slave *tmp_slave;
  1333. /* find slave that is holding the bond's mac address */
  1334. bond_for_each_slave(bond, tmp_slave, i) {
  1335. if (!memcmp(tmp_slave->dev->dev_addr,
  1336. bond->dev->dev_addr, ETH_ALEN)) {
  1337. swap_slave = tmp_slave;
  1338. break;
  1339. }
  1340. }
  1341. }
  1342. /* curr_active_slave must be set before calling alb_swap_mac_addr */
  1343. if (swap_slave) {
  1344. /* swap mac address */
  1345. alb_swap_mac_addr(bond, swap_slave, new_slave);
  1346. } else {
  1347. /* set the new_slave to the bond mac address */
  1348. alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
  1349. bond->alb_info.rlb_enabled);
  1350. /* fasten bond mac on new current slave */
  1351. alb_send_learning_packets(new_slave, bond->dev->dev_addr);
  1352. }
  1353. }
  1354. int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
  1355. {
  1356. struct bonding *bond = bond_dev->priv;
  1357. struct sockaddr *sa = addr;
  1358. struct slave *slave, *swap_slave;
  1359. int res;
  1360. int i;
  1361. if (!is_valid_ether_addr(sa->sa_data)) {
  1362. return -EADDRNOTAVAIL;
  1363. }
  1364. res = alb_set_mac_address(bond, addr);
  1365. if (res) {
  1366. return res;
  1367. }
  1368. memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
  1369. /* If there is no curr_active_slave there is nothing else to do.
  1370. * Otherwise we'll need to pass the new address to it and handle
  1371. * duplications.
  1372. */
  1373. if (!bond->curr_active_slave) {
  1374. return 0;
  1375. }
  1376. swap_slave = NULL;
  1377. bond_for_each_slave(bond, slave, i) {
  1378. if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
  1379. swap_slave = slave;
  1380. break;
  1381. }
  1382. }
  1383. if (swap_slave) {
  1384. alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
  1385. } else {
  1386. alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
  1387. bond->alb_info.rlb_enabled);
  1388. alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
  1389. if (bond->alb_info.rlb_enabled) {
  1390. /* inform clients mac address has changed */
  1391. rlb_req_update_slave_clients(bond, bond->curr_active_slave);
  1392. }
  1393. }
  1394. return 0;
  1395. }
  1396. void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  1397. {
  1398. if (bond->alb_info.current_alb_vlan &&
  1399. (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
  1400. bond->alb_info.current_alb_vlan = NULL;
  1401. }
  1402. if (bond->alb_info.rlb_enabled) {
  1403. rlb_clear_vlan(bond, vlan_id);
  1404. }
  1405. }