1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 CumulusNetworks
6 * This file is part of Quagga
17 #include "srcdest_table.h"
21 #include "zebra/zebra_router.h"
22 #include "zebra/rtadv.h"
23 #include "zebra/debug.h"
24 #include "zebra/zapi_msg.h"
25 #include "zebra/rib.h"
26 #include "zebra/zebra_vrf.h"
27 #include "zebra/zebra_rnh.h"
28 #include "zebra/router-id.h"
29 #include "zebra/interface.h"
30 #include "zebra/zebra_mpls.h"
31 #include "zebra/zebra_vxlan.h"
32 #include "zebra/zebra_netns_notify.h"
33 #include "zebra/zebra_routemap.h"
34 #include "zebra/zebra_vrf_clippy.c"
35 #include "zebra/table_manager.h"
37 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
39 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
40 struct route_node
*node
);
42 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_VRF
, "ZEBRA VRF");
43 DEFINE_MTYPE_STATIC(ZEBRA
, OTHER_TABLE
, "Other Table");
45 /* VRF information update. */
46 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
48 struct listnode
*node
, *nnode
;
51 if (IS_ZEBRA_DEBUG_EVENT
)
52 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
54 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
55 /* Do not send unsolicited messages to synchronous clients. */
56 if (client
->synchronous
)
59 zsend_vrf_add(client
, zvrf
);
63 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
65 struct listnode
*node
, *nnode
;
68 if (IS_ZEBRA_DEBUG_EVENT
)
69 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
71 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
72 /* Do not send unsolicited messages to synchronous clients. */
73 if (client
->synchronous
)
76 zsend_vrf_delete(client
, zvrf
);
80 void zebra_vrf_update_all(struct zserv
*client
)
84 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
85 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
86 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
90 /* Callback upon creating a new VRF. */
91 static int zebra_vrf_new(struct vrf
*vrf
)
93 struct zebra_vrf
*zvrf
;
95 if (IS_ZEBRA_DEBUG_EVENT
)
96 zlog_debug("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
98 zvrf
= zebra_vrf_alloc(vrf
);
99 if (!vrf_is_backend_netns())
100 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
102 otable_init(&zvrf
->other_tables
);
104 router_id_init(zvrf
);
106 /* Initiate Table Manager per ZNS */
107 table_manager_enable(zvrf
);
112 /* Callback upon enabling a VRF. */
113 static int zebra_vrf_enable(struct vrf
*vrf
)
115 struct zebra_vrf
*zvrf
= vrf
->info
;
116 struct route_table
*table
;
121 if (IS_ZEBRA_DEBUG_EVENT
)
122 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
125 if (vrf_is_backend_netns())
126 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
128 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
130 rtadv_vrf_init(zvrf
);
132 /* Inform clients that the VRF is now active. This is an
133 * add for the clients.
136 zebra_vrf_add_update(zvrf
);
137 /* Allocate tables */
138 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
139 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
140 zebra_vrf_table_create(zvrf
, afi
, safi
);
142 table
= route_table_init();
143 table
->cleanup
= zebra_rnhtable_node_cleanup
;
144 zvrf
->rnh_table
[afi
] = table
;
146 table
= route_table_init();
147 table
->cleanup
= zebra_rnhtable_node_cleanup
;
148 zvrf
->rnh_table_multicast
[afi
] = table
;
151 /* Kick off any VxLAN-EVPN processing. */
152 zebra_vxlan_vrf_enable(zvrf
);
157 /* Callback upon disabling a VRF. */
158 static int zebra_vrf_disable(struct vrf
*vrf
)
160 struct zebra_vrf
*zvrf
= vrf
->info
;
161 struct interface
*ifp
;
166 if (IS_ZEBRA_DEBUG_EVENT
)
167 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
170 /* Stop any VxLAN-EVPN processing. */
171 zebra_vxlan_vrf_disable(zvrf
);
173 rtadv_vrf_terminate(zvrf
);
175 /* Inform clients that the VRF is now inactive. This is a
176 * delete for the clients.
178 zebra_vrf_delete_update(zvrf
);
180 /* If asked to retain routes, there's nothing more to do. */
181 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
184 /* Remove all routes. */
185 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
186 route_table_finish(zvrf
->rnh_table
[afi
]);
187 zvrf
->rnh_table
[afi
] = NULL
;
188 route_table_finish(zvrf
->rnh_table_multicast
[afi
]);
189 zvrf
->rnh_table_multicast
[afi
] = NULL
;
191 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
192 rib_close_table(zvrf
->table
[afi
][safi
]);
195 /* Cleanup Vxlan, MPLS and PW tables. */
196 zebra_vxlan_cleanup_tables(zvrf
);
197 zebra_mpls_cleanup_tables(zvrf
);
200 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
202 FOR_ALL_INTERFACES (vrf
, ifp
)
203 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
205 /* clean-up work queues */
206 meta_queue_free(zrouter
.mq
, zvrf
);
208 /* Cleanup (free) routing tables and NHT tables. */
209 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
211 * Set the table pointer to NULL as that
212 * we no-longer need a copy of it, nor do we
213 * own this data, the zebra_router structure
214 * owns these tables. Once we've cleaned up the
215 * table, see rib_close_table above
216 * we no-longer need this pointer.
218 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
219 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
221 zvrf
->table
[afi
][safi
] = NULL
;
228 static int zebra_vrf_delete(struct vrf
*vrf
)
230 struct zebra_vrf
*zvrf
= vrf
->info
;
231 struct other_route_table
*otable
;
234 if (IS_ZEBRA_DEBUG_EVENT
)
235 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
238 table_manager_disable(zvrf
);
240 /* clean-up work queues */
241 meta_queue_free(zrouter
.mq
, zvrf
);
243 /* Free Vxlan and MPLS. */
244 zebra_vxlan_close_tables(zvrf
);
245 zebra_mpls_close_tables(zvrf
);
247 otable
= otable_pop(&zvrf
->other_tables
);
249 zebra_router_release_table(zvrf
, otable
->table_id
,
250 otable
->afi
, otable
->safi
);
251 XFREE(MTYPE_OTHER_TABLE
, otable
);
253 otable
= otable_pop(&zvrf
->other_tables
);
256 /* Cleanup EVPN states for vrf */
257 zebra_vxlan_vrf_delete(zvrf
);
258 zebra_routemap_vrf_delete(zvrf
);
260 list_delete_all_node(zvrf
->rid_all_sorted_list
);
261 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
263 list_delete_all_node(zvrf
->rid6_all_sorted_list
);
264 list_delete_all_node(zvrf
->rid6_lo_sorted_list
);
266 otable_fini(&zvrf
->other_tables
);
267 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
273 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
274 * NOTE: Table-id is relevant on two modes:
275 * - case VRF backend is default : on default VRF only
276 * - case VRF backend is netns : on all VRFs
278 struct route_table
*zebra_vrf_lookup_table_with_table_id(afi_t afi
, safi_t safi
,
282 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
283 struct other_route_table ort
, *otable
;
288 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
291 if (table_id
== zvrf
->table_id
)
292 return zebra_vrf_table(afi
, safi
, vrf_id
);
296 ort
.table_id
= table_id
;
297 otable
= otable_find(&zvrf
->other_tables
, &ort
);
300 return otable
->table
;
305 struct route_table
*zebra_vrf_get_table_with_table_id(afi_t afi
, safi_t safi
,
309 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
310 struct other_route_table
*otable
;
311 struct route_table
*table
;
313 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
319 /* Create it as an `other` table */
320 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
322 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
325 otable
->table_id
= table_id
;
326 otable
->table
= table
;
327 otable_add(&zvrf
->other_tables
, otable
);
333 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
334 struct route_node
*node
)
337 zebra_free_rnh(node
->info
);
341 * Create a routing table for the specific AFI/SAFI in the given VRF.
343 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
346 struct route_node
*rn
;
349 assert(!zvrf
->table
[afi
][safi
]);
351 zvrf
->table
[afi
][safi
] =
352 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
354 memset(&p
, 0, sizeof(p
));
355 p
.family
= afi2family(afi
);
357 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
358 zebra_rib_create_dest(rn
);
361 /* Allocate new zebra VRF. */
362 struct zebra_vrf
*zebra_vrf_alloc(struct vrf
*vrf
)
364 struct zebra_vrf
*zvrf
;
366 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
371 zebra_vxlan_init_tables(zvrf
);
372 zebra_mpls_init_tables(zvrf
);
374 zvrf
->table_id
= RT_TABLE_MAIN
;
375 /* by default table ID is default one */
379 /* Lookup VRF by identifier. */
380 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
382 return vrf_info_lookup(vrf_id
);
385 /* Lookup VRF by name. */
386 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
391 name
= VRF_DEFAULT_NAME
;
393 vrf
= vrf_lookup_by_name(name
);
395 return ((struct zebra_vrf
*)vrf
->info
);
400 /* Lookup the routing table in an enabled VRF. */
401 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
403 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
408 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
411 return zvrf
->table
[afi
][safi
];
414 static int vrf_config_write(struct vty
*vty
)
417 struct zebra_vrf
*zvrf
;
419 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
425 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
427 vty_out(vty
, "vni %u%s\n", zvrf
->l3vni
,
428 is_l3vni_for_prefix_routes_only(
430 ? " prefix-routes-only"
432 if (zvrf
->zebra_rnh_ip_default_route
)
433 vty_out(vty
, "ip nht resolve-via-default\n");
435 if (zvrf
->zebra_rnh_ipv6_default_route
)
436 vty_out(vty
, "ipv6 nht resolve-via-default\n");
439 && (zvrf
->tbl_mgr
->start
|| zvrf
->tbl_mgr
->end
))
440 vty_out(vty
, "ip table range %u %u\n",
441 zvrf
->tbl_mgr
->start
,
444 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
446 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
447 is_l3vni_for_prefix_routes_only(
449 ? " prefix-routes-only"
451 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
452 if (zvrf
->zebra_rnh_ip_default_route
)
453 vty_out(vty
, " ip nht resolve-via-default\n");
455 if (zvrf
->zebra_rnh_ipv6_default_route
)
456 vty_out(vty
, " ipv6 nht resolve-via-default\n");
458 if (zvrf
->tbl_mgr
&& vrf_is_backend_netns()
459 && (zvrf
->tbl_mgr
->start
|| zvrf
->tbl_mgr
->end
))
460 vty_out(vty
, " ip table range %u %u\n",
461 zvrf
->tbl_mgr
->start
,
466 zebra_routemap_config_write_protocol(vty
, zvrf
);
467 router_id_write(vty
, zvrf
);
469 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
470 vty_endframe(vty
, "exit-vrf\n!\n");
479 "netns NAME$netns_name",
480 "Attach VRF to a Namespace\n"
481 "The file name in " NS_RUN_DIR
", or a full pathname\n")
483 char *pathname
= ns_netns_pathname(vty
, netns_name
);
486 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
489 return CMD_WARNING_CONFIG_FAILED
;
491 frr_with_privs(&zserv_privs
) {
492 ret
= zebra_vrf_netns_handler_create(
493 vty
, vrf
, pathname
, NS_UNKNOWN
, NS_UNKNOWN
, NS_UNKNOWN
);
503 "Detach VRF from a Namespace\n"
504 "The file name in " NS_RUN_DIR
", or a full pathname\n")
506 struct ns
*ns
= NULL
;
508 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
510 if (!vrf_is_backend_netns()) {
511 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
512 return CMD_WARNING_CONFIG_FAILED
;
515 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
516 vrf
->name
, vrf
->vrf_id
);
517 return CMD_WARNING_CONFIG_FAILED
;
520 ns
= (struct ns
*)vrf
->ns_ctxt
;
524 /* vrf ID from VRF is necessary for Zebra
525 * so that propagate to other clients is done
532 /* if ns_id is different and not VRF_UNKNOWN,
533 * then update vrf identifier, and enable VRF
535 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
537 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
539 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
543 old_vrf_id
= vrf
->vrf_id
;
544 if (vrf_id
== vrf
->vrf_id
)
546 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
547 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
548 vrf
->vrf_id
= vrf_id
;
549 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
550 if (old_vrf_id
== VRF_UNKNOWN
)
554 int zebra_vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
,
555 char *pathname
, ns_id_t ns_id
,
556 ns_id_t internal_ns_id
,
557 ns_id_t rel_def_ns_id
)
559 struct ns
*ns
= NULL
;
562 return CMD_WARNING_CONFIG_FAILED
;
563 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
566 "VRF %u is already configured with VRF %s\n",
567 vrf
->vrf_id
, vrf
->name
);
569 zlog_info("VRF %u is already configured with VRF %s",
570 vrf
->vrf_id
, vrf
->name
);
571 return CMD_WARNING_CONFIG_FAILED
;
573 if (vrf
->ns_ctxt
!= NULL
) {
574 ns
= (struct ns
*)vrf
->ns_ctxt
;
575 if (!strcmp(ns
->name
, pathname
)) {
578 "VRF %u already configured with NETNS %s\n",
579 vrf
->vrf_id
, ns
->name
);
582 "VRF %u already configured with NETNS %s",
583 vrf
->vrf_id
, ns
->name
);
587 ns
= ns_lookup_name(pathname
);
588 if (ns
&& ns
->vrf_ctxt
) {
589 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
595 "NS %s is already configured with VRF %u(%s)\n",
596 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
598 zlog_info("NS %s is already configured with VRF %u(%s)",
599 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
600 return CMD_WARNING_CONFIG_FAILED
;
602 ns
= ns_get_created(ns
, pathname
, ns_id
);
603 ns
->internal_ns_id
= internal_ns_id
;
604 ns
->relative_default_ns
= rel_def_ns_id
;
605 ns
->vrf_ctxt
= (void *)vrf
;
606 vrf
->ns_ctxt
= (void *)ns
;
607 /* update VRF netns NAME */
608 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
610 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
612 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
613 ns
->ns_id
, ns
->name
);
615 zlog_info("Can not associate NS %u with NETNS %s",
616 ns
->ns_id
, ns
->name
);
617 return CMD_WARNING_CONFIG_FAILED
;
623 /* Zebra VRF initialization. */
624 void zebra_vrf_init(void)
626 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
629 hook_register(zserv_client_close
, release_daemon_table_chunks
);
631 vrf_cmd_init(vrf_config_write
);
633 if (vrf_is_backend_netns() && ns_have_netns()) {
634 /* Install NS commands. */
635 install_element(VRF_NODE
, &vrf_netns_cmd
);
636 install_element(VRF_NODE
, &no_vrf_netns_cmd
);