Home | History | Annotate | Line # | Download | only in net
if_bridgevar.h revision 1.23
      1 /*	$NetBSD: if_bridgevar.h,v 1.23 2015/01/16 10:36:14 ozaki-r Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      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  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1999, 2000 Jason L. Wright (jason (at) thought.net)
     40  * All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by Jason L. Wright
     53  * 4. The name of the author may not be used to endorse or promote products
     54  *    derived from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     58  * 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 
     71 /*
     72  * Data structure and control definitions for bridge interfaces.
     73  */
     74 
     75 #ifndef _NET_IF_BRIDGEVAR_H_
     76 #define _NET_IF_BRIDGEVAR_H_
     77 
     78 #include <sys/callout.h>
     79 #include <sys/queue.h>
     80 #include <sys/mutex.h>
     81 #include <sys/condvar.h>
     82 
     83 /*
     84  * Commands used in the SIOCSDRVSPEC ioctl.  Note the lookup of the
     85  * bridge interface itself is keyed off the ifdrv structure.
     86  */
     87 #define	BRDGADD			0	/* add bridge member (ifbreq) */
     88 #define	BRDGDEL			1	/* delete bridge member (ifbreq) */
     89 #define	BRDGGIFFLGS		2	/* get member if flags (ifbreq) */
     90 #define	BRDGSIFFLGS		3	/* set member if flags (ifbreq) */
     91 #define	BRDGSCACHE		4	/* set cache size (ifbrparam) */
     92 #define	BRDGGCACHE		5	/* get cache size (ifbrparam) */
     93 #define	BRDGGIFS		6	/* get member list (ifbifconf) */
     94 #define	BRDGRTS			7	/* get address list (ifbaconf) */
     95 #define	BRDGSADDR		8	/* set static address (ifbareq) */
     96 #define	BRDGSTO			9	/* set cache timeout (ifbrparam) */
     97 #define	BRDGGTO			10	/* get cache timeout (ifbrparam) */
     98 #define	BRDGDADDR		11	/* delete address (ifbareq) */
     99 #define	BRDGFLUSH		12	/* flush address cache (ifbreq) */
    100 
    101 #define	BRDGGPRI		13	/* get priority (ifbrparam) */
    102 #define	BRDGSPRI		14	/* set priority (ifbrparam) */
    103 #define	BRDGGHT			15	/* get hello time (ifbrparam) */
    104 #define	BRDGSHT			16	/* set hello time (ifbrparam) */
    105 #define	BRDGGFD			17	/* get forward delay (ifbrparam) */
    106 #define	BRDGSFD			18	/* set forward delay (ifbrparam) */
    107 #define	BRDGGMA			19	/* get max age (ifbrparam) */
    108 #define	BRDGSMA			20	/* set max age (ifbrparam) */
    109 #define	BRDGSIFPRIO		21	/* set if priority (ifbreq) */
    110 #define BRDGSIFCOST		22	/* set if path cost (ifbreq) */
    111 #define BRDGGFILT	        23	/* get filter flags (ifbrparam) */
    112 #define BRDGSFILT	        24	/* set filter flags (ifbrparam) */
    113 
    114 /*
    115  * Generic bridge control request.
    116  */
    117 struct ifbreq {
    118 	char		ifbr_ifsname[IFNAMSIZ];	/* member if name */
    119 	uint32_t	ifbr_ifsflags;		/* member if flags */
    120 	uint8_t		ifbr_state;		/* member if STP state */
    121 	uint8_t		ifbr_priority;		/* member if STP priority */
    122 	uint8_t		ifbr_path_cost;		/* member if STP cost */
    123 	uint8_t		ifbr_portno;		/* member if port number */
    124 };
    125 
    126 /* BRDGGIFFLAGS, BRDGSIFFLAGS */
    127 #define	IFBIF_LEARNING		0x01	/* if can learn */
    128 #define	IFBIF_DISCOVER		0x02	/* if sends packets w/ unknown dest. */
    129 #define	IFBIF_STP		0x04	/* if participates in spanning tree */
    130 
    131 #define	IFBIFBITS	"\020\1LEARNING\2DISCOVER\3STP"
    132 
    133 /* BRDGFLUSH */
    134 #define	IFBF_FLUSHDYN		0x00	/* flush learned addresses only */
    135 #define	IFBF_FLUSHALL		0x01	/* flush all addresses */
    136 
    137 /* BRDGSFILT */
    138 #define IFBF_FILT_USEIPF	0x00000001 /* enable ipf on bridge */
    139 #define IFBF_FILT_MASK		0x00000001 /* mask of valid values */
    140 
    141 /* STP port states */
    142 #define	BSTP_IFSTATE_DISABLED	0
    143 #define	BSTP_IFSTATE_LISTENING	1
    144 #define	BSTP_IFSTATE_LEARNING	2
    145 #define	BSTP_IFSTATE_FORWARDING	3
    146 #define	BSTP_IFSTATE_BLOCKING	4
    147 
    148 /*
    149  * Interface list structure.
    150  */
    151 struct ifbifconf {
    152 	uint32_t	ifbic_len;	/* buffer size */
    153 	union {
    154 		void *	ifbicu_buf;
    155 		struct ifbreq *ifbicu_req;
    156 	} ifbic_ifbicu;
    157 #define	ifbic_buf	ifbic_ifbicu.ifbicu_buf
    158 #define	ifbic_req	ifbic_ifbicu.ifbicu_req
    159 };
    160 
    161 /*
    162  * Bridge address request.
    163  */
    164 struct ifbareq {
    165 	char		ifba_ifsname[IFNAMSIZ];	/* member if name */
    166 	/*XXX: time_t */
    167 	long		ifba_expire;		/* address expire time */
    168 	uint8_t		ifba_flags;		/* address flags */
    169 	uint8_t		ifba_dst[ETHER_ADDR_LEN];/* destination address */
    170 };
    171 
    172 #define	IFBAF_TYPEMASK	0x03	/* address type mask */
    173 #define	IFBAF_DYNAMIC	0x00	/* dynamically learned address */
    174 #define	IFBAF_STATIC	0x01	/* static address */
    175 
    176 #define	IFBAFBITS	"\020\1STATIC"
    177 
    178 /*
    179  * Address list structure.
    180  */
    181 struct ifbaconf {
    182 	uint32_t	ifbac_len;	/* buffer size */
    183 	union {
    184 		void *ifbacu_buf;
    185 		struct ifbareq *ifbacu_req;
    186 	} ifbac_ifbacu;
    187 #define	ifbac_buf	ifbac_ifbacu.ifbacu_buf
    188 #define	ifbac_req	ifbac_ifbacu.ifbacu_req
    189 };
    190 
    191 /*
    192  * Bridge parameter structure.
    193  */
    194 struct ifbrparam {
    195 	union {
    196 		uint32_t ifbrpu_int32;
    197 		uint16_t ifbrpu_int16;
    198 		uint8_t ifbrpu_int8;
    199 	} ifbrp_ifbrpu;
    200 };
    201 #define	ifbrp_csize	ifbrp_ifbrpu.ifbrpu_int32	/* cache size */
    202 #define	ifbrp_ctime	ifbrp_ifbrpu.ifbrpu_int32	/* cache time (sec) */
    203 #define	ifbrp_prio	ifbrp_ifbrpu.ifbrpu_int16	/* bridge priority */
    204 #define	ifbrp_hellotime	ifbrp_ifbrpu.ifbrpu_int8	/* hello time (sec) */
    205 #define	ifbrp_fwddelay	ifbrp_ifbrpu.ifbrpu_int8	/* fwd time (sec) */
    206 #define	ifbrp_maxage	ifbrp_ifbrpu.ifbrpu_int8	/* max age (sec) */
    207 #define	ifbrp_filter	ifbrp_ifbrpu.ifbrpu_int32	/* filtering flags */
    208 
    209 #ifdef _KERNEL
    210 #ifdef _KERNEL_OPT
    211 #include "opt_net_mpsafe.h"
    212 #endif /* _KERNEL_OPT */
    213 
    214 #include <sys/pserialize.h>
    215 #include <sys/workqueue.h>
    216 
    217 #include <net/pktqueue.h>
    218 
    219 /*
    220  * Timekeeping structure used in spanning tree code.
    221  */
    222 struct bridge_timer {
    223 	uint16_t	active;
    224 	uint16_t	value;
    225 };
    226 
    227 struct bstp_config_unit {
    228 	uint64_t	cu_rootid;
    229 	uint64_t	cu_bridge_id;
    230 	uint32_t	cu_root_path_cost;
    231 	uint16_t	cu_message_age;
    232 	uint16_t	cu_max_age;
    233 	uint16_t	cu_hello_time;
    234 	uint16_t	cu_forward_delay;
    235 	uint16_t	cu_port_id;
    236 	uint8_t		cu_message_type;
    237 	uint8_t		cu_topology_change_acknowledgment;
    238 	uint8_t		cu_topology_change;
    239 };
    240 
    241 struct bstp_tcn_unit {
    242 	uint8_t		tu_message_type;
    243 };
    244 
    245 /*
    246  * Bridge interface list entry.
    247  */
    248 struct bridge_iflist {
    249 	LIST_ENTRY(bridge_iflist) bif_next;
    250 	uint64_t		bif_designated_root;
    251 	uint64_t		bif_designated_bridge;
    252 	uint32_t		bif_path_cost;
    253 	uint32_t		bif_designated_cost;
    254 	struct bridge_timer	bif_hold_timer;
    255 	struct bridge_timer	bif_message_age_timer;
    256 	struct bridge_timer	bif_forward_delay_timer;
    257 	uint16_t		bif_port_id;
    258 	uint16_t		bif_designated_port;
    259 	struct bstp_config_unit	bif_config_bpdu;
    260 	uint8_t			bif_state;
    261 	uint8_t			bif_topology_change_acknowledge;
    262 	uint8_t			bif_config_pending;
    263 	uint8_t			bif_change_detection_enabled;
    264 	uint8_t			bif_priority;
    265 	struct ifnet		*bif_ifp;	/* member if */
    266 	uint32_t		bif_flags;	/* member if flags */
    267 	uint32_t		bif_refs;	/* reference count */
    268 	bool			bif_waiting;	/* waiting for released  */
    269 };
    270 
    271 /*
    272  * Bridge route node.
    273  */
    274 struct bridge_rtnode {
    275 	LIST_ENTRY(bridge_rtnode) brt_hash;	/* hash table linkage */
    276 	LIST_ENTRY(bridge_rtnode) brt_list;	/* list linkage */
    277 	struct ifnet		*brt_ifp;	/* destination if */
    278 	time_t			brt_expire;	/* expiration time */
    279 	uint8_t			brt_flags;	/* address flags */
    280 	uint8_t			brt_addr[ETHER_ADDR_LEN];
    281 };
    282 
    283 /*
    284  * Software state for each bridge.
    285  */
    286 struct bridge_softc {
    287 	struct ifnet		sc_if;
    288 	LIST_ENTRY(bridge_softc) sc_list;
    289 	uint64_t		sc_designated_root;
    290 	uint64_t		sc_bridge_id;
    291 	struct bridge_iflist	*sc_root_port;
    292 	uint32_t		sc_root_path_cost;
    293 	uint16_t		sc_max_age;
    294 	uint16_t		sc_hello_time;
    295 	uint16_t		sc_forward_delay;
    296 	uint16_t		sc_bridge_max_age;
    297 	uint16_t		sc_bridge_hello_time;
    298 	uint16_t		sc_bridge_forward_delay;
    299 	uint16_t		sc_topology_change_time;
    300 	uint16_t		sc_hold_time;
    301 	uint16_t		sc_bridge_priority;
    302 	uint8_t			sc_topology_change_detected;
    303 	uint8_t			sc_topology_change;
    304 	struct bridge_timer	sc_hello_timer;
    305 	struct bridge_timer	sc_topology_change_timer;
    306 	struct bridge_timer	sc_tcn_timer;
    307 	uint32_t		sc_brtmax;	/* max # of addresses */
    308 	uint32_t		sc_brtcnt;	/* cur. # of addresses */
    309 	uint32_t		sc_brttimeout;	/* rt timeout in seconds */
    310 	callout_t		sc_brcallout;	/* bridge callout */
    311 	callout_t		sc_bstpcallout;	/* STP callout */
    312 	LIST_HEAD(, bridge_iflist) sc_iflist;	/* member interface list */
    313 	kmutex_t		*sc_iflist_intr_lock;
    314 	kcondvar_t		sc_iflist_cv;
    315 	pserialize_t		sc_iflist_psz;
    316 	kmutex_t		*sc_iflist_lock;
    317 	LIST_HEAD(, bridge_rtnode) *sc_rthash;	/* our forwarding table */
    318 	LIST_HEAD(, bridge_rtnode) sc_rtlist;	/* list version of above */
    319 	kmutex_t		*sc_rtlist_intr_lock;
    320 	kmutex_t		*sc_rtlist_lock;
    321 	pserialize_t		sc_rtlist_psz;
    322 	struct workqueue	*sc_rtage_wq;
    323 	uint32_t		sc_rthash_key;	/* key for hash */
    324 	uint32_t		sc_filter_flags; /* ipf and flags */
    325 	pktqueue_t *		sc_fwd_pktq;
    326 };
    327 
    328 extern const uint8_t bstp_etheraddr[];
    329 
    330 void	bridge_ifdetach(struct ifnet *);
    331 
    332 int	bridge_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
    333 	    struct rtentry *);
    334 
    335 void	bstp_initialization(struct bridge_softc *);
    336 void	bstp_stop(struct bridge_softc *);
    337 void	bstp_input(struct bridge_softc *, struct bridge_iflist *, struct mbuf *);
    338 
    339 void	bridge_enqueue(struct bridge_softc *, struct ifnet *, struct mbuf *,
    340 	    int);
    341 
    342 #ifdef NET_MPSAFE
    343 #define BRIDGE_MPSAFE	1
    344 #endif
    345 
    346 #define BRIDGE_LOCK(_sc)	if ((_sc)->sc_iflist_lock) \
    347 					mutex_enter((_sc)->sc_iflist_lock)
    348 #define BRIDGE_UNLOCK(_sc)	if ((_sc)->sc_iflist_lock) \
    349 					mutex_exit((_sc)->sc_iflist_lock)
    350 #define BRIDGE_LOCKED(_sc)	(!(_sc)->sc_iflist_lock || \
    351 				 mutex_owned((_sc)->sc_iflist_lock))
    352 
    353 #define BRIDGE_INTR_LOCK(_sc)	if ((_sc)->sc_iflist_intr_lock) \
    354 					mutex_enter((_sc)->sc_iflist_intr_lock)
    355 #define BRIDGE_INTR_UNLOCK(_sc)	if ((_sc)->sc_iflist_intr_lock) \
    356 					mutex_exit((_sc)->sc_iflist_intr_lock)
    357 #define BRIDGE_INTR_LOCKED(_sc)	(!(_sc)->sc_iflist_intr_lock || \
    358 				 mutex_owned((_sc)->sc_iflist_intr_lock))
    359 
    360 #ifdef BRIDGE_MPSAFE
    361 /*
    362  * These macros can be used in both HW interrupt and softint contexts.
    363  */
    364 #define BRIDGE_PSZ_RENTER(__s)	do { \
    365 					if (!cpu_intr_p()) \
    366 						__s = pserialize_read_enter(); \
    367 					else \
    368 						__s = splhigh(); \
    369 				} while (0)
    370 #define BRIDGE_PSZ_REXIT(__s)	do { \
    371 					if (!cpu_intr_p()) \
    372 						pserialize_read_exit(__s); \
    373 					else \
    374 						splx(__s); \
    375 				} while (0)
    376 #else /* BRIDGE_MPSAFE */
    377 #define BRIDGE_PSZ_RENTER(__s)	do { __s = 0; } while (0)
    378 #define BRIDGE_PSZ_REXIT(__s)	do { (void)__s; } while (0)
    379 #endif /* BRIDGE_MPSAFE */
    380 
    381 #define BRIDGE_PSZ_PERFORM(_sc)	if ((_sc)->sc_iflist_psz) \
    382 					pserialize_perform((_sc)->sc_iflist_psz);
    383 
    384 /*
    385  * Locking notes:
    386  * - Updates of sc_iflist are serialized by sc_iflist_lock (an adaptive mutex)
    387  * - Items of sc_iflist (bridge_iflist) is protected by both pserialize
    388  *   (sc_iflist_psz) and reference counting (bridge_iflist#bif_refs)
    389  * - Before destroying an item of sc_iflist, we have to do pserialize_perform
    390  *   and synchronize with the reference counting via a conditional variable
    391  *   (sc_iflist_cz)
    392  * - sc_iflist_intr_lock (a spin mutex) is used for the CV
    393  *   - A spin mutex is required because the reference counting can be used
    394  *     in HW interrupt context
    395  *   - The mutex is also used for STP
    396  *   - Once we change to execute entire Layer 2 in softint context,
    397  *     we can get rid of sc_iflist_intr_lock
    398  * - Updates of sc_rtlist are serialized by sc_rtlist_intr_lock (a spin mutex)
    399  *   - The sc_rtlist can be modified in HW interrupt context for now
    400  * - sc_rtlist_lock (an adaptive mutex) is only for pserialize
    401  *   - Once we change to execute entire Layer 2 in softint context,
    402  *     we can get rid of sc_rtlist_intr_lock
    403  * - A workqueue is used to run bridge_rtage in LWP context via bridge_timer callout
    404  *   - bridge_rtage uses pserialize that requires non-interrupt context
    405  */
    406 #endif /* _KERNEL */
    407 #endif /* !_NET_IF_BRIDGEVAR_H_ */
    408