bond_3ad.c 80 KB

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