xref: /freebsd/sys/net/route/fib_algo.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * Copyright (c) 2020
3  * 	Alexander V. Chernikov <melifaro@FreeBSD.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 
31 struct fib_data;
32 struct fib_dp;
33 enum flm_op_result {
34 	FLM_SUCCESS,	/* No errors, operation successful */
35 	FLM_REBUILD,	/* Operation cannot be completed, schedule algorithm rebuild */
36 	FLM_ERROR,	/* Operation failed, this algo cannot be used */
37 	FLM_BATCH,	/* Operation cannot be completed, algorithm asks to batch changes */
38 };
39 
40 struct rib_rtable_info {
41 	uint32_t num_prefixes;
42 	uint32_t num_nhops;
43 	uint32_t num_nhgrp;
44 };
45 
46 struct flm_lookup_key {
47 	union {
48 		const struct in6_addr *addr6;
49 		struct in_addr addr4;
50 	};
51 };
52 
53 struct fib_change_entry {
54 	union {
55 		struct in_addr	addr4;
56 		struct in6_addr	addr6;
57 	};
58 	uint32_t		scopeid;
59 	uint8_t			plen;
60 	struct nhop_object	*nh_old;
61 	struct nhop_object	*nh_new;
62 };
63 
64 struct fib_change_queue {
65 	uint32_t 		count;
66 	uint32_t		size;
67 	struct fib_change_entry	*entries;
68 };
69 
70 
71 typedef struct nhop_object *flm_lookup_t(void *algo_data,
72     const struct flm_lookup_key key, uint32_t scopeid);
73 typedef enum flm_op_result flm_init_t (uint32_t fibnum, struct fib_data *fd,
74     void *_old_data, void **new_data);
75 typedef void flm_destroy_t(void *data);
76 typedef enum flm_op_result flm_dump_t(struct rtentry *rt, void *data);
77 typedef enum flm_op_result flm_dump_end_t(void *data, struct fib_dp *dp);
78 typedef enum flm_op_result flm_change_t(struct rib_head *rnh,
79     struct rib_cmd_info *rc, void *data);
80 typedef enum flm_op_result flm_change_batch_t(struct rib_head *rnh,
81     struct fib_change_queue *q, void *data);
82 typedef uint8_t flm_get_pref_t(const struct rib_rtable_info *rinfo);
83 
84 struct fib_lookup_module {
85 	char		*flm_name;		/* algo name */
86 	int		flm_family;		/* address family this module supports */
87 	int		flm_refcount;		/* # of references */
88 	uint32_t	flm_flags;		/* flags */
89 	uint8_t		flm_index;		/* internal algo index */
90 	flm_init_t	*flm_init_cb;		/* instance init */
91 	flm_destroy_t	*flm_destroy_cb;	/* destroy instance */
92 	flm_change_t	*flm_change_rib_item_cb;/* routing table change hook */
93 	flm_dump_t	*flm_dump_rib_item_cb;	/* routing table dump cb */
94 	flm_dump_end_t	*flm_dump_end_cb;	/* end of dump */
95 	flm_lookup_t	*flm_lookup;		/* lookup function */
96 	flm_get_pref_t	*flm_get_pref;		/* get algo preference */
97 	flm_change_batch_t	*flm_change_rib_items_cb;/* routing table change hook */
98 	void		*spare[8];		/* Spare callbacks */
99 	TAILQ_ENTRY(fib_lookup_module)	entries;
100 };
101 
102 /* Datapath lookup data */
103 struct fib_dp {
104 	flm_lookup_t	*f;
105 	void		*arg;
106 };
107 
108 VNET_DECLARE(struct fib_dp *, inet_dp);
109 #define	V_inet_dp	VNET(inet_dp)
110 VNET_DECLARE(struct fib_dp *, inet6_dp);
111 #define	V_inet6_dp	VNET(inet6_dp)
112 
113 #define	FIB_PRINTF(_l, _fd, _fmt, ...)	fib_printf(_l, _fd, __func__, _fmt, ##__VA_ARGS__)
114 
115 void fib_printf(int level, struct fib_data *fd, const char *func, char *fmt, ...);
116 int fib_module_init(struct fib_lookup_module *flm, uint32_t fibnum,
117     int family);
118 int fib_module_clone(const struct fib_lookup_module *flm_orig,
119     struct fib_lookup_module *flm, bool waitok);
120 int fib_module_dumptree(struct fib_lookup_module *flm,
121     enum rib_subscription_type subscription_type);
122 int fib_module_register(struct fib_lookup_module *flm);
123 int fib_module_unregister(struct fib_lookup_module *flm);
124 
125 uint32_t fib_get_nhop_idx(struct fib_data *fd, struct nhop_object *nh);
126 struct nhop_object **fib_get_nhop_array(struct fib_data *fd);
127 void fib_get_rtable_info(struct rib_head *rh, struct rib_rtable_info *rinfo);
128 struct rib_head *fib_get_rh(struct fib_data *fd);
129 bool fib_set_datapath_ptr(struct fib_data *fd, struct fib_dp *dp);
130 void fib_set_algo_ptr(struct fib_data *fd, void *algo_data);
131 void fib_epoch_call(epoch_callback_t callback, epoch_context_t ctx);
132 
133