xref: /titanic_44/usr/src/uts/common/io/aggr/aggr_lacp.c (revision 2e107de79998f3036decec2454002940afb9a6ff)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * IEEE 802.3ad Link Aggregation - LACP & Marker Protocol processing.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/callb.h>
33 #include <sys/conf.h>
34 #include <sys/cmn_err.h>
35 #include <sys/disp.h>
36 #include <sys/list.h>
37 #include <sys/ksynch.h>
38 #include <sys/kmem.h>
39 #include <sys/stream.h>
40 #include <sys/modctl.h>
41 #include <sys/ddi.h>
42 #include <sys/sunddi.h>
43 #include <sys/atomic.h>
44 #include <sys/stat.h>
45 #include <sys/byteorder.h>
46 #include <sys/strsun.h>
47 #include <sys/isa_defs.h>
48 
49 #include <sys/aggr.h>
50 #include <sys/aggr_impl.h>
51 
52 static struct ether_addr	etherzeroaddr = {
53 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00
54 };
55 
56 /*
57  * Slow_Protocol_Multicast address, as per IEEE 802.3ad spec.
58  */
59 static struct ether_addr   slow_multicast_addr = {
60 	0x01, 0x80, 0xc2, 0x00, 0x00, 0x02
61 };
62 
63 #ifdef DEBUG
64 /* LACP state machine debugging support */
65 static uint32_t aggr_lacp_debug = 0;
66 #define	AGGR_LACP_DBG(x)	if (aggr_lacp_debug) { (void) printf x; }
67 #else
68 #define	AGGR_LACP_DBG(x)	{}
69 #endif /* DEBUG */
70 
71 #define	NSECS_PER_SEC   1000000000ll
72 
73 /* used by lacp_misconfig_walker() */
74 typedef struct lacp_misconfig_check_state_s {
75 	aggr_port_t *cs_portp;
76 	boolean_t cs_found;
77 } lacp_misconfig_check_state_t;
78 
79 static const char *lacp_receive_str[] = LACP_RECEIVE_STATE_STRINGS;
80 static const char *lacp_periodic_str[] = LACP_PERIODIC_STRINGS;
81 static const char *lacp_mux_str[] = LACP_MUX_STRINGS;
82 
83 static uint16_t lacp_port_priority = 0x1000;
84 static uint16_t lacp_system_priority = 0x1000;
85 
86 /*
87  * Maintains a list of all ports in ATTACHED state. This information
88  * is used to detect misconfiguration.
89  */
90 typedef struct lacp_sel_ports {
91 	datalink_id_t sp_grp_linkid;
92 	datalink_id_t sp_linkid;
93 	/* Note: sp_partner_system must be 2-byte aligned */
94 	struct ether_addr sp_partner_system;
95 	uint32_t sp_partner_key;
96 	struct lacp_sel_ports *sp_next;
97 } lacp_sel_ports_t;
98 
99 static lacp_sel_ports_t *sel_ports = NULL;
100 static kmutex_t lacp_sel_lock;
101 
102 static void periodic_timer_pop(void *);
103 static void periodic_timer_pop_handler(aggr_port_t *);
104 static void lacp_xmit_sm(aggr_port_t *);
105 static void lacp_periodic_sm(aggr_port_t *);
106 static void fill_lacp_pdu(aggr_port_t *, lacp_t *);
107 static void fill_lacp_ether(aggr_port_t *, struct ether_header *);
108 static void lacp_on(aggr_port_t *);
109 static void lacp_off(aggr_port_t *);
110 static boolean_t valid_lacp_pdu(aggr_port_t *, lacp_t *);
111 static void lacp_receive_sm(aggr_port_t *, lacp_t *);
112 static void aggr_set_coll_dist(aggr_port_t *, boolean_t);
113 static void start_wait_while_timer(aggr_port_t *);
114 static void stop_wait_while_timer(aggr_port_t *);
115 static void lacp_reset_port(aggr_port_t *);
116 static void stop_current_while_timer(aggr_port_t *);
117 static void current_while_timer_pop(void *);
118 static void current_while_timer_pop_handler(aggr_port_t *);
119 static void update_default_selected(aggr_port_t *);
120 static boolean_t update_selected(aggr_port_t *, lacp_t *);
121 static boolean_t lacp_sel_ports_add(aggr_port_t *);
122 static void lacp_sel_ports_del(aggr_port_t *);
123 static void wait_while_timer_pop(void *);
124 static void wait_while_timer_pop_handler(aggr_port_t *);
125 
126 void
127 aggr_lacp_init(void)
128 {
129 	mutex_init(&lacp_sel_lock, NULL, MUTEX_DEFAULT, NULL);
130 }
131 
132 void
133 aggr_lacp_fini(void)
134 {
135 	mutex_destroy(&lacp_sel_lock);
136 }
137 
138 /*
139  * The following functions are used for handling LACP timers.
140  *
141  * Note that we cannot fully rely on the aggr's mac perimeter in the timeout
142  * handler routine, otherwise it may cause deadlock with the untimeout() call
143  * which is usually called with the mac perimeter held. Instead, a
144  * lacp_timer_lock mutex is introduced, which protects a bitwise flag
145  * (lacp_timer_bits). This flag is set/cleared by timeout()/stop_timer()
146  * routines and is checked by a dedicated thread, that executes the real
147  * timeout operation.
148  */
149 static void
150 aggr_port_timer_thread(void *arg)
151 {
152 	aggr_port_t		*port = arg;
153 	aggr_lacp_port_t	*pl = &port->lp_lacp;
154 	aggr_grp_t		*grp = port->lp_grp;
155 	uint32_t		lacp_timer_bits;
156 	mac_perim_handle_t	mph;
157 	callb_cpr_t		cprinfo;
158 
159 	CALLB_CPR_INIT(&cprinfo, &pl->lacp_timer_lock, callb_generic_cpr,
160 	    "aggr_port_timer_thread");
161 
162 	mutex_enter(&pl->lacp_timer_lock);
163 
164 	for (;;) {
165 
166 		if ((lacp_timer_bits = pl->lacp_timer_bits) == 0) {
167 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
168 			cv_wait(&pl->lacp_timer_cv, &pl->lacp_timer_lock);
169 			CALLB_CPR_SAFE_END(&cprinfo, &pl->lacp_timer_lock);
170 			continue;
171 		}
172 		pl->lacp_timer_bits = 0;
173 
174 		if (lacp_timer_bits & LACP_THREAD_EXIT)
175 			break;
176 
177 		if (lacp_timer_bits & LACP_PERIODIC_TIMEOUT)
178 			pl->periodic_timer.id = 0;
179 		if (lacp_timer_bits & LACP_WAIT_WHILE_TIMEOUT)
180 			pl->wait_while_timer.id = 0;
181 		if (lacp_timer_bits & LACP_CURRENT_WHILE_TIMEOUT)
182 			pl->current_while_timer.id = 0;
183 
184 		mutex_exit(&pl->lacp_timer_lock);
185 
186 		mac_perim_enter_by_mh(grp->lg_mh, &mph);
187 		if (port->lp_closing) {
188 			mac_perim_exit(mph);
189 			mutex_enter(&pl->lacp_timer_lock);
190 			break;
191 		}
192 
193 		if (lacp_timer_bits & LACP_PERIODIC_TIMEOUT)
194 			periodic_timer_pop_handler(port);
195 		if (lacp_timer_bits & LACP_WAIT_WHILE_TIMEOUT)
196 			wait_while_timer_pop_handler(port);
197 		if (lacp_timer_bits & LACP_CURRENT_WHILE_TIMEOUT)
198 			current_while_timer_pop_handler(port);
199 		mac_perim_exit(mph);
200 
201 		mutex_enter(&pl->lacp_timer_lock);
202 		if (pl->lacp_timer_bits & LACP_THREAD_EXIT)
203 			break;
204 	}
205 
206 	pl->lacp_timer_bits = 0;
207 	pl->lacp_timer_thread = NULL;
208 	cv_broadcast(&pl->lacp_timer_cv);
209 
210 	/* CALLB_CPR_EXIT drops the lock */
211 	CALLB_CPR_EXIT(&cprinfo);
212 
213 	/*
214 	 * Release the reference of the grp so aggr_grp_delete() can call
215 	 * mac_unregister() safely.
216 	 */
217 	aggr_grp_port_rele(port);
218 	thread_exit();
219 }
220 
221 /*
222  * Set the port LACP state to SELECTED. Returns B_FALSE if the operation
223  * could not be performed due to a memory allocation error, B_TRUE otherwise.
224  */
225 static boolean_t
226 lacp_port_select(aggr_port_t *portp)
227 {
228 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
229 
230 	if (!lacp_sel_ports_add(portp))
231 		return (B_FALSE);
232 	portp->lp_lacp.sm.selected = AGGR_SELECTED;
233 	return (B_TRUE);
234 }
235 
236 /*
237  * Set the port LACP state to UNSELECTED.
238  */
239 static void
240 lacp_port_unselect(aggr_port_t *portp)
241 {
242 	aggr_grp_t	*grp = portp->lp_grp;
243 
244 	ASSERT((grp->lg_mh == NULL) || MAC_PERIM_HELD(grp->lg_mh));
245 
246 	lacp_sel_ports_del(portp);
247 	portp->lp_lacp.sm.selected = AGGR_UNSELECTED;
248 }
249 
250 /*
251  * Initialize group specific LACP state and parameters.
252  */
253 void
254 aggr_lacp_init_grp(aggr_grp_t *aggrp)
255 {
256 	aggrp->aggr.PeriodicTimer = AGGR_LACP_TIMER_SHORT;
257 	aggrp->aggr.ActorSystemPriority = (uint16_t)lacp_system_priority;
258 	aggrp->aggr.CollectorMaxDelay = 10;
259 	aggrp->lg_lacp_mode = AGGR_LACP_OFF;
260 	aggrp->aggr.ready = B_FALSE;
261 }
262 
263 /*
264  * Complete LACP info initialization at port creation time.
265  */
266 void
267 aggr_lacp_init_port(aggr_port_t *portp)
268 {
269 	aggr_grp_t *aggrp = portp->lp_grp;
270 	aggr_lacp_port_t *pl = &portp->lp_lacp;
271 
272 	ASSERT(aggrp->lg_mh == NULL || MAC_PERIM_HELD(aggrp->lg_mh));
273 	ASSERT(MAC_PERIM_HELD(portp->lp_mh));
274 
275 	/* actor port # */
276 	pl->ActorPortNumber = portp->lp_portid;
277 	AGGR_LACP_DBG(("aggr_lacp_init_port(%d): "
278 	    "ActorPortNumber = 0x%x\n", portp->lp_linkid,
279 	    pl->ActorPortNumber));
280 
281 	pl->ActorPortPriority = (uint16_t)lacp_port_priority;
282 	pl->ActorPortAggrId = 0;	/* aggregator id - not used */
283 	pl->NTT = B_FALSE;			/* need to transmit */
284 
285 	pl->ActorAdminPortKey = aggrp->lg_key;
286 	pl->ActorOperPortKey = pl->ActorAdminPortKey;
287 	AGGR_LACP_DBG(("aggr_lacp_init_port(%d) "
288 	    "ActorAdminPortKey = 0x%x, ActorAdminPortKey = 0x%x\n",
289 	    portp->lp_linkid, pl->ActorAdminPortKey, pl->ActorOperPortKey));
290 
291 	/* Actor admin. port state */
292 	pl->ActorAdminPortState.bit.activity = B_FALSE;
293 	pl->ActorAdminPortState.bit.timeout = B_TRUE;
294 	pl->ActorAdminPortState.bit.aggregation = B_TRUE;
295 	pl->ActorAdminPortState.bit.sync = B_FALSE;
296 	pl->ActorAdminPortState.bit.collecting = B_FALSE;
297 	pl->ActorAdminPortState.bit.distributing = B_FALSE;
298 	pl->ActorAdminPortState.bit.defaulted = B_FALSE;
299 	pl->ActorAdminPortState.bit.expired = B_FALSE;
300 	pl->ActorOperPortState = pl->ActorAdminPortState;
301 
302 	/*
303 	 * Partner Administrative Information
304 	 * (All initialized to zero except for the following)
305 	 * Fast Timeouts.
306 	 */
307 	pl->PartnerAdminPortState.bit.timeout =
308 	    pl->PartnerOperPortState.bit.timeout = B_TRUE;
309 
310 	pl->PartnerCollectorMaxDelay = 0; /* tens of microseconds */
311 
312 	/*
313 	 * State machine information.
314 	 */
315 	pl->sm.lacp_on = B_FALSE;		/* LACP Off default */
316 	pl->sm.begin = B_TRUE;		/* Prevents transmissions */
317 	pl->sm.lacp_enabled = B_FALSE;
318 	pl->sm.port_enabled = B_FALSE;		/* Link Down */
319 	pl->sm.actor_churn = B_FALSE;
320 	pl->sm.partner_churn = B_FALSE;
321 	pl->sm.ready_n = B_FALSE;
322 	pl->sm.port_moved = B_FALSE;
323 
324 	lacp_port_unselect(portp);
325 
326 	pl->sm.periodic_state = LACP_NO_PERIODIC;
327 	pl->sm.receive_state = LACP_INITIALIZE;
328 	pl->sm.mux_state = LACP_DETACHED;
329 	pl->sm.churn_state = LACP_NO_ACTOR_CHURN;
330 
331 	/*
332 	 * Timer information.
333 	 */
334 	pl->current_while_timer.id = 0;
335 	pl->current_while_timer.val = SHORT_TIMEOUT_TIME;
336 
337 	pl->periodic_timer.id = 0;
338 	pl->periodic_timer.val = FAST_PERIODIC_TIME;
339 
340 	pl->wait_while_timer.id = 0;
341 	pl->wait_while_timer.val = AGGREGATE_WAIT_TIME;
342 
343 	pl->lacp_timer_bits = 0;
344 
345 	mutex_init(&pl->lacp_timer_lock, NULL, MUTEX_DRIVER, NULL);
346 	cv_init(&pl->lacp_timer_cv, NULL, CV_DRIVER, NULL);
347 
348 	pl->lacp_timer_thread = thread_create(NULL, 0, aggr_port_timer_thread,
349 	    portp, 0, &p0, TS_RUN, minclsyspri);
350 
351 	/*
352 	 * Hold a reference of the grp and the port and this reference will
353 	 * be release when the thread exits.
354 	 *
355 	 * The reference on the port is used for aggr_port_delete() to
356 	 * continue without waiting for the thread to exit; the reference
357 	 * on the grp is used for aggr_grp_delete() to wait for the thread
358 	 * to exit before calling mac_unregister().
359 	 */
360 	aggr_grp_port_hold(portp);
361 }
362 
363 /*
364  * Port initialization when we need to
365  * turn LACP on/off, etc. Not everything is
366  * reset like in the above routine.
367  *		Do NOT modify things like link status.
368  */
369 static void
370 lacp_reset_port(aggr_port_t *portp)
371 {
372 	aggr_lacp_port_t *pl = &portp->lp_lacp;
373 
374 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
375 
376 	pl->NTT = B_FALSE;			/* need to transmit */
377 
378 	/* reset operational port state */
379 	pl->ActorOperPortState.bit.timeout =
380 	    pl->ActorAdminPortState.bit.timeout;
381 
382 	pl->ActorOperPortState.bit.sync = B_FALSE;
383 	pl->ActorOperPortState.bit.collecting = B_FALSE;
384 	pl->ActorOperPortState.bit.distributing = B_FALSE;
385 	pl->ActorOperPortState.bit.defaulted = B_TRUE;
386 	pl->ActorOperPortState.bit.expired = B_FALSE;
387 
388 	pl->PartnerOperPortState.bit.timeout = B_TRUE;	/* fast t/o */
389 	pl->PartnerCollectorMaxDelay = 0; /* tens of microseconds */
390 
391 	/*
392 	 * State machine information.
393 	 */
394 	pl->sm.begin = B_TRUE;		/* Prevents transmissions */
395 	pl->sm.actor_churn = B_FALSE;
396 	pl->sm.partner_churn = B_FALSE;
397 	pl->sm.ready_n = B_FALSE;
398 
399 	lacp_port_unselect(portp);
400 
401 	pl->sm.periodic_state = LACP_NO_PERIODIC;
402 	pl->sm.receive_state = LACP_INITIALIZE;
403 	pl->sm.mux_state = LACP_DETACHED;
404 	pl->sm.churn_state = LACP_NO_ACTOR_CHURN;
405 
406 	/*
407 	 * Timer information.
408 	 */
409 	pl->current_while_timer.val = SHORT_TIMEOUT_TIME;
410 	pl->periodic_timer.val = FAST_PERIODIC_TIME;
411 }
412 
413 static void
414 aggr_lacp_mcast_on(aggr_port_t *port)
415 {
416 	ASSERT(MAC_PERIM_HELD(port->lp_grp->lg_mh));
417 	ASSERT(MAC_PERIM_HELD(port->lp_mh));
418 
419 	if (port->lp_state != AGGR_PORT_STATE_ATTACHED)
420 		return;
421 
422 	(void) aggr_port_multicst(port, B_TRUE,
423 	    (uchar_t *)&slow_multicast_addr);
424 }
425 
426 static void
427 aggr_lacp_mcast_off(aggr_port_t *port)
428 {
429 	ASSERT(MAC_PERIM_HELD(port->lp_grp->lg_mh));
430 	ASSERT(MAC_PERIM_HELD(port->lp_mh));
431 
432 	if (port->lp_state != AGGR_PORT_STATE_ATTACHED)
433 		return;
434 
435 	(void) aggr_port_multicst(port, B_FALSE,
436 	    (uchar_t *)&slow_multicast_addr);
437 }
438 
439 static void
440 start_periodic_timer(aggr_port_t *portp)
441 {
442 	aggr_lacp_port_t *pl = &portp->lp_lacp;
443 
444 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
445 
446 	mutex_enter(&pl->lacp_timer_lock);
447 	if (pl->periodic_timer.id == 0) {
448 		pl->periodic_timer.id = timeout(periodic_timer_pop, portp,
449 		    drv_usectohz(1000000 * portp->lp_lacp.periodic_timer.val));
450 	}
451 	mutex_exit(&pl->lacp_timer_lock);
452 }
453 
454 static void
455 stop_periodic_timer(aggr_port_t *portp)
456 {
457 	aggr_lacp_port_t *pl = &portp->lp_lacp;
458 	timeout_id_t id;
459 
460 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
461 
462 	mutex_enter(&pl->lacp_timer_lock);
463 	if ((id = pl->periodic_timer.id) != 0) {
464 		pl->lacp_timer_bits &= ~LACP_PERIODIC_TIMEOUT;
465 		pl->periodic_timer.id = 0;
466 	}
467 	mutex_exit(&pl->lacp_timer_lock);
468 
469 	if (id != 0)
470 		(void) untimeout(id);
471 }
472 
473 /*
474  * When the timer pops, we arrive here to
475  * clear out LACPDU count as well as transmit an
476  * LACPDU. We then set the periodic state and let
477  * the periodic state machine restart the timer.
478  */
479 static void
480 periodic_timer_pop(void *data)
481 {
482 	aggr_port_t *portp = data;
483 	aggr_lacp_port_t *pl = &portp->lp_lacp;
484 
485 	mutex_enter(&pl->lacp_timer_lock);
486 	pl->lacp_timer_bits |= LACP_PERIODIC_TIMEOUT;
487 	cv_broadcast(&pl->lacp_timer_cv);
488 	mutex_exit(&pl->lacp_timer_lock);
489 }
490 
491 /*
492  * When the timer pops, we arrive here to
493  * clear out LACPDU count as well as transmit an
494  * LACPDU. We then set the periodic state and let
495  * the periodic state machine restart the timer.
496  */
497 static void
498 periodic_timer_pop_handler(aggr_port_t *portp)
499 {
500 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
501 
502 	portp->lp_lacp_stats.LACPDUsTx = 0;
503 
504 	/* current timestamp */
505 	portp->lp_lacp.time = gethrtime();
506 	portp->lp_lacp.NTT = B_TRUE;
507 	lacp_xmit_sm(portp);
508 
509 	/*
510 	 * Set Periodic State machine state based on the
511 	 * value of the Partner Operation Port State timeout
512 	 * bit.
513 	 */
514 	if (portp->lp_lacp.PartnerOperPortState.bit.timeout) {
515 		portp->lp_lacp.periodic_timer.val = FAST_PERIODIC_TIME;
516 		portp->lp_lacp.sm.periodic_state = LACP_FAST_PERIODIC;
517 	} else {
518 		portp->lp_lacp.periodic_timer.val = SLOW_PERIODIC_TIME;
519 		portp->lp_lacp.sm.periodic_state = LACP_SLOW_PERIODIC;
520 	}
521 
522 	lacp_periodic_sm(portp);
523 }
524 
525 /*
526  * Invoked from:
527  *	- startup upon aggregation
528  *	- when the periodic timer pops
529  *	- when the periodic timer value is changed
530  *	- when the port is attached or detached
531  *	- when LACP mode is changed.
532  */
533 static void
534 lacp_periodic_sm(aggr_port_t *portp)
535 {
536 	lacp_periodic_state_t oldstate = portp->lp_lacp.sm.periodic_state;
537 	aggr_lacp_port_t *pl = &portp->lp_lacp;
538 
539 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
540 
541 	/* LACP_OFF state not in specification so check here.  */
542 	if (!pl->sm.lacp_on) {
543 		/* Stop timer whether it is running or not */
544 		stop_periodic_timer(portp);
545 		pl->sm.periodic_state = LACP_NO_PERIODIC;
546 		pl->NTT = B_FALSE;
547 		AGGR_LACP_DBG(("lacp_periodic_sm(%d):NO LACP "
548 		    "%s--->%s\n", portp->lp_linkid,
549 		    lacp_periodic_str[oldstate],
550 		    lacp_periodic_str[pl->sm.periodic_state]));
551 		return;
552 	}
553 
554 	if (pl->sm.begin || !pl->sm.lacp_enabled ||
555 	    !pl->sm.port_enabled ||
556 	    !pl->ActorOperPortState.bit.activity &&
557 	    !pl->PartnerOperPortState.bit.activity) {
558 
559 		/* Stop timer whether it is running or not */
560 		stop_periodic_timer(portp);
561 		pl->sm.periodic_state = LACP_NO_PERIODIC;
562 		pl->NTT = B_FALSE;
563 		AGGR_LACP_DBG(("lacp_periodic_sm(%d):STOP %s--->%s\n",
564 		    portp->lp_linkid, lacp_periodic_str[oldstate],
565 		    lacp_periodic_str[pl->sm.periodic_state]));
566 		return;
567 	}
568 
569 	/*
570 	 * Startup with FAST_PERIODIC_TIME if no previous LACPDU
571 	 * has been received. Then after we timeout, then it is
572 	 * possible to go to SLOW_PERIODIC_TIME.
573 	 */
574 	if (pl->sm.periodic_state == LACP_NO_PERIODIC) {
575 		pl->periodic_timer.val = FAST_PERIODIC_TIME;
576 		pl->sm.periodic_state = LACP_FAST_PERIODIC;
577 	} else if ((pl->sm.periodic_state == LACP_SLOW_PERIODIC) &&
578 	    pl->PartnerOperPortState.bit.timeout) {
579 		/*
580 		 * If we receive a bit indicating we are going to
581 		 * fast periodic from slow periodic, stop the timer
582 		 * and let the periodic_timer_pop routine deal
583 		 * with reseting the periodic state and transmitting
584 		 * a LACPDU.
585 		 */
586 		stop_periodic_timer(portp);
587 		periodic_timer_pop_handler(portp);
588 	}
589 
590 	/* Rearm timer with value provided by partner */
591 	start_periodic_timer(portp);
592 }
593 
594 /*
595  * This routine transmits an LACPDU if lacp_enabled
596  * is TRUE and if NTT is set.
597  */
598 static void
599 lacp_xmit_sm(aggr_port_t *portp)
600 {
601 	aggr_lacp_port_t *pl = &portp->lp_lacp;
602 	size_t	len;
603 	mblk_t  *mp;
604 	hrtime_t now, elapsed;
605 
606 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
607 
608 	/* LACP_OFF state not in specification so check here.  */
609 	if (!pl->sm.lacp_on || !pl->NTT || !portp->lp_started)
610 		return;
611 
612 	/*
613 	 * Do nothing if LACP has been turned off or if the
614 	 * periodic state machine is not enabled.
615 	 */
616 	if ((pl->sm.periodic_state == LACP_NO_PERIODIC) ||
617 	    !pl->sm.lacp_enabled || pl->sm.begin) {
618 		pl->NTT = B_FALSE;
619 		return;
620 	}
621 
622 	/*
623 	 * If we have sent 5 Slow packets in the last second, avoid
624 	 * sending any more here. No more than three LACPDUs may be transmitted
625 	 * in any Fast_Periodic_Time interval.
626 	 */
627 	if (portp->lp_lacp_stats.LACPDUsTx >= 3) {
628 		/*
629 		 * Grab the current time value and see if
630 		 * more than 1 second has passed. If so,
631 		 * reset the timestamp and clear the count.
632 		 */
633 		now = gethrtime();
634 		elapsed = now - pl->time;
635 		if (elapsed > NSECS_PER_SEC) {
636 			portp->lp_lacp_stats.LACPDUsTx = 0;
637 			pl->time = now;
638 		} else {
639 			return;
640 		}
641 	}
642 
643 	len = sizeof (lacp_t) + sizeof (struct ether_header);
644 	mp = allocb(len, BPRI_MED);
645 	if (mp == NULL)
646 		return;
647 
648 	mp->b_wptr = mp->b_rptr + len;
649 	bzero(mp->b_rptr, len);
650 
651 	fill_lacp_ether(portp, (struct ether_header *)mp->b_rptr);
652 	fill_lacp_pdu(portp,
653 	    (lacp_t *)(mp->b_rptr + sizeof (struct ether_header)));
654 
655 	(void) mac_tx(portp->lp_mch, mp, 0, MAC_DROP_ON_NO_DESC, NULL);
656 
657 	pl->NTT = B_FALSE;
658 	portp->lp_lacp_stats.LACPDUsTx++;
659 }
660 
661 /*
662  * Initialize the ethernet header of a LACP packet sent from the specified
663  * port.
664  */
665 static void
666 fill_lacp_ether(aggr_port_t *port, struct ether_header *ether)
667 {
668 	bcopy(port->lp_addr, (uint8_t *)&(ether->ether_shost), ETHERADDRL);
669 	bcopy(&slow_multicast_addr, (uint8_t *)&(ether->ether_dhost),
670 	    ETHERADDRL);
671 	ether->ether_type = htons(ETHERTYPE_SLOW);
672 }
673 
674 static void
675 fill_lacp_pdu(aggr_port_t *portp, lacp_t *lacp)
676 {
677 	aggr_lacp_port_t *pl = &portp->lp_lacp;
678 	aggr_grp_t *aggrp = portp->lp_grp;
679 	mac_perim_handle_t pmph;
680 
681 	ASSERT(MAC_PERIM_HELD(aggrp->lg_mh));
682 	mac_perim_enter_by_mh(portp->lp_mh, &pmph);
683 
684 	lacp->subtype = LACP_SUBTYPE;
685 	lacp->version = LACP_VERSION;
686 
687 	/*
688 	 * Actor Information
689 	 */
690 	lacp->actor_info.tlv_type = ACTOR_TLV;
691 	lacp->actor_info.information_len = sizeof (link_info_t);
692 	lacp->actor_info.system_priority =
693 	    htons(aggrp->aggr.ActorSystemPriority);
694 	bcopy(aggrp->lg_addr, (uchar_t *)&lacp->actor_info.system_id,
695 	    ETHERADDRL);
696 	lacp->actor_info.key = htons(pl->ActorOperPortKey);
697 	lacp->actor_info.port_priority = htons(pl->ActorPortPriority);
698 	lacp->actor_info.port = htons(pl->ActorPortNumber);
699 	lacp->actor_info.state.state = pl->ActorOperPortState.state;
700 
701 	/*
702 	 * Partner Information
703 	 */
704 	lacp->partner_info.tlv_type = PARTNER_TLV;
705 	lacp->partner_info.information_len = sizeof (link_info_t);
706 	lacp->partner_info.system_priority =
707 	    htons(pl->PartnerOperSysPriority);
708 	lacp->partner_info.system_id = pl->PartnerOperSystem;
709 	lacp->partner_info.key = htons(pl->PartnerOperKey);
710 	lacp->partner_info.port_priority =
711 	    htons(pl->PartnerOperPortPriority);
712 	lacp->partner_info.port = htons(pl->PartnerOperPortNum);
713 	lacp->partner_info.state.state = pl->PartnerOperPortState.state;
714 
715 	/* Collector Information */
716 	lacp->tlv_collector = COLLECTOR_TLV;
717 	lacp->collector_len = 0x10;
718 	lacp->collector_max_delay = htons(aggrp->aggr.CollectorMaxDelay);
719 
720 	/* Termination Information */
721 	lacp->tlv_terminator = TERMINATOR_TLV;
722 	lacp->terminator_len = 0x0;
723 
724 	mac_perim_exit(pmph);
725 }
726 
727 /*
728  * lacp_mux_sm - LACP mux state machine
729  *		This state machine is invoked from:
730  *			- startup upon aggregation
731  *			- from the Selection logic
732  *			- when the wait_while_timer pops
733  *			- when the aggregation MAC address is changed
734  *			- when receiving DL_NOTE_LINK_UP/DOWN
735  *			- when receiving DL_NOTE_AGGR_AVAIL/UNAVAIL
736  *			- when LACP mode is changed.
737  *			- when a DL_NOTE_SPEED is received
738  */
739 static void
740 lacp_mux_sm(aggr_port_t *portp)
741 {
742 	aggr_grp_t *aggrp = portp->lp_grp;
743 	boolean_t NTT_updated = B_FALSE;
744 	aggr_lacp_port_t *pl = &portp->lp_lacp;
745 	lacp_mux_state_t oldstate = pl->sm.mux_state;
746 
747 	ASSERT(MAC_PERIM_HELD(aggrp->lg_mh));
748 
749 	/* LACP_OFF state not in specification so check here.  */
750 	if (!pl->sm.lacp_on) {
751 		pl->sm.mux_state = LACP_DETACHED;
752 		pl->ActorOperPortState.bit.sync = B_FALSE;
753 
754 		if (pl->ActorOperPortState.bit.collecting ||
755 		    pl->ActorOperPortState.bit.distributing) {
756 			AGGR_LACP_DBG(("trunk link: (%d): "
757 			    "Collector_Distributor Disabled.\n",
758 			    portp->lp_linkid));
759 		}
760 
761 		pl->ActorOperPortState.bit.collecting =
762 		    pl->ActorOperPortState.bit.distributing = B_FALSE;
763 		return;
764 	}
765 
766 	if (pl->sm.begin || !pl->sm.lacp_enabled)
767 		pl->sm.mux_state = LACP_DETACHED;
768 
769 again:
770 	/* determine next state, or return if state unchanged */
771 	switch (pl->sm.mux_state) {
772 	case LACP_DETACHED:
773 		if (pl->sm.begin) {
774 			break;
775 		}
776 
777 		if ((pl->sm.selected == AGGR_SELECTED) ||
778 		    (pl->sm.selected == AGGR_STANDBY)) {
779 			pl->sm.mux_state = LACP_WAITING;
780 			break;
781 		}
782 		return;
783 
784 	case LACP_WAITING:
785 		if (pl->sm.selected == AGGR_UNSELECTED) {
786 			pl->sm.mux_state = LACP_DETACHED;
787 			break;
788 		}
789 
790 		if ((pl->sm.selected == AGGR_SELECTED) && aggrp->aggr.ready) {
791 			pl->sm.mux_state = LACP_ATTACHED;
792 			break;
793 		}
794 		return;
795 
796 	case LACP_ATTACHED:
797 		if ((pl->sm.selected == AGGR_UNSELECTED) ||
798 		    (pl->sm.selected == AGGR_STANDBY)) {
799 			pl->sm.mux_state = LACP_DETACHED;
800 			break;
801 		}
802 
803 		if ((pl->sm.selected == AGGR_SELECTED) &&
804 		    pl->PartnerOperPortState.bit.sync) {
805 			pl->sm.mux_state = LACP_COLLECTING_DISTRIBUTING;
806 			break;
807 		}
808 		return;
809 
810 	case LACP_COLLECTING_DISTRIBUTING:
811 		if ((pl->sm.selected == AGGR_UNSELECTED) ||
812 		    (pl->sm.selected == AGGR_STANDBY) ||
813 		    !pl->PartnerOperPortState.bit.sync) {
814 			pl->sm.mux_state = LACP_ATTACHED;
815 			break;
816 		}
817 		return;
818 	}
819 
820 	AGGR_LACP_DBG(("lacp_mux_sm(%d):%s--->%s\n",
821 	    portp->lp_linkid, lacp_mux_str[oldstate],
822 	    lacp_mux_str[pl->sm.mux_state]));
823 
824 	/* perform actions on entering a new state */
825 	switch (pl->sm.mux_state) {
826 	case LACP_DETACHED:
827 		if (pl->ActorOperPortState.bit.collecting ||
828 		    pl->ActorOperPortState.bit.distributing) {
829 			AGGR_LACP_DBG(("trunk link: (%d): "
830 			    "Collector_Distributor Disabled.\n",
831 			    portp->lp_linkid));
832 		}
833 
834 		pl->ActorOperPortState.bit.sync =
835 		    pl->ActorOperPortState.bit.collecting = B_FALSE;
836 
837 		/* Turn OFF Collector_Distributor */
838 		aggr_set_coll_dist(portp, B_FALSE);
839 
840 		pl->ActorOperPortState.bit.distributing = B_FALSE;
841 		NTT_updated = B_TRUE;
842 		break;
843 
844 	case LACP_WAITING:
845 		start_wait_while_timer(portp);
846 		break;
847 
848 	case LACP_ATTACHED:
849 		if (pl->ActorOperPortState.bit.collecting ||
850 		    pl->ActorOperPortState.bit.distributing) {
851 			AGGR_LACP_DBG(("trunk link: (%d): "
852 			    "Collector_Distributor Disabled.\n",
853 			    portp->lp_linkid));
854 		}
855 
856 		pl->ActorOperPortState.bit.sync = B_TRUE;
857 		pl->ActorOperPortState.bit.collecting = B_FALSE;
858 
859 		/* Turn OFF Collector_Distributor */
860 		aggr_set_coll_dist(portp, B_FALSE);
861 
862 		pl->ActorOperPortState.bit.distributing = B_FALSE;
863 		NTT_updated = B_TRUE;
864 		if (pl->PartnerOperPortState.bit.sync) {
865 			/*
866 			 * We had already received an updated sync from
867 			 * the partner. Attempt to transition to
868 			 * collecting/distributing now.
869 			 */
870 			goto again;
871 		}
872 		break;
873 
874 	case LACP_COLLECTING_DISTRIBUTING:
875 		if (!pl->ActorOperPortState.bit.collecting &&
876 		    !pl->ActorOperPortState.bit.distributing) {
877 			AGGR_LACP_DBG(("trunk link: (%d): "
878 			    "Collector_Distributor Enabled.\n",
879 			    portp->lp_linkid));
880 		}
881 		pl->ActorOperPortState.bit.distributing = B_TRUE;
882 
883 		/* Turn Collector_Distributor back ON */
884 		aggr_set_coll_dist(portp, B_TRUE);
885 
886 		pl->ActorOperPortState.bit.collecting = B_TRUE;
887 		NTT_updated = B_TRUE;
888 		break;
889 	}
890 
891 	/*
892 	 * If we updated the state of the NTT variable, then
893 	 * initiate a LACPDU transmission.
894 	 */
895 	if (NTT_updated) {
896 		pl->NTT = B_TRUE;
897 		lacp_xmit_sm(portp);
898 	}
899 } /* lacp_mux_sm */
900 
901 
902 static int
903 receive_marker_pdu(aggr_port_t *portp, mblk_t *mp)
904 {
905 	marker_pdu_t		*markerp = (marker_pdu_t *)mp->b_rptr;
906 
907 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
908 
909 	AGGR_LACP_DBG(("trunk link: (%d): MARKER PDU received:\n",
910 	    portp->lp_linkid));
911 
912 	/* LACP_OFF state not in specification so check here.  */
913 	if (!portp->lp_lacp.sm.lacp_on)
914 		return (-1);
915 
916 	if (MBLKL(mp) < sizeof (marker_pdu_t))
917 		return (-1);
918 
919 	if (markerp->version != MARKER_VERSION) {
920 		AGGR_LACP_DBG(("trunk link (%d): Malformed MARKER PDU: "
921 		    "version = %d does not match s/w version %d\n",
922 		    portp->lp_linkid, markerp->version, MARKER_VERSION));
923 		return (-1);
924 	}
925 
926 	if (markerp->tlv_marker == MARKER_RESPONSE_TLV) {
927 		/* We do not yet send out MARKER info PDUs */
928 		AGGR_LACP_DBG(("trunk link (%d): MARKER RESPONSE PDU: "
929 		    " MARKER TLV = %d - We don't send out info type!\n",
930 		    portp->lp_linkid, markerp->tlv_marker));
931 		return (-1);
932 	}
933 
934 	if (markerp->tlv_marker != MARKER_INFO_TLV) {
935 		AGGR_LACP_DBG(("trunk link (%d): Malformed MARKER PDU: "
936 		    " MARKER TLV = %d \n", portp->lp_linkid,
937 		    markerp->tlv_marker));
938 		return (-1);
939 	}
940 
941 	if (markerp->marker_len != MARKER_INFO_RESPONSE_LENGTH) {
942 		AGGR_LACP_DBG(("trunk link (%d): Malformed MARKER PDU: "
943 		    " MARKER length = %d \n", portp->lp_linkid,
944 		    markerp->marker_len));
945 		return (-1);
946 	}
947 
948 	if (markerp->requestor_port != portp->lp_lacp.PartnerOperPortNum) {
949 		AGGR_LACP_DBG(("trunk link (%d): MARKER PDU: "
950 		    " MARKER Port %d not equal to Partner port %d\n",
951 		    portp->lp_linkid, markerp->requestor_port,
952 		    portp->lp_lacp.PartnerOperPortNum));
953 		return (-1);
954 	}
955 
956 	if (ether_cmp(&markerp->system_id,
957 	    &portp->lp_lacp.PartnerOperSystem) != 0) {
958 		AGGR_LACP_DBG(("trunk link (%d): MARKER PDU: "
959 		    " MARKER MAC not equal to Partner MAC\n",
960 		    portp->lp_linkid));
961 		return (-1);
962 	}
963 
964 	/*
965 	 * Turn into Marker Response PDU
966 	 * and return mblk to sending system
967 	 */
968 	markerp->tlv_marker = MARKER_RESPONSE_TLV;
969 
970 	/* reuse the space that was used by received ethernet header */
971 	ASSERT(MBLKHEAD(mp) >= sizeof (struct ether_header));
972 	mp->b_rptr -= sizeof (struct ether_header);
973 	fill_lacp_ether(portp, (struct ether_header *)mp->b_rptr);
974 	return (0);
975 }
976 
977 /*
978  * Update the LACP mode (off, active, or passive) of the specified group.
979  */
980 void
981 aggr_lacp_update_mode(aggr_grp_t *grp, aggr_lacp_mode_t mode)
982 {
983 	aggr_lacp_mode_t old_mode = grp->lg_lacp_mode;
984 	aggr_port_t *port;
985 
986 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
987 	ASSERT(!grp->lg_closing);
988 
989 	if (mode == old_mode)
990 		return;
991 
992 	grp->lg_lacp_mode = mode;
993 
994 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
995 		port->lp_lacp.ActorAdminPortState.bit.activity =
996 		    port->lp_lacp.ActorOperPortState.bit.activity =
997 		    (mode == AGGR_LACP_ACTIVE);
998 
999 		if (old_mode == AGGR_LACP_OFF) {
1000 			/* OFF -> {PASSIVE,ACTIVE} */
1001 			/* turn OFF Collector_Distributor */
1002 			aggr_set_coll_dist(port, B_FALSE);
1003 			lacp_on(port);
1004 		} else if (mode == AGGR_LACP_OFF) {
1005 			/* {PASSIVE,ACTIVE} -> OFF */
1006 			lacp_off(port);
1007 			/* Turn ON Collector_Distributor */
1008 			aggr_set_coll_dist(port, B_TRUE);
1009 		} else {
1010 			/* PASSIVE->ACTIVE or ACTIVE->PASSIVE */
1011 			port->lp_lacp.sm.begin = B_TRUE;
1012 			lacp_mux_sm(port);
1013 			lacp_periodic_sm(port);
1014 
1015 			/* kick off state machines */
1016 			lacp_receive_sm(port, NULL);
1017 			lacp_mux_sm(port);
1018 		}
1019 	}
1020 }
1021 
1022 
1023 /*
1024  * Update the LACP timer (short or long) of the specified group.
1025  */
1026 void
1027 aggr_lacp_update_timer(aggr_grp_t *grp, aggr_lacp_timer_t timer)
1028 {
1029 	aggr_port_t *port;
1030 
1031 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
1032 
1033 	if (timer == grp->aggr.PeriodicTimer)
1034 		return;
1035 
1036 	grp->aggr.PeriodicTimer = timer;
1037 
1038 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
1039 		port->lp_lacp.ActorAdminPortState.bit.timeout =
1040 		    port->lp_lacp.ActorOperPortState.bit.timeout =
1041 		    (timer == AGGR_LACP_TIMER_SHORT);
1042 	}
1043 }
1044 
1045 void
1046 aggr_port_lacp_set_mode(aggr_grp_t *grp, aggr_port_t *port)
1047 {
1048 	aggr_lacp_mode_t	mode;
1049 	aggr_lacp_timer_t	timer;
1050 
1051 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
1052 
1053 	mode = grp->lg_lacp_mode;
1054 	timer = grp->aggr.PeriodicTimer;
1055 
1056 	port->lp_lacp.ActorAdminPortState.bit.activity =
1057 	    port->lp_lacp.ActorOperPortState.bit.activity =
1058 	    (mode == AGGR_LACP_ACTIVE);
1059 
1060 	port->lp_lacp.ActorAdminPortState.bit.timeout =
1061 	    port->lp_lacp.ActorOperPortState.bit.timeout =
1062 	    (timer == AGGR_LACP_TIMER_SHORT);
1063 
1064 	if (mode == AGGR_LACP_OFF) {
1065 		/* Turn ON Collector_Distributor */
1066 		aggr_set_coll_dist(port, B_TRUE);
1067 	} else { /* LACP_ACTIVE/PASSIVE */
1068 		lacp_on(port);
1069 	}
1070 }
1071 
1072 /*
1073  * Sets the initial LACP mode (off, active, passive) and LACP timer
1074  * (short, long) of the specified group.
1075  */
1076 void
1077 aggr_lacp_set_mode(aggr_grp_t *grp, aggr_lacp_mode_t mode,
1078     aggr_lacp_timer_t timer)
1079 {
1080 	aggr_port_t *port;
1081 
1082 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
1083 
1084 	grp->lg_lacp_mode = mode;
1085 	grp->aggr.PeriodicTimer = timer;
1086 
1087 	for (port = grp->lg_ports; port != NULL; port = port->lp_next)
1088 		aggr_port_lacp_set_mode(grp, port);
1089 }
1090 
1091 /*
1092  * Verify that the Partner MAC and Key recorded by the specified
1093  * port are not found in other ports that are not part of our
1094  * aggregation. Returns B_TRUE if such a port is found, B_FALSE
1095  * otherwise.
1096  */
1097 static boolean_t
1098 lacp_misconfig_check(aggr_port_t *portp)
1099 {
1100 	aggr_grp_t *grp = portp->lp_grp;
1101 	lacp_sel_ports_t *cport;
1102 
1103 	mutex_enter(&lacp_sel_lock);
1104 
1105 	for (cport = sel_ports; cport != NULL; cport = cport->sp_next) {
1106 
1107 		/* skip entries of the group of the port being checked */
1108 		if (cport->sp_grp_linkid == grp->lg_linkid)
1109 			continue;
1110 
1111 		if ((ether_cmp(&cport->sp_partner_system,
1112 		    &grp->aggr.PartnerSystem) == 0) &&
1113 		    (cport->sp_partner_key == grp->aggr.PartnerOperAggrKey)) {
1114 			char mac_str[ETHERADDRL*3];
1115 			struct ether_addr *mac = &cport->sp_partner_system;
1116 
1117 			/*
1118 			 * The Partner port information is already in use
1119 			 * by ports in another aggregation so disable this
1120 			 * port.
1121 			 */
1122 
1123 			(void) snprintf(mac_str, sizeof (mac_str),
1124 			    "%x:%x:%x:%x:%x:%x",
1125 			    mac->ether_addr_octet[0], mac->ether_addr_octet[1],
1126 			    mac->ether_addr_octet[2], mac->ether_addr_octet[3],
1127 			    mac->ether_addr_octet[4], mac->ether_addr_octet[5]);
1128 
1129 			portp->lp_lacp.sm.selected = AGGR_UNSELECTED;
1130 
1131 			cmn_err(CE_NOTE, "aggr %d port %d: Port Partner "
1132 			    "MAC %s and key %d in use on aggregation %d "
1133 			    "port %d\n", grp->lg_linkid, portp->lp_linkid,
1134 			    mac_str, portp->lp_lacp.PartnerOperKey,
1135 			    cport->sp_grp_linkid, cport->sp_linkid);
1136 			break;
1137 		}
1138 	}
1139 
1140 	mutex_exit(&lacp_sel_lock);
1141 	return (cport != NULL);
1142 }
1143 
1144 /*
1145  * Remove the specified port from the list of selected ports.
1146  */
1147 static void
1148 lacp_sel_ports_del(aggr_port_t *portp)
1149 {
1150 	lacp_sel_ports_t *cport, **prev = NULL;
1151 
1152 	mutex_enter(&lacp_sel_lock);
1153 
1154 	prev = &sel_ports;
1155 	for (cport = sel_ports; cport != NULL; prev = &cport->sp_next,
1156 	    cport = cport->sp_next) {
1157 		if (portp->lp_linkid == cport->sp_linkid)
1158 			break;
1159 	}
1160 
1161 	if (cport == NULL) {
1162 		mutex_exit(&lacp_sel_lock);
1163 		return;
1164 	}
1165 
1166 	*prev = cport->sp_next;
1167 	kmem_free(cport, sizeof (*cport));
1168 
1169 	mutex_exit(&lacp_sel_lock);
1170 }
1171 
1172 /*
1173  * Add the specified port to the list of selected ports. Returns B_FALSE
1174  * if the operation could not be performed due to an memory allocation
1175  * error.
1176  */
1177 static boolean_t
1178 lacp_sel_ports_add(aggr_port_t *portp)
1179 {
1180 	lacp_sel_ports_t *new_port;
1181 	lacp_sel_ports_t *cport, **last;
1182 
1183 	mutex_enter(&lacp_sel_lock);
1184 
1185 	/* check if port is already in the list */
1186 	last = &sel_ports;
1187 	for (cport = sel_ports; cport != NULL;
1188 	    last = &cport->sp_next, cport = cport->sp_next) {
1189 		if (portp->lp_linkid == cport->sp_linkid) {
1190 			ASSERT(cport->sp_partner_key ==
1191 			    portp->lp_lacp.PartnerOperKey);
1192 			ASSERT(ether_cmp(&cport->sp_partner_system,
1193 			    &portp->lp_lacp.PartnerOperSystem) == 0);
1194 
1195 			mutex_exit(&lacp_sel_lock);
1196 			return (B_TRUE);
1197 		}
1198 	}
1199 
1200 	/* create and initialize new entry */
1201 	new_port = kmem_zalloc(sizeof (lacp_sel_ports_t), KM_NOSLEEP);
1202 	if (new_port == NULL) {
1203 		mutex_exit(&lacp_sel_lock);
1204 		return (B_FALSE);
1205 	}
1206 
1207 	new_port->sp_grp_linkid = portp->lp_grp->lg_linkid;
1208 	bcopy(&portp->lp_lacp.PartnerOperSystem,
1209 	    &new_port->sp_partner_system, sizeof (new_port->sp_partner_system));
1210 	new_port->sp_partner_key = portp->lp_lacp.PartnerOperKey;
1211 	new_port->sp_linkid = portp->lp_linkid;
1212 
1213 	*last = new_port;
1214 
1215 	mutex_exit(&lacp_sel_lock);
1216 	return (B_TRUE);
1217 }
1218 
1219 /*
1220  * lacp_selection_logic - LACP selection logic
1221  *		Sets the selected variable on a per port basis
1222  *		and sets Ready when all waiting ports are ready
1223  *		to go online.
1224  *
1225  * parameters:
1226  *      - portp - instance this applies to.
1227  *
1228  * invoked:
1229  *    - when initialization is needed
1230  *    - when UNSELECTED is set from the lacp_receive_sm() in LACP_CURRENT state
1231  *    - When the lacp_receive_sm goes to the LACP_DEFAULTED state
1232  *    - every time the wait_while_timer pops
1233  *    - everytime we turn LACP on/off
1234  */
1235 static void
1236 lacp_selection_logic(aggr_port_t *portp)
1237 {
1238 	aggr_port_t *tpp;
1239 	aggr_grp_t *aggrp = portp->lp_grp;
1240 	int ports_waiting;
1241 	boolean_t reset_mac = B_FALSE;
1242 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1243 
1244 	ASSERT(MAC_PERIM_HELD(aggrp->lg_mh));
1245 
1246 	/* LACP_OFF state not in specification so check here.  */
1247 	if (!pl->sm.lacp_on) {
1248 		lacp_port_unselect(portp);
1249 		aggrp->aggr.ready = B_FALSE;
1250 		lacp_mux_sm(portp);
1251 		return;
1252 	}
1253 
1254 	if (pl->sm.begin || !pl->sm.lacp_enabled ||
1255 	    (portp->lp_state != AGGR_PORT_STATE_ATTACHED)) {
1256 
1257 		AGGR_LACP_DBG(("lacp_selection_logic:(%d): "
1258 		    "selected %d-->%d (begin=%d, lacp_enabled = %d, "
1259 		    "lp_state=%d)\n", portp->lp_linkid, pl->sm.selected,
1260 		    AGGR_UNSELECTED, pl->sm.begin, pl->sm.lacp_enabled,
1261 		    portp->lp_state));
1262 
1263 		lacp_port_unselect(portp);
1264 		aggrp->aggr.ready = B_FALSE;
1265 		lacp_mux_sm(portp);
1266 		return;
1267 	}
1268 
1269 	/*
1270 	 * If LACP is not enabled then selected is never set.
1271 	 */
1272 	if (!pl->sm.lacp_enabled) {
1273 		AGGR_LACP_DBG(("lacp_selection_logic:(%d): selected %d-->%d\n",
1274 		    portp->lp_linkid, pl->sm.selected, AGGR_UNSELECTED));
1275 
1276 		lacp_port_unselect(portp);
1277 		lacp_mux_sm(portp);
1278 		return;
1279 	}
1280 
1281 	/*
1282 	 * Check if the Partner MAC or Key are zero. If so, we have
1283 	 * not received any LACP info or it has expired and the
1284 	 * receive machine is in the LACP_DEFAULTED state.
1285 	 */
1286 	if (ether_cmp(&pl->PartnerOperSystem, &etherzeroaddr) == 0 ||
1287 	    (pl->PartnerOperKey == 0)) {
1288 
1289 		for (tpp = aggrp->lg_ports; tpp; tpp = tpp->lp_next) {
1290 			if (ether_cmp(&tpp->lp_lacp.PartnerOperSystem,
1291 			    &etherzeroaddr) != 0 &&
1292 			    (tpp->lp_lacp.PartnerOperKey != 0))
1293 				break;
1294 		}
1295 
1296 		/*
1297 		 * If all ports have no key or aggregation address,
1298 		 * then clear the negotiated Partner MAC and key.
1299 		 */
1300 		if (tpp == NULL) {
1301 			/* Clear the aggregation Partner MAC and key */
1302 			aggrp->aggr.PartnerSystem = etherzeroaddr;
1303 			aggrp->aggr.PartnerOperAggrKey = 0;
1304 		}
1305 
1306 		return;
1307 	}
1308 
1309 	/*
1310 	 * Insure that at least one port in the aggregation
1311 	 * matches the Partner aggregation MAC and key. If not,
1312 	 * then clear the aggregation MAC and key. Later we will
1313 	 * set the Partner aggregation MAC and key to that of the
1314 	 * current port's Partner MAC and key.
1315 	 */
1316 	if (ether_cmp(&pl->PartnerOperSystem,
1317 	    &aggrp->aggr.PartnerSystem) != 0 ||
1318 	    (pl->PartnerOperKey != aggrp->aggr.PartnerOperAggrKey)) {
1319 
1320 		for (tpp = aggrp->lg_ports; tpp; tpp = tpp->lp_next) {
1321 			if (ether_cmp(&tpp->lp_lacp.PartnerOperSystem,
1322 			    &aggrp->aggr.PartnerSystem) == 0 &&
1323 			    (tpp->lp_lacp.PartnerOperKey ==
1324 			    aggrp->aggr.PartnerOperAggrKey))
1325 				break;
1326 		}
1327 
1328 		if (tpp == NULL) {
1329 			/* Clear the aggregation Partner MAC and key */
1330 			aggrp->aggr.PartnerSystem = etherzeroaddr;
1331 			aggrp->aggr.PartnerOperAggrKey = 0;
1332 			reset_mac = B_TRUE;
1333 		}
1334 	}
1335 
1336 	/*
1337 	 * If our Actor MAC is found in the Partner MAC
1338 	 * on this port then we have a loopback misconfiguration.
1339 	 */
1340 	if (ether_cmp(&pl->PartnerOperSystem,
1341 	    (struct ether_addr *)&aggrp->lg_addr) == 0) {
1342 		cmn_err(CE_NOTE, "trunk link: (%d): Loopback condition.\n",
1343 		    portp->lp_linkid);
1344 
1345 		lacp_port_unselect(portp);
1346 		lacp_mux_sm(portp);
1347 		return;
1348 	}
1349 
1350 	/*
1351 	 * If our Partner MAC and Key are found on any other
1352 	 * ports that are not in our aggregation, we have
1353 	 * a misconfiguration.
1354 	 */
1355 	if (lacp_misconfig_check(portp)) {
1356 		lacp_mux_sm(portp);
1357 		return;
1358 	}
1359 
1360 	/*
1361 	 * If the Aggregation Partner MAC and Key have not been
1362 	 * set, then this is either the first port or the aggregation
1363 	 * MAC and key have been reset. In either case we must set
1364 	 * the values of the Partner MAC and key.
1365 	 */
1366 	if (ether_cmp(&aggrp->aggr.PartnerSystem, &etherzeroaddr) == 0 &&
1367 	    (aggrp->aggr.PartnerOperAggrKey == 0)) {
1368 		/* Set aggregation Partner MAC and key */
1369 		aggrp->aggr.PartnerSystem = pl->PartnerOperSystem;
1370 		aggrp->aggr.PartnerOperAggrKey = pl->PartnerOperKey;
1371 
1372 		/*
1373 		 * If we reset Partner aggregation MAC, then restart
1374 		 * selection_logic on ports that match new MAC address.
1375 		 */
1376 		if (reset_mac) {
1377 			for (tpp = aggrp->lg_ports; tpp; tpp =
1378 			    tpp->lp_next) {
1379 				if (tpp == portp)
1380 					continue;
1381 				if (ether_cmp(&tpp->lp_lacp.PartnerOperSystem,
1382 				    &aggrp->aggr.PartnerSystem) == 0 &&
1383 				    (tpp->lp_lacp.PartnerOperKey ==
1384 				    aggrp->aggr.PartnerOperAggrKey))
1385 					lacp_selection_logic(tpp);
1386 			}
1387 		}
1388 	} else if (ether_cmp(&pl->PartnerOperSystem,
1389 	    &aggrp->aggr.PartnerSystem) != 0 ||
1390 	    (pl->PartnerOperKey != aggrp->aggr.PartnerOperAggrKey)) {
1391 		/*
1392 		 * The Partner port information does not match
1393 		 * that of the other ports in the aggregation
1394 		 * so disable this port.
1395 		 */
1396 		lacp_port_unselect(portp);
1397 
1398 		cmn_err(CE_NOTE, "trunk link: (%d): Port Partner MAC "
1399 		    "or key (%d) incompatible with Aggregation Partner "
1400 		    "MAC or key (%d)\n", portp->lp_linkid, pl->PartnerOperKey,
1401 		    aggrp->aggr.PartnerOperAggrKey);
1402 
1403 		lacp_mux_sm(portp);
1404 		return;
1405 	}
1406 
1407 	/* If we get to here, automatically set selected */
1408 	if (pl->sm.selected != AGGR_SELECTED) {
1409 		AGGR_LACP_DBG(("lacp_selection_logic:(%d): "
1410 		    "selected %d-->%d\n", portp->lp_linkid,
1411 		    pl->sm.selected, AGGR_SELECTED));
1412 		if (!lacp_port_select(portp))
1413 			return;
1414 		lacp_mux_sm(portp);
1415 	}
1416 
1417 	/*
1418 	 * From this point onward we have selected the port
1419 	 * and are simply checking if the Ready flag should
1420 	 * be set.
1421 	 */
1422 
1423 	/*
1424 	 * If at least two ports are waiting to aggregate
1425 	 * and ready_n is set on all ports waiting to aggregate
1426 	 * then set READY for the aggregation.
1427 	 */
1428 
1429 	ports_waiting = 0;
1430 
1431 	if (!aggrp->aggr.ready) {
1432 		/*
1433 		 * If all ports in the aggregation have received compatible
1434 		 * partner information and they match up correctly with the
1435 		 * switch, there is no need to wait for all the
1436 		 * wait_while_timers to pop.
1437 		 */
1438 		for (tpp = aggrp->lg_ports; tpp; tpp = tpp->lp_next) {
1439 			if (((tpp->lp_lacp.sm.mux_state == LACP_WAITING) ||
1440 			    tpp->lp_lacp.sm.begin) &&
1441 			    !pl->PartnerOperPortState.bit.sync) {
1442 				/* Add up ports uninitialized or waiting */
1443 				ports_waiting++;
1444 				if (!tpp->lp_lacp.sm.ready_n)
1445 					return;
1446 			}
1447 		}
1448 	}
1449 
1450 	if (aggrp->aggr.ready) {
1451 		AGGR_LACP_DBG(("lacp_selection_logic:(%d): "
1452 		    "aggr.ready already set\n", portp->lp_linkid));
1453 		lacp_mux_sm(portp);
1454 	} else {
1455 		AGGR_LACP_DBG(("lacp_selection_logic:(%d): Ready %d-->%d\n",
1456 		    portp->lp_linkid, aggrp->aggr.ready, B_TRUE));
1457 		aggrp->aggr.ready = B_TRUE;
1458 
1459 		for (tpp = aggrp->lg_ports; tpp; tpp = tpp->lp_next)
1460 			lacp_mux_sm(tpp);
1461 	}
1462 
1463 }
1464 
1465 /*
1466  * wait_while_timer_pop - When the timer pops, we arrive here to
1467  *			set ready_n and trigger the selection logic.
1468  */
1469 static void
1470 wait_while_timer_pop(void *data)
1471 {
1472 	aggr_port_t *portp = data;
1473 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1474 
1475 	mutex_enter(&pl->lacp_timer_lock);
1476 	pl->lacp_timer_bits |= LACP_WAIT_WHILE_TIMEOUT;
1477 	cv_broadcast(&pl->lacp_timer_cv);
1478 	mutex_exit(&pl->lacp_timer_lock);
1479 }
1480 
1481 /*
1482  * wait_while_timer_pop_handler - When the timer pops, we arrive here to
1483  *			set ready_n and trigger the selection logic.
1484  */
1485 static void
1486 wait_while_timer_pop_handler(aggr_port_t *portp)
1487 {
1488 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1489 
1490 	AGGR_LACP_DBG(("trunk link:(%d): wait_while_timer pop \n",
1491 	    portp->lp_linkid));
1492 	portp->lp_lacp.sm.ready_n = B_TRUE;
1493 
1494 	lacp_selection_logic(portp);
1495 }
1496 
1497 static void
1498 start_wait_while_timer(aggr_port_t *portp)
1499 {
1500 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1501 
1502 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1503 
1504 	mutex_enter(&pl->lacp_timer_lock);
1505 	if (pl->wait_while_timer.id == 0) {
1506 		pl->wait_while_timer.id =
1507 		    timeout(wait_while_timer_pop, portp,
1508 		    drv_usectohz(1000000 *
1509 		    portp->lp_lacp.wait_while_timer.val));
1510 	}
1511 	mutex_exit(&pl->lacp_timer_lock);
1512 }
1513 
1514 
1515 static void
1516 stop_wait_while_timer(aggr_port_t *portp)
1517 {
1518 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1519 	timeout_id_t id;
1520 
1521 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1522 
1523 	mutex_enter(&pl->lacp_timer_lock);
1524 	if ((id = pl->wait_while_timer.id) != 0) {
1525 		pl->lacp_timer_bits &= ~LACP_WAIT_WHILE_TIMEOUT;
1526 		pl->wait_while_timer.id = 0;
1527 	}
1528 	mutex_exit(&pl->lacp_timer_lock);
1529 
1530 	if (id != 0)
1531 		(void) untimeout(id);
1532 }
1533 
1534 /*
1535  * Invoked when a port has been attached to a group.
1536  * Complete the processing that couldn't be finished from lacp_on()
1537  * because the port was not started. We know that the link is full
1538  * duplex and ON, otherwise it wouldn't be attached.
1539  */
1540 void
1541 aggr_lacp_port_attached(aggr_port_t *portp)
1542 {
1543 	aggr_grp_t *grp = portp->lp_grp;
1544 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1545 
1546 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
1547 	ASSERT(MAC_PERIM_HELD(portp->lp_mh));
1548 	ASSERT(portp->lp_state == AGGR_PORT_STATE_ATTACHED);
1549 
1550 	AGGR_LACP_DBG(("aggr_lacp_port_attached: port %d\n",
1551 	    portp->lp_linkid));
1552 
1553 	portp->lp_lacp.sm.port_enabled = B_TRUE;	/* link on */
1554 
1555 	if (grp->lg_lacp_mode == AGGR_LACP_OFF)
1556 		return;
1557 
1558 	pl->sm.lacp_enabled = B_TRUE;
1559 	pl->ActorOperPortState.bit.aggregation = B_TRUE;
1560 	pl->sm.begin = B_TRUE;
1561 
1562 	lacp_receive_sm(portp, NULL);
1563 	lacp_mux_sm(portp);
1564 
1565 	/* Enable Multicast Slow Protocol address */
1566 	aggr_lacp_mcast_on(portp);
1567 
1568 	/* periodic_sm is started up from the receive machine */
1569 	lacp_selection_logic(portp);
1570 }
1571 
1572 /*
1573  * Invoked when a port has been detached from a group. Turn off
1574  * LACP processing if it was enabled.
1575  */
1576 void
1577 aggr_lacp_port_detached(aggr_port_t *portp)
1578 {
1579 	aggr_grp_t *grp = portp->lp_grp;
1580 
1581 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
1582 	ASSERT(MAC_PERIM_HELD(portp->lp_mh));
1583 
1584 	AGGR_LACP_DBG(("aggr_lacp_port_detached: port %d\n",
1585 	    portp->lp_linkid));
1586 
1587 	portp->lp_lacp.sm.port_enabled = B_FALSE;
1588 
1589 	if (grp->lg_lacp_mode == AGGR_LACP_OFF)
1590 		return;
1591 
1592 	portp->lp_lacp.sm.lacp_enabled = B_FALSE;
1593 	lacp_selection_logic(portp);
1594 	lacp_mux_sm(portp);
1595 	lacp_periodic_sm(portp);
1596 
1597 	/*
1598 	 * Disable Slow Protocol Timers.
1599 	 */
1600 	stop_periodic_timer(portp);
1601 	stop_current_while_timer(portp);
1602 	stop_wait_while_timer(portp);
1603 
1604 	/* Disable Multicast Slow Protocol address */
1605 	aggr_lacp_mcast_off(portp);
1606 	aggr_set_coll_dist(portp, B_FALSE);
1607 }
1608 
1609 /*
1610  * Enable Slow Protocol LACP and Marker PDUs.
1611  */
1612 static void
1613 lacp_on(aggr_port_t *portp)
1614 {
1615 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1616 	mac_perim_handle_t mph;
1617 
1618 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1619 
1620 	mac_perim_enter_by_mh(portp->lp_mh, &mph);
1621 
1622 	/*
1623 	 * Reset the state machines and Partner operational
1624 	 * information. Careful to not reset things like
1625 	 * our link state.
1626 	 */
1627 	lacp_reset_port(portp);
1628 	pl->sm.lacp_on = B_TRUE;
1629 
1630 	AGGR_LACP_DBG(("lacp_on:(%d): \n", portp->lp_linkid));
1631 
1632 	if (portp->lp_state == AGGR_PORT_STATE_ATTACHED) {
1633 		pl->sm.port_enabled = B_TRUE;
1634 		pl->sm.lacp_enabled = B_TRUE;
1635 		pl->ActorOperPortState.bit.aggregation = B_TRUE;
1636 	}
1637 
1638 	lacp_receive_sm(portp, NULL);
1639 	lacp_mux_sm(portp);
1640 
1641 	if (portp->lp_state == AGGR_PORT_STATE_ATTACHED) {
1642 		/* Enable Multicast Slow Protocol address */
1643 		aggr_lacp_mcast_on(portp);
1644 
1645 		/* periodic_sm is started up from the receive machine */
1646 		lacp_selection_logic(portp);
1647 	}
1648 done:
1649 	mac_perim_exit(mph);
1650 } /* lacp_on */
1651 
1652 /* Disable Slow Protocol LACP and Marker PDUs */
1653 static void
1654 lacp_off(aggr_port_t *portp)
1655 {
1656 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1657 	mac_perim_handle_t mph;
1658 
1659 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1660 	mac_perim_enter_by_mh(portp->lp_mh, &mph);
1661 
1662 	pl->sm.lacp_on = B_FALSE;
1663 
1664 	AGGR_LACP_DBG(("lacp_off:(%d): \n", portp->lp_linkid));
1665 
1666 	if (portp->lp_state == AGGR_PORT_STATE_ATTACHED) {
1667 		/*
1668 		 * Disable Slow Protocol Timers.
1669 		 */
1670 		stop_periodic_timer(portp);
1671 		stop_current_while_timer(portp);
1672 		stop_wait_while_timer(portp);
1673 
1674 		/* Disable Multicast Slow Protocol address */
1675 		aggr_lacp_mcast_off(portp);
1676 
1677 		pl->sm.port_enabled = B_FALSE;
1678 		pl->sm.lacp_enabled = B_FALSE;
1679 		pl->ActorOperPortState.bit.aggregation = B_FALSE;
1680 	}
1681 
1682 	lacp_mux_sm(portp);
1683 	lacp_periodic_sm(portp);
1684 	lacp_selection_logic(portp);
1685 
1686 	/* Turn OFF Collector_Distributor */
1687 	aggr_set_coll_dist(portp, B_FALSE);
1688 
1689 	lacp_reset_port(portp);
1690 	mac_perim_exit(mph);
1691 }
1692 
1693 
1694 static boolean_t
1695 valid_lacp_pdu(aggr_port_t *portp, lacp_t *lacp)
1696 {
1697 	/*
1698 	 * 43.4.12 - "a Receive machine shall not validate
1699 	 * the Version Number, TLV_type, or Reserved fields in received
1700 	 * LACPDUs."
1701 	 * ... "a Receive machine may validate the Actor_Information_Length,
1702 	 * Partner_Information_Length, Collector_Information_Length,
1703 	 * or Terminator_Length fields."
1704 	 */
1705 	if ((lacp->actor_info.information_len != sizeof (link_info_t)) ||
1706 	    (lacp->partner_info.information_len != sizeof (link_info_t)) ||
1707 	    (lacp->collector_len != LACP_COLLECTOR_INFO_LEN) ||
1708 	    (lacp->terminator_len != LACP_TERMINATOR_INFO_LEN)) {
1709 		AGGR_LACP_DBG(("trunk link (%d): Malformed LACPDU: "
1710 		    " Terminator Length = %d \n", portp->lp_linkid,
1711 		    lacp->terminator_len));
1712 		return (B_FALSE);
1713 	}
1714 
1715 	return (B_TRUE);
1716 }
1717 
1718 
1719 static void
1720 start_current_while_timer(aggr_port_t *portp, uint_t time)
1721 {
1722 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1723 
1724 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1725 
1726 	mutex_enter(&pl->lacp_timer_lock);
1727 	if (pl->current_while_timer.id == 0) {
1728 		if (time > 0)
1729 			pl->current_while_timer.val = time;
1730 		else if (pl->ActorOperPortState.bit.timeout)
1731 			pl->current_while_timer.val = SHORT_TIMEOUT_TIME;
1732 		else
1733 			pl->current_while_timer.val = LONG_TIMEOUT_TIME;
1734 
1735 		pl->current_while_timer.id =
1736 		    timeout(current_while_timer_pop, portp,
1737 		    drv_usectohz((clock_t)1000000 *
1738 		    (clock_t)portp->lp_lacp.current_while_timer.val));
1739 	}
1740 	mutex_exit(&pl->lacp_timer_lock);
1741 }
1742 
1743 
1744 static void
1745 stop_current_while_timer(aggr_port_t *portp)
1746 {
1747 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1748 	timeout_id_t id;
1749 
1750 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1751 
1752 	mutex_enter(&pl->lacp_timer_lock);
1753 	if ((id = pl->current_while_timer.id) != 0) {
1754 		pl->lacp_timer_bits &= ~LACP_CURRENT_WHILE_TIMEOUT;
1755 		pl->current_while_timer.id = 0;
1756 	}
1757 	mutex_exit(&pl->lacp_timer_lock);
1758 
1759 	if (id != 0)
1760 		(void) untimeout(id);
1761 }
1762 
1763 static void
1764 current_while_timer_pop(void *data)
1765 {
1766 	aggr_port_t *portp = (aggr_port_t *)data;
1767 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1768 
1769 	mutex_enter(&pl->lacp_timer_lock);
1770 	pl->lacp_timer_bits |= LACP_CURRENT_WHILE_TIMEOUT;
1771 	cv_broadcast(&pl->lacp_timer_cv);
1772 	mutex_exit(&pl->lacp_timer_lock);
1773 }
1774 
1775 static void
1776 current_while_timer_pop_handler(aggr_port_t *portp)
1777 {
1778 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1779 
1780 	AGGR_LACP_DBG(("trunk link:(%d): current_while_timer "
1781 	    "pop id=%p\n", portp->lp_linkid,
1782 	    portp->lp_lacp.current_while_timer.id));
1783 
1784 	lacp_receive_sm(portp, NULL);
1785 }
1786 
1787 /*
1788  * record_Default - Simply copies over administrative values
1789  * to the partner operational values, and sets our state to indicate we
1790  * are using defaulted values.
1791  */
1792 static void
1793 record_Default(aggr_port_t *portp)
1794 {
1795 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1796 
1797 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1798 
1799 	pl->PartnerOperPortNum = pl->PartnerAdminPortNum;
1800 	pl->PartnerOperPortPriority = pl->PartnerAdminPortPriority;
1801 	pl->PartnerOperSystem = pl->PartnerAdminSystem;
1802 	pl->PartnerOperSysPriority = pl->PartnerAdminSysPriority;
1803 	pl->PartnerOperKey = pl->PartnerAdminKey;
1804 	pl->PartnerOperPortState.state = pl->PartnerAdminPortState.state;
1805 
1806 	pl->ActorOperPortState.bit.defaulted = B_TRUE;
1807 }
1808 
1809 
1810 /* Returns B_TRUE on sync value changing */
1811 static boolean_t
1812 record_PDU(aggr_port_t *portp, lacp_t *lacp)
1813 {
1814 	aggr_grp_t *aggrp = portp->lp_grp;
1815 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1816 	uint8_t save_sync;
1817 
1818 	ASSERT(MAC_PERIM_HELD(aggrp->lg_mh));
1819 
1820 	/*
1821 	 * Partner Information
1822 	 */
1823 	pl->PartnerOperPortNum = ntohs(lacp->actor_info.port);
1824 	pl->PartnerOperPortPriority =
1825 	    ntohs(lacp->actor_info.port_priority);
1826 	pl->PartnerOperSystem = lacp->actor_info.system_id;
1827 	pl->PartnerOperSysPriority =
1828 	    htons(lacp->actor_info.system_priority);
1829 	pl->PartnerOperKey = ntohs(lacp->actor_info.key);
1830 
1831 	/* All state info except for Synchronization */
1832 	save_sync = pl->PartnerOperPortState.bit.sync;
1833 	pl->PartnerOperPortState.state = lacp->actor_info.state.state;
1834 
1835 	/* Defaulted set to FALSE */
1836 	pl->ActorOperPortState.bit.defaulted = B_FALSE;
1837 
1838 	/*
1839 	 * 43.4.9 - (Partner_Port, Partner_Port_Priority, Partner_system,
1840 	 *		Partner_System_Priority, Partner_Key, and
1841 	 *		Partner_State.Aggregation) are compared to the
1842 	 *		corresponding operations paramters values for
1843 	 *		the Actor. If these are equal, or if this is
1844 	 *		an individual link, we are synchronized.
1845 	 */
1846 	if (((ntohs(lacp->partner_info.port) == pl->ActorPortNumber) &&
1847 	    (ntohs(lacp->partner_info.port_priority) ==
1848 	    pl->ActorPortPriority) &&
1849 	    (ether_cmp(&lacp->partner_info.system_id,
1850 	    (struct ether_addr *)&aggrp->lg_addr) == 0) &&
1851 	    (ntohs(lacp->partner_info.system_priority) ==
1852 	    aggrp->aggr.ActorSystemPriority) &&
1853 	    (ntohs(lacp->partner_info.key) == pl->ActorOperPortKey) &&
1854 	    (lacp->partner_info.state.bit.aggregation ==
1855 	    pl->ActorOperPortState.bit.aggregation)) ||
1856 	    (!lacp->actor_info.state.bit.aggregation)) {
1857 
1858 		pl->PartnerOperPortState.bit.sync =
1859 		    lacp->actor_info.state.bit.sync;
1860 	} else {
1861 		pl->PartnerOperPortState.bit.sync = B_FALSE;
1862 	}
1863 
1864 	if (save_sync != pl->PartnerOperPortState.bit.sync) {
1865 		AGGR_LACP_DBG(("record_PDU:(%d): partner sync "
1866 		    "%d -->%d\n", portp->lp_linkid, save_sync,
1867 		    pl->PartnerOperPortState.bit.sync));
1868 		return (B_TRUE);
1869 	} else {
1870 		return (B_FALSE);
1871 	}
1872 }
1873 
1874 
1875 /*
1876  * update_selected - If any of the Partner parameters has
1877  *			changed from a previous value, then
1878  *			unselect the link from the aggregator.
1879  */
1880 static boolean_t
1881 update_selected(aggr_port_t *portp, lacp_t *lacp)
1882 {
1883 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1884 
1885 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1886 
1887 	if ((pl->PartnerOperPortNum != ntohs(lacp->actor_info.port)) ||
1888 	    (pl->PartnerOperPortPriority !=
1889 	    ntohs(lacp->actor_info.port_priority)) ||
1890 	    (ether_cmp(&pl->PartnerOperSystem,
1891 	    &lacp->actor_info.system_id) != 0) ||
1892 	    (pl->PartnerOperSysPriority !=
1893 	    ntohs(lacp->actor_info.system_priority)) ||
1894 	    (pl->PartnerOperKey != ntohs(lacp->actor_info.key)) ||
1895 	    (pl->PartnerOperPortState.bit.aggregation !=
1896 	    lacp->actor_info.state.bit.aggregation)) {
1897 		AGGR_LACP_DBG(("update_selected:(%d): "
1898 		    "selected  %d-->%d\n", portp->lp_linkid, pl->sm.selected,
1899 		    AGGR_UNSELECTED));
1900 
1901 		lacp_port_unselect(portp);
1902 		return (B_TRUE);
1903 	} else {
1904 		return (B_FALSE);
1905 	}
1906 }
1907 
1908 
1909 /*
1910  * update_default_selected - If any of the operational Partner parameters
1911  *			is different than that of the administrative values
1912  *			then unselect the link from the aggregator.
1913  */
1914 static void
1915 update_default_selected(aggr_port_t *portp)
1916 {
1917 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1918 
1919 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1920 
1921 	if ((pl->PartnerAdminPortNum != pl->PartnerOperPortNum) ||
1922 	    (pl->PartnerOperPortPriority != pl->PartnerAdminPortPriority) ||
1923 	    (ether_cmp(&pl->PartnerOperSystem, &pl->PartnerAdminSystem) != 0) ||
1924 	    (pl->PartnerOperSysPriority != pl->PartnerAdminSysPriority) ||
1925 	    (pl->PartnerOperKey != pl->PartnerAdminKey) ||
1926 	    (pl->PartnerOperPortState.bit.aggregation !=
1927 	    pl->PartnerAdminPortState.bit.aggregation)) {
1928 
1929 		AGGR_LACP_DBG(("update_default_selected:(%d): "
1930 		    "selected  %d-->%d\n", portp->lp_linkid,
1931 		    pl->sm.selected, AGGR_UNSELECTED));
1932 
1933 		lacp_port_unselect(portp);
1934 	}
1935 }
1936 
1937 
1938 /*
1939  * update_NTT - If any of the Partner values in the received LACPDU
1940  *			are different than that of the Actor operational
1941  *			values then set NTT to true.
1942  */
1943 static void
1944 update_NTT(aggr_port_t *portp, lacp_t *lacp)
1945 {
1946 	aggr_grp_t *aggrp = portp->lp_grp;
1947 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1948 
1949 	ASSERT(MAC_PERIM_HELD(aggrp->lg_mh));
1950 
1951 	if ((pl->ActorPortNumber != ntohs(lacp->partner_info.port)) ||
1952 	    (pl->ActorPortPriority !=
1953 	    ntohs(lacp->partner_info.port_priority)) ||
1954 	    (ether_cmp(&aggrp->lg_addr,
1955 	    &lacp->partner_info.system_id) != 0) ||
1956 	    (aggrp->aggr.ActorSystemPriority !=
1957 	    ntohs(lacp->partner_info.system_priority)) ||
1958 	    (pl->ActorOperPortKey != ntohs(lacp->partner_info.key)) ||
1959 	    (pl->ActorOperPortState.bit.activity !=
1960 	    lacp->partner_info.state.bit.activity) ||
1961 	    (pl->ActorOperPortState.bit.timeout !=
1962 	    lacp->partner_info.state.bit.timeout) ||
1963 	    (pl->ActorOperPortState.bit.sync !=
1964 	    lacp->partner_info.state.bit.sync) ||
1965 	    (pl->ActorOperPortState.bit.aggregation !=
1966 	    lacp->partner_info.state.bit.aggregation)) {
1967 
1968 		AGGR_LACP_DBG(("update_NTT:(%d): NTT  %d-->%d\n",
1969 		    portp->lp_linkid, pl->NTT, B_TRUE));
1970 
1971 		pl->NTT = B_TRUE;
1972 	}
1973 }
1974 
1975 /*
1976  * lacp_receive_sm - LACP receive state machine
1977  *
1978  * parameters:
1979  *      - portp - instance this applies to.
1980  *      - lacp - pointer in the case of a received LACPDU.
1981  *                This value is NULL if there is no LACPDU.
1982  *
1983  * invoked:
1984  *    - when initialization is needed
1985  *    - upon reception of an LACPDU. This is the common case.
1986  *    - every time the current_while_timer pops
1987  */
1988 static void
1989 lacp_receive_sm(aggr_port_t *portp, lacp_t *lacp)
1990 {
1991 	boolean_t sync_updated, selected_updated, save_activity;
1992 	aggr_lacp_port_t *pl = &portp->lp_lacp;
1993 	lacp_receive_state_t oldstate = pl->sm.receive_state;
1994 
1995 	ASSERT(MAC_PERIM_HELD(portp->lp_grp->lg_mh));
1996 
1997 	/* LACP_OFF state not in specification so check here.  */
1998 	if (!pl->sm.lacp_on)
1999 		return;
2000 
2001 	/* figure next state */
2002 	if (pl->sm.begin || pl->sm.port_moved) {
2003 		pl->sm.receive_state = LACP_INITIALIZE;
2004 	} else if (!pl->sm.port_enabled) {	/* DL_NOTE_LINK_DOWN */
2005 		pl->sm.receive_state = LACP_PORT_DISABLED;
2006 	} else if (!pl->sm.lacp_enabled) { /* DL_NOTE_AGGR_UNAVAIL */
2007 		pl->sm.receive_state =
2008 		    (pl->sm.receive_state == LACP_PORT_DISABLED) ?
2009 		    LACP_DISABLED : LACP_PORT_DISABLED;
2010 	} else if (lacp != NULL) {
2011 		if ((pl->sm.receive_state == LACP_EXPIRED) ||
2012 		    (pl->sm.receive_state == LACP_DEFAULTED)) {
2013 			pl->sm.receive_state = LACP_CURRENT;
2014 		}
2015 	} else if ((pl->sm.receive_state == LACP_CURRENT) &&
2016 	    (pl->current_while_timer.id == 0)) {
2017 		pl->sm.receive_state = LACP_EXPIRED;
2018 	} else if ((pl->sm.receive_state == LACP_EXPIRED) &&
2019 	    (pl->current_while_timer.id == 0)) {
2020 		pl->sm.receive_state = LACP_DEFAULTED;
2021 	}
2022 
2023 	if (!((lacp && (oldstate == LACP_CURRENT) &&
2024 	    (pl->sm.receive_state == LACP_CURRENT)))) {
2025 		AGGR_LACP_DBG(("lacp_receive_sm(%d):%s--->%s\n",
2026 		    portp->lp_linkid, lacp_receive_str[oldstate],
2027 		    lacp_receive_str[pl->sm.receive_state]));
2028 	}
2029 
2030 	switch (pl->sm.receive_state) {
2031 	case LACP_INITIALIZE:
2032 		lacp_port_unselect(portp);
2033 		record_Default(portp);
2034 		pl->ActorOperPortState.bit.expired = B_FALSE;
2035 		pl->sm.port_moved = B_FALSE;
2036 		pl->sm.receive_state = LACP_PORT_DISABLED;
2037 		pl->sm.begin = B_FALSE;
2038 		lacp_receive_sm(portp, NULL);
2039 		break;
2040 
2041 	case LACP_PORT_DISABLED:
2042 		pl->PartnerOperPortState.bit.sync = B_FALSE;
2043 		/*
2044 		 * Stop current_while_timer in case
2045 		 * we got here from link down
2046 		 */
2047 		stop_current_while_timer(portp);
2048 
2049 		if (pl->sm.port_enabled && !pl->sm.lacp_enabled) {
2050 			pl->sm.receive_state = LACP_DISABLED;
2051 			lacp_receive_sm(portp, lacp);
2052 			/* We goto LACP_DISABLED state */
2053 			break;
2054 		} else if (pl->sm.port_enabled && pl->sm.lacp_enabled) {
2055 			pl->sm.receive_state = LACP_EXPIRED;
2056 			/*
2057 			 * FALL THROUGH TO LACP_EXPIRED CASE:
2058 			 * We have no way of knowing if we get into
2059 			 * lacp_receive_sm() from a  current_while_timer
2060 			 * expiring as it has never been kicked off yet!
2061 			 */
2062 		} else {
2063 			/* We stay in LACP_PORT_DISABLED state */
2064 			break;
2065 		}
2066 		/* LACP_PORT_DISABLED -> LACP_EXPIRED */
2067 		/* FALLTHROUGH */
2068 
2069 	case LACP_EXPIRED:
2070 		/*
2071 		 * Arrives here from LACP_PORT_DISABLED state as well as
2072 		 * as well as current_while_timer expiring.
2073 		 */
2074 		pl->PartnerOperPortState.bit.sync = B_FALSE;
2075 		pl->PartnerOperPortState.bit.timeout = B_TRUE;
2076 
2077 		pl->ActorOperPortState.bit.expired = B_TRUE;
2078 		start_current_while_timer(portp, SHORT_TIMEOUT_TIME);
2079 		lacp_periodic_sm(portp);
2080 		break;
2081 
2082 	case LACP_DISABLED:
2083 		/*
2084 		 * This is the normal state for recv_sm when LACP_OFF
2085 		 * is set or the NIC is in half duplex mode.
2086 		 */
2087 		lacp_port_unselect(portp);
2088 		record_Default(portp);
2089 		pl->PartnerOperPortState.bit.aggregation = B_FALSE;
2090 		pl->ActorOperPortState.bit.expired = B_FALSE;
2091 		break;
2092 
2093 	case LACP_DEFAULTED:
2094 		/*
2095 		 * Current_while_timer expired a second time.
2096 		 */
2097 		update_default_selected(portp);
2098 		record_Default(portp);	/* overwrite Partner Oper val */
2099 		pl->ActorOperPortState.bit.expired = B_FALSE;
2100 		pl->PartnerOperPortState.bit.sync = B_TRUE;
2101 
2102 		lacp_selection_logic(portp);
2103 		lacp_mux_sm(portp);
2104 		break;
2105 
2106 	case LACP_CURRENT:
2107 		/*
2108 		 * Reception of LACPDU
2109 		 */
2110 
2111 		if (!lacp) /* no LACPDU so current_while_timer popped */
2112 			break;
2113 
2114 		AGGR_LACP_DBG(("lacp_receive_sm: (%d): LACPDU received:\n",
2115 		    portp->lp_linkid));
2116 
2117 		/*
2118 		 * Validate Actor_Information_Length,
2119 		 * Partner_Information_Length, Collector_Information_Length,
2120 		 * and Terminator_Length fields.
2121 		 */
2122 		if (!valid_lacp_pdu(portp, lacp)) {
2123 			AGGR_LACP_DBG(("lacp_receive_sm (%d): "
2124 			    "Invalid LACPDU received\n",
2125 			    portp->lp_linkid));
2126 			break;
2127 		}
2128 
2129 		save_activity = pl->PartnerOperPortState.bit.activity;
2130 		selected_updated = update_selected(portp, lacp);
2131 		update_NTT(portp, lacp);
2132 		sync_updated = record_PDU(portp, lacp);
2133 
2134 		pl->ActorOperPortState.bit.expired = B_FALSE;
2135 
2136 		if (selected_updated) {
2137 			lacp_selection_logic(portp);
2138 			lacp_mux_sm(portp);
2139 		} else if (sync_updated) {
2140 			lacp_mux_sm(portp);
2141 		}
2142 
2143 		/*
2144 		 * If the periodic timer value bit has been modified
2145 		 * or the partner activity bit has been changed then
2146 		 * we need to respectively:
2147 		 *  - restart the timer with the proper timeout value.
2148 		 *  - possibly enable/disable transmission of LACPDUs.
2149 		 */
2150 		if ((pl->PartnerOperPortState.bit.timeout &&
2151 		    (pl->periodic_timer.val != FAST_PERIODIC_TIME)) ||
2152 		    (!pl->PartnerOperPortState.bit.timeout &&
2153 		    (pl->periodic_timer.val != SLOW_PERIODIC_TIME)) ||
2154 		    (pl->PartnerOperPortState.bit.activity !=
2155 		    save_activity)) {
2156 			lacp_periodic_sm(portp);
2157 		}
2158 
2159 		stop_current_while_timer(portp);
2160 		/* Check if we need to transmit an LACPDU */
2161 		if (pl->NTT)
2162 			lacp_xmit_sm(portp);
2163 		start_current_while_timer(portp, 0);
2164 
2165 		break;
2166 	}
2167 }
2168 
2169 static void
2170 aggr_set_coll_dist(aggr_port_t *portp, boolean_t enable)
2171 {
2172 	mac_perim_handle_t mph;
2173 
2174 	AGGR_LACP_DBG(("AGGR_SET_COLL_DIST_TYPE: (%d) %s\n",
2175 	    portp->lp_linkid, enable ? "ENABLED" : "DISABLED"));
2176 
2177 	mac_perim_enter_by_mh(portp->lp_mh, &mph);
2178 	if (!enable) {
2179 		/*
2180 		 * Turn OFF Collector_Distributor.
2181 		 */
2182 		portp->lp_collector_enabled = B_FALSE;
2183 		aggr_send_port_disable(portp);
2184 		goto done;
2185 	}
2186 
2187 	/*
2188 	 * Turn ON Collector_Distributor.
2189 	 */
2190 
2191 	if (!portp->lp_lacp.sm.lacp_on || (portp->lp_lacp.sm.lacp_on &&
2192 	    (portp->lp_lacp.sm.mux_state == LACP_COLLECTING_DISTRIBUTING))) {
2193 		/* Port is compatible and can be aggregated */
2194 		portp->lp_collector_enabled = B_TRUE;
2195 		aggr_send_port_enable(portp);
2196 	}
2197 
2198 done:
2199 	mac_perim_exit(mph);
2200 }
2201 
2202 /*
2203  * Because the LACP packet processing needs to enter the aggr's mac perimeter
2204  * and that would potentially cause a deadlock with the thread in which the
2205  * grp/port is deleted, we defer the packet process to a worker thread. Here
2206  * we only enqueue the received Marker or LACPDU for later processing.
2207  */
2208 void
2209 aggr_lacp_rx_enqueue(aggr_port_t *portp, mblk_t *dmp)
2210 {
2211 	aggr_grp_t *grp = portp->lp_grp;
2212 	lacp_t	*lacp;
2213 
2214 	dmp->b_rptr += sizeof (struct ether_header);
2215 
2216 	if (MBLKL(dmp) < sizeof (lacp_t)) {
2217 		freemsg(dmp);
2218 		return;
2219 	}
2220 
2221 	lacp = (lacp_t *)dmp->b_rptr;
2222 	if (lacp->subtype != LACP_SUBTYPE && lacp->subtype != MARKER_SUBTYPE) {
2223 		AGGR_LACP_DBG(("aggr_lacp_rx_enqueue: (%d): "
2224 		    "Unknown Slow Protocol type %d\n",
2225 		    portp->lp_linkid, lacp->subtype));
2226 		freemsg(dmp);
2227 		return;
2228 	}
2229 
2230 	mutex_enter(&grp->lg_lacp_lock);
2231 
2232 	/*
2233 	 * If the lg_lacp_done is set, this aggregation is in the process of
2234 	 * being deleted, return directly.
2235 	 */
2236 	if (grp->lg_lacp_done) {
2237 		mutex_exit(&grp->lg_lacp_lock);
2238 		freemsg(dmp);
2239 		return;
2240 	}
2241 
2242 	if (grp->lg_lacp_tail == NULL) {
2243 		grp->lg_lacp_head = grp->lg_lacp_tail = dmp;
2244 	} else {
2245 		grp->lg_lacp_tail->b_next = dmp;
2246 		grp->lg_lacp_tail = dmp;
2247 	}
2248 
2249 	/*
2250 	 * Hold a reference of the port so that the port won't be freed when it
2251 	 * is removed from the aggr. The b_prev field is borrowed to save the
2252 	 * port information.
2253 	 */
2254 	AGGR_PORT_REFHOLD(portp);
2255 	dmp->b_prev = (mblk_t *)portp;
2256 	cv_broadcast(&grp->lg_lacp_cv);
2257 	mutex_exit(&grp->lg_lacp_lock);
2258 }
2259 
2260 static void
2261 aggr_lacp_rx(mblk_t *dmp)
2262 {
2263 	aggr_port_t *portp = (aggr_port_t *)dmp->b_prev;
2264 	mac_perim_handle_t mph;
2265 	lacp_t	*lacp;
2266 
2267 	dmp->b_prev = NULL;
2268 
2269 	mac_perim_enter_by_mh(portp->lp_grp->lg_mh, &mph);
2270 	if (portp->lp_closing)
2271 		goto done;
2272 
2273 	lacp = (lacp_t *)dmp->b_rptr;
2274 	switch (lacp->subtype) {
2275 	case LACP_SUBTYPE:
2276 		AGGR_LACP_DBG(("aggr_lacp_rx:(%d): LACPDU received.\n",
2277 		    portp->lp_linkid));
2278 
2279 		if (!portp->lp_lacp.sm.lacp_on) {
2280 			break;
2281 		}
2282 		lacp_receive_sm(portp, lacp);
2283 		break;
2284 
2285 	case MARKER_SUBTYPE:
2286 		AGGR_LACP_DBG(("aggr_lacp_rx:(%d): Marker Packet received.\n",
2287 		    portp->lp_linkid));
2288 
2289 		if (receive_marker_pdu(portp, dmp) != 0)
2290 			break;
2291 
2292 		(void) mac_tx(portp->lp_mch, dmp, 0, MAC_DROP_ON_NO_DESC, NULL);
2293 		mac_perim_exit(mph);
2294 		AGGR_PORT_REFRELE(portp);
2295 		return;
2296 	}
2297 
2298 done:
2299 	mac_perim_exit(mph);
2300 	AGGR_PORT_REFRELE(portp);
2301 	freemsg(dmp);
2302 }
2303 
2304 void
2305 aggr_lacp_rx_thread(void *arg)
2306 {
2307 	callb_cpr_t	cprinfo;
2308 	aggr_grp_t	*grp = (aggr_grp_t *)arg;
2309 	aggr_port_t	*port;
2310 	mblk_t		*mp, *nextmp;
2311 
2312 	CALLB_CPR_INIT(&cprinfo, &grp->lg_lacp_lock, callb_generic_cpr,
2313 	    "aggr_lacp_rx_thread");
2314 
2315 	mutex_enter(&grp->lg_lacp_lock);
2316 
2317 	/*
2318 	 * Quit the thread if the grp is deleted.
2319 	 */
2320 	while (!grp->lg_lacp_done) {
2321 		if ((mp = grp->lg_lacp_head) == NULL) {
2322 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
2323 			cv_wait(&grp->lg_lacp_cv, &grp->lg_lacp_lock);
2324 			CALLB_CPR_SAFE_END(&cprinfo, &grp->lg_lacp_lock);
2325 			continue;
2326 		}
2327 
2328 		grp->lg_lacp_head = grp->lg_lacp_tail = NULL;
2329 		mutex_exit(&grp->lg_lacp_lock);
2330 
2331 		while (mp != NULL) {
2332 			nextmp = mp->b_next;
2333 			mp->b_next = NULL;
2334 			aggr_lacp_rx(mp);
2335 			mp = nextmp;
2336 		}
2337 		mutex_enter(&grp->lg_lacp_lock);
2338 	}
2339 
2340 	/*
2341 	 * The grp is being destroyed, simply free all of the LACP messages
2342 	 * left in the queue which did not have the chance to be processed.
2343 	 * We cannot use freemsgchain() here since we need to clear the
2344 	 * b_prev field.
2345 	 */
2346 	while ((mp = grp->lg_lacp_head) != NULL) {
2347 		port = (aggr_port_t *)mp->b_prev;
2348 		AGGR_PORT_REFRELE(port);
2349 		nextmp = mp->b_next;
2350 		mp->b_next = NULL;
2351 		mp->b_prev = NULL;
2352 		freemsg(mp);
2353 		mp = nextmp;
2354 	}
2355 
2356 	grp->lg_lacp_head = grp->lg_lacp_tail = NULL;
2357 	grp->lg_lacp_rx_thread = NULL;
2358 	cv_broadcast(&grp->lg_lacp_cv);
2359 	CALLB_CPR_EXIT(&cprinfo);
2360 	thread_exit();
2361 }
2362