]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_vrf.c
Merge pull request #5278 from slankdev/slankdev-bgpd-fix-prefix-sid-fetch-error
[mirror_frr.git] / zebra / zebra_vrf.c
1 /*
2 * Copyright (C) 2016 CumulusNetworks
3 * Donald Sharp
4 *
5 * This file is part of Quagga
6 *
7 * Quagga 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 * Quagga 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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #include <zebra.h>
22
23 #include "log.h"
24 #include "linklist.h"
25 #include "command.h"
26 #include "memory.h"
27 #include "srcdest_table.h"
28 #include "vrf.h"
29 #include "vty.h"
30
31 #include "zebra/zebra_router.h"
32 #include "zebra/rtadv.h"
33 #include "zebra/debug.h"
34 #include "zebra/zapi_msg.h"
35 #include "zebra/rib.h"
36 #include "zebra/zebra_vrf.h"
37 #include "zebra/zebra_rnh.h"
38 #include "zebra/router-id.h"
39 #include "zebra/zebra_memory.h"
40 #include "zebra/interface.h"
41 #include "zebra/zebra_mpls.h"
42 #include "zebra/zebra_vxlan.h"
43 #include "zebra/zebra_netns_notify.h"
44 #include "zebra/zebra_routemap.h"
45
46 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
47 safi_t safi);
48 static void zebra_rnhtable_node_cleanup(struct route_table *table,
49 struct route_node *node);
50
51 DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_VRF, "ZEBRA VRF")
52 DEFINE_MTYPE_STATIC(ZEBRA, OTHER_TABLE, "Other Table")
53
54 /* VRF information update. */
55 static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
56 {
57 struct listnode *node, *nnode;
58 struct zserv *client;
59
60 if (IS_ZEBRA_DEBUG_EVENT)
61 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
62
63 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
64 zsend_vrf_add(client, zvrf);
65 }
66
67 static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
68 {
69 struct listnode *node, *nnode;
70 struct zserv *client;
71
72 if (IS_ZEBRA_DEBUG_EVENT)
73 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
74
75 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
76 zsend_vrf_delete(client, zvrf);
77 }
78
79 void zebra_vrf_update_all(struct zserv *client)
80 {
81 struct vrf *vrf;
82
83 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
84 if (vrf->vrf_id != VRF_UNKNOWN)
85 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
86 }
87 }
88
89 /* Callback upon creating a new VRF. */
90 static int zebra_vrf_new(struct vrf *vrf)
91 {
92 struct zebra_vrf *zvrf;
93
94 if (IS_ZEBRA_DEBUG_EVENT)
95 zlog_debug("VRF %s created, id %u", vrf->name, vrf->vrf_id);
96
97 zvrf = zebra_vrf_alloc();
98 vrf->info = zvrf;
99 zvrf->vrf = vrf;
100
101 otable_init(&zvrf->other_tables);
102
103 router_id_init(zvrf);
104 return 0;
105 }
106
107 /* Callback upon enabling a VRF. */
108 static int zebra_vrf_enable(struct vrf *vrf)
109 {
110 struct zebra_vrf *zvrf = vrf->info;
111 struct route_table *table;
112 afi_t afi;
113 safi_t safi;
114
115 assert(zvrf);
116 if (IS_ZEBRA_DEBUG_EVENT)
117 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
118 zvrf_id(zvrf));
119
120 if (vrf_is_backend_netns())
121 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
122 else
123 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
124 #if defined(HAVE_RTADV)
125 rtadv_init(zvrf);
126 #endif
127
128 /* Inform clients that the VRF is now active. This is an
129 * add for the clients.
130 */
131
132 zebra_vrf_add_update(zvrf);
133 /* Allocate tables */
134 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
135 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
136 zebra_vrf_table_create(zvrf, afi, safi);
137
138 table = route_table_init();
139 table->cleanup = zebra_rnhtable_node_cleanup;
140 zvrf->rnh_table[afi] = table;
141
142 table = route_table_init();
143 table->cleanup = zebra_rnhtable_node_cleanup;
144 zvrf->import_check_table[afi] = table;
145 }
146
147 /* Kick off any VxLAN-EVPN processing. */
148 zebra_vxlan_vrf_enable(zvrf);
149
150 return 0;
151 }
152
153 /* Callback upon disabling a VRF. */
154 static int zebra_vrf_disable(struct vrf *vrf)
155 {
156 struct zebra_vrf *zvrf = vrf->info;
157 struct interface *ifp;
158 afi_t afi;
159 safi_t safi;
160 unsigned i;
161
162 assert(zvrf);
163 if (IS_ZEBRA_DEBUG_EVENT)
164 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
165 zvrf_id(zvrf));
166
167 /* Stop any VxLAN-EVPN processing. */
168 zebra_vxlan_vrf_disable(zvrf);
169
170 #if defined(HAVE_RTADV)
171 rtadv_terminate(zvrf);
172 #endif
173
174 /* Inform clients that the VRF is now inactive. This is a
175 * delete for the clients.
176 */
177 zebra_vrf_delete_update(zvrf);
178
179 /* If asked to retain routes, there's nothing more to do. */
180 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
181 return 0;
182
183 /* Remove all routes. */
184 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
185 route_table_finish(zvrf->rnh_table[afi]);
186 zvrf->rnh_table[afi] = NULL;
187 route_table_finish(zvrf->import_check_table[afi]);
188 zvrf->import_check_table[afi] = NULL;
189
190 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
191 rib_close_table(zvrf->table[afi][safi]);
192 }
193
194 /* Cleanup Vxlan, MPLS and PW tables. */
195 zebra_vxlan_cleanup_tables(zvrf);
196 zebra_mpls_cleanup_tables(zvrf);
197 zebra_pw_exit(zvrf);
198
199 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
200 */
201 FOR_ALL_INTERFACES (vrf, ifp)
202 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
203
204 /* clean-up work queues */
205 for (i = 0; i < MQ_SIZE; i++) {
206 struct listnode *lnode, *nnode;
207 struct route_node *rnode;
208 rib_dest_t *dest;
209
210 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
211 rnode)) {
212 dest = rib_dest_from_rnode(rnode);
213 if (dest && rib_dest_vrf(dest) == zvrf) {
214 route_unlock_node(rnode);
215 list_delete_node(zrouter.mq->subq[i], lnode);
216 zrouter.mq->size--;
217 }
218 }
219 }
220
221 /* Cleanup (free) routing tables and NHT tables. */
222 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
223 /*
224 * Set the table pointer to NULL as that
225 * we no-longer need a copy of it, nor do we
226 * own this data, the zebra_router structure
227 * owns these tables. Once we've cleaned up the
228 * table, see rib_close_table above
229 * we no-longer need this pointer.
230 */
231 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
232 zebra_router_release_table(zvrf, zvrf->table_id, afi,
233 safi);
234 zvrf->table[afi][safi] = NULL;
235 }
236 }
237
238 return 0;
239 }
240
241 static int zebra_vrf_delete(struct vrf *vrf)
242 {
243 struct zebra_vrf *zvrf = vrf->info;
244 struct other_route_table *otable;
245 struct route_table *table;
246 afi_t afi;
247 safi_t safi;
248 unsigned i;
249
250 assert(zvrf);
251 if (IS_ZEBRA_DEBUG_EVENT)
252 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
253 zvrf_id(zvrf));
254
255 /* clean-up work queues */
256 for (i = 0; i < MQ_SIZE; i++) {
257 struct listnode *lnode, *nnode;
258 struct route_node *rnode;
259 rib_dest_t *dest;
260
261 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
262 rnode)) {
263 dest = rib_dest_from_rnode(rnode);
264 if (dest && rib_dest_vrf(dest) == zvrf) {
265 route_unlock_node(rnode);
266 list_delete_node(zrouter.mq->subq[i], lnode);
267 zrouter.mq->size--;
268 }
269 }
270 }
271
272 /* Free Vxlan and MPLS. */
273 zebra_vxlan_close_tables(zvrf);
274 zebra_mpls_close_tables(zvrf);
275
276 /* release allocated memory */
277 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
278 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
279 table = zvrf->table[afi][safi];
280 if (table) {
281 zebra_router_release_table(zvrf, zvrf->table_id,
282 afi, safi);
283 zvrf->table[afi][safi] = NULL;
284 }
285 }
286
287 if (zvrf->rnh_table[afi])
288 route_table_finish(zvrf->rnh_table[afi]);
289 if (zvrf->import_check_table[afi])
290 route_table_finish(zvrf->import_check_table[afi]);
291 }
292
293 otable = otable_pop(&zvrf->other_tables);
294 while (otable) {
295 zebra_router_release_table(zvrf, otable->table_id,
296 otable->afi, otable->safi);
297 XFREE(MTYPE_OTHER_TABLE, otable);
298
299 otable = otable_pop(&zvrf->other_tables);
300 }
301
302 /* Cleanup EVPN states for vrf */
303 zebra_vxlan_vrf_delete(zvrf);
304
305 list_delete_all_node(zvrf->rid_all_sorted_list);
306 list_delete_all_node(zvrf->rid_lo_sorted_list);
307
308 otable_fini(&zvrf->other_tables);
309 XFREE(MTYPE_ZEBRA_VRF, zvrf);
310 vrf->info = NULL;
311
312 return 0;
313 }
314
315 static int zebra_vrf_update(struct vrf *vrf)
316 {
317 struct zebra_vrf *zvrf = vrf->info;
318
319 assert(zvrf);
320 if (IS_ZEBRA_DEBUG_EVENT)
321 zlog_debug("VRF %s id %u, name updated", vrf->name,
322 zvrf_id(zvrf));
323 zebra_vrf_add_update(zvrf);
324 return 0;
325 }
326
327
328 /* Return if this VRF has any FRR configuration or not.
329 * IMPORTANT: This function needs to be updated when additional configuration
330 * is added for a VRF.
331 */
332 int zebra_vrf_has_config(struct zebra_vrf *zvrf)
333 {
334 /* EVPN L3-VNI? */
335 if (zvrf->l3vni)
336 return 1;
337
338 return 0;
339 }
340
341 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
342 * NOTE: Table-id is relevant on two modes:
343 * - case VRF backend is default : on default VRF only
344 * - case VRF backend is netns : on all VRFs
345 */
346 struct route_table *zebra_vrf_lookup_table_with_table_id(afi_t afi, safi_t safi,
347 vrf_id_t vrf_id,
348 uint32_t table_id)
349 {
350 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
351 struct other_route_table ort, *otable;
352
353 if (!zvrf)
354 return NULL;
355
356 if (afi >= AFI_MAX || safi >= SAFI_MAX)
357 return NULL;
358
359 if (table_id == zvrf->table_id)
360 return zebra_vrf_table(afi, safi, vrf_id);
361
362 ort.afi = afi;
363 ort.safi = safi;
364 ort.table_id = table_id;
365 otable = otable_find(&zvrf->other_tables, &ort);
366
367 if (otable)
368 return otable->table;
369
370 return NULL;
371 }
372
373 struct route_table *zebra_vrf_get_table_with_table_id(afi_t afi, safi_t safi,
374 vrf_id_t vrf_id,
375 uint32_t table_id)
376 {
377 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
378 struct other_route_table *otable;
379 struct route_table *table;
380
381 table = zebra_vrf_lookup_table_with_table_id(afi, safi, vrf_id,
382 table_id);
383
384 if (table)
385 goto done;
386
387 /* Create it as an `other` table */
388 table = zebra_router_get_table(zvrf, table_id, afi, safi);
389
390 otable = XCALLOC(MTYPE_OTHER_TABLE, sizeof(*otable));
391 otable->afi = afi;
392 otable->safi = safi;
393 otable->table_id = table_id;
394 otable->table = table;
395 otable_add(&zvrf->other_tables, otable);
396
397 done:
398 return table;
399 }
400
401 void zebra_rtable_node_cleanup(struct route_table *table,
402 struct route_node *node)
403 {
404 struct route_entry *re, *next;
405
406 RNODE_FOREACH_RE_SAFE (node, re, next) {
407 rib_unlink(node, re);
408 }
409
410 if (node->info) {
411 rib_dest_t *dest = node->info;
412
413 rnh_list_fini(&dest->nht);
414 XFREE(MTYPE_RIB_DEST, node->info);
415 }
416 }
417
418 static void zebra_rnhtable_node_cleanup(struct route_table *table,
419 struct route_node *node)
420 {
421 if (node->info)
422 zebra_free_rnh(node->info);
423 }
424
425 /*
426 * Create a routing table for the specific AFI/SAFI in the given VRF.
427 */
428 static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
429 safi_t safi)
430 {
431 struct route_node *rn;
432 struct prefix p;
433
434 assert(!zvrf->table[afi][safi]);
435
436 zvrf->table[afi][safi] =
437 zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);
438
439 memset(&p, 0, sizeof(p));
440 p.family = afi2family(afi);
441
442 rn = srcdest_rnode_get(zvrf->table[afi][safi], &p, NULL);
443 zebra_rib_create_dest(rn);
444 }
445
446 /* Allocate new zebra VRF. */
447 struct zebra_vrf *zebra_vrf_alloc(void)
448 {
449 struct zebra_vrf *zvrf;
450
451 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
452
453 zebra_vxlan_init_tables(zvrf);
454 zebra_mpls_init_tables(zvrf);
455 zebra_pw_init(zvrf);
456 zvrf->table_id = RT_TABLE_MAIN;
457 /* by default table ID is default one */
458 return zvrf;
459 }
460
461 /* Lookup VRF by identifier. */
462 struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
463 {
464 return vrf_info_lookup(vrf_id);
465 }
466
467 /* Lookup VRF by name. */
468 struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
469 {
470 struct vrf *vrf;
471
472 if (!name)
473 name = VRF_DEFAULT_NAME;
474
475 vrf = vrf_lookup_by_name(name);
476 if (vrf)
477 return ((struct zebra_vrf *)vrf->info);
478
479 return NULL;
480 }
481
482 /* Lookup the routing table in an enabled VRF. */
483 struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
484 {
485 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
486
487 if (!zvrf)
488 return NULL;
489
490 if (afi >= AFI_MAX || safi >= SAFI_MAX)
491 return NULL;
492
493 return zvrf->table[afi][safi];
494 }
495
496 static int vrf_config_write(struct vty *vty)
497 {
498 struct vrf *vrf;
499 struct zebra_vrf *zvrf;
500
501 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
502 zvrf = vrf->info;
503
504 if (!zvrf)
505 continue;
506
507 if (zvrf_id(zvrf) == VRF_DEFAULT) {
508 if (zvrf->l3vni)
509 vty_out(vty, "vni %u%s\n", zvrf->l3vni,
510 is_l3vni_for_prefix_routes_only(
511 zvrf->l3vni)
512 ? " prefix-routes-only"
513 : "");
514 if (zvrf->zebra_rnh_ip_default_route)
515 vty_out(vty, "ip nht resolve-via-default\n");
516
517 if (zvrf->zebra_rnh_ipv6_default_route)
518 vty_out(vty, "ipv6 nht resolve-via-default\n");
519 } else {
520 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
521 if (zvrf->l3vni)
522 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
523 is_l3vni_for_prefix_routes_only(
524 zvrf->l3vni)
525 ? " prefix-routes-only"
526 : "");
527 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
528 if (zvrf->zebra_rnh_ip_default_route)
529 vty_out(vty, " ip nht resolve-via-default\n");
530
531 if (zvrf->zebra_rnh_ipv6_default_route)
532 vty_out(vty, " ipv6 nht resolve-via-default\n");
533 }
534
535
536 zebra_routemap_config_write_protocol(vty, zvrf);
537
538 if (zvrf_id(zvrf) != VRF_DEFAULT)
539 vty_endframe(vty, " exit-vrf\n!\n");
540 else
541 vty_out(vty, "!\n");
542 }
543 return 0;
544 }
545
546 /* Zebra VRF initialization. */
547 void zebra_vrf_init(void)
548 {
549 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
550 zebra_vrf_delete, zebra_vrf_update);
551
552 vrf_cmd_init(vrf_config_write, &zserv_privs);
553 }