1168561Sthompsa/*	$NetBSD: ieee8023ad_impl.h,v 1.2 2005/12/10 23:21:39 elad Exp $	*/
2168561Sthompsa
3168561Sthompsa/*-
4168561Sthompsa * Copyright (c)2005 YAMAMOTO Takashi,
5168561Sthompsa * All rights reserved.
6168561Sthompsa *
7168561Sthompsa * Redistribution and use in source and binary forms, with or without
8168561Sthompsa * modification, are permitted provided that the following conditions
9168561Sthompsa * are met:
10168561Sthompsa * 1. Redistributions of source code must retain the above copyright
11168561Sthompsa *    notice, this list of conditions and the following disclaimer.
12168561Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
13168561Sthompsa *    notice, this list of conditions and the following disclaimer in the
14168561Sthompsa *    documentation and/or other materials provided with the distribution.
15168561Sthompsa *
16168561Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17168561Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18168561Sthompsa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19168561Sthompsa * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20168561Sthompsa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21168561Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22168561Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23168561Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24168561Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25168561Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26168561Sthompsa * SUCH DAMAGE.
27168561Sthompsa *
28168561Sthompsa * $FreeBSD$
29168561Sthompsa */
30168561Sthompsa
31168561Sthompsa/*
32168561Sthompsa * IEEE802.3ad LACP
33168561Sthompsa *
34168561Sthompsa * implementation details.
35168561Sthompsa */
36168561Sthompsa
37168561Sthompsa#define	LACP_TIMER_CURRENT_WHILE	0
38168561Sthompsa#define	LACP_TIMER_PERIODIC		1
39168561Sthompsa#define	LACP_TIMER_WAIT_WHILE		2
40168561Sthompsa#define	LACP_NTIMER			3
41168561Sthompsa
42168561Sthompsa#define	LACP_TIMER_ARM(port, timer, val) \
43168561Sthompsa	(port)->lp_timer[(timer)] = (val)
44168561Sthompsa#define	LACP_TIMER_DISARM(port, timer) \
45168561Sthompsa	(port)->lp_timer[(timer)] = 0
46168561Sthompsa#define	LACP_TIMER_ISARMED(port, timer) \
47168561Sthompsa	((port)->lp_timer[(timer)] > 0)
48168561Sthompsa
49168561Sthompsa/*
50168561Sthompsa * IEEE802.3ad LACP
51168561Sthompsa *
52168561Sthompsa * protocol definitions.
53168561Sthompsa */
54168561Sthompsa
55168561Sthompsa#define	LACP_STATE_ACTIVITY	(1<<0)
56168561Sthompsa#define	LACP_STATE_TIMEOUT	(1<<1)
57168561Sthompsa#define	LACP_STATE_AGGREGATION	(1<<2)
58168561Sthompsa#define	LACP_STATE_SYNC		(1<<3)
59168561Sthompsa#define	LACP_STATE_COLLECTING	(1<<4)
60168561Sthompsa#define	LACP_STATE_DISTRIBUTING	(1<<5)
61168561Sthompsa#define	LACP_STATE_DEFAULTED	(1<<6)
62168561Sthompsa#define	LACP_STATE_EXPIRED	(1<<7)
63168561Sthompsa
64170599Sthompsa#define	LACP_PORT_NTT		0x00000001
65170599Sthompsa#define	LACP_PORT_MARK		0x00000002
66168561Sthompsa
67168561Sthompsa#define	LACP_STATE_BITS		\
68168561Sthompsa	"\020"			\
69168561Sthompsa	"\001ACTIVITY"		\
70168561Sthompsa	"\002TIMEOUT"		\
71168561Sthompsa	"\003AGGREGATION"	\
72168561Sthompsa	"\004SYNC"		\
73168561Sthompsa	"\005COLLECTING"	\
74168561Sthompsa	"\006DISTRIBUTING"	\
75168561Sthompsa	"\007DEFAULTED"		\
76168561Sthompsa	"\010EXPIRED"
77168561Sthompsa
78168561Sthompsa/*
79168561Sthompsa * IEEE802.3 slow protocols
80168561Sthompsa *
81168561Sthompsa * protocol (on-wire) definitions.
82168561Sthompsa *
83168561Sthompsa * XXX should be elsewhere.
84168561Sthompsa */
85168561Sthompsa
86168561Sthompsa#define	SLOWPROTOCOLS_SUBTYPE_LACP	1
87168561Sthompsa#define	SLOWPROTOCOLS_SUBTYPE_MARKER	2
88168561Sthompsa
89168561Sthompsastruct slowprothdr {
90168561Sthompsa	uint8_t		sph_subtype;
91168561Sthompsa	uint8_t		sph_version;
92168561Sthompsa} __packed;
93168561Sthompsa
94168561Sthompsa/*
95168561Sthompsa * TLV on-wire structure.
96168561Sthompsa */
97168561Sthompsa
98168561Sthompsastruct tlvhdr {
99168561Sthompsa	uint8_t		tlv_type;
100168561Sthompsa	uint8_t		tlv_length;
101168561Sthompsa	/* uint8_t tlv_value[]; */
102168561Sthompsa} __packed;
103168561Sthompsa
104168561Sthompsa/*
105168561Sthompsa * ... and our implementation.
106168561Sthompsa */
107168561Sthompsa
108168561Sthompsa#define	TLV_SET(tlv, type, length) \
109168561Sthompsa	do { \
110168561Sthompsa		(tlv)->tlv_type = (type); \
111168561Sthompsa		(tlv)->tlv_length = sizeof(*tlv) + (length); \
112168561Sthompsa	} while (/*CONSTCOND*/0)
113168561Sthompsa
114168561Sthompsastruct tlv_template {
115168561Sthompsa	uint8_t			tmpl_type;
116168561Sthompsa	uint8_t			tmpl_length;
117168561Sthompsa};
118168561Sthompsa
119168561Sthompsastruct lacp_systemid {
120168561Sthompsa	uint16_t		lsi_prio;
121168561Sthompsa	uint8_t			lsi_mac[6];
122168561Sthompsa} __packed;
123168561Sthompsa
124168561Sthompsastruct lacp_portid {
125168561Sthompsa	uint16_t		lpi_prio;
126168561Sthompsa	uint16_t		lpi_portno;
127168561Sthompsa} __packed;
128168561Sthompsa
129168561Sthompsastruct lacp_peerinfo {
130170599Sthompsa	struct lacp_systemid	lip_systemid;
131168561Sthompsa	uint16_t		lip_key;
132170599Sthompsa	struct lacp_portid	lip_portid;
133168561Sthompsa	uint8_t			lip_state;
134168561Sthompsa	uint8_t			lip_resv[3];
135168561Sthompsa} __packed;
136168561Sthompsa
137168561Sthompsastruct lacp_collectorinfo {
138168561Sthompsa	uint16_t		lci_maxdelay;
139168561Sthompsa	uint8_t			lci_resv[12];
140168561Sthompsa} __packed;
141168561Sthompsa
142168561Sthompsastruct lacpdu {
143168561Sthompsa	struct ether_header	ldu_eh;
144168561Sthompsa	struct slowprothdr	ldu_sph;
145168561Sthompsa
146168561Sthompsa	struct tlvhdr		ldu_tlv_actor;
147168561Sthompsa	struct lacp_peerinfo	ldu_actor;
148168561Sthompsa	struct tlvhdr		ldu_tlv_partner;
149168561Sthompsa	struct lacp_peerinfo	ldu_partner;
150168561Sthompsa	struct tlvhdr		ldu_tlv_collector;
151168561Sthompsa	struct lacp_collectorinfo ldu_collector;
152168561Sthompsa	struct tlvhdr		ldu_tlv_term;
153168561Sthompsa	uint8_t			ldu_resv[50];
154168561Sthompsa} __packed;
155168561Sthompsa
156169739Sthompsa/*
157169739Sthompsa * IEEE802.3ad marker protocol
158169739Sthompsa *
159169739Sthompsa * protocol (on-wire) definitions.
160169739Sthompsa */
161169739Sthompsastruct lacp_markerinfo {
162169739Sthompsa	uint16_t		mi_rq_port;
163169739Sthompsa	uint8_t			mi_rq_system[ETHER_ADDR_LEN];
164169739Sthompsa	uint32_t		mi_rq_xid;
165169739Sthompsa	uint8_t			mi_pad[2];
166169739Sthompsa} __packed;
167168561Sthompsa
168169739Sthompsastruct markerdu {
169169739Sthompsa	struct ether_header	mdu_eh;
170169739Sthompsa	struct slowprothdr	mdu_sph;
171169739Sthompsa
172169739Sthompsa	struct tlvhdr		mdu_tlv;
173169739Sthompsa	struct lacp_markerinfo	mdu_info;
174169739Sthompsa	struct tlvhdr		mdu_tlv_term;
175169739Sthompsa	uint8_t			mdu_resv[90];
176169739Sthompsa} __packed;
177169739Sthompsa
178169739Sthompsa#define	MARKER_TYPE_INFO	0x01
179169739Sthompsa#define	MARKER_TYPE_RESPONSE	0x02
180169739Sthompsa
181168561Sthompsaenum lacp_selected {
182168561Sthompsa	LACP_UNSELECTED,
183168561Sthompsa	LACP_STANDBY,	/* not used in this implementation */
184168561Sthompsa	LACP_SELECTED,
185168561Sthompsa};
186168561Sthompsa
187168561Sthompsaenum lacp_mux_state {
188168561Sthompsa	LACP_MUX_DETACHED,
189168561Sthompsa	LACP_MUX_WAITING,
190168561Sthompsa	LACP_MUX_ATTACHED,
191168561Sthompsa	LACP_MUX_COLLECTING,
192168561Sthompsa	LACP_MUX_DISTRIBUTING,
193168561Sthompsa};
194168561Sthompsa
195177274Sthompsa#define	LACP_MAX_PORTS		32
196177274Sthompsa
197177274Sthompsastruct lacp_portmap {
198177274Sthompsa	int			pm_count;
199177274Sthompsa	struct lacp_port	*pm_map[LACP_MAX_PORTS];
200177274Sthompsa};
201177274Sthompsa
202168561Sthompsastruct lacp_port {
203168561Sthompsa	TAILQ_ENTRY(lacp_port)	lp_dist_q;
204168561Sthompsa	LIST_ENTRY(lacp_port)	lp_next;
205168561Sthompsa	struct lacp_softc	*lp_lsc;
206168793Sthompsa	struct lagg_port	*lp_lagg;
207168561Sthompsa	struct ifnet		*lp_ifp;
208168561Sthompsa	struct lacp_peerinfo	lp_partner;
209168561Sthompsa	struct lacp_peerinfo	lp_actor;
210169739Sthompsa	struct lacp_markerinfo	lp_marker;
211168561Sthompsa#define	lp_state	lp_actor.lip_state
212168561Sthompsa#define	lp_key		lp_actor.lip_key
213169739Sthompsa#define	lp_systemid	lp_actor.lip_systemid
214168561Sthompsa	struct timeval		lp_last_lacpdu;
215168561Sthompsa	int			lp_lacpdu_sent;
216168561Sthompsa	enum lacp_mux_state	lp_mux_state;
217168561Sthompsa	enum lacp_selected	lp_selected;
218168561Sthompsa	int			lp_flags;
219168561Sthompsa	u_int			lp_media; /* XXX redundant */
220168561Sthompsa	int			lp_timer[LACP_NTIMER];
221169327Sthompsa	struct ifmultiaddr	*lp_ifma;
222168561Sthompsa
223168561Sthompsa	struct lacp_aggregator	*lp_aggregator;
224168561Sthompsa};
225168561Sthompsa
226168561Sthompsastruct lacp_aggregator {
227168561Sthompsa	TAILQ_ENTRY(lacp_aggregator)	la_q;
228168561Sthompsa	int			la_refcnt; /* num of ports which selected us */
229168561Sthompsa	int			la_nports; /* num of distributing ports  */
230168561Sthompsa	TAILQ_HEAD(, lacp_port)	la_ports; /* distributing ports */
231168561Sthompsa	struct lacp_peerinfo	la_partner;
232168561Sthompsa	struct lacp_peerinfo	la_actor;
233170599Sthompsa	int			la_pending; /* number of ports in wait_while */
234168561Sthompsa};
235168561Sthompsa
236168561Sthompsastruct lacp_softc {
237170599Sthompsa	struct lagg_softc	*lsc_softc;
238177274Sthompsa	struct mtx		lsc_mtx;
239168561Sthompsa	struct lacp_aggregator	*lsc_active_aggregator;
240168561Sthompsa	TAILQ_HEAD(, lacp_aggregator) lsc_aggregators;
241168561Sthompsa	boolean_t		lsc_suppress_distributing;
242168561Sthompsa	struct callout		lsc_transit_callout;
243168561Sthompsa	struct callout		lsc_callout;
244168561Sthompsa	LIST_HEAD(, lacp_port)	lsc_ports;
245177274Sthompsa	struct lacp_portmap	lsc_pmap[2];
246177274Sthompsa	volatile u_int		lsc_activemap;
247168561Sthompsa	u_int32_t		lsc_hashkey;
248253687Sadrian	struct {
249253687Sadrian		u_int32_t	lsc_rx_test;
250253687Sadrian		u_int32_t	lsc_tx_test;
251253687Sadrian	} lsc_debug;
252253687Sadrian	u_int32_t		lsc_strict_mode;
253168561Sthompsa};
254168561Sthompsa
255168561Sthompsa#define	LACP_TYPE_ACTORINFO	1
256168561Sthompsa#define	LACP_TYPE_PARTNERINFO	2
257168561Sthompsa#define	LACP_TYPE_COLLECTORINFO	3
258168561Sthompsa
259168561Sthompsa/* timeout values (in sec) */
260168561Sthompsa#define	LACP_FAST_PERIODIC_TIME		(1)
261168561Sthompsa#define	LACP_SLOW_PERIODIC_TIME		(30)
262168561Sthompsa#define	LACP_SHORT_TIMEOUT_TIME		(3 * LACP_FAST_PERIODIC_TIME)
263168561Sthompsa#define	LACP_LONG_TIMEOUT_TIME		(3 * LACP_SLOW_PERIODIC_TIME)
264168561Sthompsa#define	LACP_CHURN_DETECTION_TIME	(60)
265168561Sthompsa#define	LACP_AGGREGATE_WAIT_TIME	(2)
266169739Sthompsa#define	LACP_TRANSIT_DELAY		3000	/* in msec */
267168561Sthompsa
268168561Sthompsa#define	LACP_STATE_EQ(s1, s2, mask)	\
269168561Sthompsa	((((s1) ^ (s2)) & (mask)) == 0)
270168561Sthompsa
271169741Sthompsa#define	LACP_SYS_PRI(peer)	(peer).lip_systemid.lsi_prio
272169741Sthompsa
273168793Sthompsa#define	LACP_PORT(_lp)	((struct lacp_port *)(_lp)->lp_psc)
274168793Sthompsa#define	LACP_SOFTC(_sc)	((struct lacp_softc *)(_sc)->sc_psc)
275168561Sthompsa
276177274Sthompsa#define LACP_LOCK_INIT(_lsc)		mtx_init(&(_lsc)->lsc_mtx, \
277177289Sthompsa					    "lacp mtx", NULL, MTX_DEF)
278177289Sthompsa#define LACP_LOCK_DESTROY(_lsc)		mtx_destroy(&(_lsc)->lsc_mtx)
279177274Sthompsa#define LACP_LOCK(_lsc)			mtx_lock(&(_lsc)->lsc_mtx)
280177274Sthompsa#define LACP_UNLOCK(_lsc)		mtx_unlock(&(_lsc)->lsc_mtx)
281177274Sthompsa#define LACP_LOCK_ASSERT(_lsc)		mtx_assert(&(_lsc)->lsc_mtx, MA_OWNED)
282177274Sthompsa
283175005Sthompsastruct mbuf	*lacp_input(struct lagg_port *, struct mbuf *);
284168793Sthompsastruct lagg_port *lacp_select_tx_port(struct lagg_softc *, struct mbuf *);
285168793Sthompsaint		lacp_attach(struct lagg_softc *);
286168793Sthompsaint		lacp_detach(struct lagg_softc *);
287168793Sthompsavoid		lacp_init(struct lagg_softc *);
288168793Sthompsavoid		lacp_stop(struct lagg_softc *);
289168793Sthompsaint		lacp_port_create(struct lagg_port *);
290168793Sthompsavoid		lacp_port_destroy(struct lagg_port *);
291168793Sthompsavoid		lacp_linkstate(struct lagg_port *);
292171247Sthompsavoid		lacp_req(struct lagg_softc *, caddr_t);
293171247Sthompsavoid		lacp_portreq(struct lagg_port *, caddr_t);
294168561Sthompsa
295177274Sthompsastatic __inline int
296177274Sthompsalacp_isactive(struct lagg_port *lgp)
297177274Sthompsa{
298177274Sthompsa	struct lacp_port *lp = LACP_PORT(lgp);
299177274Sthompsa	struct lacp_softc *lsc = lp->lp_lsc;
300177274Sthompsa	struct lacp_aggregator *la = lp->lp_aggregator;
301177274Sthompsa
302177274Sthompsa	/* This port is joined to the active aggregator */
303177274Sthompsa	if (la != NULL && la == lsc->lsc_active_aggregator)
304177274Sthompsa		return (1);
305177274Sthompsa
306177274Sthompsa	return (0);
307177274Sthompsa}
308177274Sthompsa
309177274Sthompsastatic __inline int
310177274Sthompsalacp_iscollecting(struct lagg_port *lgp)
311177274Sthompsa{
312177274Sthompsa	struct lacp_port *lp = LACP_PORT(lgp);
313177274Sthompsa
314177274Sthompsa	return ((lp->lp_state & LACP_STATE_COLLECTING) != 0);
315177274Sthompsa}
316177274Sthompsa
317177274Sthompsastatic __inline int
318177274Sthompsalacp_isdistributing(struct lagg_port *lgp)
319177274Sthompsa{
320177274Sthompsa	struct lacp_port *lp = LACP_PORT(lgp);
321177274Sthompsa
322177274Sthompsa	return ((lp->lp_state & LACP_STATE_DISTRIBUTING) != 0);
323177274Sthompsa}
324177274Sthompsa
325168561Sthompsa/* following constants don't include terminating NUL */
326168561Sthompsa#define	LACP_MACSTR_MAX		(2*6 + 5)
327168561Sthompsa#define	LACP_SYSTEMPRIOSTR_MAX	(4)
328168561Sthompsa#define	LACP_SYSTEMIDSTR_MAX	(LACP_SYSTEMPRIOSTR_MAX + 1 + LACP_MACSTR_MAX)
329168561Sthompsa#define	LACP_PORTPRIOSTR_MAX	(4)
330168561Sthompsa#define	LACP_PORTNOSTR_MAX	(4)
331168561Sthompsa#define	LACP_PORTIDSTR_MAX	(LACP_PORTPRIOSTR_MAX + 1 + LACP_PORTNOSTR_MAX)
332168561Sthompsa#define	LACP_KEYSTR_MAX		(4)
333168561Sthompsa#define	LACP_PARTNERSTR_MAX	\
334168561Sthompsa	(1 + LACP_SYSTEMIDSTR_MAX + 1 + LACP_KEYSTR_MAX + 1 \
335168561Sthompsa	+ LACP_PORTIDSTR_MAX + 1)
336168561Sthompsa#define	LACP_LAGIDSTR_MAX	\
337168561Sthompsa	(1 + LACP_PARTNERSTR_MAX + 1 + LACP_PARTNERSTR_MAX + 1)
338168561Sthompsa#define	LACP_STATESTR_MAX	(255) /* XXX */
339