bond_3ad.c 81 KB

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