if_bridgevar.h revision 160703
17697SMichael.Christensen@Sun.COM/*	$NetBSD: if_bridgevar.h,v 1.4 2003/07/08 07:13:50 itojun Exp $	*/
27697SMichael.Christensen@Sun.COM
37697SMichael.Christensen@Sun.COM/*
47697SMichael.Christensen@Sun.COM * Copyright 2001 Wasabi Systems, Inc.
57697SMichael.Christensen@Sun.COM * All rights reserved.
67697SMichael.Christensen@Sun.COM *
77697SMichael.Christensen@Sun.COM * Written by Jason R. Thorpe for Wasabi Systems, Inc.
87697SMichael.Christensen@Sun.COM *
97697SMichael.Christensen@Sun.COM * Redistribution and use in source and binary forms, with or without
107697SMichael.Christensen@Sun.COM * modification, are permitted provided that the following conditions
117697SMichael.Christensen@Sun.COM * are met:
127697SMichael.Christensen@Sun.COM * 1. Redistributions of source code must retain the above copyright
137697SMichael.Christensen@Sun.COM *    notice, this list of conditions and the following disclaimer.
147697SMichael.Christensen@Sun.COM * 2. Redistributions in binary form must reproduce the above copyright
157697SMichael.Christensen@Sun.COM *    notice, this list of conditions and the following disclaimer in the
167697SMichael.Christensen@Sun.COM *    documentation and/or other materials provided with the distribution.
177697SMichael.Christensen@Sun.COM * 3. All advertising materials mentioning features or use of this software
187697SMichael.Christensen@Sun.COM *    must display the following acknowledgement:
197697SMichael.Christensen@Sun.COM *	This product includes software developed for the NetBSD Project by
207697SMichael.Christensen@Sun.COM *	Wasabi Systems, Inc.
217697SMichael.Christensen@Sun.COM * 4. The name of Wasabi Systems, Inc. may not be used to endorse
227697SMichael.Christensen@Sun.COM *    or promote products derived from this software without specific prior
237697SMichael.Christensen@Sun.COM *    written permission.
247697SMichael.Christensen@Sun.COM *
257697SMichael.Christensen@Sun.COM * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
267697SMichael.Christensen@Sun.COM * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
277697SMichael.Christensen@Sun.COM * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
287697SMichael.Christensen@Sun.COM * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
297697SMichael.Christensen@Sun.COM * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
307697SMichael.Christensen@Sun.COM * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
317697SMichael.Christensen@Sun.COM * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
327697SMichael.Christensen@Sun.COM * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
337697SMichael.Christensen@Sun.COM * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
347697SMichael.Christensen@Sun.COM * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
357697SMichael.Christensen@Sun.COM * POSSIBILITY OF SUCH DAMAGE.
367697SMichael.Christensen@Sun.COM */
377697SMichael.Christensen@Sun.COM
387697SMichael.Christensen@Sun.COM/*
397697SMichael.Christensen@Sun.COM * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
407697SMichael.Christensen@Sun.COM * All rights reserved.
417697SMichael.Christensen@Sun.COM *
427697SMichael.Christensen@Sun.COM * Redistribution and use in source and binary forms, with or without
437697SMichael.Christensen@Sun.COM * modification, are permitted provided that the following conditions
447697SMichael.Christensen@Sun.COM * are met:
457697SMichael.Christensen@Sun.COM * 1. Redistributions of source code must retain the above copyright
467697SMichael.Christensen@Sun.COM *    notice, this list of conditions and the following disclaimer.
477697SMichael.Christensen@Sun.COM * 2. Redistributions in binary form must reproduce the above copyright
487697SMichael.Christensen@Sun.COM *    notice, this list of conditions and the following disclaimer in the
497697SMichael.Christensen@Sun.COM *    documentation and/or other materials provided with the distribution.
507697SMichael.Christensen@Sun.COM * 3. All advertising materials mentioning features or use of this software
517697SMichael.Christensen@Sun.COM *    must display the following acknowledgement:
527697SMichael.Christensen@Sun.COM *	This product includes software developed by Jason L. Wright
537697SMichael.Christensen@Sun.COM * 4. The name of the author may not be used to endorse or promote products
547697SMichael.Christensen@Sun.COM *    derived from this software without specific prior written permission.
557697SMichael.Christensen@Sun.COM *
567697SMichael.Christensen@Sun.COM * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
577697SMichael.Christensen@Sun.COM * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
587697SMichael.Christensen@Sun.COM * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
59 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
60 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
61 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
62 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
65 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 *
68 * OpenBSD: if_bridge.h,v 1.14 2001/03/22 03:48:29 jason Exp
69 *
70 * $FreeBSD: head/sys/net/if_bridgevar.h 160703 2006-07-26 10:43:02Z thompsa $
71 */
72
73/*
74 * Data structure and control definitions for bridge interfaces.
75 */
76
77#include <sys/callout.h>
78#include <sys/queue.h>
79#include <sys/condvar.h>
80
81/*
82 * Commands used in the SIOCSDRVSPEC ioctl.  Note the lookup of the
83 * bridge interface itself is keyed off the ifdrv structure.
84 */
85#define	BRDGADD			0	/* add bridge member (ifbreq) */
86#define	BRDGDEL			1	/* delete bridge member (ifbreq) */
87#define	BRDGGIFFLGS		2	/* get member if flags (ifbreq) */
88#define	BRDGSIFFLGS		3	/* set member if flags (ifbreq) */
89#define	BRDGSCACHE		4	/* set cache size (ifbrparam) */
90#define	BRDGGCACHE		5	/* get cache size (ifbrparam) */
91#define	BRDGGIFS		6	/* get member list (ifbifconf) */
92#define	BRDGRTS			7	/* get address list (ifbaconf) */
93#define	BRDGSADDR		8	/* set static address (ifbareq) */
94#define	BRDGSTO			9	/* set cache timeout (ifbrparam) */
95#define	BRDGGTO			10	/* get cache timeout (ifbrparam) */
96#define	BRDGDADDR		11	/* delete address (ifbareq) */
97#define	BRDGFLUSH		12	/* flush address cache (ifbreq) */
98
99#define	BRDGGPRI		13	/* get priority (ifbrparam) */
100#define	BRDGSPRI		14	/* set priority (ifbrparam) */
101#define	BRDGGHT			15	/* get hello time (ifbrparam) */
102#define	BRDGSHT			16	/* set hello time (ifbrparam) */
103#define	BRDGGFD			17	/* get forward delay (ifbrparam) */
104#define	BRDGSFD			18	/* set forward delay (ifbrparam) */
105#define	BRDGGMA			19	/* get max age (ifbrparam) */
106#define	BRDGSMA			20	/* set max age (ifbrparam) */
107#define	BRDGSIFPRIO		21	/* set if priority (ifbreq) */
108#define	BRDGSIFCOST		22	/* set if path cost (ifbreq) */
109#define	BRDGADDS		23	/* add bridge span member (ifbreq) */
110#define	BRDGDELS		24	/* delete bridge span member (ifbreq) */
111
112/*
113 * Generic bridge control request.
114 */
115struct ifbreq {
116	char		ifbr_ifsname[IFNAMSIZ];	/* member if name */
117	uint32_t	ifbr_ifsflags;		/* member if flags */
118	uint8_t		ifbr_state;		/* member if STP state */
119	uint8_t		ifbr_priority;		/* member if STP priority */
120	uint8_t		ifbr_path_cost;		/* member if STP cost */
121	uint8_t		ifbr_portno;		/* member if port number */
122};
123
124/* BRDGGIFFLAGS, BRDGSIFFLAGS */
125#define	IFBIF_LEARNING		0x01	/* if can learn */
126#define	IFBIF_DISCOVER		0x02	/* if sends packets w/ unknown dest. */
127#define	IFBIF_STP		0x04	/* if participates in spanning tree */
128#define	IFBIF_SPAN		0x08	/* if is a span port */
129
130#define	IFBIFBITS	"\020\1LEARNING\2DISCOVER\3STP\4SPAN"
131
132/* BRDGFLUSH */
133#define	IFBF_FLUSHDYN		0x00	/* flush learned addresses only */
134#define	IFBF_FLUSHALL		0x01	/* flush all addresses */
135
136/*
137 * Interface list structure.
138 */
139struct ifbifconf {
140	uint32_t	ifbic_len;	/* buffer size */
141	union {
142		caddr_t	ifbicu_buf;
143		struct ifbreq *ifbicu_req;
144	} ifbic_ifbicu;
145#define	ifbic_buf	ifbic_ifbicu.ifbicu_buf
146#define	ifbic_req	ifbic_ifbicu.ifbicu_req
147};
148
149/*
150 * Bridge address request.
151 */
152struct ifbareq {
153	char		ifba_ifsname[IFNAMSIZ];	/* member if name */
154	unsigned long	ifba_expire;		/* address expire time */
155	uint8_t		ifba_flags;		/* address flags */
156	uint8_t		ifba_dst[ETHER_ADDR_LEN];/* destination address */
157};
158
159#define	IFBAF_TYPEMASK	0x03	/* address type mask */
160#define	IFBAF_DYNAMIC	0x00	/* dynamically learned address */
161#define	IFBAF_STATIC	0x01	/* static address */
162
163#define	IFBAFBITS	"\020\1STATIC"
164
165/*
166 * Address list structure.
167 */
168struct ifbaconf {
169	uint32_t	ifbac_len;	/* buffer size */
170	union {
171		caddr_t ifbacu_buf;
172		struct ifbareq *ifbacu_req;
173	} ifbac_ifbacu;
174#define	ifbac_buf	ifbac_ifbacu.ifbacu_buf
175#define	ifbac_req	ifbac_ifbacu.ifbacu_req
176};
177
178/*
179 * Bridge parameter structure.
180 */
181struct ifbrparam {
182	union {
183		uint32_t ifbrpu_int32;
184		uint16_t ifbrpu_int16;
185		uint8_t ifbrpu_int8;
186	} ifbrp_ifbrpu;
187};
188#define	ifbrp_csize	ifbrp_ifbrpu.ifbrpu_int32	/* cache size */
189#define	ifbrp_ctime	ifbrp_ifbrpu.ifbrpu_int32	/* cache time (sec) */
190#define	ifbrp_prio	ifbrp_ifbrpu.ifbrpu_int16	/* bridge priority */
191#define	ifbrp_hellotime	ifbrp_ifbrpu.ifbrpu_int8	/* hello time (sec) */
192#define	ifbrp_fwddelay	ifbrp_ifbrpu.ifbrpu_int8	/* fwd time (sec) */
193#define	ifbrp_maxage	ifbrp_ifbrpu.ifbrpu_int8	/* max age (sec) */
194
195#ifdef _KERNEL
196
197/*
198 * Bridge interface list entry.
199 */
200struct bridge_iflist {
201	LIST_ENTRY(bridge_iflist) bif_next;
202	struct ifnet		*bif_ifp;	/* member if */
203	struct bstp_port	bif_stp;	/* STP state */
204	uint32_t		bif_flags;	/* member if flags */
205	int			bif_mutecap;	/* member muted caps */
206};
207
208/*
209 * Bridge route node.
210 */
211struct bridge_rtnode {
212	LIST_ENTRY(bridge_rtnode) brt_hash;	/* hash table linkage */
213	LIST_ENTRY(bridge_rtnode) brt_list;	/* list linkage */
214	struct ifnet		*brt_ifp;	/* destination if */
215	unsigned long		brt_expire;	/* expiration time */
216	uint8_t			brt_flags;	/* address flags */
217	uint8_t			brt_addr[ETHER_ADDR_LEN];
218};
219
220/*
221 * Software state for each bridge.
222 */
223struct bridge_softc {
224	struct ifnet		*sc_ifp;	/* make this an interface */
225	LIST_ENTRY(bridge_softc) sc_list;
226	struct mtx		sc_mtx;
227	struct cv		sc_cv;
228	uint32_t		sc_brtmax;	/* max # of addresses */
229	uint32_t		sc_brtcnt;	/* cur. # of addresses */
230	uint32_t		sc_brttimeout;	/* rt timeout in seconds */
231	struct callout		sc_brcallout;	/* bridge callout */
232	uint32_t		sc_iflist_ref;	/* refcount for sc_iflist */
233	uint32_t		sc_iflist_xcnt;	/* refcount for sc_iflist */
234	LIST_HEAD(, bridge_iflist) sc_iflist;	/* member interface list */
235	LIST_HEAD(, bridge_rtnode) *sc_rthash;	/* our forwarding table */
236	LIST_HEAD(, bridge_rtnode) sc_rtlist;	/* list version of above */
237	uint32_t		sc_rthash_key;	/* key for hash */
238	LIST_HEAD(, bridge_iflist) sc_spanlist;	/* span ports list */
239	struct bstp_state	sc_stp;		/* STP state */
240};
241
242#define BRIDGE_LOCK_INIT(_sc)		do {			\
243	mtx_init(&(_sc)->sc_mtx, "if_bridge", NULL, MTX_DEF);	\
244	cv_init(&(_sc)->sc_cv, "if_bridge_cv");			\
245} while (0)
246#define BRIDGE_LOCK_DESTROY(_sc)	do {	\
247	mtx_destroy(&(_sc)->sc_mtx);		\
248	cv_destroy(&(_sc)->sc_cv);		\
249} while (0)
250#define BRIDGE_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
251#define BRIDGE_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
252#define BRIDGE_LOCKED(_sc)		mtx_owned(&(_sc)->sc_mtx)
253#define BRIDGE_LOCK_ASSERT(_sc)		mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
254#define	BRIDGE_LOCK2REF(_sc, _err)	do {	\
255	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
256	if ((_sc)->sc_iflist_xcnt > 0)		\
257		(_err) = EBUSY;			\
258	else					\
259		(_sc)->sc_iflist_ref++;		\
260	mtx_unlock(&(_sc)->sc_mtx);		\
261} while (0)
262#define	BRIDGE_UNREF(_sc)		do {				\
263	mtx_lock(&(_sc)->sc_mtx);					\
264	(_sc)->sc_iflist_ref--;						\
265	if (((_sc)->sc_iflist_xcnt > 0) && ((_sc)->sc_iflist_ref == 0))	\
266		cv_broadcast(&(_sc)->sc_cv);				\
267	mtx_unlock(&(_sc)->sc_mtx);					\
268} while (0)
269#define	BRIDGE_XLOCK(_sc)		do {		\
270	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);		\
271	(_sc)->sc_iflist_xcnt++;			\
272	while ((_sc)->sc_iflist_ref > 0)		\
273		cv_wait(&(_sc)->sc_cv, &(_sc)->sc_mtx);	\
274} while (0)
275#define	BRIDGE_XDROP(_sc)		do {	\
276	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);	\
277	(_sc)->sc_iflist_xcnt--;		\
278} while (0)
279
280#define BRIDGE_INPUT(_ifp, _m)		do {    	\
281	KASSERT(bridge_input_p != NULL,			\
282	    ("%s: if_bridge not loaded!", __func__));	\
283	_m = (*bridge_input_p)(_ifp, _m);		\
284	if (_m != NULL)					\
285		_ifp = _m->m_pkthdr.rcvif;		\
286} while (0)
287
288#define BRIDGE_OUTPUT(_ifp, _m, _err)	do {    		\
289	KASSERT(bridge_output_p != NULL,			\
290	    ("%s: if_bridge not loaded!", __func__));		\
291	_err = (*bridge_output_p)(_ifp, _m, NULL, NULL);	\
292} while (0)
293
294void	bridge_enqueue(struct bridge_softc *, struct ifnet *, struct mbuf *);
295void	bridge_rtdelete(struct bridge_softc *, struct ifnet *ifp, int);
296
297extern	struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
298extern	int (*bridge_output_p)(struct ifnet *, struct mbuf *,
299		struct sockaddr *, struct rtentry *);
300extern	void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
301
302#endif /* _KERNEL */
303