]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_table.c
Merge pull request #8078 from idryzhov/fix-zebra-vni
[mirror_frr.git] / bgpd / bgp_table.c
1 /* BGP routing table
2 * Copyright (C) 1998, 2001 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "memory.h"
25 #include "sockunion.h"
26 #include "queue.h"
27 #include "filter.h"
28 #include "command.h"
29 #include "printfrr.h"
30
31 #include "bgpd/bgpd.h"
32 #include "bgpd/bgp_table.h"
33 #include "bgp_addpath.h"
34
35 void bgp_table_lock(struct bgp_table *rt)
36 {
37 rt->lock++;
38 }
39
40 void bgp_table_unlock(struct bgp_table *rt)
41 {
42 assert(rt->lock > 0);
43 rt->lock--;
44
45 if (rt->lock != 0) {
46 return;
47 }
48
49 route_table_finish(rt->route_table);
50 rt->route_table = NULL;
51
52 XFREE(MTYPE_BGP_TABLE, rt);
53 }
54
55 void bgp_table_finish(struct bgp_table **rt)
56 {
57 if (*rt != NULL) {
58 bgp_table_unlock(*rt);
59 *rt = NULL;
60 }
61 }
62
63 /*
64 * bgp_node_create
65 */
66 static struct route_node *bgp_node_create(route_table_delegate_t *delegate,
67 struct route_table *table)
68 {
69 struct bgp_node *node;
70 node = XCALLOC(MTYPE_BGP_NODE, sizeof(struct bgp_node));
71
72 RB_INIT(bgp_adj_out_rb, &node->adj_out);
73 return bgp_dest_to_rnode(node);
74 }
75
76 /*
77 * bgp_node_destroy
78 */
79 static void bgp_node_destroy(route_table_delegate_t *delegate,
80 struct route_table *table, struct route_node *node)
81 {
82 struct bgp_node *bgp_node;
83 struct bgp_table *rt;
84 bgp_node = bgp_dest_from_rnode(node);
85 rt = table->info;
86
87 if (rt->bgp) {
88 bgp_addpath_free_node_data(&rt->bgp->tx_addpath,
89 &bgp_node->tx_addpath,
90 rt->afi, rt->safi);
91 }
92
93 XFREE(MTYPE_BGP_NODE, bgp_node);
94 }
95
96 /*
97 * Function vector to customize the behavior of the route table
98 * library for BGP route tables.
99 */
100 route_table_delegate_t bgp_table_delegate = {.create_node = bgp_node_create,
101 .destroy_node = bgp_node_destroy};
102
103 /*
104 * bgp_table_init
105 */
106 struct bgp_table *bgp_table_init(struct bgp *bgp, afi_t afi, safi_t safi)
107 {
108 struct bgp_table *rt;
109
110 rt = XCALLOC(MTYPE_BGP_TABLE, sizeof(struct bgp_table));
111
112 rt->route_table = route_table_init_with_delegate(&bgp_table_delegate);
113
114 /*
115 * Set up back pointer to bgp_table.
116 */
117 route_table_set_info(rt->route_table, rt);
118
119 /*
120 * pointer to bgp instance allows working back from bgp_path_info to bgp
121 */
122 rt->bgp = bgp;
123
124 bgp_table_lock(rt);
125 rt->afi = afi;
126 rt->safi = safi;
127
128 return rt;
129 }
130
131 /* Delete the route node from the selection deferral route list */
132 void bgp_delete_listnode(struct bgp_node *node)
133 {
134 struct route_node *rn = NULL;
135 struct bgp_table *table = NULL;
136 struct bgp *bgp = NULL;
137 afi_t afi;
138 safi_t safi;
139
140 /* If the route to be deleted is selection pending, update the
141 * route node in gr_info
142 */
143 if (CHECK_FLAG(node->flags, BGP_NODE_SELECT_DEFER)) {
144 table = bgp_dest_table(node);
145
146 if (table) {
147 bgp = table->bgp;
148 afi = table->afi;
149 safi = table->safi;
150 } else
151 return;
152
153 rn = bgp_dest_to_rnode(node);
154
155 if (bgp && rn && rn->lock == 1) {
156 /* Delete the route from the selection pending list */
157 bgp->gr_info[afi][safi].gr_deferred--;
158 UNSET_FLAG(node->flags, BGP_NODE_SELECT_DEFER);
159 }
160 }
161 }
162
163 struct bgp_node *bgp_table_subtree_lookup(const struct bgp_table *table,
164 const struct prefix *p)
165 {
166 struct bgp_node *node = bgp_dest_from_rnode(table->route_table->top);
167 struct bgp_node *matched = NULL;
168
169 if (node == NULL)
170 return NULL;
171
172
173 while (node) {
174 const struct prefix *node_p = bgp_dest_get_prefix(node);
175
176 if (node_p->prefixlen >= p->prefixlen) {
177 if (!prefix_match(p, node_p))
178 return NULL;
179
180 matched = node;
181 break;
182 }
183
184 if (!prefix_match(node_p, p))
185 return NULL;
186
187 if (node_p->prefixlen == p->prefixlen) {
188 matched = node;
189 break;
190 }
191
192 node = bgp_dest_from_rnode(node->link[prefix_bit(
193 &p->u.prefix, node_p->prefixlen)]);
194 }
195
196 if (!matched)
197 return NULL;
198
199 bgp_dest_lock_node(matched);
200 return matched;
201 }
202
203 printfrr_ext_autoreg_p("BD", printfrr_bd)
204 static ssize_t printfrr_bd(char *buf, size_t bsz, const char *fmt,
205 int prec, const void *ptr)
206 {
207 const struct bgp_dest *dest = ptr;
208 const struct prefix *p;
209
210 if (dest) {
211 p = bgp_dest_get_prefix(dest);
212 prefix2str(p, buf, bsz);
213 } else {
214 strlcpy(buf, "NULL", bsz);
215 }
216
217 return 2;
218 }