xref: /freebsd/sys/net/ieee8023ad_lacp.h (revision 29fc4075e69fd27de0cded313ac6000165d99f8b)
1 /*	$NetBSD: ieee8023ad_impl.h,v 1.2 2005/12/10 23:21:39 elad Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c)2005 YAMAMOTO Takashi,
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  * $FreeBSD$
31  */
32 
33 /*
34  * IEEE802.3ad LACP
35  *
36  * implementation details.
37  */
38 
39 #define	LACP_TIMER_CURRENT_WHILE	0
40 #define	LACP_TIMER_PERIODIC		1
41 #define	LACP_TIMER_WAIT_WHILE		2
42 #define	LACP_NTIMER			3
43 
44 #define	LACP_TIMER_ARM(port, timer, val) \
45 	(port)->lp_timer[(timer)] = (val)
46 #define	LACP_TIMER_DISARM(port, timer) \
47 	(port)->lp_timer[(timer)] = 0
48 #define	LACP_TIMER_ISARMED(port, timer) \
49 	((port)->lp_timer[(timer)] > 0)
50 
51 /*
52  * IEEE802.3ad LACP
53  *
54  * protocol definitions.
55  */
56 
57 #define	LACP_STATE_ACTIVITY	(1<<0)
58 #define	LACP_STATE_TIMEOUT	(1<<1)
59 #define	LACP_STATE_AGGREGATION	(1<<2)
60 #define	LACP_STATE_SYNC		(1<<3)
61 #define	LACP_STATE_COLLECTING	(1<<4)
62 #define	LACP_STATE_DISTRIBUTING	(1<<5)
63 #define	LACP_STATE_DEFAULTED	(1<<6)
64 #define	LACP_STATE_EXPIRED	(1<<7)
65 
66 #define	LACP_PORT_NTT		0x00000001
67 #define	LACP_PORT_MARK		0x00000002
68 
69 #define	LACP_STATE_BITS		\
70 	"\020"			\
71 	"\001ACTIVITY"		\
72 	"\002TIMEOUT"		\
73 	"\003AGGREGATION"	\
74 	"\004SYNC"		\
75 	"\005COLLECTING"	\
76 	"\006DISTRIBUTING"	\
77 	"\007DEFAULTED"		\
78 	"\010EXPIRED"
79 
80 #ifdef _KERNEL
81 /*
82  * IEEE802.3 slow protocols
83  *
84  * protocol (on-wire) definitions.
85  *
86  * XXX should be elsewhere.
87  */
88 
89 #define	SLOWPROTOCOLS_SUBTYPE_LACP	1
90 #define	SLOWPROTOCOLS_SUBTYPE_MARKER	2
91 
92 struct slowprothdr {
93 	uint8_t		sph_subtype;
94 	uint8_t		sph_version;
95 } __packed;
96 
97 /*
98  * TLV on-wire structure.
99  */
100 
101 struct tlvhdr {
102 	uint8_t		tlv_type;
103 	uint8_t		tlv_length;
104 	/* uint8_t tlv_value[]; */
105 } __packed;
106 
107 /*
108  * ... and our implementation.
109  */
110 
111 #define	TLV_SET(tlv, type, length) \
112 	do { \
113 		(tlv)->tlv_type = (type); \
114 		(tlv)->tlv_length = sizeof(*tlv) + (length); \
115 	} while (/*CONSTCOND*/0)
116 
117 struct tlv_template {
118 	uint8_t			tmpl_type;
119 	uint8_t			tmpl_length;
120 };
121 
122 struct lacp_systemid {
123 	uint16_t		lsi_prio;
124 	uint8_t			lsi_mac[6];
125 } __packed;
126 
127 struct lacp_portid {
128 	uint16_t		lpi_prio;
129 	uint16_t		lpi_portno;
130 } __packed;
131 
132 struct lacp_peerinfo {
133 	struct lacp_systemid	lip_systemid;
134 	uint16_t		lip_key;
135 	struct lacp_portid	lip_portid;
136 	uint8_t			lip_state;
137 	uint8_t			lip_resv[3];
138 } __packed;
139 
140 struct lacp_collectorinfo {
141 	uint16_t		lci_maxdelay;
142 	uint8_t			lci_resv[12];
143 } __packed;
144 
145 struct lacpdu {
146 	struct ether_header	ldu_eh;
147 	struct slowprothdr	ldu_sph;
148 
149 	struct tlvhdr		ldu_tlv_actor;
150 	struct lacp_peerinfo	ldu_actor;
151 	struct tlvhdr		ldu_tlv_partner;
152 	struct lacp_peerinfo	ldu_partner;
153 	struct tlvhdr		ldu_tlv_collector;
154 	struct lacp_collectorinfo ldu_collector;
155 	struct tlvhdr		ldu_tlv_term;
156 	uint8_t			ldu_resv[50];
157 } __packed;
158 
159 /*
160  * IEEE802.3ad marker protocol
161  *
162  * protocol (on-wire) definitions.
163  */
164 struct lacp_markerinfo {
165 	uint16_t		mi_rq_port;
166 	uint8_t			mi_rq_system[ETHER_ADDR_LEN];
167 	uint32_t		mi_rq_xid;
168 	uint8_t			mi_pad[2];
169 } __packed;
170 
171 struct markerdu {
172 	struct ether_header	mdu_eh;
173 	struct slowprothdr	mdu_sph;
174 
175 	struct tlvhdr		mdu_tlv;
176 	struct lacp_markerinfo	mdu_info;
177 	struct tlvhdr		mdu_tlv_term;
178 	uint8_t			mdu_resv[90];
179 } __packed;
180 
181 #define	MARKER_TYPE_INFO	0x01
182 #define	MARKER_TYPE_RESPONSE	0x02
183 
184 enum lacp_selected {
185 	LACP_UNSELECTED,
186 	LACP_STANDBY,	/* not used in this implementation */
187 	LACP_SELECTED,
188 };
189 
190 enum lacp_mux_state {
191 	LACP_MUX_DETACHED,
192 	LACP_MUX_WAITING,
193 	LACP_MUX_ATTACHED,
194 	LACP_MUX_COLLECTING,
195 	LACP_MUX_DISTRIBUTING,
196 };
197 
198 #define	LACP_MAX_PORTS		32
199 
200 struct lacp_numa {
201 	int			count;
202 	struct lacp_port	*map[LACP_MAX_PORTS];
203 };
204 
205 struct lacp_portmap {
206 	int			pm_count;
207 	int			pm_num_dom;
208 	struct lacp_numa	pm_numa[MAXMEMDOM];
209 	struct lacp_port	*pm_map[LACP_MAX_PORTS];
210 };
211 
212 struct lacp_port {
213 	TAILQ_ENTRY(lacp_port)	lp_dist_q;
214 	LIST_ENTRY(lacp_port)	lp_next;
215 	struct lacp_softc	*lp_lsc;
216 	struct lagg_port	*lp_lagg;
217 	struct ifnet		*lp_ifp;
218 	struct lacp_peerinfo	lp_partner;
219 	struct lacp_peerinfo	lp_actor;
220 	struct lacp_markerinfo	lp_marker;
221 #define	lp_state	lp_actor.lip_state
222 #define	lp_key		lp_actor.lip_key
223 #define	lp_systemid	lp_actor.lip_systemid
224 	struct timeval		lp_last_lacpdu;
225 	struct timeval		lp_last_lacpdu_rx;
226 	int			lp_lacpdu_sent;
227 	enum lacp_mux_state	lp_mux_state;
228 	enum lacp_selected	lp_selected;
229 	int			lp_flags;
230 	u_int			lp_media; /* XXX redundant */
231 	int			lp_timer[LACP_NTIMER];
232 	struct ifmultiaddr	*lp_ifma;
233 
234 	struct lacp_aggregator	*lp_aggregator;
235 };
236 
237 struct lacp_aggregator {
238 	TAILQ_ENTRY(lacp_aggregator)	la_q;
239 	int			la_refcnt; /* num of ports which selected us */
240 	int			la_nports; /* num of distributing ports  */
241 	TAILQ_HEAD(, lacp_port)	la_ports; /* distributing ports */
242 	struct lacp_peerinfo	la_partner;
243 	struct lacp_peerinfo	la_actor;
244 	int			la_pending; /* number of ports in wait_while */
245 };
246 
247 struct lacp_softc {
248 	struct lagg_softc	*lsc_softc;
249 	struct mtx		lsc_mtx;
250 	struct lacp_aggregator	*lsc_active_aggregator;
251 	TAILQ_HEAD(, lacp_aggregator) lsc_aggregators;
252 	boolean_t		lsc_suppress_distributing;
253 	struct callout		lsc_transit_callout;
254 	struct callout		lsc_callout;
255 	LIST_HEAD(, lacp_port)	lsc_ports;
256 	struct lacp_portmap	lsc_pmap[2];
257 	volatile u_int		lsc_activemap;
258 	u_int32_t		lsc_hashkey;
259 	struct {
260 		u_int32_t	lsc_rx_test;
261 		u_int32_t	lsc_tx_test;
262 	} lsc_debug;
263 	u_int32_t		lsc_strict_mode;
264 	boolean_t		lsc_fast_timeout; /* if set, fast timeout */
265 };
266 
267 #define	LACP_TYPE_ACTORINFO	1
268 #define	LACP_TYPE_PARTNERINFO	2
269 #define	LACP_TYPE_COLLECTORINFO	3
270 
271 /* timeout values (in sec) */
272 #define	LACP_FAST_PERIODIC_TIME		(1)
273 #define	LACP_SLOW_PERIODIC_TIME		(30)
274 #define	LACP_SHORT_TIMEOUT_TIME		(3 * LACP_FAST_PERIODIC_TIME)
275 #define	LACP_LONG_TIMEOUT_TIME		(3 * LACP_SLOW_PERIODIC_TIME)
276 #define	LACP_CHURN_DETECTION_TIME	(60)
277 #define	LACP_AGGREGATE_WAIT_TIME	(2)
278 #define	LACP_TRANSIT_DELAY		3000	/* in msec */
279 
280 #define	LACP_STATE_EQ(s1, s2, mask)	\
281 	((((s1) ^ (s2)) & (mask)) == 0)
282 
283 #define	LACP_SYS_PRI(peer)	(peer).lip_systemid.lsi_prio
284 
285 #define	LACP_PORT(_lp)	((struct lacp_port *)(_lp)->lp_psc)
286 #define	LACP_SOFTC(_sc)	((struct lacp_softc *)(_sc)->sc_psc)
287 
288 #define LACP_LOCK_INIT(_lsc)		mtx_init(&(_lsc)->lsc_mtx, \
289 					    "lacp mtx", NULL, MTX_DEF)
290 #define LACP_LOCK_DESTROY(_lsc)		mtx_destroy(&(_lsc)->lsc_mtx)
291 #define LACP_LOCK(_lsc)			mtx_lock(&(_lsc)->lsc_mtx)
292 #define LACP_UNLOCK(_lsc)		mtx_unlock(&(_lsc)->lsc_mtx)
293 #define LACP_LOCK_ASSERT(_lsc)		mtx_assert(&(_lsc)->lsc_mtx, MA_OWNED)
294 
295 struct mbuf	*lacp_input(struct lagg_port *, struct mbuf *);
296 struct lagg_port *lacp_select_tx_port(struct lagg_softc *, struct mbuf *,
297     int *);
298 struct lagg_port *lacp_select_tx_port_by_hash(struct lagg_softc *, uint32_t,
299     uint8_t, int *);
300 void		lacp_attach(struct lagg_softc *);
301 void		lacp_detach(void *);
302 void		lacp_init(struct lagg_softc *);
303 void		lacp_stop(struct lagg_softc *);
304 int		lacp_port_create(struct lagg_port *);
305 void		lacp_port_destroy(struct lagg_port *);
306 void		lacp_linkstate(struct lagg_port *);
307 void		lacp_req(struct lagg_softc *, void *);
308 void		lacp_portreq(struct lagg_port *, void *);
309 
310 static __inline int
311 lacp_isactive(struct lagg_port *lgp)
312 {
313 	struct lacp_port *lp = LACP_PORT(lgp);
314 	struct lacp_softc *lsc = lp->lp_lsc;
315 	struct lacp_aggregator *la = lp->lp_aggregator;
316 
317 	/* This port is joined to the active aggregator */
318 	if (la != NULL && la == lsc->lsc_active_aggregator)
319 		return (1);
320 
321 	return (0);
322 }
323 
324 static __inline int
325 lacp_iscollecting(struct lagg_port *lgp)
326 {
327 	struct lacp_port *lp = LACP_PORT(lgp);
328 
329 	return ((lp->lp_state & LACP_STATE_COLLECTING) != 0);
330 }
331 
332 static __inline int
333 lacp_isdistributing(struct lagg_port *lgp)
334 {
335 	struct lacp_port *lp = LACP_PORT(lgp);
336 
337 	return ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0);
338 }
339 
340 /* following constants don't include terminating NUL */
341 #define	LACP_MACSTR_MAX		(2*6 + 5)
342 #define	LACP_SYSTEMPRIOSTR_MAX	(4)
343 #define	LACP_SYSTEMIDSTR_MAX	(LACP_SYSTEMPRIOSTR_MAX + 1 + LACP_MACSTR_MAX)
344 #define	LACP_PORTPRIOSTR_MAX	(4)
345 #define	LACP_PORTNOSTR_MAX	(4)
346 #define	LACP_PORTIDSTR_MAX	(LACP_PORTPRIOSTR_MAX + 1 + LACP_PORTNOSTR_MAX)
347 #define	LACP_KEYSTR_MAX		(4)
348 #define	LACP_PARTNERSTR_MAX	\
349 	(1 + LACP_SYSTEMIDSTR_MAX + 1 + LACP_KEYSTR_MAX + 1 \
350 	+ LACP_PORTIDSTR_MAX + 1)
351 #define	LACP_LAGIDSTR_MAX	\
352 	(1 + LACP_PARTNERSTR_MAX + 1 + LACP_PARTNERSTR_MAX + 1)
353 #define	LACP_STATESTR_MAX	(255) /* XXX */
354 #endif	/* _KERNEL */
355