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