ieee8023ad_lacp_sm_mux.c revision 1.1.2.2 1 /* $NetBSD: ieee8023ad_lacp_sm_mux.c,v 1.1.2.2 2005/03/19 08:36:35 yamt Exp $ */
2
3 /*-
4 * Copyright (c)2005 YAMAMOTO Takashi,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ieee8023ad_lacp_sm_mux.c,v 1.1.2.2 2005/03/19 08:36:35 yamt Exp $");
31
32 #include <sys/param.h>
33 #include <sys/mbuf.h>
34 #include <sys/systm.h>
35
36 #include <net/if.h>
37 #include <net/if_ether.h>
38
39 #include <net/agr/ieee8023_slowprotocols.h>
40 #include <net/agr/ieee8023_tlv.h>
41 #include <net/agr/ieee8023ad_lacp.h>
42 #include <net/agr/ieee8023ad_lacp_impl.h>
43 #include <net/agr/ieee8023ad_lacp_sm.h>
44 #include <net/agr/ieee8023ad_lacp_debug.h>
45
46 /* mux machine */
47
48 void
49 lacp_sm_mux(struct lacp_port *lp)
50 {
51 enum lacp_mux_state new_state;
52 boolean_t p_sync =
53 (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
54 boolean_t p_collecting =
55 (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
56 enum lacp_selected selected = lp->lp_selected;
57 struct lacp_aggregator *la;
58
59 /* LACP_DPRINTF((lp, "%s: state %d\n", __func__, lp->lp_mux_state)); */
60
61 re_eval:
62 la = lp->lp_aggregator;
63 KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL);
64 new_state = lp->lp_mux_state;
65 switch (lp->lp_mux_state) {
66 case LACP_MUX_DETACHED:
67 if (selected != LACP_UNSELECTED) {
68 new_state = LACP_MUX_WAITING;
69 }
70 break;
71 case LACP_MUX_WAITING:
72 KASSERT(la->la_pending > 0 ||
73 !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE));
74 if (selected == LACP_SELECTED && la->la_pending == 0) {
75 new_state = LACP_MUX_ATTACHED;
76 } else if (selected == LACP_UNSELECTED) {
77 new_state = LACP_MUX_DETACHED;
78 }
79 break;
80 case LACP_MUX_ATTACHED:
81 if (selected == LACP_SELECTED && p_sync) {
82 new_state = LACP_MUX_COLLECTING;
83 } else if (selected != LACP_SELECTED) {
84 new_state = LACP_MUX_DETACHED;
85 }
86 break;
87 case LACP_MUX_COLLECTING:
88 if (selected == LACP_SELECTED && p_sync && p_collecting) {
89 new_state = LACP_MUX_DISTRIBUTING;
90 } else if (selected != LACP_SELECTED || !p_sync) {
91 new_state = LACP_MUX_ATTACHED;
92 }
93 break;
94 case LACP_MUX_DISTRIBUTING:
95 if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
96 new_state = LACP_MUX_COLLECTING;
97 }
98 break;
99 default:
100 panic("%s: unknown state", __func__);
101 }
102
103 if (lp->lp_mux_state == new_state) {
104 return;
105 }
106
107 switch (new_state) {
108 case LACP_MUX_DETACHED:
109 lp->lp_state &= ~LACP_STATE_SYNC;
110 lacp_disable_distributing(lp);
111 lacp_disable_collecting(lp);
112 lacp_sm_assert_ntt(lp);
113 /* cancel timer */
114 if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
115 KASSERT(la->la_pending > 0);
116 la->la_pending--;
117 }
118 LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
119 lacp_unselect(lp);
120 break;
121 case LACP_MUX_WAITING:
122 LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
123 LACP_AGGREGATE_WAIT_TIME);
124 la->la_pending++;
125 break;
126 case LACP_MUX_ATTACHED:
127 lp->lp_state |= LACP_STATE_SYNC;
128 lacp_disable_collecting(lp);
129 lacp_sm_assert_ntt(lp);
130 break;
131 case LACP_MUX_COLLECTING:
132 lacp_enable_collecting(lp);
133 lp->lp_state |= LACP_STATE_COLLECTING;
134 lacp_disable_distributing(lp);
135 lacp_sm_assert_ntt(lp);
136 break;
137 case LACP_MUX_DISTRIBUTING:
138 lacp_enable_distributing(lp);
139 break;
140 default:
141 panic("%s: unknown state", __func__);
142 }
143
144 LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
145
146 lp->lp_mux_state = new_state;
147 goto re_eval;
148 }
149
150 void
151 lacp_sm_mux_timer(struct lacp_port *lp)
152 {
153 struct lacp_aggregator *la = lp->lp_aggregator;
154 #if defined(LACP_DEBUG)
155 char buf[LACP_LAGIDSTR_MAX+1];
156 #endif
157
158 KASSERT(la);
159 KASSERT(la->la_pending > 0);
160
161 LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
162 lacp_format_lagid(&la->la_actor, &la->la_partner,
163 buf, sizeof(buf)),
164 la->la_pending, la->la_pending - 1));
165
166 la->la_pending--;
167 }
168