bond_3ad.c 77 KB

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