bond_alb.c 42 KB

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