bond_alb.c 45 KB

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