bond_alb.c 45 KB

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