bond_alb.c 44 KB

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