bond_3ad.c 80 KB

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