bond_alb.c 43 KB

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