bond_3ad.c 80 KB

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