bond_3ad.c 81 KB

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