bond_3ad.c 80 KB

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