bond_3ad.c 91 KB

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