xref: /freebsd/sys/net/ieee8023ad_lacp.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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 	int			lp_lacpdu_sent;
226 	enum lacp_mux_state	lp_mux_state;
227 	enum lacp_selected	lp_selected;
228 	int			lp_flags;
229 	u_int			lp_media; /* XXX redundant */
230 	int			lp_timer[LACP_NTIMER];
231 	struct ifmultiaddr	*lp_ifma;
232 
233 	struct lacp_aggregator	*lp_aggregator;
234 };
235 
236 struct lacp_aggregator {
237 	TAILQ_ENTRY(lacp_aggregator)	la_q;
238 	int			la_refcnt; /* num of ports which selected us */
239 	int			la_nports; /* num of distributing ports  */
240 	TAILQ_HEAD(, lacp_port)	la_ports; /* distributing ports */
241 	struct lacp_peerinfo	la_partner;
242 	struct lacp_peerinfo	la_actor;
243 	int			la_pending; /* number of ports in wait_while */
244 };
245 
246 struct lacp_softc {
247 	struct lagg_softc	*lsc_softc;
248 	struct mtx		lsc_mtx;
249 	struct lacp_aggregator	*lsc_active_aggregator;
250 	TAILQ_HEAD(, lacp_aggregator) lsc_aggregators;
251 	boolean_t		lsc_suppress_distributing;
252 	struct callout		lsc_transit_callout;
253 	struct callout		lsc_callout;
254 	LIST_HEAD(, lacp_port)	lsc_ports;
255 	struct lacp_portmap	lsc_pmap[2];
256 	volatile u_int		lsc_activemap;
257 	u_int32_t		lsc_hashkey;
258 	struct {
259 		u_int32_t	lsc_rx_test;
260 		u_int32_t	lsc_tx_test;
261 	} lsc_debug;
262 	u_int32_t		lsc_strict_mode;
263 	boolean_t		lsc_fast_timeout; /* if set, fast timeout */
264 };
265 
266 #define	LACP_TYPE_ACTORINFO	1
267 #define	LACP_TYPE_PARTNERINFO	2
268 #define	LACP_TYPE_COLLECTORINFO	3
269 
270 /* timeout values (in sec) */
271 #define	LACP_FAST_PERIODIC_TIME		(1)
272 #define	LACP_SLOW_PERIODIC_TIME		(30)
273 #define	LACP_SHORT_TIMEOUT_TIME		(3 * LACP_FAST_PERIODIC_TIME)
274 #define	LACP_LONG_TIMEOUT_TIME		(3 * LACP_SLOW_PERIODIC_TIME)
275 #define	LACP_CHURN_DETECTION_TIME	(60)
276 #define	LACP_AGGREGATE_WAIT_TIME	(2)
277 #define	LACP_TRANSIT_DELAY		3000	/* in msec */
278 
279 #define	LACP_STATE_EQ(s1, s2, mask)	\
280 	((((s1) ^ (s2)) & (mask)) == 0)
281 
282 #define	LACP_SYS_PRI(peer)	(peer).lip_systemid.lsi_prio
283 
284 #define	LACP_PORT(_lp)	((struct lacp_port *)(_lp)->lp_psc)
285 #define	LACP_SOFTC(_sc)	((struct lacp_softc *)(_sc)->sc_psc)
286 
287 #define LACP_LOCK_INIT(_lsc)		mtx_init(&(_lsc)->lsc_mtx, \
288 					    "lacp mtx", NULL, MTX_DEF)
289 #define LACP_LOCK_DESTROY(_lsc)		mtx_destroy(&(_lsc)->lsc_mtx)
290 #define LACP_LOCK(_lsc)			mtx_lock(&(_lsc)->lsc_mtx)
291 #define LACP_UNLOCK(_lsc)		mtx_unlock(&(_lsc)->lsc_mtx)
292 #define LACP_LOCK_ASSERT(_lsc)		mtx_assert(&(_lsc)->lsc_mtx, MA_OWNED)
293 
294 struct mbuf	*lacp_input(struct lagg_port *, struct mbuf *);
295 struct lagg_port *lacp_select_tx_port(struct lagg_softc *, struct mbuf *);
296 #if defined(RATELIMIT) || defined(KERN_TLS)
297 struct lagg_port *lacp_select_tx_port_by_hash(struct lagg_softc *, uint32_t);
298 #endif
299 void		lacp_attach(struct lagg_softc *);
300 void		lacp_detach(void *);
301 void		lacp_init(struct lagg_softc *);
302 void		lacp_stop(struct lagg_softc *);
303 int		lacp_port_create(struct lagg_port *);
304 void		lacp_port_destroy(struct lagg_port *);
305 void		lacp_linkstate(struct lagg_port *);
306 void		lacp_req(struct lagg_softc *, void *);
307 void		lacp_portreq(struct lagg_port *, void *);
308 
309 static __inline int
310 lacp_isactive(struct lagg_port *lgp)
311 {
312 	struct lacp_port *lp = LACP_PORT(lgp);
313 	struct lacp_softc *lsc = lp->lp_lsc;
314 	struct lacp_aggregator *la = lp->lp_aggregator;
315 
316 	/* This port is joined to the active aggregator */
317 	if (la != NULL && la == lsc->lsc_active_aggregator)
318 		return (1);
319 
320 	return (0);
321 }
322 
323 static __inline int
324 lacp_iscollecting(struct lagg_port *lgp)
325 {
326 	struct lacp_port *lp = LACP_PORT(lgp);
327 
328 	return ((lp->lp_state & LACP_STATE_COLLECTING) != 0);
329 }
330 
331 static __inline int
332 lacp_isdistributing(struct lagg_port *lgp)
333 {
334 	struct lacp_port *lp = LACP_PORT(lgp);
335 
336 	return ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0);
337 }
338 
339 /* following constants don't include terminating NUL */
340 #define	LACP_MACSTR_MAX		(2*6 + 5)
341 #define	LACP_SYSTEMPRIOSTR_MAX	(4)
342 #define	LACP_SYSTEMIDSTR_MAX	(LACP_SYSTEMPRIOSTR_MAX + 1 + LACP_MACSTR_MAX)
343 #define	LACP_PORTPRIOSTR_MAX	(4)
344 #define	LACP_PORTNOSTR_MAX	(4)
345 #define	LACP_PORTIDSTR_MAX	(LACP_PORTPRIOSTR_MAX + 1 + LACP_PORTNOSTR_MAX)
346 #define	LACP_KEYSTR_MAX		(4)
347 #define	LACP_PARTNERSTR_MAX	\
348 	(1 + LACP_SYSTEMIDSTR_MAX + 1 + LACP_KEYSTR_MAX + 1 \
349 	+ LACP_PORTIDSTR_MAX + 1)
350 #define	LACP_LAGIDSTR_MAX	\
351 	(1 + LACP_PARTNERSTR_MAX + 1 + LACP_PARTNERSTR_MAX + 1)
352 #define	LACP_STATESTR_MAX	(255) /* XXX */
353 #endif	/* _KERNEL */
354