2 * Copyright (C) 2016 CumulusNetworks
5 * This file is part of Quagga
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
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.
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
30 #include "srcdest_table.h"
34 #include "zebra/zebra_router.h"
35 #include "zebra/rtadv.h"
36 #include "zebra/debug.h"
37 #include "zebra/zapi_msg.h"
38 #include "zebra/rib.h"
39 #include "zebra/zebra_vrf.h"
40 #include "zebra/zebra_rnh.h"
41 #include "zebra/router-id.h"
42 #include "zebra/interface.h"
43 #include "zebra/zebra_mpls.h"
44 #include "zebra/zebra_vxlan.h"
45 #include "zebra/zebra_netns_notify.h"
46 #include "zebra/zebra_routemap.h"
47 #ifndef VTYSH_EXTRACT_PL
48 #include "zebra/zebra_vrf_clippy.c"
50 #include "zebra/table_manager.h"
52 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
54 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
55 struct route_node
*node
);
57 DEFINE_MTYPE_STATIC(ZEBRA
, ZEBRA_VRF
, "ZEBRA VRF");
58 DEFINE_MTYPE_STATIC(ZEBRA
, OTHER_TABLE
, "Other Table");
60 /* VRF information update. */
61 static void zebra_vrf_add_update(struct zebra_vrf
*zvrf
)
63 struct listnode
*node
, *nnode
;
66 if (IS_ZEBRA_DEBUG_EVENT
)
67 zlog_debug("MESSAGE: ZEBRA_VRF_ADD %s", zvrf_name(zvrf
));
69 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
70 /* Do not send unsolicited messages to synchronous clients. */
71 if (client
->synchronous
)
74 zsend_vrf_add(client
, zvrf
);
78 static void zebra_vrf_delete_update(struct zebra_vrf
*zvrf
)
80 struct listnode
*node
, *nnode
;
83 if (IS_ZEBRA_DEBUG_EVENT
)
84 zlog_debug("MESSAGE: ZEBRA_VRF_DELETE %s", zvrf_name(zvrf
));
86 for (ALL_LIST_ELEMENTS(zrouter
.client_list
, node
, nnode
, client
)) {
87 /* Do not send unsolicited messages to synchronous clients. */
88 if (client
->synchronous
)
91 zsend_vrf_delete(client
, zvrf
);
95 void zebra_vrf_update_all(struct zserv
*client
)
99 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
100 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
101 zsend_vrf_add(client
, vrf_info_lookup(vrf
->vrf_id
));
105 /* Callback upon creating a new VRF. */
106 static int zebra_vrf_new(struct vrf
*vrf
)
108 struct zebra_vrf
*zvrf
;
110 if (IS_ZEBRA_DEBUG_EVENT
)
111 zlog_debug("VRF %s created, id %u", vrf
->name
, vrf
->vrf_id
);
113 zvrf
= zebra_vrf_alloc(vrf
);
114 if (!vrf_is_backend_netns())
115 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
117 otable_init(&zvrf
->other_tables
);
119 router_id_init(zvrf
);
121 /* Initiate Table Manager per ZNS */
122 table_manager_enable(zvrf
);
127 /* Callback upon enabling a VRF. */
128 static int zebra_vrf_enable(struct vrf
*vrf
)
130 struct zebra_vrf
*zvrf
= vrf
->info
;
131 struct route_table
*table
;
136 if (IS_ZEBRA_DEBUG_EVENT
)
137 zlog_debug("VRF %s id %u is now active", zvrf_name(zvrf
),
140 if (vrf_is_backend_netns())
141 zvrf
->zns
= zebra_ns_lookup((ns_id_t
)vrf
->vrf_id
);
143 zvrf
->zns
= zebra_ns_lookup(NS_DEFAULT
);
144 #if defined(HAVE_RTADV)
145 rtadv_vrf_init(zvrf
);
148 /* Inform clients that the VRF is now active. This is an
149 * add for the clients.
152 zebra_vrf_add_update(zvrf
);
153 /* Allocate tables */
154 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
155 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
156 zebra_vrf_table_create(zvrf
, afi
, safi
);
158 table
= route_table_init();
159 table
->cleanup
= zebra_rnhtable_node_cleanup
;
160 zvrf
->rnh_table
[afi
] = table
;
162 table
= route_table_init();
163 table
->cleanup
= zebra_rnhtable_node_cleanup
;
164 zvrf
->rnh_table_multicast
[afi
] = table
;
167 /* Kick off any VxLAN-EVPN processing. */
168 zebra_vxlan_vrf_enable(zvrf
);
173 /* Callback upon disabling a VRF. */
174 static int zebra_vrf_disable(struct vrf
*vrf
)
176 struct zebra_vrf
*zvrf
= vrf
->info
;
177 struct interface
*ifp
;
182 if (IS_ZEBRA_DEBUG_EVENT
)
183 zlog_debug("VRF %s id %u is now inactive", zvrf_name(zvrf
),
186 /* Stop any VxLAN-EVPN processing. */
187 zebra_vxlan_vrf_disable(zvrf
);
189 #if defined(HAVE_RTADV)
190 rtadv_vrf_terminate(zvrf
);
193 /* Inform clients that the VRF is now inactive. This is a
194 * delete for the clients.
196 zebra_vrf_delete_update(zvrf
);
198 /* If asked to retain routes, there's nothing more to do. */
199 if (CHECK_FLAG(zvrf
->flags
, ZEBRA_VRF_RETAIN
))
202 /* Remove all routes. */
203 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
204 route_table_finish(zvrf
->rnh_table
[afi
]);
205 zvrf
->rnh_table
[afi
] = NULL
;
206 route_table_finish(zvrf
->rnh_table_multicast
[afi
]);
207 zvrf
->rnh_table_multicast
[afi
] = NULL
;
209 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++)
210 rib_close_table(zvrf
->table
[afi
][safi
]);
213 /* Cleanup Vxlan, MPLS and PW tables. */
214 zebra_vxlan_cleanup_tables(zvrf
);
215 zebra_mpls_cleanup_tables(zvrf
);
218 /* Remove link-local IPv4 addresses created for BGP unnumbered peering.
220 FOR_ALL_INTERFACES (vrf
, ifp
)
221 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
223 /* clean-up work queues */
224 rib_meta_queue_free_vrf(zrouter
.mq
, zvrf
);
226 /* Cleanup (free) routing tables and NHT tables. */
227 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
229 * Set the table pointer to NULL as that
230 * we no-longer need a copy of it, nor do we
231 * own this data, the zebra_router structure
232 * owns these tables. Once we've cleaned up the
233 * table, see rib_close_table above
234 * we no-longer need this pointer.
236 for (safi
= SAFI_UNICAST
; safi
<= SAFI_MULTICAST
; safi
++) {
237 zebra_router_release_table(zvrf
, zvrf
->table_id
, afi
,
239 zvrf
->table
[afi
][safi
] = NULL
;
246 static int zebra_vrf_delete(struct vrf
*vrf
)
248 struct zebra_vrf
*zvrf
= vrf
->info
;
249 struct other_route_table
*otable
;
252 if (IS_ZEBRA_DEBUG_EVENT
)
253 zlog_debug("VRF %s id %u deleted", zvrf_name(zvrf
),
256 table_manager_disable(zvrf
);
258 /* clean-up work queues */
259 rib_meta_queue_free_vrf(zrouter
.mq
, zvrf
);
261 /* Free Vxlan and MPLS. */
262 zebra_vxlan_close_tables(zvrf
);
263 zebra_mpls_close_tables(zvrf
);
265 otable
= otable_pop(&zvrf
->other_tables
);
267 zebra_router_release_table(zvrf
, otable
->table_id
,
268 otable
->afi
, otable
->safi
);
269 XFREE(MTYPE_OTHER_TABLE
, otable
);
271 otable
= otable_pop(&zvrf
->other_tables
);
274 /* Cleanup EVPN states for vrf */
275 zebra_vxlan_vrf_delete(zvrf
);
277 list_delete_all_node(zvrf
->rid_all_sorted_list
);
278 list_delete_all_node(zvrf
->rid_lo_sorted_list
);
280 list_delete_all_node(zvrf
->rid6_all_sorted_list
);
281 list_delete_all_node(zvrf
->rid6_lo_sorted_list
);
283 otable_fini(&zvrf
->other_tables
);
284 XFREE(MTYPE_ZEBRA_VRF
, zvrf
);
290 /* Lookup the routing table in a VRF based on both VRF-Id and table-id.
291 * NOTE: Table-id is relevant on two modes:
292 * - case VRF backend is default : on default VRF only
293 * - case VRF backend is netns : on all VRFs
295 struct route_table
*zebra_vrf_lookup_table_with_table_id(afi_t afi
, safi_t safi
,
299 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
300 struct other_route_table ort
, *otable
;
305 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
308 if (table_id
== zvrf
->table_id
)
309 return zebra_vrf_table(afi
, safi
, vrf_id
);
313 ort
.table_id
= table_id
;
314 otable
= otable_find(&zvrf
->other_tables
, &ort
);
317 return otable
->table
;
322 struct route_table
*zebra_vrf_get_table_with_table_id(afi_t afi
, safi_t safi
,
326 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
327 struct other_route_table
*otable
;
328 struct route_table
*table
;
330 table
= zebra_vrf_lookup_table_with_table_id(afi
, safi
, vrf_id
,
336 /* Create it as an `other` table */
337 table
= zebra_router_get_table(zvrf
, table_id
, afi
, safi
);
339 otable
= XCALLOC(MTYPE_OTHER_TABLE
, sizeof(*otable
));
342 otable
->table_id
= table_id
;
343 otable
->table
= table
;
344 otable_add(&zvrf
->other_tables
, otable
);
350 static void zebra_rnhtable_node_cleanup(struct route_table
*table
,
351 struct route_node
*node
)
354 zebra_free_rnh(node
->info
);
358 * Create a routing table for the specific AFI/SAFI in the given VRF.
360 static void zebra_vrf_table_create(struct zebra_vrf
*zvrf
, afi_t afi
,
363 struct route_node
*rn
;
366 assert(!zvrf
->table
[afi
][safi
]);
368 zvrf
->table
[afi
][safi
] =
369 zebra_router_get_table(zvrf
, zvrf
->table_id
, afi
, safi
);
371 memset(&p
, 0, sizeof(p
));
372 p
.family
= afi2family(afi
);
374 rn
= srcdest_rnode_get(zvrf
->table
[afi
][safi
], &p
, NULL
);
375 zebra_rib_create_dest(rn
);
378 /* Allocate new zebra VRF. */
379 struct zebra_vrf
*zebra_vrf_alloc(struct vrf
*vrf
)
381 struct zebra_vrf
*zvrf
;
383 zvrf
= XCALLOC(MTYPE_ZEBRA_VRF
, sizeof(struct zebra_vrf
));
388 zebra_vxlan_init_tables(zvrf
);
389 zebra_mpls_init_tables(zvrf
);
391 zvrf
->table_id
= RT_TABLE_MAIN
;
392 /* by default table ID is default one */
396 /* Lookup VRF by identifier. */
397 struct zebra_vrf
*zebra_vrf_lookup_by_id(vrf_id_t vrf_id
)
399 return vrf_info_lookup(vrf_id
);
402 /* Lookup VRF by name. */
403 struct zebra_vrf
*zebra_vrf_lookup_by_name(const char *name
)
408 name
= VRF_DEFAULT_NAME
;
410 vrf
= vrf_lookup_by_name(name
);
412 return ((struct zebra_vrf
*)vrf
->info
);
417 /* Lookup the routing table in an enabled VRF. */
418 struct route_table
*zebra_vrf_table(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
)
420 struct zebra_vrf
*zvrf
= vrf_info_lookup(vrf_id
);
425 if (afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
428 return zvrf
->table
[afi
][safi
];
431 static int vrf_config_write(struct vty
*vty
)
434 struct zebra_vrf
*zvrf
;
436 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
442 if (zvrf_id(zvrf
) == VRF_DEFAULT
) {
444 vty_out(vty
, "vni %u%s\n", zvrf
->l3vni
,
445 is_l3vni_for_prefix_routes_only(
447 ? " prefix-routes-only"
449 if (zvrf
->zebra_rnh_ip_default_route
)
450 vty_out(vty
, "ip nht resolve-via-default\n");
452 if (zvrf
->zebra_rnh_ipv6_default_route
)
453 vty_out(vty
, "ipv6 nht resolve-via-default\n");
456 && (zvrf
->tbl_mgr
->start
|| zvrf
->tbl_mgr
->end
))
457 vty_out(vty
, "ip table range %u %u\n",
458 zvrf
->tbl_mgr
->start
,
461 vty_frame(vty
, "vrf %s\n", zvrf_name(zvrf
));
463 vty_out(vty
, " vni %u%s\n", zvrf
->l3vni
,
464 is_l3vni_for_prefix_routes_only(
466 ? " prefix-routes-only"
468 zebra_ns_config_write(vty
, (struct ns
*)vrf
->ns_ctxt
);
469 if (zvrf
->zebra_rnh_ip_default_route
)
470 vty_out(vty
, " ip nht resolve-via-default\n");
472 if (zvrf
->zebra_rnh_ipv6_default_route
)
473 vty_out(vty
, " ipv6 nht resolve-via-default\n");
475 if (zvrf
->tbl_mgr
&& vrf_is_backend_netns()
476 && (zvrf
->tbl_mgr
->start
|| zvrf
->tbl_mgr
->end
))
477 vty_out(vty
, " ip table range %u %u\n",
478 zvrf
->tbl_mgr
->start
,
483 zebra_routemap_config_write_protocol(vty
, zvrf
);
484 router_id_write(vty
, zvrf
);
486 if (zvrf_id(zvrf
) != VRF_DEFAULT
)
487 vty_endframe(vty
, "exit-vrf\n!\n");
496 "netns NAME$netns_name",
497 "Attach VRF to a Namespace\n"
498 "The file name in " NS_RUN_DIR
", or a full pathname\n")
500 char *pathname
= ns_netns_pathname(vty
, netns_name
);
503 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
506 return CMD_WARNING_CONFIG_FAILED
;
508 frr_with_privs(&zserv_privs
) {
509 ret
= zebra_vrf_netns_handler_create(
510 vty
, vrf
, pathname
, NS_UNKNOWN
, NS_UNKNOWN
, NS_UNKNOWN
);
520 "Detach VRF from a Namespace\n"
521 "The file name in " NS_RUN_DIR
", or a full pathname\n")
523 struct ns
*ns
= NULL
;
525 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
527 if (!vrf_is_backend_netns()) {
528 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
529 return CMD_WARNING_CONFIG_FAILED
;
532 vty_out(vty
, "VRF %s(%u) is not configured with NetNS\n",
533 vrf
->name
, vrf
->vrf_id
);
534 return CMD_WARNING_CONFIG_FAILED
;
537 ns
= (struct ns
*)vrf
->ns_ctxt
;
541 /* vrf ID from VRF is necessary for Zebra
542 * so that propagate to other clients is done
549 /* if ns_id is different and not VRF_UNKNOWN,
550 * then update vrf identifier, and enable VRF
552 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
554 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
556 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
560 old_vrf_id
= vrf
->vrf_id
;
561 if (vrf_id
== vrf
->vrf_id
)
563 if (vrf
->vrf_id
!= VRF_UNKNOWN
)
564 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
565 vrf
->vrf_id
= vrf_id
;
566 RB_INSERT(vrf_id_head
, &vrfs_by_id
, vrf
);
567 if (old_vrf_id
== VRF_UNKNOWN
)
571 int zebra_vrf_netns_handler_create(struct vty
*vty
, struct vrf
*vrf
,
572 char *pathname
, ns_id_t ns_id
,
573 ns_id_t internal_ns_id
,
574 ns_id_t rel_def_ns_id
)
576 struct ns
*ns
= NULL
;
579 return CMD_WARNING_CONFIG_FAILED
;
580 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
583 "VRF %u is already configured with VRF %s\n",
584 vrf
->vrf_id
, vrf
->name
);
586 zlog_info("VRF %u is already configured with VRF %s",
587 vrf
->vrf_id
, vrf
->name
);
588 return CMD_WARNING_CONFIG_FAILED
;
590 if (vrf
->ns_ctxt
!= NULL
) {
591 ns
= (struct ns
*)vrf
->ns_ctxt
;
592 if (!strcmp(ns
->name
, pathname
)) {
595 "VRF %u already configured with NETNS %s\n",
596 vrf
->vrf_id
, ns
->name
);
599 "VRF %u already configured with NETNS %s",
600 vrf
->vrf_id
, ns
->name
);
601 return CMD_WARNING_CONFIG_FAILED
;
604 ns
= ns_lookup_name(pathname
);
605 if (ns
&& ns
->vrf_ctxt
) {
606 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
612 "NS %s is already configured with VRF %u(%s)\n",
613 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
615 zlog_info("NS %s is already configured with VRF %u(%s)",
616 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
617 return CMD_WARNING_CONFIG_FAILED
;
619 ns
= ns_get_created(ns
, pathname
, ns_id
);
620 ns
->internal_ns_id
= internal_ns_id
;
621 ns
->relative_default_ns
= rel_def_ns_id
;
622 ns
->vrf_ctxt
= (void *)vrf
;
623 vrf
->ns_ctxt
= (void *)ns
;
624 /* update VRF netns NAME */
625 strlcpy(vrf
->data
.l
.netns_name
, basename(pathname
), NS_NAMSIZ
);
627 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
629 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
630 ns
->ns_id
, ns
->name
);
632 zlog_info("Can not associate NS %u with NETNS %s",
633 ns
->ns_id
, ns
->name
);
634 return CMD_WARNING_CONFIG_FAILED
;
640 /* Zebra VRF initialization. */
641 void zebra_vrf_init(void)
643 vrf_init(zebra_vrf_new
, zebra_vrf_enable
, zebra_vrf_disable
,
646 hook_register(zserv_client_close
, release_daemon_table_chunks
);
648 vrf_cmd_init(vrf_config_write
);
650 if (vrf_is_backend_netns() && ns_have_netns()) {
651 /* Install NS commands. */
652 install_element(VRF_NODE
, &vrf_netns_cmd
);
653 install_element(VRF_NODE
, &no_vrf_netns_cmd
);