bond_3ad.c 80 KB

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