bond_alb.c 46 KB

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