bond_alb.c 43 KB

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