1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1988, 1989, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 322f688f82SPaul Traina #ifndef _RADIX_H_ 332f688f82SPaul Traina #define _RADIX_H_ 34df8bae1dSRodney W. Grimes 358480e03dSJeffrey Hsu #ifdef _KERNEL 368480e03dSJeffrey Hsu #include <sys/_lock.h> 378480e03dSJeffrey Hsu #include <sys/_mutex.h> 3820efcfc6SAndrey V. Elsukov #include <sys/_rmlock.h> 398480e03dSJeffrey Hsu #endif 40956b0b65SJeffrey Hsu 41a1c995b6SPoul-Henning Kamp #ifdef MALLOC_DECLARE 42a1c995b6SPoul-Henning Kamp MALLOC_DECLARE(M_RTABLE); 43a1c995b6SPoul-Henning Kamp #endif 44a1c995b6SPoul-Henning Kamp 45df8bae1dSRodney W. Grimes /* 46df8bae1dSRodney W. Grimes * Radix search tree node layout. 47df8bae1dSRodney W. Grimes */ 48df8bae1dSRodney W. Grimes 49df8bae1dSRodney W. Grimes struct radix_node { 50df8bae1dSRodney W. Grimes struct radix_mask *rn_mklist; /* list of masks contained in subtree */ 511a11e63eSGarrett Wollman struct radix_node *rn_parent; /* parent */ 521a11e63eSGarrett Wollman short rn_bit; /* bit offset; -1-index(netmask) */ 53df8bae1dSRodney W. Grimes char rn_bmask; /* node: mask for bit test*/ 54df8bae1dSRodney W. Grimes u_char rn_flags; /* enumerated next */ 55df8bae1dSRodney W. Grimes #define RNF_NORMAL 1 /* leaf contains normal route */ 56df8bae1dSRodney W. Grimes #define RNF_ROOT 2 /* leaf is root leaf for tree */ 57df8bae1dSRodney W. Grimes #define RNF_ACTIVE 4 /* This node is alive (for rtfree) */ 58df8bae1dSRodney W. Grimes union { 59df8bae1dSRodney W. Grimes struct { /* leaf only data: */ 60df8bae1dSRodney W. Grimes caddr_t rn_Key; /* object of search */ 61df8bae1dSRodney W. Grimes caddr_t rn_Mask; /* netmask, if present */ 62df8bae1dSRodney W. Grimes struct radix_node *rn_Dupedkey; 63df8bae1dSRodney W. Grimes } rn_leaf; 64df8bae1dSRodney W. Grimes struct { /* node only data: */ 65df8bae1dSRodney W. Grimes int rn_Off; /* where to start compare */ 66df8bae1dSRodney W. Grimes struct radix_node *rn_L;/* progeny */ 67df8bae1dSRodney W. Grimes struct radix_node *rn_R;/* progeny */ 68df8bae1dSRodney W. Grimes } rn_node; 69df8bae1dSRodney W. Grimes } rn_u; 70df8bae1dSRodney W. Grimes #ifdef RN_DEBUG 71df8bae1dSRodney W. Grimes int rn_info; 72df8bae1dSRodney W. Grimes struct radix_node *rn_twin; 73df8bae1dSRodney W. Grimes struct radix_node *rn_ybro; 74df8bae1dSRodney W. Grimes #endif 75df8bae1dSRodney W. Grimes }; 76df8bae1dSRodney W. Grimes 77df8bae1dSRodney W. Grimes #define rn_dupedkey rn_u.rn_leaf.rn_Dupedkey 78df8bae1dSRodney W. Grimes #define rn_key rn_u.rn_leaf.rn_Key 79df8bae1dSRodney W. Grimes #define rn_mask rn_u.rn_leaf.rn_Mask 801a11e63eSGarrett Wollman #define rn_offset rn_u.rn_node.rn_Off 811a11e63eSGarrett Wollman #define rn_left rn_u.rn_node.rn_L 821a11e63eSGarrett Wollman #define rn_right rn_u.rn_node.rn_R 83df8bae1dSRodney W. Grimes 84df8bae1dSRodney W. Grimes /* 85df8bae1dSRodney W. Grimes * Annotations to tree concerning potential routes applying to subtrees. 86df8bae1dSRodney W. Grimes */ 87df8bae1dSRodney W. Grimes 88a3199409SBruce Evans struct radix_mask { 891a11e63eSGarrett Wollman short rm_bit; /* bit offset; -1-index(netmask) */ 90df8bae1dSRodney W. Grimes char rm_unused; /* cf. rn_bmask */ 91df8bae1dSRodney W. Grimes u_char rm_flags; /* cf. rn_flags */ 92df8bae1dSRodney W. Grimes struct radix_mask *rm_mklist; /* more masks to try */ 932f688f82SPaul Traina union { 942f688f82SPaul Traina caddr_t rmu_mask; /* the mask */ 952f688f82SPaul Traina struct radix_node *rmu_leaf; /* for normal routes */ 962f688f82SPaul Traina } rm_rmu; 97df8bae1dSRodney W. Grimes int rm_refs; /* # of references to this struct */ 98a3199409SBruce Evans }; 99df8bae1dSRodney W. Grimes 1002f688f82SPaul Traina #define rm_mask rm_rmu.rmu_mask 1012f688f82SPaul Traina #define rm_leaf rm_rmu.rmu_leaf /* extra field would make 32 bytes */ 1022f688f82SPaul Traina 10361eee0e2SAlexander V. Chernikov struct radix_head; 10461eee0e2SAlexander V. Chernikov 105929ddbbbSAlfred Perlstein typedef int walktree_f_t(struct radix_node *, void *); 106*97ffaff8SAlexander V. Chernikov typedef struct radix_node *rn_matchaddr_f_t(const void *v, 10761eee0e2SAlexander V. Chernikov struct radix_head *head); 108*97ffaff8SAlexander V. Chernikov typedef struct radix_node *rn_addaddr_f_t(void *v, const void *mask, 10961eee0e2SAlexander V. Chernikov struct radix_head *head, struct radix_node nodes[]); 110*97ffaff8SAlexander V. Chernikov typedef struct radix_node *rn_deladdr_f_t(const void *v, const void *mask, 11161eee0e2SAlexander V. Chernikov struct radix_head *head); 112*97ffaff8SAlexander V. Chernikov typedef struct radix_node *rn_lookup_f_t(const void *v, const void *mask, 11361eee0e2SAlexander V. Chernikov struct radix_head *head); 11461eee0e2SAlexander V. Chernikov typedef int rn_walktree_t(struct radix_head *head, walktree_f_t *f, 11561eee0e2SAlexander V. Chernikov void *w); 11661eee0e2SAlexander V. Chernikov typedef int rn_walktree_from_t(struct radix_head *head, 11761eee0e2SAlexander V. Chernikov void *a, void *m, walktree_f_t *f, void *w); 11861eee0e2SAlexander V. Chernikov typedef void rn_close_t(struct radix_node *rn, struct radix_head *head); 11936e15b71SAlexander V. Chernikov struct radix_node *rn_nextprefix(struct radix_node *rn); 12061eee0e2SAlexander V. Chernikov 12161eee0e2SAlexander V. Chernikov struct radix_mask_head; 12261eee0e2SAlexander V. Chernikov 12361eee0e2SAlexander V. Chernikov struct radix_head { 12461eee0e2SAlexander V. Chernikov struct radix_node *rnh_treetop; 12561eee0e2SAlexander V. Chernikov struct radix_mask_head *rnh_masks; /* Storage for our masks */ 12661eee0e2SAlexander V. Chernikov }; 12767df6ed3SBruce Evans 128df8bae1dSRodney W. Grimes struct radix_node_head { 12961eee0e2SAlexander V. Chernikov struct radix_head rh; 13061eee0e2SAlexander V. Chernikov rn_matchaddr_f_t *rnh_matchaddr; /* longest match for sockaddr */ 13161eee0e2SAlexander V. Chernikov rn_addaddr_f_t *rnh_addaddr; /* add based on sockaddr*/ 13261eee0e2SAlexander V. Chernikov rn_deladdr_f_t *rnh_deladdr; /* remove based on sockaddr */ 13361eee0e2SAlexander V. Chernikov rn_lookup_f_t *rnh_lookup; /* exact match for sockaddr */ 13461eee0e2SAlexander V. Chernikov rn_walktree_t *rnh_walktree; /* traverse tree */ 13561eee0e2SAlexander V. Chernikov rn_walktree_from_t *rnh_walktree_from; /* traverse tree below a */ 13661eee0e2SAlexander V. Chernikov rn_close_t *rnh_close; /*do something when the last ref drops*/ 137df8bae1dSRodney W. Grimes struct radix_node rnh_nodes[3]; /* empty tree for common case */ 1388480e03dSJeffrey Hsu #ifdef _KERNEL 13920efcfc6SAndrey V. Elsukov struct rmlock rnh_lock; /* locks entire radix tree */ 1408480e03dSJeffrey Hsu #endif 141df8bae1dSRodney W. Grimes }; 142df8bae1dSRodney W. Grimes 14361eee0e2SAlexander V. Chernikov struct radix_mask_head { 14461eee0e2SAlexander V. Chernikov struct radix_head head; 14561eee0e2SAlexander V. Chernikov struct radix_node mask_nodes[3]; 14661eee0e2SAlexander V. Chernikov }; 14761eee0e2SAlexander V. Chernikov 14861eee0e2SAlexander V. Chernikov void rn_inithead_internal(struct radix_head *rh, struct radix_node *base_nodes, 14961eee0e2SAlexander V. Chernikov int off); 15061eee0e2SAlexander V. Chernikov 151664a31e4SPeter Wemm #ifndef _KERNEL 152df8bae1dSRodney W. Grimes #define R_Malloc(p, t, n) (p = (t) malloc((unsigned int)(n))) 15391a9d708SSam Leffler #define R_Zalloc(p, t, n) (p = (t) calloc(1,(unsigned int)(n))) 15499440937SGleb Smirnoff #define R_Free(p) free((char *)p); 155df8bae1dSRodney W. Grimes #else 15686fea6beSBosko Milekic #define R_Malloc(p, t, n) (p = (t) malloc((unsigned long)(n), M_RTABLE, M_NOWAIT)) 15791a9d708SSam Leffler #define R_Zalloc(p, t, n) (p = (t) malloc((unsigned long)(n), M_RTABLE, M_NOWAIT | M_ZERO)) 1588b15f615SLuiz Otavio O Souza #define R_Free(p) free((caddr_t)p, M_RTABLE); 159956b0b65SJeffrey Hsu 160ae11b382SAndrey V. Elsukov #define RADIX_NODE_HEAD_RLOCK_TRACKER struct rm_priotracker _rnh_tracker 161956b0b65SJeffrey Hsu #define RADIX_NODE_HEAD_LOCK_INIT(rnh) \ 16220efcfc6SAndrey V. Elsukov rm_init(&(rnh)->rnh_lock, "radix node head") 16320efcfc6SAndrey V. Elsukov #define RADIX_NODE_HEAD_LOCK(rnh) rm_wlock(&(rnh)->rnh_lock) 16420efcfc6SAndrey V. Elsukov #define RADIX_NODE_HEAD_UNLOCK(rnh) rm_wunlock(&(rnh)->rnh_lock) 16520efcfc6SAndrey V. Elsukov #define RADIX_NODE_HEAD_RLOCK(rnh) rm_rlock(&(rnh)->rnh_lock,\ 166ae11b382SAndrey V. Elsukov &_rnh_tracker) 16720efcfc6SAndrey V. Elsukov #define RADIX_NODE_HEAD_RUNLOCK(rnh) rm_runlock(&(rnh)->rnh_lock,\ 168ae11b382SAndrey V. Elsukov &_rnh_tracker) 16920efcfc6SAndrey V. Elsukov #define RADIX_NODE_HEAD_DESTROY(rnh) rm_destroy(&(rnh)->rnh_lock) 17020efcfc6SAndrey V. Elsukov #define RADIX_NODE_HEAD_LOCK_ASSERT(rnh) rm_assert(&(rnh)->rnh_lock, RA_LOCKED) 17120efcfc6SAndrey V. Elsukov #define RADIX_NODE_HEAD_WLOCK_ASSERT(rnh) rm_assert(&(rnh)->rnh_lock, RA_WLOCKED) 172664a31e4SPeter Wemm #endif /* _KERNEL */ 173df8bae1dSRodney W. Grimes 174929ddbbbSAlfred Perlstein int rn_inithead(void **, int); 1751bb635b0SBjoern A. Zeeb int rn_detachhead(void **); 176*97ffaff8SAlexander V. Chernikov int rn_refines(const void *, const void *); 177*97ffaff8SAlexander V. Chernikov struct radix_node *rn_addroute(void *, const void *, struct radix_head *, 17861eee0e2SAlexander V. Chernikov struct radix_node[2]); 179*97ffaff8SAlexander V. Chernikov struct radix_node *rn_delete(const void *, const void *, struct radix_head *); 180*97ffaff8SAlexander V. Chernikov struct radix_node *rn_lookup (const void *v_arg, const void *m_arg, 18161eee0e2SAlexander V. Chernikov struct radix_head *head); 182*97ffaff8SAlexander V. Chernikov struct radix_node *rn_match(const void *, struct radix_head *); 18361eee0e2SAlexander V. Chernikov int rn_walktree_from(struct radix_head *h, void *a, void *m, 18461eee0e2SAlexander V. Chernikov walktree_f_t *f, void *w); 18561eee0e2SAlexander V. Chernikov int rn_walktree(struct radix_head *, walktree_f_t *, void *); 186f708ef1bSPoul-Henning Kamp 1872f688f82SPaul Traina #endif /* _RADIX_H_ */ 188