bond_3ad.c 84 KB

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