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