bond_alb.c 44 KB

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