xref: /illumos-gate/usr/src/uts/common/net/radix.h (revision 2679e103)
1c793af95Ssangeeta /*
2c793af95Ssangeeta  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3c793af95Ssangeeta  * Use is subject to license terms.
4c793af95Ssangeeta  *
5c793af95Ssangeeta  * Copyright (c) 1988, 1989, 1993
6c793af95Ssangeeta  *	The Regents of the University of California.  All rights reserved.
7c793af95Ssangeeta  *
8c793af95Ssangeeta  * Redistribution and use in source and binary forms, with or without
9c793af95Ssangeeta  * modification, are permitted provided that the following conditions
10c793af95Ssangeeta  * are met:
11c793af95Ssangeeta  * 1. Redistributions of source code must retain the above copyright
12c793af95Ssangeeta  *    notice, this list of conditions and the following disclaimer.
13c793af95Ssangeeta  * 2. Redistributions in binary form must reproduce the above copyright
14c793af95Ssangeeta  *    notice, this list of conditions and the following disclaimer in the
15c793af95Ssangeeta  *    documentation and/or other materials provided with the distribution.
16c793af95Ssangeeta  * 4. Neither the name of the University nor the names of its contributors
17c793af95Ssangeeta  *    may be used to endorse or promote products derived from this software
18c793af95Ssangeeta  *    without specific prior written permission.
19c793af95Ssangeeta  *
20c793af95Ssangeeta  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21c793af95Ssangeeta  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22c793af95Ssangeeta  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23c793af95Ssangeeta  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24c793af95Ssangeeta  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25c793af95Ssangeeta  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26c793af95Ssangeeta  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27c793af95Ssangeeta  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28c793af95Ssangeeta  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29c793af95Ssangeeta  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30c793af95Ssangeeta  * SUCH DAMAGE.
31c793af95Ssangeeta  *
32c793af95Ssangeeta  *	@(#)radix.h	8.2 (Berkeley) 10/31/94
33c793af95Ssangeeta  * $FreeBSD: /repoman/r/ncvs/src/sys/net/radix.h,v 1.25.2.1 2005/01/31 23:26:23
34c793af95Ssangeeta  * imp Exp $
35c793af95Ssangeeta  */
36c793af95Ssangeeta 
37c793af95Ssangeeta #ifndef _RADIX_H_
38c793af95Ssangeeta #define	_RADIX_H_
39c793af95Ssangeeta 
40c793af95Ssangeeta #pragma ident	"%Z%%M%	%I%	%E% SMI"
41c793af95Ssangeeta 
42c793af95Ssangeeta #ifdef	__cplusplus
43c793af95Ssangeeta extern "C" {
44c793af95Ssangeeta #endif
45c793af95Ssangeeta 
46c793af95Ssangeeta #ifdef _KERNEL
47c793af95Ssangeeta #include <sys/mutex.h>
48c793af95Ssangeeta #include <netinet/in.h>
49c793af95Ssangeeta #endif
50c793af95Ssangeeta #include <sys/sysmacros.h>
51c793af95Ssangeeta 
52c793af95Ssangeeta #ifdef MALLOC_DECLARE
53c793af95Ssangeeta MALLOC_DECLARE(M_RTABLE);
54c793af95Ssangeeta #endif
55c793af95Ssangeeta 
56c793af95Ssangeeta /*
57c793af95Ssangeeta  * Radix search tree node layout.
58c793af95Ssangeeta  */
59c793af95Ssangeeta 
60c793af95Ssangeeta struct radix_node {
61c793af95Ssangeeta 	struct	radix_mask *rn_mklist;	/* list of masks contained in subtree */
62c793af95Ssangeeta 	struct	radix_node *rn_parent;	/* parent */
63c793af95Ssangeeta 	short	rn_bit;			/* bit offset; -1-index(netmask) */
64c793af95Ssangeeta 	char	rn_bmask;		/* node: mask for bit test */
65c793af95Ssangeeta 	uchar_t	rn_flags;		/* enumerated next */
66c793af95Ssangeeta #define	RNF_NORMAL	1		/* leaf contains normal route */
67c793af95Ssangeeta #define	RNF_ROOT	2		/* leaf is root leaf for tree */
68c793af95Ssangeeta #define	RNF_ACTIVE	4		/* This node is alive (for rtfree) */
69c793af95Ssangeeta 	union {
70c793af95Ssangeeta 		struct {			/* leaf only data: */
71c793af95Ssangeeta 			caddr_t	rn_Key;		/* object of search */
72c793af95Ssangeeta 			caddr_t	rn_Mask;	/* netmask, if present */
73c793af95Ssangeeta 			struct	radix_node *rn_Dupedkey;
74c793af95Ssangeeta 		} rn_leaf;
75c793af95Ssangeeta 		struct {			/* node only data: */
76c793af95Ssangeeta 			int	rn_Off;		/* where to start compare */
77c793af95Ssangeeta 			struct	radix_node *rn_L; /* progeny */
78c793af95Ssangeeta 			struct	radix_node *rn_R; /* progeny */
79c793af95Ssangeeta 		} rn_node;
80c793af95Ssangeeta 	}		rn_u;
81c793af95Ssangeeta };
82c793af95Ssangeeta 
83c793af95Ssangeeta 
84c793af95Ssangeeta #define	rn_dupedkey	rn_u.rn_leaf.rn_Dupedkey
85c793af95Ssangeeta #define	rn_key		rn_u.rn_leaf.rn_Key
86c793af95Ssangeeta #define	rn_mask		rn_u.rn_leaf.rn_Mask
87c793af95Ssangeeta #define	rn_offset	rn_u.rn_node.rn_Off
88c793af95Ssangeeta #define	rn_left		rn_u.rn_node.rn_L
89c793af95Ssangeeta #define	rn_right	rn_u.rn_node.rn_R
90c793af95Ssangeeta 
91c793af95Ssangeeta /*
92c793af95Ssangeeta  * Annotations to tree concerning potential routes applying to subtrees.
93c793af95Ssangeeta  */
94c793af95Ssangeeta 
95c793af95Ssangeeta struct radix_mask {
96c793af95Ssangeeta 	short	rm_bit;			/* bit offset; -1-index(netmask) */
97c793af95Ssangeeta 	char	rm_unused;		/* cf. rn_bmask */
98c793af95Ssangeeta 	uchar_t	rm_flags;		/* cf. rn_flags */
99c793af95Ssangeeta 	struct	radix_mask *rm_mklist;	/* more masks to try */
100c793af95Ssangeeta 	union	{
101c793af95Ssangeeta 		caddr_t	rmu_mask;		/* the mask */
102c793af95Ssangeeta 		struct	radix_node *rmu_leaf;	/* for normal routes */
103c793af95Ssangeeta 	}	rm_rmu;
104c793af95Ssangeeta 	int	rm_refs;		/* # of references to this struct */
105c793af95Ssangeeta };
106c793af95Ssangeeta 
107c793af95Ssangeeta #define	rm_mask rm_rmu.rmu_mask
108c793af95Ssangeeta #define	rm_leaf rm_rmu.rmu_leaf		/* extra field would make 32 bytes */
109c793af95Ssangeeta 
110c793af95Ssangeeta typedef int walktree_f_t(struct radix_node *, void *);
111c793af95Ssangeeta typedef boolean_t match_leaf_t(struct radix_node *, void *);
112*2679e103Ssowmini typedef void (*lockf_t)(struct radix_node *);
113c793af95Ssangeeta 
114c793af95Ssangeeta struct radix_node_head {
115c793af95Ssangeeta 	struct	radix_node *rnh_treetop;
116c793af95Ssangeeta 	int	rnh_addrsize;		/* permit, but not require fixed keys */
117c793af95Ssangeeta 	int	rnh_pktsize;		/* permit, but not require fixed keys */
118c793af95Ssangeeta 	struct	radix_node *(*rnh_addaddr)	/* add based on sockaddr */
119c793af95Ssangeeta 		(void *v, void *mask,
120c793af95Ssangeeta 		struct radix_node_head *head, struct radix_node nodes[]);
121c793af95Ssangeeta 	struct	radix_node *(*rnh_addpkt)	/* add based on packet hdr */
122c793af95Ssangeeta 		(void *v, void *mask,
123c793af95Ssangeeta 		    struct radix_node_head *head, struct radix_node nodes[]);
124c793af95Ssangeeta 	struct	radix_node *(*rnh_deladdr)	/* remove based on sockaddr */
125c793af95Ssangeeta 		(void *v, void *mask, struct radix_node_head *head);
126c793af95Ssangeeta 	struct	radix_node *(*rnh_delpkt)	/* remove based on packet hdr */
127c793af95Ssangeeta 		(void *v, void *mask, struct radix_node_head *head);
128c793af95Ssangeeta 	struct	radix_node *(*rnh_matchaddr)	/* locate based on sockaddr */
129c793af95Ssangeeta 		(void *v, struct radix_node_head *head);
130c793af95Ssangeeta 	/* rnh_matchaddr_args: locate based on sockaddr and match_leaf_t() */
131c793af95Ssangeeta 	struct	radix_node *(*rnh_matchaddr_args)
132c793af95Ssangeeta 		(void *v, struct radix_node_head *head,
133c793af95Ssangeeta 		match_leaf_t *f, void *w);
134c793af95Ssangeeta 	struct	radix_node *(*rnh_lookup)	/* locate based on sockaddr */
135c793af95Ssangeeta 		(void *v, void *mask, struct radix_node_head *head);
136c793af95Ssangeeta 	struct	radix_node *(*rnh_matchpkt)	/* locate based on packet hdr */
137c793af95Ssangeeta 		(void *v, struct radix_node_head *head);
138c793af95Ssangeeta 	int	(*rnh_walktree)			/* traverse tree */
139c793af95Ssangeeta 		(struct radix_node_head *head, walktree_f_t *f, void *w);
140*2679e103Ssowmini 	int	(*rnh_walktree_mt)			/* traverse tree */
141*2679e103Ssowmini 		(struct radix_node_head *head, walktree_f_t *f, void *w,
142*2679e103Ssowmini 		lockf_t lockf, lockf_t unlockf);
143*2679e103Ssowmini 	/* rn_walktree_mt: MT safe version of rn_walktree */
144c793af95Ssangeeta 	int	(*rnh_walktree_from)		/* traverse tree below a */
145c793af95Ssangeeta 		(struct radix_node_head *head, void *a, void *m,
146c793af95Ssangeeta 		    walktree_f_t *f, void *w);
147c793af95Ssangeeta 	void	(*rnh_close)	/* do something when the last ref drops */
148c793af95Ssangeeta 		(struct radix_node *rn, struct radix_node_head *head);
149c793af95Ssangeeta 	struct	radix_node rnh_nodes[3];	/* empty tree for common case */
150c793af95Ssangeeta #ifdef _KERNEL
151c793af95Ssangeeta 	krwlock_t rnh_lock;			/* locks entire radix tree */
152c793af95Ssangeeta #endif
153c793af95Ssangeeta };
154c793af95Ssangeeta 
155c793af95Ssangeeta #ifdef _KERNEL
156c793af95Ssangeeta /*
157c793af95Ssangeeta  * BSD's sockaddr_in and sockadr have a sin_len and an sa_len
158c793af95Ssangeeta  * field respectively, as the first field in the structure, and
159c793af95Ssangeeta  * everything in radix.c assumes that the first byte of the "varg"
160c793af95Ssangeeta  * passed in tells the length of the key (the sockaddr).
161c793af95Ssangeeta  *
162c793af95Ssangeeta  * Since Solaris' sockaddr_in and sockadr, do not have these fields, we
163c793af95Ssangeeta  * define a BSD4-like sockaddr_in structure with rt_sin_len field to
164c793af95Ssangeeta  * make LEN macro wn radix.c to work correctly for Solaris
165c793af95Ssangeeta  * See comments around LEN() macro in ip/radix.c
166c793af95Ssangeeta  * The callers of functions of radix.c have to use this data structure
167c793af95Ssangeeta  */
168c793af95Ssangeeta struct rt_sockaddr  {
169c793af95Ssangeeta 	uint8_t		rt_sin_len;
170c793af95Ssangeeta 	uint8_t		rt_sin_family;
171c793af95Ssangeeta 	uint16_t	rt_sin_port;
172c793af95Ssangeeta 	struct in_addr	rt_sin_addr;
173c793af95Ssangeeta 	char		rt_sin_zero[8];
174c793af95Ssangeeta };
175c793af95Ssangeeta 
176c793af95Ssangeeta 
177c793af95Ssangeeta #define	R_Malloc(p, c, n)  p = kmem_cache_alloc((c),  KM_NOSLEEP)
178c793af95Ssangeeta #define	R_Zalloc(p, c, n) \
179c793af95Ssangeeta 		if (p = kmem_cache_alloc((c), KM_NOSLEEP)) {\
180c793af95Ssangeeta 			bzero(p, n); \
181c793af95Ssangeeta 		}
182c793af95Ssangeeta #define	R_ZallocSleep(p, t, n)	p = (t) kmem_zalloc(n, KM_SLEEP)
183c793af95Ssangeeta #define	Free(p, c)  kmem_cache_free(c, p)
184c793af95Ssangeeta #define	FreeHead(p, n)  kmem_free(p, n)
185c793af95Ssangeeta 
186c793af95Ssangeeta typedef struct radix_node rn_t;
187c793af95Ssangeeta typedef struct radix_mask rmsk_t;
188c793af95Ssangeeta typedef struct radix_node_head rnh_t;
189c793af95Ssangeeta typedef struct rt_sockaddr rt_sa_t;
190c793af95Ssangeeta 
191c793af95Ssangeeta #define	RADIX_NODE_HEAD_LOCK_INIT(rnh)	\
192c793af95Ssangeeta 	rw_init(&(rnh)->rnh_lock, NULL, RW_DEFAULT, NULL)
193c793af95Ssangeeta #define	RADIX_NODE_HEAD_RLOCK(rnh)	rw_enter(&(rnh)->rnh_lock, RW_READER)
194c793af95Ssangeeta #define	RADIX_NODE_HEAD_WLOCK(rnh)	rw_enter(&(rnh)->rnh_lock, RW_WRITER)
195c793af95Ssangeeta #define	RADIX_NODE_HEAD_UNLOCK(rnh)	rw_exit(&(rnh)->rnh_lock)
196c793af95Ssangeeta #define	RADIX_NODE_HEAD_DESTROY(rnh)	rw_destroy(&(rnh)->rnh_lock)
197c793af95Ssangeeta #define	RADIX_NODE_HEAD_LOCK_ASSERT(rnh) RW_WRITE_HELD(&(rnh)->rnh_lock)
198c793af95Ssangeeta 
199c793af95Ssangeeta #else /* _KERNEL */
200c793af95Ssangeeta 
201c793af95Ssangeeta #define	R_Malloc(p, t, n) (p =  malloc((unsigned int)(n)))
202c793af95Ssangeeta #define	R_Zalloc(p, t, n) (p =  calloc(1, (unsigned int)(n)))
203c793af95Ssangeeta #define	R_ZallocSleep(p, t, n) R_Zalloc(p, t, n)
204c793af95Ssangeeta #define	Free(p, c) free((char *)p); /* c is ignored */
205c793af95Ssangeeta #ifndef	RADIX_NODE_HEAD_RLOCK
206c793af95Ssangeeta #define	RADIX_NODE_HEAD_RLOCK(x)	/* */
207c793af95Ssangeeta #endif
208c793af95Ssangeeta #ifndef	RADIX_NODE_HEAD_WLOCK
209c793af95Ssangeeta #define	RADIX_NODE_HEAD_WLOCK(x)	/* */
210c793af95Ssangeeta #endif
211c793af95Ssangeeta #ifndef	RADIX_NODE_HEAD_UNLOCK
212c793af95Ssangeeta #define	RADIX_NODE_HEAD_UNLOCK(x)	/* */
213c793af95Ssangeeta #endif
214c793af95Ssangeeta 
215c793af95Ssangeeta #endif /* _KERNEL */
216c793af95Ssangeeta 
217c793af95Ssangeeta #ifndef min
218c793af95Ssangeeta #define	min MIN
219c793af95Ssangeeta #endif
220c793af95Ssangeeta #ifndef max
221c793af95Ssangeeta #define	max MAX
222c793af95Ssangeeta #endif
223c793af95Ssangeeta 
224c793af95Ssangeeta void	rn_init(void);
225c793af95Ssangeeta void	rn_fini(void);
226c793af95Ssangeeta int	rn_inithead(void **, int);
227c793af95Ssangeeta int	rn_freenode(struct radix_node *, void *);
228c793af95Ssangeeta void	rn_freehead(struct radix_node_head *);
229c793af95Ssangeeta 
230c793af95Ssangeeta #ifdef	__cplusplus
231c793af95Ssangeeta }
232c793af95Ssangeeta #endif
233c793af95Ssangeeta 
234c793af95Ssangeeta #endif /* _RADIX_H_ */
235