bond_3ad.c 78 KB

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