bond_3ad.c 84 KB

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