bond_3ad.c 81 KB

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