bond_alb.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /*
  2. * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, 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. ether_addr_copy(arp->mac_src, tx_slave->dev->dev_addr);
  574. netdev_dbg(bond->dev, "Server sent ARP Reply packet\n");
  575. } else if (arp->op_code == htons(ARPOP_REQUEST)) {
  576. /* Create an entry in the rx_hashtbl for this client as a
  577. * place holder.
  578. * When the arp reply is received the entry will be updated
  579. * with the correct unicast address of the client.
  580. */
  581. rlb_choose_channel(skb, bond);
  582. /* The ARP reply packets must be delayed so that
  583. * they can cancel out the influence of the ARP request.
  584. */
  585. bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
  586. /* arp requests are broadcast and are sent on the primary
  587. * the arp request will collapse all clients on the subnet to
  588. * the primary slave. We must register these clients to be
  589. * updated with their assigned mac.
  590. */
  591. rlb_req_update_subnet_clients(bond, arp->ip_src);
  592. netdev_dbg(bond->dev, "Server sent ARP Request packet\n");
  593. }
  594. return tx_slave;
  595. }
  596. static void rlb_rebalance(struct bonding *bond)
  597. {
  598. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  599. struct slave *assigned_slave;
  600. struct rlb_client_info *client_info;
  601. int ntt;
  602. u32 hash_index;
  603. spin_lock_bh(&bond->mode_lock);
  604. ntt = 0;
  605. hash_index = bond_info->rx_hashtbl_used_head;
  606. for (; hash_index != RLB_NULL_INDEX;
  607. hash_index = client_info->used_next) {
  608. client_info = &(bond_info->rx_hashtbl[hash_index]);
  609. assigned_slave = __rlb_next_rx_slave(bond);
  610. if (assigned_slave && (client_info->slave != assigned_slave)) {
  611. client_info->slave = assigned_slave;
  612. client_info->ntt = 1;
  613. ntt = 1;
  614. }
  615. }
  616. /* update the team's flag only after the whole iteration */
  617. if (ntt)
  618. bond_info->rx_ntt = 1;
  619. spin_unlock_bh(&bond->mode_lock);
  620. }
  621. /* Caller must hold mode_lock */
  622. static void rlb_init_table_entry_dst(struct rlb_client_info *entry)
  623. {
  624. entry->used_next = RLB_NULL_INDEX;
  625. entry->used_prev = RLB_NULL_INDEX;
  626. entry->assigned = 0;
  627. entry->slave = NULL;
  628. entry->vlan_id = 0;
  629. }
  630. static void rlb_init_table_entry_src(struct rlb_client_info *entry)
  631. {
  632. entry->src_first = RLB_NULL_INDEX;
  633. entry->src_prev = RLB_NULL_INDEX;
  634. entry->src_next = RLB_NULL_INDEX;
  635. }
  636. static void rlb_init_table_entry(struct rlb_client_info *entry)
  637. {
  638. memset(entry, 0, sizeof(struct rlb_client_info));
  639. rlb_init_table_entry_dst(entry);
  640. rlb_init_table_entry_src(entry);
  641. }
  642. static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index)
  643. {
  644. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  645. u32 next_index = bond_info->rx_hashtbl[index].used_next;
  646. u32 prev_index = bond_info->rx_hashtbl[index].used_prev;
  647. if (index == bond_info->rx_hashtbl_used_head)
  648. bond_info->rx_hashtbl_used_head = next_index;
  649. if (prev_index != RLB_NULL_INDEX)
  650. bond_info->rx_hashtbl[prev_index].used_next = next_index;
  651. if (next_index != RLB_NULL_INDEX)
  652. bond_info->rx_hashtbl[next_index].used_prev = prev_index;
  653. }
  654. /* unlink a rlb hash table entry from the src list */
  655. static void rlb_src_unlink(struct bonding *bond, u32 index)
  656. {
  657. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  658. u32 next_index = bond_info->rx_hashtbl[index].src_next;
  659. u32 prev_index = bond_info->rx_hashtbl[index].src_prev;
  660. bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX;
  661. bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX;
  662. if (next_index != RLB_NULL_INDEX)
  663. bond_info->rx_hashtbl[next_index].src_prev = prev_index;
  664. if (prev_index == RLB_NULL_INDEX)
  665. return;
  666. /* is prev_index pointing to the head of this list? */
  667. if (bond_info->rx_hashtbl[prev_index].src_first == index)
  668. bond_info->rx_hashtbl[prev_index].src_first = next_index;
  669. else
  670. bond_info->rx_hashtbl[prev_index].src_next = next_index;
  671. }
  672. static void rlb_delete_table_entry(struct bonding *bond, u32 index)
  673. {
  674. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  675. struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
  676. rlb_delete_table_entry_dst(bond, index);
  677. rlb_init_table_entry_dst(entry);
  678. rlb_src_unlink(bond, index);
  679. }
  680. /* add the rx_hashtbl[ip_dst_hash] entry to the list
  681. * of entries with identical ip_src_hash
  682. */
  683. static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash)
  684. {
  685. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  686. u32 next;
  687. bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash;
  688. next = bond_info->rx_hashtbl[ip_src_hash].src_first;
  689. bond_info->rx_hashtbl[ip_dst_hash].src_next = next;
  690. if (next != RLB_NULL_INDEX)
  691. bond_info->rx_hashtbl[next].src_prev = ip_dst_hash;
  692. bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash;
  693. }
  694. /* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does
  695. * not match arp->mac_src
  696. */
  697. static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp)
  698. {
  699. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  700. u32 ip_src_hash = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
  701. u32 index;
  702. spin_lock_bh(&bond->mode_lock);
  703. index = bond_info->rx_hashtbl[ip_src_hash].src_first;
  704. while (index != RLB_NULL_INDEX) {
  705. struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
  706. u32 next_index = entry->src_next;
  707. if (entry->ip_src == arp->ip_src &&
  708. !ether_addr_equal_64bits(arp->mac_src, entry->mac_src))
  709. rlb_delete_table_entry(bond, index);
  710. index = next_index;
  711. }
  712. spin_unlock_bh(&bond->mode_lock);
  713. }
  714. static int rlb_initialize(struct bonding *bond)
  715. {
  716. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  717. struct rlb_client_info *new_hashtbl;
  718. int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
  719. int i;
  720. new_hashtbl = kmalloc(size, GFP_KERNEL);
  721. if (!new_hashtbl)
  722. return -1;
  723. spin_lock_bh(&bond->mode_lock);
  724. bond_info->rx_hashtbl = new_hashtbl;
  725. bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
  726. for (i = 0; i < RLB_HASH_TABLE_SIZE; i++)
  727. rlb_init_table_entry(bond_info->rx_hashtbl + i);
  728. spin_unlock_bh(&bond->mode_lock);
  729. /* register to receive ARPs */
  730. bond->recv_probe = rlb_arp_recv;
  731. return 0;
  732. }
  733. static void rlb_deinitialize(struct bonding *bond)
  734. {
  735. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  736. spin_lock_bh(&bond->mode_lock);
  737. kfree(bond_info->rx_hashtbl);
  738. bond_info->rx_hashtbl = NULL;
  739. bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
  740. spin_unlock_bh(&bond->mode_lock);
  741. }
  742. static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  743. {
  744. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  745. u32 curr_index;
  746. spin_lock_bh(&bond->mode_lock);
  747. curr_index = bond_info->rx_hashtbl_used_head;
  748. while (curr_index != RLB_NULL_INDEX) {
  749. struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
  750. u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
  751. if (curr->vlan_id == vlan_id)
  752. rlb_delete_table_entry(bond, curr_index);
  753. curr_index = next_index;
  754. }
  755. spin_unlock_bh(&bond->mode_lock);
  756. }
  757. /*********************** tlb/rlb shared functions *********************/
  758. static void alb_send_lp_vid(struct slave *slave, u8 mac_addr[],
  759. __be16 vlan_proto, u16 vid)
  760. {
  761. struct learning_pkt pkt;
  762. struct sk_buff *skb;
  763. int size = sizeof(struct learning_pkt);
  764. char *data;
  765. memset(&pkt, 0, size);
  766. ether_addr_copy(pkt.mac_dst, mac_addr);
  767. ether_addr_copy(pkt.mac_src, mac_addr);
  768. pkt.type = cpu_to_be16(ETH_P_LOOPBACK);
  769. skb = dev_alloc_skb(size);
  770. if (!skb)
  771. return;
  772. data = skb_put(skb, size);
  773. memcpy(data, &pkt, size);
  774. skb_reset_mac_header(skb);
  775. skb->network_header = skb->mac_header + ETH_HLEN;
  776. skb->protocol = pkt.type;
  777. skb->priority = TC_PRIO_CONTROL;
  778. skb->dev = slave->dev;
  779. if (vid)
  780. __vlan_hwaccel_put_tag(skb, vlan_proto, vid);
  781. dev_queue_xmit(skb);
  782. }
  783. struct alb_walk_data {
  784. struct bonding *bond;
  785. struct slave *slave;
  786. u8 *mac_addr;
  787. bool strict_match;
  788. };
  789. static int alb_upper_dev_walk(struct net_device *upper, void *_data)
  790. {
  791. struct alb_walk_data *data = _data;
  792. bool strict_match = data->strict_match;
  793. struct bonding *bond = data->bond;
  794. struct slave *slave = data->slave;
  795. u8 *mac_addr = data->mac_addr;
  796. struct bond_vlan_tag *tags;
  797. if (is_vlan_dev(upper) && vlan_get_encap_level(upper) == 0) {
  798. if (strict_match &&
  799. ether_addr_equal_64bits(mac_addr,
  800. upper->dev_addr)) {
  801. alb_send_lp_vid(slave, mac_addr,
  802. vlan_dev_vlan_proto(upper),
  803. vlan_dev_vlan_id(upper));
  804. } else if (!strict_match) {
  805. alb_send_lp_vid(slave, upper->dev_addr,
  806. vlan_dev_vlan_proto(upper),
  807. vlan_dev_vlan_id(upper));
  808. }
  809. }
  810. /* If this is a macvlan device, then only send updates
  811. * when strict_match is turned off.
  812. */
  813. if (netif_is_macvlan(upper) && !strict_match) {
  814. tags = bond_verify_device_path(bond->dev, upper, 0);
  815. if (IS_ERR_OR_NULL(tags))
  816. BUG();
  817. alb_send_lp_vid(slave, upper->dev_addr,
  818. tags[0].vlan_proto, tags[0].vlan_id);
  819. kfree(tags);
  820. }
  821. return 0;
  822. }
  823. static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[],
  824. bool strict_match)
  825. {
  826. struct bonding *bond = bond_get_bond_by_slave(slave);
  827. struct alb_walk_data data = {
  828. .strict_match = strict_match,
  829. .mac_addr = mac_addr,
  830. .slave = slave,
  831. .bond = bond,
  832. };
  833. /* send untagged */
  834. alb_send_lp_vid(slave, mac_addr, 0, 0);
  835. /* loop through all devices and see if we need to send a packet
  836. * for that device.
  837. */
  838. rcu_read_lock();
  839. netdev_walk_all_upper_dev_rcu(bond->dev, alb_upper_dev_walk, &data);
  840. rcu_read_unlock();
  841. }
  842. static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
  843. {
  844. struct net_device *dev = slave->dev;
  845. struct sockaddr s_addr;
  846. if (BOND_MODE(slave->bond) == BOND_MODE_TLB) {
  847. memcpy(dev->dev_addr, addr, dev->addr_len);
  848. return 0;
  849. }
  850. /* for rlb each slave must have a unique hw mac addresses so that
  851. * each slave will receive packets destined to a different mac
  852. */
  853. memcpy(s_addr.sa_data, addr, dev->addr_len);
  854. s_addr.sa_family = dev->type;
  855. if (dev_set_mac_address(dev, &s_addr)) {
  856. 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",
  857. dev->name);
  858. return -EOPNOTSUPP;
  859. }
  860. return 0;
  861. }
  862. /* Swap MAC addresses between two slaves.
  863. *
  864. * Called with RTNL held, and no other locks.
  865. */
  866. static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
  867. {
  868. u8 tmp_mac_addr[ETH_ALEN];
  869. ether_addr_copy(tmp_mac_addr, slave1->dev->dev_addr);
  870. alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
  871. alb_set_slave_mac_addr(slave2, tmp_mac_addr);
  872. }
  873. /* Send learning packets after MAC address swap.
  874. *
  875. * Called with RTNL and no other locks
  876. */
  877. static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
  878. struct slave *slave2)
  879. {
  880. int slaves_state_differ = (bond_slave_can_tx(slave1) != bond_slave_can_tx(slave2));
  881. struct slave *disabled_slave = NULL;
  882. ASSERT_RTNL();
  883. /* fasten the change in the switch */
  884. if (bond_slave_can_tx(slave1)) {
  885. alb_send_learning_packets(slave1, slave1->dev->dev_addr, false);
  886. if (bond->alb_info.rlb_enabled) {
  887. /* inform the clients that the mac address
  888. * has changed
  889. */
  890. rlb_req_update_slave_clients(bond, slave1);
  891. }
  892. } else {
  893. disabled_slave = slave1;
  894. }
  895. if (bond_slave_can_tx(slave2)) {
  896. alb_send_learning_packets(slave2, slave2->dev->dev_addr, false);
  897. if (bond->alb_info.rlb_enabled) {
  898. /* inform the clients that the mac address
  899. * has changed
  900. */
  901. rlb_req_update_slave_clients(bond, slave2);
  902. }
  903. } else {
  904. disabled_slave = slave2;
  905. }
  906. if (bond->alb_info.rlb_enabled && slaves_state_differ) {
  907. /* A disabled slave was assigned an active mac addr */
  908. rlb_teach_disabled_mac_on_primary(bond,
  909. disabled_slave->dev->dev_addr);
  910. }
  911. }
  912. /**
  913. * alb_change_hw_addr_on_detach
  914. * @bond: bonding we're working on
  915. * @slave: the slave that was just detached
  916. *
  917. * We assume that @slave was already detached from the slave list.
  918. *
  919. * If @slave's permanent hw address is different both from its current
  920. * address and from @bond's address, then somewhere in the bond there's
  921. * a slave that has @slave's permanet address as its current address.
  922. * We'll make sure that that slave no longer uses @slave's permanent address.
  923. *
  924. * Caller must hold RTNL and no other locks
  925. */
  926. static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
  927. {
  928. int perm_curr_diff;
  929. int perm_bond_diff;
  930. struct slave *found_slave;
  931. perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
  932. slave->dev->dev_addr);
  933. perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
  934. bond->dev->dev_addr);
  935. if (perm_curr_diff && perm_bond_diff) {
  936. found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr);
  937. if (found_slave) {
  938. alb_swap_mac_addr(slave, found_slave);
  939. alb_fasten_mac_swap(bond, slave, found_slave);
  940. }
  941. }
  942. }
  943. /**
  944. * alb_handle_addr_collision_on_attach
  945. * @bond: bonding we're working on
  946. * @slave: the slave that was just attached
  947. *
  948. * checks uniqueness of slave's mac address and handles the case the
  949. * new slave uses the bonds mac address.
  950. *
  951. * If the permanent hw address of @slave is @bond's hw address, we need to
  952. * find a different hw address to give @slave, that isn't in use by any other
  953. * slave in the bond. This address must be, of course, one of the permanent
  954. * addresses of the other slaves.
  955. *
  956. * We go over the slave list, and for each slave there we compare its
  957. * permanent hw address with the current address of all the other slaves.
  958. * If no match was found, then we've found a slave with a permanent address
  959. * that isn't used by any other slave in the bond, so we can assign it to
  960. * @slave.
  961. *
  962. * assumption: this function is called before @slave is attached to the
  963. * bond slave list.
  964. */
  965. static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
  966. {
  967. struct slave *has_bond_addr = rcu_access_pointer(bond->curr_active_slave);
  968. struct slave *tmp_slave1, *free_mac_slave = NULL;
  969. struct list_head *iter;
  970. if (!bond_has_slaves(bond)) {
  971. /* this is the first slave */
  972. return 0;
  973. }
  974. /* if slave's mac address differs from bond's mac address
  975. * check uniqueness of slave's mac address against the other
  976. * slaves in the bond.
  977. */
  978. if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
  979. if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
  980. return 0;
  981. /* Try setting slave mac to bond address and fall-through
  982. * to code handling that situation below...
  983. */
  984. alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
  985. }
  986. /* The slave's address is equal to the address of the bond.
  987. * Search for a spare address in the bond for this slave.
  988. */
  989. bond_for_each_slave(bond, tmp_slave1, iter) {
  990. if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
  991. /* no slave has tmp_slave1's perm addr
  992. * as its curr addr
  993. */
  994. free_mac_slave = tmp_slave1;
  995. break;
  996. }
  997. if (!has_bond_addr) {
  998. if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
  999. bond->dev->dev_addr)) {
  1000. has_bond_addr = tmp_slave1;
  1001. }
  1002. }
  1003. }
  1004. if (free_mac_slave) {
  1005. alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
  1006. netdev_warn(bond->dev, "the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
  1007. slave->dev->name, free_mac_slave->dev->name);
  1008. } else if (has_bond_addr) {
  1009. 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",
  1010. slave->dev->name);
  1011. return -EFAULT;
  1012. }
  1013. return 0;
  1014. }
  1015. /**
  1016. * alb_set_mac_address
  1017. * @bond:
  1018. * @addr:
  1019. *
  1020. * In TLB mode all slaves are configured to the bond's hw address, but set
  1021. * their dev_addr field to different addresses (based on their permanent hw
  1022. * addresses).
  1023. *
  1024. * For each slave, this function sets the interface to the new address and then
  1025. * changes its dev_addr field to its previous value.
  1026. *
  1027. * Unwinding assumes bond's mac address has not yet changed.
  1028. */
  1029. static int alb_set_mac_address(struct bonding *bond, void *addr)
  1030. {
  1031. struct slave *slave, *rollback_slave;
  1032. struct list_head *iter;
  1033. struct sockaddr sa;
  1034. char tmp_addr[ETH_ALEN];
  1035. int res;
  1036. if (bond->alb_info.rlb_enabled)
  1037. return 0;
  1038. bond_for_each_slave(bond, slave, iter) {
  1039. /* save net_device's current hw address */
  1040. ether_addr_copy(tmp_addr, slave->dev->dev_addr);
  1041. res = dev_set_mac_address(slave->dev, addr);
  1042. /* restore net_device's hw address */
  1043. ether_addr_copy(slave->dev->dev_addr, tmp_addr);
  1044. if (res)
  1045. goto unwind;
  1046. }
  1047. return 0;
  1048. unwind:
  1049. memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
  1050. sa.sa_family = bond->dev->type;
  1051. /* unwind from head to the slave that failed */
  1052. bond_for_each_slave(bond, rollback_slave, iter) {
  1053. if (rollback_slave == slave)
  1054. break;
  1055. ether_addr_copy(tmp_addr, rollback_slave->dev->dev_addr);
  1056. dev_set_mac_address(rollback_slave->dev, &sa);
  1057. ether_addr_copy(rollback_slave->dev->dev_addr, tmp_addr);
  1058. }
  1059. return res;
  1060. }
  1061. /************************ exported alb funcions ************************/
  1062. int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
  1063. {
  1064. int res;
  1065. res = tlb_initialize(bond);
  1066. if (res)
  1067. return res;
  1068. if (rlb_enabled) {
  1069. bond->alb_info.rlb_enabled = 1;
  1070. res = rlb_initialize(bond);
  1071. if (res) {
  1072. tlb_deinitialize(bond);
  1073. return res;
  1074. }
  1075. } else {
  1076. bond->alb_info.rlb_enabled = 0;
  1077. }
  1078. return 0;
  1079. }
  1080. void bond_alb_deinitialize(struct bonding *bond)
  1081. {
  1082. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1083. tlb_deinitialize(bond);
  1084. if (bond_info->rlb_enabled)
  1085. rlb_deinitialize(bond);
  1086. }
  1087. static int bond_do_alb_xmit(struct sk_buff *skb, struct bonding *bond,
  1088. struct slave *tx_slave)
  1089. {
  1090. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1091. struct ethhdr *eth_data = eth_hdr(skb);
  1092. if (!tx_slave) {
  1093. /* unbalanced or unassigned, send through primary */
  1094. tx_slave = rcu_dereference(bond->curr_active_slave);
  1095. if (bond->params.tlb_dynamic_lb)
  1096. bond_info->unbalanced_load += skb->len;
  1097. }
  1098. if (tx_slave && bond_slave_can_tx(tx_slave)) {
  1099. if (tx_slave != rcu_access_pointer(bond->curr_active_slave)) {
  1100. ether_addr_copy(eth_data->h_source,
  1101. tx_slave->dev->dev_addr);
  1102. }
  1103. bond_dev_queue_xmit(bond, skb, tx_slave->dev);
  1104. goto out;
  1105. }
  1106. if (tx_slave && bond->params.tlb_dynamic_lb) {
  1107. spin_lock(&bond->mode_lock);
  1108. __tlb_clear_slave(bond, tx_slave, 0);
  1109. spin_unlock(&bond->mode_lock);
  1110. }
  1111. /* no suitable interface, frame not sent */
  1112. bond_tx_drop(bond->dev, skb);
  1113. out:
  1114. return NETDEV_TX_OK;
  1115. }
  1116. int bond_tlb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
  1117. {
  1118. struct bonding *bond = netdev_priv(bond_dev);
  1119. struct ethhdr *eth_data;
  1120. struct slave *tx_slave = NULL;
  1121. u32 hash_index;
  1122. skb_reset_mac_header(skb);
  1123. eth_data = eth_hdr(skb);
  1124. /* Do not TX balance any multicast or broadcast */
  1125. if (!is_multicast_ether_addr(eth_data->h_dest)) {
  1126. switch (skb->protocol) {
  1127. case htons(ETH_P_IP):
  1128. case htons(ETH_P_IPX):
  1129. /* In case of IPX, it will falback to L2 hash */
  1130. case htons(ETH_P_IPV6):
  1131. hash_index = bond_xmit_hash(bond, skb);
  1132. if (bond->params.tlb_dynamic_lb) {
  1133. tx_slave = tlb_choose_channel(bond,
  1134. hash_index & 0xFF,
  1135. skb->len);
  1136. } else {
  1137. struct bond_up_slave *slaves;
  1138. unsigned int count;
  1139. slaves = rcu_dereference(bond->slave_arr);
  1140. count = slaves ? ACCESS_ONCE(slaves->count) : 0;
  1141. if (likely(count))
  1142. tx_slave = slaves->arr[hash_index %
  1143. count];
  1144. }
  1145. break;
  1146. }
  1147. }
  1148. return bond_do_alb_xmit(skb, bond, tx_slave);
  1149. }
  1150. int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
  1151. {
  1152. struct bonding *bond = netdev_priv(bond_dev);
  1153. struct ethhdr *eth_data;
  1154. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1155. struct slave *tx_slave = NULL;
  1156. static const __be32 ip_bcast = htonl(0xffffffff);
  1157. int hash_size = 0;
  1158. bool do_tx_balance = true;
  1159. u32 hash_index = 0;
  1160. const u8 *hash_start = NULL;
  1161. struct ipv6hdr *ip6hdr;
  1162. skb_reset_mac_header(skb);
  1163. eth_data = eth_hdr(skb);
  1164. switch (ntohs(skb->protocol)) {
  1165. case ETH_P_IP: {
  1166. const struct iphdr *iph = ip_hdr(skb);
  1167. if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
  1168. (iph->daddr == ip_bcast) ||
  1169. (iph->protocol == IPPROTO_IGMP)) {
  1170. do_tx_balance = false;
  1171. break;
  1172. }
  1173. hash_start = (char *)&(iph->daddr);
  1174. hash_size = sizeof(iph->daddr);
  1175. }
  1176. break;
  1177. case ETH_P_IPV6:
  1178. /* IPv6 doesn't really use broadcast mac address, but leave
  1179. * that here just in case.
  1180. */
  1181. if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast)) {
  1182. do_tx_balance = false;
  1183. break;
  1184. }
  1185. /* IPv6 uses all-nodes multicast as an equivalent to
  1186. * broadcasts in IPv4.
  1187. */
  1188. if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
  1189. do_tx_balance = false;
  1190. break;
  1191. }
  1192. /* Additianally, DAD probes should not be tx-balanced as that
  1193. * will lead to false positives for duplicate addresses and
  1194. * prevent address configuration from working.
  1195. */
  1196. ip6hdr = ipv6_hdr(skb);
  1197. if (ipv6_addr_any(&ip6hdr->saddr)) {
  1198. do_tx_balance = false;
  1199. break;
  1200. }
  1201. hash_start = (char *)&(ipv6_hdr(skb)->daddr);
  1202. hash_size = sizeof(ipv6_hdr(skb)->daddr);
  1203. break;
  1204. case ETH_P_IPX:
  1205. if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
  1206. /* something is wrong with this packet */
  1207. do_tx_balance = false;
  1208. break;
  1209. }
  1210. if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
  1211. /* The only protocol worth balancing in
  1212. * this family since it has an "ARP" like
  1213. * mechanism
  1214. */
  1215. do_tx_balance = false;
  1216. break;
  1217. }
  1218. hash_start = (char *)eth_data->h_dest;
  1219. hash_size = ETH_ALEN;
  1220. break;
  1221. case ETH_P_ARP:
  1222. do_tx_balance = false;
  1223. if (bond_info->rlb_enabled)
  1224. tx_slave = rlb_arp_xmit(skb, bond);
  1225. break;
  1226. default:
  1227. do_tx_balance = false;
  1228. break;
  1229. }
  1230. if (do_tx_balance) {
  1231. hash_index = _simple_hash(hash_start, hash_size);
  1232. tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
  1233. }
  1234. return bond_do_alb_xmit(skb, bond, tx_slave);
  1235. }
  1236. void bond_alb_monitor(struct work_struct *work)
  1237. {
  1238. struct bonding *bond = container_of(work, struct bonding,
  1239. alb_work.work);
  1240. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1241. struct list_head *iter;
  1242. struct slave *slave;
  1243. if (!bond_has_slaves(bond)) {
  1244. bond_info->tx_rebalance_counter = 0;
  1245. bond_info->lp_counter = 0;
  1246. goto re_arm;
  1247. }
  1248. rcu_read_lock();
  1249. bond_info->tx_rebalance_counter++;
  1250. bond_info->lp_counter++;
  1251. /* send learning packets */
  1252. if (bond_info->lp_counter >= BOND_ALB_LP_TICKS(bond)) {
  1253. bool strict_match;
  1254. bond_for_each_slave_rcu(bond, slave, iter) {
  1255. /* If updating current_active, use all currently
  1256. * user mac addreses (!strict_match). Otherwise, only
  1257. * use mac of the slave device.
  1258. * In RLB mode, we always use strict matches.
  1259. */
  1260. strict_match = (slave != rcu_access_pointer(bond->curr_active_slave) ||
  1261. bond_info->rlb_enabled);
  1262. alb_send_learning_packets(slave, slave->dev->dev_addr,
  1263. strict_match);
  1264. }
  1265. bond_info->lp_counter = 0;
  1266. }
  1267. /* rebalance tx traffic */
  1268. if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
  1269. bond_for_each_slave_rcu(bond, slave, iter) {
  1270. tlb_clear_slave(bond, slave, 1);
  1271. if (slave == rcu_access_pointer(bond->curr_active_slave)) {
  1272. SLAVE_TLB_INFO(slave).load =
  1273. bond_info->unbalanced_load /
  1274. BOND_TLB_REBALANCE_INTERVAL;
  1275. bond_info->unbalanced_load = 0;
  1276. }
  1277. }
  1278. bond_info->tx_rebalance_counter = 0;
  1279. }
  1280. if (bond_info->rlb_enabled) {
  1281. if (bond_info->primary_is_promisc &&
  1282. (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
  1283. /* dev_set_promiscuity requires rtnl and
  1284. * nothing else. Avoid race with bond_close.
  1285. */
  1286. rcu_read_unlock();
  1287. if (!rtnl_trylock())
  1288. goto re_arm;
  1289. bond_info->rlb_promisc_timeout_counter = 0;
  1290. /* If the primary was set to promiscuous mode
  1291. * because a slave was disabled then
  1292. * it can now leave promiscuous mode.
  1293. */
  1294. dev_set_promiscuity(rtnl_dereference(bond->curr_active_slave)->dev,
  1295. -1);
  1296. bond_info->primary_is_promisc = 0;
  1297. rtnl_unlock();
  1298. rcu_read_lock();
  1299. }
  1300. if (bond_info->rlb_rebalance) {
  1301. bond_info->rlb_rebalance = 0;
  1302. rlb_rebalance(bond);
  1303. }
  1304. /* check if clients need updating */
  1305. if (bond_info->rx_ntt) {
  1306. if (bond_info->rlb_update_delay_counter) {
  1307. --bond_info->rlb_update_delay_counter;
  1308. } else {
  1309. rlb_update_rx_clients(bond);
  1310. if (bond_info->rlb_update_retry_counter)
  1311. --bond_info->rlb_update_retry_counter;
  1312. else
  1313. bond_info->rx_ntt = 0;
  1314. }
  1315. }
  1316. }
  1317. rcu_read_unlock();
  1318. re_arm:
  1319. queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
  1320. }
  1321. /* assumption: called before the slave is attached to the bond
  1322. * and not locked by the bond lock
  1323. */
  1324. int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
  1325. {
  1326. int res;
  1327. res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
  1328. if (res)
  1329. return res;
  1330. res = alb_handle_addr_collision_on_attach(bond, slave);
  1331. if (res)
  1332. return res;
  1333. tlb_init_slave(slave);
  1334. /* order a rebalance ASAP */
  1335. bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1336. if (bond->alb_info.rlb_enabled)
  1337. bond->alb_info.rlb_rebalance = 1;
  1338. return 0;
  1339. }
  1340. /* Remove slave from tlb and rlb hash tables, and fix up MAC addresses
  1341. * if necessary.
  1342. *
  1343. * Caller must hold RTNL and no other locks
  1344. */
  1345. void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
  1346. {
  1347. if (bond_has_slaves(bond))
  1348. alb_change_hw_addr_on_detach(bond, slave);
  1349. tlb_clear_slave(bond, slave, 0);
  1350. if (bond->alb_info.rlb_enabled) {
  1351. bond->alb_info.rx_slave = NULL;
  1352. rlb_clear_slave(bond, slave);
  1353. }
  1354. }
  1355. void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
  1356. {
  1357. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1358. if (link == BOND_LINK_DOWN) {
  1359. tlb_clear_slave(bond, slave, 0);
  1360. if (bond->alb_info.rlb_enabled)
  1361. rlb_clear_slave(bond, slave);
  1362. } else if (link == BOND_LINK_UP) {
  1363. /* order a rebalance ASAP */
  1364. bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1365. if (bond->alb_info.rlb_enabled) {
  1366. bond->alb_info.rlb_rebalance = 1;
  1367. /* If the updelay module parameter is smaller than the
  1368. * forwarding delay of the switch the rebalance will
  1369. * not work because the rebalance arp replies will
  1370. * not be forwarded to the clients..
  1371. */
  1372. }
  1373. }
  1374. if (bond_is_nondyn_tlb(bond)) {
  1375. if (bond_update_slave_arr(bond, NULL))
  1376. pr_err("Failed to build slave-array for TLB mode.\n");
  1377. }
  1378. }
  1379. /**
  1380. * bond_alb_handle_active_change - assign new curr_active_slave
  1381. * @bond: our bonding struct
  1382. * @new_slave: new slave to assign
  1383. *
  1384. * Set the bond->curr_active_slave to @new_slave and handle
  1385. * mac address swapping and promiscuity changes as needed.
  1386. *
  1387. * Caller must hold RTNL
  1388. */
  1389. void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
  1390. {
  1391. struct slave *swap_slave;
  1392. struct slave *curr_active;
  1393. curr_active = rtnl_dereference(bond->curr_active_slave);
  1394. if (curr_active == new_slave)
  1395. return;
  1396. if (curr_active && bond->alb_info.primary_is_promisc) {
  1397. dev_set_promiscuity(curr_active->dev, -1);
  1398. bond->alb_info.primary_is_promisc = 0;
  1399. bond->alb_info.rlb_promisc_timeout_counter = 0;
  1400. }
  1401. swap_slave = curr_active;
  1402. rcu_assign_pointer(bond->curr_active_slave, new_slave);
  1403. if (!new_slave || !bond_has_slaves(bond))
  1404. return;
  1405. /* set the new curr_active_slave to the bonds mac address
  1406. * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
  1407. */
  1408. if (!swap_slave)
  1409. swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
  1410. /* Arrange for swap_slave and new_slave to temporarily be
  1411. * ignored so we can mess with their MAC addresses without
  1412. * fear of interference from transmit activity.
  1413. */
  1414. if (swap_slave)
  1415. tlb_clear_slave(bond, swap_slave, 1);
  1416. tlb_clear_slave(bond, new_slave, 1);
  1417. /* in TLB mode, the slave might flip down/up with the old dev_addr,
  1418. * and thus filter bond->dev_addr's packets, so force bond's mac
  1419. */
  1420. if (BOND_MODE(bond) == BOND_MODE_TLB) {
  1421. struct sockaddr sa;
  1422. u8 tmp_addr[ETH_ALEN];
  1423. ether_addr_copy(tmp_addr, new_slave->dev->dev_addr);
  1424. memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
  1425. sa.sa_family = bond->dev->type;
  1426. /* we don't care if it can't change its mac, best effort */
  1427. dev_set_mac_address(new_slave->dev, &sa);
  1428. ether_addr_copy(new_slave->dev->dev_addr, tmp_addr);
  1429. }
  1430. /* curr_active_slave must be set before calling alb_swap_mac_addr */
  1431. if (swap_slave) {
  1432. /* swap mac address */
  1433. alb_swap_mac_addr(swap_slave, new_slave);
  1434. alb_fasten_mac_swap(bond, swap_slave, new_slave);
  1435. } else {
  1436. /* set the new_slave to the bond mac address */
  1437. alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
  1438. alb_send_learning_packets(new_slave, bond->dev->dev_addr,
  1439. false);
  1440. }
  1441. }
  1442. /* Called with RTNL */
  1443. int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
  1444. {
  1445. struct bonding *bond = netdev_priv(bond_dev);
  1446. struct sockaddr *sa = addr;
  1447. struct slave *curr_active;
  1448. struct slave *swap_slave;
  1449. int res;
  1450. if (!is_valid_ether_addr(sa->sa_data))
  1451. return -EADDRNOTAVAIL;
  1452. res = alb_set_mac_address(bond, addr);
  1453. if (res)
  1454. return res;
  1455. memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
  1456. /* If there is no curr_active_slave there is nothing else to do.
  1457. * Otherwise we'll need to pass the new address to it and handle
  1458. * duplications.
  1459. */
  1460. curr_active = rtnl_dereference(bond->curr_active_slave);
  1461. if (!curr_active)
  1462. return 0;
  1463. swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
  1464. if (swap_slave) {
  1465. alb_swap_mac_addr(swap_slave, curr_active);
  1466. alb_fasten_mac_swap(bond, swap_slave, curr_active);
  1467. } else {
  1468. alb_set_slave_mac_addr(curr_active, bond_dev->dev_addr);
  1469. alb_send_learning_packets(curr_active,
  1470. bond_dev->dev_addr, false);
  1471. if (bond->alb_info.rlb_enabled) {
  1472. /* inform clients mac address has changed */
  1473. rlb_req_update_slave_clients(bond, curr_active);
  1474. }
  1475. }
  1476. return 0;
  1477. }
  1478. void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  1479. {
  1480. if (bond->alb_info.rlb_enabled)
  1481. rlb_clear_vlan(bond, vlan_id);
  1482. }