bond_3ad.c 80 KB

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