bond_alb.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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. __be16 type;
  79. u8 padding[ETH_ZLEN - ETH_HLEN];
  80. };
  81. struct arp_pkt {
  82. __be16 hw_addr_space;
  83. __be16 prot_addr_space;
  84. u8 hw_addr_len;
  85. u8 prot_addr_len;
  86. __be16 op_code;
  87. u8 mac_src[ETH_ALEN]; /* sender hardware address */
  88. __be32 ip_src; /* sender IP address */
  89. u8 mac_dst[ETH_ALEN]; /* target hardware address */
  90. __be32 ip_dst; /* target IP address */
  91. };
  92. #pragma pack()
  93. static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
  94. {
  95. return (struct arp_pkt *)skb_network_header(skb);
  96. }
  97. /* Forward declaration */
  98. static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
  99. static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
  100. {
  101. int i;
  102. u8 hash = 0;
  103. for (i = 0; i < hash_size; i++) {
  104. hash ^= hash_start[i];
  105. }
  106. return hash;
  107. }
  108. /*********************** tlb specific functions ***************************/
  109. static inline void _lock_tx_hashtbl(struct bonding *bond)
  110. {
  111. spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
  112. }
  113. static inline void _unlock_tx_hashtbl(struct bonding *bond)
  114. {
  115. spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
  116. }
  117. /* Caller must hold tx_hashtbl lock */
  118. static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
  119. {
  120. if (save_load) {
  121. entry->load_history = 1 + entry->tx_bytes /
  122. BOND_TLB_REBALANCE_INTERVAL;
  123. entry->tx_bytes = 0;
  124. }
  125. entry->tx_slave = NULL;
  126. entry->next = TLB_NULL_INDEX;
  127. entry->prev = TLB_NULL_INDEX;
  128. }
  129. static inline void tlb_init_slave(struct slave *slave)
  130. {
  131. SLAVE_TLB_INFO(slave).load = 0;
  132. SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
  133. }
  134. /* Caller must hold bond lock for read */
  135. static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
  136. {
  137. struct tlb_client_info *tx_hash_table;
  138. u32 index;
  139. _lock_tx_hashtbl(bond);
  140. /* clear slave from tx_hashtbl */
  141. tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
  142. index = SLAVE_TLB_INFO(slave).head;
  143. while (index != TLB_NULL_INDEX) {
  144. u32 next_index = tx_hash_table[index].next;
  145. tlb_init_table_entry(&tx_hash_table[index], save_load);
  146. index = next_index;
  147. }
  148. tlb_init_slave(slave);
  149. _unlock_tx_hashtbl(bond);
  150. }
  151. /* Must be called before starting the monitor timer */
  152. static int tlb_initialize(struct bonding *bond)
  153. {
  154. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  155. int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
  156. struct tlb_client_info *new_hashtbl;
  157. int i;
  158. spin_lock_init(&(bond_info->tx_hashtbl_lock));
  159. new_hashtbl = kzalloc(size, GFP_KERNEL);
  160. if (!new_hashtbl) {
  161. printk(KERN_ERR DRV_NAME
  162. ": %s: Error: Failed to allocate TLB hash table\n",
  163. bond->dev->name);
  164. return -1;
  165. }
  166. _lock_tx_hashtbl(bond);
  167. bond_info->tx_hashtbl = new_hashtbl;
  168. for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
  169. tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
  170. }
  171. _unlock_tx_hashtbl(bond);
  172. return 0;
  173. }
  174. /* Must be called only after all slaves have been released */
  175. static void tlb_deinitialize(struct bonding *bond)
  176. {
  177. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  178. _lock_tx_hashtbl(bond);
  179. kfree(bond_info->tx_hashtbl);
  180. bond_info->tx_hashtbl = NULL;
  181. _unlock_tx_hashtbl(bond);
  182. }
  183. /* Caller must hold bond lock for read */
  184. static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
  185. {
  186. struct slave *slave, *least_loaded;
  187. s64 max_gap;
  188. int i, found = 0;
  189. /* Find the first enabled slave */
  190. bond_for_each_slave(bond, slave, i) {
  191. if (SLAVE_IS_OK(slave)) {
  192. found = 1;
  193. break;
  194. }
  195. }
  196. if (!found) {
  197. return NULL;
  198. }
  199. least_loaded = slave;
  200. max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
  201. (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
  202. /* Find the slave with the largest gap */
  203. bond_for_each_slave_from(bond, slave, i, least_loaded) {
  204. if (SLAVE_IS_OK(slave)) {
  205. s64 gap = (s64)(slave->speed << 20) -
  206. (s64)(SLAVE_TLB_INFO(slave).load << 3);
  207. if (max_gap < gap) {
  208. least_loaded = slave;
  209. max_gap = gap;
  210. }
  211. }
  212. }
  213. return least_loaded;
  214. }
  215. /* Caller must hold bond lock for read */
  216. static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
  217. {
  218. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  219. struct tlb_client_info *hash_table;
  220. struct slave *assigned_slave;
  221. _lock_tx_hashtbl(bond);
  222. hash_table = bond_info->tx_hashtbl;
  223. assigned_slave = hash_table[hash_index].tx_slave;
  224. if (!assigned_slave) {
  225. assigned_slave = tlb_get_least_loaded_slave(bond);
  226. if (assigned_slave) {
  227. struct tlb_slave_info *slave_info =
  228. &(SLAVE_TLB_INFO(assigned_slave));
  229. u32 next_index = slave_info->head;
  230. hash_table[hash_index].tx_slave = assigned_slave;
  231. hash_table[hash_index].next = next_index;
  232. hash_table[hash_index].prev = TLB_NULL_INDEX;
  233. if (next_index != TLB_NULL_INDEX) {
  234. hash_table[next_index].prev = hash_index;
  235. }
  236. slave_info->head = hash_index;
  237. slave_info->load +=
  238. hash_table[hash_index].load_history;
  239. }
  240. }
  241. if (assigned_slave) {
  242. hash_table[hash_index].tx_bytes += skb_len;
  243. }
  244. _unlock_tx_hashtbl(bond);
  245. return assigned_slave;
  246. }
  247. /*********************** rlb specific functions ***************************/
  248. static inline void _lock_rx_hashtbl(struct bonding *bond)
  249. {
  250. spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
  251. }
  252. static inline void _unlock_rx_hashtbl(struct bonding *bond)
  253. {
  254. spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
  255. }
  256. /* when an ARP REPLY is received from a client update its info
  257. * in the rx_hashtbl
  258. */
  259. static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
  260. {
  261. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  262. struct rlb_client_info *client_info;
  263. u32 hash_index;
  264. _lock_rx_hashtbl(bond);
  265. hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
  266. client_info = &(bond_info->rx_hashtbl[hash_index]);
  267. if ((client_info->assigned) &&
  268. (client_info->ip_src == arp->ip_dst) &&
  269. (client_info->ip_dst == arp->ip_src)) {
  270. /* update the clients MAC address */
  271. memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
  272. client_info->ntt = 1;
  273. bond_info->rx_ntt = 1;
  274. }
  275. _unlock_rx_hashtbl(bond);
  276. }
  277. static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
  278. {
  279. struct bonding *bond = bond_dev->priv;
  280. struct arp_pkt *arp = (struct arp_pkt *)skb->data;
  281. int res = NET_RX_DROP;
  282. if (dev_net(bond_dev) != &init_net)
  283. goto out;
  284. if (!(bond_dev->flags & IFF_MASTER))
  285. goto out;
  286. if (!arp) {
  287. dprintk("Packet has no ARP data\n");
  288. goto out;
  289. }
  290. if (skb->len < sizeof(struct arp_pkt)) {
  291. dprintk("Packet is too small to be an ARP\n");
  292. goto out;
  293. }
  294. if (arp->op_code == htons(ARPOP_REPLY)) {
  295. /* update rx hash table for this ARP */
  296. rlb_update_entry_from_arp(bond, arp);
  297. dprintk("Server received an ARP Reply from client\n");
  298. }
  299. res = NET_RX_SUCCESS;
  300. out:
  301. dev_kfree_skb(skb);
  302. return res;
  303. }
  304. /* Caller must hold bond lock for read */
  305. static struct slave *rlb_next_rx_slave(struct bonding *bond)
  306. {
  307. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  308. struct slave *rx_slave, *slave, *start_at;
  309. int i = 0;
  310. if (bond_info->next_rx_slave) {
  311. start_at = bond_info->next_rx_slave;
  312. } else {
  313. start_at = bond->first_slave;
  314. }
  315. rx_slave = NULL;
  316. bond_for_each_slave_from(bond, slave, i, start_at) {
  317. if (SLAVE_IS_OK(slave)) {
  318. if (!rx_slave) {
  319. rx_slave = slave;
  320. } else if (slave->speed > rx_slave->speed) {
  321. rx_slave = slave;
  322. }
  323. }
  324. }
  325. if (rx_slave) {
  326. bond_info->next_rx_slave = rx_slave->next;
  327. }
  328. return rx_slave;
  329. }
  330. /* teach the switch the mac of a disabled slave
  331. * on the primary for fault tolerance
  332. *
  333. * Caller must hold bond->curr_slave_lock for write or bond lock for write
  334. */
  335. static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
  336. {
  337. if (!bond->curr_active_slave) {
  338. return;
  339. }
  340. if (!bond->alb_info.primary_is_promisc) {
  341. bond->alb_info.primary_is_promisc = 1;
  342. dev_set_promiscuity(bond->curr_active_slave->dev, 1);
  343. }
  344. bond->alb_info.rlb_promisc_timeout_counter = 0;
  345. alb_send_learning_packets(bond->curr_active_slave, addr);
  346. }
  347. /* slave being removed should not be active at this point
  348. *
  349. * Caller must hold bond lock for read
  350. */
  351. static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
  352. {
  353. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  354. struct rlb_client_info *rx_hash_table;
  355. u32 index, next_index;
  356. /* clear slave from rx_hashtbl */
  357. _lock_rx_hashtbl(bond);
  358. rx_hash_table = bond_info->rx_hashtbl;
  359. index = bond_info->rx_hashtbl_head;
  360. for (; index != RLB_NULL_INDEX; index = next_index) {
  361. next_index = rx_hash_table[index].next;
  362. if (rx_hash_table[index].slave == slave) {
  363. struct slave *assigned_slave = rlb_next_rx_slave(bond);
  364. if (assigned_slave) {
  365. rx_hash_table[index].slave = assigned_slave;
  366. if (memcmp(rx_hash_table[index].mac_dst,
  367. mac_bcast, ETH_ALEN)) {
  368. bond_info->rx_hashtbl[index].ntt = 1;
  369. bond_info->rx_ntt = 1;
  370. /* A slave has been removed from the
  371. * table because it is either disabled
  372. * or being released. We must retry the
  373. * update to avoid clients from not
  374. * being updated & disconnecting when
  375. * there is stress
  376. */
  377. bond_info->rlb_update_retry_counter =
  378. RLB_UPDATE_RETRY;
  379. }
  380. } else { /* there is no active slave */
  381. rx_hash_table[index].slave = NULL;
  382. }
  383. }
  384. }
  385. _unlock_rx_hashtbl(bond);
  386. write_lock_bh(&bond->curr_slave_lock);
  387. if (slave != bond->curr_active_slave) {
  388. rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
  389. }
  390. write_unlock_bh(&bond->curr_slave_lock);
  391. }
  392. static void rlb_update_client(struct rlb_client_info *client_info)
  393. {
  394. int i;
  395. if (!client_info->slave) {
  396. return;
  397. }
  398. for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
  399. struct sk_buff *skb;
  400. skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
  401. client_info->ip_dst,
  402. client_info->slave->dev,
  403. client_info->ip_src,
  404. client_info->mac_dst,
  405. client_info->slave->dev->dev_addr,
  406. client_info->mac_dst);
  407. if (!skb) {
  408. printk(KERN_ERR DRV_NAME
  409. ": %s: Error: failed to create an ARP packet\n",
  410. client_info->slave->dev->master->name);
  411. continue;
  412. }
  413. skb->dev = client_info->slave->dev;
  414. if (client_info->tag) {
  415. skb = vlan_put_tag(skb, client_info->vlan_id);
  416. if (!skb) {
  417. printk(KERN_ERR DRV_NAME
  418. ": %s: Error: failed to insert VLAN tag\n",
  419. client_info->slave->dev->master->name);
  420. continue;
  421. }
  422. }
  423. arp_xmit(skb);
  424. }
  425. }
  426. /* sends ARP REPLIES that update the clients that need updating */
  427. static void rlb_update_rx_clients(struct bonding *bond)
  428. {
  429. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  430. struct rlb_client_info *client_info;
  431. u32 hash_index;
  432. _lock_rx_hashtbl(bond);
  433. hash_index = bond_info->rx_hashtbl_head;
  434. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  435. client_info = &(bond_info->rx_hashtbl[hash_index]);
  436. if (client_info->ntt) {
  437. rlb_update_client(client_info);
  438. if (bond_info->rlb_update_retry_counter == 0) {
  439. client_info->ntt = 0;
  440. }
  441. }
  442. }
  443. /* do not update the entries again untill this counter is zero so that
  444. * not to confuse the clients.
  445. */
  446. bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
  447. _unlock_rx_hashtbl(bond);
  448. }
  449. /* The slave was assigned a new mac address - update the clients */
  450. static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
  451. {
  452. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  453. struct rlb_client_info *client_info;
  454. int ntt = 0;
  455. u32 hash_index;
  456. _lock_rx_hashtbl(bond);
  457. hash_index = bond_info->rx_hashtbl_head;
  458. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  459. client_info = &(bond_info->rx_hashtbl[hash_index]);
  460. if ((client_info->slave == slave) &&
  461. memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  462. client_info->ntt = 1;
  463. ntt = 1;
  464. }
  465. }
  466. // update the team's flag only after the whole iteration
  467. if (ntt) {
  468. bond_info->rx_ntt = 1;
  469. //fasten the change
  470. bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
  471. }
  472. _unlock_rx_hashtbl(bond);
  473. }
  474. /* mark all clients using src_ip to be updated */
  475. static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
  476. {
  477. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  478. struct rlb_client_info *client_info;
  479. u32 hash_index;
  480. _lock_rx_hashtbl(bond);
  481. hash_index = bond_info->rx_hashtbl_head;
  482. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  483. client_info = &(bond_info->rx_hashtbl[hash_index]);
  484. if (!client_info->slave) {
  485. printk(KERN_ERR DRV_NAME
  486. ": %s: Error: found a client with no channel in "
  487. "the client's hash table\n",
  488. bond->dev->name);
  489. continue;
  490. }
  491. /*update all clients using this src_ip, that are not assigned
  492. * to the team's address (curr_active_slave) and have a known
  493. * unicast mac address.
  494. */
  495. if ((client_info->ip_src == src_ip) &&
  496. memcmp(client_info->slave->dev->dev_addr,
  497. bond->dev->dev_addr, ETH_ALEN) &&
  498. memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  499. client_info->ntt = 1;
  500. bond_info->rx_ntt = 1;
  501. }
  502. }
  503. _unlock_rx_hashtbl(bond);
  504. }
  505. /* Caller must hold both bond and ptr locks for read */
  506. static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
  507. {
  508. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  509. struct arp_pkt *arp = arp_pkt(skb);
  510. struct slave *assigned_slave;
  511. struct rlb_client_info *client_info;
  512. u32 hash_index = 0;
  513. _lock_rx_hashtbl(bond);
  514. hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
  515. client_info = &(bond_info->rx_hashtbl[hash_index]);
  516. if (client_info->assigned) {
  517. if ((client_info->ip_src == arp->ip_src) &&
  518. (client_info->ip_dst == arp->ip_dst)) {
  519. /* the entry is already assigned to this client */
  520. if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
  521. /* update mac address from arp */
  522. memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
  523. }
  524. assigned_slave = client_info->slave;
  525. if (assigned_slave) {
  526. _unlock_rx_hashtbl(bond);
  527. return assigned_slave;
  528. }
  529. } else {
  530. /* the entry is already assigned to some other client,
  531. * move the old client to primary (curr_active_slave) so
  532. * that the new client can be assigned to this entry.
  533. */
  534. if (bond->curr_active_slave &&
  535. client_info->slave != bond->curr_active_slave) {
  536. client_info->slave = bond->curr_active_slave;
  537. rlb_update_client(client_info);
  538. }
  539. }
  540. }
  541. /* assign a new slave */
  542. assigned_slave = rlb_next_rx_slave(bond);
  543. if (assigned_slave) {
  544. client_info->ip_src = arp->ip_src;
  545. client_info->ip_dst = arp->ip_dst;
  546. /* arp->mac_dst is broadcast for arp reqeusts.
  547. * will be updated with clients actual unicast mac address
  548. * upon receiving an arp reply.
  549. */
  550. memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
  551. client_info->slave = assigned_slave;
  552. if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
  553. client_info->ntt = 1;
  554. bond->alb_info.rx_ntt = 1;
  555. } else {
  556. client_info->ntt = 0;
  557. }
  558. if (!list_empty(&bond->vlan_list)) {
  559. if (!vlan_get_tag(skb, &client_info->vlan_id))
  560. client_info->tag = 1;
  561. }
  562. if (!client_info->assigned) {
  563. u32 prev_tbl_head = bond_info->rx_hashtbl_head;
  564. bond_info->rx_hashtbl_head = hash_index;
  565. client_info->next = prev_tbl_head;
  566. if (prev_tbl_head != RLB_NULL_INDEX) {
  567. bond_info->rx_hashtbl[prev_tbl_head].prev =
  568. hash_index;
  569. }
  570. client_info->assigned = 1;
  571. }
  572. }
  573. _unlock_rx_hashtbl(bond);
  574. return assigned_slave;
  575. }
  576. /* chooses (and returns) transmit channel for arp reply
  577. * does not choose channel for other arp types since they are
  578. * sent on the curr_active_slave
  579. */
  580. static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
  581. {
  582. struct arp_pkt *arp = arp_pkt(skb);
  583. struct slave *tx_slave = NULL;
  584. if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
  585. /* the arp must be sent on the selected
  586. * rx channel
  587. */
  588. tx_slave = rlb_choose_channel(skb, bond);
  589. if (tx_slave) {
  590. memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
  591. }
  592. dprintk("Server sent ARP Reply packet\n");
  593. } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
  594. /* Create an entry in the rx_hashtbl for this client as a
  595. * place holder.
  596. * When the arp reply is received the entry will be updated
  597. * with the correct unicast address of the client.
  598. */
  599. rlb_choose_channel(skb, bond);
  600. /* The ARP relpy packets must be delayed so that
  601. * they can cancel out the influence of the ARP request.
  602. */
  603. bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
  604. /* arp requests are broadcast and are sent on the primary
  605. * the arp request will collapse all clients on the subnet to
  606. * the primary slave. We must register these clients to be
  607. * updated with their assigned mac.
  608. */
  609. rlb_req_update_subnet_clients(bond, arp->ip_src);
  610. dprintk("Server sent ARP Request packet\n");
  611. }
  612. return tx_slave;
  613. }
  614. /* Caller must hold bond lock for read */
  615. static void rlb_rebalance(struct bonding *bond)
  616. {
  617. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  618. struct slave *assigned_slave;
  619. struct rlb_client_info *client_info;
  620. int ntt;
  621. u32 hash_index;
  622. _lock_rx_hashtbl(bond);
  623. ntt = 0;
  624. hash_index = bond_info->rx_hashtbl_head;
  625. for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
  626. client_info = &(bond_info->rx_hashtbl[hash_index]);
  627. assigned_slave = rlb_next_rx_slave(bond);
  628. if (assigned_slave && (client_info->slave != assigned_slave)) {
  629. client_info->slave = assigned_slave;
  630. client_info->ntt = 1;
  631. ntt = 1;
  632. }
  633. }
  634. /* update the team's flag only after the whole iteration */
  635. if (ntt) {
  636. bond_info->rx_ntt = 1;
  637. }
  638. _unlock_rx_hashtbl(bond);
  639. }
  640. /* Caller must hold rx_hashtbl lock */
  641. static void rlb_init_table_entry(struct rlb_client_info *entry)
  642. {
  643. memset(entry, 0, sizeof(struct rlb_client_info));
  644. entry->next = RLB_NULL_INDEX;
  645. entry->prev = RLB_NULL_INDEX;
  646. }
  647. static int rlb_initialize(struct bonding *bond)
  648. {
  649. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  650. struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
  651. struct rlb_client_info *new_hashtbl;
  652. int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
  653. int i;
  654. spin_lock_init(&(bond_info->rx_hashtbl_lock));
  655. new_hashtbl = kmalloc(size, GFP_KERNEL);
  656. if (!new_hashtbl) {
  657. printk(KERN_ERR DRV_NAME
  658. ": %s: Error: Failed to allocate RLB hash table\n",
  659. bond->dev->name);
  660. return -1;
  661. }
  662. _lock_rx_hashtbl(bond);
  663. bond_info->rx_hashtbl = new_hashtbl;
  664. bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
  665. for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
  666. rlb_init_table_entry(bond_info->rx_hashtbl + i);
  667. }
  668. _unlock_rx_hashtbl(bond);
  669. /*initialize packet type*/
  670. pk_type->type = __constant_htons(ETH_P_ARP);
  671. pk_type->dev = bond->dev;
  672. pk_type->func = rlb_arp_recv;
  673. /* register to receive ARPs */
  674. dev_add_pack(pk_type);
  675. return 0;
  676. }
  677. static void rlb_deinitialize(struct bonding *bond)
  678. {
  679. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  680. dev_remove_pack(&(bond_info->rlb_pkt_type));
  681. _lock_rx_hashtbl(bond);
  682. kfree(bond_info->rx_hashtbl);
  683. bond_info->rx_hashtbl = NULL;
  684. bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
  685. _unlock_rx_hashtbl(bond);
  686. }
  687. static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  688. {
  689. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  690. u32 curr_index;
  691. _lock_rx_hashtbl(bond);
  692. curr_index = bond_info->rx_hashtbl_head;
  693. while (curr_index != RLB_NULL_INDEX) {
  694. struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
  695. u32 next_index = bond_info->rx_hashtbl[curr_index].next;
  696. u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
  697. if (curr->tag && (curr->vlan_id == vlan_id)) {
  698. if (curr_index == bond_info->rx_hashtbl_head) {
  699. bond_info->rx_hashtbl_head = next_index;
  700. }
  701. if (prev_index != RLB_NULL_INDEX) {
  702. bond_info->rx_hashtbl[prev_index].next = next_index;
  703. }
  704. if (next_index != RLB_NULL_INDEX) {
  705. bond_info->rx_hashtbl[next_index].prev = prev_index;
  706. }
  707. rlb_init_table_entry(curr);
  708. }
  709. curr_index = next_index;
  710. }
  711. _unlock_rx_hashtbl(bond);
  712. }
  713. /*********************** tlb/rlb shared functions *********************/
  714. static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
  715. {
  716. struct bonding *bond = bond_get_bond_by_slave(slave);
  717. struct learning_pkt pkt;
  718. int size = sizeof(struct learning_pkt);
  719. int i;
  720. memset(&pkt, 0, size);
  721. memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
  722. memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
  723. pkt.type = __constant_htons(ETH_P_LOOP);
  724. for (i = 0; i < MAX_LP_BURST; i++) {
  725. struct sk_buff *skb;
  726. char *data;
  727. skb = dev_alloc_skb(size);
  728. if (!skb) {
  729. return;
  730. }
  731. data = skb_put(skb, size);
  732. memcpy(data, &pkt, size);
  733. skb_reset_mac_header(skb);
  734. skb->network_header = skb->mac_header + ETH_HLEN;
  735. skb->protocol = pkt.type;
  736. skb->priority = TC_PRIO_CONTROL;
  737. skb->dev = slave->dev;
  738. if (!list_empty(&bond->vlan_list)) {
  739. struct vlan_entry *vlan;
  740. vlan = bond_next_vlan(bond,
  741. bond->alb_info.current_alb_vlan);
  742. bond->alb_info.current_alb_vlan = vlan;
  743. if (!vlan) {
  744. kfree_skb(skb);
  745. continue;
  746. }
  747. skb = vlan_put_tag(skb, vlan->vlan_id);
  748. if (!skb) {
  749. printk(KERN_ERR DRV_NAME
  750. ": %s: Error: failed to insert VLAN tag\n",
  751. bond->dev->name);
  752. continue;
  753. }
  754. }
  755. dev_queue_xmit(skb);
  756. }
  757. }
  758. /* hw is a boolean parameter that determines whether we should try and
  759. * set the hw address of the device as well as the hw address of the
  760. * net_device
  761. */
  762. static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
  763. {
  764. struct net_device *dev = slave->dev;
  765. struct sockaddr s_addr;
  766. if (!hw) {
  767. memcpy(dev->dev_addr, addr, dev->addr_len);
  768. return 0;
  769. }
  770. /* for rlb each slave must have a unique hw mac addresses so that */
  771. /* each slave will receive packets destined to a different mac */
  772. memcpy(s_addr.sa_data, addr, dev->addr_len);
  773. s_addr.sa_family = dev->type;
  774. if (dev_set_mac_address(dev, &s_addr)) {
  775. printk(KERN_ERR DRV_NAME
  776. ": %s: Error: dev_set_mac_address of dev %s failed! ALB "
  777. "mode requires that the base driver support setting "
  778. "the hw address also when the network device's "
  779. "interface is open\n",
  780. dev->master->name, dev->name);
  781. return -EOPNOTSUPP;
  782. }
  783. return 0;
  784. }
  785. /*
  786. * Swap MAC addresses between two slaves.
  787. *
  788. * Called with RTNL held, and no other locks.
  789. *
  790. */
  791. static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
  792. {
  793. u8 tmp_mac_addr[ETH_ALEN];
  794. memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
  795. alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
  796. alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
  797. }
  798. /*
  799. * Send learning packets after MAC address swap.
  800. *
  801. * Called with RTNL and no other locks
  802. */
  803. static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
  804. struct slave *slave2)
  805. {
  806. int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
  807. struct slave *disabled_slave = NULL;
  808. ASSERT_RTNL();
  809. /* fasten the change in the switch */
  810. if (SLAVE_IS_OK(slave1)) {
  811. alb_send_learning_packets(slave1, slave1->dev->dev_addr);
  812. if (bond->alb_info.rlb_enabled) {
  813. /* inform the clients that the mac address
  814. * has changed
  815. */
  816. rlb_req_update_slave_clients(bond, slave1);
  817. }
  818. } else {
  819. disabled_slave = slave1;
  820. }
  821. if (SLAVE_IS_OK(slave2)) {
  822. alb_send_learning_packets(slave2, slave2->dev->dev_addr);
  823. if (bond->alb_info.rlb_enabled) {
  824. /* inform the clients that the mac address
  825. * has changed
  826. */
  827. rlb_req_update_slave_clients(bond, slave2);
  828. }
  829. } else {
  830. disabled_slave = slave2;
  831. }
  832. if (bond->alb_info.rlb_enabled && slaves_state_differ) {
  833. /* A disabled slave was assigned an active mac addr */
  834. rlb_teach_disabled_mac_on_primary(bond,
  835. disabled_slave->dev->dev_addr);
  836. }
  837. }
  838. /**
  839. * alb_change_hw_addr_on_detach
  840. * @bond: bonding we're working on
  841. * @slave: the slave that was just detached
  842. *
  843. * We assume that @slave was already detached from the slave list.
  844. *
  845. * If @slave's permanent hw address is different both from its current
  846. * address and from @bond's address, then somewhere in the bond there's
  847. * a slave that has @slave's permanet address as its current address.
  848. * We'll make sure that that slave no longer uses @slave's permanent address.
  849. *
  850. * Caller must hold RTNL and no other locks
  851. */
  852. static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
  853. {
  854. int perm_curr_diff;
  855. int perm_bond_diff;
  856. perm_curr_diff = memcmp(slave->perm_hwaddr,
  857. slave->dev->dev_addr,
  858. ETH_ALEN);
  859. perm_bond_diff = memcmp(slave->perm_hwaddr,
  860. bond->dev->dev_addr,
  861. ETH_ALEN);
  862. if (perm_curr_diff && perm_bond_diff) {
  863. struct slave *tmp_slave;
  864. int i, found = 0;
  865. bond_for_each_slave(bond, tmp_slave, i) {
  866. if (!memcmp(slave->perm_hwaddr,
  867. tmp_slave->dev->dev_addr,
  868. ETH_ALEN)) {
  869. found = 1;
  870. break;
  871. }
  872. }
  873. if (found) {
  874. /* locking: needs RTNL and nothing else */
  875. alb_swap_mac_addr(bond, slave, tmp_slave);
  876. alb_fasten_mac_swap(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. } else {
  1047. bond->alb_info.rlb_enabled = 0;
  1048. }
  1049. return 0;
  1050. }
  1051. void bond_alb_deinitialize(struct bonding *bond)
  1052. {
  1053. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1054. tlb_deinitialize(bond);
  1055. if (bond_info->rlb_enabled) {
  1056. rlb_deinitialize(bond);
  1057. }
  1058. }
  1059. int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
  1060. {
  1061. struct bonding *bond = bond_dev->priv;
  1062. struct ethhdr *eth_data;
  1063. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1064. struct slave *tx_slave = NULL;
  1065. static const __be32 ip_bcast = htonl(0xffffffff);
  1066. int hash_size = 0;
  1067. int do_tx_balance = 1;
  1068. u32 hash_index = 0;
  1069. const u8 *hash_start = NULL;
  1070. int res = 1;
  1071. skb_reset_mac_header(skb);
  1072. eth_data = eth_hdr(skb);
  1073. /* make sure that the curr_active_slave and the slaves list do
  1074. * not change during tx
  1075. */
  1076. read_lock(&bond->lock);
  1077. read_lock(&bond->curr_slave_lock);
  1078. if (!BOND_IS_OK(bond)) {
  1079. goto out;
  1080. }
  1081. switch (ntohs(skb->protocol)) {
  1082. case ETH_P_IP: {
  1083. const struct iphdr *iph = ip_hdr(skb);
  1084. if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
  1085. (iph->daddr == ip_bcast) ||
  1086. (iph->protocol == IPPROTO_IGMP)) {
  1087. do_tx_balance = 0;
  1088. break;
  1089. }
  1090. hash_start = (char *)&(iph->daddr);
  1091. hash_size = sizeof(iph->daddr);
  1092. }
  1093. break;
  1094. case ETH_P_IPV6:
  1095. if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
  1096. do_tx_balance = 0;
  1097. break;
  1098. }
  1099. hash_start = (char *)&(ipv6_hdr(skb)->daddr);
  1100. hash_size = sizeof(ipv6_hdr(skb)->daddr);
  1101. break;
  1102. case ETH_P_IPX:
  1103. if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
  1104. /* something is wrong with this packet */
  1105. do_tx_balance = 0;
  1106. break;
  1107. }
  1108. if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
  1109. /* The only protocol worth balancing in
  1110. * this family since it has an "ARP" like
  1111. * mechanism
  1112. */
  1113. do_tx_balance = 0;
  1114. break;
  1115. }
  1116. hash_start = (char*)eth_data->h_dest;
  1117. hash_size = ETH_ALEN;
  1118. break;
  1119. case ETH_P_ARP:
  1120. do_tx_balance = 0;
  1121. if (bond_info->rlb_enabled) {
  1122. tx_slave = rlb_arp_xmit(skb, bond);
  1123. }
  1124. break;
  1125. default:
  1126. do_tx_balance = 0;
  1127. break;
  1128. }
  1129. if (do_tx_balance) {
  1130. hash_index = _simple_hash(hash_start, hash_size);
  1131. tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
  1132. }
  1133. if (!tx_slave) {
  1134. /* unbalanced or unassigned, send through primary */
  1135. tx_slave = bond->curr_active_slave;
  1136. bond_info->unbalanced_load += skb->len;
  1137. }
  1138. if (tx_slave && SLAVE_IS_OK(tx_slave)) {
  1139. if (tx_slave != bond->curr_active_slave) {
  1140. memcpy(eth_data->h_source,
  1141. tx_slave->dev->dev_addr,
  1142. ETH_ALEN);
  1143. }
  1144. res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
  1145. } else {
  1146. if (tx_slave) {
  1147. tlb_clear_slave(bond, tx_slave, 0);
  1148. }
  1149. }
  1150. out:
  1151. if (res) {
  1152. /* no suitable interface, frame not sent */
  1153. dev_kfree_skb(skb);
  1154. }
  1155. read_unlock(&bond->curr_slave_lock);
  1156. read_unlock(&bond->lock);
  1157. return 0;
  1158. }
  1159. void bond_alb_monitor(struct work_struct *work)
  1160. {
  1161. struct bonding *bond = container_of(work, struct bonding,
  1162. alb_work.work);
  1163. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1164. struct slave *slave;
  1165. int i;
  1166. read_lock(&bond->lock);
  1167. if (bond->kill_timers) {
  1168. goto out;
  1169. }
  1170. if (bond->slave_cnt == 0) {
  1171. bond_info->tx_rebalance_counter = 0;
  1172. bond_info->lp_counter = 0;
  1173. goto re_arm;
  1174. }
  1175. bond_info->tx_rebalance_counter++;
  1176. bond_info->lp_counter++;
  1177. /* send learning packets */
  1178. if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
  1179. /* change of curr_active_slave involves swapping of mac addresses.
  1180. * in order to avoid this swapping from happening while
  1181. * sending the learning packets, the curr_slave_lock must be held for
  1182. * read.
  1183. */
  1184. read_lock(&bond->curr_slave_lock);
  1185. bond_for_each_slave(bond, slave, i) {
  1186. alb_send_learning_packets(slave, slave->dev->dev_addr);
  1187. }
  1188. read_unlock(&bond->curr_slave_lock);
  1189. bond_info->lp_counter = 0;
  1190. }
  1191. /* rebalance tx traffic */
  1192. if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
  1193. read_lock(&bond->curr_slave_lock);
  1194. bond_for_each_slave(bond, slave, i) {
  1195. tlb_clear_slave(bond, slave, 1);
  1196. if (slave == bond->curr_active_slave) {
  1197. SLAVE_TLB_INFO(slave).load =
  1198. bond_info->unbalanced_load /
  1199. BOND_TLB_REBALANCE_INTERVAL;
  1200. bond_info->unbalanced_load = 0;
  1201. }
  1202. }
  1203. read_unlock(&bond->curr_slave_lock);
  1204. bond_info->tx_rebalance_counter = 0;
  1205. }
  1206. /* handle rlb stuff */
  1207. if (bond_info->rlb_enabled) {
  1208. if (bond_info->primary_is_promisc &&
  1209. (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
  1210. /*
  1211. * dev_set_promiscuity requires rtnl and
  1212. * nothing else.
  1213. */
  1214. read_unlock(&bond->lock);
  1215. rtnl_lock();
  1216. bond_info->rlb_promisc_timeout_counter = 0;
  1217. /* If the primary was set to promiscuous mode
  1218. * because a slave was disabled then
  1219. * it can now leave promiscuous mode.
  1220. */
  1221. dev_set_promiscuity(bond->curr_active_slave->dev, -1);
  1222. bond_info->primary_is_promisc = 0;
  1223. rtnl_unlock();
  1224. read_lock(&bond->lock);
  1225. }
  1226. if (bond_info->rlb_rebalance) {
  1227. bond_info->rlb_rebalance = 0;
  1228. rlb_rebalance(bond);
  1229. }
  1230. /* check if clients need updating */
  1231. if (bond_info->rx_ntt) {
  1232. if (bond_info->rlb_update_delay_counter) {
  1233. --bond_info->rlb_update_delay_counter;
  1234. } else {
  1235. rlb_update_rx_clients(bond);
  1236. if (bond_info->rlb_update_retry_counter) {
  1237. --bond_info->rlb_update_retry_counter;
  1238. } else {
  1239. bond_info->rx_ntt = 0;
  1240. }
  1241. }
  1242. }
  1243. }
  1244. re_arm:
  1245. queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
  1246. out:
  1247. read_unlock(&bond->lock);
  1248. }
  1249. /* assumption: called before the slave is attached to the bond
  1250. * and not locked by the bond lock
  1251. */
  1252. int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
  1253. {
  1254. int res;
  1255. res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
  1256. bond->alb_info.rlb_enabled);
  1257. if (res) {
  1258. return res;
  1259. }
  1260. /* caller must hold the bond lock for write since the mac addresses
  1261. * are compared and may be swapped.
  1262. */
  1263. read_lock(&bond->lock);
  1264. res = alb_handle_addr_collision_on_attach(bond, slave);
  1265. read_unlock(&bond->lock);
  1266. if (res) {
  1267. return res;
  1268. }
  1269. tlb_init_slave(slave);
  1270. /* order a rebalance ASAP */
  1271. bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1272. if (bond->alb_info.rlb_enabled) {
  1273. bond->alb_info.rlb_rebalance = 1;
  1274. }
  1275. return 0;
  1276. }
  1277. /*
  1278. * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
  1279. * if necessary.
  1280. *
  1281. * Caller must hold RTNL and no other locks
  1282. */
  1283. void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
  1284. {
  1285. if (bond->slave_cnt > 1) {
  1286. alb_change_hw_addr_on_detach(bond, slave);
  1287. }
  1288. tlb_clear_slave(bond, slave, 0);
  1289. if (bond->alb_info.rlb_enabled) {
  1290. bond->alb_info.next_rx_slave = NULL;
  1291. rlb_clear_slave(bond, slave);
  1292. }
  1293. }
  1294. /* Caller must hold bond lock for read */
  1295. void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
  1296. {
  1297. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1298. if (link == BOND_LINK_DOWN) {
  1299. tlb_clear_slave(bond, slave, 0);
  1300. if (bond->alb_info.rlb_enabled) {
  1301. rlb_clear_slave(bond, slave);
  1302. }
  1303. } else if (link == BOND_LINK_UP) {
  1304. /* order a rebalance ASAP */
  1305. bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1306. if (bond->alb_info.rlb_enabled) {
  1307. bond->alb_info.rlb_rebalance = 1;
  1308. /* If the updelay module parameter is smaller than the
  1309. * forwarding delay of the switch the rebalance will
  1310. * not work because the rebalance arp replies will
  1311. * not be forwarded to the clients..
  1312. */
  1313. }
  1314. }
  1315. }
  1316. /**
  1317. * bond_alb_handle_active_change - assign new curr_active_slave
  1318. * @bond: our bonding struct
  1319. * @new_slave: new slave to assign
  1320. *
  1321. * Set the bond->curr_active_slave to @new_slave and handle
  1322. * mac address swapping and promiscuity changes as needed.
  1323. *
  1324. * If new_slave is NULL, caller must hold curr_slave_lock or
  1325. * bond->lock for write.
  1326. *
  1327. * If new_slave is not NULL, caller must hold RTNL, bond->lock for
  1328. * read and curr_slave_lock for write. Processing here may sleep, so
  1329. * no other locks may be held.
  1330. */
  1331. void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
  1332. {
  1333. struct slave *swap_slave;
  1334. int i;
  1335. if (bond->curr_active_slave == new_slave) {
  1336. return;
  1337. }
  1338. if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
  1339. dev_set_promiscuity(bond->curr_active_slave->dev, -1);
  1340. bond->alb_info.primary_is_promisc = 0;
  1341. bond->alb_info.rlb_promisc_timeout_counter = 0;
  1342. }
  1343. swap_slave = bond->curr_active_slave;
  1344. bond->curr_active_slave = new_slave;
  1345. if (!new_slave || (bond->slave_cnt == 0)) {
  1346. return;
  1347. }
  1348. /* set the new curr_active_slave to the bonds mac address
  1349. * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
  1350. */
  1351. if (!swap_slave) {
  1352. struct slave *tmp_slave;
  1353. /* find slave that is holding the bond's mac address */
  1354. bond_for_each_slave(bond, tmp_slave, i) {
  1355. if (!memcmp(tmp_slave->dev->dev_addr,
  1356. bond->dev->dev_addr, ETH_ALEN)) {
  1357. swap_slave = tmp_slave;
  1358. break;
  1359. }
  1360. }
  1361. }
  1362. /*
  1363. * Arrange for swap_slave and new_slave to temporarily be
  1364. * ignored so we can mess with their MAC addresses without
  1365. * fear of interference from transmit activity.
  1366. */
  1367. if (swap_slave) {
  1368. tlb_clear_slave(bond, swap_slave, 1);
  1369. }
  1370. tlb_clear_slave(bond, new_slave, 1);
  1371. write_unlock_bh(&bond->curr_slave_lock);
  1372. read_unlock(&bond->lock);
  1373. ASSERT_RTNL();
  1374. /* curr_active_slave must be set before calling alb_swap_mac_addr */
  1375. if (swap_slave) {
  1376. /* swap mac address */
  1377. alb_swap_mac_addr(bond, swap_slave, new_slave);
  1378. } else {
  1379. /* set the new_slave to the bond mac address */
  1380. alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
  1381. bond->alb_info.rlb_enabled);
  1382. }
  1383. if (swap_slave) {
  1384. alb_fasten_mac_swap(bond, swap_slave, new_slave);
  1385. read_lock(&bond->lock);
  1386. } else {
  1387. read_lock(&bond->lock);
  1388. alb_send_learning_packets(new_slave, bond->dev->dev_addr);
  1389. }
  1390. write_lock_bh(&bond->curr_slave_lock);
  1391. }
  1392. /*
  1393. * Called with RTNL
  1394. */
  1395. int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
  1396. {
  1397. struct bonding *bond = bond_dev->priv;
  1398. struct sockaddr *sa = addr;
  1399. struct slave *slave, *swap_slave;
  1400. int res;
  1401. int i;
  1402. if (!is_valid_ether_addr(sa->sa_data)) {
  1403. return -EADDRNOTAVAIL;
  1404. }
  1405. res = alb_set_mac_address(bond, addr);
  1406. if (res) {
  1407. return res;
  1408. }
  1409. memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
  1410. /* If there is no curr_active_slave there is nothing else to do.
  1411. * Otherwise we'll need to pass the new address to it and handle
  1412. * duplications.
  1413. */
  1414. if (!bond->curr_active_slave) {
  1415. return 0;
  1416. }
  1417. swap_slave = NULL;
  1418. bond_for_each_slave(bond, slave, i) {
  1419. if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
  1420. swap_slave = slave;
  1421. break;
  1422. }
  1423. }
  1424. write_unlock_bh(&bond->curr_slave_lock);
  1425. read_unlock(&bond->lock);
  1426. if (swap_slave) {
  1427. alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
  1428. alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
  1429. } else {
  1430. alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
  1431. bond->alb_info.rlb_enabled);
  1432. alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
  1433. if (bond->alb_info.rlb_enabled) {
  1434. /* inform clients mac address has changed */
  1435. rlb_req_update_slave_clients(bond, bond->curr_active_slave);
  1436. }
  1437. }
  1438. read_lock(&bond->lock);
  1439. write_lock_bh(&bond->curr_slave_lock);
  1440. return 0;
  1441. }
  1442. void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  1443. {
  1444. if (bond->alb_info.current_alb_vlan &&
  1445. (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
  1446. bond->alb_info.current_alb_vlan = NULL;
  1447. }
  1448. if (bond->alb_info.rlb_enabled) {
  1449. rlb_clear_vlan(bond, vlan_id);
  1450. }
  1451. }