bond_3ad.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  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 Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59
  16. * 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. * Changes:
  23. *
  24. * 2003/05/01 - Tsippy Mendelson <tsippy.mendelson at intel dot com> and
  25. * Amir Noam <amir.noam at intel dot com>
  26. * - Added support for lacp_rate module param.
  27. *
  28. * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
  29. * - Based on discussion on mailing list, changed locking scheme
  30. * to use lock/unlock or lock_bh/unlock_bh appropriately instead
  31. * of lock_irqsave/unlock_irqrestore. The new scheme helps exposing
  32. * hidden bugs and solves system hangs that occurred due to the fact
  33. * that holding lock_irqsave doesn't prevent softirqs from running.
  34. * This also increases total throughput since interrupts are not
  35. * blocked on each transmitted packets or monitor timeout.
  36. *
  37. * 2003/05/01 - Shmulik Hen <shmulik.hen at intel dot com>
  38. * - Renamed bond_3ad_link_status_changed() to
  39. * bond_3ad_handle_link_change() for compatibility with TLB.
  40. *
  41. * 2003/05/20 - Amir Noam <amir.noam at intel dot com>
  42. * - Fix long fail over time when releasing last slave of an active
  43. * aggregator - send LACPDU on unbind of slave to tell partner this
  44. * port is no longer aggregatable.
  45. *
  46. * 2003/06/25 - Tsippy Mendelson <tsippy.mendelson at intel dot com>
  47. * - Send LACPDU as highest priority packet to further fix the above
  48. * problem on very high Tx traffic load where packets may get dropped
  49. * by the slave.
  50. *
  51. * 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
  52. * - Code cleanup and style changes
  53. */
  54. //#define BONDING_DEBUG 1
  55. #include <linux/skbuff.h>
  56. #include <linux/if_ether.h>
  57. #include <linux/netdevice.h>
  58. #include <linux/spinlock.h>
  59. #include <linux/ethtool.h>
  60. #include <linux/if_bonding.h>
  61. #include <linux/pkt_sched.h>
  62. #include "bonding.h"
  63. #include "bond_3ad.h"
  64. // General definitions
  65. #define AD_SHORT_TIMEOUT 1
  66. #define AD_LONG_TIMEOUT 0
  67. #define AD_STANDBY 0x2
  68. #define AD_MAX_TX_IN_SECOND 3
  69. #define AD_COLLECTOR_MAX_DELAY 0
  70. // Timer definitions(43.4.4 in the 802.3ad standard)
  71. #define AD_FAST_PERIODIC_TIME 1
  72. #define AD_SLOW_PERIODIC_TIME 30
  73. #define AD_SHORT_TIMEOUT_TIME (3*AD_FAST_PERIODIC_TIME)
  74. #define AD_LONG_TIMEOUT_TIME (3*AD_SLOW_PERIODIC_TIME)
  75. #define AD_CHURN_DETECTION_TIME 60
  76. #define AD_AGGREGATE_WAIT_TIME 2
  77. // Port state definitions(43.4.2.2 in the 802.3ad standard)
  78. #define AD_STATE_LACP_ACTIVITY 0x1
  79. #define AD_STATE_LACP_TIMEOUT 0x2
  80. #define AD_STATE_AGGREGATION 0x4
  81. #define AD_STATE_SYNCHRONIZATION 0x8
  82. #define AD_STATE_COLLECTING 0x10
  83. #define AD_STATE_DISTRIBUTING 0x20
  84. #define AD_STATE_DEFAULTED 0x40
  85. #define AD_STATE_EXPIRED 0x80
  86. // Port Variables definitions used by the State Machines(43.4.7 in the 802.3ad standard)
  87. #define AD_PORT_BEGIN 0x1
  88. #define AD_PORT_LACP_ENABLED 0x2
  89. #define AD_PORT_ACTOR_CHURN 0x4
  90. #define AD_PORT_PARTNER_CHURN 0x8
  91. #define AD_PORT_READY 0x10
  92. #define AD_PORT_READY_N 0x20
  93. #define AD_PORT_MATCHED 0x40
  94. #define AD_PORT_STANDBY 0x80
  95. #define AD_PORT_SELECTED 0x100
  96. #define AD_PORT_MOVED 0x200
  97. // Port Key definitions
  98. // key is determined according to the link speed, duplex and
  99. // user key(which is yet not supported)
  100. // ------------------------------------------------------------
  101. // Port key : | User key | Speed |Duplex|
  102. // ------------------------------------------------------------
  103. // 16 6 1 0
  104. #define AD_DUPLEX_KEY_BITS 0x1
  105. #define AD_SPEED_KEY_BITS 0x3E
  106. #define AD_USER_KEY_BITS 0xFFC0
  107. //dalloun
  108. #define AD_LINK_SPEED_BITMASK_1MBPS 0x1
  109. #define AD_LINK_SPEED_BITMASK_10MBPS 0x2
  110. #define AD_LINK_SPEED_BITMASK_100MBPS 0x4
  111. #define AD_LINK_SPEED_BITMASK_1000MBPS 0x8
  112. //endalloun
  113. // compare MAC addresses
  114. #define MAC_ADDRESS_COMPARE(A, B) memcmp(A, B, ETH_ALEN)
  115. static struct mac_addr null_mac_addr = {{0, 0, 0, 0, 0, 0}};
  116. static u16 ad_ticks_per_sec;
  117. static const int ad_delta_in_ticks = (AD_TIMER_INTERVAL * HZ) / 1000;
  118. // ================= 3AD api to bonding and kernel code ==================
  119. static u16 __get_link_speed(struct port *port);
  120. static u8 __get_duplex(struct port *port);
  121. static inline void __initialize_port_locks(struct port *port);
  122. //conversions
  123. static void __ntohs_lacpdu(struct lacpdu *lacpdu);
  124. static u16 __ad_timer_to_ticks(u16 timer_type, u16 Par);
  125. // ================= ad code helper functions ==================
  126. //needed by ad_rx_machine(...)
  127. static void __record_pdu(struct lacpdu *lacpdu, struct port *port);
  128. static void __record_default(struct port *port);
  129. static void __update_selected(struct lacpdu *lacpdu, struct port *port);
  130. static void __update_default_selected(struct port *port);
  131. static void __choose_matched(struct lacpdu *lacpdu, struct port *port);
  132. static void __update_ntt(struct lacpdu *lacpdu, struct port *port);
  133. //needed for ad_mux_machine(..)
  134. static void __attach_bond_to_agg(struct port *port);
  135. static void __detach_bond_from_agg(struct port *port);
  136. static int __agg_ports_are_ready(struct aggregator *aggregator);
  137. static void __set_agg_ports_ready(struct aggregator *aggregator, int val);
  138. //needed for ad_agg_selection_logic(...)
  139. static u32 __get_agg_bandwidth(struct aggregator *aggregator);
  140. static struct aggregator *__get_active_agg(struct aggregator *aggregator);
  141. // ================= main 802.3ad protocol functions ==================
  142. static int ad_lacpdu_send(struct port *port);
  143. static int ad_marker_send(struct port *port, struct marker *marker);
  144. static void ad_mux_machine(struct port *port);
  145. static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port);
  146. static void ad_tx_machine(struct port *port);
  147. static void ad_periodic_machine(struct port *port);
  148. static void ad_port_selection_logic(struct port *port);
  149. static void ad_agg_selection_logic(struct aggregator *aggregator);
  150. static void ad_clear_agg(struct aggregator *aggregator);
  151. static void ad_initialize_agg(struct aggregator *aggregator);
  152. static void ad_initialize_port(struct port *port, int lacp_fast);
  153. static void ad_initialize_lacpdu(struct lacpdu *Lacpdu);
  154. static void ad_enable_collecting_distributing(struct port *port);
  155. static void ad_disable_collecting_distributing(struct port *port);
  156. static void ad_marker_info_received(struct marker *marker_info, struct port *port);
  157. static void ad_marker_response_received(struct marker *marker, struct port *port);
  158. /////////////////////////////////////////////////////////////////////////////////
  159. // ================= api to bonding and kernel code ==================
  160. /////////////////////////////////////////////////////////////////////////////////
  161. /**
  162. * __get_bond_by_port - get the port's bonding struct
  163. * @port: the port we're looking at
  164. *
  165. * Return @port's bonding struct, or %NULL if it can't be found.
  166. */
  167. static inline struct bonding *__get_bond_by_port(struct port *port)
  168. {
  169. if (port->slave == NULL) {
  170. return NULL;
  171. }
  172. return bond_get_bond_by_slave(port->slave);
  173. }
  174. /**
  175. * __get_first_port - get the first port in the bond
  176. * @bond: the bond we're looking at
  177. *
  178. * Return the port of the first slave in @bond, or %NULL if it can't be found.
  179. */
  180. static inline struct port *__get_first_port(struct bonding *bond)
  181. {
  182. if (bond->slave_cnt == 0) {
  183. return NULL;
  184. }
  185. return &(SLAVE_AD_INFO(bond->first_slave).port);
  186. }
  187. /**
  188. * __get_next_port - get the next port in the bond
  189. * @port: the port we're looking at
  190. *
  191. * Return the port of the slave that is next in line of @port's slave in the
  192. * bond, or %NULL if it can't be found.
  193. */
  194. static inline struct port *__get_next_port(struct port *port)
  195. {
  196. struct bonding *bond = __get_bond_by_port(port);
  197. struct slave *slave = port->slave;
  198. // If there's no bond for this port, or this is the last slave
  199. if ((bond == NULL) || (slave->next == bond->first_slave)) {
  200. return NULL;
  201. }
  202. return &(SLAVE_AD_INFO(slave->next).port);
  203. }
  204. /**
  205. * __get_first_agg - get the first aggregator in the bond
  206. * @bond: the bond we're looking at
  207. *
  208. * Return the aggregator of the first slave in @bond, or %NULL if it can't be
  209. * found.
  210. */
  211. static inline struct aggregator *__get_first_agg(struct port *port)
  212. {
  213. struct bonding *bond = __get_bond_by_port(port);
  214. // If there's no bond for this port, or bond has no slaves
  215. if ((bond == NULL) || (bond->slave_cnt == 0)) {
  216. return NULL;
  217. }
  218. return &(SLAVE_AD_INFO(bond->first_slave).aggregator);
  219. }
  220. /**
  221. * __get_next_agg - get the next aggregator in the bond
  222. * @aggregator: the aggregator we're looking at
  223. *
  224. * Return the aggregator of the slave that is next in line of @aggregator's
  225. * slave in the bond, or %NULL if it can't be found.
  226. */
  227. static inline struct aggregator *__get_next_agg(struct aggregator *aggregator)
  228. {
  229. struct slave *slave = aggregator->slave;
  230. struct bonding *bond = bond_get_bond_by_slave(slave);
  231. // If there's no bond for this aggregator, or this is the last slave
  232. if ((bond == NULL) || (slave->next == bond->first_slave)) {
  233. return NULL;
  234. }
  235. return &(SLAVE_AD_INFO(slave->next).aggregator);
  236. }
  237. /**
  238. * __disable_port - disable the port's slave
  239. * @port: the port we're looking at
  240. *
  241. */
  242. static inline void __disable_port(struct port *port)
  243. {
  244. bond_set_slave_inactive_flags(port->slave);
  245. }
  246. /**
  247. * __enable_port - enable the port's slave, if it's up
  248. * @port: the port we're looking at
  249. *
  250. */
  251. static inline void __enable_port(struct port *port)
  252. {
  253. struct slave *slave = port->slave;
  254. if ((slave->link == BOND_LINK_UP) && IS_UP(slave->dev)) {
  255. bond_set_slave_active_flags(slave);
  256. }
  257. }
  258. /**
  259. * __port_is_enabled - check if the port's slave is in active state
  260. * @port: the port we're looking at
  261. *
  262. */
  263. static inline int __port_is_enabled(struct port *port)
  264. {
  265. return(port->slave->state == BOND_STATE_ACTIVE);
  266. }
  267. /**
  268. * __get_agg_selection_mode - get the aggregator selection mode
  269. * @port: the port we're looking at
  270. *
  271. * Get the aggregator selection mode. Can be %BANDWIDTH or %COUNT.
  272. */
  273. static inline u32 __get_agg_selection_mode(struct port *port)
  274. {
  275. struct bonding *bond = __get_bond_by_port(port);
  276. if (bond == NULL) {
  277. return AD_BANDWIDTH;
  278. }
  279. return BOND_AD_INFO(bond).agg_select_mode;
  280. }
  281. /**
  282. * __check_agg_selection_timer - check if the selection timer has expired
  283. * @port: the port we're looking at
  284. *
  285. */
  286. static inline int __check_agg_selection_timer(struct port *port)
  287. {
  288. struct bonding *bond = __get_bond_by_port(port);
  289. if (bond == NULL) {
  290. return 0;
  291. }
  292. return BOND_AD_INFO(bond).agg_select_timer ? 1 : 0;
  293. }
  294. /**
  295. * __get_rx_machine_lock - lock the port's RX machine
  296. * @port: the port we're looking at
  297. *
  298. */
  299. static inline void __get_rx_machine_lock(struct port *port)
  300. {
  301. spin_lock(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
  302. }
  303. /**
  304. * __release_rx_machine_lock - unlock the port's RX machine
  305. * @port: the port we're looking at
  306. *
  307. */
  308. static inline void __release_rx_machine_lock(struct port *port)
  309. {
  310. spin_unlock(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
  311. }
  312. /**
  313. * __get_link_speed - get a port's speed
  314. * @port: the port we're looking at
  315. *
  316. * Return @port's speed in 802.3ad bitmask format. i.e. one of:
  317. * 0,
  318. * %AD_LINK_SPEED_BITMASK_10MBPS,
  319. * %AD_LINK_SPEED_BITMASK_100MBPS,
  320. * %AD_LINK_SPEED_BITMASK_1000MBPS
  321. */
  322. static u16 __get_link_speed(struct port *port)
  323. {
  324. struct slave *slave = port->slave;
  325. u16 speed;
  326. /* this if covers only a special case: when the configuration starts with
  327. * link down, it sets the speed to 0.
  328. * This is done in spite of the fact that the e100 driver reports 0 to be
  329. * compatible with MVT in the future.*/
  330. if (slave->link != BOND_LINK_UP) {
  331. speed=0;
  332. } else {
  333. switch (slave->speed) {
  334. case SPEED_10:
  335. speed = AD_LINK_SPEED_BITMASK_10MBPS;
  336. break;
  337. case SPEED_100:
  338. speed = AD_LINK_SPEED_BITMASK_100MBPS;
  339. break;
  340. case SPEED_1000:
  341. speed = AD_LINK_SPEED_BITMASK_1000MBPS;
  342. break;
  343. default:
  344. speed = 0; // unknown speed value from ethtool. shouldn't happen
  345. break;
  346. }
  347. }
  348. dprintk("Port %d Received link speed %d update from adapter\n", port->actor_port_number, speed);
  349. return speed;
  350. }
  351. /**
  352. * __get_duplex - get a port's duplex
  353. * @port: the port we're looking at
  354. *
  355. * Return @port's duplex in 802.3ad bitmask format. i.e.:
  356. * 0x01 if in full duplex
  357. * 0x00 otherwise
  358. */
  359. static u8 __get_duplex(struct port *port)
  360. {
  361. struct slave *slave = port->slave;
  362. u8 retval;
  363. // handling a special case: when the configuration starts with
  364. // link down, it sets the duplex to 0.
  365. if (slave->link != BOND_LINK_UP) {
  366. retval=0x0;
  367. } else {
  368. switch (slave->duplex) {
  369. case DUPLEX_FULL:
  370. retval=0x1;
  371. dprintk("Port %d Received status full duplex update from adapter\n", port->actor_port_number);
  372. break;
  373. case DUPLEX_HALF:
  374. default:
  375. retval=0x0;
  376. dprintk("Port %d Received status NOT full duplex update from adapter\n", port->actor_port_number);
  377. break;
  378. }
  379. }
  380. return retval;
  381. }
  382. /**
  383. * __initialize_port_locks - initialize a port's RX machine spinlock
  384. * @port: the port we're looking at
  385. *
  386. */
  387. static inline void __initialize_port_locks(struct port *port)
  388. {
  389. // make sure it isn't called twice
  390. spin_lock_init(&(SLAVE_AD_INFO(port->slave).rx_machine_lock));
  391. }
  392. //conversions
  393. /**
  394. * __ntohs_lacpdu - convert the contents of a LACPDU to host byte order
  395. * @lacpdu: the speicifed lacpdu
  396. *
  397. * For each multi-byte field in the lacpdu, convert its content
  398. */
  399. static void __ntohs_lacpdu(struct lacpdu *lacpdu)
  400. {
  401. if (lacpdu) {
  402. lacpdu->actor_system_priority = ntohs(lacpdu->actor_system_priority);
  403. lacpdu->actor_key = ntohs(lacpdu->actor_key);
  404. lacpdu->actor_port_priority = ntohs(lacpdu->actor_port_priority);
  405. lacpdu->actor_port = ntohs(lacpdu->actor_port);
  406. lacpdu->partner_system_priority = ntohs(lacpdu->partner_system_priority);
  407. lacpdu->partner_key = ntohs(lacpdu->partner_key);
  408. lacpdu->partner_port_priority = ntohs(lacpdu->partner_port_priority);
  409. lacpdu->partner_port = ntohs(lacpdu->partner_port);
  410. lacpdu->collector_max_delay = ntohs(lacpdu->collector_max_delay);
  411. }
  412. }
  413. /**
  414. * __ad_timer_to_ticks - convert a given timer type to AD module ticks
  415. * @timer_type: which timer to operate
  416. * @par: timer parameter. see below
  417. *
  418. * If @timer_type is %current_while_timer, @par indicates long/short timer.
  419. * If @timer_type is %periodic_timer, @par is one of %FAST_PERIODIC_TIME,
  420. * %SLOW_PERIODIC_TIME.
  421. */
  422. static u16 __ad_timer_to_ticks(u16 timer_type, u16 par)
  423. {
  424. u16 retval=0; //to silence the compiler
  425. switch (timer_type) {
  426. case AD_CURRENT_WHILE_TIMER: // for rx machine usage
  427. if (par) { // for short or long timeout
  428. retval = (AD_SHORT_TIMEOUT_TIME*ad_ticks_per_sec); // short timeout
  429. } else {
  430. retval = (AD_LONG_TIMEOUT_TIME*ad_ticks_per_sec); // long timeout
  431. }
  432. break;
  433. case AD_ACTOR_CHURN_TIMER: // for local churn machine
  434. retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
  435. break;
  436. case AD_PERIODIC_TIMER: // for periodic machine
  437. retval = (par*ad_ticks_per_sec); // long timeout
  438. break;
  439. case AD_PARTNER_CHURN_TIMER: // for remote churn machine
  440. retval = (AD_CHURN_DETECTION_TIME*ad_ticks_per_sec);
  441. break;
  442. case AD_WAIT_WHILE_TIMER: // for selection machine
  443. retval = (AD_AGGREGATE_WAIT_TIME*ad_ticks_per_sec);
  444. break;
  445. }
  446. return retval;
  447. }
  448. /////////////////////////////////////////////////////////////////////////////////
  449. // ================= ad_rx_machine helper functions ==================
  450. /////////////////////////////////////////////////////////////////////////////////
  451. /**
  452. * __record_pdu - record parameters from a received lacpdu
  453. * @lacpdu: the lacpdu we've received
  454. * @port: the port we're looking at
  455. *
  456. * Record the parameter values for the Actor carried in a received lacpdu as
  457. * the current partner operational parameter values and sets
  458. * actor_oper_port_state.defaulted to FALSE.
  459. */
  460. static void __record_pdu(struct lacpdu *lacpdu, struct port *port)
  461. {
  462. // validate lacpdu and port
  463. if (lacpdu && port) {
  464. // record the new parameter values for the partner operational
  465. port->partner_oper_port_number = lacpdu->actor_port;
  466. port->partner_oper_port_priority = lacpdu->actor_port_priority;
  467. port->partner_oper_system = lacpdu->actor_system;
  468. port->partner_oper_system_priority = lacpdu->actor_system_priority;
  469. port->partner_oper_key = lacpdu->actor_key;
  470. // zero partener's lase states
  471. port->partner_oper_port_state = 0;
  472. port->partner_oper_port_state |= (lacpdu->actor_state & AD_STATE_LACP_ACTIVITY);
  473. port->partner_oper_port_state |= (lacpdu->actor_state & AD_STATE_LACP_TIMEOUT);
  474. port->partner_oper_port_state |= (lacpdu->actor_state & AD_STATE_AGGREGATION);
  475. port->partner_oper_port_state |= (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION);
  476. port->partner_oper_port_state |= (lacpdu->actor_state & AD_STATE_COLLECTING);
  477. port->partner_oper_port_state |= (lacpdu->actor_state & AD_STATE_DISTRIBUTING);
  478. port->partner_oper_port_state |= (lacpdu->actor_state & AD_STATE_DEFAULTED);
  479. port->partner_oper_port_state |= (lacpdu->actor_state & AD_STATE_EXPIRED);
  480. // set actor_oper_port_state.defaulted to FALSE
  481. port->actor_oper_port_state &= ~AD_STATE_DEFAULTED;
  482. // set the partner sync. to on if the partner is sync. and the port is matched
  483. if ((port->sm_vars & AD_PORT_MATCHED) && (lacpdu->actor_state & AD_STATE_SYNCHRONIZATION)) {
  484. port->partner_oper_port_state |= AD_STATE_SYNCHRONIZATION;
  485. } else {
  486. port->partner_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
  487. }
  488. }
  489. }
  490. /**
  491. * __record_default - record default parameters
  492. * @port: the port we're looking at
  493. *
  494. * This function records the default parameter values for the partner carried
  495. * in the Partner Admin parameters as the current partner operational parameter
  496. * values and sets actor_oper_port_state.defaulted to TRUE.
  497. */
  498. static void __record_default(struct port *port)
  499. {
  500. // validate the port
  501. if (port) {
  502. // record the partner admin parameters
  503. port->partner_oper_port_number = port->partner_admin_port_number;
  504. port->partner_oper_port_priority = port->partner_admin_port_priority;
  505. port->partner_oper_system = port->partner_admin_system;
  506. port->partner_oper_system_priority = port->partner_admin_system_priority;
  507. port->partner_oper_key = port->partner_admin_key;
  508. port->partner_oper_port_state = port->partner_admin_port_state;
  509. // set actor_oper_port_state.defaulted to true
  510. port->actor_oper_port_state |= AD_STATE_DEFAULTED;
  511. }
  512. }
  513. /**
  514. * __update_selected - update a port's Selected variable from a received lacpdu
  515. * @lacpdu: the lacpdu we've received
  516. * @port: the port we're looking at
  517. *
  518. * Update the value of the selected variable, using parameter values from a
  519. * newly received lacpdu. The parameter values for the Actor carried in the
  520. * received PDU are compared with the corresponding operational parameter
  521. * values for the ports partner. If one or more of the comparisons shows that
  522. * the value(s) received in the PDU differ from the current operational values,
  523. * then selected is set to FALSE and actor_oper_port_state.synchronization is
  524. * set to out_of_sync. Otherwise, selected remains unchanged.
  525. */
  526. static void __update_selected(struct lacpdu *lacpdu, struct port *port)
  527. {
  528. // validate lacpdu and port
  529. if (lacpdu && port) {
  530. // check if any parameter is different
  531. if ((lacpdu->actor_port != port->partner_oper_port_number) ||
  532. (lacpdu->actor_port_priority != port->partner_oper_port_priority) ||
  533. MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->partner_oper_system)) ||
  534. (lacpdu->actor_system_priority != port->partner_oper_system_priority) ||
  535. (lacpdu->actor_key != port->partner_oper_key) ||
  536. ((lacpdu->actor_state & AD_STATE_AGGREGATION) != (port->partner_oper_port_state & AD_STATE_AGGREGATION))
  537. ) {
  538. // update the state machine Selected variable
  539. port->sm_vars &= ~AD_PORT_SELECTED;
  540. }
  541. }
  542. }
  543. /**
  544. * __update_default_selected - update a port's Selected variable from Partner
  545. * @port: the port we're looking at
  546. *
  547. * This function updates the value of the selected variable, using the partner
  548. * administrative parameter values. The administrative values are compared with
  549. * the corresponding operational parameter values for the partner. If one or
  550. * more of the comparisons shows that the administrative value(s) differ from
  551. * the current operational values, then Selected is set to FALSE and
  552. * actor_oper_port_state.synchronization is set to OUT_OF_SYNC. Otherwise,
  553. * Selected remains unchanged.
  554. */
  555. static void __update_default_selected(struct port *port)
  556. {
  557. // validate the port
  558. if (port) {
  559. // check if any parameter is different
  560. if ((port->partner_admin_port_number != port->partner_oper_port_number) ||
  561. (port->partner_admin_port_priority != port->partner_oper_port_priority) ||
  562. MAC_ADDRESS_COMPARE(&(port->partner_admin_system), &(port->partner_oper_system)) ||
  563. (port->partner_admin_system_priority != port->partner_oper_system_priority) ||
  564. (port->partner_admin_key != port->partner_oper_key) ||
  565. ((port->partner_admin_port_state & AD_STATE_AGGREGATION) != (port->partner_oper_port_state & AD_STATE_AGGREGATION))
  566. ) {
  567. // update the state machine Selected variable
  568. port->sm_vars &= ~AD_PORT_SELECTED;
  569. }
  570. }
  571. }
  572. /**
  573. * __choose_matched - update a port's matched variable from a received lacpdu
  574. * @lacpdu: the lacpdu we've received
  575. * @port: the port we're looking at
  576. *
  577. * Update the value of the matched variable, using parameter values from a
  578. * newly received lacpdu. Parameter values for the partner carried in the
  579. * received PDU are compared with the corresponding operational parameter
  580. * values for the actor. Matched is set to TRUE if all of these parameters
  581. * match and the PDU parameter partner_state.aggregation has the same value as
  582. * actor_oper_port_state.aggregation and lacp will actively maintain the link
  583. * in the aggregation. Matched is also set to TRUE if the value of
  584. * actor_state.aggregation in the received PDU is set to FALSE, i.e., indicates
  585. * an individual link and lacp will actively maintain the link. Otherwise,
  586. * matched is set to FALSE. LACP is considered to be actively maintaining the
  587. * link if either the PDU's actor_state.lacp_activity variable is TRUE or both
  588. * the actor's actor_oper_port_state.lacp_activity and the PDU's
  589. * partner_state.lacp_activity variables are TRUE.
  590. */
  591. static void __choose_matched(struct lacpdu *lacpdu, struct port *port)
  592. {
  593. // validate lacpdu and port
  594. if (lacpdu && port) {
  595. // check if all parameters are alike
  596. if (((lacpdu->partner_port == port->actor_port_number) &&
  597. (lacpdu->partner_port_priority == port->actor_port_priority) &&
  598. !MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) &&
  599. (lacpdu->partner_system_priority == port->actor_system_priority) &&
  600. (lacpdu->partner_key == port->actor_oper_port_key) &&
  601. ((lacpdu->partner_state & AD_STATE_AGGREGATION) == (port->actor_oper_port_state & AD_STATE_AGGREGATION))) ||
  602. // or this is individual link(aggregation == FALSE)
  603. ((lacpdu->actor_state & AD_STATE_AGGREGATION) == 0)
  604. ) {
  605. // update the state machine Matched variable
  606. port->sm_vars |= AD_PORT_MATCHED;
  607. } else {
  608. port->sm_vars &= ~AD_PORT_MATCHED;
  609. }
  610. }
  611. }
  612. /**
  613. * __update_ntt - update a port's ntt variable from a received lacpdu
  614. * @lacpdu: the lacpdu we've received
  615. * @port: the port we're looking at
  616. *
  617. * Updates the value of the ntt variable, using parameter values from a newly
  618. * received lacpdu. The parameter values for the partner carried in the
  619. * received PDU are compared with the corresponding operational parameter
  620. * values for the Actor. If one or more of the comparisons shows that the
  621. * value(s) received in the PDU differ from the current operational values,
  622. * then ntt is set to TRUE. Otherwise, ntt remains unchanged.
  623. */
  624. static void __update_ntt(struct lacpdu *lacpdu, struct port *port)
  625. {
  626. // validate lacpdu and port
  627. if (lacpdu && port) {
  628. // check if any parameter is different
  629. if ((lacpdu->partner_port != port->actor_port_number) ||
  630. (lacpdu->partner_port_priority != port->actor_port_priority) ||
  631. MAC_ADDRESS_COMPARE(&(lacpdu->partner_system), &(port->actor_system)) ||
  632. (lacpdu->partner_system_priority != port->actor_system_priority) ||
  633. (lacpdu->partner_key != port->actor_oper_port_key) ||
  634. ((lacpdu->partner_state & AD_STATE_LACP_ACTIVITY) != (port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY)) ||
  635. ((lacpdu->partner_state & AD_STATE_LACP_TIMEOUT) != (port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT)) ||
  636. ((lacpdu->partner_state & AD_STATE_SYNCHRONIZATION) != (port->actor_oper_port_state & AD_STATE_SYNCHRONIZATION)) ||
  637. ((lacpdu->partner_state & AD_STATE_AGGREGATION) != (port->actor_oper_port_state & AD_STATE_AGGREGATION))
  638. ) {
  639. // set ntt to be TRUE
  640. port->ntt = 1;
  641. }
  642. }
  643. }
  644. /**
  645. * __attach_bond_to_agg
  646. * @port: the port we're looking at
  647. *
  648. * Handle the attaching of the port's control parser/multiplexer and the
  649. * aggregator. This function does nothing since the parser/multiplexer of the
  650. * receive and the parser/multiplexer of the aggregator are already combined.
  651. */
  652. static void __attach_bond_to_agg(struct port *port)
  653. {
  654. port=NULL; // just to satisfy the compiler
  655. // This function does nothing since the parser/multiplexer of the receive
  656. // and the parser/multiplexer of the aggregator are already combined
  657. }
  658. /**
  659. * __detach_bond_from_agg
  660. * @port: the port we're looking at
  661. *
  662. * Handle the detaching of the port's control parser/multiplexer from the
  663. * aggregator. This function does nothing since the parser/multiplexer of the
  664. * receive and the parser/multiplexer of the aggregator are already combined.
  665. */
  666. static void __detach_bond_from_agg(struct port *port)
  667. {
  668. port=NULL; // just to satisfy the compiler
  669. // This function does nothing sience the parser/multiplexer of the receive
  670. // and the parser/multiplexer of the aggregator are already combined
  671. }
  672. /**
  673. * __agg_ports_are_ready - check if all ports in an aggregator are ready
  674. * @aggregator: the aggregator we're looking at
  675. *
  676. */
  677. static int __agg_ports_are_ready(struct aggregator *aggregator)
  678. {
  679. struct port *port;
  680. int retval = 1;
  681. if (aggregator) {
  682. // scan all ports in this aggregator to verfy if they are all ready
  683. for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
  684. if (!(port->sm_vars & AD_PORT_READY_N)) {
  685. retval = 0;
  686. break;
  687. }
  688. }
  689. }
  690. return retval;
  691. }
  692. /**
  693. * __set_agg_ports_ready - set value of Ready bit in all ports of an aggregator
  694. * @aggregator: the aggregator we're looking at
  695. * @val: Should the ports' ready bit be set on or off
  696. *
  697. */
  698. static void __set_agg_ports_ready(struct aggregator *aggregator, int val)
  699. {
  700. struct port *port;
  701. for (port=aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
  702. if (val) {
  703. port->sm_vars |= AD_PORT_READY;
  704. } else {
  705. port->sm_vars &= ~AD_PORT_READY;
  706. }
  707. }
  708. }
  709. /**
  710. * __get_agg_bandwidth - get the total bandwidth of an aggregator
  711. * @aggregator: the aggregator we're looking at
  712. *
  713. */
  714. static u32 __get_agg_bandwidth(struct aggregator *aggregator)
  715. {
  716. u32 bandwidth=0;
  717. u32 basic_speed;
  718. if (aggregator->num_of_ports) {
  719. basic_speed = __get_link_speed(aggregator->lag_ports);
  720. switch (basic_speed) {
  721. case AD_LINK_SPEED_BITMASK_1MBPS:
  722. bandwidth = aggregator->num_of_ports;
  723. break;
  724. case AD_LINK_SPEED_BITMASK_10MBPS:
  725. bandwidth = aggregator->num_of_ports * 10;
  726. break;
  727. case AD_LINK_SPEED_BITMASK_100MBPS:
  728. bandwidth = aggregator->num_of_ports * 100;
  729. break;
  730. case AD_LINK_SPEED_BITMASK_1000MBPS:
  731. bandwidth = aggregator->num_of_ports * 1000;
  732. break;
  733. default:
  734. bandwidth=0; // to silent the compilor ....
  735. }
  736. }
  737. return bandwidth;
  738. }
  739. /**
  740. * __get_active_agg - get the current active aggregator
  741. * @aggregator: the aggregator we're looking at
  742. *
  743. */
  744. static struct aggregator *__get_active_agg(struct aggregator *aggregator)
  745. {
  746. struct aggregator *retval = NULL;
  747. for (; aggregator; aggregator = __get_next_agg(aggregator)) {
  748. if (aggregator->is_active) {
  749. retval = aggregator;
  750. break;
  751. }
  752. }
  753. return retval;
  754. }
  755. /**
  756. * __update_lacpdu_from_port - update a port's lacpdu fields
  757. * @port: the port we're looking at
  758. *
  759. */
  760. static inline void __update_lacpdu_from_port(struct port *port)
  761. {
  762. struct lacpdu *lacpdu = &port->lacpdu;
  763. /* update current actual Actor parameters */
  764. /* lacpdu->subtype initialized
  765. * lacpdu->version_number initialized
  766. * lacpdu->tlv_type_actor_info initialized
  767. * lacpdu->actor_information_length initialized
  768. */
  769. lacpdu->actor_system_priority = port->actor_system_priority;
  770. lacpdu->actor_system = port->actor_system;
  771. lacpdu->actor_key = port->actor_oper_port_key;
  772. lacpdu->actor_port_priority = port->actor_port_priority;
  773. lacpdu->actor_port = port->actor_port_number;
  774. lacpdu->actor_state = port->actor_oper_port_state;
  775. /* lacpdu->reserved_3_1 initialized
  776. * lacpdu->tlv_type_partner_info initialized
  777. * lacpdu->partner_information_length initialized
  778. */
  779. lacpdu->partner_system_priority = port->partner_oper_system_priority;
  780. lacpdu->partner_system = port->partner_oper_system;
  781. lacpdu->partner_key = port->partner_oper_key;
  782. lacpdu->partner_port_priority = port->partner_oper_port_priority;
  783. lacpdu->partner_port = port->partner_oper_port_number;
  784. lacpdu->partner_state = port->partner_oper_port_state;
  785. /* lacpdu->reserved_3_2 initialized
  786. * lacpdu->tlv_type_collector_info initialized
  787. * lacpdu->collector_information_length initialized
  788. * collector_max_delay initialized
  789. * reserved_12[12] initialized
  790. * tlv_type_terminator initialized
  791. * terminator_length initialized
  792. * reserved_50[50] initialized
  793. */
  794. /* Convert all non u8 parameters to Big Endian for transmit */
  795. __ntohs_lacpdu(lacpdu);
  796. }
  797. //////////////////////////////////////////////////////////////////////////////////////
  798. // ================= main 802.3ad protocol code ======================================
  799. //////////////////////////////////////////////////////////////////////////////////////
  800. /**
  801. * ad_lacpdu_send - send out a lacpdu packet on a given port
  802. * @port: the port we're looking at
  803. *
  804. * Returns: 0 on success
  805. * < 0 on error
  806. */
  807. static int ad_lacpdu_send(struct port *port)
  808. {
  809. struct slave *slave = port->slave;
  810. struct sk_buff *skb;
  811. struct lacpdu_header *lacpdu_header;
  812. int length = sizeof(struct lacpdu_header);
  813. struct mac_addr lacpdu_multicast_address = AD_MULTICAST_LACPDU_ADDR;
  814. skb = dev_alloc_skb(length);
  815. if (!skb) {
  816. return -ENOMEM;
  817. }
  818. skb->dev = slave->dev;
  819. skb->mac.raw = skb->data;
  820. skb->nh.raw = skb->data + ETH_HLEN;
  821. skb->protocol = PKT_TYPE_LACPDU;
  822. skb->priority = TC_PRIO_CONTROL;
  823. lacpdu_header = (struct lacpdu_header *)skb_put(skb, length);
  824. lacpdu_header->ad_header.destination_address = lacpdu_multicast_address;
  825. /* Note: source addres is set to be the member's PERMANENT address, because we use it
  826. to identify loopback lacpdus in receive. */
  827. lacpdu_header->ad_header.source_address = *((struct mac_addr *)(slave->perm_hwaddr));
  828. lacpdu_header->ad_header.length_type = PKT_TYPE_LACPDU;
  829. lacpdu_header->lacpdu = port->lacpdu; // struct copy
  830. dev_queue_xmit(skb);
  831. return 0;
  832. }
  833. /**
  834. * ad_marker_send - send marker information/response on a given port
  835. * @port: the port we're looking at
  836. * @marker: marker data to send
  837. *
  838. * Returns: 0 on success
  839. * < 0 on error
  840. */
  841. static int ad_marker_send(struct port *port, struct marker *marker)
  842. {
  843. struct slave *slave = port->slave;
  844. struct sk_buff *skb;
  845. struct marker_header *marker_header;
  846. int length = sizeof(struct marker_header);
  847. struct mac_addr lacpdu_multicast_address = AD_MULTICAST_LACPDU_ADDR;
  848. skb = dev_alloc_skb(length + 16);
  849. if (!skb) {
  850. return -ENOMEM;
  851. }
  852. skb_reserve(skb, 16);
  853. skb->dev = slave->dev;
  854. skb->mac.raw = skb->data;
  855. skb->nh.raw = skb->data + ETH_HLEN;
  856. skb->protocol = PKT_TYPE_LACPDU;
  857. marker_header = (struct marker_header *)skb_put(skb, length);
  858. marker_header->ad_header.destination_address = lacpdu_multicast_address;
  859. /* Note: source addres is set to be the member's PERMANENT address, because we use it
  860. to identify loopback MARKERs in receive. */
  861. marker_header->ad_header.source_address = *((struct mac_addr *)(slave->perm_hwaddr));
  862. marker_header->ad_header.length_type = PKT_TYPE_LACPDU;
  863. marker_header->marker = *marker; // struct copy
  864. dev_queue_xmit(skb);
  865. return 0;
  866. }
  867. /**
  868. * ad_mux_machine - handle a port's mux state machine
  869. * @port: the port we're looking at
  870. *
  871. */
  872. static void ad_mux_machine(struct port *port)
  873. {
  874. mux_states_t last_state;
  875. // keep current State Machine state to compare later if it was changed
  876. last_state = port->sm_mux_state;
  877. if (port->sm_vars & AD_PORT_BEGIN) {
  878. port->sm_mux_state = AD_MUX_DETACHED; // next state
  879. } else {
  880. switch (port->sm_mux_state) {
  881. case AD_MUX_DETACHED:
  882. if ((port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if SELECTED or STANDBY
  883. port->sm_mux_state = AD_MUX_WAITING; // next state
  884. }
  885. break;
  886. case AD_MUX_WAITING:
  887. // if SELECTED == FALSE return to DETACH state
  888. if (!(port->sm_vars & AD_PORT_SELECTED)) { // if UNSELECTED
  889. port->sm_vars &= ~AD_PORT_READY_N;
  890. // in order to withhold the Selection Logic to check all ports READY_N value
  891. // every callback cycle to update ready variable, we check READY_N and update READY here
  892. __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
  893. port->sm_mux_state = AD_MUX_DETACHED; // next state
  894. break;
  895. }
  896. // check if the wait_while_timer expired
  897. if (port->sm_mux_timer_counter && !(--port->sm_mux_timer_counter)) {
  898. port->sm_vars |= AD_PORT_READY_N;
  899. }
  900. // in order to withhold the selection logic to check all ports READY_N value
  901. // every callback cycle to update ready variable, we check READY_N and update READY here
  902. __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
  903. // if the wait_while_timer expired, and the port is in READY state, move to ATTACHED state
  904. if ((port->sm_vars & AD_PORT_READY) && !port->sm_mux_timer_counter) {
  905. port->sm_mux_state = AD_MUX_ATTACHED; // next state
  906. }
  907. break;
  908. case AD_MUX_ATTACHED:
  909. // check also if agg_select_timer expired(so the edable port will take place only after this timer)
  910. if ((port->sm_vars & AD_PORT_SELECTED) && (port->partner_oper_port_state & AD_STATE_SYNCHRONIZATION) && !__check_agg_selection_timer(port)) {
  911. port->sm_mux_state = AD_MUX_COLLECTING_DISTRIBUTING;// next state
  912. } else if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY)) { // if UNSELECTED or STANDBY
  913. port->sm_vars &= ~AD_PORT_READY_N;
  914. // in order to withhold the selection logic to check all ports READY_N value
  915. // every callback cycle to update ready variable, we check READY_N and update READY here
  916. __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
  917. port->sm_mux_state = AD_MUX_DETACHED;// next state
  918. }
  919. break;
  920. case AD_MUX_COLLECTING_DISTRIBUTING:
  921. if (!(port->sm_vars & AD_PORT_SELECTED) || (port->sm_vars & AD_PORT_STANDBY) ||
  922. !(port->partner_oper_port_state & AD_STATE_SYNCHRONIZATION)
  923. ) {
  924. port->sm_mux_state = AD_MUX_ATTACHED;// next state
  925. } else {
  926. // if port state hasn't changed make
  927. // sure that a collecting distributing
  928. // port in an active aggregator is enabled
  929. if (port->aggregator &&
  930. port->aggregator->is_active &&
  931. !__port_is_enabled(port)) {
  932. __enable_port(port);
  933. }
  934. }
  935. break;
  936. default: //to silence the compiler
  937. break;
  938. }
  939. }
  940. // check if the state machine was changed
  941. if (port->sm_mux_state != last_state) {
  942. dprintk("Mux Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_mux_state);
  943. switch (port->sm_mux_state) {
  944. case AD_MUX_DETACHED:
  945. __detach_bond_from_agg(port);
  946. port->actor_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
  947. ad_disable_collecting_distributing(port);
  948. port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
  949. port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
  950. port->ntt = 1;
  951. break;
  952. case AD_MUX_WAITING:
  953. port->sm_mux_timer_counter = __ad_timer_to_ticks(AD_WAIT_WHILE_TIMER, 0);
  954. break;
  955. case AD_MUX_ATTACHED:
  956. __attach_bond_to_agg(port);
  957. port->actor_oper_port_state |= AD_STATE_SYNCHRONIZATION;
  958. port->actor_oper_port_state &= ~AD_STATE_COLLECTING;
  959. port->actor_oper_port_state &= ~AD_STATE_DISTRIBUTING;
  960. ad_disable_collecting_distributing(port);
  961. port->ntt = 1;
  962. break;
  963. case AD_MUX_COLLECTING_DISTRIBUTING:
  964. port->actor_oper_port_state |= AD_STATE_COLLECTING;
  965. port->actor_oper_port_state |= AD_STATE_DISTRIBUTING;
  966. ad_enable_collecting_distributing(port);
  967. port->ntt = 1;
  968. break;
  969. default: //to silence the compiler
  970. break;
  971. }
  972. }
  973. }
  974. /**
  975. * ad_rx_machine - handle a port's rx State Machine
  976. * @lacpdu: the lacpdu we've received
  977. * @port: the port we're looking at
  978. *
  979. * If lacpdu arrived, stop previous timer (if exists) and set the next state as
  980. * CURRENT. If timer expired set the state machine in the proper state.
  981. * In other cases, this function checks if we need to switch to other state.
  982. */
  983. static void ad_rx_machine(struct lacpdu *lacpdu, struct port *port)
  984. {
  985. rx_states_t last_state;
  986. // Lock to prevent 2 instances of this function to run simultaneously(rx interrupt and periodic machine callback)
  987. __get_rx_machine_lock(port);
  988. // keep current State Machine state to compare later if it was changed
  989. last_state = port->sm_rx_state;
  990. // check if state machine should change state
  991. // first, check if port was reinitialized
  992. if (port->sm_vars & AD_PORT_BEGIN) {
  993. port->sm_rx_state = AD_RX_INITIALIZE; // next state
  994. }
  995. // check if port is not enabled
  996. else if (!(port->sm_vars & AD_PORT_BEGIN) && !port->is_enabled && !(port->sm_vars & AD_PORT_MOVED)) {
  997. port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
  998. }
  999. // check if new lacpdu arrived
  1000. else if (lacpdu && ((port->sm_rx_state == AD_RX_EXPIRED) || (port->sm_rx_state == AD_RX_DEFAULTED) || (port->sm_rx_state == AD_RX_CURRENT))) {
  1001. port->sm_rx_timer_counter = 0; // zero timer
  1002. port->sm_rx_state = AD_RX_CURRENT;
  1003. } else {
  1004. // if timer is on, and if it is expired
  1005. if (port->sm_rx_timer_counter && !(--port->sm_rx_timer_counter)) {
  1006. switch (port->sm_rx_state) {
  1007. case AD_RX_EXPIRED:
  1008. port->sm_rx_state = AD_RX_DEFAULTED; // next state
  1009. break;
  1010. case AD_RX_CURRENT:
  1011. port->sm_rx_state = AD_RX_EXPIRED; // next state
  1012. break;
  1013. default: //to silence the compiler
  1014. break;
  1015. }
  1016. } else {
  1017. // if no lacpdu arrived and no timer is on
  1018. switch (port->sm_rx_state) {
  1019. case AD_RX_PORT_DISABLED:
  1020. if (port->sm_vars & AD_PORT_MOVED) {
  1021. port->sm_rx_state = AD_RX_INITIALIZE; // next state
  1022. } else if (port->is_enabled && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
  1023. port->sm_rx_state = AD_RX_EXPIRED; // next state
  1024. } else if (port->is_enabled && ((port->sm_vars & AD_PORT_LACP_ENABLED) == 0)) {
  1025. port->sm_rx_state = AD_RX_LACP_DISABLED; // next state
  1026. }
  1027. break;
  1028. default: //to silence the compiler
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. // check if the State machine was changed or new lacpdu arrived
  1034. if ((port->sm_rx_state != last_state) || (lacpdu)) {
  1035. dprintk("Rx Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_rx_state);
  1036. switch (port->sm_rx_state) {
  1037. case AD_RX_INITIALIZE:
  1038. if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
  1039. port->sm_vars &= ~AD_PORT_LACP_ENABLED;
  1040. } else {
  1041. port->sm_vars |= AD_PORT_LACP_ENABLED;
  1042. }
  1043. port->sm_vars &= ~AD_PORT_SELECTED;
  1044. __record_default(port);
  1045. port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
  1046. port->sm_vars &= ~AD_PORT_MOVED;
  1047. port->sm_rx_state = AD_RX_PORT_DISABLED; // next state
  1048. /*- Fall Through -*/
  1049. case AD_RX_PORT_DISABLED:
  1050. port->sm_vars &= ~AD_PORT_MATCHED;
  1051. break;
  1052. case AD_RX_LACP_DISABLED:
  1053. port->sm_vars &= ~AD_PORT_SELECTED;
  1054. __record_default(port);
  1055. port->partner_oper_port_state &= ~AD_STATE_AGGREGATION;
  1056. port->sm_vars |= AD_PORT_MATCHED;
  1057. port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
  1058. break;
  1059. case AD_RX_EXPIRED:
  1060. //Reset of the Synchronization flag. (Standard 43.4.12)
  1061. //This reset cause to disable this port in the COLLECTING_DISTRIBUTING state of the
  1062. //mux machine in case of EXPIRED even if LINK_DOWN didn't arrive for the port.
  1063. port->partner_oper_port_state &= ~AD_STATE_SYNCHRONIZATION;
  1064. port->sm_vars &= ~AD_PORT_MATCHED;
  1065. port->partner_oper_port_state |= AD_SHORT_TIMEOUT;
  1066. port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(AD_SHORT_TIMEOUT));
  1067. port->actor_oper_port_state |= AD_STATE_EXPIRED;
  1068. break;
  1069. case AD_RX_DEFAULTED:
  1070. __update_default_selected(port);
  1071. __record_default(port);
  1072. port->sm_vars |= AD_PORT_MATCHED;
  1073. port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
  1074. break;
  1075. case AD_RX_CURRENT:
  1076. // detect loopback situation
  1077. if (!MAC_ADDRESS_COMPARE(&(lacpdu->actor_system), &(port->actor_system))) {
  1078. // INFO_RECEIVED_LOOPBACK_FRAMES
  1079. printk(KERN_ERR DRV_NAME ": An illegal loopback occurred on adapter (%s)\n",
  1080. port->slave->dev->name);
  1081. printk(KERN_ERR "Check the configuration to verify that all Adapters "
  1082. "are connected to 802.3ad compliant switch ports\n");
  1083. __release_rx_machine_lock(port);
  1084. return;
  1085. }
  1086. __update_selected(lacpdu, port);
  1087. __update_ntt(lacpdu, port);
  1088. __record_pdu(lacpdu, port);
  1089. __choose_matched(lacpdu, port);
  1090. port->sm_rx_timer_counter = __ad_timer_to_ticks(AD_CURRENT_WHILE_TIMER, (u16)(port->actor_oper_port_state & AD_STATE_LACP_TIMEOUT));
  1091. port->actor_oper_port_state &= ~AD_STATE_EXPIRED;
  1092. // verify that if the aggregator is enabled, the port is enabled too.
  1093. //(because if the link goes down for a short time, the 802.3ad will not
  1094. // catch it, and the port will continue to be disabled)
  1095. if (port->aggregator && port->aggregator->is_active && !__port_is_enabled(port)) {
  1096. __enable_port(port);
  1097. }
  1098. break;
  1099. default: //to silence the compiler
  1100. break;
  1101. }
  1102. }
  1103. __release_rx_machine_lock(port);
  1104. }
  1105. /**
  1106. * ad_tx_machine - handle a port's tx state machine
  1107. * @port: the port we're looking at
  1108. *
  1109. */
  1110. static void ad_tx_machine(struct port *port)
  1111. {
  1112. // check if tx timer expired, to verify that we do not send more than 3 packets per second
  1113. if (port->sm_tx_timer_counter && !(--port->sm_tx_timer_counter)) {
  1114. // check if there is something to send
  1115. if (port->ntt && (port->sm_vars & AD_PORT_LACP_ENABLED)) {
  1116. __update_lacpdu_from_port(port);
  1117. // send the lacpdu
  1118. if (ad_lacpdu_send(port) >= 0) {
  1119. dprintk("Sent LACPDU on port %d\n", port->actor_port_number);
  1120. // mark ntt as false, so it will not be sent again until demanded
  1121. port->ntt = 0;
  1122. }
  1123. }
  1124. // restart tx timer(to verify that we will not exceed AD_MAX_TX_IN_SECOND
  1125. port->sm_tx_timer_counter=ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
  1126. }
  1127. }
  1128. /**
  1129. * ad_periodic_machine - handle a port's periodic state machine
  1130. * @port: the port we're looking at
  1131. *
  1132. * Turn ntt flag on priodically to perform periodic transmission of lacpdu's.
  1133. */
  1134. static void ad_periodic_machine(struct port *port)
  1135. {
  1136. periodic_states_t last_state;
  1137. // keep current state machine state to compare later if it was changed
  1138. last_state = port->sm_periodic_state;
  1139. // check if port was reinitialized
  1140. if (((port->sm_vars & AD_PORT_BEGIN) || !(port->sm_vars & AD_PORT_LACP_ENABLED) || !port->is_enabled) ||
  1141. (!(port->actor_oper_port_state & AD_STATE_LACP_ACTIVITY) && !(port->partner_oper_port_state & AD_STATE_LACP_ACTIVITY))
  1142. ) {
  1143. port->sm_periodic_state = AD_NO_PERIODIC; // next state
  1144. }
  1145. // check if state machine should change state
  1146. else if (port->sm_periodic_timer_counter) {
  1147. // check if periodic state machine expired
  1148. if (!(--port->sm_periodic_timer_counter)) {
  1149. // if expired then do tx
  1150. port->sm_periodic_state = AD_PERIODIC_TX; // next state
  1151. } else {
  1152. // If not expired, check if there is some new timeout parameter from the partner state
  1153. switch (port->sm_periodic_state) {
  1154. case AD_FAST_PERIODIC:
  1155. if (!(port->partner_oper_port_state & AD_STATE_LACP_TIMEOUT)) {
  1156. port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
  1157. }
  1158. break;
  1159. case AD_SLOW_PERIODIC:
  1160. if ((port->partner_oper_port_state & AD_STATE_LACP_TIMEOUT)) {
  1161. // stop current timer
  1162. port->sm_periodic_timer_counter = 0;
  1163. port->sm_periodic_state = AD_PERIODIC_TX; // next state
  1164. }
  1165. break;
  1166. default: //to silence the compiler
  1167. break;
  1168. }
  1169. }
  1170. } else {
  1171. switch (port->sm_periodic_state) {
  1172. case AD_NO_PERIODIC:
  1173. port->sm_periodic_state = AD_FAST_PERIODIC; // next state
  1174. break;
  1175. case AD_PERIODIC_TX:
  1176. if (!(port->partner_oper_port_state & AD_STATE_LACP_TIMEOUT)) {
  1177. port->sm_periodic_state = AD_SLOW_PERIODIC; // next state
  1178. } else {
  1179. port->sm_periodic_state = AD_FAST_PERIODIC; // next state
  1180. }
  1181. break;
  1182. default: //to silence the compiler
  1183. break;
  1184. }
  1185. }
  1186. // check if the state machine was changed
  1187. if (port->sm_periodic_state != last_state) {
  1188. dprintk("Periodic Machine: Port=%d, Last State=%d, Curr State=%d\n", port->actor_port_number, last_state, port->sm_periodic_state);
  1189. switch (port->sm_periodic_state) {
  1190. case AD_NO_PERIODIC:
  1191. port->sm_periodic_timer_counter = 0; // zero timer
  1192. break;
  1193. case AD_FAST_PERIODIC:
  1194. port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_FAST_PERIODIC_TIME))-1; // decrement 1 tick we lost in the PERIODIC_TX cycle
  1195. break;
  1196. case AD_SLOW_PERIODIC:
  1197. port->sm_periodic_timer_counter = __ad_timer_to_ticks(AD_PERIODIC_TIMER, (u16)(AD_SLOW_PERIODIC_TIME))-1; // decrement 1 tick we lost in the PERIODIC_TX cycle
  1198. break;
  1199. case AD_PERIODIC_TX:
  1200. port->ntt = 1;
  1201. break;
  1202. default: //to silence the compiler
  1203. break;
  1204. }
  1205. }
  1206. }
  1207. /**
  1208. * ad_port_selection_logic - select aggregation groups
  1209. * @port: the port we're looking at
  1210. *
  1211. * Select aggregation groups, and assign each port for it's aggregetor. The
  1212. * selection logic is called in the inititalization (after all the handshkes),
  1213. * and after every lacpdu receive (if selected is off).
  1214. */
  1215. static void ad_port_selection_logic(struct port *port)
  1216. {
  1217. struct aggregator *aggregator, *free_aggregator = NULL, *temp_aggregator;
  1218. struct port *last_port = NULL, *curr_port;
  1219. int found = 0;
  1220. // if the port is already Selected, do nothing
  1221. if (port->sm_vars & AD_PORT_SELECTED) {
  1222. return;
  1223. }
  1224. // if the port is connected to other aggregator, detach it
  1225. if (port->aggregator) {
  1226. // detach the port from its former aggregator
  1227. temp_aggregator=port->aggregator;
  1228. for (curr_port=temp_aggregator->lag_ports; curr_port; last_port=curr_port, curr_port=curr_port->next_port_in_aggregator) {
  1229. if (curr_port == port) {
  1230. temp_aggregator->num_of_ports--;
  1231. if (!last_port) {// if it is the first port attached to the aggregator
  1232. temp_aggregator->lag_ports=port->next_port_in_aggregator;
  1233. } else {// not the first port attached to the aggregator
  1234. last_port->next_port_in_aggregator=port->next_port_in_aggregator;
  1235. }
  1236. // clear the port's relations to this aggregator
  1237. port->aggregator = NULL;
  1238. port->next_port_in_aggregator=NULL;
  1239. port->actor_port_aggregator_identifier=0;
  1240. dprintk("Port %d left LAG %d\n", port->actor_port_number, temp_aggregator->aggregator_identifier);
  1241. // if the aggregator is empty, clear its parameters, and set it ready to be attached
  1242. if (!temp_aggregator->lag_ports) {
  1243. ad_clear_agg(temp_aggregator);
  1244. }
  1245. break;
  1246. }
  1247. }
  1248. if (!curr_port) { // meaning: the port was related to an aggregator but was not on the aggregator port list
  1249. printk(KERN_WARNING DRV_NAME ": Warning: Port %d (on %s) was "
  1250. "related to aggregator %d but was not on its port list\n",
  1251. port->actor_port_number, port->slave->dev->name,
  1252. port->aggregator->aggregator_identifier);
  1253. }
  1254. }
  1255. // search on all aggregators for a suitable aggregator for this port
  1256. for (aggregator = __get_first_agg(port); aggregator;
  1257. aggregator = __get_next_agg(aggregator)) {
  1258. // keep a free aggregator for later use(if needed)
  1259. if (!aggregator->lag_ports) {
  1260. if (!free_aggregator) {
  1261. free_aggregator=aggregator;
  1262. }
  1263. continue;
  1264. }
  1265. // check if current aggregator suits us
  1266. if (((aggregator->actor_oper_aggregator_key == port->actor_oper_port_key) && // if all parameters match AND
  1267. !MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(port->partner_oper_system)) &&
  1268. (aggregator->partner_system_priority == port->partner_oper_system_priority) &&
  1269. (aggregator->partner_oper_aggregator_key == port->partner_oper_key)
  1270. ) &&
  1271. ((MAC_ADDRESS_COMPARE(&(port->partner_oper_system), &(null_mac_addr)) && // partner answers
  1272. !aggregator->is_individual) // but is not individual OR
  1273. )
  1274. ) {
  1275. // attach to the founded aggregator
  1276. port->aggregator = aggregator;
  1277. port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
  1278. port->next_port_in_aggregator=aggregator->lag_ports;
  1279. port->aggregator->num_of_ports++;
  1280. aggregator->lag_ports=port;
  1281. dprintk("Port %d joined LAG %d(existing LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
  1282. // mark this port as selected
  1283. port->sm_vars |= AD_PORT_SELECTED;
  1284. found = 1;
  1285. break;
  1286. }
  1287. }
  1288. // the port couldn't find an aggregator - attach it to a new aggregator
  1289. if (!found) {
  1290. if (free_aggregator) {
  1291. // assign port a new aggregator
  1292. port->aggregator = free_aggregator;
  1293. port->actor_port_aggregator_identifier=port->aggregator->aggregator_identifier;
  1294. // update the new aggregator's parameters
  1295. // if port was responsed from the end-user
  1296. if (port->actor_oper_port_key & AD_DUPLEX_KEY_BITS) {// if port is full duplex
  1297. port->aggregator->is_individual = 0;
  1298. } else {
  1299. port->aggregator->is_individual = 1;
  1300. }
  1301. port->aggregator->actor_admin_aggregator_key = port->actor_admin_port_key;
  1302. port->aggregator->actor_oper_aggregator_key = port->actor_oper_port_key;
  1303. port->aggregator->partner_system=port->partner_oper_system;
  1304. port->aggregator->partner_system_priority = port->partner_oper_system_priority;
  1305. port->aggregator->partner_oper_aggregator_key = port->partner_oper_key;
  1306. port->aggregator->receive_state = 1;
  1307. port->aggregator->transmit_state = 1;
  1308. port->aggregator->lag_ports = port;
  1309. port->aggregator->num_of_ports++;
  1310. // mark this port as selected
  1311. port->sm_vars |= AD_PORT_SELECTED;
  1312. dprintk("Port %d joined LAG %d(new LAG)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
  1313. } else {
  1314. printk(KERN_ERR DRV_NAME ": Port %d (on %s) did not find a suitable aggregator\n",
  1315. port->actor_port_number, port->slave->dev->name);
  1316. }
  1317. }
  1318. // if all aggregator's ports are READY_N == TRUE, set ready=TRUE in all aggregator's ports
  1319. // else set ready=FALSE in all aggregator's ports
  1320. __set_agg_ports_ready(port->aggregator, __agg_ports_are_ready(port->aggregator));
  1321. if (!__check_agg_selection_timer(port) && (aggregator = __get_first_agg(port))) {
  1322. ad_agg_selection_logic(aggregator);
  1323. }
  1324. }
  1325. /**
  1326. * ad_agg_selection_logic - select an aggregation group for a team
  1327. * @aggregator: the aggregator we're looking at
  1328. *
  1329. * It is assumed that only one aggregator may be selected for a team.
  1330. * The logic of this function is to select (at first time) the aggregator with
  1331. * the most ports attached to it, and to reselect the active aggregator only if
  1332. * the previous aggregator has no more ports related to it.
  1333. *
  1334. * FIXME: this function MUST be called with the first agg in the bond, or
  1335. * __get_active_agg() won't work correctly. This function should be better
  1336. * called with the bond itself, and retrieve the first agg from it.
  1337. */
  1338. static void ad_agg_selection_logic(struct aggregator *aggregator)
  1339. {
  1340. struct aggregator *best_aggregator = NULL, *active_aggregator = NULL;
  1341. struct aggregator *last_active_aggregator = NULL, *origin_aggregator;
  1342. struct port *port;
  1343. u16 num_of_aggs=0;
  1344. origin_aggregator = aggregator;
  1345. //get current active aggregator
  1346. last_active_aggregator = __get_active_agg(aggregator);
  1347. // search for the aggregator with the most ports attached to it.
  1348. do {
  1349. // count how many candidate lag's we have
  1350. if (aggregator->lag_ports) {
  1351. num_of_aggs++;
  1352. }
  1353. if (aggregator->is_active && !aggregator->is_individual && // if current aggregator is the active aggregator
  1354. MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(null_mac_addr))) { // and partner answers to 802.3ad PDUs
  1355. if (aggregator->num_of_ports) { // if any ports attached to the current aggregator
  1356. best_aggregator=NULL; // disregard the best aggregator that was chosen by now
  1357. break; // stop the selection of other aggregator if there are any ports attached to this active aggregator
  1358. } else { // no ports attached to this active aggregator
  1359. aggregator->is_active = 0; // mark this aggregator as not active anymore
  1360. }
  1361. }
  1362. if (aggregator->num_of_ports) { // if any ports attached
  1363. if (best_aggregator) { // if there is a candidte aggregator
  1364. //The reasons for choosing new best aggregator:
  1365. // 1. if current agg is NOT individual and the best agg chosen so far is individual OR
  1366. // current and best aggs are both individual or both not individual, AND
  1367. // 2a. current agg partner reply but best agg partner do not reply OR
  1368. // 2b. current agg partner reply OR current agg partner do not reply AND best agg partner also do not reply AND
  1369. // current has more ports/bandwidth, or same amount of ports but current has faster ports, THEN
  1370. // current agg become best agg so far
  1371. //if current agg is NOT individual and the best agg chosen so far is individual change best_aggregator
  1372. if (!aggregator->is_individual && best_aggregator->is_individual) {
  1373. best_aggregator=aggregator;
  1374. }
  1375. // current and best aggs are both individual or both not individual
  1376. else if ((aggregator->is_individual && best_aggregator->is_individual) ||
  1377. (!aggregator->is_individual && !best_aggregator->is_individual)) {
  1378. // current and best aggs are both individual or both not individual AND
  1379. // current agg partner reply but best agg partner do not reply
  1380. if ((MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(null_mac_addr)) &&
  1381. !MAC_ADDRESS_COMPARE(&(best_aggregator->partner_system), &(null_mac_addr)))) {
  1382. best_aggregator=aggregator;
  1383. }
  1384. // current agg partner reply OR current agg partner do not reply AND best agg partner also do not reply
  1385. else if (! (!MAC_ADDRESS_COMPARE(&(aggregator->partner_system), &(null_mac_addr)) &&
  1386. MAC_ADDRESS_COMPARE(&(best_aggregator->partner_system), &(null_mac_addr)))) {
  1387. if ((__get_agg_selection_mode(aggregator->lag_ports) == AD_BANDWIDTH)&&
  1388. (__get_agg_bandwidth(aggregator) > __get_agg_bandwidth(best_aggregator))) {
  1389. best_aggregator=aggregator;
  1390. } else if (__get_agg_selection_mode(aggregator->lag_ports) == AD_COUNT) {
  1391. if (((aggregator->num_of_ports > best_aggregator->num_of_ports) &&
  1392. (aggregator->actor_oper_aggregator_key & AD_SPEED_KEY_BITS))||
  1393. ((aggregator->num_of_ports == best_aggregator->num_of_ports) &&
  1394. ((u16)(aggregator->actor_oper_aggregator_key & AD_SPEED_KEY_BITS) >
  1395. (u16)(best_aggregator->actor_oper_aggregator_key & AD_SPEED_KEY_BITS)))) {
  1396. best_aggregator=aggregator;
  1397. }
  1398. }
  1399. }
  1400. }
  1401. } else {
  1402. best_aggregator=aggregator;
  1403. }
  1404. }
  1405. aggregator->is_active = 0; // mark all aggregators as not active anymore
  1406. } while ((aggregator = __get_next_agg(aggregator)));
  1407. // if we have new aggregator selected, don't replace the old aggregator if it has an answering partner,
  1408. // or if both old aggregator and new aggregator don't have answering partner
  1409. if (best_aggregator) {
  1410. if (last_active_aggregator && last_active_aggregator->lag_ports && last_active_aggregator->lag_ports->is_enabled &&
  1411. (MAC_ADDRESS_COMPARE(&(last_active_aggregator->partner_system), &(null_mac_addr)) || // partner answers OR
  1412. (!MAC_ADDRESS_COMPARE(&(last_active_aggregator->partner_system), &(null_mac_addr)) && // both old and new
  1413. !MAC_ADDRESS_COMPARE(&(best_aggregator->partner_system), &(null_mac_addr)))) // partner do not answer
  1414. ) {
  1415. // if new aggregator has link, and old aggregator does not, replace old aggregator.(do nothing)
  1416. // -> don't replace otherwise.
  1417. if (!(!last_active_aggregator->actor_oper_aggregator_key && best_aggregator->actor_oper_aggregator_key)) {
  1418. best_aggregator=NULL;
  1419. last_active_aggregator->is_active = 1; // don't replace good old aggregator
  1420. }
  1421. }
  1422. }
  1423. // if there is new best aggregator, activate it
  1424. if (best_aggregator) {
  1425. for (aggregator = __get_first_agg(best_aggregator->lag_ports);
  1426. aggregator;
  1427. aggregator = __get_next_agg(aggregator)) {
  1428. dprintk("Agg=%d; Ports=%d; a key=%d; p key=%d; Indiv=%d; Active=%d\n",
  1429. aggregator->aggregator_identifier, aggregator->num_of_ports,
  1430. aggregator->actor_oper_aggregator_key, aggregator->partner_oper_aggregator_key,
  1431. aggregator->is_individual, aggregator->is_active);
  1432. }
  1433. // check if any partner replys
  1434. if (best_aggregator->is_individual) {
  1435. printk(KERN_WARNING DRV_NAME ": Warning: No 802.3ad response from the link partner "
  1436. "for any adapters in the bond\n");
  1437. }
  1438. // check if there are more than one aggregator
  1439. if (num_of_aggs > 1) {
  1440. dprintk("Warning: More than one Link Aggregation Group was "
  1441. "found in the bond. Only one group will function in the bond\n");
  1442. }
  1443. best_aggregator->is_active = 1;
  1444. dprintk("LAG %d choosed as the active LAG\n", best_aggregator->aggregator_identifier);
  1445. dprintk("Agg=%d; Ports=%d; a key=%d; p key=%d; Indiv=%d; Active=%d\n",
  1446. best_aggregator->aggregator_identifier, best_aggregator->num_of_ports,
  1447. best_aggregator->actor_oper_aggregator_key, best_aggregator->partner_oper_aggregator_key,
  1448. best_aggregator->is_individual, best_aggregator->is_active);
  1449. // disable the ports that were related to the former active_aggregator
  1450. if (last_active_aggregator) {
  1451. for (port=last_active_aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
  1452. __disable_port(port);
  1453. }
  1454. }
  1455. }
  1456. // if the selected aggregator is of join individuals(partner_system is NULL), enable their ports
  1457. active_aggregator = __get_active_agg(origin_aggregator);
  1458. if (active_aggregator) {
  1459. if (!MAC_ADDRESS_COMPARE(&(active_aggregator->partner_system), &(null_mac_addr))) {
  1460. for (port=active_aggregator->lag_ports; port; port=port->next_port_in_aggregator) {
  1461. __enable_port(port);
  1462. }
  1463. }
  1464. }
  1465. }
  1466. /**
  1467. * ad_clear_agg - clear a given aggregator's parameters
  1468. * @aggregator: the aggregator we're looking at
  1469. *
  1470. */
  1471. static void ad_clear_agg(struct aggregator *aggregator)
  1472. {
  1473. if (aggregator) {
  1474. aggregator->is_individual = 0;
  1475. aggregator->actor_admin_aggregator_key = 0;
  1476. aggregator->actor_oper_aggregator_key = 0;
  1477. aggregator->partner_system = null_mac_addr;
  1478. aggregator->partner_system_priority = 0;
  1479. aggregator->partner_oper_aggregator_key = 0;
  1480. aggregator->receive_state = 0;
  1481. aggregator->transmit_state = 0;
  1482. aggregator->lag_ports = NULL;
  1483. aggregator->is_active = 0;
  1484. aggregator->num_of_ports = 0;
  1485. dprintk("LAG %d was cleared\n", aggregator->aggregator_identifier);
  1486. }
  1487. }
  1488. /**
  1489. * ad_initialize_agg - initialize a given aggregator's parameters
  1490. * @aggregator: the aggregator we're looking at
  1491. *
  1492. */
  1493. static void ad_initialize_agg(struct aggregator *aggregator)
  1494. {
  1495. if (aggregator) {
  1496. ad_clear_agg(aggregator);
  1497. aggregator->aggregator_mac_address = null_mac_addr;
  1498. aggregator->aggregator_identifier = 0;
  1499. aggregator->slave = NULL;
  1500. }
  1501. }
  1502. /**
  1503. * ad_initialize_port - initialize a given port's parameters
  1504. * @aggregator: the aggregator we're looking at
  1505. * @lacp_fast: boolean. whether fast periodic should be used
  1506. *
  1507. */
  1508. static void ad_initialize_port(struct port *port, int lacp_fast)
  1509. {
  1510. if (port) {
  1511. port->actor_port_number = 1;
  1512. port->actor_port_priority = 0xff;
  1513. port->actor_system = null_mac_addr;
  1514. port->actor_system_priority = 0xffff;
  1515. port->actor_port_aggregator_identifier = 0;
  1516. port->ntt = 0;
  1517. port->actor_admin_port_key = 1;
  1518. port->actor_oper_port_key = 1;
  1519. port->actor_admin_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
  1520. port->actor_oper_port_state = AD_STATE_AGGREGATION | AD_STATE_LACP_ACTIVITY;
  1521. if (lacp_fast) {
  1522. port->actor_oper_port_state |= AD_STATE_LACP_TIMEOUT;
  1523. }
  1524. port->partner_admin_system = null_mac_addr;
  1525. port->partner_oper_system = null_mac_addr;
  1526. port->partner_admin_system_priority = 0xffff;
  1527. port->partner_oper_system_priority = 0xffff;
  1528. port->partner_admin_key = 1;
  1529. port->partner_oper_key = 1;
  1530. port->partner_admin_port_number = 1;
  1531. port->partner_oper_port_number = 1;
  1532. port->partner_admin_port_priority = 0xff;
  1533. port->partner_oper_port_priority = 0xff;
  1534. port->partner_admin_port_state = 1;
  1535. port->partner_oper_port_state = 1;
  1536. port->is_enabled = 1;
  1537. // ****** private parameters ******
  1538. port->sm_vars = 0x3;
  1539. port->sm_rx_state = 0;
  1540. port->sm_rx_timer_counter = 0;
  1541. port->sm_periodic_state = 0;
  1542. port->sm_periodic_timer_counter = 0;
  1543. port->sm_mux_state = 0;
  1544. port->sm_mux_timer_counter = 0;
  1545. port->sm_tx_state = 0;
  1546. port->sm_tx_timer_counter = 0;
  1547. port->slave = NULL;
  1548. port->aggregator = NULL;
  1549. port->next_port_in_aggregator = NULL;
  1550. port->transaction_id = 0;
  1551. ad_initialize_lacpdu(&(port->lacpdu));
  1552. }
  1553. }
  1554. /**
  1555. * ad_enable_collecting_distributing - enable a port's transmit/receive
  1556. * @port: the port we're looking at
  1557. *
  1558. * Enable @port if it's in an active aggregator
  1559. */
  1560. static void ad_enable_collecting_distributing(struct port *port)
  1561. {
  1562. if (port->aggregator->is_active) {
  1563. dprintk("Enabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
  1564. __enable_port(port);
  1565. }
  1566. }
  1567. /**
  1568. * ad_disable_collecting_distributing - disable a port's transmit/receive
  1569. * @port: the port we're looking at
  1570. *
  1571. */
  1572. static void ad_disable_collecting_distributing(struct port *port)
  1573. {
  1574. if (port->aggregator && MAC_ADDRESS_COMPARE(&(port->aggregator->partner_system), &(null_mac_addr))) {
  1575. dprintk("Disabling port %d(LAG %d)\n", port->actor_port_number, port->aggregator->aggregator_identifier);
  1576. __disable_port(port);
  1577. }
  1578. }
  1579. #if 0
  1580. /**
  1581. * ad_marker_info_send - send a marker information frame
  1582. * @port: the port we're looking at
  1583. *
  1584. * This function does nothing since we decided not to implement send and handle
  1585. * response for marker PDU's, in this stage, but only to respond to marker
  1586. * information.
  1587. */
  1588. static void ad_marker_info_send(struct port *port)
  1589. {
  1590. struct marker marker;
  1591. u16 index;
  1592. // fill the marker PDU with the appropriate values
  1593. marker.subtype = 0x02;
  1594. marker.version_number = 0x01;
  1595. marker.tlv_type = AD_MARKER_INFORMATION_SUBTYPE;
  1596. marker.marker_length = 0x16;
  1597. // convert requester_port to Big Endian
  1598. marker.requester_port = (((port->actor_port_number & 0xFF) << 8) |((u16)(port->actor_port_number & 0xFF00) >> 8));
  1599. marker.requester_system = port->actor_system;
  1600. // convert requester_port(u32) to Big Endian
  1601. marker.requester_transaction_id = (((++port->transaction_id & 0xFF) << 24) |((port->transaction_id & 0xFF00) << 8) |((port->transaction_id & 0xFF0000) >> 8) |((port->transaction_id & 0xFF000000) >> 24));
  1602. marker.pad = 0;
  1603. marker.tlv_type_terminator = 0x00;
  1604. marker.terminator_length = 0x00;
  1605. for (index=0; index<90; index++) {
  1606. marker.reserved_90[index]=0;
  1607. }
  1608. // send the marker information
  1609. if (ad_marker_send(port, &marker) >= 0) {
  1610. dprintk("Sent Marker Information on port %d\n", port->actor_port_number);
  1611. }
  1612. }
  1613. #endif
  1614. /**
  1615. * ad_marker_info_received - handle receive of a Marker information frame
  1616. * @marker_info: Marker info received
  1617. * @port: the port we're looking at
  1618. *
  1619. */
  1620. static void ad_marker_info_received(struct marker *marker_info,struct port *port)
  1621. {
  1622. struct marker marker;
  1623. // copy the received marker data to the response marker
  1624. //marker = *marker_info;
  1625. memcpy(&marker, marker_info, sizeof(struct marker));
  1626. // change the marker subtype to marker response
  1627. marker.tlv_type=AD_MARKER_RESPONSE_SUBTYPE;
  1628. // send the marker response
  1629. if (ad_marker_send(port, &marker) >= 0) {
  1630. dprintk("Sent Marker Response on port %d\n", port->actor_port_number);
  1631. }
  1632. }
  1633. /**
  1634. * ad_marker_response_received - handle receive of a marker response frame
  1635. * @marker: marker PDU received
  1636. * @port: the port we're looking at
  1637. *
  1638. * This function does nothing since we decided not to implement send and handle
  1639. * response for marker PDU's, in this stage, but only to respond to marker
  1640. * information.
  1641. */
  1642. static void ad_marker_response_received(struct marker *marker, struct port *port)
  1643. {
  1644. marker=NULL; // just to satisfy the compiler
  1645. port=NULL; // just to satisfy the compiler
  1646. // DO NOTHING, SINCE WE DECIDED NOT TO IMPLEMENT THIS FEATURE FOR NOW
  1647. }
  1648. /**
  1649. * ad_initialize_lacpdu - initialize a given lacpdu structure
  1650. * @lacpdu: lacpdu structure to initialize
  1651. *
  1652. */
  1653. static void ad_initialize_lacpdu(struct lacpdu *lacpdu)
  1654. {
  1655. u16 index;
  1656. // initialize lacpdu data
  1657. lacpdu->subtype = 0x01;
  1658. lacpdu->version_number = 0x01;
  1659. lacpdu->tlv_type_actor_info = 0x01;
  1660. lacpdu->actor_information_length = 0x14;
  1661. // lacpdu->actor_system_priority updated on send
  1662. // lacpdu->actor_system updated on send
  1663. // lacpdu->actor_key updated on send
  1664. // lacpdu->actor_port_priority updated on send
  1665. // lacpdu->actor_port updated on send
  1666. // lacpdu->actor_state updated on send
  1667. lacpdu->tlv_type_partner_info = 0x02;
  1668. lacpdu->partner_information_length = 0x14;
  1669. for (index=0; index<=2; index++) {
  1670. lacpdu->reserved_3_1[index]=0;
  1671. }
  1672. // lacpdu->partner_system_priority updated on send
  1673. // lacpdu->partner_system updated on send
  1674. // lacpdu->partner_key updated on send
  1675. // lacpdu->partner_port_priority updated on send
  1676. // lacpdu->partner_port updated on send
  1677. // lacpdu->partner_state updated on send
  1678. for (index=0; index<=2; index++) {
  1679. lacpdu->reserved_3_2[index]=0;
  1680. }
  1681. lacpdu->tlv_type_collector_info = 0x03;
  1682. lacpdu->collector_information_length= 0x10;
  1683. lacpdu->collector_max_delay = AD_COLLECTOR_MAX_DELAY;
  1684. for (index=0; index<=11; index++) {
  1685. lacpdu->reserved_12[index]=0;
  1686. }
  1687. lacpdu->tlv_type_terminator = 0x00;
  1688. lacpdu->terminator_length = 0;
  1689. for (index=0; index<=49; index++) {
  1690. lacpdu->reserved_50[index]=0;
  1691. }
  1692. }
  1693. //////////////////////////////////////////////////////////////////////////////////////
  1694. // ================= AD exported functions to the main bonding code ==================
  1695. //////////////////////////////////////////////////////////////////////////////////////
  1696. // Check aggregators status in team every T seconds
  1697. #define AD_AGGREGATOR_SELECTION_TIMER 8
  1698. static u16 aggregator_identifier;
  1699. /**
  1700. * bond_3ad_initialize - initialize a bond's 802.3ad parameters and structures
  1701. * @bond: bonding struct to work on
  1702. * @tick_resolution: tick duration (millisecond resolution)
  1703. * @lacp_fast: boolean. whether fast periodic should be used
  1704. *
  1705. * Can be called only after the mac address of the bond is set.
  1706. */
  1707. void bond_3ad_initialize(struct bonding *bond, u16 tick_resolution, int lacp_fast)
  1708. {
  1709. // check that the bond is not initialized yet
  1710. if (MAC_ADDRESS_COMPARE(&(BOND_AD_INFO(bond).system.sys_mac_addr), &(bond->dev->dev_addr))) {
  1711. aggregator_identifier = 0;
  1712. BOND_AD_INFO(bond).lacp_fast = lacp_fast;
  1713. BOND_AD_INFO(bond).system.sys_priority = 0xFFFF;
  1714. BOND_AD_INFO(bond).system.sys_mac_addr = *((struct mac_addr *)bond->dev->dev_addr);
  1715. // initialize how many times this module is called in one second(should be about every 100ms)
  1716. ad_ticks_per_sec = tick_resolution;
  1717. // initialize the aggregator selection timer(to activate an aggregation selection after initialize)
  1718. BOND_AD_INFO(bond).agg_select_timer = (AD_AGGREGATOR_SELECTION_TIMER * ad_ticks_per_sec);
  1719. BOND_AD_INFO(bond).agg_select_mode = AD_BANDWIDTH;
  1720. }
  1721. }
  1722. /**
  1723. * bond_3ad_bind_slave - initialize a slave's port
  1724. * @slave: slave struct to work on
  1725. *
  1726. * Returns: 0 on success
  1727. * < 0 on error
  1728. */
  1729. int bond_3ad_bind_slave(struct slave *slave)
  1730. {
  1731. struct bonding *bond = bond_get_bond_by_slave(slave);
  1732. struct port *port;
  1733. struct aggregator *aggregator;
  1734. if (bond == NULL) {
  1735. printk(KERN_ERR "The slave %s is not attached to its bond\n", slave->dev->name);
  1736. return -1;
  1737. }
  1738. //check that the slave has not been intialized yet.
  1739. if (SLAVE_AD_INFO(slave).port.slave != slave) {
  1740. // port initialization
  1741. port = &(SLAVE_AD_INFO(slave).port);
  1742. ad_initialize_port(port, BOND_AD_INFO(bond).lacp_fast);
  1743. port->slave = slave;
  1744. port->actor_port_number = SLAVE_AD_INFO(slave).id;
  1745. // key is determined according to the link speed, duplex and user key(which is yet not supported)
  1746. // ------------------------------------------------------------
  1747. // Port key : | User key | Speed |Duplex|
  1748. // ------------------------------------------------------------
  1749. // 16 6 1 0
  1750. port->actor_admin_port_key = 0; // initialize this parameter
  1751. port->actor_admin_port_key |= __get_duplex(port);
  1752. port->actor_admin_port_key |= (__get_link_speed(port) << 1);
  1753. port->actor_oper_port_key = port->actor_admin_port_key;
  1754. // if the port is not full duplex, then the port should be not lacp Enabled
  1755. if (!(port->actor_oper_port_key & AD_DUPLEX_KEY_BITS)) {
  1756. port->sm_vars &= ~AD_PORT_LACP_ENABLED;
  1757. }
  1758. // actor system is the bond's system
  1759. port->actor_system = BOND_AD_INFO(bond).system.sys_mac_addr;
  1760. // tx timer(to verify that no more than MAX_TX_IN_SECOND lacpdu's are sent in one second)
  1761. port->sm_tx_timer_counter = ad_ticks_per_sec/AD_MAX_TX_IN_SECOND;
  1762. port->aggregator = NULL;
  1763. port->next_port_in_aggregator = NULL;
  1764. __disable_port(port);
  1765. __initialize_port_locks(port);
  1766. // aggregator initialization
  1767. aggregator = &(SLAVE_AD_INFO(slave).aggregator);
  1768. ad_initialize_agg(aggregator);
  1769. aggregator->aggregator_mac_address = *((struct mac_addr *)bond->dev->dev_addr);
  1770. aggregator->aggregator_identifier = (++aggregator_identifier);
  1771. aggregator->slave = slave;
  1772. aggregator->is_active = 0;
  1773. aggregator->num_of_ports = 0;
  1774. }
  1775. return 0;
  1776. }
  1777. /**
  1778. * bond_3ad_unbind_slave - deinitialize a slave's port
  1779. * @slave: slave struct to work on
  1780. *
  1781. * Search for the aggregator that is related to this port, remove the
  1782. * aggregator and assign another aggregator for other port related to it
  1783. * (if any), and remove the port.
  1784. */
  1785. void bond_3ad_unbind_slave(struct slave *slave)
  1786. {
  1787. struct port *port, *prev_port, *temp_port;
  1788. struct aggregator *aggregator, *new_aggregator, *temp_aggregator;
  1789. int select_new_active_agg = 0;
  1790. // find the aggregator related to this slave
  1791. aggregator = &(SLAVE_AD_INFO(slave).aggregator);
  1792. // find the port related to this slave
  1793. port = &(SLAVE_AD_INFO(slave).port);
  1794. // if slave is null, the whole port is not initialized
  1795. if (!port->slave) {
  1796. printk(KERN_WARNING DRV_NAME ": Trying to unbind an uninitialized port on %s\n", slave->dev->name);
  1797. return;
  1798. }
  1799. dprintk("Unbinding Link Aggregation Group %d\n", aggregator->aggregator_identifier);
  1800. /* Tell the partner that this port is not suitable for aggregation */
  1801. port->actor_oper_port_state &= ~AD_STATE_AGGREGATION;
  1802. __update_lacpdu_from_port(port);
  1803. ad_lacpdu_send(port);
  1804. // check if this aggregator is occupied
  1805. if (aggregator->lag_ports) {
  1806. // check if there are other ports related to this aggregator except
  1807. // the port related to this slave(thats ensure us that there is a
  1808. // reason to search for new aggregator, and that we will find one
  1809. if ((aggregator->lag_ports != port) || (aggregator->lag_ports->next_port_in_aggregator)) {
  1810. // find new aggregator for the related port(s)
  1811. new_aggregator = __get_first_agg(port);
  1812. for (; new_aggregator; new_aggregator = __get_next_agg(new_aggregator)) {
  1813. // if the new aggregator is empty, or it connected to to our port only
  1814. if (!new_aggregator->lag_ports || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator)) {
  1815. break;
  1816. }
  1817. }
  1818. // if new aggregator found, copy the aggregator's parameters
  1819. // and connect the related lag_ports to the new aggregator
  1820. if ((new_aggregator) && ((!new_aggregator->lag_ports) || ((new_aggregator->lag_ports == port) && !new_aggregator->lag_ports->next_port_in_aggregator))) {
  1821. dprintk("Some port(s) related to LAG %d - replaceing with LAG %d\n", aggregator->aggregator_identifier, new_aggregator->aggregator_identifier);
  1822. if ((new_aggregator->lag_ports == port) && new_aggregator->is_active) {
  1823. printk(KERN_INFO DRV_NAME ": Removing an active aggregator\n");
  1824. // select new active aggregator
  1825. select_new_active_agg = 1;
  1826. }
  1827. new_aggregator->is_individual = aggregator->is_individual;
  1828. new_aggregator->actor_admin_aggregator_key = aggregator->actor_admin_aggregator_key;
  1829. new_aggregator->actor_oper_aggregator_key = aggregator->actor_oper_aggregator_key;
  1830. new_aggregator->partner_system = aggregator->partner_system;
  1831. new_aggregator->partner_system_priority = aggregator->partner_system_priority;
  1832. new_aggregator->partner_oper_aggregator_key = aggregator->partner_oper_aggregator_key;
  1833. new_aggregator->receive_state = aggregator->receive_state;
  1834. new_aggregator->transmit_state = aggregator->transmit_state;
  1835. new_aggregator->lag_ports = aggregator->lag_ports;
  1836. new_aggregator->is_active = aggregator->is_active;
  1837. new_aggregator->num_of_ports = aggregator->num_of_ports;
  1838. // update the information that is written on the ports about the aggregator
  1839. for (temp_port=aggregator->lag_ports; temp_port; temp_port=temp_port->next_port_in_aggregator) {
  1840. temp_port->aggregator=new_aggregator;
  1841. temp_port->actor_port_aggregator_identifier = new_aggregator->aggregator_identifier;
  1842. }
  1843. // clear the aggregator
  1844. ad_clear_agg(aggregator);
  1845. if (select_new_active_agg) {
  1846. ad_agg_selection_logic(__get_first_agg(port));
  1847. }
  1848. } else {
  1849. printk(KERN_WARNING DRV_NAME ": Warning: unbinding aggregator, "
  1850. "and could not find a new aggregator for its ports\n");
  1851. }
  1852. } else { // in case that the only port related to this aggregator is the one we want to remove
  1853. select_new_active_agg = aggregator->is_active;
  1854. // clear the aggregator
  1855. ad_clear_agg(aggregator);
  1856. if (select_new_active_agg) {
  1857. printk(KERN_INFO "Removing an active aggregator\n");
  1858. // select new active aggregator
  1859. ad_agg_selection_logic(__get_first_agg(port));
  1860. }
  1861. }
  1862. }
  1863. dprintk("Unbinding port %d\n", port->actor_port_number);
  1864. // find the aggregator that this port is connected to
  1865. temp_aggregator = __get_first_agg(port);
  1866. for (; temp_aggregator; temp_aggregator = __get_next_agg(temp_aggregator)) {
  1867. prev_port = NULL;
  1868. // search the port in the aggregator's related ports
  1869. for (temp_port=temp_aggregator->lag_ports; temp_port; prev_port=temp_port, temp_port=temp_port->next_port_in_aggregator) {
  1870. if (temp_port == port) { // the aggregator found - detach the port from this aggregator
  1871. if (prev_port) {
  1872. prev_port->next_port_in_aggregator = temp_port->next_port_in_aggregator;
  1873. } else {
  1874. temp_aggregator->lag_ports = temp_port->next_port_in_aggregator;
  1875. }
  1876. temp_aggregator->num_of_ports--;
  1877. if (temp_aggregator->num_of_ports==0) {
  1878. select_new_active_agg = temp_aggregator->is_active;
  1879. // clear the aggregator
  1880. ad_clear_agg(temp_aggregator);
  1881. if (select_new_active_agg) {
  1882. printk(KERN_INFO "Removing an active aggregator\n");
  1883. // select new active aggregator
  1884. ad_agg_selection_logic(__get_first_agg(port));
  1885. }
  1886. }
  1887. break;
  1888. }
  1889. }
  1890. }
  1891. port->slave=NULL;
  1892. }
  1893. /**
  1894. * bond_3ad_state_machine_handler - handle state machines timeout
  1895. * @bond: bonding struct to work on
  1896. *
  1897. * The state machine handling concept in this module is to check every tick
  1898. * which state machine should operate any function. The execution order is
  1899. * round robin, so when we have an interaction between state machines, the
  1900. * reply of one to each other might be delayed until next tick.
  1901. *
  1902. * This function also complete the initialization when the agg_select_timer
  1903. * times out, and it selects an aggregator for the ports that are yet not
  1904. * related to any aggregator, and selects the active aggregator for a bond.
  1905. */
  1906. void bond_3ad_state_machine_handler(struct bonding *bond)
  1907. {
  1908. struct port *port;
  1909. struct aggregator *aggregator;
  1910. read_lock(&bond->lock);
  1911. if (bond->kill_timers) {
  1912. goto out;
  1913. }
  1914. //check if there are any slaves
  1915. if (bond->slave_cnt == 0) {
  1916. goto re_arm;
  1917. }
  1918. // check if agg_select_timer timer after initialize is timed out
  1919. if (BOND_AD_INFO(bond).agg_select_timer && !(--BOND_AD_INFO(bond).agg_select_timer)) {
  1920. // select the active aggregator for the bond
  1921. if ((port = __get_first_port(bond))) {
  1922. if (!port->slave) {
  1923. printk(KERN_WARNING DRV_NAME ": Warning: bond's first port is uninitialized\n");
  1924. goto re_arm;
  1925. }
  1926. aggregator = __get_first_agg(port);
  1927. ad_agg_selection_logic(aggregator);
  1928. }
  1929. }
  1930. // for each port run the state machines
  1931. for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
  1932. if (!port->slave) {
  1933. printk(KERN_WARNING DRV_NAME ": Warning: Found an uninitialized port\n");
  1934. goto re_arm;
  1935. }
  1936. ad_rx_machine(NULL, port);
  1937. ad_periodic_machine(port);
  1938. ad_port_selection_logic(port);
  1939. ad_mux_machine(port);
  1940. ad_tx_machine(port);
  1941. // turn off the BEGIN bit, since we already handled it
  1942. if (port->sm_vars & AD_PORT_BEGIN) {
  1943. port->sm_vars &= ~AD_PORT_BEGIN;
  1944. }
  1945. }
  1946. re_arm:
  1947. mod_timer(&(BOND_AD_INFO(bond).ad_timer), jiffies + ad_delta_in_ticks);
  1948. out:
  1949. read_unlock(&bond->lock);
  1950. }
  1951. /**
  1952. * bond_3ad_rx_indication - handle a received frame
  1953. * @lacpdu: received lacpdu
  1954. * @slave: slave struct to work on
  1955. * @length: length of the data received
  1956. *
  1957. * It is assumed that frames that were sent on this NIC don't returned as new
  1958. * received frames (loopback). Since only the payload is given to this
  1959. * function, it check for loopback.
  1960. */
  1961. static void bond_3ad_rx_indication(struct lacpdu *lacpdu, struct slave *slave, u16 length)
  1962. {
  1963. struct port *port;
  1964. if (length >= sizeof(struct lacpdu)) {
  1965. port = &(SLAVE_AD_INFO(slave).port);
  1966. if (!port->slave) {
  1967. printk(KERN_WARNING DRV_NAME ": Warning: port of slave %s is uninitialized\n", slave->dev->name);
  1968. return;
  1969. }
  1970. switch (lacpdu->subtype) {
  1971. case AD_TYPE_LACPDU:
  1972. __ntohs_lacpdu(lacpdu);
  1973. dprintk("Received LACPDU on port %d\n", port->actor_port_number);
  1974. ad_rx_machine(lacpdu, port);
  1975. break;
  1976. case AD_TYPE_MARKER:
  1977. // No need to convert fields to Little Endian since we don't use the marker's fields.
  1978. switch (((struct marker *)lacpdu)->tlv_type) {
  1979. case AD_MARKER_INFORMATION_SUBTYPE:
  1980. dprintk("Received Marker Information on port %d\n", port->actor_port_number);
  1981. ad_marker_info_received((struct marker *)lacpdu, port);
  1982. break;
  1983. case AD_MARKER_RESPONSE_SUBTYPE:
  1984. dprintk("Received Marker Response on port %d\n", port->actor_port_number);
  1985. ad_marker_response_received((struct marker *)lacpdu, port);
  1986. break;
  1987. default:
  1988. dprintk("Received an unknown Marker subtype on slot %d\n", port->actor_port_number);
  1989. }
  1990. }
  1991. }
  1992. }
  1993. /**
  1994. * bond_3ad_adapter_speed_changed - handle a slave's speed change indication
  1995. * @slave: slave struct to work on
  1996. *
  1997. * Handle reselection of aggregator (if needed) for this port.
  1998. */
  1999. void bond_3ad_adapter_speed_changed(struct slave *slave)
  2000. {
  2001. struct port *port;
  2002. port = &(SLAVE_AD_INFO(slave).port);
  2003. // if slave is null, the whole port is not initialized
  2004. if (!port->slave) {
  2005. printk(KERN_WARNING DRV_NAME ": Warning: speed changed for uninitialized port on %s\n",
  2006. slave->dev->name);
  2007. return;
  2008. }
  2009. port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
  2010. port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
  2011. dprintk("Port %d changed speed\n", port->actor_port_number);
  2012. // there is no need to reselect a new aggregator, just signal the
  2013. // state machines to reinitialize
  2014. port->sm_vars |= AD_PORT_BEGIN;
  2015. }
  2016. /**
  2017. * bond_3ad_adapter_duplex_changed - handle a slave's duplex change indication
  2018. * @slave: slave struct to work on
  2019. *
  2020. * Handle reselection of aggregator (if needed) for this port.
  2021. */
  2022. void bond_3ad_adapter_duplex_changed(struct slave *slave)
  2023. {
  2024. struct port *port;
  2025. port=&(SLAVE_AD_INFO(slave).port);
  2026. // if slave is null, the whole port is not initialized
  2027. if (!port->slave) {
  2028. printk(KERN_WARNING DRV_NAME ": Warning: duplex changed for uninitialized port on %s\n",
  2029. slave->dev->name);
  2030. return;
  2031. }
  2032. port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
  2033. port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
  2034. dprintk("Port %d changed duplex\n", port->actor_port_number);
  2035. // there is no need to reselect a new aggregator, just signal the
  2036. // state machines to reinitialize
  2037. port->sm_vars |= AD_PORT_BEGIN;
  2038. }
  2039. /**
  2040. * bond_3ad_handle_link_change - handle a slave's link status change indication
  2041. * @slave: slave struct to work on
  2042. * @status: whether the link is now up or down
  2043. *
  2044. * Handle reselection of aggregator (if needed) for this port.
  2045. */
  2046. void bond_3ad_handle_link_change(struct slave *slave, char link)
  2047. {
  2048. struct port *port;
  2049. port = &(SLAVE_AD_INFO(slave).port);
  2050. // if slave is null, the whole port is not initialized
  2051. if (!port->slave) {
  2052. printk(KERN_WARNING DRV_NAME ": Warning: link status changed for uninitialized port on %s\n",
  2053. slave->dev->name);
  2054. return;
  2055. }
  2056. // on link down we are zeroing duplex and speed since some of the adaptors(ce1000.lan) report full duplex/speed instead of N/A(duplex) / 0(speed)
  2057. // on link up we are forcing recheck on the duplex and speed since some of he adaptors(ce1000.lan) report
  2058. if (link == BOND_LINK_UP) {
  2059. port->is_enabled = 1;
  2060. port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
  2061. port->actor_oper_port_key=port->actor_admin_port_key |= __get_duplex(port);
  2062. port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS;
  2063. port->actor_oper_port_key=port->actor_admin_port_key |= (__get_link_speed(port) << 1);
  2064. } else {
  2065. /* link has failed */
  2066. port->is_enabled = 0;
  2067. port->actor_admin_port_key &= ~AD_DUPLEX_KEY_BITS;
  2068. port->actor_oper_port_key= (port->actor_admin_port_key &= ~AD_SPEED_KEY_BITS);
  2069. }
  2070. //BOND_PRINT_DBG(("Port %d changed link status to %s", port->actor_port_number, ((link == BOND_LINK_UP)?"UP":"DOWN")));
  2071. // there is no need to reselect a new aggregator, just signal the
  2072. // state machines to reinitialize
  2073. port->sm_vars |= AD_PORT_BEGIN;
  2074. }
  2075. /**
  2076. * bond_3ad_get_active_agg_info - get information of the active aggregator
  2077. * @bond: bonding struct to work on
  2078. * @ad_info: ad_info struct to fill with the bond's info
  2079. *
  2080. * Returns: 0 on success
  2081. * < 0 on error
  2082. */
  2083. int bond_3ad_get_active_agg_info(struct bonding *bond, struct ad_info *ad_info)
  2084. {
  2085. struct aggregator *aggregator = NULL;
  2086. struct port *port;
  2087. for (port = __get_first_port(bond); port; port = __get_next_port(port)) {
  2088. if (port->aggregator && port->aggregator->is_active) {
  2089. aggregator = port->aggregator;
  2090. break;
  2091. }
  2092. }
  2093. if (aggregator) {
  2094. ad_info->aggregator_id = aggregator->aggregator_identifier;
  2095. ad_info->ports = aggregator->num_of_ports;
  2096. ad_info->actor_key = aggregator->actor_oper_aggregator_key;
  2097. ad_info->partner_key = aggregator->partner_oper_aggregator_key;
  2098. memcpy(ad_info->partner_system, aggregator->partner_system.mac_addr_value, ETH_ALEN);
  2099. return 0;
  2100. }
  2101. return -1;
  2102. }
  2103. int bond_3ad_xmit_xor(struct sk_buff *skb, struct net_device *dev)
  2104. {
  2105. struct slave *slave, *start_at;
  2106. struct bonding *bond = dev->priv;
  2107. int slave_agg_no;
  2108. int slaves_in_agg;
  2109. int agg_id;
  2110. int i;
  2111. struct ad_info ad_info;
  2112. int res = 1;
  2113. /* make sure that the slaves list will
  2114. * not change during tx
  2115. */
  2116. read_lock(&bond->lock);
  2117. if (!BOND_IS_OK(bond)) {
  2118. goto out;
  2119. }
  2120. if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
  2121. printk(KERN_DEBUG "ERROR: bond_3ad_get_active_agg_info failed\n");
  2122. goto out;
  2123. }
  2124. slaves_in_agg = ad_info.ports;
  2125. agg_id = ad_info.aggregator_id;
  2126. if (slaves_in_agg == 0) {
  2127. /*the aggregator is empty*/
  2128. printk(KERN_DEBUG "ERROR: active aggregator is empty\n");
  2129. goto out;
  2130. }
  2131. slave_agg_no = bond->xmit_hash_policy(skb, dev, slaves_in_agg);
  2132. bond_for_each_slave(bond, slave, i) {
  2133. struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
  2134. if (agg && (agg->aggregator_identifier == agg_id)) {
  2135. slave_agg_no--;
  2136. if (slave_agg_no < 0) {
  2137. break;
  2138. }
  2139. }
  2140. }
  2141. if (slave_agg_no >= 0) {
  2142. printk(KERN_ERR DRV_NAME ": Error: Couldn't find a slave to tx on for aggregator ID %d\n", agg_id);
  2143. goto out;
  2144. }
  2145. start_at = slave;
  2146. bond_for_each_slave_from(bond, slave, i, start_at) {
  2147. int slave_agg_id = 0;
  2148. struct aggregator *agg = SLAVE_AD_INFO(slave).port.aggregator;
  2149. if (agg) {
  2150. slave_agg_id = agg->aggregator_identifier;
  2151. }
  2152. if (SLAVE_IS_OK(slave) && agg && (slave_agg_id == agg_id)) {
  2153. res = bond_dev_queue_xmit(bond, skb, slave->dev);
  2154. break;
  2155. }
  2156. }
  2157. out:
  2158. if (res) {
  2159. /* no suitable interface, frame not sent */
  2160. dev_kfree_skb(skb);
  2161. }
  2162. read_unlock(&bond->lock);
  2163. return 0;
  2164. }
  2165. int bond_3ad_lacpdu_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type* ptype, struct net_device *orig_dev)
  2166. {
  2167. struct bonding *bond = dev->priv;
  2168. struct slave *slave = NULL;
  2169. int ret = NET_RX_DROP;
  2170. if (!(dev->flags & IFF_MASTER))
  2171. goto out;
  2172. read_lock(&bond->lock);
  2173. slave = bond_get_slave_by_dev((struct bonding *)dev->priv, orig_dev);
  2174. if (!slave)
  2175. goto out_unlock;
  2176. bond_3ad_rx_indication((struct lacpdu *) skb->data, slave, skb->len);
  2177. ret = NET_RX_SUCCESS;
  2178. out_unlock:
  2179. read_unlock(&bond->lock);
  2180. out:
  2181. dev_kfree_skb(skb);
  2182. return ret;
  2183. }