1 /*        $NetBSD: ieee8023ad_lacp_sm_mux.c,v 1.5 2021/11/30 01:17:02 yamaguchi 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.5 2021/11/30 01:17:02 yamaguchi Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/callout.h>
34 #include <sys/mbuf.h>
35 #include <sys/systm.h>
36 
37 #include <net/if.h>
38 #include <net/if_ether.h>
39 #include <net/ether_slowprotocols.h>
40 
41 #include <net/agr/ieee8023_tlv.h>
42 #include <net/agr/ieee8023ad_lacp.h>
43 #include <net/agr/ieee8023ad_lacp_impl.h>
44 #include <net/agr/ieee8023ad_lacp_sm.h>
45 #include <net/agr/ieee8023ad_lacp_debug.h>
46 
47 /* mux machine */
48 
49 void
lacp_sm_mux(struct lacp_port * lp)50 lacp_sm_mux(struct lacp_port *lp)
51 {
52           enum lacp_mux_state new_state;
53           bool p_sync =
54                         (lp->lp_partner.lip_state & LACP_STATE_SYNC) != 0;
55           bool p_collecting =
56               (lp->lp_partner.lip_state & LACP_STATE_COLLECTING) != 0;
57           enum lacp_selected selected = lp->lp_selected;
58           struct lacp_aggregator *la;
59 
60           /* LACP_DPRINTF((lp, "%s: state %d\n", __func__, lp->lp_mux_state)); */
61 
62 re_eval:
63           la = lp->lp_aggregator;
64           KASSERT(lp->lp_mux_state == LACP_MUX_DETACHED || la != NULL);
65           new_state = lp->lp_mux_state;
66           switch (lp->lp_mux_state) {
67           case LACP_MUX_DETACHED:
68                     if (selected != LACP_UNSELECTED) {
69                               new_state = LACP_MUX_WAITING;
70                     }
71                     break;
72           case LACP_MUX_WAITING:
73                     KASSERT(la->la_pending > 0 ||
74                         !LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE));
75                     if (selected == LACP_SELECTED && la->la_pending == 0) {
76                               new_state = LACP_MUX_ATTACHED;
77                     } else if (selected == LACP_UNSELECTED) {
78                               new_state = LACP_MUX_DETACHED;
79                     }
80                     break;
81           case LACP_MUX_ATTACHED:
82                     if (selected == LACP_SELECTED && p_sync) {
83                               new_state = LACP_MUX_COLLECTING;
84                     } else if (selected != LACP_SELECTED) {
85                               new_state = LACP_MUX_DETACHED;
86                     }
87                     break;
88           case LACP_MUX_COLLECTING:
89                     if (selected == LACP_SELECTED && p_sync && p_collecting) {
90                               new_state = LACP_MUX_DISTRIBUTING;
91                     } else if (selected != LACP_SELECTED || !p_sync) {
92                               new_state = LACP_MUX_ATTACHED;
93                     }
94                     break;
95           case LACP_MUX_DISTRIBUTING:
96                     if (selected != LACP_SELECTED || !p_sync || !p_collecting) {
97                               new_state = LACP_MUX_COLLECTING;
98                     }
99                     break;
100           default:
101                     panic("%s: unknown state", __func__);
102           }
103 
104           if (lp->lp_mux_state == new_state) {
105                     return;
106           }
107 
108           switch (new_state) {
109           case LACP_MUX_DETACHED:
110                     lp->lp_state &= ~LACP_STATE_SYNC;
111                     lacp_disable_distributing(lp);
112                     lacp_disable_collecting(lp);
113                     lacp_sm_assert_ntt(lp);
114                     /* cancel timer */
115                     if (LACP_TIMER_ISARMED(lp, LACP_TIMER_WAIT_WHILE)) {
116                               KASSERT(la->la_pending > 0);
117                               la->la_pending--;
118                     }
119                     LACP_TIMER_DISARM(lp, LACP_TIMER_WAIT_WHILE);
120                     lacp_unselect(lp);
121                     break;
122           case LACP_MUX_WAITING:
123                     LACP_TIMER_ARM(lp, LACP_TIMER_WAIT_WHILE,
124                         LACP_AGGREGATE_WAIT_TIME);
125                     la->la_pending++;
126                     break;
127           case LACP_MUX_ATTACHED:
128                     lp->lp_state |= LACP_STATE_SYNC;
129                     lacp_disable_collecting(lp);
130                     lacp_sm_assert_ntt(lp);
131                     break;
132           case LACP_MUX_COLLECTING:
133                     lacp_enable_collecting(lp);
134                     lp->lp_state |= LACP_STATE_COLLECTING;
135                     lacp_disable_distributing(lp);
136                     lacp_sm_assert_ntt(lp);
137                     break;
138           case LACP_MUX_DISTRIBUTING:
139                     lacp_enable_distributing(lp);
140                     break;
141           default:
142                     panic("%s: unknown state", __func__);
143           }
144 
145           LACP_DPRINTF((lp, "mux_state %d -> %d\n", lp->lp_mux_state, new_state));
146 
147           lp->lp_mux_state = new_state;
148           goto re_eval;
149 }
150 
151 void
lacp_sm_mux_timer(struct lacp_port * lp)152 lacp_sm_mux_timer(struct lacp_port *lp)
153 {
154           struct lacp_aggregator *la = lp->lp_aggregator;
155 #if defined(LACP_DEBUG)
156           char buf[LACP_LAGIDSTR_MAX+1];
157 #endif
158 
159           KASSERT(la);
160           KASSERT(la->la_pending > 0);
161 
162           LACP_DPRINTF((lp, "%s: aggregator %s, pending %d -> %d\n", __func__,
163               lacp_format_lagid(&la->la_actor, &la->la_partner,
164               buf, sizeof(buf)),
165               la->la_pending, la->la_pending - 1));
166 
167           la->la_pending--;
168 }
169