]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_router.c
Merge pull request #11302 from punith-shivakumar/master
[mirror_frr.git] / zebra / zebra_router.c
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
24 #include <pthread.h>
25 #include "lib/frratomic.h"
26
27 #include "zebra_router.h"
28 #include "zebra_pbr.h"
29 #include "zebra_vxlan.h"
30 #include "zebra_mlag.h"
31 #include "zebra_nhg.h"
32 #include "zebra_neigh.h"
33 #include "debug.h"
34 #include "zebra_script.h"
35
36 DEFINE_MTYPE_STATIC(ZEBRA, RIB_TABLE_INFO, "RIB table info");
37 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_RT_TABLE, "Zebra VRF table");
38
39 struct zebra_router zrouter = {
40 .multipath_num = MULTIPATH_NUM,
41 .ipv4_multicast_mode = MCAST_NO_CONFIG,
42 };
43
44 static inline int
45 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
46 const struct zebra_router_table *e2);
47
48 RB_GENERATE(zebra_router_table_head, zebra_router_table,
49 zebra_router_table_entry, zebra_router_table_entry_compare);
50
51
52 static inline int
53 zebra_router_table_entry_compare(const struct zebra_router_table *e1,
54 const struct zebra_router_table *e2)
55 {
56 if (e1->tableid < e2->tableid)
57 return -1;
58 if (e1->tableid > e2->tableid)
59 return 1;
60 if (e1->ns_id < e2->ns_id)
61 return -1;
62 if (e1->ns_id > e2->ns_id)
63 return 1;
64 if (e1->afi < e2->afi)
65 return -1;
66 if (e1->afi > e2->afi)
67 return 1;
68 return (e1->safi - e2->safi);
69 }
70
71 struct zebra_router_table *zebra_router_find_zrt(struct zebra_vrf *zvrf,
72 uint32_t tableid, afi_t afi,
73 safi_t safi)
74 {
75 struct zebra_router_table finder;
76 struct zebra_router_table *zrt;
77
78 memset(&finder, 0, sizeof(finder));
79 finder.afi = afi;
80 finder.safi = safi;
81 finder.tableid = tableid;
82 finder.ns_id = zvrf->zns->ns_id;
83 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
84
85 return zrt;
86 }
87
88 struct route_table *zebra_router_find_table(struct zebra_vrf *zvrf,
89 uint32_t tableid, afi_t afi,
90 safi_t safi)
91 {
92 struct zebra_router_table finder;
93 struct zebra_router_table *zrt;
94
95 memset(&finder, 0, sizeof(finder));
96 finder.afi = afi;
97 finder.safi = safi;
98 finder.tableid = tableid;
99 finder.ns_id = zvrf->zns->ns_id;
100 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
101
102 if (zrt)
103 return zrt->table;
104 else
105 return NULL;
106 }
107
108 struct route_table *zebra_router_get_table(struct zebra_vrf *zvrf,
109 uint32_t tableid, afi_t afi,
110 safi_t safi)
111 {
112 struct zebra_router_table finder;
113 struct zebra_router_table *zrt;
114 struct rib_table_info *info;
115
116 memset(&finder, 0, sizeof(finder));
117 finder.afi = afi;
118 finder.safi = safi;
119 finder.tableid = tableid;
120 finder.ns_id = zvrf->zns->ns_id;
121 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
122
123 if (zrt)
124 return zrt->table;
125
126 zrt = XCALLOC(MTYPE_ZEBRA_RT_TABLE, sizeof(*zrt));
127 zrt->tableid = tableid;
128 zrt->afi = afi;
129 zrt->safi = safi;
130 zrt->ns_id = zvrf->zns->ns_id;
131 zrt->table =
132 (afi == AFI_IP6) ? srcdest_table_init() : route_table_init();
133
134 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
135 info->zvrf = zvrf;
136 info->afi = afi;
137 info->safi = safi;
138 info->table_id = tableid;
139 route_table_set_info(zrt->table, info);
140 zrt->table->cleanup = zebra_rtable_node_cleanup;
141
142 RB_INSERT(zebra_router_table_head, &zrouter.tables, zrt);
143 return zrt->table;
144 }
145
146 void zebra_router_show_table_summary(struct vty *vty)
147 {
148 struct zebra_router_table *zrt;
149
150 vty_out(vty,
151 "VRF NS ID VRF ID AFI SAFI Table Count\n");
152 vty_out(vty,
153 "---------------------------------------------------------------------------\n");
154 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
155 struct rib_table_info *info = route_table_get_info(zrt->table);
156
157 vty_out(vty, "%-16s%5d %9d %7s %15s %8d %10lu\n", info->zvrf->vrf->name,
158 zrt->ns_id, info->zvrf->vrf->vrf_id,
159 afi2str(zrt->afi), safi2str(zrt->safi),
160 zrt->tableid,
161 zrt->table->count);
162 }
163 }
164
165 void zebra_router_sweep_route(void)
166 {
167 struct zebra_router_table *zrt;
168
169 RB_FOREACH (zrt, zebra_router_table_head, &zrouter.tables) {
170 if (zrt->ns_id != NS_DEFAULT)
171 continue;
172 rib_sweep_table(zrt->table);
173 }
174 }
175
176 void zebra_router_sweep_nhgs(void)
177 {
178 zebra_nhg_sweep_table(zrouter.nhgs_id);
179 }
180
181 static void zebra_router_free_table(struct zebra_router_table *zrt)
182 {
183 void *table_info;
184
185 table_info = route_table_get_info(zrt->table);
186 route_table_finish(zrt->table);
187 RB_REMOVE(zebra_router_table_head, &zrouter.tables, zrt);
188
189 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
190 XFREE(MTYPE_ZEBRA_RT_TABLE, zrt);
191 }
192
193 void zebra_router_release_table(struct zebra_vrf *zvrf, uint32_t tableid,
194 afi_t afi, safi_t safi)
195 {
196 struct zebra_router_table finder;
197 struct zebra_router_table *zrt;
198
199 memset(&finder, 0, sizeof(finder));
200 finder.afi = afi;
201 finder.safi = safi;
202 finder.tableid = tableid;
203 finder.ns_id = zvrf->zns->ns_id;
204 zrt = RB_FIND(zebra_router_table_head, &zrouter.tables, &finder);
205
206 if (!zrt)
207 return;
208
209 zebra_router_free_table(zrt);
210 }
211
212 uint32_t zebra_router_get_next_sequence(void)
213 {
214 return 1
215 + atomic_fetch_add_explicit(&zrouter.sequence_num, 1,
216 memory_order_relaxed);
217 }
218
219 void multicast_mode_ipv4_set(enum multicast_mode mode)
220 {
221 if (IS_ZEBRA_DEBUG_RIB)
222 zlog_debug("%s: multicast lookup mode set (%d)", __func__,
223 mode);
224 zrouter.ipv4_multicast_mode = mode;
225 }
226
227 enum multicast_mode multicast_mode_ipv4_get(void)
228 {
229 return zrouter.ipv4_multicast_mode;
230 }
231
232 void zebra_router_terminate(void)
233 {
234 struct zebra_router_table *zrt, *tmp;
235
236 THREAD_OFF(zrouter.sweeper);
237
238 RB_FOREACH_SAFE (zrt, zebra_router_table_head, &zrouter.tables, tmp)
239 zebra_router_free_table(zrt);
240
241 work_queue_free_and_null(&zrouter.ribq);
242 meta_queue_free(zrouter.mq, NULL);
243
244 zebra_vxlan_disable();
245 zebra_mlag_terminate();
246 zebra_neigh_terminate();
247
248 /* Free NHE in ID table only since it has unhashable entries as well */
249 hash_iterate(zrouter.nhgs_id, zebra_nhg_hash_free_zero_id, NULL);
250 hash_clean(zrouter.nhgs_id, zebra_nhg_hash_free);
251 hash_free(zrouter.nhgs_id);
252 hash_clean(zrouter.nhgs, NULL);
253 hash_free(zrouter.nhgs);
254
255 hash_clean(zrouter.rules_hash, zebra_pbr_rules_free);
256 hash_free(zrouter.rules_hash);
257
258 hash_clean(zrouter.ipset_entry_hash, zebra_pbr_ipset_entry_free),
259 hash_clean(zrouter.ipset_hash, zebra_pbr_ipset_free);
260 hash_free(zrouter.ipset_hash);
261 hash_free(zrouter.ipset_entry_hash);
262 hash_clean(zrouter.iptable_hash, zebra_pbr_iptable_free);
263 hash_free(zrouter.iptable_hash);
264
265 #ifdef HAVE_SCRIPTING
266 zebra_script_destroy();
267 #endif
268
269 /* OS-specific deinit */
270 kernel_router_terminate();
271 }
272
273 bool zebra_router_notify_on_ack(void)
274 {
275 return !zrouter.asic_offloaded || zrouter.notify_on_ack;
276 }
277
278 void zebra_router_init(bool asic_offload, bool notify_on_ack)
279 {
280 zrouter.sequence_num = 0;
281
282 zrouter.allow_delete = false;
283
284 zrouter.packets_to_process = ZEBRA_ZAPI_PACKETS_TO_PROCESS;
285
286 zrouter.nhg_keep = ZEBRA_DEFAULT_NHG_KEEP_TIMER;
287
288 zebra_vxlan_init();
289 zebra_mlag_init();
290 zebra_neigh_init();
291
292 zrouter.rules_hash = hash_create_size(8, zebra_pbr_rules_hash_key,
293 zebra_pbr_rules_hash_equal,
294 "Rules Hash");
295
296 zrouter.ipset_hash =
297 hash_create_size(8, zebra_pbr_ipset_hash_key,
298 zebra_pbr_ipset_hash_equal, "IPset Hash");
299
300 zrouter.ipset_entry_hash = hash_create_size(
301 8, zebra_pbr_ipset_entry_hash_key,
302 zebra_pbr_ipset_entry_hash_equal, "IPset Hash Entry");
303
304 zrouter.iptable_hash = hash_create_size(8, zebra_pbr_iptable_hash_key,
305 zebra_pbr_iptable_hash_equal,
306 "IPtable Hash Entry");
307
308 zrouter.nhgs =
309 hash_create_size(8, zebra_nhg_hash_key, zebra_nhg_hash_equal,
310 "Zebra Router Nexthop Groups");
311 zrouter.nhgs_id =
312 hash_create_size(8, zebra_nhg_id_key, zebra_nhg_hash_id_equal,
313 "Zebra Router Nexthop Groups ID index");
314
315 zrouter.asic_offloaded = asic_offload;
316 zrouter.notify_on_ack = notify_on_ack;
317
318 #ifdef HAVE_SCRIPTING
319 zebra_script_init();
320 #endif
321
322 /* OS-specific init */
323 kernel_router_init();
324 }