]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vrf.c
bgpd: another change to keep indent.py happy
[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
7c551956
DS
31#include "zebra/debug.h"
32#include "zebra/zserv.h"
33#include "zebra/rib.h"
34#include "zebra/zebra_vrf.h"
5a8dfcd8 35#include "zebra/zebra_rnh.h"
7c551956 36#include "zebra/router-id.h"
4a1ab8e4 37#include "zebra/zebra_memory.h"
28f6dde8 38#include "zebra/zebra_static.h"
5a8dfcd8 39#include "zebra/interface.h"
7758e3f3 40#include "zebra/zebra_mpls.h"
13d60d35 41#include "zebra/zebra_vxlan.h"
7c551956
DS
42
43extern struct zebra_t zebrad;
44
9d97533e 45static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
46 safi_t safi);
47static void zebra_rnhtable_node_cleanup(struct route_table *table,
48 struct route_node *node);
49
7c551956 50/* VRF information update. */
d62a17ae 51static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
7c551956 52{
d62a17ae 53 struct listnode *node, *nnode;
54 struct zserv *client;
7c551956 55
d62a17ae 56 if (IS_ZEBRA_DEBUG_EVENT)
57 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
7c551956 58
d62a17ae 59 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
60 zsend_vrf_add(client, zvrf);
7c551956
DS
61}
62
d62a17ae 63static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
7c551956 64{
d62a17ae 65 struct listnode *node, *nnode;
66 struct zserv *client;
7c551956 67
d62a17ae 68 if (IS_ZEBRA_DEBUG_EVENT)
69 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
7c551956 70
d62a17ae 71 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client))
72 zsend_vrf_delete(client, zvrf);
7c551956
DS
73}
74
d62a17ae 75void zebra_vrf_update_all(struct zserv *client)
7c551956 76{
d62a17ae 77 struct vrf *vrf;
7c551956 78
a2addae8 79 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4691b65a 80 if (vrf->vrf_id != VRF_UNKNOWN)
d62a17ae 81 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
82 }
7c551956
DS
83}
84
85/* Callback upon creating a new VRF. */
d62a17ae 86static int zebra_vrf_new(struct vrf *vrf)
7c551956 87{
d62a17ae 88 struct zebra_vrf *zvrf;
7c551956 89
d62a17ae 90 if (IS_ZEBRA_DEBUG_EVENT)
84915b0a 91 zlog_info("VRF %s created, id %u", vrf->name, vrf->vrf_id);
7c551956 92
d62a17ae 93 zvrf = zebra_vrf_alloc();
d62a17ae 94 vrf->info = zvrf;
95 zvrf->vrf = vrf;
fbb65ff5 96 router_id_init(zvrf);
d62a17ae 97 return 0;
7c551956
DS
98}
99
100/* Callback upon enabling a VRF. */
d62a17ae 101static int zebra_vrf_enable(struct vrf *vrf)
7c551956 102{
d62a17ae 103 struct zebra_vrf *zvrf = vrf->info;
104 struct route_table *stable;
105 struct route_node *rn;
106 struct static_route *si;
9d97533e 107 struct route_table *table;
d62a17ae 108 struct interface *ifp;
109 afi_t afi;
110 safi_t safi;
111
112 assert(zvrf);
84915b0a 113 if (IS_ZEBRA_DEBUG_EVENT)
114 zlog_debug("VRF %s id %u is now active",
115 zvrf_name(zvrf), zvrf_id(zvrf));
d62a17ae 116
fbb65ff5
PG
117 if (vrf_is_backend_netns())
118 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
119 else
120 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
84915b0a 121 /* Inform clients that the VRF is now active. This is an
122 * add for the clients.
123 */
d62a17ae 124 zebra_vrf_add_update(zvrf);
125
9d97533e 126 /* Allocate tables */
127 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
128 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
129 zebra_vrf_table_create(zvrf, afi, safi);
130
131 table = route_table_init();
132 table->cleanup = zebra_rnhtable_node_cleanup;
133 zvrf->rnh_table[afi] = table;
134
135 table = route_table_init();
136 table->cleanup = zebra_rnhtable_node_cleanup;
137 zvrf->import_check_table[afi] = table;
138 }
139
84915b0a 140 /* Install any static routes configured for this VRF. */
d62a17ae 141 for (afi = AFI_IP; afi < AFI_MAX; afi++)
142 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
143 stable = zvrf->stable[afi][safi];
144 if (!stable)
145 continue;
146
147 for (rn = route_top(stable); rn; rn = route_next(rn))
148 for (si = rn->info; si; si = si->next) {
149 si->vrf_id = vrf->vrf_id;
150 if (si->ifindex) {
151 ifp = if_lookup_by_name(
152 si->ifname, si->vrf_id);
153 if (ifp)
154 si->ifindex =
155 ifp->ifindex;
156 else
157 continue;
158 }
159 static_install_route(afi, safi, &rn->p,
160 NULL, si);
161 }
fb148af4 162 }
2414ffe5 163
84915b0a 164 /* Kick off any VxLAN-EVPN processing. */
165 zebra_vxlan_vrf_enable(zvrf);
166
d62a17ae 167 return 0;
7c551956
DS
168}
169
170/* Callback upon disabling a VRF. */
d62a17ae 171static int zebra_vrf_disable(struct vrf *vrf)
7c551956 172{
d62a17ae 173 struct zebra_vrf *zvrf = vrf->info;
174 struct route_table *stable;
175 struct route_node *rn;
176 struct static_route *si;
9d97533e 177 struct route_table *table;
178 struct interface *ifp;
d62a17ae 179 afi_t afi;
180 safi_t safi;
9d97533e 181 unsigned i;
d62a17ae 182
84915b0a 183 assert(zvrf);
184 if (IS_ZEBRA_DEBUG_EVENT)
185 zlog_debug("VRF %s id %u is now inactive",
186 zvrf_name(zvrf), zvrf_id(zvrf));
d62a17ae 187
84915b0a 188 /* Uninstall any static routes configured for this VRF. */
d62a17ae 189 for (afi = AFI_IP; afi < AFI_MAX; afi++)
190 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
191 stable = zvrf->stable[afi][safi];
192 if (!stable)
193 continue;
194
195 for (rn = route_top(stable); rn; rn = route_next(rn))
196 for (si = rn->info; si; si = si->next)
197 static_uninstall_route(
198 afi, safi, &rn->p, NULL, si);
199 }
200
84915b0a 201 /* Stop any VxLAN-EVPN processing. */
202 zebra_vxlan_vrf_disable(zvrf);
d62a17ae 203
84915b0a 204 /* Inform clients that the VRF is now inactive. This is a
205 * delete for the clients.
206 */
d62a17ae 207 zebra_vrf_delete_update(zvrf);
208
9d97533e 209 /* If asked to retain routes, there's nothing more to do. */
210 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
211 return 0;
212
213 /* Remove all routes. */
214 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
215 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
216 rib_close_table(zvrf->table[afi][safi]);
9d97533e 217 }
218
219 /* Cleanup Vxlan, MPLS and PW tables. */
220 zebra_vxlan_cleanup_tables(zvrf);
221 zebra_mpls_cleanup_tables(zvrf);
222 zebra_pw_exit(zvrf);
223
224 /* Remove link-local IPv4 addresses created for BGP unnumbered peering. */
225 FOR_ALL_INTERFACES (vrf, ifp)
226 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
227
228 /* clean-up work queues */
229 for (i = 0; i < MQ_SIZE; i++) {
230 struct listnode *lnode, *nnode;
231 struct route_node *rnode;
232 rib_dest_t *dest;
233
27b136bd
DS
234 for (ALL_LIST_ELEMENTS(zebrad.mq->subq[i],
235 lnode, nnode, rnode)) {
9d97533e 236 dest = rib_dest_from_rnode(rnode);
237 if (dest && rib_dest_vrf(dest) == zvrf) {
238 route_unlock_node(rnode);
239 list_delete_node(zebrad.mq->subq[i], lnode);
240 zebrad.mq->size--;
241 }
d62a17ae 242 }
9d97533e 243 }
7c551956 244
9d97533e 245 /* Cleanup (free) routing tables and NHT tables. */
246 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
247 void *table_info;
5a8dfcd8 248
9d97533e 249 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
250 table = zvrf->table[afi][safi];
251 table_info = table->info;
252 route_table_finish(table);
253 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
254 zvrf->table[afi][safi] = NULL;
255 }
256
9d97533e 257 route_table_finish(zvrf->rnh_table[afi]);
258 zvrf->rnh_table[afi] = NULL;
259 route_table_finish(zvrf->import_check_table[afi]);
260 zvrf->import_check_table[afi] = NULL;
5a8dfcd8
RW
261 }
262
d62a17ae 263 return 0;
7c551956
DS
264}
265
d62a17ae 266static int zebra_vrf_delete(struct vrf *vrf)
7c551956 267{
d62a17ae 268 struct zebra_vrf *zvrf = vrf->info;
269 struct route_table *table;
d62a17ae 270 afi_t afi;
271 safi_t safi;
272 unsigned i;
273
274 assert(zvrf);
84915b0a 275 if (IS_ZEBRA_DEBUG_EVENT)
276 zlog_debug("VRF %s id %u deleted",
277 zvrf_name(zvrf), zvrf_id(zvrf));
5a8dfcd8 278
d62a17ae 279 /* clean-up work queues */
280 for (i = 0; i < MQ_SIZE; i++) {
281 struct listnode *lnode, *nnode;
282 struct route_node *rnode;
283 rib_dest_t *dest;
284
9d97533e 285 for (ALL_LIST_ELEMENTS(zebrad.mq->subq[i], lnode, nnode, rnode)) {
d62a17ae 286 dest = rib_dest_from_rnode(rnode);
287 if (dest && rib_dest_vrf(dest) == zvrf) {
288 route_unlock_node(rnode);
289 list_delete_node(zebrad.mq->subq[i], lnode);
290 zebrad.mq->size--;
291 }
292 }
5a8dfcd8 293 }
5a8dfcd8 294
84915b0a 295 /* Free Vxlan and MPLS. */
296 zebra_vxlan_close_tables(zvrf);
297 zebra_mpls_close_tables(zvrf);
298
d62a17ae 299 /* release allocated memory */
300 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
301 void *table_info;
0f124559 302
d62a17ae 303 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
304 table = zvrf->table[afi][safi];
9d97533e 305 if (table) {
306 table_info = table->info;
307 route_table_finish(table);
308 XFREE(MTYPE_RIB_TABLE_INFO, table_info);
309 }
d62a17ae 310
311 table = zvrf->stable[afi][safi];
312 route_table_finish(table);
313 }
5a8dfcd8 314
d62a17ae 315 route_table_finish(zvrf->rnh_table[afi]);
316 route_table_finish(zvrf->import_check_table[afi]);
5a8dfcd8 317 }
b7cfce93 318
84915b0a 319 /* Cleanup EVPN states for vrf */
b7cfce93
MK
320 zebra_vxlan_vrf_delete(zvrf);
321
d62a17ae 322 list_delete_all_node(zvrf->rid_all_sorted_list);
323 list_delete_all_node(zvrf->rid_lo_sorted_list);
324 XFREE(MTYPE_ZEBRA_VRF, zvrf);
325 vrf->info = NULL;
5a8dfcd8 326
d62a17ae 327 return 0;
7c551956
DS
328}
329
22bd3e94 330/* Return if this VRF has any FRR configuration or not.
331 * IMPORTANT: This function needs to be updated when additional configuration
332 * is added for a VRF.
333 */
334int zebra_vrf_has_config(struct zebra_vrf *zvrf)
335{
336 afi_t afi;
337 safi_t safi;
338 struct route_table *stable;
339
340 /* NOTE: This is a don't care for the default VRF, but we go through
341 * the motions to keep things consistent.
342 */
343 /* Any static routes? */
344 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
345 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
346 stable = zvrf->stable[afi][safi];
347 if (!stable)
348 continue;
349 if (route_table_count(stable))
350 return 1;
351 }
352 }
353
354 /* EVPN L3-VNI? */
355 if (zvrf->l3vni)
356 return 1;
357
358 return 0;
359}
360
7c551956
DS
361/* Lookup the routing table in a VRF based on both VRF-Id and table-id.
362 * NOTE: Table-id is relevant only in the Default VRF.
363 */
d62a17ae 364struct route_table *zebra_vrf_table_with_table_id(afi_t afi, safi_t safi,
365 vrf_id_t vrf_id,
366 u_int32_t table_id)
7c551956 367{
d62a17ae 368 struct route_table *table = NULL;
369
370 if (afi >= AFI_MAX || safi >= SAFI_MAX)
371 return NULL;
372
373 if (vrf_id == VRF_DEFAULT) {
374 if (table_id == RT_TABLE_MAIN
375 || table_id == zebrad.rtm_table_default)
376 table = zebra_vrf_table(afi, safi, vrf_id);
377 else
378 table = zebra_vrf_other_route_table(afi, table_id,
379 vrf_id);
380 } else
381 table = zebra_vrf_table(afi, safi, vrf_id);
382
383 return table;
7c551956
DS
384}
385
5335613b
DS
386void zebra_rtable_node_cleanup(struct route_table *table,
387 struct route_node *node)
5a8dfcd8 388{
d62a17ae 389 struct route_entry *re, *next;
5a8dfcd8 390
a2addae8 391 RNODE_FOREACH_RE_SAFE (node, re, next) {
407c87a6
DS
392 rib_unlink(node, re);
393 }
5a8dfcd8 394
d62a17ae 395 if (node->info)
396 XFREE(MTYPE_RIB_DEST, node->info);
5a8dfcd8
RW
397}
398
d62a17ae 399static void zebra_stable_node_cleanup(struct route_table *table,
400 struct route_node *node)
5a8dfcd8 401{
d62a17ae 402 struct static_route *si, *next;
403
404 if (node->info)
405 for (si = node->info; si; si = next) {
406 next = si->next;
407 XFREE(MTYPE_STATIC_ROUTE, si);
408 }
5a8dfcd8
RW
409}
410
d62a17ae 411static void zebra_rnhtable_node_cleanup(struct route_table *table,
412 struct route_node *node)
5a8dfcd8 413{
d62a17ae 414 if (node->info)
415 zebra_free_rnh(node->info);
5a8dfcd8
RW
416}
417
7c551956
DS
418/*
419 * Create a routing table for the specific AFI/SAFI in the given VRF.
420 */
d62a17ae 421static void zebra_vrf_table_create(struct zebra_vrf *zvrf, afi_t afi,
422 safi_t safi)
7c551956 423{
d62a17ae 424 rib_table_info_t *info;
425 struct route_table *table;
426
427 assert(!zvrf->table[afi][safi]);
428
429 if (afi == AFI_IP6)
430 table = srcdest_table_init();
431 else
432 table = route_table_init();
433 table->cleanup = zebra_rtable_node_cleanup;
434 zvrf->table[afi][safi] = table;
435
436 info = XCALLOC(MTYPE_RIB_TABLE_INFO, sizeof(*info));
437 info->zvrf = zvrf;
438 info->afi = afi;
439 info->safi = safi;
440 table->info = info;
7c551956
DS
441}
442
443/* Allocate new zebra VRF. */
d62a17ae 444struct zebra_vrf *zebra_vrf_alloc(void)
7c551956 445{
d62a17ae 446 struct zebra_vrf *zvrf;
447 afi_t afi;
448 safi_t safi;
449 struct route_table *table;
450
451 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
452
9d97533e 453 /* Allocate table for static route configuration. */
d62a17ae 454 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
455 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
d62a17ae 456 if (afi == AFI_IP6)
457 table = srcdest_table_init();
458 else
459 table = route_table_init();
460 table->cleanup = zebra_stable_node_cleanup;
461 zvrf->stable[afi][safi] = table;
462 }
d62a17ae 463 }
464
465 zebra_vxlan_init_tables(zvrf);
466 zebra_mpls_init_tables(zvrf);
6833ae01 467 zebra_pw_init(zvrf);
d62a17ae 468
469 return zvrf;
7c551956
DS
470}
471
472/* Lookup VRF by identifier. */
d62a17ae 473struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
7c551956 474{
d62a17ae 475 return vrf_info_lookup(vrf_id);
7c551956
DS
476}
477
51bdc5f8 478/* Lookup VRF by name. */
d62a17ae 479struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
871d39b3 480{
d62a17ae 481 struct vrf *vrf;
871d39b3 482
d62a17ae 483 if (!name)
484 name = VRF_DEFAULT_NAME;
a3d21ef3 485
d62a17ae 486 vrf = vrf_lookup_by_name(name);
487 if (vrf)
488 return ((struct zebra_vrf *)vrf->info);
51bdc5f8 489
d62a17ae 490 return NULL;
871d39b3
DS
491}
492
7c551956 493/* Lookup the routing table in an enabled VRF. */
d62a17ae 494struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
7c551956 495{
d62a17ae 496 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
7c551956 497
d62a17ae 498 if (!zvrf)
499 return NULL;
7c551956 500
d62a17ae 501 if (afi >= AFI_MAX || safi >= SAFI_MAX)
502 return NULL;
7c551956 503
d62a17ae 504 return zvrf->table[afi][safi];
7c551956
DS
505}
506
507/* Lookup the static routing table in a VRF. */
d62a17ae 508struct route_table *zebra_vrf_static_table(afi_t afi, safi_t safi,
509 struct zebra_vrf *zvrf)
7c551956 510{
d62a17ae 511 if (!zvrf)
512 return NULL;
7c551956 513
d62a17ae 514 if (afi >= AFI_MAX || safi >= SAFI_MAX)
515 return NULL;
7c551956 516
d62a17ae 517 return zvrf->stable[afi][safi];
7c551956
DS
518}
519
d62a17ae 520struct route_table *zebra_vrf_other_route_table(afi_t afi, u_int32_t table_id,
521 vrf_id_t vrf_id)
7c551956 522{
d62a17ae 523 struct zebra_vrf *zvrf;
5335613b 524 struct zebra_ns *zns;
d62a17ae 525
526 zvrf = vrf_info_lookup(vrf_id);
527 if (!zvrf)
528 return NULL;
529
5335613b
DS
530 zns = zvrf->zns;
531
d62a17ae 532 if (afi >= AFI_MAX)
533 return NULL;
534
d62a17ae 535 if ((vrf_id == VRF_DEFAULT) && (table_id != RT_TABLE_MAIN)
536 && (table_id != zebrad.rtm_table_default)) {
5335613b 537 return zebra_ns_get_table(zns, zvrf, table_id, afi);
d62a17ae 538 }
539
540 return zvrf->table[afi][SAFI_UNICAST];
7c551956
DS
541}
542
d62a17ae 543static int vrf_config_write(struct vty *vty)
f30c50b9 544{
d62a17ae 545 struct vrf *vrf;
546 struct zebra_vrf *zvrf;
547
a2addae8 548 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 549 zvrf = vrf->info;
1e9f448f
DS
550
551 if (!zvrf)
552 continue;
553
a5654735
MK
554 if (zvrf_id(zvrf) == VRF_DEFAULT) {
555 if (zvrf->l3vni)
556 vty_out(vty, "vni %u\n", zvrf->l3vni);
557 vty_out(vty, "!\n");
558 }
559
84915b0a 560 if (vrf_is_user_cfged(vrf)) {
37728041 561 vty_out(vty, "vrf %s\n", zvrf_name(zvrf));
22bd3e94 562 if (zvrf->l3vni)
c48d9f5f
MK
563 vty_out(vty, " vni %u%s\n",
564 zvrf->l3vni,
565 is_l3vni_for_prefix_routes_only(zvrf->l3vni) ?
566 " prefix-routes-only" :"");
b95c1883 567 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
22bd3e94 568 vty_out(vty, "!\n");
569 }
37728041
DS
570
571 static_config(vty, zvrf, AFI_IP, SAFI_UNICAST, "ip route");
572 static_config(vty, zvrf, AFI_IP, SAFI_MULTICAST, "ip mroute");
573 static_config(vty, zvrf, AFI_IP6, SAFI_UNICAST, "ipv6 route");
ab32921c 574
37728041
DS
575 if (vrf->vrf_id != VRF_DEFAULT)
576 vty_out(vty, "!\n");
d62a17ae 577 }
578 return 0;
f30c50b9
RW
579}
580
7c551956 581/* Zebra VRF initialization. */
d62a17ae 582void zebra_vrf_init(void)
7c551956 583{
736d41ad
PG
584 vrf_init(zebra_vrf_new, zebra_vrf_enable,
585 zebra_vrf_disable, zebra_vrf_delete);
7c551956 586
d62a17ae 587 vrf_cmd_init(vrf_config_write);
7c551956 588}