bond_3ad.c 80 KB

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