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