bond_alb.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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_learning_packets(struct slave *slave, u8 mac_addr[])
  808. {
  809. struct bonding *bond = bond_get_bond_by_slave(slave);
  810. struct learning_pkt pkt;
  811. int size = sizeof(struct learning_pkt);
  812. int i;
  813. memset(&pkt, 0, size);
  814. memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
  815. memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
  816. pkt.type = cpu_to_be16(ETH_P_LOOP);
  817. for (i = 0; i < MAX_LP_BURST; i++) {
  818. struct sk_buff *skb;
  819. char *data;
  820. skb = dev_alloc_skb(size);
  821. if (!skb) {
  822. return;
  823. }
  824. data = skb_put(skb, size);
  825. memcpy(data, &pkt, size);
  826. skb_reset_mac_header(skb);
  827. skb->network_header = skb->mac_header + ETH_HLEN;
  828. skb->protocol = pkt.type;
  829. skb->priority = TC_PRIO_CONTROL;
  830. skb->dev = slave->dev;
  831. if (bond_vlan_used(bond)) {
  832. struct vlan_entry *vlan;
  833. vlan = bond_next_vlan(bond,
  834. bond->alb_info.current_alb_vlan);
  835. bond->alb_info.current_alb_vlan = vlan;
  836. if (!vlan) {
  837. kfree_skb(skb);
  838. continue;
  839. }
  840. skb = vlan_put_tag(skb, htons(ETH_P_8021Q), vlan->vlan_id);
  841. if (!skb) {
  842. pr_err("%s: Error: failed to insert VLAN tag\n",
  843. bond->dev->name);
  844. continue;
  845. }
  846. }
  847. dev_queue_xmit(skb);
  848. }
  849. }
  850. static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
  851. {
  852. struct net_device *dev = slave->dev;
  853. struct sockaddr s_addr;
  854. if (slave->bond->params.mode == BOND_MODE_TLB) {
  855. memcpy(dev->dev_addr, addr, dev->addr_len);
  856. return 0;
  857. }
  858. /* for rlb each slave must have a unique hw mac addresses so that */
  859. /* each slave will receive packets destined to a different mac */
  860. memcpy(s_addr.sa_data, addr, dev->addr_len);
  861. s_addr.sa_family = dev->type;
  862. if (dev_set_mac_address(dev, &s_addr)) {
  863. pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
  864. "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
  865. slave->bond->dev->name, dev->name);
  866. return -EOPNOTSUPP;
  867. }
  868. return 0;
  869. }
  870. /*
  871. * Swap MAC addresses between two slaves.
  872. *
  873. * Called with RTNL held, and no other locks.
  874. *
  875. */
  876. static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
  877. {
  878. u8 tmp_mac_addr[ETH_ALEN];
  879. memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
  880. alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
  881. alb_set_slave_mac_addr(slave2, tmp_mac_addr);
  882. }
  883. /*
  884. * Send learning packets after MAC address swap.
  885. *
  886. * Called with RTNL and no other locks
  887. */
  888. static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
  889. struct slave *slave2)
  890. {
  891. int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
  892. struct slave *disabled_slave = NULL;
  893. ASSERT_RTNL();
  894. /* fasten the change in the switch */
  895. if (SLAVE_IS_OK(slave1)) {
  896. alb_send_learning_packets(slave1, slave1->dev->dev_addr);
  897. if (bond->alb_info.rlb_enabled) {
  898. /* inform the clients that the mac address
  899. * has changed
  900. */
  901. rlb_req_update_slave_clients(bond, slave1);
  902. }
  903. } else {
  904. disabled_slave = slave1;
  905. }
  906. if (SLAVE_IS_OK(slave2)) {
  907. alb_send_learning_packets(slave2, slave2->dev->dev_addr);
  908. if (bond->alb_info.rlb_enabled) {
  909. /* inform the clients that the mac address
  910. * has changed
  911. */
  912. rlb_req_update_slave_clients(bond, slave2);
  913. }
  914. } else {
  915. disabled_slave = slave2;
  916. }
  917. if (bond->alb_info.rlb_enabled && slaves_state_differ) {
  918. /* A disabled slave was assigned an active mac addr */
  919. rlb_teach_disabled_mac_on_primary(bond,
  920. disabled_slave->dev->dev_addr);
  921. }
  922. }
  923. /**
  924. * alb_change_hw_addr_on_detach
  925. * @bond: bonding we're working on
  926. * @slave: the slave that was just detached
  927. *
  928. * We assume that @slave was already detached from the slave list.
  929. *
  930. * If @slave's permanent hw address is different both from its current
  931. * address and from @bond's address, then somewhere in the bond there's
  932. * a slave that has @slave's permanet address as its current address.
  933. * We'll make sure that that slave no longer uses @slave's permanent address.
  934. *
  935. * Caller must hold RTNL and no other locks
  936. */
  937. static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
  938. {
  939. int perm_curr_diff;
  940. int perm_bond_diff;
  941. struct slave *found_slave;
  942. perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
  943. slave->dev->dev_addr);
  944. perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
  945. bond->dev->dev_addr);
  946. if (perm_curr_diff && perm_bond_diff) {
  947. found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr);
  948. if (found_slave) {
  949. /* locking: needs RTNL and nothing else */
  950. alb_swap_mac_addr(slave, found_slave);
  951. alb_fasten_mac_swap(bond, slave, found_slave);
  952. }
  953. }
  954. }
  955. /**
  956. * alb_handle_addr_collision_on_attach
  957. * @bond: bonding we're working on
  958. * @slave: the slave that was just attached
  959. *
  960. * checks uniqueness of slave's mac address and handles the case the
  961. * new slave uses the bonds mac address.
  962. *
  963. * If the permanent hw address of @slave is @bond's hw address, we need to
  964. * find a different hw address to give @slave, that isn't in use by any other
  965. * slave in the bond. This address must be, of course, one of the permanent
  966. * addresses of the other slaves.
  967. *
  968. * We go over the slave list, and for each slave there we compare its
  969. * permanent hw address with the current address of all the other slaves.
  970. * If no match was found, then we've found a slave with a permanent address
  971. * that isn't used by any other slave in the bond, so we can assign it to
  972. * @slave.
  973. *
  974. * assumption: this function is called before @slave is attached to the
  975. * bond slave list.
  976. */
  977. static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
  978. {
  979. struct slave *tmp_slave1, *free_mac_slave = NULL;
  980. struct slave *has_bond_addr = bond->curr_active_slave;
  981. if (list_empty(&bond->slave_list)) {
  982. /* this is the first slave */
  983. return 0;
  984. }
  985. /* if slave's mac address differs from bond's mac address
  986. * check uniqueness of slave's mac address against the other
  987. * slaves in the bond.
  988. */
  989. if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
  990. if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
  991. return 0;
  992. /* Try setting slave mac to bond address and fall-through
  993. to code handling that situation below... */
  994. alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
  995. }
  996. /* The slave's address is equal to the address of the bond.
  997. * Search for a spare address in the bond for this slave.
  998. */
  999. bond_for_each_slave(bond, tmp_slave1) {
  1000. if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
  1001. /* no slave has tmp_slave1's perm addr
  1002. * as its curr addr
  1003. */
  1004. free_mac_slave = tmp_slave1;
  1005. break;
  1006. }
  1007. if (!has_bond_addr) {
  1008. if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
  1009. bond->dev->dev_addr)) {
  1010. has_bond_addr = tmp_slave1;
  1011. }
  1012. }
  1013. }
  1014. if (free_mac_slave) {
  1015. alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
  1016. pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
  1017. bond->dev->name, slave->dev->name,
  1018. free_mac_slave->dev->name);
  1019. } else if (has_bond_addr) {
  1020. 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",
  1021. bond->dev->name, slave->dev->name);
  1022. return -EFAULT;
  1023. }
  1024. return 0;
  1025. }
  1026. /**
  1027. * alb_set_mac_address
  1028. * @bond:
  1029. * @addr:
  1030. *
  1031. * In TLB mode all slaves are configured to the bond's hw address, but set
  1032. * their dev_addr field to different addresses (based on their permanent hw
  1033. * addresses).
  1034. *
  1035. * For each slave, this function sets the interface to the new address and then
  1036. * changes its dev_addr field to its previous value.
  1037. *
  1038. * Unwinding assumes bond's mac address has not yet changed.
  1039. */
  1040. static int alb_set_mac_address(struct bonding *bond, void *addr)
  1041. {
  1042. char tmp_addr[ETH_ALEN];
  1043. struct slave *slave;
  1044. struct sockaddr sa;
  1045. int res;
  1046. if (bond->alb_info.rlb_enabled)
  1047. return 0;
  1048. bond_for_each_slave(bond, slave) {
  1049. /* save net_device's current hw address */
  1050. memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
  1051. res = dev_set_mac_address(slave->dev, addr);
  1052. /* restore net_device's hw address */
  1053. memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
  1054. if (res)
  1055. goto unwind;
  1056. }
  1057. return 0;
  1058. unwind:
  1059. memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
  1060. sa.sa_family = bond->dev->type;
  1061. /* unwind from head to the slave that failed */
  1062. bond_for_each_slave_continue_reverse(bond, slave) {
  1063. memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
  1064. dev_set_mac_address(slave->dev, &sa);
  1065. memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
  1066. }
  1067. return res;
  1068. }
  1069. /************************ exported alb funcions ************************/
  1070. int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
  1071. {
  1072. int res;
  1073. res = tlb_initialize(bond);
  1074. if (res) {
  1075. return res;
  1076. }
  1077. if (rlb_enabled) {
  1078. bond->alb_info.rlb_enabled = 1;
  1079. /* initialize rlb */
  1080. res = rlb_initialize(bond);
  1081. if (res) {
  1082. tlb_deinitialize(bond);
  1083. return res;
  1084. }
  1085. } else {
  1086. bond->alb_info.rlb_enabled = 0;
  1087. }
  1088. return 0;
  1089. }
  1090. void bond_alb_deinitialize(struct bonding *bond)
  1091. {
  1092. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1093. tlb_deinitialize(bond);
  1094. if (bond_info->rlb_enabled) {
  1095. rlb_deinitialize(bond);
  1096. }
  1097. }
  1098. int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
  1099. {
  1100. struct bonding *bond = netdev_priv(bond_dev);
  1101. struct ethhdr *eth_data;
  1102. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1103. struct slave *tx_slave = NULL;
  1104. static const __be32 ip_bcast = htonl(0xffffffff);
  1105. int hash_size = 0;
  1106. int do_tx_balance = 1;
  1107. u32 hash_index = 0;
  1108. const u8 *hash_start = NULL;
  1109. int res = 1;
  1110. struct ipv6hdr *ip6hdr;
  1111. skb_reset_mac_header(skb);
  1112. eth_data = eth_hdr(skb);
  1113. /* make sure that the curr_active_slave do not change during tx
  1114. */
  1115. read_lock(&bond->lock);
  1116. read_lock(&bond->curr_slave_lock);
  1117. switch (ntohs(skb->protocol)) {
  1118. case ETH_P_IP: {
  1119. const struct iphdr *iph = ip_hdr(skb);
  1120. if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
  1121. (iph->daddr == ip_bcast) ||
  1122. (iph->protocol == IPPROTO_IGMP)) {
  1123. do_tx_balance = 0;
  1124. break;
  1125. }
  1126. hash_start = (char *)&(iph->daddr);
  1127. hash_size = sizeof(iph->daddr);
  1128. }
  1129. break;
  1130. case ETH_P_IPV6:
  1131. /* IPv6 doesn't really use broadcast mac address, but leave
  1132. * that here just in case.
  1133. */
  1134. if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast)) {
  1135. do_tx_balance = 0;
  1136. break;
  1137. }
  1138. /* IPv6 uses all-nodes multicast as an equivalent to
  1139. * broadcasts in IPv4.
  1140. */
  1141. if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
  1142. do_tx_balance = 0;
  1143. break;
  1144. }
  1145. /* Additianally, DAD probes should not be tx-balanced as that
  1146. * will lead to false positives for duplicate addresses and
  1147. * prevent address configuration from working.
  1148. */
  1149. ip6hdr = ipv6_hdr(skb);
  1150. if (ipv6_addr_any(&ip6hdr->saddr)) {
  1151. do_tx_balance = 0;
  1152. break;
  1153. }
  1154. hash_start = (char *)&(ipv6_hdr(skb)->daddr);
  1155. hash_size = sizeof(ipv6_hdr(skb)->daddr);
  1156. break;
  1157. case ETH_P_IPX:
  1158. if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
  1159. /* something is wrong with this packet */
  1160. do_tx_balance = 0;
  1161. break;
  1162. }
  1163. if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
  1164. /* The only protocol worth balancing in
  1165. * this family since it has an "ARP" like
  1166. * mechanism
  1167. */
  1168. do_tx_balance = 0;
  1169. break;
  1170. }
  1171. hash_start = (char*)eth_data->h_dest;
  1172. hash_size = ETH_ALEN;
  1173. break;
  1174. case ETH_P_ARP:
  1175. do_tx_balance = 0;
  1176. if (bond_info->rlb_enabled) {
  1177. tx_slave = rlb_arp_xmit(skb, bond);
  1178. }
  1179. break;
  1180. default:
  1181. do_tx_balance = 0;
  1182. break;
  1183. }
  1184. if (do_tx_balance) {
  1185. hash_index = _simple_hash(hash_start, hash_size);
  1186. tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
  1187. }
  1188. if (!tx_slave) {
  1189. /* unbalanced or unassigned, send through primary */
  1190. tx_slave = bond->curr_active_slave;
  1191. bond_info->unbalanced_load += skb->len;
  1192. }
  1193. if (tx_slave && SLAVE_IS_OK(tx_slave)) {
  1194. if (tx_slave != bond->curr_active_slave) {
  1195. memcpy(eth_data->h_source,
  1196. tx_slave->dev->dev_addr,
  1197. ETH_ALEN);
  1198. }
  1199. res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
  1200. } else {
  1201. if (tx_slave) {
  1202. _lock_tx_hashtbl(bond);
  1203. __tlb_clear_slave(bond, tx_slave, 0);
  1204. _unlock_tx_hashtbl(bond);
  1205. }
  1206. }
  1207. read_unlock(&bond->curr_slave_lock);
  1208. read_unlock(&bond->lock);
  1209. if (res) {
  1210. /* no suitable interface, frame not sent */
  1211. kfree_skb(skb);
  1212. }
  1213. return NETDEV_TX_OK;
  1214. }
  1215. void bond_alb_monitor(struct work_struct *work)
  1216. {
  1217. struct bonding *bond = container_of(work, struct bonding,
  1218. alb_work.work);
  1219. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1220. struct slave *slave;
  1221. read_lock(&bond->lock);
  1222. if (list_empty(&bond->slave_list)) {
  1223. bond_info->tx_rebalance_counter = 0;
  1224. bond_info->lp_counter = 0;
  1225. goto re_arm;
  1226. }
  1227. bond_info->tx_rebalance_counter++;
  1228. bond_info->lp_counter++;
  1229. /* send learning packets */
  1230. if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
  1231. /* change of curr_active_slave involves swapping of mac addresses.
  1232. * in order to avoid this swapping from happening while
  1233. * sending the learning packets, the curr_slave_lock must be held for
  1234. * read.
  1235. */
  1236. read_lock(&bond->curr_slave_lock);
  1237. bond_for_each_slave(bond, slave)
  1238. alb_send_learning_packets(slave, slave->dev->dev_addr);
  1239. read_unlock(&bond->curr_slave_lock);
  1240. bond_info->lp_counter = 0;
  1241. }
  1242. /* rebalance tx traffic */
  1243. if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
  1244. read_lock(&bond->curr_slave_lock);
  1245. bond_for_each_slave(bond, slave) {
  1246. tlb_clear_slave(bond, slave, 1);
  1247. if (slave == bond->curr_active_slave) {
  1248. SLAVE_TLB_INFO(slave).load =
  1249. bond_info->unbalanced_load /
  1250. BOND_TLB_REBALANCE_INTERVAL;
  1251. bond_info->unbalanced_load = 0;
  1252. }
  1253. }
  1254. read_unlock(&bond->curr_slave_lock);
  1255. bond_info->tx_rebalance_counter = 0;
  1256. }
  1257. /* handle rlb stuff */
  1258. if (bond_info->rlb_enabled) {
  1259. if (bond_info->primary_is_promisc &&
  1260. (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
  1261. /*
  1262. * dev_set_promiscuity requires rtnl and
  1263. * nothing else. Avoid race with bond_close.
  1264. */
  1265. read_unlock(&bond->lock);
  1266. if (!rtnl_trylock()) {
  1267. read_lock(&bond->lock);
  1268. goto re_arm;
  1269. }
  1270. bond_info->rlb_promisc_timeout_counter = 0;
  1271. /* If the primary was set to promiscuous mode
  1272. * because a slave was disabled then
  1273. * it can now leave promiscuous mode.
  1274. */
  1275. dev_set_promiscuity(bond->curr_active_slave->dev, -1);
  1276. bond_info->primary_is_promisc = 0;
  1277. rtnl_unlock();
  1278. read_lock(&bond->lock);
  1279. }
  1280. if (bond_info->rlb_rebalance) {
  1281. bond_info->rlb_rebalance = 0;
  1282. rlb_rebalance(bond);
  1283. }
  1284. /* check if clients need updating */
  1285. if (bond_info->rx_ntt) {
  1286. if (bond_info->rlb_update_delay_counter) {
  1287. --bond_info->rlb_update_delay_counter;
  1288. } else {
  1289. rlb_update_rx_clients(bond);
  1290. if (bond_info->rlb_update_retry_counter) {
  1291. --bond_info->rlb_update_retry_counter;
  1292. } else {
  1293. bond_info->rx_ntt = 0;
  1294. }
  1295. }
  1296. }
  1297. }
  1298. re_arm:
  1299. queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
  1300. read_unlock(&bond->lock);
  1301. }
  1302. /* assumption: called before the slave is attached to the bond
  1303. * and not locked by the bond lock
  1304. */
  1305. int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
  1306. {
  1307. int res;
  1308. res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
  1309. if (res) {
  1310. return res;
  1311. }
  1312. res = alb_handle_addr_collision_on_attach(bond, slave);
  1313. if (res) {
  1314. return res;
  1315. }
  1316. tlb_init_slave(slave);
  1317. /* order a rebalance ASAP */
  1318. bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1319. if (bond->alb_info.rlb_enabled) {
  1320. bond->alb_info.rlb_rebalance = 1;
  1321. }
  1322. return 0;
  1323. }
  1324. /*
  1325. * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
  1326. * if necessary.
  1327. *
  1328. * Caller must hold RTNL and no other locks
  1329. */
  1330. void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
  1331. {
  1332. if (!list_empty(&bond->slave_list))
  1333. alb_change_hw_addr_on_detach(bond, slave);
  1334. tlb_clear_slave(bond, slave, 0);
  1335. if (bond->alb_info.rlb_enabled) {
  1336. bond->alb_info.next_rx_slave = NULL;
  1337. rlb_clear_slave(bond, slave);
  1338. }
  1339. }
  1340. /* Caller must hold bond lock for read */
  1341. void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
  1342. {
  1343. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1344. if (link == BOND_LINK_DOWN) {
  1345. tlb_clear_slave(bond, slave, 0);
  1346. if (bond->alb_info.rlb_enabled) {
  1347. rlb_clear_slave(bond, slave);
  1348. }
  1349. } else if (link == BOND_LINK_UP) {
  1350. /* order a rebalance ASAP */
  1351. bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1352. if (bond->alb_info.rlb_enabled) {
  1353. bond->alb_info.rlb_rebalance = 1;
  1354. /* If the updelay module parameter is smaller than the
  1355. * forwarding delay of the switch the rebalance will
  1356. * not work because the rebalance arp replies will
  1357. * not be forwarded to the clients..
  1358. */
  1359. }
  1360. }
  1361. }
  1362. /**
  1363. * bond_alb_handle_active_change - assign new curr_active_slave
  1364. * @bond: our bonding struct
  1365. * @new_slave: new slave to assign
  1366. *
  1367. * Set the bond->curr_active_slave to @new_slave and handle
  1368. * mac address swapping and promiscuity changes as needed.
  1369. *
  1370. * If new_slave is NULL, caller must hold curr_slave_lock or
  1371. * bond->lock for write.
  1372. *
  1373. * If new_slave is not NULL, caller must hold RTNL, bond->lock for
  1374. * read and curr_slave_lock for write. Processing here may sleep, so
  1375. * no other locks may be held.
  1376. */
  1377. void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
  1378. __releases(&bond->curr_slave_lock)
  1379. __releases(&bond->lock)
  1380. __acquires(&bond->lock)
  1381. __acquires(&bond->curr_slave_lock)
  1382. {
  1383. struct slave *swap_slave;
  1384. if (bond->curr_active_slave == new_slave)
  1385. return;
  1386. if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
  1387. dev_set_promiscuity(bond->curr_active_slave->dev, -1);
  1388. bond->alb_info.primary_is_promisc = 0;
  1389. bond->alb_info.rlb_promisc_timeout_counter = 0;
  1390. }
  1391. swap_slave = bond->curr_active_slave;
  1392. rcu_assign_pointer(bond->curr_active_slave, new_slave);
  1393. if (!new_slave || list_empty(&bond->slave_list))
  1394. return;
  1395. /* set the new curr_active_slave to the bonds mac address
  1396. * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
  1397. */
  1398. if (!swap_slave)
  1399. swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
  1400. /*
  1401. * Arrange for swap_slave and new_slave to temporarily be
  1402. * ignored so we can mess with their MAC addresses without
  1403. * fear of interference from transmit activity.
  1404. */
  1405. if (swap_slave)
  1406. tlb_clear_slave(bond, swap_slave, 1);
  1407. tlb_clear_slave(bond, new_slave, 1);
  1408. write_unlock_bh(&bond->curr_slave_lock);
  1409. read_unlock(&bond->lock);
  1410. ASSERT_RTNL();
  1411. /* curr_active_slave must be set before calling alb_swap_mac_addr */
  1412. if (swap_slave) {
  1413. /* swap mac address */
  1414. alb_swap_mac_addr(swap_slave, new_slave);
  1415. alb_fasten_mac_swap(bond, swap_slave, new_slave);
  1416. read_lock(&bond->lock);
  1417. } else {
  1418. /* set the new_slave to the bond mac address */
  1419. alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
  1420. read_lock(&bond->lock);
  1421. alb_send_learning_packets(new_slave, bond->dev->dev_addr);
  1422. }
  1423. write_lock_bh(&bond->curr_slave_lock);
  1424. }
  1425. /*
  1426. * Called with RTNL
  1427. */
  1428. int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
  1429. __acquires(&bond->lock)
  1430. __releases(&bond->lock)
  1431. {
  1432. struct bonding *bond = netdev_priv(bond_dev);
  1433. struct sockaddr *sa = addr;
  1434. struct slave *swap_slave;
  1435. int res;
  1436. if (!is_valid_ether_addr(sa->sa_data)) {
  1437. return -EADDRNOTAVAIL;
  1438. }
  1439. res = alb_set_mac_address(bond, addr);
  1440. if (res) {
  1441. return res;
  1442. }
  1443. memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
  1444. /* If there is no curr_active_slave there is nothing else to do.
  1445. * Otherwise we'll need to pass the new address to it and handle
  1446. * duplications.
  1447. */
  1448. if (!bond->curr_active_slave) {
  1449. return 0;
  1450. }
  1451. swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
  1452. if (swap_slave) {
  1453. alb_swap_mac_addr(swap_slave, bond->curr_active_slave);
  1454. alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
  1455. } else {
  1456. alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr);
  1457. read_lock(&bond->lock);
  1458. alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
  1459. if (bond->alb_info.rlb_enabled) {
  1460. /* inform clients mac address has changed */
  1461. rlb_req_update_slave_clients(bond, bond->curr_active_slave);
  1462. }
  1463. read_unlock(&bond->lock);
  1464. }
  1465. return 0;
  1466. }
  1467. void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  1468. {
  1469. if (bond->alb_info.current_alb_vlan &&
  1470. (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
  1471. bond->alb_info.current_alb_vlan = NULL;
  1472. }
  1473. if (bond->alb_info.rlb_enabled) {
  1474. rlb_clear_vlan(bond, vlan_id);
  1475. }
  1476. }