bond_alb.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  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. bond_hw_addr_copy(arp->mac_src, tx_slave->dev->dev_addr,
  574. tx_slave->dev->addr_len);
  575. netdev_dbg(bond->dev, "Server sent ARP Reply packet\n");
  576. } else if (arp->op_code == htons(ARPOP_REQUEST)) {
  577. /* Create an entry in the rx_hashtbl for this client as a
  578. * place holder.
  579. * When the arp reply is received the entry will be updated
  580. * with the correct unicast address of the client.
  581. */
  582. rlb_choose_channel(skb, bond);
  583. /* The ARP reply packets must be delayed so that
  584. * they can cancel out the influence of the ARP request.
  585. */
  586. bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
  587. /* arp requests are broadcast and are sent on the primary
  588. * the arp request will collapse all clients on the subnet to
  589. * the primary slave. We must register these clients to be
  590. * updated with their assigned mac.
  591. */
  592. rlb_req_update_subnet_clients(bond, arp->ip_src);
  593. netdev_dbg(bond->dev, "Server sent ARP Request packet\n");
  594. }
  595. return tx_slave;
  596. }
  597. static void rlb_rebalance(struct bonding *bond)
  598. {
  599. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  600. struct slave *assigned_slave;
  601. struct rlb_client_info *client_info;
  602. int ntt;
  603. u32 hash_index;
  604. spin_lock_bh(&bond->mode_lock);
  605. ntt = 0;
  606. hash_index = bond_info->rx_hashtbl_used_head;
  607. for (; hash_index != RLB_NULL_INDEX;
  608. hash_index = client_info->used_next) {
  609. client_info = &(bond_info->rx_hashtbl[hash_index]);
  610. assigned_slave = __rlb_next_rx_slave(bond);
  611. if (assigned_slave && (client_info->slave != assigned_slave)) {
  612. client_info->slave = assigned_slave;
  613. client_info->ntt = 1;
  614. ntt = 1;
  615. }
  616. }
  617. /* update the team's flag only after the whole iteration */
  618. if (ntt)
  619. bond_info->rx_ntt = 1;
  620. spin_unlock_bh(&bond->mode_lock);
  621. }
  622. /* Caller must hold mode_lock */
  623. static void rlb_init_table_entry_dst(struct rlb_client_info *entry)
  624. {
  625. entry->used_next = RLB_NULL_INDEX;
  626. entry->used_prev = RLB_NULL_INDEX;
  627. entry->assigned = 0;
  628. entry->slave = NULL;
  629. entry->vlan_id = 0;
  630. }
  631. static void rlb_init_table_entry_src(struct rlb_client_info *entry)
  632. {
  633. entry->src_first = RLB_NULL_INDEX;
  634. entry->src_prev = RLB_NULL_INDEX;
  635. entry->src_next = RLB_NULL_INDEX;
  636. }
  637. static void rlb_init_table_entry(struct rlb_client_info *entry)
  638. {
  639. memset(entry, 0, sizeof(struct rlb_client_info));
  640. rlb_init_table_entry_dst(entry);
  641. rlb_init_table_entry_src(entry);
  642. }
  643. static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index)
  644. {
  645. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  646. u32 next_index = bond_info->rx_hashtbl[index].used_next;
  647. u32 prev_index = bond_info->rx_hashtbl[index].used_prev;
  648. if (index == bond_info->rx_hashtbl_used_head)
  649. bond_info->rx_hashtbl_used_head = next_index;
  650. if (prev_index != RLB_NULL_INDEX)
  651. bond_info->rx_hashtbl[prev_index].used_next = next_index;
  652. if (next_index != RLB_NULL_INDEX)
  653. bond_info->rx_hashtbl[next_index].used_prev = prev_index;
  654. }
  655. /* unlink a rlb hash table entry from the src list */
  656. static void rlb_src_unlink(struct bonding *bond, u32 index)
  657. {
  658. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  659. u32 next_index = bond_info->rx_hashtbl[index].src_next;
  660. u32 prev_index = bond_info->rx_hashtbl[index].src_prev;
  661. bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX;
  662. bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX;
  663. if (next_index != RLB_NULL_INDEX)
  664. bond_info->rx_hashtbl[next_index].src_prev = prev_index;
  665. if (prev_index == RLB_NULL_INDEX)
  666. return;
  667. /* is prev_index pointing to the head of this list? */
  668. if (bond_info->rx_hashtbl[prev_index].src_first == index)
  669. bond_info->rx_hashtbl[prev_index].src_first = next_index;
  670. else
  671. bond_info->rx_hashtbl[prev_index].src_next = next_index;
  672. }
  673. static void rlb_delete_table_entry(struct bonding *bond, u32 index)
  674. {
  675. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  676. struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
  677. rlb_delete_table_entry_dst(bond, index);
  678. rlb_init_table_entry_dst(entry);
  679. rlb_src_unlink(bond, index);
  680. }
  681. /* add the rx_hashtbl[ip_dst_hash] entry to the list
  682. * of entries with identical ip_src_hash
  683. */
  684. static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash)
  685. {
  686. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  687. u32 next;
  688. bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash;
  689. next = bond_info->rx_hashtbl[ip_src_hash].src_first;
  690. bond_info->rx_hashtbl[ip_dst_hash].src_next = next;
  691. if (next != RLB_NULL_INDEX)
  692. bond_info->rx_hashtbl[next].src_prev = ip_dst_hash;
  693. bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash;
  694. }
  695. /* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does
  696. * not match arp->mac_src
  697. */
  698. static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp)
  699. {
  700. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  701. u32 ip_src_hash = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
  702. u32 index;
  703. spin_lock_bh(&bond->mode_lock);
  704. index = bond_info->rx_hashtbl[ip_src_hash].src_first;
  705. while (index != RLB_NULL_INDEX) {
  706. struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
  707. u32 next_index = entry->src_next;
  708. if (entry->ip_src == arp->ip_src &&
  709. !ether_addr_equal_64bits(arp->mac_src, entry->mac_src))
  710. rlb_delete_table_entry(bond, index);
  711. index = next_index;
  712. }
  713. spin_unlock_bh(&bond->mode_lock);
  714. }
  715. static int rlb_initialize(struct bonding *bond)
  716. {
  717. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  718. struct rlb_client_info *new_hashtbl;
  719. int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
  720. int i;
  721. new_hashtbl = kmalloc(size, GFP_KERNEL);
  722. if (!new_hashtbl)
  723. return -1;
  724. spin_lock_bh(&bond->mode_lock);
  725. bond_info->rx_hashtbl = new_hashtbl;
  726. bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
  727. for (i = 0; i < RLB_HASH_TABLE_SIZE; i++)
  728. rlb_init_table_entry(bond_info->rx_hashtbl + i);
  729. spin_unlock_bh(&bond->mode_lock);
  730. /* register to receive ARPs */
  731. bond->recv_probe = rlb_arp_recv;
  732. return 0;
  733. }
  734. static void rlb_deinitialize(struct bonding *bond)
  735. {
  736. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  737. spin_lock_bh(&bond->mode_lock);
  738. kfree(bond_info->rx_hashtbl);
  739. bond_info->rx_hashtbl = NULL;
  740. bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
  741. spin_unlock_bh(&bond->mode_lock);
  742. }
  743. static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  744. {
  745. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  746. u32 curr_index;
  747. spin_lock_bh(&bond->mode_lock);
  748. curr_index = bond_info->rx_hashtbl_used_head;
  749. while (curr_index != RLB_NULL_INDEX) {
  750. struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
  751. u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
  752. if (curr->vlan_id == vlan_id)
  753. rlb_delete_table_entry(bond, curr_index);
  754. curr_index = next_index;
  755. }
  756. spin_unlock_bh(&bond->mode_lock);
  757. }
  758. /*********************** tlb/rlb shared functions *********************/
  759. static void alb_send_lp_vid(struct slave *slave, u8 mac_addr[],
  760. __be16 vlan_proto, u16 vid)
  761. {
  762. struct learning_pkt pkt;
  763. struct sk_buff *skb;
  764. int size = sizeof(struct learning_pkt);
  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. skb_put_data(skb, &pkt, size);
  773. skb_reset_mac_header(skb);
  774. skb->network_header = skb->mac_header + ETH_HLEN;
  775. skb->protocol = pkt.type;
  776. skb->priority = TC_PRIO_CONTROL;
  777. skb->dev = slave->dev;
  778. if (vid)
  779. __vlan_hwaccel_put_tag(skb, vlan_proto, vid);
  780. dev_queue_xmit(skb);
  781. }
  782. struct alb_walk_data {
  783. struct bonding *bond;
  784. struct slave *slave;
  785. u8 *mac_addr;
  786. bool strict_match;
  787. };
  788. static int alb_upper_dev_walk(struct net_device *upper, void *_data)
  789. {
  790. struct alb_walk_data *data = _data;
  791. bool strict_match = data->strict_match;
  792. struct bonding *bond = data->bond;
  793. struct slave *slave = data->slave;
  794. u8 *mac_addr = data->mac_addr;
  795. struct bond_vlan_tag *tags;
  796. if (is_vlan_dev(upper) && vlan_get_encap_level(upper) == 0) {
  797. if (strict_match &&
  798. ether_addr_equal_64bits(mac_addr,
  799. upper->dev_addr)) {
  800. alb_send_lp_vid(slave, mac_addr,
  801. vlan_dev_vlan_proto(upper),
  802. vlan_dev_vlan_id(upper));
  803. } else if (!strict_match) {
  804. alb_send_lp_vid(slave, upper->dev_addr,
  805. vlan_dev_vlan_proto(upper),
  806. vlan_dev_vlan_id(upper));
  807. }
  808. }
  809. /* If this is a macvlan device, then only send updates
  810. * when strict_match is turned off.
  811. */
  812. if (netif_is_macvlan(upper) && !strict_match) {
  813. tags = bond_verify_device_path(bond->dev, upper, 0);
  814. if (IS_ERR_OR_NULL(tags))
  815. BUG();
  816. alb_send_lp_vid(slave, upper->dev_addr,
  817. tags[0].vlan_proto, tags[0].vlan_id);
  818. kfree(tags);
  819. }
  820. return 0;
  821. }
  822. static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[],
  823. bool strict_match)
  824. {
  825. struct bonding *bond = bond_get_bond_by_slave(slave);
  826. struct alb_walk_data data = {
  827. .strict_match = strict_match,
  828. .mac_addr = mac_addr,
  829. .slave = slave,
  830. .bond = bond,
  831. };
  832. /* send untagged */
  833. alb_send_lp_vid(slave, mac_addr, 0, 0);
  834. /* loop through all devices and see if we need to send a packet
  835. * for that device.
  836. */
  837. rcu_read_lock();
  838. netdev_walk_all_upper_dev_rcu(bond->dev, alb_upper_dev_walk, &data);
  839. rcu_read_unlock();
  840. }
  841. static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[],
  842. unsigned int len)
  843. {
  844. struct net_device *dev = slave->dev;
  845. struct sockaddr_storage ss;
  846. if (BOND_MODE(slave->bond) == BOND_MODE_TLB) {
  847. memcpy(dev->dev_addr, addr, len);
  848. return 0;
  849. }
  850. /* for rlb each slave must have a unique hw mac addresses so that
  851. * each slave will receive packets destined to a different mac
  852. */
  853. memcpy(ss.__data, addr, len);
  854. ss.ss_family = dev->type;
  855. if (dev_set_mac_address(dev, (struct sockaddr *)&ss)) {
  856. 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",
  857. dev->name);
  858. return -EOPNOTSUPP;
  859. }
  860. return 0;
  861. }
  862. /* Swap MAC addresses between two slaves.
  863. *
  864. * Called with RTNL held, and no other locks.
  865. */
  866. static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
  867. {
  868. u8 tmp_mac_addr[MAX_ADDR_LEN];
  869. bond_hw_addr_copy(tmp_mac_addr, slave1->dev->dev_addr,
  870. slave1->dev->addr_len);
  871. alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr,
  872. slave2->dev->addr_len);
  873. alb_set_slave_mac_addr(slave2, tmp_mac_addr,
  874. slave1->dev->addr_len);
  875. }
  876. /* Send learning packets after MAC address swap.
  877. *
  878. * Called with RTNL and no other locks
  879. */
  880. static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
  881. struct slave *slave2)
  882. {
  883. int slaves_state_differ = (bond_slave_can_tx(slave1) != bond_slave_can_tx(slave2));
  884. struct slave *disabled_slave = NULL;
  885. ASSERT_RTNL();
  886. /* fasten the change in the switch */
  887. if (bond_slave_can_tx(slave1)) {
  888. alb_send_learning_packets(slave1, slave1->dev->dev_addr, false);
  889. if (bond->alb_info.rlb_enabled) {
  890. /* inform the clients that the mac address
  891. * has changed
  892. */
  893. rlb_req_update_slave_clients(bond, slave1);
  894. }
  895. } else {
  896. disabled_slave = slave1;
  897. }
  898. if (bond_slave_can_tx(slave2)) {
  899. alb_send_learning_packets(slave2, slave2->dev->dev_addr, false);
  900. if (bond->alb_info.rlb_enabled) {
  901. /* inform the clients that the mac address
  902. * has changed
  903. */
  904. rlb_req_update_slave_clients(bond, slave2);
  905. }
  906. } else {
  907. disabled_slave = slave2;
  908. }
  909. if (bond->alb_info.rlb_enabled && slaves_state_differ) {
  910. /* A disabled slave was assigned an active mac addr */
  911. rlb_teach_disabled_mac_on_primary(bond,
  912. disabled_slave->dev->dev_addr);
  913. }
  914. }
  915. /**
  916. * alb_change_hw_addr_on_detach
  917. * @bond: bonding we're working on
  918. * @slave: the slave that was just detached
  919. *
  920. * We assume that @slave was already detached from the slave list.
  921. *
  922. * If @slave's permanent hw address is different both from its current
  923. * address and from @bond's address, then somewhere in the bond there's
  924. * a slave that has @slave's permanet address as its current address.
  925. * We'll make sure that that slave no longer uses @slave's permanent address.
  926. *
  927. * Caller must hold RTNL and no other locks
  928. */
  929. static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
  930. {
  931. int perm_curr_diff;
  932. int perm_bond_diff;
  933. struct slave *found_slave;
  934. perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
  935. slave->dev->dev_addr);
  936. perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
  937. bond->dev->dev_addr);
  938. if (perm_curr_diff && perm_bond_diff) {
  939. found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr);
  940. if (found_slave) {
  941. alb_swap_mac_addr(slave, found_slave);
  942. alb_fasten_mac_swap(bond, slave, found_slave);
  943. }
  944. }
  945. }
  946. /**
  947. * alb_handle_addr_collision_on_attach
  948. * @bond: bonding we're working on
  949. * @slave: the slave that was just attached
  950. *
  951. * checks uniqueness of slave's mac address and handles the case the
  952. * new slave uses the bonds mac address.
  953. *
  954. * If the permanent hw address of @slave is @bond's hw address, we need to
  955. * find a different hw address to give @slave, that isn't in use by any other
  956. * slave in the bond. This address must be, of course, one of the permanent
  957. * addresses of the other slaves.
  958. *
  959. * We go over the slave list, and for each slave there we compare its
  960. * permanent hw address with the current address of all the other slaves.
  961. * If no match was found, then we've found a slave with a permanent address
  962. * that isn't used by any other slave in the bond, so we can assign it to
  963. * @slave.
  964. *
  965. * assumption: this function is called before @slave is attached to the
  966. * bond slave list.
  967. */
  968. static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
  969. {
  970. struct slave *has_bond_addr = rcu_access_pointer(bond->curr_active_slave);
  971. struct slave *tmp_slave1, *free_mac_slave = NULL;
  972. struct list_head *iter;
  973. if (!bond_has_slaves(bond)) {
  974. /* this is the first slave */
  975. return 0;
  976. }
  977. /* if slave's mac address differs from bond's mac address
  978. * check uniqueness of slave's mac address against the other
  979. * slaves in the bond.
  980. */
  981. if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
  982. if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
  983. return 0;
  984. /* Try setting slave mac to bond address and fall-through
  985. * to code handling that situation below...
  986. */
  987. alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
  988. bond->dev->addr_len);
  989. }
  990. /* The slave's address is equal to the address of the bond.
  991. * Search for a spare address in the bond for this slave.
  992. */
  993. bond_for_each_slave(bond, tmp_slave1, iter) {
  994. if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
  995. /* no slave has tmp_slave1's perm addr
  996. * as its curr addr
  997. */
  998. free_mac_slave = tmp_slave1;
  999. break;
  1000. }
  1001. if (!has_bond_addr) {
  1002. if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
  1003. bond->dev->dev_addr)) {
  1004. has_bond_addr = tmp_slave1;
  1005. }
  1006. }
  1007. }
  1008. if (free_mac_slave) {
  1009. alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
  1010. free_mac_slave->dev->addr_len);
  1011. netdev_warn(bond->dev, "the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
  1012. slave->dev->name, free_mac_slave->dev->name);
  1013. } else if (has_bond_addr) {
  1014. 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",
  1015. slave->dev->name);
  1016. return -EFAULT;
  1017. }
  1018. return 0;
  1019. }
  1020. /**
  1021. * alb_set_mac_address
  1022. * @bond:
  1023. * @addr:
  1024. *
  1025. * In TLB mode all slaves are configured to the bond's hw address, but set
  1026. * their dev_addr field to different addresses (based on their permanent hw
  1027. * addresses).
  1028. *
  1029. * For each slave, this function sets the interface to the new address and then
  1030. * changes its dev_addr field to its previous value.
  1031. *
  1032. * Unwinding assumes bond's mac address has not yet changed.
  1033. */
  1034. static int alb_set_mac_address(struct bonding *bond, void *addr)
  1035. {
  1036. struct slave *slave, *rollback_slave;
  1037. struct list_head *iter;
  1038. struct sockaddr_storage ss;
  1039. char tmp_addr[MAX_ADDR_LEN];
  1040. int res;
  1041. if (bond->alb_info.rlb_enabled)
  1042. return 0;
  1043. bond_for_each_slave(bond, slave, iter) {
  1044. /* save net_device's current hw address */
  1045. bond_hw_addr_copy(tmp_addr, slave->dev->dev_addr,
  1046. slave->dev->addr_len);
  1047. res = dev_set_mac_address(slave->dev, addr);
  1048. /* restore net_device's hw address */
  1049. bond_hw_addr_copy(slave->dev->dev_addr, tmp_addr,
  1050. slave->dev->addr_len);
  1051. if (res)
  1052. goto unwind;
  1053. }
  1054. return 0;
  1055. unwind:
  1056. memcpy(ss.__data, bond->dev->dev_addr, bond->dev->addr_len);
  1057. ss.ss_family = bond->dev->type;
  1058. /* unwind from head to the slave that failed */
  1059. bond_for_each_slave(bond, rollback_slave, iter) {
  1060. if (rollback_slave == slave)
  1061. break;
  1062. bond_hw_addr_copy(tmp_addr, rollback_slave->dev->dev_addr,
  1063. rollback_slave->dev->addr_len);
  1064. dev_set_mac_address(rollback_slave->dev,
  1065. (struct sockaddr *)&ss);
  1066. bond_hw_addr_copy(rollback_slave->dev->dev_addr, tmp_addr,
  1067. rollback_slave->dev->addr_len);
  1068. }
  1069. return res;
  1070. }
  1071. /************************ exported alb funcions ************************/
  1072. int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
  1073. {
  1074. int res;
  1075. res = tlb_initialize(bond);
  1076. if (res)
  1077. return res;
  1078. if (rlb_enabled) {
  1079. bond->alb_info.rlb_enabled = 1;
  1080. res = rlb_initialize(bond);
  1081. if (res) {
  1082. tlb_deinitialize(bond);
  1083. return res;
  1084. }
  1085. } else {
  1086. bond->alb_info.rlb_enabled = 0;
  1087. }
  1088. return 0;
  1089. }
  1090. void bond_alb_deinitialize(struct bonding *bond)
  1091. {
  1092. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1093. tlb_deinitialize(bond);
  1094. if (bond_info->rlb_enabled)
  1095. rlb_deinitialize(bond);
  1096. }
  1097. static int bond_do_alb_xmit(struct sk_buff *skb, struct bonding *bond,
  1098. struct slave *tx_slave)
  1099. {
  1100. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1101. struct ethhdr *eth_data = eth_hdr(skb);
  1102. if (!tx_slave) {
  1103. /* unbalanced or unassigned, send through primary */
  1104. tx_slave = rcu_dereference(bond->curr_active_slave);
  1105. if (bond->params.tlb_dynamic_lb)
  1106. bond_info->unbalanced_load += skb->len;
  1107. }
  1108. if (tx_slave && bond_slave_can_tx(tx_slave)) {
  1109. if (tx_slave != rcu_access_pointer(bond->curr_active_slave)) {
  1110. ether_addr_copy(eth_data->h_source,
  1111. tx_slave->dev->dev_addr);
  1112. }
  1113. bond_dev_queue_xmit(bond, skb, tx_slave->dev);
  1114. goto out;
  1115. }
  1116. if (tx_slave && bond->params.tlb_dynamic_lb) {
  1117. spin_lock(&bond->mode_lock);
  1118. __tlb_clear_slave(bond, tx_slave, 0);
  1119. spin_unlock(&bond->mode_lock);
  1120. }
  1121. /* no suitable interface, frame not sent */
  1122. bond_tx_drop(bond->dev, skb);
  1123. out:
  1124. return NETDEV_TX_OK;
  1125. }
  1126. int bond_tlb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
  1127. {
  1128. struct bonding *bond = netdev_priv(bond_dev);
  1129. struct ethhdr *eth_data;
  1130. struct slave *tx_slave = NULL;
  1131. u32 hash_index;
  1132. skb_reset_mac_header(skb);
  1133. eth_data = eth_hdr(skb);
  1134. /* Do not TX balance any multicast or broadcast */
  1135. if (!is_multicast_ether_addr(eth_data->h_dest)) {
  1136. switch (skb->protocol) {
  1137. case htons(ETH_P_IP):
  1138. case htons(ETH_P_IPX):
  1139. /* In case of IPX, it will falback to L2 hash */
  1140. case htons(ETH_P_IPV6):
  1141. hash_index = bond_xmit_hash(bond, skb);
  1142. if (bond->params.tlb_dynamic_lb) {
  1143. tx_slave = tlb_choose_channel(bond,
  1144. hash_index & 0xFF,
  1145. skb->len);
  1146. } else {
  1147. struct bond_up_slave *slaves;
  1148. unsigned int count;
  1149. slaves = rcu_dereference(bond->slave_arr);
  1150. count = slaves ? ACCESS_ONCE(slaves->count) : 0;
  1151. if (likely(count))
  1152. tx_slave = slaves->arr[hash_index %
  1153. count];
  1154. }
  1155. break;
  1156. }
  1157. }
  1158. return bond_do_alb_xmit(skb, bond, tx_slave);
  1159. }
  1160. int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
  1161. {
  1162. struct bonding *bond = netdev_priv(bond_dev);
  1163. struct ethhdr *eth_data;
  1164. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1165. struct slave *tx_slave = NULL;
  1166. static const __be32 ip_bcast = htonl(0xffffffff);
  1167. int hash_size = 0;
  1168. bool do_tx_balance = true;
  1169. u32 hash_index = 0;
  1170. const u8 *hash_start = NULL;
  1171. struct ipv6hdr *ip6hdr;
  1172. skb_reset_mac_header(skb);
  1173. eth_data = eth_hdr(skb);
  1174. switch (ntohs(skb->protocol)) {
  1175. case ETH_P_IP: {
  1176. const struct iphdr *iph = ip_hdr(skb);
  1177. if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
  1178. (iph->daddr == ip_bcast) ||
  1179. (iph->protocol == IPPROTO_IGMP)) {
  1180. do_tx_balance = false;
  1181. break;
  1182. }
  1183. hash_start = (char *)&(iph->daddr);
  1184. hash_size = sizeof(iph->daddr);
  1185. }
  1186. break;
  1187. case ETH_P_IPV6:
  1188. /* IPv6 doesn't really use broadcast mac address, but leave
  1189. * that here just in case.
  1190. */
  1191. if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast)) {
  1192. do_tx_balance = false;
  1193. break;
  1194. }
  1195. /* IPv6 uses all-nodes multicast as an equivalent to
  1196. * broadcasts in IPv4.
  1197. */
  1198. if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
  1199. do_tx_balance = false;
  1200. break;
  1201. }
  1202. /* Additianally, DAD probes should not be tx-balanced as that
  1203. * will lead to false positives for duplicate addresses and
  1204. * prevent address configuration from working.
  1205. */
  1206. ip6hdr = ipv6_hdr(skb);
  1207. if (ipv6_addr_any(&ip6hdr->saddr)) {
  1208. do_tx_balance = false;
  1209. break;
  1210. }
  1211. hash_start = (char *)&(ipv6_hdr(skb)->daddr);
  1212. hash_size = sizeof(ipv6_hdr(skb)->daddr);
  1213. break;
  1214. case ETH_P_IPX:
  1215. if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
  1216. /* something is wrong with this packet */
  1217. do_tx_balance = false;
  1218. break;
  1219. }
  1220. if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
  1221. /* The only protocol worth balancing in
  1222. * this family since it has an "ARP" like
  1223. * mechanism
  1224. */
  1225. do_tx_balance = false;
  1226. break;
  1227. }
  1228. hash_start = (char *)eth_data->h_dest;
  1229. hash_size = ETH_ALEN;
  1230. break;
  1231. case ETH_P_ARP:
  1232. do_tx_balance = false;
  1233. if (bond_info->rlb_enabled)
  1234. tx_slave = rlb_arp_xmit(skb, bond);
  1235. break;
  1236. default:
  1237. do_tx_balance = false;
  1238. break;
  1239. }
  1240. if (do_tx_balance) {
  1241. hash_index = _simple_hash(hash_start, hash_size);
  1242. tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
  1243. }
  1244. return bond_do_alb_xmit(skb, bond, tx_slave);
  1245. }
  1246. void bond_alb_monitor(struct work_struct *work)
  1247. {
  1248. struct bonding *bond = container_of(work, struct bonding,
  1249. alb_work.work);
  1250. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1251. struct list_head *iter;
  1252. struct slave *slave;
  1253. if (!bond_has_slaves(bond)) {
  1254. bond_info->tx_rebalance_counter = 0;
  1255. bond_info->lp_counter = 0;
  1256. goto re_arm;
  1257. }
  1258. rcu_read_lock();
  1259. bond_info->tx_rebalance_counter++;
  1260. bond_info->lp_counter++;
  1261. /* send learning packets */
  1262. if (bond_info->lp_counter >= BOND_ALB_LP_TICKS(bond)) {
  1263. bool strict_match;
  1264. bond_for_each_slave_rcu(bond, slave, iter) {
  1265. /* If updating current_active, use all currently
  1266. * user mac addreses (!strict_match). Otherwise, only
  1267. * use mac of the slave device.
  1268. * In RLB mode, we always use strict matches.
  1269. */
  1270. strict_match = (slave != rcu_access_pointer(bond->curr_active_slave) ||
  1271. bond_info->rlb_enabled);
  1272. alb_send_learning_packets(slave, slave->dev->dev_addr,
  1273. strict_match);
  1274. }
  1275. bond_info->lp_counter = 0;
  1276. }
  1277. /* rebalance tx traffic */
  1278. if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
  1279. bond_for_each_slave_rcu(bond, slave, iter) {
  1280. tlb_clear_slave(bond, slave, 1);
  1281. if (slave == rcu_access_pointer(bond->curr_active_slave)) {
  1282. SLAVE_TLB_INFO(slave).load =
  1283. bond_info->unbalanced_load /
  1284. BOND_TLB_REBALANCE_INTERVAL;
  1285. bond_info->unbalanced_load = 0;
  1286. }
  1287. }
  1288. bond_info->tx_rebalance_counter = 0;
  1289. }
  1290. if (bond_info->rlb_enabled) {
  1291. if (bond_info->primary_is_promisc &&
  1292. (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
  1293. /* dev_set_promiscuity requires rtnl and
  1294. * nothing else. Avoid race with bond_close.
  1295. */
  1296. rcu_read_unlock();
  1297. if (!rtnl_trylock())
  1298. goto re_arm;
  1299. bond_info->rlb_promisc_timeout_counter = 0;
  1300. /* If the primary was set to promiscuous mode
  1301. * because a slave was disabled then
  1302. * it can now leave promiscuous mode.
  1303. */
  1304. dev_set_promiscuity(rtnl_dereference(bond->curr_active_slave)->dev,
  1305. -1);
  1306. bond_info->primary_is_promisc = 0;
  1307. rtnl_unlock();
  1308. rcu_read_lock();
  1309. }
  1310. if (bond_info->rlb_rebalance) {
  1311. bond_info->rlb_rebalance = 0;
  1312. rlb_rebalance(bond);
  1313. }
  1314. /* check if clients need updating */
  1315. if (bond_info->rx_ntt) {
  1316. if (bond_info->rlb_update_delay_counter) {
  1317. --bond_info->rlb_update_delay_counter;
  1318. } else {
  1319. rlb_update_rx_clients(bond);
  1320. if (bond_info->rlb_update_retry_counter)
  1321. --bond_info->rlb_update_retry_counter;
  1322. else
  1323. bond_info->rx_ntt = 0;
  1324. }
  1325. }
  1326. }
  1327. rcu_read_unlock();
  1328. re_arm:
  1329. queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
  1330. }
  1331. /* assumption: called before the slave is attached to the bond
  1332. * and not locked by the bond lock
  1333. */
  1334. int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
  1335. {
  1336. int res;
  1337. res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
  1338. slave->dev->addr_len);
  1339. if (res)
  1340. return res;
  1341. res = alb_handle_addr_collision_on_attach(bond, slave);
  1342. if (res)
  1343. return res;
  1344. tlb_init_slave(slave);
  1345. /* order a rebalance ASAP */
  1346. bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1347. if (bond->alb_info.rlb_enabled)
  1348. bond->alb_info.rlb_rebalance = 1;
  1349. return 0;
  1350. }
  1351. /* Remove slave from tlb and rlb hash tables, and fix up MAC addresses
  1352. * if necessary.
  1353. *
  1354. * Caller must hold RTNL and no other locks
  1355. */
  1356. void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
  1357. {
  1358. if (bond_has_slaves(bond))
  1359. alb_change_hw_addr_on_detach(bond, slave);
  1360. tlb_clear_slave(bond, slave, 0);
  1361. if (bond->alb_info.rlb_enabled) {
  1362. bond->alb_info.rx_slave = NULL;
  1363. rlb_clear_slave(bond, slave);
  1364. }
  1365. }
  1366. void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
  1367. {
  1368. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1369. if (link == BOND_LINK_DOWN) {
  1370. tlb_clear_slave(bond, slave, 0);
  1371. if (bond->alb_info.rlb_enabled)
  1372. rlb_clear_slave(bond, slave);
  1373. } else if (link == BOND_LINK_UP) {
  1374. /* order a rebalance ASAP */
  1375. bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1376. if (bond->alb_info.rlb_enabled) {
  1377. bond->alb_info.rlb_rebalance = 1;
  1378. /* If the updelay module parameter is smaller than the
  1379. * forwarding delay of the switch the rebalance will
  1380. * not work because the rebalance arp replies will
  1381. * not be forwarded to the clients..
  1382. */
  1383. }
  1384. }
  1385. if (bond_is_nondyn_tlb(bond)) {
  1386. if (bond_update_slave_arr(bond, NULL))
  1387. pr_err("Failed to build slave-array for TLB mode.\n");
  1388. }
  1389. }
  1390. /**
  1391. * bond_alb_handle_active_change - assign new curr_active_slave
  1392. * @bond: our bonding struct
  1393. * @new_slave: new slave to assign
  1394. *
  1395. * Set the bond->curr_active_slave to @new_slave and handle
  1396. * mac address swapping and promiscuity changes as needed.
  1397. *
  1398. * Caller must hold RTNL
  1399. */
  1400. void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
  1401. {
  1402. struct slave *swap_slave;
  1403. struct slave *curr_active;
  1404. curr_active = rtnl_dereference(bond->curr_active_slave);
  1405. if (curr_active == new_slave)
  1406. return;
  1407. if (curr_active && bond->alb_info.primary_is_promisc) {
  1408. dev_set_promiscuity(curr_active->dev, -1);
  1409. bond->alb_info.primary_is_promisc = 0;
  1410. bond->alb_info.rlb_promisc_timeout_counter = 0;
  1411. }
  1412. swap_slave = curr_active;
  1413. rcu_assign_pointer(bond->curr_active_slave, new_slave);
  1414. if (!new_slave || !bond_has_slaves(bond))
  1415. return;
  1416. /* set the new curr_active_slave to the bonds mac address
  1417. * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
  1418. */
  1419. if (!swap_slave)
  1420. swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
  1421. /* Arrange for swap_slave and new_slave to temporarily be
  1422. * ignored so we can mess with their MAC addresses without
  1423. * fear of interference from transmit activity.
  1424. */
  1425. if (swap_slave)
  1426. tlb_clear_slave(bond, swap_slave, 1);
  1427. tlb_clear_slave(bond, new_slave, 1);
  1428. /* in TLB mode, the slave might flip down/up with the old dev_addr,
  1429. * and thus filter bond->dev_addr's packets, so force bond's mac
  1430. */
  1431. if (BOND_MODE(bond) == BOND_MODE_TLB) {
  1432. struct sockaddr_storage ss;
  1433. u8 tmp_addr[MAX_ADDR_LEN];
  1434. bond_hw_addr_copy(tmp_addr, new_slave->dev->dev_addr,
  1435. new_slave->dev->addr_len);
  1436. bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
  1437. bond->dev->addr_len);
  1438. ss.ss_family = bond->dev->type;
  1439. /* we don't care if it can't change its mac, best effort */
  1440. dev_set_mac_address(new_slave->dev, (struct sockaddr *)&ss);
  1441. bond_hw_addr_copy(new_slave->dev->dev_addr, tmp_addr,
  1442. new_slave->dev->addr_len);
  1443. }
  1444. /* curr_active_slave must be set before calling alb_swap_mac_addr */
  1445. if (swap_slave) {
  1446. /* swap mac address */
  1447. alb_swap_mac_addr(swap_slave, new_slave);
  1448. alb_fasten_mac_swap(bond, swap_slave, new_slave);
  1449. } else {
  1450. /* set the new_slave to the bond mac address */
  1451. alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
  1452. bond->dev->addr_len);
  1453. alb_send_learning_packets(new_slave, bond->dev->dev_addr,
  1454. false);
  1455. }
  1456. }
  1457. /* Called with RTNL */
  1458. int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
  1459. {
  1460. struct bonding *bond = netdev_priv(bond_dev);
  1461. struct sockaddr_storage *ss = addr;
  1462. struct slave *curr_active;
  1463. struct slave *swap_slave;
  1464. int res;
  1465. if (!is_valid_ether_addr(ss->__data))
  1466. return -EADDRNOTAVAIL;
  1467. res = alb_set_mac_address(bond, addr);
  1468. if (res)
  1469. return res;
  1470. bond_hw_addr_copy(bond_dev->dev_addr, ss->__data, bond_dev->addr_len);
  1471. /* If there is no curr_active_slave there is nothing else to do.
  1472. * Otherwise we'll need to pass the new address to it and handle
  1473. * duplications.
  1474. */
  1475. curr_active = rtnl_dereference(bond->curr_active_slave);
  1476. if (!curr_active)
  1477. return 0;
  1478. swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
  1479. if (swap_slave) {
  1480. alb_swap_mac_addr(swap_slave, curr_active);
  1481. alb_fasten_mac_swap(bond, swap_slave, curr_active);
  1482. } else {
  1483. alb_set_slave_mac_addr(curr_active, bond_dev->dev_addr,
  1484. bond_dev->addr_len);
  1485. alb_send_learning_packets(curr_active,
  1486. bond_dev->dev_addr, false);
  1487. if (bond->alb_info.rlb_enabled) {
  1488. /* inform clients mac address has changed */
  1489. rlb_req_update_slave_clients(bond, curr_active);
  1490. }
  1491. }
  1492. return 0;
  1493. }
  1494. void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  1495. {
  1496. if (bond->alb_info.rlb_enabled)
  1497. rlb_clear_vlan(bond, vlan_id);
  1498. }