bond_alb.c 48 KB

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