bond_3ad.c 80 KB

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