bond_alb.c 43 KB

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