bond_3ad.c 80 KB

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