bond_alb.c 45 KB

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