bond_3ad.c 83 KB

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