trunklacp.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  1. /* $OpenBSD: trunklacp.c,v 1.24 2015/06/30 13:54:42 mpi Exp $ */
  2. /* $NetBSD: ieee8023ad_lacp.c,v 1.3 2005/12/11 12:24:54 christos Exp $ */
  3. /* $FreeBSD:ieee8023ad_lacp.c,v 1.15 2008/03/16 19:25:30 thompsa Exp $ */
  4. /*
  5. * Copyright (c)2005 YAMAMOTO Takashi,
  6. * Copyright (c)2008 Andrew Thompson <thompsa@FreeBSD.org>
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/param.h>
  31. #include <sys/mbuf.h>
  32. #include <sys/systm.h>
  33. #include <sys/malloc.h>
  34. #include <sys/kernel.h>
  35. #include <sys/socket.h>
  36. #include <sys/sockio.h>
  37. #include <sys/lock.h>
  38. #include <sys/rwlock.h>
  39. #include <sys/queue.h>
  40. #include <sys/timeout.h>
  41. #include <crypto/siphash.h>
  42. #include <net/if.h>
  43. #include <net/ethertypes.h>
  44. #include <net/if_media.h>
  45. #include <net/if_types.h>
  46. #include <netinet/in.h>
  47. #include <netinet/if_ether.h>
  48. #include "if_trunk.h"
  49. #include "trunklacp.h"
  50. #include "bpfilter.h"
  51. #if NBPFILTER > 0
  52. #include <net/bpf.h>
  53. #endif
  54. /*
  55. * actor system priority and port priority.
  56. * XXX should be configurable.
  57. */
  58. #define LACP_SYSTEM_PRIO 0x8000
  59. #define LACP_PORT_PRIO 0x8000
  60. #define LACP_IFQ_PRIO 6
  61. const u_int8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
  62. { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
  63. const struct tlv_template lacp_info_tlv_template[] = {
  64. { LACP_TYPE_ACTORINFO,
  65. sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
  66. { LACP_TYPE_PARTNERINFO,
  67. sizeof(struct tlvhdr) + sizeof(struct lacp_peerinfo) },
  68. { LACP_TYPE_COLLECTORINFO,
  69. sizeof(struct tlvhdr) + sizeof(struct lacp_collectorinfo) },
  70. { 0, 0 },
  71. };
  72. const struct tlv_template marker_info_tlv_template[] = {
  73. { MARKER_TYPE_INFO,
  74. sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
  75. { 0, 0 },
  76. };
  77. const struct tlv_template marker_response_tlv_template[] = {
  78. { MARKER_TYPE_RESPONSE,
  79. sizeof(struct tlvhdr) + sizeof(struct lacp_markerinfo) },
  80. { 0, 0 },
  81. };
  82. typedef void (*lacp_timer_func_t)(struct lacp_port *);
  83. void lacp_fill_actorinfo(struct lacp_port *, struct lacp_peerinfo *);
  84. void lacp_fill_markerinfo(struct lacp_port *,
  85. struct lacp_markerinfo *);
  86. u_int64_t lacp_aggregator_bandwidth(struct lacp_aggregator *);
  87. void lacp_suppress_distributing(struct lacp_softc *,
  88. struct lacp_aggregator *);
  89. void lacp_transit_expire(void *);
  90. void lacp_update_portmap(struct lacp_softc *);
  91. void lacp_select_active_aggregator(struct lacp_softc *);
  92. u_int16_t lacp_compose_key(struct lacp_port *);
  93. int tlv_check(const void *, size_t, const struct tlvhdr *,
  94. const struct tlv_template *, int);
  95. void lacp_tick(void *);
  96. void lacp_fill_aggregator_id(struct lacp_aggregator *,
  97. const struct lacp_port *);
  98. void lacp_fill_aggregator_id_peer(struct lacp_peerinfo *,
  99. const struct lacp_peerinfo *);
  100. int lacp_aggregator_is_compatible(const struct lacp_aggregator *,
  101. const struct lacp_port *);
  102. int lacp_peerinfo_is_compatible(const struct lacp_peerinfo *,
  103. const struct lacp_peerinfo *);
  104. struct lacp_aggregator *lacp_aggregator_get(struct lacp_softc *,
  105. struct lacp_port *);
  106. void lacp_aggregator_addref(struct lacp_softc *,
  107. struct lacp_aggregator *);
  108. void lacp_aggregator_delref(struct lacp_softc *,
  109. struct lacp_aggregator *);
  110. /* receive machine */
  111. int lacp_pdu_input(struct lacp_port *, struct mbuf *);
  112. int lacp_marker_input(struct lacp_port *, struct mbuf *);
  113. void lacp_sm_rx(struct lacp_port *, const struct lacpdu *);
  114. void lacp_sm_rx_timer(struct lacp_port *);
  115. void lacp_sm_rx_set_expired(struct lacp_port *);
  116. void lacp_sm_rx_update_ntt(struct lacp_port *,
  117. const struct lacpdu *);
  118. void lacp_sm_rx_record_pdu(struct lacp_port *,
  119. const struct lacpdu *);
  120. void lacp_sm_rx_update_selected(struct lacp_port *,
  121. const struct lacpdu *);
  122. void lacp_sm_rx_record_default(struct lacp_port *);
  123. void lacp_sm_rx_update_default_selected(struct lacp_port *);
  124. void lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *,
  125. const struct lacp_peerinfo *);
  126. /* mux machine */
  127. void lacp_sm_mux(struct lacp_port *);
  128. void lacp_set_mux(struct lacp_port *, enum lacp_mux_state);
  129. void lacp_sm_mux_timer(struct lacp_port *);
  130. /* periodic transmit machine */
  131. void lacp_sm_ptx_update_timeout(struct lacp_port *, u_int8_t);
  132. void lacp_sm_ptx_tx_schedule(struct lacp_port *);
  133. void lacp_sm_ptx_timer(struct lacp_port *);
  134. /* transmit machine */
  135. void lacp_sm_tx(struct lacp_port *);
  136. void lacp_sm_assert_ntt(struct lacp_port *);
  137. void lacp_run_timers(struct lacp_port *);
  138. int lacp_compare_peerinfo(const struct lacp_peerinfo *,
  139. const struct lacp_peerinfo *);
  140. int lacp_compare_systemid(const struct lacp_systemid *,
  141. const struct lacp_systemid *);
  142. void lacp_port_enable(struct lacp_port *);
  143. void lacp_port_disable(struct lacp_port *);
  144. void lacp_select(struct lacp_port *);
  145. void lacp_unselect(struct lacp_port *);
  146. void lacp_disable_collecting(struct lacp_port *);
  147. void lacp_enable_collecting(struct lacp_port *);
  148. void lacp_disable_distributing(struct lacp_port *);
  149. void lacp_enable_distributing(struct lacp_port *);
  150. int lacp_xmit_lacpdu(struct lacp_port *);
  151. int lacp_xmit_marker(struct lacp_port *);
  152. #if defined(LACP_DEBUG)
  153. void lacp_dump_lacpdu(const struct lacpdu *);
  154. const char *lacp_format_partner(const struct lacp_peerinfo *, char *,
  155. size_t);
  156. const char *lacp_format_lagid(const struct lacp_peerinfo *,
  157. const struct lacp_peerinfo *, char *, size_t);
  158. const char *lacp_format_lagid_aggregator(const struct lacp_aggregator *,
  159. char *, size_t);
  160. const char *lacp_format_state(u_int8_t, char *, size_t);
  161. const char *lacp_format_mac(const u_int8_t *, char *, size_t);
  162. const char *lacp_format_systemid(const struct lacp_systemid *, char *,
  163. size_t);
  164. const char *lacp_format_portid(const struct lacp_portid *, char *,
  165. size_t);
  166. void lacp_dprintf(const struct lacp_port *, const char *, ...)
  167. __attribute__((__format__(__printf__, 2, 3)));
  168. #define LACP_DPRINTF(a) lacp_dprintf a
  169. #else
  170. #define LACP_DPRINTF(a) /* nothing */
  171. #endif
  172. /*
  173. * partner administration variables.
  174. * XXX should be configurable.
  175. */
  176. const struct lacp_peerinfo lacp_partner_admin = {
  177. { 0xffff }, /* lip_systemid.lsi_prio */
  178. 0, /* lip_key */
  179. { 0xffff }, /* lip_portid.lpi_prio */
  180. #if 1
  181. /* optimistic lip_state */
  182. LACP_STATE_SYNC | LACP_STATE_AGGREGATION |
  183. LACP_STATE_COLLECTING | LACP_STATE_DISTRIBUTING
  184. #else
  185. /* pessimistic lip_state */
  186. 0
  187. #endif
  188. };
  189. const lacp_timer_func_t lacp_timer_funcs[LACP_NTIMER] = {
  190. [LACP_TIMER_CURRENT_WHILE] = lacp_sm_rx_timer,
  191. [LACP_TIMER_PERIODIC] = lacp_sm_ptx_timer,
  192. [LACP_TIMER_WAIT_WHILE] = lacp_sm_mux_timer,
  193. };
  194. int
  195. lacp_input(struct trunk_port *tp, struct mbuf *m)
  196. {
  197. struct lacp_port *lp = LACP_PORT(tp);
  198. struct lacp_softc *lsc = lp->lp_lsc;
  199. struct lacp_aggregator *la = lp->lp_aggregator;
  200. struct ether_header *eh;
  201. u_int8_t subtype;
  202. eh = mtod(m, struct ether_header *);
  203. if (ntohs(eh->ether_type) == ETHERTYPE_SLOW) {
  204. #if NBPFILTER > 0
  205. if (tp->tp_if->if_bpf)
  206. bpf_mtap_ether(tp->tp_if->if_bpf, m, BPF_DIRECTION_IN);
  207. #endif
  208. if (m->m_pkthdr.len < (sizeof(*eh) + sizeof(subtype)))
  209. return (-1);
  210. m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
  211. switch (subtype) {
  212. case SLOWPROTOCOLS_SUBTYPE_LACP:
  213. lacp_pdu_input(lp, m);
  214. return (1);
  215. case SLOWPROTOCOLS_SUBTYPE_MARKER:
  216. lacp_marker_input(lp, m);
  217. return (1);
  218. }
  219. }
  220. /*
  221. * If the port is not collecting or not in the active aggregator then
  222. * free and return.
  223. */
  224. /* This port is joined to the active aggregator */
  225. if ((lp->lp_state & LACP_STATE_COLLECTING) == 0 ||
  226. la == NULL || la != lsc->lsc_active_aggregator)
  227. return (-1);
  228. /* Not a subtype we are interested in */
  229. return (0);
  230. }
  231. /*
  232. * lacp_pdu_input: process lacpdu
  233. */
  234. int
  235. lacp_pdu_input(struct lacp_port *lp, struct mbuf *m)
  236. {
  237. struct lacpdu *du;
  238. int error = 0;
  239. if (m->m_pkthdr.len != sizeof(*du))
  240. goto bad;
  241. if (m->m_len < sizeof(*du)) {
  242. m = m_pullup(m, sizeof(*du));
  243. if (m == NULL) {
  244. return (ENOMEM);
  245. }
  246. }
  247. du = mtod(m, struct lacpdu *);
  248. if (memcmp(&du->ldu_eh.ether_dhost,
  249. &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN))
  250. goto bad;
  251. /*
  252. * ignore the version for compatibility with
  253. * the future protocol revisions.
  254. */
  255. #if 0
  256. if (du->ldu_sph.sph_version != 1)
  257. goto bad;
  258. #endif
  259. /*
  260. * ignore tlv types for compatibility with the
  261. * future protocol revisions. (IEEE 802.3-2005 43.4.12)
  262. */
  263. if (tlv_check(du, sizeof(*du), &du->ldu_tlv_actor,
  264. lacp_info_tlv_template, 0))
  265. goto bad;
  266. #if defined(LACP_DEBUG)
  267. LACP_DPRINTF((lp, "lacpdu receive\n"));
  268. lacp_dump_lacpdu(du);
  269. #endif /* defined(LACP_DEBUG) */
  270. lacp_sm_rx(lp, du);
  271. return (error);
  272. bad:
  273. return (EINVAL);
  274. }
  275. void
  276. lacp_fill_actorinfo(struct lacp_port *lp, struct lacp_peerinfo *info)
  277. {
  278. struct trunk_port *tp = lp->lp_trunk;
  279. struct trunk_softc *sc = tp->tp_trunk;
  280. info->lip_systemid.lsi_prio = htons(LACP_SYSTEM_PRIO);
  281. memcpy(&info->lip_systemid.lsi_mac,
  282. sc->tr_ac.ac_enaddr, ETHER_ADDR_LEN);
  283. info->lip_portid.lpi_prio = htons(LACP_PORT_PRIO);
  284. info->lip_portid.lpi_portno = htons(lp->lp_ifp->if_index);
  285. info->lip_state = lp->lp_state;
  286. }
  287. void
  288. lacp_fill_markerinfo(struct lacp_port *lp, struct lacp_markerinfo *info)
  289. {
  290. struct ifnet *ifp = lp->lp_ifp;
  291. /* Fill in the port index and system id (encoded as the MAC) */
  292. info->mi_rq_port = htons(ifp->if_index);
  293. memcpy(&info->mi_rq_system, lp->lp_systemid.lsi_mac, ETHER_ADDR_LEN);
  294. info->mi_rq_xid = htonl(0);
  295. }
  296. int
  297. lacp_xmit_lacpdu(struct lacp_port *lp)
  298. {
  299. struct trunk_port *tp = lp->lp_trunk;
  300. struct mbuf *m;
  301. struct lacpdu *du;
  302. int error;
  303. m = m_gethdr(M_DONTWAIT, MT_DATA);
  304. if (m == NULL)
  305. return (ENOMEM);
  306. m->m_len = m->m_pkthdr.len = sizeof(*du);
  307. m->m_pkthdr.pf.prio = LACP_IFQ_PRIO;
  308. du = mtod(m, struct lacpdu *);
  309. memset(du, 0, sizeof(*du));
  310. memcpy(&du->ldu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
  311. ETHER_ADDR_LEN);
  312. memcpy(&du->ldu_eh.ether_shost, tp->tp_lladdr, ETHER_ADDR_LEN);
  313. du->ldu_eh.ether_type = htons(ETHERTYPE_SLOW);
  314. du->ldu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_LACP;
  315. du->ldu_sph.sph_version = 1;
  316. TLV_SET(&du->ldu_tlv_actor, LACP_TYPE_ACTORINFO, sizeof(du->ldu_actor));
  317. du->ldu_actor = lp->lp_actor;
  318. TLV_SET(&du->ldu_tlv_partner, LACP_TYPE_PARTNERINFO,
  319. sizeof(du->ldu_partner));
  320. du->ldu_partner = lp->lp_partner;
  321. TLV_SET(&du->ldu_tlv_collector, LACP_TYPE_COLLECTORINFO,
  322. sizeof(du->ldu_collector));
  323. du->ldu_collector.lci_maxdelay = 0;
  324. #if defined(LACP_DEBUG)
  325. LACP_DPRINTF((lp, "lacpdu transmit\n"));
  326. lacp_dump_lacpdu(du);
  327. #endif /* defined(LACP_DEBUG) */
  328. m->m_flags |= M_MCAST;
  329. /*
  330. * XXX should use higher priority queue.
  331. * otherwise network congestion can break aggregation.
  332. */
  333. error = if_enqueue(lp->lp_ifp, m);
  334. return (error);
  335. }
  336. int
  337. lacp_xmit_marker(struct lacp_port *lp)
  338. {
  339. struct trunk_port *tp = lp->lp_trunk;
  340. struct mbuf *m;
  341. struct markerdu *mdu;
  342. int error;
  343. m = m_gethdr(M_DONTWAIT, MT_DATA);
  344. if (m == NULL)
  345. return (ENOMEM);
  346. m->m_len = m->m_pkthdr.len = sizeof(*mdu);
  347. m->m_pkthdr.pf.prio = LACP_IFQ_PRIO;
  348. mdu = mtod(m, struct markerdu *);
  349. memset(mdu, 0, sizeof(*mdu));
  350. memcpy(&mdu->mdu_eh.ether_dhost, ethermulticastaddr_slowprotocols,
  351. ETHER_ADDR_LEN);
  352. memcpy(&mdu->mdu_eh.ether_shost, tp->tp_lladdr, ETHER_ADDR_LEN);
  353. mdu->mdu_eh.ether_type = htons(ETHERTYPE_SLOW);
  354. mdu->mdu_sph.sph_subtype = SLOWPROTOCOLS_SUBTYPE_MARKER;
  355. mdu->mdu_sph.sph_version = 1;
  356. /* Bump the transaction id and copy over the marker info */
  357. lp->lp_marker.mi_rq_xid = htonl(ntohl(lp->lp_marker.mi_rq_xid) + 1);
  358. TLV_SET(&mdu->mdu_tlv, MARKER_TYPE_INFO, sizeof(mdu->mdu_info));
  359. mdu->mdu_info = lp->lp_marker;
  360. LACP_DPRINTF((lp, "marker transmit, port=%u, sys=%s, id=%u\n",
  361. ntohs(mdu->mdu_info.mi_rq_port),
  362. ether_sprintf(mdu->mdu_info.mi_rq_system),
  363. ntohl(mdu->mdu_info.mi_rq_xid)));
  364. m->m_flags |= M_MCAST;
  365. error = if_enqueue(lp->lp_ifp, m);
  366. return (error);
  367. }
  368. void
  369. lacp_linkstate(struct trunk_port *tp)
  370. {
  371. struct lacp_port *lp = LACP_PORT(tp);
  372. u_int8_t old_state;
  373. u_int16_t old_key;
  374. old_state = lp->lp_state;
  375. old_key = lp->lp_key;
  376. /*
  377. * If the port is not an active full duplex Ethernet link then it can
  378. * not be aggregated.
  379. */
  380. if (tp->tp_link_state == LINK_STATE_UNKNOWN ||
  381. tp->tp_link_state == LINK_STATE_FULL_DUPLEX)
  382. lacp_port_enable(lp);
  383. else
  384. lacp_port_disable(lp);
  385. lp->lp_key = lacp_compose_key(lp);
  386. if (old_state != lp->lp_state || old_key != lp->lp_key) {
  387. LACP_DPRINTF((lp, "-> UNSELECTED\n"));
  388. lp->lp_selected = LACP_UNSELECTED;
  389. }
  390. }
  391. void
  392. lacp_tick(void *arg)
  393. {
  394. struct lacp_softc *lsc = arg;
  395. struct lacp_port *lp;
  396. LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
  397. if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0)
  398. continue;
  399. lacp_run_timers(lp);
  400. lacp_select(lp);
  401. lacp_sm_mux(lp);
  402. lacp_sm_tx(lp);
  403. lacp_sm_ptx_tx_schedule(lp);
  404. }
  405. timeout_add_sec(&lsc->lsc_callout, 1);
  406. }
  407. int
  408. lacp_port_create(struct trunk_port *tp)
  409. {
  410. struct trunk_softc *sc = tp->tp_trunk;
  411. struct lacp_softc *lsc = LACP_SOFTC(sc);
  412. struct lacp_port *lp;
  413. struct ifnet *ifp = tp->tp_if;
  414. struct ifreq ifr;
  415. int error;
  416. int active = 1; /* XXX should be configurable */
  417. int fast = 0; /* XXX should be configurable */
  418. bzero(&ifr, sizeof(ifr));
  419. ifr.ifr_addr.sa_family = AF_UNSPEC;
  420. ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
  421. bcopy(&ethermulticastaddr_slowprotocols,
  422. ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
  423. error = ether_addmulti(&ifr, (struct arpcom *)ifp);
  424. if (error && error != ENETRESET) {
  425. printf("%s: ADDMULTI failed on %s\n", __func__, tp->tp_ifname);
  426. return (error);
  427. }
  428. lp = malloc(sizeof(struct lacp_port),
  429. M_DEVBUF, M_NOWAIT|M_ZERO);
  430. if (lp == NULL)
  431. return (ENOMEM);
  432. tp->tp_psc = (caddr_t)lp;
  433. lp->lp_ifp = ifp;
  434. lp->lp_trunk = tp;
  435. lp->lp_lsc = lsc;
  436. LIST_INSERT_HEAD(&lsc->lsc_ports, lp, lp_next);
  437. lacp_fill_actorinfo(lp, &lp->lp_actor);
  438. lacp_fill_markerinfo(lp, &lp->lp_marker);
  439. lp->lp_state =
  440. (active ? LACP_STATE_ACTIVITY : 0) |
  441. (fast ? LACP_STATE_TIMEOUT : 0);
  442. lp->lp_aggregator = NULL;
  443. lacp_sm_rx_set_expired(lp);
  444. lacp_linkstate(tp);
  445. return (0);
  446. }
  447. void
  448. lacp_port_destroy(struct trunk_port *tp)
  449. {
  450. struct lacp_port *lp = LACP_PORT(tp);
  451. int i;
  452. for (i = 0; i < LACP_NTIMER; i++)
  453. LACP_TIMER_DISARM(lp, i);
  454. lacp_disable_collecting(lp);
  455. lacp_disable_distributing(lp);
  456. lacp_unselect(lp);
  457. LIST_REMOVE(lp, lp_next);
  458. free(lp, M_DEVBUF, sizeof(*lp));
  459. }
  460. void
  461. lacp_req(struct trunk_softc *sc, caddr_t data)
  462. {
  463. struct lacp_opreq *req = (struct lacp_opreq *)data;
  464. struct lacp_softc *lsc = LACP_SOFTC(sc);
  465. struct lacp_aggregator *la = lsc->lsc_active_aggregator;
  466. bzero(req, sizeof(struct lacp_opreq));
  467. if (la != NULL) {
  468. req->actor_prio = ntohs(la->la_actor.lip_systemid.lsi_prio);
  469. memcpy(&req->actor_mac, &la->la_actor.lip_systemid.lsi_mac,
  470. ETHER_ADDR_LEN);
  471. req->actor_key = ntohs(la->la_actor.lip_key);
  472. req->actor_portprio = ntohs(la->la_actor.lip_portid.lpi_prio);
  473. req->actor_portno = ntohs(la->la_actor.lip_portid.lpi_portno);
  474. req->actor_state = la->la_actor.lip_state;
  475. req->partner_prio = ntohs(la->la_partner.lip_systemid.lsi_prio);
  476. memcpy(&req->partner_mac, &la->la_partner.lip_systemid.lsi_mac,
  477. ETHER_ADDR_LEN);
  478. req->partner_key = ntohs(la->la_partner.lip_key);
  479. req->partner_portprio =
  480. ntohs(la->la_partner.lip_portid.lpi_prio);
  481. req->partner_portno =
  482. ntohs(la->la_partner.lip_portid.lpi_portno);
  483. req->partner_state = la->la_partner.lip_state;
  484. }
  485. }
  486. u_int
  487. lacp_port_status(struct trunk_port *lgp)
  488. {
  489. struct lacp_port *lp = LACP_PORT(lgp);
  490. struct lacp_softc *lsc = lp->lp_lsc;
  491. struct lacp_aggregator *la = lp->lp_aggregator;
  492. u_int flags = 0;
  493. /* This port is joined to the active aggregator */
  494. if (la != NULL && la == lsc->lsc_active_aggregator)
  495. flags |= TRUNK_PORT_ACTIVE;
  496. if (lp->lp_state & LACP_STATE_COLLECTING)
  497. flags |= TRUNK_PORT_COLLECTING;
  498. if (lp->lp_state & LACP_STATE_DISTRIBUTING)
  499. flags |= TRUNK_PORT_DISTRIBUTING;
  500. return (flags);
  501. }
  502. void
  503. lacp_portreq(struct trunk_port *tp, caddr_t data)
  504. {
  505. struct lacp_opreq *req = (struct lacp_opreq *)data;
  506. struct lacp_port *lp = LACP_PORT(tp);
  507. req->actor_prio = ntohs(lp->lp_actor.lip_systemid.lsi_prio);
  508. memcpy(&req->actor_mac, &lp->lp_actor.lip_systemid.lsi_mac,
  509. ETHER_ADDR_LEN);
  510. req->actor_key = ntohs(lp->lp_actor.lip_key);
  511. req->actor_portprio = ntohs(lp->lp_actor.lip_portid.lpi_prio);
  512. req->actor_portno = ntohs(lp->lp_actor.lip_portid.lpi_portno);
  513. req->actor_state = lp->lp_actor.lip_state;
  514. req->partner_prio = ntohs(lp->lp_partner.lip_systemid.lsi_prio);
  515. memcpy(&req->partner_mac, &lp->lp_partner.lip_systemid.lsi_mac,
  516. ETHER_ADDR_LEN);
  517. req->partner_key = ntohs(lp->lp_partner.lip_key);
  518. req->partner_portprio = ntohs(lp->lp_partner.lip_portid.lpi_prio);
  519. req->partner_portno = ntohs(lp->lp_partner.lip_portid.lpi_portno);
  520. req->partner_state = lp->lp_partner.lip_state;
  521. }
  522. void
  523. lacp_disable_collecting(struct lacp_port *lp)
  524. {
  525. LACP_DPRINTF((lp, "collecting disabled\n"));
  526. lp->lp_state &= ~LACP_STATE_COLLECTING;
  527. }
  528. void
  529. lacp_enable_collecting(struct lacp_port *lp)
  530. {
  531. LACP_DPRINTF((lp, "collecting enabled\n"));
  532. lp->lp_state |= LACP_STATE_COLLECTING;
  533. }
  534. void
  535. lacp_disable_distributing(struct lacp_port *lp)
  536. {
  537. struct lacp_aggregator *la = lp->lp_aggregator;
  538. struct lacp_softc *lsc = lp->lp_lsc;
  539. #if defined(LACP_DEBUG)
  540. char buf[LACP_LAGIDSTR_MAX+1];
  541. #endif /* defined(LACP_DEBUG) */
  542. if (la == NULL || (lp->lp_state & LACP_STATE_DISTRIBUTING) == 0)
  543. return;
  544. KASSERT(!TAILQ_EMPTY(&la->la_ports));
  545. KASSERT(la->la_nports > 0);
  546. KASSERT(la->la_refcnt >= la->la_nports);
  547. LACP_DPRINTF((lp, "disable distributing on aggregator %s, "
  548. "nports %d -> %d\n",
  549. lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
  550. la->la_nports, la->la_nports - 1));
  551. TAILQ_REMOVE(&la->la_ports, lp, lp_dist_q);
  552. la->la_nports--;
  553. if (lsc->lsc_active_aggregator == la) {
  554. lacp_suppress_distributing(lsc, la);
  555. lacp_select_active_aggregator(lsc);
  556. /* regenerate the port map, the active aggregator has changed */
  557. lacp_update_portmap(lsc);
  558. }
  559. lp->lp_state &= ~LACP_STATE_DISTRIBUTING;
  560. }
  561. void
  562. lacp_enable_distributing(struct lacp_port *lp)
  563. {
  564. struct lacp_aggregator *la = lp->lp_aggregator;
  565. struct lacp_softc *lsc = lp->lp_lsc;
  566. #if defined(LACP_DEBUG)
  567. char buf[LACP_LAGIDSTR_MAX+1];
  568. #endif /* defined(LACP_DEBUG) */
  569. if ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0)
  570. return;
  571. LACP_DPRINTF((lp, "enable distributing on aggregator %s, "
  572. "nports %d -> %d\n",
  573. lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
  574. la->la_nports, la->la_nports + 1));
  575. KASSERT(la->la_refcnt > la->la_nports);
  576. TAILQ_INSERT_HEAD(&la->la_ports, lp, lp_dist_q);
  577. la->la_nports++;
  578. lp->lp_state |= LACP_STATE_DISTRIBUTING;
  579. if (lsc->lsc_active_aggregator == la) {
  580. lacp_suppress_distributing(lsc, la);
  581. lacp_update_portmap(lsc);
  582. } else
  583. /* try to become the active aggregator */
  584. lacp_select_active_aggregator(lsc);
  585. }
  586. void
  587. lacp_transit_expire(void *vp)
  588. {
  589. struct lacp_softc *lsc = vp;
  590. LACP_DPRINTF((NULL, "%s\n", __func__));
  591. lsc->lsc_suppress_distributing = 0;
  592. }
  593. int
  594. lacp_attach(struct trunk_softc *sc)
  595. {
  596. struct lacp_softc *lsc;
  597. lsc = malloc(sizeof(struct lacp_softc),
  598. M_DEVBUF, M_NOWAIT|M_ZERO);
  599. if (lsc == NULL)
  600. return (ENOMEM);
  601. sc->tr_psc = (caddr_t)lsc;
  602. lsc->lsc_softc = sc;
  603. arc4random_buf(&lsc->lsc_hashkey, sizeof(lsc->lsc_hashkey));
  604. lsc->lsc_active_aggregator = NULL;
  605. TAILQ_INIT(&lsc->lsc_aggregators);
  606. LIST_INIT(&lsc->lsc_ports);
  607. timeout_set(&lsc->lsc_transit_callout, lacp_transit_expire, lsc);
  608. timeout_set(&lsc->lsc_callout, lacp_tick, lsc);
  609. /* if the trunk is already up then do the same */
  610. if (sc->tr_ac.ac_if.if_flags & IFF_RUNNING)
  611. lacp_init(sc);
  612. return (0);
  613. }
  614. int
  615. lacp_detach(struct trunk_softc *sc)
  616. {
  617. struct lacp_softc *lsc = LACP_SOFTC(sc);
  618. KASSERT(TAILQ_EMPTY(&lsc->lsc_aggregators));
  619. KASSERT(lsc->lsc_active_aggregator == NULL);
  620. sc->tr_psc = NULL;
  621. timeout_del(&lsc->lsc_transit_callout);
  622. timeout_del(&lsc->lsc_callout);
  623. free(lsc, M_DEVBUF, sizeof(*lsc));
  624. return (0);
  625. }
  626. void
  627. lacp_init(struct trunk_softc *sc)
  628. {
  629. struct lacp_softc *lsc = LACP_SOFTC(sc);
  630. timeout_add_sec(&lsc->lsc_callout, 1);
  631. }
  632. void
  633. lacp_stop(struct trunk_softc *sc)
  634. {
  635. struct lacp_softc *lsc = LACP_SOFTC(sc);
  636. timeout_del(&lsc->lsc_transit_callout);
  637. timeout_del(&lsc->lsc_callout);
  638. }
  639. struct trunk_port *
  640. lacp_select_tx_port(struct trunk_softc *sc, struct mbuf *m)
  641. {
  642. struct lacp_softc *lsc = LACP_SOFTC(sc);
  643. struct lacp_portmap *pm;
  644. struct lacp_port *lp;
  645. u_int32_t hash;
  646. if (__predict_false(lsc->lsc_suppress_distributing)) {
  647. LACP_DPRINTF((NULL, "%s: waiting transit\n", __func__));
  648. return (NULL);
  649. }
  650. pm = &lsc->lsc_pmap[lsc->lsc_activemap];
  651. if (pm->pm_count == 0) {
  652. LACP_DPRINTF((NULL, "%s: no active aggregator\n", __func__));
  653. return (NULL);
  654. }
  655. hash = trunk_hashmbuf(m, &lsc->lsc_hashkey);
  656. hash %= pm->pm_count;
  657. lp = pm->pm_map[hash];
  658. KASSERT((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0);
  659. return (lp->lp_trunk);
  660. }
  661. /*
  662. * lacp_suppress_distributing: drop transmit packets for a while
  663. * to preserve packet ordering.
  664. */
  665. void
  666. lacp_suppress_distributing(struct lacp_softc *lsc, struct lacp_aggregator *la)
  667. {
  668. struct lacp_port *lp;
  669. if (lsc->lsc_active_aggregator != la)
  670. return;
  671. LACP_DPRINTF((NULL, "%s\n", __func__));
  672. lsc->lsc_suppress_distributing = 1;
  673. /* send a marker frame down each port to verify the queues are empty */
  674. LIST_FOREACH(lp, &lsc->lsc_ports, lp_next) {
  675. lp->lp_flags |= LACP_PORT_MARK;
  676. lacp_xmit_marker(lp);
  677. }
  678. /* set a timeout for the marker frames */
  679. timeout_add_msec(&lsc->lsc_transit_callout, LACP_TRANSIT_DELAY);
  680. }
  681. int
  682. lacp_compare_peerinfo(const struct lacp_peerinfo *a,
  683. const struct lacp_peerinfo *b)
  684. {
  685. return (memcmp(a, b, offsetof(struct lacp_peerinfo, lip_state)));
  686. }
  687. int
  688. lacp_compare_systemid(const struct lacp_systemid *a,
  689. const struct lacp_systemid *b)
  690. {
  691. return (memcmp(a, b, sizeof(*a)));
  692. }
  693. #if 0 /* unused */
  694. int
  695. lacp_compare_portid(const struct lacp_portid *a,
  696. const struct lacp_portid *b)
  697. {
  698. return (memcmp(a, b, sizeof(*a)));
  699. }
  700. #endif
  701. u_int64_t
  702. lacp_aggregator_bandwidth(struct lacp_aggregator *la)
  703. {
  704. struct lacp_port *lp;
  705. u_int64_t speed;
  706. lp = TAILQ_FIRST(&la->la_ports);
  707. if (lp == NULL)
  708. return (0);
  709. speed = lp->lp_ifp->if_baudrate;
  710. speed *= la->la_nports;
  711. if (speed == 0) {
  712. LACP_DPRINTF((lp, "speed 0? media=0x%x nports=%d\n",
  713. lp->lp_media, la->la_nports));
  714. }
  715. return (speed);
  716. }
  717. /*
  718. * lacp_select_active_aggregator: select an aggregator to be used to transmit
  719. * packets from trunk(4) interface.
  720. */
  721. void
  722. lacp_select_active_aggregator(struct lacp_softc *lsc)
  723. {
  724. struct lacp_aggregator *la;
  725. struct lacp_aggregator *best_la = NULL;
  726. u_int64_t best_speed = 0;
  727. #if defined(LACP_DEBUG)
  728. char buf[LACP_LAGIDSTR_MAX+1];
  729. #endif /* defined(LACP_DEBUG) */
  730. LACP_DPRINTF((NULL, "%s:\n", __func__));
  731. TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
  732. u_int64_t speed;
  733. if (la->la_nports == 0)
  734. continue;
  735. speed = lacp_aggregator_bandwidth(la);
  736. LACP_DPRINTF((NULL, "%s, speed=%jd, nports=%d\n",
  737. lacp_format_lagid_aggregator(la, buf, sizeof(buf)),
  738. speed, la->la_nports));
  739. /*
  740. * This aggregator is chosen if
  741. * the partner has a better system priority
  742. * or, the total aggregated speed is higher
  743. * or, it is already the chosen aggregator
  744. */
  745. if ((best_la != NULL && LACP_SYS_PRI(la->la_partner) <
  746. LACP_SYS_PRI(best_la->la_partner)) ||
  747. speed > best_speed ||
  748. (speed == best_speed &&
  749. la == lsc->lsc_active_aggregator)) {
  750. best_la = la;
  751. best_speed = speed;
  752. }
  753. }
  754. KASSERT(best_la == NULL || best_la->la_nports > 0);
  755. KASSERT(best_la == NULL || !TAILQ_EMPTY(&best_la->la_ports));
  756. #if defined(LACP_DEBUG)
  757. if (lsc->lsc_active_aggregator != best_la) {
  758. LACP_DPRINTF((NULL, "active aggregator changed\n"));
  759. LACP_DPRINTF((NULL, "old %s\n",
  760. lacp_format_lagid_aggregator(lsc->lsc_active_aggregator,
  761. buf, sizeof(buf))));
  762. } else
  763. LACP_DPRINTF((NULL, "active aggregator not changed\n"));
  764. LACP_DPRINTF((NULL, "new %s\n",
  765. lacp_format_lagid_aggregator(best_la, buf, sizeof(buf))));
  766. #endif /* defined(LACP_DEBUG) */
  767. if (lsc->lsc_active_aggregator != best_la) {
  768. lsc->lsc_active_aggregator = best_la;
  769. lacp_update_portmap(lsc);
  770. if (best_la)
  771. lacp_suppress_distributing(lsc, best_la);
  772. }
  773. }
  774. /*
  775. * Updated the inactive portmap array with the new list of ports and
  776. * make it live.
  777. */
  778. void
  779. lacp_update_portmap(struct lacp_softc *lsc)
  780. {
  781. struct lacp_aggregator *la;
  782. struct lacp_portmap *p;
  783. struct lacp_port *lp;
  784. u_int newmap;
  785. int i;
  786. newmap = lsc->lsc_activemap == 0 ? 1 : 0;
  787. p = &lsc->lsc_pmap[newmap];
  788. la = lsc->lsc_active_aggregator;
  789. bzero(p, sizeof(struct lacp_portmap));
  790. if (la != NULL && la->la_nports > 0) {
  791. p->pm_count = la->la_nports;
  792. i = 0;
  793. TAILQ_FOREACH(lp, &la->la_ports, lp_dist_q)
  794. p->pm_map[i++] = lp;
  795. KASSERT(i == p->pm_count);
  796. }
  797. /* switch the active portmap over */
  798. lsc->lsc_activemap = newmap;
  799. LACP_DPRINTF((NULL, "Set table %d with %d ports\n",
  800. lsc->lsc_activemap,
  801. lsc->lsc_pmap[lsc->lsc_activemap].pm_count));
  802. }
  803. u_int16_t
  804. lacp_compose_key(struct lacp_port *lp)
  805. {
  806. struct trunk_port *tp = lp->lp_trunk;
  807. struct trunk_softc *sc = tp->tp_trunk;
  808. u_int64_t speed;
  809. u_int16_t key;
  810. if ((lp->lp_state & LACP_STATE_AGGREGATION) == 0) {
  811. /* bit 0..14: (some bits of) if_index of this port */
  812. key = lp->lp_ifp->if_index;
  813. /* non-aggregatable */
  814. key |= 0x8000;
  815. } else {
  816. /* bit 0..2: speed indication */
  817. speed = lp->lp_ifp->if_baudrate;
  818. if (speed == 0)
  819. key = 0;
  820. else if (speed <= IF_Mbps(1))
  821. key = 1;
  822. else if (speed <= IF_Mbps(10))
  823. key = 2;
  824. else if (speed <= IF_Mbps(100))
  825. key = 3;
  826. else if (speed <= IF_Gbps(1))
  827. key = 4;
  828. else if (speed <= IF_Gbps(10))
  829. key = 5;
  830. else if (speed <= IF_Gbps(100))
  831. key = 6;
  832. else
  833. key = 7;
  834. /* bit 3..13: (some bits of) if_index of the trunk device */
  835. key |= sc->tr_ac.ac_if.if_index << 3;
  836. /* bit 14: the port active flag (includes link state) */
  837. if (TRUNK_PORTACTIVE(tp))
  838. key |= 0x4000;
  839. else
  840. key &= ~0x4000;
  841. /* clear the non-aggregatable bit */
  842. key &= ~0x8000;
  843. }
  844. return (htons(key));
  845. }
  846. void
  847. lacp_aggregator_addref(struct lacp_softc *lsc, struct lacp_aggregator *la)
  848. {
  849. #if defined(LACP_DEBUG)
  850. char buf[LACP_LAGIDSTR_MAX+1];
  851. #endif
  852. LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
  853. __func__,
  854. lacp_format_lagid(&la->la_actor, &la->la_partner,
  855. buf, sizeof(buf)),
  856. la->la_refcnt, la->la_refcnt + 1));
  857. KASSERT(la->la_refcnt > 0);
  858. la->la_refcnt++;
  859. KASSERT(la->la_refcnt > la->la_nports);
  860. }
  861. void
  862. lacp_aggregator_delref(struct lacp_softc *lsc, struct lacp_aggregator *la)
  863. {
  864. #if defined(LACP_DEBUG)
  865. char buf[LACP_LAGIDSTR_MAX+1];
  866. #endif
  867. LACP_DPRINTF((NULL, "%s: lagid=%s, refcnt %d -> %d\n",
  868. __func__,
  869. lacp_format_lagid(&la->la_actor, &la->la_partner,
  870. buf, sizeof(buf)),
  871. la->la_refcnt, la->la_refcnt - 1));
  872. KASSERT(la->la_refcnt > la->la_nports);
  873. la->la_refcnt--;
  874. if (la->la_refcnt > 0)
  875. return;
  876. KASSERT(la->la_refcnt == 0);
  877. KASSERT(lsc->lsc_active_aggregator != la);
  878. TAILQ_REMOVE(&lsc->lsc_aggregators, la, la_q);
  879. free(la, M_DEVBUF, sizeof(*la));
  880. }
  881. /*
  882. * lacp_aggregator_get: allocate an aggregator.
  883. */
  884. struct lacp_aggregator *
  885. lacp_aggregator_get(struct lacp_softc *lsc, struct lacp_port *lp)
  886. {
  887. struct lacp_aggregator *la;
  888. la = malloc(sizeof(*la), M_DEVBUF, M_NOWAIT);
  889. if (la) {
  890. la->la_refcnt = 1;
  891. la->la_nports = 0;
  892. TAILQ_INIT(&la->la_ports);
  893. la->la_pending = 0;
  894. TAILQ_INSERT_TAIL(&lsc->lsc_aggregators, la, la_q);
  895. }
  896. return (la);
  897. }
  898. /*
  899. * lacp_fill_aggregator_id: setup a newly allocated aggregator from a port.
  900. */
  901. void
  902. lacp_fill_aggregator_id(struct lacp_aggregator *la, const struct lacp_port *lp)
  903. {
  904. lacp_fill_aggregator_id_peer(&la->la_partner, &lp->lp_partner);
  905. lacp_fill_aggregator_id_peer(&la->la_actor, &lp->lp_actor);
  906. la->la_actor.lip_state = lp->lp_state & LACP_STATE_AGGREGATION;
  907. }
  908. void
  909. lacp_fill_aggregator_id_peer(struct lacp_peerinfo *lpi_aggr,
  910. const struct lacp_peerinfo *lpi_port)
  911. {
  912. memset(lpi_aggr, 0, sizeof(*lpi_aggr));
  913. lpi_aggr->lip_systemid = lpi_port->lip_systemid;
  914. lpi_aggr->lip_key = lpi_port->lip_key;
  915. }
  916. /*
  917. * lacp_aggregator_is_compatible: check if a port can join to an aggregator.
  918. */
  919. int
  920. lacp_aggregator_is_compatible(const struct lacp_aggregator *la,
  921. const struct lacp_port *lp)
  922. {
  923. if (!(lp->lp_state & LACP_STATE_AGGREGATION) ||
  924. !(lp->lp_partner.lip_state & LACP_STATE_AGGREGATION))
  925. return (0);
  926. if (!(la->la_actor.lip_state & LACP_STATE_AGGREGATION))
  927. return (0);
  928. if (!lacp_peerinfo_is_compatible(&la->la_partner, &lp->lp_partner))
  929. return (0);
  930. if (!lacp_peerinfo_is_compatible(&la->la_actor, &lp->lp_actor))
  931. return (0);
  932. return (1);
  933. }
  934. int
  935. lacp_peerinfo_is_compatible(const struct lacp_peerinfo *a,
  936. const struct lacp_peerinfo *b)
  937. {
  938. if (memcmp(&a->lip_systemid, &b->lip_systemid,
  939. sizeof(a->lip_systemid)))
  940. return (0);
  941. if (memcmp(&a->lip_key, &b->lip_key, sizeof(a->lip_key)))
  942. return (0);
  943. return (1);
  944. }
  945. void
  946. lacp_port_enable(struct lacp_port *lp)
  947. {
  948. lp->lp_state |= LACP_STATE_AGGREGATION;
  949. }
  950. void
  951. lacp_port_disable(struct lacp_port *lp)
  952. {
  953. lacp_set_mux(lp, LACP_MUX_DETACHED);
  954. lp->lp_state &= ~LACP_STATE_AGGREGATION;
  955. lp->lp_selected = LACP_UNSELECTED;
  956. lacp_sm_rx_record_default(lp);
  957. lp->lp_partner.lip_state &= ~LACP_STATE_AGGREGATION;
  958. lp->lp_state &= ~LACP_STATE_EXPIRED;
  959. }
  960. /*
  961. * lacp_select: select an aggregator. create one if necessary.
  962. */
  963. void
  964. lacp_select(struct lacp_port *lp)
  965. {
  966. struct lacp_softc *lsc = lp->lp_lsc;
  967. struct lacp_aggregator *la;
  968. #if defined(LACP_DEBUG)
  969. char buf[LACP_LAGIDSTR_MAX+1];
  970. #endif
  971. if (lp->lp_aggregator)
  972. return;
  973. KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE));
  974. LACP_DPRINTF((lp, "port lagid=%s\n",
  975. lacp_format_lagid(&lp->lp_actor, &lp->lp_partner,
  976. buf, sizeof(buf))));
  977. TAILQ_FOREACH(la, &lsc->lsc_aggregators, la_q) {
  978. if (lacp_aggregator_is_compatible(la, lp))
  979. break;
  980. }
  981. if (la == NULL) {
  982. la = lacp_aggregator_get(lsc, lp);
  983. if (la == NULL) {
  984. LACP_DPRINTF((lp, "aggregator creation failed\n"));
  985. /*
  986. * will retry on the next tick.
  987. */
  988. return;
  989. }
  990. lacp_fill_aggregator_id(la, lp);
  991. LACP_DPRINTF((lp, "aggregator created\n"));
  992. } else {
  993. LACP_DPRINTF((lp, "compatible aggregator found\n"));
  994. if (la->la_refcnt == LACP_MAX_PORTS)
  995. return;
  996. lacp_aggregator_addref(lsc, la);
  997. }
  998. LACP_DPRINTF((lp, "aggregator lagid=%s\n",
  999. lacp_format_lagid(&la->la_actor, &la->la_partner,
  1000. buf, sizeof(buf))));
  1001. lp->lp_aggregator = la;
  1002. lp->lp_selected = LACP_SELECTED;
  1003. }
  1004. /*
  1005. * lacp_unselect: finish unselect/detach process.
  1006. */
  1007. void
  1008. lacp_unselect(struct lacp_port *lp)
  1009. {
  1010. struct lacp_softc *lsc = lp->lp_lsc;
  1011. struct lacp_aggregator *la = lp->lp_aggregator;
  1012. KASSERT(!LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE));
  1013. if (la == NULL)
  1014. return;
  1015. lp->lp_aggregator = NULL;
  1016. lacp_aggregator_delref(lsc, la);
  1017. }
  1018. /* mux machine */
  1019. void
  1020. lacp_sm_mux(struct lacp_port *lp)
  1021. {
  1022. enum lacp_mux_state new_state;
  1023. int p_sync =
  1024. (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
  1025. int p_collecting =
  1026. (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
  1027. enum lacp_selected selected = lp->lp_selected;
  1028. struct lacp_aggregator *la;
  1029. /* LACP_DPRINTF((lp, "%s: state %d\n", __func__, lp->lp_mux_state)); */
  1030. re_eval:
  1031. la = lp->lp_aggregator;
  1032. KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL);
  1033. new_state = lp->lp_mux_state;
  1034. switch (lp->lp_mux_state) {
  1035. case LACP_MUX_DETACHED:
  1036. if (selected != LACP_UNSELECTED)
  1037. new_state = LACP_MUX_WAITING;
  1038. break;
  1039. case LACP_MUX_WAITING:
  1040. KASSERT(la->la_pending > 0 ||
  1041. !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE));
  1042. if (selected == LACP_SELECTED && la->la_pending == 0)
  1043. new_state = LACP_MUX_ATTACHED;
  1044. else if (selected == LACP_UNSELECTED)
  1045. new_state = LACP_MUX_DETACHED;
  1046. break;
  1047. case LACP_MUX_ATTACHED:
  1048. if (selected == LACP_SELECTED && p_sync)
  1049. new_state = LACP_MUX_COLLECTING;
  1050. else if (selected != LACP_SELECTED)
  1051. new_state = LACP_MUX_DETACHED;
  1052. break;
  1053. case LACP_MUX_COLLECTING:
  1054. if (selected == LACP_SELECTED && p_sync && p_collecting)
  1055. new_state = LACP_MUX_DISTRIBUTING;
  1056. else if (selected != LACP_SELECTED || !p_sync)
  1057. new_state = LACP_MUX_ATTACHED;
  1058. break;
  1059. case LACP_MUX_DISTRIBUTING:
  1060. if (selected != LACP_SELECTED || !p_sync || !p_collecting)
  1061. new_state = LACP_MUX_COLLECTING;
  1062. break;
  1063. default:
  1064. panic("%s: unknown state", __func__);
  1065. }
  1066. if (lp->lp_mux_state == new_state)
  1067. return;
  1068. lacp_set_mux(lp, new_state);
  1069. goto re_eval;
  1070. }
  1071. void
  1072. lacp_set_mux(struct lacp_port *lp, enum lacp_mux_state new_state)
  1073. {
  1074. struct lacp_aggregator *la = lp->lp_aggregator;
  1075. if (lp->lp_mux_state == new_state)
  1076. return;
  1077. switch (new_state) {
  1078. case LACP_MUX_DETACHED:
  1079. lp->lp_state &= ~LACP_STATE_SYNC;
  1080. lacp_disable_distributing(lp);
  1081. lacp_disable_collecting(lp);
  1082. lacp_sm_assert_ntt(lp);
  1083. /* cancel timer */
  1084. if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
  1085. KASSERT(la->la_pending > 0);
  1086. la->la_pending--;
  1087. }
  1088. LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
  1089. lacp_unselect(lp);
  1090. break;
  1091. case LACP_MUX_WAITING:
  1092. LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
  1093. LACP_AGGREGATE_WAIT_TIME);
  1094. la->la_pending++;
  1095. break;
  1096. case LACP_MUX_ATTACHED:
  1097. lp->lp_state |= LACP_STATE_SYNC;
  1098. lacp_disable_collecting(lp);
  1099. lacp_sm_assert_ntt(lp);
  1100. break;
  1101. case LACP_MUX_COLLECTING:
  1102. lacp_enable_collecting(lp);
  1103. lacp_disable_distributing(lp);
  1104. lacp_sm_assert_ntt(lp);
  1105. break;
  1106. case LACP_MUX_DISTRIBUTING:
  1107. lacp_enable_distributing(lp);
  1108. break;
  1109. default:
  1110. panic("%s: unknown state", __func__);
  1111. }
  1112. LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
  1113. lp->lp_mux_state = new_state;
  1114. }
  1115. void
  1116. lacp_sm_mux_timer(struct lacp_port *lp)
  1117. {
  1118. struct lacp_aggregator *la = lp->lp_aggregator;
  1119. #if defined(LACP_DEBUG)
  1120. char buf[LACP_LAGIDSTR_MAX+1];
  1121. #endif
  1122. KASSERT(la->la_pending > 0);
  1123. LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
  1124. lacp_format_lagid(&la->la_actor, &la->la_partner,
  1125. buf, sizeof(buf)),
  1126. la->la_pending, la->la_pending - 1));
  1127. la->la_pending--;
  1128. }
  1129. /* periodic transmit machine */
  1130. void
  1131. lacp_sm_ptx_update_timeout(struct lacp_port *lp, u_int8_t oldpstate)
  1132. {
  1133. if (LACP_STATE_EQ(oldpstate, lp->lp_partner.lip_state,
  1134. LACP_STATE_TIMEOUT))
  1135. return;
  1136. LACP_DPRINTF((lp, "partner timeout changed\n"));
  1137. /*
  1138. * FAST_PERIODIC -> SLOW_PERIODIC
  1139. * or
  1140. * SLOW_PERIODIC (-> PERIODIC_TX) -> FAST_PERIODIC
  1141. *
  1142. * let lacp_sm_ptx_tx_schedule to update timeout.
  1143. */
  1144. LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
  1145. /* if timeout has been shortened, assert NTT. */
  1146. if ((lp->lp_partner.lip_state & LACP_STATE_TIMEOUT))
  1147. lacp_sm_assert_ntt(lp);
  1148. }
  1149. void
  1150. lacp_sm_ptx_tx_schedule(struct lacp_port *lp)
  1151. {
  1152. int timeout;
  1153. if (!(lp->lp_state & LACP_STATE_ACTIVITY) &&
  1154. !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY)) {
  1155. /* NO_PERIODIC */
  1156. LACP_TIMER_DISARM(lp, LACP_TIMER_PERIODIC);
  1157. return;
  1158. }
  1159. if (LACP_TIMER_ISARMED(lp, LACP_TIMER_PERIODIC))
  1160. return;
  1161. timeout = (lp->lp_partner.lip_state & LACP_STATE_TIMEOUT) ?
  1162. LACP_FAST_PERIODIC_TIME : LACP_SLOW_PERIODIC_TIME;
  1163. LACP_TIMER_ARM(lp, LACP_TIMER_PERIODIC, timeout);
  1164. }
  1165. void
  1166. lacp_sm_ptx_timer(struct lacp_port *lp)
  1167. {
  1168. lacp_sm_assert_ntt(lp);
  1169. }
  1170. void
  1171. lacp_sm_rx(struct lacp_port *lp, const struct lacpdu *du)
  1172. {
  1173. int timeout;
  1174. /* check LACP_DISABLED first */
  1175. if (!(lp->lp_state & LACP_STATE_AGGREGATION))
  1176. return;
  1177. /* check loopback condition. */
  1178. if (!lacp_compare_systemid(&du->ldu_actor.lip_systemid,
  1179. &lp->lp_actor.lip_systemid))
  1180. return;
  1181. /*
  1182. * EXPIRED, DEFAULTED, CURRENT -> CURRENT
  1183. */
  1184. lacp_sm_rx_update_selected(lp, du);
  1185. lacp_sm_rx_update_ntt(lp, du);
  1186. lacp_sm_rx_record_pdu(lp, du);
  1187. timeout = (lp->lp_state & LACP_STATE_TIMEOUT) ?
  1188. LACP_SHORT_TIMEOUT_TIME : LACP_LONG_TIMEOUT_TIME;
  1189. LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, timeout);
  1190. lp->lp_state &= ~LACP_STATE_EXPIRED;
  1191. /* kick transmit machine without waiting the next tick. */
  1192. lacp_sm_tx(lp);
  1193. }
  1194. void
  1195. lacp_sm_rx_set_expired(struct lacp_port *lp)
  1196. {
  1197. lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
  1198. lp->lp_partner.lip_state |= LACP_STATE_TIMEOUT;
  1199. LACP_TIMER_ARM(lp, LACP_TIMER_CURRENT_WHILE, LACP_SHORT_TIMEOUT_TIME);
  1200. lp->lp_state |= LACP_STATE_EXPIRED;
  1201. }
  1202. void
  1203. lacp_sm_rx_timer(struct lacp_port *lp)
  1204. {
  1205. if ((lp->lp_state & LACP_STATE_EXPIRED) == 0) {
  1206. /* CURRENT -> EXPIRED */
  1207. LACP_DPRINTF((lp, "%s: CURRENT -> EXPIRED\n", __func__));
  1208. lacp_sm_rx_set_expired(lp);
  1209. } else {
  1210. /* EXPIRED -> DEFAULTED */
  1211. LACP_DPRINTF((lp, "%s: EXPIRED -> DEFAULTED\n", __func__));
  1212. lacp_sm_rx_update_default_selected(lp);
  1213. lacp_sm_rx_record_default(lp);
  1214. lp->lp_state &= ~LACP_STATE_EXPIRED;
  1215. }
  1216. }
  1217. void
  1218. lacp_sm_rx_record_pdu(struct lacp_port *lp, const struct lacpdu *du)
  1219. {
  1220. int active;
  1221. u_int8_t oldpstate;
  1222. #if defined(LACP_DEBUG)
  1223. char buf[LACP_STATESTR_MAX+1];
  1224. #endif
  1225. /* LACP_DPRINTF((lp, "%s\n", __func__)); */
  1226. oldpstate = lp->lp_partner.lip_state;
  1227. active = (du->ldu_actor.lip_state & LACP_STATE_ACTIVITY)
  1228. || ((lp->lp_state & LACP_STATE_ACTIVITY) &&
  1229. (du->ldu_partner.lip_state & LACP_STATE_ACTIVITY));
  1230. lp->lp_partner = du->ldu_actor;
  1231. if (active &&
  1232. ((LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
  1233. LACP_STATE_AGGREGATION) &&
  1234. !lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner))
  1235. || (du->ldu_partner.lip_state & LACP_STATE_AGGREGATION) == 0)) {
  1236. /* XXX nothing? */
  1237. } else
  1238. lp->lp_partner.lip_state &= ~LACP_STATE_SYNC;
  1239. lp->lp_state &= ~LACP_STATE_DEFAULTED;
  1240. if (oldpstate != lp->lp_partner.lip_state) {
  1241. LACP_DPRINTF((lp, "old pstate %s\n",
  1242. lacp_format_state(oldpstate, buf, sizeof(buf))));
  1243. LACP_DPRINTF((lp, "new pstate %s\n",
  1244. lacp_format_state(lp->lp_partner.lip_state, buf,
  1245. sizeof(buf))));
  1246. }
  1247. lacp_sm_ptx_update_timeout(lp, oldpstate);
  1248. }
  1249. void
  1250. lacp_sm_rx_update_ntt(struct lacp_port *lp, const struct lacpdu *du)
  1251. {
  1252. /* LACP_DPRINTF((lp, "%s\n", __func__)); */
  1253. if (lacp_compare_peerinfo(&lp->lp_actor, &du->ldu_partner) ||
  1254. !LACP_STATE_EQ(lp->lp_state, du->ldu_partner.lip_state,
  1255. LACP_STATE_ACTIVITY | LACP_STATE_SYNC | LACP_STATE_AGGREGATION)) {
  1256. LACP_DPRINTF((lp, "%s: assert ntt\n", __func__));
  1257. lacp_sm_assert_ntt(lp);
  1258. }
  1259. }
  1260. void
  1261. lacp_sm_rx_record_default(struct lacp_port *lp)
  1262. {
  1263. u_int8_t oldpstate;
  1264. /* LACP_DPRINTF((lp, "%s\n", __func__)); */
  1265. oldpstate = lp->lp_partner.lip_state;
  1266. lp->lp_partner = lacp_partner_admin;
  1267. lp->lp_state |= LACP_STATE_DEFAULTED;
  1268. lacp_sm_ptx_update_timeout(lp, oldpstate);
  1269. }
  1270. void
  1271. lacp_sm_rx_update_selected_from_peerinfo(struct lacp_port *lp,
  1272. const struct lacp_peerinfo *info)
  1273. {
  1274. /* LACP_DPRINTF((lp, "%s\n", __func__)); */
  1275. if (lacp_compare_peerinfo(&lp->lp_partner, info) ||
  1276. !LACP_STATE_EQ(lp->lp_partner.lip_state, info->lip_state,
  1277. LACP_STATE_AGGREGATION)) {
  1278. lp->lp_selected = LACP_UNSELECTED;
  1279. /* mux machine will clean up lp->lp_aggregator */
  1280. }
  1281. }
  1282. void
  1283. lacp_sm_rx_update_selected(struct lacp_port *lp, const struct lacpdu *du)
  1284. {
  1285. /* LACP_DPRINTF((lp, "%s\n", __func__)); */
  1286. lacp_sm_rx_update_selected_from_peerinfo(lp, &du->ldu_actor);
  1287. }
  1288. void
  1289. lacp_sm_rx_update_default_selected(struct lacp_port *lp)
  1290. {
  1291. /* LACP_DPRINTF((lp, "%s\n", __func__)); */
  1292. lacp_sm_rx_update_selected_from_peerinfo(lp, &lacp_partner_admin);
  1293. }
  1294. /* transmit machine */
  1295. void
  1296. lacp_sm_tx(struct lacp_port *lp)
  1297. {
  1298. int error;
  1299. if (!(lp->lp_state & LACP_STATE_AGGREGATION)
  1300. #if 1
  1301. || (!(lp->lp_state & LACP_STATE_ACTIVITY)
  1302. && !(lp->lp_partner.lip_state & LACP_STATE_ACTIVITY))
  1303. #endif
  1304. ) {
  1305. lp->lp_flags &= ~LACP_PORT_NTT;
  1306. }
  1307. if (!(lp->lp_flags & LACP_PORT_NTT))
  1308. return;
  1309. /* Rate limit to 3 PDUs per LACP_FAST_PERIODIC_TIME */
  1310. if (ppsratecheck(&lp->lp_last_lacpdu, &lp->lp_lacpdu_sent,
  1311. (3 / LACP_FAST_PERIODIC_TIME)) == 0) {
  1312. LACP_DPRINTF((lp, "rate limited pdu\n"));
  1313. return;
  1314. }
  1315. error = lacp_xmit_lacpdu(lp);
  1316. if (error == 0)
  1317. lp->lp_flags &= ~LACP_PORT_NTT;
  1318. else
  1319. LACP_DPRINTF((lp, "lacpdu transmit failure, error %d\n",
  1320. error));
  1321. }
  1322. void
  1323. lacp_sm_assert_ntt(struct lacp_port *lp)
  1324. {
  1325. lp->lp_flags |= LACP_PORT_NTT;
  1326. }
  1327. void
  1328. lacp_run_timers(struct lacp_port *lp)
  1329. {
  1330. int i;
  1331. for (i = 0; i < LACP_NTIMER; i++) {
  1332. KASSERT(lp->lp_timer[i] >= 0);
  1333. if (lp->lp_timer[i] == 0)
  1334. continue;
  1335. else if (--lp->lp_timer[i] <= 0) {
  1336. if (lacp_timer_funcs[i])
  1337. (*lacp_timer_funcs[i])(lp);
  1338. }
  1339. }
  1340. }
  1341. int
  1342. lacp_marker_input(struct lacp_port *lp, struct mbuf *m)
  1343. {
  1344. struct lacp_softc *lsc = lp->lp_lsc;
  1345. struct trunk_port *tp = lp->lp_trunk;
  1346. struct lacp_port *lp2;
  1347. struct markerdu *mdu;
  1348. int error = 0;
  1349. int pending = 0;
  1350. if (m->m_pkthdr.len != sizeof(*mdu))
  1351. goto bad;
  1352. if ((m->m_flags & M_MCAST) == 0)
  1353. goto bad;
  1354. if (m->m_len < sizeof(*mdu)) {
  1355. m = m_pullup(m, sizeof(*mdu));
  1356. if (m == NULL)
  1357. return (ENOMEM);
  1358. }
  1359. mdu = mtod(m, struct markerdu *);
  1360. if (memcmp(&mdu->mdu_eh.ether_dhost,
  1361. &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN))
  1362. goto bad;
  1363. if (mdu->mdu_sph.sph_version != 1)
  1364. goto bad;
  1365. switch (mdu->mdu_tlv.tlv_type) {
  1366. case MARKER_TYPE_INFO:
  1367. if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
  1368. marker_info_tlv_template, 1))
  1369. goto bad;
  1370. mdu->mdu_tlv.tlv_type = MARKER_TYPE_RESPONSE;
  1371. memcpy(&mdu->mdu_eh.ether_dhost,
  1372. &ethermulticastaddr_slowprotocols, ETHER_ADDR_LEN);
  1373. memcpy(&mdu->mdu_eh.ether_shost,
  1374. tp->tp_lladdr, ETHER_ADDR_LEN);
  1375. error = if_enqueue(lp->lp_ifp, m);
  1376. break;
  1377. case MARKER_TYPE_RESPONSE:
  1378. if (tlv_check(mdu, sizeof(*mdu), &mdu->mdu_tlv,
  1379. marker_response_tlv_template, 1))
  1380. goto bad;
  1381. LACP_DPRINTF((lp, "marker response, port=%u, sys=%s, id=%u\n",
  1382. ntohs(mdu->mdu_info.mi_rq_port),
  1383. ether_sprintf(mdu->mdu_info.mi_rq_system),
  1384. ntohl(mdu->mdu_info.mi_rq_xid)));
  1385. /* Verify that it is the last marker we sent out */
  1386. if (memcmp(&mdu->mdu_info, &lp->lp_marker,
  1387. sizeof(struct lacp_markerinfo)))
  1388. goto bad;
  1389. lp->lp_flags &= ~LACP_PORT_MARK;
  1390. if (lsc->lsc_suppress_distributing) {
  1391. /* Check if any ports are waiting for a response */
  1392. LIST_FOREACH(lp2, &lsc->lsc_ports, lp_next) {
  1393. if (lp2->lp_flags & LACP_PORT_MARK) {
  1394. pending = 1;
  1395. break;
  1396. }
  1397. }
  1398. if (pending == 0) {
  1399. /* All interface queues are clear */
  1400. LACP_DPRINTF((NULL, "queue flush complete\n"));
  1401. lsc->lsc_suppress_distributing = 0;
  1402. }
  1403. }
  1404. break;
  1405. default:
  1406. goto bad;
  1407. }
  1408. return (error);
  1409. bad:
  1410. LACP_DPRINTF((lp, "bad marker frame\n"));
  1411. return (EINVAL);
  1412. }
  1413. int
  1414. tlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
  1415. const struct tlv_template *tmpl, int check_type)
  1416. {
  1417. while (/* CONSTCOND */ 1) {
  1418. if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size)
  1419. return (EINVAL);
  1420. if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
  1421. tlv->tlv_length != tmpl->tmpl_length)
  1422. return (EINVAL);
  1423. if (tmpl->tmpl_type == 0)
  1424. break;
  1425. tlv = (const struct tlvhdr *)
  1426. ((const char *)tlv + tlv->tlv_length);
  1427. tmpl++;
  1428. }
  1429. return (0);
  1430. }
  1431. #if defined(LACP_DEBUG)
  1432. const char *
  1433. lacp_format_mac(const u_int8_t *mac, char *buf, size_t buflen)
  1434. {
  1435. snprintf(buf, buflen, "%02X-%02X-%02X-%02X-%02X-%02X",
  1436. (int)mac[0],
  1437. (int)mac[1],
  1438. (int)mac[2],
  1439. (int)mac[3],
  1440. (int)mac[4],
  1441. (int)mac[5]);
  1442. return (buf);
  1443. }
  1444. const char *
  1445. lacp_format_systemid(const struct lacp_systemid *sysid,
  1446. char *buf, size_t buflen)
  1447. {
  1448. char macbuf[LACP_MACSTR_MAX+1];
  1449. snprintf(buf, buflen, "%04X,%s",
  1450. ntohs(sysid->lsi_prio),
  1451. lacp_format_mac(sysid->lsi_mac, macbuf, sizeof(macbuf)));
  1452. return (buf);
  1453. }
  1454. const char *
  1455. lacp_format_portid(const struct lacp_portid *portid, char *buf, size_t buflen)
  1456. {
  1457. snprintf(buf, buflen, "%04X,%04X",
  1458. ntohs(portid->lpi_prio),
  1459. ntohs(portid->lpi_portno));
  1460. return (buf);
  1461. }
  1462. const char *
  1463. lacp_format_partner(const struct lacp_peerinfo *peer, char *buf, size_t buflen)
  1464. {
  1465. char sysid[LACP_SYSTEMIDSTR_MAX+1];
  1466. char portid[LACP_PORTIDSTR_MAX+1];
  1467. snprintf(buf, buflen, "(%s,%04X,%s)",
  1468. lacp_format_systemid(&peer->lip_systemid, sysid, sizeof(sysid)),
  1469. ntohs(peer->lip_key),
  1470. lacp_format_portid(&peer->lip_portid, portid, sizeof(portid)));
  1471. return (buf);
  1472. }
  1473. const char *
  1474. lacp_format_lagid(const struct lacp_peerinfo *a,
  1475. const struct lacp_peerinfo *b, char *buf, size_t buflen)
  1476. {
  1477. char astr[LACP_PARTNERSTR_MAX+1];
  1478. char bstr[LACP_PARTNERSTR_MAX+1];
  1479. #if 0
  1480. /*
  1481. * there's a convention to display small numbered peer
  1482. * in the left.
  1483. */
  1484. if (lacp_compare_peerinfo(a, b) > 0) {
  1485. const struct lacp_peerinfo *t;
  1486. t = a;
  1487. a = b;
  1488. b = t;
  1489. }
  1490. #endif
  1491. snprintf(buf, buflen, "[%s,%s]",
  1492. lacp_format_partner(a, astr, sizeof(astr)),
  1493. lacp_format_partner(b, bstr, sizeof(bstr)));
  1494. return (buf);
  1495. }
  1496. const char *
  1497. lacp_format_lagid_aggregator(const struct lacp_aggregator *la,
  1498. char *buf, size_t buflen)
  1499. {
  1500. if (la == NULL)
  1501. return ("(none)");
  1502. return (lacp_format_lagid(&la->la_actor, &la->la_partner, buf, buflen));
  1503. }
  1504. const char *
  1505. lacp_format_state(u_int8_t state, char *buf, size_t buflen)
  1506. {
  1507. snprintf(buf, buflen, "%b", state, LACP_STATE_BITS);
  1508. return (buf);
  1509. }
  1510. void
  1511. lacp_dump_lacpdu(const struct lacpdu *du)
  1512. {
  1513. char buf[LACP_PARTNERSTR_MAX+1];
  1514. char buf2[LACP_STATESTR_MAX+1];
  1515. printf("actor=%s\n",
  1516. lacp_format_partner(&du->ldu_actor, buf, sizeof(buf)));
  1517. printf("actor.state=%s\n",
  1518. lacp_format_state(du->ldu_actor.lip_state, buf2, sizeof(buf2)));
  1519. printf("partner=%s\n",
  1520. lacp_format_partner(&du->ldu_partner, buf, sizeof(buf)));
  1521. printf("partner.state=%s\n",
  1522. lacp_format_state(du->ldu_partner.lip_state, buf2, sizeof(buf2)));
  1523. printf("maxdelay=%d\n", ntohs(du->ldu_collector.lci_maxdelay));
  1524. }
  1525. void
  1526. lacp_dprintf(const struct lacp_port *lp, const char *fmt, ...)
  1527. {
  1528. va_list va;
  1529. if (lp)
  1530. printf("%s: ", lp->lp_ifp->if_xname);
  1531. va_start(va, fmt);
  1532. vprintf(fmt, va);
  1533. va_end(va);
  1534. }
  1535. #endif