bond_alb.c 47 KB

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