bond_alb.c 45 KB

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