bond_3ad.c 85 KB

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