bond_alb.c 51 KB

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