bond_3ad.c 85 KB

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