xref: /freebsd/sys/net/route/nhop_var.h (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
28 /*
29  * This header file contains private definitions for nexthop routing.
30  *
31  * Header is not intended to be included by the code external to the
32  * routing subsystem.
33  */
34 
35 #ifndef	_NET_ROUTE_NHOP_VAR_H_
36 #define	_NET_ROUTE_NHOP_VAR_H_
37 
38 MALLOC_DECLARE(M_NHOP);
39 
40 /* define nhop hash table */
41 struct nhop_priv;
42 CHT_SLIST_DEFINE(nhops, struct nhop_priv);
43 /* produce hash value for an object */
44 #define	nhops_hash_obj(_obj)	hash_priv(_obj)
45 /* compare two objects */
46 #define	nhops_cmp(_one, _two)	cmp_priv(_one, _two)
47 /* next object accessor */
48 #define	nhops_next(_obj)	(_obj)->nh_next
49 
50 /* define multipath hash table */
51 struct nhgrp_priv;
52 CHT_SLIST_DEFINE(nhgroups, struct nhgrp_priv);
53 
54 struct nh_control {
55 	struct nhops_head	nh_head;	/* hash table head */
56 	struct bitmask_head	nh_idx_head;	/* nhop index head */
57 	struct nhgroups_head	gr_head;	/* nhgrp hash table head */
58 	struct rwlock		ctl_lock;	/* overall ctl lock */
59 	struct rib_head		*ctl_rh;	/* pointer back to rnh */
60 	struct epoch_context	ctl_epoch_ctx;	/* epoch ctl helper */
61 };
62 
63 #define	NHOPS_WLOCK(ctl)	rw_wlock(&(ctl)->ctl_lock)
64 #define	NHOPS_RLOCK(ctl)	rw_rlock(&(ctl)->ctl_lock)
65 #define	NHOPS_WUNLOCK(ctl)	rw_wunlock(&(ctl)->ctl_lock)
66 #define	NHOPS_RUNLOCK(ctl)	rw_runlock(&(ctl)->ctl_lock)
67 #define	NHOPS_LOCK_INIT(ctl)	rw_init(&(ctl)->ctl_lock, "nhop_ctl")
68 #define	NHOPS_LOCK_DESTROY(ctl)	rw_destroy(&(ctl)->ctl_lock)
69 #define	NHOPS_WLOCK_ASSERT(ctl)	rw_assert(&(ctl)->ctl_lock, RA_WLOCKED)
70 
71 /* Control plane-only nhop data */
72 struct nhop_object;
73 struct nhop_priv {
74 	/* nhop lookup comparison start */
75 	uint8_t			nh_upper_family;/* address family of the lookup */
76 	uint8_t			nh_neigh_family;/* neighbor address family */
77 	uint16_t		nh_type;	/* nexthop type */
78 	uint32_t		rt_flags;	/* routing flags for the control plane */
79 	uint32_t		nh_expire;	/* path expiration time */
80 	uint32_t		nh_uidx;	/* userland-provided index */
81 	/* nhop lookup comparison end */
82 	uint32_t		nh_idx;		/* nexthop index */
83 	uint32_t		nh_fibnum;	/* nexthop fib */
84 	void			*cb_func;	/* function handling additional rewrite caps */
85 	u_int			nh_refcnt;	/* number of references, refcount(9)  */
86 	u_int			nh_linked;	/* refcount(9), == 2 if linked to the list */
87 	int			nh_finalized;	/* non-zero if finalized() was called */
88 	uint8_t			nh_origin;	/* protocol that originated the nexthop */
89 	struct nhop_object	*nh;		/* backreference to the dataplane nhop */
90 	struct nh_control	*nh_control;	/* backreference to the rnh */
91 	struct nhop_priv	*nh_next;	/* hash table membership */
92 	struct vnet		*nh_vnet;	/* vnet nhop belongs to */
93 	struct epoch_context	nh_epoch_ctx;	/* epoch data for nhop */
94 };
95 
96 #define	NH_PRIV_END_CMP	(__offsetof(struct nhop_priv, nh_idx))
97 
98 #define	NH_IS_PINNED(_nh)	((!NH_IS_NHGRP(_nh)) && \
99 				((_nh)->nh_priv->rt_flags & RTF_PINNED))
100 #define	NH_IS_LINKED(_nh)	((_nh)->nh_priv->nh_idx != 0)
101 
102 /* nhop.c */
103 struct nhop_priv *find_nhop(struct nh_control *ctl,
104     const struct nhop_priv *nh_priv);
105 int link_nhop(struct nh_control *ctl, struct nhop_priv *nh_priv);
106 struct nhop_priv *unlink_nhop(struct nh_control *ctl, struct nhop_priv *nh_priv);
107 
108 /* nhop_ctl.c */
109 int cmp_priv(const struct nhop_priv *_one, const struct nhop_priv *_two);
110 
111 #endif
112