bond_alb.c 50 KB

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