bond_alb.c 43 KB

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