bond_alb.c 47 KB

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