bond_alb.c 44 KB

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