bond_3ad.c 80 KB

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