bond_alb.c 43 KB

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