bond_3ad.c 81 KB

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