bond_alb.c 45 KB

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