bond_alb.c 44 KB

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