bond_alb.c 43 KB

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