bond_alb.c 43 KB

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