]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vrf.c
Merge pull request #5263 from donaldsharp/pim_excessive_warnings
[mirror_frr.git] / zebra / zebra_vrf.c
CommitLineData
7c551956
DS
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 *
896014f4
DL
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
7c551956
DS
20 */
21#include <zebra.h>
22
23#include "log.h"
24#include "linklist.h"
f30c50b9 25#include "command.h"
4a1ab8e4 26#include "memory.h"
05737783 27#include "srcdest_table.h"
78dd30b2 28#include "vrf.h"
82f97584 29#include "vty.h"
78dd30b2 30
89272910 31#include "zebra/zebra_router.h"
df9c8c57 32#include "zebra/rtadv.h"
7c551956 33#include "zebra/debug.h"
bf094f69 34#include "zebra/zapi_msg.h"
7c551956
DS
35#include "zebra/rib.h"
36#include "zebra/zebra_vrf.h"
5a8dfcd8 37#include "zebra/zebra_rnh.h"
7c551956 38#include "zebra/router-id.h"
4a1ab8e4 39#include "zebra/zebra_memory.h"
5a8dfcd8 40#include "zebra/interface.h"
7758e3f3 41#include "zebra/zebra_mpls.h"
13d60d35 42#include "zebra/zebra_vxlan.h"
3bc34908 43#include "zebra/zebra_netns_notify.h"
7cf16e19 44#include "zebra/zebra_routemap.h"
7c551956 45
9d97533e 46static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
47 safi_t safi);
48static void zebra_rnhtable_node_cleanup(struct route_table *table,
49 struct route_node *node);
50
c1344b54
DL
51DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_VRF, "ZEBRA VRF")
52DEFINE_MTYPE_STATIC(ZEBRA, OTHER_TABLE, "Other Table")
d8612e65 53
7c551956 54/* VRF information update. */
d62a17ae 55static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
7c551956 56{
d62a17ae 57 struct listnode *node, *nnode;
58 struct zserv *client;
7c551956 59
d62a17ae 60 if (IS_ZEBRA_DEBUG_EVENT)
61 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
7c551956 62
161e9ab7 63 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
d62a17ae 64 zsend_vrf_add(client, zvrf);
7c551956
DS
65}
66
d62a17ae 67static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
7c551956 68{
d62a17ae 69 struct listnode *node, *nnode;
70 struct zserv *client;
7c551956 71
d62a17ae 72 if (IS_ZEBRA_DEBUG_EVENT)
73 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
7c551956 74
161e9ab7 75 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
d62a17ae 76 zsend_vrf_delete(client, zvrf);
7c551956
DS
77}
78
d62a17ae 79void zebra_vrf_update_all(struct zserv *client)
7c551956 80{
d62a17ae 81 struct vrf *vrf;
7c551956 82
a2addae8 83 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4691b65a 84 if (vrf->vrf_id != VRF_UNKNOWN)
d62a17ae 85 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
86 }
7c551956
DS
87}
88
89/* Callback upon creating a new VRF. */
d62a17ae 90static int zebra_vrf_new(struct vrf *vrf)
7c551956 91{
d62a17ae 92 struct zebra_vrf *zvrf;
7c551956 93
d62a17ae 94 if (IS_ZEBRA_DEBUG_EVENT)
14a4d9d0 95 zlog_debug("VRF %s created, id %u", vrf->name, vrf->vrf_id);
7c551956 96
d62a17ae 97 zvrf = zebra_vrf_alloc();
d62a17ae 98 vrf->info = zvrf;
99 zvrf->vrf = vrf;
d8612e65
DS
100
101 otable_init(&zvrf->other_tables);
102
fbb65ff5 103 router_id_init(zvrf);
d62a17ae 104 return 0;
7c551956
DS
105}
106
107/* Callback upon enabling a VRF. */
d62a17ae 108static int zebra_vrf_enable(struct vrf *vrf)
7c551956 109{
d62a17ae 110 struct zebra_vrf *zvrf = vrf->info;
9d97533e 111 struct route_table *table;
d62a17ae 112 afi_t afi;
113 safi_t safi;
114
115 assert(zvrf);
84915b0a 116 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
117 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
118 zvrf_id(zvrf));
d62a17ae 119
fbb65ff5
PG
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);
df9c8c57
PG
124#if defined(HAVE_RTADV)
125 rtadv_init(zvrf);
126#endif
127
84915b0a 128 /* Inform clients that the VRF is now active. This is an
129 * add for the clients.
130 */
d62a17ae 131
8288a24f 132 zebra_vrf_add_update(zvrf);
9d97533e 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
84915b0a 147 /* Kick off any VxLAN-EVPN processing. */
148 zebra_vxlan_vrf_enable(zvrf);
149
d62a17ae 150 return 0;
7c551956
DS
151}
152
153/* Callback upon disabling a VRF. */
d62a17ae 154static int zebra_vrf_disable(struct vrf *vrf)
7c551956 155{
d62a17ae 156 struct zebra_vrf *zvrf = vrf->info;
9d97533e 157 struct interface *ifp;
d62a17ae 158 afi_t afi;
159 safi_t safi;
9d97533e 160 unsigned i;
d62a17ae 161
84915b0a 162 assert(zvrf);
163 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
164 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
165 zvrf_id(zvrf));
d62a17ae 166
84915b0a 167 /* Stop any VxLAN-EVPN processing. */
168 zebra_vxlan_vrf_disable(zvrf);
d62a17ae 169
df9c8c57
PG
170#if defined(HAVE_RTADV)
171 rtadv_terminate(zvrf);
172#endif
173
84915b0a 174 /* Inform clients that the VRF is now inactive. This is a
175 * delete for the clients.
176 */
d62a17ae 177 zebra_vrf_delete_update(zvrf);
178
9d97533e 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++) {
41dc8c14
DS
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
9d97533e 190 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
191 rib_close_table(zvrf->table[afi][safi]);
9d97533e 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
996c9314
LB
199 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
200 */
9d97533e 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
ea45a4e7 210 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
996c9314 211 rnode)) {
9d97533e 212 dest = rib_dest_from_rnode(rnode);
213 if (dest && rib_dest_vrf(dest) == zvrf) {
214 route_unlock_node(rnode);
ea45a4e7
DS
215 list_delete_node(zrouter.mq->subq[i], lnode);
216 zrouter.mq->size--;
9d97533e 217 }
d62a17ae 218 }
9d97533e 219 }
7c551956 220
9d97533e 221 /* Cleanup (free) routing tables and NHT tables. */
222 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
89272910
DS
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 */
bd4fb615
DS
231 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
232 zebra_router_release_table(zvrf, zvrf->table_id, afi,
233 safi);
9d97533e 234 zvrf->table[afi][safi] = NULL;
bd4fb615 235 }
5a8dfcd8
RW
236 }
237
d62a17ae 238 return 0;
7c551956
DS
239}
240
d62a17ae 241static int zebra_vrf_delete(struct vrf *vrf)
7c551956 242{
d62a17ae 243 struct zebra_vrf *zvrf = vrf->info;
d8612e65 244 struct other_route_table *otable;
d62a17ae 245 struct route_table *table;
d62a17ae 246 afi_t afi;
247 safi_t safi;
248 unsigned i;
249
250 assert(zvrf);
84915b0a 251 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
252 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
253 zvrf_id(zvrf));
5a8dfcd8 254
d62a17ae 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
ea45a4e7 261 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
996c9314 262 rnode)) {
d62a17ae 263 dest = rib_dest_from_rnode(rnode);
264 if (dest && rib_dest_vrf(dest) == zvrf) {
265 route_unlock_node(rnode);
ea45a4e7
DS
266 list_delete_node(zrouter.mq->subq[i], lnode);
267 zrouter.mq->size--;
d62a17ae 268 }
269 }
5a8dfcd8 270 }
5a8dfcd8 271
84915b0a 272 /* Free Vxlan and MPLS. */
273 zebra_vxlan_close_tables(zvrf);
274 zebra_mpls_close_tables(zvrf);
275
d62a17ae 276 /* release allocated memory */
277 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
d62a17ae 278 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
279 table = zvrf->table[afi][safi];
9d97533e 280 if (table) {
bd4fb615
DS
281 zebra_router_release_table(zvrf, zvrf->table_id,
282 afi, safi);
283 zvrf->table[afi][safi] = NULL;
9d97533e 284 }
d62a17ae 285 }
5a8dfcd8 286
41dc8c14
DS
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]);
5a8dfcd8 291 }
b7cfce93 292
d8612e65
DS
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
84915b0a 302 /* Cleanup EVPN states for vrf */
b7cfce93
MK
303 zebra_vxlan_vrf_delete(zvrf);
304
d62a17ae 305 list_delete_all_node(zvrf->rid_all_sorted_list);
306 list_delete_all_node(zvrf->rid_lo_sorted_list);
d8612e65
DS
307
308 otable_fini(&zvrf->other_tables);
d62a17ae 309 XFREE(MTYPE_ZEBRA_VRF, zvrf);
310 vrf->info = NULL;
5a8dfcd8 311
d62a17ae 312 return 0;
7c551956
DS
313}
314
ecbc5a37
PG
315static 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
22bd3e94 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 */
332int zebra_vrf_has_config(struct zebra_vrf *zvrf)
333{
22bd3e94 334 /* EVPN L3-VNI? */
335 if (zvrf->l3vni)
336 return 1;
337
338 return 0;
339}
340
7c551956 341/* Lookup the routing table in a VRF based on both VRF-Id and table-id.
e9748a89
PG
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
7c551956 345 */
d62a17ae 346struct route_table *zebra_vrf_table_with_table_id(afi_t afi, safi_t safi,
347 vrf_id_t vrf_id,
d7c0a89a 348 uint32_t table_id)
7c551956 349{
8ab39b7f 350 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
d8612e65
DS
351 struct other_route_table ort, *otable;
352 struct route_table *table;
8ab39b7f
DS
353
354 if (!zvrf)
355 return NULL;
d62a17ae 356
357 if (afi >= AFI_MAX || safi >= SAFI_MAX)
358 return NULL;
359
8ab39b7f
DS
360 if (table_id == zvrf->table_id)
361 return zebra_vrf_table(afi, safi, vrf_id);
d62a17ae 362
d8612e65
DS
363 ort.afi = afi;
364 ort.safi = safi;
365 ort.table_id = table_id;
366 otable = otable_find(&zvrf->other_tables, &ort);
367 if (otable)
368 return otable->table;
369
370 table = zebra_router_get_table(zvrf, table_id, afi, safi);
371
372 otable = XCALLOC(MTYPE_OTHER_TABLE, sizeof(*otable));
373 otable->afi = afi;
374 otable->safi = safi;
375 otable->table_id = table_id;
376 otable->table = table;
377 otable_add(&zvrf->other_tables, otable);
d62a17ae 378
379 return table;
7c551956
DS
380}
381
5335613b
DS
382void zebra_rtable_node_cleanup(struct route_table *table,
383 struct route_node *node)
5a8dfcd8 384{
d62a17ae 385 struct route_entry *re, *next;
5a8dfcd8 386
a2addae8 387 RNODE_FOREACH_RE_SAFE (node, re, next) {
407c87a6
DS
388 rib_unlink(node, re);
389 }
5a8dfcd8 390
699dae23
DS
391 if (node->info) {
392 rib_dest_t *dest = node->info;
393
aa57abfb 394 rnh_list_fini(&dest->nht);
699dae23
DS
395 XFREE(MTYPE_RIB_DEST, node->info);
396 }
5a8dfcd8
RW
397}
398
d62a17ae 399static void zebra_rnhtable_node_cleanup(struct route_table *table,
400 struct route_node *node)
5a8dfcd8 401{
d62a17ae 402 if (node->info)
403 zebra_free_rnh(node->info);
5a8dfcd8
RW
404}
405
7c551956
DS
406/*
407 * Create a routing table for the specific AFI/SAFI in the given VRF.
408 */
d62a17ae 409static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
410 safi_t safi)
7c551956 411{
c86ba6c2
DS
412 struct route_node *rn;
413 struct prefix p;
414
d62a17ae 415 assert(!zvrf->table[afi][safi]);
416
ea66cec4
DS
417 zvrf->table[afi][safi] =
418 zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);
c86ba6c2
DS
419
420 memset(&p, 0, sizeof(p));
421 p.family = afi2family(afi);
422
423 rn = srcdest_rnode_get(zvrf->table[afi][safi], &p, NULL);
424 zebra_rib_create_dest(rn);
7c551956
DS
425}
426
427/* Allocate new zebra VRF. */
d62a17ae 428struct zebra_vrf *zebra_vrf_alloc(void)
7c551956 429{
d62a17ae 430 struct zebra_vrf *zvrf;
d62a17ae 431
432 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
433
d62a17ae 434 zebra_vxlan_init_tables(zvrf);
435 zebra_mpls_init_tables(zvrf);
6833ae01 436 zebra_pw_init(zvrf);
e9748a89
PG
437 zvrf->table_id = RT_TABLE_MAIN;
438 /* by default table ID is default one */
d62a17ae 439 return zvrf;
7c551956
DS
440}
441
442/* Lookup VRF by identifier. */
d62a17ae 443struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
7c551956 444{
d62a17ae 445 return vrf_info_lookup(vrf_id);
7c551956
DS
446}
447
51bdc5f8 448/* Lookup VRF by name. */
d62a17ae 449struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
871d39b3 450{
d62a17ae 451 struct vrf *vrf;
871d39b3 452
d62a17ae 453 if (!name)
454 name = VRF_DEFAULT_NAME;
a3d21ef3 455
d62a17ae 456 vrf = vrf_lookup_by_name(name);
457 if (vrf)
458 return ((struct zebra_vrf *)vrf->info);
51bdc5f8 459
d62a17ae 460 return NULL;
871d39b3
DS
461}
462
7c551956 463/* Lookup the routing table in an enabled VRF. */
d62a17ae 464struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
7c551956 465{
d62a17ae 466 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
7c551956 467
d62a17ae 468 if (!zvrf)
469 return NULL;
7c551956 470
d62a17ae 471 if (afi >= AFI_MAX || safi >= SAFI_MAX)
472 return NULL;
7c551956 473
d62a17ae 474 return zvrf->table[afi][safi];
7c551956
DS
475}
476
d62a17ae 477static int vrf_config_write(struct vty *vty)
f30c50b9 478{
d62a17ae 479 struct vrf *vrf;
480 struct zebra_vrf *zvrf;
481
a2addae8 482 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 483 zvrf = vrf->info;
1e9f448f
DS
484
485 if (!zvrf)
486 continue;
487
a5654735
MK
488 if (zvrf_id(zvrf) == VRF_DEFAULT) {
489 if (zvrf->l3vni)
8d0f01f1
CS
490 vty_out(vty, "vni %u%s\n", zvrf->l3vni,
491 is_l3vni_for_prefix_routes_only(
492 zvrf->l3vni)
493 ? " prefix-routes-only"
494 : "");
5a0bdc78
PG
495 if (zvrf->zebra_rnh_ip_default_route)
496 vty_out(vty, "ip nht resolve-via-default\n");
497
498 if (zvrf->zebra_rnh_ipv6_default_route)
499 vty_out(vty, "ipv6 nht resolve-via-default\n");
c319e19d
QY
500 } else {
501 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
22bd3e94 502 if (zvrf->l3vni)
996c9314
LB
503 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
504 is_l3vni_for_prefix_routes_only(
505 zvrf->l3vni)
506 ? " prefix-routes-only"
507 : "");
b95c1883 508 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
5a0bdc78
PG
509 if (zvrf->zebra_rnh_ip_default_route)
510 vty_out(vty, " ip nht resolve-via-default\n");
511
512 if (zvrf->zebra_rnh_ipv6_default_route)
513 vty_out(vty, " ipv6 nht resolve-via-default\n");
22bd3e94 514 }
37728041 515
5a0bdc78 516
7cf16e19 517 zebra_routemap_config_write_protocol(vty, zvrf);
518
c319e19d
QY
519 if (zvrf_id(zvrf) != VRF_DEFAULT)
520 vty_endframe(vty, " exit-vrf\n!\n");
7cf16e19 521 else
522 vty_out(vty, "!\n");
d62a17ae 523 }
524 return 0;
f30c50b9
RW
525}
526
7c551956 527/* Zebra VRF initialization. */
d62a17ae 528void zebra_vrf_init(void)
7c551956 529{
996c9314 530 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
ecbc5a37 531 zebra_vrf_delete, zebra_vrf_update);
7c551956 532
3bc34908 533 vrf_cmd_init(vrf_config_write, &zserv_privs);
7c551956 534}