]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_vrf.c
Merge pull request #9446 from mobash-rasool/ospfv3-bug-fixes
[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"
5a8dfcd8 39#include "zebra/interface.h"
7758e3f3 40#include "zebra/zebra_mpls.h"
13d60d35 41#include "zebra/zebra_vxlan.h"
3bc34908 42#include "zebra/zebra_netns_notify.h"
7cf16e19 43#include "zebra/zebra_routemap.h"
7c551956 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
bf8d3d6a
DL
50DEFINE_MTYPE_STATIC(ZEBRA, ZEBRA_VRF, "ZEBRA VRF");
51DEFINE_MTYPE_STATIC(ZEBRA, OTHER_TABLE, "Other Table");
d8612e65 52
7c551956 53/* VRF information update. */
d62a17ae 54static void zebra_vrf_add_update(struct zebra_vrf *zvrf)
7c551956 55{
d62a17ae 56 struct listnode *node, *nnode;
57 struct zserv *client;
7c551956 58
d62a17ae 59 if (IS_ZEBRA_DEBUG_EVENT)
60 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf));
7c551956 61
17da84a4
KS
62 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
63 /* Do not send unsolicited messages to synchronous clients. */
64 if (client->synchronous)
65 continue;
66
d62a17ae 67 zsend_vrf_add(client, zvrf);
17da84a4 68 }
7c551956
DS
69}
70
d62a17ae 71static void zebra_vrf_delete_update(struct zebra_vrf *zvrf)
7c551956 72{
d62a17ae 73 struct listnode *node, *nnode;
74 struct zserv *client;
7c551956 75
d62a17ae 76 if (IS_ZEBRA_DEBUG_EVENT)
77 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf));
7c551956 78
17da84a4
KS
79 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
80 /* Do not send unsolicited messages to synchronous clients. */
81 if (client->synchronous)
82 continue;
83
d62a17ae 84 zsend_vrf_delete(client, zvrf);
17da84a4 85 }
7c551956
DS
86}
87
d62a17ae 88void zebra_vrf_update_all(struct zserv *client)
7c551956 89{
d62a17ae 90 struct vrf *vrf;
7c551956 91
a2addae8 92 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
4691b65a 93 if (vrf->vrf_id != VRF_UNKNOWN)
d62a17ae 94 zsend_vrf_add(client, vrf_info_lookup(vrf->vrf_id));
95 }
7c551956
DS
96}
97
98/* Callback upon creating a new VRF. */
d62a17ae 99static int zebra_vrf_new(struct vrf *vrf)
7c551956 100{
d62a17ae 101 struct zebra_vrf *zvrf;
7c551956 102
d62a17ae 103 if (IS_ZEBRA_DEBUG_EVENT)
14a4d9d0 104 zlog_debug("VRF %s created, id %u", vrf->name, vrf->vrf_id);
7c551956 105
ec64a634 106 zvrf = zebra_vrf_alloc(vrf);
91b1421e
PG
107 if (!vrf_is_backend_netns())
108 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
d8612e65
DS
109
110 otable_init(&zvrf->other_tables);
111
fbb65ff5 112 router_id_init(zvrf);
d62a17ae 113 return 0;
7c551956
DS
114}
115
116/* Callback upon enabling a VRF. */
d62a17ae 117static int zebra_vrf_enable(struct vrf *vrf)
7c551956 118{
d62a17ae 119 struct zebra_vrf *zvrf = vrf->info;
9d97533e 120 struct route_table *table;
d62a17ae 121 afi_t afi;
122 safi_t safi;
123
124 assert(zvrf);
84915b0a 125 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
126 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf),
127 zvrf_id(zvrf));
d62a17ae 128
fbb65ff5
PG
129 if (vrf_is_backend_netns())
130 zvrf->zns = zebra_ns_lookup((ns_id_t)vrf->vrf_id);
131 else
132 zvrf->zns = zebra_ns_lookup(NS_DEFAULT);
df9c8c57 133#if defined(HAVE_RTADV)
7c2ddfb9 134 rtadv_vrf_init(zvrf);
df9c8c57
PG
135#endif
136
84915b0a 137 /* Inform clients that the VRF is now active. This is an
138 * add for the clients.
139 */
d62a17ae 140
8288a24f 141 zebra_vrf_add_update(zvrf);
9d97533e 142 /* Allocate tables */
143 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
144 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
145 zebra_vrf_table_create(zvrf, afi, safi);
146
147 table = route_table_init();
148 table->cleanup = zebra_rnhtable_node_cleanup;
149 zvrf->rnh_table[afi] = table;
150
151 table = route_table_init();
152 table->cleanup = zebra_rnhtable_node_cleanup;
153 zvrf->import_check_table[afi] = table;
154 }
155
84915b0a 156 /* Kick off any VxLAN-EVPN processing. */
157 zebra_vxlan_vrf_enable(zvrf);
158
d62a17ae 159 return 0;
7c551956
DS
160}
161
162/* Callback upon disabling a VRF. */
d62a17ae 163static int zebra_vrf_disable(struct vrf *vrf)
7c551956 164{
d62a17ae 165 struct zebra_vrf *zvrf = vrf->info;
9d97533e 166 struct interface *ifp;
d62a17ae 167 afi_t afi;
168 safi_t safi;
9d97533e 169 unsigned i;
d62a17ae 170
84915b0a 171 assert(zvrf);
172 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
173 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf),
174 zvrf_id(zvrf));
d62a17ae 175
84915b0a 176 /* Stop any VxLAN-EVPN processing. */
177 zebra_vxlan_vrf_disable(zvrf);
d62a17ae 178
df9c8c57 179#if defined(HAVE_RTADV)
aab5893a 180 rtadv_vrf_terminate(zvrf);
df9c8c57
PG
181#endif
182
84915b0a 183 /* Inform clients that the VRF is now inactive. This is a
184 * delete for the clients.
185 */
d62a17ae 186 zebra_vrf_delete_update(zvrf);
187
9d97533e 188 /* If asked to retain routes, there's nothing more to do. */
189 if (CHECK_FLAG(zvrf->flags, ZEBRA_VRF_RETAIN))
190 return 0;
191
192 /* Remove all routes. */
193 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
41dc8c14
DS
194 route_table_finish(zvrf->rnh_table[afi]);
195 zvrf->rnh_table[afi] = NULL;
196 route_table_finish(zvrf->import_check_table[afi]);
197 zvrf->import_check_table[afi] = NULL;
198
9d97533e 199 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++)
200 rib_close_table(zvrf->table[afi][safi]);
9d97533e 201 }
202
203 /* Cleanup Vxlan, MPLS and PW tables. */
204 zebra_vxlan_cleanup_tables(zvrf);
205 zebra_mpls_cleanup_tables(zvrf);
206 zebra_pw_exit(zvrf);
207
996c9314
LB
208 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
209 */
9d97533e 210 FOR_ALL_INTERFACES (vrf, ifp)
211 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
212
213 /* clean-up work queues */
214 for (i = 0; i < MQ_SIZE; i++) {
215 struct listnode *lnode, *nnode;
216 struct route_node *rnode;
217 rib_dest_t *dest;
218
ea45a4e7 219 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
996c9314 220 rnode)) {
9d97533e 221 dest = rib_dest_from_rnode(rnode);
222 if (dest && rib_dest_vrf(dest) == zvrf) {
223 route_unlock_node(rnode);
ea45a4e7
DS
224 list_delete_node(zrouter.mq->subq[i], lnode);
225 zrouter.mq->size--;
9d97533e 226 }
d62a17ae 227 }
9d97533e 228 }
7c551956 229
9d97533e 230 /* Cleanup (free) routing tables and NHT tables. */
231 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
89272910
DS
232 /*
233 * Set the table pointer to NULL as that
234 * we no-longer need a copy of it, nor do we
235 * own this data, the zebra_router structure
236 * owns these tables. Once we've cleaned up the
237 * table, see rib_close_table above
238 * we no-longer need this pointer.
239 */
bd4fb615
DS
240 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
241 zebra_router_release_table(zvrf, zvrf->table_id, afi,
242 safi);
9d97533e 243 zvrf->table[afi][safi] = NULL;
bd4fb615 244 }
5a8dfcd8
RW
245 }
246
d62a17ae 247 return 0;
7c551956
DS
248}
249
d62a17ae 250static int zebra_vrf_delete(struct vrf *vrf)
7c551956 251{
d62a17ae 252 struct zebra_vrf *zvrf = vrf->info;
d8612e65 253 struct other_route_table *otable;
d62a17ae 254 struct route_table *table;
d62a17ae 255 afi_t afi;
256 safi_t safi;
257 unsigned i;
258
259 assert(zvrf);
84915b0a 260 if (IS_ZEBRA_DEBUG_EVENT)
996c9314
LB
261 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf),
262 zvrf_id(zvrf));
5a8dfcd8 263
d62a17ae 264 /* clean-up work queues */
265 for (i = 0; i < MQ_SIZE; i++) {
266 struct listnode *lnode, *nnode;
267 struct route_node *rnode;
268 rib_dest_t *dest;
269
ea45a4e7 270 for (ALL_LIST_ELEMENTS(zrouter.mq->subq[i], lnode, nnode,
996c9314 271 rnode)) {
d62a17ae 272 dest = rib_dest_from_rnode(rnode);
273 if (dest && rib_dest_vrf(dest) == zvrf) {
274 route_unlock_node(rnode);
ea45a4e7
DS
275 list_delete_node(zrouter.mq->subq[i], lnode);
276 zrouter.mq->size--;
d62a17ae 277 }
278 }
5a8dfcd8 279 }
5a8dfcd8 280
84915b0a 281 /* Free Vxlan and MPLS. */
282 zebra_vxlan_close_tables(zvrf);
283 zebra_mpls_close_tables(zvrf);
284
d62a17ae 285 /* release allocated memory */
286 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
d62a17ae 287 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
288 table = zvrf->table[afi][safi];
9d97533e 289 if (table) {
bd4fb615
DS
290 zebra_router_release_table(zvrf, zvrf->table_id,
291 afi, safi);
292 zvrf->table[afi][safi] = NULL;
9d97533e 293 }
d62a17ae 294 }
5a8dfcd8 295
41dc8c14
DS
296 if (zvrf->rnh_table[afi])
297 route_table_finish(zvrf->rnh_table[afi]);
298 if (zvrf->import_check_table[afi])
299 route_table_finish(zvrf->import_check_table[afi]);
5a8dfcd8 300 }
b7cfce93 301
d8612e65
DS
302 otable = otable_pop(&zvrf->other_tables);
303 while (otable) {
304 zebra_router_release_table(zvrf, otable->table_id,
305 otable->afi, otable->safi);
306 XFREE(MTYPE_OTHER_TABLE, otable);
307
308 otable = otable_pop(&zvrf->other_tables);
309 }
310
84915b0a 311 /* Cleanup EVPN states for vrf */
b7cfce93
MK
312 zebra_vxlan_vrf_delete(zvrf);
313
d62a17ae 314 list_delete_all_node(zvrf->rid_all_sorted_list);
315 list_delete_all_node(zvrf->rid_lo_sorted_list);
d8612e65 316
cdc09a4b
MS
317 list_delete_all_node(zvrf->rid6_all_sorted_list);
318 list_delete_all_node(zvrf->rid6_lo_sorted_list);
319
d8612e65 320 otable_fini(&zvrf->other_tables);
d62a17ae 321 XFREE(MTYPE_ZEBRA_VRF, zvrf);
322 vrf->info = NULL;
5a8dfcd8 323
d62a17ae 324 return 0;
7c551956
DS
325}
326
ecbc5a37
PG
327static int zebra_vrf_update(struct vrf *vrf)
328{
329 struct zebra_vrf *zvrf = vrf->info;
330
331 assert(zvrf);
332 if (IS_ZEBRA_DEBUG_EVENT)
333 zlog_debug("VRF %s id %u, name updated", vrf->name,
334 zvrf_id(zvrf));
335 zebra_vrf_add_update(zvrf);
336 return 0;
337}
338
7c551956 339/* Lookup the routing table in a VRF based on both VRF-Id and table-id.
e9748a89
PG
340 * NOTE: Table-id is relevant on two modes:
341 * - case VRF backend is default : on default VRF only
342 * - case VRF backend is netns : on all VRFs
7c551956 343 */
c7c0b007
SW
344struct route_table *zebra_vrf_lookup_table_with_table_id(afi_t afi, safi_t safi,
345 vrf_id_t vrf_id,
346 uint32_t table_id)
7c551956 347{
8ab39b7f 348 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
d8612e65 349 struct other_route_table ort, *otable;
8ab39b7f
DS
350
351 if (!zvrf)
352 return NULL;
d62a17ae 353
354 if (afi >= AFI_MAX || safi >= SAFI_MAX)
355 return NULL;
356
8ab39b7f
DS
357 if (table_id == zvrf->table_id)
358 return zebra_vrf_table(afi, safi, vrf_id);
d62a17ae 359
d8612e65
DS
360 ort.afi = afi;
361 ort.safi = safi;
362 ort.table_id = table_id;
363 otable = otable_find(&zvrf->other_tables, &ort);
c7c0b007 364
d8612e65
DS
365 if (otable)
366 return otable->table;
367
c7c0b007
SW
368 return NULL;
369}
370
371struct route_table *zebra_vrf_get_table_with_table_id(afi_t afi, safi_t safi,
372 vrf_id_t vrf_id,
373 uint32_t table_id)
374{
375 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
376 struct other_route_table *otable;
377 struct route_table *table;
378
379 table = zebra_vrf_lookup_table_with_table_id(afi, safi, vrf_id,
380 table_id);
381
382 if (table)
383 goto done;
384
385 /* Create it as an `other` table */
d8612e65
DS
386 table = zebra_router_get_table(zvrf, table_id, afi, safi);
387
388 otable = XCALLOC(MTYPE_OTHER_TABLE, sizeof(*otable));
389 otable->afi = afi;
390 otable->safi = safi;
391 otable->table_id = table_id;
392 otable->table = table;
393 otable_add(&zvrf->other_tables, otable);
d62a17ae 394
c7c0b007 395done:
d62a17ae 396 return table;
7c551956
DS
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. */
ec64a634 428struct zebra_vrf *zebra_vrf_alloc(struct vrf *vrf)
7c551956 429{
d62a17ae 430 struct zebra_vrf *zvrf;
d62a17ae 431
432 zvrf = XCALLOC(MTYPE_ZEBRA_VRF, sizeof(struct zebra_vrf));
433
ec64a634
DS
434 zvrf->vrf = vrf;
435 vrf->info = zvrf;
436
d62a17ae 437 zebra_vxlan_init_tables(zvrf);
438 zebra_mpls_init_tables(zvrf);
6833ae01 439 zebra_pw_init(zvrf);
e9748a89
PG
440 zvrf->table_id = RT_TABLE_MAIN;
441 /* by default table ID is default one */
d62a17ae 442 return zvrf;
7c551956
DS
443}
444
445/* Lookup VRF by identifier. */
d62a17ae 446struct zebra_vrf *zebra_vrf_lookup_by_id(vrf_id_t vrf_id)
7c551956 447{
d62a17ae 448 return vrf_info_lookup(vrf_id);
7c551956
DS
449}
450
51bdc5f8 451/* Lookup VRF by name. */
d62a17ae 452struct zebra_vrf *zebra_vrf_lookup_by_name(const char *name)
871d39b3 453{
d62a17ae 454 struct vrf *vrf;
871d39b3 455
d62a17ae 456 if (!name)
457 name = VRF_DEFAULT_NAME;
a3d21ef3 458
d62a17ae 459 vrf = vrf_lookup_by_name(name);
460 if (vrf)
461 return ((struct zebra_vrf *)vrf->info);
51bdc5f8 462
d62a17ae 463 return NULL;
871d39b3
DS
464}
465
7c551956 466/* Lookup the routing table in an enabled VRF. */
d62a17ae 467struct route_table *zebra_vrf_table(afi_t afi, safi_t safi, vrf_id_t vrf_id)
7c551956 468{
d62a17ae 469 struct zebra_vrf *zvrf = vrf_info_lookup(vrf_id);
7c551956 470
d62a17ae 471 if (!zvrf)
472 return NULL;
7c551956 473
d62a17ae 474 if (afi >= AFI_MAX || safi >= SAFI_MAX)
475 return NULL;
7c551956 476
d62a17ae 477 return zvrf->table[afi][safi];
7c551956
DS
478}
479
d62a17ae 480static int vrf_config_write(struct vty *vty)
f30c50b9 481{
d62a17ae 482 struct vrf *vrf;
483 struct zebra_vrf *zvrf;
484
a2addae8 485 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
d62a17ae 486 zvrf = vrf->info;
1e9f448f
DS
487
488 if (!zvrf)
489 continue;
490
a5654735
MK
491 if (zvrf_id(zvrf) == VRF_DEFAULT) {
492 if (zvrf->l3vni)
8d0f01f1
CS
493 vty_out(vty, "vni %u%s\n", zvrf->l3vni,
494 is_l3vni_for_prefix_routes_only(
495 zvrf->l3vni)
496 ? " prefix-routes-only"
497 : "");
5a0bdc78
PG
498 if (zvrf->zebra_rnh_ip_default_route)
499 vty_out(vty, "ip nht resolve-via-default\n");
500
501 if (zvrf->zebra_rnh_ipv6_default_route)
502 vty_out(vty, "ipv6 nht resolve-via-default\n");
c319e19d
QY
503 } else {
504 vty_frame(vty, "vrf %s\n", zvrf_name(zvrf));
22bd3e94 505 if (zvrf->l3vni)
996c9314
LB
506 vty_out(vty, " vni %u%s\n", zvrf->l3vni,
507 is_l3vni_for_prefix_routes_only(
508 zvrf->l3vni)
509 ? " prefix-routes-only"
510 : "");
b95c1883 511 zebra_ns_config_write(vty, (struct ns *)vrf->ns_ctxt);
5a0bdc78
PG
512 if (zvrf->zebra_rnh_ip_default_route)
513 vty_out(vty, " ip nht resolve-via-default\n");
514
515 if (zvrf->zebra_rnh_ipv6_default_route)
516 vty_out(vty, " ipv6 nht resolve-via-default\n");
22bd3e94 517 }
37728041 518
5a0bdc78 519
7cf16e19 520 zebra_routemap_config_write_protocol(vty, zvrf);
03fba42e 521 router_id_write(vty, zvrf);
7cf16e19 522
c319e19d
QY
523 if (zvrf_id(zvrf) != VRF_DEFAULT)
524 vty_endframe(vty, " exit-vrf\n!\n");
7cf16e19 525 else
526 vty_out(vty, "!\n");
d62a17ae 527 }
528 return 0;
f30c50b9
RW
529}
530
7c551956 531/* Zebra VRF initialization. */
d62a17ae 532void zebra_vrf_init(void)
7c551956 533{
996c9314 534 vrf_init(zebra_vrf_new, zebra_vrf_enable, zebra_vrf_disable,
ecbc5a37 535 zebra_vrf_delete, zebra_vrf_update);
7c551956 536
3bc34908 537 vrf_cmd_init(vrf_config_write, &zserv_privs);
7c551956 538}