bond_3ad.c 77 KB

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