bond_alb.c 48 KB

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