bond_3ad.c 81 KB

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