bond_3ad.c 80 KB

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