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