bond_3ad.c 85 KB

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