bond_alb.c 47 KB

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