bond_alb.c 42 KB

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