bond_alb.c 48 KB

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