]>
Commit | Line | Data |
---|---|---|
89272910 DS |
1 | /* Zebra Router Code. |
2 | * Copyright (C) 2018 Cumulus Networks, Inc. | |
3 | * Donald Sharp | |
4 | * | |
5 | * This file is part of FRR. | |
6 | * | |
7 | * FRR is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * FRR is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with FRR; see the file COPYING. If not, write to the Free | |
19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | #include "zebra.h" | |
23 | ||
1485bbe7 DS |
24 | #include <pthread.h> |
25 | #include "lib/frratomic.h" | |
26 | ||
89272910 | 27 | #include "zebra_router.h" |
7f0ea8a4 | 28 | #include "zebra_pbr.h" |
6548050a | 29 | #include "zebra_vxlan.h" |
df395600 | 30 | #include "zebra_mlag.h" |
0eb97b86 | 31 | #include "zebra_nhg.h" |
526052fb | 32 | #include "debug.h" |
cbefb650 | 33 | #include "zebra_script.h" |
89272910 | 34 | |
bf8d3d6a | 35 | DEFINE_MTYPE_STATIC(ZEBRA, RIB_TABLE_INFO, "RIB table info"); |
224ccf29 | 36 | DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_RT_TABLE, "Zebra VRF table"); |
c1344b54 | 37 | |
b3f2b590 DS |
38 | struct zebra_router zrouter = { |
39 | .multipath_num = MULTIPATH_NUM, | |
526052fb | 40 | .ipv4_multicast_mode = MCAST_NO_CONFIG, |
b3f2b590 | 41 | }; |
89272910 DS |
42 | |
43 | static inline int | |
44 | zebra_router_table_entry_compare(const struct zebra_router_table *e1, | |
45 | const struct zebra_router_table *e2); | |
46 | ||
47 | RB_GENERATE(zebra_router_table_head, zebra_router_table, | |
48 | zebra_router_table_entry, zebra_router_table_entry_compare); | |
49 | ||
50 | ||
51 | static inline int | |
52 | zebra_router_table_entry_compare(const struct zebra_router_table *e1, | |
53 | const struct zebra_router_table *e2) | |
54 | { | |
55 | if (e1->tableid < e2->tableid) | |
56 | return -1; | |
57 | if (e1->tableid > e2->tableid) | |
58 | return 1; | |
59 | if (e1->ns_id < e2->ns_id) | |
60 | return -1; | |
61 | if (e1->ns_id > e2->ns_id) | |
62 | return 1; | |
63 | if (e1->afi < e2->afi) | |
64 | return -1; | |
65 | if (e1->afi > e2->afi) | |
66 | return 1; | |
67 | return (e1->safi - e2->safi); | |
68 | } | |
69 | ||
9d86e091 CS |
70 | struct zebra_router_table *zebra_router_find_zrt(struct zebra_vrf *zvrf, |
71 | uint32_t tableid, afi_t afi, | |
72 | safi_t safi) | |
73 | { | |
74 | struct zebra_router_table finder; | |
75 | struct zebra_router_table *zrt; | |
76 | ||
77 | memset(&finder, 0, sizeof(finder)); | |
78 | finder.afi = afi; | |
79 | finder.safi = safi; | |
80 | finder.tableid = tableid; | |
81 | finder.ns_id = zvrf->zns->ns_id; | |
82 | zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder); | |
83 | ||
84 | return zrt; | |
85 | } | |
89272910 DS |
86 | |
87 | struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf, | |
88 | uint32_t tableid, afi_t afi, | |
89 | safi_t safi) | |
90 | { | |
91 | struct zebra_router_table finder; | |
92 | struct zebra_router_table *zrt; | |
93 | ||
94 | memset(&finder, 0, sizeof(finder)); | |
95 | finder.afi = afi; | |
96 | finder.safi = safi; | |
97 | finder.tableid = tableid; | |
98 | finder.ns_id = zvrf->zns->ns_id; | |
99 | zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder); | |
100 | ||
101 | if (zrt) | |
102 | return zrt->table; | |
103 | else | |
104 | return NULL; | |
105 | } | |
106 | ||
107 | struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf, | |
108 | uint32_t tableid, afi_t afi, | |
109 | safi_t safi) | |
110 | { | |
111 | struct zebra_router_table finder; | |
112 | struct zebra_router_table *zrt; | |
630d5962 | 113 | struct rib_table_info *info; |
89272910 DS |
114 | |
115 | memset(&finder, 0, sizeof(finder)); | |
116 | finder.afi = afi; | |
117 | finder.safi = safi; | |
118 | finder.tableid = tableid; | |
119 | finder.ns_id = zvrf->zns->ns_id; | |
120 | zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder); | |
121 | ||
122 | if (zrt) | |
123 | return zrt->table; | |
124 | ||
224ccf29 | 125 | zrt = XCALLOC(MTYPE_ZEBRA_RT_TABLE, sizeof(*zrt)); |
89272910 DS |
126 | zrt->tableid = tableid; |
127 | zrt->afi = afi; | |
99b2c423 | 128 | zrt->safi = safi; |
89272910 DS |
129 | zrt->ns_id = zvrf->zns->ns_id; |
130 | zrt->table = | |
131 | (afi == AFI_IP6) ? srcdest_table_init() : route_table_init(); | |
132 | ||
133 | info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info)); | |
134 | info->zvrf = zvrf; | |
135 | info->afi = afi; | |
ea66cec4 | 136 | info->safi = safi; |
b62983cf | 137 | info->table_id = tableid; |
89272910 DS |
138 | route_table_set_info(zrt->table, info); |
139 | zrt->table->cleanup = zebra_rtable_node_cleanup; | |
140 | ||
141 | RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt); | |
142 | return zrt->table; | |
143 | } | |
144 | ||
ac5aa23f DS |
145 | void zebra_router_show_table_summary(struct vty *vty) |
146 | { | |
147 | struct zebra_router_table *zrt; | |
148 | ||
149 | vty_out(vty, | |
150 | "VRF NS ID VRF ID AFI SAFI Table Count\n"); | |
151 | vty_out(vty, | |
152 | "---------------------------------------------------------------------------\n"); | |
153 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { | |
630d5962 | 154 | struct rib_table_info *info = route_table_get_info(zrt->table); |
ac5aa23f DS |
155 | |
156 | vty_out(vty, "%-16s%5d %9d %7s %15s %8d %10lu\n", info->zvrf->vrf->name, | |
157 | zrt->ns_id, info->zvrf->vrf->vrf_id, | |
158 | afi2str(zrt->afi), safi2str(zrt->safi), | |
159 | zrt->tableid, | |
160 | zrt->table->count); | |
161 | } | |
162 | } | |
163 | ||
89272910 DS |
164 | void zebra_router_sweep_route(void) |
165 | { | |
166 | struct zebra_router_table *zrt; | |
167 | ||
168 | RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) { | |
169 | if (zrt->ns_id != NS_DEFAULT) | |
170 | continue; | |
171 | rib_sweep_table(zrt->table); | |
172 | } | |
173 | } | |
174 | ||
38e40db1 SW |
175 | void zebra_router_sweep_nhgs(void) |
176 | { | |
177 | zebra_nhg_sweep_table(zrouter.nhgs_id); | |
178 | } | |
179 | ||
89272910 DS |
180 | static void zebra_router_free_table(struct zebra_router_table *zrt) |
181 | { | |
182 | void *table_info; | |
183 | ||
89272910 DS |
184 | table_info = route_table_get_info(zrt->table); |
185 | route_table_finish(zrt->table); | |
bd4fb615 DS |
186 | RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt); |
187 | ||
89272910 | 188 | XFREE(MTYPE_RIB_TABLE_INFO, table_info); |
224ccf29 | 189 | XFREE(MTYPE_ZEBRA_RT_TABLE, zrt); |
89272910 DS |
190 | } |
191 | ||
bd4fb615 DS |
192 | void zebra_router_release_table(struct zebra_vrf *zvrf, uint32_t tableid, |
193 | afi_t afi, safi_t safi) | |
194 | { | |
195 | struct zebra_router_table finder; | |
196 | struct zebra_router_table *zrt; | |
197 | ||
198 | memset(&finder, 0, sizeof(finder)); | |
199 | finder.afi = afi; | |
200 | finder.safi = safi; | |
201 | finder.tableid = tableid; | |
202 | finder.ns_id = zvrf->zns->ns_id; | |
203 | zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder); | |
204 | ||
205 | if (!zrt) | |
206 | return; | |
207 | ||
208 | zebra_router_free_table(zrt); | |
209 | } | |
210 | ||
1485bbe7 DS |
211 | uint32_t zebra_router_get_next_sequence(void) |
212 | { | |
213 | return 1 | |
214 | + atomic_fetch_add_explicit(&zrouter.sequence_num, 1, | |
215 | memory_order_relaxed); | |
216 | } | |
217 | ||
526052fb DS |
218 | void multicast_mode_ipv4_set(enum multicast_mode mode) |
219 | { | |
220 | if (IS_ZEBRA_DEBUG_RIB) | |
221 | zlog_debug("%s: multicast lookup mode set (%d)", __func__, | |
222 | mode); | |
223 | zrouter.ipv4_multicast_mode = mode; | |
224 | } | |
225 | ||
226 | enum multicast_mode multicast_mode_ipv4_get(void) | |
227 | { | |
228 | return zrouter.ipv4_multicast_mode; | |
229 | } | |
230 | ||
89272910 DS |
231 | void zebra_router_terminate(void) |
232 | { | |
233 | struct zebra_router_table *zrt, *tmp; | |
234 | ||
8a88f815 | 235 | RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp) |
89272910 | 236 | zebra_router_free_table(zrt); |
7f0ea8a4 | 237 | |
dd9538c5 MS |
238 | if (zrouter.lsp_process_q) |
239 | work_queue_free_and_null(&zrouter.lsp_process_q); | |
240 | ||
489a9614 | 241 | work_queue_free_and_null(&zrouter.ribq); |
ea45a4e7 | 242 | meta_queue_free(zrouter.mq); |
489a9614 | 243 | |
6548050a | 244 | zebra_vxlan_disable(); |
df395600 DS |
245 | zebra_mlag_terminate(); |
246 | ||
c25c3ea5 SW |
247 | /* Free NHE in ID table only since it has unhashable entries as well */ |
248 | hash_clean(zrouter.nhgs_id, zebra_nhg_hash_free); | |
d9f5b2f5 | 249 | hash_free(zrouter.nhgs_id); |
c25c3ea5 SW |
250 | hash_clean(zrouter.nhgs, NULL); |
251 | hash_free(zrouter.nhgs); | |
d9f5b2f5 | 252 | |
7f0ea8a4 DS |
253 | hash_clean(zrouter.rules_hash, zebra_pbr_rules_free); |
254 | hash_free(zrouter.rules_hash); | |
62f20a52 DS |
255 | |
256 | hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free), | |
257 | hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free); | |
258 | hash_free(zrouter.ipset_hash); | |
259 | hash_free(zrouter.ipset_entry_hash); | |
260 | hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free); | |
261 | hash_free(zrouter.iptable_hash); | |
461c173c DL |
262 | |
263 | #ifdef HAVE_SCRIPTING | |
264 | zebra_script_destroy(); | |
265 | #endif | |
89272910 DS |
266 | } |
267 | ||
e4876266 DS |
268 | bool zebra_router_notify_on_ack(void) |
269 | { | |
270 | return !zrouter.asic_offloaded || zrouter.notify_on_ack; | |
271 | } | |
272 | ||
273 | void zebra_router_init(bool asic_offload, bool notify_on_ack) | |
89272910 | 274 | { |
1485bbe7 DS |
275 | zrouter.sequence_num = 0; |
276 | ||
5ec5a716 | 277 | zrouter.packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS; |
b3d43ff4 | 278 | |
6548050a | 279 | zebra_vxlan_init(); |
df395600 DS |
280 | zebra_mlag_init(); |
281 | ||
7f0ea8a4 DS |
282 | zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key, |
283 | zebra_pbr_rules_hash_equal, | |
284 | "Rules Hash"); | |
62f20a52 DS |
285 | |
286 | zrouter.ipset_hash = | |
287 | hash_create_size(8, zebra_pbr_ipset_hash_key, | |
288 | zebra_pbr_ipset_hash_equal, "IPset Hash"); | |
289 | ||
290 | zrouter.ipset_entry_hash = hash_create_size( | |
291 | 8, zebra_pbr_ipset_entry_hash_key, | |
292 | zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry"); | |
293 | ||
294 | zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key, | |
295 | zebra_pbr_iptable_hash_equal, | |
296 | "IPtable Hash Entry"); | |
69171da2 DS |
297 | |
298 | zrouter.nhgs = | |
299 | hash_create_size(8, zebra_nhg_hash_key, zebra_nhg_hash_equal, | |
300 | "Zebra Router Nexthop Groups"); | |
a95b8020 | 301 | zrouter.nhgs_id = |
d9f5b2f5 | 302 | hash_create_size(8, zebra_nhg_id_key, zebra_nhg_hash_id_equal, |
a95b8020 | 303 | "Zebra Router Nexthop Groups ID index"); |
4c56ce1c | 304 | |
e4876266 DS |
305 | zrouter.asic_offloaded = asic_offload; |
306 | zrouter.notify_on_ack = notify_on_ack; | |
461c173c DL |
307 | |
308 | #ifdef HAVE_SCRIPTING | |
309 | zebra_script_init(); | |
310 | #endif | |
89272910 | 311 | } |