1a6663252SAlexander V. Chernikov /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3a6663252SAlexander V. Chernikov * 4a6663252SAlexander V. Chernikov * Copyright (c) 2020 Alexander V. Chernikov 5a6663252SAlexander V. Chernikov * 6a6663252SAlexander V. Chernikov * Redistribution and use in source and binary forms, with or without 7a6663252SAlexander V. Chernikov * modification, are permitted provided that the following conditions 8a6663252SAlexander V. Chernikov * are met: 9a6663252SAlexander V. Chernikov * 1. Redistributions of source code must retain the above copyright 10a6663252SAlexander V. Chernikov * notice, this list of conditions and the following disclaimer. 11a6663252SAlexander V. Chernikov * 2. Redistributions in binary form must reproduce the above copyright 12a6663252SAlexander V. Chernikov * notice, this list of conditions and the following disclaimer in the 13a6663252SAlexander V. Chernikov * documentation and/or other materials provided with the distribution. 14a6663252SAlexander V. Chernikov * 15a6663252SAlexander V. Chernikov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16a6663252SAlexander V. Chernikov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17a6663252SAlexander V. Chernikov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18a6663252SAlexander V. Chernikov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19a6663252SAlexander V. Chernikov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20a6663252SAlexander V. Chernikov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21a6663252SAlexander V. Chernikov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22a6663252SAlexander V. Chernikov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23a6663252SAlexander V. Chernikov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24a6663252SAlexander V. Chernikov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25a6663252SAlexander V. Chernikov * SUCH DAMAGE. 26a6663252SAlexander V. Chernikov */ 27a6663252SAlexander V. Chernikov 28a6663252SAlexander V. Chernikov /* 29a6663252SAlexander V. Chernikov * This header file contains private definitions for nexthop routing. 30a6663252SAlexander V. Chernikov * 31a6663252SAlexander V. Chernikov * Header is not intended to be included by the code external to the 32a6663252SAlexander V. Chernikov * routing subsystem. 33a6663252SAlexander V. Chernikov */ 34a6663252SAlexander V. Chernikov 35a6663252SAlexander V. Chernikov #ifndef _NET_ROUTE_NHOP_VAR_H_ 36a6663252SAlexander V. Chernikov #define _NET_ROUTE_NHOP_VAR_H_ 37a6663252SAlexander V. Chernikov 38fedeb08bSAlexander V. Chernikov MALLOC_DECLARE(M_NHOP); 39fedeb08bSAlexander V. Chernikov 40a6663252SAlexander V. Chernikov /* define nhop hash table */ 41a6663252SAlexander V. Chernikov struct nhop_priv; 42a6663252SAlexander V. Chernikov CHT_SLIST_DEFINE(nhops, struct nhop_priv); 43a6663252SAlexander V. Chernikov /* produce hash value for an object */ 44a6663252SAlexander V. Chernikov #define nhops_hash_obj(_obj) hash_priv(_obj) 45a6663252SAlexander V. Chernikov /* compare two objects */ 46a6663252SAlexander V. Chernikov #define nhops_cmp(_one, _two) cmp_priv(_one, _two) 47a6663252SAlexander V. Chernikov /* next object accessor */ 48a6663252SAlexander V. Chernikov #define nhops_next(_obj) (_obj)->nh_next 49a6663252SAlexander V. Chernikov 50fedeb08bSAlexander V. Chernikov /* define multipath hash table */ 51fedeb08bSAlexander V. Chernikov struct nhgrp_priv; 52fedeb08bSAlexander V. Chernikov CHT_SLIST_DEFINE(nhgroups, struct nhgrp_priv); 53fedeb08bSAlexander V. Chernikov 54a6663252SAlexander V. Chernikov struct nh_control { 55a6663252SAlexander V. Chernikov struct nhops_head nh_head; /* hash table head */ 56a6663252SAlexander V. Chernikov struct bitmask_head nh_idx_head; /* nhop index head */ 57fedeb08bSAlexander V. Chernikov struct nhgroups_head gr_head; /* nhgrp hash table head */ 58a6663252SAlexander V. Chernikov struct rwlock ctl_lock; /* overall ctl lock */ 59a6663252SAlexander V. Chernikov struct rib_head *ctl_rh; /* pointer back to rnh */ 60a6663252SAlexander V. Chernikov struct epoch_context ctl_epoch_ctx; /* epoch ctl helper */ 61a6663252SAlexander V. Chernikov }; 62a6663252SAlexander V. Chernikov 63a6663252SAlexander V. Chernikov #define NHOPS_WLOCK(ctl) rw_wlock(&(ctl)->ctl_lock) 64a6663252SAlexander V. Chernikov #define NHOPS_RLOCK(ctl) rw_rlock(&(ctl)->ctl_lock) 65a6663252SAlexander V. Chernikov #define NHOPS_WUNLOCK(ctl) rw_wunlock(&(ctl)->ctl_lock) 66a6663252SAlexander V. Chernikov #define NHOPS_RUNLOCK(ctl) rw_runlock(&(ctl)->ctl_lock) 67a6663252SAlexander V. Chernikov #define NHOPS_LOCK_INIT(ctl) rw_init(&(ctl)->ctl_lock, "nhop_ctl") 68a6663252SAlexander V. Chernikov #define NHOPS_LOCK_DESTROY(ctl) rw_destroy(&(ctl)->ctl_lock) 69a6663252SAlexander V. Chernikov #define NHOPS_WLOCK_ASSERT(ctl) rw_assert(&(ctl)->ctl_lock, RA_WLOCKED) 70a6663252SAlexander V. Chernikov 71a6663252SAlexander V. Chernikov /* Control plane-only nhop data */ 72a6663252SAlexander V. Chernikov struct nhop_object; 73a6663252SAlexander V. Chernikov struct nhop_priv { 741b95005eSAlexander V. Chernikov /* nhop lookup comparison start */ 75823a08d7SAlexander V. Chernikov uint8_t nh_upper_family;/* address family of the lookup */ 76823a08d7SAlexander V. Chernikov uint8_t nh_neigh_family;/* neighbor address family */ 77a6663252SAlexander V. Chernikov uint16_t nh_type; /* nexthop type */ 781b95005eSAlexander V. Chernikov uint32_t rt_flags; /* routing flags for the control plane */ 792717e958SAlexander V. Chernikov uint32_t nh_expire; /* path expiration time */ 80db4ca190SAlexander V. Chernikov uint32_t nh_uidx; /* userland-provided index */ 811b95005eSAlexander V. Chernikov /* nhop lookup comparison end */ 821b95005eSAlexander V. Chernikov uint32_t nh_idx; /* nexthop index */ 83823a08d7SAlexander V. Chernikov uint32_t nh_fibnum; /* nexthop fib */ 84a6663252SAlexander V. Chernikov void *cb_func; /* function handling additional rewrite caps */ 85a6663252SAlexander V. Chernikov u_int nh_refcnt; /* number of references, refcount(9) */ 86a6663252SAlexander V. Chernikov u_int nh_linked; /* refcount(9), == 2 if linked to the list */ 87800c6846SAlexander V. Chernikov int nh_finalized; /* non-zero if finalized() was called */ 88000250beSAlexander V. Chernikov uint8_t nh_origin; /* protocol that originated the nexthop */ 89a6663252SAlexander V. Chernikov struct nhop_object *nh; /* backreference to the dataplane nhop */ 90a6663252SAlexander V. Chernikov struct nh_control *nh_control; /* backreference to the rnh */ 91a6663252SAlexander V. Chernikov struct nhop_priv *nh_next; /* hash table membership */ 922bbab0afSAlexander V. Chernikov struct vnet *nh_vnet; /* vnet nhop belongs to */ 93a6663252SAlexander V. Chernikov struct epoch_context nh_epoch_ctx; /* epoch data for nhop */ 94a6663252SAlexander V. Chernikov }; 95a6663252SAlexander V. Chernikov 961b95005eSAlexander V. Chernikov #define NH_PRIV_END_CMP (__offsetof(struct nhop_priv, nh_idx)) 971b95005eSAlexander V. Chernikov 98fedeb08bSAlexander V. Chernikov #define NH_IS_PINNED(_nh) ((!NH_IS_NHGRP(_nh)) && \ 99fedeb08bSAlexander V. Chernikov ((_nh)->nh_priv->rt_flags & RTF_PINNED)) 1002717e958SAlexander V. Chernikov #define NH_IS_LINKED(_nh) ((_nh)->nh_priv->nh_idx != 0) 101a6663252SAlexander V. Chernikov 102a6663252SAlexander V. Chernikov /* nhop.c */ 103a6663252SAlexander V. Chernikov struct nhop_priv *find_nhop(struct nh_control *ctl, 104a6663252SAlexander V. Chernikov const struct nhop_priv *nh_priv); 105a6663252SAlexander V. Chernikov int link_nhop(struct nh_control *ctl, struct nhop_priv *nh_priv); 106a6663252SAlexander V. Chernikov struct nhop_priv *unlink_nhop(struct nh_control *ctl, struct nhop_priv *nh_priv); 107a6663252SAlexander V. Chernikov 108a6663252SAlexander V. Chernikov /* nhop_ctl.c */ 109a6663252SAlexander V. Chernikov int cmp_priv(const struct nhop_priv *_one, const struct nhop_priv *_two); 110a6663252SAlexander V. Chernikov 111a6663252SAlexander V. Chernikov #endif 112