]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vrf.c
Merge pull request #5706 from mjstapp/fix_nh_debug_show
[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 */
c7c0b007
SW
346struct 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)
7c551956 349{
8ab39b7f 350 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
d8612e65 351 struct other_route_table ort, *otable;
8ab39b7f
DS
352
353 if (!zvrf)
354 return NULL;
d62a17ae 355
356 if (afi >= AFI_MAX || safi >= SAFI_MAX)
357 return NULL;
358
8ab39b7f
DS
359 if (table_id == zvrf->table_id)
360 return zebra_vrf_table(afi, safi, vrf_id);
d62a17ae 361
d8612e65
DS
362 ort.afi = afi;
363 ort.safi = safi;
364 ort.table_id = table_id;
365 otable = otable_find(&zvrf->other_tables, &ort);
c7c0b007 366
d8612e65
DS
367 if (otable)
368 return otable->table;
369
c7c0b007
SW
370 return NULL;
371}
372
373struct 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 */
d8612e65
DS
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);
d62a17ae 396
c7c0b007 397done:
d62a17ae 398 return table;
7c551956
DS
399}
400
5335613b
DS
401void zebra_rtable_node_cleanup(struct route_table *table,
402 struct route_node *node)
5a8dfcd8 403{
d62a17ae 404 struct route_entry *re, *next;
5a8dfcd8 405
a2addae8 406 RNODE_FOREACH_RE_SAFE (node, re, next) {
407c87a6
DS
407 rib_unlink(node, re);
408 }
5a8dfcd8 409
699dae23
DS
410 if (node->info) {
411 rib_dest_t *dest = node->info;
412
aa57abfb 413 rnh_list_fini(&dest->nht);
699dae23
DS
414 XFREE(MTYPE_RIB_DEST, node->info);
415 }
5a8dfcd8
RW
416}
417
d62a17ae 418static void zebra_rnhtable_node_cleanup(struct route_table *table,
419 struct route_node *node)
5a8dfcd8 420{
d62a17ae 421 if (node->info)
422 zebra_free_rnh(node->info);
5a8dfcd8
RW
423}
424
7c551956
DS
425/*
426 * Create a routing table for the specific AFI/SAFI in the given VRF.
427 */
d62a17ae 428static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
429 safi_t safi)
7c551956 430{
c86ba6c2
DS
431 struct route_node *rn;
432 struct prefix p;
433
d62a17ae 434 assert(!zvrf->table[afi][safi]);
435
ea66cec4
DS
436 zvrf->table[afi][safi] =
437 zebra_router_get_table(zvrf, zvrf->table_id, afi, safi);
c86ba6c2
DS
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);
7c551956
DS
444}
445
446/* Allocate new zebra VRF. */
d62a17ae 447struct zebra_vrf *zebra_vrf_alloc(void)
7c551956 448{
d62a17ae 449 struct zebra_vrf *zvrf;
d62a17ae 450
451 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
452
d62a17ae 453 zebra_vxlan_init_tables(zvrf);
454 zebra_mpls_init_tables(zvrf);
6833ae01 455 zebra_pw_init(zvrf);
e9748a89
PG
456 zvrf->table_id = RT_TABLE_MAIN;
457 /* by default table ID is default one */
d62a17ae 458 return zvrf;
7c551956
DS
459}
460
461/* Lookup VRF by identifier. */
d62a17ae 462struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
7c551956 463{
d62a17ae 464 return vrf_info_lookup(vrf_id);
7c551956
DS
465}
466
51bdc5f8 467/* Lookup VRF by name. */
d62a17ae 468struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
871d39b3 469{
d62a17ae 470 struct vrf *vrf;
871d39b3 471
d62a17ae 472 if (!name)
473 name = VRF_DEFAULT_NAME;
a3d21ef3 474
d62a17ae 475 vrf = vrf_lookup_by_name(name);
476 if (vrf)
477 return ((struct zebra_vrf *)vrf->info);
51bdc5f8 478
d62a17ae 479 return NULL;
871d39b3
DS
480}
481
7c551956 482/* Lookup the routing table in an enabled VRF. */
d62a17ae 483struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
7c551956 484{
d62a17ae 485 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
7c551956 486
d62a17ae 487 if (!zvrf)
488 return NULL;
7c551956 489
d62a17ae 490 if (afi >= AFI_MAX || safi >= SAFI_MAX)
491 return NULL;
7c551956 492
d62a17ae 493 return zvrf->table[afi][safi];
7c551956
DS
494}
495
d62a17ae 496static int vrf_config_write(struct vty *vty)
f30c50b9 497{
d62a17ae 498 struct vrf *vrf;
499 struct zebra_vrf *zvrf;
500
a2addae8 501 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 502 zvrf = vrf->info;
1e9f448f
DS
503
504 if (!zvrf)
505 continue;
506
a5654735
MK
507 if (zvrf_id(zvrf) == VRF_DEFAULT) {
508 if (zvrf->l3vni)
8d0f01f1
CS
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 : "");
5a0bdc78
PG
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");
c319e19d
QY
519 } else {
520 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
22bd3e94 521 if (zvrf->l3vni)
996c9314
LB
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 : "");
b95c1883 527 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
5a0bdc78
PG
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");
22bd3e94 533 }
37728041 534
5a0bdc78 535
7cf16e19 536 zebra_routemap_config_write_protocol(vty, zvrf);
537
c319e19d
QY
538 if (zvrf_id(zvrf) != VRF_DEFAULT)
539 vty_endframe(vty, " exit-vrf\n!\n");
7cf16e19 540 else
541 vty_out(vty, "!\n");
d62a17ae 542 }
543 return 0;
f30c50b9
RW
544}
545
7c551956 546/* Zebra VRF initialization. */
d62a17ae 547void zebra_vrf_init(void)
7c551956 548{
996c9314 549 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
ecbc5a37 550 zebra_vrf_delete, zebra_vrf_update);
7c551956 551
3bc34908 552 vrf_cmd_init(vrf_config_write, &zserv_privs);
7c551956 553}