bond_alb.c 44 KB

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