bond_3ad.c 76 KB

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