bond_alb.c 45 KB

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