bond_3ad.c 85 KB

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