bond_3ad.c 77 KB

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