bond_alb.c 43 KB

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