xref: /freebsd/sys/net/route/nhop_var.h (revision 19261079b74319502c6ffa1249920079f0f69a72)
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 bitmask_head	gr_idx_head;	/* nhgrp index head */
61 	struct rwlock		ctl_lock;	/* overall ctl lock */
62 	struct rib_head		*ctl_rh;	/* pointer back to rnh */
63 	struct epoch_context	ctl_epoch_ctx;	/* epoch ctl helper */
64 };
65 
66 #define	NHOPS_WLOCK(ctl)	rw_wlock(&(ctl)->ctl_lock)
67 #define	NHOPS_RLOCK(ctl)	rw_rlock(&(ctl)->ctl_lock)
68 #define	NHOPS_WUNLOCK(ctl)	rw_wunlock(&(ctl)->ctl_lock)
69 #define	NHOPS_RUNLOCK(ctl)	rw_runlock(&(ctl)->ctl_lock)
70 #define	NHOPS_LOCK_INIT(ctl)	rw_init(&(ctl)->ctl_lock, "nhop_ctl")
71 #define	NHOPS_LOCK_DESTROY(ctl)	rw_destroy(&(ctl)->ctl_lock)
72 #define	NHOPS_WLOCK_ASSERT(ctl)	rw_assert(&(ctl)->ctl_lock, RA_WLOCKED)
73 
74 /* Control plane-only nhop data */
75 struct nhop_object;
76 struct nhop_priv {
77 	/* nhop lookup comparison start */
78 	uint8_t			nh_family;	/* address family of the lookup */
79 	uint8_t			spare;
80 	uint16_t		nh_type;	/* nexthop type */
81 	uint32_t		rt_flags;	/* routing flags for the control plane */
82 	/* nhop lookup comparison end */
83 	uint32_t		nh_idx;		/* nexthop index */
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 	struct nhop_object	*nh;		/* backreference to the dataplane nhop */
88 	struct nh_control	*nh_control;	/* backreference to the rnh */
89 	struct nhop_priv	*nh_next;	/* hash table membership */
90 	struct vnet		*nh_vnet;	/* vnet nhop belongs to */
91 	struct epoch_context	nh_epoch_ctx;	/* epoch data for nhop */
92 };
93 
94 #define	NH_PRIV_END_CMP	(__offsetof(struct nhop_priv, nh_idx))
95 
96 #define	NH_IS_PINNED(_nh)	((!NH_IS_NHGRP(_nh)) && \
97 				((_nh)->nh_priv->rt_flags & RTF_PINNED))
98 
99 /* nhop.c */
100 struct nhop_priv *find_nhop(struct nh_control *ctl,
101     const struct nhop_priv *nh_priv);
102 int link_nhop(struct nh_control *ctl, struct nhop_priv *nh_priv);
103 struct nhop_priv *unlink_nhop(struct nh_control *ctl, struct nhop_priv *nh_priv);
104 
105 /* nhop_ctl.c */
106 int cmp_priv(const struct nhop_priv *_one, const struct nhop_priv *_two);
107 
108 #endif
109