bond_alb.c 45 KB

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