bond_alb.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  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_v6_allmcast[ETH_ALEN + 2] __long_aligned = {
  42. 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
  43. };
  44. static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
  45. #pragma pack(1)
  46. struct learning_pkt {
  47. u8 mac_dst[ETH_ALEN];
  48. u8 mac_src[ETH_ALEN];
  49. __be16 type;
  50. u8 padding[ETH_ZLEN - ETH_HLEN];
  51. };
  52. struct arp_pkt {
  53. __be16 hw_addr_space;
  54. __be16 prot_addr_space;
  55. u8 hw_addr_len;
  56. u8 prot_addr_len;
  57. __be16 op_code;
  58. u8 mac_src[ETH_ALEN]; /* sender hardware address */
  59. __be32 ip_src; /* sender IP address */
  60. u8 mac_dst[ETH_ALEN]; /* target hardware address */
  61. __be32 ip_dst; /* target IP address */
  62. };
  63. #pragma pack()
  64. /* Forward declaration */
  65. static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[],
  66. bool strict_match);
  67. static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp);
  68. static void rlb_src_unlink(struct bonding *bond, u32 index);
  69. static void rlb_src_link(struct bonding *bond, u32 ip_src_hash,
  70. u32 ip_dst_hash);
  71. static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
  72. {
  73. int i;
  74. u8 hash = 0;
  75. for (i = 0; i < hash_size; i++)
  76. hash ^= hash_start[i];
  77. return hash;
  78. }
  79. /*********************** tlb specific functions ***************************/
  80. static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
  81. {
  82. if (save_load) {
  83. entry->load_history = 1 + entry->tx_bytes /
  84. BOND_TLB_REBALANCE_INTERVAL;
  85. entry->tx_bytes = 0;
  86. }
  87. entry->tx_slave = NULL;
  88. entry->next = TLB_NULL_INDEX;
  89. entry->prev = TLB_NULL_INDEX;
  90. }
  91. static inline void tlb_init_slave(struct slave *slave)
  92. {
  93. SLAVE_TLB_INFO(slave).load = 0;
  94. SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
  95. }
  96. static void __tlb_clear_slave(struct bonding *bond, struct slave *slave,
  97. int save_load)
  98. {
  99. struct tlb_client_info *tx_hash_table;
  100. u32 index;
  101. /* clear slave from tx_hashtbl */
  102. tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
  103. /* skip this if we've already freed the tx hash table */
  104. if (tx_hash_table) {
  105. index = SLAVE_TLB_INFO(slave).head;
  106. while (index != TLB_NULL_INDEX) {
  107. u32 next_index = tx_hash_table[index].next;
  108. tlb_init_table_entry(&tx_hash_table[index], save_load);
  109. index = next_index;
  110. }
  111. }
  112. tlb_init_slave(slave);
  113. }
  114. static void tlb_clear_slave(struct bonding *bond, struct slave *slave,
  115. int save_load)
  116. {
  117. spin_lock_bh(&bond->mode_lock);
  118. __tlb_clear_slave(bond, slave, save_load);
  119. spin_unlock_bh(&bond->mode_lock);
  120. }
  121. /* Must be called before starting the monitor timer */
  122. static int tlb_initialize(struct bonding *bond)
  123. {
  124. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  125. int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
  126. struct tlb_client_info *new_hashtbl;
  127. int i;
  128. new_hashtbl = kzalloc(size, GFP_KERNEL);
  129. if (!new_hashtbl)
  130. return -ENOMEM;
  131. spin_lock_bh(&bond->mode_lock);
  132. bond_info->tx_hashtbl = new_hashtbl;
  133. for (i = 0; i < TLB_HASH_TABLE_SIZE; i++)
  134. tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
  135. spin_unlock_bh(&bond->mode_lock);
  136. return 0;
  137. }
  138. /* Must be called only after all slaves have been released */
  139. static void tlb_deinitialize(struct bonding *bond)
  140. {
  141. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  142. spin_lock_bh(&bond->mode_lock);
  143. kfree(bond_info->tx_hashtbl);
  144. bond_info->tx_hashtbl = NULL;
  145. spin_unlock_bh(&bond->mode_lock);
  146. }
  147. static long long compute_gap(struct slave *slave)
  148. {
  149. return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
  150. (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
  151. }
  152. static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
  153. {
  154. struct slave *slave, *least_loaded;
  155. struct list_head *iter;
  156. long long max_gap;
  157. least_loaded = NULL;
  158. max_gap = LLONG_MIN;
  159. /* Find the slave with the largest gap */
  160. bond_for_each_slave_rcu(bond, slave, iter) {
  161. if (bond_slave_can_tx(slave)) {
  162. long long gap = compute_gap(slave);
  163. if (max_gap < gap) {
  164. least_loaded = slave;
  165. max_gap = gap;
  166. }
  167. }
  168. }
  169. return least_loaded;
  170. }
  171. static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
  172. u32 skb_len)
  173. {
  174. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  175. struct tlb_client_info *hash_table;
  176. struct slave *assigned_slave;
  177. hash_table = bond_info->tx_hashtbl;
  178. assigned_slave = hash_table[hash_index].tx_slave;
  179. if (!assigned_slave) {
  180. assigned_slave = tlb_get_least_loaded_slave(bond);
  181. if (assigned_slave) {
  182. struct tlb_slave_info *slave_info =
  183. &(SLAVE_TLB_INFO(assigned_slave));
  184. u32 next_index = slave_info->head;
  185. hash_table[hash_index].tx_slave = assigned_slave;
  186. hash_table[hash_index].next = next_index;
  187. hash_table[hash_index].prev = TLB_NULL_INDEX;
  188. if (next_index != TLB_NULL_INDEX)
  189. hash_table[next_index].prev = hash_index;
  190. slave_info->head = hash_index;
  191. slave_info->load +=
  192. hash_table[hash_index].load_history;
  193. }
  194. }
  195. if (assigned_slave)
  196. hash_table[hash_index].tx_bytes += skb_len;
  197. return assigned_slave;
  198. }
  199. static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index,
  200. u32 skb_len)
  201. {
  202. struct slave *tx_slave;
  203. /* We don't need to disable softirq here, becase
  204. * tlb_choose_channel() is only called by bond_alb_xmit()
  205. * which already has softirq disabled.
  206. */
  207. spin_lock(&bond->mode_lock);
  208. tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
  209. spin_unlock(&bond->mode_lock);
  210. return tx_slave;
  211. }
  212. /*********************** rlb specific functions ***************************/
  213. /* when an ARP REPLY is received from a client update its info
  214. * in the rx_hashtbl
  215. */
  216. static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
  217. {
  218. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  219. struct rlb_client_info *client_info;
  220. u32 hash_index;
  221. spin_lock_bh(&bond->mode_lock);
  222. hash_index = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
  223. client_info = &(bond_info->rx_hashtbl[hash_index]);
  224. if ((client_info->assigned) &&
  225. (client_info->ip_src == arp->ip_dst) &&
  226. (client_info->ip_dst == arp->ip_src) &&
  227. (!ether_addr_equal_64bits(client_info->mac_dst, arp->mac_src))) {
  228. /* update the clients MAC address */
  229. ether_addr_copy(client_info->mac_dst, arp->mac_src);
  230. client_info->ntt = 1;
  231. bond_info->rx_ntt = 1;
  232. }
  233. spin_unlock_bh(&bond->mode_lock);
  234. }
  235. static int rlb_arp_recv(const struct sk_buff *skb, struct bonding *bond,
  236. struct slave *slave)
  237. {
  238. struct arp_pkt *arp, _arp;
  239. if (skb->protocol != cpu_to_be16(ETH_P_ARP))
  240. goto out;
  241. arp = skb_header_pointer(skb, 0, sizeof(_arp), &_arp);
  242. if (!arp)
  243. goto out;
  244. /* We received an ARP from arp->ip_src.
  245. * We might have used this IP address previously (on the bonding host
  246. * itself or on a system that is bridged together with the bond).
  247. * However, if arp->mac_src is different than what is stored in
  248. * rx_hashtbl, some other host is now using the IP and we must prevent
  249. * sending out client updates with this IP address and the old MAC
  250. * address.
  251. * Clean up all hash table entries that have this address as ip_src but
  252. * have a different mac_src.
  253. */
  254. rlb_purge_src_ip(bond, arp);
  255. if (arp->op_code == htons(ARPOP_REPLY)) {
  256. /* update rx hash table for this ARP */
  257. rlb_update_entry_from_arp(bond, arp);
  258. netdev_dbg(bond->dev, "Server received an ARP Reply from client\n");
  259. }
  260. out:
  261. return RX_HANDLER_ANOTHER;
  262. }
  263. /* Caller must hold rcu_read_lock() */
  264. static struct slave *__rlb_next_rx_slave(struct bonding *bond)
  265. {
  266. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  267. struct slave *before = NULL, *rx_slave = NULL, *slave;
  268. struct list_head *iter;
  269. bool found = false;
  270. bond_for_each_slave_rcu(bond, slave, iter) {
  271. if (!bond_slave_can_tx(slave))
  272. continue;
  273. if (!found) {
  274. if (!before || before->speed < slave->speed)
  275. before = slave;
  276. } else {
  277. if (!rx_slave || rx_slave->speed < slave->speed)
  278. rx_slave = slave;
  279. }
  280. if (slave == bond_info->rx_slave)
  281. found = true;
  282. }
  283. /* we didn't find anything after the current or we have something
  284. * better before and up to the current slave
  285. */
  286. if (!rx_slave || (before && rx_slave->speed < before->speed))
  287. rx_slave = before;
  288. if (rx_slave)
  289. bond_info->rx_slave = rx_slave;
  290. return rx_slave;
  291. }
  292. /* Caller must hold RTNL, rcu_read_lock is obtained only to silence checkers */
  293. static struct slave *rlb_next_rx_slave(struct bonding *bond)
  294. {
  295. struct slave *rx_slave;
  296. ASSERT_RTNL();
  297. rcu_read_lock();
  298. rx_slave = __rlb_next_rx_slave(bond);
  299. rcu_read_unlock();
  300. return rx_slave;
  301. }
  302. /* teach the switch the mac of a disabled slave
  303. * on the primary for fault tolerance
  304. *
  305. * Caller must hold RTNL
  306. */
  307. static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
  308. {
  309. struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
  310. if (!curr_active)
  311. return;
  312. if (!bond->alb_info.primary_is_promisc) {
  313. if (!dev_set_promiscuity(curr_active->dev, 1))
  314. bond->alb_info.primary_is_promisc = 1;
  315. else
  316. bond->alb_info.primary_is_promisc = 0;
  317. }
  318. bond->alb_info.rlb_promisc_timeout_counter = 0;
  319. alb_send_learning_packets(curr_active, addr, true);
  320. }
  321. /* slave being removed should not be active at this point
  322. *
  323. * Caller must hold rtnl.
  324. */
  325. static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
  326. {
  327. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  328. struct rlb_client_info *rx_hash_table;
  329. u32 index, next_index;
  330. /* clear slave from rx_hashtbl */
  331. spin_lock_bh(&bond->mode_lock);
  332. rx_hash_table = bond_info->rx_hashtbl;
  333. index = bond_info->rx_hashtbl_used_head;
  334. for (; index != RLB_NULL_INDEX; index = next_index) {
  335. next_index = rx_hash_table[index].used_next;
  336. if (rx_hash_table[index].slave == slave) {
  337. struct slave *assigned_slave = rlb_next_rx_slave(bond);
  338. if (assigned_slave) {
  339. rx_hash_table[index].slave = assigned_slave;
  340. if (is_valid_ether_addr(rx_hash_table[index].mac_dst)) {
  341. bond_info->rx_hashtbl[index].ntt = 1;
  342. bond_info->rx_ntt = 1;
  343. /* A slave has been removed from the
  344. * table because it is either disabled
  345. * or being released. We must retry the
  346. * update to avoid clients from not
  347. * being updated & disconnecting when
  348. * there is stress
  349. */
  350. bond_info->rlb_update_retry_counter =
  351. RLB_UPDATE_RETRY;
  352. }
  353. } else { /* there is no active slave */
  354. rx_hash_table[index].slave = NULL;
  355. }
  356. }
  357. }
  358. spin_unlock_bh(&bond->mode_lock);
  359. if (slave != rtnl_dereference(bond->curr_active_slave))
  360. rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
  361. }
  362. static void rlb_update_client(struct rlb_client_info *client_info)
  363. {
  364. int i;
  365. if (!client_info->slave || !is_valid_ether_addr(client_info->mac_dst))
  366. return;
  367. for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
  368. struct sk_buff *skb;
  369. skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
  370. client_info->ip_dst,
  371. client_info->slave->dev,
  372. client_info->ip_src,
  373. client_info->mac_dst,
  374. client_info->slave->dev->dev_addr,
  375. client_info->mac_dst);
  376. if (!skb) {
  377. netdev_err(client_info->slave->bond->dev,
  378. "failed to create an ARP packet\n");
  379. continue;
  380. }
  381. skb->dev = client_info->slave->dev;
  382. if (client_info->vlan_id) {
  383. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  384. client_info->vlan_id);
  385. }
  386. arp_xmit(skb);
  387. }
  388. }
  389. /* sends ARP REPLIES that update the clients that need updating */
  390. static void rlb_update_rx_clients(struct bonding *bond)
  391. {
  392. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  393. struct rlb_client_info *client_info;
  394. u32 hash_index;
  395. spin_lock_bh(&bond->mode_lock);
  396. hash_index = bond_info->rx_hashtbl_used_head;
  397. for (; hash_index != RLB_NULL_INDEX;
  398. hash_index = client_info->used_next) {
  399. client_info = &(bond_info->rx_hashtbl[hash_index]);
  400. if (client_info->ntt) {
  401. rlb_update_client(client_info);
  402. if (bond_info->rlb_update_retry_counter == 0)
  403. client_info->ntt = 0;
  404. }
  405. }
  406. /* do not update the entries again until this counter is zero so that
  407. * not to confuse the clients.
  408. */
  409. bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
  410. spin_unlock_bh(&bond->mode_lock);
  411. }
  412. /* The slave was assigned a new mac address - update the clients */
  413. static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
  414. {
  415. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  416. struct rlb_client_info *client_info;
  417. int ntt = 0;
  418. u32 hash_index;
  419. spin_lock_bh(&bond->mode_lock);
  420. hash_index = bond_info->rx_hashtbl_used_head;
  421. for (; hash_index != RLB_NULL_INDEX;
  422. hash_index = client_info->used_next) {
  423. client_info = &(bond_info->rx_hashtbl[hash_index]);
  424. if ((client_info->slave == slave) &&
  425. is_valid_ether_addr(client_info->mac_dst)) {
  426. client_info->ntt = 1;
  427. ntt = 1;
  428. }
  429. }
  430. /* update the team's flag only after the whole iteration */
  431. if (ntt) {
  432. bond_info->rx_ntt = 1;
  433. /* fasten the change */
  434. bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
  435. }
  436. spin_unlock_bh(&bond->mode_lock);
  437. }
  438. /* mark all clients using src_ip to be updated */
  439. static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
  440. {
  441. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  442. struct rlb_client_info *client_info;
  443. u32 hash_index;
  444. spin_lock(&bond->mode_lock);
  445. hash_index = bond_info->rx_hashtbl_used_head;
  446. for (; hash_index != RLB_NULL_INDEX;
  447. hash_index = client_info->used_next) {
  448. client_info = &(bond_info->rx_hashtbl[hash_index]);
  449. if (!client_info->slave) {
  450. netdev_err(bond->dev, "found a client with no channel in the client's hash table\n");
  451. continue;
  452. }
  453. /* update all clients using this src_ip, that are not assigned
  454. * to the team's address (curr_active_slave) and have a known
  455. * unicast mac address.
  456. */
  457. if ((client_info->ip_src == src_ip) &&
  458. !ether_addr_equal_64bits(client_info->slave->dev->dev_addr,
  459. bond->dev->dev_addr) &&
  460. is_valid_ether_addr(client_info->mac_dst)) {
  461. client_info->ntt = 1;
  462. bond_info->rx_ntt = 1;
  463. }
  464. }
  465. spin_unlock(&bond->mode_lock);
  466. }
  467. static struct slave *rlb_choose_channel(struct sk_buff *skb,
  468. struct bonding *bond,
  469. const struct arp_pkt *arp)
  470. {
  471. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  472. struct slave *assigned_slave, *curr_active_slave;
  473. struct rlb_client_info *client_info;
  474. u32 hash_index = 0;
  475. spin_lock(&bond->mode_lock);
  476. curr_active_slave = rcu_dereference(bond->curr_active_slave);
  477. hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
  478. client_info = &(bond_info->rx_hashtbl[hash_index]);
  479. if (client_info->assigned) {
  480. if ((client_info->ip_src == arp->ip_src) &&
  481. (client_info->ip_dst == arp->ip_dst)) {
  482. /* the entry is already assigned to this client */
  483. if (!is_broadcast_ether_addr(arp->mac_dst)) {
  484. /* update mac address from arp */
  485. ether_addr_copy(client_info->mac_dst, arp->mac_dst);
  486. }
  487. ether_addr_copy(client_info->mac_src, arp->mac_src);
  488. assigned_slave = client_info->slave;
  489. if (assigned_slave) {
  490. spin_unlock(&bond->mode_lock);
  491. return assigned_slave;
  492. }
  493. } else {
  494. /* the entry is already assigned to some other client,
  495. * move the old client to primary (curr_active_slave) so
  496. * that the new client can be assigned to this entry.
  497. */
  498. if (curr_active_slave &&
  499. client_info->slave != curr_active_slave) {
  500. client_info->slave = curr_active_slave;
  501. rlb_update_client(client_info);
  502. }
  503. }
  504. }
  505. /* assign a new slave */
  506. assigned_slave = __rlb_next_rx_slave(bond);
  507. if (assigned_slave) {
  508. if (!(client_info->assigned &&
  509. client_info->ip_src == arp->ip_src)) {
  510. /* ip_src is going to be updated,
  511. * fix the src hash list
  512. */
  513. u32 hash_src = _simple_hash((u8 *)&arp->ip_src,
  514. sizeof(arp->ip_src));
  515. rlb_src_unlink(bond, hash_index);
  516. rlb_src_link(bond, hash_src, hash_index);
  517. }
  518. client_info->ip_src = arp->ip_src;
  519. client_info->ip_dst = arp->ip_dst;
  520. /* arp->mac_dst is broadcast for arp reqeusts.
  521. * will be updated with clients actual unicast mac address
  522. * upon receiving an arp reply.
  523. */
  524. ether_addr_copy(client_info->mac_dst, arp->mac_dst);
  525. ether_addr_copy(client_info->mac_src, arp->mac_src);
  526. client_info->slave = assigned_slave;
  527. if (is_valid_ether_addr(client_info->mac_dst)) {
  528. client_info->ntt = 1;
  529. bond->alb_info.rx_ntt = 1;
  530. } else {
  531. client_info->ntt = 0;
  532. }
  533. if (vlan_get_tag(skb, &client_info->vlan_id))
  534. client_info->vlan_id = 0;
  535. if (!client_info->assigned) {
  536. u32 prev_tbl_head = bond_info->rx_hashtbl_used_head;
  537. bond_info->rx_hashtbl_used_head = hash_index;
  538. client_info->used_next = prev_tbl_head;
  539. if (prev_tbl_head != RLB_NULL_INDEX) {
  540. bond_info->rx_hashtbl[prev_tbl_head].used_prev =
  541. hash_index;
  542. }
  543. client_info->assigned = 1;
  544. }
  545. }
  546. spin_unlock(&bond->mode_lock);
  547. return assigned_slave;
  548. }
  549. /* chooses (and returns) transmit channel for arp reply
  550. * does not choose channel for other arp types since they are
  551. * sent on the curr_active_slave
  552. */
  553. static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
  554. {
  555. struct slave *tx_slave = NULL;
  556. struct arp_pkt *arp;
  557. if (!pskb_network_may_pull(skb, sizeof(*arp)))
  558. return NULL;
  559. arp = (struct arp_pkt *)skb_network_header(skb);
  560. /* Don't modify or load balance ARPs that do not originate locally
  561. * (e.g.,arrive via a bridge).
  562. */
  563. if (!bond_slave_has_mac_rx(bond, arp->mac_src))
  564. return NULL;
  565. if (arp->op_code == htons(ARPOP_REPLY)) {
  566. /* the arp must be sent on the selected rx channel */
  567. tx_slave = rlb_choose_channel(skb, bond, arp);
  568. if (tx_slave)
  569. bond_hw_addr_copy(arp->mac_src, tx_slave->dev->dev_addr,
  570. tx_slave->dev->addr_len);
  571. netdev_dbg(bond->dev, "Server sent ARP Reply packet\n");
  572. } else if (arp->op_code == htons(ARPOP_REQUEST)) {
  573. /* Create an entry in the rx_hashtbl for this client as a
  574. * place holder.
  575. * When the arp reply is received the entry will be updated
  576. * with the correct unicast address of the client.
  577. */
  578. rlb_choose_channel(skb, bond, arp);
  579. /* The ARP reply packets must be delayed so that
  580. * they can cancel out the influence of the ARP request.
  581. */
  582. bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
  583. /* arp requests are broadcast and are sent on the primary
  584. * the arp request will collapse all clients on the subnet to
  585. * the primary slave. We must register these clients to be
  586. * updated with their assigned mac.
  587. */
  588. rlb_req_update_subnet_clients(bond, arp->ip_src);
  589. netdev_dbg(bond->dev, "Server sent ARP Request packet\n");
  590. }
  591. return tx_slave;
  592. }
  593. static void rlb_rebalance(struct bonding *bond)
  594. {
  595. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  596. struct slave *assigned_slave;
  597. struct rlb_client_info *client_info;
  598. int ntt;
  599. u32 hash_index;
  600. spin_lock_bh(&bond->mode_lock);
  601. ntt = 0;
  602. hash_index = bond_info->rx_hashtbl_used_head;
  603. for (; hash_index != RLB_NULL_INDEX;
  604. hash_index = client_info->used_next) {
  605. client_info = &(bond_info->rx_hashtbl[hash_index]);
  606. assigned_slave = __rlb_next_rx_slave(bond);
  607. if (assigned_slave && (client_info->slave != assigned_slave)) {
  608. client_info->slave = assigned_slave;
  609. if (!is_zero_ether_addr(client_info->mac_dst)) {
  610. client_info->ntt = 1;
  611. ntt = 1;
  612. }
  613. }
  614. }
  615. /* update the team's flag only after the whole iteration */
  616. if (ntt)
  617. bond_info->rx_ntt = 1;
  618. spin_unlock_bh(&bond->mode_lock);
  619. }
  620. /* Caller must hold mode_lock */
  621. static void rlb_init_table_entry_dst(struct rlb_client_info *entry)
  622. {
  623. entry->used_next = RLB_NULL_INDEX;
  624. entry->used_prev = RLB_NULL_INDEX;
  625. entry->assigned = 0;
  626. entry->slave = NULL;
  627. entry->vlan_id = 0;
  628. }
  629. static void rlb_init_table_entry_src(struct rlb_client_info *entry)
  630. {
  631. entry->src_first = RLB_NULL_INDEX;
  632. entry->src_prev = RLB_NULL_INDEX;
  633. entry->src_next = RLB_NULL_INDEX;
  634. }
  635. static void rlb_init_table_entry(struct rlb_client_info *entry)
  636. {
  637. memset(entry, 0, sizeof(struct rlb_client_info));
  638. rlb_init_table_entry_dst(entry);
  639. rlb_init_table_entry_src(entry);
  640. }
  641. static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index)
  642. {
  643. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  644. u32 next_index = bond_info->rx_hashtbl[index].used_next;
  645. u32 prev_index = bond_info->rx_hashtbl[index].used_prev;
  646. if (index == bond_info->rx_hashtbl_used_head)
  647. bond_info->rx_hashtbl_used_head = next_index;
  648. if (prev_index != RLB_NULL_INDEX)
  649. bond_info->rx_hashtbl[prev_index].used_next = next_index;
  650. if (next_index != RLB_NULL_INDEX)
  651. bond_info->rx_hashtbl[next_index].used_prev = prev_index;
  652. }
  653. /* unlink a rlb hash table entry from the src list */
  654. static void rlb_src_unlink(struct bonding *bond, u32 index)
  655. {
  656. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  657. u32 next_index = bond_info->rx_hashtbl[index].src_next;
  658. u32 prev_index = bond_info->rx_hashtbl[index].src_prev;
  659. bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX;
  660. bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX;
  661. if (next_index != RLB_NULL_INDEX)
  662. bond_info->rx_hashtbl[next_index].src_prev = prev_index;
  663. if (prev_index == RLB_NULL_INDEX)
  664. return;
  665. /* is prev_index pointing to the head of this list? */
  666. if (bond_info->rx_hashtbl[prev_index].src_first == index)
  667. bond_info->rx_hashtbl[prev_index].src_first = next_index;
  668. else
  669. bond_info->rx_hashtbl[prev_index].src_next = next_index;
  670. }
  671. static void rlb_delete_table_entry(struct bonding *bond, u32 index)
  672. {
  673. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  674. struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
  675. rlb_delete_table_entry_dst(bond, index);
  676. rlb_init_table_entry_dst(entry);
  677. rlb_src_unlink(bond, index);
  678. }
  679. /* add the rx_hashtbl[ip_dst_hash] entry to the list
  680. * of entries with identical ip_src_hash
  681. */
  682. static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash)
  683. {
  684. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  685. u32 next;
  686. bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash;
  687. next = bond_info->rx_hashtbl[ip_src_hash].src_first;
  688. bond_info->rx_hashtbl[ip_dst_hash].src_next = next;
  689. if (next != RLB_NULL_INDEX)
  690. bond_info->rx_hashtbl[next].src_prev = ip_dst_hash;
  691. bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash;
  692. }
  693. /* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does
  694. * not match arp->mac_src
  695. */
  696. static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp)
  697. {
  698. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  699. u32 ip_src_hash = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
  700. u32 index;
  701. spin_lock_bh(&bond->mode_lock);
  702. index = bond_info->rx_hashtbl[ip_src_hash].src_first;
  703. while (index != RLB_NULL_INDEX) {
  704. struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
  705. u32 next_index = entry->src_next;
  706. if (entry->ip_src == arp->ip_src &&
  707. !ether_addr_equal_64bits(arp->mac_src, entry->mac_src))
  708. rlb_delete_table_entry(bond, index);
  709. index = next_index;
  710. }
  711. spin_unlock_bh(&bond->mode_lock);
  712. }
  713. static int rlb_initialize(struct bonding *bond)
  714. {
  715. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  716. struct rlb_client_info *new_hashtbl;
  717. int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
  718. int i;
  719. new_hashtbl = kmalloc(size, GFP_KERNEL);
  720. if (!new_hashtbl)
  721. return -1;
  722. spin_lock_bh(&bond->mode_lock);
  723. bond_info->rx_hashtbl = new_hashtbl;
  724. bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
  725. for (i = 0; i < RLB_HASH_TABLE_SIZE; i++)
  726. rlb_init_table_entry(bond_info->rx_hashtbl + i);
  727. spin_unlock_bh(&bond->mode_lock);
  728. /* register to receive ARPs */
  729. bond->recv_probe = rlb_arp_recv;
  730. return 0;
  731. }
  732. static void rlb_deinitialize(struct bonding *bond)
  733. {
  734. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  735. spin_lock_bh(&bond->mode_lock);
  736. kfree(bond_info->rx_hashtbl);
  737. bond_info->rx_hashtbl = NULL;
  738. bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
  739. spin_unlock_bh(&bond->mode_lock);
  740. }
  741. static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  742. {
  743. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  744. u32 curr_index;
  745. spin_lock_bh(&bond->mode_lock);
  746. curr_index = bond_info->rx_hashtbl_used_head;
  747. while (curr_index != RLB_NULL_INDEX) {
  748. struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
  749. u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
  750. if (curr->vlan_id == vlan_id)
  751. rlb_delete_table_entry(bond, curr_index);
  752. curr_index = next_index;
  753. }
  754. spin_unlock_bh(&bond->mode_lock);
  755. }
  756. /*********************** tlb/rlb shared functions *********************/
  757. static void alb_send_lp_vid(struct slave *slave, u8 mac_addr[],
  758. __be16 vlan_proto, u16 vid)
  759. {
  760. struct learning_pkt pkt;
  761. struct sk_buff *skb;
  762. int size = sizeof(struct learning_pkt);
  763. memset(&pkt, 0, size);
  764. ether_addr_copy(pkt.mac_dst, mac_addr);
  765. ether_addr_copy(pkt.mac_src, mac_addr);
  766. pkt.type = cpu_to_be16(ETH_P_LOOPBACK);
  767. skb = dev_alloc_skb(size);
  768. if (!skb)
  769. return;
  770. skb_put_data(skb, &pkt, size);
  771. skb_reset_mac_header(skb);
  772. skb->network_header = skb->mac_header + ETH_HLEN;
  773. skb->protocol = pkt.type;
  774. skb->priority = TC_PRIO_CONTROL;
  775. skb->dev = slave->dev;
  776. netdev_dbg(slave->bond->dev,
  777. "Send learning packet: dev %s mac %pM vlan %d\n",
  778. slave->dev->name, mac_addr, vid);
  779. if (vid)
  780. __vlan_hwaccel_put_tag(skb, vlan_proto, vid);
  781. dev_queue_xmit(skb);
  782. }
  783. struct alb_walk_data {
  784. struct bonding *bond;
  785. struct slave *slave;
  786. u8 *mac_addr;
  787. bool strict_match;
  788. };
  789. static int alb_upper_dev_walk(struct net_device *upper, void *_data)
  790. {
  791. struct alb_walk_data *data = _data;
  792. bool strict_match = data->strict_match;
  793. struct bonding *bond = data->bond;
  794. struct slave *slave = data->slave;
  795. u8 *mac_addr = data->mac_addr;
  796. struct bond_vlan_tag *tags;
  797. if (is_vlan_dev(upper) &&
  798. bond->nest_level == vlan_get_encap_level(upper) - 1) {
  799. if (upper->addr_assign_type == NET_ADDR_STOLEN) {
  800. alb_send_lp_vid(slave, mac_addr,
  801. vlan_dev_vlan_proto(upper),
  802. vlan_dev_vlan_id(upper));
  803. } else {
  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 netdev_tx_t 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. netdev_tx_t 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 ? READ_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. netdev_tx_t 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. skb_reset_mac_header(skb);
  1172. eth_data = eth_hdr(skb);
  1173. switch (ntohs(skb->protocol)) {
  1174. case ETH_P_IP: {
  1175. const struct iphdr *iph;
  1176. if (is_broadcast_ether_addr(eth_data->h_dest) ||
  1177. !pskb_network_may_pull(skb, sizeof(*iph))) {
  1178. do_tx_balance = false;
  1179. break;
  1180. }
  1181. iph = ip_hdr(skb);
  1182. if (iph->daddr == ip_bcast || iph->protocol == IPPROTO_IGMP) {
  1183. do_tx_balance = false;
  1184. break;
  1185. }
  1186. hash_start = (char *)&(iph->daddr);
  1187. hash_size = sizeof(iph->daddr);
  1188. break;
  1189. }
  1190. case ETH_P_IPV6: {
  1191. const struct ipv6hdr *ip6hdr;
  1192. /* IPv6 doesn't really use broadcast mac address, but leave
  1193. * that here just in case.
  1194. */
  1195. if (is_broadcast_ether_addr(eth_data->h_dest)) {
  1196. do_tx_balance = false;
  1197. break;
  1198. }
  1199. /* IPv6 uses all-nodes multicast as an equivalent to
  1200. * broadcasts in IPv4.
  1201. */
  1202. if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
  1203. do_tx_balance = false;
  1204. break;
  1205. }
  1206. if (!pskb_network_may_pull(skb, sizeof(*ip6hdr))) {
  1207. do_tx_balance = false;
  1208. break;
  1209. }
  1210. /* Additionally, DAD probes should not be tx-balanced as that
  1211. * will lead to false positives for duplicate addresses and
  1212. * prevent address configuration from working.
  1213. */
  1214. ip6hdr = ipv6_hdr(skb);
  1215. if (ipv6_addr_any(&ip6hdr->saddr)) {
  1216. do_tx_balance = false;
  1217. break;
  1218. }
  1219. hash_start = (char *)&ip6hdr->daddr;
  1220. hash_size = sizeof(ip6hdr->daddr);
  1221. break;
  1222. }
  1223. case ETH_P_IPX: {
  1224. const struct ipxhdr *ipxhdr;
  1225. if (pskb_network_may_pull(skb, sizeof(*ipxhdr))) {
  1226. do_tx_balance = false;
  1227. break;
  1228. }
  1229. ipxhdr = (struct ipxhdr *)skb_network_header(skb);
  1230. if (ipxhdr->ipx_checksum != IPX_NO_CHECKSUM) {
  1231. /* something is wrong with this packet */
  1232. do_tx_balance = false;
  1233. break;
  1234. }
  1235. if (ipxhdr->ipx_type != IPX_TYPE_NCP) {
  1236. /* The only protocol worth balancing in
  1237. * this family since it has an "ARP" like
  1238. * mechanism
  1239. */
  1240. do_tx_balance = false;
  1241. break;
  1242. }
  1243. eth_data = eth_hdr(skb);
  1244. hash_start = (char *)eth_data->h_dest;
  1245. hash_size = ETH_ALEN;
  1246. break;
  1247. }
  1248. case ETH_P_ARP:
  1249. do_tx_balance = false;
  1250. if (bond_info->rlb_enabled)
  1251. tx_slave = rlb_arp_xmit(skb, bond);
  1252. break;
  1253. default:
  1254. do_tx_balance = false;
  1255. break;
  1256. }
  1257. if (do_tx_balance) {
  1258. if (bond->params.tlb_dynamic_lb) {
  1259. hash_index = _simple_hash(hash_start, hash_size);
  1260. tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
  1261. } else {
  1262. /*
  1263. * do_tx_balance means we are free to select the tx_slave
  1264. * So we do exactly what tlb would do for hash selection
  1265. */
  1266. struct bond_up_slave *slaves;
  1267. unsigned int count;
  1268. slaves = rcu_dereference(bond->slave_arr);
  1269. count = slaves ? READ_ONCE(slaves->count) : 0;
  1270. if (likely(count))
  1271. tx_slave = slaves->arr[bond_xmit_hash(bond, skb) %
  1272. count];
  1273. }
  1274. }
  1275. return bond_do_alb_xmit(skb, bond, tx_slave);
  1276. }
  1277. void bond_alb_monitor(struct work_struct *work)
  1278. {
  1279. struct bonding *bond = container_of(work, struct bonding,
  1280. alb_work.work);
  1281. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1282. struct list_head *iter;
  1283. struct slave *slave;
  1284. if (!bond_has_slaves(bond)) {
  1285. bond_info->tx_rebalance_counter = 0;
  1286. bond_info->lp_counter = 0;
  1287. goto re_arm;
  1288. }
  1289. rcu_read_lock();
  1290. bond_info->tx_rebalance_counter++;
  1291. bond_info->lp_counter++;
  1292. /* send learning packets */
  1293. if (bond_info->lp_counter >= BOND_ALB_LP_TICKS(bond)) {
  1294. bool strict_match;
  1295. bond_for_each_slave_rcu(bond, slave, iter) {
  1296. /* If updating current_active, use all currently
  1297. * user mac addreses (!strict_match). Otherwise, only
  1298. * use mac of the slave device.
  1299. * In RLB mode, we always use strict matches.
  1300. */
  1301. strict_match = (slave != rcu_access_pointer(bond->curr_active_slave) ||
  1302. bond_info->rlb_enabled);
  1303. alb_send_learning_packets(slave, slave->dev->dev_addr,
  1304. strict_match);
  1305. }
  1306. bond_info->lp_counter = 0;
  1307. }
  1308. /* rebalance tx traffic */
  1309. if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
  1310. bond_for_each_slave_rcu(bond, slave, iter) {
  1311. tlb_clear_slave(bond, slave, 1);
  1312. if (slave == rcu_access_pointer(bond->curr_active_slave)) {
  1313. SLAVE_TLB_INFO(slave).load =
  1314. bond_info->unbalanced_load /
  1315. BOND_TLB_REBALANCE_INTERVAL;
  1316. bond_info->unbalanced_load = 0;
  1317. }
  1318. }
  1319. bond_info->tx_rebalance_counter = 0;
  1320. }
  1321. if (bond_info->rlb_enabled) {
  1322. if (bond_info->primary_is_promisc &&
  1323. (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
  1324. /* dev_set_promiscuity requires rtnl and
  1325. * nothing else. Avoid race with bond_close.
  1326. */
  1327. rcu_read_unlock();
  1328. if (!rtnl_trylock())
  1329. goto re_arm;
  1330. bond_info->rlb_promisc_timeout_counter = 0;
  1331. /* If the primary was set to promiscuous mode
  1332. * because a slave was disabled then
  1333. * it can now leave promiscuous mode.
  1334. */
  1335. dev_set_promiscuity(rtnl_dereference(bond->curr_active_slave)->dev,
  1336. -1);
  1337. bond_info->primary_is_promisc = 0;
  1338. rtnl_unlock();
  1339. rcu_read_lock();
  1340. }
  1341. if (bond_info->rlb_rebalance) {
  1342. bond_info->rlb_rebalance = 0;
  1343. rlb_rebalance(bond);
  1344. }
  1345. /* check if clients need updating */
  1346. if (bond_info->rx_ntt) {
  1347. if (bond_info->rlb_update_delay_counter) {
  1348. --bond_info->rlb_update_delay_counter;
  1349. } else {
  1350. rlb_update_rx_clients(bond);
  1351. if (bond_info->rlb_update_retry_counter)
  1352. --bond_info->rlb_update_retry_counter;
  1353. else
  1354. bond_info->rx_ntt = 0;
  1355. }
  1356. }
  1357. }
  1358. rcu_read_unlock();
  1359. re_arm:
  1360. queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
  1361. }
  1362. /* assumption: called before the slave is attached to the bond
  1363. * and not locked by the bond lock
  1364. */
  1365. int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
  1366. {
  1367. int res;
  1368. res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
  1369. slave->dev->addr_len);
  1370. if (res)
  1371. return res;
  1372. res = alb_handle_addr_collision_on_attach(bond, slave);
  1373. if (res)
  1374. return res;
  1375. tlb_init_slave(slave);
  1376. /* order a rebalance ASAP */
  1377. bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1378. if (bond->alb_info.rlb_enabled)
  1379. bond->alb_info.rlb_rebalance = 1;
  1380. return 0;
  1381. }
  1382. /* Remove slave from tlb and rlb hash tables, and fix up MAC addresses
  1383. * if necessary.
  1384. *
  1385. * Caller must hold RTNL and no other locks
  1386. */
  1387. void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
  1388. {
  1389. if (bond_has_slaves(bond))
  1390. alb_change_hw_addr_on_detach(bond, slave);
  1391. tlb_clear_slave(bond, slave, 0);
  1392. if (bond->alb_info.rlb_enabled) {
  1393. bond->alb_info.rx_slave = NULL;
  1394. rlb_clear_slave(bond, slave);
  1395. }
  1396. }
  1397. void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
  1398. {
  1399. struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
  1400. if (link == BOND_LINK_DOWN) {
  1401. tlb_clear_slave(bond, slave, 0);
  1402. if (bond->alb_info.rlb_enabled)
  1403. rlb_clear_slave(bond, slave);
  1404. } else if (link == BOND_LINK_UP) {
  1405. /* order a rebalance ASAP */
  1406. bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
  1407. if (bond->alb_info.rlb_enabled) {
  1408. bond->alb_info.rlb_rebalance = 1;
  1409. /* If the updelay module parameter is smaller than the
  1410. * forwarding delay of the switch the rebalance will
  1411. * not work because the rebalance arp replies will
  1412. * not be forwarded to the clients..
  1413. */
  1414. }
  1415. }
  1416. if (bond_is_nondyn_tlb(bond)) {
  1417. if (bond_update_slave_arr(bond, NULL))
  1418. pr_err("Failed to build slave-array for TLB mode.\n");
  1419. }
  1420. }
  1421. /**
  1422. * bond_alb_handle_active_change - assign new curr_active_slave
  1423. * @bond: our bonding struct
  1424. * @new_slave: new slave to assign
  1425. *
  1426. * Set the bond->curr_active_slave to @new_slave and handle
  1427. * mac address swapping and promiscuity changes as needed.
  1428. *
  1429. * Caller must hold RTNL
  1430. */
  1431. void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
  1432. {
  1433. struct slave *swap_slave;
  1434. struct slave *curr_active;
  1435. curr_active = rtnl_dereference(bond->curr_active_slave);
  1436. if (curr_active == new_slave)
  1437. return;
  1438. if (curr_active && bond->alb_info.primary_is_promisc) {
  1439. dev_set_promiscuity(curr_active->dev, -1);
  1440. bond->alb_info.primary_is_promisc = 0;
  1441. bond->alb_info.rlb_promisc_timeout_counter = 0;
  1442. }
  1443. swap_slave = curr_active;
  1444. rcu_assign_pointer(bond->curr_active_slave, new_slave);
  1445. if (!new_slave || !bond_has_slaves(bond))
  1446. return;
  1447. /* set the new curr_active_slave to the bonds mac address
  1448. * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
  1449. */
  1450. if (!swap_slave)
  1451. swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
  1452. /* Arrange for swap_slave and new_slave to temporarily be
  1453. * ignored so we can mess with their MAC addresses without
  1454. * fear of interference from transmit activity.
  1455. */
  1456. if (swap_slave)
  1457. tlb_clear_slave(bond, swap_slave, 1);
  1458. tlb_clear_slave(bond, new_slave, 1);
  1459. /* in TLB mode, the slave might flip down/up with the old dev_addr,
  1460. * and thus filter bond->dev_addr's packets, so force bond's mac
  1461. */
  1462. if (BOND_MODE(bond) == BOND_MODE_TLB) {
  1463. struct sockaddr_storage ss;
  1464. u8 tmp_addr[MAX_ADDR_LEN];
  1465. bond_hw_addr_copy(tmp_addr, new_slave->dev->dev_addr,
  1466. new_slave->dev->addr_len);
  1467. bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
  1468. bond->dev->addr_len);
  1469. ss.ss_family = bond->dev->type;
  1470. /* we don't care if it can't change its mac, best effort */
  1471. dev_set_mac_address(new_slave->dev, (struct sockaddr *)&ss);
  1472. bond_hw_addr_copy(new_slave->dev->dev_addr, tmp_addr,
  1473. new_slave->dev->addr_len);
  1474. }
  1475. /* curr_active_slave must be set before calling alb_swap_mac_addr */
  1476. if (swap_slave) {
  1477. /* swap mac address */
  1478. alb_swap_mac_addr(swap_slave, new_slave);
  1479. alb_fasten_mac_swap(bond, swap_slave, new_slave);
  1480. } else {
  1481. /* set the new_slave to the bond mac address */
  1482. alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
  1483. bond->dev->addr_len);
  1484. alb_send_learning_packets(new_slave, bond->dev->dev_addr,
  1485. false);
  1486. }
  1487. }
  1488. /* Called with RTNL */
  1489. int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
  1490. {
  1491. struct bonding *bond = netdev_priv(bond_dev);
  1492. struct sockaddr_storage *ss = addr;
  1493. struct slave *curr_active;
  1494. struct slave *swap_slave;
  1495. int res;
  1496. if (!is_valid_ether_addr(ss->__data))
  1497. return -EADDRNOTAVAIL;
  1498. res = alb_set_mac_address(bond, addr);
  1499. if (res)
  1500. return res;
  1501. bond_hw_addr_copy(bond_dev->dev_addr, ss->__data, bond_dev->addr_len);
  1502. /* If there is no curr_active_slave there is nothing else to do.
  1503. * Otherwise we'll need to pass the new address to it and handle
  1504. * duplications.
  1505. */
  1506. curr_active = rtnl_dereference(bond->curr_active_slave);
  1507. if (!curr_active)
  1508. return 0;
  1509. swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
  1510. if (swap_slave) {
  1511. alb_swap_mac_addr(swap_slave, curr_active);
  1512. alb_fasten_mac_swap(bond, swap_slave, curr_active);
  1513. } else {
  1514. alb_set_slave_mac_addr(curr_active, bond_dev->dev_addr,
  1515. bond_dev->addr_len);
  1516. alb_send_learning_packets(curr_active,
  1517. bond_dev->dev_addr, false);
  1518. if (bond->alb_info.rlb_enabled) {
  1519. /* inform clients mac address has changed */
  1520. rlb_req_update_slave_clients(bond, curr_active);
  1521. }
  1522. }
  1523. return 0;
  1524. }
  1525. void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
  1526. {
  1527. if (bond->alb_info.rlb_enabled)
  1528. rlb_clear_vlan(bond, vlan_id);
  1529. }